blob: c448116ed2e56d8a0867e9e536973e5e42ce1e29 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000029
Chris Lattner6d9a6852006-10-25 05:11:20 +000030using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000031
Chris Lattner88f70d62008-03-15 05:43:15 +000032//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000033// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000034//===----------------------------------------------------------------------===//
35
John McCallb7139c42010-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 McCall457a04e2010-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 McCallc273f242010-10-30 11:50:40 +000065typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall457a04e2010-10-22 21:05:15 +000066typedef std::pair<Linkage,Visibility> LVPair;
John McCallc273f242010-10-30 11:50:40 +000067
John McCall457a04e2010-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 McCallc273f242010-10-30 11:50:40 +000073static LVPair merge(LVPair L, LinkageInfo R) {
74 return LVPair(minLinkage(L.first, R.linkage()),
75 minVisibility(L.second, R.visibility()));
76}
77
Benjamin Kramer396dcf32010-11-05 19:56:37 +000078namespace {
John McCall07072662010-11-02 01:45:15 +000079/// Flags controlling the computation of linkage and visibility.
80struct LVFlags {
81 bool ConsiderGlobalVisibility;
82 bool ConsiderVisibilityAttributes;
83
84 LVFlags() : ConsiderGlobalVisibility(true),
85 ConsiderVisibilityAttributes(true) {
86 }
87
88 /// Returns a set of flags, otherwise based on these, which ignores
89 /// off all sources of visibility except template arguments.
90 LVFlags onlyTemplateVisibility() const {
91 LVFlags F = *this;
92 F.ConsiderGlobalVisibility = false;
93 F.ConsiderVisibilityAttributes = false;
94 return F;
95 }
96};
Benjamin Kramer396dcf32010-11-05 19:56:37 +000097} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +000098
Douglas Gregor7dc5c172010-02-03 09:33:45 +000099/// \brief Get the most restrictive linkage for the types in the given
100/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +0000101static LVPair
102getLVForTemplateParameterList(const TemplateParameterList *Params) {
103 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000104 for (TemplateParameterList::const_iterator P = Params->begin(),
105 PEnd = Params->end();
106 P != PEnd; ++P) {
107 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
108 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +0000109 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000110 continue;
111 }
112
113 if (TemplateTemplateParmDecl *TTP
114 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +0000115 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000116 }
117 }
118
John McCall457a04e2010-10-22 21:05:15 +0000119 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000120}
121
122/// \brief Get the most restrictive linkage for the types and
123/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +0000124static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
125 unsigned NumArgs) {
126 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000127
128 for (unsigned I = 0; I != NumArgs; ++I) {
129 switch (Args[I].getKind()) {
130 case TemplateArgument::Null:
131 case TemplateArgument::Integral:
132 case TemplateArgument::Expression:
133 break;
134
135 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000136 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000137 break;
138
139 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000140 // The decl can validly be null as the representation of nullptr
141 // arguments, valid only in C++0x.
142 if (Decl *D = Args[I].getAsDecl()) {
143 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
144 LV = merge(LV, ND->getLinkageAndVisibility());
145 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
John McCallc273f242010-10-30 11:50:40 +0000146 LV = merge(LV, VD->getLinkageAndVisibility());
John McCall457a04e2010-10-22 21:05:15 +0000147 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000148 break;
149
150 case TemplateArgument::Template:
John McCall457a04e2010-10-22 21:05:15 +0000151 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
152 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000153 break;
154
155 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000156 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
157 Args[I].pack_size()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000158 break;
159 }
160 }
161
John McCall457a04e2010-10-22 21:05:15 +0000162 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000163}
164
John McCallc273f242010-10-30 11:50:40 +0000165static LVPair
166getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000167 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size());
John McCall8823c652010-08-13 08:35:10 +0000168}
169
John McCall033caa52010-10-29 00:29:13 +0000170/// getLVForDecl - Get the cached linkage and visibility for the given
171/// declaration.
John McCall07072662010-11-02 01:45:15 +0000172static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
John McCall033caa52010-10-29 00:29:13 +0000173
John McCall07072662010-11-02 01:45:15 +0000174static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000175 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000176 "Not a name having namespace scope");
177 ASTContext &Context = D->getASTContext();
178
179 // C++ [basic.link]p3:
180 // A name having namespace scope (3.3.6) has internal linkage if it
181 // is the name of
182 // - an object, reference, function or function template that is
183 // explicitly declared static; or,
184 // (This bullet corresponds to C99 6.2.2p3.)
185 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
186 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000187 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000188 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000189
190 // - an object or reference that is explicitly declared const
191 // and neither explicitly declared extern nor previously
192 // declared to have external linkage; or
193 // (there is no equivalent in C99)
194 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000195 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000196 Var->getStorageClass() != SC_Extern &&
197 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000198 bool FoundExtern = false;
199 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
200 PrevVar && !FoundExtern;
201 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000202 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000203 FoundExtern = true;
204
205 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000206 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000207 }
208 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000209 // C++ [temp]p4:
210 // A non-member function template can have internal linkage; any
211 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000212 const FunctionDecl *Function = 0;
213 if (const FunctionTemplateDecl *FunTmpl
214 = dyn_cast<FunctionTemplateDecl>(D))
215 Function = FunTmpl->getTemplatedDecl();
216 else
217 Function = cast<FunctionDecl>(D);
218
219 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000220 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000221 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000222 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
223 // - a data member of an anonymous union.
224 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000225 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000226 }
227
John McCall457a04e2010-10-22 21:05:15 +0000228 if (D->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000229 return LinkageInfo::uniqueExternal();
John McCallb7139c42010-10-28 04:18:25 +0000230
John McCall457a04e2010-10-22 21:05:15 +0000231 // Set up the defaults.
232
233 // C99 6.2.2p5:
234 // If the declaration of an identifier for an object has file
235 // scope and no storage-class specifier, its linkage is
236 // external.
John McCallc273f242010-10-30 11:50:40 +0000237 LinkageInfo LV;
238
John McCall07072662010-11-02 01:45:15 +0000239 if (F.ConsiderVisibilityAttributes) {
240 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
241 LV.setVisibility(GetVisibilityFromAttr(VA), true);
242 F.ConsiderGlobalVisibility = false;
243 }
John McCallc273f242010-10-30 11:50:40 +0000244 }
John McCall457a04e2010-10-22 21:05:15 +0000245
Douglas Gregorf73b2822009-11-25 22:24:25 +0000246 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000247
Douglas Gregorf73b2822009-11-25 22:24:25 +0000248 // A name having namespace scope has external linkage if it is the
249 // name of
250 //
251 // - an object or reference, unless it has internal linkage; or
252 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000253 // GCC applies the following optimization to variables and static
254 // data members, but not to functions:
255 //
John McCall457a04e2010-10-22 21:05:15 +0000256 // Modify the variable's LV by the LV of its type unless this is
257 // C or extern "C". This follows from [basic.link]p9:
258 // A type without linkage shall not be used as the type of a
259 // variable or function with external linkage unless
260 // - the entity has C language linkage, or
261 // - the entity is declared within an unnamed namespace, or
262 // - the entity is not used or is defined in the same
263 // translation unit.
264 // and [basic.link]p10:
265 // ...the types specified by all declarations referring to a
266 // given variable or function shall be identical...
267 // C does not have an equivalent rule.
268 //
John McCall5fe84122010-10-26 04:59:26 +0000269 // Ignore this if we've got an explicit attribute; the user
270 // probably knows what they're doing.
271 //
John McCall457a04e2010-10-22 21:05:15 +0000272 // Note that we don't want to make the variable non-external
273 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000274 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000275 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
276 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000277 return LinkageInfo::uniqueExternal();
278 if (!LV.visibilityExplicit())
279 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000280 }
281
John McCall23032652010-11-02 18:38:13 +0000282 if (Var->getStorageClass() == SC_PrivateExtern)
283 LV.setVisibility(HiddenVisibility, true);
284
Douglas Gregorf73b2822009-11-25 22:24:25 +0000285 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000286 (Var->getStorageClass() == SC_Extern ||
287 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000288
Douglas Gregorf73b2822009-11-25 22:24:25 +0000289 // C99 6.2.2p4:
290 // For an identifier declared with the storage-class specifier
291 // extern in a scope in which a prior declaration of that
292 // identifier is visible, if the prior declaration specifies
293 // internal or external linkage, the linkage of the identifier
294 // at the later declaration is the same as the linkage
295 // specified at the prior declaration. If no prior declaration
296 // is visible, or if the prior declaration specifies no
297 // linkage, then the identifier has external linkage.
298 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000299 LinkageInfo PrevLV = PrevVar->getLinkageAndVisibility();
300 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
301 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000302 }
303 }
304
Douglas Gregorf73b2822009-11-25 22:24:25 +0000305 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000306 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000307 // In theory, we can modify the function's LV by the LV of its
308 // type unless it has C linkage (see comment above about variables
309 // for justification). In practice, GCC doesn't do this, so it's
310 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000311
John McCall23032652010-11-02 18:38:13 +0000312 if (Function->getStorageClass() == SC_PrivateExtern)
313 LV.setVisibility(HiddenVisibility, true);
314
Douglas Gregorf73b2822009-11-25 22:24:25 +0000315 // C99 6.2.2p5:
316 // If the declaration of an identifier for a function has no
317 // storage-class specifier, its linkage is determined exactly
318 // as if it were declared with the storage-class specifier
319 // extern.
320 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000321 (Function->getStorageClass() == SC_Extern ||
322 Function->getStorageClass() == SC_PrivateExtern ||
323 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000324 // C99 6.2.2p4:
325 // For an identifier declared with the storage-class specifier
326 // extern in a scope in which a prior declaration of that
327 // identifier is visible, if the prior declaration specifies
328 // internal or external linkage, the linkage of the identifier
329 // at the later declaration is the same as the linkage
330 // specified at the prior declaration. If no prior declaration
331 // is visible, or if the prior declaration specifies no
332 // linkage, then the identifier has external linkage.
333 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000334 LinkageInfo PrevLV = PrevFunc->getLinkageAndVisibility();
335 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
336 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000337 }
338 }
339
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000340 if (FunctionTemplateSpecializationInfo *SpecInfo
341 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000342 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
343 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000344 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCallc273f242010-10-30 11:50:40 +0000345 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000346 }
347
Douglas Gregorf73b2822009-11-25 22:24:25 +0000348 // - a named class (Clause 9), or an unnamed class defined in a
349 // typedef declaration in which the class has the typedef name
350 // for linkage purposes (7.1.3); or
351 // - a named enumeration (7.2), or an unnamed enumeration
352 // defined in a typedef declaration in which the enumeration
353 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000354 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
355 // Unnamed tags have no linkage.
356 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000357 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000358
John McCall457a04e2010-10-22 21:05:15 +0000359 // If this is a class template specialization, consider the
360 // linkage of the template and template arguments.
361 if (const ClassTemplateSpecializationDecl *Spec
362 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000363 // From the template.
364 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
365 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000366
John McCall457a04e2010-10-22 21:05:15 +0000367 // The arguments at which the template was instantiated.
368 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
John McCallc273f242010-10-30 11:50:40 +0000369 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000370 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000371
John McCall5fe84122010-10-26 04:59:26 +0000372 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000373 if (F.ConsiderGlobalVisibility)
374 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000375 (Context.getLangOptions().CPlusPlus &&
376 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000377
Douglas Gregorf73b2822009-11-25 22:24:25 +0000378 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000379 } else if (isa<EnumConstantDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000380 LinkageInfo EnumLV =
John McCall457a04e2010-10-22 21:05:15 +0000381 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
John McCallc273f242010-10-30 11:50:40 +0000382 if (!isExternalLinkage(EnumLV.linkage()))
383 return LinkageInfo::none();
384 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000385
386 // - a template, unless it is a function template that has
387 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000388 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000389 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000390
Douglas Gregorf73b2822009-11-25 22:24:25 +0000391 // - a namespace (7.3), unless it is declared within an unnamed
392 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000393 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
394 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000395
John McCall457a04e2010-10-22 21:05:15 +0000396 // By extension, we assign external linkage to Objective-C
397 // interfaces.
398 } else if (isa<ObjCInterfaceDecl>(D)) {
399 // fallout
400
401 // Everything not covered here has no linkage.
402 } else {
John McCallc273f242010-10-30 11:50:40 +0000403 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000404 }
405
406 // If we ended up with non-external linkage, visibility should
407 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000408 if (LV.linkage() != ExternalLinkage)
409 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000410
411 // If we didn't end up with hidden visibility, consider attributes
412 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000413 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000414 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000415
416 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000417}
418
John McCall07072662010-11-02 01:45:15 +0000419static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000420 // Only certain class members have linkage. Note that fields don't
421 // really have linkage, but it's convenient to say they do for the
422 // purposes of calculating linkage of pointer-to-data-member
423 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000424 if (!(isa<CXXMethodDecl>(D) ||
425 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000426 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000427 (isa<TagDecl>(D) &&
428 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000429 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000430
John McCall07072662010-11-02 01:45:15 +0000431 LinkageInfo LV;
432
433 // The flags we're going to use to compute the class's visibility.
434 LVFlags ClassF = F;
435
436 // If we have an explicit visibility attribute, merge that in.
437 if (F.ConsiderVisibilityAttributes) {
438 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
439 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
440
441 // Ignore global visibility later, but not this attribute.
442 F.ConsiderGlobalVisibility = false;
443
444 // Ignore both global visibility and attributes when computing our
445 // parent's visibility.
446 ClassF = F.onlyTemplateVisibility();
447 }
448 }
John McCallc273f242010-10-30 11:50:40 +0000449
450 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000451 // linkage.
452 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
453 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000454 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000455
456 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000457 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000458 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000459
John McCall8823c652010-08-13 08:35:10 +0000460 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000461 TemplateSpecializationKind TSK = TSK_Undeclared;
462
John McCall457a04e2010-10-22 21:05:15 +0000463 // If this is a method template specialization, use the linkage for
464 // the template parameters and arguments.
465 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000466 = MD->getTemplateSpecializationInfo()) {
John McCallc273f242010-10-30 11:50:40 +0000467 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments));
468 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000469 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000470
471 TSK = Spec->getTemplateSpecializationKind();
472 } else if (MemberSpecializationInfo *MSI =
473 MD->getMemberSpecializationInfo()) {
474 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000475 }
476
John McCall37bb6c92010-10-29 22:22:43 +0000477 // If we're paying attention to global visibility, apply
478 // -finline-visibility-hidden if this is an inline method.
479 //
John McCallc273f242010-10-30 11:50:40 +0000480 // Note that ConsiderGlobalVisibility doesn't yet have information
481 // about whether containing classes have visibility attributes,
482 // and that's intentional.
483 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000484 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000485 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
486 // InlineVisibilityHidden only applies to definitions, and
487 // isInlined() only gives meaningful answers on definitions
488 // anyway.
489 const FunctionDecl *Def = 0;
490 if (MD->hasBody(Def) && Def->isInlined())
491 LV.setVisibility(HiddenVisibility);
492 }
John McCall457a04e2010-10-22 21:05:15 +0000493
John McCall37bb6c92010-10-29 22:22:43 +0000494 // Note that in contrast to basically every other situation, we
495 // *do* apply -fvisibility to method declarations.
496
497 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000498 if (const ClassTemplateSpecializationDecl *Spec
499 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
500 // Merge template argument/parameter information for member
501 // class template specializations.
John McCallc273f242010-10-30 11:50:40 +0000502 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs()));
503 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000504 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000505 }
506
John McCall37bb6c92010-10-29 22:22:43 +0000507 // Static data members.
508 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000509 // Modify the variable's linkage by its type, but ignore the
510 // type's visibility unless it's a definition.
511 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
512 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000513 LV.mergeLinkage(UniqueExternalLinkage);
514 if (!LV.visibilityExplicit())
515 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000516 }
517
John McCall07072662010-11-02 01:45:15 +0000518 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000519
520 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000521 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000522 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000523 }
524
John McCall457a04e2010-10-22 21:05:15 +0000525 return LV;
John McCall8823c652010-08-13 08:35:10 +0000526}
527
John McCallc273f242010-10-30 11:50:40 +0000528LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCall07072662010-11-02 01:45:15 +0000529 return getLVForDecl(this, LVFlags());
John McCall033caa52010-10-29 00:29:13 +0000530}
Ted Kremenek926d8602010-04-20 23:15:35 +0000531
John McCall07072662010-11-02 01:45:15 +0000532static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000533 // Objective-C: treat all Objective-C declarations as having external
534 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000535 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000536 default:
537 break;
John McCall457a04e2010-10-22 21:05:15 +0000538 case Decl::TemplateTemplateParm: // count these as external
539 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000540 case Decl::ObjCAtDefsField:
541 case Decl::ObjCCategory:
542 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000543 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000544 case Decl::ObjCForwardProtocol:
545 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000546 case Decl::ObjCMethod:
547 case Decl::ObjCProperty:
548 case Decl::ObjCPropertyImpl:
549 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000550 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000551 }
552
Douglas Gregorf73b2822009-11-25 22:24:25 +0000553 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000554 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000555 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000556
557 // C++ [basic.link]p5:
558 // In addition, a member function, static data member, a named
559 // class or enumeration of class scope, or an unnamed class or
560 // enumeration defined in a class-scope typedef declaration such
561 // that the class or enumeration has the typedef name for linkage
562 // purposes (7.1.3), has external linkage if the name of the class
563 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000564 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000565 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000566
567 // C++ [basic.link]p6:
568 // The name of a function declared in block scope and the name of
569 // an object declared by a block scope extern declaration have
570 // linkage. If there is a visible declaration of an entity with
571 // linkage having the same name and type, ignoring entities
572 // declared outside the innermost enclosing namespace scope, the
573 // block scope declaration declares that same entity and receives
574 // the linkage of the previous declaration. If there is more than
575 // one such matching entity, the program is ill-formed. Otherwise,
576 // if no matching entity is found, the block scope entity receives
577 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000578 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
579 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000580 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000581 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000582
John McCallc273f242010-10-30 11:50:40 +0000583 LinkageInfo LV;
John McCallb7139c42010-10-28 04:18:25 +0000584 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCallc273f242010-10-30 11:50:40 +0000585 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000586
587 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000588 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
589 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
590 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000591 }
592
593 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000594 }
595
John McCall033caa52010-10-29 00:29:13 +0000596 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000597 if (Var->getStorageClass() == SC_Extern ||
598 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000599 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000600 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000601
John McCallc273f242010-10-30 11:50:40 +0000602 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000603 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000604 LV.setVisibility(HiddenVisibility);
John McCallb7139c42010-10-28 04:18:25 +0000605 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCallc273f242010-10-30 11:50:40 +0000606 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000607
608 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000609 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
610 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
611 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000612 }
613
614 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000615 }
616 }
617
618 // C++ [basic.link]p6:
619 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000620 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000621}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000622
Douglas Gregor2ada0482009-02-04 17:27:36 +0000623std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000624 return getQualifiedNameAsString(getASTContext().getLangOptions());
625}
626
627std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000628 const DeclContext *Ctx = getDeclContext();
629
630 if (Ctx->isFunctionOrMethod())
631 return getNameAsString();
632
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000633 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
634 ContextsTy Contexts;
635
636 // Collect contexts.
637 while (Ctx && isa<NamedDecl>(Ctx)) {
638 Contexts.push_back(Ctx);
639 Ctx = Ctx->getParent();
640 };
641
642 std::string QualName;
643 llvm::raw_string_ostream OS(QualName);
644
645 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
646 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000647 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000648 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000649 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
650 std::string TemplateArgsStr
651 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000652 TemplateArgs.data(),
653 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000654 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000655 OS << Spec->getName() << TemplateArgsStr;
656 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000657 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000658 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000659 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000660 OS << ND;
661 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
662 if (!RD->getIdentifier())
663 OS << "<anonymous " << RD->getKindName() << '>';
664 else
665 OS << RD;
666 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000667 const FunctionProtoType *FT = 0;
668 if (FD->hasWrittenPrototype())
669 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
670
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000671 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000672 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000673 unsigned NumParams = FD->getNumParams();
674 for (unsigned i = 0; i < NumParams; ++i) {
675 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000676 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000677 std::string Param;
678 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000679 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000680 }
681
682 if (FT->isVariadic()) {
683 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000684 OS << ", ";
685 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000686 }
687 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000688 OS << ')';
689 } else {
690 OS << cast<NamedDecl>(*I);
691 }
692 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000693 }
694
John McCalla2a3f7d2010-03-16 21:48:18 +0000695 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000696 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000697 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000698 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000699
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000700 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000701}
702
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000703bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000704 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
705
Douglas Gregor889ceb72009-02-03 19:21:40 +0000706 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
707 // We want to keep it, unless it nominates same namespace.
708 if (getKind() == Decl::UsingDirective) {
709 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
710 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
711 }
Mike Stump11289f42009-09-09 15:08:12 +0000712
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000713 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
714 // For function declarations, we keep track of redeclarations.
715 return FD->getPreviousDeclaration() == OldD;
716
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000717 // For function templates, the underlying function declarations are linked.
718 if (const FunctionTemplateDecl *FunctionTemplate
719 = dyn_cast<FunctionTemplateDecl>(this))
720 if (const FunctionTemplateDecl *OldFunctionTemplate
721 = dyn_cast<FunctionTemplateDecl>(OldD))
722 return FunctionTemplate->getTemplatedDecl()
723 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000724
Steve Naroffc4173fa2009-02-22 19:35:57 +0000725 // For method declarations, we keep track of redeclarations.
726 if (isa<ObjCMethodDecl>(this))
727 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000728
John McCall9f3059a2009-10-09 21:13:30 +0000729 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
730 return true;
731
John McCall3f746822009-11-17 05:59:44 +0000732 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
733 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
734 cast<UsingShadowDecl>(OldD)->getTargetDecl();
735
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000736 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
737 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
738 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
739
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000740 // For non-function declarations, if the declarations are of the
741 // same kind then this must be a redeclaration, or semantic analysis
742 // would not have given us the new declaration.
743 return this->getKind() == OldD->getKind();
744}
745
Douglas Gregoreddf4332009-02-24 20:03:32 +0000746bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000747 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000748}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000749
Anders Carlsson6915bf62009-06-26 06:29:23 +0000750NamedDecl *NamedDecl::getUnderlyingDecl() {
751 NamedDecl *ND = this;
752 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000753 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000754 ND = UD->getTargetDecl();
755 else if (ObjCCompatibleAliasDecl *AD
756 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
757 return AD->getClassInterface();
758 else
759 return ND;
760 }
761}
762
John McCalla8ae2222010-04-06 21:38:20 +0000763bool NamedDecl::isCXXInstanceMember() const {
764 assert(isCXXClassMember() &&
765 "checking whether non-member is instance member");
766
767 const NamedDecl *D = this;
768 if (isa<UsingShadowDecl>(D))
769 D = cast<UsingShadowDecl>(D)->getTargetDecl();
770
771 if (isa<FieldDecl>(D))
772 return true;
773 if (isa<CXXMethodDecl>(D))
774 return cast<CXXMethodDecl>(D)->isInstance();
775 if (isa<FunctionTemplateDecl>(D))
776 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
777 ->getTemplatedDecl())->isInstance();
778 return false;
779}
780
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000781//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000782// DeclaratorDecl Implementation
783//===----------------------------------------------------------------------===//
784
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000785template <typename DeclT>
786static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
787 if (decl->getNumTemplateParameterLists() > 0)
788 return decl->getTemplateParameterList(0)->getTemplateLoc();
789 else
790 return decl->getInnerLocStart();
791}
792
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000793SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000794 TypeSourceInfo *TSI = getTypeSourceInfo();
795 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000796 return SourceLocation();
797}
798
John McCall3e11ebe2010-03-15 10:12:16 +0000799void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
800 SourceRange QualifierRange) {
801 if (Qualifier) {
802 // Make sure the extended decl info is allocated.
803 if (!hasExtInfo()) {
804 // Save (non-extended) type source info pointer.
805 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
806 // Allocate external info struct.
807 DeclInfo = new (getASTContext()) ExtInfo;
808 // Restore savedTInfo into (extended) decl info.
809 getExtInfo()->TInfo = savedTInfo;
810 }
811 // Set qualifier info.
812 getExtInfo()->NNS = Qualifier;
813 getExtInfo()->NNSRange = QualifierRange;
814 }
815 else {
816 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
817 assert(QualifierRange.isInvalid());
818 if (hasExtInfo()) {
819 // Save type source info pointer.
820 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
821 // Deallocate the extended decl info.
822 getASTContext().Deallocate(getExtInfo());
823 // Restore savedTInfo into (non-extended) decl info.
824 DeclInfo = savedTInfo;
825 }
826 }
827}
828
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000829SourceLocation DeclaratorDecl::getOuterLocStart() const {
830 return getTemplateOrInnerLocStart(this);
831}
832
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000833void
Douglas Gregor20527e22010-06-15 17:44:38 +0000834QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
835 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000836 TemplateParameterList **TPLists) {
837 assert((NumTPLists == 0 || TPLists != 0) &&
838 "Empty array of template parameters with positive size!");
839 assert((NumTPLists == 0 || NNS) &&
840 "Nonempty array of template parameters with no qualifier!");
841
842 // Free previous template parameters (if any).
843 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000844 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000845 TemplParamLists = 0;
846 NumTemplParamLists = 0;
847 }
848 // Set info on matched template parameter lists (if any).
849 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000850 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000851 NumTemplParamLists = NumTPLists;
852 for (unsigned i = NumTPLists; i-- > 0; )
853 TemplParamLists[i] = TPLists[i];
854 }
855}
856
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000857//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000858// VarDecl Implementation
859//===----------------------------------------------------------------------===//
860
Sebastian Redl833ef452010-01-26 22:01:41 +0000861const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
862 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000863 case SC_None: break;
864 case SC_Auto: return "auto"; break;
865 case SC_Extern: return "extern"; break;
866 case SC_PrivateExtern: return "__private_extern__"; break;
867 case SC_Register: return "register"; break;
868 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000869 }
870
871 assert(0 && "Invalid storage class");
872 return 0;
873}
874
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000875VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000876 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000877 StorageClass S, StorageClass SCAsWritten) {
878 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000879}
880
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000881SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000882 SourceLocation Start = getTypeSpecStartLoc();
883 if (Start.isInvalid())
884 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000885 return Start;
886}
887
888SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000889 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000890 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
891 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000892}
893
Sebastian Redl833ef452010-01-26 22:01:41 +0000894bool VarDecl::isExternC() const {
895 ASTContext &Context = getASTContext();
896 if (!Context.getLangOptions().CPlusPlus)
897 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000898 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000899 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
900
901 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
902 DC = DC->getParent()) {
903 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
904 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000905 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000906
907 break;
908 }
909
910 if (DC->isFunctionOrMethod())
911 return false;
912 }
913
914 return false;
915}
916
917VarDecl *VarDecl::getCanonicalDecl() {
918 return getFirstDeclaration();
919}
920
Sebastian Redl35351a92010-01-31 22:27:38 +0000921VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
922 // C++ [basic.def]p2:
923 // A declaration is a definition unless [...] it contains the 'extern'
924 // specifier or a linkage-specification and neither an initializer [...],
925 // it declares a static data member in a class declaration [...].
926 // C++ [temp.expl.spec]p15:
927 // An explicit specialization of a static data member of a template is a
928 // definition if the declaration includes an initializer; otherwise, it is
929 // a declaration.
930 if (isStaticDataMember()) {
931 if (isOutOfLine() && (hasInit() ||
932 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
933 return Definition;
934 else
935 return DeclarationOnly;
936 }
937 // C99 6.7p5:
938 // A definition of an identifier is a declaration for that identifier that
939 // [...] causes storage to be reserved for that object.
940 // Note: that applies for all non-file-scope objects.
941 // C99 6.9.2p1:
942 // If the declaration of an identifier for an object has file scope and an
943 // initializer, the declaration is an external definition for the identifier
944 if (hasInit())
945 return Definition;
946 // AST for 'extern "C" int foo;' is annotated with 'extern'.
947 if (hasExternalStorage())
948 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000949
John McCall8e7d6562010-08-26 03:08:43 +0000950 if (getStorageClassAsWritten() == SC_Extern ||
951 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000952 for (const VarDecl *PrevVar = getPreviousDeclaration();
953 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
954 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
955 return DeclarationOnly;
956 }
957 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000958 // C99 6.9.2p2:
959 // A declaration of an object that has file scope without an initializer,
960 // and without a storage class specifier or the scs 'static', constitutes
961 // a tentative definition.
962 // No such thing in C++.
963 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
964 return TentativeDefinition;
965
966 // What's left is (in C, block-scope) declarations without initializers or
967 // external storage. These are definitions.
968 return Definition;
969}
970
Sebastian Redl35351a92010-01-31 22:27:38 +0000971VarDecl *VarDecl::getActingDefinition() {
972 DefinitionKind Kind = isThisDeclarationADefinition();
973 if (Kind != TentativeDefinition)
974 return 0;
975
Chris Lattner48eb14d2010-06-14 18:31:46 +0000976 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000977 VarDecl *First = getFirstDeclaration();
978 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
979 I != E; ++I) {
980 Kind = (*I)->isThisDeclarationADefinition();
981 if (Kind == Definition)
982 return 0;
983 else if (Kind == TentativeDefinition)
984 LastTentative = *I;
985 }
986 return LastTentative;
987}
988
989bool VarDecl::isTentativeDefinitionNow() const {
990 DefinitionKind Kind = isThisDeclarationADefinition();
991 if (Kind != TentativeDefinition)
992 return false;
993
994 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
995 if ((*I)->isThisDeclarationADefinition() == Definition)
996 return false;
997 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000998 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000999}
1000
Sebastian Redl5ca79842010-02-01 20:16:42 +00001001VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001002 VarDecl *First = getFirstDeclaration();
1003 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1004 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001005 if ((*I)->isThisDeclarationADefinition() == Definition)
1006 return *I;
1007 }
1008 return 0;
1009}
1010
John McCall37bb6c92010-10-29 22:22:43 +00001011VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1012 DefinitionKind Kind = DeclarationOnly;
1013
1014 const VarDecl *First = getFirstDeclaration();
1015 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1016 I != E; ++I)
1017 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1018
1019 return Kind;
1020}
1021
Sebastian Redl5ca79842010-02-01 20:16:42 +00001022const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001023 redecl_iterator I = redecls_begin(), E = redecls_end();
1024 while (I != E && !I->getInit())
1025 ++I;
1026
1027 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001028 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001029 return I->getInit();
1030 }
1031 return 0;
1032}
1033
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001034bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001035 if (Decl::isOutOfLine())
1036 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001037
1038 if (!isStaticDataMember())
1039 return false;
1040
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001041 // If this static data member was instantiated from a static data member of
1042 // a class template, check whether that static data member was defined
1043 // out-of-line.
1044 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1045 return VD->isOutOfLine();
1046
1047 return false;
1048}
1049
Douglas Gregor1d957a32009-10-27 18:42:08 +00001050VarDecl *VarDecl::getOutOfLineDefinition() {
1051 if (!isStaticDataMember())
1052 return 0;
1053
1054 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1055 RD != RDEnd; ++RD) {
1056 if (RD->getLexicalDeclContext()->isFileContext())
1057 return *RD;
1058 }
1059
1060 return 0;
1061}
1062
Douglas Gregord5058122010-02-11 01:19:42 +00001063void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001064 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1065 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001066 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001067 }
1068
1069 Init = I;
1070}
1071
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001072VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001073 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001074 return cast<VarDecl>(MSI->getInstantiatedFrom());
1075
1076 return 0;
1077}
1078
Douglas Gregor3c74d412009-10-14 20:14:33 +00001079TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001080 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001081 return MSI->getTemplateSpecializationKind();
1082
1083 return TSK_Undeclared;
1084}
1085
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001086MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001087 return getASTContext().getInstantiatedFromStaticDataMember(this);
1088}
1089
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001090void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1091 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001092 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001093 assert(MSI && "Not an instantiated static data member?");
1094 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001095 if (TSK != TSK_ExplicitSpecialization &&
1096 PointOfInstantiation.isValid() &&
1097 MSI->getPointOfInstantiation().isInvalid())
1098 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001099}
1100
Sebastian Redl833ef452010-01-26 22:01:41 +00001101//===----------------------------------------------------------------------===//
1102// ParmVarDecl Implementation
1103//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001104
Sebastian Redl833ef452010-01-26 22:01:41 +00001105ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1106 SourceLocation L, IdentifierInfo *Id,
1107 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001108 StorageClass S, StorageClass SCAsWritten,
1109 Expr *DefArg) {
1110 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1111 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001112}
1113
Sebastian Redl833ef452010-01-26 22:01:41 +00001114Expr *ParmVarDecl::getDefaultArg() {
1115 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1116 assert(!hasUninstantiatedDefaultArg() &&
1117 "Default argument is not yet instantiated!");
1118
1119 Expr *Arg = getInit();
1120 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1121 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001122
Sebastian Redl833ef452010-01-26 22:01:41 +00001123 return Arg;
1124}
1125
1126unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1127 if (const CXXExprWithTemporaries *E =
1128 dyn_cast<CXXExprWithTemporaries>(getInit()))
1129 return E->getNumTemporaries();
1130
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001131 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001132}
1133
Sebastian Redl833ef452010-01-26 22:01:41 +00001134CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1135 assert(getNumDefaultArgTemporaries() &&
1136 "Default arguments does not have any temporaries!");
1137
1138 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1139 return E->getTemporary(i);
1140}
1141
1142SourceRange ParmVarDecl::getDefaultArgRange() const {
1143 if (const Expr *E = getInit())
1144 return E->getSourceRange();
1145
1146 if (hasUninstantiatedDefaultArg())
1147 return getUninstantiatedDefaultArg()->getSourceRange();
1148
1149 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001150}
1151
Nuno Lopes394ec982008-12-17 23:39:55 +00001152//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001153// FunctionDecl Implementation
1154//===----------------------------------------------------------------------===//
1155
John McCalle1f2ec22009-09-11 06:45:03 +00001156void FunctionDecl::getNameForDiagnostic(std::string &S,
1157 const PrintingPolicy &Policy,
1158 bool Qualified) const {
1159 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1160 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1161 if (TemplateArgs)
1162 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001163 TemplateArgs->data(),
1164 TemplateArgs->size(),
John McCalle1f2ec22009-09-11 06:45:03 +00001165 Policy);
1166
1167}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001168
Ted Kremenek186a0742010-04-29 16:49:01 +00001169bool FunctionDecl::isVariadic() const {
1170 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1171 return FT->isVariadic();
1172 return false;
1173}
1174
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001175bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1176 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1177 if (I->Body) {
1178 Definition = *I;
1179 return true;
1180 }
1181 }
1182
1183 return false;
1184}
1185
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001186Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001187 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1188 if (I->Body) {
1189 Definition = *I;
1190 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001191 }
1192 }
1193
1194 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001195}
1196
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001197void FunctionDecl::setBody(Stmt *B) {
1198 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001199 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001200 EndRangeLoc = B->getLocEnd();
1201}
1202
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001203void FunctionDecl::setPure(bool P) {
1204 IsPure = P;
1205 if (P)
1206 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1207 Parent->markedVirtualFunctionPure();
1208}
1209
Douglas Gregor16618f22009-09-12 00:17:51 +00001210bool FunctionDecl::isMain() const {
1211 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001212 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001213 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001214 getIdentifier() && getIdentifier()->isStr("main");
1215}
1216
Douglas Gregor16618f22009-09-12 00:17:51 +00001217bool FunctionDecl::isExternC() const {
1218 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001219 // In C, any non-static, non-overloadable function has external
1220 // linkage.
1221 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001222 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001223
Mike Stump11289f42009-09-09 15:08:12 +00001224 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001225 DC = DC->getParent()) {
1226 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1227 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001228 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001229 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001230
1231 break;
1232 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001233
1234 if (DC->isRecord())
1235 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001236 }
1237
Douglas Gregorbff62032010-10-21 16:57:46 +00001238 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001239}
1240
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001241bool FunctionDecl::isGlobal() const {
1242 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1243 return Method->isStatic();
1244
John McCall8e7d6562010-08-26 03:08:43 +00001245 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001246 return false;
1247
Mike Stump11289f42009-09-09 15:08:12 +00001248 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001249 DC->isNamespace();
1250 DC = DC->getParent()) {
1251 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1252 if (!Namespace->getDeclName())
1253 return false;
1254 break;
1255 }
1256 }
1257
1258 return true;
1259}
1260
Sebastian Redl833ef452010-01-26 22:01:41 +00001261void
1262FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1263 redeclarable_base::setPreviousDeclaration(PrevDecl);
1264
1265 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1266 FunctionTemplateDecl *PrevFunTmpl
1267 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1268 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1269 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1270 }
1271}
1272
1273const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1274 return getFirstDeclaration();
1275}
1276
1277FunctionDecl *FunctionDecl::getCanonicalDecl() {
1278 return getFirstDeclaration();
1279}
1280
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001281/// \brief Returns a value indicating whether this function
1282/// corresponds to a builtin function.
1283///
1284/// The function corresponds to a built-in function if it is
1285/// declared at translation scope or within an extern "C" block and
1286/// its name matches with the name of a builtin. The returned value
1287/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001288/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001289/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001290unsigned FunctionDecl::getBuiltinID() const {
1291 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001292 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1293 return 0;
1294
1295 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1296 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1297 return BuiltinID;
1298
1299 // This function has the name of a known C library
1300 // function. Determine whether it actually refers to the C library
1301 // function or whether it just has the same name.
1302
Douglas Gregora908e7f2009-02-17 03:23:10 +00001303 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001304 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001305 return 0;
1306
Douglas Gregore711f702009-02-14 18:57:46 +00001307 // If this function is at translation-unit scope and we're not in
1308 // C++, it refers to the C library function.
1309 if (!Context.getLangOptions().CPlusPlus &&
1310 getDeclContext()->isTranslationUnit())
1311 return BuiltinID;
1312
1313 // If the function is in an extern "C" linkage specification and is
1314 // not marked "overloadable", it's the real function.
1315 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001316 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001317 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001318 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001319 return BuiltinID;
1320
1321 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001322 return 0;
1323}
1324
1325
Chris Lattner47c0d002009-04-25 06:03:53 +00001326/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001327/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001328/// after it has been created.
1329unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001330 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001331 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001332 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001333 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001334
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001335}
1336
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001337void FunctionDecl::setParams(ASTContext &C,
1338 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001339 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001340 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001341
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001342 // Zero params -> null pointer.
1343 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001344 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001345 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001346 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001347
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001348 // Update source range. The check below allows us to set EndRangeLoc before
1349 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001350 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001351 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001352 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001353}
Chris Lattner41943152007-01-25 04:52:46 +00001354
Chris Lattner58258242008-04-10 02:22:51 +00001355/// getMinRequiredArguments - Returns the minimum number of arguments
1356/// needed to call this function. This may be fewer than the number of
1357/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001358/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001359unsigned FunctionDecl::getMinRequiredArguments() const {
1360 unsigned NumRequiredArgs = getNumParams();
1361 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001362 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001363 --NumRequiredArgs;
1364
1365 return NumRequiredArgs;
1366}
1367
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001368bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001369 // FIXME: This is not enough. Consider:
1370 //
1371 // inline void f();
1372 // void f() { }
1373 //
1374 // f is inlined, but does not have inline specified.
1375 // To fix this we should add an 'inline' flag to FunctionDecl.
1376 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001377 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001378
1379 if (isa<CXXMethodDecl>(this)) {
1380 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1381 return true;
1382 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001383
1384 switch (getTemplateSpecializationKind()) {
1385 case TSK_Undeclared:
1386 case TSK_ExplicitSpecialization:
1387 return false;
1388
1389 case TSK_ImplicitInstantiation:
1390 case TSK_ExplicitInstantiationDeclaration:
1391 case TSK_ExplicitInstantiationDefinition:
1392 // Handle below.
1393 break;
1394 }
1395
1396 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001397 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001398 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001399 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001400
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001401 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001402 return PatternDecl->isInlined();
1403
1404 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001405}
1406
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001407/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001408/// definition will be externally visible.
1409///
1410/// Inline function definitions are always available for inlining optimizations.
1411/// However, depending on the language dialect, declaration specifiers, and
1412/// attributes, the definition of an inline function may or may not be
1413/// "externally" visible to other translation units in the program.
1414///
1415/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001416/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001417/// inline definition becomes externally visible (C99 6.7.4p6).
1418///
1419/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1420/// definition, we use the GNU semantics for inline, which are nearly the
1421/// opposite of C99 semantics. In particular, "inline" by itself will create
1422/// an externally visible symbol, but "extern inline" will not create an
1423/// externally visible symbol.
1424bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1425 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001426 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001427 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001428
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001429 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001430 // GNU inline semantics. Based on a number of examples, we came up with the
1431 // following heuristic: if the "inline" keyword is present on a
1432 // declaration of the function but "extern" is not present on that
1433 // declaration, then the symbol is externally visible. Otherwise, the GNU
1434 // "extern inline" semantics applies and the symbol is not externally
1435 // visible.
1436 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1437 Redecl != RedeclEnd;
1438 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001439 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001440 return true;
1441 }
1442
1443 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001444 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001445 }
1446
1447 // C99 6.7.4p6:
1448 // [...] If all of the file scope declarations for a function in a
1449 // translation unit include the inline function specifier without extern,
1450 // then the definition in that translation unit is an inline definition.
1451 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1452 Redecl != RedeclEnd;
1453 ++Redecl) {
1454 // Only consider file-scope declarations in this test.
1455 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1456 continue;
1457
John McCall8e7d6562010-08-26 03:08:43 +00001458 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001459 return true; // Not an inline definition
1460 }
1461
1462 // C99 6.7.4p6:
1463 // An inline definition does not provide an external definition for the
1464 // function, and does not forbid an external definition in another
1465 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001466 return false;
1467}
1468
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001469/// getOverloadedOperator - Which C++ overloaded operator this
1470/// function represents, if any.
1471OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001472 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1473 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001474 else
1475 return OO_None;
1476}
1477
Alexis Huntc88db062010-01-13 09:01:02 +00001478/// getLiteralIdentifier - The literal suffix identifier this function
1479/// represents, if any.
1480const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1481 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1482 return getDeclName().getCXXLiteralIdentifier();
1483 else
1484 return 0;
1485}
1486
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001487FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1488 if (TemplateOrSpecialization.isNull())
1489 return TK_NonTemplate;
1490 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1491 return TK_FunctionTemplate;
1492 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1493 return TK_MemberSpecialization;
1494 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1495 return TK_FunctionTemplateSpecialization;
1496 if (TemplateOrSpecialization.is
1497 <DependentFunctionTemplateSpecializationInfo*>())
1498 return TK_DependentFunctionTemplateSpecialization;
1499
1500 assert(false && "Did we miss a TemplateOrSpecialization type?");
1501 return TK_NonTemplate;
1502}
1503
Douglas Gregord801b062009-10-07 23:56:10 +00001504FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001505 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001506 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1507
1508 return 0;
1509}
1510
Douglas Gregor06db9f52009-10-12 20:18:28 +00001511MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1512 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1513}
1514
Douglas Gregord801b062009-10-07 23:56:10 +00001515void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001516FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1517 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001518 TemplateSpecializationKind TSK) {
1519 assert(TemplateOrSpecialization.isNull() &&
1520 "Member function is already a specialization");
1521 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001522 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001523 TemplateOrSpecialization = Info;
1524}
1525
Douglas Gregorafca3b42009-10-27 20:53:28 +00001526bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001527 // If the function is invalid, it can't be implicitly instantiated.
1528 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001529 return false;
1530
1531 switch (getTemplateSpecializationKind()) {
1532 case TSK_Undeclared:
1533 case TSK_ExplicitSpecialization:
1534 case TSK_ExplicitInstantiationDefinition:
1535 return false;
1536
1537 case TSK_ImplicitInstantiation:
1538 return true;
1539
1540 case TSK_ExplicitInstantiationDeclaration:
1541 // Handled below.
1542 break;
1543 }
1544
1545 // Find the actual template from which we will instantiate.
1546 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001547 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001548 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001549 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001550
1551 // C++0x [temp.explicit]p9:
1552 // Except for inline functions, other explicit instantiation declarations
1553 // have the effect of suppressing the implicit instantiation of the entity
1554 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001555 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001556 return true;
1557
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001558 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001559}
1560
1561FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1562 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1563 while (Primary->getInstantiatedFromMemberTemplate()) {
1564 // If we have hit a point where the user provided a specialization of
1565 // this template, we're done looking.
1566 if (Primary->isMemberSpecialization())
1567 break;
1568
1569 Primary = Primary->getInstantiatedFromMemberTemplate();
1570 }
1571
1572 return Primary->getTemplatedDecl();
1573 }
1574
1575 return getInstantiatedFromMemberFunction();
1576}
1577
Douglas Gregor70d83e22009-06-29 17:30:29 +00001578FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001579 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001580 = TemplateOrSpecialization
1581 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001582 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001583 }
1584 return 0;
1585}
1586
1587const TemplateArgumentList *
1588FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001589 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001590 = TemplateOrSpecialization
1591 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001592 return Info->TemplateArguments;
1593 }
1594 return 0;
1595}
1596
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001597const TemplateArgumentListInfo *
1598FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1599 if (FunctionTemplateSpecializationInfo *Info
1600 = TemplateOrSpecialization
1601 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1602 return Info->TemplateArgumentsAsWritten;
1603 }
1604 return 0;
1605}
1606
Mike Stump11289f42009-09-09 15:08:12 +00001607void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001608FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1609 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001610 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001611 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001612 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001613 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1614 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001615 assert(TSK != TSK_Undeclared &&
1616 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001617 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001618 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001619 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001620 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1621 TemplateArgs,
1622 TemplateArgsAsWritten,
1623 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001624 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001625
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001626 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001627 // function template specializations.
1628 if (InsertPos)
1629 Template->getSpecializations().InsertNode(Info, InsertPos);
1630 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001631 // Try to insert the new node. If there is an existing node, leave it, the
1632 // set will contain the canonical decls while
1633 // FunctionTemplateDecl::findSpecialization will return
1634 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001635 FunctionTemplateSpecializationInfo *Existing
1636 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001637 (void)Existing;
1638 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1639 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001640 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001641}
1642
John McCallb9c78482010-04-08 09:05:18 +00001643void
1644FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1645 const UnresolvedSetImpl &Templates,
1646 const TemplateArgumentListInfo &TemplateArgs) {
1647 assert(TemplateOrSpecialization.isNull());
1648 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1649 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001650 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001651 void *Buffer = Context.Allocate(Size);
1652 DependentFunctionTemplateSpecializationInfo *Info =
1653 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1654 TemplateArgs);
1655 TemplateOrSpecialization = Info;
1656}
1657
1658DependentFunctionTemplateSpecializationInfo::
1659DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1660 const TemplateArgumentListInfo &TArgs)
1661 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1662
1663 d.NumTemplates = Ts.size();
1664 d.NumArgs = TArgs.size();
1665
1666 FunctionTemplateDecl **TsArray =
1667 const_cast<FunctionTemplateDecl**>(getTemplates());
1668 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1669 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1670
1671 TemplateArgumentLoc *ArgsArray =
1672 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1673 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1674 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1675}
1676
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001677TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001678 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001679 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001680 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001681 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001682 if (FTSInfo)
1683 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001684
Douglas Gregord801b062009-10-07 23:56:10 +00001685 MemberSpecializationInfo *MSInfo
1686 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1687 if (MSInfo)
1688 return MSInfo->getTemplateSpecializationKind();
1689
1690 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001691}
1692
Mike Stump11289f42009-09-09 15:08:12 +00001693void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001694FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1695 SourceLocation PointOfInstantiation) {
1696 if (FunctionTemplateSpecializationInfo *FTSInfo
1697 = TemplateOrSpecialization.dyn_cast<
1698 FunctionTemplateSpecializationInfo*>()) {
1699 FTSInfo->setTemplateSpecializationKind(TSK);
1700 if (TSK != TSK_ExplicitSpecialization &&
1701 PointOfInstantiation.isValid() &&
1702 FTSInfo->getPointOfInstantiation().isInvalid())
1703 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1704 } else if (MemberSpecializationInfo *MSInfo
1705 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1706 MSInfo->setTemplateSpecializationKind(TSK);
1707 if (TSK != TSK_ExplicitSpecialization &&
1708 PointOfInstantiation.isValid() &&
1709 MSInfo->getPointOfInstantiation().isInvalid())
1710 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1711 } else
1712 assert(false && "Function cannot have a template specialization kind");
1713}
1714
1715SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001716 if (FunctionTemplateSpecializationInfo *FTSInfo
1717 = TemplateOrSpecialization.dyn_cast<
1718 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001719 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001720 else if (MemberSpecializationInfo *MSInfo
1721 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001722 return MSInfo->getPointOfInstantiation();
1723
1724 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001725}
1726
Douglas Gregor6411b922009-09-11 20:15:17 +00001727bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001728 if (Decl::isOutOfLine())
1729 return true;
1730
1731 // If this function was instantiated from a member function of a
1732 // class template, check whether that member function was defined out-of-line.
1733 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1734 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001735 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001736 return Definition->isOutOfLine();
1737 }
1738
1739 // If this function was instantiated from a function template,
1740 // check whether that function template was defined out-of-line.
1741 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1742 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001743 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001744 return Definition->isOutOfLine();
1745 }
1746
1747 return false;
1748}
1749
Chris Lattner59a25942008-03-31 00:36:02 +00001750//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001751// FieldDecl Implementation
1752//===----------------------------------------------------------------------===//
1753
1754FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1755 IdentifierInfo *Id, QualType T,
1756 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1757 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1758}
1759
1760bool FieldDecl::isAnonymousStructOrUnion() const {
1761 if (!isImplicit() || getDeclName())
1762 return false;
1763
1764 if (const RecordType *Record = getType()->getAs<RecordType>())
1765 return Record->getDecl()->isAnonymousStructOrUnion();
1766
1767 return false;
1768}
1769
1770//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001771// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001772//===----------------------------------------------------------------------===//
1773
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001774SourceLocation TagDecl::getOuterLocStart() const {
1775 return getTemplateOrInnerLocStart(this);
1776}
1777
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001778SourceRange TagDecl::getSourceRange() const {
1779 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001780 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001781}
1782
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001783TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001784 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001785}
1786
Douglas Gregora72a4e32010-05-19 18:39:18 +00001787void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1788 TypedefDeclOrQualifier = TDD;
1789 if (TypeForDecl)
1790 TypeForDecl->ClearLinkageCache();
1791}
1792
Douglas Gregordee1be82009-01-17 00:42:38 +00001793void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001794 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001795
1796 if (isa<CXXRecordDecl>(this)) {
1797 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1798 struct CXXRecordDecl::DefinitionData *Data =
1799 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001800 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1801 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001802 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001803}
1804
1805void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001806 assert((!isa<CXXRecordDecl>(this) ||
1807 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1808 "definition completed but not started");
1809
Douglas Gregordee1be82009-01-17 00:42:38 +00001810 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001811 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001812
1813 if (ASTMutationListener *L = getASTMutationListener())
1814 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001815}
1816
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001817TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001818 if (isDefinition())
1819 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001820 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1821 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001822
1823 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001824 R != REnd; ++R)
1825 if (R->isDefinition())
1826 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001827
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001828 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001829}
1830
John McCall3e11ebe2010-03-15 10:12:16 +00001831void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1832 SourceRange QualifierRange) {
1833 if (Qualifier) {
1834 // Make sure the extended qualifier info is allocated.
1835 if (!hasExtInfo())
1836 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1837 // Set qualifier info.
1838 getExtInfo()->NNS = Qualifier;
1839 getExtInfo()->NNSRange = QualifierRange;
1840 }
1841 else {
1842 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1843 assert(QualifierRange.isInvalid());
1844 if (hasExtInfo()) {
1845 getASTContext().Deallocate(getExtInfo());
1846 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1847 }
1848 }
1849}
1850
Ted Kremenek21475702008-09-05 17:16:31 +00001851//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001852// EnumDecl Implementation
1853//===----------------------------------------------------------------------===//
1854
1855EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1856 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001857 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1858 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1859 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001860 C.getTypeDeclType(Enum, PrevDecl);
1861 return Enum;
1862}
1863
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001864EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001865 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1866 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001867}
1868
Douglas Gregord5058122010-02-11 01:19:42 +00001869void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001870 QualType NewPromotionType,
1871 unsigned NumPositiveBits,
1872 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001873 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001874 if (!IntegerType)
1875 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001876 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001877 setNumPositiveBits(NumPositiveBits);
1878 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001879 TagDecl::completeDefinition();
1880}
1881
1882//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001883// RecordDecl Implementation
1884//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001885
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001886RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001887 IdentifierInfo *Id, RecordDecl *PrevDecl,
1888 SourceLocation TKL)
1889 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001890 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001891 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001892 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001893 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001894 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001895}
1896
1897RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001898 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001899 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001900
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001901 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001902 C.getTypeDeclType(R, PrevDecl);
1903 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001904}
1905
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001906RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1907 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1908 SourceLocation());
1909}
1910
Douglas Gregordfcad112009-03-25 15:59:44 +00001911bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001912 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001913 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1914}
1915
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001916RecordDecl::field_iterator RecordDecl::field_begin() const {
1917 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1918 LoadFieldsFromExternalStorage();
1919
1920 return field_iterator(decl_iterator(FirstDecl));
1921}
1922
Douglas Gregor91f84212008-12-11 16:49:14 +00001923/// completeDefinition - Notes that the definition of this type is now
1924/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001925void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001926 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001927 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001928}
Steve Naroffcc321422007-03-26 23:09:51 +00001929
John McCall61925b02010-05-21 01:17:40 +00001930ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1931 // Force the decl chain to come into existence properly.
1932 if (!getNextDeclInContext()) getParent()->decls_begin();
1933
1934 assert(isAnonymousStructOrUnion());
1935 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1936 assert(D->getType()->isRecordType());
1937 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1938 return D;
1939}
1940
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001941void RecordDecl::LoadFieldsFromExternalStorage() const {
1942 ExternalASTSource *Source = getASTContext().getExternalSource();
1943 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1944
1945 // Notify that we have a RecordDecl doing some initialization.
1946 ExternalASTSource::Deserializing TheFields(Source);
1947
1948 llvm::SmallVector<Decl*, 64> Decls;
1949 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1950 return;
1951
1952#ifndef NDEBUG
1953 // Check that all decls we got were FieldDecls.
1954 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1955 assert(isa<FieldDecl>(Decls[i]));
1956#endif
1957
1958 LoadedFieldsFromExternalStorage = true;
1959
1960 if (Decls.empty())
1961 return;
1962
1963 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1964}
1965
Steve Naroff415d3d52008-10-08 17:01:13 +00001966//===----------------------------------------------------------------------===//
1967// BlockDecl Implementation
1968//===----------------------------------------------------------------------===//
1969
Douglas Gregord5058122010-02-11 01:19:42 +00001970void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001971 unsigned NParms) {
1972 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001973
Steve Naroffc4b30e52009-03-13 16:56:44 +00001974 // Zero params -> null pointer.
1975 if (NParms) {
1976 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001977 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001978 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1979 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1980 }
1981}
1982
1983unsigned BlockDecl::getNumParams() const {
1984 return NumParams;
1985}
Sebastian Redl833ef452010-01-26 22:01:41 +00001986
1987
1988//===----------------------------------------------------------------------===//
1989// Other Decl Allocation/Deallocation Method Implementations
1990//===----------------------------------------------------------------------===//
1991
1992TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1993 return new (C) TranslationUnitDecl(C);
1994}
1995
1996NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1997 SourceLocation L, IdentifierInfo *Id) {
1998 return new (C) NamespaceDecl(DC, L, Id);
1999}
2000
Douglas Gregor417e87c2010-10-27 19:49:05 +00002001NamespaceDecl *NamespaceDecl::getNextNamespace() {
2002 return dyn_cast_or_null<NamespaceDecl>(
2003 NextNamespace.get(getASTContext().getExternalSource()));
2004}
2005
Sebastian Redl833ef452010-01-26 22:01:41 +00002006ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2007 SourceLocation L, IdentifierInfo *Id, QualType T) {
2008 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2009}
2010
2011FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002012 const DeclarationNameInfo &NameInfo,
2013 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002014 StorageClass S, StorageClass SCAsWritten,
2015 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002016 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002017 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00002018 New->HasWrittenPrototype = hasWrittenPrototype;
2019 return New;
2020}
2021
2022BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2023 return new (C) BlockDecl(DC, L);
2024}
2025
2026EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2027 SourceLocation L,
2028 IdentifierInfo *Id, QualType T,
2029 Expr *E, const llvm::APSInt &V) {
2030 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2031}
2032
Douglas Gregorbe996932010-09-01 20:41:53 +00002033SourceRange EnumConstantDecl::getSourceRange() const {
2034 SourceLocation End = getLocation();
2035 if (Init)
2036 End = Init->getLocEnd();
2037 return SourceRange(getLocation(), End);
2038}
2039
Sebastian Redl833ef452010-01-26 22:01:41 +00002040TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2041 SourceLocation L, IdentifierInfo *Id,
2042 TypeSourceInfo *TInfo) {
2043 return new (C) TypedefDecl(DC, L, Id, TInfo);
2044}
2045
Sebastian Redl833ef452010-01-26 22:01:41 +00002046FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2047 SourceLocation L,
2048 StringLiteral *Str) {
2049 return new (C) FileScopeAsmDecl(DC, L, Str);
2050}