blob: ca963ad7e599832236f963b94bd5899c6b56396d [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
John McCall07072662010-11-02 01:45:15 +000078/// Flags controlling the computation of linkage and visibility.
79struct LVFlags {
80 bool ConsiderGlobalVisibility;
81 bool ConsiderVisibilityAttributes;
82
83 LVFlags() : ConsiderGlobalVisibility(true),
84 ConsiderVisibilityAttributes(true) {
85 }
86
87 /// Returns a set of flags, otherwise based on these, which ignores
88 /// off all sources of visibility except template arguments.
89 LVFlags onlyTemplateVisibility() const {
90 LVFlags F = *this;
91 F.ConsiderGlobalVisibility = false;
92 F.ConsiderVisibilityAttributes = false;
93 return F;
94 }
95};
96
Douglas Gregor7dc5c172010-02-03 09:33:45 +000097/// \brief Get the most restrictive linkage for the types in the given
98/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +000099static LVPair
100getLVForTemplateParameterList(const TemplateParameterList *Params) {
101 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000102 for (TemplateParameterList::const_iterator P = Params->begin(),
103 PEnd = Params->end();
104 P != PEnd; ++P) {
105 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
106 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +0000107 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000108 continue;
109 }
110
111 if (TemplateTemplateParmDecl *TTP
112 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +0000113 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000114 }
115 }
116
John McCall457a04e2010-10-22 21:05:15 +0000117 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000118}
119
120/// \brief Get the most restrictive linkage for the types and
121/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +0000122static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
123 unsigned NumArgs) {
124 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000125
126 for (unsigned I = 0; I != NumArgs; ++I) {
127 switch (Args[I].getKind()) {
128 case TemplateArgument::Null:
129 case TemplateArgument::Integral:
130 case TemplateArgument::Expression:
131 break;
132
133 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000134 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000135 break;
136
137 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000138 // The decl can validly be null as the representation of nullptr
139 // arguments, valid only in C++0x.
140 if (Decl *D = Args[I].getAsDecl()) {
141 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
142 LV = merge(LV, ND->getLinkageAndVisibility());
143 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
John McCallc273f242010-10-30 11:50:40 +0000144 LV = merge(LV, VD->getLinkageAndVisibility());
John McCall457a04e2010-10-22 21:05:15 +0000145 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000146 break;
147
148 case TemplateArgument::Template:
John McCall457a04e2010-10-22 21:05:15 +0000149 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
150 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000151 break;
152
153 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000154 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
155 Args[I].pack_size()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000156 break;
157 }
158 }
159
John McCall457a04e2010-10-22 21:05:15 +0000160 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000161}
162
John McCallc273f242010-10-30 11:50:40 +0000163static LVPair
164getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
John McCall457a04e2010-10-22 21:05:15 +0000165 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
166 TArgs.flat_size());
John McCall8823c652010-08-13 08:35:10 +0000167}
168
John McCall033caa52010-10-29 00:29:13 +0000169/// getLVForDecl - Get the cached linkage and visibility for the given
170/// declaration.
John McCall07072662010-11-02 01:45:15 +0000171static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
John McCall033caa52010-10-29 00:29:13 +0000172
John McCall07072662010-11-02 01:45:15 +0000173static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000174 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000175 "Not a name having namespace scope");
176 ASTContext &Context = D->getASTContext();
177
178 // C++ [basic.link]p3:
179 // A name having namespace scope (3.3.6) has internal linkage if it
180 // is the name of
181 // - an object, reference, function or function template that is
182 // explicitly declared static; or,
183 // (This bullet corresponds to C99 6.2.2p3.)
184 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
185 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000186 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000187 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000188
189 // - an object or reference that is explicitly declared const
190 // and neither explicitly declared extern nor previously
191 // declared to have external linkage; or
192 // (there is no equivalent in C99)
193 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000194 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000195 Var->getStorageClass() != SC_Extern &&
196 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000197 bool FoundExtern = false;
198 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
199 PrevVar && !FoundExtern;
200 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000201 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000202 FoundExtern = true;
203
204 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000205 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000206 }
207 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000208 // C++ [temp]p4:
209 // A non-member function template can have internal linkage; any
210 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000211 const FunctionDecl *Function = 0;
212 if (const FunctionTemplateDecl *FunTmpl
213 = dyn_cast<FunctionTemplateDecl>(D))
214 Function = FunTmpl->getTemplatedDecl();
215 else
216 Function = cast<FunctionDecl>(D);
217
218 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000219 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000220 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000221 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
222 // - a data member of an anonymous union.
223 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000224 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000225 }
226
John McCall457a04e2010-10-22 21:05:15 +0000227 if (D->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000228 return LinkageInfo::uniqueExternal();
John McCallb7139c42010-10-28 04:18:25 +0000229
John McCall457a04e2010-10-22 21:05:15 +0000230 // Set up the defaults.
231
232 // C99 6.2.2p5:
233 // If the declaration of an identifier for an object has file
234 // scope and no storage-class specifier, its linkage is
235 // external.
John McCallc273f242010-10-30 11:50:40 +0000236 LinkageInfo LV;
237
John McCall07072662010-11-02 01:45:15 +0000238 if (F.ConsiderVisibilityAttributes) {
239 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
240 LV.setVisibility(GetVisibilityFromAttr(VA), true);
241 F.ConsiderGlobalVisibility = false;
242 }
John McCallc273f242010-10-30 11:50:40 +0000243 }
John McCall457a04e2010-10-22 21:05:15 +0000244
Douglas Gregorf73b2822009-11-25 22:24:25 +0000245 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000246
Douglas Gregorf73b2822009-11-25 22:24:25 +0000247 // A name having namespace scope has external linkage if it is the
248 // name of
249 //
250 // - an object or reference, unless it has internal linkage; or
251 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000252 // GCC applies the following optimization to variables and static
253 // data members, but not to functions:
254 //
John McCall457a04e2010-10-22 21:05:15 +0000255 // Modify the variable's LV by the LV of its type unless this is
256 // C or extern "C". This follows from [basic.link]p9:
257 // A type without linkage shall not be used as the type of a
258 // variable or function with external linkage unless
259 // - the entity has C language linkage, or
260 // - the entity is declared within an unnamed namespace, or
261 // - the entity is not used or is defined in the same
262 // translation unit.
263 // and [basic.link]p10:
264 // ...the types specified by all declarations referring to a
265 // given variable or function shall be identical...
266 // C does not have an equivalent rule.
267 //
John McCall5fe84122010-10-26 04:59:26 +0000268 // Ignore this if we've got an explicit attribute; the user
269 // probably knows what they're doing.
270 //
John McCall457a04e2010-10-22 21:05:15 +0000271 // Note that we don't want to make the variable non-external
272 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000273 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000274 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
275 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000276 return LinkageInfo::uniqueExternal();
277 if (!LV.visibilityExplicit())
278 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000279 }
280
John McCall23032652010-11-02 18:38:13 +0000281 if (Var->getStorageClass() == SC_PrivateExtern)
282 LV.setVisibility(HiddenVisibility, true);
283
Douglas Gregorf73b2822009-11-25 22:24:25 +0000284 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000285 (Var->getStorageClass() == SC_Extern ||
286 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000287
Douglas Gregorf73b2822009-11-25 22:24:25 +0000288 // C99 6.2.2p4:
289 // For an identifier declared with the storage-class specifier
290 // extern in a scope in which a prior declaration of that
291 // identifier is visible, if the prior declaration specifies
292 // internal or external linkage, the linkage of the identifier
293 // at the later declaration is the same as the linkage
294 // specified at the prior declaration. If no prior declaration
295 // is visible, or if the prior declaration specifies no
296 // linkage, then the identifier has external linkage.
297 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000298 LinkageInfo PrevLV = PrevVar->getLinkageAndVisibility();
299 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
300 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000301 }
302 }
303
Douglas Gregorf73b2822009-11-25 22:24:25 +0000304 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000305 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000306 // In theory, we can modify the function's LV by the LV of its
307 // type unless it has C linkage (see comment above about variables
308 // for justification). In practice, GCC doesn't do this, so it's
309 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000310
John McCall23032652010-11-02 18:38:13 +0000311 if (Function->getStorageClass() == SC_PrivateExtern)
312 LV.setVisibility(HiddenVisibility, true);
313
Douglas Gregorf73b2822009-11-25 22:24:25 +0000314 // C99 6.2.2p5:
315 // If the declaration of an identifier for a function has no
316 // storage-class specifier, its linkage is determined exactly
317 // as if it were declared with the storage-class specifier
318 // extern.
319 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000320 (Function->getStorageClass() == SC_Extern ||
321 Function->getStorageClass() == SC_PrivateExtern ||
322 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000323 // C99 6.2.2p4:
324 // For an identifier declared with the storage-class specifier
325 // extern in a scope in which a prior declaration of that
326 // identifier is visible, if the prior declaration specifies
327 // internal or external linkage, the linkage of the identifier
328 // at the later declaration is the same as the linkage
329 // specified at the prior declaration. If no prior declaration
330 // is visible, or if the prior declaration specifies no
331 // linkage, then the identifier has external linkage.
332 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000333 LinkageInfo PrevLV = PrevFunc->getLinkageAndVisibility();
334 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
335 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000336 }
337 }
338
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000339 if (FunctionTemplateSpecializationInfo *SpecInfo
340 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000341 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
342 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000343 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCallc273f242010-10-30 11:50:40 +0000344 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000345 }
346
Douglas Gregorf73b2822009-11-25 22:24:25 +0000347 // - a named class (Clause 9), or an unnamed class defined in a
348 // typedef declaration in which the class has the typedef name
349 // for linkage purposes (7.1.3); or
350 // - a named enumeration (7.2), or an unnamed enumeration
351 // defined in a typedef declaration in which the enumeration
352 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000353 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
354 // Unnamed tags have no linkage.
355 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000356 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000357
John McCall457a04e2010-10-22 21:05:15 +0000358 // If this is a class template specialization, consider the
359 // linkage of the template and template arguments.
360 if (const ClassTemplateSpecializationDecl *Spec
361 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000362 // From the template.
363 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
364 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000365
John McCall457a04e2010-10-22 21:05:15 +0000366 // The arguments at which the template was instantiated.
367 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
John McCallc273f242010-10-30 11:50:40 +0000368 LV.merge(getLVForTemplateArgumentList(TemplateArgs));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000369 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000370
John McCall5fe84122010-10-26 04:59:26 +0000371 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000372 if (F.ConsiderGlobalVisibility)
373 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000374 (Context.getLangOptions().CPlusPlus &&
375 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000376
Douglas Gregorf73b2822009-11-25 22:24:25 +0000377 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000378 } else if (isa<EnumConstantDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000379 LinkageInfo EnumLV =
John McCall457a04e2010-10-22 21:05:15 +0000380 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
John McCallc273f242010-10-30 11:50:40 +0000381 if (!isExternalLinkage(EnumLV.linkage()))
382 return LinkageInfo::none();
383 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000384
385 // - a template, unless it is a function template that has
386 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000387 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000388 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000389
Douglas Gregorf73b2822009-11-25 22:24:25 +0000390 // - a namespace (7.3), unless it is declared within an unnamed
391 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000392 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
393 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000394
John McCall457a04e2010-10-22 21:05:15 +0000395 // By extension, we assign external linkage to Objective-C
396 // interfaces.
397 } else if (isa<ObjCInterfaceDecl>(D)) {
398 // fallout
399
400 // Everything not covered here has no linkage.
401 } else {
John McCallc273f242010-10-30 11:50:40 +0000402 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000403 }
404
405 // If we ended up with non-external linkage, visibility should
406 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000407 if (LV.linkage() != ExternalLinkage)
408 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000409
410 // If we didn't end up with hidden visibility, consider attributes
411 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000412 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000413 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000414
415 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000416}
417
John McCall07072662010-11-02 01:45:15 +0000418static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000419 // Only certain class members have linkage. Note that fields don't
420 // really have linkage, but it's convenient to say they do for the
421 // purposes of calculating linkage of pointer-to-data-member
422 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000423 if (!(isa<CXXMethodDecl>(D) ||
424 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000425 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000426 (isa<TagDecl>(D) &&
427 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000428 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000429
John McCall07072662010-11-02 01:45:15 +0000430 LinkageInfo LV;
431
432 // The flags we're going to use to compute the class's visibility.
433 LVFlags ClassF = F;
434
435 // If we have an explicit visibility attribute, merge that in.
436 if (F.ConsiderVisibilityAttributes) {
437 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
438 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
439
440 // Ignore global visibility later, but not this attribute.
441 F.ConsiderGlobalVisibility = false;
442
443 // Ignore both global visibility and attributes when computing our
444 // parent's visibility.
445 ClassF = F.onlyTemplateVisibility();
446 }
447 }
John McCallc273f242010-10-30 11:50:40 +0000448
449 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000450 // linkage.
451 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
452 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000453 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000454
455 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000456 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000457 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000458
John McCall8823c652010-08-13 08:35:10 +0000459 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000460 TemplateSpecializationKind TSK = TSK_Undeclared;
461
John McCall457a04e2010-10-22 21:05:15 +0000462 // If this is a method template specialization, use the linkage for
463 // the template parameters and arguments.
464 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000465 = MD->getTemplateSpecializationInfo()) {
John McCallc273f242010-10-30 11:50:40 +0000466 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments));
467 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000468 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000469
470 TSK = Spec->getTemplateSpecializationKind();
471 } else if (MemberSpecializationInfo *MSI =
472 MD->getMemberSpecializationInfo()) {
473 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000474 }
475
John McCall37bb6c92010-10-29 22:22:43 +0000476 // If we're paying attention to global visibility, apply
477 // -finline-visibility-hidden if this is an inline method.
478 //
John McCallc273f242010-10-30 11:50:40 +0000479 // Note that ConsiderGlobalVisibility doesn't yet have information
480 // about whether containing classes have visibility attributes,
481 // and that's intentional.
482 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000483 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000484 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
485 // InlineVisibilityHidden only applies to definitions, and
486 // isInlined() only gives meaningful answers on definitions
487 // anyway.
488 const FunctionDecl *Def = 0;
489 if (MD->hasBody(Def) && Def->isInlined())
490 LV.setVisibility(HiddenVisibility);
491 }
John McCall457a04e2010-10-22 21:05:15 +0000492
John McCall37bb6c92010-10-29 22:22:43 +0000493 // Note that in contrast to basically every other situation, we
494 // *do* apply -fvisibility to method declarations.
495
496 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000497 if (const ClassTemplateSpecializationDecl *Spec
498 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
499 // Merge template argument/parameter information for member
500 // class template specializations.
John McCallc273f242010-10-30 11:50:40 +0000501 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs()));
502 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000503 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000504 }
505
John McCall37bb6c92010-10-29 22:22:43 +0000506 // Static data members.
507 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000508 // Modify the variable's linkage by its type, but ignore the
509 // type's visibility unless it's a definition.
510 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
511 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000512 LV.mergeLinkage(UniqueExternalLinkage);
513 if (!LV.visibilityExplicit())
514 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000515 }
516
John McCall07072662010-11-02 01:45:15 +0000517 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000518
519 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000520 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000521 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000522 }
523
John McCall457a04e2010-10-22 21:05:15 +0000524 return LV;
John McCall8823c652010-08-13 08:35:10 +0000525}
526
John McCallc273f242010-10-30 11:50:40 +0000527LinkageInfo NamedDecl::getLinkageAndVisibility() const {
John McCall07072662010-11-02 01:45:15 +0000528 return getLVForDecl(this, LVFlags());
John McCall033caa52010-10-29 00:29:13 +0000529}
Ted Kremenek926d8602010-04-20 23:15:35 +0000530
John McCall07072662010-11-02 01:45:15 +0000531static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000532 // Objective-C: treat all Objective-C declarations as having external
533 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000534 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000535 default:
536 break;
John McCall457a04e2010-10-22 21:05:15 +0000537 case Decl::TemplateTemplateParm: // count these as external
538 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000539 case Decl::ObjCAtDefsField:
540 case Decl::ObjCCategory:
541 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000542 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000543 case Decl::ObjCForwardProtocol:
544 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000545 case Decl::ObjCMethod:
546 case Decl::ObjCProperty:
547 case Decl::ObjCPropertyImpl:
548 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000549 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000550 }
551
Douglas Gregorf73b2822009-11-25 22:24:25 +0000552 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000553 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000554 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000555
556 // C++ [basic.link]p5:
557 // In addition, a member function, static data member, a named
558 // class or enumeration of class scope, or an unnamed class or
559 // enumeration defined in a class-scope typedef declaration such
560 // that the class or enumeration has the typedef name for linkage
561 // purposes (7.1.3), has external linkage if the name of the class
562 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000563 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000564 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000565
566 // C++ [basic.link]p6:
567 // The name of a function declared in block scope and the name of
568 // an object declared by a block scope extern declaration have
569 // linkage. If there is a visible declaration of an entity with
570 // linkage having the same name and type, ignoring entities
571 // declared outside the innermost enclosing namespace scope, the
572 // block scope declaration declares that same entity and receives
573 // the linkage of the previous declaration. If there is more than
574 // one such matching entity, the program is ill-formed. Otherwise,
575 // if no matching entity is found, the block scope entity receives
576 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000577 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
578 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000579 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000580 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000581
John McCallc273f242010-10-30 11:50:40 +0000582 LinkageInfo LV;
John McCallb7139c42010-10-28 04:18:25 +0000583 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCallc273f242010-10-30 11:50:40 +0000584 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000585
586 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000587 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
588 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
589 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000590 }
591
592 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000593 }
594
John McCall033caa52010-10-29 00:29:13 +0000595 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000596 if (Var->getStorageClass() == SC_Extern ||
597 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000598 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000599 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000600
John McCallc273f242010-10-30 11:50:40 +0000601 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000602 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000603 LV.setVisibility(HiddenVisibility);
John McCallb7139c42010-10-28 04:18:25 +0000604 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCallc273f242010-10-30 11:50:40 +0000605 LV.setVisibility(GetVisibilityFromAttr(VA));
John McCall457a04e2010-10-22 21:05:15 +0000606
607 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
John McCallc273f242010-10-30 11:50:40 +0000608 LinkageInfo PrevLV = Prev->getLinkageAndVisibility();
609 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
610 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000611 }
612
613 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000614 }
615 }
616
617 // C++ [basic.link]p6:
618 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000619 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000620}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000621
Douglas Gregor2ada0482009-02-04 17:27:36 +0000622std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000623 return getQualifiedNameAsString(getASTContext().getLangOptions());
624}
625
626std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000627 const DeclContext *Ctx = getDeclContext();
628
629 if (Ctx->isFunctionOrMethod())
630 return getNameAsString();
631
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000632 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
633 ContextsTy Contexts;
634
635 // Collect contexts.
636 while (Ctx && isa<NamedDecl>(Ctx)) {
637 Contexts.push_back(Ctx);
638 Ctx = Ctx->getParent();
639 };
640
641 std::string QualName;
642 llvm::raw_string_ostream OS(QualName);
643
644 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
645 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000646 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000647 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000648 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
649 std::string TemplateArgsStr
650 = TemplateSpecializationType::PrintTemplateArgumentList(
651 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000652 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000653 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000654 OS << Spec->getName() << TemplateArgsStr;
655 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000656 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000657 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000658 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000659 OS << ND;
660 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
661 if (!RD->getIdentifier())
662 OS << "<anonymous " << RD->getKindName() << '>';
663 else
664 OS << RD;
665 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000666 const FunctionProtoType *FT = 0;
667 if (FD->hasWrittenPrototype())
668 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
669
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000670 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000671 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000672 unsigned NumParams = FD->getNumParams();
673 for (unsigned i = 0; i < NumParams; ++i) {
674 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000675 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000676 std::string Param;
677 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000678 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000679 }
680
681 if (FT->isVariadic()) {
682 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000683 OS << ", ";
684 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000685 }
686 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000687 OS << ')';
688 } else {
689 OS << cast<NamedDecl>(*I);
690 }
691 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000692 }
693
John McCalla2a3f7d2010-03-16 21:48:18 +0000694 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000695 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000696 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000697 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000698
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000699 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000700}
701
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000702bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000703 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
704
Douglas Gregor889ceb72009-02-03 19:21:40 +0000705 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
706 // We want to keep it, unless it nominates same namespace.
707 if (getKind() == Decl::UsingDirective) {
708 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
709 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
710 }
Mike Stump11289f42009-09-09 15:08:12 +0000711
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000712 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
713 // For function declarations, we keep track of redeclarations.
714 return FD->getPreviousDeclaration() == OldD;
715
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000716 // For function templates, the underlying function declarations are linked.
717 if (const FunctionTemplateDecl *FunctionTemplate
718 = dyn_cast<FunctionTemplateDecl>(this))
719 if (const FunctionTemplateDecl *OldFunctionTemplate
720 = dyn_cast<FunctionTemplateDecl>(OldD))
721 return FunctionTemplate->getTemplatedDecl()
722 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000723
Steve Naroffc4173fa2009-02-22 19:35:57 +0000724 // For method declarations, we keep track of redeclarations.
725 if (isa<ObjCMethodDecl>(this))
726 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000727
John McCall9f3059a2009-10-09 21:13:30 +0000728 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
729 return true;
730
John McCall3f746822009-11-17 05:59:44 +0000731 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
732 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
733 cast<UsingShadowDecl>(OldD)->getTargetDecl();
734
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000735 // For non-function declarations, if the declarations are of the
736 // same kind then this must be a redeclaration, or semantic analysis
737 // would not have given us the new declaration.
738 return this->getKind() == OldD->getKind();
739}
740
Douglas Gregoreddf4332009-02-24 20:03:32 +0000741bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000742 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000743}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000744
Anders Carlsson6915bf62009-06-26 06:29:23 +0000745NamedDecl *NamedDecl::getUnderlyingDecl() {
746 NamedDecl *ND = this;
747 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000748 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000749 ND = UD->getTargetDecl();
750 else if (ObjCCompatibleAliasDecl *AD
751 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
752 return AD->getClassInterface();
753 else
754 return ND;
755 }
756}
757
John McCalla8ae2222010-04-06 21:38:20 +0000758bool NamedDecl::isCXXInstanceMember() const {
759 assert(isCXXClassMember() &&
760 "checking whether non-member is instance member");
761
762 const NamedDecl *D = this;
763 if (isa<UsingShadowDecl>(D))
764 D = cast<UsingShadowDecl>(D)->getTargetDecl();
765
766 if (isa<FieldDecl>(D))
767 return true;
768 if (isa<CXXMethodDecl>(D))
769 return cast<CXXMethodDecl>(D)->isInstance();
770 if (isa<FunctionTemplateDecl>(D))
771 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
772 ->getTemplatedDecl())->isInstance();
773 return false;
774}
775
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000776//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000777// DeclaratorDecl Implementation
778//===----------------------------------------------------------------------===//
779
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000780template <typename DeclT>
781static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
782 if (decl->getNumTemplateParameterLists() > 0)
783 return decl->getTemplateParameterList(0)->getTemplateLoc();
784 else
785 return decl->getInnerLocStart();
786}
787
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000788SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000789 TypeSourceInfo *TSI = getTypeSourceInfo();
790 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000791 return SourceLocation();
792}
793
John McCall3e11ebe2010-03-15 10:12:16 +0000794void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
795 SourceRange QualifierRange) {
796 if (Qualifier) {
797 // Make sure the extended decl info is allocated.
798 if (!hasExtInfo()) {
799 // Save (non-extended) type source info pointer.
800 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
801 // Allocate external info struct.
802 DeclInfo = new (getASTContext()) ExtInfo;
803 // Restore savedTInfo into (extended) decl info.
804 getExtInfo()->TInfo = savedTInfo;
805 }
806 // Set qualifier info.
807 getExtInfo()->NNS = Qualifier;
808 getExtInfo()->NNSRange = QualifierRange;
809 }
810 else {
811 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
812 assert(QualifierRange.isInvalid());
813 if (hasExtInfo()) {
814 // Save type source info pointer.
815 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
816 // Deallocate the extended decl info.
817 getASTContext().Deallocate(getExtInfo());
818 // Restore savedTInfo into (non-extended) decl info.
819 DeclInfo = savedTInfo;
820 }
821 }
822}
823
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000824SourceLocation DeclaratorDecl::getOuterLocStart() const {
825 return getTemplateOrInnerLocStart(this);
826}
827
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000828void
Douglas Gregor20527e22010-06-15 17:44:38 +0000829QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
830 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000831 TemplateParameterList **TPLists) {
832 assert((NumTPLists == 0 || TPLists != 0) &&
833 "Empty array of template parameters with positive size!");
834 assert((NumTPLists == 0 || NNS) &&
835 "Nonempty array of template parameters with no qualifier!");
836
837 // Free previous template parameters (if any).
838 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000839 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000840 TemplParamLists = 0;
841 NumTemplParamLists = 0;
842 }
843 // Set info on matched template parameter lists (if any).
844 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000845 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000846 NumTemplParamLists = NumTPLists;
847 for (unsigned i = NumTPLists; i-- > 0; )
848 TemplParamLists[i] = TPLists[i];
849 }
850}
851
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000852//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000853// VarDecl Implementation
854//===----------------------------------------------------------------------===//
855
Sebastian Redl833ef452010-01-26 22:01:41 +0000856const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
857 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000858 case SC_None: break;
859 case SC_Auto: return "auto"; break;
860 case SC_Extern: return "extern"; break;
861 case SC_PrivateExtern: return "__private_extern__"; break;
862 case SC_Register: return "register"; break;
863 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000864 }
865
866 assert(0 && "Invalid storage class");
867 return 0;
868}
869
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000870VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000871 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000872 StorageClass S, StorageClass SCAsWritten) {
873 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000874}
875
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000876SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000877 SourceLocation Start = getTypeSpecStartLoc();
878 if (Start.isInvalid())
879 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000880 return Start;
881}
882
883SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000884 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000885 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
886 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000887}
888
Sebastian Redl833ef452010-01-26 22:01:41 +0000889bool VarDecl::isExternC() const {
890 ASTContext &Context = getASTContext();
891 if (!Context.getLangOptions().CPlusPlus)
892 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000893 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000894 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
895
896 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
897 DC = DC->getParent()) {
898 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
899 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000900 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000901
902 break;
903 }
904
905 if (DC->isFunctionOrMethod())
906 return false;
907 }
908
909 return false;
910}
911
912VarDecl *VarDecl::getCanonicalDecl() {
913 return getFirstDeclaration();
914}
915
Sebastian Redl35351a92010-01-31 22:27:38 +0000916VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
917 // C++ [basic.def]p2:
918 // A declaration is a definition unless [...] it contains the 'extern'
919 // specifier or a linkage-specification and neither an initializer [...],
920 // it declares a static data member in a class declaration [...].
921 // C++ [temp.expl.spec]p15:
922 // An explicit specialization of a static data member of a template is a
923 // definition if the declaration includes an initializer; otherwise, it is
924 // a declaration.
925 if (isStaticDataMember()) {
926 if (isOutOfLine() && (hasInit() ||
927 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
928 return Definition;
929 else
930 return DeclarationOnly;
931 }
932 // C99 6.7p5:
933 // A definition of an identifier is a declaration for that identifier that
934 // [...] causes storage to be reserved for that object.
935 // Note: that applies for all non-file-scope objects.
936 // C99 6.9.2p1:
937 // If the declaration of an identifier for an object has file scope and an
938 // initializer, the declaration is an external definition for the identifier
939 if (hasInit())
940 return Definition;
941 // AST for 'extern "C" int foo;' is annotated with 'extern'.
942 if (hasExternalStorage())
943 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000944
John McCall8e7d6562010-08-26 03:08:43 +0000945 if (getStorageClassAsWritten() == SC_Extern ||
946 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000947 for (const VarDecl *PrevVar = getPreviousDeclaration();
948 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
949 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
950 return DeclarationOnly;
951 }
952 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000953 // C99 6.9.2p2:
954 // A declaration of an object that has file scope without an initializer,
955 // and without a storage class specifier or the scs 'static', constitutes
956 // a tentative definition.
957 // No such thing in C++.
958 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
959 return TentativeDefinition;
960
961 // What's left is (in C, block-scope) declarations without initializers or
962 // external storage. These are definitions.
963 return Definition;
964}
965
Sebastian Redl35351a92010-01-31 22:27:38 +0000966VarDecl *VarDecl::getActingDefinition() {
967 DefinitionKind Kind = isThisDeclarationADefinition();
968 if (Kind != TentativeDefinition)
969 return 0;
970
Chris Lattner48eb14d2010-06-14 18:31:46 +0000971 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000972 VarDecl *First = getFirstDeclaration();
973 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
974 I != E; ++I) {
975 Kind = (*I)->isThisDeclarationADefinition();
976 if (Kind == Definition)
977 return 0;
978 else if (Kind == TentativeDefinition)
979 LastTentative = *I;
980 }
981 return LastTentative;
982}
983
984bool VarDecl::isTentativeDefinitionNow() const {
985 DefinitionKind Kind = isThisDeclarationADefinition();
986 if (Kind != TentativeDefinition)
987 return false;
988
989 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
990 if ((*I)->isThisDeclarationADefinition() == Definition)
991 return false;
992 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000993 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000994}
995
Sebastian Redl5ca79842010-02-01 20:16:42 +0000996VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000997 VarDecl *First = getFirstDeclaration();
998 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
999 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001000 if ((*I)->isThisDeclarationADefinition() == Definition)
1001 return *I;
1002 }
1003 return 0;
1004}
1005
John McCall37bb6c92010-10-29 22:22:43 +00001006VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1007 DefinitionKind Kind = DeclarationOnly;
1008
1009 const VarDecl *First = getFirstDeclaration();
1010 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1011 I != E; ++I)
1012 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1013
1014 return Kind;
1015}
1016
Sebastian Redl5ca79842010-02-01 20:16:42 +00001017const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001018 redecl_iterator I = redecls_begin(), E = redecls_end();
1019 while (I != E && !I->getInit())
1020 ++I;
1021
1022 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001023 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001024 return I->getInit();
1025 }
1026 return 0;
1027}
1028
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001029bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001030 if (Decl::isOutOfLine())
1031 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001032
1033 if (!isStaticDataMember())
1034 return false;
1035
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001036 // If this static data member was instantiated from a static data member of
1037 // a class template, check whether that static data member was defined
1038 // out-of-line.
1039 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1040 return VD->isOutOfLine();
1041
1042 return false;
1043}
1044
Douglas Gregor1d957a32009-10-27 18:42:08 +00001045VarDecl *VarDecl::getOutOfLineDefinition() {
1046 if (!isStaticDataMember())
1047 return 0;
1048
1049 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1050 RD != RDEnd; ++RD) {
1051 if (RD->getLexicalDeclContext()->isFileContext())
1052 return *RD;
1053 }
1054
1055 return 0;
1056}
1057
Douglas Gregord5058122010-02-11 01:19:42 +00001058void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001059 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1060 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001061 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001062 }
1063
1064 Init = I;
1065}
1066
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001067VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001068 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001069 return cast<VarDecl>(MSI->getInstantiatedFrom());
1070
1071 return 0;
1072}
1073
Douglas Gregor3c74d412009-10-14 20:14:33 +00001074TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001075 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001076 return MSI->getTemplateSpecializationKind();
1077
1078 return TSK_Undeclared;
1079}
1080
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001081MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001082 return getASTContext().getInstantiatedFromStaticDataMember(this);
1083}
1084
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001085void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1086 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001087 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001088 assert(MSI && "Not an instantiated static data member?");
1089 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001090 if (TSK != TSK_ExplicitSpecialization &&
1091 PointOfInstantiation.isValid() &&
1092 MSI->getPointOfInstantiation().isInvalid())
1093 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001094}
1095
Sebastian Redl833ef452010-01-26 22:01:41 +00001096//===----------------------------------------------------------------------===//
1097// ParmVarDecl Implementation
1098//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001099
Sebastian Redl833ef452010-01-26 22:01:41 +00001100ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1101 SourceLocation L, IdentifierInfo *Id,
1102 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001103 StorageClass S, StorageClass SCAsWritten,
1104 Expr *DefArg) {
1105 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1106 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001107}
1108
Sebastian Redl833ef452010-01-26 22:01:41 +00001109Expr *ParmVarDecl::getDefaultArg() {
1110 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1111 assert(!hasUninstantiatedDefaultArg() &&
1112 "Default argument is not yet instantiated!");
1113
1114 Expr *Arg = getInit();
1115 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1116 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001117
Sebastian Redl833ef452010-01-26 22:01:41 +00001118 return Arg;
1119}
1120
1121unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1122 if (const CXXExprWithTemporaries *E =
1123 dyn_cast<CXXExprWithTemporaries>(getInit()))
1124 return E->getNumTemporaries();
1125
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001126 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001127}
1128
Sebastian Redl833ef452010-01-26 22:01:41 +00001129CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1130 assert(getNumDefaultArgTemporaries() &&
1131 "Default arguments does not have any temporaries!");
1132
1133 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1134 return E->getTemporary(i);
1135}
1136
1137SourceRange ParmVarDecl::getDefaultArgRange() const {
1138 if (const Expr *E = getInit())
1139 return E->getSourceRange();
1140
1141 if (hasUninstantiatedDefaultArg())
1142 return getUninstantiatedDefaultArg()->getSourceRange();
1143
1144 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001145}
1146
Nuno Lopes394ec982008-12-17 23:39:55 +00001147//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001148// FunctionDecl Implementation
1149//===----------------------------------------------------------------------===//
1150
John McCalle1f2ec22009-09-11 06:45:03 +00001151void FunctionDecl::getNameForDiagnostic(std::string &S,
1152 const PrintingPolicy &Policy,
1153 bool Qualified) const {
1154 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1155 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1156 if (TemplateArgs)
1157 S += TemplateSpecializationType::PrintTemplateArgumentList(
1158 TemplateArgs->getFlatArgumentList(),
1159 TemplateArgs->flat_size(),
1160 Policy);
1161
1162}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001163
Ted Kremenek186a0742010-04-29 16:49:01 +00001164bool FunctionDecl::isVariadic() const {
1165 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1166 return FT->isVariadic();
1167 return false;
1168}
1169
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001170bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1171 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1172 if (I->Body) {
1173 Definition = *I;
1174 return true;
1175 }
1176 }
1177
1178 return false;
1179}
1180
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001181Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001182 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1183 if (I->Body) {
1184 Definition = *I;
1185 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001186 }
1187 }
1188
1189 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001190}
1191
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001192void FunctionDecl::setBody(Stmt *B) {
1193 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001194 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001195 EndRangeLoc = B->getLocEnd();
1196}
1197
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001198void FunctionDecl::setPure(bool P) {
1199 IsPure = P;
1200 if (P)
1201 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1202 Parent->markedVirtualFunctionPure();
1203}
1204
Douglas Gregor16618f22009-09-12 00:17:51 +00001205bool FunctionDecl::isMain() const {
1206 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001207 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001208 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001209 getIdentifier() && getIdentifier()->isStr("main");
1210}
1211
Douglas Gregor16618f22009-09-12 00:17:51 +00001212bool FunctionDecl::isExternC() const {
1213 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001214 // In C, any non-static, non-overloadable function has external
1215 // linkage.
1216 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001217 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001218
Mike Stump11289f42009-09-09 15:08:12 +00001219 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001220 DC = DC->getParent()) {
1221 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1222 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001223 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001224 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001225
1226 break;
1227 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001228
1229 if (DC->isRecord())
1230 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001231 }
1232
Douglas Gregorbff62032010-10-21 16:57:46 +00001233 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001234}
1235
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001236bool FunctionDecl::isGlobal() const {
1237 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1238 return Method->isStatic();
1239
John McCall8e7d6562010-08-26 03:08:43 +00001240 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001241 return false;
1242
Mike Stump11289f42009-09-09 15:08:12 +00001243 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001244 DC->isNamespace();
1245 DC = DC->getParent()) {
1246 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1247 if (!Namespace->getDeclName())
1248 return false;
1249 break;
1250 }
1251 }
1252
1253 return true;
1254}
1255
Sebastian Redl833ef452010-01-26 22:01:41 +00001256void
1257FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1258 redeclarable_base::setPreviousDeclaration(PrevDecl);
1259
1260 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1261 FunctionTemplateDecl *PrevFunTmpl
1262 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1263 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1264 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1265 }
1266}
1267
1268const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1269 return getFirstDeclaration();
1270}
1271
1272FunctionDecl *FunctionDecl::getCanonicalDecl() {
1273 return getFirstDeclaration();
1274}
1275
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001276/// \brief Returns a value indicating whether this function
1277/// corresponds to a builtin function.
1278///
1279/// The function corresponds to a built-in function if it is
1280/// declared at translation scope or within an extern "C" block and
1281/// its name matches with the name of a builtin. The returned value
1282/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001283/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001284/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001285unsigned FunctionDecl::getBuiltinID() const {
1286 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001287 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1288 return 0;
1289
1290 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1291 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1292 return BuiltinID;
1293
1294 // This function has the name of a known C library
1295 // function. Determine whether it actually refers to the C library
1296 // function or whether it just has the same name.
1297
Douglas Gregora908e7f2009-02-17 03:23:10 +00001298 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001299 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001300 return 0;
1301
Douglas Gregore711f702009-02-14 18:57:46 +00001302 // If this function is at translation-unit scope and we're not in
1303 // C++, it refers to the C library function.
1304 if (!Context.getLangOptions().CPlusPlus &&
1305 getDeclContext()->isTranslationUnit())
1306 return BuiltinID;
1307
1308 // If the function is in an extern "C" linkage specification and is
1309 // not marked "overloadable", it's the real function.
1310 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001311 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001312 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001313 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001314 return BuiltinID;
1315
1316 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001317 return 0;
1318}
1319
1320
Chris Lattner47c0d002009-04-25 06:03:53 +00001321/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001322/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001323/// after it has been created.
1324unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001325 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001326 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001327 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001328 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001329
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001330}
1331
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001332void FunctionDecl::setParams(ASTContext &C,
1333 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001334 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001335 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001336
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001337 // Zero params -> null pointer.
1338 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001339 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001340 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001341 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001342
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001343 // Update source range. The check below allows us to set EndRangeLoc before
1344 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001345 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001346 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001347 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001348}
Chris Lattner41943152007-01-25 04:52:46 +00001349
Chris Lattner58258242008-04-10 02:22:51 +00001350/// getMinRequiredArguments - Returns the minimum number of arguments
1351/// needed to call this function. This may be fewer than the number of
1352/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001353/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001354unsigned FunctionDecl::getMinRequiredArguments() const {
1355 unsigned NumRequiredArgs = getNumParams();
1356 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001357 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001358 --NumRequiredArgs;
1359
1360 return NumRequiredArgs;
1361}
1362
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001363bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001364 // FIXME: This is not enough. Consider:
1365 //
1366 // inline void f();
1367 // void f() { }
1368 //
1369 // f is inlined, but does not have inline specified.
1370 // To fix this we should add an 'inline' flag to FunctionDecl.
1371 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001372 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001373
1374 if (isa<CXXMethodDecl>(this)) {
1375 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1376 return true;
1377 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001378
1379 switch (getTemplateSpecializationKind()) {
1380 case TSK_Undeclared:
1381 case TSK_ExplicitSpecialization:
1382 return false;
1383
1384 case TSK_ImplicitInstantiation:
1385 case TSK_ExplicitInstantiationDeclaration:
1386 case TSK_ExplicitInstantiationDefinition:
1387 // Handle below.
1388 break;
1389 }
1390
1391 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001392 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001393 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001394 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001395
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001396 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001397 return PatternDecl->isInlined();
1398
1399 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001400}
1401
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001402/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001403/// definition will be externally visible.
1404///
1405/// Inline function definitions are always available for inlining optimizations.
1406/// However, depending on the language dialect, declaration specifiers, and
1407/// attributes, the definition of an inline function may or may not be
1408/// "externally" visible to other translation units in the program.
1409///
1410/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001411/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001412/// inline definition becomes externally visible (C99 6.7.4p6).
1413///
1414/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1415/// definition, we use the GNU semantics for inline, which are nearly the
1416/// opposite of C99 semantics. In particular, "inline" by itself will create
1417/// an externally visible symbol, but "extern inline" will not create an
1418/// externally visible symbol.
1419bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1420 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001421 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001422 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001423
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001424 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001425 // GNU inline semantics. Based on a number of examples, we came up with the
1426 // following heuristic: if the "inline" keyword is present on a
1427 // declaration of the function but "extern" is not present on that
1428 // declaration, then the symbol is externally visible. Otherwise, the GNU
1429 // "extern inline" semantics applies and the symbol is not externally
1430 // visible.
1431 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1432 Redecl != RedeclEnd;
1433 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001434 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001435 return true;
1436 }
1437
1438 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001439 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001440 }
1441
1442 // C99 6.7.4p6:
1443 // [...] If all of the file scope declarations for a function in a
1444 // translation unit include the inline function specifier without extern,
1445 // then the definition in that translation unit is an inline definition.
1446 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1447 Redecl != RedeclEnd;
1448 ++Redecl) {
1449 // Only consider file-scope declarations in this test.
1450 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1451 continue;
1452
John McCall8e7d6562010-08-26 03:08:43 +00001453 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001454 return true; // Not an inline definition
1455 }
1456
1457 // C99 6.7.4p6:
1458 // An inline definition does not provide an external definition for the
1459 // function, and does not forbid an external definition in another
1460 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001461 return false;
1462}
1463
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001464/// getOverloadedOperator - Which C++ overloaded operator this
1465/// function represents, if any.
1466OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001467 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1468 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001469 else
1470 return OO_None;
1471}
1472
Alexis Huntc88db062010-01-13 09:01:02 +00001473/// getLiteralIdentifier - The literal suffix identifier this function
1474/// represents, if any.
1475const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1476 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1477 return getDeclName().getCXXLiteralIdentifier();
1478 else
1479 return 0;
1480}
1481
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001482FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1483 if (TemplateOrSpecialization.isNull())
1484 return TK_NonTemplate;
1485 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1486 return TK_FunctionTemplate;
1487 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1488 return TK_MemberSpecialization;
1489 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1490 return TK_FunctionTemplateSpecialization;
1491 if (TemplateOrSpecialization.is
1492 <DependentFunctionTemplateSpecializationInfo*>())
1493 return TK_DependentFunctionTemplateSpecialization;
1494
1495 assert(false && "Did we miss a TemplateOrSpecialization type?");
1496 return TK_NonTemplate;
1497}
1498
Douglas Gregord801b062009-10-07 23:56:10 +00001499FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001500 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001501 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1502
1503 return 0;
1504}
1505
Douglas Gregor06db9f52009-10-12 20:18:28 +00001506MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1507 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1508}
1509
Douglas Gregord801b062009-10-07 23:56:10 +00001510void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001511FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1512 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001513 TemplateSpecializationKind TSK) {
1514 assert(TemplateOrSpecialization.isNull() &&
1515 "Member function is already a specialization");
1516 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001517 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001518 TemplateOrSpecialization = Info;
1519}
1520
Douglas Gregorafca3b42009-10-27 20:53:28 +00001521bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001522 // If the function is invalid, it can't be implicitly instantiated.
1523 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001524 return false;
1525
1526 switch (getTemplateSpecializationKind()) {
1527 case TSK_Undeclared:
1528 case TSK_ExplicitSpecialization:
1529 case TSK_ExplicitInstantiationDefinition:
1530 return false;
1531
1532 case TSK_ImplicitInstantiation:
1533 return true;
1534
1535 case TSK_ExplicitInstantiationDeclaration:
1536 // Handled below.
1537 break;
1538 }
1539
1540 // Find the actual template from which we will instantiate.
1541 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001542 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001543 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001544 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001545
1546 // C++0x [temp.explicit]p9:
1547 // Except for inline functions, other explicit instantiation declarations
1548 // have the effect of suppressing the implicit instantiation of the entity
1549 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001550 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001551 return true;
1552
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001553 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001554}
1555
1556FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1557 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1558 while (Primary->getInstantiatedFromMemberTemplate()) {
1559 // If we have hit a point where the user provided a specialization of
1560 // this template, we're done looking.
1561 if (Primary->isMemberSpecialization())
1562 break;
1563
1564 Primary = Primary->getInstantiatedFromMemberTemplate();
1565 }
1566
1567 return Primary->getTemplatedDecl();
1568 }
1569
1570 return getInstantiatedFromMemberFunction();
1571}
1572
Douglas Gregor70d83e22009-06-29 17:30:29 +00001573FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001574 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001575 = TemplateOrSpecialization
1576 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001577 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001578 }
1579 return 0;
1580}
1581
1582const TemplateArgumentList *
1583FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001584 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001585 = TemplateOrSpecialization
1586 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001587 return Info->TemplateArguments;
1588 }
1589 return 0;
1590}
1591
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001592const TemplateArgumentListInfo *
1593FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1594 if (FunctionTemplateSpecializationInfo *Info
1595 = TemplateOrSpecialization
1596 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1597 return Info->TemplateArgumentsAsWritten;
1598 }
1599 return 0;
1600}
1601
Mike Stump11289f42009-09-09 15:08:12 +00001602void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001603FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1604 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001605 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001606 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001607 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001608 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1609 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001610 assert(TSK != TSK_Undeclared &&
1611 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001612 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001613 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001614 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001615 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1616 TemplateArgs,
1617 TemplateArgsAsWritten,
1618 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001619 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001620
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001621 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001622 // function template specializations.
1623 if (InsertPos)
1624 Template->getSpecializations().InsertNode(Info, InsertPos);
1625 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001626 // Try to insert the new node. If there is an existing node, leave it, the
1627 // set will contain the canonical decls while
1628 // FunctionTemplateDecl::findSpecialization will return
1629 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001630 FunctionTemplateSpecializationInfo *Existing
1631 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001632 (void)Existing;
1633 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1634 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001635 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001636}
1637
John McCallb9c78482010-04-08 09:05:18 +00001638void
1639FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1640 const UnresolvedSetImpl &Templates,
1641 const TemplateArgumentListInfo &TemplateArgs) {
1642 assert(TemplateOrSpecialization.isNull());
1643 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1644 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001645 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001646 void *Buffer = Context.Allocate(Size);
1647 DependentFunctionTemplateSpecializationInfo *Info =
1648 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1649 TemplateArgs);
1650 TemplateOrSpecialization = Info;
1651}
1652
1653DependentFunctionTemplateSpecializationInfo::
1654DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1655 const TemplateArgumentListInfo &TArgs)
1656 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1657
1658 d.NumTemplates = Ts.size();
1659 d.NumArgs = TArgs.size();
1660
1661 FunctionTemplateDecl **TsArray =
1662 const_cast<FunctionTemplateDecl**>(getTemplates());
1663 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1664 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1665
1666 TemplateArgumentLoc *ArgsArray =
1667 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1668 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1669 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1670}
1671
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001672TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001673 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001674 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001675 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001676 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001677 if (FTSInfo)
1678 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001679
Douglas Gregord801b062009-10-07 23:56:10 +00001680 MemberSpecializationInfo *MSInfo
1681 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1682 if (MSInfo)
1683 return MSInfo->getTemplateSpecializationKind();
1684
1685 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001686}
1687
Mike Stump11289f42009-09-09 15:08:12 +00001688void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001689FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1690 SourceLocation PointOfInstantiation) {
1691 if (FunctionTemplateSpecializationInfo *FTSInfo
1692 = TemplateOrSpecialization.dyn_cast<
1693 FunctionTemplateSpecializationInfo*>()) {
1694 FTSInfo->setTemplateSpecializationKind(TSK);
1695 if (TSK != TSK_ExplicitSpecialization &&
1696 PointOfInstantiation.isValid() &&
1697 FTSInfo->getPointOfInstantiation().isInvalid())
1698 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1699 } else if (MemberSpecializationInfo *MSInfo
1700 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1701 MSInfo->setTemplateSpecializationKind(TSK);
1702 if (TSK != TSK_ExplicitSpecialization &&
1703 PointOfInstantiation.isValid() &&
1704 MSInfo->getPointOfInstantiation().isInvalid())
1705 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1706 } else
1707 assert(false && "Function cannot have a template specialization kind");
1708}
1709
1710SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001711 if (FunctionTemplateSpecializationInfo *FTSInfo
1712 = TemplateOrSpecialization.dyn_cast<
1713 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001714 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001715 else if (MemberSpecializationInfo *MSInfo
1716 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001717 return MSInfo->getPointOfInstantiation();
1718
1719 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001720}
1721
Douglas Gregor6411b922009-09-11 20:15:17 +00001722bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001723 if (Decl::isOutOfLine())
1724 return true;
1725
1726 // If this function was instantiated from a member function of a
1727 // class template, check whether that member function was defined out-of-line.
1728 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1729 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001730 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001731 return Definition->isOutOfLine();
1732 }
1733
1734 // If this function was instantiated from a function template,
1735 // check whether that function template was defined out-of-line.
1736 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1737 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001738 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001739 return Definition->isOutOfLine();
1740 }
1741
1742 return false;
1743}
1744
Chris Lattner59a25942008-03-31 00:36:02 +00001745//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001746// FieldDecl Implementation
1747//===----------------------------------------------------------------------===//
1748
1749FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1750 IdentifierInfo *Id, QualType T,
1751 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1752 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1753}
1754
1755bool FieldDecl::isAnonymousStructOrUnion() const {
1756 if (!isImplicit() || getDeclName())
1757 return false;
1758
1759 if (const RecordType *Record = getType()->getAs<RecordType>())
1760 return Record->getDecl()->isAnonymousStructOrUnion();
1761
1762 return false;
1763}
1764
1765//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001766// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001767//===----------------------------------------------------------------------===//
1768
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001769SourceLocation TagDecl::getOuterLocStart() const {
1770 return getTemplateOrInnerLocStart(this);
1771}
1772
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001773SourceRange TagDecl::getSourceRange() const {
1774 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001775 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001776}
1777
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001778TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001779 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001780}
1781
Douglas Gregora72a4e32010-05-19 18:39:18 +00001782void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1783 TypedefDeclOrQualifier = TDD;
1784 if (TypeForDecl)
1785 TypeForDecl->ClearLinkageCache();
1786}
1787
Douglas Gregordee1be82009-01-17 00:42:38 +00001788void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001789 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001790
1791 if (isa<CXXRecordDecl>(this)) {
1792 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1793 struct CXXRecordDecl::DefinitionData *Data =
1794 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001795 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1796 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001797 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001798}
1799
1800void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001801 assert((!isa<CXXRecordDecl>(this) ||
1802 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1803 "definition completed but not started");
1804
Douglas Gregordee1be82009-01-17 00:42:38 +00001805 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001806 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001807
1808 if (ASTMutationListener *L = getASTMutationListener())
1809 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001810}
1811
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001812TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001813 if (isDefinition())
1814 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001815 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1816 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001817
1818 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001819 R != REnd; ++R)
1820 if (R->isDefinition())
1821 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001822
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001823 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001824}
1825
John McCall3e11ebe2010-03-15 10:12:16 +00001826void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1827 SourceRange QualifierRange) {
1828 if (Qualifier) {
1829 // Make sure the extended qualifier info is allocated.
1830 if (!hasExtInfo())
1831 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1832 // Set qualifier info.
1833 getExtInfo()->NNS = Qualifier;
1834 getExtInfo()->NNSRange = QualifierRange;
1835 }
1836 else {
1837 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1838 assert(QualifierRange.isInvalid());
1839 if (hasExtInfo()) {
1840 getASTContext().Deallocate(getExtInfo());
1841 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1842 }
1843 }
1844}
1845
Ted Kremenek21475702008-09-05 17:16:31 +00001846//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001847// EnumDecl Implementation
1848//===----------------------------------------------------------------------===//
1849
1850EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1851 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001852 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1853 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1854 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001855 C.getTypeDeclType(Enum, PrevDecl);
1856 return Enum;
1857}
1858
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001859EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001860 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1861 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001862}
1863
Douglas Gregord5058122010-02-11 01:19:42 +00001864void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001865 QualType NewPromotionType,
1866 unsigned NumPositiveBits,
1867 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001868 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001869 if (!IntegerType)
1870 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001871 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001872 setNumPositiveBits(NumPositiveBits);
1873 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001874 TagDecl::completeDefinition();
1875}
1876
1877//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001878// RecordDecl Implementation
1879//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001880
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001881RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001882 IdentifierInfo *Id, RecordDecl *PrevDecl,
1883 SourceLocation TKL)
1884 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001885 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001886 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001887 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001888 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001889 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001890}
1891
1892RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001893 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001894 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001895
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001896 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001897 C.getTypeDeclType(R, PrevDecl);
1898 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001899}
1900
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001901RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1902 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1903 SourceLocation());
1904}
1905
Douglas Gregordfcad112009-03-25 15:59:44 +00001906bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001907 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001908 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1909}
1910
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001911RecordDecl::field_iterator RecordDecl::field_begin() const {
1912 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1913 LoadFieldsFromExternalStorage();
1914
1915 return field_iterator(decl_iterator(FirstDecl));
1916}
1917
Douglas Gregor91f84212008-12-11 16:49:14 +00001918/// completeDefinition - Notes that the definition of this type is now
1919/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001920void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001921 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001922 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001923}
Steve Naroffcc321422007-03-26 23:09:51 +00001924
John McCall61925b02010-05-21 01:17:40 +00001925ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1926 // Force the decl chain to come into existence properly.
1927 if (!getNextDeclInContext()) getParent()->decls_begin();
1928
1929 assert(isAnonymousStructOrUnion());
1930 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1931 assert(D->getType()->isRecordType());
1932 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1933 return D;
1934}
1935
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001936void RecordDecl::LoadFieldsFromExternalStorage() const {
1937 ExternalASTSource *Source = getASTContext().getExternalSource();
1938 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1939
1940 // Notify that we have a RecordDecl doing some initialization.
1941 ExternalASTSource::Deserializing TheFields(Source);
1942
1943 llvm::SmallVector<Decl*, 64> Decls;
1944 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1945 return;
1946
1947#ifndef NDEBUG
1948 // Check that all decls we got were FieldDecls.
1949 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1950 assert(isa<FieldDecl>(Decls[i]));
1951#endif
1952
1953 LoadedFieldsFromExternalStorage = true;
1954
1955 if (Decls.empty())
1956 return;
1957
1958 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1959}
1960
Steve Naroff415d3d52008-10-08 17:01:13 +00001961//===----------------------------------------------------------------------===//
1962// BlockDecl Implementation
1963//===----------------------------------------------------------------------===//
1964
Douglas Gregord5058122010-02-11 01:19:42 +00001965void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001966 unsigned NParms) {
1967 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001968
Steve Naroffc4b30e52009-03-13 16:56:44 +00001969 // Zero params -> null pointer.
1970 if (NParms) {
1971 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001972 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001973 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1974 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1975 }
1976}
1977
1978unsigned BlockDecl::getNumParams() const {
1979 return NumParams;
1980}
Sebastian Redl833ef452010-01-26 22:01:41 +00001981
1982
1983//===----------------------------------------------------------------------===//
1984// Other Decl Allocation/Deallocation Method Implementations
1985//===----------------------------------------------------------------------===//
1986
1987TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1988 return new (C) TranslationUnitDecl(C);
1989}
1990
1991NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1992 SourceLocation L, IdentifierInfo *Id) {
1993 return new (C) NamespaceDecl(DC, L, Id);
1994}
1995
Douglas Gregor417e87c2010-10-27 19:49:05 +00001996NamespaceDecl *NamespaceDecl::getNextNamespace() {
1997 return dyn_cast_or_null<NamespaceDecl>(
1998 NextNamespace.get(getASTContext().getExternalSource()));
1999}
2000
Sebastian Redl833ef452010-01-26 22:01:41 +00002001ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2002 SourceLocation L, IdentifierInfo *Id, QualType T) {
2003 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2004}
2005
2006FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002007 const DeclarationNameInfo &NameInfo,
2008 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002009 StorageClass S, StorageClass SCAsWritten,
2010 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002011 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002012 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00002013 New->HasWrittenPrototype = hasWrittenPrototype;
2014 return New;
2015}
2016
2017BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2018 return new (C) BlockDecl(DC, L);
2019}
2020
2021EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2022 SourceLocation L,
2023 IdentifierInfo *Id, QualType T,
2024 Expr *E, const llvm::APSInt &V) {
2025 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2026}
2027
Douglas Gregorbe996932010-09-01 20:41:53 +00002028SourceRange EnumConstantDecl::getSourceRange() const {
2029 SourceLocation End = getLocation();
2030 if (Init)
2031 End = Init->getLocEnd();
2032 return SourceRange(getLocation(), End);
2033}
2034
Sebastian Redl833ef452010-01-26 22:01:41 +00002035TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2036 SourceLocation L, IdentifierInfo *Id,
2037 TypeSourceInfo *TInfo) {
2038 return new (C) TypedefDecl(DC, L, Id, TInfo);
2039}
2040
Sebastian Redl833ef452010-01-26 22:01:41 +00002041FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2042 SourceLocation L,
2043 StringLiteral *Str) {
2044 return new (C) FileScopeAsmDecl(DC, L, Str);
2045}