blob: 19cfe9e1199d1df5189b1ce18b310e923f7fe4fa [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 McCall457a04e2010-10-22 21:05:15 +0000143static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000144 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000145 "Not a name having namespace scope");
146 ASTContext &Context = D->getASTContext();
147
148 // C++ [basic.link]p3:
149 // A name having namespace scope (3.3.6) has internal linkage if it
150 // is the name of
151 // - an object, reference, function or function template that is
152 // explicitly declared static; or,
153 // (This bullet corresponds to C99 6.2.2p3.)
154 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
155 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000156 if (Var->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000157 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000158
159 // - an object or reference that is explicitly declared const
160 // and neither explicitly declared extern nor previously
161 // declared to have external linkage; or
162 // (there is no equivalent in C99)
163 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000164 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000165 Var->getStorageClass() != SC_Extern &&
166 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000167 bool FoundExtern = false;
168 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
169 PrevVar && !FoundExtern;
170 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000171 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000172 FoundExtern = true;
173
174 if (!FoundExtern)
John McCall457a04e2010-10-22 21:05:15 +0000175 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000176 }
177 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000178 // C++ [temp]p4:
179 // A non-member function template can have internal linkage; any
180 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000181 const FunctionDecl *Function = 0;
182 if (const FunctionTemplateDecl *FunTmpl
183 = dyn_cast<FunctionTemplateDecl>(D))
184 Function = FunTmpl->getTemplatedDecl();
185 else
186 Function = cast<FunctionDecl>(D);
187
188 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000189 if (Function->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000190 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000191 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
192 // - a data member of an anonymous union.
193 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCall457a04e2010-10-22 21:05:15 +0000194 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000195 }
196
John McCall457a04e2010-10-22 21:05:15 +0000197 if (D->isInAnonymousNamespace())
198 return LVPair(UniqueExternalLinkage, DefaultVisibility);
199
John McCallb7139c42010-10-28 04:18:25 +0000200 const VisibilityAttr *ExplicitVisibility = GetExplicitVisibility(D);
201
John McCall457a04e2010-10-22 21:05:15 +0000202 // Set up the defaults.
203
204 // C99 6.2.2p5:
205 // If the declaration of an identifier for an object has file
206 // scope and no storage-class specifier, its linkage is
207 // external.
208 LVPair LV(ExternalLinkage, DefaultVisibility);
209
210 // We ignore -fvisibility on non-definitions and explicit
211 // instantiation declarations.
212 bool ConsiderDashFVisibility = true;
213
Douglas Gregorf73b2822009-11-25 22:24:25 +0000214 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000215
Douglas Gregorf73b2822009-11-25 22:24:25 +0000216 // A name having namespace scope has external linkage if it is the
217 // name of
218 //
219 // - an object or reference, unless it has internal linkage; or
220 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000221 // Modify the variable's LV by the LV of its type unless this is
222 // C or extern "C". This follows from [basic.link]p9:
223 // A type without linkage shall not be used as the type of a
224 // variable or function with external linkage unless
225 // - the entity has C language linkage, or
226 // - the entity is declared within an unnamed namespace, or
227 // - the entity is not used or is defined in the same
228 // translation unit.
229 // and [basic.link]p10:
230 // ...the types specified by all declarations referring to a
231 // given variable or function shall be identical...
232 // C does not have an equivalent rule.
233 //
John McCall5fe84122010-10-26 04:59:26 +0000234 // Ignore this if we've got an explicit attribute; the user
235 // probably knows what they're doing.
236 //
John McCall457a04e2010-10-22 21:05:15 +0000237 // Note that we don't want to make the variable non-external
238 // because of this, but unique-external linkage suits us.
John McCall5fe84122010-10-26 04:59:26 +0000239 if (Context.getLangOptions().CPlusPlus && !Var->isExternC() &&
John McCallb7139c42010-10-28 04:18:25 +0000240 !ExplicitVisibility) {
John McCall457a04e2010-10-22 21:05:15 +0000241 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
242 if (TypeLV.first != ExternalLinkage)
243 return LVPair(UniqueExternalLinkage, DefaultVisibility);
244 LV.second = minVisibility(LV.second, TypeLV.second);
245 }
246
Douglas Gregorf73b2822009-11-25 22:24:25 +0000247 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000248 (Var->getStorageClass() == SC_Extern ||
249 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000250 if (Var->getStorageClass() == SC_PrivateExtern)
251 LV.second = HiddenVisibility;
252
Douglas Gregorf73b2822009-11-25 22:24:25 +0000253 // C99 6.2.2p4:
254 // For an identifier declared with the storage-class specifier
255 // extern in a scope in which a prior declaration of that
256 // identifier is visible, if the prior declaration specifies
257 // internal or external linkage, the linkage of the identifier
258 // at the later declaration is the same as the linkage
259 // specified at the prior declaration. If no prior declaration
260 // is visible, or if the prior declaration specifies no
261 // linkage, then the identifier has external linkage.
262 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000263 LVPair PrevLV = PrevVar->getLinkageAndVisibility();
264 if (PrevLV.first) LV.first = PrevLV.first;
265 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000266 }
267 }
268
Douglas Gregorf73b2822009-11-25 22:24:25 +0000269 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000270 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000271 // In theory, we can modify the function's LV by the LV of its
272 // type unless it has C linkage (see comment above about variables
273 // for justification). In practice, GCC doesn't do this, so it's
274 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000275
Douglas Gregorf73b2822009-11-25 22:24:25 +0000276 // C99 6.2.2p5:
277 // If the declaration of an identifier for a function has no
278 // storage-class specifier, its linkage is determined exactly
279 // as if it were declared with the storage-class specifier
280 // extern.
281 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000282 (Function->getStorageClass() == SC_Extern ||
283 Function->getStorageClass() == SC_PrivateExtern ||
284 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000285 // C99 6.2.2p4:
286 // For an identifier declared with the storage-class specifier
287 // extern in a scope in which a prior declaration of that
288 // identifier is visible, if the prior declaration specifies
289 // internal or external linkage, the linkage of the identifier
290 // at the later declaration is the same as the linkage
291 // specified at the prior declaration. If no prior declaration
292 // is visible, or if the prior declaration specifies no
293 // linkage, then the identifier has external linkage.
294 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000295 LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
296 if (PrevLV.first) LV.first = PrevLV.first;
297 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000298 }
299 }
300
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000301 if (FunctionTemplateSpecializationInfo *SpecInfo
302 = Function->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000303 LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000304 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCall457a04e2010-10-22 21:05:15 +0000305 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
306
307 if (SpecInfo->getTemplateSpecializationKind()
308 == TSK_ExplicitInstantiationDeclaration)
309 ConsiderDashFVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000310 }
311
John McCall457a04e2010-10-22 21:05:15 +0000312 if (ConsiderDashFVisibility)
313 ConsiderDashFVisibility = Function->hasBody();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000314
315 // - a named class (Clause 9), or an unnamed class defined in a
316 // typedef declaration in which the class has the typedef name
317 // for linkage purposes (7.1.3); or
318 // - a named enumeration (7.2), or an unnamed enumeration
319 // defined in a typedef declaration in which the enumeration
320 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000321 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
322 // Unnamed tags have no linkage.
323 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
324 return LVPair(NoLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000325
John McCall457a04e2010-10-22 21:05:15 +0000326 // If this is a class template specialization, consider the
327 // linkage of the template and template arguments.
328 if (const ClassTemplateSpecializationDecl *Spec
329 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
330 // From the template. Note below the restrictions on how we
331 // compute template visibility.
332 LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000333
John McCall457a04e2010-10-22 21:05:15 +0000334 // The arguments at which the template was instantiated.
335 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
336 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
337
338 if (Spec->getTemplateSpecializationKind()
339 == TSK_ExplicitInstantiationDeclaration)
340 ConsiderDashFVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000341 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000342
John McCall5fe84122010-10-26 04:59:26 +0000343 // Consider -fvisibility unless the type has C linkage.
John McCall457a04e2010-10-22 21:05:15 +0000344 if (ConsiderDashFVisibility)
John McCall5fe84122010-10-26 04:59:26 +0000345 ConsiderDashFVisibility =
346 (Context.getLangOptions().CPlusPlus &&
347 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000348
Douglas Gregorf73b2822009-11-25 22:24:25 +0000349 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000350 } else if (isa<EnumConstantDecl>(D)) {
351 LVPair EnumLV =
352 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
353 if (!isExternalLinkage(EnumLV.first))
354 return LVPair(NoLinkage, DefaultVisibility);
355 LV = merge(LV, EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000356
357 // - a template, unless it is a function template that has
358 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000359 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
360 LV = merge(LV, getLVForTemplateParameterList(
361 Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000362
John McCall457a04e2010-10-22 21:05:15 +0000363 // We do not want to consider attributes or global settings when
364 // computing template visibility.
365 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000366
367 // - a namespace (7.3), unless it is declared within an unnamed
368 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000369 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
370 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000371
John McCall457a04e2010-10-22 21:05:15 +0000372 // By extension, we assign external linkage to Objective-C
373 // interfaces.
374 } else if (isa<ObjCInterfaceDecl>(D)) {
375 // fallout
376
377 // Everything not covered here has no linkage.
378 } else {
379 return LVPair(NoLinkage, DefaultVisibility);
380 }
381
382 // If we ended up with non-external linkage, visibility should
383 // always be default.
384 if (LV.first != ExternalLinkage)
385 return LVPair(LV.first, DefaultVisibility);
386
387 // If we didn't end up with hidden visibility, consider attributes
388 // and -fvisibility.
389 if (LV.second != HiddenVisibility) {
390 Visibility StandardV;
391
392 // If we have an explicit visibility attribute, merge that in.
John McCallb7139c42010-10-28 04:18:25 +0000393 if (ExplicitVisibility)
394 StandardV = GetVisibilityFromAttr(ExplicitVisibility);
John McCall457a04e2010-10-22 21:05:15 +0000395 else if (ConsiderDashFVisibility)
396 StandardV = Context.getLangOptions().getVisibilityMode();
397 else
398 StandardV = DefaultVisibility; // no-op
399
400 LV.second = minVisibility(LV.second, StandardV);
401 }
402
403 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000404}
405
John McCall457a04e2010-10-22 21:05:15 +0000406static LVPair getLVForClassMember(const NamedDecl *D) {
407 // Only certain class members have linkage. Note that fields don't
408 // really have linkage, but it's convenient to say they do for the
409 // purposes of calculating linkage of pointer-to-data-member
410 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000411 if (!(isa<CXXMethodDecl>(D) ||
412 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000413 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000414 (isa<TagDecl>(D) &&
415 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCall457a04e2010-10-22 21:05:15 +0000416 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000417
418 // Class members only have linkage if their class has external linkage.
John McCall457a04e2010-10-22 21:05:15 +0000419 LVPair ClassLV =
420 cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility();
421 if (!isExternalLinkage(ClassLV.first))
422 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000423
424 // If the class already has unique-external linkage, we can't improve.
John McCall457a04e2010-10-22 21:05:15 +0000425 if (ClassLV.first == UniqueExternalLinkage)
426 return LVPair(UniqueExternalLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000427
John McCall457a04e2010-10-22 21:05:15 +0000428 // Start with the class's linkage and visibility.
429 LVPair LV = ClassLV;
430
431 // If we have an explicit visibility attribute, merge that in.
John McCallb7139c42010-10-28 04:18:25 +0000432 const VisibilityAttr *VA = GetExplicitVisibility(D);
John McCall457a04e2010-10-22 21:05:15 +0000433 if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
434
John McCall2efaf112010-10-28 07:07:52 +0000435 // If it's a variable declaration and we don't have an explicit
436 // visibility attribute, apply the LV from its type.
John McCall457a04e2010-10-22 21:05:15 +0000437 // See the comment about namespace-scope variable decls above.
John McCall2efaf112010-10-28 07:07:52 +0000438 if (!VA && isa<VarDecl>(D)) {
439 LVPair TypeLV = cast<VarDecl>(D)->getType()->getLinkageAndVisibility();
John McCall457a04e2010-10-22 21:05:15 +0000440 if (TypeLV.first != ExternalLinkage)
441 LV.first = minLinkage(LV.first, UniqueExternalLinkage);
442 LV.second = minVisibility(LV.second, TypeLV.second);
443 }
444
John McCall8823c652010-08-13 08:35:10 +0000445 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000446 // If this is a method template specialization, use the linkage for
447 // the template parameters and arguments.
448 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000449 = MD->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000450 LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
451 LV = merge(LV, getLVForTemplateParameterList(
452 Spec->getTemplate()->getTemplateParameters()));
John McCall8823c652010-08-13 08:35:10 +0000453 }
454
John McCall457a04e2010-10-22 21:05:15 +0000455 // If -fvisibility-inlines-hidden was provided, then inline C++
456 // member functions get "hidden" visibility if they don't have an
457 // explicit visibility attribute.
458 if (!VA && MD->isInlined() && LV.second > HiddenVisibility &&
John McCall62b68622010-10-28 18:10:36 +0000459 D->getASTContext().getLangOptions().InlineVisibilityHidden &&
460 MD->getTemplateSpecializationKind()
461 != TSK_ExplicitInstantiationDeclaration)
John McCall457a04e2010-10-22 21:05:15 +0000462 LV.second = HiddenVisibility;
463
John McCall8823c652010-08-13 08:35:10 +0000464 // Similarly for member class template specializations.
465 } else if (const ClassTemplateSpecializationDecl *Spec
466 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000467 LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
468 LV = merge(LV, getLVForTemplateParameterList(
469 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall8823c652010-08-13 08:35:10 +0000470 }
471
John McCall457a04e2010-10-22 21:05:15 +0000472 return LV;
John McCall8823c652010-08-13 08:35:10 +0000473}
474
John McCall457a04e2010-10-22 21:05:15 +0000475LVPair NamedDecl::getLinkageAndVisibility() const {
Ted Kremenek926d8602010-04-20 23:15:35 +0000476
477 // Objective-C: treat all Objective-C declarations as having external
478 // linkage.
479 switch (getKind()) {
480 default:
481 break;
John McCall457a04e2010-10-22 21:05:15 +0000482 case Decl::TemplateTemplateParm: // count these as external
483 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000484 case Decl::ObjCAtDefsField:
485 case Decl::ObjCCategory:
486 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000487 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000488 case Decl::ObjCForwardProtocol:
489 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000490 case Decl::ObjCMethod:
491 case Decl::ObjCProperty:
492 case Decl::ObjCPropertyImpl:
493 case Decl::ObjCProtocol:
John McCall457a04e2010-10-22 21:05:15 +0000494 return LVPair(ExternalLinkage, DefaultVisibility);
Ted Kremenek926d8602010-04-20 23:15:35 +0000495 }
496
Douglas Gregorf73b2822009-11-25 22:24:25 +0000497 // Handle linkage for namespace-scope names.
Sebastian Redl50c68252010-08-31 00:36:30 +0000498 if (getDeclContext()->getRedeclContext()->isFileContext())
John McCall457a04e2010-10-22 21:05:15 +0000499 return getLVForNamespaceScopeDecl(this);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000500
501 // C++ [basic.link]p5:
502 // In addition, a member function, static data member, a named
503 // class or enumeration of class scope, or an unnamed class or
504 // enumeration defined in a class-scope typedef declaration such
505 // that the class or enumeration has the typedef name for linkage
506 // purposes (7.1.3), has external linkage if the name of the class
507 // has external linkage.
John McCall8823c652010-08-13 08:35:10 +0000508 if (getDeclContext()->isRecord())
John McCall457a04e2010-10-22 21:05:15 +0000509 return getLVForClassMember(this);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000510
511 // C++ [basic.link]p6:
512 // The name of a function declared in block scope and the name of
513 // an object declared by a block scope extern declaration have
514 // linkage. If there is a visible declaration of an entity with
515 // linkage having the same name and type, ignoring entities
516 // declared outside the innermost enclosing namespace scope, the
517 // block scope declaration declares that same entity and receives
518 // the linkage of the previous declaration. If there is more than
519 // one such matching entity, the program is ill-formed. Otherwise,
520 // if no matching entity is found, the block scope entity receives
521 // external linkage.
522 if (getLexicalDeclContext()->isFunctionOrMethod()) {
523 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000524 if (Function->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000525 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000526
John McCall457a04e2010-10-22 21:05:15 +0000527 LVPair LV(ExternalLinkage, DefaultVisibility);
John McCallb7139c42010-10-28 04:18:25 +0000528 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCall457a04e2010-10-22 21:05:15 +0000529 LV.second = GetVisibilityFromAttr(VA);
530
531 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
532 LVPair PrevLV = Prev->getLinkageAndVisibility();
533 if (PrevLV.first) LV.first = PrevLV.first;
534 LV.second = minVisibility(LV.second, PrevLV.second);
535 }
536
537 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000538 }
539
540 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
John McCall8e7d6562010-08-26 03:08:43 +0000541 if (Var->getStorageClass() == SC_Extern ||
542 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000543 if (Var->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000544 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000545
John McCall457a04e2010-10-22 21:05:15 +0000546 LVPair LV(ExternalLinkage, DefaultVisibility);
547 if (Var->getStorageClass() == SC_PrivateExtern)
548 LV.second = HiddenVisibility;
John McCallb7139c42010-10-28 04:18:25 +0000549 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCall457a04e2010-10-22 21:05:15 +0000550 LV.second = GetVisibilityFromAttr(VA);
551
552 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
553 LVPair PrevLV = Prev->getLinkageAndVisibility();
554 if (PrevLV.first) LV.first = PrevLV.first;
555 LV.second = minVisibility(LV.second, PrevLV.second);
556 }
557
558 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000559 }
560 }
561
562 // C++ [basic.link]p6:
563 // Names not covered by these rules have no linkage.
John McCall457a04e2010-10-22 21:05:15 +0000564 return LVPair(NoLinkage, DefaultVisibility);
565}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000566
Douglas Gregor2ada0482009-02-04 17:27:36 +0000567std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000568 return getQualifiedNameAsString(getASTContext().getLangOptions());
569}
570
571std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000572 const DeclContext *Ctx = getDeclContext();
573
574 if (Ctx->isFunctionOrMethod())
575 return getNameAsString();
576
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000577 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
578 ContextsTy Contexts;
579
580 // Collect contexts.
581 while (Ctx && isa<NamedDecl>(Ctx)) {
582 Contexts.push_back(Ctx);
583 Ctx = Ctx->getParent();
584 };
585
586 std::string QualName;
587 llvm::raw_string_ostream OS(QualName);
588
589 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
590 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000591 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000592 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000593 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
594 std::string TemplateArgsStr
595 = TemplateSpecializationType::PrintTemplateArgumentList(
596 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000597 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000598 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000599 OS << Spec->getName() << TemplateArgsStr;
600 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000601 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000602 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000603 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000604 OS << ND;
605 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
606 if (!RD->getIdentifier())
607 OS << "<anonymous " << RD->getKindName() << '>';
608 else
609 OS << RD;
610 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000611 const FunctionProtoType *FT = 0;
612 if (FD->hasWrittenPrototype())
613 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
614
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000615 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000616 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000617 unsigned NumParams = FD->getNumParams();
618 for (unsigned i = 0; i < NumParams; ++i) {
619 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000620 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000621 std::string Param;
622 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000623 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000624 }
625
626 if (FT->isVariadic()) {
627 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000628 OS << ", ";
629 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000630 }
631 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000632 OS << ')';
633 } else {
634 OS << cast<NamedDecl>(*I);
635 }
636 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000637 }
638
John McCalla2a3f7d2010-03-16 21:48:18 +0000639 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000640 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000641 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000642 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000643
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000644 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000645}
646
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000647bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000648 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
649
Douglas Gregor889ceb72009-02-03 19:21:40 +0000650 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
651 // We want to keep it, unless it nominates same namespace.
652 if (getKind() == Decl::UsingDirective) {
653 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
654 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
655 }
Mike Stump11289f42009-09-09 15:08:12 +0000656
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000657 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
658 // For function declarations, we keep track of redeclarations.
659 return FD->getPreviousDeclaration() == OldD;
660
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000661 // For function templates, the underlying function declarations are linked.
662 if (const FunctionTemplateDecl *FunctionTemplate
663 = dyn_cast<FunctionTemplateDecl>(this))
664 if (const FunctionTemplateDecl *OldFunctionTemplate
665 = dyn_cast<FunctionTemplateDecl>(OldD))
666 return FunctionTemplate->getTemplatedDecl()
667 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000668
Steve Naroffc4173fa2009-02-22 19:35:57 +0000669 // For method declarations, we keep track of redeclarations.
670 if (isa<ObjCMethodDecl>(this))
671 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000672
John McCall9f3059a2009-10-09 21:13:30 +0000673 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
674 return true;
675
John McCall3f746822009-11-17 05:59:44 +0000676 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
677 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
678 cast<UsingShadowDecl>(OldD)->getTargetDecl();
679
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000680 // For non-function declarations, if the declarations are of the
681 // same kind then this must be a redeclaration, or semantic analysis
682 // would not have given us the new declaration.
683 return this->getKind() == OldD->getKind();
684}
685
Douglas Gregoreddf4332009-02-24 20:03:32 +0000686bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000687 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000688}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000689
Anders Carlsson6915bf62009-06-26 06:29:23 +0000690NamedDecl *NamedDecl::getUnderlyingDecl() {
691 NamedDecl *ND = this;
692 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000693 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000694 ND = UD->getTargetDecl();
695 else if (ObjCCompatibleAliasDecl *AD
696 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
697 return AD->getClassInterface();
698 else
699 return ND;
700 }
701}
702
John McCalla8ae2222010-04-06 21:38:20 +0000703bool NamedDecl::isCXXInstanceMember() const {
704 assert(isCXXClassMember() &&
705 "checking whether non-member is instance member");
706
707 const NamedDecl *D = this;
708 if (isa<UsingShadowDecl>(D))
709 D = cast<UsingShadowDecl>(D)->getTargetDecl();
710
711 if (isa<FieldDecl>(D))
712 return true;
713 if (isa<CXXMethodDecl>(D))
714 return cast<CXXMethodDecl>(D)->isInstance();
715 if (isa<FunctionTemplateDecl>(D))
716 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
717 ->getTemplatedDecl())->isInstance();
718 return false;
719}
720
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000721//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000722// DeclaratorDecl Implementation
723//===----------------------------------------------------------------------===//
724
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000725template <typename DeclT>
726static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
727 if (decl->getNumTemplateParameterLists() > 0)
728 return decl->getTemplateParameterList(0)->getTemplateLoc();
729 else
730 return decl->getInnerLocStart();
731}
732
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000733SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000734 TypeSourceInfo *TSI = getTypeSourceInfo();
735 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000736 return SourceLocation();
737}
738
John McCall3e11ebe2010-03-15 10:12:16 +0000739void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
740 SourceRange QualifierRange) {
741 if (Qualifier) {
742 // Make sure the extended decl info is allocated.
743 if (!hasExtInfo()) {
744 // Save (non-extended) type source info pointer.
745 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
746 // Allocate external info struct.
747 DeclInfo = new (getASTContext()) ExtInfo;
748 // Restore savedTInfo into (extended) decl info.
749 getExtInfo()->TInfo = savedTInfo;
750 }
751 // Set qualifier info.
752 getExtInfo()->NNS = Qualifier;
753 getExtInfo()->NNSRange = QualifierRange;
754 }
755 else {
756 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
757 assert(QualifierRange.isInvalid());
758 if (hasExtInfo()) {
759 // Save type source info pointer.
760 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
761 // Deallocate the extended decl info.
762 getASTContext().Deallocate(getExtInfo());
763 // Restore savedTInfo into (non-extended) decl info.
764 DeclInfo = savedTInfo;
765 }
766 }
767}
768
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000769SourceLocation DeclaratorDecl::getOuterLocStart() const {
770 return getTemplateOrInnerLocStart(this);
771}
772
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000773void
Douglas Gregor20527e22010-06-15 17:44:38 +0000774QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
775 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000776 TemplateParameterList **TPLists) {
777 assert((NumTPLists == 0 || TPLists != 0) &&
778 "Empty array of template parameters with positive size!");
779 assert((NumTPLists == 0 || NNS) &&
780 "Nonempty array of template parameters with no qualifier!");
781
782 // Free previous template parameters (if any).
783 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000784 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000785 TemplParamLists = 0;
786 NumTemplParamLists = 0;
787 }
788 // Set info on matched template parameter lists (if any).
789 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000790 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000791 NumTemplParamLists = NumTPLists;
792 for (unsigned i = NumTPLists; i-- > 0; )
793 TemplParamLists[i] = TPLists[i];
794 }
795}
796
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000797//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000798// VarDecl Implementation
799//===----------------------------------------------------------------------===//
800
Sebastian Redl833ef452010-01-26 22:01:41 +0000801const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
802 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000803 case SC_None: break;
804 case SC_Auto: return "auto"; break;
805 case SC_Extern: return "extern"; break;
806 case SC_PrivateExtern: return "__private_extern__"; break;
807 case SC_Register: return "register"; break;
808 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000809 }
810
811 assert(0 && "Invalid storage class");
812 return 0;
813}
814
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000815VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000816 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000817 StorageClass S, StorageClass SCAsWritten) {
818 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000819}
820
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000821SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000822 SourceLocation Start = getTypeSpecStartLoc();
823 if (Start.isInvalid())
824 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000825 return Start;
826}
827
828SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000829 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000830 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
831 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000832}
833
Sebastian Redl833ef452010-01-26 22:01:41 +0000834bool VarDecl::isExternC() const {
835 ASTContext &Context = getASTContext();
836 if (!Context.getLangOptions().CPlusPlus)
837 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000838 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000839 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
840
841 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
842 DC = DC->getParent()) {
843 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
844 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000845 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000846
847 break;
848 }
849
850 if (DC->isFunctionOrMethod())
851 return false;
852 }
853
854 return false;
855}
856
857VarDecl *VarDecl::getCanonicalDecl() {
858 return getFirstDeclaration();
859}
860
Sebastian Redl35351a92010-01-31 22:27:38 +0000861VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
862 // C++ [basic.def]p2:
863 // A declaration is a definition unless [...] it contains the 'extern'
864 // specifier or a linkage-specification and neither an initializer [...],
865 // it declares a static data member in a class declaration [...].
866 // C++ [temp.expl.spec]p15:
867 // An explicit specialization of a static data member of a template is a
868 // definition if the declaration includes an initializer; otherwise, it is
869 // a declaration.
870 if (isStaticDataMember()) {
871 if (isOutOfLine() && (hasInit() ||
872 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
873 return Definition;
874 else
875 return DeclarationOnly;
876 }
877 // C99 6.7p5:
878 // A definition of an identifier is a declaration for that identifier that
879 // [...] causes storage to be reserved for that object.
880 // Note: that applies for all non-file-scope objects.
881 // C99 6.9.2p1:
882 // If the declaration of an identifier for an object has file scope and an
883 // initializer, the declaration is an external definition for the identifier
884 if (hasInit())
885 return Definition;
886 // AST for 'extern "C" int foo;' is annotated with 'extern'.
887 if (hasExternalStorage())
888 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000889
John McCall8e7d6562010-08-26 03:08:43 +0000890 if (getStorageClassAsWritten() == SC_Extern ||
891 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000892 for (const VarDecl *PrevVar = getPreviousDeclaration();
893 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
894 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
895 return DeclarationOnly;
896 }
897 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000898 // C99 6.9.2p2:
899 // A declaration of an object that has file scope without an initializer,
900 // and without a storage class specifier or the scs 'static', constitutes
901 // a tentative definition.
902 // No such thing in C++.
903 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
904 return TentativeDefinition;
905
906 // What's left is (in C, block-scope) declarations without initializers or
907 // external storage. These are definitions.
908 return Definition;
909}
910
Sebastian Redl35351a92010-01-31 22:27:38 +0000911VarDecl *VarDecl::getActingDefinition() {
912 DefinitionKind Kind = isThisDeclarationADefinition();
913 if (Kind != TentativeDefinition)
914 return 0;
915
Chris Lattner48eb14d2010-06-14 18:31:46 +0000916 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000917 VarDecl *First = getFirstDeclaration();
918 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
919 I != E; ++I) {
920 Kind = (*I)->isThisDeclarationADefinition();
921 if (Kind == Definition)
922 return 0;
923 else if (Kind == TentativeDefinition)
924 LastTentative = *I;
925 }
926 return LastTentative;
927}
928
929bool VarDecl::isTentativeDefinitionNow() const {
930 DefinitionKind Kind = isThisDeclarationADefinition();
931 if (Kind != TentativeDefinition)
932 return false;
933
934 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
935 if ((*I)->isThisDeclarationADefinition() == Definition)
936 return false;
937 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000938 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000939}
940
Sebastian Redl5ca79842010-02-01 20:16:42 +0000941VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000942 VarDecl *First = getFirstDeclaration();
943 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
944 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000945 if ((*I)->isThisDeclarationADefinition() == Definition)
946 return *I;
947 }
948 return 0;
949}
950
951const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000952 redecl_iterator I = redecls_begin(), E = redecls_end();
953 while (I != E && !I->getInit())
954 ++I;
955
956 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000957 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000958 return I->getInit();
959 }
960 return 0;
961}
962
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000963bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000964 if (Decl::isOutOfLine())
965 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000966
967 if (!isStaticDataMember())
968 return false;
969
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000970 // If this static data member was instantiated from a static data member of
971 // a class template, check whether that static data member was defined
972 // out-of-line.
973 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
974 return VD->isOutOfLine();
975
976 return false;
977}
978
Douglas Gregor1d957a32009-10-27 18:42:08 +0000979VarDecl *VarDecl::getOutOfLineDefinition() {
980 if (!isStaticDataMember())
981 return 0;
982
983 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
984 RD != RDEnd; ++RD) {
985 if (RD->getLexicalDeclContext()->isFileContext())
986 return *RD;
987 }
988
989 return 0;
990}
991
Douglas Gregord5058122010-02-11 01:19:42 +0000992void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000993 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
994 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000995 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000996 }
997
998 Init = I;
999}
1000
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001001VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001002 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001003 return cast<VarDecl>(MSI->getInstantiatedFrom());
1004
1005 return 0;
1006}
1007
Douglas Gregor3c74d412009-10-14 20:14:33 +00001008TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001009 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001010 return MSI->getTemplateSpecializationKind();
1011
1012 return TSK_Undeclared;
1013}
1014
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001015MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001016 return getASTContext().getInstantiatedFromStaticDataMember(this);
1017}
1018
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001019void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1020 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001021 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001022 assert(MSI && "Not an instantiated static data member?");
1023 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001024 if (TSK != TSK_ExplicitSpecialization &&
1025 PointOfInstantiation.isValid() &&
1026 MSI->getPointOfInstantiation().isInvalid())
1027 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001028}
1029
Sebastian Redl833ef452010-01-26 22:01:41 +00001030//===----------------------------------------------------------------------===//
1031// ParmVarDecl Implementation
1032//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001033
Sebastian Redl833ef452010-01-26 22:01:41 +00001034ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1035 SourceLocation L, IdentifierInfo *Id,
1036 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001037 StorageClass S, StorageClass SCAsWritten,
1038 Expr *DefArg) {
1039 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1040 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001041}
1042
Sebastian Redl833ef452010-01-26 22:01:41 +00001043Expr *ParmVarDecl::getDefaultArg() {
1044 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1045 assert(!hasUninstantiatedDefaultArg() &&
1046 "Default argument is not yet instantiated!");
1047
1048 Expr *Arg = getInit();
1049 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1050 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001051
Sebastian Redl833ef452010-01-26 22:01:41 +00001052 return Arg;
1053}
1054
1055unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1056 if (const CXXExprWithTemporaries *E =
1057 dyn_cast<CXXExprWithTemporaries>(getInit()))
1058 return E->getNumTemporaries();
1059
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001060 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001061}
1062
Sebastian Redl833ef452010-01-26 22:01:41 +00001063CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1064 assert(getNumDefaultArgTemporaries() &&
1065 "Default arguments does not have any temporaries!");
1066
1067 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1068 return E->getTemporary(i);
1069}
1070
1071SourceRange ParmVarDecl::getDefaultArgRange() const {
1072 if (const Expr *E = getInit())
1073 return E->getSourceRange();
1074
1075 if (hasUninstantiatedDefaultArg())
1076 return getUninstantiatedDefaultArg()->getSourceRange();
1077
1078 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001079}
1080
Nuno Lopes394ec982008-12-17 23:39:55 +00001081//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001082// FunctionDecl Implementation
1083//===----------------------------------------------------------------------===//
1084
John McCalle1f2ec22009-09-11 06:45:03 +00001085void FunctionDecl::getNameForDiagnostic(std::string &S,
1086 const PrintingPolicy &Policy,
1087 bool Qualified) const {
1088 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1089 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1090 if (TemplateArgs)
1091 S += TemplateSpecializationType::PrintTemplateArgumentList(
1092 TemplateArgs->getFlatArgumentList(),
1093 TemplateArgs->flat_size(),
1094 Policy);
1095
1096}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001097
Ted Kremenek186a0742010-04-29 16:49:01 +00001098bool FunctionDecl::isVariadic() const {
1099 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1100 return FT->isVariadic();
1101 return false;
1102}
1103
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001104bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1105 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1106 if (I->Body) {
1107 Definition = *I;
1108 return true;
1109 }
1110 }
1111
1112 return false;
1113}
1114
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001115Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001116 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1117 if (I->Body) {
1118 Definition = *I;
1119 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001120 }
1121 }
1122
1123 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001124}
1125
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001126void FunctionDecl::setBody(Stmt *B) {
1127 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001128 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001129 EndRangeLoc = B->getLocEnd();
1130}
1131
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001132void FunctionDecl::setPure(bool P) {
1133 IsPure = P;
1134 if (P)
1135 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1136 Parent->markedVirtualFunctionPure();
1137}
1138
Douglas Gregor16618f22009-09-12 00:17:51 +00001139bool FunctionDecl::isMain() const {
1140 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001141 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001142 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001143 getIdentifier() && getIdentifier()->isStr("main");
1144}
1145
Douglas Gregor16618f22009-09-12 00:17:51 +00001146bool FunctionDecl::isExternC() const {
1147 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001148 // In C, any non-static, non-overloadable function has external
1149 // linkage.
1150 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001151 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001152
Mike Stump11289f42009-09-09 15:08:12 +00001153 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001154 DC = DC->getParent()) {
1155 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1156 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001157 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001158 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001159
1160 break;
1161 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001162
1163 if (DC->isRecord())
1164 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001165 }
1166
Douglas Gregorbff62032010-10-21 16:57:46 +00001167 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001168}
1169
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001170bool FunctionDecl::isGlobal() const {
1171 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1172 return Method->isStatic();
1173
John McCall8e7d6562010-08-26 03:08:43 +00001174 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001175 return false;
1176
Mike Stump11289f42009-09-09 15:08:12 +00001177 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001178 DC->isNamespace();
1179 DC = DC->getParent()) {
1180 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1181 if (!Namespace->getDeclName())
1182 return false;
1183 break;
1184 }
1185 }
1186
1187 return true;
1188}
1189
Sebastian Redl833ef452010-01-26 22:01:41 +00001190void
1191FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1192 redeclarable_base::setPreviousDeclaration(PrevDecl);
1193
1194 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1195 FunctionTemplateDecl *PrevFunTmpl
1196 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1197 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1198 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1199 }
1200}
1201
1202const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1203 return getFirstDeclaration();
1204}
1205
1206FunctionDecl *FunctionDecl::getCanonicalDecl() {
1207 return getFirstDeclaration();
1208}
1209
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001210/// \brief Returns a value indicating whether this function
1211/// corresponds to a builtin function.
1212///
1213/// The function corresponds to a built-in function if it is
1214/// declared at translation scope or within an extern "C" block and
1215/// its name matches with the name of a builtin. The returned value
1216/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001217/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001218/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001219unsigned FunctionDecl::getBuiltinID() const {
1220 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001221 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1222 return 0;
1223
1224 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1225 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1226 return BuiltinID;
1227
1228 // This function has the name of a known C library
1229 // function. Determine whether it actually refers to the C library
1230 // function or whether it just has the same name.
1231
Douglas Gregora908e7f2009-02-17 03:23:10 +00001232 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001233 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001234 return 0;
1235
Douglas Gregore711f702009-02-14 18:57:46 +00001236 // If this function is at translation-unit scope and we're not in
1237 // C++, it refers to the C library function.
1238 if (!Context.getLangOptions().CPlusPlus &&
1239 getDeclContext()->isTranslationUnit())
1240 return BuiltinID;
1241
1242 // If the function is in an extern "C" linkage specification and is
1243 // not marked "overloadable", it's the real function.
1244 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001245 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001246 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001247 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001248 return BuiltinID;
1249
1250 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001251 return 0;
1252}
1253
1254
Chris Lattner47c0d002009-04-25 06:03:53 +00001255/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001256/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001257/// after it has been created.
1258unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001259 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001260 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001261 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001262 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001263
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001264}
1265
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001266void FunctionDecl::setParams(ASTContext &C,
1267 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001268 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001269 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001270
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001271 // Zero params -> null pointer.
1272 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001273 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001274 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001275 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001276
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001277 // Update source range. The check below allows us to set EndRangeLoc before
1278 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001279 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001280 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001281 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001282}
Chris Lattner41943152007-01-25 04:52:46 +00001283
Chris Lattner58258242008-04-10 02:22:51 +00001284/// getMinRequiredArguments - Returns the minimum number of arguments
1285/// needed to call this function. This may be fewer than the number of
1286/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001287/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001288unsigned FunctionDecl::getMinRequiredArguments() const {
1289 unsigned NumRequiredArgs = getNumParams();
1290 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001291 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001292 --NumRequiredArgs;
1293
1294 return NumRequiredArgs;
1295}
1296
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001297bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001298 // FIXME: This is not enough. Consider:
1299 //
1300 // inline void f();
1301 // void f() { }
1302 //
1303 // f is inlined, but does not have inline specified.
1304 // To fix this we should add an 'inline' flag to FunctionDecl.
1305 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001306 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001307
1308 if (isa<CXXMethodDecl>(this)) {
1309 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1310 return true;
1311 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001312
1313 switch (getTemplateSpecializationKind()) {
1314 case TSK_Undeclared:
1315 case TSK_ExplicitSpecialization:
1316 return false;
1317
1318 case TSK_ImplicitInstantiation:
1319 case TSK_ExplicitInstantiationDeclaration:
1320 case TSK_ExplicitInstantiationDefinition:
1321 // Handle below.
1322 break;
1323 }
1324
1325 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001326 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001327 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001328 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001329
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001330 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001331 return PatternDecl->isInlined();
1332
1333 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001334}
1335
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001336/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001337/// definition will be externally visible.
1338///
1339/// Inline function definitions are always available for inlining optimizations.
1340/// However, depending on the language dialect, declaration specifiers, and
1341/// attributes, the definition of an inline function may or may not be
1342/// "externally" visible to other translation units in the program.
1343///
1344/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001345/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001346/// inline definition becomes externally visible (C99 6.7.4p6).
1347///
1348/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1349/// definition, we use the GNU semantics for inline, which are nearly the
1350/// opposite of C99 semantics. In particular, "inline" by itself will create
1351/// an externally visible symbol, but "extern inline" will not create an
1352/// externally visible symbol.
1353bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1354 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001355 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001356 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001357
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001358 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001359 // GNU inline semantics. Based on a number of examples, we came up with the
1360 // following heuristic: if the "inline" keyword is present on a
1361 // declaration of the function but "extern" is not present on that
1362 // declaration, then the symbol is externally visible. Otherwise, the GNU
1363 // "extern inline" semantics applies and the symbol is not externally
1364 // visible.
1365 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1366 Redecl != RedeclEnd;
1367 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001368 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001369 return true;
1370 }
1371
1372 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001373 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001374 }
1375
1376 // C99 6.7.4p6:
1377 // [...] If all of the file scope declarations for a function in a
1378 // translation unit include the inline function specifier without extern,
1379 // then the definition in that translation unit is an inline definition.
1380 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1381 Redecl != RedeclEnd;
1382 ++Redecl) {
1383 // Only consider file-scope declarations in this test.
1384 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1385 continue;
1386
John McCall8e7d6562010-08-26 03:08:43 +00001387 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001388 return true; // Not an inline definition
1389 }
1390
1391 // C99 6.7.4p6:
1392 // An inline definition does not provide an external definition for the
1393 // function, and does not forbid an external definition in another
1394 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001395 return false;
1396}
1397
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001398/// getOverloadedOperator - Which C++ overloaded operator this
1399/// function represents, if any.
1400OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001401 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1402 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001403 else
1404 return OO_None;
1405}
1406
Alexis Huntc88db062010-01-13 09:01:02 +00001407/// getLiteralIdentifier - The literal suffix identifier this function
1408/// represents, if any.
1409const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1410 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1411 return getDeclName().getCXXLiteralIdentifier();
1412 else
1413 return 0;
1414}
1415
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001416FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1417 if (TemplateOrSpecialization.isNull())
1418 return TK_NonTemplate;
1419 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1420 return TK_FunctionTemplate;
1421 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1422 return TK_MemberSpecialization;
1423 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1424 return TK_FunctionTemplateSpecialization;
1425 if (TemplateOrSpecialization.is
1426 <DependentFunctionTemplateSpecializationInfo*>())
1427 return TK_DependentFunctionTemplateSpecialization;
1428
1429 assert(false && "Did we miss a TemplateOrSpecialization type?");
1430 return TK_NonTemplate;
1431}
1432
Douglas Gregord801b062009-10-07 23:56:10 +00001433FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001434 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001435 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1436
1437 return 0;
1438}
1439
Douglas Gregor06db9f52009-10-12 20:18:28 +00001440MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1441 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1442}
1443
Douglas Gregord801b062009-10-07 23:56:10 +00001444void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001445FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1446 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001447 TemplateSpecializationKind TSK) {
1448 assert(TemplateOrSpecialization.isNull() &&
1449 "Member function is already a specialization");
1450 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001451 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001452 TemplateOrSpecialization = Info;
1453}
1454
Douglas Gregorafca3b42009-10-27 20:53:28 +00001455bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001456 // If the function is invalid, it can't be implicitly instantiated.
1457 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001458 return false;
1459
1460 switch (getTemplateSpecializationKind()) {
1461 case TSK_Undeclared:
1462 case TSK_ExplicitSpecialization:
1463 case TSK_ExplicitInstantiationDefinition:
1464 return false;
1465
1466 case TSK_ImplicitInstantiation:
1467 return true;
1468
1469 case TSK_ExplicitInstantiationDeclaration:
1470 // Handled below.
1471 break;
1472 }
1473
1474 // Find the actual template from which we will instantiate.
1475 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001476 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001477 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001478 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001479
1480 // C++0x [temp.explicit]p9:
1481 // Except for inline functions, other explicit instantiation declarations
1482 // have the effect of suppressing the implicit instantiation of the entity
1483 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001484 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001485 return true;
1486
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001487 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001488}
1489
1490FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1491 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1492 while (Primary->getInstantiatedFromMemberTemplate()) {
1493 // If we have hit a point where the user provided a specialization of
1494 // this template, we're done looking.
1495 if (Primary->isMemberSpecialization())
1496 break;
1497
1498 Primary = Primary->getInstantiatedFromMemberTemplate();
1499 }
1500
1501 return Primary->getTemplatedDecl();
1502 }
1503
1504 return getInstantiatedFromMemberFunction();
1505}
1506
Douglas Gregor70d83e22009-06-29 17:30:29 +00001507FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001508 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001509 = TemplateOrSpecialization
1510 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001511 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001512 }
1513 return 0;
1514}
1515
1516const TemplateArgumentList *
1517FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001518 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001519 = TemplateOrSpecialization
1520 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001521 return Info->TemplateArguments;
1522 }
1523 return 0;
1524}
1525
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001526const TemplateArgumentListInfo *
1527FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1528 if (FunctionTemplateSpecializationInfo *Info
1529 = TemplateOrSpecialization
1530 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1531 return Info->TemplateArgumentsAsWritten;
1532 }
1533 return 0;
1534}
1535
Mike Stump11289f42009-09-09 15:08:12 +00001536void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001537FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1538 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001539 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001540 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001541 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001542 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1543 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001544 assert(TSK != TSK_Undeclared &&
1545 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001546 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001547 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001548 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001549 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1550 TemplateArgs,
1551 TemplateArgsAsWritten,
1552 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001553 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001555 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001556 // function template specializations.
1557 if (InsertPos)
1558 Template->getSpecializations().InsertNode(Info, InsertPos);
1559 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001560 // Try to insert the new node. If there is an existing node, leave it, the
1561 // set will contain the canonical decls while
1562 // FunctionTemplateDecl::findSpecialization will return
1563 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001564 FunctionTemplateSpecializationInfo *Existing
1565 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001566 (void)Existing;
1567 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1568 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001569 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001570}
1571
John McCallb9c78482010-04-08 09:05:18 +00001572void
1573FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1574 const UnresolvedSetImpl &Templates,
1575 const TemplateArgumentListInfo &TemplateArgs) {
1576 assert(TemplateOrSpecialization.isNull());
1577 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1578 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001579 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001580 void *Buffer = Context.Allocate(Size);
1581 DependentFunctionTemplateSpecializationInfo *Info =
1582 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1583 TemplateArgs);
1584 TemplateOrSpecialization = Info;
1585}
1586
1587DependentFunctionTemplateSpecializationInfo::
1588DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1589 const TemplateArgumentListInfo &TArgs)
1590 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1591
1592 d.NumTemplates = Ts.size();
1593 d.NumArgs = TArgs.size();
1594
1595 FunctionTemplateDecl **TsArray =
1596 const_cast<FunctionTemplateDecl**>(getTemplates());
1597 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1598 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1599
1600 TemplateArgumentLoc *ArgsArray =
1601 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1602 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1603 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1604}
1605
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001606TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001607 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001608 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001609 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001610 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001611 if (FTSInfo)
1612 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001613
Douglas Gregord801b062009-10-07 23:56:10 +00001614 MemberSpecializationInfo *MSInfo
1615 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1616 if (MSInfo)
1617 return MSInfo->getTemplateSpecializationKind();
1618
1619 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001620}
1621
Mike Stump11289f42009-09-09 15:08:12 +00001622void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001623FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1624 SourceLocation PointOfInstantiation) {
1625 if (FunctionTemplateSpecializationInfo *FTSInfo
1626 = TemplateOrSpecialization.dyn_cast<
1627 FunctionTemplateSpecializationInfo*>()) {
1628 FTSInfo->setTemplateSpecializationKind(TSK);
1629 if (TSK != TSK_ExplicitSpecialization &&
1630 PointOfInstantiation.isValid() &&
1631 FTSInfo->getPointOfInstantiation().isInvalid())
1632 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1633 } else if (MemberSpecializationInfo *MSInfo
1634 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1635 MSInfo->setTemplateSpecializationKind(TSK);
1636 if (TSK != TSK_ExplicitSpecialization &&
1637 PointOfInstantiation.isValid() &&
1638 MSInfo->getPointOfInstantiation().isInvalid())
1639 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1640 } else
1641 assert(false && "Function cannot have a template specialization kind");
1642}
1643
1644SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001645 if (FunctionTemplateSpecializationInfo *FTSInfo
1646 = TemplateOrSpecialization.dyn_cast<
1647 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001648 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001649 else if (MemberSpecializationInfo *MSInfo
1650 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001651 return MSInfo->getPointOfInstantiation();
1652
1653 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001654}
1655
Douglas Gregor6411b922009-09-11 20:15:17 +00001656bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001657 if (Decl::isOutOfLine())
1658 return true;
1659
1660 // If this function was instantiated from a member function of a
1661 // class template, check whether that member function was defined out-of-line.
1662 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1663 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001664 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001665 return Definition->isOutOfLine();
1666 }
1667
1668 // If this function was instantiated from a function template,
1669 // check whether that function template was defined out-of-line.
1670 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1671 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001672 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001673 return Definition->isOutOfLine();
1674 }
1675
1676 return false;
1677}
1678
Chris Lattner59a25942008-03-31 00:36:02 +00001679//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001680// FieldDecl Implementation
1681//===----------------------------------------------------------------------===//
1682
1683FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1684 IdentifierInfo *Id, QualType T,
1685 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1686 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1687}
1688
1689bool FieldDecl::isAnonymousStructOrUnion() const {
1690 if (!isImplicit() || getDeclName())
1691 return false;
1692
1693 if (const RecordType *Record = getType()->getAs<RecordType>())
1694 return Record->getDecl()->isAnonymousStructOrUnion();
1695
1696 return false;
1697}
1698
1699//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001700// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001701//===----------------------------------------------------------------------===//
1702
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001703SourceLocation TagDecl::getOuterLocStart() const {
1704 return getTemplateOrInnerLocStart(this);
1705}
1706
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001707SourceRange TagDecl::getSourceRange() const {
1708 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001709 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001710}
1711
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001712TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001713 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001714}
1715
Douglas Gregora72a4e32010-05-19 18:39:18 +00001716void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1717 TypedefDeclOrQualifier = TDD;
1718 if (TypeForDecl)
1719 TypeForDecl->ClearLinkageCache();
1720}
1721
Douglas Gregordee1be82009-01-17 00:42:38 +00001722void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001723 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001724
1725 if (isa<CXXRecordDecl>(this)) {
1726 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1727 struct CXXRecordDecl::DefinitionData *Data =
1728 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001729 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1730 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001731 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001732}
1733
1734void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001735 assert((!isa<CXXRecordDecl>(this) ||
1736 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1737 "definition completed but not started");
1738
Douglas Gregordee1be82009-01-17 00:42:38 +00001739 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001740 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001741
1742 if (ASTMutationListener *L = getASTMutationListener())
1743 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001744}
1745
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001746TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001747 if (isDefinition())
1748 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001749 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1750 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001751
1752 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001753 R != REnd; ++R)
1754 if (R->isDefinition())
1755 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001756
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001757 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001758}
1759
John McCall3e11ebe2010-03-15 10:12:16 +00001760void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1761 SourceRange QualifierRange) {
1762 if (Qualifier) {
1763 // Make sure the extended qualifier info is allocated.
1764 if (!hasExtInfo())
1765 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1766 // Set qualifier info.
1767 getExtInfo()->NNS = Qualifier;
1768 getExtInfo()->NNSRange = QualifierRange;
1769 }
1770 else {
1771 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1772 assert(QualifierRange.isInvalid());
1773 if (hasExtInfo()) {
1774 getASTContext().Deallocate(getExtInfo());
1775 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1776 }
1777 }
1778}
1779
Ted Kremenek21475702008-09-05 17:16:31 +00001780//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001781// EnumDecl Implementation
1782//===----------------------------------------------------------------------===//
1783
1784EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1785 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001786 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1787 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1788 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001789 C.getTypeDeclType(Enum, PrevDecl);
1790 return Enum;
1791}
1792
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001793EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001794 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1795 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001796}
1797
Douglas Gregord5058122010-02-11 01:19:42 +00001798void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001799 QualType NewPromotionType,
1800 unsigned NumPositiveBits,
1801 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001802 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001803 if (!IntegerType)
1804 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001805 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001806 setNumPositiveBits(NumPositiveBits);
1807 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001808 TagDecl::completeDefinition();
1809}
1810
1811//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001812// RecordDecl Implementation
1813//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001814
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001815RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001816 IdentifierInfo *Id, RecordDecl *PrevDecl,
1817 SourceLocation TKL)
1818 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001819 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001820 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001821 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001822 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001823 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001824}
1825
1826RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001827 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001828 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001829
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001830 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001831 C.getTypeDeclType(R, PrevDecl);
1832 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001833}
1834
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001835RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1836 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1837 SourceLocation());
1838}
1839
Douglas Gregordfcad112009-03-25 15:59:44 +00001840bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001841 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001842 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1843}
1844
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001845RecordDecl::field_iterator RecordDecl::field_begin() const {
1846 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1847 LoadFieldsFromExternalStorage();
1848
1849 return field_iterator(decl_iterator(FirstDecl));
1850}
1851
Douglas Gregor91f84212008-12-11 16:49:14 +00001852/// completeDefinition - Notes that the definition of this type is now
1853/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001854void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001855 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001856 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001857}
Steve Naroffcc321422007-03-26 23:09:51 +00001858
John McCall61925b02010-05-21 01:17:40 +00001859ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1860 // Force the decl chain to come into existence properly.
1861 if (!getNextDeclInContext()) getParent()->decls_begin();
1862
1863 assert(isAnonymousStructOrUnion());
1864 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1865 assert(D->getType()->isRecordType());
1866 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1867 return D;
1868}
1869
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001870void RecordDecl::LoadFieldsFromExternalStorage() const {
1871 ExternalASTSource *Source = getASTContext().getExternalSource();
1872 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1873
1874 // Notify that we have a RecordDecl doing some initialization.
1875 ExternalASTSource::Deserializing TheFields(Source);
1876
1877 llvm::SmallVector<Decl*, 64> Decls;
1878 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1879 return;
1880
1881#ifndef NDEBUG
1882 // Check that all decls we got were FieldDecls.
1883 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1884 assert(isa<FieldDecl>(Decls[i]));
1885#endif
1886
1887 LoadedFieldsFromExternalStorage = true;
1888
1889 if (Decls.empty())
1890 return;
1891
1892 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1893}
1894
Steve Naroff415d3d52008-10-08 17:01:13 +00001895//===----------------------------------------------------------------------===//
1896// BlockDecl Implementation
1897//===----------------------------------------------------------------------===//
1898
Douglas Gregord5058122010-02-11 01:19:42 +00001899void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001900 unsigned NParms) {
1901 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001902
Steve Naroffc4b30e52009-03-13 16:56:44 +00001903 // Zero params -> null pointer.
1904 if (NParms) {
1905 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001906 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001907 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1908 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1909 }
1910}
1911
1912unsigned BlockDecl::getNumParams() const {
1913 return NumParams;
1914}
Sebastian Redl833ef452010-01-26 22:01:41 +00001915
1916
1917//===----------------------------------------------------------------------===//
1918// Other Decl Allocation/Deallocation Method Implementations
1919//===----------------------------------------------------------------------===//
1920
1921TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1922 return new (C) TranslationUnitDecl(C);
1923}
1924
1925NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1926 SourceLocation L, IdentifierInfo *Id) {
1927 return new (C) NamespaceDecl(DC, L, Id);
1928}
1929
Douglas Gregor417e87c2010-10-27 19:49:05 +00001930NamespaceDecl *NamespaceDecl::getNextNamespace() {
1931 return dyn_cast_or_null<NamespaceDecl>(
1932 NextNamespace.get(getASTContext().getExternalSource()));
1933}
1934
Sebastian Redl833ef452010-01-26 22:01:41 +00001935ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1936 SourceLocation L, IdentifierInfo *Id, QualType T) {
1937 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1938}
1939
1940FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001941 const DeclarationNameInfo &NameInfo,
1942 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001943 StorageClass S, StorageClass SCAsWritten,
1944 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001945 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001946 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001947 New->HasWrittenPrototype = hasWrittenPrototype;
1948 return New;
1949}
1950
1951BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1952 return new (C) BlockDecl(DC, L);
1953}
1954
1955EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1956 SourceLocation L,
1957 IdentifierInfo *Id, QualType T,
1958 Expr *E, const llvm::APSInt &V) {
1959 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1960}
1961
Douglas Gregorbe996932010-09-01 20:41:53 +00001962SourceRange EnumConstantDecl::getSourceRange() const {
1963 SourceLocation End = getLocation();
1964 if (Init)
1965 End = Init->getLocEnd();
1966 return SourceRange(getLocation(), End);
1967}
1968
Sebastian Redl833ef452010-01-26 22:01:41 +00001969TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1970 SourceLocation L, IdentifierInfo *Id,
1971 TypeSourceInfo *TInfo) {
1972 return new (C) TypedefDecl(DC, L, Id, TInfo);
1973}
1974
Sebastian Redl833ef452010-01-26 22:01:41 +00001975FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1976 SourceLocation L,
1977 StringLiteral *Str) {
1978 return new (C) FileScopeAsmDecl(DC, L, Str);
1979}