blob: c6c7649bdac1d5f57341b5c20224beba0ba29ff6 [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
John McCall36987482010-11-02 01:45:15 +000078/// Flags controlling the computation of linkage and visibility.
79struct LVFlags {
80 bool ConsiderGlobalVisibility;
81 bool ConsiderVisibilityAttributes;
82
83 LVFlags() : ConsiderGlobalVisibility(true),
84 ConsiderVisibilityAttributes(true) {
85 }
86
87 /// Returns a set of flags, otherwise based on these, which ignores
88 /// off all sources of visibility except template arguments.
89 LVFlags onlyTemplateVisibility() const {
90 LVFlags F = *this;
91 F.ConsiderGlobalVisibility = false;
92 F.ConsiderVisibilityAttributes = false;
93 return F;
94 }
95};
96
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000097/// \brief Get the most restrictive linkage for the types in the given
98/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +000099static LVPair
100getLVForTemplateParameterList(const TemplateParameterList *Params) {
101 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000102 for (TemplateParameterList::const_iterator P = Params->begin(),
103 PEnd = Params->end();
104 P != PEnd; ++P) {
105 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
106 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000107 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000108 continue;
109 }
110
111 if (TemplateTemplateParmDecl *TTP
112 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000113 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000114 }
115 }
116
John McCall1fb0caa2010-10-22 21:05:15 +0000117 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000118}
119
120/// \brief Get the most restrictive linkage for the types and
121/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +0000122static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
123 unsigned NumArgs) {
124 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000125
126 for (unsigned I = 0; I != NumArgs; ++I) {
127 switch (Args[I].getKind()) {
128 case TemplateArgument::Null:
129 case TemplateArgument::Integral:
130 case TemplateArgument::Expression:
131 break;
132
133 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +0000134 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000135 break;
136
137 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000138 // The decl can validly be null as the representation of nullptr
139 // arguments, valid only in C++0x.
140 if (Decl *D = Args[I].getAsDecl()) {
141 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
142 LV = merge(LV, ND->getLinkageAndVisibility());
143 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
John McCallaf146032010-10-30 11:50:40 +0000144 LV = merge(LV, VD->getLinkageAndVisibility());
John McCall1fb0caa2010-10-22 21:05:15 +0000145 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000146 break;
147
148 case TemplateArgument::Template:
John McCall1fb0caa2010-10-22 21:05:15 +0000149 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
150 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000151 break;
152
153 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000154 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
155 Args[I].pack_size()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000156 break;
157 }
158 }
159
John McCall1fb0caa2010-10-22 21:05:15 +0000160 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000161}
162
John McCallaf146032010-10-30 11:50:40 +0000163static LVPair
164getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
John McCall1fb0caa2010-10-22 21:05:15 +0000165 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
166 TArgs.flat_size());
John McCall3cdfc4d2010-08-13 08:35:10 +0000167}
168
John McCall0df95872010-10-29 00:29:13 +0000169/// getLVForDecl - Get the cached linkage and visibility for the given
170/// declaration.
John McCall36987482010-11-02 01:45:15 +0000171static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
John McCall0df95872010-10-29 00:29:13 +0000172
John McCall36987482010-11-02 01:45:15 +0000173static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000174 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000175 "Not a name having namespace scope");
176 ASTContext &Context = D->getASTContext();
177
178 // C++ [basic.link]p3:
179 // A name having namespace scope (3.3.6) has internal linkage if it
180 // is the name of
181 // - an object, reference, function or function template that is
182 // explicitly declared static; or,
183 // (This bullet corresponds to C99 6.2.2p3.)
184 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
185 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000186 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000187 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000188
189 // - an object or reference that is explicitly declared const
190 // and neither explicitly declared extern nor previously
191 // declared to have external linkage; or
192 // (there is no equivalent in C99)
193 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000194 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000195 Var->getStorageClass() != SC_Extern &&
196 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000197 bool FoundExtern = false;
198 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
199 PrevVar && !FoundExtern;
200 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000201 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000202 FoundExtern = true;
203
204 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000205 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000206 }
207 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000208 // C++ [temp]p4:
209 // A non-member function template can have internal linkage; any
210 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000211 const FunctionDecl *Function = 0;
212 if (const FunctionTemplateDecl *FunTmpl
213 = dyn_cast<FunctionTemplateDecl>(D))
214 Function = FunTmpl->getTemplatedDecl();
215 else
216 Function = cast<FunctionDecl>(D);
217
218 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000219 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000220 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000221 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
222 // - a data member of an anonymous union.
223 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000224 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000225 }
226
John McCall1fb0caa2010-10-22 21:05:15 +0000227 if (D->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000228 return LinkageInfo::uniqueExternal();
John McCalle7bc9722010-10-28 04:18:25 +0000229
John McCall1fb0caa2010-10-22 21:05:15 +0000230 // Set up the defaults.
231
232 // C99 6.2.2p5:
233 // If the declaration of an identifier for an object has file
234 // scope and no storage-class specifier, its linkage is
235 // external.
John McCallaf146032010-10-30 11:50:40 +0000236 LinkageInfo LV;
237
John McCall36987482010-11-02 01:45:15 +0000238 if (F.ConsiderVisibilityAttributes) {
239 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
240 LV.setVisibility(GetVisibilityFromAttr(VA), true);
241 F.ConsiderGlobalVisibility = false;
242 }
John McCallaf146032010-10-30 11:50:40 +0000243 }
John McCall1fb0caa2010-10-22 21:05:15 +0000244
Douglas Gregord85b5b92009-11-25 22:24:25 +0000245 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000246
Douglas Gregord85b5b92009-11-25 22:24:25 +0000247 // A name having namespace scope has external linkage if it is the
248 // name of
249 //
250 // - an object or reference, unless it has internal linkage; or
251 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000252 // GCC applies the following optimization to variables and static
253 // data members, but not to functions:
254 //
John McCall1fb0caa2010-10-22 21:05:15 +0000255 // Modify the variable's LV by the LV of its type unless this is
256 // C or extern "C". This follows from [basic.link]p9:
257 // A type without linkage shall not be used as the type of a
258 // variable or function with external linkage unless
259 // - the entity has C language linkage, or
260 // - the entity is declared within an unnamed namespace, or
261 // - the entity is not used or is defined in the same
262 // translation unit.
263 // and [basic.link]p10:
264 // ...the types specified by all declarations referring to a
265 // given variable or function shall be identical...
266 // C does not have an equivalent rule.
267 //
John McCallac65c622010-10-26 04:59:26 +0000268 // Ignore this if we've got an explicit attribute; the user
269 // probably knows what they're doing.
270 //
John McCall1fb0caa2010-10-22 21:05:15 +0000271 // Note that we don't want to make the variable non-external
272 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000273 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000274 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
275 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000276 return LinkageInfo::uniqueExternal();
277 if (!LV.visibilityExplicit())
278 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000279 }
280
John McCall35cebc32010-11-02 18:38:13 +0000281 if (Var->getStorageClass() == SC_PrivateExtern)
282 LV.setVisibility(HiddenVisibility, true);
283
Douglas Gregord85b5b92009-11-25 22:24:25 +0000284 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000285 (Var->getStorageClass() == SC_Extern ||
286 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000287
Douglas Gregord85b5b92009-11-25 22:24:25 +0000288 // C99 6.2.2p4:
289 // For an identifier declared with the storage-class specifier
290 // extern in a scope in which a prior declaration of that
291 // identifier is visible, if the prior declaration specifies
292 // internal or external linkage, the linkage of the identifier
293 // at the later declaration is the same as the linkage
294 // specified at the prior declaration. If no prior declaration
295 // is visible, or if the prior declaration specifies no
296 // linkage, then the identifier has external linkage.
297 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCallaf146032010-10-30 11:50:40 +0000298 LinkageInfo PrevLV = PrevVar->getLinkageAndVisibility();
299 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
300 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000301 }
302 }
303
Douglas Gregord85b5b92009-11-25 22:24:25 +0000304 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000305 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000306 // In theory, we can modify the function's LV by the LV of its
307 // type unless it has C linkage (see comment above about variables
308 // for justification). In practice, GCC doesn't do this, so it's
309 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000310
John McCall35cebc32010-11-02 18:38:13 +0000311 if (Function->getStorageClass() == SC_PrivateExtern)
312 LV.setVisibility(HiddenVisibility, true);
313
Douglas Gregord85b5b92009-11-25 22:24:25 +0000314 // C99 6.2.2p5:
315 // If the declaration of an identifier for a function has no
316 // storage-class specifier, its linkage is determined exactly
317 // as if it were declared with the storage-class specifier
318 // extern.
319 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000320 (Function->getStorageClass() == SC_Extern ||
321 Function->getStorageClass() == SC_PrivateExtern ||
322 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000323 // C99 6.2.2p4:
324 // For an identifier declared with the storage-class specifier
325 // extern in a scope in which a prior declaration of that
326 // identifier is visible, if the prior declaration specifies
327 // internal or external linkage, the linkage of the identifier
328 // at the later declaration is the same as the linkage
329 // specified at the prior declaration. If no prior declaration
330 // is visible, or if the prior declaration specifies no
331 // linkage, then the identifier has external linkage.
332 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCallaf146032010-10-30 11:50:40 +0000333 LinkageInfo PrevLV = PrevFunc->getLinkageAndVisibility();
334 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
335 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000336 }
337 }
338
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000339 if (FunctionTemplateSpecializationInfo *SpecInfo
340 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000341 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
342 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000343 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCallaf146032010-10-30 11:50:40 +0000344 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000345 }
346
Douglas Gregord85b5b92009-11-25 22:24:25 +0000347 // - a named class (Clause 9), or an unnamed class defined in a
348 // typedef declaration in which the class has the typedef name
349 // for linkage purposes (7.1.3); or
350 // - a named enumeration (7.2), or an unnamed enumeration
351 // defined in a typedef declaration in which the enumeration
352 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000353 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
354 // Unnamed tags have no linkage.
355 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000356 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000357
John McCall1fb0caa2010-10-22 21:05:15 +0000358 // If this is a class template specialization, consider the
359 // linkage of the template and template arguments.
360 if (const ClassTemplateSpecializationDecl *Spec
361 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000362 // From the template.
363 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
364 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000365
John McCall1fb0caa2010-10-22 21:05:15 +0000366 // The arguments at which the template was instantiated.
367 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
John McCallaf146032010-10-30 11:50:40 +0000368 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000369 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000370
John McCallac65c622010-10-26 04:59:26 +0000371 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000372 if (F.ConsiderGlobalVisibility)
373 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000374 (Context.getLangOptions().CPlusPlus &&
375 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000376
Douglas Gregord85b5b92009-11-25 22:24:25 +0000377 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000378 } else if (isa<EnumConstantDecl>(D)) {
John McCallaf146032010-10-30 11:50:40 +0000379 LinkageInfo EnumLV =
John McCall1fb0caa2010-10-22 21:05:15 +0000380 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
John McCallaf146032010-10-30 11:50:40 +0000381 if (!isExternalLinkage(EnumLV.linkage()))
382 return LinkageInfo::none();
383 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000384
385 // - a template, unless it is a function template that has
386 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000387 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallaf146032010-10-30 11:50:40 +0000388 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000389
Douglas Gregord85b5b92009-11-25 22:24:25 +0000390 // - a namespace (7.3), unless it is declared within an unnamed
391 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000392 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
393 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000394
John McCall1fb0caa2010-10-22 21:05:15 +0000395 // By extension, we assign external linkage to Objective-C
396 // interfaces.
397 } else if (isa<ObjCInterfaceDecl>(D)) {
398 // fallout
399
400 // Everything not covered here has no linkage.
401 } else {
John McCallaf146032010-10-30 11:50:40 +0000402 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000403 }
404
405 // If we ended up with non-external linkage, visibility should
406 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000407 if (LV.linkage() != ExternalLinkage)
408 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000409
410 // If we didn't end up with hidden visibility, consider attributes
411 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000412 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000413 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000414
415 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000416}
417
John McCall36987482010-11-02 01:45:15 +0000418static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000419 // Only certain class members have linkage. Note that fields don't
420 // really have linkage, but it's convenient to say they do for the
421 // purposes of calculating linkage of pointer-to-data-member
422 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000423 if (!(isa<CXXMethodDecl>(D) ||
424 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000425 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000426 (isa<TagDecl>(D) &&
427 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000428 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000429
John McCall36987482010-11-02 01:45:15 +0000430 LinkageInfo LV;
431
432 // The flags we're going to use to compute the class's visibility.
433 LVFlags ClassF = F;
434
435 // If we have an explicit visibility attribute, merge that in.
436 if (F.ConsiderVisibilityAttributes) {
437 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
438 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
439
440 // Ignore global visibility later, but not this attribute.
441 F.ConsiderGlobalVisibility = false;
442
443 // Ignore both global visibility and attributes when computing our
444 // parent's visibility.
445 ClassF = F.onlyTemplateVisibility();
446 }
447 }
John McCallaf146032010-10-30 11:50:40 +0000448
449 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000450 // linkage.
451 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
452 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000453 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000454
455 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000456 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000457 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000458
John McCall3cdfc4d2010-08-13 08:35:10 +0000459 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000460 TemplateSpecializationKind TSK = TSK_Undeclared;
461
John McCall1fb0caa2010-10-22 21:05:15 +0000462 // If this is a method template specialization, use the linkage for
463 // the template parameters and arguments.
464 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000465 = MD->getTemplateSpecializationInfo()) {
John McCallaf146032010-10-30 11:50:40 +0000466 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments));
467 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000468 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000469
470 TSK = Spec->getTemplateSpecializationKind();
471 } else if (MemberSpecializationInfo *MSI =
472 MD->getMemberSpecializationInfo()) {
473 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000474 }
475
John McCall110e8e52010-10-29 22:22:43 +0000476 // If we're paying attention to global visibility, apply
477 // -finline-visibility-hidden if this is an inline method.
478 //
John McCallaf146032010-10-30 11:50:40 +0000479 // Note that ConsiderGlobalVisibility doesn't yet have information
480 // about whether containing classes have visibility attributes,
481 // and that's intentional.
482 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000483 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000484 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
485 // InlineVisibilityHidden only applies to definitions, and
486 // isInlined() only gives meaningful answers on definitions
487 // anyway.
488 const FunctionDecl *Def = 0;
489 if (MD->hasBody(Def) && Def->isInlined())
490 LV.setVisibility(HiddenVisibility);
491 }
John McCall1fb0caa2010-10-22 21:05:15 +0000492
John McCall110e8e52010-10-29 22:22:43 +0000493 // Note that in contrast to basically every other situation, we
494 // *do* apply -fvisibility to method declarations.
495
496 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000497 if (const ClassTemplateSpecializationDecl *Spec
498 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
499 // Merge template argument/parameter information for member
500 // class template specializations.
John McCallaf146032010-10-30 11:50:40 +0000501 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs()));
502 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000503 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000504 }
505
John McCall110e8e52010-10-29 22:22:43 +0000506 // Static data members.
507 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000508 // Modify the variable's linkage by its type, but ignore the
509 // type's visibility unless it's a definition.
510 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
511 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000512 LV.mergeLinkage(UniqueExternalLinkage);
513 if (!LV.visibilityExplicit())
514 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000515 }
516
John McCall36987482010-11-02 01:45:15 +0000517 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000518
519 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000520 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000521 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000522 }
523
John McCall1fb0caa2010-10-22 21:05:15 +0000524 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000525}
526
John McCallaf146032010-10-30 11:50:40 +0000527LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCall36987482010-11-02 01:45:15 +0000528 return getLVForDecl(this, LVFlags());
John McCall0df95872010-10-29 00:29:13 +0000529}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000530
John McCall36987482010-11-02 01:45:15 +0000531static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000532 // Objective-C: treat all Objective-C declarations as having external
533 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000534 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000535 default:
536 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000537 case Decl::TemplateTemplateParm: // count these as external
538 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000539 case Decl::ObjCAtDefsField:
540 case Decl::ObjCCategory:
541 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000542 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000543 case Decl::ObjCForwardProtocol:
544 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000545 case Decl::ObjCMethod:
546 case Decl::ObjCProperty:
547 case Decl::ObjCPropertyImpl:
548 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000549 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000550 }
551
Douglas Gregord85b5b92009-11-25 22:24:25 +0000552 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000553 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000554 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000555
556 // C++ [basic.link]p5:
557 // In addition, a member function, static data member, a named
558 // class or enumeration of class scope, or an unnamed class or
559 // enumeration defined in a class-scope typedef declaration such
560 // that the class or enumeration has the typedef name for linkage
561 // purposes (7.1.3), has external linkage if the name of the class
562 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000563 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000564 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000565
566 // C++ [basic.link]p6:
567 // The name of a function declared in block scope and the name of
568 // an object declared by a block scope extern declaration have
569 // linkage. If there is a visible declaration of an entity with
570 // linkage having the same name and type, ignoring entities
571 // declared outside the innermost enclosing namespace scope, the
572 // block scope declaration declares that same entity and receives
573 // the linkage of the previous declaration. If there is more than
574 // one such matching entity, the program is ill-formed. Otherwise,
575 // if no matching entity is found, the block scope entity receives
576 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000577 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
578 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000579 if (Function->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000580 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000581
John McCallaf146032010-10-30 11:50:40 +0000582 LinkageInfo LV;
John McCalle7bc9722010-10-28 04:18:25 +0000583 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCallaf146032010-10-30 11:50:40 +0000584 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall1fb0caa2010-10-22 21:05:15 +0000585
586 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
John McCallaf146032010-10-30 11:50:40 +0000587 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
588 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
589 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000590 }
591
592 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000593 }
594
John McCall0df95872010-10-29 00:29:13 +0000595 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000596 if (Var->getStorageClass() == SC_Extern ||
597 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000598 if (Var->isInAnonymousNamespace())
John McCallaf146032010-10-30 11:50:40 +0000599 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000600
John McCallaf146032010-10-30 11:50:40 +0000601 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000602 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000603 LV.setVisibility(HiddenVisibility);
John McCalle7bc9722010-10-28 04:18:25 +0000604 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCallaf146032010-10-30 11:50:40 +0000605 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall1fb0caa2010-10-22 21:05:15 +0000606
607 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
John McCallaf146032010-10-30 11:50:40 +0000608 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
609 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
610 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000611 }
612
613 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000614 }
615 }
616
617 // C++ [basic.link]p6:
618 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000619 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000620}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000621
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000622std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000623 return getQualifiedNameAsString(getASTContext().getLangOptions());
624}
625
626std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000627 const DeclContext *Ctx = getDeclContext();
628
629 if (Ctx->isFunctionOrMethod())
630 return getNameAsString();
631
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000632 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
633 ContextsTy Contexts;
634
635 // Collect contexts.
636 while (Ctx && isa<NamedDecl>(Ctx)) {
637 Contexts.push_back(Ctx);
638 Ctx = Ctx->getParent();
639 };
640
641 std::string QualName;
642 llvm::raw_string_ostream OS(QualName);
643
644 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
645 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000646 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000647 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000648 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
649 std::string TemplateArgsStr
650 = TemplateSpecializationType::PrintTemplateArgumentList(
651 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000652 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000653 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000654 OS << Spec->getName() << TemplateArgsStr;
655 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000656 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000657 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000658 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000659 OS << ND;
660 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
661 if (!RD->getIdentifier())
662 OS << "<anonymous " << RD->getKindName() << '>';
663 else
664 OS << RD;
665 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000666 const FunctionProtoType *FT = 0;
667 if (FD->hasWrittenPrototype())
668 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
669
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000670 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000671 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000672 unsigned NumParams = FD->getNumParams();
673 for (unsigned i = 0; i < NumParams; ++i) {
674 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000675 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000676 std::string Param;
677 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000678 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000679 }
680
681 if (FT->isVariadic()) {
682 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000683 OS << ", ";
684 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000685 }
686 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000687 OS << ')';
688 } else {
689 OS << cast<NamedDecl>(*I);
690 }
691 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000692 }
693
John McCall8472af42010-03-16 21:48:18 +0000694 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000695 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000696 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000697 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000698
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000699 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000700}
701
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000702bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000703 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
704
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000705 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
706 // We want to keep it, unless it nominates same namespace.
707 if (getKind() == Decl::UsingDirective) {
708 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
709 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
710 }
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000712 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
713 // For function declarations, we keep track of redeclarations.
714 return FD->getPreviousDeclaration() == OldD;
715
Douglas Gregore53060f2009-06-25 22:08:12 +0000716 // For function templates, the underlying function declarations are linked.
717 if (const FunctionTemplateDecl *FunctionTemplate
718 = dyn_cast<FunctionTemplateDecl>(this))
719 if (const FunctionTemplateDecl *OldFunctionTemplate
720 = dyn_cast<FunctionTemplateDecl>(OldD))
721 return FunctionTemplate->getTemplatedDecl()
722 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Steve Naroff0de21fd2009-02-22 19:35:57 +0000724 // For method declarations, we keep track of redeclarations.
725 if (isa<ObjCMethodDecl>(this))
726 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000727
John McCallf36e02d2009-10-09 21:13:30 +0000728 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
729 return true;
730
John McCall9488ea12009-11-17 05:59:44 +0000731 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
732 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
733 cast<UsingShadowDecl>(OldD)->getTargetDecl();
734
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000735 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
736 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
737 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
738
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000739 // For non-function declarations, if the declarations are of the
740 // same kind then this must be a redeclaration, or semantic analysis
741 // would not have given us the new declaration.
742 return this->getKind() == OldD->getKind();
743}
744
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000745bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000746 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000747}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000748
Anders Carlssone136e0e2009-06-26 06:29:23 +0000749NamedDecl *NamedDecl::getUnderlyingDecl() {
750 NamedDecl *ND = this;
751 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000752 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000753 ND = UD->getTargetDecl();
754 else if (ObjCCompatibleAliasDecl *AD
755 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
756 return AD->getClassInterface();
757 else
758 return ND;
759 }
760}
761
John McCall161755a2010-04-06 21:38:20 +0000762bool NamedDecl::isCXXInstanceMember() const {
763 assert(isCXXClassMember() &&
764 "checking whether non-member is instance member");
765
766 const NamedDecl *D = this;
767 if (isa<UsingShadowDecl>(D))
768 D = cast<UsingShadowDecl>(D)->getTargetDecl();
769
770 if (isa<FieldDecl>(D))
771 return true;
772 if (isa<CXXMethodDecl>(D))
773 return cast<CXXMethodDecl>(D)->isInstance();
774 if (isa<FunctionTemplateDecl>(D))
775 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
776 ->getTemplatedDecl())->isInstance();
777 return false;
778}
779
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000780//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000781// DeclaratorDecl Implementation
782//===----------------------------------------------------------------------===//
783
Douglas Gregor1693e152010-07-06 18:42:40 +0000784template <typename DeclT>
785static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
786 if (decl->getNumTemplateParameterLists() > 0)
787 return decl->getTemplateParameterList(0)->getTemplateLoc();
788 else
789 return decl->getInnerLocStart();
790}
791
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000792SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000793 TypeSourceInfo *TSI = getTypeSourceInfo();
794 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000795 return SourceLocation();
796}
797
John McCallb6217662010-03-15 10:12:16 +0000798void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
799 SourceRange QualifierRange) {
800 if (Qualifier) {
801 // Make sure the extended decl info is allocated.
802 if (!hasExtInfo()) {
803 // Save (non-extended) type source info pointer.
804 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
805 // Allocate external info struct.
806 DeclInfo = new (getASTContext()) ExtInfo;
807 // Restore savedTInfo into (extended) decl info.
808 getExtInfo()->TInfo = savedTInfo;
809 }
810 // Set qualifier info.
811 getExtInfo()->NNS = Qualifier;
812 getExtInfo()->NNSRange = QualifierRange;
813 }
814 else {
815 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
816 assert(QualifierRange.isInvalid());
817 if (hasExtInfo()) {
818 // Save type source info pointer.
819 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
820 // Deallocate the extended decl info.
821 getASTContext().Deallocate(getExtInfo());
822 // Restore savedTInfo into (non-extended) decl info.
823 DeclInfo = savedTInfo;
824 }
825 }
826}
827
Douglas Gregor1693e152010-07-06 18:42:40 +0000828SourceLocation DeclaratorDecl::getOuterLocStart() const {
829 return getTemplateOrInnerLocStart(this);
830}
831
Abramo Bagnara9b934882010-06-12 08:15:14 +0000832void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000833QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
834 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000835 TemplateParameterList **TPLists) {
836 assert((NumTPLists == 0 || TPLists != 0) &&
837 "Empty array of template parameters with positive size!");
838 assert((NumTPLists == 0 || NNS) &&
839 "Nonempty array of template parameters with no qualifier!");
840
841 // Free previous template parameters (if any).
842 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000843 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000844 TemplParamLists = 0;
845 NumTemplParamLists = 0;
846 }
847 // Set info on matched template parameter lists (if any).
848 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000849 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000850 NumTemplParamLists = NumTPLists;
851 for (unsigned i = NumTPLists; i-- > 0; )
852 TemplParamLists[i] = TPLists[i];
853 }
854}
855
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000856//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000857// VarDecl Implementation
858//===----------------------------------------------------------------------===//
859
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000860const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
861 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000862 case SC_None: break;
863 case SC_Auto: return "auto"; break;
864 case SC_Extern: return "extern"; break;
865 case SC_PrivateExtern: return "__private_extern__"; break;
866 case SC_Register: return "register"; break;
867 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000868 }
869
870 assert(0 && "Invalid storage class");
871 return 0;
872}
873
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000874VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000875 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000876 StorageClass S, StorageClass SCAsWritten) {
877 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000878}
879
Douglas Gregor1693e152010-07-06 18:42:40 +0000880SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000881 SourceLocation Start = getTypeSpecStartLoc();
882 if (Start.isInvalid())
883 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000884 return Start;
885}
886
887SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000888 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000889 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
890 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000891}
892
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000893bool VarDecl::isExternC() const {
894 ASTContext &Context = getASTContext();
895 if (!Context.getLangOptions().CPlusPlus)
896 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +0000897 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000898 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
899
900 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
901 DC = DC->getParent()) {
902 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
903 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000904 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000905
906 break;
907 }
908
909 if (DC->isFunctionOrMethod())
910 return false;
911 }
912
913 return false;
914}
915
916VarDecl *VarDecl::getCanonicalDecl() {
917 return getFirstDeclaration();
918}
919
Sebastian Redle9d12b62010-01-31 22:27:38 +0000920VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
921 // C++ [basic.def]p2:
922 // A declaration is a definition unless [...] it contains the 'extern'
923 // specifier or a linkage-specification and neither an initializer [...],
924 // it declares a static data member in a class declaration [...].
925 // C++ [temp.expl.spec]p15:
926 // An explicit specialization of a static data member of a template is a
927 // definition if the declaration includes an initializer; otherwise, it is
928 // a declaration.
929 if (isStaticDataMember()) {
930 if (isOutOfLine() && (hasInit() ||
931 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
932 return Definition;
933 else
934 return DeclarationOnly;
935 }
936 // C99 6.7p5:
937 // A definition of an identifier is a declaration for that identifier that
938 // [...] causes storage to be reserved for that object.
939 // Note: that applies for all non-file-scope objects.
940 // C99 6.9.2p1:
941 // If the declaration of an identifier for an object has file scope and an
942 // initializer, the declaration is an external definition for the identifier
943 if (hasInit())
944 return Definition;
945 // AST for 'extern "C" int foo;' is annotated with 'extern'.
946 if (hasExternalStorage())
947 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000948
John McCalld931b082010-08-26 03:08:43 +0000949 if (getStorageClassAsWritten() == SC_Extern ||
950 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000951 for (const VarDecl *PrevVar = getPreviousDeclaration();
952 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
953 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
954 return DeclarationOnly;
955 }
956 }
Sebastian Redle9d12b62010-01-31 22:27:38 +0000957 // C99 6.9.2p2:
958 // A declaration of an object that has file scope without an initializer,
959 // and without a storage class specifier or the scs 'static', constitutes
960 // a tentative definition.
961 // No such thing in C++.
962 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
963 return TentativeDefinition;
964
965 // What's left is (in C, block-scope) declarations without initializers or
966 // external storage. These are definitions.
967 return Definition;
968}
969
Sebastian Redle9d12b62010-01-31 22:27:38 +0000970VarDecl *VarDecl::getActingDefinition() {
971 DefinitionKind Kind = isThisDeclarationADefinition();
972 if (Kind != TentativeDefinition)
973 return 0;
974
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +0000975 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000976 VarDecl *First = getFirstDeclaration();
977 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
978 I != E; ++I) {
979 Kind = (*I)->isThisDeclarationADefinition();
980 if (Kind == Definition)
981 return 0;
982 else if (Kind == TentativeDefinition)
983 LastTentative = *I;
984 }
985 return LastTentative;
986}
987
988bool VarDecl::isTentativeDefinitionNow() const {
989 DefinitionKind Kind = isThisDeclarationADefinition();
990 if (Kind != TentativeDefinition)
991 return false;
992
993 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
994 if ((*I)->isThisDeclarationADefinition() == Definition)
995 return false;
996 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000997 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000998}
999
Sebastian Redl31310a22010-02-01 20:16:42 +00001000VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001001 VarDecl *First = getFirstDeclaration();
1002 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1003 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001004 if ((*I)->isThisDeclarationADefinition() == Definition)
1005 return *I;
1006 }
1007 return 0;
1008}
1009
John McCall110e8e52010-10-29 22:22:43 +00001010VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1011 DefinitionKind Kind = DeclarationOnly;
1012
1013 const VarDecl *First = getFirstDeclaration();
1014 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1015 I != E; ++I)
1016 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1017
1018 return Kind;
1019}
1020
Sebastian Redl31310a22010-02-01 20:16:42 +00001021const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001022 redecl_iterator I = redecls_begin(), E = redecls_end();
1023 while (I != E && !I->getInit())
1024 ++I;
1025
1026 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001027 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001028 return I->getInit();
1029 }
1030 return 0;
1031}
1032
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001033bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001034 if (Decl::isOutOfLine())
1035 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001036
1037 if (!isStaticDataMember())
1038 return false;
1039
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001040 // If this static data member was instantiated from a static data member of
1041 // a class template, check whether that static data member was defined
1042 // out-of-line.
1043 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1044 return VD->isOutOfLine();
1045
1046 return false;
1047}
1048
Douglas Gregor0d035142009-10-27 18:42:08 +00001049VarDecl *VarDecl::getOutOfLineDefinition() {
1050 if (!isStaticDataMember())
1051 return 0;
1052
1053 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1054 RD != RDEnd; ++RD) {
1055 if (RD->getLexicalDeclContext()->isFileContext())
1056 return *RD;
1057 }
1058
1059 return 0;
1060}
1061
Douglas Gregor838db382010-02-11 01:19:42 +00001062void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001063 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1064 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001065 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001066 }
1067
1068 Init = I;
1069}
1070
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001071VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001072 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001073 return cast<VarDecl>(MSI->getInstantiatedFrom());
1074
1075 return 0;
1076}
1077
Douglas Gregor663b5a02009-10-14 20:14:33 +00001078TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001079 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001080 return MSI->getTemplateSpecializationKind();
1081
1082 return TSK_Undeclared;
1083}
1084
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001085MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001086 return getASTContext().getInstantiatedFromStaticDataMember(this);
1087}
1088
Douglas Gregor0a897e32009-10-15 17:21:20 +00001089void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1090 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001091 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001092 assert(MSI && "Not an instantiated static data member?");
1093 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001094 if (TSK != TSK_ExplicitSpecialization &&
1095 PointOfInstantiation.isValid() &&
1096 MSI->getPointOfInstantiation().isInvalid())
1097 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001098}
1099
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001100//===----------------------------------------------------------------------===//
1101// ParmVarDecl Implementation
1102//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001103
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001104ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1105 SourceLocation L, IdentifierInfo *Id,
1106 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001107 StorageClass S, StorageClass SCAsWritten,
1108 Expr *DefArg) {
1109 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1110 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001111}
1112
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001113Expr *ParmVarDecl::getDefaultArg() {
1114 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1115 assert(!hasUninstantiatedDefaultArg() &&
1116 "Default argument is not yet instantiated!");
1117
1118 Expr *Arg = getInit();
1119 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1120 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001121
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001122 return Arg;
1123}
1124
1125unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1126 if (const CXXExprWithTemporaries *E =
1127 dyn_cast<CXXExprWithTemporaries>(getInit()))
1128 return E->getNumTemporaries();
1129
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001130 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001131}
1132
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001133CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1134 assert(getNumDefaultArgTemporaries() &&
1135 "Default arguments does not have any temporaries!");
1136
1137 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1138 return E->getTemporary(i);
1139}
1140
1141SourceRange ParmVarDecl::getDefaultArgRange() const {
1142 if (const Expr *E = getInit())
1143 return E->getSourceRange();
1144
1145 if (hasUninstantiatedDefaultArg())
1146 return getUninstantiatedDefaultArg()->getSourceRange();
1147
1148 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001149}
1150
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001151//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001152// FunctionDecl Implementation
1153//===----------------------------------------------------------------------===//
1154
John McCall136a6982009-09-11 06:45:03 +00001155void FunctionDecl::getNameForDiagnostic(std::string &S,
1156 const PrintingPolicy &Policy,
1157 bool Qualified) const {
1158 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1159 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1160 if (TemplateArgs)
1161 S += TemplateSpecializationType::PrintTemplateArgumentList(
1162 TemplateArgs->getFlatArgumentList(),
1163 TemplateArgs->flat_size(),
1164 Policy);
1165
1166}
Ted Kremenek27f8a282008-05-20 00:43:19 +00001167
Ted Kremenek9498d382010-04-29 16:49:01 +00001168bool FunctionDecl::isVariadic() const {
1169 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1170 return FT->isVariadic();
1171 return false;
1172}
1173
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001174bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1175 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1176 if (I->Body) {
1177 Definition = *I;
1178 return true;
1179 }
1180 }
1181
1182 return false;
1183}
1184
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001185Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001186 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1187 if (I->Body) {
1188 Definition = *I;
1189 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001190 }
1191 }
1192
1193 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001194}
1195
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001196void FunctionDecl::setBody(Stmt *B) {
1197 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +00001198 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001199 EndRangeLoc = B->getLocEnd();
1200}
1201
Douglas Gregor21386642010-09-28 21:55:22 +00001202void FunctionDecl::setPure(bool P) {
1203 IsPure = P;
1204 if (P)
1205 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1206 Parent->markedVirtualFunctionPure();
1207}
1208
Douglas Gregor48a83b52009-09-12 00:17:51 +00001209bool FunctionDecl::isMain() const {
1210 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001211 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001212 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001213 getIdentifier() && getIdentifier()->isStr("main");
1214}
1215
Douglas Gregor48a83b52009-09-12 00:17:51 +00001216bool FunctionDecl::isExternC() const {
1217 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001218 // In C, any non-static, non-overloadable function has external
1219 // linkage.
1220 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001221 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001222
Mike Stump1eb44332009-09-09 15:08:12 +00001223 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001224 DC = DC->getParent()) {
1225 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1226 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001227 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001228 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001229
1230 break;
1231 }
Douglas Gregor45975532010-08-17 16:09:23 +00001232
1233 if (DC->isRecord())
1234 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001235 }
1236
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001237 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001238}
1239
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001240bool FunctionDecl::isGlobal() const {
1241 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1242 return Method->isStatic();
1243
John McCalld931b082010-08-26 03:08:43 +00001244 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001245 return false;
1246
Mike Stump1eb44332009-09-09 15:08:12 +00001247 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001248 DC->isNamespace();
1249 DC = DC->getParent()) {
1250 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1251 if (!Namespace->getDeclName())
1252 return false;
1253 break;
1254 }
1255 }
1256
1257 return true;
1258}
1259
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001260void
1261FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1262 redeclarable_base::setPreviousDeclaration(PrevDecl);
1263
1264 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1265 FunctionTemplateDecl *PrevFunTmpl
1266 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1267 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1268 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1269 }
1270}
1271
1272const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1273 return getFirstDeclaration();
1274}
1275
1276FunctionDecl *FunctionDecl::getCanonicalDecl() {
1277 return getFirstDeclaration();
1278}
1279
Douglas Gregor3e41d602009-02-13 23:20:09 +00001280/// \brief Returns a value indicating whether this function
1281/// corresponds to a builtin function.
1282///
1283/// The function corresponds to a built-in function if it is
1284/// declared at translation scope or within an extern "C" block and
1285/// its name matches with the name of a builtin. The returned value
1286/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001287/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001288/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001289unsigned FunctionDecl::getBuiltinID() const {
1290 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001291 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1292 return 0;
1293
1294 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1295 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1296 return BuiltinID;
1297
1298 // This function has the name of a known C library
1299 // function. Determine whether it actually refers to the C library
1300 // function or whether it just has the same name.
1301
Douglas Gregor9add3172009-02-17 03:23:10 +00001302 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001303 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001304 return 0;
1305
Douglas Gregor3c385e52009-02-14 18:57:46 +00001306 // If this function is at translation-unit scope and we're not in
1307 // C++, it refers to the C library function.
1308 if (!Context.getLangOptions().CPlusPlus &&
1309 getDeclContext()->isTranslationUnit())
1310 return BuiltinID;
1311
1312 // If the function is in an extern "C" linkage specification and is
1313 // not marked "overloadable", it's the real function.
1314 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001315 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001316 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001317 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001318 return BuiltinID;
1319
1320 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001321 return 0;
1322}
1323
1324
Chris Lattner1ad9b282009-04-25 06:03:53 +00001325/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001326/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001327/// after it has been created.
1328unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001329 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001330 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001331 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001332 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001333
Reid Spencer5f016e22007-07-11 17:01:13 +00001334}
1335
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001336void FunctionDecl::setParams(ASTContext &C,
1337 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001339 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 // Zero params -> null pointer.
1342 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001343 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001344 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001345 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001346
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001347 // Update source range. The check below allows us to set EndRangeLoc before
1348 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001349 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001350 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 }
1352}
1353
Chris Lattner8123a952008-04-10 02:22:51 +00001354/// getMinRequiredArguments - Returns the minimum number of arguments
1355/// needed to call this function. This may be fewer than the number of
1356/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001357/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001358unsigned FunctionDecl::getMinRequiredArguments() const {
1359 unsigned NumRequiredArgs = getNumParams();
1360 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001361 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001362 --NumRequiredArgs;
1363
1364 return NumRequiredArgs;
1365}
1366
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001367bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001368 // FIXME: This is not enough. Consider:
1369 //
1370 // inline void f();
1371 // void f() { }
1372 //
1373 // f is inlined, but does not have inline specified.
1374 // To fix this we should add an 'inline' flag to FunctionDecl.
1375 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001376 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001377
1378 if (isa<CXXMethodDecl>(this)) {
1379 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1380 return true;
1381 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001382
1383 switch (getTemplateSpecializationKind()) {
1384 case TSK_Undeclared:
1385 case TSK_ExplicitSpecialization:
1386 return false;
1387
1388 case TSK_ImplicitInstantiation:
1389 case TSK_ExplicitInstantiationDeclaration:
1390 case TSK_ExplicitInstantiationDefinition:
1391 // Handle below.
1392 break;
1393 }
1394
1395 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001396 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001397 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001398 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001399
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001400 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001401 return PatternDecl->isInlined();
1402
1403 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001404}
1405
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001406/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001407/// definition will be externally visible.
1408///
1409/// Inline function definitions are always available for inlining optimizations.
1410/// However, depending on the language dialect, declaration specifiers, and
1411/// attributes, the definition of an inline function may or may not be
1412/// "externally" visible to other translation units in the program.
1413///
1414/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001415/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001416/// inline definition becomes externally visible (C99 6.7.4p6).
1417///
1418/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1419/// definition, we use the GNU semantics for inline, which are nearly the
1420/// opposite of C99 semantics. In particular, "inline" by itself will create
1421/// an externally visible symbol, but "extern inline" will not create an
1422/// externally visible symbol.
1423bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1424 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001425 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001426 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001427
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001428 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001429 // GNU inline semantics. Based on a number of examples, we came up with the
1430 // following heuristic: if the "inline" keyword is present on a
1431 // declaration of the function but "extern" is not present on that
1432 // declaration, then the symbol is externally visible. Otherwise, the GNU
1433 // "extern inline" semantics applies and the symbol is not externally
1434 // visible.
1435 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1436 Redecl != RedeclEnd;
1437 ++Redecl) {
John McCalld931b082010-08-26 03:08:43 +00001438 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001439 return true;
1440 }
1441
1442 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001443 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001444 }
1445
1446 // C99 6.7.4p6:
1447 // [...] If all of the file scope declarations for a function in a
1448 // translation unit include the inline function specifier without extern,
1449 // then the definition in that translation unit is an inline definition.
1450 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1451 Redecl != RedeclEnd;
1452 ++Redecl) {
1453 // Only consider file-scope declarations in this test.
1454 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1455 continue;
1456
John McCalld931b082010-08-26 03:08:43 +00001457 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001458 return true; // Not an inline definition
1459 }
1460
1461 // C99 6.7.4p6:
1462 // An inline definition does not provide an external definition for the
1463 // function, and does not forbid an external definition in another
1464 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001465 return false;
1466}
1467
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001468/// getOverloadedOperator - Which C++ overloaded operator this
1469/// function represents, if any.
1470OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001471 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1472 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001473 else
1474 return OO_None;
1475}
1476
Sean Hunta6c058d2010-01-13 09:01:02 +00001477/// getLiteralIdentifier - The literal suffix identifier this function
1478/// represents, if any.
1479const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1480 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1481 return getDeclName().getCXXLiteralIdentifier();
1482 else
1483 return 0;
1484}
1485
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001486FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1487 if (TemplateOrSpecialization.isNull())
1488 return TK_NonTemplate;
1489 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1490 return TK_FunctionTemplate;
1491 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1492 return TK_MemberSpecialization;
1493 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1494 return TK_FunctionTemplateSpecialization;
1495 if (TemplateOrSpecialization.is
1496 <DependentFunctionTemplateSpecializationInfo*>())
1497 return TK_DependentFunctionTemplateSpecialization;
1498
1499 assert(false && "Did we miss a TemplateOrSpecialization type?");
1500 return TK_NonTemplate;
1501}
1502
Douglas Gregor2db32322009-10-07 23:56:10 +00001503FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001504 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001505 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1506
1507 return 0;
1508}
1509
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001510MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1511 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1512}
1513
Douglas Gregor2db32322009-10-07 23:56:10 +00001514void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001515FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1516 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001517 TemplateSpecializationKind TSK) {
1518 assert(TemplateOrSpecialization.isNull() &&
1519 "Member function is already a specialization");
1520 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001521 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001522 TemplateOrSpecialization = Info;
1523}
1524
Douglas Gregor3b846b62009-10-27 20:53:28 +00001525bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001526 // If the function is invalid, it can't be implicitly instantiated.
1527 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001528 return false;
1529
1530 switch (getTemplateSpecializationKind()) {
1531 case TSK_Undeclared:
1532 case TSK_ExplicitSpecialization:
1533 case TSK_ExplicitInstantiationDefinition:
1534 return false;
1535
1536 case TSK_ImplicitInstantiation:
1537 return true;
1538
1539 case TSK_ExplicitInstantiationDeclaration:
1540 // Handled below.
1541 break;
1542 }
1543
1544 // Find the actual template from which we will instantiate.
1545 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001546 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001547 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001548 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001549
1550 // C++0x [temp.explicit]p9:
1551 // Except for inline functions, other explicit instantiation declarations
1552 // have the effect of suppressing the implicit instantiation of the entity
1553 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001554 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001555 return true;
1556
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001557 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001558}
1559
1560FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1561 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1562 while (Primary->getInstantiatedFromMemberTemplate()) {
1563 // If we have hit a point where the user provided a specialization of
1564 // this template, we're done looking.
1565 if (Primary->isMemberSpecialization())
1566 break;
1567
1568 Primary = Primary->getInstantiatedFromMemberTemplate();
1569 }
1570
1571 return Primary->getTemplatedDecl();
1572 }
1573
1574 return getInstantiatedFromMemberFunction();
1575}
1576
Douglas Gregor16e8be22009-06-29 17:30:29 +00001577FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001578 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001579 = TemplateOrSpecialization
1580 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001581 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001582 }
1583 return 0;
1584}
1585
1586const TemplateArgumentList *
1587FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001588 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001589 = TemplateOrSpecialization
1590 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001591 return Info->TemplateArguments;
1592 }
1593 return 0;
1594}
1595
Abramo Bagnarae03db982010-05-20 15:32:11 +00001596const TemplateArgumentListInfo *
1597FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1598 if (FunctionTemplateSpecializationInfo *Info
1599 = TemplateOrSpecialization
1600 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1601 return Info->TemplateArgumentsAsWritten;
1602 }
1603 return 0;
1604}
1605
Mike Stump1eb44332009-09-09 15:08:12 +00001606void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001607FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1608 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001609 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001610 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001611 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001612 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1613 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001614 assert(TSK != TSK_Undeclared &&
1615 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001616 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001617 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001618 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001619 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1620 TemplateArgs,
1621 TemplateArgsAsWritten,
1622 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001623 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001624
Douglas Gregor127102b2009-06-29 20:59:39 +00001625 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001626 // function template specializations.
1627 if (InsertPos)
1628 Template->getSpecializations().InsertNode(Info, InsertPos);
1629 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001630 // Try to insert the new node. If there is an existing node, leave it, the
1631 // set will contain the canonical decls while
1632 // FunctionTemplateDecl::findSpecialization will return
1633 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001634 FunctionTemplateSpecializationInfo *Existing
1635 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001636 (void)Existing;
1637 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1638 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001639 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001640}
1641
John McCallaf2094e2010-04-08 09:05:18 +00001642void
1643FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1644 const UnresolvedSetImpl &Templates,
1645 const TemplateArgumentListInfo &TemplateArgs) {
1646 assert(TemplateOrSpecialization.isNull());
1647 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1648 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001649 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001650 void *Buffer = Context.Allocate(Size);
1651 DependentFunctionTemplateSpecializationInfo *Info =
1652 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1653 TemplateArgs);
1654 TemplateOrSpecialization = Info;
1655}
1656
1657DependentFunctionTemplateSpecializationInfo::
1658DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1659 const TemplateArgumentListInfo &TArgs)
1660 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1661
1662 d.NumTemplates = Ts.size();
1663 d.NumArgs = TArgs.size();
1664
1665 FunctionTemplateDecl **TsArray =
1666 const_cast<FunctionTemplateDecl**>(getTemplates());
1667 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1668 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1669
1670 TemplateArgumentLoc *ArgsArray =
1671 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1672 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1673 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1674}
1675
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001676TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001677 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001678 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001679 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001680 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001681 if (FTSInfo)
1682 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001683
Douglas Gregor2db32322009-10-07 23:56:10 +00001684 MemberSpecializationInfo *MSInfo
1685 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1686 if (MSInfo)
1687 return MSInfo->getTemplateSpecializationKind();
1688
1689 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001690}
1691
Mike Stump1eb44332009-09-09 15:08:12 +00001692void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001693FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1694 SourceLocation PointOfInstantiation) {
1695 if (FunctionTemplateSpecializationInfo *FTSInfo
1696 = TemplateOrSpecialization.dyn_cast<
1697 FunctionTemplateSpecializationInfo*>()) {
1698 FTSInfo->setTemplateSpecializationKind(TSK);
1699 if (TSK != TSK_ExplicitSpecialization &&
1700 PointOfInstantiation.isValid() &&
1701 FTSInfo->getPointOfInstantiation().isInvalid())
1702 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1703 } else if (MemberSpecializationInfo *MSInfo
1704 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1705 MSInfo->setTemplateSpecializationKind(TSK);
1706 if (TSK != TSK_ExplicitSpecialization &&
1707 PointOfInstantiation.isValid() &&
1708 MSInfo->getPointOfInstantiation().isInvalid())
1709 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1710 } else
1711 assert(false && "Function cannot have a template specialization kind");
1712}
1713
1714SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001715 if (FunctionTemplateSpecializationInfo *FTSInfo
1716 = TemplateOrSpecialization.dyn_cast<
1717 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001718 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001719 else if (MemberSpecializationInfo *MSInfo
1720 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001721 return MSInfo->getPointOfInstantiation();
1722
1723 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001724}
1725
Douglas Gregor9f185072009-09-11 20:15:17 +00001726bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001727 if (Decl::isOutOfLine())
1728 return true;
1729
1730 // If this function was instantiated from a member function of a
1731 // class template, check whether that member function was defined out-of-line.
1732 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1733 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001734 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001735 return Definition->isOutOfLine();
1736 }
1737
1738 // If this function was instantiated from a function template,
1739 // check whether that function template was defined out-of-line.
1740 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1741 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001742 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001743 return Definition->isOutOfLine();
1744 }
1745
1746 return false;
1747}
1748
Chris Lattner8a934232008-03-31 00:36:02 +00001749//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001750// FieldDecl Implementation
1751//===----------------------------------------------------------------------===//
1752
1753FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1754 IdentifierInfo *Id, QualType T,
1755 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1756 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1757}
1758
1759bool FieldDecl::isAnonymousStructOrUnion() const {
1760 if (!isImplicit() || getDeclName())
1761 return false;
1762
1763 if (const RecordType *Record = getType()->getAs<RecordType>())
1764 return Record->getDecl()->isAnonymousStructOrUnion();
1765
1766 return false;
1767}
1768
1769//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001770// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001771//===----------------------------------------------------------------------===//
1772
Douglas Gregor1693e152010-07-06 18:42:40 +00001773SourceLocation TagDecl::getOuterLocStart() const {
1774 return getTemplateOrInnerLocStart(this);
1775}
1776
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001777SourceRange TagDecl::getSourceRange() const {
1778 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001779 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001780}
1781
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001782TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001783 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001784}
1785
Douglas Gregor60e70642010-05-19 18:39:18 +00001786void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1787 TypedefDeclOrQualifier = TDD;
1788 if (TypeForDecl)
1789 TypeForDecl->ClearLinkageCache();
1790}
1791
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001792void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001793 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001794
1795 if (isa<CXXRecordDecl>(this)) {
1796 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1797 struct CXXRecordDecl::DefinitionData *Data =
1798 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001799 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1800 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001801 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001802}
1803
1804void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001805 assert((!isa<CXXRecordDecl>(this) ||
1806 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1807 "definition completed but not started");
1808
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001809 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001810 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00001811
1812 if (ASTMutationListener *L = getASTMutationListener())
1813 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001814}
1815
Douglas Gregor952b0172010-02-11 01:04:33 +00001816TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001817 if (isDefinition())
1818 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00001819 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1820 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001821
1822 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001823 R != REnd; ++R)
1824 if (R->isDefinition())
1825 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001826
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001827 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001828}
1829
John McCallb6217662010-03-15 10:12:16 +00001830void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1831 SourceRange QualifierRange) {
1832 if (Qualifier) {
1833 // Make sure the extended qualifier info is allocated.
1834 if (!hasExtInfo())
1835 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1836 // Set qualifier info.
1837 getExtInfo()->NNS = Qualifier;
1838 getExtInfo()->NNSRange = QualifierRange;
1839 }
1840 else {
1841 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1842 assert(QualifierRange.isInvalid());
1843 if (hasExtInfo()) {
1844 getASTContext().Deallocate(getExtInfo());
1845 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1846 }
1847 }
1848}
1849
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001850//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001851// EnumDecl Implementation
1852//===----------------------------------------------------------------------===//
1853
1854EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1855 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001856 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1857 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1858 IsScoped, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001859 C.getTypeDeclType(Enum, PrevDecl);
1860 return Enum;
1861}
1862
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001863EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001864 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1865 false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001866}
1867
Douglas Gregor838db382010-02-11 01:19:42 +00001868void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001869 QualType NewPromotionType,
1870 unsigned NumPositiveBits,
1871 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001872 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001873 if (!IntegerType)
1874 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001875 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001876 setNumPositiveBits(NumPositiveBits);
1877 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001878 TagDecl::completeDefinition();
1879}
1880
1881//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001882// RecordDecl Implementation
1883//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001884
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001885RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001886 IdentifierInfo *Id, RecordDecl *PrevDecl,
1887 SourceLocation TKL)
1888 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001889 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001890 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001891 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001892 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001893 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001894}
1895
1896RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001897 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001898 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001899
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001900 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001901 C.getTypeDeclType(R, PrevDecl);
1902 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001903}
1904
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001905RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1906 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1907 SourceLocation());
1908}
1909
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001910bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001911 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001912 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1913}
1914
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001915RecordDecl::field_iterator RecordDecl::field_begin() const {
1916 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1917 LoadFieldsFromExternalStorage();
1918
1919 return field_iterator(decl_iterator(FirstDecl));
1920}
1921
Douglas Gregor44b43212008-12-11 16:49:14 +00001922/// completeDefinition - Notes that the definition of this type is now
1923/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001924void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001925 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001926 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001927}
1928
John McCallbc365c52010-05-21 01:17:40 +00001929ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1930 // Force the decl chain to come into existence properly.
1931 if (!getNextDeclInContext()) getParent()->decls_begin();
1932
1933 assert(isAnonymousStructOrUnion());
1934 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1935 assert(D->getType()->isRecordType());
1936 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1937 return D;
1938}
1939
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001940void RecordDecl::LoadFieldsFromExternalStorage() const {
1941 ExternalASTSource *Source = getASTContext().getExternalSource();
1942 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1943
1944 // Notify that we have a RecordDecl doing some initialization.
1945 ExternalASTSource::Deserializing TheFields(Source);
1946
1947 llvm::SmallVector<Decl*, 64> Decls;
1948 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1949 return;
1950
1951#ifndef NDEBUG
1952 // Check that all decls we got were FieldDecls.
1953 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1954 assert(isa<FieldDecl>(Decls[i]));
1955#endif
1956
1957 LoadedFieldsFromExternalStorage = true;
1958
1959 if (Decls.empty())
1960 return;
1961
1962 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1963}
1964
Steve Naroff56ee6892008-10-08 17:01:13 +00001965//===----------------------------------------------------------------------===//
1966// BlockDecl Implementation
1967//===----------------------------------------------------------------------===//
1968
Douglas Gregor838db382010-02-11 01:19:42 +00001969void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001970 unsigned NParms) {
1971 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001972
Steve Naroffe78b8092009-03-13 16:56:44 +00001973 // Zero params -> null pointer.
1974 if (NParms) {
1975 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001976 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001977 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1978 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1979 }
1980}
1981
1982unsigned BlockDecl::getNumParams() const {
1983 return NumParams;
1984}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001985
1986
1987//===----------------------------------------------------------------------===//
1988// Other Decl Allocation/Deallocation Method Implementations
1989//===----------------------------------------------------------------------===//
1990
1991TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1992 return new (C) TranslationUnitDecl(C);
1993}
1994
1995NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1996 SourceLocation L, IdentifierInfo *Id) {
1997 return new (C) NamespaceDecl(DC, L, Id);
1998}
1999
Douglas Gregor06c91932010-10-27 19:49:05 +00002000NamespaceDecl *NamespaceDecl::getNextNamespace() {
2001 return dyn_cast_or_null<NamespaceDecl>(
2002 NextNamespace.get(getASTContext().getExternalSource()));
2003}
2004
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002005ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2006 SourceLocation L, IdentifierInfo *Id, QualType T) {
2007 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2008}
2009
2010FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00002011 const DeclarationNameInfo &NameInfo,
2012 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002013 StorageClass S, StorageClass SCAsWritten,
2014 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00002015 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00002016 S, SCAsWritten, isInline);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002017 New->HasWrittenPrototype = hasWrittenPrototype;
2018 return New;
2019}
2020
2021BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2022 return new (C) BlockDecl(DC, L);
2023}
2024
2025EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2026 SourceLocation L,
2027 IdentifierInfo *Id, QualType T,
2028 Expr *E, const llvm::APSInt &V) {
2029 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2030}
2031
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002032SourceRange EnumConstantDecl::getSourceRange() const {
2033 SourceLocation End = getLocation();
2034 if (Init)
2035 End = Init->getLocEnd();
2036 return SourceRange(getLocation(), End);
2037}
2038
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002039TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2040 SourceLocation L, IdentifierInfo *Id,
2041 TypeSourceInfo *TInfo) {
2042 return new (C) TypedefDecl(DC, L, Id, TInfo);
2043}
2044
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002045FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2046 SourceLocation L,
2047 StringLiteral *Str) {
2048 return new (C) FileScopeAsmDecl(DC, L, Str);
2049}