blob: 11614df500350c1c6d569639881e1f656c7e2071 [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
John McCall457a04e2010-10-22 21:05:15 +0000269 if (D->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000270 return LinkageInfo::uniqueExternal();
John McCallb7139c42010-10-28 04:18:25 +0000271
John McCall457a04e2010-10-22 21:05:15 +0000272 // Set up the defaults.
273
274 // C99 6.2.2p5:
275 // If the declaration of an identifier for an object has file
276 // scope and no storage-class specifier, its linkage is
277 // external.
John McCallc273f242010-10-30 11:50:40 +0000278 LinkageInfo LV;
279
John McCall07072662010-11-02 01:45:15 +0000280 if (F.ConsiderVisibilityAttributes) {
281 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
282 LV.setVisibility(GetVisibilityFromAttr(VA), true);
283 F.ConsiderGlobalVisibility = false;
John McCall2faf32c2010-12-10 02:59:44 +0000284 } else {
285 // If we're declared in a namespace with a visibility attribute,
286 // use that namespace's visibility, but don't call it explicit.
287 for (const DeclContext *DC = D->getDeclContext();
288 !isa<TranslationUnitDecl>(DC);
289 DC = DC->getParent()) {
290 if (!isa<NamespaceDecl>(DC)) continue;
291 if (const VisibilityAttr *VA =
292 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
293 LV.setVisibility(GetVisibilityFromAttr(VA), false);
294 F.ConsiderGlobalVisibility = false;
295 break;
296 }
297 }
John McCall07072662010-11-02 01:45:15 +0000298 }
John McCallc273f242010-10-30 11:50:40 +0000299 }
John McCall457a04e2010-10-22 21:05:15 +0000300
Douglas Gregorf73b2822009-11-25 22:24:25 +0000301 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000302
Douglas Gregorf73b2822009-11-25 22:24:25 +0000303 // A name having namespace scope has external linkage if it is the
304 // name of
305 //
306 // - an object or reference, unless it has internal linkage; or
307 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000308 // GCC applies the following optimization to variables and static
309 // data members, but not to functions:
310 //
John McCall457a04e2010-10-22 21:05:15 +0000311 // Modify the variable's LV by the LV of its type unless this is
312 // C or extern "C". This follows from [basic.link]p9:
313 // A type without linkage shall not be used as the type of a
314 // variable or function with external linkage unless
315 // - the entity has C language linkage, or
316 // - the entity is declared within an unnamed namespace, or
317 // - the entity is not used or is defined in the same
318 // translation unit.
319 // and [basic.link]p10:
320 // ...the types specified by all declarations referring to a
321 // given variable or function shall be identical...
322 // C does not have an equivalent rule.
323 //
John McCall5fe84122010-10-26 04:59:26 +0000324 // Ignore this if we've got an explicit attribute; the user
325 // probably knows what they're doing.
326 //
John McCall457a04e2010-10-22 21:05:15 +0000327 // Note that we don't want to make the variable non-external
328 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000329 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000330 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
331 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000332 return LinkageInfo::uniqueExternal();
333 if (!LV.visibilityExplicit())
334 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000335 }
336
John McCall23032652010-11-02 18:38:13 +0000337 if (Var->getStorageClass() == SC_PrivateExtern)
338 LV.setVisibility(HiddenVisibility, true);
339
Douglas Gregorf73b2822009-11-25 22:24:25 +0000340 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000341 (Var->getStorageClass() == SC_Extern ||
342 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000343
Douglas Gregorf73b2822009-11-25 22:24:25 +0000344 // C99 6.2.2p4:
345 // For an identifier declared with the storage-class specifier
346 // extern in a scope in which a prior declaration of that
347 // identifier is visible, if the prior declaration specifies
348 // internal or external linkage, the linkage of the identifier
349 // at the later declaration is the same as the linkage
350 // specified at the prior declaration. If no prior declaration
351 // is visible, or if the prior declaration specifies no
352 // linkage, then the identifier has external linkage.
353 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000354 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallc273f242010-10-30 11:50:40 +0000355 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
356 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000357 }
358 }
359
Douglas Gregorf73b2822009-11-25 22:24:25 +0000360 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000361 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000362 // In theory, we can modify the function's LV by the LV of its
363 // type unless it has C linkage (see comment above about variables
364 // for justification). In practice, GCC doesn't do this, so it's
365 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000366
John McCall23032652010-11-02 18:38:13 +0000367 if (Function->getStorageClass() == SC_PrivateExtern)
368 LV.setVisibility(HiddenVisibility, true);
369
Douglas Gregorf73b2822009-11-25 22:24:25 +0000370 // C99 6.2.2p5:
371 // If the declaration of an identifier for a function has no
372 // storage-class specifier, its linkage is determined exactly
373 // as if it were declared with the storage-class specifier
374 // extern.
375 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000376 (Function->getStorageClass() == SC_Extern ||
377 Function->getStorageClass() == SC_PrivateExtern ||
378 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000379 // C99 6.2.2p4:
380 // For an identifier declared with the storage-class specifier
381 // extern in a scope in which a prior declaration of that
382 // identifier is visible, if the prior declaration specifies
383 // internal or external linkage, the linkage of the identifier
384 // at the later declaration is the same as the linkage
385 // specified at the prior declaration. If no prior declaration
386 // is visible, or if the prior declaration specifies no
387 // linkage, then the identifier has external linkage.
388 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000389 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallc273f242010-10-30 11:50:40 +0000390 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
391 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000392 }
393 }
394
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000395 if (FunctionTemplateSpecializationInfo *SpecInfo
396 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000397 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
398 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000399 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000400 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000401 }
402
Douglas Gregorf73b2822009-11-25 22:24:25 +0000403 // - a named class (Clause 9), or an unnamed class defined in a
404 // typedef declaration in which the class has the typedef name
405 // for linkage purposes (7.1.3); or
406 // - a named enumeration (7.2), or an unnamed enumeration
407 // defined in a typedef declaration in which the enumeration
408 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000409 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
410 // Unnamed tags have no linkage.
411 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000412 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000413
John McCall457a04e2010-10-22 21:05:15 +0000414 // If this is a class template specialization, consider the
415 // linkage of the template and template arguments.
416 if (const ClassTemplateSpecializationDecl *Spec
417 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000418 // From the template.
419 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
420 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000421
John McCall457a04e2010-10-22 21:05:15 +0000422 // The arguments at which the template was instantiated.
423 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000424 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000425 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000426
John McCall5fe84122010-10-26 04:59:26 +0000427 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000428 if (F.ConsiderGlobalVisibility)
429 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000430 (Context.getLangOptions().CPlusPlus &&
431 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000432
Douglas Gregorf73b2822009-11-25 22:24:25 +0000433 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000434 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000435 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000436 if (!isExternalLinkage(EnumLV.linkage()))
437 return LinkageInfo::none();
438 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000439
440 // - a template, unless it is a function template that has
441 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000442 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000443 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000444
Douglas Gregorf73b2822009-11-25 22:24:25 +0000445 // - a namespace (7.3), unless it is declared within an unnamed
446 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000447 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
448 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000449
John McCall457a04e2010-10-22 21:05:15 +0000450 // By extension, we assign external linkage to Objective-C
451 // interfaces.
452 } else if (isa<ObjCInterfaceDecl>(D)) {
453 // fallout
454
455 // Everything not covered here has no linkage.
456 } else {
John McCallc273f242010-10-30 11:50:40 +0000457 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000458 }
459
460 // If we ended up with non-external linkage, visibility should
461 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000462 if (LV.linkage() != ExternalLinkage)
463 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000464
465 // If we didn't end up with hidden visibility, consider attributes
466 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000467 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000468 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000469
470 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000471}
472
John McCall07072662010-11-02 01:45:15 +0000473static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000474 // Only certain class members have linkage. Note that fields don't
475 // really have linkage, but it's convenient to say they do for the
476 // purposes of calculating linkage of pointer-to-data-member
477 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000478 if (!(isa<CXXMethodDecl>(D) ||
479 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000480 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000481 (isa<TagDecl>(D) &&
482 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000483 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000484
John McCall07072662010-11-02 01:45:15 +0000485 LinkageInfo LV;
486
487 // The flags we're going to use to compute the class's visibility.
488 LVFlags ClassF = F;
489
490 // If we have an explicit visibility attribute, merge that in.
491 if (F.ConsiderVisibilityAttributes) {
492 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
493 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
494
495 // Ignore global visibility later, but not this attribute.
496 F.ConsiderGlobalVisibility = false;
497
498 // Ignore both global visibility and attributes when computing our
499 // parent's visibility.
500 ClassF = F.onlyTemplateVisibility();
501 }
502 }
John McCallc273f242010-10-30 11:50:40 +0000503
504 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000505 // linkage.
506 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
507 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000508 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000509
510 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000511 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000512 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000513
John McCall8823c652010-08-13 08:35:10 +0000514 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000515 TemplateSpecializationKind TSK = TSK_Undeclared;
516
John McCall457a04e2010-10-22 21:05:15 +0000517 // If this is a method template specialization, use the linkage for
518 // the template parameters and arguments.
519 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000520 = MD->getTemplateSpecializationInfo()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000521 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallc273f242010-10-30 11:50:40 +0000522 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000523 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000524
525 TSK = Spec->getTemplateSpecializationKind();
526 } else if (MemberSpecializationInfo *MSI =
527 MD->getMemberSpecializationInfo()) {
528 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000529 }
530
John McCall37bb6c92010-10-29 22:22:43 +0000531 // If we're paying attention to global visibility, apply
532 // -finline-visibility-hidden if this is an inline method.
533 //
John McCallc273f242010-10-30 11:50:40 +0000534 // Note that ConsiderGlobalVisibility doesn't yet have information
535 // about whether containing classes have visibility attributes,
536 // and that's intentional.
537 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000538 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000539 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
540 // InlineVisibilityHidden only applies to definitions, and
541 // isInlined() only gives meaningful answers on definitions
542 // anyway.
543 const FunctionDecl *Def = 0;
544 if (MD->hasBody(Def) && Def->isInlined())
545 LV.setVisibility(HiddenVisibility);
546 }
John McCall457a04e2010-10-22 21:05:15 +0000547
John McCall37bb6c92010-10-29 22:22:43 +0000548 // Note that in contrast to basically every other situation, we
549 // *do* apply -fvisibility to method declarations.
550
551 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000552 if (const ClassTemplateSpecializationDecl *Spec
553 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
554 // Merge template argument/parameter information for member
555 // class template specializations.
Douglas Gregorbf62d642010-12-06 18:36:25 +0000556 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallc273f242010-10-30 11:50:40 +0000557 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000558 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000559 }
560
John McCall37bb6c92010-10-29 22:22:43 +0000561 // Static data members.
562 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000563 // Modify the variable's linkage by its type, but ignore the
564 // type's visibility unless it's a definition.
565 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
566 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000567 LV.mergeLinkage(UniqueExternalLinkage);
568 if (!LV.visibilityExplicit())
569 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000570 }
571
John McCall07072662010-11-02 01:45:15 +0000572 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000573
574 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000575 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000576 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000577 }
578
John McCall457a04e2010-10-22 21:05:15 +0000579 return LV;
John McCall8823c652010-08-13 08:35:10 +0000580}
581
John McCalld396b972011-02-08 19:01:05 +0000582static void clearLinkageForClass(const CXXRecordDecl *record) {
583 for (CXXRecordDecl::decl_iterator
584 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
585 Decl *child = *i;
586 if (isa<NamedDecl>(child))
587 cast<NamedDecl>(child)->ClearLinkageCache();
588 }
589}
590
591void NamedDecl::ClearLinkageCache() {
592 // Note that we can't skip clearing the linkage of children just
593 // because the parent doesn't have cached linkage: we don't cache
594 // when computing linkage for parent contexts.
595
596 HasCachedLinkage = 0;
597
598 // If we're changing the linkage of a class, we need to reset the
599 // linkage of child declarations, too.
600 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
601 clearLinkageForClass(record);
602
603 if (const ClassTemplateDecl *temp = dyn_cast<ClassTemplateDecl>(this)) {
604 // Clear linkage for the template pattern.
605 CXXRecordDecl *record = temp->getTemplatedDecl();
606 record->HasCachedLinkage = 0;
607 clearLinkageForClass(record);
608
609 // ...do we need to clear linkage for specializations, too?
610 }
611}
612
Douglas Gregorbf62d642010-12-06 18:36:25 +0000613Linkage NamedDecl::getLinkage() const {
614 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000615 assert(Linkage(CachedLinkage) ==
616 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000617 return Linkage(CachedLinkage);
618 }
619
620 CachedLinkage = getLVForDecl(this,
621 LVFlags::CreateOnlyDeclLinkage()).linkage();
622 HasCachedLinkage = 1;
623 return Linkage(CachedLinkage);
624}
625
John McCallc273f242010-10-30 11:50:40 +0000626LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000627 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000628 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000629 HasCachedLinkage = 1;
630 CachedLinkage = LI.linkage();
631 return LI;
John McCall033caa52010-10-29 00:29:13 +0000632}
Ted Kremenek926d8602010-04-20 23:15:35 +0000633
John McCall07072662010-11-02 01:45:15 +0000634static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000635 // Objective-C: treat all Objective-C declarations as having external
636 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000637 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000638 default:
639 break;
John McCall457a04e2010-10-22 21:05:15 +0000640 case Decl::TemplateTemplateParm: // count these as external
641 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000642 case Decl::ObjCAtDefsField:
643 case Decl::ObjCCategory:
644 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000645 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000646 case Decl::ObjCForwardProtocol:
647 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000648 case Decl::ObjCMethod:
649 case Decl::ObjCProperty:
650 case Decl::ObjCPropertyImpl:
651 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000652 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000653 }
654
Douglas Gregorf73b2822009-11-25 22:24:25 +0000655 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000656 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000657 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000658
659 // C++ [basic.link]p5:
660 // In addition, a member function, static data member, a named
661 // class or enumeration of class scope, or an unnamed class or
662 // enumeration defined in a class-scope typedef declaration such
663 // that the class or enumeration has the typedef name for linkage
664 // purposes (7.1.3), has external linkage if the name of the class
665 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000666 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000667 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000668
669 // C++ [basic.link]p6:
670 // The name of a function declared in block scope and the name of
671 // an object declared by a block scope extern declaration have
672 // linkage. If there is a visible declaration of an entity with
673 // linkage having the same name and type, ignoring entities
674 // declared outside the innermost enclosing namespace scope, the
675 // block scope declaration declares that same entity and receives
676 // the linkage of the previous declaration. If there is more than
677 // one such matching entity, the program is ill-formed. Otherwise,
678 // if no matching entity is found, the block scope entity receives
679 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000680 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
681 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000682 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000683 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000684
John McCallc273f242010-10-30 11:50:40 +0000685 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000686 if (Flags.ConsiderVisibilityAttributes) {
687 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
688 LV.setVisibility(GetVisibilityFromAttr(VA));
689 }
690
John McCall457a04e2010-10-22 21:05:15 +0000691 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000692 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000693 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
694 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000695 }
696
697 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000698 }
699
John McCall033caa52010-10-29 00:29:13 +0000700 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000701 if (Var->getStorageClass() == SC_Extern ||
702 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000703 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000704 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000705
John McCallc273f242010-10-30 11:50:40 +0000706 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000707 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000708 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000709 else if (Flags.ConsiderVisibilityAttributes) {
710 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
711 LV.setVisibility(GetVisibilityFromAttr(VA));
712 }
713
John McCall457a04e2010-10-22 21:05:15 +0000714 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000715 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000716 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
717 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000718 }
719
720 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000721 }
722 }
723
724 // C++ [basic.link]p6:
725 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000726 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000727}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000728
Douglas Gregor2ada0482009-02-04 17:27:36 +0000729std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000730 return getQualifiedNameAsString(getASTContext().getLangOptions());
731}
732
733std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000734 const DeclContext *Ctx = getDeclContext();
735
736 if (Ctx->isFunctionOrMethod())
737 return getNameAsString();
738
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000739 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
740 ContextsTy Contexts;
741
742 // Collect contexts.
743 while (Ctx && isa<NamedDecl>(Ctx)) {
744 Contexts.push_back(Ctx);
745 Ctx = Ctx->getParent();
746 };
747
748 std::string QualName;
749 llvm::raw_string_ostream OS(QualName);
750
751 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
752 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000753 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000754 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000755 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
756 std::string TemplateArgsStr
757 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000758 TemplateArgs.data(),
759 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000760 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000761 OS << Spec->getName() << TemplateArgsStr;
762 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000763 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000764 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000765 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000766 OS << ND;
767 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
768 if (!RD->getIdentifier())
769 OS << "<anonymous " << RD->getKindName() << '>';
770 else
771 OS << RD;
772 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000773 const FunctionProtoType *FT = 0;
774 if (FD->hasWrittenPrototype())
775 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
776
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000777 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000778 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000779 unsigned NumParams = FD->getNumParams();
780 for (unsigned i = 0; i < NumParams; ++i) {
781 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000782 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000783 std::string Param;
784 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000785 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000786 }
787
788 if (FT->isVariadic()) {
789 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000790 OS << ", ";
791 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000792 }
793 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000794 OS << ')';
795 } else {
796 OS << cast<NamedDecl>(*I);
797 }
798 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000799 }
800
John McCalla2a3f7d2010-03-16 21:48:18 +0000801 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000802 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000803 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000804 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000805
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000806 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000807}
808
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000809bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000810 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
811
Douglas Gregor889ceb72009-02-03 19:21:40 +0000812 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
813 // We want to keep it, unless it nominates same namespace.
814 if (getKind() == Decl::UsingDirective) {
815 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
816 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
817 }
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000819 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
820 // For function declarations, we keep track of redeclarations.
821 return FD->getPreviousDeclaration() == OldD;
822
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000823 // For function templates, the underlying function declarations are linked.
824 if (const FunctionTemplateDecl *FunctionTemplate
825 = dyn_cast<FunctionTemplateDecl>(this))
826 if (const FunctionTemplateDecl *OldFunctionTemplate
827 = dyn_cast<FunctionTemplateDecl>(OldD))
828 return FunctionTemplate->getTemplatedDecl()
829 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000830
Steve Naroffc4173fa2009-02-22 19:35:57 +0000831 // For method declarations, we keep track of redeclarations.
832 if (isa<ObjCMethodDecl>(this))
833 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000834
John McCall9f3059a2009-10-09 21:13:30 +0000835 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
836 return true;
837
John McCall3f746822009-11-17 05:59:44 +0000838 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
839 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
840 cast<UsingShadowDecl>(OldD)->getTargetDecl();
841
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000842 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
843 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
844 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
845
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000846 // For non-function declarations, if the declarations are of the
847 // same kind then this must be a redeclaration, or semantic analysis
848 // would not have given us the new declaration.
849 return this->getKind() == OldD->getKind();
850}
851
Douglas Gregoreddf4332009-02-24 20:03:32 +0000852bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000853 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000854}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000855
Anders Carlsson6915bf62009-06-26 06:29:23 +0000856NamedDecl *NamedDecl::getUnderlyingDecl() {
857 NamedDecl *ND = this;
858 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000859 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000860 ND = UD->getTargetDecl();
861 else if (ObjCCompatibleAliasDecl *AD
862 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
863 return AD->getClassInterface();
864 else
865 return ND;
866 }
867}
868
John McCalla8ae2222010-04-06 21:38:20 +0000869bool NamedDecl::isCXXInstanceMember() const {
870 assert(isCXXClassMember() &&
871 "checking whether non-member is instance member");
872
873 const NamedDecl *D = this;
874 if (isa<UsingShadowDecl>(D))
875 D = cast<UsingShadowDecl>(D)->getTargetDecl();
876
Francois Pichet783dd6e2010-11-21 06:08:52 +0000877 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000878 return true;
879 if (isa<CXXMethodDecl>(D))
880 return cast<CXXMethodDecl>(D)->isInstance();
881 if (isa<FunctionTemplateDecl>(D))
882 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
883 ->getTemplatedDecl())->isInstance();
884 return false;
885}
886
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000887//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000888// DeclaratorDecl Implementation
889//===----------------------------------------------------------------------===//
890
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000891template <typename DeclT>
892static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
893 if (decl->getNumTemplateParameterLists() > 0)
894 return decl->getTemplateParameterList(0)->getTemplateLoc();
895 else
896 return decl->getInnerLocStart();
897}
898
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000899SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000900 TypeSourceInfo *TSI = getTypeSourceInfo();
901 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000902 return SourceLocation();
903}
904
John McCall3e11ebe2010-03-15 10:12:16 +0000905void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
906 SourceRange QualifierRange) {
907 if (Qualifier) {
908 // Make sure the extended decl info is allocated.
909 if (!hasExtInfo()) {
910 // Save (non-extended) type source info pointer.
911 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
912 // Allocate external info struct.
913 DeclInfo = new (getASTContext()) ExtInfo;
914 // Restore savedTInfo into (extended) decl info.
915 getExtInfo()->TInfo = savedTInfo;
916 }
917 // Set qualifier info.
918 getExtInfo()->NNS = Qualifier;
919 getExtInfo()->NNSRange = QualifierRange;
920 }
921 else {
922 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
923 assert(QualifierRange.isInvalid());
924 if (hasExtInfo()) {
925 // Save type source info pointer.
926 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
927 // Deallocate the extended decl info.
928 getASTContext().Deallocate(getExtInfo());
929 // Restore savedTInfo into (non-extended) decl info.
930 DeclInfo = savedTInfo;
931 }
932 }
933}
934
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000935SourceLocation DeclaratorDecl::getOuterLocStart() const {
936 return getTemplateOrInnerLocStart(this);
937}
938
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000939void
Douglas Gregor20527e22010-06-15 17:44:38 +0000940QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
941 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000942 TemplateParameterList **TPLists) {
943 assert((NumTPLists == 0 || TPLists != 0) &&
944 "Empty array of template parameters with positive size!");
945 assert((NumTPLists == 0 || NNS) &&
946 "Nonempty array of template parameters with no qualifier!");
947
948 // Free previous template parameters (if any).
949 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000950 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000951 TemplParamLists = 0;
952 NumTemplParamLists = 0;
953 }
954 // Set info on matched template parameter lists (if any).
955 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000956 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000957 NumTemplParamLists = NumTPLists;
958 for (unsigned i = NumTPLists; i-- > 0; )
959 TemplParamLists[i] = TPLists[i];
960 }
961}
962
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000963//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000964// VarDecl Implementation
965//===----------------------------------------------------------------------===//
966
Sebastian Redl833ef452010-01-26 22:01:41 +0000967const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
968 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000969 case SC_None: break;
970 case SC_Auto: return "auto"; break;
971 case SC_Extern: return "extern"; break;
972 case SC_PrivateExtern: return "__private_extern__"; break;
973 case SC_Register: return "register"; break;
974 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000975 }
976
977 assert(0 && "Invalid storage class");
978 return 0;
979}
980
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000981VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000982 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000983 StorageClass S, StorageClass SCAsWritten) {
984 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000985}
986
Douglas Gregorbf62d642010-12-06 18:36:25 +0000987void VarDecl::setStorageClass(StorageClass SC) {
988 assert(isLegalForVariable(SC));
989 if (getStorageClass() != SC)
990 ClearLinkageCache();
991
992 SClass = SC;
993}
994
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000995SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000996 SourceLocation Start = getTypeSpecStartLoc();
997 if (Start.isInvalid())
998 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000999 return Start;
1000}
1001
1002SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001003 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001004 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1005 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001006}
1007
Sebastian Redl833ef452010-01-26 22:01:41 +00001008bool VarDecl::isExternC() const {
1009 ASTContext &Context = getASTContext();
1010 if (!Context.getLangOptions().CPlusPlus)
1011 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001012 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001013 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1014
1015 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
1016 DC = DC->getParent()) {
1017 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1018 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001019 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001020
1021 break;
1022 }
1023
1024 if (DC->isFunctionOrMethod())
1025 return false;
1026 }
1027
1028 return false;
1029}
1030
1031VarDecl *VarDecl::getCanonicalDecl() {
1032 return getFirstDeclaration();
1033}
1034
Sebastian Redl35351a92010-01-31 22:27:38 +00001035VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1036 // C++ [basic.def]p2:
1037 // A declaration is a definition unless [...] it contains the 'extern'
1038 // specifier or a linkage-specification and neither an initializer [...],
1039 // it declares a static data member in a class declaration [...].
1040 // C++ [temp.expl.spec]p15:
1041 // An explicit specialization of a static data member of a template is a
1042 // definition if the declaration includes an initializer; otherwise, it is
1043 // a declaration.
1044 if (isStaticDataMember()) {
1045 if (isOutOfLine() && (hasInit() ||
1046 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1047 return Definition;
1048 else
1049 return DeclarationOnly;
1050 }
1051 // C99 6.7p5:
1052 // A definition of an identifier is a declaration for that identifier that
1053 // [...] causes storage to be reserved for that object.
1054 // Note: that applies for all non-file-scope objects.
1055 // C99 6.9.2p1:
1056 // If the declaration of an identifier for an object has file scope and an
1057 // initializer, the declaration is an external definition for the identifier
1058 if (hasInit())
1059 return Definition;
1060 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1061 if (hasExternalStorage())
1062 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001063
John McCall8e7d6562010-08-26 03:08:43 +00001064 if (getStorageClassAsWritten() == SC_Extern ||
1065 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001066 for (const VarDecl *PrevVar = getPreviousDeclaration();
1067 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1068 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1069 return DeclarationOnly;
1070 }
1071 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001072 // C99 6.9.2p2:
1073 // A declaration of an object that has file scope without an initializer,
1074 // and without a storage class specifier or the scs 'static', constitutes
1075 // a tentative definition.
1076 // No such thing in C++.
1077 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1078 return TentativeDefinition;
1079
1080 // What's left is (in C, block-scope) declarations without initializers or
1081 // external storage. These are definitions.
1082 return Definition;
1083}
1084
Sebastian Redl35351a92010-01-31 22:27:38 +00001085VarDecl *VarDecl::getActingDefinition() {
1086 DefinitionKind Kind = isThisDeclarationADefinition();
1087 if (Kind != TentativeDefinition)
1088 return 0;
1089
Chris Lattner48eb14d2010-06-14 18:31:46 +00001090 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001091 VarDecl *First = getFirstDeclaration();
1092 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1093 I != E; ++I) {
1094 Kind = (*I)->isThisDeclarationADefinition();
1095 if (Kind == Definition)
1096 return 0;
1097 else if (Kind == TentativeDefinition)
1098 LastTentative = *I;
1099 }
1100 return LastTentative;
1101}
1102
1103bool VarDecl::isTentativeDefinitionNow() const {
1104 DefinitionKind Kind = isThisDeclarationADefinition();
1105 if (Kind != TentativeDefinition)
1106 return false;
1107
1108 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1109 if ((*I)->isThisDeclarationADefinition() == Definition)
1110 return false;
1111 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001112 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001113}
1114
Sebastian Redl5ca79842010-02-01 20:16:42 +00001115VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001116 VarDecl *First = getFirstDeclaration();
1117 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1118 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001119 if ((*I)->isThisDeclarationADefinition() == Definition)
1120 return *I;
1121 }
1122 return 0;
1123}
1124
John McCall37bb6c92010-10-29 22:22:43 +00001125VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1126 DefinitionKind Kind = DeclarationOnly;
1127
1128 const VarDecl *First = getFirstDeclaration();
1129 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1130 I != E; ++I)
1131 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1132
1133 return Kind;
1134}
1135
Sebastian Redl5ca79842010-02-01 20:16:42 +00001136const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001137 redecl_iterator I = redecls_begin(), E = redecls_end();
1138 while (I != E && !I->getInit())
1139 ++I;
1140
1141 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001142 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001143 return I->getInit();
1144 }
1145 return 0;
1146}
1147
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001148bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001149 if (Decl::isOutOfLine())
1150 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001151
1152 if (!isStaticDataMember())
1153 return false;
1154
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001155 // If this static data member was instantiated from a static data member of
1156 // a class template, check whether that static data member was defined
1157 // out-of-line.
1158 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1159 return VD->isOutOfLine();
1160
1161 return false;
1162}
1163
Douglas Gregor1d957a32009-10-27 18:42:08 +00001164VarDecl *VarDecl::getOutOfLineDefinition() {
1165 if (!isStaticDataMember())
1166 return 0;
1167
1168 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1169 RD != RDEnd; ++RD) {
1170 if (RD->getLexicalDeclContext()->isFileContext())
1171 return *RD;
1172 }
1173
1174 return 0;
1175}
1176
Douglas Gregord5058122010-02-11 01:19:42 +00001177void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001178 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1179 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001180 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001181 }
1182
1183 Init = I;
1184}
1185
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001186VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001187 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001188 return cast<VarDecl>(MSI->getInstantiatedFrom());
1189
1190 return 0;
1191}
1192
Douglas Gregor3c74d412009-10-14 20:14:33 +00001193TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001194 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001195 return MSI->getTemplateSpecializationKind();
1196
1197 return TSK_Undeclared;
1198}
1199
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001200MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001201 return getASTContext().getInstantiatedFromStaticDataMember(this);
1202}
1203
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001204void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1205 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001206 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001207 assert(MSI && "Not an instantiated static data member?");
1208 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001209 if (TSK != TSK_ExplicitSpecialization &&
1210 PointOfInstantiation.isValid() &&
1211 MSI->getPointOfInstantiation().isInvalid())
1212 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001213}
1214
Sebastian Redl833ef452010-01-26 22:01:41 +00001215//===----------------------------------------------------------------------===//
1216// ParmVarDecl Implementation
1217//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001218
Sebastian Redl833ef452010-01-26 22:01:41 +00001219ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1220 SourceLocation L, IdentifierInfo *Id,
1221 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001222 StorageClass S, StorageClass SCAsWritten,
1223 Expr *DefArg) {
1224 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1225 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001226}
1227
Sebastian Redl833ef452010-01-26 22:01:41 +00001228Expr *ParmVarDecl::getDefaultArg() {
1229 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1230 assert(!hasUninstantiatedDefaultArg() &&
1231 "Default argument is not yet instantiated!");
1232
1233 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001234 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001235 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001236
Sebastian Redl833ef452010-01-26 22:01:41 +00001237 return Arg;
1238}
1239
1240unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001241 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001242 return E->getNumTemporaries();
1243
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001244 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001245}
1246
Sebastian Redl833ef452010-01-26 22:01:41 +00001247CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1248 assert(getNumDefaultArgTemporaries() &&
1249 "Default arguments does not have any temporaries!");
1250
John McCall5d413782010-12-06 08:20:24 +00001251 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001252 return E->getTemporary(i);
1253}
1254
1255SourceRange ParmVarDecl::getDefaultArgRange() const {
1256 if (const Expr *E = getInit())
1257 return E->getSourceRange();
1258
1259 if (hasUninstantiatedDefaultArg())
1260 return getUninstantiatedDefaultArg()->getSourceRange();
1261
1262 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001263}
1264
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001265bool ParmVarDecl::isParameterPack() const {
1266 return isa<PackExpansionType>(getType());
1267}
1268
Nuno Lopes394ec982008-12-17 23:39:55 +00001269//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001270// FunctionDecl Implementation
1271//===----------------------------------------------------------------------===//
1272
John McCalle1f2ec22009-09-11 06:45:03 +00001273void FunctionDecl::getNameForDiagnostic(std::string &S,
1274 const PrintingPolicy &Policy,
1275 bool Qualified) const {
1276 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1277 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1278 if (TemplateArgs)
1279 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001280 TemplateArgs->data(),
1281 TemplateArgs->size(),
John McCalle1f2ec22009-09-11 06:45:03 +00001282 Policy);
1283
1284}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001285
Ted Kremenek186a0742010-04-29 16:49:01 +00001286bool FunctionDecl::isVariadic() const {
1287 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1288 return FT->isVariadic();
1289 return false;
1290}
1291
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001292bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1293 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1294 if (I->Body) {
1295 Definition = *I;
1296 return true;
1297 }
1298 }
1299
1300 return false;
1301}
1302
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001303Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001304 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1305 if (I->Body) {
1306 Definition = *I;
1307 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001308 }
1309 }
1310
1311 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001312}
1313
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001314void FunctionDecl::setBody(Stmt *B) {
1315 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001316 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001317 EndRangeLoc = B->getLocEnd();
1318}
1319
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001320void FunctionDecl::setPure(bool P) {
1321 IsPure = P;
1322 if (P)
1323 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1324 Parent->markedVirtualFunctionPure();
1325}
1326
Douglas Gregor16618f22009-09-12 00:17:51 +00001327bool FunctionDecl::isMain() const {
1328 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001329 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001330 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001331 getIdentifier() && getIdentifier()->isStr("main");
1332}
1333
Douglas Gregor16618f22009-09-12 00:17:51 +00001334bool FunctionDecl::isExternC() const {
1335 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001336 // In C, any non-static, non-overloadable function has external
1337 // linkage.
1338 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001339 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001340
Mike Stump11289f42009-09-09 15:08:12 +00001341 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001342 DC = DC->getParent()) {
1343 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1344 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001345 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001346 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001347
1348 break;
1349 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001350
1351 if (DC->isRecord())
1352 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001353 }
1354
Douglas Gregorbff62032010-10-21 16:57:46 +00001355 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001356}
1357
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001358bool FunctionDecl::isGlobal() const {
1359 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1360 return Method->isStatic();
1361
John McCall8e7d6562010-08-26 03:08:43 +00001362 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001363 return false;
1364
Mike Stump11289f42009-09-09 15:08:12 +00001365 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001366 DC->isNamespace();
1367 DC = DC->getParent()) {
1368 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1369 if (!Namespace->getDeclName())
1370 return false;
1371 break;
1372 }
1373 }
1374
1375 return true;
1376}
1377
Sebastian Redl833ef452010-01-26 22:01:41 +00001378void
1379FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1380 redeclarable_base::setPreviousDeclaration(PrevDecl);
1381
1382 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1383 FunctionTemplateDecl *PrevFunTmpl
1384 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1385 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1386 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1387 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001388
1389 if (PrevDecl->IsInline)
1390 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001391}
1392
1393const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1394 return getFirstDeclaration();
1395}
1396
1397FunctionDecl *FunctionDecl::getCanonicalDecl() {
1398 return getFirstDeclaration();
1399}
1400
Douglas Gregorbf62d642010-12-06 18:36:25 +00001401void FunctionDecl::setStorageClass(StorageClass SC) {
1402 assert(isLegalForFunction(SC));
1403 if (getStorageClass() != SC)
1404 ClearLinkageCache();
1405
1406 SClass = SC;
1407}
1408
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001409/// \brief Returns a value indicating whether this function
1410/// corresponds to a builtin function.
1411///
1412/// The function corresponds to a built-in function if it is
1413/// declared at translation scope or within an extern "C" block and
1414/// its name matches with the name of a builtin. The returned value
1415/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001416/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001417/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001418unsigned FunctionDecl::getBuiltinID() const {
1419 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001420 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1421 return 0;
1422
1423 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1424 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1425 return BuiltinID;
1426
1427 // This function has the name of a known C library
1428 // function. Determine whether it actually refers to the C library
1429 // function or whether it just has the same name.
1430
Douglas Gregora908e7f2009-02-17 03:23:10 +00001431 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001432 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001433 return 0;
1434
Douglas Gregore711f702009-02-14 18:57:46 +00001435 // If this function is at translation-unit scope and we're not in
1436 // C++, it refers to the C library function.
1437 if (!Context.getLangOptions().CPlusPlus &&
1438 getDeclContext()->isTranslationUnit())
1439 return BuiltinID;
1440
1441 // If the function is in an extern "C" linkage specification and is
1442 // not marked "overloadable", it's the real function.
1443 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001444 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001445 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001446 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001447 return BuiltinID;
1448
1449 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001450 return 0;
1451}
1452
1453
Chris Lattner47c0d002009-04-25 06:03:53 +00001454/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001455/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001456/// after it has been created.
1457unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001458 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001459 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001460 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001461 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001462
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001463}
1464
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001465void FunctionDecl::setParams(ASTContext &C,
1466 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001467 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001468 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001469
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001470 // Zero params -> null pointer.
1471 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001472 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001473 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001474 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001475
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001476 // Update source range. The check below allows us to set EndRangeLoc before
1477 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001478 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001479 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001480 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001481}
Chris Lattner41943152007-01-25 04:52:46 +00001482
Chris Lattner58258242008-04-10 02:22:51 +00001483/// getMinRequiredArguments - Returns the minimum number of arguments
1484/// needed to call this function. This may be fewer than the number of
1485/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001486/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001487unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001488 if (!getASTContext().getLangOptions().CPlusPlus)
1489 return getNumParams();
1490
Douglas Gregor7825bf32011-01-06 22:09:01 +00001491 unsigned NumRequiredArgs = getNumParams();
1492
1493 // If the last parameter is a parameter pack, we don't need an argument for
1494 // it.
1495 if (NumRequiredArgs > 0 &&
1496 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1497 --NumRequiredArgs;
1498
1499 // If this parameter has a default argument, we don't need an argument for
1500 // it.
1501 while (NumRequiredArgs > 0 &&
1502 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001503 --NumRequiredArgs;
1504
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001505 // We might have parameter packs before the end. These can't be deduced,
1506 // but they can still handle multiple arguments.
1507 unsigned ArgIdx = NumRequiredArgs;
1508 while (ArgIdx > 0) {
1509 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1510 NumRequiredArgs = ArgIdx;
1511
1512 --ArgIdx;
1513 }
1514
Chris Lattner58258242008-04-10 02:22:51 +00001515 return NumRequiredArgs;
1516}
1517
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001518bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001519 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001520 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001521
1522 if (isa<CXXMethodDecl>(this)) {
1523 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1524 return true;
1525 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001526
1527 switch (getTemplateSpecializationKind()) {
1528 case TSK_Undeclared:
1529 case TSK_ExplicitSpecialization:
1530 return false;
1531
1532 case TSK_ImplicitInstantiation:
1533 case TSK_ExplicitInstantiationDeclaration:
1534 case TSK_ExplicitInstantiationDefinition:
1535 // Handle below.
1536 break;
1537 }
1538
1539 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001540 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001541 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001542 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001543
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001544 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001545 return PatternDecl->isInlined();
1546
1547 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001548}
1549
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001550/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001551/// definition will be externally visible.
1552///
1553/// Inline function definitions are always available for inlining optimizations.
1554/// However, depending on the language dialect, declaration specifiers, and
1555/// attributes, the definition of an inline function may or may not be
1556/// "externally" visible to other translation units in the program.
1557///
1558/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001559/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001560/// inline definition becomes externally visible (C99 6.7.4p6).
1561///
1562/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1563/// definition, we use the GNU semantics for inline, which are nearly the
1564/// opposite of C99 semantics. In particular, "inline" by itself will create
1565/// an externally visible symbol, but "extern inline" will not create an
1566/// externally visible symbol.
1567bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1568 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001569 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001570 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001571
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001572 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001573 // If it's not the case that both 'inline' and 'extern' are
1574 // specified on the definition, then this inline definition is
1575 // externally visible.
1576 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1577 return true;
1578
1579 // If any declaration is 'inline' but not 'extern', then this definition
1580 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001581 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1582 Redecl != RedeclEnd;
1583 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001584 if (Redecl->isInlineSpecified() &&
1585 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001586 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001587 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001588
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001589 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001590 }
1591
1592 // C99 6.7.4p6:
1593 // [...] If all of the file scope declarations for a function in a
1594 // translation unit include the inline function specifier without extern,
1595 // then the definition in that translation unit is an inline definition.
1596 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1597 Redecl != RedeclEnd;
1598 ++Redecl) {
1599 // Only consider file-scope declarations in this test.
1600 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1601 continue;
1602
John McCall8e7d6562010-08-26 03:08:43 +00001603 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001604 return true; // Not an inline definition
1605 }
1606
1607 // C99 6.7.4p6:
1608 // An inline definition does not provide an external definition for the
1609 // function, and does not forbid an external definition in another
1610 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001611 return false;
1612}
1613
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001614/// getOverloadedOperator - Which C++ overloaded operator this
1615/// function represents, if any.
1616OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001617 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1618 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001619 else
1620 return OO_None;
1621}
1622
Alexis Huntc88db062010-01-13 09:01:02 +00001623/// getLiteralIdentifier - The literal suffix identifier this function
1624/// represents, if any.
1625const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1626 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1627 return getDeclName().getCXXLiteralIdentifier();
1628 else
1629 return 0;
1630}
1631
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001632FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1633 if (TemplateOrSpecialization.isNull())
1634 return TK_NonTemplate;
1635 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1636 return TK_FunctionTemplate;
1637 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1638 return TK_MemberSpecialization;
1639 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1640 return TK_FunctionTemplateSpecialization;
1641 if (TemplateOrSpecialization.is
1642 <DependentFunctionTemplateSpecializationInfo*>())
1643 return TK_DependentFunctionTemplateSpecialization;
1644
1645 assert(false && "Did we miss a TemplateOrSpecialization type?");
1646 return TK_NonTemplate;
1647}
1648
Douglas Gregord801b062009-10-07 23:56:10 +00001649FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001650 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001651 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1652
1653 return 0;
1654}
1655
Douglas Gregor06db9f52009-10-12 20:18:28 +00001656MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1657 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1658}
1659
Douglas Gregord801b062009-10-07 23:56:10 +00001660void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001661FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1662 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001663 TemplateSpecializationKind TSK) {
1664 assert(TemplateOrSpecialization.isNull() &&
1665 "Member function is already a specialization");
1666 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001667 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001668 TemplateOrSpecialization = Info;
1669}
1670
Douglas Gregorafca3b42009-10-27 20:53:28 +00001671bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001672 // If the function is invalid, it can't be implicitly instantiated.
1673 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001674 return false;
1675
1676 switch (getTemplateSpecializationKind()) {
1677 case TSK_Undeclared:
1678 case TSK_ExplicitSpecialization:
1679 case TSK_ExplicitInstantiationDefinition:
1680 return false;
1681
1682 case TSK_ImplicitInstantiation:
1683 return true;
1684
1685 case TSK_ExplicitInstantiationDeclaration:
1686 // Handled below.
1687 break;
1688 }
1689
1690 // Find the actual template from which we will instantiate.
1691 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001692 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001693 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001694 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001695
1696 // C++0x [temp.explicit]p9:
1697 // Except for inline functions, other explicit instantiation declarations
1698 // have the effect of suppressing the implicit instantiation of the entity
1699 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001700 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001701 return true;
1702
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001703 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001704}
1705
1706FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1707 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1708 while (Primary->getInstantiatedFromMemberTemplate()) {
1709 // If we have hit a point where the user provided a specialization of
1710 // this template, we're done looking.
1711 if (Primary->isMemberSpecialization())
1712 break;
1713
1714 Primary = Primary->getInstantiatedFromMemberTemplate();
1715 }
1716
1717 return Primary->getTemplatedDecl();
1718 }
1719
1720 return getInstantiatedFromMemberFunction();
1721}
1722
Douglas Gregor70d83e22009-06-29 17:30:29 +00001723FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001724 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001725 = TemplateOrSpecialization
1726 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001727 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001728 }
1729 return 0;
1730}
1731
1732const TemplateArgumentList *
1733FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001734 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001735 = TemplateOrSpecialization
1736 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001737 return Info->TemplateArguments;
1738 }
1739 return 0;
1740}
1741
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001742const TemplateArgumentListInfo *
1743FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1744 if (FunctionTemplateSpecializationInfo *Info
1745 = TemplateOrSpecialization
1746 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1747 return Info->TemplateArgumentsAsWritten;
1748 }
1749 return 0;
1750}
1751
Mike Stump11289f42009-09-09 15:08:12 +00001752void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001753FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1754 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001755 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001756 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001757 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001758 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1759 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001760 assert(TSK != TSK_Undeclared &&
1761 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001762 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001763 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001764 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001765 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1766 TemplateArgs,
1767 TemplateArgsAsWritten,
1768 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001769 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001770
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001771 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001772 // function template specializations.
1773 if (InsertPos)
1774 Template->getSpecializations().InsertNode(Info, InsertPos);
1775 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001776 // Try to insert the new node. If there is an existing node, leave it, the
1777 // set will contain the canonical decls while
1778 // FunctionTemplateDecl::findSpecialization will return
1779 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001780 FunctionTemplateSpecializationInfo *Existing
1781 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001782 (void)Existing;
1783 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1784 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001785 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001786}
1787
John McCallb9c78482010-04-08 09:05:18 +00001788void
1789FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1790 const UnresolvedSetImpl &Templates,
1791 const TemplateArgumentListInfo &TemplateArgs) {
1792 assert(TemplateOrSpecialization.isNull());
1793 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1794 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001795 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001796 void *Buffer = Context.Allocate(Size);
1797 DependentFunctionTemplateSpecializationInfo *Info =
1798 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1799 TemplateArgs);
1800 TemplateOrSpecialization = Info;
1801}
1802
1803DependentFunctionTemplateSpecializationInfo::
1804DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1805 const TemplateArgumentListInfo &TArgs)
1806 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1807
1808 d.NumTemplates = Ts.size();
1809 d.NumArgs = TArgs.size();
1810
1811 FunctionTemplateDecl **TsArray =
1812 const_cast<FunctionTemplateDecl**>(getTemplates());
1813 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1814 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1815
1816 TemplateArgumentLoc *ArgsArray =
1817 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1818 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1819 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1820}
1821
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001822TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001823 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001824 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001825 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001826 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001827 if (FTSInfo)
1828 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001829
Douglas Gregord801b062009-10-07 23:56:10 +00001830 MemberSpecializationInfo *MSInfo
1831 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1832 if (MSInfo)
1833 return MSInfo->getTemplateSpecializationKind();
1834
1835 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001836}
1837
Mike Stump11289f42009-09-09 15:08:12 +00001838void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001839FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1840 SourceLocation PointOfInstantiation) {
1841 if (FunctionTemplateSpecializationInfo *FTSInfo
1842 = TemplateOrSpecialization.dyn_cast<
1843 FunctionTemplateSpecializationInfo*>()) {
1844 FTSInfo->setTemplateSpecializationKind(TSK);
1845 if (TSK != TSK_ExplicitSpecialization &&
1846 PointOfInstantiation.isValid() &&
1847 FTSInfo->getPointOfInstantiation().isInvalid())
1848 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1849 } else if (MemberSpecializationInfo *MSInfo
1850 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1851 MSInfo->setTemplateSpecializationKind(TSK);
1852 if (TSK != TSK_ExplicitSpecialization &&
1853 PointOfInstantiation.isValid() &&
1854 MSInfo->getPointOfInstantiation().isInvalid())
1855 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1856 } else
1857 assert(false && "Function cannot have a template specialization kind");
1858}
1859
1860SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001861 if (FunctionTemplateSpecializationInfo *FTSInfo
1862 = TemplateOrSpecialization.dyn_cast<
1863 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001864 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001865 else if (MemberSpecializationInfo *MSInfo
1866 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001867 return MSInfo->getPointOfInstantiation();
1868
1869 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001870}
1871
Douglas Gregor6411b922009-09-11 20:15:17 +00001872bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001873 if (Decl::isOutOfLine())
1874 return true;
1875
1876 // If this function was instantiated from a member function of a
1877 // class template, check whether that member function was defined out-of-line.
1878 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1879 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001880 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001881 return Definition->isOutOfLine();
1882 }
1883
1884 // If this function was instantiated from a function template,
1885 // check whether that function template was defined out-of-line.
1886 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1887 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001888 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001889 return Definition->isOutOfLine();
1890 }
1891
1892 return false;
1893}
1894
Chris Lattner59a25942008-03-31 00:36:02 +00001895//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001896// FieldDecl Implementation
1897//===----------------------------------------------------------------------===//
1898
Jay Foad39c79802011-01-12 09:06:06 +00001899FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1900 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl833ef452010-01-26 22:01:41 +00001901 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1902 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1903}
1904
1905bool FieldDecl::isAnonymousStructOrUnion() const {
1906 if (!isImplicit() || getDeclName())
1907 return false;
1908
1909 if (const RecordType *Record = getType()->getAs<RecordType>())
1910 return Record->getDecl()->isAnonymousStructOrUnion();
1911
1912 return false;
1913}
1914
John McCall4e819612011-01-20 07:57:12 +00001915unsigned FieldDecl::getFieldIndex() const {
1916 if (CachedFieldIndex) return CachedFieldIndex - 1;
1917
1918 unsigned index = 0;
1919 RecordDecl::field_iterator
1920 i = getParent()->field_begin(), e = getParent()->field_end();
1921 while (true) {
1922 assert(i != e && "failed to find field in parent!");
1923 if (*i == this)
1924 break;
1925
1926 ++i;
1927 ++index;
1928 }
1929
1930 CachedFieldIndex = index + 1;
1931 return index;
1932}
1933
Sebastian Redl833ef452010-01-26 22:01:41 +00001934//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001935// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001936//===----------------------------------------------------------------------===//
1937
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001938SourceLocation TagDecl::getOuterLocStart() const {
1939 return getTemplateOrInnerLocStart(this);
1940}
1941
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001942SourceRange TagDecl::getSourceRange() const {
1943 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001944 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001945}
1946
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001947TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001948 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001949}
1950
Douglas Gregora72a4e32010-05-19 18:39:18 +00001951void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1952 TypedefDeclOrQualifier = TDD;
1953 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00001954 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001955 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00001956}
1957
Douglas Gregordee1be82009-01-17 00:42:38 +00001958void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001959 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001960
1961 if (isa<CXXRecordDecl>(this)) {
1962 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1963 struct CXXRecordDecl::DefinitionData *Data =
1964 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001965 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1966 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001967 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001968}
1969
1970void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001971 assert((!isa<CXXRecordDecl>(this) ||
1972 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1973 "definition completed but not started");
1974
Douglas Gregordee1be82009-01-17 00:42:38 +00001975 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001976 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001977
1978 if (ASTMutationListener *L = getASTMutationListener())
1979 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001980}
1981
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001982TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001983 if (isDefinition())
1984 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001985 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1986 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001987
1988 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001989 R != REnd; ++R)
1990 if (R->isDefinition())
1991 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001992
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001993 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001994}
1995
John McCall3e11ebe2010-03-15 10:12:16 +00001996void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1997 SourceRange QualifierRange) {
1998 if (Qualifier) {
1999 // Make sure the extended qualifier info is allocated.
2000 if (!hasExtInfo())
2001 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2002 // Set qualifier info.
2003 getExtInfo()->NNS = Qualifier;
2004 getExtInfo()->NNSRange = QualifierRange;
2005 }
2006 else {
2007 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2008 assert(QualifierRange.isInvalid());
2009 if (hasExtInfo()) {
2010 getASTContext().Deallocate(getExtInfo());
2011 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2012 }
2013 }
2014}
2015
Ted Kremenek21475702008-09-05 17:16:31 +00002016//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002017// EnumDecl Implementation
2018//===----------------------------------------------------------------------===//
2019
2020EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2021 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002022 EnumDecl *PrevDecl, bool IsScoped,
2023 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002024 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002025 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002026 C.getTypeDeclType(Enum, PrevDecl);
2027 return Enum;
2028}
2029
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002030EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002031 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002032 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002033}
2034
Douglas Gregord5058122010-02-11 01:19:42 +00002035void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002036 QualType NewPromotionType,
2037 unsigned NumPositiveBits,
2038 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002039 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002040 if (!IntegerType)
2041 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002042 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002043 setNumPositiveBits(NumPositiveBits);
2044 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002045 TagDecl::completeDefinition();
2046}
2047
2048//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002049// RecordDecl Implementation
2050//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002051
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002052RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002053 IdentifierInfo *Id, RecordDecl *PrevDecl,
2054 SourceLocation TKL)
2055 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002056 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002057 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002058 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002059 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002060 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002061}
2062
Jay Foad39c79802011-01-12 09:06:06 +00002063RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00002064 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00002065 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002066
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002067 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00002068 C.getTypeDeclType(R, PrevDecl);
2069 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002070}
2071
Jay Foad39c79802011-01-12 09:06:06 +00002072RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002073 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2074 SourceLocation());
2075}
2076
Douglas Gregordfcad112009-03-25 15:59:44 +00002077bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002078 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002079 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2080}
2081
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002082RecordDecl::field_iterator RecordDecl::field_begin() const {
2083 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2084 LoadFieldsFromExternalStorage();
2085
2086 return field_iterator(decl_iterator(FirstDecl));
2087}
2088
Douglas Gregor91f84212008-12-11 16:49:14 +00002089/// completeDefinition - Notes that the definition of this type is now
2090/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00002091void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00002092 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00002093 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00002094}
Steve Naroffcc321422007-03-26 23:09:51 +00002095
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002096void RecordDecl::LoadFieldsFromExternalStorage() const {
2097 ExternalASTSource *Source = getASTContext().getExternalSource();
2098 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2099
2100 // Notify that we have a RecordDecl doing some initialization.
2101 ExternalASTSource::Deserializing TheFields(Source);
2102
2103 llvm::SmallVector<Decl*, 64> Decls;
2104 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2105 return;
2106
2107#ifndef NDEBUG
2108 // Check that all decls we got were FieldDecls.
2109 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2110 assert(isa<FieldDecl>(Decls[i]));
2111#endif
2112
2113 LoadedFieldsFromExternalStorage = true;
2114
2115 if (Decls.empty())
2116 return;
2117
2118 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2119}
2120
Steve Naroff415d3d52008-10-08 17:01:13 +00002121//===----------------------------------------------------------------------===//
2122// BlockDecl Implementation
2123//===----------------------------------------------------------------------===//
2124
Douglas Gregord5058122010-02-11 01:19:42 +00002125void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002126 unsigned NParms) {
2127 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002128
Steve Naroffc4b30e52009-03-13 16:56:44 +00002129 // Zero params -> null pointer.
2130 if (NParms) {
2131 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002132 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002133 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2134 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2135 }
2136}
2137
John McCall351762c2011-02-07 10:33:21 +00002138void BlockDecl::setCaptures(ASTContext &Context,
2139 const Capture *begin,
2140 const Capture *end,
2141 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002142 CapturesCXXThis = capturesCXXThis;
2143
2144 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002145 NumCaptures = 0;
2146 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002147 return;
2148 }
2149
John McCall351762c2011-02-07 10:33:21 +00002150 NumCaptures = end - begin;
2151
2152 // Avoid new Capture[] because we don't want to provide a default
2153 // constructor.
2154 size_t allocationSize = NumCaptures * sizeof(Capture);
2155 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2156 memcpy(buffer, begin, allocationSize);
2157 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002158}
Sebastian Redl833ef452010-01-26 22:01:41 +00002159
Douglas Gregor70226da2010-12-21 16:27:07 +00002160SourceRange BlockDecl::getSourceRange() const {
2161 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2162}
Sebastian Redl833ef452010-01-26 22:01:41 +00002163
2164//===----------------------------------------------------------------------===//
2165// Other Decl Allocation/Deallocation Method Implementations
2166//===----------------------------------------------------------------------===//
2167
2168TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2169 return new (C) TranslationUnitDecl(C);
2170}
2171
2172NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2173 SourceLocation L, IdentifierInfo *Id) {
2174 return new (C) NamespaceDecl(DC, L, Id);
2175}
2176
Douglas Gregor417e87c2010-10-27 19:49:05 +00002177NamespaceDecl *NamespaceDecl::getNextNamespace() {
2178 return dyn_cast_or_null<NamespaceDecl>(
2179 NextNamespace.get(getASTContext().getExternalSource()));
2180}
2181
Sebastian Redl833ef452010-01-26 22:01:41 +00002182ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2183 SourceLocation L, IdentifierInfo *Id, QualType T) {
2184 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2185}
2186
2187FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002188 const DeclarationNameInfo &NameInfo,
2189 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002190 StorageClass S, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002191 bool isInlineSpecified,
2192 bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002193 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002194 S, SCAsWritten, isInlineSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002195 New->HasWrittenPrototype = hasWrittenPrototype;
2196 return New;
2197}
2198
2199BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2200 return new (C) BlockDecl(DC, L);
2201}
2202
2203EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2204 SourceLocation L,
2205 IdentifierInfo *Id, QualType T,
2206 Expr *E, const llvm::APSInt &V) {
2207 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2208}
2209
Benjamin Kramer39593702010-11-21 14:11:41 +00002210IndirectFieldDecl *
2211IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2212 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2213 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002214 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2215}
2216
Douglas Gregorbe996932010-09-01 20:41:53 +00002217SourceRange EnumConstantDecl::getSourceRange() const {
2218 SourceLocation End = getLocation();
2219 if (Init)
2220 End = Init->getLocEnd();
2221 return SourceRange(getLocation(), End);
2222}
2223
Sebastian Redl833ef452010-01-26 22:01:41 +00002224TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2225 SourceLocation L, IdentifierInfo *Id,
2226 TypeSourceInfo *TInfo) {
2227 return new (C) TypedefDecl(DC, L, Id, TInfo);
2228}
2229
Sebastian Redl833ef452010-01-26 22:01:41 +00002230FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2231 SourceLocation L,
2232 StringLiteral *Str) {
2233 return new (C) FileScopeAsmDecl(DC, L, Str);
2234}