blob: 7d4b461f5a7c87d718cbb7939a1bbe7b0e973406 [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
Douglas Gregora991f3a2011-02-17 17:23:19 +0000604void NamedDecl::getNameForDiagnostic(std::string &S,
605 const PrintingPolicy &Policy,
606 bool Qualified) const {
607 if (Qualified)
608 S += getQualifiedNameAsString(Policy);
609 else
610 S += getNameAsString();
611
612 const TemplateArgumentList *TemplateArgs = 0;
613
614 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
615 TemplateArgs = FD->getTemplateSpecializationArgs();
616 else if (const ClassTemplateSpecializationDecl *Spec
617 = dyn_cast<ClassTemplateSpecializationDecl>(this))
618 TemplateArgs = &Spec->getTemplateArgs();
619
620
621 if (TemplateArgs)
622 S += TemplateSpecializationType::PrintTemplateArgumentList(
623 TemplateArgs->data(),
624 TemplateArgs->size(),
625 Policy);
626}
627
John McCalld396b972011-02-08 19:01:05 +0000628void NamedDecl::ClearLinkageCache() {
629 // Note that we can't skip clearing the linkage of children just
630 // because the parent doesn't have cached linkage: we don't cache
631 // when computing linkage for parent contexts.
632
633 HasCachedLinkage = 0;
634
635 // If we're changing the linkage of a class, we need to reset the
636 // linkage of child declarations, too.
637 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
638 clearLinkageForClass(record);
639
John McCall83779672011-02-19 02:53:41 +0000640 if (ClassTemplateDecl *temp =
641 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000642 // Clear linkage for the template pattern.
643 CXXRecordDecl *record = temp->getTemplatedDecl();
644 record->HasCachedLinkage = 0;
645 clearLinkageForClass(record);
646
John McCall83779672011-02-19 02:53:41 +0000647 // We need to clear linkage for specializations, too.
648 for (ClassTemplateDecl::spec_iterator
649 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
650 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000651 }
John McCall83779672011-02-19 02:53:41 +0000652
653 // Clear cached linkage for function template decls, too.
654 if (FunctionTemplateDecl *temp =
655 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
656 for (FunctionTemplateDecl::spec_iterator
657 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
658 i->ClearLinkageCache();
659
John McCalld396b972011-02-08 19:01:05 +0000660}
661
Douglas Gregorbf62d642010-12-06 18:36:25 +0000662Linkage NamedDecl::getLinkage() const {
663 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000664 assert(Linkage(CachedLinkage) ==
665 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000666 return Linkage(CachedLinkage);
667 }
668
669 CachedLinkage = getLVForDecl(this,
670 LVFlags::CreateOnlyDeclLinkage()).linkage();
671 HasCachedLinkage = 1;
672 return Linkage(CachedLinkage);
673}
674
John McCallc273f242010-10-30 11:50:40 +0000675LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000676 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000677 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000678 HasCachedLinkage = 1;
679 CachedLinkage = LI.linkage();
680 return LI;
John McCall033caa52010-10-29 00:29:13 +0000681}
Ted Kremenek926d8602010-04-20 23:15:35 +0000682
John McCall07072662010-11-02 01:45:15 +0000683static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000684 // Objective-C: treat all Objective-C declarations as having external
685 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000686 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000687 default:
688 break;
John McCall457a04e2010-10-22 21:05:15 +0000689 case Decl::TemplateTemplateParm: // count these as external
690 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000691 case Decl::ObjCAtDefsField:
692 case Decl::ObjCCategory:
693 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000694 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000695 case Decl::ObjCForwardProtocol:
696 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000697 case Decl::ObjCMethod:
698 case Decl::ObjCProperty:
699 case Decl::ObjCPropertyImpl:
700 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000701 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000702 }
703
Douglas Gregorf73b2822009-11-25 22:24:25 +0000704 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000705 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000706 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000707
708 // C++ [basic.link]p5:
709 // In addition, a member function, static data member, a named
710 // class or enumeration of class scope, or an unnamed class or
711 // enumeration defined in a class-scope typedef declaration such
712 // that the class or enumeration has the typedef name for linkage
713 // purposes (7.1.3), has external linkage if the name of the class
714 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000715 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000716 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000717
718 // C++ [basic.link]p6:
719 // The name of a function declared in block scope and the name of
720 // an object declared by a block scope extern declaration have
721 // linkage. If there is a visible declaration of an entity with
722 // linkage having the same name and type, ignoring entities
723 // declared outside the innermost enclosing namespace scope, the
724 // block scope declaration declares that same entity and receives
725 // the linkage of the previous declaration. If there is more than
726 // one such matching entity, the program is ill-formed. Otherwise,
727 // if no matching entity is found, the block scope entity receives
728 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000729 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
730 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000731 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000732 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000733
John McCallc273f242010-10-30 11:50:40 +0000734 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000735 if (Flags.ConsiderVisibilityAttributes) {
736 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
737 LV.setVisibility(GetVisibilityFromAttr(VA));
738 }
739
John McCall457a04e2010-10-22 21:05:15 +0000740 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000741 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000742 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
743 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000744 }
745
746 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000747 }
748
John McCall033caa52010-10-29 00:29:13 +0000749 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000750 if (Var->getStorageClass() == SC_Extern ||
751 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000752 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000753 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000754
John McCallc273f242010-10-30 11:50:40 +0000755 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000756 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000757 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000758 else if (Flags.ConsiderVisibilityAttributes) {
759 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
760 LV.setVisibility(GetVisibilityFromAttr(VA));
761 }
762
John McCall457a04e2010-10-22 21:05:15 +0000763 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000764 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000765 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
766 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000767 }
768
769 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000770 }
771 }
772
773 // C++ [basic.link]p6:
774 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000775 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000776}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000777
Douglas Gregor2ada0482009-02-04 17:27:36 +0000778std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000779 return getQualifiedNameAsString(getASTContext().getLangOptions());
780}
781
782std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000783 const DeclContext *Ctx = getDeclContext();
784
785 if (Ctx->isFunctionOrMethod())
786 return getNameAsString();
787
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000788 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
789 ContextsTy Contexts;
790
791 // Collect contexts.
792 while (Ctx && isa<NamedDecl>(Ctx)) {
793 Contexts.push_back(Ctx);
794 Ctx = Ctx->getParent();
795 };
796
797 std::string QualName;
798 llvm::raw_string_ostream OS(QualName);
799
800 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
801 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000802 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000803 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000804 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
805 std::string TemplateArgsStr
806 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000807 TemplateArgs.data(),
808 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000809 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000810 OS << Spec->getName() << TemplateArgsStr;
811 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000812 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000813 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000814 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000815 OS << ND;
816 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
817 if (!RD->getIdentifier())
818 OS << "<anonymous " << RD->getKindName() << '>';
819 else
820 OS << RD;
821 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000822 const FunctionProtoType *FT = 0;
823 if (FD->hasWrittenPrototype())
824 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
825
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000826 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000827 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000828 unsigned NumParams = FD->getNumParams();
829 for (unsigned i = 0; i < NumParams; ++i) {
830 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000831 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000832 std::string Param;
833 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000834 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000835 }
836
837 if (FT->isVariadic()) {
838 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000839 OS << ", ";
840 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000841 }
842 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000843 OS << ')';
844 } else {
845 OS << cast<NamedDecl>(*I);
846 }
847 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000848 }
849
John McCalla2a3f7d2010-03-16 21:48:18 +0000850 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000851 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000852 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000853 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000854
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000855 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000856}
857
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000858bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000859 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
860
Douglas Gregor889ceb72009-02-03 19:21:40 +0000861 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
862 // We want to keep it, unless it nominates same namespace.
863 if (getKind() == Decl::UsingDirective) {
864 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
865 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
866 }
Mike Stump11289f42009-09-09 15:08:12 +0000867
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000868 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
869 // For function declarations, we keep track of redeclarations.
870 return FD->getPreviousDeclaration() == OldD;
871
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000872 // For function templates, the underlying function declarations are linked.
873 if (const FunctionTemplateDecl *FunctionTemplate
874 = dyn_cast<FunctionTemplateDecl>(this))
875 if (const FunctionTemplateDecl *OldFunctionTemplate
876 = dyn_cast<FunctionTemplateDecl>(OldD))
877 return FunctionTemplate->getTemplatedDecl()
878 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000879
Steve Naroffc4173fa2009-02-22 19:35:57 +0000880 // For method declarations, we keep track of redeclarations.
881 if (isa<ObjCMethodDecl>(this))
882 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000883
John McCall9f3059a2009-10-09 21:13:30 +0000884 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
885 return true;
886
John McCall3f746822009-11-17 05:59:44 +0000887 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
888 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
889 cast<UsingShadowDecl>(OldD)->getTargetDecl();
890
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000891 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
892 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
893 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
894
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000895 // For non-function declarations, if the declarations are of the
896 // same kind then this must be a redeclaration, or semantic analysis
897 // would not have given us the new declaration.
898 return this->getKind() == OldD->getKind();
899}
900
Douglas Gregoreddf4332009-02-24 20:03:32 +0000901bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000902 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000903}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000904
Anders Carlsson6915bf62009-06-26 06:29:23 +0000905NamedDecl *NamedDecl::getUnderlyingDecl() {
906 NamedDecl *ND = this;
907 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000908 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000909 ND = UD->getTargetDecl();
910 else if (ObjCCompatibleAliasDecl *AD
911 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
912 return AD->getClassInterface();
913 else
914 return ND;
915 }
916}
917
John McCalla8ae2222010-04-06 21:38:20 +0000918bool NamedDecl::isCXXInstanceMember() const {
919 assert(isCXXClassMember() &&
920 "checking whether non-member is instance member");
921
922 const NamedDecl *D = this;
923 if (isa<UsingShadowDecl>(D))
924 D = cast<UsingShadowDecl>(D)->getTargetDecl();
925
Francois Pichet783dd6e2010-11-21 06:08:52 +0000926 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000927 return true;
928 if (isa<CXXMethodDecl>(D))
929 return cast<CXXMethodDecl>(D)->isInstance();
930 if (isa<FunctionTemplateDecl>(D))
931 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
932 ->getTemplatedDecl())->isInstance();
933 return false;
934}
935
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000936//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000937// DeclaratorDecl Implementation
938//===----------------------------------------------------------------------===//
939
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000940template <typename DeclT>
941static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
942 if (decl->getNumTemplateParameterLists() > 0)
943 return decl->getTemplateParameterList(0)->getTemplateLoc();
944 else
945 return decl->getInnerLocStart();
946}
947
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000948SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000949 TypeSourceInfo *TSI = getTypeSourceInfo();
950 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000951 return SourceLocation();
952}
953
John McCall3e11ebe2010-03-15 10:12:16 +0000954void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
955 SourceRange QualifierRange) {
956 if (Qualifier) {
957 // Make sure the extended decl info is allocated.
958 if (!hasExtInfo()) {
959 // Save (non-extended) type source info pointer.
960 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
961 // Allocate external info struct.
962 DeclInfo = new (getASTContext()) ExtInfo;
963 // Restore savedTInfo into (extended) decl info.
964 getExtInfo()->TInfo = savedTInfo;
965 }
966 // Set qualifier info.
967 getExtInfo()->NNS = Qualifier;
968 getExtInfo()->NNSRange = QualifierRange;
969 }
970 else {
971 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
972 assert(QualifierRange.isInvalid());
973 if (hasExtInfo()) {
974 // Save type source info pointer.
975 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
976 // Deallocate the extended decl info.
977 getASTContext().Deallocate(getExtInfo());
978 // Restore savedTInfo into (non-extended) decl info.
979 DeclInfo = savedTInfo;
980 }
981 }
982}
983
Douglas Gregorfe590df2011-02-17 17:39:40 +0000984SourceLocation DeclaratorDecl::getInnerLocStart() const {
985 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
986 SourceLocation Start = Var->getTypeSpecStartLoc();
987 if (Start.isValid())
988 return Start;
989 } else if (const NonTypeTemplateParmDecl *NTTP
990 = dyn_cast<NonTypeTemplateParmDecl>(this)) {
991 SourceLocation Start = NTTP->getTypeSpecStartLoc();
992 if (Start.isValid())
993 return Start;
994 }
995 return getLocation();
996}
997
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000998SourceLocation DeclaratorDecl::getOuterLocStart() const {
999 return getTemplateOrInnerLocStart(this);
1000}
1001
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001002void
Douglas Gregor20527e22010-06-15 17:44:38 +00001003QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1004 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001005 TemplateParameterList **TPLists) {
1006 assert((NumTPLists == 0 || TPLists != 0) &&
1007 "Empty array of template parameters with positive size!");
1008 assert((NumTPLists == 0 || NNS) &&
1009 "Nonempty array of template parameters with no qualifier!");
1010
1011 // Free previous template parameters (if any).
1012 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001013 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001014 TemplParamLists = 0;
1015 NumTemplParamLists = 0;
1016 }
1017 // Set info on matched template parameter lists (if any).
1018 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001019 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001020 NumTemplParamLists = NumTPLists;
1021 for (unsigned i = NumTPLists; i-- > 0; )
1022 TemplParamLists[i] = TPLists[i];
1023 }
1024}
1025
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001026//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001027// VarDecl Implementation
1028//===----------------------------------------------------------------------===//
1029
Sebastian Redl833ef452010-01-26 22:01:41 +00001030const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1031 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +00001032 case SC_None: break;
1033 case SC_Auto: return "auto"; break;
1034 case SC_Extern: return "extern"; break;
1035 case SC_PrivateExtern: return "__private_extern__"; break;
1036 case SC_Register: return "register"; break;
1037 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +00001038 }
1039
1040 assert(0 && "Invalid storage class");
1041 return 0;
1042}
1043
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001044VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +00001045 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001046 StorageClass S, StorageClass SCAsWritten) {
1047 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001048}
1049
Douglas Gregorbf62d642010-12-06 18:36:25 +00001050void VarDecl::setStorageClass(StorageClass SC) {
1051 assert(isLegalForVariable(SC));
1052 if (getStorageClass() != SC)
1053 ClearLinkageCache();
1054
1055 SClass = SC;
1056}
1057
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001058SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001059 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001060 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
1061 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001062}
1063
Sebastian Redl833ef452010-01-26 22:01:41 +00001064bool VarDecl::isExternC() const {
1065 ASTContext &Context = getASTContext();
1066 if (!Context.getLangOptions().CPlusPlus)
1067 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001068 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001069 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1070
1071 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
1072 DC = DC->getParent()) {
1073 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1074 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001075 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001076
1077 break;
1078 }
1079
1080 if (DC->isFunctionOrMethod())
1081 return false;
1082 }
1083
1084 return false;
1085}
1086
1087VarDecl *VarDecl::getCanonicalDecl() {
1088 return getFirstDeclaration();
1089}
1090
Sebastian Redl35351a92010-01-31 22:27:38 +00001091VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1092 // C++ [basic.def]p2:
1093 // A declaration is a definition unless [...] it contains the 'extern'
1094 // specifier or a linkage-specification and neither an initializer [...],
1095 // it declares a static data member in a class declaration [...].
1096 // C++ [temp.expl.spec]p15:
1097 // An explicit specialization of a static data member of a template is a
1098 // definition if the declaration includes an initializer; otherwise, it is
1099 // a declaration.
1100 if (isStaticDataMember()) {
1101 if (isOutOfLine() && (hasInit() ||
1102 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1103 return Definition;
1104 else
1105 return DeclarationOnly;
1106 }
1107 // C99 6.7p5:
1108 // A definition of an identifier is a declaration for that identifier that
1109 // [...] causes storage to be reserved for that object.
1110 // Note: that applies for all non-file-scope objects.
1111 // C99 6.9.2p1:
1112 // If the declaration of an identifier for an object has file scope and an
1113 // initializer, the declaration is an external definition for the identifier
1114 if (hasInit())
1115 return Definition;
1116 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1117 if (hasExternalStorage())
1118 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001119
John McCall8e7d6562010-08-26 03:08:43 +00001120 if (getStorageClassAsWritten() == SC_Extern ||
1121 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001122 for (const VarDecl *PrevVar = getPreviousDeclaration();
1123 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1124 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1125 return DeclarationOnly;
1126 }
1127 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001128 // C99 6.9.2p2:
1129 // A declaration of an object that has file scope without an initializer,
1130 // and without a storage class specifier or the scs 'static', constitutes
1131 // a tentative definition.
1132 // No such thing in C++.
1133 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1134 return TentativeDefinition;
1135
1136 // What's left is (in C, block-scope) declarations without initializers or
1137 // external storage. These are definitions.
1138 return Definition;
1139}
1140
Sebastian Redl35351a92010-01-31 22:27:38 +00001141VarDecl *VarDecl::getActingDefinition() {
1142 DefinitionKind Kind = isThisDeclarationADefinition();
1143 if (Kind != TentativeDefinition)
1144 return 0;
1145
Chris Lattner48eb14d2010-06-14 18:31:46 +00001146 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001147 VarDecl *First = getFirstDeclaration();
1148 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1149 I != E; ++I) {
1150 Kind = (*I)->isThisDeclarationADefinition();
1151 if (Kind == Definition)
1152 return 0;
1153 else if (Kind == TentativeDefinition)
1154 LastTentative = *I;
1155 }
1156 return LastTentative;
1157}
1158
1159bool VarDecl::isTentativeDefinitionNow() const {
1160 DefinitionKind Kind = isThisDeclarationADefinition();
1161 if (Kind != TentativeDefinition)
1162 return false;
1163
1164 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1165 if ((*I)->isThisDeclarationADefinition() == Definition)
1166 return false;
1167 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001168 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001169}
1170
Sebastian Redl5ca79842010-02-01 20:16:42 +00001171VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001172 VarDecl *First = getFirstDeclaration();
1173 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1174 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001175 if ((*I)->isThisDeclarationADefinition() == Definition)
1176 return *I;
1177 }
1178 return 0;
1179}
1180
John McCall37bb6c92010-10-29 22:22:43 +00001181VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1182 DefinitionKind Kind = DeclarationOnly;
1183
1184 const VarDecl *First = getFirstDeclaration();
1185 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1186 I != E; ++I)
1187 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1188
1189 return Kind;
1190}
1191
Sebastian Redl5ca79842010-02-01 20:16:42 +00001192const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001193 redecl_iterator I = redecls_begin(), E = redecls_end();
1194 while (I != E && !I->getInit())
1195 ++I;
1196
1197 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001198 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001199 return I->getInit();
1200 }
1201 return 0;
1202}
1203
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001204bool VarDecl::isOutOfLine() const {
Douglas Gregora43942a2011-02-17 07:02:32 +00001205 if (getLexicalDeclContext() != getDeclContext())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001206 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001207
1208 if (!isStaticDataMember())
1209 return false;
1210
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001211 // If this static data member was instantiated from a static data member of
1212 // a class template, check whether that static data member was defined
1213 // out-of-line.
1214 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1215 return VD->isOutOfLine();
1216
1217 return false;
1218}
1219
Douglas Gregor1d957a32009-10-27 18:42:08 +00001220VarDecl *VarDecl::getOutOfLineDefinition() {
1221 if (!isStaticDataMember())
1222 return 0;
1223
1224 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1225 RD != RDEnd; ++RD) {
1226 if (RD->getLexicalDeclContext()->isFileContext())
1227 return *RD;
1228 }
1229
1230 return 0;
1231}
1232
Douglas Gregord5058122010-02-11 01:19:42 +00001233void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001234 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1235 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001236 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001237 }
1238
1239 Init = I;
1240}
1241
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001242VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001243 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001244 return cast<VarDecl>(MSI->getInstantiatedFrom());
1245
1246 return 0;
1247}
1248
Douglas Gregor3c74d412009-10-14 20:14:33 +00001249TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001250 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001251 return MSI->getTemplateSpecializationKind();
1252
1253 return TSK_Undeclared;
1254}
1255
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001256MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001257 return getASTContext().getInstantiatedFromStaticDataMember(this);
1258}
1259
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001260void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1261 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001262 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001263 assert(MSI && "Not an instantiated static data member?");
1264 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001265 if (TSK != TSK_ExplicitSpecialization &&
1266 PointOfInstantiation.isValid() &&
1267 MSI->getPointOfInstantiation().isInvalid())
1268 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001269}
1270
Sebastian Redl833ef452010-01-26 22:01:41 +00001271//===----------------------------------------------------------------------===//
1272// ParmVarDecl Implementation
1273//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001274
Sebastian Redl833ef452010-01-26 22:01:41 +00001275ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1276 SourceLocation L, IdentifierInfo *Id,
1277 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001278 StorageClass S, StorageClass SCAsWritten,
1279 Expr *DefArg) {
1280 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1281 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001282}
1283
Sebastian Redl833ef452010-01-26 22:01:41 +00001284Expr *ParmVarDecl::getDefaultArg() {
1285 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1286 assert(!hasUninstantiatedDefaultArg() &&
1287 "Default argument is not yet instantiated!");
1288
1289 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001290 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001291 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001292
Sebastian Redl833ef452010-01-26 22:01:41 +00001293 return Arg;
1294}
1295
1296unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001297 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001298 return E->getNumTemporaries();
1299
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001300 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001301}
1302
Sebastian Redl833ef452010-01-26 22:01:41 +00001303CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1304 assert(getNumDefaultArgTemporaries() &&
1305 "Default arguments does not have any temporaries!");
1306
John McCall5d413782010-12-06 08:20:24 +00001307 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001308 return E->getTemporary(i);
1309}
1310
1311SourceRange ParmVarDecl::getDefaultArgRange() const {
1312 if (const Expr *E = getInit())
1313 return E->getSourceRange();
1314
1315 if (hasUninstantiatedDefaultArg())
1316 return getUninstantiatedDefaultArg()->getSourceRange();
1317
1318 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001319}
1320
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001321bool ParmVarDecl::isParameterPack() const {
1322 return isa<PackExpansionType>(getType());
1323}
1324
Nuno Lopes394ec982008-12-17 23:39:55 +00001325//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001326// FunctionDecl Implementation
1327//===----------------------------------------------------------------------===//
1328
Ted Kremenek186a0742010-04-29 16:49:01 +00001329bool FunctionDecl::isVariadic() const {
1330 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1331 return FT->isVariadic();
1332 return false;
1333}
1334
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001335bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1336 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1337 if (I->Body) {
1338 Definition = *I;
1339 return true;
1340 }
1341 }
1342
1343 return false;
1344}
1345
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001346Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001347 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1348 if (I->Body) {
1349 Definition = *I;
1350 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001351 }
1352 }
1353
1354 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001355}
1356
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001357void FunctionDecl::setBody(Stmt *B) {
1358 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001359 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001360 EndRangeLoc = B->getLocEnd();
1361}
1362
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001363void FunctionDecl::setPure(bool P) {
1364 IsPure = P;
1365 if (P)
1366 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1367 Parent->markedVirtualFunctionPure();
1368}
1369
Douglas Gregor16618f22009-09-12 00:17:51 +00001370bool FunctionDecl::isMain() const {
1371 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001372 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001373 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001374 getIdentifier() && getIdentifier()->isStr("main");
1375}
1376
Douglas Gregor16618f22009-09-12 00:17:51 +00001377bool FunctionDecl::isExternC() const {
1378 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001379 // In C, any non-static, non-overloadable function has external
1380 // linkage.
1381 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001382 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001383
Mike Stump11289f42009-09-09 15:08:12 +00001384 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001385 DC = DC->getParent()) {
1386 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1387 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001388 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001389 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001390
1391 break;
1392 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001393
1394 if (DC->isRecord())
1395 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001396 }
1397
Douglas Gregorbff62032010-10-21 16:57:46 +00001398 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001399}
1400
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001401bool FunctionDecl::isGlobal() const {
1402 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1403 return Method->isStatic();
1404
John McCall8e7d6562010-08-26 03:08:43 +00001405 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001406 return false;
1407
Mike Stump11289f42009-09-09 15:08:12 +00001408 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001409 DC->isNamespace();
1410 DC = DC->getParent()) {
1411 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1412 if (!Namespace->getDeclName())
1413 return false;
1414 break;
1415 }
1416 }
1417
1418 return true;
1419}
1420
Sebastian Redl833ef452010-01-26 22:01:41 +00001421void
1422FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1423 redeclarable_base::setPreviousDeclaration(PrevDecl);
1424
1425 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1426 FunctionTemplateDecl *PrevFunTmpl
1427 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1428 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1429 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1430 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001431
1432 if (PrevDecl->IsInline)
1433 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001434}
1435
1436const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1437 return getFirstDeclaration();
1438}
1439
1440FunctionDecl *FunctionDecl::getCanonicalDecl() {
1441 return getFirstDeclaration();
1442}
1443
Douglas Gregorbf62d642010-12-06 18:36:25 +00001444void FunctionDecl::setStorageClass(StorageClass SC) {
1445 assert(isLegalForFunction(SC));
1446 if (getStorageClass() != SC)
1447 ClearLinkageCache();
1448
1449 SClass = SC;
1450}
1451
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001452/// \brief Returns a value indicating whether this function
1453/// corresponds to a builtin function.
1454///
1455/// The function corresponds to a built-in function if it is
1456/// declared at translation scope or within an extern "C" block and
1457/// its name matches with the name of a builtin. The returned value
1458/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001459/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001460/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001461unsigned FunctionDecl::getBuiltinID() const {
1462 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001463 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1464 return 0;
1465
1466 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1467 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1468 return BuiltinID;
1469
1470 // This function has the name of a known C library
1471 // function. Determine whether it actually refers to the C library
1472 // function or whether it just has the same name.
1473
Douglas Gregora908e7f2009-02-17 03:23:10 +00001474 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001475 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001476 return 0;
1477
Douglas Gregore711f702009-02-14 18:57:46 +00001478 // If this function is at translation-unit scope and we're not in
1479 // C++, it refers to the C library function.
1480 if (!Context.getLangOptions().CPlusPlus &&
1481 getDeclContext()->isTranslationUnit())
1482 return BuiltinID;
1483
1484 // If the function is in an extern "C" linkage specification and is
1485 // not marked "overloadable", it's the real function.
1486 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001487 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001488 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001489 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001490 return BuiltinID;
1491
1492 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001493 return 0;
1494}
1495
1496
Chris Lattner47c0d002009-04-25 06:03:53 +00001497/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001498/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001499/// after it has been created.
1500unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001501 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001502 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001503 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001504 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001505
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001506}
1507
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001508void FunctionDecl::setParams(ASTContext &C,
1509 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001510 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001511 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001512
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001513 // Zero params -> null pointer.
1514 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001515 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001516 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001517 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001518
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001519 // Update source range. The check below allows us to set EndRangeLoc before
1520 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001521 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001522 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001523 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001524}
Chris Lattner41943152007-01-25 04:52:46 +00001525
Chris Lattner58258242008-04-10 02:22:51 +00001526/// getMinRequiredArguments - Returns the minimum number of arguments
1527/// needed to call this function. This may be fewer than the number of
1528/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001529/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001530unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001531 if (!getASTContext().getLangOptions().CPlusPlus)
1532 return getNumParams();
1533
Douglas Gregor7825bf32011-01-06 22:09:01 +00001534 unsigned NumRequiredArgs = getNumParams();
1535
1536 // If the last parameter is a parameter pack, we don't need an argument for
1537 // it.
1538 if (NumRequiredArgs > 0 &&
1539 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1540 --NumRequiredArgs;
1541
1542 // If this parameter has a default argument, we don't need an argument for
1543 // it.
1544 while (NumRequiredArgs > 0 &&
1545 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001546 --NumRequiredArgs;
1547
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001548 // We might have parameter packs before the end. These can't be deduced,
1549 // but they can still handle multiple arguments.
1550 unsigned ArgIdx = NumRequiredArgs;
1551 while (ArgIdx > 0) {
1552 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1553 NumRequiredArgs = ArgIdx;
1554
1555 --ArgIdx;
1556 }
1557
Chris Lattner58258242008-04-10 02:22:51 +00001558 return NumRequiredArgs;
1559}
1560
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001561bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001562 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001563 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001564
1565 if (isa<CXXMethodDecl>(this)) {
1566 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1567 return true;
1568 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001569
1570 switch (getTemplateSpecializationKind()) {
1571 case TSK_Undeclared:
1572 case TSK_ExplicitSpecialization:
1573 return false;
1574
1575 case TSK_ImplicitInstantiation:
1576 case TSK_ExplicitInstantiationDeclaration:
1577 case TSK_ExplicitInstantiationDefinition:
1578 // Handle below.
1579 break;
1580 }
1581
1582 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001583 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001584 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001585 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001586
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001587 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001588 return PatternDecl->isInlined();
1589
1590 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001591}
1592
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001593/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001594/// definition will be externally visible.
1595///
1596/// Inline function definitions are always available for inlining optimizations.
1597/// However, depending on the language dialect, declaration specifiers, and
1598/// attributes, the definition of an inline function may or may not be
1599/// "externally" visible to other translation units in the program.
1600///
1601/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001602/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001603/// inline definition becomes externally visible (C99 6.7.4p6).
1604///
1605/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1606/// definition, we use the GNU semantics for inline, which are nearly the
1607/// opposite of C99 semantics. In particular, "inline" by itself will create
1608/// an externally visible symbol, but "extern inline" will not create an
1609/// externally visible symbol.
1610bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1611 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001612 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001613 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001614
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001615 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001616 // If it's not the case that both 'inline' and 'extern' are
1617 // specified on the definition, then this inline definition is
1618 // externally visible.
1619 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1620 return true;
1621
1622 // If any declaration is 'inline' but not 'extern', then this definition
1623 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001624 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1625 Redecl != RedeclEnd;
1626 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001627 if (Redecl->isInlineSpecified() &&
1628 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001629 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001630 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001631
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001632 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001633 }
1634
1635 // C99 6.7.4p6:
1636 // [...] If all of the file scope declarations for a function in a
1637 // translation unit include the inline function specifier without extern,
1638 // then the definition in that translation unit is an inline definition.
1639 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1640 Redecl != RedeclEnd;
1641 ++Redecl) {
1642 // Only consider file-scope declarations in this test.
1643 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1644 continue;
1645
John McCall8e7d6562010-08-26 03:08:43 +00001646 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001647 return true; // Not an inline definition
1648 }
1649
1650 // C99 6.7.4p6:
1651 // An inline definition does not provide an external definition for the
1652 // function, and does not forbid an external definition in another
1653 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001654 return false;
1655}
1656
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001657/// getOverloadedOperator - Which C++ overloaded operator this
1658/// function represents, if any.
1659OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001660 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1661 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001662 else
1663 return OO_None;
1664}
1665
Alexis Huntc88db062010-01-13 09:01:02 +00001666/// getLiteralIdentifier - The literal suffix identifier this function
1667/// represents, if any.
1668const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1669 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1670 return getDeclName().getCXXLiteralIdentifier();
1671 else
1672 return 0;
1673}
1674
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001675FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1676 if (TemplateOrSpecialization.isNull())
1677 return TK_NonTemplate;
1678 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1679 return TK_FunctionTemplate;
1680 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1681 return TK_MemberSpecialization;
1682 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1683 return TK_FunctionTemplateSpecialization;
1684 if (TemplateOrSpecialization.is
1685 <DependentFunctionTemplateSpecializationInfo*>())
1686 return TK_DependentFunctionTemplateSpecialization;
1687
1688 assert(false && "Did we miss a TemplateOrSpecialization type?");
1689 return TK_NonTemplate;
1690}
1691
Douglas Gregord801b062009-10-07 23:56:10 +00001692FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001693 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001694 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1695
1696 return 0;
1697}
1698
Douglas Gregor06db9f52009-10-12 20:18:28 +00001699MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1700 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1701}
1702
Douglas Gregord801b062009-10-07 23:56:10 +00001703void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001704FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1705 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001706 TemplateSpecializationKind TSK) {
1707 assert(TemplateOrSpecialization.isNull() &&
1708 "Member function is already a specialization");
1709 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001710 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001711 TemplateOrSpecialization = Info;
1712}
1713
Douglas Gregorafca3b42009-10-27 20:53:28 +00001714bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001715 // If the function is invalid, it can't be implicitly instantiated.
1716 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001717 return false;
1718
1719 switch (getTemplateSpecializationKind()) {
1720 case TSK_Undeclared:
1721 case TSK_ExplicitSpecialization:
1722 case TSK_ExplicitInstantiationDefinition:
1723 return false;
1724
1725 case TSK_ImplicitInstantiation:
1726 return true;
1727
1728 case TSK_ExplicitInstantiationDeclaration:
1729 // Handled below.
1730 break;
1731 }
1732
1733 // Find the actual template from which we will instantiate.
1734 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001735 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001736 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001737 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001738
1739 // C++0x [temp.explicit]p9:
1740 // Except for inline functions, other explicit instantiation declarations
1741 // have the effect of suppressing the implicit instantiation of the entity
1742 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001743 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001744 return true;
1745
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001746 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001747}
1748
1749FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1750 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1751 while (Primary->getInstantiatedFromMemberTemplate()) {
1752 // If we have hit a point where the user provided a specialization of
1753 // this template, we're done looking.
1754 if (Primary->isMemberSpecialization())
1755 break;
1756
1757 Primary = Primary->getInstantiatedFromMemberTemplate();
1758 }
1759
1760 return Primary->getTemplatedDecl();
1761 }
1762
1763 return getInstantiatedFromMemberFunction();
1764}
1765
Douglas Gregor70d83e22009-06-29 17:30:29 +00001766FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001767 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001768 = TemplateOrSpecialization
1769 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001770 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001771 }
1772 return 0;
1773}
1774
1775const TemplateArgumentList *
1776FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001777 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001778 = TemplateOrSpecialization
1779 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001780 return Info->TemplateArguments;
1781 }
1782 return 0;
1783}
1784
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001785const TemplateArgumentListInfo *
1786FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1787 if (FunctionTemplateSpecializationInfo *Info
1788 = TemplateOrSpecialization
1789 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1790 return Info->TemplateArgumentsAsWritten;
1791 }
1792 return 0;
1793}
1794
Mike Stump11289f42009-09-09 15:08:12 +00001795void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001796FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1797 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001798 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001799 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001800 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001801 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1802 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001803 assert(TSK != TSK_Undeclared &&
1804 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001805 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001806 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001807 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001808 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1809 TemplateArgs,
1810 TemplateArgsAsWritten,
1811 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001812 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001813
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001814 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001815 // function template specializations.
1816 if (InsertPos)
1817 Template->getSpecializations().InsertNode(Info, InsertPos);
1818 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001819 // Try to insert the new node. If there is an existing node, leave it, the
1820 // set will contain the canonical decls while
1821 // FunctionTemplateDecl::findSpecialization will return
1822 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001823 FunctionTemplateSpecializationInfo *Existing
1824 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001825 (void)Existing;
1826 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1827 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001828 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001829}
1830
John McCallb9c78482010-04-08 09:05:18 +00001831void
1832FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1833 const UnresolvedSetImpl &Templates,
1834 const TemplateArgumentListInfo &TemplateArgs) {
1835 assert(TemplateOrSpecialization.isNull());
1836 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1837 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001838 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001839 void *Buffer = Context.Allocate(Size);
1840 DependentFunctionTemplateSpecializationInfo *Info =
1841 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1842 TemplateArgs);
1843 TemplateOrSpecialization = Info;
1844}
1845
1846DependentFunctionTemplateSpecializationInfo::
1847DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1848 const TemplateArgumentListInfo &TArgs)
1849 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1850
1851 d.NumTemplates = Ts.size();
1852 d.NumArgs = TArgs.size();
1853
1854 FunctionTemplateDecl **TsArray =
1855 const_cast<FunctionTemplateDecl**>(getTemplates());
1856 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1857 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1858
1859 TemplateArgumentLoc *ArgsArray =
1860 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1861 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1862 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1863}
1864
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001865TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001866 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001867 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001868 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001869 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001870 if (FTSInfo)
1871 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001872
Douglas Gregord801b062009-10-07 23:56:10 +00001873 MemberSpecializationInfo *MSInfo
1874 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1875 if (MSInfo)
1876 return MSInfo->getTemplateSpecializationKind();
1877
1878 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001879}
1880
Mike Stump11289f42009-09-09 15:08:12 +00001881void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001882FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1883 SourceLocation PointOfInstantiation) {
1884 if (FunctionTemplateSpecializationInfo *FTSInfo
1885 = TemplateOrSpecialization.dyn_cast<
1886 FunctionTemplateSpecializationInfo*>()) {
1887 FTSInfo->setTemplateSpecializationKind(TSK);
1888 if (TSK != TSK_ExplicitSpecialization &&
1889 PointOfInstantiation.isValid() &&
1890 FTSInfo->getPointOfInstantiation().isInvalid())
1891 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1892 } else if (MemberSpecializationInfo *MSInfo
1893 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1894 MSInfo->setTemplateSpecializationKind(TSK);
1895 if (TSK != TSK_ExplicitSpecialization &&
1896 PointOfInstantiation.isValid() &&
1897 MSInfo->getPointOfInstantiation().isInvalid())
1898 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1899 } else
1900 assert(false && "Function cannot have a template specialization kind");
1901}
1902
1903SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001904 if (FunctionTemplateSpecializationInfo *FTSInfo
1905 = TemplateOrSpecialization.dyn_cast<
1906 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001907 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001908 else if (MemberSpecializationInfo *MSInfo
1909 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001910 return MSInfo->getPointOfInstantiation();
1911
1912 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001913}
1914
Douglas Gregor6411b922009-09-11 20:15:17 +00001915bool FunctionDecl::isOutOfLine() const {
Douglas Gregora43942a2011-02-17 07:02:32 +00001916 if (getLexicalDeclContext() != getDeclContext())
Douglas Gregor6411b922009-09-11 20:15:17 +00001917 return true;
1918
1919 // If this function was instantiated from a member function of a
1920 // class template, check whether that member function was defined out-of-line.
1921 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1922 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001923 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001924 return Definition->isOutOfLine();
1925 }
1926
1927 // If this function was instantiated from a function template,
1928 // check whether that function template was defined out-of-line.
1929 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1930 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001931 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001932 return Definition->isOutOfLine();
1933 }
1934
1935 return false;
1936}
1937
Chris Lattner59a25942008-03-31 00:36:02 +00001938//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001939// FieldDecl Implementation
1940//===----------------------------------------------------------------------===//
1941
Jay Foad39c79802011-01-12 09:06:06 +00001942FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1943 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl833ef452010-01-26 22:01:41 +00001944 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1945 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1946}
1947
1948bool FieldDecl::isAnonymousStructOrUnion() const {
1949 if (!isImplicit() || getDeclName())
1950 return false;
1951
1952 if (const RecordType *Record = getType()->getAs<RecordType>())
1953 return Record->getDecl()->isAnonymousStructOrUnion();
1954
1955 return false;
1956}
1957
John McCall4e819612011-01-20 07:57:12 +00001958unsigned FieldDecl::getFieldIndex() const {
1959 if (CachedFieldIndex) return CachedFieldIndex - 1;
1960
1961 unsigned index = 0;
1962 RecordDecl::field_iterator
1963 i = getParent()->field_begin(), e = getParent()->field_end();
1964 while (true) {
1965 assert(i != e && "failed to find field in parent!");
1966 if (*i == this)
1967 break;
1968
1969 ++i;
1970 ++index;
1971 }
1972
1973 CachedFieldIndex = index + 1;
1974 return index;
1975}
1976
Sebastian Redl833ef452010-01-26 22:01:41 +00001977//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001978// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001979//===----------------------------------------------------------------------===//
1980
Douglas Gregorfe590df2011-02-17 17:39:40 +00001981SourceLocation TagDecl::getInnerLocStart() const {
1982 if (const ClassTemplateSpecializationDecl *Spec
1983 = dyn_cast<ClassTemplateSpecializationDecl>(this)) {
1984 SourceLocation Start = Spec->getTemplateKeywordLoc();
1985 if (Start.isValid())
1986 return Start;
1987 }
1988
1989 return getTagKeywordLoc();
1990}
1991
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001992SourceLocation TagDecl::getOuterLocStart() const {
1993 return getTemplateOrInnerLocStart(this);
1994}
1995
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001996SourceRange TagDecl::getSourceRange() const {
1997 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001998 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001999}
2000
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002001TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002002 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002003}
2004
Douglas Gregora72a4e32010-05-19 18:39:18 +00002005void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
2006 TypedefDeclOrQualifier = TDD;
2007 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002008 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002009 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002010}
2011
Douglas Gregordee1be82009-01-17 00:42:38 +00002012void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002013 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002014
2015 if (isa<CXXRecordDecl>(this)) {
2016 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2017 struct CXXRecordDecl::DefinitionData *Data =
2018 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002019 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2020 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002021 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002022}
2023
2024void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002025 assert((!isa<CXXRecordDecl>(this) ||
2026 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2027 "definition completed but not started");
2028
Douglas Gregordee1be82009-01-17 00:42:38 +00002029 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002030 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002031
2032 if (ASTMutationListener *L = getASTMutationListener())
2033 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002034}
2035
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002036TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002037 if (isDefinition())
2038 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002039 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2040 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002041
2042 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002043 R != REnd; ++R)
2044 if (R->isDefinition())
2045 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002046
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002047 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002048}
2049
John McCall3e11ebe2010-03-15 10:12:16 +00002050void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
2051 SourceRange QualifierRange) {
2052 if (Qualifier) {
2053 // Make sure the extended qualifier info is allocated.
2054 if (!hasExtInfo())
2055 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2056 // Set qualifier info.
2057 getExtInfo()->NNS = Qualifier;
2058 getExtInfo()->NNSRange = QualifierRange;
2059 }
2060 else {
2061 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
2062 assert(QualifierRange.isInvalid());
2063 if (hasExtInfo()) {
2064 getASTContext().Deallocate(getExtInfo());
2065 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2066 }
2067 }
2068}
2069
Ted Kremenek21475702008-09-05 17:16:31 +00002070//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002071// EnumDecl Implementation
2072//===----------------------------------------------------------------------===//
2073
2074EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2075 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002076 EnumDecl *PrevDecl, bool IsScoped,
2077 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002078 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002079 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002080 C.getTypeDeclType(Enum, PrevDecl);
2081 return Enum;
2082}
2083
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002084EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00002085 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002086 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002087}
2088
Douglas Gregord5058122010-02-11 01:19:42 +00002089void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002090 QualType NewPromotionType,
2091 unsigned NumPositiveBits,
2092 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002093 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002094 if (!IntegerType)
2095 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002096 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002097 setNumPositiveBits(NumPositiveBits);
2098 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002099 TagDecl::completeDefinition();
2100}
2101
2102//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002103// RecordDecl Implementation
2104//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002105
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00002106RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002107 IdentifierInfo *Id, RecordDecl *PrevDecl,
2108 SourceLocation TKL)
2109 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002110 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002111 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002112 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002113 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002114 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002115}
2116
Jay Foad39c79802011-01-12 09:06:06 +00002117RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00002118 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00002119 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00002120
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002121 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00002122 C.getTypeDeclType(R, PrevDecl);
2123 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002124}
2125
Jay Foad39c79802011-01-12 09:06:06 +00002126RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002127 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2128 SourceLocation());
2129}
2130
Douglas Gregordfcad112009-03-25 15:59:44 +00002131bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002132 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002133 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2134}
2135
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002136RecordDecl::field_iterator RecordDecl::field_begin() const {
2137 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2138 LoadFieldsFromExternalStorage();
2139
2140 return field_iterator(decl_iterator(FirstDecl));
2141}
2142
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002143void RecordDecl::LoadFieldsFromExternalStorage() const {
2144 ExternalASTSource *Source = getASTContext().getExternalSource();
2145 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2146
2147 // Notify that we have a RecordDecl doing some initialization.
2148 ExternalASTSource::Deserializing TheFields(Source);
2149
2150 llvm::SmallVector<Decl*, 64> Decls;
2151 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2152 return;
2153
2154#ifndef NDEBUG
2155 // Check that all decls we got were FieldDecls.
2156 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2157 assert(isa<FieldDecl>(Decls[i]));
2158#endif
2159
2160 LoadedFieldsFromExternalStorage = true;
2161
2162 if (Decls.empty())
2163 return;
2164
2165 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2166}
2167
Douglas Gregorb4941732011-02-17 18:06:05 +00002168void RecordDecl::completeDefinition() {
2169 assert(!isDefinition() && "Cannot redefine record!");
2170 TagDecl::completeDefinition();
2171 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(this))
2172 CXXRecord->completeDefinitionImpl(0);
2173}
2174
Steve Naroff415d3d52008-10-08 17:01:13 +00002175//===----------------------------------------------------------------------===//
2176// BlockDecl Implementation
2177//===----------------------------------------------------------------------===//
2178
Douglas Gregord5058122010-02-11 01:19:42 +00002179void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002180 unsigned NParms) {
2181 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002182
Steve Naroffc4b30e52009-03-13 16:56:44 +00002183 // Zero params -> null pointer.
2184 if (NParms) {
2185 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002186 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002187 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2188 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2189 }
2190}
2191
John McCall351762c2011-02-07 10:33:21 +00002192void BlockDecl::setCaptures(ASTContext &Context,
2193 const Capture *begin,
2194 const Capture *end,
2195 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002196 CapturesCXXThis = capturesCXXThis;
2197
2198 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002199 NumCaptures = 0;
2200 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002201 return;
2202 }
2203
John McCall351762c2011-02-07 10:33:21 +00002204 NumCaptures = end - begin;
2205
2206 // Avoid new Capture[] because we don't want to provide a default
2207 // constructor.
2208 size_t allocationSize = NumCaptures * sizeof(Capture);
2209 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2210 memcpy(buffer, begin, allocationSize);
2211 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002212}
Sebastian Redl833ef452010-01-26 22:01:41 +00002213
Douglas Gregor70226da2010-12-21 16:27:07 +00002214SourceRange BlockDecl::getSourceRange() const {
2215 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2216}
Sebastian Redl833ef452010-01-26 22:01:41 +00002217
2218//===----------------------------------------------------------------------===//
2219// Other Decl Allocation/Deallocation Method Implementations
2220//===----------------------------------------------------------------------===//
2221
2222TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2223 return new (C) TranslationUnitDecl(C);
2224}
2225
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002226LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2227 SourceLocation L, IdentifierInfo *II) {
2228 return new (C) LabelDecl(DC, L, II, 0);
2229}
2230
2231
Sebastian Redl833ef452010-01-26 22:01:41 +00002232NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2233 SourceLocation L, IdentifierInfo *Id) {
2234 return new (C) NamespaceDecl(DC, L, Id);
2235}
2236
Douglas Gregor417e87c2010-10-27 19:49:05 +00002237NamespaceDecl *NamespaceDecl::getNextNamespace() {
2238 return dyn_cast_or_null<NamespaceDecl>(
2239 NextNamespace.get(getASTContext().getExternalSource()));
2240}
2241
Sebastian Redl833ef452010-01-26 22:01:41 +00002242ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2243 SourceLocation L, IdentifierInfo *Id, QualType T) {
2244 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2245}
2246
2247FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002248 const DeclarationNameInfo &NameInfo,
2249 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002250 StorageClass S, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002251 bool isInlineSpecified,
2252 bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002253 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002254 S, SCAsWritten, isInlineSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002255 New->HasWrittenPrototype = hasWrittenPrototype;
2256 return New;
2257}
2258
2259BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2260 return new (C) BlockDecl(DC, L);
2261}
2262
2263EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2264 SourceLocation L,
2265 IdentifierInfo *Id, QualType T,
2266 Expr *E, const llvm::APSInt &V) {
2267 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2268}
2269
Benjamin Kramer39593702010-11-21 14:11:41 +00002270IndirectFieldDecl *
2271IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2272 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2273 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002274 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2275}
2276
Douglas Gregorbe996932010-09-01 20:41:53 +00002277SourceRange EnumConstantDecl::getSourceRange() const {
2278 SourceLocation End = getLocation();
2279 if (Init)
2280 End = Init->getLocEnd();
2281 return SourceRange(getLocation(), End);
2282}
2283
Sebastian Redl833ef452010-01-26 22:01:41 +00002284TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2285 SourceLocation L, IdentifierInfo *Id,
2286 TypeSourceInfo *TInfo) {
2287 return new (C) TypedefDecl(DC, L, Id, TInfo);
2288}
2289
Sebastian Redl833ef452010-01-26 22:01:41 +00002290FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2291 SourceLocation L,
2292 StringLiteral *Str) {
2293 return new (C) FileScopeAsmDecl(DC, L, Str);
2294}