blob: 56db8c7e330b27abe1ac3bb000651e632f733e7a [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
John McCallf768aa72011-02-10 06:50:24 +0000395 // In C++, then if the type of the function uses a type with
396 // unique-external linkage, it's not legally usable from outside
397 // this translation unit. However, we should use the C linkage
398 // rules instead for extern "C" declarations.
399 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
400 Function->getType()->getLinkage() == UniqueExternalLinkage)
401 return LinkageInfo::uniqueExternal();
402
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000403 if (FunctionTemplateSpecializationInfo *SpecInfo
404 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000405 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
406 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000407 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000408 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000409 }
410
Douglas Gregorf73b2822009-11-25 22:24:25 +0000411 // - a named class (Clause 9), or an unnamed class defined in a
412 // typedef declaration in which the class has the typedef name
413 // for linkage purposes (7.1.3); or
414 // - a named enumeration (7.2), or an unnamed enumeration
415 // defined in a typedef declaration in which the enumeration
416 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000417 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
418 // Unnamed tags have no linkage.
419 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000420 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000421
John McCall457a04e2010-10-22 21:05:15 +0000422 // If this is a class template specialization, consider the
423 // linkage of the template and template arguments.
424 if (const ClassTemplateSpecializationDecl *Spec
425 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000426 // From the template.
427 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
428 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000429
John McCall457a04e2010-10-22 21:05:15 +0000430 // The arguments at which the template was instantiated.
431 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000432 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000433 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000434
John McCall5fe84122010-10-26 04:59:26 +0000435 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000436 if (F.ConsiderGlobalVisibility)
437 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000438 (Context.getLangOptions().CPlusPlus &&
439 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000440
Douglas Gregorf73b2822009-11-25 22:24:25 +0000441 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000442 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000443 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000444 if (!isExternalLinkage(EnumLV.linkage()))
445 return LinkageInfo::none();
446 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000447
448 // - a template, unless it is a function template that has
449 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000450 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000451 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000452
Douglas Gregorf73b2822009-11-25 22:24:25 +0000453 // - a namespace (7.3), unless it is declared within an unnamed
454 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000455 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
456 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000457
John McCall457a04e2010-10-22 21:05:15 +0000458 // By extension, we assign external linkage to Objective-C
459 // interfaces.
460 } else if (isa<ObjCInterfaceDecl>(D)) {
461 // fallout
462
463 // Everything not covered here has no linkage.
464 } else {
John McCallc273f242010-10-30 11:50:40 +0000465 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000466 }
467
468 // If we ended up with non-external linkage, visibility should
469 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000470 if (LV.linkage() != ExternalLinkage)
471 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000472
473 // If we didn't end up with hidden visibility, consider attributes
474 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000475 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000476 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000477
478 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000479}
480
John McCall07072662010-11-02 01:45:15 +0000481static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000482 // Only certain class members have linkage. Note that fields don't
483 // really have linkage, but it's convenient to say they do for the
484 // purposes of calculating linkage of pointer-to-data-member
485 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000486 if (!(isa<CXXMethodDecl>(D) ||
487 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000488 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000489 (isa<TagDecl>(D) &&
490 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000491 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000492
John McCall07072662010-11-02 01:45:15 +0000493 LinkageInfo LV;
494
495 // The flags we're going to use to compute the class's visibility.
496 LVFlags ClassF = F;
497
498 // If we have an explicit visibility attribute, merge that in.
499 if (F.ConsiderVisibilityAttributes) {
500 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
501 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
502
503 // Ignore global visibility later, but not this attribute.
504 F.ConsiderGlobalVisibility = false;
505
506 // Ignore both global visibility and attributes when computing our
507 // parent's visibility.
508 ClassF = F.onlyTemplateVisibility();
509 }
510 }
John McCallc273f242010-10-30 11:50:40 +0000511
512 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000513 // linkage.
514 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
515 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000516 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000517
518 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000519 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000520 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000521
John McCall8823c652010-08-13 08:35:10 +0000522 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000523 // If the type of the function uses a type with unique-external
524 // linkage, it's not legally usable from outside this translation unit.
525 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
526 return LinkageInfo::uniqueExternal();
527
John McCall37bb6c92010-10-29 22:22:43 +0000528 TemplateSpecializationKind TSK = TSK_Undeclared;
529
John McCall457a04e2010-10-22 21:05:15 +0000530 // If this is a method template specialization, use the linkage for
531 // the template parameters and arguments.
532 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000533 = MD->getTemplateSpecializationInfo()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000534 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallc273f242010-10-30 11:50:40 +0000535 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000536 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000537
538 TSK = Spec->getTemplateSpecializationKind();
539 } else if (MemberSpecializationInfo *MSI =
540 MD->getMemberSpecializationInfo()) {
541 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000542 }
543
John McCall37bb6c92010-10-29 22:22:43 +0000544 // If we're paying attention to global visibility, apply
545 // -finline-visibility-hidden if this is an inline method.
546 //
John McCallc273f242010-10-30 11:50:40 +0000547 // Note that ConsiderGlobalVisibility doesn't yet have information
548 // about whether containing classes have visibility attributes,
549 // and that's intentional.
550 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000551 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000552 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
553 // InlineVisibilityHidden only applies to definitions, and
554 // isInlined() only gives meaningful answers on definitions
555 // anyway.
556 const FunctionDecl *Def = 0;
557 if (MD->hasBody(Def) && Def->isInlined())
558 LV.setVisibility(HiddenVisibility);
559 }
John McCall457a04e2010-10-22 21:05:15 +0000560
John McCall37bb6c92010-10-29 22:22:43 +0000561 // Note that in contrast to basically every other situation, we
562 // *do* apply -fvisibility to method declarations.
563
564 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000565 if (const ClassTemplateSpecializationDecl *Spec
566 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
567 // Merge template argument/parameter information for member
568 // class template specializations.
Douglas Gregorbf62d642010-12-06 18:36:25 +0000569 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallc273f242010-10-30 11:50:40 +0000570 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000571 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000572 }
573
John McCall37bb6c92010-10-29 22:22:43 +0000574 // Static data members.
575 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000576 // Modify the variable's linkage by its type, but ignore the
577 // type's visibility unless it's a definition.
578 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
579 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000580 LV.mergeLinkage(UniqueExternalLinkage);
581 if (!LV.visibilityExplicit())
582 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000583 }
584
John McCall07072662010-11-02 01:45:15 +0000585 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000586
587 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000588 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000589 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000590 }
591
John McCall457a04e2010-10-22 21:05:15 +0000592 return LV;
John McCall8823c652010-08-13 08:35:10 +0000593}
594
John McCalld396b972011-02-08 19:01:05 +0000595static void clearLinkageForClass(const CXXRecordDecl *record) {
596 for (CXXRecordDecl::decl_iterator
597 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
598 Decl *child = *i;
599 if (isa<NamedDecl>(child))
600 cast<NamedDecl>(child)->ClearLinkageCache();
601 }
602}
603
604void NamedDecl::ClearLinkageCache() {
605 // Note that we can't skip clearing the linkage of children just
606 // because the parent doesn't have cached linkage: we don't cache
607 // when computing linkage for parent contexts.
608
609 HasCachedLinkage = 0;
610
611 // If we're changing the linkage of a class, we need to reset the
612 // linkage of child declarations, too.
613 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
614 clearLinkageForClass(record);
615
John McCall83779672011-02-19 02:53:41 +0000616 if (ClassTemplateDecl *temp =
617 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000618 // Clear linkage for the template pattern.
619 CXXRecordDecl *record = temp->getTemplatedDecl();
620 record->HasCachedLinkage = 0;
621 clearLinkageForClass(record);
622
John McCall83779672011-02-19 02:53:41 +0000623 // We need to clear linkage for specializations, too.
624 for (ClassTemplateDecl::spec_iterator
625 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
626 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000627 }
John McCall83779672011-02-19 02:53:41 +0000628
629 // Clear cached linkage for function template decls, too.
630 if (FunctionTemplateDecl *temp =
631 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
632 for (FunctionTemplateDecl::spec_iterator
633 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
634 i->ClearLinkageCache();
635
John McCalld396b972011-02-08 19:01:05 +0000636}
637
Douglas Gregorbf62d642010-12-06 18:36:25 +0000638Linkage NamedDecl::getLinkage() const {
639 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000640 assert(Linkage(CachedLinkage) ==
641 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000642 return Linkage(CachedLinkage);
643 }
644
645 CachedLinkage = getLVForDecl(this,
646 LVFlags::CreateOnlyDeclLinkage()).linkage();
647 HasCachedLinkage = 1;
648 return Linkage(CachedLinkage);
649}
650
John McCallc273f242010-10-30 11:50:40 +0000651LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000652 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000653 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000654 HasCachedLinkage = 1;
655 CachedLinkage = LI.linkage();
656 return LI;
John McCall033caa52010-10-29 00:29:13 +0000657}
Ted Kremenek926d8602010-04-20 23:15:35 +0000658
John McCall07072662010-11-02 01:45:15 +0000659static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000660 // Objective-C: treat all Objective-C declarations as having external
661 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000662 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000663 default:
664 break;
John McCall457a04e2010-10-22 21:05:15 +0000665 case Decl::TemplateTemplateParm: // count these as external
666 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000667 case Decl::ObjCAtDefsField:
668 case Decl::ObjCCategory:
669 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000670 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000671 case Decl::ObjCForwardProtocol:
672 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000673 case Decl::ObjCMethod:
674 case Decl::ObjCProperty:
675 case Decl::ObjCPropertyImpl:
676 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000677 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000678 }
679
Douglas Gregorf73b2822009-11-25 22:24:25 +0000680 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000681 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000682 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000683
684 // C++ [basic.link]p5:
685 // In addition, a member function, static data member, a named
686 // class or enumeration of class scope, or an unnamed class or
687 // enumeration defined in a class-scope typedef declaration such
688 // that the class or enumeration has the typedef name for linkage
689 // purposes (7.1.3), has external linkage if the name of the class
690 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000691 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000692 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000693
694 // C++ [basic.link]p6:
695 // The name of a function declared in block scope and the name of
696 // an object declared by a block scope extern declaration have
697 // linkage. If there is a visible declaration of an entity with
698 // linkage having the same name and type, ignoring entities
699 // declared outside the innermost enclosing namespace scope, the
700 // block scope declaration declares that same entity and receives
701 // the linkage of the previous declaration. If there is more than
702 // one such matching entity, the program is ill-formed. Otherwise,
703 // if no matching entity is found, the block scope entity receives
704 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000705 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
706 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000707 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000708 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000709
John McCallc273f242010-10-30 11:50:40 +0000710 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000711 if (Flags.ConsiderVisibilityAttributes) {
712 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
713 LV.setVisibility(GetVisibilityFromAttr(VA));
714 }
715
John McCall457a04e2010-10-22 21:05:15 +0000716 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000717 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000718 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
719 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000720 }
721
722 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000723 }
724
John McCall033caa52010-10-29 00:29:13 +0000725 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000726 if (Var->getStorageClass() == SC_Extern ||
727 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000728 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000729 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000730
John McCallc273f242010-10-30 11:50:40 +0000731 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000732 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000733 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000734 else if (Flags.ConsiderVisibilityAttributes) {
735 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
736 LV.setVisibility(GetVisibilityFromAttr(VA));
737 }
738
John McCall457a04e2010-10-22 21:05:15 +0000739 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000740 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000741 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
742 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000743 }
744
745 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000746 }
747 }
748
749 // C++ [basic.link]p6:
750 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000751 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000752}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000753
Douglas Gregor2ada0482009-02-04 17:27:36 +0000754std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000755 return getQualifiedNameAsString(getASTContext().getLangOptions());
756}
757
758std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000759 const DeclContext *Ctx = getDeclContext();
760
761 if (Ctx->isFunctionOrMethod())
762 return getNameAsString();
763
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000764 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
765 ContextsTy Contexts;
766
767 // Collect contexts.
768 while (Ctx && isa<NamedDecl>(Ctx)) {
769 Contexts.push_back(Ctx);
770 Ctx = Ctx->getParent();
771 };
772
773 std::string QualName;
774 llvm::raw_string_ostream OS(QualName);
775
776 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
777 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000778 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000779 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000780 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
781 std::string TemplateArgsStr
782 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000783 TemplateArgs.data(),
784 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000785 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000786 OS << Spec->getName() << TemplateArgsStr;
787 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000788 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000789 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000790 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000791 OS << ND;
792 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
793 if (!RD->getIdentifier())
794 OS << "<anonymous " << RD->getKindName() << '>';
795 else
796 OS << RD;
797 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000798 const FunctionProtoType *FT = 0;
799 if (FD->hasWrittenPrototype())
800 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
801
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000802 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000803 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000804 unsigned NumParams = FD->getNumParams();
805 for (unsigned i = 0; i < NumParams; ++i) {
806 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000807 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000808 std::string Param;
809 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000810 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000811 }
812
813 if (FT->isVariadic()) {
814 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000815 OS << ", ";
816 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000817 }
818 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000819 OS << ')';
820 } else {
821 OS << cast<NamedDecl>(*I);
822 }
823 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000824 }
825
John McCalla2a3f7d2010-03-16 21:48:18 +0000826 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000827 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000828 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000829 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000830
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000831 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000832}
833
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000834bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000835 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
836
Douglas Gregor889ceb72009-02-03 19:21:40 +0000837 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
838 // We want to keep it, unless it nominates same namespace.
839 if (getKind() == Decl::UsingDirective) {
840 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
841 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
842 }
Mike Stump11289f42009-09-09 15:08:12 +0000843
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000844 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
845 // For function declarations, we keep track of redeclarations.
846 return FD->getPreviousDeclaration() == OldD;
847
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000848 // For function templates, the underlying function declarations are linked.
849 if (const FunctionTemplateDecl *FunctionTemplate
850 = dyn_cast<FunctionTemplateDecl>(this))
851 if (const FunctionTemplateDecl *OldFunctionTemplate
852 = dyn_cast<FunctionTemplateDecl>(OldD))
853 return FunctionTemplate->getTemplatedDecl()
854 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000855
Steve Naroffc4173fa2009-02-22 19:35:57 +0000856 // For method declarations, we keep track of redeclarations.
857 if (isa<ObjCMethodDecl>(this))
858 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000859
John McCall9f3059a2009-10-09 21:13:30 +0000860 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
861 return true;
862
John McCall3f746822009-11-17 05:59:44 +0000863 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
864 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
865 cast<UsingShadowDecl>(OldD)->getTargetDecl();
866
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000867 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
868 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
869 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
870
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000871 // For non-function declarations, if the declarations are of the
872 // same kind then this must be a redeclaration, or semantic analysis
873 // would not have given us the new declaration.
874 return this->getKind() == OldD->getKind();
875}
876
Douglas Gregoreddf4332009-02-24 20:03:32 +0000877bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000878 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000879}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000880
Anders Carlsson6915bf62009-06-26 06:29:23 +0000881NamedDecl *NamedDecl::getUnderlyingDecl() {
882 NamedDecl *ND = this;
883 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000884 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000885 ND = UD->getTargetDecl();
886 else if (ObjCCompatibleAliasDecl *AD
887 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
888 return AD->getClassInterface();
889 else
890 return ND;
891 }
892}
893
John McCalla8ae2222010-04-06 21:38:20 +0000894bool NamedDecl::isCXXInstanceMember() const {
895 assert(isCXXClassMember() &&
896 "checking whether non-member is instance member");
897
898 const NamedDecl *D = this;
899 if (isa<UsingShadowDecl>(D))
900 D = cast<UsingShadowDecl>(D)->getTargetDecl();
901
Francois Pichet783dd6e2010-11-21 06:08:52 +0000902 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000903 return true;
904 if (isa<CXXMethodDecl>(D))
905 return cast<CXXMethodDecl>(D)->isInstance();
906 if (isa<FunctionTemplateDecl>(D))
907 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
908 ->getTemplatedDecl())->isInstance();
909 return false;
910}
911
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000912//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000913// DeclaratorDecl Implementation
914//===----------------------------------------------------------------------===//
915
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000916template <typename DeclT>
917static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
918 if (decl->getNumTemplateParameterLists() > 0)
919 return decl->getTemplateParameterList(0)->getTemplateLoc();
920 else
921 return decl->getInnerLocStart();
922}
923
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000924SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000925 TypeSourceInfo *TSI = getTypeSourceInfo();
926 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000927 return SourceLocation();
928}
929
John McCall3e11ebe2010-03-15 10:12:16 +0000930void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
931 SourceRange QualifierRange) {
932 if (Qualifier) {
933 // Make sure the extended decl info is allocated.
934 if (!hasExtInfo()) {
935 // Save (non-extended) type source info pointer.
936 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
937 // Allocate external info struct.
938 DeclInfo = new (getASTContext()) ExtInfo;
939 // Restore savedTInfo into (extended) decl info.
940 getExtInfo()->TInfo = savedTInfo;
941 }
942 // Set qualifier info.
943 getExtInfo()->NNS = Qualifier;
944 getExtInfo()->NNSRange = QualifierRange;
945 }
946 else {
947 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
948 assert(QualifierRange.isInvalid());
949 if (hasExtInfo()) {
950 // Save type source info pointer.
951 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
952 // Deallocate the extended decl info.
953 getASTContext().Deallocate(getExtInfo());
954 // Restore savedTInfo into (non-extended) decl info.
955 DeclInfo = savedTInfo;
956 }
957 }
958}
959
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000960SourceLocation DeclaratorDecl::getOuterLocStart() const {
961 return getTemplateOrInnerLocStart(this);
962}
963
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000964void
Douglas Gregor20527e22010-06-15 17:44:38 +0000965QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
966 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000967 TemplateParameterList **TPLists) {
968 assert((NumTPLists == 0 || TPLists != 0) &&
969 "Empty array of template parameters with positive size!");
970 assert((NumTPLists == 0 || NNS) &&
971 "Nonempty array of template parameters with no qualifier!");
972
973 // Free previous template parameters (if any).
974 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000975 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000976 TemplParamLists = 0;
977 NumTemplParamLists = 0;
978 }
979 // Set info on matched template parameter lists (if any).
980 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000981 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000982 NumTemplParamLists = NumTPLists;
983 for (unsigned i = NumTPLists; i-- > 0; )
984 TemplParamLists[i] = TPLists[i];
985 }
986}
987
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000988//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000989// VarDecl Implementation
990//===----------------------------------------------------------------------===//
991
Sebastian Redl833ef452010-01-26 22:01:41 +0000992const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
993 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000994 case SC_None: break;
995 case SC_Auto: return "auto"; break;
996 case SC_Extern: return "extern"; break;
997 case SC_PrivateExtern: return "__private_extern__"; break;
998 case SC_Register: return "register"; break;
999 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +00001000 }
1001
1002 assert(0 && "Invalid storage class");
1003 return 0;
1004}
1005
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001006VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +00001007 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001008 StorageClass S, StorageClass SCAsWritten) {
1009 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001010}
1011
Douglas Gregorbf62d642010-12-06 18:36:25 +00001012void VarDecl::setStorageClass(StorageClass SC) {
1013 assert(isLegalForVariable(SC));
1014 if (getStorageClass() != SC)
1015 ClearLinkageCache();
1016
1017 SClass = SC;
1018}
1019
Douglas Gregorb11aad82011-02-19 18:51:44 +00001020SourceLocation VarDecl::getInnerLocStart() const {
1021 SourceLocation Start = getTypeSpecStartLoc();
1022 if (Start.isInvalid())
1023 Start = getLocation();
1024 return Start;
1025}
1026
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001027SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001028 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001029 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1030 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001031}
1032
Sebastian Redl833ef452010-01-26 22:01:41 +00001033bool VarDecl::isExternC() const {
1034 ASTContext &Context = getASTContext();
1035 if (!Context.getLangOptions().CPlusPlus)
1036 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001037 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001038 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1039
1040 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
1041 DC = DC->getParent()) {
1042 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1043 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001044 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001045
1046 break;
1047 }
1048
1049 if (DC->isFunctionOrMethod())
1050 return false;
1051 }
1052
1053 return false;
1054}
1055
1056VarDecl *VarDecl::getCanonicalDecl() {
1057 return getFirstDeclaration();
1058}
1059
Sebastian Redl35351a92010-01-31 22:27:38 +00001060VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1061 // C++ [basic.def]p2:
1062 // A declaration is a definition unless [...] it contains the 'extern'
1063 // specifier or a linkage-specification and neither an initializer [...],
1064 // it declares a static data member in a class declaration [...].
1065 // C++ [temp.expl.spec]p15:
1066 // An explicit specialization of a static data member of a template is a
1067 // definition if the declaration includes an initializer; otherwise, it is
1068 // a declaration.
1069 if (isStaticDataMember()) {
1070 if (isOutOfLine() && (hasInit() ||
1071 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1072 return Definition;
1073 else
1074 return DeclarationOnly;
1075 }
1076 // C99 6.7p5:
1077 // A definition of an identifier is a declaration for that identifier that
1078 // [...] causes storage to be reserved for that object.
1079 // Note: that applies for all non-file-scope objects.
1080 // C99 6.9.2p1:
1081 // If the declaration of an identifier for an object has file scope and an
1082 // initializer, the declaration is an external definition for the identifier
1083 if (hasInit())
1084 return Definition;
1085 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1086 if (hasExternalStorage())
1087 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001088
John McCall8e7d6562010-08-26 03:08:43 +00001089 if (getStorageClassAsWritten() == SC_Extern ||
1090 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001091 for (const VarDecl *PrevVar = getPreviousDeclaration();
1092 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1093 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1094 return DeclarationOnly;
1095 }
1096 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001097 // C99 6.9.2p2:
1098 // A declaration of an object that has file scope without an initializer,
1099 // and without a storage class specifier or the scs 'static', constitutes
1100 // a tentative definition.
1101 // No such thing in C++.
1102 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1103 return TentativeDefinition;
1104
1105 // What's left is (in C, block-scope) declarations without initializers or
1106 // external storage. These are definitions.
1107 return Definition;
1108}
1109
Sebastian Redl35351a92010-01-31 22:27:38 +00001110VarDecl *VarDecl::getActingDefinition() {
1111 DefinitionKind Kind = isThisDeclarationADefinition();
1112 if (Kind != TentativeDefinition)
1113 return 0;
1114
Chris Lattner48eb14d2010-06-14 18:31:46 +00001115 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001116 VarDecl *First = getFirstDeclaration();
1117 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1118 I != E; ++I) {
1119 Kind = (*I)->isThisDeclarationADefinition();
1120 if (Kind == Definition)
1121 return 0;
1122 else if (Kind == TentativeDefinition)
1123 LastTentative = *I;
1124 }
1125 return LastTentative;
1126}
1127
1128bool VarDecl::isTentativeDefinitionNow() const {
1129 DefinitionKind Kind = isThisDeclarationADefinition();
1130 if (Kind != TentativeDefinition)
1131 return false;
1132
1133 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1134 if ((*I)->isThisDeclarationADefinition() == Definition)
1135 return false;
1136 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001137 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001138}
1139
Sebastian Redl5ca79842010-02-01 20:16:42 +00001140VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001141 VarDecl *First = getFirstDeclaration();
1142 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1143 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001144 if ((*I)->isThisDeclarationADefinition() == Definition)
1145 return *I;
1146 }
1147 return 0;
1148}
1149
John McCall37bb6c92010-10-29 22:22:43 +00001150VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1151 DefinitionKind Kind = DeclarationOnly;
1152
1153 const VarDecl *First = getFirstDeclaration();
1154 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1155 I != E; ++I)
1156 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1157
1158 return Kind;
1159}
1160
Sebastian Redl5ca79842010-02-01 20:16:42 +00001161const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001162 redecl_iterator I = redecls_begin(), E = redecls_end();
1163 while (I != E && !I->getInit())
1164 ++I;
1165
1166 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001167 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001168 return I->getInit();
1169 }
1170 return 0;
1171}
1172
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001173bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001174 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001175 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001176
1177 if (!isStaticDataMember())
1178 return false;
1179
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001180 // If this static data member was instantiated from a static data member of
1181 // a class template, check whether that static data member was defined
1182 // out-of-line.
1183 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1184 return VD->isOutOfLine();
1185
1186 return false;
1187}
1188
Douglas Gregor1d957a32009-10-27 18:42:08 +00001189VarDecl *VarDecl::getOutOfLineDefinition() {
1190 if (!isStaticDataMember())
1191 return 0;
1192
1193 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1194 RD != RDEnd; ++RD) {
1195 if (RD->getLexicalDeclContext()->isFileContext())
1196 return *RD;
1197 }
1198
1199 return 0;
1200}
1201
Douglas Gregord5058122010-02-11 01:19:42 +00001202void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001203 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1204 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001205 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001206 }
1207
1208 Init = I;
1209}
1210
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001211VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001212 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001213 return cast<VarDecl>(MSI->getInstantiatedFrom());
1214
1215 return 0;
1216}
1217
Douglas Gregor3c74d412009-10-14 20:14:33 +00001218TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001219 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001220 return MSI->getTemplateSpecializationKind();
1221
1222 return TSK_Undeclared;
1223}
1224
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001225MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001226 return getASTContext().getInstantiatedFromStaticDataMember(this);
1227}
1228
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001229void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1230 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001231 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001232 assert(MSI && "Not an instantiated static data member?");
1233 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001234 if (TSK != TSK_ExplicitSpecialization &&
1235 PointOfInstantiation.isValid() &&
1236 MSI->getPointOfInstantiation().isInvalid())
1237 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001238}
1239
Sebastian Redl833ef452010-01-26 22:01:41 +00001240//===----------------------------------------------------------------------===//
1241// ParmVarDecl Implementation
1242//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001243
Sebastian Redl833ef452010-01-26 22:01:41 +00001244ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1245 SourceLocation L, IdentifierInfo *Id,
1246 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001247 StorageClass S, StorageClass SCAsWritten,
1248 Expr *DefArg) {
1249 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1250 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001251}
1252
Sebastian Redl833ef452010-01-26 22:01:41 +00001253Expr *ParmVarDecl::getDefaultArg() {
1254 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1255 assert(!hasUninstantiatedDefaultArg() &&
1256 "Default argument is not yet instantiated!");
1257
1258 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001259 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001260 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001261
Sebastian Redl833ef452010-01-26 22:01:41 +00001262 return Arg;
1263}
1264
1265unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001266 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001267 return E->getNumTemporaries();
1268
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001269 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001270}
1271
Sebastian Redl833ef452010-01-26 22:01:41 +00001272CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1273 assert(getNumDefaultArgTemporaries() &&
1274 "Default arguments does not have any temporaries!");
1275
John McCall5d413782010-12-06 08:20:24 +00001276 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001277 return E->getTemporary(i);
1278}
1279
1280SourceRange ParmVarDecl::getDefaultArgRange() const {
1281 if (const Expr *E = getInit())
1282 return E->getSourceRange();
1283
1284 if (hasUninstantiatedDefaultArg())
1285 return getUninstantiatedDefaultArg()->getSourceRange();
1286
1287 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001288}
1289
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001290bool ParmVarDecl::isParameterPack() const {
1291 return isa<PackExpansionType>(getType());
1292}
1293
Nuno Lopes394ec982008-12-17 23:39:55 +00001294//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001295// FunctionDecl Implementation
1296//===----------------------------------------------------------------------===//
1297
Douglas Gregorb11aad82011-02-19 18:51:44 +00001298void FunctionDecl::getNameForDiagnostic(std::string &S,
1299 const PrintingPolicy &Policy,
1300 bool Qualified) const {
1301 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1302 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1303 if (TemplateArgs)
1304 S += TemplateSpecializationType::PrintTemplateArgumentList(
1305 TemplateArgs->data(),
1306 TemplateArgs->size(),
1307 Policy);
1308
1309}
1310
Ted Kremenek186a0742010-04-29 16:49:01 +00001311bool FunctionDecl::isVariadic() const {
1312 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1313 return FT->isVariadic();
1314 return false;
1315}
1316
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001317bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1318 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1319 if (I->Body) {
1320 Definition = *I;
1321 return true;
1322 }
1323 }
1324
1325 return false;
1326}
1327
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001328Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001329 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1330 if (I->Body) {
1331 Definition = *I;
1332 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001333 }
1334 }
1335
1336 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001337}
1338
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001339void FunctionDecl::setBody(Stmt *B) {
1340 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001341 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001342 EndRangeLoc = B->getLocEnd();
1343}
1344
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001345void FunctionDecl::setPure(bool P) {
1346 IsPure = P;
1347 if (P)
1348 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1349 Parent->markedVirtualFunctionPure();
1350}
1351
Douglas Gregor16618f22009-09-12 00:17:51 +00001352bool FunctionDecl::isMain() const {
1353 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001354 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001355 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001356 getIdentifier() && getIdentifier()->isStr("main");
1357}
1358
Douglas Gregor16618f22009-09-12 00:17:51 +00001359bool FunctionDecl::isExternC() const {
1360 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001361 // In C, any non-static, non-overloadable function has external
1362 // linkage.
1363 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001364 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001365
Mike Stump11289f42009-09-09 15:08:12 +00001366 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001367 DC = DC->getParent()) {
1368 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1369 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001370 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001371 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001372
1373 break;
1374 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001375
1376 if (DC->isRecord())
1377 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001378 }
1379
Douglas Gregorbff62032010-10-21 16:57:46 +00001380 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001381}
1382
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001383bool FunctionDecl::isGlobal() const {
1384 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1385 return Method->isStatic();
1386
John McCall8e7d6562010-08-26 03:08:43 +00001387 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001388 return false;
1389
Mike Stump11289f42009-09-09 15:08:12 +00001390 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001391 DC->isNamespace();
1392 DC = DC->getParent()) {
1393 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1394 if (!Namespace->getDeclName())
1395 return false;
1396 break;
1397 }
1398 }
1399
1400 return true;
1401}
1402
Sebastian Redl833ef452010-01-26 22:01:41 +00001403void
1404FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1405 redeclarable_base::setPreviousDeclaration(PrevDecl);
1406
1407 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1408 FunctionTemplateDecl *PrevFunTmpl
1409 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1410 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1411 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1412 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001413
1414 if (PrevDecl->IsInline)
1415 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001416}
1417
1418const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1419 return getFirstDeclaration();
1420}
1421
1422FunctionDecl *FunctionDecl::getCanonicalDecl() {
1423 return getFirstDeclaration();
1424}
1425
Douglas Gregorbf62d642010-12-06 18:36:25 +00001426void FunctionDecl::setStorageClass(StorageClass SC) {
1427 assert(isLegalForFunction(SC));
1428 if (getStorageClass() != SC)
1429 ClearLinkageCache();
1430
1431 SClass = SC;
1432}
1433
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001434/// \brief Returns a value indicating whether this function
1435/// corresponds to a builtin function.
1436///
1437/// The function corresponds to a built-in function if it is
1438/// declared at translation scope or within an extern "C" block and
1439/// its name matches with the name of a builtin. The returned value
1440/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001441/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001442/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001443unsigned FunctionDecl::getBuiltinID() const {
1444 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001445 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1446 return 0;
1447
1448 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1449 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1450 return BuiltinID;
1451
1452 // This function has the name of a known C library
1453 // function. Determine whether it actually refers to the C library
1454 // function or whether it just has the same name.
1455
Douglas Gregora908e7f2009-02-17 03:23:10 +00001456 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001457 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001458 return 0;
1459
Douglas Gregore711f702009-02-14 18:57:46 +00001460 // If this function is at translation-unit scope and we're not in
1461 // C++, it refers to the C library function.
1462 if (!Context.getLangOptions().CPlusPlus &&
1463 getDeclContext()->isTranslationUnit())
1464 return BuiltinID;
1465
1466 // If the function is in an extern "C" linkage specification and is
1467 // not marked "overloadable", it's the real function.
1468 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001469 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001470 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001471 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001472 return BuiltinID;
1473
1474 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001475 return 0;
1476}
1477
1478
Chris Lattner47c0d002009-04-25 06:03:53 +00001479/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001480/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001481/// after it has been created.
1482unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001483 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001484 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001485 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001486 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001487
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001488}
1489
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001490void FunctionDecl::setParams(ASTContext &C,
1491 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001492 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001493 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001494
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001495 // Zero params -> null pointer.
1496 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001497 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001498 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001499 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001500
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001501 // Update source range. The check below allows us to set EndRangeLoc before
1502 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001503 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001504 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001505 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001506}
Chris Lattner41943152007-01-25 04:52:46 +00001507
Chris Lattner58258242008-04-10 02:22:51 +00001508/// getMinRequiredArguments - Returns the minimum number of arguments
1509/// needed to call this function. This may be fewer than the number of
1510/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001511/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001512unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001513 if (!getASTContext().getLangOptions().CPlusPlus)
1514 return getNumParams();
1515
Douglas Gregor7825bf32011-01-06 22:09:01 +00001516 unsigned NumRequiredArgs = getNumParams();
1517
1518 // If the last parameter is a parameter pack, we don't need an argument for
1519 // it.
1520 if (NumRequiredArgs > 0 &&
1521 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1522 --NumRequiredArgs;
1523
1524 // If this parameter has a default argument, we don't need an argument for
1525 // it.
1526 while (NumRequiredArgs > 0 &&
1527 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001528 --NumRequiredArgs;
1529
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001530 // We might have parameter packs before the end. These can't be deduced,
1531 // but they can still handle multiple arguments.
1532 unsigned ArgIdx = NumRequiredArgs;
1533 while (ArgIdx > 0) {
1534 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1535 NumRequiredArgs = ArgIdx;
1536
1537 --ArgIdx;
1538 }
1539
Chris Lattner58258242008-04-10 02:22:51 +00001540 return NumRequiredArgs;
1541}
1542
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001543bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001544 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001545 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001546
1547 if (isa<CXXMethodDecl>(this)) {
1548 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1549 return true;
1550 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001551
1552 switch (getTemplateSpecializationKind()) {
1553 case TSK_Undeclared:
1554 case TSK_ExplicitSpecialization:
1555 return false;
1556
1557 case TSK_ImplicitInstantiation:
1558 case TSK_ExplicitInstantiationDeclaration:
1559 case TSK_ExplicitInstantiationDefinition:
1560 // Handle below.
1561 break;
1562 }
1563
1564 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001565 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001566 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001567 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001568
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001569 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001570 return PatternDecl->isInlined();
1571
1572 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001573}
1574
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001575/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001576/// definition will be externally visible.
1577///
1578/// Inline function definitions are always available for inlining optimizations.
1579/// However, depending on the language dialect, declaration specifiers, and
1580/// attributes, the definition of an inline function may or may not be
1581/// "externally" visible to other translation units in the program.
1582///
1583/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001584/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001585/// inline definition becomes externally visible (C99 6.7.4p6).
1586///
1587/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1588/// definition, we use the GNU semantics for inline, which are nearly the
1589/// opposite of C99 semantics. In particular, "inline" by itself will create
1590/// an externally visible symbol, but "extern inline" will not create an
1591/// externally visible symbol.
1592bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1593 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001594 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001595 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001596
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001597 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001598 // If it's not the case that both 'inline' and 'extern' are
1599 // specified on the definition, then this inline definition is
1600 // externally visible.
1601 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1602 return true;
1603
1604 // If any declaration is 'inline' but not 'extern', then this definition
1605 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001606 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1607 Redecl != RedeclEnd;
1608 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001609 if (Redecl->isInlineSpecified() &&
1610 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001611 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001612 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001613
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001614 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001615 }
1616
1617 // C99 6.7.4p6:
1618 // [...] If all of the file scope declarations for a function in a
1619 // translation unit include the inline function specifier without extern,
1620 // then the definition in that translation unit is an inline definition.
1621 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1622 Redecl != RedeclEnd;
1623 ++Redecl) {
1624 // Only consider file-scope declarations in this test.
1625 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1626 continue;
1627
John McCall8e7d6562010-08-26 03:08:43 +00001628 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001629 return true; // Not an inline definition
1630 }
1631
1632 // C99 6.7.4p6:
1633 // An inline definition does not provide an external definition for the
1634 // function, and does not forbid an external definition in another
1635 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001636 return false;
1637}
1638
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001639/// getOverloadedOperator - Which C++ overloaded operator this
1640/// function represents, if any.
1641OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001642 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1643 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001644 else
1645 return OO_None;
1646}
1647
Alexis Huntc88db062010-01-13 09:01:02 +00001648/// getLiteralIdentifier - The literal suffix identifier this function
1649/// represents, if any.
1650const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1651 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1652 return getDeclName().getCXXLiteralIdentifier();
1653 else
1654 return 0;
1655}
1656
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001657FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1658 if (TemplateOrSpecialization.isNull())
1659 return TK_NonTemplate;
1660 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1661 return TK_FunctionTemplate;
1662 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1663 return TK_MemberSpecialization;
1664 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1665 return TK_FunctionTemplateSpecialization;
1666 if (TemplateOrSpecialization.is
1667 <DependentFunctionTemplateSpecializationInfo*>())
1668 return TK_DependentFunctionTemplateSpecialization;
1669
1670 assert(false && "Did we miss a TemplateOrSpecialization type?");
1671 return TK_NonTemplate;
1672}
1673
Douglas Gregord801b062009-10-07 23:56:10 +00001674FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001675 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001676 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1677
1678 return 0;
1679}
1680
Douglas Gregor06db9f52009-10-12 20:18:28 +00001681MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1682 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1683}
1684
Douglas Gregord801b062009-10-07 23:56:10 +00001685void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001686FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1687 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001688 TemplateSpecializationKind TSK) {
1689 assert(TemplateOrSpecialization.isNull() &&
1690 "Member function is already a specialization");
1691 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001692 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001693 TemplateOrSpecialization = Info;
1694}
1695
Douglas Gregorafca3b42009-10-27 20:53:28 +00001696bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001697 // If the function is invalid, it can't be implicitly instantiated.
1698 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001699 return false;
1700
1701 switch (getTemplateSpecializationKind()) {
1702 case TSK_Undeclared:
1703 case TSK_ExplicitSpecialization:
1704 case TSK_ExplicitInstantiationDefinition:
1705 return false;
1706
1707 case TSK_ImplicitInstantiation:
1708 return true;
1709
1710 case TSK_ExplicitInstantiationDeclaration:
1711 // Handled below.
1712 break;
1713 }
1714
1715 // Find the actual template from which we will instantiate.
1716 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001717 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001718 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001719 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001720
1721 // C++0x [temp.explicit]p9:
1722 // Except for inline functions, other explicit instantiation declarations
1723 // have the effect of suppressing the implicit instantiation of the entity
1724 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001725 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001726 return true;
1727
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001728 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001729}
1730
1731FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1732 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1733 while (Primary->getInstantiatedFromMemberTemplate()) {
1734 // If we have hit a point where the user provided a specialization of
1735 // this template, we're done looking.
1736 if (Primary->isMemberSpecialization())
1737 break;
1738
1739 Primary = Primary->getInstantiatedFromMemberTemplate();
1740 }
1741
1742 return Primary->getTemplatedDecl();
1743 }
1744
1745 return getInstantiatedFromMemberFunction();
1746}
1747
Douglas Gregor70d83e22009-06-29 17:30:29 +00001748FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001749 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001750 = TemplateOrSpecialization
1751 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001752 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001753 }
1754 return 0;
1755}
1756
1757const TemplateArgumentList *
1758FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001759 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001760 = TemplateOrSpecialization
1761 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001762 return Info->TemplateArguments;
1763 }
1764 return 0;
1765}
1766
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001767const TemplateArgumentListInfo *
1768FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1769 if (FunctionTemplateSpecializationInfo *Info
1770 = TemplateOrSpecialization
1771 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1772 return Info->TemplateArgumentsAsWritten;
1773 }
1774 return 0;
1775}
1776
Mike Stump11289f42009-09-09 15:08:12 +00001777void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001778FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1779 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001780 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001781 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001782 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001783 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1784 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001785 assert(TSK != TSK_Undeclared &&
1786 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001787 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001788 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001789 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001790 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1791 TemplateArgs,
1792 TemplateArgsAsWritten,
1793 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001794 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001795
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001796 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001797 // function template specializations.
1798 if (InsertPos)
1799 Template->getSpecializations().InsertNode(Info, InsertPos);
1800 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001801 // Try to insert the new node. If there is an existing node, leave it, the
1802 // set will contain the canonical decls while
1803 // FunctionTemplateDecl::findSpecialization will return
1804 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001805 FunctionTemplateSpecializationInfo *Existing
1806 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001807 (void)Existing;
1808 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1809 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001810 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001811}
1812
John McCallb9c78482010-04-08 09:05:18 +00001813void
1814FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1815 const UnresolvedSetImpl &Templates,
1816 const TemplateArgumentListInfo &TemplateArgs) {
1817 assert(TemplateOrSpecialization.isNull());
1818 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1819 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001820 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001821 void *Buffer = Context.Allocate(Size);
1822 DependentFunctionTemplateSpecializationInfo *Info =
1823 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1824 TemplateArgs);
1825 TemplateOrSpecialization = Info;
1826}
1827
1828DependentFunctionTemplateSpecializationInfo::
1829DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1830 const TemplateArgumentListInfo &TArgs)
1831 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1832
1833 d.NumTemplates = Ts.size();
1834 d.NumArgs = TArgs.size();
1835
1836 FunctionTemplateDecl **TsArray =
1837 const_cast<FunctionTemplateDecl**>(getTemplates());
1838 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1839 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1840
1841 TemplateArgumentLoc *ArgsArray =
1842 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1843 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1844 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1845}
1846
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001847TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001848 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001849 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001850 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001851 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001852 if (FTSInfo)
1853 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001854
Douglas Gregord801b062009-10-07 23:56:10 +00001855 MemberSpecializationInfo *MSInfo
1856 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1857 if (MSInfo)
1858 return MSInfo->getTemplateSpecializationKind();
1859
1860 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001861}
1862
Mike Stump11289f42009-09-09 15:08:12 +00001863void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001864FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1865 SourceLocation PointOfInstantiation) {
1866 if (FunctionTemplateSpecializationInfo *FTSInfo
1867 = TemplateOrSpecialization.dyn_cast<
1868 FunctionTemplateSpecializationInfo*>()) {
1869 FTSInfo->setTemplateSpecializationKind(TSK);
1870 if (TSK != TSK_ExplicitSpecialization &&
1871 PointOfInstantiation.isValid() &&
1872 FTSInfo->getPointOfInstantiation().isInvalid())
1873 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1874 } else if (MemberSpecializationInfo *MSInfo
1875 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1876 MSInfo->setTemplateSpecializationKind(TSK);
1877 if (TSK != TSK_ExplicitSpecialization &&
1878 PointOfInstantiation.isValid() &&
1879 MSInfo->getPointOfInstantiation().isInvalid())
1880 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1881 } else
1882 assert(false && "Function cannot have a template specialization kind");
1883}
1884
1885SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001886 if (FunctionTemplateSpecializationInfo *FTSInfo
1887 = TemplateOrSpecialization.dyn_cast<
1888 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001889 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001890 else if (MemberSpecializationInfo *MSInfo
1891 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001892 return MSInfo->getPointOfInstantiation();
1893
1894 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001895}
1896
Douglas Gregor6411b922009-09-11 20:15:17 +00001897bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001898 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00001899 return true;
1900
1901 // If this function was instantiated from a member function of a
1902 // class template, check whether that member function was defined out-of-line.
1903 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1904 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001905 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001906 return Definition->isOutOfLine();
1907 }
1908
1909 // If this function was instantiated from a function template,
1910 // check whether that function template was defined out-of-line.
1911 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1912 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001913 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001914 return Definition->isOutOfLine();
1915 }
1916
1917 return false;
1918}
1919
Chris Lattner59a25942008-03-31 00:36:02 +00001920//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001921// FieldDecl Implementation
1922//===----------------------------------------------------------------------===//
1923
Jay Foad39c79802011-01-12 09:06:06 +00001924FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1925 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl833ef452010-01-26 22:01:41 +00001926 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1927 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1928}
1929
1930bool FieldDecl::isAnonymousStructOrUnion() const {
1931 if (!isImplicit() || getDeclName())
1932 return false;
1933
1934 if (const RecordType *Record = getType()->getAs<RecordType>())
1935 return Record->getDecl()->isAnonymousStructOrUnion();
1936
1937 return false;
1938}
1939
John McCall4e819612011-01-20 07:57:12 +00001940unsigned FieldDecl::getFieldIndex() const {
1941 if (CachedFieldIndex) return CachedFieldIndex - 1;
1942
1943 unsigned index = 0;
1944 RecordDecl::field_iterator
1945 i = getParent()->field_begin(), e = getParent()->field_end();
1946 while (true) {
1947 assert(i != e && "failed to find field in parent!");
1948 if (*i == this)
1949 break;
1950
1951 ++i;
1952 ++index;
1953 }
1954
1955 CachedFieldIndex = index + 1;
1956 return index;
1957}
1958
Sebastian Redl833ef452010-01-26 22:01:41 +00001959//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001960// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001961//===----------------------------------------------------------------------===//
1962
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001963SourceLocation TagDecl::getOuterLocStart() const {
1964 return getTemplateOrInnerLocStart(this);
1965}
1966
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001967SourceRange TagDecl::getSourceRange() const {
1968 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001969 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001970}
1971
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001972TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001973 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001974}
1975
Douglas Gregora72a4e32010-05-19 18:39:18 +00001976void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1977 TypedefDeclOrQualifier = TDD;
1978 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00001979 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001980 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00001981}
1982
Douglas Gregordee1be82009-01-17 00:42:38 +00001983void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001984 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001985
1986 if (isa<CXXRecordDecl>(this)) {
1987 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1988 struct CXXRecordDecl::DefinitionData *Data =
1989 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001990 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1991 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001992 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001993}
1994
1995void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001996 assert((!isa<CXXRecordDecl>(this) ||
1997 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1998 "definition completed but not started");
1999
Douglas Gregordee1be82009-01-17 00:42:38 +00002000 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002001 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002002
2003 if (ASTMutationListener *L = getASTMutationListener())
2004 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002005}
2006
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002007TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002008 if (isDefinition())
2009 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002010 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2011 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002012
2013 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002014 R != REnd; ++R)
2015 if (R->isDefinition())
2016 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002017
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002018 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002019}
2020
John McCall3e11ebe2010-03-15 10:12:16 +00002021void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
2022 SourceRange QualifierRange) {
2023 if (Qualifier) {
2024 // Make sure the extended qualifier info is allocated.
2025 if (!hasExtInfo())
2026 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2027 // Set qualifier info.
2028 getExtInfo()->NNS = Qualifier;
2029 getExtInfo()->NNSRange = QualifierRange;
2030 }
2031 else {
2032 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2033 assert(QualifierRange.isInvalid());
2034 if (hasExtInfo()) {
2035 getASTContext().Deallocate(getExtInfo());
2036 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2037 }
2038 }
2039}
2040
Ted Kremenek21475702008-09-05 17:16:31 +00002041//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002042// EnumDecl Implementation
2043//===----------------------------------------------------------------------===//
2044
2045EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2046 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002047 EnumDecl *PrevDecl, bool IsScoped,
2048 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002049 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002050 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002051 C.getTypeDeclType(Enum, PrevDecl);
2052 return Enum;
2053}
2054
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002055EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002056 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002057 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002058}
2059
Douglas Gregord5058122010-02-11 01:19:42 +00002060void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002061 QualType NewPromotionType,
2062 unsigned NumPositiveBits,
2063 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002064 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002065 if (!IntegerType)
2066 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002067 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002068 setNumPositiveBits(NumPositiveBits);
2069 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002070 TagDecl::completeDefinition();
2071}
2072
2073//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002074// RecordDecl Implementation
2075//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002076
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002077RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002078 IdentifierInfo *Id, RecordDecl *PrevDecl,
2079 SourceLocation TKL)
2080 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002081 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002082 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002083 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002084 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002085 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002086}
2087
Jay Foad39c79802011-01-12 09:06:06 +00002088RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00002089 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00002090 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002091
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002092 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00002093 C.getTypeDeclType(R, PrevDecl);
2094 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002095}
2096
Jay Foad39c79802011-01-12 09:06:06 +00002097RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002098 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2099 SourceLocation());
2100}
2101
Douglas Gregordfcad112009-03-25 15:59:44 +00002102bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002103 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002104 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2105}
2106
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002107RecordDecl::field_iterator RecordDecl::field_begin() const {
2108 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2109 LoadFieldsFromExternalStorage();
2110
2111 return field_iterator(decl_iterator(FirstDecl));
2112}
2113
Douglas Gregorb11aad82011-02-19 18:51:44 +00002114/// completeDefinition - Notes that the definition of this type is now
2115/// complete.
2116void RecordDecl::completeDefinition() {
2117 assert(!isDefinition() && "Cannot redefine record!");
2118 TagDecl::completeDefinition();
2119}
2120
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002121void RecordDecl::LoadFieldsFromExternalStorage() const {
2122 ExternalASTSource *Source = getASTContext().getExternalSource();
2123 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2124
2125 // Notify that we have a RecordDecl doing some initialization.
2126 ExternalASTSource::Deserializing TheFields(Source);
2127
2128 llvm::SmallVector<Decl*, 64> Decls;
2129 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2130 return;
2131
2132#ifndef NDEBUG
2133 // Check that all decls we got were FieldDecls.
2134 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2135 assert(isa<FieldDecl>(Decls[i]));
2136#endif
2137
2138 LoadedFieldsFromExternalStorage = true;
2139
2140 if (Decls.empty())
2141 return;
2142
2143 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2144}
2145
Steve Naroff415d3d52008-10-08 17:01:13 +00002146//===----------------------------------------------------------------------===//
2147// BlockDecl Implementation
2148//===----------------------------------------------------------------------===//
2149
Douglas Gregord5058122010-02-11 01:19:42 +00002150void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002151 unsigned NParms) {
2152 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002153
Steve Naroffc4b30e52009-03-13 16:56:44 +00002154 // Zero params -> null pointer.
2155 if (NParms) {
2156 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002157 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002158 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2159 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2160 }
2161}
2162
John McCall351762c2011-02-07 10:33:21 +00002163void BlockDecl::setCaptures(ASTContext &Context,
2164 const Capture *begin,
2165 const Capture *end,
2166 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002167 CapturesCXXThis = capturesCXXThis;
2168
2169 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002170 NumCaptures = 0;
2171 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002172 return;
2173 }
2174
John McCall351762c2011-02-07 10:33:21 +00002175 NumCaptures = end - begin;
2176
2177 // Avoid new Capture[] because we don't want to provide a default
2178 // constructor.
2179 size_t allocationSize = NumCaptures * sizeof(Capture);
2180 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2181 memcpy(buffer, begin, allocationSize);
2182 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002183}
Sebastian Redl833ef452010-01-26 22:01:41 +00002184
Douglas Gregor70226da2010-12-21 16:27:07 +00002185SourceRange BlockDecl::getSourceRange() const {
2186 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2187}
Sebastian Redl833ef452010-01-26 22:01:41 +00002188
2189//===----------------------------------------------------------------------===//
2190// Other Decl Allocation/Deallocation Method Implementations
2191//===----------------------------------------------------------------------===//
2192
2193TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2194 return new (C) TranslationUnitDecl(C);
2195}
2196
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002197LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2198 SourceLocation L, IdentifierInfo *II) {
2199 return new (C) LabelDecl(DC, L, II, 0);
2200}
2201
2202
Sebastian Redl833ef452010-01-26 22:01:41 +00002203NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2204 SourceLocation L, IdentifierInfo *Id) {
2205 return new (C) NamespaceDecl(DC, L, Id);
2206}
2207
Douglas Gregor417e87c2010-10-27 19:49:05 +00002208NamespaceDecl *NamespaceDecl::getNextNamespace() {
2209 return dyn_cast_or_null<NamespaceDecl>(
2210 NextNamespace.get(getASTContext().getExternalSource()));
2211}
2212
Sebastian Redl833ef452010-01-26 22:01:41 +00002213ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2214 SourceLocation L, IdentifierInfo *Id, QualType T) {
2215 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2216}
2217
2218FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002219 const DeclarationNameInfo &NameInfo,
2220 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002221 StorageClass S, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002222 bool isInlineSpecified,
2223 bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002224 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002225 S, SCAsWritten, isInlineSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002226 New->HasWrittenPrototype = hasWrittenPrototype;
2227 return New;
2228}
2229
2230BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2231 return new (C) BlockDecl(DC, L);
2232}
2233
2234EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2235 SourceLocation L,
2236 IdentifierInfo *Id, QualType T,
2237 Expr *E, const llvm::APSInt &V) {
2238 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2239}
2240
Benjamin Kramer39593702010-11-21 14:11:41 +00002241IndirectFieldDecl *
2242IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2243 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2244 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002245 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2246}
2247
Douglas Gregorbe996932010-09-01 20:41:53 +00002248SourceRange EnumConstantDecl::getSourceRange() const {
2249 SourceLocation End = getLocation();
2250 if (Init)
2251 End = Init->getLocEnd();
2252 return SourceRange(getLocation(), End);
2253}
2254
Sebastian Redl833ef452010-01-26 22:01:41 +00002255TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2256 SourceLocation L, IdentifierInfo *Id,
2257 TypeSourceInfo *TInfo) {
2258 return new (C) TypedefDecl(DC, L, Id, TInfo);
2259}
2260
Sebastian Redl833ef452010-01-26 22:01:41 +00002261FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2262 SourceLocation L,
2263 StringLiteral *Str) {
2264 return new (C) FileScopeAsmDecl(DC, L, Str);
2265}