blob: 5eb9e8501ed76f3307db793a399013a118474dba [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
Douglas Gregor7dc5c172010-02-03 09:33:45 +000078/// \brief Get the most restrictive linkage for the types in the given
79/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +000080static LVPair
81getLVForTemplateParameterList(const TemplateParameterList *Params) {
82 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000083 for (TemplateParameterList::const_iterator P = Params->begin(),
84 PEnd = Params->end();
85 P != PEnd; ++P) {
86 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
87 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +000088 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +000089 continue;
90 }
91
92 if (TemplateTemplateParmDecl *TTP
93 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +000094 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +000095 }
96 }
97
John McCall457a04e2010-10-22 21:05:15 +000098 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +000099}
100
101/// \brief Get the most restrictive linkage for the types and
102/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +0000103static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
104 unsigned NumArgs) {
105 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000106
107 for (unsigned I = 0; I != NumArgs; ++I) {
108 switch (Args[I].getKind()) {
109 case TemplateArgument::Null:
110 case TemplateArgument::Integral:
111 case TemplateArgument::Expression:
112 break;
113
114 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000115 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000116 break;
117
118 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000119 // The decl can validly be null as the representation of nullptr
120 // arguments, valid only in C++0x.
121 if (Decl *D = Args[I].getAsDecl()) {
122 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
123 LV = merge(LV, ND->getLinkageAndVisibility());
124 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
John McCallc273f242010-10-30 11:50:40 +0000125 LV = merge(LV, VD->getLinkageAndVisibility());
John McCall457a04e2010-10-22 21:05:15 +0000126 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000127 break;
128
129 case TemplateArgument::Template:
John McCall457a04e2010-10-22 21:05:15 +0000130 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
131 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000132 break;
133
134 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000135 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
136 Args[I].pack_size()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000137 break;
138 }
139 }
140
John McCall457a04e2010-10-22 21:05:15 +0000141 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000142}
143
John McCallc273f242010-10-30 11:50:40 +0000144static LVPair
145getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
John McCall457a04e2010-10-22 21:05:15 +0000146 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
147 TArgs.flat_size());
John McCall8823c652010-08-13 08:35:10 +0000148}
149
John McCall033caa52010-10-29 00:29:13 +0000150/// getLVForDecl - Get the cached linkage and visibility for the given
151/// declaration.
152///
John McCall37bb6c92010-10-29 22:22:43 +0000153/// \param ConsiderGlobalVisibility - Whether to honor global visibility
154/// settings. This is generally false when computing the visibility
155/// of the context of a declaration.
John McCallc273f242010-10-30 11:50:40 +0000156static LinkageInfo getLVForDecl(const NamedDecl *D,
157 bool ConsiderGlobalVisibility);
John McCall033caa52010-10-29 00:29:13 +0000158
John McCallc273f242010-10-30 11:50:40 +0000159static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
160 bool ConsiderGlobalVisibility) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000161 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000162 "Not a name having namespace scope");
163 ASTContext &Context = D->getASTContext();
164
165 // C++ [basic.link]p3:
166 // A name having namespace scope (3.3.6) has internal linkage if it
167 // is the name of
168 // - an object, reference, function or function template that is
169 // explicitly declared static; or,
170 // (This bullet corresponds to C99 6.2.2p3.)
171 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
172 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000173 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000174 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000175
176 // - an object or reference that is explicitly declared const
177 // and neither explicitly declared extern nor previously
178 // declared to have external linkage; or
179 // (there is no equivalent in C99)
180 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000181 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000182 Var->getStorageClass() != SC_Extern &&
183 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000184 bool FoundExtern = false;
185 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
186 PrevVar && !FoundExtern;
187 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000188 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000189 FoundExtern = true;
190
191 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000192 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000193 }
194 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000195 // C++ [temp]p4:
196 // A non-member function template can have internal linkage; any
197 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000198 const FunctionDecl *Function = 0;
199 if (const FunctionTemplateDecl *FunTmpl
200 = dyn_cast<FunctionTemplateDecl>(D))
201 Function = FunTmpl->getTemplatedDecl();
202 else
203 Function = cast<FunctionDecl>(D);
204
205 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000206 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000207 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000208 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
209 // - a data member of an anonymous union.
210 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000211 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000212 }
213
John McCall457a04e2010-10-22 21:05:15 +0000214 if (D->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000215 return LinkageInfo::uniqueExternal();
John McCallb7139c42010-10-28 04:18:25 +0000216
John McCall457a04e2010-10-22 21:05:15 +0000217 // Set up the defaults.
218
219 // C99 6.2.2p5:
220 // If the declaration of an identifier for an object has file
221 // scope and no storage-class specifier, its linkage is
222 // external.
John McCallc273f242010-10-30 11:50:40 +0000223 LinkageInfo LV;
224
225 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
226 LV.setVisibility(GetVisibilityFromAttr(VA), true);
227 }
John McCall457a04e2010-10-22 21:05:15 +0000228
Douglas Gregorf73b2822009-11-25 22:24:25 +0000229 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000230
Douglas Gregorf73b2822009-11-25 22:24:25 +0000231 // A name having namespace scope has external linkage if it is the
232 // name of
233 //
234 // - an object or reference, unless it has internal linkage; or
235 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000236 // GCC applies the following optimization to variables and static
237 // data members, but not to functions:
238 //
John McCall457a04e2010-10-22 21:05:15 +0000239 // Modify the variable's LV by the LV of its type unless this is
240 // C or extern "C". This follows from [basic.link]p9:
241 // A type without linkage shall not be used as the type of a
242 // variable or function with external linkage unless
243 // - the entity has C language linkage, or
244 // - the entity is declared within an unnamed namespace, or
245 // - the entity is not used or is defined in the same
246 // translation unit.
247 // and [basic.link]p10:
248 // ...the types specified by all declarations referring to a
249 // given variable or function shall be identical...
250 // C does not have an equivalent rule.
251 //
John McCall5fe84122010-10-26 04:59:26 +0000252 // Ignore this if we've got an explicit attribute; the user
253 // probably knows what they're doing.
254 //
John McCall457a04e2010-10-22 21:05:15 +0000255 // Note that we don't want to make the variable non-external
256 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000257 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000258 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
259 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000260 return LinkageInfo::uniqueExternal();
261 if (!LV.visibilityExplicit())
262 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000263 }
264
Douglas Gregorf73b2822009-11-25 22:24:25 +0000265 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000266 (Var->getStorageClass() == SC_Extern ||
267 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000268 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000269 LV.setVisibility(HiddenVisibility, true);
John McCall457a04e2010-10-22 21:05:15 +0000270
Douglas Gregorf73b2822009-11-25 22:24:25 +0000271 // C99 6.2.2p4:
272 // For an identifier declared with the storage-class specifier
273 // extern in a scope in which a prior declaration of that
274 // identifier is visible, if the prior declaration specifies
275 // internal or external linkage, the linkage of the identifier
276 // at the later declaration is the same as the linkage
277 // specified at the prior declaration. If no prior declaration
278 // is visible, or if the prior declaration specifies no
279 // linkage, then the identifier has external linkage.
280 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000281 LinkageInfo PrevLV = PrevVar->getLinkageAndVisibility();
282 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
283 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000284 }
285 }
286
Douglas Gregorf73b2822009-11-25 22:24:25 +0000287 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000288 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000289 // In theory, we can modify the function's LV by the LV of its
290 // type unless it has C linkage (see comment above about variables
291 // for justification). In practice, GCC doesn't do this, so it's
292 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000293
Douglas Gregorf73b2822009-11-25 22:24:25 +0000294 // C99 6.2.2p5:
295 // If the declaration of an identifier for a function has no
296 // storage-class specifier, its linkage is determined exactly
297 // as if it were declared with the storage-class specifier
298 // extern.
299 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000300 (Function->getStorageClass() == SC_Extern ||
301 Function->getStorageClass() == SC_PrivateExtern ||
302 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000303 // C99 6.2.2p4:
304 // For an identifier declared with the storage-class specifier
305 // extern in a scope in which a prior declaration of that
306 // identifier is visible, if the prior declaration specifies
307 // internal or external linkage, the linkage of the identifier
308 // at the later declaration is the same as the linkage
309 // specified at the prior declaration. If no prior declaration
310 // is visible, or if the prior declaration specifies no
311 // linkage, then the identifier has external linkage.
312 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000313 LinkageInfo PrevLV = PrevFunc->getLinkageAndVisibility();
314 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
315 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000316 }
317 }
318
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000319 if (FunctionTemplateSpecializationInfo *SpecInfo
320 = Function->getTemplateSpecializationInfo()) {
John McCallc273f242010-10-30 11:50:40 +0000321 LV.merge(SpecInfo->getTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000322 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCallc273f242010-10-30 11:50:40 +0000323 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000324 }
325
Douglas Gregorf73b2822009-11-25 22:24:25 +0000326 // - a named class (Clause 9), or an unnamed class defined in a
327 // typedef declaration in which the class has the typedef name
328 // for linkage purposes (7.1.3); or
329 // - a named enumeration (7.2), or an unnamed enumeration
330 // defined in a typedef declaration in which the enumeration
331 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000332 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
333 // Unnamed tags have no linkage.
334 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000335 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000336
John McCall457a04e2010-10-22 21:05:15 +0000337 // If this is a class template specialization, consider the
338 // linkage of the template and template arguments.
339 if (const ClassTemplateSpecializationDecl *Spec
340 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
341 // From the template. Note below the restrictions on how we
342 // compute template visibility.
John McCallc273f242010-10-30 11:50:40 +0000343 LV.merge(Spec->getSpecializedTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000344
John McCall457a04e2010-10-22 21:05:15 +0000345 // The arguments at which the template was instantiated.
346 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
John McCallc273f242010-10-30 11:50:40 +0000347 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000348 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000349
John McCall5fe84122010-10-26 04:59:26 +0000350 // Consider -fvisibility unless the type has C linkage.
John McCall37bb6c92010-10-29 22:22:43 +0000351 if (ConsiderGlobalVisibility)
352 ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000353 (Context.getLangOptions().CPlusPlus &&
354 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000355
Douglas Gregorf73b2822009-11-25 22:24:25 +0000356 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000357 } else if (isa<EnumConstantDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000358 LinkageInfo EnumLV =
John McCall457a04e2010-10-22 21:05:15 +0000359 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
John McCallc273f242010-10-30 11:50:40 +0000360 if (!isExternalLinkage(EnumLV.linkage()))
361 return LinkageInfo::none();
362 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000363
364 // - a template, unless it is a function template that has
365 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000366 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000367 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000368
John McCall457a04e2010-10-22 21:05:15 +0000369 // We do not want to consider attributes or global settings when
370 // computing template visibility.
371 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000372
373 // - a namespace (7.3), unless it is declared within an unnamed
374 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000375 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
376 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000377
John McCall457a04e2010-10-22 21:05:15 +0000378 // By extension, we assign external linkage to Objective-C
379 // interfaces.
380 } else if (isa<ObjCInterfaceDecl>(D)) {
381 // fallout
382
383 // Everything not covered here has no linkage.
384 } else {
John McCallc273f242010-10-30 11:50:40 +0000385 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000386 }
387
388 // If we ended up with non-external linkage, visibility should
389 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000390 if (LV.linkage() != ExternalLinkage)
391 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000392
393 // If we didn't end up with hidden visibility, consider attributes
394 // and -fvisibility.
John McCallc273f242010-10-30 11:50:40 +0000395 if (ConsiderGlobalVisibility && !LV.visibilityExplicit() &&
396 LV.visibility() != HiddenVisibility)
397 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000398
399 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000400}
401
John McCallc273f242010-10-30 11:50:40 +0000402static LinkageInfo getLVForClassMember(const NamedDecl *D,
403 bool ConsiderGlobalVisibility) {
John McCall457a04e2010-10-22 21:05:15 +0000404 // Only certain class members have linkage. Note that fields don't
405 // really have linkage, but it's convenient to say they do for the
406 // purposes of calculating linkage of pointer-to-data-member
407 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000408 if (!(isa<CXXMethodDecl>(D) ||
409 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000410 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000411 (isa<TagDecl>(D) &&
412 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000413 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000414
John McCallc273f242010-10-30 11:50:40 +0000415 const VisibilityAttr *VA = GetExplicitVisibility(D);
416 if (VA)
417 ConsiderGlobalVisibility = false;
418
419 // Class members only have linkage if their class has external
420 // linkage. Consider global visibility only if we have no explicit
421 // visibility attributes.
422 LinkageInfo ClassLV = getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
423 ConsiderGlobalVisibility);
424 if (!isExternalLinkage(ClassLV.linkage()))
425 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000426
427 // If the class already has unique-external linkage, we can't improve.
John McCallc273f242010-10-30 11:50:40 +0000428 if (ClassLV.linkage() == UniqueExternalLinkage)
429 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000430
John McCall457a04e2010-10-22 21:05:15 +0000431 // Start with the class's linkage and visibility.
John McCallc273f242010-10-30 11:50:40 +0000432 LinkageInfo LV = ClassLV;
John McCall457a04e2010-10-22 21:05:15 +0000433
John McCallc273f242010-10-30 11:50:40 +0000434 // If we have an explicit visibility attribute, merge that in.
435 if (VA)
436 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
John McCall37bb6c92010-10-29 22:22:43 +0000437
John McCall8823c652010-08-13 08:35:10 +0000438 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000439 TemplateSpecializationKind TSK = TSK_Undeclared;
440
John McCall457a04e2010-10-22 21:05:15 +0000441 // If this is a method template specialization, use the linkage for
442 // the template parameters and arguments.
443 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000444 = MD->getTemplateSpecializationInfo()) {
John McCallc273f242010-10-30 11:50:40 +0000445 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments));
446 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000447 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000448
449 TSK = Spec->getTemplateSpecializationKind();
450 } else if (MemberSpecializationInfo *MSI =
451 MD->getMemberSpecializationInfo()) {
452 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000453 }
454
John McCall37bb6c92010-10-29 22:22:43 +0000455 // If we're paying attention to global visibility, apply
456 // -finline-visibility-hidden if this is an inline method.
457 //
John McCallc273f242010-10-30 11:50:40 +0000458 // Note that ConsiderGlobalVisibility doesn't yet have information
459 // about whether containing classes have visibility attributes,
460 // and that's intentional.
461 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCalle6e622e2010-11-01 01:29:57 +0000462 ConsiderGlobalVisibility &&
463 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
464 // InlineVisibilityHidden only applies to definitions, and
465 // isInlined() only gives meaningful answers on definitions
466 // anyway.
467 const FunctionDecl *Def = 0;
468 if (MD->hasBody(Def) && Def->isInlined())
469 LV.setVisibility(HiddenVisibility);
470 }
John McCall457a04e2010-10-22 21:05:15 +0000471
John McCall37bb6c92010-10-29 22:22:43 +0000472 // Note that in contrast to basically every other situation, we
473 // *do* apply -fvisibility to method declarations.
474
475 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000476 if (const ClassTemplateSpecializationDecl *Spec
477 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
478 // Merge template argument/parameter information for member
479 // class template specializations.
John McCallc273f242010-10-30 11:50:40 +0000480 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs()));
481 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000482 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000483 }
484
John McCall37bb6c92010-10-29 22:22:43 +0000485 // Static data members.
486 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000487 // Modify the variable's linkage by its type, but ignore the
488 // type's visibility unless it's a definition.
489 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
490 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000491 LV.mergeLinkage(UniqueExternalLinkage);
492 if (!LV.visibilityExplicit())
493 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000494 }
495
John McCallc273f242010-10-30 11:50:40 +0000496 ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000497
498 // Apply -fvisibility if desired.
John McCallc273f242010-10-30 11:50:40 +0000499 if (ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
500 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000501 }
502
John McCall457a04e2010-10-22 21:05:15 +0000503 return LV;
John McCall8823c652010-08-13 08:35:10 +0000504}
505
John McCallc273f242010-10-30 11:50:40 +0000506LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCall033caa52010-10-29 00:29:13 +0000507 return getLVForDecl(this, /*ConsiderGlobalSettings*/ true);
508}
Ted Kremenek926d8602010-04-20 23:15:35 +0000509
John McCallc273f242010-10-30 11:50:40 +0000510static LinkageInfo getLVForDecl(const NamedDecl *D,
511 bool ConsiderGlobalVisibility) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000512 // Objective-C: treat all Objective-C declarations as having external
513 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000514 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000515 default:
516 break;
John McCall457a04e2010-10-22 21:05:15 +0000517 case Decl::TemplateTemplateParm: // count these as external
518 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000519 case Decl::ObjCAtDefsField:
520 case Decl::ObjCCategory:
521 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000522 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000523 case Decl::ObjCForwardProtocol:
524 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000525 case Decl::ObjCMethod:
526 case Decl::ObjCProperty:
527 case Decl::ObjCPropertyImpl:
528 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000529 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000530 }
531
Douglas Gregorf73b2822009-11-25 22:24:25 +0000532 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000533 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCallc273f242010-10-30 11:50:40 +0000534 return getLVForNamespaceScopeDecl(D, ConsiderGlobalVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000535
536 // C++ [basic.link]p5:
537 // In addition, a member function, static data member, a named
538 // class or enumeration of class scope, or an unnamed class or
539 // enumeration defined in a class-scope typedef declaration such
540 // that the class or enumeration has the typedef name for linkage
541 // purposes (7.1.3), has external linkage if the name of the class
542 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000543 if (D->getDeclContext()->isRecord())
John McCallc273f242010-10-30 11:50:40 +0000544 return getLVForClassMember(D, ConsiderGlobalVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000545
546 // C++ [basic.link]p6:
547 // The name of a function declared in block scope and the name of
548 // an object declared by a block scope extern declaration have
549 // linkage. If there is a visible declaration of an entity with
550 // linkage having the same name and type, ignoring entities
551 // declared outside the innermost enclosing namespace scope, the
552 // block scope declaration declares that same entity and receives
553 // the linkage of the previous declaration. If there is more than
554 // one such matching entity, the program is ill-formed. Otherwise,
555 // if no matching entity is found, the block scope entity receives
556 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000557 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
558 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000559 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000560 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000561
John McCallc273f242010-10-30 11:50:40 +0000562 LinkageInfo LV;
John McCallb7139c42010-10-28 04:18:25 +0000563 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCallc273f242010-10-30 11:50:40 +0000564 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000565
566 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000567 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
568 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
569 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000570 }
571
572 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000573 }
574
John McCall033caa52010-10-29 00:29:13 +0000575 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000576 if (Var->getStorageClass() == SC_Extern ||
577 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000578 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000579 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000580
John McCallc273f242010-10-30 11:50:40 +0000581 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000582 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000583 LV.setVisibility(HiddenVisibility);
John McCallb7139c42010-10-28 04:18:25 +0000584 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCallc273f242010-10-30 11:50:40 +0000585 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000586
587 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000588 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
589 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
590 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000591 }
592
593 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000594 }
595 }
596
597 // C++ [basic.link]p6:
598 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000599 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000600}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000601
Douglas Gregor2ada0482009-02-04 17:27:36 +0000602std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000603 return getQualifiedNameAsString(getASTContext().getLangOptions());
604}
605
606std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000607 const DeclContext *Ctx = getDeclContext();
608
609 if (Ctx->isFunctionOrMethod())
610 return getNameAsString();
611
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000612 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
613 ContextsTy Contexts;
614
615 // Collect contexts.
616 while (Ctx && isa<NamedDecl>(Ctx)) {
617 Contexts.push_back(Ctx);
618 Ctx = Ctx->getParent();
619 };
620
621 std::string QualName;
622 llvm::raw_string_ostream OS(QualName);
623
624 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
625 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000626 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000627 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000628 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
629 std::string TemplateArgsStr
630 = TemplateSpecializationType::PrintTemplateArgumentList(
631 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000632 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000633 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000634 OS << Spec->getName() << TemplateArgsStr;
635 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000636 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000637 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000638 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000639 OS << ND;
640 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
641 if (!RD->getIdentifier())
642 OS << "<anonymous " << RD->getKindName() << '>';
643 else
644 OS << RD;
645 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000646 const FunctionProtoType *FT = 0;
647 if (FD->hasWrittenPrototype())
648 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
649
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000650 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000651 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000652 unsigned NumParams = FD->getNumParams();
653 for (unsigned i = 0; i < NumParams; ++i) {
654 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000655 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000656 std::string Param;
657 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000658 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000659 }
660
661 if (FT->isVariadic()) {
662 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000663 OS << ", ";
664 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000665 }
666 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000667 OS << ')';
668 } else {
669 OS << cast<NamedDecl>(*I);
670 }
671 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000672 }
673
John McCalla2a3f7d2010-03-16 21:48:18 +0000674 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000675 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000676 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000677 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000678
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000679 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000680}
681
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000682bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000683 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
684
Douglas Gregor889ceb72009-02-03 19:21:40 +0000685 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
686 // We want to keep it, unless it nominates same namespace.
687 if (getKind() == Decl::UsingDirective) {
688 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
689 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
690 }
Mike Stump11289f42009-09-09 15:08:12 +0000691
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000692 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
693 // For function declarations, we keep track of redeclarations.
694 return FD->getPreviousDeclaration() == OldD;
695
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000696 // For function templates, the underlying function declarations are linked.
697 if (const FunctionTemplateDecl *FunctionTemplate
698 = dyn_cast<FunctionTemplateDecl>(this))
699 if (const FunctionTemplateDecl *OldFunctionTemplate
700 = dyn_cast<FunctionTemplateDecl>(OldD))
701 return FunctionTemplate->getTemplatedDecl()
702 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000703
Steve Naroffc4173fa2009-02-22 19:35:57 +0000704 // For method declarations, we keep track of redeclarations.
705 if (isa<ObjCMethodDecl>(this))
706 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000707
John McCall9f3059a2009-10-09 21:13:30 +0000708 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
709 return true;
710
John McCall3f746822009-11-17 05:59:44 +0000711 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
712 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
713 cast<UsingShadowDecl>(OldD)->getTargetDecl();
714
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000715 // For non-function declarations, if the declarations are of the
716 // same kind then this must be a redeclaration, or semantic analysis
717 // would not have given us the new declaration.
718 return this->getKind() == OldD->getKind();
719}
720
Douglas Gregoreddf4332009-02-24 20:03:32 +0000721bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000722 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000723}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000724
Anders Carlsson6915bf62009-06-26 06:29:23 +0000725NamedDecl *NamedDecl::getUnderlyingDecl() {
726 NamedDecl *ND = this;
727 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000728 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000729 ND = UD->getTargetDecl();
730 else if (ObjCCompatibleAliasDecl *AD
731 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
732 return AD->getClassInterface();
733 else
734 return ND;
735 }
736}
737
John McCalla8ae2222010-04-06 21:38:20 +0000738bool NamedDecl::isCXXInstanceMember() const {
739 assert(isCXXClassMember() &&
740 "checking whether non-member is instance member");
741
742 const NamedDecl *D = this;
743 if (isa<UsingShadowDecl>(D))
744 D = cast<UsingShadowDecl>(D)->getTargetDecl();
745
746 if (isa<FieldDecl>(D))
747 return true;
748 if (isa<CXXMethodDecl>(D))
749 return cast<CXXMethodDecl>(D)->isInstance();
750 if (isa<FunctionTemplateDecl>(D))
751 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
752 ->getTemplatedDecl())->isInstance();
753 return false;
754}
755
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000756//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000757// DeclaratorDecl Implementation
758//===----------------------------------------------------------------------===//
759
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000760template <typename DeclT>
761static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
762 if (decl->getNumTemplateParameterLists() > 0)
763 return decl->getTemplateParameterList(0)->getTemplateLoc();
764 else
765 return decl->getInnerLocStart();
766}
767
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000768SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000769 TypeSourceInfo *TSI = getTypeSourceInfo();
770 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000771 return SourceLocation();
772}
773
John McCall3e11ebe2010-03-15 10:12:16 +0000774void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
775 SourceRange QualifierRange) {
776 if (Qualifier) {
777 // Make sure the extended decl info is allocated.
778 if (!hasExtInfo()) {
779 // Save (non-extended) type source info pointer.
780 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
781 // Allocate external info struct.
782 DeclInfo = new (getASTContext()) ExtInfo;
783 // Restore savedTInfo into (extended) decl info.
784 getExtInfo()->TInfo = savedTInfo;
785 }
786 // Set qualifier info.
787 getExtInfo()->NNS = Qualifier;
788 getExtInfo()->NNSRange = QualifierRange;
789 }
790 else {
791 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
792 assert(QualifierRange.isInvalid());
793 if (hasExtInfo()) {
794 // Save type source info pointer.
795 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
796 // Deallocate the extended decl info.
797 getASTContext().Deallocate(getExtInfo());
798 // Restore savedTInfo into (non-extended) decl info.
799 DeclInfo = savedTInfo;
800 }
801 }
802}
803
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000804SourceLocation DeclaratorDecl::getOuterLocStart() const {
805 return getTemplateOrInnerLocStart(this);
806}
807
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000808void
Douglas Gregor20527e22010-06-15 17:44:38 +0000809QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
810 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000811 TemplateParameterList **TPLists) {
812 assert((NumTPLists == 0 || TPLists != 0) &&
813 "Empty array of template parameters with positive size!");
814 assert((NumTPLists == 0 || NNS) &&
815 "Nonempty array of template parameters with no qualifier!");
816
817 // Free previous template parameters (if any).
818 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000819 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000820 TemplParamLists = 0;
821 NumTemplParamLists = 0;
822 }
823 // Set info on matched template parameter lists (if any).
824 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000825 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000826 NumTemplParamLists = NumTPLists;
827 for (unsigned i = NumTPLists; i-- > 0; )
828 TemplParamLists[i] = TPLists[i];
829 }
830}
831
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000832//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000833// VarDecl Implementation
834//===----------------------------------------------------------------------===//
835
Sebastian Redl833ef452010-01-26 22:01:41 +0000836const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
837 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000838 case SC_None: break;
839 case SC_Auto: return "auto"; break;
840 case SC_Extern: return "extern"; break;
841 case SC_PrivateExtern: return "__private_extern__"; break;
842 case SC_Register: return "register"; break;
843 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000844 }
845
846 assert(0 && "Invalid storage class");
847 return 0;
848}
849
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000850VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000851 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000852 StorageClass S, StorageClass SCAsWritten) {
853 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000854}
855
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000856SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000857 SourceLocation Start = getTypeSpecStartLoc();
858 if (Start.isInvalid())
859 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000860 return Start;
861}
862
863SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000864 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000865 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
866 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000867}
868
Sebastian Redl833ef452010-01-26 22:01:41 +0000869bool VarDecl::isExternC() const {
870 ASTContext &Context = getASTContext();
871 if (!Context.getLangOptions().CPlusPlus)
872 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000873 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000874 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
875
876 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
877 DC = DC->getParent()) {
878 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
879 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000880 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000881
882 break;
883 }
884
885 if (DC->isFunctionOrMethod())
886 return false;
887 }
888
889 return false;
890}
891
892VarDecl *VarDecl::getCanonicalDecl() {
893 return getFirstDeclaration();
894}
895
Sebastian Redl35351a92010-01-31 22:27:38 +0000896VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
897 // C++ [basic.def]p2:
898 // A declaration is a definition unless [...] it contains the 'extern'
899 // specifier or a linkage-specification and neither an initializer [...],
900 // it declares a static data member in a class declaration [...].
901 // C++ [temp.expl.spec]p15:
902 // An explicit specialization of a static data member of a template is a
903 // definition if the declaration includes an initializer; otherwise, it is
904 // a declaration.
905 if (isStaticDataMember()) {
906 if (isOutOfLine() && (hasInit() ||
907 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
908 return Definition;
909 else
910 return DeclarationOnly;
911 }
912 // C99 6.7p5:
913 // A definition of an identifier is a declaration for that identifier that
914 // [...] causes storage to be reserved for that object.
915 // Note: that applies for all non-file-scope objects.
916 // C99 6.9.2p1:
917 // If the declaration of an identifier for an object has file scope and an
918 // initializer, the declaration is an external definition for the identifier
919 if (hasInit())
920 return Definition;
921 // AST for 'extern "C" int foo;' is annotated with 'extern'.
922 if (hasExternalStorage())
923 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000924
John McCall8e7d6562010-08-26 03:08:43 +0000925 if (getStorageClassAsWritten() == SC_Extern ||
926 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000927 for (const VarDecl *PrevVar = getPreviousDeclaration();
928 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
929 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
930 return DeclarationOnly;
931 }
932 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000933 // C99 6.9.2p2:
934 // A declaration of an object that has file scope without an initializer,
935 // and without a storage class specifier or the scs 'static', constitutes
936 // a tentative definition.
937 // No such thing in C++.
938 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
939 return TentativeDefinition;
940
941 // What's left is (in C, block-scope) declarations without initializers or
942 // external storage. These are definitions.
943 return Definition;
944}
945
Sebastian Redl35351a92010-01-31 22:27:38 +0000946VarDecl *VarDecl::getActingDefinition() {
947 DefinitionKind Kind = isThisDeclarationADefinition();
948 if (Kind != TentativeDefinition)
949 return 0;
950
Chris Lattner48eb14d2010-06-14 18:31:46 +0000951 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000952 VarDecl *First = getFirstDeclaration();
953 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
954 I != E; ++I) {
955 Kind = (*I)->isThisDeclarationADefinition();
956 if (Kind == Definition)
957 return 0;
958 else if (Kind == TentativeDefinition)
959 LastTentative = *I;
960 }
961 return LastTentative;
962}
963
964bool VarDecl::isTentativeDefinitionNow() const {
965 DefinitionKind Kind = isThisDeclarationADefinition();
966 if (Kind != TentativeDefinition)
967 return false;
968
969 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
970 if ((*I)->isThisDeclarationADefinition() == Definition)
971 return false;
972 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000973 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000974}
975
Sebastian Redl5ca79842010-02-01 20:16:42 +0000976VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000977 VarDecl *First = getFirstDeclaration();
978 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
979 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000980 if ((*I)->isThisDeclarationADefinition() == Definition)
981 return *I;
982 }
983 return 0;
984}
985
John McCall37bb6c92010-10-29 22:22:43 +0000986VarDecl::DefinitionKind VarDecl::hasDefinition() const {
987 DefinitionKind Kind = DeclarationOnly;
988
989 const VarDecl *First = getFirstDeclaration();
990 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
991 I != E; ++I)
992 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
993
994 return Kind;
995}
996
Sebastian Redl5ca79842010-02-01 20:16:42 +0000997const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000998 redecl_iterator I = redecls_begin(), E = redecls_end();
999 while (I != E && !I->getInit())
1000 ++I;
1001
1002 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001003 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001004 return I->getInit();
1005 }
1006 return 0;
1007}
1008
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001009bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001010 if (Decl::isOutOfLine())
1011 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001012
1013 if (!isStaticDataMember())
1014 return false;
1015
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001016 // If this static data member was instantiated from a static data member of
1017 // a class template, check whether that static data member was defined
1018 // out-of-line.
1019 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1020 return VD->isOutOfLine();
1021
1022 return false;
1023}
1024
Douglas Gregor1d957a32009-10-27 18:42:08 +00001025VarDecl *VarDecl::getOutOfLineDefinition() {
1026 if (!isStaticDataMember())
1027 return 0;
1028
1029 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1030 RD != RDEnd; ++RD) {
1031 if (RD->getLexicalDeclContext()->isFileContext())
1032 return *RD;
1033 }
1034
1035 return 0;
1036}
1037
Douglas Gregord5058122010-02-11 01:19:42 +00001038void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001039 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1040 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001041 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001042 }
1043
1044 Init = I;
1045}
1046
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001047VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001048 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001049 return cast<VarDecl>(MSI->getInstantiatedFrom());
1050
1051 return 0;
1052}
1053
Douglas Gregor3c74d412009-10-14 20:14:33 +00001054TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001055 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001056 return MSI->getTemplateSpecializationKind();
1057
1058 return TSK_Undeclared;
1059}
1060
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001061MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001062 return getASTContext().getInstantiatedFromStaticDataMember(this);
1063}
1064
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001065void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1066 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001067 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001068 assert(MSI && "Not an instantiated static data member?");
1069 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001070 if (TSK != TSK_ExplicitSpecialization &&
1071 PointOfInstantiation.isValid() &&
1072 MSI->getPointOfInstantiation().isInvalid())
1073 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001074}
1075
Sebastian Redl833ef452010-01-26 22:01:41 +00001076//===----------------------------------------------------------------------===//
1077// ParmVarDecl Implementation
1078//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001079
Sebastian Redl833ef452010-01-26 22:01:41 +00001080ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1081 SourceLocation L, IdentifierInfo *Id,
1082 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001083 StorageClass S, StorageClass SCAsWritten,
1084 Expr *DefArg) {
1085 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1086 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001087}
1088
Sebastian Redl833ef452010-01-26 22:01:41 +00001089Expr *ParmVarDecl::getDefaultArg() {
1090 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1091 assert(!hasUninstantiatedDefaultArg() &&
1092 "Default argument is not yet instantiated!");
1093
1094 Expr *Arg = getInit();
1095 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1096 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001097
Sebastian Redl833ef452010-01-26 22:01:41 +00001098 return Arg;
1099}
1100
1101unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1102 if (const CXXExprWithTemporaries *E =
1103 dyn_cast<CXXExprWithTemporaries>(getInit()))
1104 return E->getNumTemporaries();
1105
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001106 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001107}
1108
Sebastian Redl833ef452010-01-26 22:01:41 +00001109CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1110 assert(getNumDefaultArgTemporaries() &&
1111 "Default arguments does not have any temporaries!");
1112
1113 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1114 return E->getTemporary(i);
1115}
1116
1117SourceRange ParmVarDecl::getDefaultArgRange() const {
1118 if (const Expr *E = getInit())
1119 return E->getSourceRange();
1120
1121 if (hasUninstantiatedDefaultArg())
1122 return getUninstantiatedDefaultArg()->getSourceRange();
1123
1124 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001125}
1126
Nuno Lopes394ec982008-12-17 23:39:55 +00001127//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001128// FunctionDecl Implementation
1129//===----------------------------------------------------------------------===//
1130
John McCalle1f2ec22009-09-11 06:45:03 +00001131void FunctionDecl::getNameForDiagnostic(std::string &S,
1132 const PrintingPolicy &Policy,
1133 bool Qualified) const {
1134 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1135 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1136 if (TemplateArgs)
1137 S += TemplateSpecializationType::PrintTemplateArgumentList(
1138 TemplateArgs->getFlatArgumentList(),
1139 TemplateArgs->flat_size(),
1140 Policy);
1141
1142}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001143
Ted Kremenek186a0742010-04-29 16:49:01 +00001144bool FunctionDecl::isVariadic() const {
1145 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1146 return FT->isVariadic();
1147 return false;
1148}
1149
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001150bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1151 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1152 if (I->Body) {
1153 Definition = *I;
1154 return true;
1155 }
1156 }
1157
1158 return false;
1159}
1160
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001161Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001162 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1163 if (I->Body) {
1164 Definition = *I;
1165 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001166 }
1167 }
1168
1169 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001170}
1171
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001172void FunctionDecl::setBody(Stmt *B) {
1173 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001174 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001175 EndRangeLoc = B->getLocEnd();
1176}
1177
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001178void FunctionDecl::setPure(bool P) {
1179 IsPure = P;
1180 if (P)
1181 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1182 Parent->markedVirtualFunctionPure();
1183}
1184
Douglas Gregor16618f22009-09-12 00:17:51 +00001185bool FunctionDecl::isMain() const {
1186 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001187 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001188 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001189 getIdentifier() && getIdentifier()->isStr("main");
1190}
1191
Douglas Gregor16618f22009-09-12 00:17:51 +00001192bool FunctionDecl::isExternC() const {
1193 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001194 // In C, any non-static, non-overloadable function has external
1195 // linkage.
1196 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001197 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001198
Mike Stump11289f42009-09-09 15:08:12 +00001199 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001200 DC = DC->getParent()) {
1201 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1202 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001203 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001204 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001205
1206 break;
1207 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001208
1209 if (DC->isRecord())
1210 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001211 }
1212
Douglas Gregorbff62032010-10-21 16:57:46 +00001213 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001214}
1215
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001216bool FunctionDecl::isGlobal() const {
1217 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1218 return Method->isStatic();
1219
John McCall8e7d6562010-08-26 03:08:43 +00001220 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001221 return false;
1222
Mike Stump11289f42009-09-09 15:08:12 +00001223 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001224 DC->isNamespace();
1225 DC = DC->getParent()) {
1226 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1227 if (!Namespace->getDeclName())
1228 return false;
1229 break;
1230 }
1231 }
1232
1233 return true;
1234}
1235
Sebastian Redl833ef452010-01-26 22:01:41 +00001236void
1237FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1238 redeclarable_base::setPreviousDeclaration(PrevDecl);
1239
1240 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1241 FunctionTemplateDecl *PrevFunTmpl
1242 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1243 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1244 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1245 }
1246}
1247
1248const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1249 return getFirstDeclaration();
1250}
1251
1252FunctionDecl *FunctionDecl::getCanonicalDecl() {
1253 return getFirstDeclaration();
1254}
1255
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001256/// \brief Returns a value indicating whether this function
1257/// corresponds to a builtin function.
1258///
1259/// The function corresponds to a built-in function if it is
1260/// declared at translation scope or within an extern "C" block and
1261/// its name matches with the name of a builtin. The returned value
1262/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001263/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001264/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001265unsigned FunctionDecl::getBuiltinID() const {
1266 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001267 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1268 return 0;
1269
1270 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1271 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1272 return BuiltinID;
1273
1274 // This function has the name of a known C library
1275 // function. Determine whether it actually refers to the C library
1276 // function or whether it just has the same name.
1277
Douglas Gregora908e7f2009-02-17 03:23:10 +00001278 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001279 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001280 return 0;
1281
Douglas Gregore711f702009-02-14 18:57:46 +00001282 // If this function is at translation-unit scope and we're not in
1283 // C++, it refers to the C library function.
1284 if (!Context.getLangOptions().CPlusPlus &&
1285 getDeclContext()->isTranslationUnit())
1286 return BuiltinID;
1287
1288 // If the function is in an extern "C" linkage specification and is
1289 // not marked "overloadable", it's the real function.
1290 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001291 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001292 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001293 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001294 return BuiltinID;
1295
1296 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001297 return 0;
1298}
1299
1300
Chris Lattner47c0d002009-04-25 06:03:53 +00001301/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001302/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001303/// after it has been created.
1304unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001305 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001306 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001307 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001308 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001309
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001310}
1311
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001312void FunctionDecl::setParams(ASTContext &C,
1313 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001314 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001315 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001316
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001317 // Zero params -> null pointer.
1318 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001319 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001320 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001321 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001322
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001323 // Update source range. The check below allows us to set EndRangeLoc before
1324 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001325 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001326 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001327 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001328}
Chris Lattner41943152007-01-25 04:52:46 +00001329
Chris Lattner58258242008-04-10 02:22:51 +00001330/// getMinRequiredArguments - Returns the minimum number of arguments
1331/// needed to call this function. This may be fewer than the number of
1332/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001333/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001334unsigned FunctionDecl::getMinRequiredArguments() const {
1335 unsigned NumRequiredArgs = getNumParams();
1336 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001337 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001338 --NumRequiredArgs;
1339
1340 return NumRequiredArgs;
1341}
1342
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001343bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001344 // FIXME: This is not enough. Consider:
1345 //
1346 // inline void f();
1347 // void f() { }
1348 //
1349 // f is inlined, but does not have inline specified.
1350 // To fix this we should add an 'inline' flag to FunctionDecl.
1351 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001352 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001353
1354 if (isa<CXXMethodDecl>(this)) {
1355 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1356 return true;
1357 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001358
1359 switch (getTemplateSpecializationKind()) {
1360 case TSK_Undeclared:
1361 case TSK_ExplicitSpecialization:
1362 return false;
1363
1364 case TSK_ImplicitInstantiation:
1365 case TSK_ExplicitInstantiationDeclaration:
1366 case TSK_ExplicitInstantiationDefinition:
1367 // Handle below.
1368 break;
1369 }
1370
1371 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001372 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001373 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001374 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001375
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001376 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001377 return PatternDecl->isInlined();
1378
1379 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001380}
1381
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001382/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001383/// definition will be externally visible.
1384///
1385/// Inline function definitions are always available for inlining optimizations.
1386/// However, depending on the language dialect, declaration specifiers, and
1387/// attributes, the definition of an inline function may or may not be
1388/// "externally" visible to other translation units in the program.
1389///
1390/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001391/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001392/// inline definition becomes externally visible (C99 6.7.4p6).
1393///
1394/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1395/// definition, we use the GNU semantics for inline, which are nearly the
1396/// opposite of C99 semantics. In particular, "inline" by itself will create
1397/// an externally visible symbol, but "extern inline" will not create an
1398/// externally visible symbol.
1399bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1400 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001401 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001402 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001403
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001404 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001405 // GNU inline semantics. Based on a number of examples, we came up with the
1406 // following heuristic: if the "inline" keyword is present on a
1407 // declaration of the function but "extern" is not present on that
1408 // declaration, then the symbol is externally visible. Otherwise, the GNU
1409 // "extern inline" semantics applies and the symbol is not externally
1410 // visible.
1411 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1412 Redecl != RedeclEnd;
1413 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001414 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001415 return true;
1416 }
1417
1418 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001419 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001420 }
1421
1422 // C99 6.7.4p6:
1423 // [...] If all of the file scope declarations for a function in a
1424 // translation unit include the inline function specifier without extern,
1425 // then the definition in that translation unit is an inline definition.
1426 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1427 Redecl != RedeclEnd;
1428 ++Redecl) {
1429 // Only consider file-scope declarations in this test.
1430 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1431 continue;
1432
John McCall8e7d6562010-08-26 03:08:43 +00001433 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001434 return true; // Not an inline definition
1435 }
1436
1437 // C99 6.7.4p6:
1438 // An inline definition does not provide an external definition for the
1439 // function, and does not forbid an external definition in another
1440 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001441 return false;
1442}
1443
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001444/// getOverloadedOperator - Which C++ overloaded operator this
1445/// function represents, if any.
1446OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001447 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1448 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001449 else
1450 return OO_None;
1451}
1452
Alexis Huntc88db062010-01-13 09:01:02 +00001453/// getLiteralIdentifier - The literal suffix identifier this function
1454/// represents, if any.
1455const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1456 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1457 return getDeclName().getCXXLiteralIdentifier();
1458 else
1459 return 0;
1460}
1461
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001462FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1463 if (TemplateOrSpecialization.isNull())
1464 return TK_NonTemplate;
1465 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1466 return TK_FunctionTemplate;
1467 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1468 return TK_MemberSpecialization;
1469 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1470 return TK_FunctionTemplateSpecialization;
1471 if (TemplateOrSpecialization.is
1472 <DependentFunctionTemplateSpecializationInfo*>())
1473 return TK_DependentFunctionTemplateSpecialization;
1474
1475 assert(false && "Did we miss a TemplateOrSpecialization type?");
1476 return TK_NonTemplate;
1477}
1478
Douglas Gregord801b062009-10-07 23:56:10 +00001479FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001480 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001481 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1482
1483 return 0;
1484}
1485
Douglas Gregor06db9f52009-10-12 20:18:28 +00001486MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1487 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1488}
1489
Douglas Gregord801b062009-10-07 23:56:10 +00001490void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001491FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1492 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001493 TemplateSpecializationKind TSK) {
1494 assert(TemplateOrSpecialization.isNull() &&
1495 "Member function is already a specialization");
1496 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001497 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001498 TemplateOrSpecialization = Info;
1499}
1500
Douglas Gregorafca3b42009-10-27 20:53:28 +00001501bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001502 // If the function is invalid, it can't be implicitly instantiated.
1503 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001504 return false;
1505
1506 switch (getTemplateSpecializationKind()) {
1507 case TSK_Undeclared:
1508 case TSK_ExplicitSpecialization:
1509 case TSK_ExplicitInstantiationDefinition:
1510 return false;
1511
1512 case TSK_ImplicitInstantiation:
1513 return true;
1514
1515 case TSK_ExplicitInstantiationDeclaration:
1516 // Handled below.
1517 break;
1518 }
1519
1520 // Find the actual template from which we will instantiate.
1521 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001522 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001523 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001524 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001525
1526 // C++0x [temp.explicit]p9:
1527 // Except for inline functions, other explicit instantiation declarations
1528 // have the effect of suppressing the implicit instantiation of the entity
1529 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001530 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001531 return true;
1532
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001533 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001534}
1535
1536FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1537 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1538 while (Primary->getInstantiatedFromMemberTemplate()) {
1539 // If we have hit a point where the user provided a specialization of
1540 // this template, we're done looking.
1541 if (Primary->isMemberSpecialization())
1542 break;
1543
1544 Primary = Primary->getInstantiatedFromMemberTemplate();
1545 }
1546
1547 return Primary->getTemplatedDecl();
1548 }
1549
1550 return getInstantiatedFromMemberFunction();
1551}
1552
Douglas Gregor70d83e22009-06-29 17:30:29 +00001553FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001554 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001555 = TemplateOrSpecialization
1556 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001557 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001558 }
1559 return 0;
1560}
1561
1562const TemplateArgumentList *
1563FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001564 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001565 = TemplateOrSpecialization
1566 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001567 return Info->TemplateArguments;
1568 }
1569 return 0;
1570}
1571
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001572const TemplateArgumentListInfo *
1573FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1574 if (FunctionTemplateSpecializationInfo *Info
1575 = TemplateOrSpecialization
1576 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1577 return Info->TemplateArgumentsAsWritten;
1578 }
1579 return 0;
1580}
1581
Mike Stump11289f42009-09-09 15:08:12 +00001582void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001583FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1584 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001585 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001586 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001587 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001588 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1589 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001590 assert(TSK != TSK_Undeclared &&
1591 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001592 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001593 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001594 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001595 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1596 TemplateArgs,
1597 TemplateArgsAsWritten,
1598 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001599 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001600
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001601 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001602 // function template specializations.
1603 if (InsertPos)
1604 Template->getSpecializations().InsertNode(Info, InsertPos);
1605 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001606 // Try to insert the new node. If there is an existing node, leave it, the
1607 // set will contain the canonical decls while
1608 // FunctionTemplateDecl::findSpecialization will return
1609 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001610 FunctionTemplateSpecializationInfo *Existing
1611 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001612 (void)Existing;
1613 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1614 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001615 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001616}
1617
John McCallb9c78482010-04-08 09:05:18 +00001618void
1619FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1620 const UnresolvedSetImpl &Templates,
1621 const TemplateArgumentListInfo &TemplateArgs) {
1622 assert(TemplateOrSpecialization.isNull());
1623 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1624 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001625 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001626 void *Buffer = Context.Allocate(Size);
1627 DependentFunctionTemplateSpecializationInfo *Info =
1628 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1629 TemplateArgs);
1630 TemplateOrSpecialization = Info;
1631}
1632
1633DependentFunctionTemplateSpecializationInfo::
1634DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1635 const TemplateArgumentListInfo &TArgs)
1636 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1637
1638 d.NumTemplates = Ts.size();
1639 d.NumArgs = TArgs.size();
1640
1641 FunctionTemplateDecl **TsArray =
1642 const_cast<FunctionTemplateDecl**>(getTemplates());
1643 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1644 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1645
1646 TemplateArgumentLoc *ArgsArray =
1647 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1648 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1649 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1650}
1651
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001652TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001653 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001654 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001655 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001656 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001657 if (FTSInfo)
1658 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001659
Douglas Gregord801b062009-10-07 23:56:10 +00001660 MemberSpecializationInfo *MSInfo
1661 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1662 if (MSInfo)
1663 return MSInfo->getTemplateSpecializationKind();
1664
1665 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001666}
1667
Mike Stump11289f42009-09-09 15:08:12 +00001668void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001669FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1670 SourceLocation PointOfInstantiation) {
1671 if (FunctionTemplateSpecializationInfo *FTSInfo
1672 = TemplateOrSpecialization.dyn_cast<
1673 FunctionTemplateSpecializationInfo*>()) {
1674 FTSInfo->setTemplateSpecializationKind(TSK);
1675 if (TSK != TSK_ExplicitSpecialization &&
1676 PointOfInstantiation.isValid() &&
1677 FTSInfo->getPointOfInstantiation().isInvalid())
1678 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1679 } else if (MemberSpecializationInfo *MSInfo
1680 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1681 MSInfo->setTemplateSpecializationKind(TSK);
1682 if (TSK != TSK_ExplicitSpecialization &&
1683 PointOfInstantiation.isValid() &&
1684 MSInfo->getPointOfInstantiation().isInvalid())
1685 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1686 } else
1687 assert(false && "Function cannot have a template specialization kind");
1688}
1689
1690SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001691 if (FunctionTemplateSpecializationInfo *FTSInfo
1692 = TemplateOrSpecialization.dyn_cast<
1693 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001694 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001695 else if (MemberSpecializationInfo *MSInfo
1696 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001697 return MSInfo->getPointOfInstantiation();
1698
1699 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001700}
1701
Douglas Gregor6411b922009-09-11 20:15:17 +00001702bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001703 if (Decl::isOutOfLine())
1704 return true;
1705
1706 // If this function was instantiated from a member function of a
1707 // class template, check whether that member function was defined out-of-line.
1708 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1709 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001710 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001711 return Definition->isOutOfLine();
1712 }
1713
1714 // If this function was instantiated from a function template,
1715 // check whether that function template was defined out-of-line.
1716 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1717 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001718 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001719 return Definition->isOutOfLine();
1720 }
1721
1722 return false;
1723}
1724
Chris Lattner59a25942008-03-31 00:36:02 +00001725//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001726// FieldDecl Implementation
1727//===----------------------------------------------------------------------===//
1728
1729FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1730 IdentifierInfo *Id, QualType T,
1731 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1732 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1733}
1734
1735bool FieldDecl::isAnonymousStructOrUnion() const {
1736 if (!isImplicit() || getDeclName())
1737 return false;
1738
1739 if (const RecordType *Record = getType()->getAs<RecordType>())
1740 return Record->getDecl()->isAnonymousStructOrUnion();
1741
1742 return false;
1743}
1744
1745//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001746// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001747//===----------------------------------------------------------------------===//
1748
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001749SourceLocation TagDecl::getOuterLocStart() const {
1750 return getTemplateOrInnerLocStart(this);
1751}
1752
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001753SourceRange TagDecl::getSourceRange() const {
1754 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001755 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001756}
1757
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001758TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001759 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001760}
1761
Douglas Gregora72a4e32010-05-19 18:39:18 +00001762void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1763 TypedefDeclOrQualifier = TDD;
1764 if (TypeForDecl)
1765 TypeForDecl->ClearLinkageCache();
1766}
1767
Douglas Gregordee1be82009-01-17 00:42:38 +00001768void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001769 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001770
1771 if (isa<CXXRecordDecl>(this)) {
1772 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1773 struct CXXRecordDecl::DefinitionData *Data =
1774 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001775 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1776 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001777 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001778}
1779
1780void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001781 assert((!isa<CXXRecordDecl>(this) ||
1782 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1783 "definition completed but not started");
1784
Douglas Gregordee1be82009-01-17 00:42:38 +00001785 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001786 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001787
1788 if (ASTMutationListener *L = getASTMutationListener())
1789 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001790}
1791
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001792TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001793 if (isDefinition())
1794 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001795 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1796 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001797
1798 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001799 R != REnd; ++R)
1800 if (R->isDefinition())
1801 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001802
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001803 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001804}
1805
John McCall3e11ebe2010-03-15 10:12:16 +00001806void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1807 SourceRange QualifierRange) {
1808 if (Qualifier) {
1809 // Make sure the extended qualifier info is allocated.
1810 if (!hasExtInfo())
1811 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1812 // Set qualifier info.
1813 getExtInfo()->NNS = Qualifier;
1814 getExtInfo()->NNSRange = QualifierRange;
1815 }
1816 else {
1817 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1818 assert(QualifierRange.isInvalid());
1819 if (hasExtInfo()) {
1820 getASTContext().Deallocate(getExtInfo());
1821 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1822 }
1823 }
1824}
1825
Ted Kremenek21475702008-09-05 17:16:31 +00001826//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001827// EnumDecl Implementation
1828//===----------------------------------------------------------------------===//
1829
1830EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1831 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001832 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1833 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1834 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001835 C.getTypeDeclType(Enum, PrevDecl);
1836 return Enum;
1837}
1838
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001839EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001840 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1841 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001842}
1843
Douglas Gregord5058122010-02-11 01:19:42 +00001844void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001845 QualType NewPromotionType,
1846 unsigned NumPositiveBits,
1847 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001848 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001849 if (!IntegerType)
1850 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001851 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001852 setNumPositiveBits(NumPositiveBits);
1853 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001854 TagDecl::completeDefinition();
1855}
1856
1857//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001858// RecordDecl Implementation
1859//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001860
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001861RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001862 IdentifierInfo *Id, RecordDecl *PrevDecl,
1863 SourceLocation TKL)
1864 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001865 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001866 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001867 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001868 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001869 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001870}
1871
1872RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001873 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001874 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001875
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001876 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001877 C.getTypeDeclType(R, PrevDecl);
1878 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001879}
1880
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001881RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1882 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1883 SourceLocation());
1884}
1885
Douglas Gregordfcad112009-03-25 15:59:44 +00001886bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001887 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001888 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1889}
1890
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001891RecordDecl::field_iterator RecordDecl::field_begin() const {
1892 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1893 LoadFieldsFromExternalStorage();
1894
1895 return field_iterator(decl_iterator(FirstDecl));
1896}
1897
Douglas Gregor91f84212008-12-11 16:49:14 +00001898/// completeDefinition - Notes that the definition of this type is now
1899/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001900void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001901 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001902 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001903}
Steve Naroffcc321422007-03-26 23:09:51 +00001904
John McCall61925b02010-05-21 01:17:40 +00001905ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1906 // Force the decl chain to come into existence properly.
1907 if (!getNextDeclInContext()) getParent()->decls_begin();
1908
1909 assert(isAnonymousStructOrUnion());
1910 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1911 assert(D->getType()->isRecordType());
1912 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1913 return D;
1914}
1915
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001916void RecordDecl::LoadFieldsFromExternalStorage() const {
1917 ExternalASTSource *Source = getASTContext().getExternalSource();
1918 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1919
1920 // Notify that we have a RecordDecl doing some initialization.
1921 ExternalASTSource::Deserializing TheFields(Source);
1922
1923 llvm::SmallVector<Decl*, 64> Decls;
1924 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1925 return;
1926
1927#ifndef NDEBUG
1928 // Check that all decls we got were FieldDecls.
1929 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1930 assert(isa<FieldDecl>(Decls[i]));
1931#endif
1932
1933 LoadedFieldsFromExternalStorage = true;
1934
1935 if (Decls.empty())
1936 return;
1937
1938 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1939}
1940
Steve Naroff415d3d52008-10-08 17:01:13 +00001941//===----------------------------------------------------------------------===//
1942// BlockDecl Implementation
1943//===----------------------------------------------------------------------===//
1944
Douglas Gregord5058122010-02-11 01:19:42 +00001945void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001946 unsigned NParms) {
1947 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001948
Steve Naroffc4b30e52009-03-13 16:56:44 +00001949 // Zero params -> null pointer.
1950 if (NParms) {
1951 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001952 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001953 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1954 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1955 }
1956}
1957
1958unsigned BlockDecl::getNumParams() const {
1959 return NumParams;
1960}
Sebastian Redl833ef452010-01-26 22:01:41 +00001961
1962
1963//===----------------------------------------------------------------------===//
1964// Other Decl Allocation/Deallocation Method Implementations
1965//===----------------------------------------------------------------------===//
1966
1967TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1968 return new (C) TranslationUnitDecl(C);
1969}
1970
1971NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1972 SourceLocation L, IdentifierInfo *Id) {
1973 return new (C) NamespaceDecl(DC, L, Id);
1974}
1975
Douglas Gregor417e87c2010-10-27 19:49:05 +00001976NamespaceDecl *NamespaceDecl::getNextNamespace() {
1977 return dyn_cast_or_null<NamespaceDecl>(
1978 NextNamespace.get(getASTContext().getExternalSource()));
1979}
1980
Sebastian Redl833ef452010-01-26 22:01:41 +00001981ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1982 SourceLocation L, IdentifierInfo *Id, QualType T) {
1983 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1984}
1985
1986FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001987 const DeclarationNameInfo &NameInfo,
1988 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001989 StorageClass S, StorageClass SCAsWritten,
1990 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001991 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001992 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001993 New->HasWrittenPrototype = hasWrittenPrototype;
1994 return New;
1995}
1996
1997BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1998 return new (C) BlockDecl(DC, L);
1999}
2000
2001EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2002 SourceLocation L,
2003 IdentifierInfo *Id, QualType T,
2004 Expr *E, const llvm::APSInt &V) {
2005 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2006}
2007
Douglas Gregorbe996932010-09-01 20:41:53 +00002008SourceRange EnumConstantDecl::getSourceRange() const {
2009 SourceLocation End = getLocation();
2010 if (Init)
2011 End = Init->getLocEnd();
2012 return SourceRange(getLocation(), End);
2013}
2014
Sebastian Redl833ef452010-01-26 22:01:41 +00002015TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2016 SourceLocation L, IdentifierInfo *Id,
2017 TypeSourceInfo *TInfo) {
2018 return new (C) TypedefDecl(DC, L, Id, TInfo);
2019}
2020
Sebastian Redl833ef452010-01-26 22:01:41 +00002021FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2022 SourceLocation L,
2023 StringLiteral *Str) {
2024 return new (C) FileScopeAsmDecl(DC, L, Str);
2025}