blob: b482a0329942b0d05f7d682e7f168b3710150601 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000029
Chris Lattner6d9a6852006-10-25 05:11:20 +000030using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000031
Chris Lattner88f70d62008-03-15 05:43:15 +000032//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000033// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000034//===----------------------------------------------------------------------===//
35
John McCall659a3372010-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 McCallb7139c42010-10-28 04:18:25 +000056 }
John McCall659a3372010-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 McCallb7139c42010-10-28 04:18:25 +000071}
72
John McCall457a04e2010-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 McCallc273f242010-10-30 11:50:40 +000085typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall457a04e2010-10-22 21:05:15 +000086typedef std::pair<Linkage,Visibility> LVPair;
John McCallc273f242010-10-30 11:50:40 +000087
John McCall457a04e2010-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 McCallc273f242010-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 Kramer396dcf32010-11-05 19:56:37 +000098namespace {
John McCall07072662010-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 Gregorbf62d642010-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 McCall07072662010-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 Gregor91df6cf2010-12-06 18:50:56 +0000125};
Benjamin Kramer396dcf32010-11-05 19:56:37 +0000126} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +0000127
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000128/// \brief Get the most restrictive linkage for the types in the given
129/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +0000130static LVPair
131getLVForTemplateParameterList(const TemplateParameterList *Params) {
132 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000133 for (TemplateParameterList::const_iterator P = Params->begin(),
134 PEnd = Params->end();
135 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000136 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
137 if (NTTP->isExpandedParameterPack()) {
138 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
139 QualType T = NTTP->getExpansionType(I);
140 if (!T->isDependentType())
141 LV = merge(LV, T->getLinkageAndVisibility());
142 }
143 continue;
144 }
145
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000146 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +0000147 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000148 continue;
149 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000150 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000151
152 if (TemplateTemplateParmDecl *TTP
153 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +0000154 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000155 }
156 }
157
John McCall457a04e2010-10-22 21:05:15 +0000158 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000159}
160
Douglas Gregorbf62d642010-12-06 18:36:25 +0000161/// getLVForDecl - Get the linkage and visibility for the given declaration.
162static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
163
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000164/// \brief Get the most restrictive linkage for the types and
165/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +0000166static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregorbf62d642010-12-06 18:36:25 +0000167 unsigned NumArgs,
168 LVFlags &F) {
John McCall457a04e2010-10-22 21:05:15 +0000169 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000170
171 for (unsigned I = 0; I != NumArgs; ++I) {
172 switch (Args[I].getKind()) {
173 case TemplateArgument::Null:
174 case TemplateArgument::Integral:
175 case TemplateArgument::Expression:
176 break;
177
178 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000179 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000180 break;
181
182 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000183 // The decl can validly be null as the representation of nullptr
184 // arguments, valid only in C++0x.
185 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000186 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
187 LV = merge(LV, getLVForDecl(ND, F));
John McCall457a04e2010-10-22 21:05:15 +0000188 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000189 break;
190
191 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000192 case TemplateArgument::TemplateExpansion:
193 if (TemplateDecl *Template
194 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000195 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000196 break;
197
198 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000199 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregorbf62d642010-12-06 18:36:25 +0000200 Args[I].pack_size(),
201 F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000202 break;
203 }
204 }
205
John McCall457a04e2010-10-22 21:05:15 +0000206 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000207}
208
John McCallc273f242010-10-30 11:50:40 +0000209static LVPair
Douglas Gregorbf62d642010-12-06 18:36:25 +0000210getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
211 LVFlags &F) {
212 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall8823c652010-08-13 08:35:10 +0000213}
214
John McCall07072662010-11-02 01:45:15 +0000215static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000216 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000217 "Not a name having namespace scope");
218 ASTContext &Context = D->getASTContext();
219
220 // C++ [basic.link]p3:
221 // A name having namespace scope (3.3.6) has internal linkage if it
222 // is the name of
223 // - an object, reference, function or function template that is
224 // explicitly declared static; or,
225 // (This bullet corresponds to C99 6.2.2p3.)
226 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
227 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000228 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000229 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230
231 // - an object or reference that is explicitly declared const
232 // and neither explicitly declared extern nor previously
233 // declared to have external linkage; or
234 // (there is no equivalent in C99)
235 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000236 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000237 Var->getStorageClass() != SC_Extern &&
238 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000239 bool FoundExtern = false;
240 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
241 PrevVar && !FoundExtern;
242 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000243 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000244 FoundExtern = true;
245
246 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000247 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000248 }
249 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000250 // C++ [temp]p4:
251 // A non-member function template can have internal linkage; any
252 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000253 const FunctionDecl *Function = 0;
254 if (const FunctionTemplateDecl *FunTmpl
255 = dyn_cast<FunctionTemplateDecl>(D))
256 Function = FunTmpl->getTemplatedDecl();
257 else
258 Function = cast<FunctionDecl>(D);
259
260 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000261 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000262 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000263 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
264 // - a data member of an anonymous union.
265 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000266 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000267 }
268
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000269 if (D->isInAnonymousNamespace()) {
270 const VarDecl *Var = dyn_cast<VarDecl>(D);
271 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
272 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
273 return LinkageInfo::uniqueExternal();
274 }
John McCallb7139c42010-10-28 04:18:25 +0000275
John McCall457a04e2010-10-22 21:05:15 +0000276 // Set up the defaults.
277
278 // C99 6.2.2p5:
279 // If the declaration of an identifier for an object has file
280 // scope and no storage-class specifier, its linkage is
281 // external.
John McCallc273f242010-10-30 11:50:40 +0000282 LinkageInfo LV;
283
John McCall07072662010-11-02 01:45:15 +0000284 if (F.ConsiderVisibilityAttributes) {
285 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
286 LV.setVisibility(GetVisibilityFromAttr(VA), true);
287 F.ConsiderGlobalVisibility = false;
John McCall2faf32c2010-12-10 02:59:44 +0000288 } else {
289 // If we're declared in a namespace with a visibility attribute,
290 // use that namespace's visibility, but don't call it explicit.
291 for (const DeclContext *DC = D->getDeclContext();
292 !isa<TranslationUnitDecl>(DC);
293 DC = DC->getParent()) {
294 if (!isa<NamespaceDecl>(DC)) continue;
295 if (const VisibilityAttr *VA =
296 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
297 LV.setVisibility(GetVisibilityFromAttr(VA), false);
298 F.ConsiderGlobalVisibility = false;
299 break;
300 }
301 }
John McCall07072662010-11-02 01:45:15 +0000302 }
John McCallc273f242010-10-30 11:50:40 +0000303 }
John McCall457a04e2010-10-22 21:05:15 +0000304
Douglas Gregorf73b2822009-11-25 22:24:25 +0000305 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000306
Douglas Gregorf73b2822009-11-25 22:24:25 +0000307 // A name having namespace scope has external linkage if it is the
308 // name of
309 //
310 // - an object or reference, unless it has internal linkage; or
311 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000312 // GCC applies the following optimization to variables and static
313 // data members, but not to functions:
314 //
John McCall457a04e2010-10-22 21:05:15 +0000315 // Modify the variable's LV by the LV of its type unless this is
316 // C or extern "C". This follows from [basic.link]p9:
317 // A type without linkage shall not be used as the type of a
318 // variable or function with external linkage unless
319 // - the entity has C language linkage, or
320 // - the entity is declared within an unnamed namespace, or
321 // - the entity is not used or is defined in the same
322 // translation unit.
323 // and [basic.link]p10:
324 // ...the types specified by all declarations referring to a
325 // given variable or function shall be identical...
326 // C does not have an equivalent rule.
327 //
John McCall5fe84122010-10-26 04:59:26 +0000328 // Ignore this if we've got an explicit attribute; the user
329 // probably knows what they're doing.
330 //
John McCall457a04e2010-10-22 21:05:15 +0000331 // Note that we don't want to make the variable non-external
332 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000333 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000334 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
335 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000336 return LinkageInfo::uniqueExternal();
337 if (!LV.visibilityExplicit())
338 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000339 }
340
John McCall23032652010-11-02 18:38:13 +0000341 if (Var->getStorageClass() == SC_PrivateExtern)
342 LV.setVisibility(HiddenVisibility, true);
343
Douglas Gregorf73b2822009-11-25 22:24:25 +0000344 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000345 (Var->getStorageClass() == SC_Extern ||
346 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000347
Douglas Gregorf73b2822009-11-25 22:24:25 +0000348 // C99 6.2.2p4:
349 // For an identifier declared with the storage-class specifier
350 // extern in a scope in which a prior declaration of that
351 // identifier is visible, if the prior declaration specifies
352 // internal or external linkage, the linkage of the identifier
353 // at the later declaration is the same as the linkage
354 // specified at the prior declaration. If no prior declaration
355 // is visible, or if the prior declaration specifies no
356 // linkage, then the identifier has external linkage.
357 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000358 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallc273f242010-10-30 11:50:40 +0000359 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
360 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000361 }
362 }
363
Douglas Gregorf73b2822009-11-25 22:24:25 +0000364 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000365 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000366 // In theory, we can modify the function's LV by the LV of its
367 // type unless it has C linkage (see comment above about variables
368 // for justification). In practice, GCC doesn't do this, so it's
369 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000370
John McCall23032652010-11-02 18:38:13 +0000371 if (Function->getStorageClass() == SC_PrivateExtern)
372 LV.setVisibility(HiddenVisibility, true);
373
Douglas Gregorf73b2822009-11-25 22:24:25 +0000374 // C99 6.2.2p5:
375 // If the declaration of an identifier for a function has no
376 // storage-class specifier, its linkage is determined exactly
377 // as if it were declared with the storage-class specifier
378 // extern.
379 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000380 (Function->getStorageClass() == SC_Extern ||
381 Function->getStorageClass() == SC_PrivateExtern ||
382 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000383 // C99 6.2.2p4:
384 // For an identifier declared with the storage-class specifier
385 // extern in a scope in which a prior declaration of that
386 // identifier is visible, if the prior declaration specifies
387 // internal or external linkage, the linkage of the identifier
388 // at the later declaration is the same as the linkage
389 // specified at the prior declaration. If no prior declaration
390 // is visible, or if the prior declaration specifies no
391 // linkage, then the identifier has external linkage.
392 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000393 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallc273f242010-10-30 11:50:40 +0000394 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
395 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000396 }
397 }
398
John McCallf768aa72011-02-10 06:50:24 +0000399 // In C++, then if the type of the function uses a type with
400 // unique-external linkage, it's not legally usable from outside
401 // this translation unit. However, we should use the C linkage
402 // rules instead for extern "C" declarations.
403 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
404 Function->getType()->getLinkage() == UniqueExternalLinkage)
405 return LinkageInfo::uniqueExternal();
406
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000407 if (FunctionTemplateSpecializationInfo *SpecInfo
408 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000409 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
410 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000411 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000412 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000413 }
414
Douglas Gregorf73b2822009-11-25 22:24:25 +0000415 // - a named class (Clause 9), or an unnamed class defined in a
416 // typedef declaration in which the class has the typedef name
417 // for linkage purposes (7.1.3); or
418 // - a named enumeration (7.2), or an unnamed enumeration
419 // defined in a typedef declaration in which the enumeration
420 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000421 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
422 // Unnamed tags have no linkage.
423 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000424 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000425
John McCall457a04e2010-10-22 21:05:15 +0000426 // If this is a class template specialization, consider the
427 // linkage of the template and template arguments.
428 if (const ClassTemplateSpecializationDecl *Spec
429 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000430 // From the template.
431 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
432 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000433
John McCall457a04e2010-10-22 21:05:15 +0000434 // The arguments at which the template was instantiated.
435 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000436 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000437 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000438
John McCall5fe84122010-10-26 04:59:26 +0000439 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000440 if (F.ConsiderGlobalVisibility)
441 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000442 (Context.getLangOptions().CPlusPlus &&
443 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000444
Douglas Gregorf73b2822009-11-25 22:24:25 +0000445 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000446 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000447 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000448 if (!isExternalLinkage(EnumLV.linkage()))
449 return LinkageInfo::none();
450 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000451
452 // - a template, unless it is a function template that has
453 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000454 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000455 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000456
Douglas Gregorf73b2822009-11-25 22:24:25 +0000457 // - a namespace (7.3), unless it is declared within an unnamed
458 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000459 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
460 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000461
John McCall457a04e2010-10-22 21:05:15 +0000462 // By extension, we assign external linkage to Objective-C
463 // interfaces.
464 } else if (isa<ObjCInterfaceDecl>(D)) {
465 // fallout
466
467 // Everything not covered here has no linkage.
468 } else {
John McCallc273f242010-10-30 11:50:40 +0000469 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000470 }
471
472 // If we ended up with non-external linkage, visibility should
473 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000474 if (LV.linkage() != ExternalLinkage)
475 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000476
477 // If we didn't end up with hidden visibility, consider attributes
478 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000479 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000480 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000481
482 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000483}
484
John McCall07072662010-11-02 01:45:15 +0000485static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000486 // Only certain class members have linkage. Note that fields don't
487 // really have linkage, but it's convenient to say they do for the
488 // purposes of calculating linkage of pointer-to-data-member
489 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000490 if (!(isa<CXXMethodDecl>(D) ||
491 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000492 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000493 (isa<TagDecl>(D) &&
494 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000495 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000496
John McCall07072662010-11-02 01:45:15 +0000497 LinkageInfo LV;
498
499 // The flags we're going to use to compute the class's visibility.
500 LVFlags ClassF = F;
501
502 // If we have an explicit visibility attribute, merge that in.
503 if (F.ConsiderVisibilityAttributes) {
504 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
505 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
506
507 // Ignore global visibility later, but not this attribute.
508 F.ConsiderGlobalVisibility = false;
509
510 // Ignore both global visibility and attributes when computing our
511 // parent's visibility.
512 ClassF = F.onlyTemplateVisibility();
513 }
514 }
John McCallc273f242010-10-30 11:50:40 +0000515
516 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000517 // linkage.
518 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
519 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000520 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000521
522 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000523 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000524 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000525
John McCall8823c652010-08-13 08:35:10 +0000526 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000527 // If the type of the function uses a type with unique-external
528 // linkage, it's not legally usable from outside this translation unit.
529 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
530 return LinkageInfo::uniqueExternal();
531
John McCall37bb6c92010-10-29 22:22:43 +0000532 TemplateSpecializationKind TSK = TSK_Undeclared;
533
John McCall457a04e2010-10-22 21:05:15 +0000534 // If this is a method template specialization, use the linkage for
535 // the template parameters and arguments.
536 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000537 = MD->getTemplateSpecializationInfo()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000538 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallc273f242010-10-30 11:50:40 +0000539 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000540 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000541
542 TSK = Spec->getTemplateSpecializationKind();
543 } else if (MemberSpecializationInfo *MSI =
544 MD->getMemberSpecializationInfo()) {
545 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000546 }
547
John McCall37bb6c92010-10-29 22:22:43 +0000548 // If we're paying attention to global visibility, apply
549 // -finline-visibility-hidden if this is an inline method.
550 //
John McCallc273f242010-10-30 11:50:40 +0000551 // Note that ConsiderGlobalVisibility doesn't yet have information
552 // about whether containing classes have visibility attributes,
553 // and that's intentional.
554 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000555 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000556 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
557 // InlineVisibilityHidden only applies to definitions, and
558 // isInlined() only gives meaningful answers on definitions
559 // anyway.
560 const FunctionDecl *Def = 0;
561 if (MD->hasBody(Def) && Def->isInlined())
562 LV.setVisibility(HiddenVisibility);
563 }
John McCall457a04e2010-10-22 21:05:15 +0000564
John McCall37bb6c92010-10-29 22:22:43 +0000565 // Note that in contrast to basically every other situation, we
566 // *do* apply -fvisibility to method declarations.
567
568 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000569 if (const ClassTemplateSpecializationDecl *Spec
570 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
571 // Merge template argument/parameter information for member
572 // class template specializations.
Douglas Gregorbf62d642010-12-06 18:36:25 +0000573 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallc273f242010-10-30 11:50:40 +0000574 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000575 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000576 }
577
John McCall37bb6c92010-10-29 22:22:43 +0000578 // Static data members.
579 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000580 // Modify the variable's linkage by its type, but ignore the
581 // type's visibility unless it's a definition.
582 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
583 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000584 LV.mergeLinkage(UniqueExternalLinkage);
585 if (!LV.visibilityExplicit())
586 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000587 }
588
John McCall07072662010-11-02 01:45:15 +0000589 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000590
591 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000592 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000593 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000594 }
595
John McCall457a04e2010-10-22 21:05:15 +0000596 return LV;
John McCall8823c652010-08-13 08:35:10 +0000597}
598
John McCalld396b972011-02-08 19:01:05 +0000599static void clearLinkageForClass(const CXXRecordDecl *record) {
600 for (CXXRecordDecl::decl_iterator
601 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
602 Decl *child = *i;
603 if (isa<NamedDecl>(child))
604 cast<NamedDecl>(child)->ClearLinkageCache();
605 }
606}
607
608void NamedDecl::ClearLinkageCache() {
609 // Note that we can't skip clearing the linkage of children just
610 // because the parent doesn't have cached linkage: we don't cache
611 // when computing linkage for parent contexts.
612
613 HasCachedLinkage = 0;
614
615 // If we're changing the linkage of a class, we need to reset the
616 // linkage of child declarations, too.
617 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
618 clearLinkageForClass(record);
619
John McCall83779672011-02-19 02:53:41 +0000620 if (ClassTemplateDecl *temp =
621 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000622 // Clear linkage for the template pattern.
623 CXXRecordDecl *record = temp->getTemplatedDecl();
624 record->HasCachedLinkage = 0;
625 clearLinkageForClass(record);
626
John McCall83779672011-02-19 02:53:41 +0000627 // We need to clear linkage for specializations, too.
628 for (ClassTemplateDecl::spec_iterator
629 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
630 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000631 }
John McCall83779672011-02-19 02:53:41 +0000632
633 // Clear cached linkage for function template decls, too.
634 if (FunctionTemplateDecl *temp =
635 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
636 for (FunctionTemplateDecl::spec_iterator
637 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
638 i->ClearLinkageCache();
639
John McCalld396b972011-02-08 19:01:05 +0000640}
641
Douglas Gregorbf62d642010-12-06 18:36:25 +0000642Linkage NamedDecl::getLinkage() const {
643 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000644 assert(Linkage(CachedLinkage) ==
645 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000646 return Linkage(CachedLinkage);
647 }
648
649 CachedLinkage = getLVForDecl(this,
650 LVFlags::CreateOnlyDeclLinkage()).linkage();
651 HasCachedLinkage = 1;
652 return Linkage(CachedLinkage);
653}
654
John McCallc273f242010-10-30 11:50:40 +0000655LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000656 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000657 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000658 HasCachedLinkage = 1;
659 CachedLinkage = LI.linkage();
660 return LI;
John McCall033caa52010-10-29 00:29:13 +0000661}
Ted Kremenek926d8602010-04-20 23:15:35 +0000662
John McCall07072662010-11-02 01:45:15 +0000663static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000664 // Objective-C: treat all Objective-C declarations as having external
665 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000666 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000667 default:
668 break;
John McCall457a04e2010-10-22 21:05:15 +0000669 case Decl::TemplateTemplateParm: // count these as external
670 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000671 case Decl::ObjCAtDefsField:
672 case Decl::ObjCCategory:
673 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000674 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000675 case Decl::ObjCForwardProtocol:
676 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000677 case Decl::ObjCMethod:
678 case Decl::ObjCProperty:
679 case Decl::ObjCPropertyImpl:
680 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000681 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000682 }
683
Douglas Gregorf73b2822009-11-25 22:24:25 +0000684 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000685 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000686 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000687
688 // C++ [basic.link]p5:
689 // In addition, a member function, static data member, a named
690 // class or enumeration of class scope, or an unnamed class or
691 // enumeration defined in a class-scope typedef declaration such
692 // that the class or enumeration has the typedef name for linkage
693 // purposes (7.1.3), has external linkage if the name of the class
694 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000695 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000696 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000697
698 // C++ [basic.link]p6:
699 // The name of a function declared in block scope and the name of
700 // an object declared by a block scope extern declaration have
701 // linkage. If there is a visible declaration of an entity with
702 // linkage having the same name and type, ignoring entities
703 // declared outside the innermost enclosing namespace scope, the
704 // block scope declaration declares that same entity and receives
705 // the linkage of the previous declaration. If there is more than
706 // one such matching entity, the program is ill-formed. Otherwise,
707 // if no matching entity is found, the block scope entity receives
708 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000709 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
710 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000711 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000712 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000713
John McCallc273f242010-10-30 11:50:40 +0000714 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000715 if (Flags.ConsiderVisibilityAttributes) {
716 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
717 LV.setVisibility(GetVisibilityFromAttr(VA));
718 }
719
John McCall457a04e2010-10-22 21:05:15 +0000720 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000721 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000722 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
723 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000724 }
725
726 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000727 }
728
John McCall033caa52010-10-29 00:29:13 +0000729 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000730 if (Var->getStorageClass() == SC_Extern ||
731 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000732 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000733 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000734
John McCallc273f242010-10-30 11:50:40 +0000735 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000736 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000737 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000738 else if (Flags.ConsiderVisibilityAttributes) {
739 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
740 LV.setVisibility(GetVisibilityFromAttr(VA));
741 }
742
John McCall457a04e2010-10-22 21:05:15 +0000743 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000744 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000745 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
746 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000747 }
748
749 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000750 }
751 }
752
753 // C++ [basic.link]p6:
754 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000755 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000756}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000757
Douglas Gregor2ada0482009-02-04 17:27:36 +0000758std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000759 return getQualifiedNameAsString(getASTContext().getLangOptions());
760}
761
762std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000763 const DeclContext *Ctx = getDeclContext();
764
765 if (Ctx->isFunctionOrMethod())
766 return getNameAsString();
767
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000768 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
769 ContextsTy Contexts;
770
771 // Collect contexts.
772 while (Ctx && isa<NamedDecl>(Ctx)) {
773 Contexts.push_back(Ctx);
774 Ctx = Ctx->getParent();
775 };
776
777 std::string QualName;
778 llvm::raw_string_ostream OS(QualName);
779
780 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
781 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000782 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000783 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000784 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
785 std::string TemplateArgsStr
786 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000787 TemplateArgs.data(),
788 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000789 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000790 OS << Spec->getName() << TemplateArgsStr;
791 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000792 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000793 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000794 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000795 OS << ND;
796 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
797 if (!RD->getIdentifier())
798 OS << "<anonymous " << RD->getKindName() << '>';
799 else
800 OS << RD;
801 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000802 const FunctionProtoType *FT = 0;
803 if (FD->hasWrittenPrototype())
804 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
805
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000806 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000807 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000808 unsigned NumParams = FD->getNumParams();
809 for (unsigned i = 0; i < NumParams; ++i) {
810 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000811 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000812 std::string Param;
813 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000814 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000815 }
816
817 if (FT->isVariadic()) {
818 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000819 OS << ", ";
820 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000821 }
822 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000823 OS << ')';
824 } else {
825 OS << cast<NamedDecl>(*I);
826 }
827 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000828 }
829
John McCalla2a3f7d2010-03-16 21:48:18 +0000830 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000831 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000832 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000833 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000834
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000835 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000836}
837
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000838bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000839 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
840
Douglas Gregor889ceb72009-02-03 19:21:40 +0000841 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
842 // We want to keep it, unless it nominates same namespace.
843 if (getKind() == Decl::UsingDirective) {
844 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
845 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
846 }
Mike Stump11289f42009-09-09 15:08:12 +0000847
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000848 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
849 // For function declarations, we keep track of redeclarations.
850 return FD->getPreviousDeclaration() == OldD;
851
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000852 // For function templates, the underlying function declarations are linked.
853 if (const FunctionTemplateDecl *FunctionTemplate
854 = dyn_cast<FunctionTemplateDecl>(this))
855 if (const FunctionTemplateDecl *OldFunctionTemplate
856 = dyn_cast<FunctionTemplateDecl>(OldD))
857 return FunctionTemplate->getTemplatedDecl()
858 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000859
Steve Naroffc4173fa2009-02-22 19:35:57 +0000860 // For method declarations, we keep track of redeclarations.
861 if (isa<ObjCMethodDecl>(this))
862 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000863
John McCall9f3059a2009-10-09 21:13:30 +0000864 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
865 return true;
866
John McCall3f746822009-11-17 05:59:44 +0000867 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
868 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
869 cast<UsingShadowDecl>(OldD)->getTargetDecl();
870
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000871 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
872 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
873 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
874
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000875 // For non-function declarations, if the declarations are of the
876 // same kind then this must be a redeclaration, or semantic analysis
877 // would not have given us the new declaration.
878 return this->getKind() == OldD->getKind();
879}
880
Douglas Gregoreddf4332009-02-24 20:03:32 +0000881bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000882 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000883}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000884
Anders Carlsson6915bf62009-06-26 06:29:23 +0000885NamedDecl *NamedDecl::getUnderlyingDecl() {
886 NamedDecl *ND = this;
887 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000888 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000889 ND = UD->getTargetDecl();
890 else if (ObjCCompatibleAliasDecl *AD
891 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
892 return AD->getClassInterface();
893 else
894 return ND;
895 }
896}
897
John McCalla8ae2222010-04-06 21:38:20 +0000898bool NamedDecl::isCXXInstanceMember() const {
899 assert(isCXXClassMember() &&
900 "checking whether non-member is instance member");
901
902 const NamedDecl *D = this;
903 if (isa<UsingShadowDecl>(D))
904 D = cast<UsingShadowDecl>(D)->getTargetDecl();
905
Francois Pichet783dd6e2010-11-21 06:08:52 +0000906 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000907 return true;
908 if (isa<CXXMethodDecl>(D))
909 return cast<CXXMethodDecl>(D)->isInstance();
910 if (isa<FunctionTemplateDecl>(D))
911 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
912 ->getTemplatedDecl())->isInstance();
913 return false;
914}
915
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000916//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000917// DeclaratorDecl Implementation
918//===----------------------------------------------------------------------===//
919
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000920template <typename DeclT>
921static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
922 if (decl->getNumTemplateParameterLists() > 0)
923 return decl->getTemplateParameterList(0)->getTemplateLoc();
924 else
925 return decl->getInnerLocStart();
926}
927
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000928SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000929 TypeSourceInfo *TSI = getTypeSourceInfo();
930 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000931 return SourceLocation();
932}
933
John McCall3e11ebe2010-03-15 10:12:16 +0000934void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
935 SourceRange QualifierRange) {
936 if (Qualifier) {
937 // Make sure the extended decl info is allocated.
938 if (!hasExtInfo()) {
939 // Save (non-extended) type source info pointer.
940 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
941 // Allocate external info struct.
942 DeclInfo = new (getASTContext()) ExtInfo;
943 // Restore savedTInfo into (extended) decl info.
944 getExtInfo()->TInfo = savedTInfo;
945 }
946 // Set qualifier info.
947 getExtInfo()->NNS = Qualifier;
948 getExtInfo()->NNSRange = QualifierRange;
949 }
950 else {
951 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
952 assert(QualifierRange.isInvalid());
953 if (hasExtInfo()) {
954 // Save type source info pointer.
955 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
956 // Deallocate the extended decl info.
957 getASTContext().Deallocate(getExtInfo());
958 // Restore savedTInfo into (non-extended) decl info.
959 DeclInfo = savedTInfo;
960 }
961 }
962}
963
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000964SourceLocation DeclaratorDecl::getOuterLocStart() const {
965 return getTemplateOrInnerLocStart(this);
966}
967
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000968void
Douglas Gregor20527e22010-06-15 17:44:38 +0000969QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
970 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000971 TemplateParameterList **TPLists) {
972 assert((NumTPLists == 0 || TPLists != 0) &&
973 "Empty array of template parameters with positive size!");
974 assert((NumTPLists == 0 || NNS) &&
975 "Nonempty array of template parameters with no qualifier!");
976
977 // Free previous template parameters (if any).
978 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000979 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000980 TemplParamLists = 0;
981 NumTemplParamLists = 0;
982 }
983 // Set info on matched template parameter lists (if any).
984 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000985 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000986 NumTemplParamLists = NumTPLists;
987 for (unsigned i = NumTPLists; i-- > 0; )
988 TemplParamLists[i] = TPLists[i];
989 }
990}
991
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000992//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000993// VarDecl Implementation
994//===----------------------------------------------------------------------===//
995
Sebastian Redl833ef452010-01-26 22:01:41 +0000996const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
997 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000998 case SC_None: break;
999 case SC_Auto: return "auto"; break;
1000 case SC_Extern: return "extern"; break;
1001 case SC_PrivateExtern: return "__private_extern__"; break;
1002 case SC_Register: return "register"; break;
1003 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +00001004 }
1005
1006 assert(0 && "Invalid storage class");
1007 return 0;
1008}
1009
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001010VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +00001011 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001012 StorageClass S, StorageClass SCAsWritten) {
1013 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001014}
1015
Douglas Gregorbf62d642010-12-06 18:36:25 +00001016void VarDecl::setStorageClass(StorageClass SC) {
1017 assert(isLegalForVariable(SC));
1018 if (getStorageClass() != SC)
1019 ClearLinkageCache();
1020
1021 SClass = SC;
1022}
1023
Douglas Gregorb11aad82011-02-19 18:51:44 +00001024SourceLocation VarDecl::getInnerLocStart() const {
1025 SourceLocation Start = getTypeSpecStartLoc();
1026 if (Start.isInvalid())
1027 Start = getLocation();
1028 return Start;
1029}
1030
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001031SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001032 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001033 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1034 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001035}
1036
Sebastian Redl833ef452010-01-26 22:01:41 +00001037bool VarDecl::isExternC() const {
1038 ASTContext &Context = getASTContext();
1039 if (!Context.getLangOptions().CPlusPlus)
1040 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001041 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001042 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1043
1044 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
1045 DC = DC->getParent()) {
1046 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1047 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001048 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001049
1050 break;
1051 }
1052
1053 if (DC->isFunctionOrMethod())
1054 return false;
1055 }
1056
1057 return false;
1058}
1059
1060VarDecl *VarDecl::getCanonicalDecl() {
1061 return getFirstDeclaration();
1062}
1063
Sebastian Redl35351a92010-01-31 22:27:38 +00001064VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1065 // C++ [basic.def]p2:
1066 // A declaration is a definition unless [...] it contains the 'extern'
1067 // specifier or a linkage-specification and neither an initializer [...],
1068 // it declares a static data member in a class declaration [...].
1069 // C++ [temp.expl.spec]p15:
1070 // An explicit specialization of a static data member of a template is a
1071 // definition if the declaration includes an initializer; otherwise, it is
1072 // a declaration.
1073 if (isStaticDataMember()) {
1074 if (isOutOfLine() && (hasInit() ||
1075 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1076 return Definition;
1077 else
1078 return DeclarationOnly;
1079 }
1080 // C99 6.7p5:
1081 // A definition of an identifier is a declaration for that identifier that
1082 // [...] causes storage to be reserved for that object.
1083 // Note: that applies for all non-file-scope objects.
1084 // C99 6.9.2p1:
1085 // If the declaration of an identifier for an object has file scope and an
1086 // initializer, the declaration is an external definition for the identifier
1087 if (hasInit())
1088 return Definition;
1089 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1090 if (hasExternalStorage())
1091 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001092
John McCall8e7d6562010-08-26 03:08:43 +00001093 if (getStorageClassAsWritten() == SC_Extern ||
1094 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001095 for (const VarDecl *PrevVar = getPreviousDeclaration();
1096 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1097 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1098 return DeclarationOnly;
1099 }
1100 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001101 // C99 6.9.2p2:
1102 // A declaration of an object that has file scope without an initializer,
1103 // and without a storage class specifier or the scs 'static', constitutes
1104 // a tentative definition.
1105 // No such thing in C++.
1106 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1107 return TentativeDefinition;
1108
1109 // What's left is (in C, block-scope) declarations without initializers or
1110 // external storage. These are definitions.
1111 return Definition;
1112}
1113
Sebastian Redl35351a92010-01-31 22:27:38 +00001114VarDecl *VarDecl::getActingDefinition() {
1115 DefinitionKind Kind = isThisDeclarationADefinition();
1116 if (Kind != TentativeDefinition)
1117 return 0;
1118
Chris Lattner48eb14d2010-06-14 18:31:46 +00001119 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001120 VarDecl *First = getFirstDeclaration();
1121 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1122 I != E; ++I) {
1123 Kind = (*I)->isThisDeclarationADefinition();
1124 if (Kind == Definition)
1125 return 0;
1126 else if (Kind == TentativeDefinition)
1127 LastTentative = *I;
1128 }
1129 return LastTentative;
1130}
1131
1132bool VarDecl::isTentativeDefinitionNow() const {
1133 DefinitionKind Kind = isThisDeclarationADefinition();
1134 if (Kind != TentativeDefinition)
1135 return false;
1136
1137 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1138 if ((*I)->isThisDeclarationADefinition() == Definition)
1139 return false;
1140 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001141 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001142}
1143
Sebastian Redl5ca79842010-02-01 20:16:42 +00001144VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001145 VarDecl *First = getFirstDeclaration();
1146 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1147 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001148 if ((*I)->isThisDeclarationADefinition() == Definition)
1149 return *I;
1150 }
1151 return 0;
1152}
1153
John McCall37bb6c92010-10-29 22:22:43 +00001154VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1155 DefinitionKind Kind = DeclarationOnly;
1156
1157 const VarDecl *First = getFirstDeclaration();
1158 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1159 I != E; ++I)
1160 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1161
1162 return Kind;
1163}
1164
Sebastian Redl5ca79842010-02-01 20:16:42 +00001165const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001166 redecl_iterator I = redecls_begin(), E = redecls_end();
1167 while (I != E && !I->getInit())
1168 ++I;
1169
1170 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001171 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001172 return I->getInit();
1173 }
1174 return 0;
1175}
1176
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001177bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001178 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001179 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001180
1181 if (!isStaticDataMember())
1182 return false;
1183
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001184 // If this static data member was instantiated from a static data member of
1185 // a class template, check whether that static data member was defined
1186 // out-of-line.
1187 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1188 return VD->isOutOfLine();
1189
1190 return false;
1191}
1192
Douglas Gregor1d957a32009-10-27 18:42:08 +00001193VarDecl *VarDecl::getOutOfLineDefinition() {
1194 if (!isStaticDataMember())
1195 return 0;
1196
1197 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1198 RD != RDEnd; ++RD) {
1199 if (RD->getLexicalDeclContext()->isFileContext())
1200 return *RD;
1201 }
1202
1203 return 0;
1204}
1205
Douglas Gregord5058122010-02-11 01:19:42 +00001206void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001207 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1208 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001209 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001210 }
1211
1212 Init = I;
1213}
1214
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001215VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001216 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001217 return cast<VarDecl>(MSI->getInstantiatedFrom());
1218
1219 return 0;
1220}
1221
Douglas Gregor3c74d412009-10-14 20:14:33 +00001222TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001223 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001224 return MSI->getTemplateSpecializationKind();
1225
1226 return TSK_Undeclared;
1227}
1228
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001229MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001230 return getASTContext().getInstantiatedFromStaticDataMember(this);
1231}
1232
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001233void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1234 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001235 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001236 assert(MSI && "Not an instantiated static data member?");
1237 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001238 if (TSK != TSK_ExplicitSpecialization &&
1239 PointOfInstantiation.isValid() &&
1240 MSI->getPointOfInstantiation().isInvalid())
1241 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001242}
1243
Sebastian Redl833ef452010-01-26 22:01:41 +00001244//===----------------------------------------------------------------------===//
1245// ParmVarDecl Implementation
1246//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001247
Sebastian Redl833ef452010-01-26 22:01:41 +00001248ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1249 SourceLocation L, IdentifierInfo *Id,
1250 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001251 StorageClass S, StorageClass SCAsWritten,
1252 Expr *DefArg) {
1253 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1254 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001255}
1256
Sebastian Redl833ef452010-01-26 22:01:41 +00001257Expr *ParmVarDecl::getDefaultArg() {
1258 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1259 assert(!hasUninstantiatedDefaultArg() &&
1260 "Default argument is not yet instantiated!");
1261
1262 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001263 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001264 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001265
Sebastian Redl833ef452010-01-26 22:01:41 +00001266 return Arg;
1267}
1268
1269unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001270 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001271 return E->getNumTemporaries();
1272
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001273 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001274}
1275
Sebastian Redl833ef452010-01-26 22:01:41 +00001276CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1277 assert(getNumDefaultArgTemporaries() &&
1278 "Default arguments does not have any temporaries!");
1279
John McCall5d413782010-12-06 08:20:24 +00001280 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001281 return E->getTemporary(i);
1282}
1283
1284SourceRange ParmVarDecl::getDefaultArgRange() const {
1285 if (const Expr *E = getInit())
1286 return E->getSourceRange();
1287
1288 if (hasUninstantiatedDefaultArg())
1289 return getUninstantiatedDefaultArg()->getSourceRange();
1290
1291 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001292}
1293
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001294bool ParmVarDecl::isParameterPack() const {
1295 return isa<PackExpansionType>(getType());
1296}
1297
Nuno Lopes394ec982008-12-17 23:39:55 +00001298//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001299// FunctionDecl Implementation
1300//===----------------------------------------------------------------------===//
1301
Douglas Gregorb11aad82011-02-19 18:51:44 +00001302void FunctionDecl::getNameForDiagnostic(std::string &S,
1303 const PrintingPolicy &Policy,
1304 bool Qualified) const {
1305 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1306 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1307 if (TemplateArgs)
1308 S += TemplateSpecializationType::PrintTemplateArgumentList(
1309 TemplateArgs->data(),
1310 TemplateArgs->size(),
1311 Policy);
1312
1313}
1314
Ted Kremenek186a0742010-04-29 16:49:01 +00001315bool FunctionDecl::isVariadic() const {
1316 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1317 return FT->isVariadic();
1318 return false;
1319}
1320
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001321bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1322 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1323 if (I->Body) {
1324 Definition = *I;
1325 return true;
1326 }
1327 }
1328
1329 return false;
1330}
1331
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001332Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001333 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1334 if (I->Body) {
1335 Definition = *I;
1336 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001337 }
1338 }
1339
1340 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001341}
1342
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001343void FunctionDecl::setBody(Stmt *B) {
1344 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001345 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001346 EndRangeLoc = B->getLocEnd();
1347}
1348
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001349void FunctionDecl::setPure(bool P) {
1350 IsPure = P;
1351 if (P)
1352 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1353 Parent->markedVirtualFunctionPure();
1354}
1355
Douglas Gregor16618f22009-09-12 00:17:51 +00001356bool FunctionDecl::isMain() const {
1357 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001358 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001359 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001360 getIdentifier() && getIdentifier()->isStr("main");
1361}
1362
Douglas Gregor16618f22009-09-12 00:17:51 +00001363bool FunctionDecl::isExternC() const {
1364 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001365 // In C, any non-static, non-overloadable function has external
1366 // linkage.
1367 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001368 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001369
Mike Stump11289f42009-09-09 15:08:12 +00001370 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001371 DC = DC->getParent()) {
1372 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1373 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001374 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001375 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001376
1377 break;
1378 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001379
1380 if (DC->isRecord())
1381 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001382 }
1383
Douglas Gregorbff62032010-10-21 16:57:46 +00001384 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001385}
1386
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001387bool FunctionDecl::isGlobal() const {
1388 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1389 return Method->isStatic();
1390
John McCall8e7d6562010-08-26 03:08:43 +00001391 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001392 return false;
1393
Mike Stump11289f42009-09-09 15:08:12 +00001394 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001395 DC->isNamespace();
1396 DC = DC->getParent()) {
1397 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1398 if (!Namespace->getDeclName())
1399 return false;
1400 break;
1401 }
1402 }
1403
1404 return true;
1405}
1406
Sebastian Redl833ef452010-01-26 22:01:41 +00001407void
1408FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1409 redeclarable_base::setPreviousDeclaration(PrevDecl);
1410
1411 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1412 FunctionTemplateDecl *PrevFunTmpl
1413 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1414 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1415 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1416 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001417
1418 if (PrevDecl->IsInline)
1419 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001420}
1421
1422const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1423 return getFirstDeclaration();
1424}
1425
1426FunctionDecl *FunctionDecl::getCanonicalDecl() {
1427 return getFirstDeclaration();
1428}
1429
Douglas Gregorbf62d642010-12-06 18:36:25 +00001430void FunctionDecl::setStorageClass(StorageClass SC) {
1431 assert(isLegalForFunction(SC));
1432 if (getStorageClass() != SC)
1433 ClearLinkageCache();
1434
1435 SClass = SC;
1436}
1437
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001438/// \brief Returns a value indicating whether this function
1439/// corresponds to a builtin function.
1440///
1441/// The function corresponds to a built-in function if it is
1442/// declared at translation scope or within an extern "C" block and
1443/// its name matches with the name of a builtin. The returned value
1444/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001445/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001446/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001447unsigned FunctionDecl::getBuiltinID() const {
1448 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001449 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1450 return 0;
1451
1452 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1453 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1454 return BuiltinID;
1455
1456 // This function has the name of a known C library
1457 // function. Determine whether it actually refers to the C library
1458 // function or whether it just has the same name.
1459
Douglas Gregora908e7f2009-02-17 03:23:10 +00001460 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001461 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001462 return 0;
1463
Douglas Gregore711f702009-02-14 18:57:46 +00001464 // If this function is at translation-unit scope and we're not in
1465 // C++, it refers to the C library function.
1466 if (!Context.getLangOptions().CPlusPlus &&
1467 getDeclContext()->isTranslationUnit())
1468 return BuiltinID;
1469
1470 // If the function is in an extern "C" linkage specification and is
1471 // not marked "overloadable", it's the real function.
1472 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001473 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001474 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001475 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001476 return BuiltinID;
1477
1478 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001479 return 0;
1480}
1481
1482
Chris Lattner47c0d002009-04-25 06:03:53 +00001483/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001484/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001485/// after it has been created.
1486unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001487 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001488 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001489 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001490 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001491
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001492}
1493
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001494void FunctionDecl::setParams(ASTContext &C,
1495 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001496 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001497 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001498
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001499 // Zero params -> null pointer.
1500 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001501 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001502 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001503 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001504
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001505 // Update source range. The check below allows us to set EndRangeLoc before
1506 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001507 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001508 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001509 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001510}
Chris Lattner41943152007-01-25 04:52:46 +00001511
Chris Lattner58258242008-04-10 02:22:51 +00001512/// getMinRequiredArguments - Returns the minimum number of arguments
1513/// needed to call this function. This may be fewer than the number of
1514/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001515/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001516unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001517 if (!getASTContext().getLangOptions().CPlusPlus)
1518 return getNumParams();
1519
Douglas Gregor7825bf32011-01-06 22:09:01 +00001520 unsigned NumRequiredArgs = getNumParams();
1521
1522 // If the last parameter is a parameter pack, we don't need an argument for
1523 // it.
1524 if (NumRequiredArgs > 0 &&
1525 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1526 --NumRequiredArgs;
1527
1528 // If this parameter has a default argument, we don't need an argument for
1529 // it.
1530 while (NumRequiredArgs > 0 &&
1531 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001532 --NumRequiredArgs;
1533
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001534 // We might have parameter packs before the end. These can't be deduced,
1535 // but they can still handle multiple arguments.
1536 unsigned ArgIdx = NumRequiredArgs;
1537 while (ArgIdx > 0) {
1538 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1539 NumRequiredArgs = ArgIdx;
1540
1541 --ArgIdx;
1542 }
1543
Chris Lattner58258242008-04-10 02:22:51 +00001544 return NumRequiredArgs;
1545}
1546
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001547bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001548 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001549 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001550
1551 if (isa<CXXMethodDecl>(this)) {
1552 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1553 return true;
1554 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001555
1556 switch (getTemplateSpecializationKind()) {
1557 case TSK_Undeclared:
1558 case TSK_ExplicitSpecialization:
1559 return false;
1560
1561 case TSK_ImplicitInstantiation:
1562 case TSK_ExplicitInstantiationDeclaration:
1563 case TSK_ExplicitInstantiationDefinition:
1564 // Handle below.
1565 break;
1566 }
1567
1568 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001569 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001570 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001571 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001572
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001573 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001574 return PatternDecl->isInlined();
1575
1576 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001577}
1578
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001579/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001580/// definition will be externally visible.
1581///
1582/// Inline function definitions are always available for inlining optimizations.
1583/// However, depending on the language dialect, declaration specifiers, and
1584/// attributes, the definition of an inline function may or may not be
1585/// "externally" visible to other translation units in the program.
1586///
1587/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001588/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001589/// inline definition becomes externally visible (C99 6.7.4p6).
1590///
1591/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1592/// definition, we use the GNU semantics for inline, which are nearly the
1593/// opposite of C99 semantics. In particular, "inline" by itself will create
1594/// an externally visible symbol, but "extern inline" will not create an
1595/// externally visible symbol.
1596bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1597 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001598 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001599 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001600
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001601 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001602 // If it's not the case that both 'inline' and 'extern' are
1603 // specified on the definition, then this inline definition is
1604 // externally visible.
1605 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1606 return true;
1607
1608 // If any declaration is 'inline' but not 'extern', then this definition
1609 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001610 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1611 Redecl != RedeclEnd;
1612 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001613 if (Redecl->isInlineSpecified() &&
1614 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001615 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001616 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001617
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001618 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001619 }
1620
1621 // C99 6.7.4p6:
1622 // [...] If all of the file scope declarations for a function in a
1623 // translation unit include the inline function specifier without extern,
1624 // then the definition in that translation unit is an inline definition.
1625 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1626 Redecl != RedeclEnd;
1627 ++Redecl) {
1628 // Only consider file-scope declarations in this test.
1629 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1630 continue;
1631
John McCall8e7d6562010-08-26 03:08:43 +00001632 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001633 return true; // Not an inline definition
1634 }
1635
1636 // C99 6.7.4p6:
1637 // An inline definition does not provide an external definition for the
1638 // function, and does not forbid an external definition in another
1639 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001640 return false;
1641}
1642
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001643/// getOverloadedOperator - Which C++ overloaded operator this
1644/// function represents, if any.
1645OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001646 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1647 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001648 else
1649 return OO_None;
1650}
1651
Alexis Huntc88db062010-01-13 09:01:02 +00001652/// getLiteralIdentifier - The literal suffix identifier this function
1653/// represents, if any.
1654const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1655 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1656 return getDeclName().getCXXLiteralIdentifier();
1657 else
1658 return 0;
1659}
1660
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001661FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1662 if (TemplateOrSpecialization.isNull())
1663 return TK_NonTemplate;
1664 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1665 return TK_FunctionTemplate;
1666 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1667 return TK_MemberSpecialization;
1668 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1669 return TK_FunctionTemplateSpecialization;
1670 if (TemplateOrSpecialization.is
1671 <DependentFunctionTemplateSpecializationInfo*>())
1672 return TK_DependentFunctionTemplateSpecialization;
1673
1674 assert(false && "Did we miss a TemplateOrSpecialization type?");
1675 return TK_NonTemplate;
1676}
1677
Douglas Gregord801b062009-10-07 23:56:10 +00001678FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001679 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001680 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1681
1682 return 0;
1683}
1684
Douglas Gregor06db9f52009-10-12 20:18:28 +00001685MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1686 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1687}
1688
Douglas Gregord801b062009-10-07 23:56:10 +00001689void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001690FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1691 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001692 TemplateSpecializationKind TSK) {
1693 assert(TemplateOrSpecialization.isNull() &&
1694 "Member function is already a specialization");
1695 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001696 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001697 TemplateOrSpecialization = Info;
1698}
1699
Douglas Gregorafca3b42009-10-27 20:53:28 +00001700bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001701 // If the function is invalid, it can't be implicitly instantiated.
1702 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001703 return false;
1704
1705 switch (getTemplateSpecializationKind()) {
1706 case TSK_Undeclared:
1707 case TSK_ExplicitSpecialization:
1708 case TSK_ExplicitInstantiationDefinition:
1709 return false;
1710
1711 case TSK_ImplicitInstantiation:
1712 return true;
1713
1714 case TSK_ExplicitInstantiationDeclaration:
1715 // Handled below.
1716 break;
1717 }
1718
1719 // Find the actual template from which we will instantiate.
1720 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001721 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001722 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001723 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001724
1725 // C++0x [temp.explicit]p9:
1726 // Except for inline functions, other explicit instantiation declarations
1727 // have the effect of suppressing the implicit instantiation of the entity
1728 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001729 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001730 return true;
1731
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001732 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001733}
1734
1735FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1736 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1737 while (Primary->getInstantiatedFromMemberTemplate()) {
1738 // If we have hit a point where the user provided a specialization of
1739 // this template, we're done looking.
1740 if (Primary->isMemberSpecialization())
1741 break;
1742
1743 Primary = Primary->getInstantiatedFromMemberTemplate();
1744 }
1745
1746 return Primary->getTemplatedDecl();
1747 }
1748
1749 return getInstantiatedFromMemberFunction();
1750}
1751
Douglas Gregor70d83e22009-06-29 17:30:29 +00001752FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001753 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001754 = TemplateOrSpecialization
1755 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001756 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001757 }
1758 return 0;
1759}
1760
1761const TemplateArgumentList *
1762FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001763 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001764 = TemplateOrSpecialization
1765 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001766 return Info->TemplateArguments;
1767 }
1768 return 0;
1769}
1770
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001771const TemplateArgumentListInfo *
1772FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1773 if (FunctionTemplateSpecializationInfo *Info
1774 = TemplateOrSpecialization
1775 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1776 return Info->TemplateArgumentsAsWritten;
1777 }
1778 return 0;
1779}
1780
Mike Stump11289f42009-09-09 15:08:12 +00001781void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001782FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1783 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001784 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001785 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001786 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001787 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1788 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001789 assert(TSK != TSK_Undeclared &&
1790 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001791 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001792 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001793 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001794 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1795 TemplateArgs,
1796 TemplateArgsAsWritten,
1797 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001798 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001799
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001800 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001801 // function template specializations.
1802 if (InsertPos)
1803 Template->getSpecializations().InsertNode(Info, InsertPos);
1804 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001805 // Try to insert the new node. If there is an existing node, leave it, the
1806 // set will contain the canonical decls while
1807 // FunctionTemplateDecl::findSpecialization will return
1808 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001809 FunctionTemplateSpecializationInfo *Existing
1810 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001811 (void)Existing;
1812 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1813 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001814 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001815}
1816
John McCallb9c78482010-04-08 09:05:18 +00001817void
1818FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1819 const UnresolvedSetImpl &Templates,
1820 const TemplateArgumentListInfo &TemplateArgs) {
1821 assert(TemplateOrSpecialization.isNull());
1822 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1823 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001824 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001825 void *Buffer = Context.Allocate(Size);
1826 DependentFunctionTemplateSpecializationInfo *Info =
1827 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1828 TemplateArgs);
1829 TemplateOrSpecialization = Info;
1830}
1831
1832DependentFunctionTemplateSpecializationInfo::
1833DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1834 const TemplateArgumentListInfo &TArgs)
1835 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1836
1837 d.NumTemplates = Ts.size();
1838 d.NumArgs = TArgs.size();
1839
1840 FunctionTemplateDecl **TsArray =
1841 const_cast<FunctionTemplateDecl**>(getTemplates());
1842 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1843 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1844
1845 TemplateArgumentLoc *ArgsArray =
1846 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1847 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1848 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1849}
1850
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001851TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001852 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001853 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001854 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001855 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001856 if (FTSInfo)
1857 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001858
Douglas Gregord801b062009-10-07 23:56:10 +00001859 MemberSpecializationInfo *MSInfo
1860 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1861 if (MSInfo)
1862 return MSInfo->getTemplateSpecializationKind();
1863
1864 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001865}
1866
Mike Stump11289f42009-09-09 15:08:12 +00001867void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001868FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1869 SourceLocation PointOfInstantiation) {
1870 if (FunctionTemplateSpecializationInfo *FTSInfo
1871 = TemplateOrSpecialization.dyn_cast<
1872 FunctionTemplateSpecializationInfo*>()) {
1873 FTSInfo->setTemplateSpecializationKind(TSK);
1874 if (TSK != TSK_ExplicitSpecialization &&
1875 PointOfInstantiation.isValid() &&
1876 FTSInfo->getPointOfInstantiation().isInvalid())
1877 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1878 } else if (MemberSpecializationInfo *MSInfo
1879 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1880 MSInfo->setTemplateSpecializationKind(TSK);
1881 if (TSK != TSK_ExplicitSpecialization &&
1882 PointOfInstantiation.isValid() &&
1883 MSInfo->getPointOfInstantiation().isInvalid())
1884 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1885 } else
1886 assert(false && "Function cannot have a template specialization kind");
1887}
1888
1889SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001890 if (FunctionTemplateSpecializationInfo *FTSInfo
1891 = TemplateOrSpecialization.dyn_cast<
1892 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001893 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001894 else if (MemberSpecializationInfo *MSInfo
1895 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001896 return MSInfo->getPointOfInstantiation();
1897
1898 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001899}
1900
Douglas Gregor6411b922009-09-11 20:15:17 +00001901bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001902 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00001903 return true;
1904
1905 // If this function was instantiated from a member function of a
1906 // class template, check whether that member function was defined out-of-line.
1907 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1908 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001909 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001910 return Definition->isOutOfLine();
1911 }
1912
1913 // If this function was instantiated from a function template,
1914 // check whether that function template was defined out-of-line.
1915 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1916 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001917 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001918 return Definition->isOutOfLine();
1919 }
1920
1921 return false;
1922}
1923
Chris Lattner59a25942008-03-31 00:36:02 +00001924//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001925// FieldDecl Implementation
1926//===----------------------------------------------------------------------===//
1927
Jay Foad39c79802011-01-12 09:06:06 +00001928FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1929 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl833ef452010-01-26 22:01:41 +00001930 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1931 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1932}
1933
1934bool FieldDecl::isAnonymousStructOrUnion() const {
1935 if (!isImplicit() || getDeclName())
1936 return false;
1937
1938 if (const RecordType *Record = getType()->getAs<RecordType>())
1939 return Record->getDecl()->isAnonymousStructOrUnion();
1940
1941 return false;
1942}
1943
John McCall4e819612011-01-20 07:57:12 +00001944unsigned FieldDecl::getFieldIndex() const {
1945 if (CachedFieldIndex) return CachedFieldIndex - 1;
1946
1947 unsigned index = 0;
1948 RecordDecl::field_iterator
1949 i = getParent()->field_begin(), e = getParent()->field_end();
1950 while (true) {
1951 assert(i != e && "failed to find field in parent!");
1952 if (*i == this)
1953 break;
1954
1955 ++i;
1956 ++index;
1957 }
1958
1959 CachedFieldIndex = index + 1;
1960 return index;
1961}
1962
Sebastian Redl833ef452010-01-26 22:01:41 +00001963//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001964// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001965//===----------------------------------------------------------------------===//
1966
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001967SourceLocation TagDecl::getOuterLocStart() const {
1968 return getTemplateOrInnerLocStart(this);
1969}
1970
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001971SourceRange TagDecl::getSourceRange() const {
1972 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001973 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001974}
1975
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001976TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001977 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001978}
1979
Douglas Gregora72a4e32010-05-19 18:39:18 +00001980void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1981 TypedefDeclOrQualifier = TDD;
1982 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00001983 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001984 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00001985}
1986
Douglas Gregordee1be82009-01-17 00:42:38 +00001987void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001988 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001989
1990 if (isa<CXXRecordDecl>(this)) {
1991 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1992 struct CXXRecordDecl::DefinitionData *Data =
1993 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001994 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1995 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001996 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001997}
1998
1999void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002000 assert((!isa<CXXRecordDecl>(this) ||
2001 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2002 "definition completed but not started");
2003
Douglas Gregordee1be82009-01-17 00:42:38 +00002004 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002005 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002006
2007 if (ASTMutationListener *L = getASTMutationListener())
2008 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002009}
2010
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002011TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002012 if (isDefinition())
2013 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002014 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2015 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002016
2017 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002018 R != REnd; ++R)
2019 if (R->isDefinition())
2020 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002021
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002022 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002023}
2024
John McCall3e11ebe2010-03-15 10:12:16 +00002025void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
2026 SourceRange QualifierRange) {
2027 if (Qualifier) {
2028 // Make sure the extended qualifier info is allocated.
2029 if (!hasExtInfo())
2030 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2031 // Set qualifier info.
2032 getExtInfo()->NNS = Qualifier;
2033 getExtInfo()->NNSRange = QualifierRange;
2034 }
2035 else {
2036 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2037 assert(QualifierRange.isInvalid());
2038 if (hasExtInfo()) {
2039 getASTContext().Deallocate(getExtInfo());
2040 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2041 }
2042 }
2043}
2044
Ted Kremenek21475702008-09-05 17:16:31 +00002045//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002046// EnumDecl Implementation
2047//===----------------------------------------------------------------------===//
2048
2049EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2050 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002051 EnumDecl *PrevDecl, bool IsScoped,
2052 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002053 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002054 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002055 C.getTypeDeclType(Enum, PrevDecl);
2056 return Enum;
2057}
2058
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002059EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002060 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002061 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002062}
2063
Douglas Gregord5058122010-02-11 01:19:42 +00002064void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002065 QualType NewPromotionType,
2066 unsigned NumPositiveBits,
2067 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002068 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002069 if (!IntegerType)
2070 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002071 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002072 setNumPositiveBits(NumPositiveBits);
2073 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002074 TagDecl::completeDefinition();
2075}
2076
2077//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002078// RecordDecl Implementation
2079//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002080
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002081RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002082 IdentifierInfo *Id, RecordDecl *PrevDecl,
2083 SourceLocation TKL)
2084 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002085 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002086 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002087 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002088 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002089 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002090}
2091
Jay Foad39c79802011-01-12 09:06:06 +00002092RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00002093 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00002094 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002095
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002096 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00002097 C.getTypeDeclType(R, PrevDecl);
2098 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002099}
2100
Jay Foad39c79802011-01-12 09:06:06 +00002101RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002102 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2103 SourceLocation());
2104}
2105
Douglas Gregordfcad112009-03-25 15:59:44 +00002106bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002107 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002108 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2109}
2110
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002111RecordDecl::field_iterator RecordDecl::field_begin() const {
2112 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2113 LoadFieldsFromExternalStorage();
2114
2115 return field_iterator(decl_iterator(FirstDecl));
2116}
2117
Douglas Gregorb11aad82011-02-19 18:51:44 +00002118/// completeDefinition - Notes that the definition of this type is now
2119/// complete.
2120void RecordDecl::completeDefinition() {
2121 assert(!isDefinition() && "Cannot redefine record!");
2122 TagDecl::completeDefinition();
2123}
2124
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002125void RecordDecl::LoadFieldsFromExternalStorage() const {
2126 ExternalASTSource *Source = getASTContext().getExternalSource();
2127 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2128
2129 // Notify that we have a RecordDecl doing some initialization.
2130 ExternalASTSource::Deserializing TheFields(Source);
2131
2132 llvm::SmallVector<Decl*, 64> Decls;
2133 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2134 return;
2135
2136#ifndef NDEBUG
2137 // Check that all decls we got were FieldDecls.
2138 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2139 assert(isa<FieldDecl>(Decls[i]));
2140#endif
2141
2142 LoadedFieldsFromExternalStorage = true;
2143
2144 if (Decls.empty())
2145 return;
2146
2147 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2148}
2149
Steve Naroff415d3d52008-10-08 17:01:13 +00002150//===----------------------------------------------------------------------===//
2151// BlockDecl Implementation
2152//===----------------------------------------------------------------------===//
2153
Douglas Gregord5058122010-02-11 01:19:42 +00002154void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002155 unsigned NParms) {
2156 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002157
Steve Naroffc4b30e52009-03-13 16:56:44 +00002158 // Zero params -> null pointer.
2159 if (NParms) {
2160 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002161 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002162 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2163 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2164 }
2165}
2166
John McCall351762c2011-02-07 10:33:21 +00002167void BlockDecl::setCaptures(ASTContext &Context,
2168 const Capture *begin,
2169 const Capture *end,
2170 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002171 CapturesCXXThis = capturesCXXThis;
2172
2173 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002174 NumCaptures = 0;
2175 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002176 return;
2177 }
2178
John McCall351762c2011-02-07 10:33:21 +00002179 NumCaptures = end - begin;
2180
2181 // Avoid new Capture[] because we don't want to provide a default
2182 // constructor.
2183 size_t allocationSize = NumCaptures * sizeof(Capture);
2184 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2185 memcpy(buffer, begin, allocationSize);
2186 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002187}
Sebastian Redl833ef452010-01-26 22:01:41 +00002188
Douglas Gregor70226da2010-12-21 16:27:07 +00002189SourceRange BlockDecl::getSourceRange() const {
2190 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2191}
Sebastian Redl833ef452010-01-26 22:01:41 +00002192
2193//===----------------------------------------------------------------------===//
2194// Other Decl Allocation/Deallocation Method Implementations
2195//===----------------------------------------------------------------------===//
2196
2197TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2198 return new (C) TranslationUnitDecl(C);
2199}
2200
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002201LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2202 SourceLocation L, IdentifierInfo *II) {
2203 return new (C) LabelDecl(DC, L, II, 0);
2204}
2205
2206
Sebastian Redl833ef452010-01-26 22:01:41 +00002207NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2208 SourceLocation L, IdentifierInfo *Id) {
2209 return new (C) NamespaceDecl(DC, L, Id);
2210}
2211
Douglas Gregor417e87c2010-10-27 19:49:05 +00002212NamespaceDecl *NamespaceDecl::getNextNamespace() {
2213 return dyn_cast_or_null<NamespaceDecl>(
2214 NextNamespace.get(getASTContext().getExternalSource()));
2215}
2216
Sebastian Redl833ef452010-01-26 22:01:41 +00002217ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
John McCall550d13a2011-02-22 22:25:56 +00002218 SourceLocation loc,
2219 IdentifierInfo *name,
2220 QualType type) {
2221 return new (C) ImplicitParamDecl(DC, loc, name, type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002222}
2223
2224FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002225 const DeclarationNameInfo &NameInfo,
2226 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002227 StorageClass S, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002228 bool isInlineSpecified,
2229 bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002230 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002231 S, SCAsWritten, isInlineSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002232 New->HasWrittenPrototype = hasWrittenPrototype;
2233 return New;
2234}
2235
2236BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2237 return new (C) BlockDecl(DC, L);
2238}
2239
2240EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2241 SourceLocation L,
2242 IdentifierInfo *Id, QualType T,
2243 Expr *E, const llvm::APSInt &V) {
2244 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2245}
2246
Benjamin Kramer39593702010-11-21 14:11:41 +00002247IndirectFieldDecl *
2248IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2249 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2250 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002251 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2252}
2253
Douglas Gregorbe996932010-09-01 20:41:53 +00002254SourceRange EnumConstantDecl::getSourceRange() const {
2255 SourceLocation End = getLocation();
2256 if (Init)
2257 End = Init->getLocEnd();
2258 return SourceRange(getLocation(), End);
2259}
2260
Sebastian Redl833ef452010-01-26 22:01:41 +00002261TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2262 SourceLocation L, IdentifierInfo *Id,
2263 TypeSourceInfo *TInfo) {
2264 return new (C) TypedefDecl(DC, L, Id, TInfo);
2265}
2266
Sebastian Redl833ef452010-01-26 22:01:41 +00002267FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2268 SourceLocation L,
2269 StringLiteral *Str) {
2270 return new (C) FileScopeAsmDecl(DC, L, Str);
2271}