blob: dbd14e067a36ddbbb3552f2ffc6d1c2a77594498 [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
65typedef std::pair<Linkage,Visibility> LVPair;
66static LVPair merge(LVPair L, LVPair R) {
67 return LVPair(minLinkage(L.first, R.first),
68 minVisibility(L.second, R.second));
69}
70
Douglas Gregor7dc5c172010-02-03 09:33:45 +000071/// \brief Get the most restrictive linkage for the types in the given
72/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +000073static LVPair
74getLVForTemplateParameterList(const TemplateParameterList *Params) {
75 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000076 for (TemplateParameterList::const_iterator P = Params->begin(),
77 PEnd = Params->end();
78 P != PEnd; ++P) {
79 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
80 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +000081 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +000082 continue;
83 }
84
85 if (TemplateTemplateParmDecl *TTP
86 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCall457a04e2010-10-22 21:05:15 +000087 LV =
88 merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +000089 }
90 }
91
John McCall457a04e2010-10-22 21:05:15 +000092 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +000093}
94
95/// \brief Get the most restrictive linkage for the types and
96/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +000097static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
98 unsigned NumArgs) {
99 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000100
101 for (unsigned I = 0; I != NumArgs; ++I) {
102 switch (Args[I].getKind()) {
103 case TemplateArgument::Null:
104 case TemplateArgument::Integral:
105 case TemplateArgument::Expression:
106 break;
107
108 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000109 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000110 break;
111
112 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000113 // The decl can validly be null as the representation of nullptr
114 // arguments, valid only in C++0x.
115 if (Decl *D = Args[I].getAsDecl()) {
116 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
117 LV = merge(LV, ND->getLinkageAndVisibility());
118 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
119 LV = merge(LV, VD->getType()->getLinkageAndVisibility());
120 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000121 break;
122
123 case TemplateArgument::Template:
John McCall457a04e2010-10-22 21:05:15 +0000124 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
125 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000126 break;
127
128 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000129 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
130 Args[I].pack_size()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000131 break;
132 }
133 }
134
John McCall457a04e2010-10-22 21:05:15 +0000135 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000136}
137
John McCall457a04e2010-10-22 21:05:15 +0000138static LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
139 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
140 TArgs.flat_size());
John McCall8823c652010-08-13 08:35:10 +0000141}
142
John McCall37bb6c92010-10-29 22:22:43 +0000143/// Answers whether the given class, or any containing class, has an
144/// explicit visibility attribute.
145static bool HasExplicitVisibilityInHierarchy(const RecordDecl *RD) {
146 if (RD->hasAttr<VisibilityAttr>()) return true;
147 if (const RecordDecl *Parent = dyn_cast<RecordDecl>(RD->getDeclContext()))
148 return HasExplicitVisibilityInHierarchy(Parent);
149 return false;
150}
151
John McCall033caa52010-10-29 00:29:13 +0000152/// getLVForDecl - Get the cached linkage and visibility for the given
153/// declaration.
154///
John McCall37bb6c92010-10-29 22:22:43 +0000155/// \param ConsiderGlobalVisibility - Whether to honor global visibility
156/// settings. This is generally false when computing the visibility
157/// of the context of a declaration.
158static LVPair getLVForDecl(const NamedDecl *D, bool ConsiderGlobalVisibility);
John McCall033caa52010-10-29 00:29:13 +0000159
160static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D,
John McCall37bb6c92010-10-29 22:22:43 +0000161 bool ConsiderGlobalVisibility) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000162 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000163 "Not a name having namespace scope");
164 ASTContext &Context = D->getASTContext();
165
166 // C++ [basic.link]p3:
167 // A name having namespace scope (3.3.6) has internal linkage if it
168 // is the name of
169 // - an object, reference, function or function template that is
170 // explicitly declared static; or,
171 // (This bullet corresponds to C99 6.2.2p3.)
172 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
173 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000174 if (Var->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000175 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000176
177 // - an object or reference that is explicitly declared const
178 // and neither explicitly declared extern nor previously
179 // declared to have external linkage; or
180 // (there is no equivalent in C99)
181 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000182 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000183 Var->getStorageClass() != SC_Extern &&
184 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000185 bool FoundExtern = false;
186 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
187 PrevVar && !FoundExtern;
188 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000189 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000190 FoundExtern = true;
191
192 if (!FoundExtern)
John McCall457a04e2010-10-22 21:05:15 +0000193 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000194 }
195 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000196 // C++ [temp]p4:
197 // A non-member function template can have internal linkage; any
198 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000199 const FunctionDecl *Function = 0;
200 if (const FunctionTemplateDecl *FunTmpl
201 = dyn_cast<FunctionTemplateDecl>(D))
202 Function = FunTmpl->getTemplatedDecl();
203 else
204 Function = cast<FunctionDecl>(D);
205
206 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000207 if (Function->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000208 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000209 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
210 // - a data member of an anonymous union.
211 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCall457a04e2010-10-22 21:05:15 +0000212 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000213 }
214
John McCall457a04e2010-10-22 21:05:15 +0000215 if (D->isInAnonymousNamespace())
216 return LVPair(UniqueExternalLinkage, DefaultVisibility);
217
John McCallb7139c42010-10-28 04:18:25 +0000218 const VisibilityAttr *ExplicitVisibility = GetExplicitVisibility(D);
219
John McCall457a04e2010-10-22 21:05:15 +0000220 // Set up the defaults.
221
222 // C99 6.2.2p5:
223 // If the declaration of an identifier for an object has file
224 // scope and no storage-class specifier, its linkage is
225 // external.
226 LVPair LV(ExternalLinkage, DefaultVisibility);
227
Douglas Gregorf73b2822009-11-25 22:24:25 +0000228 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000229
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 // A name having namespace scope has external linkage if it is the
231 // name of
232 //
233 // - an object or reference, unless it has internal linkage; or
234 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000235 bool isDeclaration = (Var->hasDefinition() == VarDecl::DeclarationOnly);
236
237 // GCC applies the following optimization to variables and static
238 // data members, but not to functions:
239 //
John McCall457a04e2010-10-22 21:05:15 +0000240 // Modify the variable's LV by the LV of its type unless this is
241 // C or extern "C". This follows from [basic.link]p9:
242 // A type without linkage shall not be used as the type of a
243 // variable or function with external linkage unless
244 // - the entity has C language linkage, or
245 // - the entity is declared within an unnamed namespace, or
246 // - the entity is not used or is defined in the same
247 // translation unit.
248 // and [basic.link]p10:
249 // ...the types specified by all declarations referring to a
250 // given variable or function shall be identical...
251 // C does not have an equivalent rule.
252 //
John McCall5fe84122010-10-26 04:59:26 +0000253 // Ignore this if we've got an explicit attribute; the user
254 // probably knows what they're doing.
255 //
John McCall457a04e2010-10-22 21:05:15 +0000256 // Note that we don't want to make the variable non-external
257 // because of this, but unique-external linkage suits us.
John McCall37bb6c92010-10-29 22:22:43 +0000258 if (Context.getLangOptions().CPlusPlus && !ExplicitVisibility &&
259 !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000260 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
261 if (TypeLV.first != ExternalLinkage)
262 return LVPair(UniqueExternalLinkage, DefaultVisibility);
John McCall37bb6c92010-10-29 22:22:43 +0000263
264 // Otherwise, ignore type visibility for declarations.
265 if (!isDeclaration)
266 LV.second = minVisibility(LV.second, TypeLV.second);
267 }
268
269 // Don't consider -fvisibility for pure declarations.
270 if (isDeclaration) {
271 ConsiderGlobalVisibility = false;
John McCall457a04e2010-10-22 21:05:15 +0000272 }
273
Douglas Gregorf73b2822009-11-25 22:24:25 +0000274 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000275 (Var->getStorageClass() == SC_Extern ||
276 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000277 if (Var->getStorageClass() == SC_PrivateExtern)
278 LV.second = HiddenVisibility;
279
Douglas Gregorf73b2822009-11-25 22:24:25 +0000280 // C99 6.2.2p4:
281 // For an identifier declared with the storage-class specifier
282 // extern in a scope in which a prior declaration of that
283 // identifier is visible, if the prior declaration specifies
284 // internal or external linkage, the linkage of the identifier
285 // at the later declaration is the same as the linkage
286 // specified at the prior declaration. If no prior declaration
287 // is visible, or if the prior declaration specifies no
288 // linkage, then the identifier has external linkage.
289 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000290 LVPair PrevLV = PrevVar->getLinkageAndVisibility();
291 if (PrevLV.first) LV.first = PrevLV.first;
292 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000293 }
294 }
295
Douglas Gregorf73b2822009-11-25 22:24:25 +0000296 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000297 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000298 // In theory, we can modify the function's LV by the LV of its
299 // type unless it has C linkage (see comment above about variables
300 // for justification). In practice, GCC doesn't do this, so it's
301 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000302
Douglas Gregorf73b2822009-11-25 22:24:25 +0000303 // C99 6.2.2p5:
304 // If the declaration of an identifier for a function has no
305 // storage-class specifier, its linkage is determined exactly
306 // as if it were declared with the storage-class specifier
307 // extern.
308 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000309 (Function->getStorageClass() == SC_Extern ||
310 Function->getStorageClass() == SC_PrivateExtern ||
311 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000312 // C99 6.2.2p4:
313 // For an identifier declared with the storage-class specifier
314 // extern in a scope in which a prior declaration of that
315 // identifier is visible, if the prior declaration specifies
316 // internal or external linkage, the linkage of the identifier
317 // at the later declaration is the same as the linkage
318 // specified at the prior declaration. If no prior declaration
319 // is visible, or if the prior declaration specifies no
320 // linkage, then the identifier has external linkage.
321 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000322 LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
323 if (PrevLV.first) LV.first = PrevLV.first;
324 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000325 }
326 }
327
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000328 if (FunctionTemplateSpecializationInfo *SpecInfo
329 = Function->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000330 LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000331 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCall457a04e2010-10-22 21:05:15 +0000332 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
333
334 if (SpecInfo->getTemplateSpecializationKind()
335 == TSK_ExplicitInstantiationDeclaration)
John McCall37bb6c92010-10-29 22:22:43 +0000336 ConsiderGlobalVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000337 }
338
John McCall37bb6c92010-10-29 22:22:43 +0000339 // -fvisibility only applies to function definitions.
340 if (ConsiderGlobalVisibility)
341 ConsiderGlobalVisibility = Function->hasBody();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000342
343 // - a named class (Clause 9), or an unnamed class defined in a
344 // typedef declaration in which the class has the typedef name
345 // for linkage purposes (7.1.3); or
346 // - a named enumeration (7.2), or an unnamed enumeration
347 // defined in a typedef declaration in which the enumeration
348 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000349 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
350 // Unnamed tags have no linkage.
351 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
352 return LVPair(NoLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000353
John McCall457a04e2010-10-22 21:05:15 +0000354 // If this is a class template specialization, consider the
355 // linkage of the template and template arguments.
356 if (const ClassTemplateSpecializationDecl *Spec
357 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
358 // From the template. Note below the restrictions on how we
359 // compute template visibility.
360 LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000361
John McCall457a04e2010-10-22 21:05:15 +0000362 // The arguments at which the template was instantiated.
363 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
364 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
365
366 if (Spec->getTemplateSpecializationKind()
367 == TSK_ExplicitInstantiationDeclaration)
John McCall37bb6c92010-10-29 22:22:43 +0000368 ConsiderGlobalVisibility = false;
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 McCall37bb6c92010-10-29 22:22:43 +0000372 if (ConsiderGlobalVisibility)
373 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)) {
379 LVPair EnumLV =
380 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
381 if (!isExternalLinkage(EnumLV.first))
382 return LVPair(NoLinkage, DefaultVisibility);
383 LV = merge(LV, 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)) {
388 LV = merge(LV, getLVForTemplateParameterList(
389 Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000390
John McCall457a04e2010-10-22 21:05:15 +0000391 // We do not want to consider attributes or global settings when
392 // computing template visibility.
393 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000394
395 // - a namespace (7.3), unless it is declared within an unnamed
396 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000397 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
398 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000399
John McCall457a04e2010-10-22 21:05:15 +0000400 // By extension, we assign external linkage to Objective-C
401 // interfaces.
402 } else if (isa<ObjCInterfaceDecl>(D)) {
403 // fallout
404
405 // Everything not covered here has no linkage.
406 } else {
407 return LVPair(NoLinkage, DefaultVisibility);
408 }
409
410 // If we ended up with non-external linkage, visibility should
411 // always be default.
412 if (LV.first != ExternalLinkage)
413 return LVPair(LV.first, DefaultVisibility);
414
415 // If we didn't end up with hidden visibility, consider attributes
416 // and -fvisibility.
417 if (LV.second != HiddenVisibility) {
418 Visibility StandardV;
419
420 // If we have an explicit visibility attribute, merge that in.
John McCallb7139c42010-10-28 04:18:25 +0000421 if (ExplicitVisibility)
422 StandardV = GetVisibilityFromAttr(ExplicitVisibility);
John McCall37bb6c92010-10-29 22:22:43 +0000423 else if (ConsiderGlobalVisibility)
John McCall457a04e2010-10-22 21:05:15 +0000424 StandardV = Context.getLangOptions().getVisibilityMode();
425 else
426 StandardV = DefaultVisibility; // no-op
427
428 LV.second = minVisibility(LV.second, StandardV);
429 }
430
431 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000432}
433
John McCall033caa52010-10-29 00:29:13 +0000434static LVPair getLVForClassMember(const NamedDecl *D,
John McCall37bb6c92010-10-29 22:22:43 +0000435 bool ConsiderGlobalVisibility) {
John McCall457a04e2010-10-22 21:05:15 +0000436 // Only certain class members have linkage. Note that fields don't
437 // really have linkage, but it's convenient to say they do for the
438 // purposes of calculating linkage of pointer-to-data-member
439 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000440 if (!(isa<CXXMethodDecl>(D) ||
441 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000442 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000443 (isa<TagDecl>(D) &&
444 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCall457a04e2010-10-22 21:05:15 +0000445 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000446
447 // Class members only have linkage if their class has external linkage.
John McCall37bb6c92010-10-29 22:22:43 +0000448 // Always ignore global visibility settings during this.
449 LVPair ClassLV = getLVForDecl(cast<RecordDecl>(D->getDeclContext()), false);
John McCall457a04e2010-10-22 21:05:15 +0000450 if (!isExternalLinkage(ClassLV.first))
451 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000452
453 // If the class already has unique-external linkage, we can't improve.
John McCall457a04e2010-10-22 21:05:15 +0000454 if (ClassLV.first == UniqueExternalLinkage)
455 return LVPair(UniqueExternalLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000456
John McCall457a04e2010-10-22 21:05:15 +0000457 // Start with the class's linkage and visibility.
458 LVPair LV = ClassLV;
John McCall457a04e2010-10-22 21:05:15 +0000459
John McCall37bb6c92010-10-29 22:22:43 +0000460 // If we have an explicit visibility attribute, merge that in and
461 // ignore global visibility settings.
462 const VisibilityAttr *VA = GetExplicitVisibility(D);
463 if (VA) {
464 LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
465 ConsiderGlobalVisibility = false;
John McCall457a04e2010-10-22 21:05:15 +0000466 }
467
John McCall37bb6c92010-10-29 22:22:43 +0000468 bool HasExplicitVisibility = (VA ||
469 HasExplicitVisibilityInHierarchy(cast<RecordDecl>(D->getDeclContext())));
470
John McCall8823c652010-08-13 08:35:10 +0000471 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000472 TemplateSpecializationKind TSK = TSK_Undeclared;
473
John McCall457a04e2010-10-22 21:05:15 +0000474 // If this is a method template specialization, use the linkage for
475 // the template parameters and arguments.
476 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000477 = MD->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000478 LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
479 LV = merge(LV, getLVForTemplateParameterList(
480 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000481
482 TSK = Spec->getTemplateSpecializationKind();
483 } else if (MemberSpecializationInfo *MSI =
484 MD->getMemberSpecializationInfo()) {
485 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000486 }
487
John McCall37bb6c92010-10-29 22:22:43 +0000488 // Ignore global visibility if it's an extern template.
489 if (ConsiderGlobalVisibility)
490 ConsiderGlobalVisibility = (TSK != TSK_ExplicitInstantiationDeclaration);
491
492 // If we're paying attention to global visibility, apply
493 // -finline-visibility-hidden if this is an inline method.
494 //
495 // Note that we ignore the existence of visibility attributes
496 // on containing classes when deciding whether to do this.
497 if (ConsiderGlobalVisibility && MD->isInlined() &&
498 MD->getASTContext().getLangOptions().InlineVisibilityHidden)
John McCall457a04e2010-10-22 21:05:15 +0000499 LV.second = HiddenVisibility;
500
John McCall37bb6c92010-10-29 22:22:43 +0000501 // Note that in contrast to basically every other situation, we
502 // *do* apply -fvisibility to method declarations.
503
504 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
505 TemplateSpecializationKind TSK = TSK_Undeclared;
506
507 if (const ClassTemplateSpecializationDecl *Spec
508 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
509 // Merge template argument/parameter information for member
510 // class template specializations.
511 LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
512 LV = merge(LV, getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000513 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000514
515 TSK = Spec->getTemplateSpecializationKind();
516 } else if (MemberSpecializationInfo *MSI =
517 RD->getMemberSpecializationInfo()) {
518 TSK = MSI->getTemplateSpecializationKind();
519 }
520
521 // Ignore global visibility if it's an extern template.
522 if (ConsiderGlobalVisibility)
523 ConsiderGlobalVisibility = (TSK != TSK_ExplicitInstantiationDeclaration);
524
525 // Static data members.
526 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
527 // If we don't have explicit visibility information in the
528 // hierarchy, apply the LV from its type. See the comment about
529 // namespace-scope variables for justification for this
530 // optimization.
531 if (!HasExplicitVisibility) {
532 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
533 if (TypeLV.first != ExternalLinkage)
534 LV.first = minLinkage(LV.first, UniqueExternalLinkage);
535 LV.second = minVisibility(LV.second, TypeLV.second);
536 }
537
538 // Ignore global visibility if it's an extern template or
539 // just a declaration.
540 if (ConsiderGlobalVisibility)
541 ConsiderGlobalVisibility =
542 (VD->getDefinition() &&
543 VD->getTemplateSpecializationKind()
544 != TSK_ExplicitInstantiationDeclaration);
545 }
546
547 // Suppress -fvisibility if we have explicit visibility on any of
548 // our ancestors.
549 ConsiderGlobalVisibility &= !HasExplicitVisibility;
550
551 // Apply -fvisibility if desired.
552 if (ConsiderGlobalVisibility && LV.second != HiddenVisibility) {
553 LV.second = minVisibility(LV.second,
554 D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000555 }
556
John McCall457a04e2010-10-22 21:05:15 +0000557 return LV;
John McCall8823c652010-08-13 08:35:10 +0000558}
559
John McCall457a04e2010-10-22 21:05:15 +0000560LVPair NamedDecl::getLinkageAndVisibility() const {
John McCall033caa52010-10-29 00:29:13 +0000561 return getLVForDecl(this, /*ConsiderGlobalSettings*/ true);
562}
Ted Kremenek926d8602010-04-20 23:15:35 +0000563
John McCall033caa52010-10-29 00:29:13 +0000564static LVPair getLVForDecl(const NamedDecl *D, bool ConsiderGlobalSettings) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000565 // Objective-C: treat all Objective-C declarations as having external
566 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000567 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000568 default:
569 break;
John McCall457a04e2010-10-22 21:05:15 +0000570 case Decl::TemplateTemplateParm: // count these as external
571 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000572 case Decl::ObjCAtDefsField:
573 case Decl::ObjCCategory:
574 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000575 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000576 case Decl::ObjCForwardProtocol:
577 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000578 case Decl::ObjCMethod:
579 case Decl::ObjCProperty:
580 case Decl::ObjCPropertyImpl:
581 case Decl::ObjCProtocol:
John McCall457a04e2010-10-22 21:05:15 +0000582 return LVPair(ExternalLinkage, DefaultVisibility);
Ted Kremenek926d8602010-04-20 23:15:35 +0000583 }
584
Douglas Gregorf73b2822009-11-25 22:24:25 +0000585 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000586 if (D->getDeclContext()->getRedeclContext()->isFileContext())
587 return getLVForNamespaceScopeDecl(D, ConsiderGlobalSettings);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000588
589 // C++ [basic.link]p5:
590 // In addition, a member function, static data member, a named
591 // class or enumeration of class scope, or an unnamed class or
592 // enumeration defined in a class-scope typedef declaration such
593 // that the class or enumeration has the typedef name for linkage
594 // purposes (7.1.3), has external linkage if the name of the class
595 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000596 if (D->getDeclContext()->isRecord())
597 return getLVForClassMember(D, ConsiderGlobalSettings);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000598
599 // C++ [basic.link]p6:
600 // The name of a function declared in block scope and the name of
601 // an object declared by a block scope extern declaration have
602 // linkage. If there is a visible declaration of an entity with
603 // linkage having the same name and type, ignoring entities
604 // declared outside the innermost enclosing namespace scope, the
605 // block scope declaration declares that same entity and receives
606 // the linkage of the previous declaration. If there is more than
607 // one such matching entity, the program is ill-formed. Otherwise,
608 // if no matching entity is found, the block scope entity receives
609 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000610 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
611 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000612 if (Function->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000613 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000614
John McCall457a04e2010-10-22 21:05:15 +0000615 LVPair LV(ExternalLinkage, DefaultVisibility);
John McCallb7139c42010-10-28 04:18:25 +0000616 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCall457a04e2010-10-22 21:05:15 +0000617 LV.second = GetVisibilityFromAttr(VA);
618
619 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
620 LVPair PrevLV = Prev->getLinkageAndVisibility();
621 if (PrevLV.first) LV.first = PrevLV.first;
622 LV.second = minVisibility(LV.second, PrevLV.second);
623 }
624
625 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000626 }
627
John McCall033caa52010-10-29 00:29:13 +0000628 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000629 if (Var->getStorageClass() == SC_Extern ||
630 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000631 if (Var->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000632 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000633
John McCall457a04e2010-10-22 21:05:15 +0000634 LVPair LV(ExternalLinkage, DefaultVisibility);
635 if (Var->getStorageClass() == SC_PrivateExtern)
636 LV.second = HiddenVisibility;
John McCallb7139c42010-10-28 04:18:25 +0000637 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCall457a04e2010-10-22 21:05:15 +0000638 LV.second = GetVisibilityFromAttr(VA);
639
640 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
641 LVPair PrevLV = Prev->getLinkageAndVisibility();
642 if (PrevLV.first) LV.first = PrevLV.first;
643 LV.second = minVisibility(LV.second, PrevLV.second);
644 }
645
646 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000647 }
648 }
649
650 // C++ [basic.link]p6:
651 // Names not covered by these rules have no linkage.
John McCall457a04e2010-10-22 21:05:15 +0000652 return LVPair(NoLinkage, DefaultVisibility);
653}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000654
Douglas Gregor2ada0482009-02-04 17:27:36 +0000655std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000656 return getQualifiedNameAsString(getASTContext().getLangOptions());
657}
658
659std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000660 const DeclContext *Ctx = getDeclContext();
661
662 if (Ctx->isFunctionOrMethod())
663 return getNameAsString();
664
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000665 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
666 ContextsTy Contexts;
667
668 // Collect contexts.
669 while (Ctx && isa<NamedDecl>(Ctx)) {
670 Contexts.push_back(Ctx);
671 Ctx = Ctx->getParent();
672 };
673
674 std::string QualName;
675 llvm::raw_string_ostream OS(QualName);
676
677 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
678 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000679 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000680 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000681 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
682 std::string TemplateArgsStr
683 = TemplateSpecializationType::PrintTemplateArgumentList(
684 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000685 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000686 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000687 OS << Spec->getName() << TemplateArgsStr;
688 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000689 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000690 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000691 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000692 OS << ND;
693 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
694 if (!RD->getIdentifier())
695 OS << "<anonymous " << RD->getKindName() << '>';
696 else
697 OS << RD;
698 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000699 const FunctionProtoType *FT = 0;
700 if (FD->hasWrittenPrototype())
701 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
702
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000703 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000704 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000705 unsigned NumParams = FD->getNumParams();
706 for (unsigned i = 0; i < NumParams; ++i) {
707 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000708 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000709 std::string Param;
710 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000711 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000712 }
713
714 if (FT->isVariadic()) {
715 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000716 OS << ", ";
717 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000718 }
719 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000720 OS << ')';
721 } else {
722 OS << cast<NamedDecl>(*I);
723 }
724 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000725 }
726
John McCalla2a3f7d2010-03-16 21:48:18 +0000727 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000728 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000729 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000730 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000731
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000732 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000733}
734
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000735bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000736 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
737
Douglas Gregor889ceb72009-02-03 19:21:40 +0000738 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
739 // We want to keep it, unless it nominates same namespace.
740 if (getKind() == Decl::UsingDirective) {
741 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
742 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
743 }
Mike Stump11289f42009-09-09 15:08:12 +0000744
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000745 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
746 // For function declarations, we keep track of redeclarations.
747 return FD->getPreviousDeclaration() == OldD;
748
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000749 // For function templates, the underlying function declarations are linked.
750 if (const FunctionTemplateDecl *FunctionTemplate
751 = dyn_cast<FunctionTemplateDecl>(this))
752 if (const FunctionTemplateDecl *OldFunctionTemplate
753 = dyn_cast<FunctionTemplateDecl>(OldD))
754 return FunctionTemplate->getTemplatedDecl()
755 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000756
Steve Naroffc4173fa2009-02-22 19:35:57 +0000757 // For method declarations, we keep track of redeclarations.
758 if (isa<ObjCMethodDecl>(this))
759 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000760
John McCall9f3059a2009-10-09 21:13:30 +0000761 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
762 return true;
763
John McCall3f746822009-11-17 05:59:44 +0000764 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
765 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
766 cast<UsingShadowDecl>(OldD)->getTargetDecl();
767
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000768 // For non-function declarations, if the declarations are of the
769 // same kind then this must be a redeclaration, or semantic analysis
770 // would not have given us the new declaration.
771 return this->getKind() == OldD->getKind();
772}
773
Douglas Gregoreddf4332009-02-24 20:03:32 +0000774bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000775 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000776}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000777
Anders Carlsson6915bf62009-06-26 06:29:23 +0000778NamedDecl *NamedDecl::getUnderlyingDecl() {
779 NamedDecl *ND = this;
780 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000781 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000782 ND = UD->getTargetDecl();
783 else if (ObjCCompatibleAliasDecl *AD
784 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
785 return AD->getClassInterface();
786 else
787 return ND;
788 }
789}
790
John McCalla8ae2222010-04-06 21:38:20 +0000791bool NamedDecl::isCXXInstanceMember() const {
792 assert(isCXXClassMember() &&
793 "checking whether non-member is instance member");
794
795 const NamedDecl *D = this;
796 if (isa<UsingShadowDecl>(D))
797 D = cast<UsingShadowDecl>(D)->getTargetDecl();
798
799 if (isa<FieldDecl>(D))
800 return true;
801 if (isa<CXXMethodDecl>(D))
802 return cast<CXXMethodDecl>(D)->isInstance();
803 if (isa<FunctionTemplateDecl>(D))
804 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
805 ->getTemplatedDecl())->isInstance();
806 return false;
807}
808
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000809//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000810// DeclaratorDecl Implementation
811//===----------------------------------------------------------------------===//
812
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000813template <typename DeclT>
814static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
815 if (decl->getNumTemplateParameterLists() > 0)
816 return decl->getTemplateParameterList(0)->getTemplateLoc();
817 else
818 return decl->getInnerLocStart();
819}
820
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000821SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000822 TypeSourceInfo *TSI = getTypeSourceInfo();
823 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000824 return SourceLocation();
825}
826
John McCall3e11ebe2010-03-15 10:12:16 +0000827void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
828 SourceRange QualifierRange) {
829 if (Qualifier) {
830 // Make sure the extended decl info is allocated.
831 if (!hasExtInfo()) {
832 // Save (non-extended) type source info pointer.
833 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
834 // Allocate external info struct.
835 DeclInfo = new (getASTContext()) ExtInfo;
836 // Restore savedTInfo into (extended) decl info.
837 getExtInfo()->TInfo = savedTInfo;
838 }
839 // Set qualifier info.
840 getExtInfo()->NNS = Qualifier;
841 getExtInfo()->NNSRange = QualifierRange;
842 }
843 else {
844 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
845 assert(QualifierRange.isInvalid());
846 if (hasExtInfo()) {
847 // Save type source info pointer.
848 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
849 // Deallocate the extended decl info.
850 getASTContext().Deallocate(getExtInfo());
851 // Restore savedTInfo into (non-extended) decl info.
852 DeclInfo = savedTInfo;
853 }
854 }
855}
856
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000857SourceLocation DeclaratorDecl::getOuterLocStart() const {
858 return getTemplateOrInnerLocStart(this);
859}
860
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000861void
Douglas Gregor20527e22010-06-15 17:44:38 +0000862QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
863 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000864 TemplateParameterList **TPLists) {
865 assert((NumTPLists == 0 || TPLists != 0) &&
866 "Empty array of template parameters with positive size!");
867 assert((NumTPLists == 0 || NNS) &&
868 "Nonempty array of template parameters with no qualifier!");
869
870 // Free previous template parameters (if any).
871 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000872 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000873 TemplParamLists = 0;
874 NumTemplParamLists = 0;
875 }
876 // Set info on matched template parameter lists (if any).
877 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000878 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000879 NumTemplParamLists = NumTPLists;
880 for (unsigned i = NumTPLists; i-- > 0; )
881 TemplParamLists[i] = TPLists[i];
882 }
883}
884
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000885//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000886// VarDecl Implementation
887//===----------------------------------------------------------------------===//
888
Sebastian Redl833ef452010-01-26 22:01:41 +0000889const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
890 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000891 case SC_None: break;
892 case SC_Auto: return "auto"; break;
893 case SC_Extern: return "extern"; break;
894 case SC_PrivateExtern: return "__private_extern__"; break;
895 case SC_Register: return "register"; break;
896 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000897 }
898
899 assert(0 && "Invalid storage class");
900 return 0;
901}
902
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000903VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000904 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000905 StorageClass S, StorageClass SCAsWritten) {
906 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000907}
908
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000909SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000910 SourceLocation Start = getTypeSpecStartLoc();
911 if (Start.isInvalid())
912 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000913 return Start;
914}
915
916SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000917 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000918 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
919 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000920}
921
Sebastian Redl833ef452010-01-26 22:01:41 +0000922bool VarDecl::isExternC() const {
923 ASTContext &Context = getASTContext();
924 if (!Context.getLangOptions().CPlusPlus)
925 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000926 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000927 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
928
929 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
930 DC = DC->getParent()) {
931 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
932 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000933 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000934
935 break;
936 }
937
938 if (DC->isFunctionOrMethod())
939 return false;
940 }
941
942 return false;
943}
944
945VarDecl *VarDecl::getCanonicalDecl() {
946 return getFirstDeclaration();
947}
948
Sebastian Redl35351a92010-01-31 22:27:38 +0000949VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
950 // C++ [basic.def]p2:
951 // A declaration is a definition unless [...] it contains the 'extern'
952 // specifier or a linkage-specification and neither an initializer [...],
953 // it declares a static data member in a class declaration [...].
954 // C++ [temp.expl.spec]p15:
955 // An explicit specialization of a static data member of a template is a
956 // definition if the declaration includes an initializer; otherwise, it is
957 // a declaration.
958 if (isStaticDataMember()) {
959 if (isOutOfLine() && (hasInit() ||
960 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
961 return Definition;
962 else
963 return DeclarationOnly;
964 }
965 // C99 6.7p5:
966 // A definition of an identifier is a declaration for that identifier that
967 // [...] causes storage to be reserved for that object.
968 // Note: that applies for all non-file-scope objects.
969 // C99 6.9.2p1:
970 // If the declaration of an identifier for an object has file scope and an
971 // initializer, the declaration is an external definition for the identifier
972 if (hasInit())
973 return Definition;
974 // AST for 'extern "C" int foo;' is annotated with 'extern'.
975 if (hasExternalStorage())
976 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000977
John McCall8e7d6562010-08-26 03:08:43 +0000978 if (getStorageClassAsWritten() == SC_Extern ||
979 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000980 for (const VarDecl *PrevVar = getPreviousDeclaration();
981 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
982 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
983 return DeclarationOnly;
984 }
985 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000986 // C99 6.9.2p2:
987 // A declaration of an object that has file scope without an initializer,
988 // and without a storage class specifier or the scs 'static', constitutes
989 // a tentative definition.
990 // No such thing in C++.
991 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
992 return TentativeDefinition;
993
994 // What's left is (in C, block-scope) declarations without initializers or
995 // external storage. These are definitions.
996 return Definition;
997}
998
Sebastian Redl35351a92010-01-31 22:27:38 +0000999VarDecl *VarDecl::getActingDefinition() {
1000 DefinitionKind Kind = isThisDeclarationADefinition();
1001 if (Kind != TentativeDefinition)
1002 return 0;
1003
Chris Lattner48eb14d2010-06-14 18:31:46 +00001004 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001005 VarDecl *First = getFirstDeclaration();
1006 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1007 I != E; ++I) {
1008 Kind = (*I)->isThisDeclarationADefinition();
1009 if (Kind == Definition)
1010 return 0;
1011 else if (Kind == TentativeDefinition)
1012 LastTentative = *I;
1013 }
1014 return LastTentative;
1015}
1016
1017bool VarDecl::isTentativeDefinitionNow() const {
1018 DefinitionKind Kind = isThisDeclarationADefinition();
1019 if (Kind != TentativeDefinition)
1020 return false;
1021
1022 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1023 if ((*I)->isThisDeclarationADefinition() == Definition)
1024 return false;
1025 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001026 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001027}
1028
Sebastian Redl5ca79842010-02-01 20:16:42 +00001029VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001030 VarDecl *First = getFirstDeclaration();
1031 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1032 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001033 if ((*I)->isThisDeclarationADefinition() == Definition)
1034 return *I;
1035 }
1036 return 0;
1037}
1038
John McCall37bb6c92010-10-29 22:22:43 +00001039VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1040 DefinitionKind Kind = DeclarationOnly;
1041
1042 const VarDecl *First = getFirstDeclaration();
1043 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1044 I != E; ++I)
1045 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1046
1047 return Kind;
1048}
1049
Sebastian Redl5ca79842010-02-01 20:16:42 +00001050const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001051 redecl_iterator I = redecls_begin(), E = redecls_end();
1052 while (I != E && !I->getInit())
1053 ++I;
1054
1055 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001056 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001057 return I->getInit();
1058 }
1059 return 0;
1060}
1061
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001062bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001063 if (Decl::isOutOfLine())
1064 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001065
1066 if (!isStaticDataMember())
1067 return false;
1068
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001069 // If this static data member was instantiated from a static data member of
1070 // a class template, check whether that static data member was defined
1071 // out-of-line.
1072 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1073 return VD->isOutOfLine();
1074
1075 return false;
1076}
1077
Douglas Gregor1d957a32009-10-27 18:42:08 +00001078VarDecl *VarDecl::getOutOfLineDefinition() {
1079 if (!isStaticDataMember())
1080 return 0;
1081
1082 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1083 RD != RDEnd; ++RD) {
1084 if (RD->getLexicalDeclContext()->isFileContext())
1085 return *RD;
1086 }
1087
1088 return 0;
1089}
1090
Douglas Gregord5058122010-02-11 01:19:42 +00001091void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001092 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1093 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001094 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001095 }
1096
1097 Init = I;
1098}
1099
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001100VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001101 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001102 return cast<VarDecl>(MSI->getInstantiatedFrom());
1103
1104 return 0;
1105}
1106
Douglas Gregor3c74d412009-10-14 20:14:33 +00001107TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001108 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001109 return MSI->getTemplateSpecializationKind();
1110
1111 return TSK_Undeclared;
1112}
1113
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001114MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001115 return getASTContext().getInstantiatedFromStaticDataMember(this);
1116}
1117
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001118void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1119 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001120 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001121 assert(MSI && "Not an instantiated static data member?");
1122 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001123 if (TSK != TSK_ExplicitSpecialization &&
1124 PointOfInstantiation.isValid() &&
1125 MSI->getPointOfInstantiation().isInvalid())
1126 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001127}
1128
Sebastian Redl833ef452010-01-26 22:01:41 +00001129//===----------------------------------------------------------------------===//
1130// ParmVarDecl Implementation
1131//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001132
Sebastian Redl833ef452010-01-26 22:01:41 +00001133ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1134 SourceLocation L, IdentifierInfo *Id,
1135 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001136 StorageClass S, StorageClass SCAsWritten,
1137 Expr *DefArg) {
1138 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1139 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001140}
1141
Sebastian Redl833ef452010-01-26 22:01:41 +00001142Expr *ParmVarDecl::getDefaultArg() {
1143 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1144 assert(!hasUninstantiatedDefaultArg() &&
1145 "Default argument is not yet instantiated!");
1146
1147 Expr *Arg = getInit();
1148 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1149 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001150
Sebastian Redl833ef452010-01-26 22:01:41 +00001151 return Arg;
1152}
1153
1154unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1155 if (const CXXExprWithTemporaries *E =
1156 dyn_cast<CXXExprWithTemporaries>(getInit()))
1157 return E->getNumTemporaries();
1158
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001159 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001160}
1161
Sebastian Redl833ef452010-01-26 22:01:41 +00001162CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1163 assert(getNumDefaultArgTemporaries() &&
1164 "Default arguments does not have any temporaries!");
1165
1166 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1167 return E->getTemporary(i);
1168}
1169
1170SourceRange ParmVarDecl::getDefaultArgRange() const {
1171 if (const Expr *E = getInit())
1172 return E->getSourceRange();
1173
1174 if (hasUninstantiatedDefaultArg())
1175 return getUninstantiatedDefaultArg()->getSourceRange();
1176
1177 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001178}
1179
Nuno Lopes394ec982008-12-17 23:39:55 +00001180//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001181// FunctionDecl Implementation
1182//===----------------------------------------------------------------------===//
1183
John McCalle1f2ec22009-09-11 06:45:03 +00001184void FunctionDecl::getNameForDiagnostic(std::string &S,
1185 const PrintingPolicy &Policy,
1186 bool Qualified) const {
1187 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1188 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1189 if (TemplateArgs)
1190 S += TemplateSpecializationType::PrintTemplateArgumentList(
1191 TemplateArgs->getFlatArgumentList(),
1192 TemplateArgs->flat_size(),
1193 Policy);
1194
1195}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001196
Ted Kremenek186a0742010-04-29 16:49:01 +00001197bool FunctionDecl::isVariadic() const {
1198 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1199 return FT->isVariadic();
1200 return false;
1201}
1202
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001203bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1204 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1205 if (I->Body) {
1206 Definition = *I;
1207 return true;
1208 }
1209 }
1210
1211 return false;
1212}
1213
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001214Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001215 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1216 if (I->Body) {
1217 Definition = *I;
1218 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001219 }
1220 }
1221
1222 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001223}
1224
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001225void FunctionDecl::setBody(Stmt *B) {
1226 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001227 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001228 EndRangeLoc = B->getLocEnd();
1229}
1230
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001231void FunctionDecl::setPure(bool P) {
1232 IsPure = P;
1233 if (P)
1234 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1235 Parent->markedVirtualFunctionPure();
1236}
1237
Douglas Gregor16618f22009-09-12 00:17:51 +00001238bool FunctionDecl::isMain() const {
1239 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001240 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001241 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001242 getIdentifier() && getIdentifier()->isStr("main");
1243}
1244
Douglas Gregor16618f22009-09-12 00:17:51 +00001245bool FunctionDecl::isExternC() const {
1246 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001247 // In C, any non-static, non-overloadable function has external
1248 // linkage.
1249 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001250 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001251
Mike Stump11289f42009-09-09 15:08:12 +00001252 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001253 DC = DC->getParent()) {
1254 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1255 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001256 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001257 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001258
1259 break;
1260 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001261
1262 if (DC->isRecord())
1263 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001264 }
1265
Douglas Gregorbff62032010-10-21 16:57:46 +00001266 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001267}
1268
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001269bool FunctionDecl::isGlobal() const {
1270 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1271 return Method->isStatic();
1272
John McCall8e7d6562010-08-26 03:08:43 +00001273 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001274 return false;
1275
Mike Stump11289f42009-09-09 15:08:12 +00001276 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001277 DC->isNamespace();
1278 DC = DC->getParent()) {
1279 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1280 if (!Namespace->getDeclName())
1281 return false;
1282 break;
1283 }
1284 }
1285
1286 return true;
1287}
1288
Sebastian Redl833ef452010-01-26 22:01:41 +00001289void
1290FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1291 redeclarable_base::setPreviousDeclaration(PrevDecl);
1292
1293 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1294 FunctionTemplateDecl *PrevFunTmpl
1295 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1296 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1297 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1298 }
1299}
1300
1301const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1302 return getFirstDeclaration();
1303}
1304
1305FunctionDecl *FunctionDecl::getCanonicalDecl() {
1306 return getFirstDeclaration();
1307}
1308
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001309/// \brief Returns a value indicating whether this function
1310/// corresponds to a builtin function.
1311///
1312/// The function corresponds to a built-in function if it is
1313/// declared at translation scope or within an extern "C" block and
1314/// its name matches with the name of a builtin. The returned value
1315/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001316/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001317/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001318unsigned FunctionDecl::getBuiltinID() const {
1319 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001320 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1321 return 0;
1322
1323 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1324 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1325 return BuiltinID;
1326
1327 // This function has the name of a known C library
1328 // function. Determine whether it actually refers to the C library
1329 // function or whether it just has the same name.
1330
Douglas Gregora908e7f2009-02-17 03:23:10 +00001331 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001332 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001333 return 0;
1334
Douglas Gregore711f702009-02-14 18:57:46 +00001335 // If this function is at translation-unit scope and we're not in
1336 // C++, it refers to the C library function.
1337 if (!Context.getLangOptions().CPlusPlus &&
1338 getDeclContext()->isTranslationUnit())
1339 return BuiltinID;
1340
1341 // If the function is in an extern "C" linkage specification and is
1342 // not marked "overloadable", it's the real function.
1343 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001344 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001345 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001346 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001347 return BuiltinID;
1348
1349 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001350 return 0;
1351}
1352
1353
Chris Lattner47c0d002009-04-25 06:03:53 +00001354/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001355/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001356/// after it has been created.
1357unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001358 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001359 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001360 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001361 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001362
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001363}
1364
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001365void FunctionDecl::setParams(ASTContext &C,
1366 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001367 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001368 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001370 // Zero params -> null pointer.
1371 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001372 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001373 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001374 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001375
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001376 // Update source range. The check below allows us to set EndRangeLoc before
1377 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001378 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001379 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001380 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001381}
Chris Lattner41943152007-01-25 04:52:46 +00001382
Chris Lattner58258242008-04-10 02:22:51 +00001383/// getMinRequiredArguments - Returns the minimum number of arguments
1384/// needed to call this function. This may be fewer than the number of
1385/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001386/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001387unsigned FunctionDecl::getMinRequiredArguments() const {
1388 unsigned NumRequiredArgs = getNumParams();
1389 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001390 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001391 --NumRequiredArgs;
1392
1393 return NumRequiredArgs;
1394}
1395
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001396bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001397 // FIXME: This is not enough. Consider:
1398 //
1399 // inline void f();
1400 // void f() { }
1401 //
1402 // f is inlined, but does not have inline specified.
1403 // To fix this we should add an 'inline' flag to FunctionDecl.
1404 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001405 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001406
1407 if (isa<CXXMethodDecl>(this)) {
1408 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1409 return true;
1410 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001411
1412 switch (getTemplateSpecializationKind()) {
1413 case TSK_Undeclared:
1414 case TSK_ExplicitSpecialization:
1415 return false;
1416
1417 case TSK_ImplicitInstantiation:
1418 case TSK_ExplicitInstantiationDeclaration:
1419 case TSK_ExplicitInstantiationDefinition:
1420 // Handle below.
1421 break;
1422 }
1423
1424 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001425 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001426 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001427 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001428
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001429 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001430 return PatternDecl->isInlined();
1431
1432 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001433}
1434
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001435/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001436/// definition will be externally visible.
1437///
1438/// Inline function definitions are always available for inlining optimizations.
1439/// However, depending on the language dialect, declaration specifiers, and
1440/// attributes, the definition of an inline function may or may not be
1441/// "externally" visible to other translation units in the program.
1442///
1443/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001444/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001445/// inline definition becomes externally visible (C99 6.7.4p6).
1446///
1447/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1448/// definition, we use the GNU semantics for inline, which are nearly the
1449/// opposite of C99 semantics. In particular, "inline" by itself will create
1450/// an externally visible symbol, but "extern inline" will not create an
1451/// externally visible symbol.
1452bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1453 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001454 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001455 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001456
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001457 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001458 // GNU inline semantics. Based on a number of examples, we came up with the
1459 // following heuristic: if the "inline" keyword is present on a
1460 // declaration of the function but "extern" is not present on that
1461 // declaration, then the symbol is externally visible. Otherwise, the GNU
1462 // "extern inline" semantics applies and the symbol is not externally
1463 // visible.
1464 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1465 Redecl != RedeclEnd;
1466 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001467 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001468 return true;
1469 }
1470
1471 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001472 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001473 }
1474
1475 // C99 6.7.4p6:
1476 // [...] If all of the file scope declarations for a function in a
1477 // translation unit include the inline function specifier without extern,
1478 // then the definition in that translation unit is an inline definition.
1479 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1480 Redecl != RedeclEnd;
1481 ++Redecl) {
1482 // Only consider file-scope declarations in this test.
1483 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1484 continue;
1485
John McCall8e7d6562010-08-26 03:08:43 +00001486 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001487 return true; // Not an inline definition
1488 }
1489
1490 // C99 6.7.4p6:
1491 // An inline definition does not provide an external definition for the
1492 // function, and does not forbid an external definition in another
1493 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001494 return false;
1495}
1496
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001497/// getOverloadedOperator - Which C++ overloaded operator this
1498/// function represents, if any.
1499OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001500 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1501 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001502 else
1503 return OO_None;
1504}
1505
Alexis Huntc88db062010-01-13 09:01:02 +00001506/// getLiteralIdentifier - The literal suffix identifier this function
1507/// represents, if any.
1508const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1509 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1510 return getDeclName().getCXXLiteralIdentifier();
1511 else
1512 return 0;
1513}
1514
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001515FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1516 if (TemplateOrSpecialization.isNull())
1517 return TK_NonTemplate;
1518 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1519 return TK_FunctionTemplate;
1520 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1521 return TK_MemberSpecialization;
1522 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1523 return TK_FunctionTemplateSpecialization;
1524 if (TemplateOrSpecialization.is
1525 <DependentFunctionTemplateSpecializationInfo*>())
1526 return TK_DependentFunctionTemplateSpecialization;
1527
1528 assert(false && "Did we miss a TemplateOrSpecialization type?");
1529 return TK_NonTemplate;
1530}
1531
Douglas Gregord801b062009-10-07 23:56:10 +00001532FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001533 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001534 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1535
1536 return 0;
1537}
1538
Douglas Gregor06db9f52009-10-12 20:18:28 +00001539MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1540 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1541}
1542
Douglas Gregord801b062009-10-07 23:56:10 +00001543void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001544FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1545 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001546 TemplateSpecializationKind TSK) {
1547 assert(TemplateOrSpecialization.isNull() &&
1548 "Member function is already a specialization");
1549 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001550 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001551 TemplateOrSpecialization = Info;
1552}
1553
Douglas Gregorafca3b42009-10-27 20:53:28 +00001554bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001555 // If the function is invalid, it can't be implicitly instantiated.
1556 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001557 return false;
1558
1559 switch (getTemplateSpecializationKind()) {
1560 case TSK_Undeclared:
1561 case TSK_ExplicitSpecialization:
1562 case TSK_ExplicitInstantiationDefinition:
1563 return false;
1564
1565 case TSK_ImplicitInstantiation:
1566 return true;
1567
1568 case TSK_ExplicitInstantiationDeclaration:
1569 // Handled below.
1570 break;
1571 }
1572
1573 // Find the actual template from which we will instantiate.
1574 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001575 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001576 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001577 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001578
1579 // C++0x [temp.explicit]p9:
1580 // Except for inline functions, other explicit instantiation declarations
1581 // have the effect of suppressing the implicit instantiation of the entity
1582 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001583 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001584 return true;
1585
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001586 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001587}
1588
1589FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1590 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1591 while (Primary->getInstantiatedFromMemberTemplate()) {
1592 // If we have hit a point where the user provided a specialization of
1593 // this template, we're done looking.
1594 if (Primary->isMemberSpecialization())
1595 break;
1596
1597 Primary = Primary->getInstantiatedFromMemberTemplate();
1598 }
1599
1600 return Primary->getTemplatedDecl();
1601 }
1602
1603 return getInstantiatedFromMemberFunction();
1604}
1605
Douglas Gregor70d83e22009-06-29 17:30:29 +00001606FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001607 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001608 = TemplateOrSpecialization
1609 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001610 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001611 }
1612 return 0;
1613}
1614
1615const TemplateArgumentList *
1616FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001617 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001618 = TemplateOrSpecialization
1619 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001620 return Info->TemplateArguments;
1621 }
1622 return 0;
1623}
1624
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001625const TemplateArgumentListInfo *
1626FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1627 if (FunctionTemplateSpecializationInfo *Info
1628 = TemplateOrSpecialization
1629 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1630 return Info->TemplateArgumentsAsWritten;
1631 }
1632 return 0;
1633}
1634
Mike Stump11289f42009-09-09 15:08:12 +00001635void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001636FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1637 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001638 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001639 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001640 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001641 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1642 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001643 assert(TSK != TSK_Undeclared &&
1644 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001645 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001646 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001647 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001648 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1649 TemplateArgs,
1650 TemplateArgsAsWritten,
1651 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001652 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001653
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001654 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001655 // function template specializations.
1656 if (InsertPos)
1657 Template->getSpecializations().InsertNode(Info, InsertPos);
1658 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001659 // Try to insert the new node. If there is an existing node, leave it, the
1660 // set will contain the canonical decls while
1661 // FunctionTemplateDecl::findSpecialization will return
1662 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001663 FunctionTemplateSpecializationInfo *Existing
1664 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001665 (void)Existing;
1666 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1667 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001668 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001669}
1670
John McCallb9c78482010-04-08 09:05:18 +00001671void
1672FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1673 const UnresolvedSetImpl &Templates,
1674 const TemplateArgumentListInfo &TemplateArgs) {
1675 assert(TemplateOrSpecialization.isNull());
1676 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1677 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001678 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001679 void *Buffer = Context.Allocate(Size);
1680 DependentFunctionTemplateSpecializationInfo *Info =
1681 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1682 TemplateArgs);
1683 TemplateOrSpecialization = Info;
1684}
1685
1686DependentFunctionTemplateSpecializationInfo::
1687DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1688 const TemplateArgumentListInfo &TArgs)
1689 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1690
1691 d.NumTemplates = Ts.size();
1692 d.NumArgs = TArgs.size();
1693
1694 FunctionTemplateDecl **TsArray =
1695 const_cast<FunctionTemplateDecl**>(getTemplates());
1696 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1697 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1698
1699 TemplateArgumentLoc *ArgsArray =
1700 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1701 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1702 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1703}
1704
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001705TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001706 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001707 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001708 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001709 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001710 if (FTSInfo)
1711 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001712
Douglas Gregord801b062009-10-07 23:56:10 +00001713 MemberSpecializationInfo *MSInfo
1714 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1715 if (MSInfo)
1716 return MSInfo->getTemplateSpecializationKind();
1717
1718 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001719}
1720
Mike Stump11289f42009-09-09 15:08:12 +00001721void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001722FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1723 SourceLocation PointOfInstantiation) {
1724 if (FunctionTemplateSpecializationInfo *FTSInfo
1725 = TemplateOrSpecialization.dyn_cast<
1726 FunctionTemplateSpecializationInfo*>()) {
1727 FTSInfo->setTemplateSpecializationKind(TSK);
1728 if (TSK != TSK_ExplicitSpecialization &&
1729 PointOfInstantiation.isValid() &&
1730 FTSInfo->getPointOfInstantiation().isInvalid())
1731 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1732 } else if (MemberSpecializationInfo *MSInfo
1733 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1734 MSInfo->setTemplateSpecializationKind(TSK);
1735 if (TSK != TSK_ExplicitSpecialization &&
1736 PointOfInstantiation.isValid() &&
1737 MSInfo->getPointOfInstantiation().isInvalid())
1738 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1739 } else
1740 assert(false && "Function cannot have a template specialization kind");
1741}
1742
1743SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001744 if (FunctionTemplateSpecializationInfo *FTSInfo
1745 = TemplateOrSpecialization.dyn_cast<
1746 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001747 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001748 else if (MemberSpecializationInfo *MSInfo
1749 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001750 return MSInfo->getPointOfInstantiation();
1751
1752 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001753}
1754
Douglas Gregor6411b922009-09-11 20:15:17 +00001755bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001756 if (Decl::isOutOfLine())
1757 return true;
1758
1759 // If this function was instantiated from a member function of a
1760 // class template, check whether that member function was defined out-of-line.
1761 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1762 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001763 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001764 return Definition->isOutOfLine();
1765 }
1766
1767 // If this function was instantiated from a function template,
1768 // check whether that function template was defined out-of-line.
1769 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1770 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001771 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001772 return Definition->isOutOfLine();
1773 }
1774
1775 return false;
1776}
1777
Chris Lattner59a25942008-03-31 00:36:02 +00001778//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001779// FieldDecl Implementation
1780//===----------------------------------------------------------------------===//
1781
1782FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1783 IdentifierInfo *Id, QualType T,
1784 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1785 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1786}
1787
1788bool FieldDecl::isAnonymousStructOrUnion() const {
1789 if (!isImplicit() || getDeclName())
1790 return false;
1791
1792 if (const RecordType *Record = getType()->getAs<RecordType>())
1793 return Record->getDecl()->isAnonymousStructOrUnion();
1794
1795 return false;
1796}
1797
1798//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001799// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001800//===----------------------------------------------------------------------===//
1801
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001802SourceLocation TagDecl::getOuterLocStart() const {
1803 return getTemplateOrInnerLocStart(this);
1804}
1805
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001806SourceRange TagDecl::getSourceRange() const {
1807 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001808 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001809}
1810
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001811TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001812 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001813}
1814
Douglas Gregora72a4e32010-05-19 18:39:18 +00001815void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1816 TypedefDeclOrQualifier = TDD;
1817 if (TypeForDecl)
1818 TypeForDecl->ClearLinkageCache();
1819}
1820
Douglas Gregordee1be82009-01-17 00:42:38 +00001821void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001822 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001823
1824 if (isa<CXXRecordDecl>(this)) {
1825 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1826 struct CXXRecordDecl::DefinitionData *Data =
1827 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001828 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1829 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001830 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001831}
1832
1833void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001834 assert((!isa<CXXRecordDecl>(this) ||
1835 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1836 "definition completed but not started");
1837
Douglas Gregordee1be82009-01-17 00:42:38 +00001838 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001839 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001840
1841 if (ASTMutationListener *L = getASTMutationListener())
1842 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001843}
1844
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001845TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001846 if (isDefinition())
1847 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001848 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1849 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001850
1851 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001852 R != REnd; ++R)
1853 if (R->isDefinition())
1854 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001855
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001856 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001857}
1858
John McCall3e11ebe2010-03-15 10:12:16 +00001859void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1860 SourceRange QualifierRange) {
1861 if (Qualifier) {
1862 // Make sure the extended qualifier info is allocated.
1863 if (!hasExtInfo())
1864 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1865 // Set qualifier info.
1866 getExtInfo()->NNS = Qualifier;
1867 getExtInfo()->NNSRange = QualifierRange;
1868 }
1869 else {
1870 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1871 assert(QualifierRange.isInvalid());
1872 if (hasExtInfo()) {
1873 getASTContext().Deallocate(getExtInfo());
1874 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1875 }
1876 }
1877}
1878
Ted Kremenek21475702008-09-05 17:16:31 +00001879//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001880// EnumDecl Implementation
1881//===----------------------------------------------------------------------===//
1882
1883EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1884 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001885 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1886 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1887 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001888 C.getTypeDeclType(Enum, PrevDecl);
1889 return Enum;
1890}
1891
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001892EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001893 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1894 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001895}
1896
Douglas Gregord5058122010-02-11 01:19:42 +00001897void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001898 QualType NewPromotionType,
1899 unsigned NumPositiveBits,
1900 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001901 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001902 if (!IntegerType)
1903 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001904 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001905 setNumPositiveBits(NumPositiveBits);
1906 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001907 TagDecl::completeDefinition();
1908}
1909
1910//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001911// RecordDecl Implementation
1912//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001913
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001914RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001915 IdentifierInfo *Id, RecordDecl *PrevDecl,
1916 SourceLocation TKL)
1917 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001918 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001919 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001920 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001921 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001922 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001923}
1924
1925RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001926 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001927 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001928
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001929 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001930 C.getTypeDeclType(R, PrevDecl);
1931 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001932}
1933
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001934RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1935 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1936 SourceLocation());
1937}
1938
Douglas Gregordfcad112009-03-25 15:59:44 +00001939bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001940 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001941 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1942}
1943
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001944RecordDecl::field_iterator RecordDecl::field_begin() const {
1945 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1946 LoadFieldsFromExternalStorage();
1947
1948 return field_iterator(decl_iterator(FirstDecl));
1949}
1950
Douglas Gregor91f84212008-12-11 16:49:14 +00001951/// completeDefinition - Notes that the definition of this type is now
1952/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001953void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001954 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001955 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001956}
Steve Naroffcc321422007-03-26 23:09:51 +00001957
John McCall61925b02010-05-21 01:17:40 +00001958ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1959 // Force the decl chain to come into existence properly.
1960 if (!getNextDeclInContext()) getParent()->decls_begin();
1961
1962 assert(isAnonymousStructOrUnion());
1963 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1964 assert(D->getType()->isRecordType());
1965 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1966 return D;
1967}
1968
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001969void RecordDecl::LoadFieldsFromExternalStorage() const {
1970 ExternalASTSource *Source = getASTContext().getExternalSource();
1971 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1972
1973 // Notify that we have a RecordDecl doing some initialization.
1974 ExternalASTSource::Deserializing TheFields(Source);
1975
1976 llvm::SmallVector<Decl*, 64> Decls;
1977 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1978 return;
1979
1980#ifndef NDEBUG
1981 // Check that all decls we got were FieldDecls.
1982 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1983 assert(isa<FieldDecl>(Decls[i]));
1984#endif
1985
1986 LoadedFieldsFromExternalStorage = true;
1987
1988 if (Decls.empty())
1989 return;
1990
1991 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1992}
1993
Steve Naroff415d3d52008-10-08 17:01:13 +00001994//===----------------------------------------------------------------------===//
1995// BlockDecl Implementation
1996//===----------------------------------------------------------------------===//
1997
Douglas Gregord5058122010-02-11 01:19:42 +00001998void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001999 unsigned NParms) {
2000 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002001
Steve Naroffc4b30e52009-03-13 16:56:44 +00002002 // Zero params -> null pointer.
2003 if (NParms) {
2004 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002005 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002006 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2007 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2008 }
2009}
2010
2011unsigned BlockDecl::getNumParams() const {
2012 return NumParams;
2013}
Sebastian Redl833ef452010-01-26 22:01:41 +00002014
2015
2016//===----------------------------------------------------------------------===//
2017// Other Decl Allocation/Deallocation Method Implementations
2018//===----------------------------------------------------------------------===//
2019
2020TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2021 return new (C) TranslationUnitDecl(C);
2022}
2023
2024NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2025 SourceLocation L, IdentifierInfo *Id) {
2026 return new (C) NamespaceDecl(DC, L, Id);
2027}
2028
Douglas Gregor417e87c2010-10-27 19:49:05 +00002029NamespaceDecl *NamespaceDecl::getNextNamespace() {
2030 return dyn_cast_or_null<NamespaceDecl>(
2031 NextNamespace.get(getASTContext().getExternalSource()));
2032}
2033
Sebastian Redl833ef452010-01-26 22:01:41 +00002034ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2035 SourceLocation L, IdentifierInfo *Id, QualType T) {
2036 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2037}
2038
2039FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002040 const DeclarationNameInfo &NameInfo,
2041 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002042 StorageClass S, StorageClass SCAsWritten,
2043 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002044 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002045 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00002046 New->HasWrittenPrototype = hasWrittenPrototype;
2047 return New;
2048}
2049
2050BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2051 return new (C) BlockDecl(DC, L);
2052}
2053
2054EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2055 SourceLocation L,
2056 IdentifierInfo *Id, QualType T,
2057 Expr *E, const llvm::APSInt &V) {
2058 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2059}
2060
Douglas Gregorbe996932010-09-01 20:41:53 +00002061SourceRange EnumConstantDecl::getSourceRange() const {
2062 SourceLocation End = getLocation();
2063 if (Init)
2064 End = Init->getLocEnd();
2065 return SourceRange(getLocation(), End);
2066}
2067
Sebastian Redl833ef452010-01-26 22:01:41 +00002068TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2069 SourceLocation L, IdentifierInfo *Id,
2070 TypeSourceInfo *TInfo) {
2071 return new (C) TypedefDecl(DC, L, Id, TInfo);
2072}
2073
Sebastian Redl833ef452010-01-26 22:01:41 +00002074FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2075 SourceLocation L,
2076 StringLiteral *Str) {
2077 return new (C) FileScopeAsmDecl(DC, L, Str);
2078}