blob: f7fd7b8d5a9e708b1a0f9135d79e8c529f50b64d [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 McCall033caa52010-10-29 00:29:13 +0000143/// getLVForDecl - Get the cached linkage and visibility for the given
144/// declaration.
145///
146/// \param ConsiderGlobalSettings - Whether to honor global visibility
147/// settings. This is false when computing the visibility of the
148/// context of a declaration with an explicit visibility attribute.
149static LVPair getLVForDecl(const NamedDecl *D, bool ConsiderGlobalSettings);
150
151static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D,
152 bool ConsiderGlobalSettings) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000153 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000154 "Not a name having namespace scope");
155 ASTContext &Context = D->getASTContext();
156
157 // C++ [basic.link]p3:
158 // A name having namespace scope (3.3.6) has internal linkage if it
159 // is the name of
160 // - an object, reference, function or function template that is
161 // explicitly declared static; or,
162 // (This bullet corresponds to C99 6.2.2p3.)
163 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
164 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000165 if (Var->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000166 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000167
168 // - an object or reference that is explicitly declared const
169 // and neither explicitly declared extern nor previously
170 // declared to have external linkage; or
171 // (there is no equivalent in C99)
172 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000173 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000174 Var->getStorageClass() != SC_Extern &&
175 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000176 bool FoundExtern = false;
177 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
178 PrevVar && !FoundExtern;
179 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000180 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000181 FoundExtern = true;
182
183 if (!FoundExtern)
John McCall457a04e2010-10-22 21:05:15 +0000184 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000185 }
186 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000187 // C++ [temp]p4:
188 // A non-member function template can have internal linkage; any
189 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000190 const FunctionDecl *Function = 0;
191 if (const FunctionTemplateDecl *FunTmpl
192 = dyn_cast<FunctionTemplateDecl>(D))
193 Function = FunTmpl->getTemplatedDecl();
194 else
195 Function = cast<FunctionDecl>(D);
196
197 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000198 if (Function->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000199 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000200 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
201 // - a data member of an anonymous union.
202 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCall457a04e2010-10-22 21:05:15 +0000203 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000204 }
205
John McCall457a04e2010-10-22 21:05:15 +0000206 if (D->isInAnonymousNamespace())
207 return LVPair(UniqueExternalLinkage, DefaultVisibility);
208
John McCallb7139c42010-10-28 04:18:25 +0000209 const VisibilityAttr *ExplicitVisibility = GetExplicitVisibility(D);
210
John McCall457a04e2010-10-22 21:05:15 +0000211 // Set up the defaults.
212
213 // C99 6.2.2p5:
214 // If the declaration of an identifier for an object has file
215 // scope and no storage-class specifier, its linkage is
216 // external.
217 LVPair LV(ExternalLinkage, DefaultVisibility);
218
219 // We ignore -fvisibility on non-definitions and explicit
220 // instantiation declarations.
John McCall033caa52010-10-29 00:29:13 +0000221 bool ConsiderDashFVisibility = ConsiderGlobalSettings;
John McCall457a04e2010-10-22 21:05:15 +0000222
Douglas Gregorf73b2822009-11-25 22:24:25 +0000223 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000224
Douglas Gregorf73b2822009-11-25 22:24:25 +0000225 // A name having namespace scope has external linkage if it is the
226 // name of
227 //
228 // - an object or reference, unless it has internal linkage; or
229 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000230 // Modify the variable's LV by the LV of its type unless this is
231 // C or extern "C". This follows from [basic.link]p9:
232 // A type without linkage shall not be used as the type of a
233 // variable or function with external linkage unless
234 // - the entity has C language linkage, or
235 // - the entity is declared within an unnamed namespace, or
236 // - the entity is not used or is defined in the same
237 // translation unit.
238 // and [basic.link]p10:
239 // ...the types specified by all declarations referring to a
240 // given variable or function shall be identical...
241 // C does not have an equivalent rule.
242 //
John McCall5fe84122010-10-26 04:59:26 +0000243 // Ignore this if we've got an explicit attribute; the user
244 // probably knows what they're doing.
245 //
John McCall457a04e2010-10-22 21:05:15 +0000246 // Note that we don't want to make the variable non-external
247 // because of this, but unique-external linkage suits us.
John McCall5fe84122010-10-26 04:59:26 +0000248 if (Context.getLangOptions().CPlusPlus && !Var->isExternC() &&
John McCallb7139c42010-10-28 04:18:25 +0000249 !ExplicitVisibility) {
John McCall457a04e2010-10-22 21:05:15 +0000250 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
251 if (TypeLV.first != ExternalLinkage)
252 return LVPair(UniqueExternalLinkage, DefaultVisibility);
253 LV.second = minVisibility(LV.second, TypeLV.second);
254 }
255
Douglas Gregorf73b2822009-11-25 22:24:25 +0000256 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000257 (Var->getStorageClass() == SC_Extern ||
258 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000259 if (Var->getStorageClass() == SC_PrivateExtern)
260 LV.second = HiddenVisibility;
261
Douglas Gregorf73b2822009-11-25 22:24:25 +0000262 // C99 6.2.2p4:
263 // For an identifier declared with the storage-class specifier
264 // extern in a scope in which a prior declaration of that
265 // identifier is visible, if the prior declaration specifies
266 // internal or external linkage, the linkage of the identifier
267 // at the later declaration is the same as the linkage
268 // specified at the prior declaration. If no prior declaration
269 // is visible, or if the prior declaration specifies no
270 // linkage, then the identifier has external linkage.
271 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000272 LVPair PrevLV = PrevVar->getLinkageAndVisibility();
273 if (PrevLV.first) LV.first = PrevLV.first;
274 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000275 }
276 }
277
Douglas Gregorf73b2822009-11-25 22:24:25 +0000278 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000279 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000280 // In theory, we can modify the function's LV by the LV of its
281 // type unless it has C linkage (see comment above about variables
282 // for justification). In practice, GCC doesn't do this, so it's
283 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000284
Douglas Gregorf73b2822009-11-25 22:24:25 +0000285 // C99 6.2.2p5:
286 // If the declaration of an identifier for a function has no
287 // storage-class specifier, its linkage is determined exactly
288 // as if it were declared with the storage-class specifier
289 // extern.
290 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000291 (Function->getStorageClass() == SC_Extern ||
292 Function->getStorageClass() == SC_PrivateExtern ||
293 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000294 // C99 6.2.2p4:
295 // For an identifier declared with the storage-class specifier
296 // extern in a scope in which a prior declaration of that
297 // identifier is visible, if the prior declaration specifies
298 // internal or external linkage, the linkage of the identifier
299 // at the later declaration is the same as the linkage
300 // specified at the prior declaration. If no prior declaration
301 // is visible, or if the prior declaration specifies no
302 // linkage, then the identifier has external linkage.
303 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000304 LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
305 if (PrevLV.first) LV.first = PrevLV.first;
306 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000307 }
308 }
309
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000310 if (FunctionTemplateSpecializationInfo *SpecInfo
311 = Function->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000312 LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000313 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCall457a04e2010-10-22 21:05:15 +0000314 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
315
316 if (SpecInfo->getTemplateSpecializationKind()
317 == TSK_ExplicitInstantiationDeclaration)
318 ConsiderDashFVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000319 }
320
John McCall457a04e2010-10-22 21:05:15 +0000321 if (ConsiderDashFVisibility)
322 ConsiderDashFVisibility = Function->hasBody();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000323
324 // - a named class (Clause 9), or an unnamed class defined in a
325 // typedef declaration in which the class has the typedef name
326 // for linkage purposes (7.1.3); or
327 // - a named enumeration (7.2), or an unnamed enumeration
328 // defined in a typedef declaration in which the enumeration
329 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000330 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
331 // Unnamed tags have no linkage.
332 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
333 return LVPair(NoLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000334
John McCall457a04e2010-10-22 21:05:15 +0000335 // If this is a class template specialization, consider the
336 // linkage of the template and template arguments.
337 if (const ClassTemplateSpecializationDecl *Spec
338 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
339 // From the template. Note below the restrictions on how we
340 // compute template visibility.
341 LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000342
John McCall457a04e2010-10-22 21:05:15 +0000343 // The arguments at which the template was instantiated.
344 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
345 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
346
347 if (Spec->getTemplateSpecializationKind()
348 == TSK_ExplicitInstantiationDeclaration)
349 ConsiderDashFVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000350 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000351
John McCall5fe84122010-10-26 04:59:26 +0000352 // Consider -fvisibility unless the type has C linkage.
John McCall457a04e2010-10-22 21:05:15 +0000353 if (ConsiderDashFVisibility)
John McCall5fe84122010-10-26 04:59:26 +0000354 ConsiderDashFVisibility =
355 (Context.getLangOptions().CPlusPlus &&
356 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000357
Douglas Gregorf73b2822009-11-25 22:24:25 +0000358 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000359 } else if (isa<EnumConstantDecl>(D)) {
360 LVPair EnumLV =
361 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
362 if (!isExternalLinkage(EnumLV.first))
363 return LVPair(NoLinkage, DefaultVisibility);
364 LV = merge(LV, EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000365
366 // - a template, unless it is a function template that has
367 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000368 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
369 LV = merge(LV, getLVForTemplateParameterList(
370 Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000371
John McCall457a04e2010-10-22 21:05:15 +0000372 // We do not want to consider attributes or global settings when
373 // computing template visibility.
374 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000375
376 // - a namespace (7.3), unless it is declared within an unnamed
377 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000378 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
379 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000380
John McCall457a04e2010-10-22 21:05:15 +0000381 // By extension, we assign external linkage to Objective-C
382 // interfaces.
383 } else if (isa<ObjCInterfaceDecl>(D)) {
384 // fallout
385
386 // Everything not covered here has no linkage.
387 } else {
388 return LVPair(NoLinkage, DefaultVisibility);
389 }
390
391 // If we ended up with non-external linkage, visibility should
392 // always be default.
393 if (LV.first != ExternalLinkage)
394 return LVPair(LV.first, DefaultVisibility);
395
396 // If we didn't end up with hidden visibility, consider attributes
397 // and -fvisibility.
398 if (LV.second != HiddenVisibility) {
399 Visibility StandardV;
400
401 // If we have an explicit visibility attribute, merge that in.
John McCallb7139c42010-10-28 04:18:25 +0000402 if (ExplicitVisibility)
403 StandardV = GetVisibilityFromAttr(ExplicitVisibility);
John McCall457a04e2010-10-22 21:05:15 +0000404 else if (ConsiderDashFVisibility)
405 StandardV = Context.getLangOptions().getVisibilityMode();
406 else
407 StandardV = DefaultVisibility; // no-op
408
409 LV.second = minVisibility(LV.second, StandardV);
410 }
411
412 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000413}
414
John McCall033caa52010-10-29 00:29:13 +0000415static LVPair getLVForClassMember(const NamedDecl *D,
416 bool ConsiderGlobalSettings) {
John McCall457a04e2010-10-22 21:05:15 +0000417 // Only certain class members have linkage. Note that fields don't
418 // really have linkage, but it's convenient to say they do for the
419 // purposes of calculating linkage of pointer-to-data-member
420 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000421 if (!(isa<CXXMethodDecl>(D) ||
422 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000423 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000424 (isa<TagDecl>(D) &&
425 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCall457a04e2010-10-22 21:05:15 +0000426 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000427
John McCall033caa52010-10-29 00:29:13 +0000428 // If we have an explicit visibility attribute, merge that in.
429 const VisibilityAttr *VA = GetExplicitVisibility(D);
430
John McCall8823c652010-08-13 08:35:10 +0000431 // Class members only have linkage if their class has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000432 LVPair ClassLV = getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
433 ConsiderGlobalSettings && !VA);
John McCall457a04e2010-10-22 21:05:15 +0000434 if (!isExternalLinkage(ClassLV.first))
435 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000436
437 // If the class already has unique-external linkage, we can't improve.
John McCall457a04e2010-10-22 21:05:15 +0000438 if (ClassLV.first == UniqueExternalLinkage)
439 return LVPair(UniqueExternalLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000440
John McCall457a04e2010-10-22 21:05:15 +0000441 // Start with the class's linkage and visibility.
442 LVPair LV = ClassLV;
John McCall457a04e2010-10-22 21:05:15 +0000443 if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
444
John McCall2efaf112010-10-28 07:07:52 +0000445 // If it's a variable declaration and we don't have an explicit
446 // visibility attribute, apply the LV from its type.
John McCall457a04e2010-10-22 21:05:15 +0000447 // See the comment about namespace-scope variable decls above.
John McCall2efaf112010-10-28 07:07:52 +0000448 if (!VA && isa<VarDecl>(D)) {
449 LVPair TypeLV = cast<VarDecl>(D)->getType()->getLinkageAndVisibility();
John McCall457a04e2010-10-22 21:05:15 +0000450 if (TypeLV.first != ExternalLinkage)
451 LV.first = minLinkage(LV.first, UniqueExternalLinkage);
452 LV.second = minVisibility(LV.second, TypeLV.second);
453 }
454
John McCall8823c652010-08-13 08:35:10 +0000455 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000456 // If this is a method template specialization, use the linkage for
457 // the template parameters and arguments.
458 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000459 = MD->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000460 LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
461 LV = merge(LV, getLVForTemplateParameterList(
462 Spec->getTemplate()->getTemplateParameters()));
John McCall8823c652010-08-13 08:35:10 +0000463 }
464
John McCall457a04e2010-10-22 21:05:15 +0000465 // If -fvisibility-inlines-hidden was provided, then inline C++
466 // member functions get "hidden" visibility if they don't have an
467 // explicit visibility attribute.
John McCall033caa52010-10-29 00:29:13 +0000468 if (ConsiderGlobalSettings && !VA && MD->isInlined() &&
469 LV.second > HiddenVisibility &&
John McCall62b68622010-10-28 18:10:36 +0000470 D->getASTContext().getLangOptions().InlineVisibilityHidden &&
471 MD->getTemplateSpecializationKind()
472 != TSK_ExplicitInstantiationDeclaration)
John McCall457a04e2010-10-22 21:05:15 +0000473 LV.second = HiddenVisibility;
474
John McCall8823c652010-08-13 08:35:10 +0000475 // Similarly for member class template specializations.
476 } else if (const ClassTemplateSpecializationDecl *Spec
477 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000478 LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
479 LV = merge(LV, getLVForTemplateParameterList(
480 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall8823c652010-08-13 08:35:10 +0000481 }
482
John McCall457a04e2010-10-22 21:05:15 +0000483 return LV;
John McCall8823c652010-08-13 08:35:10 +0000484}
485
John McCall457a04e2010-10-22 21:05:15 +0000486LVPair NamedDecl::getLinkageAndVisibility() const {
John McCall033caa52010-10-29 00:29:13 +0000487 return getLVForDecl(this, /*ConsiderGlobalSettings*/ true);
488}
Ted Kremenek926d8602010-04-20 23:15:35 +0000489
John McCall033caa52010-10-29 00:29:13 +0000490static LVPair getLVForDecl(const NamedDecl *D, bool ConsiderGlobalSettings) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000491 // Objective-C: treat all Objective-C declarations as having external
492 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000493 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000494 default:
495 break;
John McCall457a04e2010-10-22 21:05:15 +0000496 case Decl::TemplateTemplateParm: // count these as external
497 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000498 case Decl::ObjCAtDefsField:
499 case Decl::ObjCCategory:
500 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000501 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000502 case Decl::ObjCForwardProtocol:
503 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000504 case Decl::ObjCMethod:
505 case Decl::ObjCProperty:
506 case Decl::ObjCPropertyImpl:
507 case Decl::ObjCProtocol:
John McCall457a04e2010-10-22 21:05:15 +0000508 return LVPair(ExternalLinkage, DefaultVisibility);
Ted Kremenek926d8602010-04-20 23:15:35 +0000509 }
510
Douglas Gregorf73b2822009-11-25 22:24:25 +0000511 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000512 if (D->getDeclContext()->getRedeclContext()->isFileContext())
513 return getLVForNamespaceScopeDecl(D, ConsiderGlobalSettings);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000514
515 // C++ [basic.link]p5:
516 // In addition, a member function, static data member, a named
517 // class or enumeration of class scope, or an unnamed class or
518 // enumeration defined in a class-scope typedef declaration such
519 // that the class or enumeration has the typedef name for linkage
520 // purposes (7.1.3), has external linkage if the name of the class
521 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000522 if (D->getDeclContext()->isRecord())
523 return getLVForClassMember(D, ConsiderGlobalSettings);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000524
525 // C++ [basic.link]p6:
526 // The name of a function declared in block scope and the name of
527 // an object declared by a block scope extern declaration have
528 // linkage. If there is a visible declaration of an entity with
529 // linkage having the same name and type, ignoring entities
530 // declared outside the innermost enclosing namespace scope, the
531 // block scope declaration declares that same entity and receives
532 // the linkage of the previous declaration. If there is more than
533 // one such matching entity, the program is ill-formed. Otherwise,
534 // if no matching entity is found, the block scope entity receives
535 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000536 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
537 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000538 if (Function->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000539 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000540
John McCall457a04e2010-10-22 21:05:15 +0000541 LVPair LV(ExternalLinkage, DefaultVisibility);
John McCallb7139c42010-10-28 04:18:25 +0000542 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCall457a04e2010-10-22 21:05:15 +0000543 LV.second = GetVisibilityFromAttr(VA);
544
545 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
546 LVPair PrevLV = Prev->getLinkageAndVisibility();
547 if (PrevLV.first) LV.first = PrevLV.first;
548 LV.second = minVisibility(LV.second, PrevLV.second);
549 }
550
551 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000552 }
553
John McCall033caa52010-10-29 00:29:13 +0000554 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000555 if (Var->getStorageClass() == SC_Extern ||
556 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000557 if (Var->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000558 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000559
John McCall457a04e2010-10-22 21:05:15 +0000560 LVPair LV(ExternalLinkage, DefaultVisibility);
561 if (Var->getStorageClass() == SC_PrivateExtern)
562 LV.second = HiddenVisibility;
John McCallb7139c42010-10-28 04:18:25 +0000563 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCall457a04e2010-10-22 21:05:15 +0000564 LV.second = GetVisibilityFromAttr(VA);
565
566 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
567 LVPair PrevLV = Prev->getLinkageAndVisibility();
568 if (PrevLV.first) LV.first = PrevLV.first;
569 LV.second = minVisibility(LV.second, PrevLV.second);
570 }
571
572 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000573 }
574 }
575
576 // C++ [basic.link]p6:
577 // Names not covered by these rules have no linkage.
John McCall457a04e2010-10-22 21:05:15 +0000578 return LVPair(NoLinkage, DefaultVisibility);
579}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000580
Douglas Gregor2ada0482009-02-04 17:27:36 +0000581std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000582 return getQualifiedNameAsString(getASTContext().getLangOptions());
583}
584
585std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000586 const DeclContext *Ctx = getDeclContext();
587
588 if (Ctx->isFunctionOrMethod())
589 return getNameAsString();
590
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000591 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
592 ContextsTy Contexts;
593
594 // Collect contexts.
595 while (Ctx && isa<NamedDecl>(Ctx)) {
596 Contexts.push_back(Ctx);
597 Ctx = Ctx->getParent();
598 };
599
600 std::string QualName;
601 llvm::raw_string_ostream OS(QualName);
602
603 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
604 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000605 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000606 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000607 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
608 std::string TemplateArgsStr
609 = TemplateSpecializationType::PrintTemplateArgumentList(
610 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000611 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000612 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000613 OS << Spec->getName() << TemplateArgsStr;
614 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000615 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000616 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000617 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000618 OS << ND;
619 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
620 if (!RD->getIdentifier())
621 OS << "<anonymous " << RD->getKindName() << '>';
622 else
623 OS << RD;
624 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000625 const FunctionProtoType *FT = 0;
626 if (FD->hasWrittenPrototype())
627 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
628
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000629 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000630 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000631 unsigned NumParams = FD->getNumParams();
632 for (unsigned i = 0; i < NumParams; ++i) {
633 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000634 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000635 std::string Param;
636 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000637 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000638 }
639
640 if (FT->isVariadic()) {
641 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000642 OS << ", ";
643 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000644 }
645 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000646 OS << ')';
647 } else {
648 OS << cast<NamedDecl>(*I);
649 }
650 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000651 }
652
John McCalla2a3f7d2010-03-16 21:48:18 +0000653 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000654 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000655 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000656 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000657
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000658 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000659}
660
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000661bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000662 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
663
Douglas Gregor889ceb72009-02-03 19:21:40 +0000664 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
665 // We want to keep it, unless it nominates same namespace.
666 if (getKind() == Decl::UsingDirective) {
667 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
668 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
669 }
Mike Stump11289f42009-09-09 15:08:12 +0000670
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000671 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
672 // For function declarations, we keep track of redeclarations.
673 return FD->getPreviousDeclaration() == OldD;
674
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000675 // For function templates, the underlying function declarations are linked.
676 if (const FunctionTemplateDecl *FunctionTemplate
677 = dyn_cast<FunctionTemplateDecl>(this))
678 if (const FunctionTemplateDecl *OldFunctionTemplate
679 = dyn_cast<FunctionTemplateDecl>(OldD))
680 return FunctionTemplate->getTemplatedDecl()
681 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000682
Steve Naroffc4173fa2009-02-22 19:35:57 +0000683 // For method declarations, we keep track of redeclarations.
684 if (isa<ObjCMethodDecl>(this))
685 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000686
John McCall9f3059a2009-10-09 21:13:30 +0000687 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
688 return true;
689
John McCall3f746822009-11-17 05:59:44 +0000690 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
691 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
692 cast<UsingShadowDecl>(OldD)->getTargetDecl();
693
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000694 // For non-function declarations, if the declarations are of the
695 // same kind then this must be a redeclaration, or semantic analysis
696 // would not have given us the new declaration.
697 return this->getKind() == OldD->getKind();
698}
699
Douglas Gregoreddf4332009-02-24 20:03:32 +0000700bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000701 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000702}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000703
Anders Carlsson6915bf62009-06-26 06:29:23 +0000704NamedDecl *NamedDecl::getUnderlyingDecl() {
705 NamedDecl *ND = this;
706 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000707 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000708 ND = UD->getTargetDecl();
709 else if (ObjCCompatibleAliasDecl *AD
710 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
711 return AD->getClassInterface();
712 else
713 return ND;
714 }
715}
716
John McCalla8ae2222010-04-06 21:38:20 +0000717bool NamedDecl::isCXXInstanceMember() const {
718 assert(isCXXClassMember() &&
719 "checking whether non-member is instance member");
720
721 const NamedDecl *D = this;
722 if (isa<UsingShadowDecl>(D))
723 D = cast<UsingShadowDecl>(D)->getTargetDecl();
724
725 if (isa<FieldDecl>(D))
726 return true;
727 if (isa<CXXMethodDecl>(D))
728 return cast<CXXMethodDecl>(D)->isInstance();
729 if (isa<FunctionTemplateDecl>(D))
730 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
731 ->getTemplatedDecl())->isInstance();
732 return false;
733}
734
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000735//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000736// DeclaratorDecl Implementation
737//===----------------------------------------------------------------------===//
738
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000739template <typename DeclT>
740static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
741 if (decl->getNumTemplateParameterLists() > 0)
742 return decl->getTemplateParameterList(0)->getTemplateLoc();
743 else
744 return decl->getInnerLocStart();
745}
746
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000747SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000748 TypeSourceInfo *TSI = getTypeSourceInfo();
749 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000750 return SourceLocation();
751}
752
John McCall3e11ebe2010-03-15 10:12:16 +0000753void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
754 SourceRange QualifierRange) {
755 if (Qualifier) {
756 // Make sure the extended decl info is allocated.
757 if (!hasExtInfo()) {
758 // Save (non-extended) type source info pointer.
759 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
760 // Allocate external info struct.
761 DeclInfo = new (getASTContext()) ExtInfo;
762 // Restore savedTInfo into (extended) decl info.
763 getExtInfo()->TInfo = savedTInfo;
764 }
765 // Set qualifier info.
766 getExtInfo()->NNS = Qualifier;
767 getExtInfo()->NNSRange = QualifierRange;
768 }
769 else {
770 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
771 assert(QualifierRange.isInvalid());
772 if (hasExtInfo()) {
773 // Save type source info pointer.
774 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
775 // Deallocate the extended decl info.
776 getASTContext().Deallocate(getExtInfo());
777 // Restore savedTInfo into (non-extended) decl info.
778 DeclInfo = savedTInfo;
779 }
780 }
781}
782
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000783SourceLocation DeclaratorDecl::getOuterLocStart() const {
784 return getTemplateOrInnerLocStart(this);
785}
786
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000787void
Douglas Gregor20527e22010-06-15 17:44:38 +0000788QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
789 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000790 TemplateParameterList **TPLists) {
791 assert((NumTPLists == 0 || TPLists != 0) &&
792 "Empty array of template parameters with positive size!");
793 assert((NumTPLists == 0 || NNS) &&
794 "Nonempty array of template parameters with no qualifier!");
795
796 // Free previous template parameters (if any).
797 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000798 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000799 TemplParamLists = 0;
800 NumTemplParamLists = 0;
801 }
802 // Set info on matched template parameter lists (if any).
803 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000804 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000805 NumTemplParamLists = NumTPLists;
806 for (unsigned i = NumTPLists; i-- > 0; )
807 TemplParamLists[i] = TPLists[i];
808 }
809}
810
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000811//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000812// VarDecl Implementation
813//===----------------------------------------------------------------------===//
814
Sebastian Redl833ef452010-01-26 22:01:41 +0000815const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
816 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000817 case SC_None: break;
818 case SC_Auto: return "auto"; break;
819 case SC_Extern: return "extern"; break;
820 case SC_PrivateExtern: return "__private_extern__"; break;
821 case SC_Register: return "register"; break;
822 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000823 }
824
825 assert(0 && "Invalid storage class");
826 return 0;
827}
828
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000829VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000830 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000831 StorageClass S, StorageClass SCAsWritten) {
832 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000833}
834
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000835SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000836 SourceLocation Start = getTypeSpecStartLoc();
837 if (Start.isInvalid())
838 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000839 return Start;
840}
841
842SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000843 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000844 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
845 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000846}
847
Sebastian Redl833ef452010-01-26 22:01:41 +0000848bool VarDecl::isExternC() const {
849 ASTContext &Context = getASTContext();
850 if (!Context.getLangOptions().CPlusPlus)
851 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000852 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000853 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
854
855 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
856 DC = DC->getParent()) {
857 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
858 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000859 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000860
861 break;
862 }
863
864 if (DC->isFunctionOrMethod())
865 return false;
866 }
867
868 return false;
869}
870
871VarDecl *VarDecl::getCanonicalDecl() {
872 return getFirstDeclaration();
873}
874
Sebastian Redl35351a92010-01-31 22:27:38 +0000875VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
876 // C++ [basic.def]p2:
877 // A declaration is a definition unless [...] it contains the 'extern'
878 // specifier or a linkage-specification and neither an initializer [...],
879 // it declares a static data member in a class declaration [...].
880 // C++ [temp.expl.spec]p15:
881 // An explicit specialization of a static data member of a template is a
882 // definition if the declaration includes an initializer; otherwise, it is
883 // a declaration.
884 if (isStaticDataMember()) {
885 if (isOutOfLine() && (hasInit() ||
886 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
887 return Definition;
888 else
889 return DeclarationOnly;
890 }
891 // C99 6.7p5:
892 // A definition of an identifier is a declaration for that identifier that
893 // [...] causes storage to be reserved for that object.
894 // Note: that applies for all non-file-scope objects.
895 // C99 6.9.2p1:
896 // If the declaration of an identifier for an object has file scope and an
897 // initializer, the declaration is an external definition for the identifier
898 if (hasInit())
899 return Definition;
900 // AST for 'extern "C" int foo;' is annotated with 'extern'.
901 if (hasExternalStorage())
902 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000903
John McCall8e7d6562010-08-26 03:08:43 +0000904 if (getStorageClassAsWritten() == SC_Extern ||
905 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000906 for (const VarDecl *PrevVar = getPreviousDeclaration();
907 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
908 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
909 return DeclarationOnly;
910 }
911 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000912 // C99 6.9.2p2:
913 // A declaration of an object that has file scope without an initializer,
914 // and without a storage class specifier or the scs 'static', constitutes
915 // a tentative definition.
916 // No such thing in C++.
917 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
918 return TentativeDefinition;
919
920 // What's left is (in C, block-scope) declarations without initializers or
921 // external storage. These are definitions.
922 return Definition;
923}
924
Sebastian Redl35351a92010-01-31 22:27:38 +0000925VarDecl *VarDecl::getActingDefinition() {
926 DefinitionKind Kind = isThisDeclarationADefinition();
927 if (Kind != TentativeDefinition)
928 return 0;
929
Chris Lattner48eb14d2010-06-14 18:31:46 +0000930 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000931 VarDecl *First = getFirstDeclaration();
932 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
933 I != E; ++I) {
934 Kind = (*I)->isThisDeclarationADefinition();
935 if (Kind == Definition)
936 return 0;
937 else if (Kind == TentativeDefinition)
938 LastTentative = *I;
939 }
940 return LastTentative;
941}
942
943bool VarDecl::isTentativeDefinitionNow() const {
944 DefinitionKind Kind = isThisDeclarationADefinition();
945 if (Kind != TentativeDefinition)
946 return false;
947
948 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
949 if ((*I)->isThisDeclarationADefinition() == Definition)
950 return false;
951 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000952 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000953}
954
Sebastian Redl5ca79842010-02-01 20:16:42 +0000955VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000956 VarDecl *First = getFirstDeclaration();
957 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
958 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000959 if ((*I)->isThisDeclarationADefinition() == Definition)
960 return *I;
961 }
962 return 0;
963}
964
965const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000966 redecl_iterator I = redecls_begin(), E = redecls_end();
967 while (I != E && !I->getInit())
968 ++I;
969
970 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000971 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000972 return I->getInit();
973 }
974 return 0;
975}
976
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000977bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000978 if (Decl::isOutOfLine())
979 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000980
981 if (!isStaticDataMember())
982 return false;
983
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000984 // If this static data member was instantiated from a static data member of
985 // a class template, check whether that static data member was defined
986 // out-of-line.
987 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
988 return VD->isOutOfLine();
989
990 return false;
991}
992
Douglas Gregor1d957a32009-10-27 18:42:08 +0000993VarDecl *VarDecl::getOutOfLineDefinition() {
994 if (!isStaticDataMember())
995 return 0;
996
997 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
998 RD != RDEnd; ++RD) {
999 if (RD->getLexicalDeclContext()->isFileContext())
1000 return *RD;
1001 }
1002
1003 return 0;
1004}
1005
Douglas Gregord5058122010-02-11 01:19:42 +00001006void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001007 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1008 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001009 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001010 }
1011
1012 Init = I;
1013}
1014
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001015VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001016 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001017 return cast<VarDecl>(MSI->getInstantiatedFrom());
1018
1019 return 0;
1020}
1021
Douglas Gregor3c74d412009-10-14 20:14:33 +00001022TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001023 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001024 return MSI->getTemplateSpecializationKind();
1025
1026 return TSK_Undeclared;
1027}
1028
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001029MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001030 return getASTContext().getInstantiatedFromStaticDataMember(this);
1031}
1032
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001033void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1034 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001035 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001036 assert(MSI && "Not an instantiated static data member?");
1037 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001038 if (TSK != TSK_ExplicitSpecialization &&
1039 PointOfInstantiation.isValid() &&
1040 MSI->getPointOfInstantiation().isInvalid())
1041 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001042}
1043
Sebastian Redl833ef452010-01-26 22:01:41 +00001044//===----------------------------------------------------------------------===//
1045// ParmVarDecl Implementation
1046//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001047
Sebastian Redl833ef452010-01-26 22:01:41 +00001048ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1049 SourceLocation L, IdentifierInfo *Id,
1050 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001051 StorageClass S, StorageClass SCAsWritten,
1052 Expr *DefArg) {
1053 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1054 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001055}
1056
Sebastian Redl833ef452010-01-26 22:01:41 +00001057Expr *ParmVarDecl::getDefaultArg() {
1058 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1059 assert(!hasUninstantiatedDefaultArg() &&
1060 "Default argument is not yet instantiated!");
1061
1062 Expr *Arg = getInit();
1063 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1064 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001065
Sebastian Redl833ef452010-01-26 22:01:41 +00001066 return Arg;
1067}
1068
1069unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1070 if (const CXXExprWithTemporaries *E =
1071 dyn_cast<CXXExprWithTemporaries>(getInit()))
1072 return E->getNumTemporaries();
1073
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001074 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001075}
1076
Sebastian Redl833ef452010-01-26 22:01:41 +00001077CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1078 assert(getNumDefaultArgTemporaries() &&
1079 "Default arguments does not have any temporaries!");
1080
1081 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1082 return E->getTemporary(i);
1083}
1084
1085SourceRange ParmVarDecl::getDefaultArgRange() const {
1086 if (const Expr *E = getInit())
1087 return E->getSourceRange();
1088
1089 if (hasUninstantiatedDefaultArg())
1090 return getUninstantiatedDefaultArg()->getSourceRange();
1091
1092 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001093}
1094
Nuno Lopes394ec982008-12-17 23:39:55 +00001095//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001096// FunctionDecl Implementation
1097//===----------------------------------------------------------------------===//
1098
John McCalle1f2ec22009-09-11 06:45:03 +00001099void FunctionDecl::getNameForDiagnostic(std::string &S,
1100 const PrintingPolicy &Policy,
1101 bool Qualified) const {
1102 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1103 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1104 if (TemplateArgs)
1105 S += TemplateSpecializationType::PrintTemplateArgumentList(
1106 TemplateArgs->getFlatArgumentList(),
1107 TemplateArgs->flat_size(),
1108 Policy);
1109
1110}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001111
Ted Kremenek186a0742010-04-29 16:49:01 +00001112bool FunctionDecl::isVariadic() const {
1113 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1114 return FT->isVariadic();
1115 return false;
1116}
1117
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001118bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1119 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1120 if (I->Body) {
1121 Definition = *I;
1122 return true;
1123 }
1124 }
1125
1126 return false;
1127}
1128
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001129Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001130 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1131 if (I->Body) {
1132 Definition = *I;
1133 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001134 }
1135 }
1136
1137 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001138}
1139
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001140void FunctionDecl::setBody(Stmt *B) {
1141 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001142 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001143 EndRangeLoc = B->getLocEnd();
1144}
1145
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001146void FunctionDecl::setPure(bool P) {
1147 IsPure = P;
1148 if (P)
1149 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1150 Parent->markedVirtualFunctionPure();
1151}
1152
Douglas Gregor16618f22009-09-12 00:17:51 +00001153bool FunctionDecl::isMain() const {
1154 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001155 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001156 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001157 getIdentifier() && getIdentifier()->isStr("main");
1158}
1159
Douglas Gregor16618f22009-09-12 00:17:51 +00001160bool FunctionDecl::isExternC() const {
1161 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001162 // In C, any non-static, non-overloadable function has external
1163 // linkage.
1164 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001165 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001166
Mike Stump11289f42009-09-09 15:08:12 +00001167 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001168 DC = DC->getParent()) {
1169 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1170 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001171 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001172 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001173
1174 break;
1175 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001176
1177 if (DC->isRecord())
1178 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001179 }
1180
Douglas Gregorbff62032010-10-21 16:57:46 +00001181 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001182}
1183
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001184bool FunctionDecl::isGlobal() const {
1185 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1186 return Method->isStatic();
1187
John McCall8e7d6562010-08-26 03:08:43 +00001188 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001189 return false;
1190
Mike Stump11289f42009-09-09 15:08:12 +00001191 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001192 DC->isNamespace();
1193 DC = DC->getParent()) {
1194 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1195 if (!Namespace->getDeclName())
1196 return false;
1197 break;
1198 }
1199 }
1200
1201 return true;
1202}
1203
Sebastian Redl833ef452010-01-26 22:01:41 +00001204void
1205FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1206 redeclarable_base::setPreviousDeclaration(PrevDecl);
1207
1208 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1209 FunctionTemplateDecl *PrevFunTmpl
1210 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1211 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1212 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1213 }
1214}
1215
1216const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1217 return getFirstDeclaration();
1218}
1219
1220FunctionDecl *FunctionDecl::getCanonicalDecl() {
1221 return getFirstDeclaration();
1222}
1223
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001224/// \brief Returns a value indicating whether this function
1225/// corresponds to a builtin function.
1226///
1227/// The function corresponds to a built-in function if it is
1228/// declared at translation scope or within an extern "C" block and
1229/// its name matches with the name of a builtin. The returned value
1230/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001231/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001232/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001233unsigned FunctionDecl::getBuiltinID() const {
1234 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001235 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1236 return 0;
1237
1238 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1239 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1240 return BuiltinID;
1241
1242 // This function has the name of a known C library
1243 // function. Determine whether it actually refers to the C library
1244 // function or whether it just has the same name.
1245
Douglas Gregora908e7f2009-02-17 03:23:10 +00001246 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001247 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001248 return 0;
1249
Douglas Gregore711f702009-02-14 18:57:46 +00001250 // If this function is at translation-unit scope and we're not in
1251 // C++, it refers to the C library function.
1252 if (!Context.getLangOptions().CPlusPlus &&
1253 getDeclContext()->isTranslationUnit())
1254 return BuiltinID;
1255
1256 // If the function is in an extern "C" linkage specification and is
1257 // not marked "overloadable", it's the real function.
1258 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001259 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001260 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001261 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001262 return BuiltinID;
1263
1264 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001265 return 0;
1266}
1267
1268
Chris Lattner47c0d002009-04-25 06:03:53 +00001269/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001270/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001271/// after it has been created.
1272unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001273 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001274 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001275 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001276 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001277
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001278}
1279
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001280void FunctionDecl::setParams(ASTContext &C,
1281 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001282 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001283 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001284
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001285 // Zero params -> null pointer.
1286 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001287 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001288 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001289 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001290
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001291 // Update source range. The check below allows us to set EndRangeLoc before
1292 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001293 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001294 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001295 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001296}
Chris Lattner41943152007-01-25 04:52:46 +00001297
Chris Lattner58258242008-04-10 02:22:51 +00001298/// getMinRequiredArguments - Returns the minimum number of arguments
1299/// needed to call this function. This may be fewer than the number of
1300/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001301/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001302unsigned FunctionDecl::getMinRequiredArguments() const {
1303 unsigned NumRequiredArgs = getNumParams();
1304 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001305 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001306 --NumRequiredArgs;
1307
1308 return NumRequiredArgs;
1309}
1310
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001311bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001312 // FIXME: This is not enough. Consider:
1313 //
1314 // inline void f();
1315 // void f() { }
1316 //
1317 // f is inlined, but does not have inline specified.
1318 // To fix this we should add an 'inline' flag to FunctionDecl.
1319 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001320 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001321
1322 if (isa<CXXMethodDecl>(this)) {
1323 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1324 return true;
1325 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001326
1327 switch (getTemplateSpecializationKind()) {
1328 case TSK_Undeclared:
1329 case TSK_ExplicitSpecialization:
1330 return false;
1331
1332 case TSK_ImplicitInstantiation:
1333 case TSK_ExplicitInstantiationDeclaration:
1334 case TSK_ExplicitInstantiationDefinition:
1335 // Handle below.
1336 break;
1337 }
1338
1339 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001340 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001341 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001342 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001343
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001344 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001345 return PatternDecl->isInlined();
1346
1347 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001348}
1349
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001350/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001351/// definition will be externally visible.
1352///
1353/// Inline function definitions are always available for inlining optimizations.
1354/// However, depending on the language dialect, declaration specifiers, and
1355/// attributes, the definition of an inline function may or may not be
1356/// "externally" visible to other translation units in the program.
1357///
1358/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001359/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001360/// inline definition becomes externally visible (C99 6.7.4p6).
1361///
1362/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1363/// definition, we use the GNU semantics for inline, which are nearly the
1364/// opposite of C99 semantics. In particular, "inline" by itself will create
1365/// an externally visible symbol, but "extern inline" will not create an
1366/// externally visible symbol.
1367bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1368 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001369 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001370 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001371
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001372 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001373 // GNU inline semantics. Based on a number of examples, we came up with the
1374 // following heuristic: if the "inline" keyword is present on a
1375 // declaration of the function but "extern" is not present on that
1376 // declaration, then the symbol is externally visible. Otherwise, the GNU
1377 // "extern inline" semantics applies and the symbol is not externally
1378 // visible.
1379 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1380 Redecl != RedeclEnd;
1381 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001382 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001383 return true;
1384 }
1385
1386 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001387 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001388 }
1389
1390 // C99 6.7.4p6:
1391 // [...] If all of the file scope declarations for a function in a
1392 // translation unit include the inline function specifier without extern,
1393 // then the definition in that translation unit is an inline definition.
1394 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1395 Redecl != RedeclEnd;
1396 ++Redecl) {
1397 // Only consider file-scope declarations in this test.
1398 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1399 continue;
1400
John McCall8e7d6562010-08-26 03:08:43 +00001401 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001402 return true; // Not an inline definition
1403 }
1404
1405 // C99 6.7.4p6:
1406 // An inline definition does not provide an external definition for the
1407 // function, and does not forbid an external definition in another
1408 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001409 return false;
1410}
1411
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001412/// getOverloadedOperator - Which C++ overloaded operator this
1413/// function represents, if any.
1414OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001415 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1416 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001417 else
1418 return OO_None;
1419}
1420
Alexis Huntc88db062010-01-13 09:01:02 +00001421/// getLiteralIdentifier - The literal suffix identifier this function
1422/// represents, if any.
1423const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1424 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1425 return getDeclName().getCXXLiteralIdentifier();
1426 else
1427 return 0;
1428}
1429
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001430FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1431 if (TemplateOrSpecialization.isNull())
1432 return TK_NonTemplate;
1433 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1434 return TK_FunctionTemplate;
1435 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1436 return TK_MemberSpecialization;
1437 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1438 return TK_FunctionTemplateSpecialization;
1439 if (TemplateOrSpecialization.is
1440 <DependentFunctionTemplateSpecializationInfo*>())
1441 return TK_DependentFunctionTemplateSpecialization;
1442
1443 assert(false && "Did we miss a TemplateOrSpecialization type?");
1444 return TK_NonTemplate;
1445}
1446
Douglas Gregord801b062009-10-07 23:56:10 +00001447FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001448 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001449 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1450
1451 return 0;
1452}
1453
Douglas Gregor06db9f52009-10-12 20:18:28 +00001454MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1455 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1456}
1457
Douglas Gregord801b062009-10-07 23:56:10 +00001458void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001459FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1460 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001461 TemplateSpecializationKind TSK) {
1462 assert(TemplateOrSpecialization.isNull() &&
1463 "Member function is already a specialization");
1464 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001465 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001466 TemplateOrSpecialization = Info;
1467}
1468
Douglas Gregorafca3b42009-10-27 20:53:28 +00001469bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001470 // If the function is invalid, it can't be implicitly instantiated.
1471 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001472 return false;
1473
1474 switch (getTemplateSpecializationKind()) {
1475 case TSK_Undeclared:
1476 case TSK_ExplicitSpecialization:
1477 case TSK_ExplicitInstantiationDefinition:
1478 return false;
1479
1480 case TSK_ImplicitInstantiation:
1481 return true;
1482
1483 case TSK_ExplicitInstantiationDeclaration:
1484 // Handled below.
1485 break;
1486 }
1487
1488 // Find the actual template from which we will instantiate.
1489 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001490 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001491 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001492 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001493
1494 // C++0x [temp.explicit]p9:
1495 // Except for inline functions, other explicit instantiation declarations
1496 // have the effect of suppressing the implicit instantiation of the entity
1497 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001498 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001499 return true;
1500
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001501 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001502}
1503
1504FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1505 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1506 while (Primary->getInstantiatedFromMemberTemplate()) {
1507 // If we have hit a point where the user provided a specialization of
1508 // this template, we're done looking.
1509 if (Primary->isMemberSpecialization())
1510 break;
1511
1512 Primary = Primary->getInstantiatedFromMemberTemplate();
1513 }
1514
1515 return Primary->getTemplatedDecl();
1516 }
1517
1518 return getInstantiatedFromMemberFunction();
1519}
1520
Douglas Gregor70d83e22009-06-29 17:30:29 +00001521FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001522 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001523 = TemplateOrSpecialization
1524 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001525 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001526 }
1527 return 0;
1528}
1529
1530const TemplateArgumentList *
1531FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001532 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001533 = TemplateOrSpecialization
1534 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001535 return Info->TemplateArguments;
1536 }
1537 return 0;
1538}
1539
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001540const TemplateArgumentListInfo *
1541FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1542 if (FunctionTemplateSpecializationInfo *Info
1543 = TemplateOrSpecialization
1544 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1545 return Info->TemplateArgumentsAsWritten;
1546 }
1547 return 0;
1548}
1549
Mike Stump11289f42009-09-09 15:08:12 +00001550void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001551FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1552 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001553 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001554 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001555 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001556 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1557 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001558 assert(TSK != TSK_Undeclared &&
1559 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001560 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001561 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001562 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001563 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1564 TemplateArgs,
1565 TemplateArgsAsWritten,
1566 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001567 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001568
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001569 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001570 // function template specializations.
1571 if (InsertPos)
1572 Template->getSpecializations().InsertNode(Info, InsertPos);
1573 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001574 // Try to insert the new node. If there is an existing node, leave it, the
1575 // set will contain the canonical decls while
1576 // FunctionTemplateDecl::findSpecialization will return
1577 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001578 FunctionTemplateSpecializationInfo *Existing
1579 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001580 (void)Existing;
1581 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1582 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001583 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001584}
1585
John McCallb9c78482010-04-08 09:05:18 +00001586void
1587FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1588 const UnresolvedSetImpl &Templates,
1589 const TemplateArgumentListInfo &TemplateArgs) {
1590 assert(TemplateOrSpecialization.isNull());
1591 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1592 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001593 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001594 void *Buffer = Context.Allocate(Size);
1595 DependentFunctionTemplateSpecializationInfo *Info =
1596 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1597 TemplateArgs);
1598 TemplateOrSpecialization = Info;
1599}
1600
1601DependentFunctionTemplateSpecializationInfo::
1602DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1603 const TemplateArgumentListInfo &TArgs)
1604 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1605
1606 d.NumTemplates = Ts.size();
1607 d.NumArgs = TArgs.size();
1608
1609 FunctionTemplateDecl **TsArray =
1610 const_cast<FunctionTemplateDecl**>(getTemplates());
1611 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1612 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1613
1614 TemplateArgumentLoc *ArgsArray =
1615 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1616 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1617 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1618}
1619
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001620TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001621 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001622 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001623 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001624 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001625 if (FTSInfo)
1626 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001627
Douglas Gregord801b062009-10-07 23:56:10 +00001628 MemberSpecializationInfo *MSInfo
1629 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1630 if (MSInfo)
1631 return MSInfo->getTemplateSpecializationKind();
1632
1633 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001634}
1635
Mike Stump11289f42009-09-09 15:08:12 +00001636void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001637FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1638 SourceLocation PointOfInstantiation) {
1639 if (FunctionTemplateSpecializationInfo *FTSInfo
1640 = TemplateOrSpecialization.dyn_cast<
1641 FunctionTemplateSpecializationInfo*>()) {
1642 FTSInfo->setTemplateSpecializationKind(TSK);
1643 if (TSK != TSK_ExplicitSpecialization &&
1644 PointOfInstantiation.isValid() &&
1645 FTSInfo->getPointOfInstantiation().isInvalid())
1646 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1647 } else if (MemberSpecializationInfo *MSInfo
1648 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1649 MSInfo->setTemplateSpecializationKind(TSK);
1650 if (TSK != TSK_ExplicitSpecialization &&
1651 PointOfInstantiation.isValid() &&
1652 MSInfo->getPointOfInstantiation().isInvalid())
1653 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1654 } else
1655 assert(false && "Function cannot have a template specialization kind");
1656}
1657
1658SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001659 if (FunctionTemplateSpecializationInfo *FTSInfo
1660 = TemplateOrSpecialization.dyn_cast<
1661 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001662 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001663 else if (MemberSpecializationInfo *MSInfo
1664 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001665 return MSInfo->getPointOfInstantiation();
1666
1667 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001668}
1669
Douglas Gregor6411b922009-09-11 20:15:17 +00001670bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001671 if (Decl::isOutOfLine())
1672 return true;
1673
1674 // If this function was instantiated from a member function of a
1675 // class template, check whether that member function was defined out-of-line.
1676 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1677 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001678 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001679 return Definition->isOutOfLine();
1680 }
1681
1682 // If this function was instantiated from a function template,
1683 // check whether that function template was defined out-of-line.
1684 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1685 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001686 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001687 return Definition->isOutOfLine();
1688 }
1689
1690 return false;
1691}
1692
Chris Lattner59a25942008-03-31 00:36:02 +00001693//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001694// FieldDecl Implementation
1695//===----------------------------------------------------------------------===//
1696
1697FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1698 IdentifierInfo *Id, QualType T,
1699 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1700 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1701}
1702
1703bool FieldDecl::isAnonymousStructOrUnion() const {
1704 if (!isImplicit() || getDeclName())
1705 return false;
1706
1707 if (const RecordType *Record = getType()->getAs<RecordType>())
1708 return Record->getDecl()->isAnonymousStructOrUnion();
1709
1710 return false;
1711}
1712
1713//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001714// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001715//===----------------------------------------------------------------------===//
1716
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001717SourceLocation TagDecl::getOuterLocStart() const {
1718 return getTemplateOrInnerLocStart(this);
1719}
1720
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001721SourceRange TagDecl::getSourceRange() const {
1722 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001723 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001724}
1725
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001726TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001727 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001728}
1729
Douglas Gregora72a4e32010-05-19 18:39:18 +00001730void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1731 TypedefDeclOrQualifier = TDD;
1732 if (TypeForDecl)
1733 TypeForDecl->ClearLinkageCache();
1734}
1735
Douglas Gregordee1be82009-01-17 00:42:38 +00001736void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001737 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001738
1739 if (isa<CXXRecordDecl>(this)) {
1740 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1741 struct CXXRecordDecl::DefinitionData *Data =
1742 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001743 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1744 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001745 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001746}
1747
1748void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001749 assert((!isa<CXXRecordDecl>(this) ||
1750 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1751 "definition completed but not started");
1752
Douglas Gregordee1be82009-01-17 00:42:38 +00001753 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001754 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001755
1756 if (ASTMutationListener *L = getASTMutationListener())
1757 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001758}
1759
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001760TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001761 if (isDefinition())
1762 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001763 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1764 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001765
1766 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001767 R != REnd; ++R)
1768 if (R->isDefinition())
1769 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001770
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001771 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001772}
1773
John McCall3e11ebe2010-03-15 10:12:16 +00001774void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1775 SourceRange QualifierRange) {
1776 if (Qualifier) {
1777 // Make sure the extended qualifier info is allocated.
1778 if (!hasExtInfo())
1779 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1780 // Set qualifier info.
1781 getExtInfo()->NNS = Qualifier;
1782 getExtInfo()->NNSRange = QualifierRange;
1783 }
1784 else {
1785 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1786 assert(QualifierRange.isInvalid());
1787 if (hasExtInfo()) {
1788 getASTContext().Deallocate(getExtInfo());
1789 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1790 }
1791 }
1792}
1793
Ted Kremenek21475702008-09-05 17:16:31 +00001794//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001795// EnumDecl Implementation
1796//===----------------------------------------------------------------------===//
1797
1798EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1799 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001800 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1801 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1802 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001803 C.getTypeDeclType(Enum, PrevDecl);
1804 return Enum;
1805}
1806
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001807EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001808 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1809 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001810}
1811
Douglas Gregord5058122010-02-11 01:19:42 +00001812void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001813 QualType NewPromotionType,
1814 unsigned NumPositiveBits,
1815 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001816 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001817 if (!IntegerType)
1818 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001819 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001820 setNumPositiveBits(NumPositiveBits);
1821 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001822 TagDecl::completeDefinition();
1823}
1824
1825//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001826// RecordDecl Implementation
1827//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001828
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001829RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001830 IdentifierInfo *Id, RecordDecl *PrevDecl,
1831 SourceLocation TKL)
1832 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001833 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001834 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001835 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001836 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001837 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001838}
1839
1840RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001841 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001842 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001843
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001844 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001845 C.getTypeDeclType(R, PrevDecl);
1846 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001847}
1848
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001849RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1850 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1851 SourceLocation());
1852}
1853
Douglas Gregordfcad112009-03-25 15:59:44 +00001854bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001855 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001856 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1857}
1858
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001859RecordDecl::field_iterator RecordDecl::field_begin() const {
1860 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1861 LoadFieldsFromExternalStorage();
1862
1863 return field_iterator(decl_iterator(FirstDecl));
1864}
1865
Douglas Gregor91f84212008-12-11 16:49:14 +00001866/// completeDefinition - Notes that the definition of this type is now
1867/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001868void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001869 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001870 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001871}
Steve Naroffcc321422007-03-26 23:09:51 +00001872
John McCall61925b02010-05-21 01:17:40 +00001873ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1874 // Force the decl chain to come into existence properly.
1875 if (!getNextDeclInContext()) getParent()->decls_begin();
1876
1877 assert(isAnonymousStructOrUnion());
1878 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1879 assert(D->getType()->isRecordType());
1880 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1881 return D;
1882}
1883
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001884void RecordDecl::LoadFieldsFromExternalStorage() const {
1885 ExternalASTSource *Source = getASTContext().getExternalSource();
1886 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1887
1888 // Notify that we have a RecordDecl doing some initialization.
1889 ExternalASTSource::Deserializing TheFields(Source);
1890
1891 llvm::SmallVector<Decl*, 64> Decls;
1892 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1893 return;
1894
1895#ifndef NDEBUG
1896 // Check that all decls we got were FieldDecls.
1897 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1898 assert(isa<FieldDecl>(Decls[i]));
1899#endif
1900
1901 LoadedFieldsFromExternalStorage = true;
1902
1903 if (Decls.empty())
1904 return;
1905
1906 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1907}
1908
Steve Naroff415d3d52008-10-08 17:01:13 +00001909//===----------------------------------------------------------------------===//
1910// BlockDecl Implementation
1911//===----------------------------------------------------------------------===//
1912
Douglas Gregord5058122010-02-11 01:19:42 +00001913void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001914 unsigned NParms) {
1915 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001916
Steve Naroffc4b30e52009-03-13 16:56:44 +00001917 // Zero params -> null pointer.
1918 if (NParms) {
1919 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001920 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001921 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1922 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1923 }
1924}
1925
1926unsigned BlockDecl::getNumParams() const {
1927 return NumParams;
1928}
Sebastian Redl833ef452010-01-26 22:01:41 +00001929
1930
1931//===----------------------------------------------------------------------===//
1932// Other Decl Allocation/Deallocation Method Implementations
1933//===----------------------------------------------------------------------===//
1934
1935TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1936 return new (C) TranslationUnitDecl(C);
1937}
1938
1939NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1940 SourceLocation L, IdentifierInfo *Id) {
1941 return new (C) NamespaceDecl(DC, L, Id);
1942}
1943
Douglas Gregor417e87c2010-10-27 19:49:05 +00001944NamespaceDecl *NamespaceDecl::getNextNamespace() {
1945 return dyn_cast_or_null<NamespaceDecl>(
1946 NextNamespace.get(getASTContext().getExternalSource()));
1947}
1948
Sebastian Redl833ef452010-01-26 22:01:41 +00001949ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1950 SourceLocation L, IdentifierInfo *Id, QualType T) {
1951 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1952}
1953
1954FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001955 const DeclarationNameInfo &NameInfo,
1956 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001957 StorageClass S, StorageClass SCAsWritten,
1958 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001959 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001960 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001961 New->HasWrittenPrototype = hasWrittenPrototype;
1962 return New;
1963}
1964
1965BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1966 return new (C) BlockDecl(DC, L);
1967}
1968
1969EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1970 SourceLocation L,
1971 IdentifierInfo *Id, QualType T,
1972 Expr *E, const llvm::APSInt &V) {
1973 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1974}
1975
Douglas Gregorbe996932010-09-01 20:41:53 +00001976SourceRange EnumConstantDecl::getSourceRange() const {
1977 SourceLocation End = getLocation();
1978 if (Init)
1979 End = Init->getLocEnd();
1980 return SourceRange(getLocation(), End);
1981}
1982
Sebastian Redl833ef452010-01-26 22:01:41 +00001983TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1984 SourceLocation L, IdentifierInfo *Id,
1985 TypeSourceInfo *TInfo) {
1986 return new (C) TypedefDecl(DC, L, Id, TInfo);
1987}
1988
Sebastian Redl833ef452010-01-26 22:01:41 +00001989FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1990 SourceLocation L,
1991 StringLiteral *Str) {
1992 return new (C) FileScopeAsmDecl(DC, L, Str);
1993}