blob: 7469e8b27fb65cb152201143f3c3fb776076d420 [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 &&
462 ConsiderGlobalVisibility && MD->isInlined() &&
John McCall37bb6c92010-10-29 22:22:43 +0000463 MD->getASTContext().getLangOptions().InlineVisibilityHidden)
John McCallc273f242010-10-30 11:50:40 +0000464 LV.setVisibility(HiddenVisibility);
John McCall457a04e2010-10-22 21:05:15 +0000465
John McCall37bb6c92010-10-29 22:22:43 +0000466 // Note that in contrast to basically every other situation, we
467 // *do* apply -fvisibility to method declarations.
468
469 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000470 if (const ClassTemplateSpecializationDecl *Spec
471 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
472 // Merge template argument/parameter information for member
473 // class template specializations.
John McCallc273f242010-10-30 11:50:40 +0000474 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs()));
475 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000476 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000477 }
478
John McCall37bb6c92010-10-29 22:22:43 +0000479 // Static data members.
480 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000481 // Modify the variable's linkage by its type, but ignore the
482 // type's visibility unless it's a definition.
483 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
484 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000485 LV.mergeLinkage(UniqueExternalLinkage);
486 if (!LV.visibilityExplicit())
487 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000488 }
489
John McCallc273f242010-10-30 11:50:40 +0000490 ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000491
492 // Apply -fvisibility if desired.
John McCallc273f242010-10-30 11:50:40 +0000493 if (ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
494 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000495 }
496
John McCall457a04e2010-10-22 21:05:15 +0000497 return LV;
John McCall8823c652010-08-13 08:35:10 +0000498}
499
John McCallc273f242010-10-30 11:50:40 +0000500LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCall033caa52010-10-29 00:29:13 +0000501 return getLVForDecl(this, /*ConsiderGlobalSettings*/ true);
502}
Ted Kremenek926d8602010-04-20 23:15:35 +0000503
John McCallc273f242010-10-30 11:50:40 +0000504static LinkageInfo getLVForDecl(const NamedDecl *D,
505 bool ConsiderGlobalVisibility) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000506 // Objective-C: treat all Objective-C declarations as having external
507 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000508 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000509 default:
510 break;
John McCall457a04e2010-10-22 21:05:15 +0000511 case Decl::TemplateTemplateParm: // count these as external
512 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000513 case Decl::ObjCAtDefsField:
514 case Decl::ObjCCategory:
515 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000516 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000517 case Decl::ObjCForwardProtocol:
518 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000519 case Decl::ObjCMethod:
520 case Decl::ObjCProperty:
521 case Decl::ObjCPropertyImpl:
522 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000523 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000524 }
525
Douglas Gregorf73b2822009-11-25 22:24:25 +0000526 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000527 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCallc273f242010-10-30 11:50:40 +0000528 return getLVForNamespaceScopeDecl(D, ConsiderGlobalVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000529
530 // C++ [basic.link]p5:
531 // In addition, a member function, static data member, a named
532 // class or enumeration of class scope, or an unnamed class or
533 // enumeration defined in a class-scope typedef declaration such
534 // that the class or enumeration has the typedef name for linkage
535 // purposes (7.1.3), has external linkage if the name of the class
536 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000537 if (D->getDeclContext()->isRecord())
John McCallc273f242010-10-30 11:50:40 +0000538 return getLVForClassMember(D, ConsiderGlobalVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000539
540 // C++ [basic.link]p6:
541 // The name of a function declared in block scope and the name of
542 // an object declared by a block scope extern declaration have
543 // linkage. If there is a visible declaration of an entity with
544 // linkage having the same name and type, ignoring entities
545 // declared outside the innermost enclosing namespace scope, the
546 // block scope declaration declares that same entity and receives
547 // the linkage of the previous declaration. If there is more than
548 // one such matching entity, the program is ill-formed. Otherwise,
549 // if no matching entity is found, the block scope entity receives
550 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000551 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
552 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000553 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000554 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000555
John McCallc273f242010-10-30 11:50:40 +0000556 LinkageInfo LV;
John McCallb7139c42010-10-28 04:18:25 +0000557 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCallc273f242010-10-30 11:50:40 +0000558 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000559
560 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000561 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
562 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
563 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000564 }
565
566 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000567 }
568
John McCall033caa52010-10-29 00:29:13 +0000569 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000570 if (Var->getStorageClass() == SC_Extern ||
571 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000572 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000573 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000574
John McCallc273f242010-10-30 11:50:40 +0000575 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000576 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000577 LV.setVisibility(HiddenVisibility);
John McCallb7139c42010-10-28 04:18:25 +0000578 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCallc273f242010-10-30 11:50:40 +0000579 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000580
581 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000582 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
583 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
584 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000585 }
586
587 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000588 }
589 }
590
591 // C++ [basic.link]p6:
592 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000593 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000594}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000595
Douglas Gregor2ada0482009-02-04 17:27:36 +0000596std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000597 return getQualifiedNameAsString(getASTContext().getLangOptions());
598}
599
600std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000601 const DeclContext *Ctx = getDeclContext();
602
603 if (Ctx->isFunctionOrMethod())
604 return getNameAsString();
605
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000606 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
607 ContextsTy Contexts;
608
609 // Collect contexts.
610 while (Ctx && isa<NamedDecl>(Ctx)) {
611 Contexts.push_back(Ctx);
612 Ctx = Ctx->getParent();
613 };
614
615 std::string QualName;
616 llvm::raw_string_ostream OS(QualName);
617
618 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
619 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000620 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000621 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000622 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
623 std::string TemplateArgsStr
624 = TemplateSpecializationType::PrintTemplateArgumentList(
625 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000626 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000627 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000628 OS << Spec->getName() << TemplateArgsStr;
629 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000630 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000631 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000632 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000633 OS << ND;
634 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
635 if (!RD->getIdentifier())
636 OS << "<anonymous " << RD->getKindName() << '>';
637 else
638 OS << RD;
639 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000640 const FunctionProtoType *FT = 0;
641 if (FD->hasWrittenPrototype())
642 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
643
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000644 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000645 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000646 unsigned NumParams = FD->getNumParams();
647 for (unsigned i = 0; i < NumParams; ++i) {
648 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000649 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000650 std::string Param;
651 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000652 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000653 }
654
655 if (FT->isVariadic()) {
656 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000657 OS << ", ";
658 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000659 }
660 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000661 OS << ')';
662 } else {
663 OS << cast<NamedDecl>(*I);
664 }
665 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000666 }
667
John McCalla2a3f7d2010-03-16 21:48:18 +0000668 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000669 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000670 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000671 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000672
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000673 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000674}
675
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000676bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000677 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
678
Douglas Gregor889ceb72009-02-03 19:21:40 +0000679 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
680 // We want to keep it, unless it nominates same namespace.
681 if (getKind() == Decl::UsingDirective) {
682 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
683 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
684 }
Mike Stump11289f42009-09-09 15:08:12 +0000685
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000686 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
687 // For function declarations, we keep track of redeclarations.
688 return FD->getPreviousDeclaration() == OldD;
689
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000690 // For function templates, the underlying function declarations are linked.
691 if (const FunctionTemplateDecl *FunctionTemplate
692 = dyn_cast<FunctionTemplateDecl>(this))
693 if (const FunctionTemplateDecl *OldFunctionTemplate
694 = dyn_cast<FunctionTemplateDecl>(OldD))
695 return FunctionTemplate->getTemplatedDecl()
696 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000697
Steve Naroffc4173fa2009-02-22 19:35:57 +0000698 // For method declarations, we keep track of redeclarations.
699 if (isa<ObjCMethodDecl>(this))
700 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000701
John McCall9f3059a2009-10-09 21:13:30 +0000702 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
703 return true;
704
John McCall3f746822009-11-17 05:59:44 +0000705 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
706 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
707 cast<UsingShadowDecl>(OldD)->getTargetDecl();
708
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000709 // For non-function declarations, if the declarations are of the
710 // same kind then this must be a redeclaration, or semantic analysis
711 // would not have given us the new declaration.
712 return this->getKind() == OldD->getKind();
713}
714
Douglas Gregoreddf4332009-02-24 20:03:32 +0000715bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000716 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000717}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000718
Anders Carlsson6915bf62009-06-26 06:29:23 +0000719NamedDecl *NamedDecl::getUnderlyingDecl() {
720 NamedDecl *ND = this;
721 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000722 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000723 ND = UD->getTargetDecl();
724 else if (ObjCCompatibleAliasDecl *AD
725 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
726 return AD->getClassInterface();
727 else
728 return ND;
729 }
730}
731
John McCalla8ae2222010-04-06 21:38:20 +0000732bool NamedDecl::isCXXInstanceMember() const {
733 assert(isCXXClassMember() &&
734 "checking whether non-member is instance member");
735
736 const NamedDecl *D = this;
737 if (isa<UsingShadowDecl>(D))
738 D = cast<UsingShadowDecl>(D)->getTargetDecl();
739
740 if (isa<FieldDecl>(D))
741 return true;
742 if (isa<CXXMethodDecl>(D))
743 return cast<CXXMethodDecl>(D)->isInstance();
744 if (isa<FunctionTemplateDecl>(D))
745 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
746 ->getTemplatedDecl())->isInstance();
747 return false;
748}
749
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000750//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000751// DeclaratorDecl Implementation
752//===----------------------------------------------------------------------===//
753
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000754template <typename DeclT>
755static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
756 if (decl->getNumTemplateParameterLists() > 0)
757 return decl->getTemplateParameterList(0)->getTemplateLoc();
758 else
759 return decl->getInnerLocStart();
760}
761
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000762SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000763 TypeSourceInfo *TSI = getTypeSourceInfo();
764 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000765 return SourceLocation();
766}
767
John McCall3e11ebe2010-03-15 10:12:16 +0000768void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
769 SourceRange QualifierRange) {
770 if (Qualifier) {
771 // Make sure the extended decl info is allocated.
772 if (!hasExtInfo()) {
773 // Save (non-extended) type source info pointer.
774 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
775 // Allocate external info struct.
776 DeclInfo = new (getASTContext()) ExtInfo;
777 // Restore savedTInfo into (extended) decl info.
778 getExtInfo()->TInfo = savedTInfo;
779 }
780 // Set qualifier info.
781 getExtInfo()->NNS = Qualifier;
782 getExtInfo()->NNSRange = QualifierRange;
783 }
784 else {
785 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
786 assert(QualifierRange.isInvalid());
787 if (hasExtInfo()) {
788 // Save type source info pointer.
789 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
790 // Deallocate the extended decl info.
791 getASTContext().Deallocate(getExtInfo());
792 // Restore savedTInfo into (non-extended) decl info.
793 DeclInfo = savedTInfo;
794 }
795 }
796}
797
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000798SourceLocation DeclaratorDecl::getOuterLocStart() const {
799 return getTemplateOrInnerLocStart(this);
800}
801
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000802void
Douglas Gregor20527e22010-06-15 17:44:38 +0000803QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
804 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000805 TemplateParameterList **TPLists) {
806 assert((NumTPLists == 0 || TPLists != 0) &&
807 "Empty array of template parameters with positive size!");
808 assert((NumTPLists == 0 || NNS) &&
809 "Nonempty array of template parameters with no qualifier!");
810
811 // Free previous template parameters (if any).
812 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000813 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000814 TemplParamLists = 0;
815 NumTemplParamLists = 0;
816 }
817 // Set info on matched template parameter lists (if any).
818 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000819 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000820 NumTemplParamLists = NumTPLists;
821 for (unsigned i = NumTPLists; i-- > 0; )
822 TemplParamLists[i] = TPLists[i];
823 }
824}
825
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000826//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000827// VarDecl Implementation
828//===----------------------------------------------------------------------===//
829
Sebastian Redl833ef452010-01-26 22:01:41 +0000830const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
831 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000832 case SC_None: break;
833 case SC_Auto: return "auto"; break;
834 case SC_Extern: return "extern"; break;
835 case SC_PrivateExtern: return "__private_extern__"; break;
836 case SC_Register: return "register"; break;
837 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000838 }
839
840 assert(0 && "Invalid storage class");
841 return 0;
842}
843
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000844VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000845 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000846 StorageClass S, StorageClass SCAsWritten) {
847 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000848}
849
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000850SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000851 SourceLocation Start = getTypeSpecStartLoc();
852 if (Start.isInvalid())
853 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000854 return Start;
855}
856
857SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000858 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000859 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
860 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000861}
862
Sebastian Redl833ef452010-01-26 22:01:41 +0000863bool VarDecl::isExternC() const {
864 ASTContext &Context = getASTContext();
865 if (!Context.getLangOptions().CPlusPlus)
866 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000867 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000868 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
869
870 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
871 DC = DC->getParent()) {
872 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
873 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000874 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000875
876 break;
877 }
878
879 if (DC->isFunctionOrMethod())
880 return false;
881 }
882
883 return false;
884}
885
886VarDecl *VarDecl::getCanonicalDecl() {
887 return getFirstDeclaration();
888}
889
Sebastian Redl35351a92010-01-31 22:27:38 +0000890VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
891 // C++ [basic.def]p2:
892 // A declaration is a definition unless [...] it contains the 'extern'
893 // specifier or a linkage-specification and neither an initializer [...],
894 // it declares a static data member in a class declaration [...].
895 // C++ [temp.expl.spec]p15:
896 // An explicit specialization of a static data member of a template is a
897 // definition if the declaration includes an initializer; otherwise, it is
898 // a declaration.
899 if (isStaticDataMember()) {
900 if (isOutOfLine() && (hasInit() ||
901 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
902 return Definition;
903 else
904 return DeclarationOnly;
905 }
906 // C99 6.7p5:
907 // A definition of an identifier is a declaration for that identifier that
908 // [...] causes storage to be reserved for that object.
909 // Note: that applies for all non-file-scope objects.
910 // C99 6.9.2p1:
911 // If the declaration of an identifier for an object has file scope and an
912 // initializer, the declaration is an external definition for the identifier
913 if (hasInit())
914 return Definition;
915 // AST for 'extern "C" int foo;' is annotated with 'extern'.
916 if (hasExternalStorage())
917 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000918
John McCall8e7d6562010-08-26 03:08:43 +0000919 if (getStorageClassAsWritten() == SC_Extern ||
920 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000921 for (const VarDecl *PrevVar = getPreviousDeclaration();
922 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
923 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
924 return DeclarationOnly;
925 }
926 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000927 // C99 6.9.2p2:
928 // A declaration of an object that has file scope without an initializer,
929 // and without a storage class specifier or the scs 'static', constitutes
930 // a tentative definition.
931 // No such thing in C++.
932 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
933 return TentativeDefinition;
934
935 // What's left is (in C, block-scope) declarations without initializers or
936 // external storage. These are definitions.
937 return Definition;
938}
939
Sebastian Redl35351a92010-01-31 22:27:38 +0000940VarDecl *VarDecl::getActingDefinition() {
941 DefinitionKind Kind = isThisDeclarationADefinition();
942 if (Kind != TentativeDefinition)
943 return 0;
944
Chris Lattner48eb14d2010-06-14 18:31:46 +0000945 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000946 VarDecl *First = getFirstDeclaration();
947 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
948 I != E; ++I) {
949 Kind = (*I)->isThisDeclarationADefinition();
950 if (Kind == Definition)
951 return 0;
952 else if (Kind == TentativeDefinition)
953 LastTentative = *I;
954 }
955 return LastTentative;
956}
957
958bool VarDecl::isTentativeDefinitionNow() const {
959 DefinitionKind Kind = isThisDeclarationADefinition();
960 if (Kind != TentativeDefinition)
961 return false;
962
963 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
964 if ((*I)->isThisDeclarationADefinition() == Definition)
965 return false;
966 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000967 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000968}
969
Sebastian Redl5ca79842010-02-01 20:16:42 +0000970VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000971 VarDecl *First = getFirstDeclaration();
972 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
973 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000974 if ((*I)->isThisDeclarationADefinition() == Definition)
975 return *I;
976 }
977 return 0;
978}
979
John McCall37bb6c92010-10-29 22:22:43 +0000980VarDecl::DefinitionKind VarDecl::hasDefinition() const {
981 DefinitionKind Kind = DeclarationOnly;
982
983 const VarDecl *First = getFirstDeclaration();
984 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
985 I != E; ++I)
986 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
987
988 return Kind;
989}
990
Sebastian Redl5ca79842010-02-01 20:16:42 +0000991const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000992 redecl_iterator I = redecls_begin(), E = redecls_end();
993 while (I != E && !I->getInit())
994 ++I;
995
996 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000997 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000998 return I->getInit();
999 }
1000 return 0;
1001}
1002
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001003bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001004 if (Decl::isOutOfLine())
1005 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001006
1007 if (!isStaticDataMember())
1008 return false;
1009
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001010 // If this static data member was instantiated from a static data member of
1011 // a class template, check whether that static data member was defined
1012 // out-of-line.
1013 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1014 return VD->isOutOfLine();
1015
1016 return false;
1017}
1018
Douglas Gregor1d957a32009-10-27 18:42:08 +00001019VarDecl *VarDecl::getOutOfLineDefinition() {
1020 if (!isStaticDataMember())
1021 return 0;
1022
1023 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1024 RD != RDEnd; ++RD) {
1025 if (RD->getLexicalDeclContext()->isFileContext())
1026 return *RD;
1027 }
1028
1029 return 0;
1030}
1031
Douglas Gregord5058122010-02-11 01:19:42 +00001032void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001033 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1034 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001035 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001036 }
1037
1038 Init = I;
1039}
1040
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001041VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001042 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001043 return cast<VarDecl>(MSI->getInstantiatedFrom());
1044
1045 return 0;
1046}
1047
Douglas Gregor3c74d412009-10-14 20:14:33 +00001048TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001049 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001050 return MSI->getTemplateSpecializationKind();
1051
1052 return TSK_Undeclared;
1053}
1054
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001055MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001056 return getASTContext().getInstantiatedFromStaticDataMember(this);
1057}
1058
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001059void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1060 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001061 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001062 assert(MSI && "Not an instantiated static data member?");
1063 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001064 if (TSK != TSK_ExplicitSpecialization &&
1065 PointOfInstantiation.isValid() &&
1066 MSI->getPointOfInstantiation().isInvalid())
1067 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001068}
1069
Sebastian Redl833ef452010-01-26 22:01:41 +00001070//===----------------------------------------------------------------------===//
1071// ParmVarDecl Implementation
1072//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001073
Sebastian Redl833ef452010-01-26 22:01:41 +00001074ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1075 SourceLocation L, IdentifierInfo *Id,
1076 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001077 StorageClass S, StorageClass SCAsWritten,
1078 Expr *DefArg) {
1079 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1080 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001081}
1082
Sebastian Redl833ef452010-01-26 22:01:41 +00001083Expr *ParmVarDecl::getDefaultArg() {
1084 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1085 assert(!hasUninstantiatedDefaultArg() &&
1086 "Default argument is not yet instantiated!");
1087
1088 Expr *Arg = getInit();
1089 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1090 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001091
Sebastian Redl833ef452010-01-26 22:01:41 +00001092 return Arg;
1093}
1094
1095unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1096 if (const CXXExprWithTemporaries *E =
1097 dyn_cast<CXXExprWithTemporaries>(getInit()))
1098 return E->getNumTemporaries();
1099
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001100 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001101}
1102
Sebastian Redl833ef452010-01-26 22:01:41 +00001103CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1104 assert(getNumDefaultArgTemporaries() &&
1105 "Default arguments does not have any temporaries!");
1106
1107 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1108 return E->getTemporary(i);
1109}
1110
1111SourceRange ParmVarDecl::getDefaultArgRange() const {
1112 if (const Expr *E = getInit())
1113 return E->getSourceRange();
1114
1115 if (hasUninstantiatedDefaultArg())
1116 return getUninstantiatedDefaultArg()->getSourceRange();
1117
1118 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001119}
1120
Nuno Lopes394ec982008-12-17 23:39:55 +00001121//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001122// FunctionDecl Implementation
1123//===----------------------------------------------------------------------===//
1124
John McCalle1f2ec22009-09-11 06:45:03 +00001125void FunctionDecl::getNameForDiagnostic(std::string &S,
1126 const PrintingPolicy &Policy,
1127 bool Qualified) const {
1128 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1129 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1130 if (TemplateArgs)
1131 S += TemplateSpecializationType::PrintTemplateArgumentList(
1132 TemplateArgs->getFlatArgumentList(),
1133 TemplateArgs->flat_size(),
1134 Policy);
1135
1136}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001137
Ted Kremenek186a0742010-04-29 16:49:01 +00001138bool FunctionDecl::isVariadic() const {
1139 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1140 return FT->isVariadic();
1141 return false;
1142}
1143
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001144bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1145 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1146 if (I->Body) {
1147 Definition = *I;
1148 return true;
1149 }
1150 }
1151
1152 return false;
1153}
1154
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001155Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001156 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1157 if (I->Body) {
1158 Definition = *I;
1159 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001160 }
1161 }
1162
1163 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001164}
1165
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001166void FunctionDecl::setBody(Stmt *B) {
1167 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001168 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001169 EndRangeLoc = B->getLocEnd();
1170}
1171
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001172void FunctionDecl::setPure(bool P) {
1173 IsPure = P;
1174 if (P)
1175 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1176 Parent->markedVirtualFunctionPure();
1177}
1178
Douglas Gregor16618f22009-09-12 00:17:51 +00001179bool FunctionDecl::isMain() const {
1180 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001181 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001182 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001183 getIdentifier() && getIdentifier()->isStr("main");
1184}
1185
Douglas Gregor16618f22009-09-12 00:17:51 +00001186bool FunctionDecl::isExternC() const {
1187 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001188 // In C, any non-static, non-overloadable function has external
1189 // linkage.
1190 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001191 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001192
Mike Stump11289f42009-09-09 15:08:12 +00001193 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001194 DC = DC->getParent()) {
1195 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1196 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001197 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001198 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001199
1200 break;
1201 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001202
1203 if (DC->isRecord())
1204 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001205 }
1206
Douglas Gregorbff62032010-10-21 16:57:46 +00001207 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001208}
1209
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001210bool FunctionDecl::isGlobal() const {
1211 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1212 return Method->isStatic();
1213
John McCall8e7d6562010-08-26 03:08:43 +00001214 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001215 return false;
1216
Mike Stump11289f42009-09-09 15:08:12 +00001217 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001218 DC->isNamespace();
1219 DC = DC->getParent()) {
1220 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1221 if (!Namespace->getDeclName())
1222 return false;
1223 break;
1224 }
1225 }
1226
1227 return true;
1228}
1229
Sebastian Redl833ef452010-01-26 22:01:41 +00001230void
1231FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1232 redeclarable_base::setPreviousDeclaration(PrevDecl);
1233
1234 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1235 FunctionTemplateDecl *PrevFunTmpl
1236 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1237 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1238 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1239 }
1240}
1241
1242const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1243 return getFirstDeclaration();
1244}
1245
1246FunctionDecl *FunctionDecl::getCanonicalDecl() {
1247 return getFirstDeclaration();
1248}
1249
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001250/// \brief Returns a value indicating whether this function
1251/// corresponds to a builtin function.
1252///
1253/// The function corresponds to a built-in function if it is
1254/// declared at translation scope or within an extern "C" block and
1255/// its name matches with the name of a builtin. The returned value
1256/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001257/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001258/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001259unsigned FunctionDecl::getBuiltinID() const {
1260 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001261 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1262 return 0;
1263
1264 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1265 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1266 return BuiltinID;
1267
1268 // This function has the name of a known C library
1269 // function. Determine whether it actually refers to the C library
1270 // function or whether it just has the same name.
1271
Douglas Gregora908e7f2009-02-17 03:23:10 +00001272 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001273 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001274 return 0;
1275
Douglas Gregore711f702009-02-14 18:57:46 +00001276 // If this function is at translation-unit scope and we're not in
1277 // C++, it refers to the C library function.
1278 if (!Context.getLangOptions().CPlusPlus &&
1279 getDeclContext()->isTranslationUnit())
1280 return BuiltinID;
1281
1282 // If the function is in an extern "C" linkage specification and is
1283 // not marked "overloadable", it's the real function.
1284 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001285 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001286 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001287 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001288 return BuiltinID;
1289
1290 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001291 return 0;
1292}
1293
1294
Chris Lattner47c0d002009-04-25 06:03:53 +00001295/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001296/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001297/// after it has been created.
1298unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001299 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001300 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001301 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001302 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001303
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001304}
1305
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001306void FunctionDecl::setParams(ASTContext &C,
1307 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001308 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001309 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001310
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001311 // Zero params -> null pointer.
1312 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001313 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001314 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001315 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001316
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001317 // Update source range. The check below allows us to set EndRangeLoc before
1318 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001319 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001320 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001321 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001322}
Chris Lattner41943152007-01-25 04:52:46 +00001323
Chris Lattner58258242008-04-10 02:22:51 +00001324/// getMinRequiredArguments - Returns the minimum number of arguments
1325/// needed to call this function. This may be fewer than the number of
1326/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001327/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001328unsigned FunctionDecl::getMinRequiredArguments() const {
1329 unsigned NumRequiredArgs = getNumParams();
1330 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001331 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001332 --NumRequiredArgs;
1333
1334 return NumRequiredArgs;
1335}
1336
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001337bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001338 // FIXME: This is not enough. Consider:
1339 //
1340 // inline void f();
1341 // void f() { }
1342 //
1343 // f is inlined, but does not have inline specified.
1344 // To fix this we should add an 'inline' flag to FunctionDecl.
1345 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001346 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001347
1348 if (isa<CXXMethodDecl>(this)) {
1349 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1350 return true;
1351 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001352
1353 switch (getTemplateSpecializationKind()) {
1354 case TSK_Undeclared:
1355 case TSK_ExplicitSpecialization:
1356 return false;
1357
1358 case TSK_ImplicitInstantiation:
1359 case TSK_ExplicitInstantiationDeclaration:
1360 case TSK_ExplicitInstantiationDefinition:
1361 // Handle below.
1362 break;
1363 }
1364
1365 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001366 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001367 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001368 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001369
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001370 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001371 return PatternDecl->isInlined();
1372
1373 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001374}
1375
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001376/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001377/// definition will be externally visible.
1378///
1379/// Inline function definitions are always available for inlining optimizations.
1380/// However, depending on the language dialect, declaration specifiers, and
1381/// attributes, the definition of an inline function may or may not be
1382/// "externally" visible to other translation units in the program.
1383///
1384/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001385/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001386/// inline definition becomes externally visible (C99 6.7.4p6).
1387///
1388/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1389/// definition, we use the GNU semantics for inline, which are nearly the
1390/// opposite of C99 semantics. In particular, "inline" by itself will create
1391/// an externally visible symbol, but "extern inline" will not create an
1392/// externally visible symbol.
1393bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1394 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001395 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001396 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001397
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001398 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001399 // GNU inline semantics. Based on a number of examples, we came up with the
1400 // following heuristic: if the "inline" keyword is present on a
1401 // declaration of the function but "extern" is not present on that
1402 // declaration, then the symbol is externally visible. Otherwise, the GNU
1403 // "extern inline" semantics applies and the symbol is not externally
1404 // visible.
1405 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1406 Redecl != RedeclEnd;
1407 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001408 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001409 return true;
1410 }
1411
1412 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001413 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001414 }
1415
1416 // C99 6.7.4p6:
1417 // [...] If all of the file scope declarations for a function in a
1418 // translation unit include the inline function specifier without extern,
1419 // then the definition in that translation unit is an inline definition.
1420 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1421 Redecl != RedeclEnd;
1422 ++Redecl) {
1423 // Only consider file-scope declarations in this test.
1424 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1425 continue;
1426
John McCall8e7d6562010-08-26 03:08:43 +00001427 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001428 return true; // Not an inline definition
1429 }
1430
1431 // C99 6.7.4p6:
1432 // An inline definition does not provide an external definition for the
1433 // function, and does not forbid an external definition in another
1434 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001435 return false;
1436}
1437
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001438/// getOverloadedOperator - Which C++ overloaded operator this
1439/// function represents, if any.
1440OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001441 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1442 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001443 else
1444 return OO_None;
1445}
1446
Alexis Huntc88db062010-01-13 09:01:02 +00001447/// getLiteralIdentifier - The literal suffix identifier this function
1448/// represents, if any.
1449const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1450 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1451 return getDeclName().getCXXLiteralIdentifier();
1452 else
1453 return 0;
1454}
1455
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001456FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1457 if (TemplateOrSpecialization.isNull())
1458 return TK_NonTemplate;
1459 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1460 return TK_FunctionTemplate;
1461 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1462 return TK_MemberSpecialization;
1463 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1464 return TK_FunctionTemplateSpecialization;
1465 if (TemplateOrSpecialization.is
1466 <DependentFunctionTemplateSpecializationInfo*>())
1467 return TK_DependentFunctionTemplateSpecialization;
1468
1469 assert(false && "Did we miss a TemplateOrSpecialization type?");
1470 return TK_NonTemplate;
1471}
1472
Douglas Gregord801b062009-10-07 23:56:10 +00001473FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001474 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001475 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1476
1477 return 0;
1478}
1479
Douglas Gregor06db9f52009-10-12 20:18:28 +00001480MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1481 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1482}
1483
Douglas Gregord801b062009-10-07 23:56:10 +00001484void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001485FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1486 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001487 TemplateSpecializationKind TSK) {
1488 assert(TemplateOrSpecialization.isNull() &&
1489 "Member function is already a specialization");
1490 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001491 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001492 TemplateOrSpecialization = Info;
1493}
1494
Douglas Gregorafca3b42009-10-27 20:53:28 +00001495bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001496 // If the function is invalid, it can't be implicitly instantiated.
1497 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001498 return false;
1499
1500 switch (getTemplateSpecializationKind()) {
1501 case TSK_Undeclared:
1502 case TSK_ExplicitSpecialization:
1503 case TSK_ExplicitInstantiationDefinition:
1504 return false;
1505
1506 case TSK_ImplicitInstantiation:
1507 return true;
1508
1509 case TSK_ExplicitInstantiationDeclaration:
1510 // Handled below.
1511 break;
1512 }
1513
1514 // Find the actual template from which we will instantiate.
1515 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001516 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001517 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001518 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001519
1520 // C++0x [temp.explicit]p9:
1521 // Except for inline functions, other explicit instantiation declarations
1522 // have the effect of suppressing the implicit instantiation of the entity
1523 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001524 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001525 return true;
1526
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001527 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001528}
1529
1530FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1531 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1532 while (Primary->getInstantiatedFromMemberTemplate()) {
1533 // If we have hit a point where the user provided a specialization of
1534 // this template, we're done looking.
1535 if (Primary->isMemberSpecialization())
1536 break;
1537
1538 Primary = Primary->getInstantiatedFromMemberTemplate();
1539 }
1540
1541 return Primary->getTemplatedDecl();
1542 }
1543
1544 return getInstantiatedFromMemberFunction();
1545}
1546
Douglas Gregor70d83e22009-06-29 17:30:29 +00001547FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001548 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001549 = TemplateOrSpecialization
1550 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001551 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001552 }
1553 return 0;
1554}
1555
1556const TemplateArgumentList *
1557FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001558 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001559 = TemplateOrSpecialization
1560 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001561 return Info->TemplateArguments;
1562 }
1563 return 0;
1564}
1565
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001566const TemplateArgumentListInfo *
1567FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1568 if (FunctionTemplateSpecializationInfo *Info
1569 = TemplateOrSpecialization
1570 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1571 return Info->TemplateArgumentsAsWritten;
1572 }
1573 return 0;
1574}
1575
Mike Stump11289f42009-09-09 15:08:12 +00001576void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001577FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1578 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001579 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001580 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001581 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001582 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1583 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001584 assert(TSK != TSK_Undeclared &&
1585 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001586 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001587 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001588 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001589 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1590 TemplateArgs,
1591 TemplateArgsAsWritten,
1592 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001593 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001594
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001595 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001596 // function template specializations.
1597 if (InsertPos)
1598 Template->getSpecializations().InsertNode(Info, InsertPos);
1599 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001600 // Try to insert the new node. If there is an existing node, leave it, the
1601 // set will contain the canonical decls while
1602 // FunctionTemplateDecl::findSpecialization will return
1603 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001604 FunctionTemplateSpecializationInfo *Existing
1605 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001606 (void)Existing;
1607 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1608 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001609 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001610}
1611
John McCallb9c78482010-04-08 09:05:18 +00001612void
1613FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1614 const UnresolvedSetImpl &Templates,
1615 const TemplateArgumentListInfo &TemplateArgs) {
1616 assert(TemplateOrSpecialization.isNull());
1617 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1618 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001619 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001620 void *Buffer = Context.Allocate(Size);
1621 DependentFunctionTemplateSpecializationInfo *Info =
1622 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1623 TemplateArgs);
1624 TemplateOrSpecialization = Info;
1625}
1626
1627DependentFunctionTemplateSpecializationInfo::
1628DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1629 const TemplateArgumentListInfo &TArgs)
1630 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1631
1632 d.NumTemplates = Ts.size();
1633 d.NumArgs = TArgs.size();
1634
1635 FunctionTemplateDecl **TsArray =
1636 const_cast<FunctionTemplateDecl**>(getTemplates());
1637 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1638 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1639
1640 TemplateArgumentLoc *ArgsArray =
1641 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1642 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1643 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1644}
1645
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001646TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001647 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001648 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001649 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001650 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001651 if (FTSInfo)
1652 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001653
Douglas Gregord801b062009-10-07 23:56:10 +00001654 MemberSpecializationInfo *MSInfo
1655 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1656 if (MSInfo)
1657 return MSInfo->getTemplateSpecializationKind();
1658
1659 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001660}
1661
Mike Stump11289f42009-09-09 15:08:12 +00001662void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001663FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1664 SourceLocation PointOfInstantiation) {
1665 if (FunctionTemplateSpecializationInfo *FTSInfo
1666 = TemplateOrSpecialization.dyn_cast<
1667 FunctionTemplateSpecializationInfo*>()) {
1668 FTSInfo->setTemplateSpecializationKind(TSK);
1669 if (TSK != TSK_ExplicitSpecialization &&
1670 PointOfInstantiation.isValid() &&
1671 FTSInfo->getPointOfInstantiation().isInvalid())
1672 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1673 } else if (MemberSpecializationInfo *MSInfo
1674 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1675 MSInfo->setTemplateSpecializationKind(TSK);
1676 if (TSK != TSK_ExplicitSpecialization &&
1677 PointOfInstantiation.isValid() &&
1678 MSInfo->getPointOfInstantiation().isInvalid())
1679 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1680 } else
1681 assert(false && "Function cannot have a template specialization kind");
1682}
1683
1684SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001685 if (FunctionTemplateSpecializationInfo *FTSInfo
1686 = TemplateOrSpecialization.dyn_cast<
1687 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001688 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001689 else if (MemberSpecializationInfo *MSInfo
1690 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001691 return MSInfo->getPointOfInstantiation();
1692
1693 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001694}
1695
Douglas Gregor6411b922009-09-11 20:15:17 +00001696bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001697 if (Decl::isOutOfLine())
1698 return true;
1699
1700 // If this function was instantiated from a member function of a
1701 // class template, check whether that member function was defined out-of-line.
1702 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1703 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001704 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001705 return Definition->isOutOfLine();
1706 }
1707
1708 // If this function was instantiated from a function template,
1709 // check whether that function template was defined out-of-line.
1710 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1711 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001712 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001713 return Definition->isOutOfLine();
1714 }
1715
1716 return false;
1717}
1718
Chris Lattner59a25942008-03-31 00:36:02 +00001719//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001720// FieldDecl Implementation
1721//===----------------------------------------------------------------------===//
1722
1723FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1724 IdentifierInfo *Id, QualType T,
1725 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1726 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1727}
1728
1729bool FieldDecl::isAnonymousStructOrUnion() const {
1730 if (!isImplicit() || getDeclName())
1731 return false;
1732
1733 if (const RecordType *Record = getType()->getAs<RecordType>())
1734 return Record->getDecl()->isAnonymousStructOrUnion();
1735
1736 return false;
1737}
1738
1739//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001740// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001741//===----------------------------------------------------------------------===//
1742
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001743SourceLocation TagDecl::getOuterLocStart() const {
1744 return getTemplateOrInnerLocStart(this);
1745}
1746
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001747SourceRange TagDecl::getSourceRange() const {
1748 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001749 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001750}
1751
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001752TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001753 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001754}
1755
Douglas Gregora72a4e32010-05-19 18:39:18 +00001756void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1757 TypedefDeclOrQualifier = TDD;
1758 if (TypeForDecl)
1759 TypeForDecl->ClearLinkageCache();
1760}
1761
Douglas Gregordee1be82009-01-17 00:42:38 +00001762void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001763 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001764
1765 if (isa<CXXRecordDecl>(this)) {
1766 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1767 struct CXXRecordDecl::DefinitionData *Data =
1768 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001769 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1770 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001771 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001772}
1773
1774void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001775 assert((!isa<CXXRecordDecl>(this) ||
1776 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1777 "definition completed but not started");
1778
Douglas Gregordee1be82009-01-17 00:42:38 +00001779 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001780 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001781
1782 if (ASTMutationListener *L = getASTMutationListener())
1783 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001784}
1785
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001786TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001787 if (isDefinition())
1788 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001789 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1790 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001791
1792 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001793 R != REnd; ++R)
1794 if (R->isDefinition())
1795 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001796
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001797 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001798}
1799
John McCall3e11ebe2010-03-15 10:12:16 +00001800void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1801 SourceRange QualifierRange) {
1802 if (Qualifier) {
1803 // Make sure the extended qualifier info is allocated.
1804 if (!hasExtInfo())
1805 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1806 // Set qualifier info.
1807 getExtInfo()->NNS = Qualifier;
1808 getExtInfo()->NNSRange = QualifierRange;
1809 }
1810 else {
1811 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1812 assert(QualifierRange.isInvalid());
1813 if (hasExtInfo()) {
1814 getASTContext().Deallocate(getExtInfo());
1815 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1816 }
1817 }
1818}
1819
Ted Kremenek21475702008-09-05 17:16:31 +00001820//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001821// EnumDecl Implementation
1822//===----------------------------------------------------------------------===//
1823
1824EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1825 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001826 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1827 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1828 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001829 C.getTypeDeclType(Enum, PrevDecl);
1830 return Enum;
1831}
1832
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001833EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001834 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1835 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001836}
1837
Douglas Gregord5058122010-02-11 01:19:42 +00001838void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001839 QualType NewPromotionType,
1840 unsigned NumPositiveBits,
1841 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001842 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001843 if (!IntegerType)
1844 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001845 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001846 setNumPositiveBits(NumPositiveBits);
1847 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001848 TagDecl::completeDefinition();
1849}
1850
1851//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001852// RecordDecl Implementation
1853//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001854
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001855RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001856 IdentifierInfo *Id, RecordDecl *PrevDecl,
1857 SourceLocation TKL)
1858 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001859 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001860 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001861 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001862 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001863 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001864}
1865
1866RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001867 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001868 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001869
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001870 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001871 C.getTypeDeclType(R, PrevDecl);
1872 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001873}
1874
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001875RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1876 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1877 SourceLocation());
1878}
1879
Douglas Gregordfcad112009-03-25 15:59:44 +00001880bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001881 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001882 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1883}
1884
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001885RecordDecl::field_iterator RecordDecl::field_begin() const {
1886 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1887 LoadFieldsFromExternalStorage();
1888
1889 return field_iterator(decl_iterator(FirstDecl));
1890}
1891
Douglas Gregor91f84212008-12-11 16:49:14 +00001892/// completeDefinition - Notes that the definition of this type is now
1893/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001894void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001895 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001896 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001897}
Steve Naroffcc321422007-03-26 23:09:51 +00001898
John McCall61925b02010-05-21 01:17:40 +00001899ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1900 // Force the decl chain to come into existence properly.
1901 if (!getNextDeclInContext()) getParent()->decls_begin();
1902
1903 assert(isAnonymousStructOrUnion());
1904 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1905 assert(D->getType()->isRecordType());
1906 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1907 return D;
1908}
1909
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001910void RecordDecl::LoadFieldsFromExternalStorage() const {
1911 ExternalASTSource *Source = getASTContext().getExternalSource();
1912 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1913
1914 // Notify that we have a RecordDecl doing some initialization.
1915 ExternalASTSource::Deserializing TheFields(Source);
1916
1917 llvm::SmallVector<Decl*, 64> Decls;
1918 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1919 return;
1920
1921#ifndef NDEBUG
1922 // Check that all decls we got were FieldDecls.
1923 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1924 assert(isa<FieldDecl>(Decls[i]));
1925#endif
1926
1927 LoadedFieldsFromExternalStorage = true;
1928
1929 if (Decls.empty())
1930 return;
1931
1932 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1933}
1934
Steve Naroff415d3d52008-10-08 17:01:13 +00001935//===----------------------------------------------------------------------===//
1936// BlockDecl Implementation
1937//===----------------------------------------------------------------------===//
1938
Douglas Gregord5058122010-02-11 01:19:42 +00001939void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001940 unsigned NParms) {
1941 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001942
Steve Naroffc4b30e52009-03-13 16:56:44 +00001943 // Zero params -> null pointer.
1944 if (NParms) {
1945 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001946 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001947 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1948 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1949 }
1950}
1951
1952unsigned BlockDecl::getNumParams() const {
1953 return NumParams;
1954}
Sebastian Redl833ef452010-01-26 22:01:41 +00001955
1956
1957//===----------------------------------------------------------------------===//
1958// Other Decl Allocation/Deallocation Method Implementations
1959//===----------------------------------------------------------------------===//
1960
1961TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1962 return new (C) TranslationUnitDecl(C);
1963}
1964
1965NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1966 SourceLocation L, IdentifierInfo *Id) {
1967 return new (C) NamespaceDecl(DC, L, Id);
1968}
1969
Douglas Gregor417e87c2010-10-27 19:49:05 +00001970NamespaceDecl *NamespaceDecl::getNextNamespace() {
1971 return dyn_cast_or_null<NamespaceDecl>(
1972 NextNamespace.get(getASTContext().getExternalSource()));
1973}
1974
Sebastian Redl833ef452010-01-26 22:01:41 +00001975ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1976 SourceLocation L, IdentifierInfo *Id, QualType T) {
1977 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1978}
1979
1980FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001981 const DeclarationNameInfo &NameInfo,
1982 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001983 StorageClass S, StorageClass SCAsWritten,
1984 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001985 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001986 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001987 New->HasWrittenPrototype = hasWrittenPrototype;
1988 return New;
1989}
1990
1991BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1992 return new (C) BlockDecl(DC, L);
1993}
1994
1995EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1996 SourceLocation L,
1997 IdentifierInfo *Id, QualType T,
1998 Expr *E, const llvm::APSInt &V) {
1999 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2000}
2001
Douglas Gregorbe996932010-09-01 20:41:53 +00002002SourceRange EnumConstantDecl::getSourceRange() const {
2003 SourceLocation End = getLocation();
2004 if (Init)
2005 End = Init->getLocEnd();
2006 return SourceRange(getLocation(), End);
2007}
2008
Sebastian Redl833ef452010-01-26 22:01:41 +00002009TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2010 SourceLocation L, IdentifierInfo *Id,
2011 TypeSourceInfo *TInfo) {
2012 return new (C) TypedefDecl(DC, L, Id, TInfo);
2013}
2014
Sebastian Redl833ef452010-01-26 22:01:41 +00002015FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2016 SourceLocation L,
2017 StringLiteral *Str) {
2018 return new (C) FileScopeAsmDecl(DC, L, Str);
2019}