blob: db69e0878afaaedc9ac88dffc3e0df38af38b3b2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
John McCallf1bbbb42009-09-04 01:14:41 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000029
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Chris Lattnerd3b90652008-03-15 05:43:15 +000032//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000033// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000034//===----------------------------------------------------------------------===//
35
John McCall7f1b9872010-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 McCalle7bc9722010-10-28 04:18:25 +000056 }
John McCall7f1b9872010-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 McCalle7bc9722010-10-28 04:18:25 +000071}
72
John McCall1fb0caa2010-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 McCallaf146032010-10-30 11:50:40 +000085typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall1fb0caa2010-10-22 21:05:15 +000086typedef std::pair<Linkage,Visibility> LVPair;
John McCallaf146032010-10-30 11:50:40 +000087
John McCall1fb0caa2010-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 McCallaf146032010-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 Kramer752c2e92010-11-05 19:56:37 +000098namespace {
John McCall36987482010-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 Gregor381d34e2010-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 McCall36987482010-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 Gregor89d63e52010-12-06 18:50:56 +0000125};
Benjamin Kramer752c2e92010-11-05 19:56:37 +0000126} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +0000127
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000128/// \brief Get the most restrictive linkage for the types in the given
129/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +0000130static LVPair
131getLVForTemplateParameterList(const TemplateParameterList *Params) {
132 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000133 for (TemplateParameterList::const_iterator P = Params->begin(),
134 PEnd = Params->end();
135 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000146 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000147 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000148 continue;
149 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000150 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000151
152 if (TemplateTemplateParmDecl *TTP
153 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000154 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000155 }
156 }
157
John McCall1fb0caa2010-10-22 21:05:15 +0000158 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000159}
160
Douglas Gregor381d34e2010-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 Gregor0b6bc8b2010-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 McCall1fb0caa2010-10-22 21:05:15 +0000166static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregor381d34e2010-12-06 18:36:25 +0000167 unsigned NumArgs,
168 LVFlags &F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000169 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-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 McCall1fb0caa2010-10-22 21:05:15 +0000179 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000180 break;
181
182 case TemplateArgument::Declaration:
John McCall1fb0caa2010-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 Gregor89d63e52010-12-06 18:50:56 +0000186 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
187 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000188 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000189 break;
190
191 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000192 case TemplateArgument::TemplateExpansion:
193 if (TemplateDecl *Template
194 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor89d63e52010-12-06 18:50:56 +0000195 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000196 break;
197
198 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000199 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000200 Args[I].pack_size(),
201 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000202 break;
203 }
204 }
205
John McCall1fb0caa2010-10-22 21:05:15 +0000206 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000207}
208
John McCallaf146032010-10-30 11:50:40 +0000209static LVPair
Douglas Gregor381d34e2010-12-06 18:36:25 +0000210getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
211 LVFlags &F) {
212 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000213}
214
John McCall36987482010-11-02 01:45:15 +0000215static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000216 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-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 McCalld931b082010-08-26 03:08:43 +0000228 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000229 return LinkageInfo::internal();
Douglas Gregord85b5b92009-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 Friedmane9d65542009-11-26 03:04:01 +0000236 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000237 Var->getStorageClass() != SC_Extern &&
238 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000239 bool FoundExtern = false;
240 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
241 PrevVar && !FoundExtern;
242 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000243 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000244 FoundExtern = true;
245
246 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000247 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000248 }
249 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-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 Gregord85b5b92009-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 McCalld931b082010-08-26 03:08:43 +0000261 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000262 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-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 McCallaf146032010-10-30 11:50:40 +0000266 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000267 }
268
John McCall1fb0caa2010-10-22 21:05:15 +0000269 if (D->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000270 return LinkageInfo::uniqueExternal();
John McCalle7bc9722010-10-28 04:18:25 +0000271
John McCall1fb0caa2010-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 McCallaf146032010-10-30 11:50:40 +0000278 LinkageInfo LV;
279
John McCall36987482010-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 McCall90f14502010-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 McCall36987482010-11-02 01:45:15 +0000298 }
John McCallaf146032010-10-30 11:50:40 +0000299 }
John McCall1fb0caa2010-10-22 21:05:15 +0000300
Douglas Gregord85b5b92009-11-25 22:24:25 +0000301 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000302
Douglas Gregord85b5b92009-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 McCall110e8e52010-10-29 22:22:43 +0000308 // GCC applies the following optimization to variables and static
309 // data members, but not to functions:
310 //
John McCall1fb0caa2010-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 McCallac65c622010-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 McCall1fb0caa2010-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 McCallee301022010-10-30 09:18:49 +0000329 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000330 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
331 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000332 return LinkageInfo::uniqueExternal();
333 if (!LV.visibilityExplicit())
334 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000335 }
336
John McCall35cebc32010-11-02 18:38:13 +0000337 if (Var->getStorageClass() == SC_PrivateExtern)
338 LV.setVisibility(HiddenVisibility, true);
339
Douglas Gregord85b5b92009-11-25 22:24:25 +0000340 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000341 (Var->getStorageClass() == SC_Extern ||
342 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000343
Douglas Gregord85b5b92009-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 Gregor381d34e2010-12-06 18:36:25 +0000354 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000355 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
356 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000357 }
358 }
359
Douglas Gregord85b5b92009-11-25 22:24:25 +0000360 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000361 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-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 McCall1fb0caa2010-10-22 21:05:15 +0000366
John McCall35cebc32010-11-02 18:38:13 +0000367 if (Function->getStorageClass() == SC_PrivateExtern)
368 LV.setVisibility(HiddenVisibility, true);
369
Douglas Gregord85b5b92009-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 McCalld931b082010-08-26 03:08:43 +0000376 (Function->getStorageClass() == SC_Extern ||
377 Function->getStorageClass() == SC_PrivateExtern ||
378 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-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 Gregor381d34e2010-12-06 18:36:25 +0000389 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000390 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
391 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000392 }
393 }
394
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000395 if (FunctionTemplateSpecializationInfo *SpecInfo
396 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000397 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
398 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000399 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000400 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000401 }
402
Douglas Gregord85b5b92009-11-25 22:24:25 +0000403 // - a named class (Clause 9), or an unnamed class defined in a
404 // typedef declaration in which the class has the typedef name
405 // for linkage purposes (7.1.3); or
406 // - a named enumeration (7.2), or an unnamed enumeration
407 // defined in a typedef declaration in which the enumeration
408 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000409 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
410 // Unnamed tags have no linkage.
411 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000412 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000413
John McCall1fb0caa2010-10-22 21:05:15 +0000414 // If this is a class template specialization, consider the
415 // linkage of the template and template arguments.
416 if (const ClassTemplateSpecializationDecl *Spec
417 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000418 // From the template.
419 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
420 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000421
John McCall1fb0caa2010-10-22 21:05:15 +0000422 // The arguments at which the template was instantiated.
423 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000424 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000425 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000426
John McCallac65c622010-10-26 04:59:26 +0000427 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000428 if (F.ConsiderGlobalVisibility)
429 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000430 (Context.getLangOptions().CPlusPlus &&
431 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000432
Douglas Gregord85b5b92009-11-25 22:24:25 +0000433 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000434 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000435 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000436 if (!isExternalLinkage(EnumLV.linkage()))
437 return LinkageInfo::none();
438 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000439
440 // - a template, unless it is a function template that has
441 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000442 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallaf146032010-10-30 11:50:40 +0000443 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000444
Douglas Gregord85b5b92009-11-25 22:24:25 +0000445 // - a namespace (7.3), unless it is declared within an unnamed
446 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000447 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
448 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000449
John McCall1fb0caa2010-10-22 21:05:15 +0000450 // By extension, we assign external linkage to Objective-C
451 // interfaces.
452 } else if (isa<ObjCInterfaceDecl>(D)) {
453 // fallout
454
455 // Everything not covered here has no linkage.
456 } else {
John McCallaf146032010-10-30 11:50:40 +0000457 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000458 }
459
460 // If we ended up with non-external linkage, visibility should
461 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000462 if (LV.linkage() != ExternalLinkage)
463 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000464
465 // If we didn't end up with hidden visibility, consider attributes
466 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000467 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000468 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000469
470 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000471}
472
John McCall36987482010-11-02 01:45:15 +0000473static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000474 // Only certain class members have linkage. Note that fields don't
475 // really have linkage, but it's convenient to say they do for the
476 // purposes of calculating linkage of pointer-to-data-member
477 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000478 if (!(isa<CXXMethodDecl>(D) ||
479 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000480 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000481 (isa<TagDecl>(D) &&
482 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000483 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000484
John McCall36987482010-11-02 01:45:15 +0000485 LinkageInfo LV;
486
487 // The flags we're going to use to compute the class's visibility.
488 LVFlags ClassF = F;
489
490 // If we have an explicit visibility attribute, merge that in.
491 if (F.ConsiderVisibilityAttributes) {
492 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
493 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
494
495 // Ignore global visibility later, but not this attribute.
496 F.ConsiderGlobalVisibility = false;
497
498 // Ignore both global visibility and attributes when computing our
499 // parent's visibility.
500 ClassF = F.onlyTemplateVisibility();
501 }
502 }
John McCallaf146032010-10-30 11:50:40 +0000503
504 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000505 // linkage.
506 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
507 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000508 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000509
510 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000511 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000512 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000513
John McCall3cdfc4d2010-08-13 08:35:10 +0000514 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000515 TemplateSpecializationKind TSK = TSK_Undeclared;
516
John McCall1fb0caa2010-10-22 21:05:15 +0000517 // If this is a method template specialization, use the linkage for
518 // the template parameters and arguments.
519 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000520 = MD->getTemplateSpecializationInfo()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000521 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallaf146032010-10-30 11:50:40 +0000522 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000523 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000524
525 TSK = Spec->getTemplateSpecializationKind();
526 } else if (MemberSpecializationInfo *MSI =
527 MD->getMemberSpecializationInfo()) {
528 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000529 }
530
John McCall110e8e52010-10-29 22:22:43 +0000531 // If we're paying attention to global visibility, apply
532 // -finline-visibility-hidden if this is an inline method.
533 //
John McCallaf146032010-10-30 11:50:40 +0000534 // Note that ConsiderGlobalVisibility doesn't yet have information
535 // about whether containing classes have visibility attributes,
536 // and that's intentional.
537 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000538 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000539 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
540 // InlineVisibilityHidden only applies to definitions, and
541 // isInlined() only gives meaningful answers on definitions
542 // anyway.
543 const FunctionDecl *Def = 0;
544 if (MD->hasBody(Def) && Def->isInlined())
545 LV.setVisibility(HiddenVisibility);
546 }
John McCall1fb0caa2010-10-22 21:05:15 +0000547
John McCall110e8e52010-10-29 22:22:43 +0000548 // Note that in contrast to basically every other situation, we
549 // *do* apply -fvisibility to method declarations.
550
551 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000552 if (const ClassTemplateSpecializationDecl *Spec
553 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
554 // Merge template argument/parameter information for member
555 // class template specializations.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000556 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallaf146032010-10-30 11:50:40 +0000557 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000558 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000559 }
560
John McCall110e8e52010-10-29 22:22:43 +0000561 // Static data members.
562 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000563 // Modify the variable's linkage by its type, but ignore the
564 // type's visibility unless it's a definition.
565 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
566 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000567 LV.mergeLinkage(UniqueExternalLinkage);
568 if (!LV.visibilityExplicit())
569 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000570 }
571
John McCall36987482010-11-02 01:45:15 +0000572 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000573
574 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000575 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000576 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000577 }
578
John McCall1fb0caa2010-10-22 21:05:15 +0000579 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000580}
581
Douglas Gregor381d34e2010-12-06 18:36:25 +0000582Linkage NamedDecl::getLinkage() const {
583 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000584 assert(Linkage(CachedLinkage) ==
585 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000586 return Linkage(CachedLinkage);
587 }
588
589 CachedLinkage = getLVForDecl(this,
590 LVFlags::CreateOnlyDeclLinkage()).linkage();
591 HasCachedLinkage = 1;
592 return Linkage(CachedLinkage);
593}
594
John McCallaf146032010-10-30 11:50:40 +0000595LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000596 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000597 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000598 HasCachedLinkage = 1;
599 CachedLinkage = LI.linkage();
600 return LI;
John McCall0df95872010-10-29 00:29:13 +0000601}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000602
John McCall36987482010-11-02 01:45:15 +0000603static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000604 // Objective-C: treat all Objective-C declarations as having external
605 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000606 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000607 default:
608 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000609 case Decl::TemplateTemplateParm: // count these as external
610 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000611 case Decl::ObjCAtDefsField:
612 case Decl::ObjCCategory:
613 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000614 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000615 case Decl::ObjCForwardProtocol:
616 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000617 case Decl::ObjCMethod:
618 case Decl::ObjCProperty:
619 case Decl::ObjCPropertyImpl:
620 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000621 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000622 }
623
Douglas Gregord85b5b92009-11-25 22:24:25 +0000624 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000625 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000626 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000627
628 // C++ [basic.link]p5:
629 // In addition, a member function, static data member, a named
630 // class or enumeration of class scope, or an unnamed class or
631 // enumeration defined in a class-scope typedef declaration such
632 // that the class or enumeration has the typedef name for linkage
633 // purposes (7.1.3), has external linkage if the name of the class
634 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000635 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000636 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000637
638 // C++ [basic.link]p6:
639 // The name of a function declared in block scope and the name of
640 // an object declared by a block scope extern declaration have
641 // linkage. If there is a visible declaration of an entity with
642 // linkage having the same name and type, ignoring entities
643 // declared outside the innermost enclosing namespace scope, the
644 // block scope declaration declares that same entity and receives
645 // the linkage of the previous declaration. If there is more than
646 // one such matching entity, the program is ill-formed. Otherwise,
647 // if no matching entity is found, the block scope entity receives
648 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000649 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
650 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000651 if (Function->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000652 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000653
John McCallaf146032010-10-30 11:50:40 +0000654 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000655 if (Flags.ConsiderVisibilityAttributes) {
656 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
657 LV.setVisibility(GetVisibilityFromAttr(VA));
658 }
659
John McCall1fb0caa2010-10-22 21:05:15 +0000660 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000661 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000662 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
663 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000664 }
665
666 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000667 }
668
John McCall0df95872010-10-29 00:29:13 +0000669 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000670 if (Var->getStorageClass() == SC_Extern ||
671 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000672 if (Var->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000673 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000674
John McCallaf146032010-10-30 11:50:40 +0000675 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000676 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000677 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000678 else if (Flags.ConsiderVisibilityAttributes) {
679 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
680 LV.setVisibility(GetVisibilityFromAttr(VA));
681 }
682
John McCall1fb0caa2010-10-22 21:05:15 +0000683 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000684 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000685 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
686 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000687 }
688
689 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000690 }
691 }
692
693 // C++ [basic.link]p6:
694 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000695 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000696}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000697
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000698std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000699 return getQualifiedNameAsString(getASTContext().getLangOptions());
700}
701
702std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000703 const DeclContext *Ctx = getDeclContext();
704
705 if (Ctx->isFunctionOrMethod())
706 return getNameAsString();
707
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000708 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
709 ContextsTy Contexts;
710
711 // Collect contexts.
712 while (Ctx && isa<NamedDecl>(Ctx)) {
713 Contexts.push_back(Ctx);
714 Ctx = Ctx->getParent();
715 };
716
717 std::string QualName;
718 llvm::raw_string_ostream OS(QualName);
719
720 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
721 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000722 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000723 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000724 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
725 std::string TemplateArgsStr
726 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000727 TemplateArgs.data(),
728 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000729 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000730 OS << Spec->getName() << TemplateArgsStr;
731 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000732 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000733 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000734 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000735 OS << ND;
736 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
737 if (!RD->getIdentifier())
738 OS << "<anonymous " << RD->getKindName() << '>';
739 else
740 OS << RD;
741 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000742 const FunctionProtoType *FT = 0;
743 if (FD->hasWrittenPrototype())
744 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
745
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000746 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000747 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000748 unsigned NumParams = FD->getNumParams();
749 for (unsigned i = 0; i < NumParams; ++i) {
750 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000751 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000752 std::string Param;
753 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000754 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000755 }
756
757 if (FT->isVariadic()) {
758 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000759 OS << ", ";
760 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000761 }
762 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000763 OS << ')';
764 } else {
765 OS << cast<NamedDecl>(*I);
766 }
767 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000768 }
769
John McCall8472af42010-03-16 21:48:18 +0000770 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000771 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000772 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000773 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000774
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000775 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000776}
777
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000778bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000779 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
780
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000781 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
782 // We want to keep it, unless it nominates same namespace.
783 if (getKind() == Decl::UsingDirective) {
784 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
785 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000788 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
789 // For function declarations, we keep track of redeclarations.
790 return FD->getPreviousDeclaration() == OldD;
791
Douglas Gregore53060f2009-06-25 22:08:12 +0000792 // For function templates, the underlying function declarations are linked.
793 if (const FunctionTemplateDecl *FunctionTemplate
794 = dyn_cast<FunctionTemplateDecl>(this))
795 if (const FunctionTemplateDecl *OldFunctionTemplate
796 = dyn_cast<FunctionTemplateDecl>(OldD))
797 return FunctionTemplate->getTemplatedDecl()
798 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Steve Naroff0de21fd2009-02-22 19:35:57 +0000800 // For method declarations, we keep track of redeclarations.
801 if (isa<ObjCMethodDecl>(this))
802 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000803
John McCallf36e02d2009-10-09 21:13:30 +0000804 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
805 return true;
806
John McCall9488ea12009-11-17 05:59:44 +0000807 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
808 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
809 cast<UsingShadowDecl>(OldD)->getTargetDecl();
810
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000811 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
812 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
813 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
814
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000815 // For non-function declarations, if the declarations are of the
816 // same kind then this must be a redeclaration, or semantic analysis
817 // would not have given us the new declaration.
818 return this->getKind() == OldD->getKind();
819}
820
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000821bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000822 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000823}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000824
Anders Carlssone136e0e2009-06-26 06:29:23 +0000825NamedDecl *NamedDecl::getUnderlyingDecl() {
826 NamedDecl *ND = this;
827 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000828 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000829 ND = UD->getTargetDecl();
830 else if (ObjCCompatibleAliasDecl *AD
831 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
832 return AD->getClassInterface();
833 else
834 return ND;
835 }
836}
837
John McCall161755a2010-04-06 21:38:20 +0000838bool NamedDecl::isCXXInstanceMember() const {
839 assert(isCXXClassMember() &&
840 "checking whether non-member is instance member");
841
842 const NamedDecl *D = this;
843 if (isa<UsingShadowDecl>(D))
844 D = cast<UsingShadowDecl>(D)->getTargetDecl();
845
Francois Pichet87c2e122010-11-21 06:08:52 +0000846 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000847 return true;
848 if (isa<CXXMethodDecl>(D))
849 return cast<CXXMethodDecl>(D)->isInstance();
850 if (isa<FunctionTemplateDecl>(D))
851 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
852 ->getTemplatedDecl())->isInstance();
853 return false;
854}
855
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000856//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000857// DeclaratorDecl Implementation
858//===----------------------------------------------------------------------===//
859
Douglas Gregor1693e152010-07-06 18:42:40 +0000860template <typename DeclT>
861static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
862 if (decl->getNumTemplateParameterLists() > 0)
863 return decl->getTemplateParameterList(0)->getTemplateLoc();
864 else
865 return decl->getInnerLocStart();
866}
867
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000868SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000869 TypeSourceInfo *TSI = getTypeSourceInfo();
870 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000871 return SourceLocation();
872}
873
John McCallb6217662010-03-15 10:12:16 +0000874void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
875 SourceRange QualifierRange) {
876 if (Qualifier) {
877 // Make sure the extended decl info is allocated.
878 if (!hasExtInfo()) {
879 // Save (non-extended) type source info pointer.
880 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
881 // Allocate external info struct.
882 DeclInfo = new (getASTContext()) ExtInfo;
883 // Restore savedTInfo into (extended) decl info.
884 getExtInfo()->TInfo = savedTInfo;
885 }
886 // Set qualifier info.
887 getExtInfo()->NNS = Qualifier;
888 getExtInfo()->NNSRange = QualifierRange;
889 }
890 else {
891 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
892 assert(QualifierRange.isInvalid());
893 if (hasExtInfo()) {
894 // Save type source info pointer.
895 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
896 // Deallocate the extended decl info.
897 getASTContext().Deallocate(getExtInfo());
898 // Restore savedTInfo into (non-extended) decl info.
899 DeclInfo = savedTInfo;
900 }
901 }
902}
903
Douglas Gregor1693e152010-07-06 18:42:40 +0000904SourceLocation DeclaratorDecl::getOuterLocStart() const {
905 return getTemplateOrInnerLocStart(this);
906}
907
Abramo Bagnara9b934882010-06-12 08:15:14 +0000908void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000909QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
910 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000911 TemplateParameterList **TPLists) {
912 assert((NumTPLists == 0 || TPLists != 0) &&
913 "Empty array of template parameters with positive size!");
914 assert((NumTPLists == 0 || NNS) &&
915 "Nonempty array of template parameters with no qualifier!");
916
917 // Free previous template parameters (if any).
918 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000919 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000920 TemplParamLists = 0;
921 NumTemplParamLists = 0;
922 }
923 // Set info on matched template parameter lists (if any).
924 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000925 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000926 NumTemplParamLists = NumTPLists;
927 for (unsigned i = NumTPLists; i-- > 0; )
928 TemplParamLists[i] = TPLists[i];
929 }
930}
931
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000932//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000933// VarDecl Implementation
934//===----------------------------------------------------------------------===//
935
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000936const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
937 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000938 case SC_None: break;
939 case SC_Auto: return "auto"; break;
940 case SC_Extern: return "extern"; break;
941 case SC_PrivateExtern: return "__private_extern__"; break;
942 case SC_Register: return "register"; break;
943 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000944 }
945
946 assert(0 && "Invalid storage class");
947 return 0;
948}
949
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000950VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000951 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000952 StorageClass S, StorageClass SCAsWritten) {
953 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000954}
955
Douglas Gregor381d34e2010-12-06 18:36:25 +0000956void VarDecl::setStorageClass(StorageClass SC) {
957 assert(isLegalForVariable(SC));
958 if (getStorageClass() != SC)
959 ClearLinkageCache();
960
961 SClass = SC;
962}
963
Douglas Gregor1693e152010-07-06 18:42:40 +0000964SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000965 SourceLocation Start = getTypeSpecStartLoc();
966 if (Start.isInvalid())
967 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000968 return Start;
969}
970
971SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000972 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000973 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
974 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000975}
976
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000977bool VarDecl::isExternC() const {
978 ASTContext &Context = getASTContext();
979 if (!Context.getLangOptions().CPlusPlus)
980 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +0000981 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000982 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
983
984 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
985 DC = DC->getParent()) {
986 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
987 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000988 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000989
990 break;
991 }
992
993 if (DC->isFunctionOrMethod())
994 return false;
995 }
996
997 return false;
998}
999
1000VarDecl *VarDecl::getCanonicalDecl() {
1001 return getFirstDeclaration();
1002}
1003
Sebastian Redle9d12b62010-01-31 22:27:38 +00001004VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1005 // C++ [basic.def]p2:
1006 // A declaration is a definition unless [...] it contains the 'extern'
1007 // specifier or a linkage-specification and neither an initializer [...],
1008 // it declares a static data member in a class declaration [...].
1009 // C++ [temp.expl.spec]p15:
1010 // An explicit specialization of a static data member of a template is a
1011 // definition if the declaration includes an initializer; otherwise, it is
1012 // a declaration.
1013 if (isStaticDataMember()) {
1014 if (isOutOfLine() && (hasInit() ||
1015 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1016 return Definition;
1017 else
1018 return DeclarationOnly;
1019 }
1020 // C99 6.7p5:
1021 // A definition of an identifier is a declaration for that identifier that
1022 // [...] causes storage to be reserved for that object.
1023 // Note: that applies for all non-file-scope objects.
1024 // C99 6.9.2p1:
1025 // If the declaration of an identifier for an object has file scope and an
1026 // initializer, the declaration is an external definition for the identifier
1027 if (hasInit())
1028 return Definition;
1029 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1030 if (hasExternalStorage())
1031 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001032
John McCalld931b082010-08-26 03:08:43 +00001033 if (getStorageClassAsWritten() == SC_Extern ||
1034 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001035 for (const VarDecl *PrevVar = getPreviousDeclaration();
1036 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1037 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1038 return DeclarationOnly;
1039 }
1040 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001041 // C99 6.9.2p2:
1042 // A declaration of an object that has file scope without an initializer,
1043 // and without a storage class specifier or the scs 'static', constitutes
1044 // a tentative definition.
1045 // No such thing in C++.
1046 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1047 return TentativeDefinition;
1048
1049 // What's left is (in C, block-scope) declarations without initializers or
1050 // external storage. These are definitions.
1051 return Definition;
1052}
1053
Sebastian Redle9d12b62010-01-31 22:27:38 +00001054VarDecl *VarDecl::getActingDefinition() {
1055 DefinitionKind Kind = isThisDeclarationADefinition();
1056 if (Kind != TentativeDefinition)
1057 return 0;
1058
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001059 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001060 VarDecl *First = getFirstDeclaration();
1061 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1062 I != E; ++I) {
1063 Kind = (*I)->isThisDeclarationADefinition();
1064 if (Kind == Definition)
1065 return 0;
1066 else if (Kind == TentativeDefinition)
1067 LastTentative = *I;
1068 }
1069 return LastTentative;
1070}
1071
1072bool VarDecl::isTentativeDefinitionNow() const {
1073 DefinitionKind Kind = isThisDeclarationADefinition();
1074 if (Kind != TentativeDefinition)
1075 return false;
1076
1077 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1078 if ((*I)->isThisDeclarationADefinition() == Definition)
1079 return false;
1080 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001081 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001082}
1083
Sebastian Redl31310a22010-02-01 20:16:42 +00001084VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001085 VarDecl *First = getFirstDeclaration();
1086 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1087 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001088 if ((*I)->isThisDeclarationADefinition() == Definition)
1089 return *I;
1090 }
1091 return 0;
1092}
1093
John McCall110e8e52010-10-29 22:22:43 +00001094VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1095 DefinitionKind Kind = DeclarationOnly;
1096
1097 const VarDecl *First = getFirstDeclaration();
1098 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1099 I != E; ++I)
1100 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1101
1102 return Kind;
1103}
1104
Sebastian Redl31310a22010-02-01 20:16:42 +00001105const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001106 redecl_iterator I = redecls_begin(), E = redecls_end();
1107 while (I != E && !I->getInit())
1108 ++I;
1109
1110 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001111 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001112 return I->getInit();
1113 }
1114 return 0;
1115}
1116
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001117bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001118 if (Decl::isOutOfLine())
1119 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001120
1121 if (!isStaticDataMember())
1122 return false;
1123
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001124 // If this static data member was instantiated from a static data member of
1125 // a class template, check whether that static data member was defined
1126 // out-of-line.
1127 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1128 return VD->isOutOfLine();
1129
1130 return false;
1131}
1132
Douglas Gregor0d035142009-10-27 18:42:08 +00001133VarDecl *VarDecl::getOutOfLineDefinition() {
1134 if (!isStaticDataMember())
1135 return 0;
1136
1137 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1138 RD != RDEnd; ++RD) {
1139 if (RD->getLexicalDeclContext()->isFileContext())
1140 return *RD;
1141 }
1142
1143 return 0;
1144}
1145
Douglas Gregor838db382010-02-11 01:19:42 +00001146void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001147 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1148 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001149 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001150 }
1151
1152 Init = I;
1153}
1154
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001155VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001156 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001157 return cast<VarDecl>(MSI->getInstantiatedFrom());
1158
1159 return 0;
1160}
1161
Douglas Gregor663b5a02009-10-14 20:14:33 +00001162TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001163 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001164 return MSI->getTemplateSpecializationKind();
1165
1166 return TSK_Undeclared;
1167}
1168
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001169MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001170 return getASTContext().getInstantiatedFromStaticDataMember(this);
1171}
1172
Douglas Gregor0a897e32009-10-15 17:21:20 +00001173void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1174 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001175 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001176 assert(MSI && "Not an instantiated static data member?");
1177 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001178 if (TSK != TSK_ExplicitSpecialization &&
1179 PointOfInstantiation.isValid() &&
1180 MSI->getPointOfInstantiation().isInvalid())
1181 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001182}
1183
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001184//===----------------------------------------------------------------------===//
1185// ParmVarDecl Implementation
1186//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001187
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001188ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1189 SourceLocation L, IdentifierInfo *Id,
1190 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001191 StorageClass S, StorageClass SCAsWritten,
1192 Expr *DefArg) {
1193 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1194 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001195}
1196
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001197Expr *ParmVarDecl::getDefaultArg() {
1198 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1199 assert(!hasUninstantiatedDefaultArg() &&
1200 "Default argument is not yet instantiated!");
1201
1202 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001203 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001204 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001205
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001206 return Arg;
1207}
1208
1209unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001210 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001211 return E->getNumTemporaries();
1212
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001213 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001214}
1215
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001216CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1217 assert(getNumDefaultArgTemporaries() &&
1218 "Default arguments does not have any temporaries!");
1219
John McCall4765fa02010-12-06 08:20:24 +00001220 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001221 return E->getTemporary(i);
1222}
1223
1224SourceRange ParmVarDecl::getDefaultArgRange() const {
1225 if (const Expr *E = getInit())
1226 return E->getSourceRange();
1227
1228 if (hasUninstantiatedDefaultArg())
1229 return getUninstantiatedDefaultArg()->getSourceRange();
1230
1231 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001232}
1233
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001234bool ParmVarDecl::isParameterPack() const {
1235 return isa<PackExpansionType>(getType());
1236}
1237
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001238//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001239// FunctionDecl Implementation
1240//===----------------------------------------------------------------------===//
1241
John McCall136a6982009-09-11 06:45:03 +00001242void FunctionDecl::getNameForDiagnostic(std::string &S,
1243 const PrintingPolicy &Policy,
1244 bool Qualified) const {
1245 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1246 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1247 if (TemplateArgs)
1248 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00001249 TemplateArgs->data(),
1250 TemplateArgs->size(),
John McCall136a6982009-09-11 06:45:03 +00001251 Policy);
1252
1253}
Ted Kremenek27f8a282008-05-20 00:43:19 +00001254
Ted Kremenek9498d382010-04-29 16:49:01 +00001255bool FunctionDecl::isVariadic() const {
1256 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1257 return FT->isVariadic();
1258 return false;
1259}
1260
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001261bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1262 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1263 if (I->Body) {
1264 Definition = *I;
1265 return true;
1266 }
1267 }
1268
1269 return false;
1270}
1271
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001272Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001273 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1274 if (I->Body) {
1275 Definition = *I;
1276 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001277 }
1278 }
1279
1280 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001281}
1282
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001283void FunctionDecl::setBody(Stmt *B) {
1284 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001285 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001286 EndRangeLoc = B->getLocEnd();
1287}
1288
Douglas Gregor21386642010-09-28 21:55:22 +00001289void FunctionDecl::setPure(bool P) {
1290 IsPure = P;
1291 if (P)
1292 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1293 Parent->markedVirtualFunctionPure();
1294}
1295
Douglas Gregor48a83b52009-09-12 00:17:51 +00001296bool FunctionDecl::isMain() const {
1297 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001298 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001299 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001300 getIdentifier() && getIdentifier()->isStr("main");
1301}
1302
Douglas Gregor48a83b52009-09-12 00:17:51 +00001303bool FunctionDecl::isExternC() const {
1304 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001305 // In C, any non-static, non-overloadable function has external
1306 // linkage.
1307 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001308 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001309
Mike Stump1eb44332009-09-09 15:08:12 +00001310 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001311 DC = DC->getParent()) {
1312 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1313 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001314 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001315 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001316
1317 break;
1318 }
Douglas Gregor45975532010-08-17 16:09:23 +00001319
1320 if (DC->isRecord())
1321 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001322 }
1323
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001324 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001325}
1326
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001327bool FunctionDecl::isGlobal() const {
1328 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1329 return Method->isStatic();
1330
John McCalld931b082010-08-26 03:08:43 +00001331 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001332 return false;
1333
Mike Stump1eb44332009-09-09 15:08:12 +00001334 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001335 DC->isNamespace();
1336 DC = DC->getParent()) {
1337 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1338 if (!Namespace->getDeclName())
1339 return false;
1340 break;
1341 }
1342 }
1343
1344 return true;
1345}
1346
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001347void
1348FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1349 redeclarable_base::setPreviousDeclaration(PrevDecl);
1350
1351 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1352 FunctionTemplateDecl *PrevFunTmpl
1353 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1354 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1355 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1356 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001357
1358 if (PrevDecl->IsInline)
1359 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001360}
1361
1362const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1363 return getFirstDeclaration();
1364}
1365
1366FunctionDecl *FunctionDecl::getCanonicalDecl() {
1367 return getFirstDeclaration();
1368}
1369
Douglas Gregor381d34e2010-12-06 18:36:25 +00001370void FunctionDecl::setStorageClass(StorageClass SC) {
1371 assert(isLegalForFunction(SC));
1372 if (getStorageClass() != SC)
1373 ClearLinkageCache();
1374
1375 SClass = SC;
1376}
1377
Douglas Gregor3e41d602009-02-13 23:20:09 +00001378/// \brief Returns a value indicating whether this function
1379/// corresponds to a builtin function.
1380///
1381/// The function corresponds to a built-in function if it is
1382/// declared at translation scope or within an extern "C" block and
1383/// its name matches with the name of a builtin. The returned value
1384/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001385/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001386/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001387unsigned FunctionDecl::getBuiltinID() const {
1388 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001389 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1390 return 0;
1391
1392 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1393 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1394 return BuiltinID;
1395
1396 // This function has the name of a known C library
1397 // function. Determine whether it actually refers to the C library
1398 // function or whether it just has the same name.
1399
Douglas Gregor9add3172009-02-17 03:23:10 +00001400 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001401 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001402 return 0;
1403
Douglas Gregor3c385e52009-02-14 18:57:46 +00001404 // If this function is at translation-unit scope and we're not in
1405 // C++, it refers to the C library function.
1406 if (!Context.getLangOptions().CPlusPlus &&
1407 getDeclContext()->isTranslationUnit())
1408 return BuiltinID;
1409
1410 // If the function is in an extern "C" linkage specification and is
1411 // not marked "overloadable", it's the real function.
1412 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001413 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001414 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001415 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001416 return BuiltinID;
1417
1418 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001419 return 0;
1420}
1421
1422
Chris Lattner1ad9b282009-04-25 06:03:53 +00001423/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001424/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001425/// after it has been created.
1426unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001427 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001428 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001429 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001430 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Reid Spencer5f016e22007-07-11 17:01:13 +00001432}
1433
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001434void FunctionDecl::setParams(ASTContext &C,
1435 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001437 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001438
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 // Zero params -> null pointer.
1440 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001441 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001442 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001444
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001445 // Update source range. The check below allows us to set EndRangeLoc before
1446 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001447 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001448 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 }
1450}
1451
Chris Lattner8123a952008-04-10 02:22:51 +00001452/// getMinRequiredArguments - Returns the minimum number of arguments
1453/// needed to call this function. This may be fewer than the number of
1454/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001455/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001456unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001457 if (!getASTContext().getLangOptions().CPlusPlus)
1458 return getNumParams();
1459
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001460 unsigned NumRequiredArgs = getNumParams();
1461
1462 // If the last parameter is a parameter pack, we don't need an argument for
1463 // it.
1464 if (NumRequiredArgs > 0 &&
1465 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1466 --NumRequiredArgs;
1467
1468 // If this parameter has a default argument, we don't need an argument for
1469 // it.
1470 while (NumRequiredArgs > 0 &&
1471 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001472 --NumRequiredArgs;
1473
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001474 // We might have parameter packs before the end. These can't be deduced,
1475 // but they can still handle multiple arguments.
1476 unsigned ArgIdx = NumRequiredArgs;
1477 while (ArgIdx > 0) {
1478 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1479 NumRequiredArgs = ArgIdx;
1480
1481 --ArgIdx;
1482 }
1483
Chris Lattner8123a952008-04-10 02:22:51 +00001484 return NumRequiredArgs;
1485}
1486
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001487bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001488 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001489 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001490
1491 if (isa<CXXMethodDecl>(this)) {
1492 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1493 return true;
1494 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001495
1496 switch (getTemplateSpecializationKind()) {
1497 case TSK_Undeclared:
1498 case TSK_ExplicitSpecialization:
1499 return false;
1500
1501 case TSK_ImplicitInstantiation:
1502 case TSK_ExplicitInstantiationDeclaration:
1503 case TSK_ExplicitInstantiationDefinition:
1504 // Handle below.
1505 break;
1506 }
1507
1508 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001509 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001510 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001511 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001512
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001513 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001514 return PatternDecl->isInlined();
1515
1516 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001517}
1518
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001519/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001520/// definition will be externally visible.
1521///
1522/// Inline function definitions are always available for inlining optimizations.
1523/// However, depending on the language dialect, declaration specifiers, and
1524/// attributes, the definition of an inline function may or may not be
1525/// "externally" visible to other translation units in the program.
1526///
1527/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001528/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001529/// inline definition becomes externally visible (C99 6.7.4p6).
1530///
1531/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1532/// definition, we use the GNU semantics for inline, which are nearly the
1533/// opposite of C99 semantics. In particular, "inline" by itself will create
1534/// an externally visible symbol, but "extern inline" will not create an
1535/// externally visible symbol.
1536bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1537 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001538 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001539 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001540
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001541 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001542 // If it's not the case that both 'inline' and 'extern' are
1543 // specified on the definition, then this inline definition is
1544 // externally visible.
1545 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1546 return true;
1547
1548 // If any declaration is 'inline' but not 'extern', then this definition
1549 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001550 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1551 Redecl != RedeclEnd;
1552 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001553 if (Redecl->isInlineSpecified() &&
1554 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001555 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001556 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001557
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001558 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001559 }
1560
1561 // C99 6.7.4p6:
1562 // [...] If all of the file scope declarations for a function in a
1563 // translation unit include the inline function specifier without extern,
1564 // then the definition in that translation unit is an inline definition.
1565 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1566 Redecl != RedeclEnd;
1567 ++Redecl) {
1568 // Only consider file-scope declarations in this test.
1569 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1570 continue;
1571
John McCalld931b082010-08-26 03:08:43 +00001572 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001573 return true; // Not an inline definition
1574 }
1575
1576 // C99 6.7.4p6:
1577 // An inline definition does not provide an external definition for the
1578 // function, and does not forbid an external definition in another
1579 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001580 return false;
1581}
1582
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001583/// getOverloadedOperator - Which C++ overloaded operator this
1584/// function represents, if any.
1585OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001586 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1587 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001588 else
1589 return OO_None;
1590}
1591
Sean Hunta6c058d2010-01-13 09:01:02 +00001592/// getLiteralIdentifier - The literal suffix identifier this function
1593/// represents, if any.
1594const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1595 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1596 return getDeclName().getCXXLiteralIdentifier();
1597 else
1598 return 0;
1599}
1600
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001601FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1602 if (TemplateOrSpecialization.isNull())
1603 return TK_NonTemplate;
1604 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1605 return TK_FunctionTemplate;
1606 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1607 return TK_MemberSpecialization;
1608 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1609 return TK_FunctionTemplateSpecialization;
1610 if (TemplateOrSpecialization.is
1611 <DependentFunctionTemplateSpecializationInfo*>())
1612 return TK_DependentFunctionTemplateSpecialization;
1613
1614 assert(false && "Did we miss a TemplateOrSpecialization type?");
1615 return TK_NonTemplate;
1616}
1617
Douglas Gregor2db32322009-10-07 23:56:10 +00001618FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001619 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001620 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1621
1622 return 0;
1623}
1624
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001625MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1626 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1627}
1628
Douglas Gregor2db32322009-10-07 23:56:10 +00001629void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001630FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1631 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001632 TemplateSpecializationKind TSK) {
1633 assert(TemplateOrSpecialization.isNull() &&
1634 "Member function is already a specialization");
1635 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001636 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001637 TemplateOrSpecialization = Info;
1638}
1639
Douglas Gregor3b846b62009-10-27 20:53:28 +00001640bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001641 // If the function is invalid, it can't be implicitly instantiated.
1642 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001643 return false;
1644
1645 switch (getTemplateSpecializationKind()) {
1646 case TSK_Undeclared:
1647 case TSK_ExplicitSpecialization:
1648 case TSK_ExplicitInstantiationDefinition:
1649 return false;
1650
1651 case TSK_ImplicitInstantiation:
1652 return true;
1653
1654 case TSK_ExplicitInstantiationDeclaration:
1655 // Handled below.
1656 break;
1657 }
1658
1659 // Find the actual template from which we will instantiate.
1660 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001661 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001662 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001663 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001664
1665 // C++0x [temp.explicit]p9:
1666 // Except for inline functions, other explicit instantiation declarations
1667 // have the effect of suppressing the implicit instantiation of the entity
1668 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001669 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001670 return true;
1671
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001672 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001673}
1674
1675FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1676 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1677 while (Primary->getInstantiatedFromMemberTemplate()) {
1678 // If we have hit a point where the user provided a specialization of
1679 // this template, we're done looking.
1680 if (Primary->isMemberSpecialization())
1681 break;
1682
1683 Primary = Primary->getInstantiatedFromMemberTemplate();
1684 }
1685
1686 return Primary->getTemplatedDecl();
1687 }
1688
1689 return getInstantiatedFromMemberFunction();
1690}
1691
Douglas Gregor16e8be22009-06-29 17:30:29 +00001692FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001693 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001694 = TemplateOrSpecialization
1695 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001696 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001697 }
1698 return 0;
1699}
1700
1701const TemplateArgumentList *
1702FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001703 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001704 = TemplateOrSpecialization
1705 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001706 return Info->TemplateArguments;
1707 }
1708 return 0;
1709}
1710
Abramo Bagnarae03db982010-05-20 15:32:11 +00001711const TemplateArgumentListInfo *
1712FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1713 if (FunctionTemplateSpecializationInfo *Info
1714 = TemplateOrSpecialization
1715 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1716 return Info->TemplateArgumentsAsWritten;
1717 }
1718 return 0;
1719}
1720
Mike Stump1eb44332009-09-09 15:08:12 +00001721void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001722FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1723 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001724 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001725 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001726 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001727 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1728 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001729 assert(TSK != TSK_Undeclared &&
1730 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001731 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001732 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001733 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001734 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1735 TemplateArgs,
1736 TemplateArgsAsWritten,
1737 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001738 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001739
Douglas Gregor127102b2009-06-29 20:59:39 +00001740 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001741 // function template specializations.
1742 if (InsertPos)
1743 Template->getSpecializations().InsertNode(Info, InsertPos);
1744 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001745 // Try to insert the new node. If there is an existing node, leave it, the
1746 // set will contain the canonical decls while
1747 // FunctionTemplateDecl::findSpecialization will return
1748 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001749 FunctionTemplateSpecializationInfo *Existing
1750 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001751 (void)Existing;
1752 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1753 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001754 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001755}
1756
John McCallaf2094e2010-04-08 09:05:18 +00001757void
1758FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1759 const UnresolvedSetImpl &Templates,
1760 const TemplateArgumentListInfo &TemplateArgs) {
1761 assert(TemplateOrSpecialization.isNull());
1762 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1763 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001764 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001765 void *Buffer = Context.Allocate(Size);
1766 DependentFunctionTemplateSpecializationInfo *Info =
1767 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1768 TemplateArgs);
1769 TemplateOrSpecialization = Info;
1770}
1771
1772DependentFunctionTemplateSpecializationInfo::
1773DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1774 const TemplateArgumentListInfo &TArgs)
1775 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1776
1777 d.NumTemplates = Ts.size();
1778 d.NumArgs = TArgs.size();
1779
1780 FunctionTemplateDecl **TsArray =
1781 const_cast<FunctionTemplateDecl**>(getTemplates());
1782 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1783 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1784
1785 TemplateArgumentLoc *ArgsArray =
1786 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1787 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1788 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1789}
1790
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001791TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001792 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001793 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001794 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001795 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001796 if (FTSInfo)
1797 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Douglas Gregor2db32322009-10-07 23:56:10 +00001799 MemberSpecializationInfo *MSInfo
1800 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1801 if (MSInfo)
1802 return MSInfo->getTemplateSpecializationKind();
1803
1804 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001805}
1806
Mike Stump1eb44332009-09-09 15:08:12 +00001807void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001808FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1809 SourceLocation PointOfInstantiation) {
1810 if (FunctionTemplateSpecializationInfo *FTSInfo
1811 = TemplateOrSpecialization.dyn_cast<
1812 FunctionTemplateSpecializationInfo*>()) {
1813 FTSInfo->setTemplateSpecializationKind(TSK);
1814 if (TSK != TSK_ExplicitSpecialization &&
1815 PointOfInstantiation.isValid() &&
1816 FTSInfo->getPointOfInstantiation().isInvalid())
1817 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1818 } else if (MemberSpecializationInfo *MSInfo
1819 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1820 MSInfo->setTemplateSpecializationKind(TSK);
1821 if (TSK != TSK_ExplicitSpecialization &&
1822 PointOfInstantiation.isValid() &&
1823 MSInfo->getPointOfInstantiation().isInvalid())
1824 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1825 } else
1826 assert(false && "Function cannot have a template specialization kind");
1827}
1828
1829SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001830 if (FunctionTemplateSpecializationInfo *FTSInfo
1831 = TemplateOrSpecialization.dyn_cast<
1832 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001833 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001834 else if (MemberSpecializationInfo *MSInfo
1835 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001836 return MSInfo->getPointOfInstantiation();
1837
1838 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001839}
1840
Douglas Gregor9f185072009-09-11 20:15:17 +00001841bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001842 if (Decl::isOutOfLine())
1843 return true;
1844
1845 // If this function was instantiated from a member function of a
1846 // class template, check whether that member function was defined out-of-line.
1847 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1848 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001849 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001850 return Definition->isOutOfLine();
1851 }
1852
1853 // If this function was instantiated from a function template,
1854 // check whether that function template was defined out-of-line.
1855 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1856 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001857 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001858 return Definition->isOutOfLine();
1859 }
1860
1861 return false;
1862}
1863
Chris Lattner8a934232008-03-31 00:36:02 +00001864//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001865// FieldDecl Implementation
1866//===----------------------------------------------------------------------===//
1867
Jay Foad4ba2a172011-01-12 09:06:06 +00001868FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
1869 SourceLocation L, IdentifierInfo *Id, QualType T,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001870 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1871 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1872}
1873
1874bool FieldDecl::isAnonymousStructOrUnion() const {
1875 if (!isImplicit() || getDeclName())
1876 return false;
1877
1878 if (const RecordType *Record = getType()->getAs<RecordType>())
1879 return Record->getDecl()->isAnonymousStructOrUnion();
1880
1881 return false;
1882}
1883
John McCallba4f5d52011-01-20 07:57:12 +00001884unsigned FieldDecl::getFieldIndex() const {
1885 if (CachedFieldIndex) return CachedFieldIndex - 1;
1886
1887 unsigned index = 0;
1888 RecordDecl::field_iterator
1889 i = getParent()->field_begin(), e = getParent()->field_end();
1890 while (true) {
1891 assert(i != e && "failed to find field in parent!");
1892 if (*i == this)
1893 break;
1894
1895 ++i;
1896 ++index;
1897 }
1898
1899 CachedFieldIndex = index + 1;
1900 return index;
1901}
1902
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001903//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001904// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001905//===----------------------------------------------------------------------===//
1906
Douglas Gregor1693e152010-07-06 18:42:40 +00001907SourceLocation TagDecl::getOuterLocStart() const {
1908 return getTemplateOrInnerLocStart(this);
1909}
1910
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001911SourceRange TagDecl::getSourceRange() const {
1912 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001913 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001914}
1915
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001916TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001917 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001918}
1919
Douglas Gregor60e70642010-05-19 18:39:18 +00001920void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1921 TypedefDeclOrQualifier = TDD;
1922 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00001923 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001924 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00001925}
1926
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001927void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001928 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001929
1930 if (isa<CXXRecordDecl>(this)) {
1931 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1932 struct CXXRecordDecl::DefinitionData *Data =
1933 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001934 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1935 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001936 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001937}
1938
1939void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001940 assert((!isa<CXXRecordDecl>(this) ||
1941 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1942 "definition completed but not started");
1943
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001944 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001945 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00001946
1947 if (ASTMutationListener *L = getASTMutationListener())
1948 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001949}
1950
Douglas Gregor952b0172010-02-11 01:04:33 +00001951TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001952 if (isDefinition())
1953 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00001954 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1955 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001956
1957 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001958 R != REnd; ++R)
1959 if (R->isDefinition())
1960 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001961
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001962 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001963}
1964
John McCallb6217662010-03-15 10:12:16 +00001965void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1966 SourceRange QualifierRange) {
1967 if (Qualifier) {
1968 // Make sure the extended qualifier info is allocated.
1969 if (!hasExtInfo())
1970 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1971 // Set qualifier info.
1972 getExtInfo()->NNS = Qualifier;
1973 getExtInfo()->NNSRange = QualifierRange;
1974 }
1975 else {
1976 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1977 assert(QualifierRange.isInvalid());
1978 if (hasExtInfo()) {
1979 getASTContext().Deallocate(getExtInfo());
1980 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1981 }
1982 }
1983}
1984
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001985//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001986// EnumDecl Implementation
1987//===----------------------------------------------------------------------===//
1988
1989EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1990 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001991 EnumDecl *PrevDecl, bool IsScoped,
1992 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001993 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001994 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001995 C.getTypeDeclType(Enum, PrevDecl);
1996 return Enum;
1997}
1998
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001999EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002000 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002001 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002002}
2003
Douglas Gregor838db382010-02-11 01:19:42 +00002004void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002005 QualType NewPromotionType,
2006 unsigned NumPositiveBits,
2007 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002008 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002009 if (!IntegerType)
2010 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002011 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002012 setNumPositiveBits(NumPositiveBits);
2013 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002014 TagDecl::completeDefinition();
2015}
2016
2017//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002018// RecordDecl Implementation
2019//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002020
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00002021RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002022 IdentifierInfo *Id, RecordDecl *PrevDecl,
2023 SourceLocation TKL)
2024 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00002025 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002026 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002027 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002028 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002029 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002030}
2031
Jay Foad4ba2a172011-01-12 09:06:06 +00002032RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002033 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00002034 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00002035
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002036 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002037 C.getTypeDeclType(R, PrevDecl);
2038 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002039}
2040
Jay Foad4ba2a172011-01-12 09:06:06 +00002041RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002042 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
2043 SourceLocation());
2044}
2045
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002046bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002047 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002048 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2049}
2050
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002051RecordDecl::field_iterator RecordDecl::field_begin() const {
2052 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2053 LoadFieldsFromExternalStorage();
2054
2055 return field_iterator(decl_iterator(FirstDecl));
2056}
2057
Douglas Gregor44b43212008-12-11 16:49:14 +00002058/// completeDefinition - Notes that the definition of this type is now
2059/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00002060void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00002061 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002062 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00002063}
2064
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002065void RecordDecl::LoadFieldsFromExternalStorage() const {
2066 ExternalASTSource *Source = getASTContext().getExternalSource();
2067 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2068
2069 // Notify that we have a RecordDecl doing some initialization.
2070 ExternalASTSource::Deserializing TheFields(Source);
2071
2072 llvm::SmallVector<Decl*, 64> Decls;
2073 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2074 return;
2075
2076#ifndef NDEBUG
2077 // Check that all decls we got were FieldDecls.
2078 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2079 assert(isa<FieldDecl>(Decls[i]));
2080#endif
2081
2082 LoadedFieldsFromExternalStorage = true;
2083
2084 if (Decls.empty())
2085 return;
2086
2087 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2088}
2089
Steve Naroff56ee6892008-10-08 17:01:13 +00002090//===----------------------------------------------------------------------===//
2091// BlockDecl Implementation
2092//===----------------------------------------------------------------------===//
2093
Douglas Gregor838db382010-02-11 01:19:42 +00002094void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002095 unsigned NParms) {
2096 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Steve Naroffe78b8092009-03-13 16:56:44 +00002098 // Zero params -> null pointer.
2099 if (NParms) {
2100 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002101 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002102 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2103 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2104 }
2105}
2106
John McCall469a1eb2011-02-02 13:00:07 +00002107void BlockDecl::setCapturedDecls(ASTContext &Context,
2108 VarDecl * const *begin,
2109 VarDecl * const *end,
2110 bool capturesCXXThis) {
2111 CapturesCXXThis = capturesCXXThis;
2112
2113 if (begin == end) {
2114 NumCapturedDecls = 0;
2115 CapturedDecls = 0;
2116 return;
2117 }
2118
2119 NumCapturedDecls = end - begin;
2120 CapturedDecls = new (Context) VarDecl*[NumCapturedDecls];
2121 memcpy(CapturedDecls, begin, NumCapturedDecls * sizeof(VarDecl*));
Steve Naroffe78b8092009-03-13 16:56:44 +00002122}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002123
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002124SourceRange BlockDecl::getSourceRange() const {
2125 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2126}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002127
2128//===----------------------------------------------------------------------===//
2129// Other Decl Allocation/Deallocation Method Implementations
2130//===----------------------------------------------------------------------===//
2131
2132TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2133 return new (C) TranslationUnitDecl(C);
2134}
2135
2136NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2137 SourceLocation L, IdentifierInfo *Id) {
2138 return new (C) NamespaceDecl(DC, L, Id);
2139}
2140
Douglas Gregor06c91932010-10-27 19:49:05 +00002141NamespaceDecl *NamespaceDecl::getNextNamespace() {
2142 return dyn_cast_or_null<NamespaceDecl>(
2143 NextNamespace.get(getASTContext().getExternalSource()));
2144}
2145
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002146ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2147 SourceLocation L, IdentifierInfo *Id, QualType T) {
2148 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2149}
2150
2151FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00002152 const DeclarationNameInfo &NameInfo,
2153 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002154 StorageClass S, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002155 bool isInlineSpecified,
2156 bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002157 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor8f150942010-12-09 16:59:22 +00002158 S, SCAsWritten, isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002159 New->HasWrittenPrototype = hasWrittenPrototype;
2160 return New;
2161}
2162
2163BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2164 return new (C) BlockDecl(DC, L);
2165}
2166
2167EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2168 SourceLocation L,
2169 IdentifierInfo *Id, QualType T,
2170 Expr *E, const llvm::APSInt &V) {
2171 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2172}
2173
Benjamin Kramerd9811462010-11-21 14:11:41 +00002174IndirectFieldDecl *
2175IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2176 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2177 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002178 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2179}
2180
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002181SourceRange EnumConstantDecl::getSourceRange() const {
2182 SourceLocation End = getLocation();
2183 if (Init)
2184 End = Init->getLocEnd();
2185 return SourceRange(getLocation(), End);
2186}
2187
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002188TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2189 SourceLocation L, IdentifierInfo *Id,
2190 TypeSourceInfo *TInfo) {
2191 return new (C) TypedefDecl(DC, L, Id, TInfo);
2192}
2193
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002194FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2195 SourceLocation L,
2196 StringLiteral *Str) {
2197 return new (C) FileScopeAsmDecl(DC, L, Str);
2198}