blob: d7e389e616413988fb1ff41d6c226300d91852c4 [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)) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000711 if (Function->isInAnonymousNamespace() && !Function->isExternC())
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) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000732 if (Var->isInAnonymousNamespace() && !Var->isExternC())
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
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000871 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
872 ASTContext &Context = getASTContext();
873 return Context.getCanonicalNestedNameSpecifier(
874 cast<UsingDecl>(this)->getQualifier()) ==
875 Context.getCanonicalNestedNameSpecifier(
876 cast<UsingDecl>(OldD)->getQualifier());
877 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000878
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000879 // For non-function declarations, if the declarations are of the
880 // same kind then this must be a redeclaration, or semantic analysis
881 // would not have given us the new declaration.
882 return this->getKind() == OldD->getKind();
883}
884
Douglas Gregoreddf4332009-02-24 20:03:32 +0000885bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000886 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000887}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000888
Anders Carlsson6915bf62009-06-26 06:29:23 +0000889NamedDecl *NamedDecl::getUnderlyingDecl() {
890 NamedDecl *ND = this;
891 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000892 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000893 ND = UD->getTargetDecl();
894 else if (ObjCCompatibleAliasDecl *AD
895 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
896 return AD->getClassInterface();
897 else
898 return ND;
899 }
900}
901
John McCalla8ae2222010-04-06 21:38:20 +0000902bool NamedDecl::isCXXInstanceMember() const {
903 assert(isCXXClassMember() &&
904 "checking whether non-member is instance member");
905
906 const NamedDecl *D = this;
907 if (isa<UsingShadowDecl>(D))
908 D = cast<UsingShadowDecl>(D)->getTargetDecl();
909
Francois Pichet783dd6e2010-11-21 06:08:52 +0000910 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000911 return true;
912 if (isa<CXXMethodDecl>(D))
913 return cast<CXXMethodDecl>(D)->isInstance();
914 if (isa<FunctionTemplateDecl>(D))
915 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
916 ->getTemplatedDecl())->isInstance();
917 return false;
918}
919
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000920//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000921// DeclaratorDecl Implementation
922//===----------------------------------------------------------------------===//
923
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000924template <typename DeclT>
925static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
926 if (decl->getNumTemplateParameterLists() > 0)
927 return decl->getTemplateParameterList(0)->getTemplateLoc();
928 else
929 return decl->getInnerLocStart();
930}
931
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000932SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000933 TypeSourceInfo *TSI = getTypeSourceInfo();
934 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000935 return SourceLocation();
936}
937
Douglas Gregor14454802011-02-25 02:25:35 +0000938void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
939 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +0000940 // Make sure the extended decl info is allocated.
941 if (!hasExtInfo()) {
942 // Save (non-extended) type source info pointer.
943 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
944 // Allocate external info struct.
945 DeclInfo = new (getASTContext()) ExtInfo;
946 // Restore savedTInfo into (extended) decl info.
947 getExtInfo()->TInfo = savedTInfo;
948 }
949 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +0000950 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +0000951 }
952 else {
953 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +0000954 if (hasExtInfo()) {
955 // Save type source info pointer.
956 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
957 // Deallocate the extended decl info.
958 getASTContext().Deallocate(getExtInfo());
959 // Restore savedTInfo into (non-extended) decl info.
960 DeclInfo = savedTInfo;
961 }
962 }
963}
964
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000965SourceLocation DeclaratorDecl::getOuterLocStart() const {
966 return getTemplateOrInnerLocStart(this);
967}
968
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000969void
Douglas Gregor20527e22010-06-15 17:44:38 +0000970QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
971 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000972 TemplateParameterList **TPLists) {
973 assert((NumTPLists == 0 || TPLists != 0) &&
974 "Empty array of template parameters with positive size!");
Douglas Gregor14454802011-02-25 02:25:35 +0000975 assert((NumTPLists == 0 || QualifierLoc) &&
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000976 "Nonempty array of template parameters with no qualifier!");
977
978 // Free previous template parameters (if any).
979 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000980 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000981 TemplParamLists = 0;
982 NumTemplParamLists = 0;
983 }
984 // Set info on matched template parameter lists (if any).
985 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000986 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000987 NumTemplParamLists = NumTPLists;
988 for (unsigned i = NumTPLists; i-- > 0; )
989 TemplParamLists[i] = TPLists[i];
990 }
991}
992
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000993//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000994// VarDecl Implementation
995//===----------------------------------------------------------------------===//
996
Sebastian Redl833ef452010-01-26 22:01:41 +0000997const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
998 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000999 case SC_None: break;
1000 case SC_Auto: return "auto"; break;
1001 case SC_Extern: return "extern"; break;
1002 case SC_PrivateExtern: return "__private_extern__"; break;
1003 case SC_Register: return "register"; break;
1004 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +00001005 }
1006
1007 assert(0 && "Invalid storage class");
1008 return 0;
1009}
1010
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001011VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +00001012 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001013 StorageClass S, StorageClass SCAsWritten) {
1014 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001015}
1016
Douglas Gregorbf62d642010-12-06 18:36:25 +00001017void VarDecl::setStorageClass(StorageClass SC) {
1018 assert(isLegalForVariable(SC));
1019 if (getStorageClass() != SC)
1020 ClearLinkageCache();
1021
1022 SClass = SC;
1023}
1024
Douglas Gregorb11aad82011-02-19 18:51:44 +00001025SourceLocation VarDecl::getInnerLocStart() const {
1026 SourceLocation Start = getTypeSpecStartLoc();
1027 if (Start.isInvalid())
1028 Start = getLocation();
1029 return Start;
1030}
1031
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001032SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001033 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001034 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1035 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001036}
1037
Sebastian Redl833ef452010-01-26 22:01:41 +00001038bool VarDecl::isExternC() const {
1039 ASTContext &Context = getASTContext();
1040 if (!Context.getLangOptions().CPlusPlus)
1041 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001042 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001043 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1044
Chandler Carruth4322a282011-02-25 00:05:02 +00001045 const DeclContext *DC = getDeclContext();
1046 if (DC->isFunctionOrMethod())
1047 return false;
1048
1049 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001050 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1051 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001052 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001053
1054 break;
1055 }
1056
Sebastian Redl833ef452010-01-26 22:01:41 +00001057 }
1058
1059 return false;
1060}
1061
1062VarDecl *VarDecl::getCanonicalDecl() {
1063 return getFirstDeclaration();
1064}
1065
Sebastian Redl35351a92010-01-31 22:27:38 +00001066VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1067 // C++ [basic.def]p2:
1068 // A declaration is a definition unless [...] it contains the 'extern'
1069 // specifier or a linkage-specification and neither an initializer [...],
1070 // it declares a static data member in a class declaration [...].
1071 // C++ [temp.expl.spec]p15:
1072 // An explicit specialization of a static data member of a template is a
1073 // definition if the declaration includes an initializer; otherwise, it is
1074 // a declaration.
1075 if (isStaticDataMember()) {
1076 if (isOutOfLine() && (hasInit() ||
1077 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1078 return Definition;
1079 else
1080 return DeclarationOnly;
1081 }
1082 // C99 6.7p5:
1083 // A definition of an identifier is a declaration for that identifier that
1084 // [...] causes storage to be reserved for that object.
1085 // Note: that applies for all non-file-scope objects.
1086 // C99 6.9.2p1:
1087 // If the declaration of an identifier for an object has file scope and an
1088 // initializer, the declaration is an external definition for the identifier
1089 if (hasInit())
1090 return Definition;
1091 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1092 if (hasExternalStorage())
1093 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001094
John McCall8e7d6562010-08-26 03:08:43 +00001095 if (getStorageClassAsWritten() == SC_Extern ||
1096 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001097 for (const VarDecl *PrevVar = getPreviousDeclaration();
1098 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1099 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1100 return DeclarationOnly;
1101 }
1102 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001103 // C99 6.9.2p2:
1104 // A declaration of an object that has file scope without an initializer,
1105 // and without a storage class specifier or the scs 'static', constitutes
1106 // a tentative definition.
1107 // No such thing in C++.
1108 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1109 return TentativeDefinition;
1110
1111 // What's left is (in C, block-scope) declarations without initializers or
1112 // external storage. These are definitions.
1113 return Definition;
1114}
1115
Sebastian Redl35351a92010-01-31 22:27:38 +00001116VarDecl *VarDecl::getActingDefinition() {
1117 DefinitionKind Kind = isThisDeclarationADefinition();
1118 if (Kind != TentativeDefinition)
1119 return 0;
1120
Chris Lattner48eb14d2010-06-14 18:31:46 +00001121 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001122 VarDecl *First = getFirstDeclaration();
1123 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1124 I != E; ++I) {
1125 Kind = (*I)->isThisDeclarationADefinition();
1126 if (Kind == Definition)
1127 return 0;
1128 else if (Kind == TentativeDefinition)
1129 LastTentative = *I;
1130 }
1131 return LastTentative;
1132}
1133
1134bool VarDecl::isTentativeDefinitionNow() const {
1135 DefinitionKind Kind = isThisDeclarationADefinition();
1136 if (Kind != TentativeDefinition)
1137 return false;
1138
1139 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1140 if ((*I)->isThisDeclarationADefinition() == Definition)
1141 return false;
1142 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001143 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001144}
1145
Sebastian Redl5ca79842010-02-01 20:16:42 +00001146VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001147 VarDecl *First = getFirstDeclaration();
1148 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1149 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001150 if ((*I)->isThisDeclarationADefinition() == Definition)
1151 return *I;
1152 }
1153 return 0;
1154}
1155
John McCall37bb6c92010-10-29 22:22:43 +00001156VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1157 DefinitionKind Kind = DeclarationOnly;
1158
1159 const VarDecl *First = getFirstDeclaration();
1160 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1161 I != E; ++I)
1162 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1163
1164 return Kind;
1165}
1166
Sebastian Redl5ca79842010-02-01 20:16:42 +00001167const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001168 redecl_iterator I = redecls_begin(), E = redecls_end();
1169 while (I != E && !I->getInit())
1170 ++I;
1171
1172 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001173 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001174 return I->getInit();
1175 }
1176 return 0;
1177}
1178
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001179bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001180 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001181 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001182
1183 if (!isStaticDataMember())
1184 return false;
1185
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001186 // If this static data member was instantiated from a static data member of
1187 // a class template, check whether that static data member was defined
1188 // out-of-line.
1189 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1190 return VD->isOutOfLine();
1191
1192 return false;
1193}
1194
Douglas Gregor1d957a32009-10-27 18:42:08 +00001195VarDecl *VarDecl::getOutOfLineDefinition() {
1196 if (!isStaticDataMember())
1197 return 0;
1198
1199 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1200 RD != RDEnd; ++RD) {
1201 if (RD->getLexicalDeclContext()->isFileContext())
1202 return *RD;
1203 }
1204
1205 return 0;
1206}
1207
Douglas Gregord5058122010-02-11 01:19:42 +00001208void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001209 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1210 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001211 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001212 }
1213
1214 Init = I;
1215}
1216
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001217VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001218 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001219 return cast<VarDecl>(MSI->getInstantiatedFrom());
1220
1221 return 0;
1222}
1223
Douglas Gregor3c74d412009-10-14 20:14:33 +00001224TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001225 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001226 return MSI->getTemplateSpecializationKind();
1227
1228 return TSK_Undeclared;
1229}
1230
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001231MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001232 return getASTContext().getInstantiatedFromStaticDataMember(this);
1233}
1234
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001235void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1236 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001237 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001238 assert(MSI && "Not an instantiated static data member?");
1239 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001240 if (TSK != TSK_ExplicitSpecialization &&
1241 PointOfInstantiation.isValid() &&
1242 MSI->getPointOfInstantiation().isInvalid())
1243 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001244}
1245
Sebastian Redl833ef452010-01-26 22:01:41 +00001246//===----------------------------------------------------------------------===//
1247// ParmVarDecl Implementation
1248//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001249
Sebastian Redl833ef452010-01-26 22:01:41 +00001250ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1251 SourceLocation L, IdentifierInfo *Id,
1252 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001253 StorageClass S, StorageClass SCAsWritten,
1254 Expr *DefArg) {
1255 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1256 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001257}
1258
Sebastian Redl833ef452010-01-26 22:01:41 +00001259Expr *ParmVarDecl::getDefaultArg() {
1260 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1261 assert(!hasUninstantiatedDefaultArg() &&
1262 "Default argument is not yet instantiated!");
1263
1264 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001265 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001266 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001267
Sebastian Redl833ef452010-01-26 22:01:41 +00001268 return Arg;
1269}
1270
1271unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001272 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001273 return E->getNumTemporaries();
1274
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001275 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001276}
1277
Sebastian Redl833ef452010-01-26 22:01:41 +00001278CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1279 assert(getNumDefaultArgTemporaries() &&
1280 "Default arguments does not have any temporaries!");
1281
John McCall5d413782010-12-06 08:20:24 +00001282 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001283 return E->getTemporary(i);
1284}
1285
1286SourceRange ParmVarDecl::getDefaultArgRange() const {
1287 if (const Expr *E = getInit())
1288 return E->getSourceRange();
1289
1290 if (hasUninstantiatedDefaultArg())
1291 return getUninstantiatedDefaultArg()->getSourceRange();
1292
1293 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001294}
1295
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001296bool ParmVarDecl::isParameterPack() const {
1297 return isa<PackExpansionType>(getType());
1298}
1299
Nuno Lopes394ec982008-12-17 23:39:55 +00001300//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001301// FunctionDecl Implementation
1302//===----------------------------------------------------------------------===//
1303
Douglas Gregorb11aad82011-02-19 18:51:44 +00001304void FunctionDecl::getNameForDiagnostic(std::string &S,
1305 const PrintingPolicy &Policy,
1306 bool Qualified) const {
1307 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1308 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1309 if (TemplateArgs)
1310 S += TemplateSpecializationType::PrintTemplateArgumentList(
1311 TemplateArgs->data(),
1312 TemplateArgs->size(),
1313 Policy);
1314
1315}
1316
Ted Kremenek186a0742010-04-29 16:49:01 +00001317bool FunctionDecl::isVariadic() const {
1318 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1319 return FT->isVariadic();
1320 return false;
1321}
1322
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001323bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1324 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1325 if (I->Body) {
1326 Definition = *I;
1327 return true;
1328 }
1329 }
1330
1331 return false;
1332}
1333
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001334Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001335 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1336 if (I->Body) {
1337 Definition = *I;
1338 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001339 }
1340 }
1341
1342 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001343}
1344
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001345void FunctionDecl::setBody(Stmt *B) {
1346 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001347 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001348 EndRangeLoc = B->getLocEnd();
1349}
1350
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001351void FunctionDecl::setPure(bool P) {
1352 IsPure = P;
1353 if (P)
1354 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1355 Parent->markedVirtualFunctionPure();
1356}
1357
Douglas Gregor16618f22009-09-12 00:17:51 +00001358bool FunctionDecl::isMain() const {
1359 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001360 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001361 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001362 getIdentifier() && getIdentifier()->isStr("main");
1363}
1364
Douglas Gregor16618f22009-09-12 00:17:51 +00001365bool FunctionDecl::isExternC() const {
1366 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001367 // In C, any non-static, non-overloadable function has external
1368 // linkage.
1369 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001370 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001371
Chandler Carruth4322a282011-02-25 00:05:02 +00001372 const DeclContext *DC = getDeclContext();
1373 if (DC->isRecord())
1374 return false;
1375
1376 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001377 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1378 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001379 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001380 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001381
1382 break;
1383 }
1384 }
1385
Douglas Gregorbff62032010-10-21 16:57:46 +00001386 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001387}
1388
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001389bool FunctionDecl::isGlobal() const {
1390 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1391 return Method->isStatic();
1392
John McCall8e7d6562010-08-26 03:08:43 +00001393 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001394 return false;
1395
Mike Stump11289f42009-09-09 15:08:12 +00001396 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001397 DC->isNamespace();
1398 DC = DC->getParent()) {
1399 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1400 if (!Namespace->getDeclName())
1401 return false;
1402 break;
1403 }
1404 }
1405
1406 return true;
1407}
1408
Sebastian Redl833ef452010-01-26 22:01:41 +00001409void
1410FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1411 redeclarable_base::setPreviousDeclaration(PrevDecl);
1412
1413 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1414 FunctionTemplateDecl *PrevFunTmpl
1415 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1416 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1417 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1418 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001419
1420 if (PrevDecl->IsInline)
1421 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001422}
1423
1424const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1425 return getFirstDeclaration();
1426}
1427
1428FunctionDecl *FunctionDecl::getCanonicalDecl() {
1429 return getFirstDeclaration();
1430}
1431
Douglas Gregorbf62d642010-12-06 18:36:25 +00001432void FunctionDecl::setStorageClass(StorageClass SC) {
1433 assert(isLegalForFunction(SC));
1434 if (getStorageClass() != SC)
1435 ClearLinkageCache();
1436
1437 SClass = SC;
1438}
1439
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001440/// \brief Returns a value indicating whether this function
1441/// corresponds to a builtin function.
1442///
1443/// The function corresponds to a built-in function if it is
1444/// declared at translation scope or within an extern "C" block and
1445/// its name matches with the name of a builtin. The returned value
1446/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001447/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001448/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001449unsigned FunctionDecl::getBuiltinID() const {
1450 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001451 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1452 return 0;
1453
1454 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1455 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1456 return BuiltinID;
1457
1458 // This function has the name of a known C library
1459 // function. Determine whether it actually refers to the C library
1460 // function or whether it just has the same name.
1461
Douglas Gregora908e7f2009-02-17 03:23:10 +00001462 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001463 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001464 return 0;
1465
Douglas Gregore711f702009-02-14 18:57:46 +00001466 // If this function is at translation-unit scope and we're not in
1467 // C++, it refers to the C library function.
1468 if (!Context.getLangOptions().CPlusPlus &&
1469 getDeclContext()->isTranslationUnit())
1470 return BuiltinID;
1471
1472 // If the function is in an extern "C" linkage specification and is
1473 // not marked "overloadable", it's the real function.
1474 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001475 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001476 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001477 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001478 return BuiltinID;
1479
1480 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001481 return 0;
1482}
1483
1484
Chris Lattner47c0d002009-04-25 06:03:53 +00001485/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001486/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001487/// after it has been created.
1488unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001489 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001490 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001491 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001492 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001493
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001494}
1495
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001496void FunctionDecl::setParams(ASTContext &C,
1497 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001498 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001499 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001500
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001501 // Zero params -> null pointer.
1502 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001503 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001504 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001505 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001506
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001507 // Update source range. The check below allows us to set EndRangeLoc before
1508 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001509 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001510 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001511 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001512}
Chris Lattner41943152007-01-25 04:52:46 +00001513
Chris Lattner58258242008-04-10 02:22:51 +00001514/// getMinRequiredArguments - Returns the minimum number of arguments
1515/// needed to call this function. This may be fewer than the number of
1516/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001517/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001518unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001519 if (!getASTContext().getLangOptions().CPlusPlus)
1520 return getNumParams();
1521
Douglas Gregor7825bf32011-01-06 22:09:01 +00001522 unsigned NumRequiredArgs = getNumParams();
1523
1524 // If the last parameter is a parameter pack, we don't need an argument for
1525 // it.
1526 if (NumRequiredArgs > 0 &&
1527 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1528 --NumRequiredArgs;
1529
1530 // If this parameter has a default argument, we don't need an argument for
1531 // it.
1532 while (NumRequiredArgs > 0 &&
1533 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001534 --NumRequiredArgs;
1535
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001536 // We might have parameter packs before the end. These can't be deduced,
1537 // but they can still handle multiple arguments.
1538 unsigned ArgIdx = NumRequiredArgs;
1539 while (ArgIdx > 0) {
1540 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1541 NumRequiredArgs = ArgIdx;
1542
1543 --ArgIdx;
1544 }
1545
Chris Lattner58258242008-04-10 02:22:51 +00001546 return NumRequiredArgs;
1547}
1548
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001549bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001550 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001551 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001552
1553 if (isa<CXXMethodDecl>(this)) {
1554 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1555 return true;
1556 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001557
1558 switch (getTemplateSpecializationKind()) {
1559 case TSK_Undeclared:
1560 case TSK_ExplicitSpecialization:
1561 return false;
1562
1563 case TSK_ImplicitInstantiation:
1564 case TSK_ExplicitInstantiationDeclaration:
1565 case TSK_ExplicitInstantiationDefinition:
1566 // Handle below.
1567 break;
1568 }
1569
1570 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001571 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001572 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001573 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001574
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001575 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001576 return PatternDecl->isInlined();
1577
1578 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001579}
1580
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001581/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001582/// definition will be externally visible.
1583///
1584/// Inline function definitions are always available for inlining optimizations.
1585/// However, depending on the language dialect, declaration specifiers, and
1586/// attributes, the definition of an inline function may or may not be
1587/// "externally" visible to other translation units in the program.
1588///
1589/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001590/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001591/// inline definition becomes externally visible (C99 6.7.4p6).
1592///
1593/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1594/// definition, we use the GNU semantics for inline, which are nearly the
1595/// opposite of C99 semantics. In particular, "inline" by itself will create
1596/// an externally visible symbol, but "extern inline" will not create an
1597/// externally visible symbol.
1598bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1599 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001600 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001601 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001602
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001603 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001604 // If it's not the case that both 'inline' and 'extern' are
1605 // specified on the definition, then this inline definition is
1606 // externally visible.
1607 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1608 return true;
1609
1610 // If any declaration is 'inline' but not 'extern', then this definition
1611 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001612 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1613 Redecl != RedeclEnd;
1614 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001615 if (Redecl->isInlineSpecified() &&
1616 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001617 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001618 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001619
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001620 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001621 }
1622
1623 // C99 6.7.4p6:
1624 // [...] If all of the file scope declarations for a function in a
1625 // translation unit include the inline function specifier without extern,
1626 // then the definition in that translation unit is an inline definition.
1627 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1628 Redecl != RedeclEnd;
1629 ++Redecl) {
1630 // Only consider file-scope declarations in this test.
1631 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1632 continue;
1633
John McCall8e7d6562010-08-26 03:08:43 +00001634 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001635 return true; // Not an inline definition
1636 }
1637
1638 // C99 6.7.4p6:
1639 // An inline definition does not provide an external definition for the
1640 // function, and does not forbid an external definition in another
1641 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001642 return false;
1643}
1644
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001645/// getOverloadedOperator - Which C++ overloaded operator this
1646/// function represents, if any.
1647OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001648 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1649 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001650 else
1651 return OO_None;
1652}
1653
Alexis Huntc88db062010-01-13 09:01:02 +00001654/// getLiteralIdentifier - The literal suffix identifier this function
1655/// represents, if any.
1656const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1657 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1658 return getDeclName().getCXXLiteralIdentifier();
1659 else
1660 return 0;
1661}
1662
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001663FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1664 if (TemplateOrSpecialization.isNull())
1665 return TK_NonTemplate;
1666 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1667 return TK_FunctionTemplate;
1668 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1669 return TK_MemberSpecialization;
1670 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1671 return TK_FunctionTemplateSpecialization;
1672 if (TemplateOrSpecialization.is
1673 <DependentFunctionTemplateSpecializationInfo*>())
1674 return TK_DependentFunctionTemplateSpecialization;
1675
1676 assert(false && "Did we miss a TemplateOrSpecialization type?");
1677 return TK_NonTemplate;
1678}
1679
Douglas Gregord801b062009-10-07 23:56:10 +00001680FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001681 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001682 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1683
1684 return 0;
1685}
1686
Douglas Gregor06db9f52009-10-12 20:18:28 +00001687MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1688 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1689}
1690
Douglas Gregord801b062009-10-07 23:56:10 +00001691void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001692FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1693 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001694 TemplateSpecializationKind TSK) {
1695 assert(TemplateOrSpecialization.isNull() &&
1696 "Member function is already a specialization");
1697 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001698 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001699 TemplateOrSpecialization = Info;
1700}
1701
Douglas Gregorafca3b42009-10-27 20:53:28 +00001702bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001703 // If the function is invalid, it can't be implicitly instantiated.
1704 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001705 return false;
1706
1707 switch (getTemplateSpecializationKind()) {
1708 case TSK_Undeclared:
1709 case TSK_ExplicitSpecialization:
1710 case TSK_ExplicitInstantiationDefinition:
1711 return false;
1712
1713 case TSK_ImplicitInstantiation:
1714 return true;
1715
1716 case TSK_ExplicitInstantiationDeclaration:
1717 // Handled below.
1718 break;
1719 }
1720
1721 // Find the actual template from which we will instantiate.
1722 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001723 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001724 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001725 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001726
1727 // C++0x [temp.explicit]p9:
1728 // Except for inline functions, other explicit instantiation declarations
1729 // have the effect of suppressing the implicit instantiation of the entity
1730 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001731 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001732 return true;
1733
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001734 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001735}
1736
1737FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1738 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1739 while (Primary->getInstantiatedFromMemberTemplate()) {
1740 // If we have hit a point where the user provided a specialization of
1741 // this template, we're done looking.
1742 if (Primary->isMemberSpecialization())
1743 break;
1744
1745 Primary = Primary->getInstantiatedFromMemberTemplate();
1746 }
1747
1748 return Primary->getTemplatedDecl();
1749 }
1750
1751 return getInstantiatedFromMemberFunction();
1752}
1753
Douglas Gregor70d83e22009-06-29 17:30:29 +00001754FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001755 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001756 = TemplateOrSpecialization
1757 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001758 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001759 }
1760 return 0;
1761}
1762
1763const TemplateArgumentList *
1764FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001765 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001766 = TemplateOrSpecialization
1767 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001768 return Info->TemplateArguments;
1769 }
1770 return 0;
1771}
1772
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001773const TemplateArgumentListInfo *
1774FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1775 if (FunctionTemplateSpecializationInfo *Info
1776 = TemplateOrSpecialization
1777 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1778 return Info->TemplateArgumentsAsWritten;
1779 }
1780 return 0;
1781}
1782
Mike Stump11289f42009-09-09 15:08:12 +00001783void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001784FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1785 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001786 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001787 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001788 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001789 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1790 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001791 assert(TSK != TSK_Undeclared &&
1792 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001793 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001794 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001795 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001796 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1797 TemplateArgs,
1798 TemplateArgsAsWritten,
1799 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001800 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001801
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001802 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001803 // function template specializations.
1804 if (InsertPos)
1805 Template->getSpecializations().InsertNode(Info, InsertPos);
1806 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001807 // Try to insert the new node. If there is an existing node, leave it, the
1808 // set will contain the canonical decls while
1809 // FunctionTemplateDecl::findSpecialization will return
1810 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001811 FunctionTemplateSpecializationInfo *Existing
1812 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001813 (void)Existing;
1814 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1815 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001816 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001817}
1818
John McCallb9c78482010-04-08 09:05:18 +00001819void
1820FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1821 const UnresolvedSetImpl &Templates,
1822 const TemplateArgumentListInfo &TemplateArgs) {
1823 assert(TemplateOrSpecialization.isNull());
1824 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1825 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001826 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001827 void *Buffer = Context.Allocate(Size);
1828 DependentFunctionTemplateSpecializationInfo *Info =
1829 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1830 TemplateArgs);
1831 TemplateOrSpecialization = Info;
1832}
1833
1834DependentFunctionTemplateSpecializationInfo::
1835DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1836 const TemplateArgumentListInfo &TArgs)
1837 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1838
1839 d.NumTemplates = Ts.size();
1840 d.NumArgs = TArgs.size();
1841
1842 FunctionTemplateDecl **TsArray =
1843 const_cast<FunctionTemplateDecl**>(getTemplates());
1844 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1845 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1846
1847 TemplateArgumentLoc *ArgsArray =
1848 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1849 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1850 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1851}
1852
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001853TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001854 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001855 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001856 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001857 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001858 if (FTSInfo)
1859 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001860
Douglas Gregord801b062009-10-07 23:56:10 +00001861 MemberSpecializationInfo *MSInfo
1862 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1863 if (MSInfo)
1864 return MSInfo->getTemplateSpecializationKind();
1865
1866 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001867}
1868
Mike Stump11289f42009-09-09 15:08:12 +00001869void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001870FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1871 SourceLocation PointOfInstantiation) {
1872 if (FunctionTemplateSpecializationInfo *FTSInfo
1873 = TemplateOrSpecialization.dyn_cast<
1874 FunctionTemplateSpecializationInfo*>()) {
1875 FTSInfo->setTemplateSpecializationKind(TSK);
1876 if (TSK != TSK_ExplicitSpecialization &&
1877 PointOfInstantiation.isValid() &&
1878 FTSInfo->getPointOfInstantiation().isInvalid())
1879 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1880 } else if (MemberSpecializationInfo *MSInfo
1881 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1882 MSInfo->setTemplateSpecializationKind(TSK);
1883 if (TSK != TSK_ExplicitSpecialization &&
1884 PointOfInstantiation.isValid() &&
1885 MSInfo->getPointOfInstantiation().isInvalid())
1886 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1887 } else
1888 assert(false && "Function cannot have a template specialization kind");
1889}
1890
1891SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001892 if (FunctionTemplateSpecializationInfo *FTSInfo
1893 = TemplateOrSpecialization.dyn_cast<
1894 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001895 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001896 else if (MemberSpecializationInfo *MSInfo
1897 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001898 return MSInfo->getPointOfInstantiation();
1899
1900 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001901}
1902
Douglas Gregor6411b922009-09-11 20:15:17 +00001903bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001904 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00001905 return true;
1906
1907 // If this function was instantiated from a member function of a
1908 // class template, check whether that member function was defined out-of-line.
1909 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1910 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001911 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001912 return Definition->isOutOfLine();
1913 }
1914
1915 // If this function was instantiated from a function template,
1916 // check whether that function template was defined out-of-line.
1917 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1918 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001919 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001920 return Definition->isOutOfLine();
1921 }
1922
1923 return false;
1924}
1925
Chris Lattner59a25942008-03-31 00:36:02 +00001926//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001927// FieldDecl Implementation
1928//===----------------------------------------------------------------------===//
1929
Jay Foad39c79802011-01-12 09:06:06 +00001930FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1931 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl833ef452010-01-26 22:01:41 +00001932 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1933 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1934}
1935
1936bool FieldDecl::isAnonymousStructOrUnion() const {
1937 if (!isImplicit() || getDeclName())
1938 return false;
1939
1940 if (const RecordType *Record = getType()->getAs<RecordType>())
1941 return Record->getDecl()->isAnonymousStructOrUnion();
1942
1943 return false;
1944}
1945
John McCall4e819612011-01-20 07:57:12 +00001946unsigned FieldDecl::getFieldIndex() const {
1947 if (CachedFieldIndex) return CachedFieldIndex - 1;
1948
1949 unsigned index = 0;
1950 RecordDecl::field_iterator
1951 i = getParent()->field_begin(), e = getParent()->field_end();
1952 while (true) {
1953 assert(i != e && "failed to find field in parent!");
1954 if (*i == this)
1955 break;
1956
1957 ++i;
1958 ++index;
1959 }
1960
1961 CachedFieldIndex = index + 1;
1962 return index;
1963}
1964
Sebastian Redl833ef452010-01-26 22:01:41 +00001965//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001966// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001967//===----------------------------------------------------------------------===//
1968
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001969SourceLocation TagDecl::getOuterLocStart() const {
1970 return getTemplateOrInnerLocStart(this);
1971}
1972
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001973SourceRange TagDecl::getSourceRange() const {
1974 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001975 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001976}
1977
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001978TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001979 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001980}
1981
Douglas Gregora72a4e32010-05-19 18:39:18 +00001982void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1983 TypedefDeclOrQualifier = TDD;
1984 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00001985 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001986 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00001987}
1988
Douglas Gregordee1be82009-01-17 00:42:38 +00001989void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001990 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001991
1992 if (isa<CXXRecordDecl>(this)) {
1993 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1994 struct CXXRecordDecl::DefinitionData *Data =
1995 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001996 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1997 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001998 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001999}
2000
2001void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002002 assert((!isa<CXXRecordDecl>(this) ||
2003 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2004 "definition completed but not started");
2005
Douglas Gregordee1be82009-01-17 00:42:38 +00002006 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002007 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002008
2009 if (ASTMutationListener *L = getASTMutationListener())
2010 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002011}
2012
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002013TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002014 if (isDefinition())
2015 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002016 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2017 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002018
2019 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002020 R != REnd; ++R)
2021 if (R->isDefinition())
2022 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002023
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002024 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002025}
2026
Douglas Gregor14454802011-02-25 02:25:35 +00002027void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2028 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002029 // Make sure the extended qualifier info is allocated.
2030 if (!hasExtInfo())
2031 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2032 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002033 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002034 }
2035 else {
2036 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002037 if (hasExtInfo()) {
2038 getASTContext().Deallocate(getExtInfo());
2039 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2040 }
2041 }
2042}
2043
Ted Kremenek21475702008-09-05 17:16:31 +00002044//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002045// EnumDecl Implementation
2046//===----------------------------------------------------------------------===//
2047
2048EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2049 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002050 EnumDecl *PrevDecl, bool IsScoped,
2051 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002052 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002053 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002054 C.getTypeDeclType(Enum, PrevDecl);
2055 return Enum;
2056}
2057
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002058EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002059 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002060 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002061}
2062
Douglas Gregord5058122010-02-11 01:19:42 +00002063void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002064 QualType NewPromotionType,
2065 unsigned NumPositiveBits,
2066 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002067 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002068 if (!IntegerType)
2069 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002070 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002071 setNumPositiveBits(NumPositiveBits);
2072 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002073 TagDecl::completeDefinition();
2074}
2075
2076//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002077// RecordDecl Implementation
2078//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002079
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002080RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002081 IdentifierInfo *Id, RecordDecl *PrevDecl,
2082 SourceLocation TKL)
2083 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002084 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002085 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002086 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002087 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002088 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002089}
2090
Jay Foad39c79802011-01-12 09:06:06 +00002091RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00002092 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00002093 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002094
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002095 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00002096 C.getTypeDeclType(R, PrevDecl);
2097 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002098}
2099
Jay Foad39c79802011-01-12 09:06:06 +00002100RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002101 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2102 SourceLocation());
2103}
2104
Douglas Gregordfcad112009-03-25 15:59:44 +00002105bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002106 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002107 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2108}
2109
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002110RecordDecl::field_iterator RecordDecl::field_begin() const {
2111 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2112 LoadFieldsFromExternalStorage();
2113
2114 return field_iterator(decl_iterator(FirstDecl));
2115}
2116
Douglas Gregorb11aad82011-02-19 18:51:44 +00002117/// completeDefinition - Notes that the definition of this type is now
2118/// complete.
2119void RecordDecl::completeDefinition() {
2120 assert(!isDefinition() && "Cannot redefine record!");
2121 TagDecl::completeDefinition();
2122}
2123
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002124void RecordDecl::LoadFieldsFromExternalStorage() const {
2125 ExternalASTSource *Source = getASTContext().getExternalSource();
2126 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2127
2128 // Notify that we have a RecordDecl doing some initialization.
2129 ExternalASTSource::Deserializing TheFields(Source);
2130
2131 llvm::SmallVector<Decl*, 64> Decls;
2132 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2133 return;
2134
2135#ifndef NDEBUG
2136 // Check that all decls we got were FieldDecls.
2137 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2138 assert(isa<FieldDecl>(Decls[i]));
2139#endif
2140
2141 LoadedFieldsFromExternalStorage = true;
2142
2143 if (Decls.empty())
2144 return;
2145
2146 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2147}
2148
Steve Naroff415d3d52008-10-08 17:01:13 +00002149//===----------------------------------------------------------------------===//
2150// BlockDecl Implementation
2151//===----------------------------------------------------------------------===//
2152
Douglas Gregord5058122010-02-11 01:19:42 +00002153void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002154 unsigned NParms) {
2155 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002156
Steve Naroffc4b30e52009-03-13 16:56:44 +00002157 // Zero params -> null pointer.
2158 if (NParms) {
2159 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002160 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002161 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2162 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2163 }
2164}
2165
John McCall351762c2011-02-07 10:33:21 +00002166void BlockDecl::setCaptures(ASTContext &Context,
2167 const Capture *begin,
2168 const Capture *end,
2169 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002170 CapturesCXXThis = capturesCXXThis;
2171
2172 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002173 NumCaptures = 0;
2174 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002175 return;
2176 }
2177
John McCall351762c2011-02-07 10:33:21 +00002178 NumCaptures = end - begin;
2179
2180 // Avoid new Capture[] because we don't want to provide a default
2181 // constructor.
2182 size_t allocationSize = NumCaptures * sizeof(Capture);
2183 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2184 memcpy(buffer, begin, allocationSize);
2185 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002186}
Sebastian Redl833ef452010-01-26 22:01:41 +00002187
Douglas Gregor70226da2010-12-21 16:27:07 +00002188SourceRange BlockDecl::getSourceRange() const {
2189 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2190}
Sebastian Redl833ef452010-01-26 22:01:41 +00002191
2192//===----------------------------------------------------------------------===//
2193// Other Decl Allocation/Deallocation Method Implementations
2194//===----------------------------------------------------------------------===//
2195
2196TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2197 return new (C) TranslationUnitDecl(C);
2198}
2199
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002200LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2201 SourceLocation L, IdentifierInfo *II) {
2202 return new (C) LabelDecl(DC, L, II, 0);
2203}
2204
2205
Sebastian Redl833ef452010-01-26 22:01:41 +00002206NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2207 SourceLocation L, IdentifierInfo *Id) {
2208 return new (C) NamespaceDecl(DC, L, Id);
2209}
2210
Douglas Gregor417e87c2010-10-27 19:49:05 +00002211NamespaceDecl *NamespaceDecl::getNextNamespace() {
2212 return dyn_cast_or_null<NamespaceDecl>(
2213 NextNamespace.get(getASTContext().getExternalSource()));
2214}
2215
Sebastian Redl833ef452010-01-26 22:01:41 +00002216ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
John McCall550d13a2011-02-22 22:25:56 +00002217 SourceLocation loc,
2218 IdentifierInfo *name,
2219 QualType type) {
2220 return new (C) ImplicitParamDecl(DC, loc, name, type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002221}
2222
2223FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002224 const DeclarationNameInfo &NameInfo,
2225 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002226 StorageClass S, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002227 bool isInlineSpecified,
2228 bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002229 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002230 S, SCAsWritten, isInlineSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002231 New->HasWrittenPrototype = hasWrittenPrototype;
2232 return New;
2233}
2234
2235BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2236 return new (C) BlockDecl(DC, L);
2237}
2238
2239EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2240 SourceLocation L,
2241 IdentifierInfo *Id, QualType T,
2242 Expr *E, const llvm::APSInt &V) {
2243 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2244}
2245
Benjamin Kramer39593702010-11-21 14:11:41 +00002246IndirectFieldDecl *
2247IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2248 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2249 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002250 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2251}
2252
Douglas Gregorbe996932010-09-01 20:41:53 +00002253SourceRange EnumConstantDecl::getSourceRange() const {
2254 SourceLocation End = getLocation();
2255 if (Init)
2256 End = Init->getLocEnd();
2257 return SourceRange(getLocation(), End);
2258}
2259
Sebastian Redl833ef452010-01-26 22:01:41 +00002260TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2261 SourceLocation L, IdentifierInfo *Id,
2262 TypeSourceInfo *TInfo) {
2263 return new (C) TypedefDecl(DC, L, Id, TInfo);
2264}
2265
Sebastian Redl833ef452010-01-26 22:01:41 +00002266FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2267 SourceLocation L,
2268 StringLiteral *Str) {
2269 return new (C) FileScopeAsmDecl(DC, L, Str);
2270}