blob: c88f79b29fafff53825713f70035d5567e47b04a [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) {
John McCall457a04e2010-10-22 21:05:15 +0000167 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
168 TArgs.flat_size());
John McCall8823c652010-08-13 08:35:10 +0000169}
170
John McCall033caa52010-10-29 00:29:13 +0000171/// getLVForDecl - Get the cached linkage and visibility for the given
172/// declaration.
John McCall07072662010-11-02 01:45:15 +0000173static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
John McCall033caa52010-10-29 00:29:13 +0000174
John McCall07072662010-11-02 01:45:15 +0000175static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000176 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000177 "Not a name having namespace scope");
178 ASTContext &Context = D->getASTContext();
179
180 // C++ [basic.link]p3:
181 // A name having namespace scope (3.3.6) has internal linkage if it
182 // is the name of
183 // - an object, reference, function or function template that is
184 // explicitly declared static; or,
185 // (This bullet corresponds to C99 6.2.2p3.)
186 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
187 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000188 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000189 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000190
191 // - an object or reference that is explicitly declared const
192 // and neither explicitly declared extern nor previously
193 // declared to have external linkage; or
194 // (there is no equivalent in C99)
195 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000196 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000197 Var->getStorageClass() != SC_Extern &&
198 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000199 bool FoundExtern = false;
200 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
201 PrevVar && !FoundExtern;
202 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000203 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000204 FoundExtern = true;
205
206 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000207 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000208 }
209 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000210 // C++ [temp]p4:
211 // A non-member function template can have internal linkage; any
212 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000213 const FunctionDecl *Function = 0;
214 if (const FunctionTemplateDecl *FunTmpl
215 = dyn_cast<FunctionTemplateDecl>(D))
216 Function = FunTmpl->getTemplatedDecl();
217 else
218 Function = cast<FunctionDecl>(D);
219
220 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000221 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000222 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000223 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
224 // - a data member of an anonymous union.
225 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000226 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000227 }
228
John McCall457a04e2010-10-22 21:05:15 +0000229 if (D->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000230 return LinkageInfo::uniqueExternal();
John McCallb7139c42010-10-28 04:18:25 +0000231
John McCall457a04e2010-10-22 21:05:15 +0000232 // Set up the defaults.
233
234 // C99 6.2.2p5:
235 // If the declaration of an identifier for an object has file
236 // scope and no storage-class specifier, its linkage is
237 // external.
John McCallc273f242010-10-30 11:50:40 +0000238 LinkageInfo LV;
239
John McCall07072662010-11-02 01:45:15 +0000240 if (F.ConsiderVisibilityAttributes) {
241 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
242 LV.setVisibility(GetVisibilityFromAttr(VA), true);
243 F.ConsiderGlobalVisibility = false;
244 }
John McCallc273f242010-10-30 11:50:40 +0000245 }
John McCall457a04e2010-10-22 21:05:15 +0000246
Douglas Gregorf73b2822009-11-25 22:24:25 +0000247 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000248
Douglas Gregorf73b2822009-11-25 22:24:25 +0000249 // A name having namespace scope has external linkage if it is the
250 // name of
251 //
252 // - an object or reference, unless it has internal linkage; or
253 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000254 // GCC applies the following optimization to variables and static
255 // data members, but not to functions:
256 //
John McCall457a04e2010-10-22 21:05:15 +0000257 // Modify the variable's LV by the LV of its type unless this is
258 // C or extern "C". This follows from [basic.link]p9:
259 // A type without linkage shall not be used as the type of a
260 // variable or function with external linkage unless
261 // - the entity has C language linkage, or
262 // - the entity is declared within an unnamed namespace, or
263 // - the entity is not used or is defined in the same
264 // translation unit.
265 // and [basic.link]p10:
266 // ...the types specified by all declarations referring to a
267 // given variable or function shall be identical...
268 // C does not have an equivalent rule.
269 //
John McCall5fe84122010-10-26 04:59:26 +0000270 // Ignore this if we've got an explicit attribute; the user
271 // probably knows what they're doing.
272 //
John McCall457a04e2010-10-22 21:05:15 +0000273 // Note that we don't want to make the variable non-external
274 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000275 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000276 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
277 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000278 return LinkageInfo::uniqueExternal();
279 if (!LV.visibilityExplicit())
280 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000281 }
282
John McCall23032652010-11-02 18:38:13 +0000283 if (Var->getStorageClass() == SC_PrivateExtern)
284 LV.setVisibility(HiddenVisibility, true);
285
Douglas Gregorf73b2822009-11-25 22:24:25 +0000286 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000287 (Var->getStorageClass() == SC_Extern ||
288 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000289
Douglas Gregorf73b2822009-11-25 22:24:25 +0000290 // C99 6.2.2p4:
291 // For an identifier declared with the storage-class specifier
292 // extern in a scope in which a prior declaration of that
293 // identifier is visible, if the prior declaration specifies
294 // internal or external linkage, the linkage of the identifier
295 // at the later declaration is the same as the linkage
296 // specified at the prior declaration. If no prior declaration
297 // is visible, or if the prior declaration specifies no
298 // linkage, then the identifier has external linkage.
299 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000300 LinkageInfo PrevLV = PrevVar->getLinkageAndVisibility();
301 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
302 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000303 }
304 }
305
Douglas Gregorf73b2822009-11-25 22:24:25 +0000306 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000307 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000308 // In theory, we can modify the function's LV by the LV of its
309 // type unless it has C linkage (see comment above about variables
310 // for justification). In practice, GCC doesn't do this, so it's
311 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000312
John McCall23032652010-11-02 18:38:13 +0000313 if (Function->getStorageClass() == SC_PrivateExtern)
314 LV.setVisibility(HiddenVisibility, true);
315
Douglas Gregorf73b2822009-11-25 22:24:25 +0000316 // C99 6.2.2p5:
317 // If the declaration of an identifier for a function has no
318 // storage-class specifier, its linkage is determined exactly
319 // as if it were declared with the storage-class specifier
320 // extern.
321 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000322 (Function->getStorageClass() == SC_Extern ||
323 Function->getStorageClass() == SC_PrivateExtern ||
324 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000325 // C99 6.2.2p4:
326 // For an identifier declared with the storage-class specifier
327 // extern in a scope in which a prior declaration of that
328 // identifier is visible, if the prior declaration specifies
329 // internal or external linkage, the linkage of the identifier
330 // at the later declaration is the same as the linkage
331 // specified at the prior declaration. If no prior declaration
332 // is visible, or if the prior declaration specifies no
333 // linkage, then the identifier has external linkage.
334 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000335 LinkageInfo PrevLV = PrevFunc->getLinkageAndVisibility();
336 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
337 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000338 }
339 }
340
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000341 if (FunctionTemplateSpecializationInfo *SpecInfo
342 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000343 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
344 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000345 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCallc273f242010-10-30 11:50:40 +0000346 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000347 }
348
Douglas Gregorf73b2822009-11-25 22:24:25 +0000349 // - a named class (Clause 9), or an unnamed class defined in a
350 // typedef declaration in which the class has the typedef name
351 // for linkage purposes (7.1.3); or
352 // - a named enumeration (7.2), or an unnamed enumeration
353 // defined in a typedef declaration in which the enumeration
354 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000355 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
356 // Unnamed tags have no linkage.
357 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000358 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000359
John McCall457a04e2010-10-22 21:05:15 +0000360 // If this is a class template specialization, consider the
361 // linkage of the template and template arguments.
362 if (const ClassTemplateSpecializationDecl *Spec
363 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000364 // From the template.
365 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
366 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000367
John McCall457a04e2010-10-22 21:05:15 +0000368 // The arguments at which the template was instantiated.
369 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
John McCallc273f242010-10-30 11:50:40 +0000370 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000371 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000372
John McCall5fe84122010-10-26 04:59:26 +0000373 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000374 if (F.ConsiderGlobalVisibility)
375 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000376 (Context.getLangOptions().CPlusPlus &&
377 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000378
Douglas Gregorf73b2822009-11-25 22:24:25 +0000379 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000380 } else if (isa<EnumConstantDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000381 LinkageInfo EnumLV =
John McCall457a04e2010-10-22 21:05:15 +0000382 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
John McCallc273f242010-10-30 11:50:40 +0000383 if (!isExternalLinkage(EnumLV.linkage()))
384 return LinkageInfo::none();
385 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000386
387 // - a template, unless it is a function template that has
388 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000389 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000390 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000391
Douglas Gregorf73b2822009-11-25 22:24:25 +0000392 // - a namespace (7.3), unless it is declared within an unnamed
393 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000394 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
395 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000396
John McCall457a04e2010-10-22 21:05:15 +0000397 // By extension, we assign external linkage to Objective-C
398 // interfaces.
399 } else if (isa<ObjCInterfaceDecl>(D)) {
400 // fallout
401
402 // Everything not covered here has no linkage.
403 } else {
John McCallc273f242010-10-30 11:50:40 +0000404 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000405 }
406
407 // If we ended up with non-external linkage, visibility should
408 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000409 if (LV.linkage() != ExternalLinkage)
410 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000411
412 // If we didn't end up with hidden visibility, consider attributes
413 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000414 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000415 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000416
417 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000418}
419
John McCall07072662010-11-02 01:45:15 +0000420static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000421 // Only certain class members have linkage. Note that fields don't
422 // really have linkage, but it's convenient to say they do for the
423 // purposes of calculating linkage of pointer-to-data-member
424 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000425 if (!(isa<CXXMethodDecl>(D) ||
426 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000427 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000428 (isa<TagDecl>(D) &&
429 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000430 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000431
John McCall07072662010-11-02 01:45:15 +0000432 LinkageInfo LV;
433
434 // The flags we're going to use to compute the class's visibility.
435 LVFlags ClassF = F;
436
437 // If we have an explicit visibility attribute, merge that in.
438 if (F.ConsiderVisibilityAttributes) {
439 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
440 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
441
442 // Ignore global visibility later, but not this attribute.
443 F.ConsiderGlobalVisibility = false;
444
445 // Ignore both global visibility and attributes when computing our
446 // parent's visibility.
447 ClassF = F.onlyTemplateVisibility();
448 }
449 }
John McCallc273f242010-10-30 11:50:40 +0000450
451 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000452 // linkage.
453 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
454 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000455 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000456
457 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000458 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000459 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000460
John McCall8823c652010-08-13 08:35:10 +0000461 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000462 TemplateSpecializationKind TSK = TSK_Undeclared;
463
John McCall457a04e2010-10-22 21:05:15 +0000464 // If this is a method template specialization, use the linkage for
465 // the template parameters and arguments.
466 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000467 = MD->getTemplateSpecializationInfo()) {
John McCallc273f242010-10-30 11:50:40 +0000468 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments));
469 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000470 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000471
472 TSK = Spec->getTemplateSpecializationKind();
473 } else if (MemberSpecializationInfo *MSI =
474 MD->getMemberSpecializationInfo()) {
475 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000476 }
477
John McCall37bb6c92010-10-29 22:22:43 +0000478 // If we're paying attention to global visibility, apply
479 // -finline-visibility-hidden if this is an inline method.
480 //
John McCallc273f242010-10-30 11:50:40 +0000481 // Note that ConsiderGlobalVisibility doesn't yet have information
482 // about whether containing classes have visibility attributes,
483 // and that's intentional.
484 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000485 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000486 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
487 // InlineVisibilityHidden only applies to definitions, and
488 // isInlined() only gives meaningful answers on definitions
489 // anyway.
490 const FunctionDecl *Def = 0;
491 if (MD->hasBody(Def) && Def->isInlined())
492 LV.setVisibility(HiddenVisibility);
493 }
John McCall457a04e2010-10-22 21:05:15 +0000494
John McCall37bb6c92010-10-29 22:22:43 +0000495 // Note that in contrast to basically every other situation, we
496 // *do* apply -fvisibility to method declarations.
497
498 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000499 if (const ClassTemplateSpecializationDecl *Spec
500 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
501 // Merge template argument/parameter information for member
502 // class template specializations.
John McCallc273f242010-10-30 11:50:40 +0000503 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs()));
504 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000505 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000506 }
507
John McCall37bb6c92010-10-29 22:22:43 +0000508 // Static data members.
509 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000510 // Modify the variable's linkage by its type, but ignore the
511 // type's visibility unless it's a definition.
512 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
513 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000514 LV.mergeLinkage(UniqueExternalLinkage);
515 if (!LV.visibilityExplicit())
516 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000517 }
518
John McCall07072662010-11-02 01:45:15 +0000519 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000520
521 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000522 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000523 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000524 }
525
John McCall457a04e2010-10-22 21:05:15 +0000526 return LV;
John McCall8823c652010-08-13 08:35:10 +0000527}
528
John McCallc273f242010-10-30 11:50:40 +0000529LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCall07072662010-11-02 01:45:15 +0000530 return getLVForDecl(this, LVFlags());
John McCall033caa52010-10-29 00:29:13 +0000531}
Ted Kremenek926d8602010-04-20 23:15:35 +0000532
John McCall07072662010-11-02 01:45:15 +0000533static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000534 // Objective-C: treat all Objective-C declarations as having external
535 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000536 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000537 default:
538 break;
John McCall457a04e2010-10-22 21:05:15 +0000539 case Decl::TemplateTemplateParm: // count these as external
540 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000541 case Decl::ObjCAtDefsField:
542 case Decl::ObjCCategory:
543 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000544 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000545 case Decl::ObjCForwardProtocol:
546 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000547 case Decl::ObjCMethod:
548 case Decl::ObjCProperty:
549 case Decl::ObjCPropertyImpl:
550 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000551 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000552 }
553
Douglas Gregorf73b2822009-11-25 22:24:25 +0000554 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000555 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000556 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000557
558 // C++ [basic.link]p5:
559 // In addition, a member function, static data member, a named
560 // class or enumeration of class scope, or an unnamed class or
561 // enumeration defined in a class-scope typedef declaration such
562 // that the class or enumeration has the typedef name for linkage
563 // purposes (7.1.3), has external linkage if the name of the class
564 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000565 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000566 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000567
568 // C++ [basic.link]p6:
569 // The name of a function declared in block scope and the name of
570 // an object declared by a block scope extern declaration have
571 // linkage. If there is a visible declaration of an entity with
572 // linkage having the same name and type, ignoring entities
573 // declared outside the innermost enclosing namespace scope, the
574 // block scope declaration declares that same entity and receives
575 // the linkage of the previous declaration. If there is more than
576 // one such matching entity, the program is ill-formed. Otherwise,
577 // if no matching entity is found, the block scope entity receives
578 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000579 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
580 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000581 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000582 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000583
John McCallc273f242010-10-30 11:50:40 +0000584 LinkageInfo LV;
John McCallb7139c42010-10-28 04:18:25 +0000585 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCallc273f242010-10-30 11:50:40 +0000586 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000587
588 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000589 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
590 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
591 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000592 }
593
594 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000595 }
596
John McCall033caa52010-10-29 00:29:13 +0000597 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000598 if (Var->getStorageClass() == SC_Extern ||
599 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000600 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000601 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000602
John McCallc273f242010-10-30 11:50:40 +0000603 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000604 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000605 LV.setVisibility(HiddenVisibility);
John McCallb7139c42010-10-28 04:18:25 +0000606 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCallc273f242010-10-30 11:50:40 +0000607 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000608
609 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000610 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
611 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
612 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000613 }
614
615 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000616 }
617 }
618
619 // C++ [basic.link]p6:
620 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000621 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000622}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000623
Douglas Gregor2ada0482009-02-04 17:27:36 +0000624std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000625 return getQualifiedNameAsString(getASTContext().getLangOptions());
626}
627
628std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000629 const DeclContext *Ctx = getDeclContext();
630
631 if (Ctx->isFunctionOrMethod())
632 return getNameAsString();
633
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000634 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
635 ContextsTy Contexts;
636
637 // Collect contexts.
638 while (Ctx && isa<NamedDecl>(Ctx)) {
639 Contexts.push_back(Ctx);
640 Ctx = Ctx->getParent();
641 };
642
643 std::string QualName;
644 llvm::raw_string_ostream OS(QualName);
645
646 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
647 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000648 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000649 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000650 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
651 std::string TemplateArgsStr
652 = TemplateSpecializationType::PrintTemplateArgumentList(
653 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000654 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000655 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000656 OS << Spec->getName() << TemplateArgsStr;
657 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000658 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000659 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000660 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000661 OS << ND;
662 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
663 if (!RD->getIdentifier())
664 OS << "<anonymous " << RD->getKindName() << '>';
665 else
666 OS << RD;
667 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000668 const FunctionProtoType *FT = 0;
669 if (FD->hasWrittenPrototype())
670 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
671
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000672 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000673 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000674 unsigned NumParams = FD->getNumParams();
675 for (unsigned i = 0; i < NumParams; ++i) {
676 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000677 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000678 std::string Param;
679 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000680 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000681 }
682
683 if (FT->isVariadic()) {
684 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000685 OS << ", ";
686 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000687 }
688 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000689 OS << ')';
690 } else {
691 OS << cast<NamedDecl>(*I);
692 }
693 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000694 }
695
John McCalla2a3f7d2010-03-16 21:48:18 +0000696 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000697 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000698 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000699 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000700
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000701 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000702}
703
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000704bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000705 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
706
Douglas Gregor889ceb72009-02-03 19:21:40 +0000707 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
708 // We want to keep it, unless it nominates same namespace.
709 if (getKind() == Decl::UsingDirective) {
710 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
711 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
712 }
Mike Stump11289f42009-09-09 15:08:12 +0000713
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000714 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
715 // For function declarations, we keep track of redeclarations.
716 return FD->getPreviousDeclaration() == OldD;
717
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000718 // For function templates, the underlying function declarations are linked.
719 if (const FunctionTemplateDecl *FunctionTemplate
720 = dyn_cast<FunctionTemplateDecl>(this))
721 if (const FunctionTemplateDecl *OldFunctionTemplate
722 = dyn_cast<FunctionTemplateDecl>(OldD))
723 return FunctionTemplate->getTemplatedDecl()
724 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000725
Steve Naroffc4173fa2009-02-22 19:35:57 +0000726 // For method declarations, we keep track of redeclarations.
727 if (isa<ObjCMethodDecl>(this))
728 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000729
John McCall9f3059a2009-10-09 21:13:30 +0000730 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
731 return true;
732
John McCall3f746822009-11-17 05:59:44 +0000733 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
734 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
735 cast<UsingShadowDecl>(OldD)->getTargetDecl();
736
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000737 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
738 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
739 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
740
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000741 // For non-function declarations, if the declarations are of the
742 // same kind then this must be a redeclaration, or semantic analysis
743 // would not have given us the new declaration.
744 return this->getKind() == OldD->getKind();
745}
746
Douglas Gregoreddf4332009-02-24 20:03:32 +0000747bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000748 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000749}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000750
Anders Carlsson6915bf62009-06-26 06:29:23 +0000751NamedDecl *NamedDecl::getUnderlyingDecl() {
752 NamedDecl *ND = this;
753 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000754 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000755 ND = UD->getTargetDecl();
756 else if (ObjCCompatibleAliasDecl *AD
757 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
758 return AD->getClassInterface();
759 else
760 return ND;
761 }
762}
763
John McCalla8ae2222010-04-06 21:38:20 +0000764bool NamedDecl::isCXXInstanceMember() const {
765 assert(isCXXClassMember() &&
766 "checking whether non-member is instance member");
767
768 const NamedDecl *D = this;
769 if (isa<UsingShadowDecl>(D))
770 D = cast<UsingShadowDecl>(D)->getTargetDecl();
771
772 if (isa<FieldDecl>(D))
773 return true;
774 if (isa<CXXMethodDecl>(D))
775 return cast<CXXMethodDecl>(D)->isInstance();
776 if (isa<FunctionTemplateDecl>(D))
777 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
778 ->getTemplatedDecl())->isInstance();
779 return false;
780}
781
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000782//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000783// DeclaratorDecl Implementation
784//===----------------------------------------------------------------------===//
785
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000786template <typename DeclT>
787static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
788 if (decl->getNumTemplateParameterLists() > 0)
789 return decl->getTemplateParameterList(0)->getTemplateLoc();
790 else
791 return decl->getInnerLocStart();
792}
793
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000794SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000795 TypeSourceInfo *TSI = getTypeSourceInfo();
796 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000797 return SourceLocation();
798}
799
John McCall3e11ebe2010-03-15 10:12:16 +0000800void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
801 SourceRange QualifierRange) {
802 if (Qualifier) {
803 // Make sure the extended decl info is allocated.
804 if (!hasExtInfo()) {
805 // Save (non-extended) type source info pointer.
806 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
807 // Allocate external info struct.
808 DeclInfo = new (getASTContext()) ExtInfo;
809 // Restore savedTInfo into (extended) decl info.
810 getExtInfo()->TInfo = savedTInfo;
811 }
812 // Set qualifier info.
813 getExtInfo()->NNS = Qualifier;
814 getExtInfo()->NNSRange = QualifierRange;
815 }
816 else {
817 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
818 assert(QualifierRange.isInvalid());
819 if (hasExtInfo()) {
820 // Save type source info pointer.
821 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
822 // Deallocate the extended decl info.
823 getASTContext().Deallocate(getExtInfo());
824 // Restore savedTInfo into (non-extended) decl info.
825 DeclInfo = savedTInfo;
826 }
827 }
828}
829
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000830SourceLocation DeclaratorDecl::getOuterLocStart() const {
831 return getTemplateOrInnerLocStart(this);
832}
833
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000834void
Douglas Gregor20527e22010-06-15 17:44:38 +0000835QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
836 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000837 TemplateParameterList **TPLists) {
838 assert((NumTPLists == 0 || TPLists != 0) &&
839 "Empty array of template parameters with positive size!");
840 assert((NumTPLists == 0 || NNS) &&
841 "Nonempty array of template parameters with no qualifier!");
842
843 // Free previous template parameters (if any).
844 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000845 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000846 TemplParamLists = 0;
847 NumTemplParamLists = 0;
848 }
849 // Set info on matched template parameter lists (if any).
850 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000851 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000852 NumTemplParamLists = NumTPLists;
853 for (unsigned i = NumTPLists; i-- > 0; )
854 TemplParamLists[i] = TPLists[i];
855 }
856}
857
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000858//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000859// VarDecl Implementation
860//===----------------------------------------------------------------------===//
861
Sebastian Redl833ef452010-01-26 22:01:41 +0000862const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
863 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000864 case SC_None: break;
865 case SC_Auto: return "auto"; break;
866 case SC_Extern: return "extern"; break;
867 case SC_PrivateExtern: return "__private_extern__"; break;
868 case SC_Register: return "register"; break;
869 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000870 }
871
872 assert(0 && "Invalid storage class");
873 return 0;
874}
875
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000876VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000877 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000878 StorageClass S, StorageClass SCAsWritten) {
879 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000880}
881
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000882SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000883 SourceLocation Start = getTypeSpecStartLoc();
884 if (Start.isInvalid())
885 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000886 return Start;
887}
888
889SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000890 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000891 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
892 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000893}
894
Sebastian Redl833ef452010-01-26 22:01:41 +0000895bool VarDecl::isExternC() const {
896 ASTContext &Context = getASTContext();
897 if (!Context.getLangOptions().CPlusPlus)
898 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000899 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000900 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
901
902 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
903 DC = DC->getParent()) {
904 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
905 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000906 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000907
908 break;
909 }
910
911 if (DC->isFunctionOrMethod())
912 return false;
913 }
914
915 return false;
916}
917
918VarDecl *VarDecl::getCanonicalDecl() {
919 return getFirstDeclaration();
920}
921
Sebastian Redl35351a92010-01-31 22:27:38 +0000922VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
923 // C++ [basic.def]p2:
924 // A declaration is a definition unless [...] it contains the 'extern'
925 // specifier or a linkage-specification and neither an initializer [...],
926 // it declares a static data member in a class declaration [...].
927 // C++ [temp.expl.spec]p15:
928 // An explicit specialization of a static data member of a template is a
929 // definition if the declaration includes an initializer; otherwise, it is
930 // a declaration.
931 if (isStaticDataMember()) {
932 if (isOutOfLine() && (hasInit() ||
933 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
934 return Definition;
935 else
936 return DeclarationOnly;
937 }
938 // C99 6.7p5:
939 // A definition of an identifier is a declaration for that identifier that
940 // [...] causes storage to be reserved for that object.
941 // Note: that applies for all non-file-scope objects.
942 // C99 6.9.2p1:
943 // If the declaration of an identifier for an object has file scope and an
944 // initializer, the declaration is an external definition for the identifier
945 if (hasInit())
946 return Definition;
947 // AST for 'extern "C" int foo;' is annotated with 'extern'.
948 if (hasExternalStorage())
949 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000950
John McCall8e7d6562010-08-26 03:08:43 +0000951 if (getStorageClassAsWritten() == SC_Extern ||
952 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000953 for (const VarDecl *PrevVar = getPreviousDeclaration();
954 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
955 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
956 return DeclarationOnly;
957 }
958 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000959 // C99 6.9.2p2:
960 // A declaration of an object that has file scope without an initializer,
961 // and without a storage class specifier or the scs 'static', constitutes
962 // a tentative definition.
963 // No such thing in C++.
964 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
965 return TentativeDefinition;
966
967 // What's left is (in C, block-scope) declarations without initializers or
968 // external storage. These are definitions.
969 return Definition;
970}
971
Sebastian Redl35351a92010-01-31 22:27:38 +0000972VarDecl *VarDecl::getActingDefinition() {
973 DefinitionKind Kind = isThisDeclarationADefinition();
974 if (Kind != TentativeDefinition)
975 return 0;
976
Chris Lattner48eb14d2010-06-14 18:31:46 +0000977 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000978 VarDecl *First = getFirstDeclaration();
979 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
980 I != E; ++I) {
981 Kind = (*I)->isThisDeclarationADefinition();
982 if (Kind == Definition)
983 return 0;
984 else if (Kind == TentativeDefinition)
985 LastTentative = *I;
986 }
987 return LastTentative;
988}
989
990bool VarDecl::isTentativeDefinitionNow() const {
991 DefinitionKind Kind = isThisDeclarationADefinition();
992 if (Kind != TentativeDefinition)
993 return false;
994
995 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
996 if ((*I)->isThisDeclarationADefinition() == Definition)
997 return false;
998 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000999 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001000}
1001
Sebastian Redl5ca79842010-02-01 20:16:42 +00001002VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001003 VarDecl *First = getFirstDeclaration();
1004 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1005 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001006 if ((*I)->isThisDeclarationADefinition() == Definition)
1007 return *I;
1008 }
1009 return 0;
1010}
1011
John McCall37bb6c92010-10-29 22:22:43 +00001012VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1013 DefinitionKind Kind = DeclarationOnly;
1014
1015 const VarDecl *First = getFirstDeclaration();
1016 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1017 I != E; ++I)
1018 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1019
1020 return Kind;
1021}
1022
Sebastian Redl5ca79842010-02-01 20:16:42 +00001023const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001024 redecl_iterator I = redecls_begin(), E = redecls_end();
1025 while (I != E && !I->getInit())
1026 ++I;
1027
1028 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001029 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001030 return I->getInit();
1031 }
1032 return 0;
1033}
1034
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001035bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001036 if (Decl::isOutOfLine())
1037 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001038
1039 if (!isStaticDataMember())
1040 return false;
1041
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001042 // If this static data member was instantiated from a static data member of
1043 // a class template, check whether that static data member was defined
1044 // out-of-line.
1045 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1046 return VD->isOutOfLine();
1047
1048 return false;
1049}
1050
Douglas Gregor1d957a32009-10-27 18:42:08 +00001051VarDecl *VarDecl::getOutOfLineDefinition() {
1052 if (!isStaticDataMember())
1053 return 0;
1054
1055 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1056 RD != RDEnd; ++RD) {
1057 if (RD->getLexicalDeclContext()->isFileContext())
1058 return *RD;
1059 }
1060
1061 return 0;
1062}
1063
Douglas Gregord5058122010-02-11 01:19:42 +00001064void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001065 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1066 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001067 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001068 }
1069
1070 Init = I;
1071}
1072
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001073VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001074 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001075 return cast<VarDecl>(MSI->getInstantiatedFrom());
1076
1077 return 0;
1078}
1079
Douglas Gregor3c74d412009-10-14 20:14:33 +00001080TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001081 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001082 return MSI->getTemplateSpecializationKind();
1083
1084 return TSK_Undeclared;
1085}
1086
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001087MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001088 return getASTContext().getInstantiatedFromStaticDataMember(this);
1089}
1090
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001091void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1092 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001093 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001094 assert(MSI && "Not an instantiated static data member?");
1095 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001096 if (TSK != TSK_ExplicitSpecialization &&
1097 PointOfInstantiation.isValid() &&
1098 MSI->getPointOfInstantiation().isInvalid())
1099 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001100}
1101
Sebastian Redl833ef452010-01-26 22:01:41 +00001102//===----------------------------------------------------------------------===//
1103// ParmVarDecl Implementation
1104//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001105
Sebastian Redl833ef452010-01-26 22:01:41 +00001106ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1107 SourceLocation L, IdentifierInfo *Id,
1108 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001109 StorageClass S, StorageClass SCAsWritten,
1110 Expr *DefArg) {
1111 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1112 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001113}
1114
Sebastian Redl833ef452010-01-26 22:01:41 +00001115Expr *ParmVarDecl::getDefaultArg() {
1116 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1117 assert(!hasUninstantiatedDefaultArg() &&
1118 "Default argument is not yet instantiated!");
1119
1120 Expr *Arg = getInit();
1121 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1122 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001123
Sebastian Redl833ef452010-01-26 22:01:41 +00001124 return Arg;
1125}
1126
1127unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1128 if (const CXXExprWithTemporaries *E =
1129 dyn_cast<CXXExprWithTemporaries>(getInit()))
1130 return E->getNumTemporaries();
1131
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001132 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001133}
1134
Sebastian Redl833ef452010-01-26 22:01:41 +00001135CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1136 assert(getNumDefaultArgTemporaries() &&
1137 "Default arguments does not have any temporaries!");
1138
1139 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1140 return E->getTemporary(i);
1141}
1142
1143SourceRange ParmVarDecl::getDefaultArgRange() const {
1144 if (const Expr *E = getInit())
1145 return E->getSourceRange();
1146
1147 if (hasUninstantiatedDefaultArg())
1148 return getUninstantiatedDefaultArg()->getSourceRange();
1149
1150 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001151}
1152
Nuno Lopes394ec982008-12-17 23:39:55 +00001153//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001154// FunctionDecl Implementation
1155//===----------------------------------------------------------------------===//
1156
John McCalle1f2ec22009-09-11 06:45:03 +00001157void FunctionDecl::getNameForDiagnostic(std::string &S,
1158 const PrintingPolicy &Policy,
1159 bool Qualified) const {
1160 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1161 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1162 if (TemplateArgs)
1163 S += TemplateSpecializationType::PrintTemplateArgumentList(
1164 TemplateArgs->getFlatArgumentList(),
1165 TemplateArgs->flat_size(),
1166 Policy);
1167
1168}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001169
Ted Kremenek186a0742010-04-29 16:49:01 +00001170bool FunctionDecl::isVariadic() const {
1171 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1172 return FT->isVariadic();
1173 return false;
1174}
1175
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001176bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1177 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1178 if (I->Body) {
1179 Definition = *I;
1180 return true;
1181 }
1182 }
1183
1184 return false;
1185}
1186
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001187Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001188 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1189 if (I->Body) {
1190 Definition = *I;
1191 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001192 }
1193 }
1194
1195 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001196}
1197
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001198void FunctionDecl::setBody(Stmt *B) {
1199 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001200 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001201 EndRangeLoc = B->getLocEnd();
1202}
1203
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001204void FunctionDecl::setPure(bool P) {
1205 IsPure = P;
1206 if (P)
1207 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1208 Parent->markedVirtualFunctionPure();
1209}
1210
Douglas Gregor16618f22009-09-12 00:17:51 +00001211bool FunctionDecl::isMain() const {
1212 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001213 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001214 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001215 getIdentifier() && getIdentifier()->isStr("main");
1216}
1217
Douglas Gregor16618f22009-09-12 00:17:51 +00001218bool FunctionDecl::isExternC() const {
1219 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001220 // In C, any non-static, non-overloadable function has external
1221 // linkage.
1222 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001223 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001224
Mike Stump11289f42009-09-09 15:08:12 +00001225 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001226 DC = DC->getParent()) {
1227 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1228 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001229 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001230 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001231
1232 break;
1233 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001234
1235 if (DC->isRecord())
1236 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001237 }
1238
Douglas Gregorbff62032010-10-21 16:57:46 +00001239 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001240}
1241
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001242bool FunctionDecl::isGlobal() const {
1243 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1244 return Method->isStatic();
1245
John McCall8e7d6562010-08-26 03:08:43 +00001246 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001247 return false;
1248
Mike Stump11289f42009-09-09 15:08:12 +00001249 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001250 DC->isNamespace();
1251 DC = DC->getParent()) {
1252 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1253 if (!Namespace->getDeclName())
1254 return false;
1255 break;
1256 }
1257 }
1258
1259 return true;
1260}
1261
Sebastian Redl833ef452010-01-26 22:01:41 +00001262void
1263FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1264 redeclarable_base::setPreviousDeclaration(PrevDecl);
1265
1266 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1267 FunctionTemplateDecl *PrevFunTmpl
1268 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1269 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1270 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1271 }
1272}
1273
1274const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1275 return getFirstDeclaration();
1276}
1277
1278FunctionDecl *FunctionDecl::getCanonicalDecl() {
1279 return getFirstDeclaration();
1280}
1281
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001282/// \brief Returns a value indicating whether this function
1283/// corresponds to a builtin function.
1284///
1285/// The function corresponds to a built-in function if it is
1286/// declared at translation scope or within an extern "C" block and
1287/// its name matches with the name of a builtin. The returned value
1288/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001289/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001290/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001291unsigned FunctionDecl::getBuiltinID() const {
1292 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001293 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1294 return 0;
1295
1296 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1297 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1298 return BuiltinID;
1299
1300 // This function has the name of a known C library
1301 // function. Determine whether it actually refers to the C library
1302 // function or whether it just has the same name.
1303
Douglas Gregora908e7f2009-02-17 03:23:10 +00001304 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001305 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001306 return 0;
1307
Douglas Gregore711f702009-02-14 18:57:46 +00001308 // If this function is at translation-unit scope and we're not in
1309 // C++, it refers to the C library function.
1310 if (!Context.getLangOptions().CPlusPlus &&
1311 getDeclContext()->isTranslationUnit())
1312 return BuiltinID;
1313
1314 // If the function is in an extern "C" linkage specification and is
1315 // not marked "overloadable", it's the real function.
1316 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001317 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001318 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001319 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001320 return BuiltinID;
1321
1322 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001323 return 0;
1324}
1325
1326
Chris Lattner47c0d002009-04-25 06:03:53 +00001327/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001328/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001329/// after it has been created.
1330unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001331 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001332 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001333 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001334 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001335
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001336}
1337
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001338void FunctionDecl::setParams(ASTContext &C,
1339 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001340 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001341 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001342
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001343 // Zero params -> null pointer.
1344 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001345 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001346 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001347 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001348
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001349 // Update source range. The check below allows us to set EndRangeLoc before
1350 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001351 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001352 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001353 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001354}
Chris Lattner41943152007-01-25 04:52:46 +00001355
Chris Lattner58258242008-04-10 02:22:51 +00001356/// getMinRequiredArguments - Returns the minimum number of arguments
1357/// needed to call this function. This may be fewer than the number of
1358/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001359/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001360unsigned FunctionDecl::getMinRequiredArguments() const {
1361 unsigned NumRequiredArgs = getNumParams();
1362 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001363 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001364 --NumRequiredArgs;
1365
1366 return NumRequiredArgs;
1367}
1368
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001369bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001370 // FIXME: This is not enough. Consider:
1371 //
1372 // inline void f();
1373 // void f() { }
1374 //
1375 // f is inlined, but does not have inline specified.
1376 // To fix this we should add an 'inline' flag to FunctionDecl.
1377 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001378 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001379
1380 if (isa<CXXMethodDecl>(this)) {
1381 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1382 return true;
1383 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001384
1385 switch (getTemplateSpecializationKind()) {
1386 case TSK_Undeclared:
1387 case TSK_ExplicitSpecialization:
1388 return false;
1389
1390 case TSK_ImplicitInstantiation:
1391 case TSK_ExplicitInstantiationDeclaration:
1392 case TSK_ExplicitInstantiationDefinition:
1393 // Handle below.
1394 break;
1395 }
1396
1397 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001398 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001399 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001400 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001401
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001402 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001403 return PatternDecl->isInlined();
1404
1405 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001406}
1407
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001408/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001409/// definition will be externally visible.
1410///
1411/// Inline function definitions are always available for inlining optimizations.
1412/// However, depending on the language dialect, declaration specifiers, and
1413/// attributes, the definition of an inline function may or may not be
1414/// "externally" visible to other translation units in the program.
1415///
1416/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001417/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001418/// inline definition becomes externally visible (C99 6.7.4p6).
1419///
1420/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1421/// definition, we use the GNU semantics for inline, which are nearly the
1422/// opposite of C99 semantics. In particular, "inline" by itself will create
1423/// an externally visible symbol, but "extern inline" will not create an
1424/// externally visible symbol.
1425bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1426 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001427 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001428 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001429
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001430 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001431 // GNU inline semantics. Based on a number of examples, we came up with the
1432 // following heuristic: if the "inline" keyword is present on a
1433 // declaration of the function but "extern" is not present on that
1434 // declaration, then the symbol is externally visible. Otherwise, the GNU
1435 // "extern inline" semantics applies and the symbol is not externally
1436 // visible.
1437 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1438 Redecl != RedeclEnd;
1439 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001440 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001441 return true;
1442 }
1443
1444 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001445 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001446 }
1447
1448 // C99 6.7.4p6:
1449 // [...] If all of the file scope declarations for a function in a
1450 // translation unit include the inline function specifier without extern,
1451 // then the definition in that translation unit is an inline definition.
1452 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1453 Redecl != RedeclEnd;
1454 ++Redecl) {
1455 // Only consider file-scope declarations in this test.
1456 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1457 continue;
1458
John McCall8e7d6562010-08-26 03:08:43 +00001459 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001460 return true; // Not an inline definition
1461 }
1462
1463 // C99 6.7.4p6:
1464 // An inline definition does not provide an external definition for the
1465 // function, and does not forbid an external definition in another
1466 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001467 return false;
1468}
1469
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001470/// getOverloadedOperator - Which C++ overloaded operator this
1471/// function represents, if any.
1472OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001473 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1474 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001475 else
1476 return OO_None;
1477}
1478
Alexis Huntc88db062010-01-13 09:01:02 +00001479/// getLiteralIdentifier - The literal suffix identifier this function
1480/// represents, if any.
1481const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1482 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1483 return getDeclName().getCXXLiteralIdentifier();
1484 else
1485 return 0;
1486}
1487
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001488FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1489 if (TemplateOrSpecialization.isNull())
1490 return TK_NonTemplate;
1491 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1492 return TK_FunctionTemplate;
1493 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1494 return TK_MemberSpecialization;
1495 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1496 return TK_FunctionTemplateSpecialization;
1497 if (TemplateOrSpecialization.is
1498 <DependentFunctionTemplateSpecializationInfo*>())
1499 return TK_DependentFunctionTemplateSpecialization;
1500
1501 assert(false && "Did we miss a TemplateOrSpecialization type?");
1502 return TK_NonTemplate;
1503}
1504
Douglas Gregord801b062009-10-07 23:56:10 +00001505FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001506 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001507 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1508
1509 return 0;
1510}
1511
Douglas Gregor06db9f52009-10-12 20:18:28 +00001512MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1513 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1514}
1515
Douglas Gregord801b062009-10-07 23:56:10 +00001516void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001517FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1518 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001519 TemplateSpecializationKind TSK) {
1520 assert(TemplateOrSpecialization.isNull() &&
1521 "Member function is already a specialization");
1522 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001523 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001524 TemplateOrSpecialization = Info;
1525}
1526
Douglas Gregorafca3b42009-10-27 20:53:28 +00001527bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001528 // If the function is invalid, it can't be implicitly instantiated.
1529 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001530 return false;
1531
1532 switch (getTemplateSpecializationKind()) {
1533 case TSK_Undeclared:
1534 case TSK_ExplicitSpecialization:
1535 case TSK_ExplicitInstantiationDefinition:
1536 return false;
1537
1538 case TSK_ImplicitInstantiation:
1539 return true;
1540
1541 case TSK_ExplicitInstantiationDeclaration:
1542 // Handled below.
1543 break;
1544 }
1545
1546 // Find the actual template from which we will instantiate.
1547 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001548 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001549 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001550 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001551
1552 // C++0x [temp.explicit]p9:
1553 // Except for inline functions, other explicit instantiation declarations
1554 // have the effect of suppressing the implicit instantiation of the entity
1555 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001556 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001557 return true;
1558
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001559 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001560}
1561
1562FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1563 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1564 while (Primary->getInstantiatedFromMemberTemplate()) {
1565 // If we have hit a point where the user provided a specialization of
1566 // this template, we're done looking.
1567 if (Primary->isMemberSpecialization())
1568 break;
1569
1570 Primary = Primary->getInstantiatedFromMemberTemplate();
1571 }
1572
1573 return Primary->getTemplatedDecl();
1574 }
1575
1576 return getInstantiatedFromMemberFunction();
1577}
1578
Douglas Gregor70d83e22009-06-29 17:30:29 +00001579FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001580 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001581 = TemplateOrSpecialization
1582 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001583 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001584 }
1585 return 0;
1586}
1587
1588const TemplateArgumentList *
1589FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001590 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001591 = TemplateOrSpecialization
1592 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001593 return Info->TemplateArguments;
1594 }
1595 return 0;
1596}
1597
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001598const TemplateArgumentListInfo *
1599FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1600 if (FunctionTemplateSpecializationInfo *Info
1601 = TemplateOrSpecialization
1602 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1603 return Info->TemplateArgumentsAsWritten;
1604 }
1605 return 0;
1606}
1607
Mike Stump11289f42009-09-09 15:08:12 +00001608void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001609FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1610 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001611 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001612 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001613 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001614 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1615 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001616 assert(TSK != TSK_Undeclared &&
1617 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001618 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001619 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001620 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001621 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1622 TemplateArgs,
1623 TemplateArgsAsWritten,
1624 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001625 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001626
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001627 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001628 // function template specializations.
1629 if (InsertPos)
1630 Template->getSpecializations().InsertNode(Info, InsertPos);
1631 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001632 // Try to insert the new node. If there is an existing node, leave it, the
1633 // set will contain the canonical decls while
1634 // FunctionTemplateDecl::findSpecialization will return
1635 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001636 FunctionTemplateSpecializationInfo *Existing
1637 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001638 (void)Existing;
1639 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1640 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001641 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001642}
1643
John McCallb9c78482010-04-08 09:05:18 +00001644void
1645FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1646 const UnresolvedSetImpl &Templates,
1647 const TemplateArgumentListInfo &TemplateArgs) {
1648 assert(TemplateOrSpecialization.isNull());
1649 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1650 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001651 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001652 void *Buffer = Context.Allocate(Size);
1653 DependentFunctionTemplateSpecializationInfo *Info =
1654 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1655 TemplateArgs);
1656 TemplateOrSpecialization = Info;
1657}
1658
1659DependentFunctionTemplateSpecializationInfo::
1660DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1661 const TemplateArgumentListInfo &TArgs)
1662 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1663
1664 d.NumTemplates = Ts.size();
1665 d.NumArgs = TArgs.size();
1666
1667 FunctionTemplateDecl **TsArray =
1668 const_cast<FunctionTemplateDecl**>(getTemplates());
1669 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1670 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1671
1672 TemplateArgumentLoc *ArgsArray =
1673 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1674 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1675 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1676}
1677
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001678TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001679 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001680 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001681 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001682 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001683 if (FTSInfo)
1684 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001685
Douglas Gregord801b062009-10-07 23:56:10 +00001686 MemberSpecializationInfo *MSInfo
1687 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1688 if (MSInfo)
1689 return MSInfo->getTemplateSpecializationKind();
1690
1691 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001692}
1693
Mike Stump11289f42009-09-09 15:08:12 +00001694void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001695FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1696 SourceLocation PointOfInstantiation) {
1697 if (FunctionTemplateSpecializationInfo *FTSInfo
1698 = TemplateOrSpecialization.dyn_cast<
1699 FunctionTemplateSpecializationInfo*>()) {
1700 FTSInfo->setTemplateSpecializationKind(TSK);
1701 if (TSK != TSK_ExplicitSpecialization &&
1702 PointOfInstantiation.isValid() &&
1703 FTSInfo->getPointOfInstantiation().isInvalid())
1704 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1705 } else if (MemberSpecializationInfo *MSInfo
1706 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1707 MSInfo->setTemplateSpecializationKind(TSK);
1708 if (TSK != TSK_ExplicitSpecialization &&
1709 PointOfInstantiation.isValid() &&
1710 MSInfo->getPointOfInstantiation().isInvalid())
1711 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1712 } else
1713 assert(false && "Function cannot have a template specialization kind");
1714}
1715
1716SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001717 if (FunctionTemplateSpecializationInfo *FTSInfo
1718 = TemplateOrSpecialization.dyn_cast<
1719 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001720 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001721 else if (MemberSpecializationInfo *MSInfo
1722 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001723 return MSInfo->getPointOfInstantiation();
1724
1725 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001726}
1727
Douglas Gregor6411b922009-09-11 20:15:17 +00001728bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001729 if (Decl::isOutOfLine())
1730 return true;
1731
1732 // If this function was instantiated from a member function of a
1733 // class template, check whether that member function was defined out-of-line.
1734 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1735 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001736 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001737 return Definition->isOutOfLine();
1738 }
1739
1740 // If this function was instantiated from a function template,
1741 // check whether that function template was defined out-of-line.
1742 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1743 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001744 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001745 return Definition->isOutOfLine();
1746 }
1747
1748 return false;
1749}
1750
Chris Lattner59a25942008-03-31 00:36:02 +00001751//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001752// FieldDecl Implementation
1753//===----------------------------------------------------------------------===//
1754
1755FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1756 IdentifierInfo *Id, QualType T,
1757 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1758 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1759}
1760
1761bool FieldDecl::isAnonymousStructOrUnion() const {
1762 if (!isImplicit() || getDeclName())
1763 return false;
1764
1765 if (const RecordType *Record = getType()->getAs<RecordType>())
1766 return Record->getDecl()->isAnonymousStructOrUnion();
1767
1768 return false;
1769}
1770
1771//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001772// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001773//===----------------------------------------------------------------------===//
1774
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001775SourceLocation TagDecl::getOuterLocStart() const {
1776 return getTemplateOrInnerLocStart(this);
1777}
1778
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001779SourceRange TagDecl::getSourceRange() const {
1780 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001781 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001782}
1783
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001784TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001785 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001786}
1787
Douglas Gregora72a4e32010-05-19 18:39:18 +00001788void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1789 TypedefDeclOrQualifier = TDD;
1790 if (TypeForDecl)
1791 TypeForDecl->ClearLinkageCache();
1792}
1793
Douglas Gregordee1be82009-01-17 00:42:38 +00001794void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001795 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001796
1797 if (isa<CXXRecordDecl>(this)) {
1798 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1799 struct CXXRecordDecl::DefinitionData *Data =
1800 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001801 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1802 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001803 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001804}
1805
1806void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001807 assert((!isa<CXXRecordDecl>(this) ||
1808 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1809 "definition completed but not started");
1810
Douglas Gregordee1be82009-01-17 00:42:38 +00001811 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001812 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001813
1814 if (ASTMutationListener *L = getASTMutationListener())
1815 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001816}
1817
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001818TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001819 if (isDefinition())
1820 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001821 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1822 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001823
1824 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001825 R != REnd; ++R)
1826 if (R->isDefinition())
1827 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001828
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001829 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001830}
1831
John McCall3e11ebe2010-03-15 10:12:16 +00001832void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1833 SourceRange QualifierRange) {
1834 if (Qualifier) {
1835 // Make sure the extended qualifier info is allocated.
1836 if (!hasExtInfo())
1837 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1838 // Set qualifier info.
1839 getExtInfo()->NNS = Qualifier;
1840 getExtInfo()->NNSRange = QualifierRange;
1841 }
1842 else {
1843 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1844 assert(QualifierRange.isInvalid());
1845 if (hasExtInfo()) {
1846 getASTContext().Deallocate(getExtInfo());
1847 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1848 }
1849 }
1850}
1851
Ted Kremenek21475702008-09-05 17:16:31 +00001852//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001853// EnumDecl Implementation
1854//===----------------------------------------------------------------------===//
1855
1856EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1857 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001858 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1859 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1860 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001861 C.getTypeDeclType(Enum, PrevDecl);
1862 return Enum;
1863}
1864
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001865EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001866 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1867 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001868}
1869
Douglas Gregord5058122010-02-11 01:19:42 +00001870void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001871 QualType NewPromotionType,
1872 unsigned NumPositiveBits,
1873 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001874 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001875 if (!IntegerType)
1876 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001877 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001878 setNumPositiveBits(NumPositiveBits);
1879 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001880 TagDecl::completeDefinition();
1881}
1882
1883//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001884// RecordDecl Implementation
1885//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001886
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001887RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001888 IdentifierInfo *Id, RecordDecl *PrevDecl,
1889 SourceLocation TKL)
1890 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001891 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001892 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001893 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001894 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001895 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001896}
1897
1898RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001899 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001900 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001901
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001902 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001903 C.getTypeDeclType(R, PrevDecl);
1904 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001905}
1906
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001907RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1908 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1909 SourceLocation());
1910}
1911
Douglas Gregordfcad112009-03-25 15:59:44 +00001912bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001913 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001914 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1915}
1916
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001917RecordDecl::field_iterator RecordDecl::field_begin() const {
1918 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1919 LoadFieldsFromExternalStorage();
1920
1921 return field_iterator(decl_iterator(FirstDecl));
1922}
1923
Douglas Gregor91f84212008-12-11 16:49:14 +00001924/// completeDefinition - Notes that the definition of this type is now
1925/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001926void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001927 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001928 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001929}
Steve Naroffcc321422007-03-26 23:09:51 +00001930
John McCall61925b02010-05-21 01:17:40 +00001931ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1932 // Force the decl chain to come into existence properly.
1933 if (!getNextDeclInContext()) getParent()->decls_begin();
1934
1935 assert(isAnonymousStructOrUnion());
1936 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1937 assert(D->getType()->isRecordType());
1938 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1939 return D;
1940}
1941
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001942void RecordDecl::LoadFieldsFromExternalStorage() const {
1943 ExternalASTSource *Source = getASTContext().getExternalSource();
1944 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1945
1946 // Notify that we have a RecordDecl doing some initialization.
1947 ExternalASTSource::Deserializing TheFields(Source);
1948
1949 llvm::SmallVector<Decl*, 64> Decls;
1950 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1951 return;
1952
1953#ifndef NDEBUG
1954 // Check that all decls we got were FieldDecls.
1955 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1956 assert(isa<FieldDecl>(Decls[i]));
1957#endif
1958
1959 LoadedFieldsFromExternalStorage = true;
1960
1961 if (Decls.empty())
1962 return;
1963
1964 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1965}
1966
Steve Naroff415d3d52008-10-08 17:01:13 +00001967//===----------------------------------------------------------------------===//
1968// BlockDecl Implementation
1969//===----------------------------------------------------------------------===//
1970
Douglas Gregord5058122010-02-11 01:19:42 +00001971void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001972 unsigned NParms) {
1973 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001974
Steve Naroffc4b30e52009-03-13 16:56:44 +00001975 // Zero params -> null pointer.
1976 if (NParms) {
1977 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001978 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001979 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1980 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1981 }
1982}
1983
1984unsigned BlockDecl::getNumParams() const {
1985 return NumParams;
1986}
Sebastian Redl833ef452010-01-26 22:01:41 +00001987
1988
1989//===----------------------------------------------------------------------===//
1990// Other Decl Allocation/Deallocation Method Implementations
1991//===----------------------------------------------------------------------===//
1992
1993TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1994 return new (C) TranslationUnitDecl(C);
1995}
1996
1997NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1998 SourceLocation L, IdentifierInfo *Id) {
1999 return new (C) NamespaceDecl(DC, L, Id);
2000}
2001
Douglas Gregor417e87c2010-10-27 19:49:05 +00002002NamespaceDecl *NamespaceDecl::getNextNamespace() {
2003 return dyn_cast_or_null<NamespaceDecl>(
2004 NextNamespace.get(getASTContext().getExternalSource()));
2005}
2006
Sebastian Redl833ef452010-01-26 22:01:41 +00002007ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2008 SourceLocation L, IdentifierInfo *Id, QualType T) {
2009 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2010}
2011
2012FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002013 const DeclarationNameInfo &NameInfo,
2014 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002015 StorageClass S, StorageClass SCAsWritten,
2016 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002017 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002018 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00002019 New->HasWrittenPrototype = hasWrittenPrototype;
2020 return New;
2021}
2022
2023BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2024 return new (C) BlockDecl(DC, L);
2025}
2026
2027EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2028 SourceLocation L,
2029 IdentifierInfo *Id, QualType T,
2030 Expr *E, const llvm::APSInt &V) {
2031 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2032}
2033
Douglas Gregorbe996932010-09-01 20:41:53 +00002034SourceRange EnumConstantDecl::getSourceRange() const {
2035 SourceLocation End = getLocation();
2036 if (Init)
2037 End = Init->getLocEnd();
2038 return SourceRange(getLocation(), End);
2039}
2040
Sebastian Redl833ef452010-01-26 22:01:41 +00002041TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2042 SourceLocation L, IdentifierInfo *Id,
2043 TypeSourceInfo *TInfo) {
2044 return new (C) TypedefDecl(DC, L, Id, TInfo);
2045}
2046
Sebastian Redl833ef452010-01-26 22:01:41 +00002047FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2048 SourceLocation L,
2049 StringLiteral *Str) {
2050 return new (C) FileScopeAsmDecl(DC, L, Str);
2051}