blob: f40907cb732f5605a2eb1f7cba2cdfa6869285ef [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 McCalle7bc9722010-10-28 04:18:25 +000036static const VisibilityAttr *GetExplicitVisibility(const Decl *D) {
37 // If the decl is redeclarable, make sure we use the explicit
38 // visibility attribute from the most recent declaration.
39 //
40 // Note that this isn't necessary for tags, which can't have their
41 // visibility adjusted.
42 if (isa<VarDecl>(D)) {
43 return cast<VarDecl>(D)->getMostRecentDeclaration()
44 ->getAttr<VisibilityAttr>();
45 } else if (isa<FunctionDecl>(D)) {
46 return cast<FunctionDecl>(D)->getMostRecentDeclaration()
47 ->getAttr<VisibilityAttr>();
48 } else {
49 return D->getAttr<VisibilityAttr>();
50 }
51}
52
John McCall1fb0caa2010-10-22 21:05:15 +000053static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
54 switch (A->getVisibility()) {
55 case VisibilityAttr::Default:
56 return DefaultVisibility;
57 case VisibilityAttr::Hidden:
58 return HiddenVisibility;
59 case VisibilityAttr::Protected:
60 return ProtectedVisibility;
61 }
62 return DefaultVisibility;
63}
64
John McCallaf146032010-10-30 11:50:40 +000065typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall1fb0caa2010-10-22 21:05:15 +000066typedef std::pair<Linkage,Visibility> LVPair;
John McCallaf146032010-10-30 11:50:40 +000067
John McCall1fb0caa2010-10-22 21:05:15 +000068static LVPair merge(LVPair L, LVPair R) {
69 return LVPair(minLinkage(L.first, R.first),
70 minVisibility(L.second, R.second));
71}
72
John McCallaf146032010-10-30 11:50:40 +000073static LVPair merge(LVPair L, LinkageInfo R) {
74 return LVPair(minLinkage(L.first, R.linkage()),
75 minVisibility(L.second, R.visibility()));
76}
77
Benjamin Kramer752c2e92010-11-05 19:56:37 +000078namespace {
John McCall36987482010-11-02 01:45:15 +000079/// Flags controlling the computation of linkage and visibility.
80struct LVFlags {
81 bool ConsiderGlobalVisibility;
82 bool ConsiderVisibilityAttributes;
83
84 LVFlags() : ConsiderGlobalVisibility(true),
85 ConsiderVisibilityAttributes(true) {
86 }
87
Douglas Gregor381d34e2010-12-06 18:36:25 +000088 /// \brief Returns a set of flags that is only useful for computing the
89 /// linkage, not the visibility, of a declaration.
90 static LVFlags CreateOnlyDeclLinkage() {
91 LVFlags F;
92 F.ConsiderGlobalVisibility = false;
93 F.ConsiderVisibilityAttributes = false;
94 return F;
95 }
96
John McCall36987482010-11-02 01:45:15 +000097 /// Returns a set of flags, otherwise based on these, which ignores
98 /// off all sources of visibility except template arguments.
99 LVFlags onlyTemplateVisibility() const {
100 LVFlags F = *this;
101 F.ConsiderGlobalVisibility = false;
102 F.ConsiderVisibilityAttributes = false;
103 return F;
104 }
Douglas Gregor89d63e52010-12-06 18:50:56 +0000105};
Benjamin Kramer752c2e92010-11-05 19:56:37 +0000106} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +0000107
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000108/// \brief Get the most restrictive linkage for the types in the given
109/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +0000110static LVPair
111getLVForTemplateParameterList(const TemplateParameterList *Params) {
112 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000113 for (TemplateParameterList::const_iterator P = Params->begin(),
114 PEnd = Params->end();
115 P != PEnd; ++P) {
116 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
117 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000118 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000119 continue;
120 }
121
122 if (TemplateTemplateParmDecl *TTP
123 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000124 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000125 }
126 }
127
John McCall1fb0caa2010-10-22 21:05:15 +0000128 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000129}
130
Douglas Gregor381d34e2010-12-06 18:36:25 +0000131/// getLVForDecl - Get the linkage and visibility for the given declaration.
132static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
133
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000134/// \brief Get the most restrictive linkage for the types and
135/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +0000136static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregor381d34e2010-12-06 18:36:25 +0000137 unsigned NumArgs,
138 LVFlags &F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000139 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000140
141 for (unsigned I = 0; I != NumArgs; ++I) {
142 switch (Args[I].getKind()) {
143 case TemplateArgument::Null:
144 case TemplateArgument::Integral:
145 case TemplateArgument::Expression:
146 break;
147
148 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +0000149 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000150 break;
151
152 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000153 // The decl can validly be null as the representation of nullptr
154 // arguments, valid only in C++0x.
155 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor89d63e52010-12-06 18:50:56 +0000156 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
157 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000158 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000159 break;
160
161 case TemplateArgument::Template:
Douglas Gregor89d63e52010-12-06 18:50:56 +0000162 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
163 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000164 break;
165
166 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000167 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000168 Args[I].pack_size(),
169 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000170 break;
171 }
172 }
173
John McCall1fb0caa2010-10-22 21:05:15 +0000174 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000175}
176
John McCallaf146032010-10-30 11:50:40 +0000177static LVPair
Douglas Gregor381d34e2010-12-06 18:36:25 +0000178getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
179 LVFlags &F) {
180 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000181}
182
John McCall36987482010-11-02 01:45:15 +0000183static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000184 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000185 "Not a name having namespace scope");
186 ASTContext &Context = D->getASTContext();
187
188 // C++ [basic.link]p3:
189 // A name having namespace scope (3.3.6) has internal linkage if it
190 // is the name of
191 // - an object, reference, function or function template that is
192 // explicitly declared static; or,
193 // (This bullet corresponds to C99 6.2.2p3.)
194 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
195 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000196 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000197 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000198
199 // - an object or reference that is explicitly declared const
200 // and neither explicitly declared extern nor previously
201 // declared to have external linkage; or
202 // (there is no equivalent in C99)
203 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000204 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000205 Var->getStorageClass() != SC_Extern &&
206 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000207 bool FoundExtern = false;
208 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
209 PrevVar && !FoundExtern;
210 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000211 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000212 FoundExtern = true;
213
214 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000215 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000216 }
217 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000218 // C++ [temp]p4:
219 // A non-member function template can have internal linkage; any
220 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000221 const FunctionDecl *Function = 0;
222 if (const FunctionTemplateDecl *FunTmpl
223 = dyn_cast<FunctionTemplateDecl>(D))
224 Function = FunTmpl->getTemplatedDecl();
225 else
226 Function = cast<FunctionDecl>(D);
227
228 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000229 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000230 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000231 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
232 // - a data member of an anonymous union.
233 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000234 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000235 }
236
John McCall1fb0caa2010-10-22 21:05:15 +0000237 if (D->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000238 return LinkageInfo::uniqueExternal();
John McCalle7bc9722010-10-28 04:18:25 +0000239
John McCall1fb0caa2010-10-22 21:05:15 +0000240 // Set up the defaults.
241
242 // C99 6.2.2p5:
243 // If the declaration of an identifier for an object has file
244 // scope and no storage-class specifier, its linkage is
245 // external.
John McCallaf146032010-10-30 11:50:40 +0000246 LinkageInfo LV;
247
John McCall36987482010-11-02 01:45:15 +0000248 if (F.ConsiderVisibilityAttributes) {
249 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
250 LV.setVisibility(GetVisibilityFromAttr(VA), true);
251 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-12-10 02:59:44 +0000252 } else {
253 // If we're declared in a namespace with a visibility attribute,
254 // use that namespace's visibility, but don't call it explicit.
255 for (const DeclContext *DC = D->getDeclContext();
256 !isa<TranslationUnitDecl>(DC);
257 DC = DC->getParent()) {
258 if (!isa<NamespaceDecl>(DC)) continue;
259 if (const VisibilityAttr *VA =
260 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
261 LV.setVisibility(GetVisibilityFromAttr(VA), false);
262 F.ConsiderGlobalVisibility = false;
263 break;
264 }
265 }
John McCall36987482010-11-02 01:45:15 +0000266 }
John McCallaf146032010-10-30 11:50:40 +0000267 }
John McCall1fb0caa2010-10-22 21:05:15 +0000268
Douglas Gregord85b5b92009-11-25 22:24:25 +0000269 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000270
Douglas Gregord85b5b92009-11-25 22:24:25 +0000271 // A name having namespace scope has external linkage if it is the
272 // name of
273 //
274 // - an object or reference, unless it has internal linkage; or
275 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000276 // GCC applies the following optimization to variables and static
277 // data members, but not to functions:
278 //
John McCall1fb0caa2010-10-22 21:05:15 +0000279 // Modify the variable's LV by the LV of its type unless this is
280 // C or extern "C". This follows from [basic.link]p9:
281 // A type without linkage shall not be used as the type of a
282 // variable or function with external linkage unless
283 // - the entity has C language linkage, or
284 // - the entity is declared within an unnamed namespace, or
285 // - the entity is not used or is defined in the same
286 // translation unit.
287 // and [basic.link]p10:
288 // ...the types specified by all declarations referring to a
289 // given variable or function shall be identical...
290 // C does not have an equivalent rule.
291 //
John McCallac65c622010-10-26 04:59:26 +0000292 // Ignore this if we've got an explicit attribute; the user
293 // probably knows what they're doing.
294 //
John McCall1fb0caa2010-10-22 21:05:15 +0000295 // Note that we don't want to make the variable non-external
296 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000297 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000298 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
299 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000300 return LinkageInfo::uniqueExternal();
301 if (!LV.visibilityExplicit())
302 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000303 }
304
John McCall35cebc32010-11-02 18:38:13 +0000305 if (Var->getStorageClass() == SC_PrivateExtern)
306 LV.setVisibility(HiddenVisibility, true);
307
Douglas Gregord85b5b92009-11-25 22:24:25 +0000308 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000309 (Var->getStorageClass() == SC_Extern ||
310 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000311
Douglas Gregord85b5b92009-11-25 22:24:25 +0000312 // C99 6.2.2p4:
313 // For an identifier declared with the storage-class specifier
314 // extern in a scope in which a prior declaration of that
315 // identifier is visible, if the prior declaration specifies
316 // internal or external linkage, the linkage of the identifier
317 // at the later declaration is the same as the linkage
318 // specified at the prior declaration. If no prior declaration
319 // is visible, or if the prior declaration specifies no
320 // linkage, then the identifier has external linkage.
321 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000322 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000323 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
324 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000325 }
326 }
327
Douglas Gregord85b5b92009-11-25 22:24:25 +0000328 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000329 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000330 // In theory, we can modify the function's LV by the LV of its
331 // type unless it has C linkage (see comment above about variables
332 // for justification). In practice, GCC doesn't do this, so it's
333 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000334
John McCall35cebc32010-11-02 18:38:13 +0000335 if (Function->getStorageClass() == SC_PrivateExtern)
336 LV.setVisibility(HiddenVisibility, true);
337
Douglas Gregord85b5b92009-11-25 22:24:25 +0000338 // C99 6.2.2p5:
339 // If the declaration of an identifier for a function has no
340 // storage-class specifier, its linkage is determined exactly
341 // as if it were declared with the storage-class specifier
342 // extern.
343 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000344 (Function->getStorageClass() == SC_Extern ||
345 Function->getStorageClass() == SC_PrivateExtern ||
346 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000347 // C99 6.2.2p4:
348 // For an identifier declared with the storage-class specifier
349 // extern in a scope in which a prior declaration of that
350 // identifier is visible, if the prior declaration specifies
351 // internal or external linkage, the linkage of the identifier
352 // at the later declaration is the same as the linkage
353 // specified at the prior declaration. If no prior declaration
354 // is visible, or if the prior declaration specifies no
355 // linkage, then the identifier has external linkage.
356 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000357 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000358 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
359 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000360 }
361 }
362
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000363 if (FunctionTemplateSpecializationInfo *SpecInfo
364 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000365 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
366 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000367 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000368 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000369 }
370
Douglas Gregord85b5b92009-11-25 22:24:25 +0000371 // - a named class (Clause 9), or an unnamed class defined in a
372 // typedef declaration in which the class has the typedef name
373 // for linkage purposes (7.1.3); or
374 // - a named enumeration (7.2), or an unnamed enumeration
375 // defined in a typedef declaration in which the enumeration
376 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000377 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
378 // Unnamed tags have no linkage.
379 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000380 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000381
John McCall1fb0caa2010-10-22 21:05:15 +0000382 // If this is a class template specialization, consider the
383 // linkage of the template and template arguments.
384 if (const ClassTemplateSpecializationDecl *Spec
385 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000386 // From the template.
387 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
388 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000389
John McCall1fb0caa2010-10-22 21:05:15 +0000390 // The arguments at which the template was instantiated.
391 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000392 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000393 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000394
John McCallac65c622010-10-26 04:59:26 +0000395 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000396 if (F.ConsiderGlobalVisibility)
397 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000398 (Context.getLangOptions().CPlusPlus &&
399 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000400
Douglas Gregord85b5b92009-11-25 22:24:25 +0000401 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000402 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000403 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000404 if (!isExternalLinkage(EnumLV.linkage()))
405 return LinkageInfo::none();
406 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000407
408 // - a template, unless it is a function template that has
409 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000410 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallaf146032010-10-30 11:50:40 +0000411 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000412
Douglas Gregord85b5b92009-11-25 22:24:25 +0000413 // - a namespace (7.3), unless it is declared within an unnamed
414 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000415 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
416 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000417
John McCall1fb0caa2010-10-22 21:05:15 +0000418 // By extension, we assign external linkage to Objective-C
419 // interfaces.
420 } else if (isa<ObjCInterfaceDecl>(D)) {
421 // fallout
422
423 // Everything not covered here has no linkage.
424 } else {
John McCallaf146032010-10-30 11:50:40 +0000425 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000426 }
427
428 // If we ended up with non-external linkage, visibility should
429 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000430 if (LV.linkage() != ExternalLinkage)
431 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000432
433 // If we didn't end up with hidden visibility, consider attributes
434 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000435 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000436 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000437
438 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000439}
440
John McCall36987482010-11-02 01:45:15 +0000441static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000442 // Only certain class members have linkage. Note that fields don't
443 // really have linkage, but it's convenient to say they do for the
444 // purposes of calculating linkage of pointer-to-data-member
445 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000446 if (!(isa<CXXMethodDecl>(D) ||
447 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000448 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000449 (isa<TagDecl>(D) &&
450 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000451 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000452
John McCall36987482010-11-02 01:45:15 +0000453 LinkageInfo LV;
454
455 // The flags we're going to use to compute the class's visibility.
456 LVFlags ClassF = F;
457
458 // If we have an explicit visibility attribute, merge that in.
459 if (F.ConsiderVisibilityAttributes) {
460 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
461 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
462
463 // Ignore global visibility later, but not this attribute.
464 F.ConsiderGlobalVisibility = false;
465
466 // Ignore both global visibility and attributes when computing our
467 // parent's visibility.
468 ClassF = F.onlyTemplateVisibility();
469 }
470 }
John McCallaf146032010-10-30 11:50:40 +0000471
472 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000473 // linkage.
474 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
475 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000476 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000477
478 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000479 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000480 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000481
John McCall3cdfc4d2010-08-13 08:35:10 +0000482 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000483 TemplateSpecializationKind TSK = TSK_Undeclared;
484
John McCall1fb0caa2010-10-22 21:05:15 +0000485 // If this is a method template specialization, use the linkage for
486 // the template parameters and arguments.
487 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000488 = MD->getTemplateSpecializationInfo()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000489 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallaf146032010-10-30 11:50:40 +0000490 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000491 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000492
493 TSK = Spec->getTemplateSpecializationKind();
494 } else if (MemberSpecializationInfo *MSI =
495 MD->getMemberSpecializationInfo()) {
496 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000497 }
498
John McCall110e8e52010-10-29 22:22:43 +0000499 // If we're paying attention to global visibility, apply
500 // -finline-visibility-hidden if this is an inline method.
501 //
John McCallaf146032010-10-30 11:50:40 +0000502 // Note that ConsiderGlobalVisibility doesn't yet have information
503 // about whether containing classes have visibility attributes,
504 // and that's intentional.
505 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000506 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000507 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
508 // InlineVisibilityHidden only applies to definitions, and
509 // isInlined() only gives meaningful answers on definitions
510 // anyway.
511 const FunctionDecl *Def = 0;
512 if (MD->hasBody(Def) && Def->isInlined())
513 LV.setVisibility(HiddenVisibility);
514 }
John McCall1fb0caa2010-10-22 21:05:15 +0000515
John McCall110e8e52010-10-29 22:22:43 +0000516 // Note that in contrast to basically every other situation, we
517 // *do* apply -fvisibility to method declarations.
518
519 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000520 if (const ClassTemplateSpecializationDecl *Spec
521 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
522 // Merge template argument/parameter information for member
523 // class template specializations.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000524 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallaf146032010-10-30 11:50:40 +0000525 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000526 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000527 }
528
John McCall110e8e52010-10-29 22:22:43 +0000529 // Static data members.
530 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000531 // Modify the variable's linkage by its type, but ignore the
532 // type's visibility unless it's a definition.
533 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
534 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000535 LV.mergeLinkage(UniqueExternalLinkage);
536 if (!LV.visibilityExplicit())
537 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000538 }
539
John McCall36987482010-11-02 01:45:15 +0000540 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000541
542 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000543 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000544 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000545 }
546
John McCall1fb0caa2010-10-22 21:05:15 +0000547 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000548}
549
Douglas Gregor381d34e2010-12-06 18:36:25 +0000550Linkage NamedDecl::getLinkage() const {
551 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000552 assert(Linkage(CachedLinkage) ==
553 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000554 return Linkage(CachedLinkage);
555 }
556
557 CachedLinkage = getLVForDecl(this,
558 LVFlags::CreateOnlyDeclLinkage()).linkage();
559 HasCachedLinkage = 1;
560 return Linkage(CachedLinkage);
561}
562
John McCallaf146032010-10-30 11:50:40 +0000563LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000564 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000565 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000566 HasCachedLinkage = 1;
567 CachedLinkage = LI.linkage();
568 return LI;
John McCall0df95872010-10-29 00:29:13 +0000569}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000570
John McCall36987482010-11-02 01:45:15 +0000571static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000572 // Objective-C: treat all Objective-C declarations as having external
573 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000574 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000575 default:
576 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000577 case Decl::TemplateTemplateParm: // count these as external
578 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000579 case Decl::ObjCAtDefsField:
580 case Decl::ObjCCategory:
581 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000582 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000583 case Decl::ObjCForwardProtocol:
584 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000585 case Decl::ObjCMethod:
586 case Decl::ObjCProperty:
587 case Decl::ObjCPropertyImpl:
588 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000589 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000590 }
591
Douglas Gregord85b5b92009-11-25 22:24:25 +0000592 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000593 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000594 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000595
596 // C++ [basic.link]p5:
597 // In addition, a member function, static data member, a named
598 // class or enumeration of class scope, or an unnamed class or
599 // enumeration defined in a class-scope typedef declaration such
600 // that the class or enumeration has the typedef name for linkage
601 // purposes (7.1.3), has external linkage if the name of the class
602 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000603 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000604 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000605
606 // C++ [basic.link]p6:
607 // The name of a function declared in block scope and the name of
608 // an object declared by a block scope extern declaration have
609 // linkage. If there is a visible declaration of an entity with
610 // linkage having the same name and type, ignoring entities
611 // declared outside the innermost enclosing namespace scope, the
612 // block scope declaration declares that same entity and receives
613 // the linkage of the previous declaration. If there is more than
614 // one such matching entity, the program is ill-formed. Otherwise,
615 // if no matching entity is found, the block scope entity receives
616 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000617 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
618 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000619 if (Function->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000620 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000621
John McCallaf146032010-10-30 11:50:40 +0000622 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000623 if (Flags.ConsiderVisibilityAttributes) {
624 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
625 LV.setVisibility(GetVisibilityFromAttr(VA));
626 }
627
John McCall1fb0caa2010-10-22 21:05:15 +0000628 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000629 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000630 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
631 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000632 }
633
634 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000635 }
636
John McCall0df95872010-10-29 00:29:13 +0000637 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000638 if (Var->getStorageClass() == SC_Extern ||
639 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000640 if (Var->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000641 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000642
John McCallaf146032010-10-30 11:50:40 +0000643 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000644 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000645 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000646 else if (Flags.ConsiderVisibilityAttributes) {
647 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
648 LV.setVisibility(GetVisibilityFromAttr(VA));
649 }
650
John McCall1fb0caa2010-10-22 21:05:15 +0000651 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000652 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000653 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
654 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000655 }
656
657 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000658 }
659 }
660
661 // C++ [basic.link]p6:
662 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000663 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000664}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000665
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000666std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000667 return getQualifiedNameAsString(getASTContext().getLangOptions());
668}
669
670std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000671 const DeclContext *Ctx = getDeclContext();
672
673 if (Ctx->isFunctionOrMethod())
674 return getNameAsString();
675
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000676 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
677 ContextsTy Contexts;
678
679 // Collect contexts.
680 while (Ctx && isa<NamedDecl>(Ctx)) {
681 Contexts.push_back(Ctx);
682 Ctx = Ctx->getParent();
683 };
684
685 std::string QualName;
686 llvm::raw_string_ostream OS(QualName);
687
688 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
689 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000690 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000691 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000692 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
693 std::string TemplateArgsStr
694 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000695 TemplateArgs.data(),
696 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000697 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000698 OS << Spec->getName() << TemplateArgsStr;
699 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000700 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000701 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000702 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000703 OS << ND;
704 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
705 if (!RD->getIdentifier())
706 OS << "<anonymous " << RD->getKindName() << '>';
707 else
708 OS << RD;
709 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000710 const FunctionProtoType *FT = 0;
711 if (FD->hasWrittenPrototype())
712 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
713
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000714 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000715 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000716 unsigned NumParams = FD->getNumParams();
717 for (unsigned i = 0; i < NumParams; ++i) {
718 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000719 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000720 std::string Param;
721 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000722 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000723 }
724
725 if (FT->isVariadic()) {
726 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000727 OS << ", ";
728 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000729 }
730 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000731 OS << ')';
732 } else {
733 OS << cast<NamedDecl>(*I);
734 }
735 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000736 }
737
John McCall8472af42010-03-16 21:48:18 +0000738 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000739 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000740 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000741 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000742
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000743 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000744}
745
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000746bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000747 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
748
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000749 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
750 // We want to keep it, unless it nominates same namespace.
751 if (getKind() == Decl::UsingDirective) {
752 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
753 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000756 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
757 // For function declarations, we keep track of redeclarations.
758 return FD->getPreviousDeclaration() == OldD;
759
Douglas Gregore53060f2009-06-25 22:08:12 +0000760 // For function templates, the underlying function declarations are linked.
761 if (const FunctionTemplateDecl *FunctionTemplate
762 = dyn_cast<FunctionTemplateDecl>(this))
763 if (const FunctionTemplateDecl *OldFunctionTemplate
764 = dyn_cast<FunctionTemplateDecl>(OldD))
765 return FunctionTemplate->getTemplatedDecl()
766 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Steve Naroff0de21fd2009-02-22 19:35:57 +0000768 // For method declarations, we keep track of redeclarations.
769 if (isa<ObjCMethodDecl>(this))
770 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000771
John McCallf36e02d2009-10-09 21:13:30 +0000772 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
773 return true;
774
John McCall9488ea12009-11-17 05:59:44 +0000775 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
776 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
777 cast<UsingShadowDecl>(OldD)->getTargetDecl();
778
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000779 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
780 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
781 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
782
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000783 // For non-function declarations, if the declarations are of the
784 // same kind then this must be a redeclaration, or semantic analysis
785 // would not have given us the new declaration.
786 return this->getKind() == OldD->getKind();
787}
788
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000789bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000790 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000791}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000792
Anders Carlssone136e0e2009-06-26 06:29:23 +0000793NamedDecl *NamedDecl::getUnderlyingDecl() {
794 NamedDecl *ND = this;
795 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000796 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000797 ND = UD->getTargetDecl();
798 else if (ObjCCompatibleAliasDecl *AD
799 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
800 return AD->getClassInterface();
801 else
802 return ND;
803 }
804}
805
John McCall161755a2010-04-06 21:38:20 +0000806bool NamedDecl::isCXXInstanceMember() const {
807 assert(isCXXClassMember() &&
808 "checking whether non-member is instance member");
809
810 const NamedDecl *D = this;
811 if (isa<UsingShadowDecl>(D))
812 D = cast<UsingShadowDecl>(D)->getTargetDecl();
813
Francois Pichet87c2e122010-11-21 06:08:52 +0000814 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000815 return true;
816 if (isa<CXXMethodDecl>(D))
817 return cast<CXXMethodDecl>(D)->isInstance();
818 if (isa<FunctionTemplateDecl>(D))
819 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
820 ->getTemplatedDecl())->isInstance();
821 return false;
822}
823
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000824//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000825// DeclaratorDecl Implementation
826//===----------------------------------------------------------------------===//
827
Douglas Gregor1693e152010-07-06 18:42:40 +0000828template <typename DeclT>
829static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
830 if (decl->getNumTemplateParameterLists() > 0)
831 return decl->getTemplateParameterList(0)->getTemplateLoc();
832 else
833 return decl->getInnerLocStart();
834}
835
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000836SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000837 TypeSourceInfo *TSI = getTypeSourceInfo();
838 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000839 return SourceLocation();
840}
841
John McCallb6217662010-03-15 10:12:16 +0000842void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
843 SourceRange QualifierRange) {
844 if (Qualifier) {
845 // Make sure the extended decl info is allocated.
846 if (!hasExtInfo()) {
847 // Save (non-extended) type source info pointer.
848 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
849 // Allocate external info struct.
850 DeclInfo = new (getASTContext()) ExtInfo;
851 // Restore savedTInfo into (extended) decl info.
852 getExtInfo()->TInfo = savedTInfo;
853 }
854 // Set qualifier info.
855 getExtInfo()->NNS = Qualifier;
856 getExtInfo()->NNSRange = QualifierRange;
857 }
858 else {
859 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
860 assert(QualifierRange.isInvalid());
861 if (hasExtInfo()) {
862 // Save type source info pointer.
863 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
864 // Deallocate the extended decl info.
865 getASTContext().Deallocate(getExtInfo());
866 // Restore savedTInfo into (non-extended) decl info.
867 DeclInfo = savedTInfo;
868 }
869 }
870}
871
Douglas Gregor1693e152010-07-06 18:42:40 +0000872SourceLocation DeclaratorDecl::getOuterLocStart() const {
873 return getTemplateOrInnerLocStart(this);
874}
875
Abramo Bagnara9b934882010-06-12 08:15:14 +0000876void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000877QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
878 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000879 TemplateParameterList **TPLists) {
880 assert((NumTPLists == 0 || TPLists != 0) &&
881 "Empty array of template parameters with positive size!");
882 assert((NumTPLists == 0 || NNS) &&
883 "Nonempty array of template parameters with no qualifier!");
884
885 // Free previous template parameters (if any).
886 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000887 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000888 TemplParamLists = 0;
889 NumTemplParamLists = 0;
890 }
891 // Set info on matched template parameter lists (if any).
892 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000893 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000894 NumTemplParamLists = NumTPLists;
895 for (unsigned i = NumTPLists; i-- > 0; )
896 TemplParamLists[i] = TPLists[i];
897 }
898}
899
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000900//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000901// VarDecl Implementation
902//===----------------------------------------------------------------------===//
903
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000904const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
905 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000906 case SC_None: break;
907 case SC_Auto: return "auto"; break;
908 case SC_Extern: return "extern"; break;
909 case SC_PrivateExtern: return "__private_extern__"; break;
910 case SC_Register: return "register"; break;
911 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000912 }
913
914 assert(0 && "Invalid storage class");
915 return 0;
916}
917
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000918VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000919 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000920 StorageClass S, StorageClass SCAsWritten) {
921 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000922}
923
Douglas Gregor381d34e2010-12-06 18:36:25 +0000924void VarDecl::setStorageClass(StorageClass SC) {
925 assert(isLegalForVariable(SC));
926 if (getStorageClass() != SC)
927 ClearLinkageCache();
928
929 SClass = SC;
930}
931
Douglas Gregor1693e152010-07-06 18:42:40 +0000932SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000933 SourceLocation Start = getTypeSpecStartLoc();
934 if (Start.isInvalid())
935 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000936 return Start;
937}
938
939SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000940 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000941 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
942 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000943}
944
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000945bool VarDecl::isExternC() const {
946 ASTContext &Context = getASTContext();
947 if (!Context.getLangOptions().CPlusPlus)
948 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +0000949 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000950 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
951
952 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
953 DC = DC->getParent()) {
954 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
955 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000956 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000957
958 break;
959 }
960
961 if (DC->isFunctionOrMethod())
962 return false;
963 }
964
965 return false;
966}
967
968VarDecl *VarDecl::getCanonicalDecl() {
969 return getFirstDeclaration();
970}
971
Sebastian Redle9d12b62010-01-31 22:27:38 +0000972VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
973 // C++ [basic.def]p2:
974 // A declaration is a definition unless [...] it contains the 'extern'
975 // specifier or a linkage-specification and neither an initializer [...],
976 // it declares a static data member in a class declaration [...].
977 // C++ [temp.expl.spec]p15:
978 // An explicit specialization of a static data member of a template is a
979 // definition if the declaration includes an initializer; otherwise, it is
980 // a declaration.
981 if (isStaticDataMember()) {
982 if (isOutOfLine() && (hasInit() ||
983 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
984 return Definition;
985 else
986 return DeclarationOnly;
987 }
988 // C99 6.7p5:
989 // A definition of an identifier is a declaration for that identifier that
990 // [...] causes storage to be reserved for that object.
991 // Note: that applies for all non-file-scope objects.
992 // C99 6.9.2p1:
993 // If the declaration of an identifier for an object has file scope and an
994 // initializer, the declaration is an external definition for the identifier
995 if (hasInit())
996 return Definition;
997 // AST for 'extern "C" int foo;' is annotated with 'extern'.
998 if (hasExternalStorage())
999 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001000
John McCalld931b082010-08-26 03:08:43 +00001001 if (getStorageClassAsWritten() == SC_Extern ||
1002 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001003 for (const VarDecl *PrevVar = getPreviousDeclaration();
1004 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1005 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1006 return DeclarationOnly;
1007 }
1008 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001009 // C99 6.9.2p2:
1010 // A declaration of an object that has file scope without an initializer,
1011 // and without a storage class specifier or the scs 'static', constitutes
1012 // a tentative definition.
1013 // No such thing in C++.
1014 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1015 return TentativeDefinition;
1016
1017 // What's left is (in C, block-scope) declarations without initializers or
1018 // external storage. These are definitions.
1019 return Definition;
1020}
1021
Sebastian Redle9d12b62010-01-31 22:27:38 +00001022VarDecl *VarDecl::getActingDefinition() {
1023 DefinitionKind Kind = isThisDeclarationADefinition();
1024 if (Kind != TentativeDefinition)
1025 return 0;
1026
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001027 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001028 VarDecl *First = getFirstDeclaration();
1029 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1030 I != E; ++I) {
1031 Kind = (*I)->isThisDeclarationADefinition();
1032 if (Kind == Definition)
1033 return 0;
1034 else if (Kind == TentativeDefinition)
1035 LastTentative = *I;
1036 }
1037 return LastTentative;
1038}
1039
1040bool VarDecl::isTentativeDefinitionNow() const {
1041 DefinitionKind Kind = isThisDeclarationADefinition();
1042 if (Kind != TentativeDefinition)
1043 return false;
1044
1045 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1046 if ((*I)->isThisDeclarationADefinition() == Definition)
1047 return false;
1048 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001049 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001050}
1051
Sebastian Redl31310a22010-02-01 20:16:42 +00001052VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001053 VarDecl *First = getFirstDeclaration();
1054 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1055 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001056 if ((*I)->isThisDeclarationADefinition() == Definition)
1057 return *I;
1058 }
1059 return 0;
1060}
1061
John McCall110e8e52010-10-29 22:22:43 +00001062VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1063 DefinitionKind Kind = DeclarationOnly;
1064
1065 const VarDecl *First = getFirstDeclaration();
1066 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1067 I != E; ++I)
1068 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1069
1070 return Kind;
1071}
1072
Sebastian Redl31310a22010-02-01 20:16:42 +00001073const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001074 redecl_iterator I = redecls_begin(), E = redecls_end();
1075 while (I != E && !I->getInit())
1076 ++I;
1077
1078 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001079 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001080 return I->getInit();
1081 }
1082 return 0;
1083}
1084
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001085bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001086 if (Decl::isOutOfLine())
1087 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001088
1089 if (!isStaticDataMember())
1090 return false;
1091
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001092 // If this static data member was instantiated from a static data member of
1093 // a class template, check whether that static data member was defined
1094 // out-of-line.
1095 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1096 return VD->isOutOfLine();
1097
1098 return false;
1099}
1100
Douglas Gregor0d035142009-10-27 18:42:08 +00001101VarDecl *VarDecl::getOutOfLineDefinition() {
1102 if (!isStaticDataMember())
1103 return 0;
1104
1105 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1106 RD != RDEnd; ++RD) {
1107 if (RD->getLexicalDeclContext()->isFileContext())
1108 return *RD;
1109 }
1110
1111 return 0;
1112}
1113
Douglas Gregor838db382010-02-11 01:19:42 +00001114void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001115 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1116 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001117 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001118 }
1119
1120 Init = I;
1121}
1122
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001123VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001124 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001125 return cast<VarDecl>(MSI->getInstantiatedFrom());
1126
1127 return 0;
1128}
1129
Douglas Gregor663b5a02009-10-14 20:14:33 +00001130TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001131 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001132 return MSI->getTemplateSpecializationKind();
1133
1134 return TSK_Undeclared;
1135}
1136
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001137MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001138 return getASTContext().getInstantiatedFromStaticDataMember(this);
1139}
1140
Douglas Gregor0a897e32009-10-15 17:21:20 +00001141void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1142 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001143 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001144 assert(MSI && "Not an instantiated static data member?");
1145 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001146 if (TSK != TSK_ExplicitSpecialization &&
1147 PointOfInstantiation.isValid() &&
1148 MSI->getPointOfInstantiation().isInvalid())
1149 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001150}
1151
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001152//===----------------------------------------------------------------------===//
1153// ParmVarDecl Implementation
1154//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001155
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001156ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1157 SourceLocation L, IdentifierInfo *Id,
1158 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001159 StorageClass S, StorageClass SCAsWritten,
1160 Expr *DefArg) {
1161 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1162 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001163}
1164
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001165Expr *ParmVarDecl::getDefaultArg() {
1166 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1167 assert(!hasUninstantiatedDefaultArg() &&
1168 "Default argument is not yet instantiated!");
1169
1170 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001171 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001172 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001173
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001174 return Arg;
1175}
1176
1177unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001178 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001179 return E->getNumTemporaries();
1180
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001181 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001182}
1183
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001184CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1185 assert(getNumDefaultArgTemporaries() &&
1186 "Default arguments does not have any temporaries!");
1187
John McCall4765fa02010-12-06 08:20:24 +00001188 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001189 return E->getTemporary(i);
1190}
1191
1192SourceRange ParmVarDecl::getDefaultArgRange() const {
1193 if (const Expr *E = getInit())
1194 return E->getSourceRange();
1195
1196 if (hasUninstantiatedDefaultArg())
1197 return getUninstantiatedDefaultArg()->getSourceRange();
1198
1199 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001200}
1201
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001202//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001203// FunctionDecl Implementation
1204//===----------------------------------------------------------------------===//
1205
John McCall136a6982009-09-11 06:45:03 +00001206void FunctionDecl::getNameForDiagnostic(std::string &S,
1207 const PrintingPolicy &Policy,
1208 bool Qualified) const {
1209 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1210 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1211 if (TemplateArgs)
1212 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +00001213 TemplateArgs->data(),
1214 TemplateArgs->size(),
John McCall136a6982009-09-11 06:45:03 +00001215 Policy);
1216
1217}
Ted Kremenek27f8a282008-05-20 00:43:19 +00001218
Ted Kremenek9498d382010-04-29 16:49:01 +00001219bool FunctionDecl::isVariadic() const {
1220 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1221 return FT->isVariadic();
1222 return false;
1223}
1224
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001225bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1226 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1227 if (I->Body) {
1228 Definition = *I;
1229 return true;
1230 }
1231 }
1232
1233 return false;
1234}
1235
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001236Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001237 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1238 if (I->Body) {
1239 Definition = *I;
1240 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001241 }
1242 }
1243
1244 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001245}
1246
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001247void FunctionDecl::setBody(Stmt *B) {
1248 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001249 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001250 EndRangeLoc = B->getLocEnd();
1251}
1252
Douglas Gregor21386642010-09-28 21:55:22 +00001253void FunctionDecl::setPure(bool P) {
1254 IsPure = P;
1255 if (P)
1256 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1257 Parent->markedVirtualFunctionPure();
1258}
1259
Douglas Gregor48a83b52009-09-12 00:17:51 +00001260bool FunctionDecl::isMain() const {
1261 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001262 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001263 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001264 getIdentifier() && getIdentifier()->isStr("main");
1265}
1266
Douglas Gregor48a83b52009-09-12 00:17:51 +00001267bool FunctionDecl::isExternC() const {
1268 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001269 // In C, any non-static, non-overloadable function has external
1270 // linkage.
1271 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001272 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001273
Mike Stump1eb44332009-09-09 15:08:12 +00001274 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001275 DC = DC->getParent()) {
1276 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1277 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001278 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001279 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001280
1281 break;
1282 }
Douglas Gregor45975532010-08-17 16:09:23 +00001283
1284 if (DC->isRecord())
1285 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001286 }
1287
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001288 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001289}
1290
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001291bool FunctionDecl::isGlobal() const {
1292 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1293 return Method->isStatic();
1294
John McCalld931b082010-08-26 03:08:43 +00001295 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001296 return false;
1297
Mike Stump1eb44332009-09-09 15:08:12 +00001298 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001299 DC->isNamespace();
1300 DC = DC->getParent()) {
1301 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1302 if (!Namespace->getDeclName())
1303 return false;
1304 break;
1305 }
1306 }
1307
1308 return true;
1309}
1310
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001311void
1312FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1313 redeclarable_base::setPreviousDeclaration(PrevDecl);
1314
1315 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1316 FunctionTemplateDecl *PrevFunTmpl
1317 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1318 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1319 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1320 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001321
1322 if (PrevDecl->IsInline)
1323 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001324}
1325
1326const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1327 return getFirstDeclaration();
1328}
1329
1330FunctionDecl *FunctionDecl::getCanonicalDecl() {
1331 return getFirstDeclaration();
1332}
1333
Douglas Gregor381d34e2010-12-06 18:36:25 +00001334void FunctionDecl::setStorageClass(StorageClass SC) {
1335 assert(isLegalForFunction(SC));
1336 if (getStorageClass() != SC)
1337 ClearLinkageCache();
1338
1339 SClass = SC;
1340}
1341
Douglas Gregor3e41d602009-02-13 23:20:09 +00001342/// \brief Returns a value indicating whether this function
1343/// corresponds to a builtin function.
1344///
1345/// The function corresponds to a built-in function if it is
1346/// declared at translation scope or within an extern "C" block and
1347/// its name matches with the name of a builtin. The returned value
1348/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001349/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001350/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001351unsigned FunctionDecl::getBuiltinID() const {
1352 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001353 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1354 return 0;
1355
1356 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1357 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1358 return BuiltinID;
1359
1360 // This function has the name of a known C library
1361 // function. Determine whether it actually refers to the C library
1362 // function or whether it just has the same name.
1363
Douglas Gregor9add3172009-02-17 03:23:10 +00001364 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001365 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001366 return 0;
1367
Douglas Gregor3c385e52009-02-14 18:57:46 +00001368 // If this function is at translation-unit scope and we're not in
1369 // C++, it refers to the C library function.
1370 if (!Context.getLangOptions().CPlusPlus &&
1371 getDeclContext()->isTranslationUnit())
1372 return BuiltinID;
1373
1374 // If the function is in an extern "C" linkage specification and is
1375 // not marked "overloadable", it's the real function.
1376 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001377 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001378 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001379 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001380 return BuiltinID;
1381
1382 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001383 return 0;
1384}
1385
1386
Chris Lattner1ad9b282009-04-25 06:03:53 +00001387/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001388/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001389/// after it has been created.
1390unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001391 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001392 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001393 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001394 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Reid Spencer5f016e22007-07-11 17:01:13 +00001396}
1397
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001398void FunctionDecl::setParams(ASTContext &C,
1399 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001401 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001402
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 // Zero params -> null pointer.
1404 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001405 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001406 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001407 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001408
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001409 // Update source range. The check below allows us to set EndRangeLoc before
1410 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001411 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001412 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 }
1414}
1415
Chris Lattner8123a952008-04-10 02:22:51 +00001416/// getMinRequiredArguments - Returns the minimum number of arguments
1417/// needed to call this function. This may be fewer than the number of
1418/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001419/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001420unsigned FunctionDecl::getMinRequiredArguments() const {
1421 unsigned NumRequiredArgs = getNumParams();
1422 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001423 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001424 --NumRequiredArgs;
1425
1426 return NumRequiredArgs;
1427}
1428
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001429bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001430 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001431 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001432
1433 if (isa<CXXMethodDecl>(this)) {
1434 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1435 return true;
1436 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001437
1438 switch (getTemplateSpecializationKind()) {
1439 case TSK_Undeclared:
1440 case TSK_ExplicitSpecialization:
1441 return false;
1442
1443 case TSK_ImplicitInstantiation:
1444 case TSK_ExplicitInstantiationDeclaration:
1445 case TSK_ExplicitInstantiationDefinition:
1446 // Handle below.
1447 break;
1448 }
1449
1450 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001451 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001452 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001453 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001454
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001455 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001456 return PatternDecl->isInlined();
1457
1458 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001459}
1460
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001461/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001462/// definition will be externally visible.
1463///
1464/// Inline function definitions are always available for inlining optimizations.
1465/// However, depending on the language dialect, declaration specifiers, and
1466/// attributes, the definition of an inline function may or may not be
1467/// "externally" visible to other translation units in the program.
1468///
1469/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001470/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001471/// inline definition becomes externally visible (C99 6.7.4p6).
1472///
1473/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1474/// definition, we use the GNU semantics for inline, which are nearly the
1475/// opposite of C99 semantics. In particular, "inline" by itself will create
1476/// an externally visible symbol, but "extern inline" will not create an
1477/// externally visible symbol.
1478bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1479 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001480 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001481 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001482
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001483 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001484 // If it's not the case that both 'inline' and 'extern' are
1485 // specified on the definition, then this inline definition is
1486 // externally visible.
1487 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1488 return true;
1489
1490 // If any declaration is 'inline' but not 'extern', then this definition
1491 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001492 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1493 Redecl != RedeclEnd;
1494 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001495 if (Redecl->isInlineSpecified() &&
1496 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001497 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001498 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001499
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001500 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001501 }
1502
1503 // C99 6.7.4p6:
1504 // [...] If all of the file scope declarations for a function in a
1505 // translation unit include the inline function specifier without extern,
1506 // then the definition in that translation unit is an inline definition.
1507 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1508 Redecl != RedeclEnd;
1509 ++Redecl) {
1510 // Only consider file-scope declarations in this test.
1511 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1512 continue;
1513
John McCalld931b082010-08-26 03:08:43 +00001514 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001515 return true; // Not an inline definition
1516 }
1517
1518 // C99 6.7.4p6:
1519 // An inline definition does not provide an external definition for the
1520 // function, and does not forbid an external definition in another
1521 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001522 return false;
1523}
1524
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001525/// getOverloadedOperator - Which C++ overloaded operator this
1526/// function represents, if any.
1527OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001528 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1529 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001530 else
1531 return OO_None;
1532}
1533
Sean Hunta6c058d2010-01-13 09:01:02 +00001534/// getLiteralIdentifier - The literal suffix identifier this function
1535/// represents, if any.
1536const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1537 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1538 return getDeclName().getCXXLiteralIdentifier();
1539 else
1540 return 0;
1541}
1542
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001543FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1544 if (TemplateOrSpecialization.isNull())
1545 return TK_NonTemplate;
1546 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1547 return TK_FunctionTemplate;
1548 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1549 return TK_MemberSpecialization;
1550 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1551 return TK_FunctionTemplateSpecialization;
1552 if (TemplateOrSpecialization.is
1553 <DependentFunctionTemplateSpecializationInfo*>())
1554 return TK_DependentFunctionTemplateSpecialization;
1555
1556 assert(false && "Did we miss a TemplateOrSpecialization type?");
1557 return TK_NonTemplate;
1558}
1559
Douglas Gregor2db32322009-10-07 23:56:10 +00001560FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001561 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001562 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1563
1564 return 0;
1565}
1566
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001567MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1568 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1569}
1570
Douglas Gregor2db32322009-10-07 23:56:10 +00001571void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001572FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1573 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001574 TemplateSpecializationKind TSK) {
1575 assert(TemplateOrSpecialization.isNull() &&
1576 "Member function is already a specialization");
1577 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001578 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001579 TemplateOrSpecialization = Info;
1580}
1581
Douglas Gregor3b846b62009-10-27 20:53:28 +00001582bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001583 // If the function is invalid, it can't be implicitly instantiated.
1584 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001585 return false;
1586
1587 switch (getTemplateSpecializationKind()) {
1588 case TSK_Undeclared:
1589 case TSK_ExplicitSpecialization:
1590 case TSK_ExplicitInstantiationDefinition:
1591 return false;
1592
1593 case TSK_ImplicitInstantiation:
1594 return true;
1595
1596 case TSK_ExplicitInstantiationDeclaration:
1597 // Handled below.
1598 break;
1599 }
1600
1601 // Find the actual template from which we will instantiate.
1602 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001603 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001604 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001605 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001606
1607 // C++0x [temp.explicit]p9:
1608 // Except for inline functions, other explicit instantiation declarations
1609 // have the effect of suppressing the implicit instantiation of the entity
1610 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001611 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001612 return true;
1613
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001614 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001615}
1616
1617FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1618 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1619 while (Primary->getInstantiatedFromMemberTemplate()) {
1620 // If we have hit a point where the user provided a specialization of
1621 // this template, we're done looking.
1622 if (Primary->isMemberSpecialization())
1623 break;
1624
1625 Primary = Primary->getInstantiatedFromMemberTemplate();
1626 }
1627
1628 return Primary->getTemplatedDecl();
1629 }
1630
1631 return getInstantiatedFromMemberFunction();
1632}
1633
Douglas Gregor16e8be22009-06-29 17:30:29 +00001634FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001635 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001636 = TemplateOrSpecialization
1637 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001638 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001639 }
1640 return 0;
1641}
1642
1643const TemplateArgumentList *
1644FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001645 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001646 = TemplateOrSpecialization
1647 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001648 return Info->TemplateArguments;
1649 }
1650 return 0;
1651}
1652
Abramo Bagnarae03db982010-05-20 15:32:11 +00001653const TemplateArgumentListInfo *
1654FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1655 if (FunctionTemplateSpecializationInfo *Info
1656 = TemplateOrSpecialization
1657 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1658 return Info->TemplateArgumentsAsWritten;
1659 }
1660 return 0;
1661}
1662
Mike Stump1eb44332009-09-09 15:08:12 +00001663void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001664FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1665 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001666 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001667 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001668 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001669 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1670 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001671 assert(TSK != TSK_Undeclared &&
1672 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001673 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001674 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001675 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001676 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1677 TemplateArgs,
1678 TemplateArgsAsWritten,
1679 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001680 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Douglas Gregor127102b2009-06-29 20:59:39 +00001682 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001683 // function template specializations.
1684 if (InsertPos)
1685 Template->getSpecializations().InsertNode(Info, InsertPos);
1686 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001687 // Try to insert the new node. If there is an existing node, leave it, the
1688 // set will contain the canonical decls while
1689 // FunctionTemplateDecl::findSpecialization will return
1690 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001691 FunctionTemplateSpecializationInfo *Existing
1692 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001693 (void)Existing;
1694 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1695 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001696 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001697}
1698
John McCallaf2094e2010-04-08 09:05:18 +00001699void
1700FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1701 const UnresolvedSetImpl &Templates,
1702 const TemplateArgumentListInfo &TemplateArgs) {
1703 assert(TemplateOrSpecialization.isNull());
1704 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1705 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001706 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001707 void *Buffer = Context.Allocate(Size);
1708 DependentFunctionTemplateSpecializationInfo *Info =
1709 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1710 TemplateArgs);
1711 TemplateOrSpecialization = Info;
1712}
1713
1714DependentFunctionTemplateSpecializationInfo::
1715DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1716 const TemplateArgumentListInfo &TArgs)
1717 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1718
1719 d.NumTemplates = Ts.size();
1720 d.NumArgs = TArgs.size();
1721
1722 FunctionTemplateDecl **TsArray =
1723 const_cast<FunctionTemplateDecl**>(getTemplates());
1724 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1725 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1726
1727 TemplateArgumentLoc *ArgsArray =
1728 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1729 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1730 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1731}
1732
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001733TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001734 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001735 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001736 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001737 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001738 if (FTSInfo)
1739 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Douglas Gregor2db32322009-10-07 23:56:10 +00001741 MemberSpecializationInfo *MSInfo
1742 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1743 if (MSInfo)
1744 return MSInfo->getTemplateSpecializationKind();
1745
1746 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001747}
1748
Mike Stump1eb44332009-09-09 15:08:12 +00001749void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001750FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1751 SourceLocation PointOfInstantiation) {
1752 if (FunctionTemplateSpecializationInfo *FTSInfo
1753 = TemplateOrSpecialization.dyn_cast<
1754 FunctionTemplateSpecializationInfo*>()) {
1755 FTSInfo->setTemplateSpecializationKind(TSK);
1756 if (TSK != TSK_ExplicitSpecialization &&
1757 PointOfInstantiation.isValid() &&
1758 FTSInfo->getPointOfInstantiation().isInvalid())
1759 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1760 } else if (MemberSpecializationInfo *MSInfo
1761 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1762 MSInfo->setTemplateSpecializationKind(TSK);
1763 if (TSK != TSK_ExplicitSpecialization &&
1764 PointOfInstantiation.isValid() &&
1765 MSInfo->getPointOfInstantiation().isInvalid())
1766 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1767 } else
1768 assert(false && "Function cannot have a template specialization kind");
1769}
1770
1771SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001772 if (FunctionTemplateSpecializationInfo *FTSInfo
1773 = TemplateOrSpecialization.dyn_cast<
1774 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001775 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001776 else if (MemberSpecializationInfo *MSInfo
1777 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001778 return MSInfo->getPointOfInstantiation();
1779
1780 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001781}
1782
Douglas Gregor9f185072009-09-11 20:15:17 +00001783bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001784 if (Decl::isOutOfLine())
1785 return true;
1786
1787 // If this function was instantiated from a member function of a
1788 // class template, check whether that member function was defined out-of-line.
1789 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1790 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001791 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001792 return Definition->isOutOfLine();
1793 }
1794
1795 // If this function was instantiated from a function template,
1796 // check whether that function template was defined out-of-line.
1797 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1798 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001799 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001800 return Definition->isOutOfLine();
1801 }
1802
1803 return false;
1804}
1805
Chris Lattner8a934232008-03-31 00:36:02 +00001806//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001807// FieldDecl Implementation
1808//===----------------------------------------------------------------------===//
1809
1810FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1811 IdentifierInfo *Id, QualType T,
1812 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1813 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1814}
1815
1816bool FieldDecl::isAnonymousStructOrUnion() const {
1817 if (!isImplicit() || getDeclName())
1818 return false;
1819
1820 if (const RecordType *Record = getType()->getAs<RecordType>())
1821 return Record->getDecl()->isAnonymousStructOrUnion();
1822
1823 return false;
1824}
1825
1826//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001827// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001828//===----------------------------------------------------------------------===//
1829
Douglas Gregor1693e152010-07-06 18:42:40 +00001830SourceLocation TagDecl::getOuterLocStart() const {
1831 return getTemplateOrInnerLocStart(this);
1832}
1833
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001834SourceRange TagDecl::getSourceRange() const {
1835 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001836 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001837}
1838
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001839TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001840 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001841}
1842
Douglas Gregor60e70642010-05-19 18:39:18 +00001843void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1844 TypedefDeclOrQualifier = TDD;
1845 if (TypeForDecl)
1846 TypeForDecl->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00001847 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00001848}
1849
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001850void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001851 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001852
1853 if (isa<CXXRecordDecl>(this)) {
1854 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1855 struct CXXRecordDecl::DefinitionData *Data =
1856 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001857 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1858 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001859 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001860}
1861
1862void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001863 assert((!isa<CXXRecordDecl>(this) ||
1864 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1865 "definition completed but not started");
1866
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001867 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001868 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00001869
1870 if (ASTMutationListener *L = getASTMutationListener())
1871 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001872}
1873
Douglas Gregor952b0172010-02-11 01:04:33 +00001874TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001875 if (isDefinition())
1876 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00001877 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1878 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001879
1880 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001881 R != REnd; ++R)
1882 if (R->isDefinition())
1883 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001885 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001886}
1887
John McCallb6217662010-03-15 10:12:16 +00001888void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1889 SourceRange QualifierRange) {
1890 if (Qualifier) {
1891 // Make sure the extended qualifier info is allocated.
1892 if (!hasExtInfo())
1893 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1894 // Set qualifier info.
1895 getExtInfo()->NNS = Qualifier;
1896 getExtInfo()->NNSRange = QualifierRange;
1897 }
1898 else {
1899 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1900 assert(QualifierRange.isInvalid());
1901 if (hasExtInfo()) {
1902 getASTContext().Deallocate(getExtInfo());
1903 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1904 }
1905 }
1906}
1907
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001908//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001909// EnumDecl Implementation
1910//===----------------------------------------------------------------------===//
1911
1912EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1913 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001914 EnumDecl *PrevDecl, bool IsScoped,
1915 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001916 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001917 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001918 C.getTypeDeclType(Enum, PrevDecl);
1919 return Enum;
1920}
1921
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001922EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001923 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00001924 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001925}
1926
Douglas Gregor838db382010-02-11 01:19:42 +00001927void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001928 QualType NewPromotionType,
1929 unsigned NumPositiveBits,
1930 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001931 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001932 if (!IntegerType)
1933 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001934 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001935 setNumPositiveBits(NumPositiveBits);
1936 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001937 TagDecl::completeDefinition();
1938}
1939
1940//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001941// RecordDecl Implementation
1942//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001943
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001944RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001945 IdentifierInfo *Id, RecordDecl *PrevDecl,
1946 SourceLocation TKL)
1947 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001948 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001949 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001950 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001951 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001952 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001953}
1954
1955RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001956 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001957 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001958
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001959 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001960 C.getTypeDeclType(R, PrevDecl);
1961 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001962}
1963
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001964RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1965 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1966 SourceLocation());
1967}
1968
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001969bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001970 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001971 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1972}
1973
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001974RecordDecl::field_iterator RecordDecl::field_begin() const {
1975 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1976 LoadFieldsFromExternalStorage();
1977
1978 return field_iterator(decl_iterator(FirstDecl));
1979}
1980
Douglas Gregor44b43212008-12-11 16:49:14 +00001981/// completeDefinition - Notes that the definition of this type is now
1982/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001983void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001985 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001986}
1987
John McCallbc365c52010-05-21 01:17:40 +00001988ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1989 // Force the decl chain to come into existence properly.
1990 if (!getNextDeclInContext()) getParent()->decls_begin();
1991
1992 assert(isAnonymousStructOrUnion());
1993 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1994 assert(D->getType()->isRecordType());
1995 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1996 return D;
1997}
1998
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001999void RecordDecl::LoadFieldsFromExternalStorage() const {
2000 ExternalASTSource *Source = getASTContext().getExternalSource();
2001 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2002
2003 // Notify that we have a RecordDecl doing some initialization.
2004 ExternalASTSource::Deserializing TheFields(Source);
2005
2006 llvm::SmallVector<Decl*, 64> Decls;
2007 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2008 return;
2009
2010#ifndef NDEBUG
2011 // Check that all decls we got were FieldDecls.
2012 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2013 assert(isa<FieldDecl>(Decls[i]));
2014#endif
2015
2016 LoadedFieldsFromExternalStorage = true;
2017
2018 if (Decls.empty())
2019 return;
2020
2021 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2022}
2023
Steve Naroff56ee6892008-10-08 17:01:13 +00002024//===----------------------------------------------------------------------===//
2025// BlockDecl Implementation
2026//===----------------------------------------------------------------------===//
2027
Douglas Gregor838db382010-02-11 01:19:42 +00002028void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002029 unsigned NParms) {
2030 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Steve Naroffe78b8092009-03-13 16:56:44 +00002032 // Zero params -> null pointer.
2033 if (NParms) {
2034 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002035 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002036 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2037 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2038 }
2039}
2040
2041unsigned BlockDecl::getNumParams() const {
2042 return NumParams;
2043}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002044
2045
2046//===----------------------------------------------------------------------===//
2047// Other Decl Allocation/Deallocation Method Implementations
2048//===----------------------------------------------------------------------===//
2049
2050TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2051 return new (C) TranslationUnitDecl(C);
2052}
2053
2054NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2055 SourceLocation L, IdentifierInfo *Id) {
2056 return new (C) NamespaceDecl(DC, L, Id);
2057}
2058
Douglas Gregor06c91932010-10-27 19:49:05 +00002059NamespaceDecl *NamespaceDecl::getNextNamespace() {
2060 return dyn_cast_or_null<NamespaceDecl>(
2061 NextNamespace.get(getASTContext().getExternalSource()));
2062}
2063
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002064ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2065 SourceLocation L, IdentifierInfo *Id, QualType T) {
2066 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2067}
2068
2069FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00002070 const DeclarationNameInfo &NameInfo,
2071 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002072 StorageClass S, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002073 bool isInlineSpecified,
2074 bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002075 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor8f150942010-12-09 16:59:22 +00002076 S, SCAsWritten, isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002077 New->HasWrittenPrototype = hasWrittenPrototype;
2078 return New;
2079}
2080
2081BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2082 return new (C) BlockDecl(DC, L);
2083}
2084
2085EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2086 SourceLocation L,
2087 IdentifierInfo *Id, QualType T,
2088 Expr *E, const llvm::APSInt &V) {
2089 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2090}
2091
Benjamin Kramerd9811462010-11-21 14:11:41 +00002092IndirectFieldDecl *
2093IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2094 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2095 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002096 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2097}
2098
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002099SourceRange EnumConstantDecl::getSourceRange() const {
2100 SourceLocation End = getLocation();
2101 if (Init)
2102 End = Init->getLocEnd();
2103 return SourceRange(getLocation(), End);
2104}
2105
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002106TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2107 SourceLocation L, IdentifierInfo *Id,
2108 TypeSourceInfo *TInfo) {
2109 return new (C) TypedefDecl(DC, L, Id, TInfo);
2110}
2111
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002112FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2113 SourceLocation L,
2114 StringLiteral *Str) {
2115 return new (C) FileScopeAsmDecl(DC, L, Str);
2116}