blob: 42e762d56ab676fc5eb4b0ad7dfd445cf2bef10c [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 McCall457a04e2010-10-22 21:05:15 +000036static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
37 switch (A->getVisibility()) {
38 case VisibilityAttr::Default:
39 return DefaultVisibility;
40 case VisibilityAttr::Hidden:
41 return HiddenVisibility;
42 case VisibilityAttr::Protected:
43 return ProtectedVisibility;
44 }
45 return DefaultVisibility;
46}
47
48typedef std::pair<Linkage,Visibility> LVPair;
49static LVPair merge(LVPair L, LVPair R) {
50 return LVPair(minLinkage(L.first, R.first),
51 minVisibility(L.second, R.second));
52}
53
Douglas Gregor7dc5c172010-02-03 09:33:45 +000054/// \brief Get the most restrictive linkage for the types in the given
55/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +000056static LVPair
57getLVForTemplateParameterList(const TemplateParameterList *Params) {
58 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000059 for (TemplateParameterList::const_iterator P = Params->begin(),
60 PEnd = Params->end();
61 P != PEnd; ++P) {
62 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
63 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +000064 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +000065 continue;
66 }
67
68 if (TemplateTemplateParmDecl *TTP
69 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCall457a04e2010-10-22 21:05:15 +000070 LV =
71 merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +000072 }
73 }
74
John McCall457a04e2010-10-22 21:05:15 +000075 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +000076}
77
78/// \brief Get the most restrictive linkage for the types and
79/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +000080static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
81 unsigned NumArgs) {
82 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000083
84 for (unsigned I = 0; I != NumArgs; ++I) {
85 switch (Args[I].getKind()) {
86 case TemplateArgument::Null:
87 case TemplateArgument::Integral:
88 case TemplateArgument::Expression:
89 break;
90
91 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +000092 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +000093 break;
94
95 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +000096 // The decl can validly be null as the representation of nullptr
97 // arguments, valid only in C++0x.
98 if (Decl *D = Args[I].getAsDecl()) {
99 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
100 LV = merge(LV, ND->getLinkageAndVisibility());
101 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
102 LV = merge(LV, VD->getType()->getLinkageAndVisibility());
103 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000104 break;
105
106 case TemplateArgument::Template:
John McCall457a04e2010-10-22 21:05:15 +0000107 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
108 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000109 break;
110
111 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000112 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
113 Args[I].pack_size()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000114 break;
115 }
116 }
117
John McCall457a04e2010-10-22 21:05:15 +0000118 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000119}
120
John McCall457a04e2010-10-22 21:05:15 +0000121static LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
122 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
123 TArgs.flat_size());
John McCall8823c652010-08-13 08:35:10 +0000124}
125
John McCall457a04e2010-10-22 21:05:15 +0000126static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000127 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000128 "Not a name having namespace scope");
129 ASTContext &Context = D->getASTContext();
130
131 // C++ [basic.link]p3:
132 // A name having namespace scope (3.3.6) has internal linkage if it
133 // is the name of
134 // - an object, reference, function or function template that is
135 // explicitly declared static; or,
136 // (This bullet corresponds to C99 6.2.2p3.)
137 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
138 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000139 if (Var->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000140 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000141
142 // - an object or reference that is explicitly declared const
143 // and neither explicitly declared extern nor previously
144 // declared to have external linkage; or
145 // (there is no equivalent in C99)
146 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000147 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000148 Var->getStorageClass() != SC_Extern &&
149 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000150 bool FoundExtern = false;
151 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
152 PrevVar && !FoundExtern;
153 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000154 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000155 FoundExtern = true;
156
157 if (!FoundExtern)
John McCall457a04e2010-10-22 21:05:15 +0000158 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000159 }
160 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000161 // C++ [temp]p4:
162 // A non-member function template can have internal linkage; any
163 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000164 const FunctionDecl *Function = 0;
165 if (const FunctionTemplateDecl *FunTmpl
166 = dyn_cast<FunctionTemplateDecl>(D))
167 Function = FunTmpl->getTemplatedDecl();
168 else
169 Function = cast<FunctionDecl>(D);
170
171 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000172 if (Function->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000173 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000174 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
175 // - a data member of an anonymous union.
176 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCall457a04e2010-10-22 21:05:15 +0000177 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000178 }
179
John McCall457a04e2010-10-22 21:05:15 +0000180 if (D->isInAnonymousNamespace())
181 return LVPair(UniqueExternalLinkage, DefaultVisibility);
182
183 // Set up the defaults.
184
185 // C99 6.2.2p5:
186 // If the declaration of an identifier for an object has file
187 // scope and no storage-class specifier, its linkage is
188 // external.
189 LVPair LV(ExternalLinkage, DefaultVisibility);
190
191 // We ignore -fvisibility on non-definitions and explicit
192 // instantiation declarations.
193 bool ConsiderDashFVisibility = true;
194
Douglas Gregorf73b2822009-11-25 22:24:25 +0000195 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000196
Douglas Gregorf73b2822009-11-25 22:24:25 +0000197 // A name having namespace scope has external linkage if it is the
198 // name of
199 //
200 // - an object or reference, unless it has internal linkage; or
201 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000202 // Modify the variable's LV by the LV of its type unless this is
203 // C or extern "C". This follows from [basic.link]p9:
204 // A type without linkage shall not be used as the type of a
205 // variable or function with external linkage unless
206 // - the entity has C language linkage, or
207 // - the entity is declared within an unnamed namespace, or
208 // - the entity is not used or is defined in the same
209 // translation unit.
210 // and [basic.link]p10:
211 // ...the types specified by all declarations referring to a
212 // given variable or function shall be identical...
213 // C does not have an equivalent rule.
214 //
215 // Note that we don't want to make the variable non-external
216 // because of this, but unique-external linkage suits us.
217 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
218 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
219 if (TypeLV.first != ExternalLinkage)
220 return LVPair(UniqueExternalLinkage, DefaultVisibility);
221 LV.second = minVisibility(LV.second, TypeLV.second);
222 }
223
Douglas Gregorf73b2822009-11-25 22:24:25 +0000224 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000225 (Var->getStorageClass() == SC_Extern ||
226 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000227 if (Var->getStorageClass() == SC_PrivateExtern)
228 LV.second = HiddenVisibility;
229
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 // C99 6.2.2p4:
231 // For an identifier declared with the storage-class specifier
232 // extern in a scope in which a prior declaration of that
233 // identifier is visible, if the prior declaration specifies
234 // internal or external linkage, the linkage of the identifier
235 // at the later declaration is the same as the linkage
236 // specified at the prior declaration. If no prior declaration
237 // is visible, or if the prior declaration specifies no
238 // linkage, then the identifier has external linkage.
239 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000240 LVPair PrevLV = PrevVar->getLinkageAndVisibility();
241 if (PrevLV.first) LV.first = PrevLV.first;
242 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000243 }
244 }
245
Douglas Gregorf73b2822009-11-25 22:24:25 +0000246 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000247 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
248 // Modify the function's LV by the LV of its type unless this is
249 // C or extern "C". See the comment above about variables.
250 if (Context.getLangOptions().CPlusPlus && !Function->isExternC()) {
251 LVPair TypeLV = Function->getType()->getLinkageAndVisibility();
252 if (TypeLV.first != ExternalLinkage)
253 return LVPair(UniqueExternalLinkage, DefaultVisibility);
254 LV.second = minVisibility(LV.second, TypeLV.second);
255 }
256
Douglas Gregorf73b2822009-11-25 22:24:25 +0000257 // C99 6.2.2p5:
258 // If the declaration of an identifier for a function has no
259 // storage-class specifier, its linkage is determined exactly
260 // as if it were declared with the storage-class specifier
261 // extern.
262 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000263 (Function->getStorageClass() == SC_Extern ||
264 Function->getStorageClass() == SC_PrivateExtern ||
265 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000266 // C99 6.2.2p4:
267 // For an identifier declared with the storage-class specifier
268 // extern in a scope in which a prior declaration of that
269 // identifier is visible, if the prior declaration specifies
270 // internal or external linkage, the linkage of the identifier
271 // at the later declaration is the same as the linkage
272 // specified at the prior declaration. If no prior declaration
273 // is visible, or if the prior declaration specifies no
274 // linkage, then the identifier has external linkage.
275 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000276 LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
277 if (PrevLV.first) LV.first = PrevLV.first;
278 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000279 }
280 }
281
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000282 if (FunctionTemplateSpecializationInfo *SpecInfo
283 = Function->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000284 LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000285 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCall457a04e2010-10-22 21:05:15 +0000286 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
287
288 if (SpecInfo->getTemplateSpecializationKind()
289 == TSK_ExplicitInstantiationDeclaration)
290 ConsiderDashFVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000291 }
292
John McCall457a04e2010-10-22 21:05:15 +0000293 if (ConsiderDashFVisibility)
294 ConsiderDashFVisibility = Function->hasBody();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000295
296 // - a named class (Clause 9), or an unnamed class defined in a
297 // typedef declaration in which the class has the typedef name
298 // for linkage purposes (7.1.3); or
299 // - a named enumeration (7.2), or an unnamed enumeration
300 // defined in a typedef declaration in which the enumeration
301 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000302 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
303 // Unnamed tags have no linkage.
304 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
305 return LVPair(NoLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000306
John McCall457a04e2010-10-22 21:05:15 +0000307 // If this is a class template specialization, consider the
308 // linkage of the template and template arguments.
309 if (const ClassTemplateSpecializationDecl *Spec
310 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
311 // From the template. Note below the restrictions on how we
312 // compute template visibility.
313 LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000314
John McCall457a04e2010-10-22 21:05:15 +0000315 // The arguments at which the template was instantiated.
316 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
317 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
318
319 if (Spec->getTemplateSpecializationKind()
320 == TSK_ExplicitInstantiationDeclaration)
321 ConsiderDashFVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000322 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000323
John McCall457a04e2010-10-22 21:05:15 +0000324 if (ConsiderDashFVisibility)
325 ConsiderDashFVisibility = Tag->isDefinition();
326
Douglas Gregorf73b2822009-11-25 22:24:25 +0000327 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000328 } else if (isa<EnumConstantDecl>(D)) {
329 LVPair EnumLV =
330 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
331 if (!isExternalLinkage(EnumLV.first))
332 return LVPair(NoLinkage, DefaultVisibility);
333 LV = merge(LV, EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000334
335 // - a template, unless it is a function template that has
336 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000337 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
338 LV = merge(LV, getLVForTemplateParameterList(
339 Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000340
John McCall457a04e2010-10-22 21:05:15 +0000341 // We do not want to consider attributes or global settings when
342 // computing template visibility.
343 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000344
345 // - a namespace (7.3), unless it is declared within an unnamed
346 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000347 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
348 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000349
John McCall457a04e2010-10-22 21:05:15 +0000350 // By extension, we assign external linkage to Objective-C
351 // interfaces.
352 } else if (isa<ObjCInterfaceDecl>(D)) {
353 // fallout
354
355 // Everything not covered here has no linkage.
356 } else {
357 return LVPair(NoLinkage, DefaultVisibility);
358 }
359
360 // If we ended up with non-external linkage, visibility should
361 // always be default.
362 if (LV.first != ExternalLinkage)
363 return LVPair(LV.first, DefaultVisibility);
364
365 // If we didn't end up with hidden visibility, consider attributes
366 // and -fvisibility.
367 if (LV.second != HiddenVisibility) {
368 Visibility StandardV;
369
370 // If we have an explicit visibility attribute, merge that in.
371 const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
372 if (VA)
373 StandardV = GetVisibilityFromAttr(VA);
374 else if (ConsiderDashFVisibility)
375 StandardV = Context.getLangOptions().getVisibilityMode();
376 else
377 StandardV = DefaultVisibility; // no-op
378
379 LV.second = minVisibility(LV.second, StandardV);
380 }
381
382 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000383}
384
John McCall457a04e2010-10-22 21:05:15 +0000385static LVPair getLVForClassMember(const NamedDecl *D) {
386 // Only certain class members have linkage. Note that fields don't
387 // really have linkage, but it's convenient to say they do for the
388 // purposes of calculating linkage of pointer-to-data-member
389 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000390 if (!(isa<CXXMethodDecl>(D) ||
391 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000392 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000393 (isa<TagDecl>(D) &&
394 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCall457a04e2010-10-22 21:05:15 +0000395 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000396
397 // Class members only have linkage if their class has external linkage.
John McCall457a04e2010-10-22 21:05:15 +0000398 LVPair ClassLV =
399 cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility();
400 if (!isExternalLinkage(ClassLV.first))
401 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000402
403 // If the class already has unique-external linkage, we can't improve.
John McCall457a04e2010-10-22 21:05:15 +0000404 if (ClassLV.first == UniqueExternalLinkage)
405 return LVPair(UniqueExternalLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000406
John McCall457a04e2010-10-22 21:05:15 +0000407 // Start with the class's linkage and visibility.
408 LVPair LV = ClassLV;
409
410 // If we have an explicit visibility attribute, merge that in.
411 const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
412 if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
413
414 // If it's a value declaration, apply the LV from its type.
415 // See the comment about namespace-scope variable decls above.
416 if (isa<ValueDecl>(D)) {
417 LVPair TypeLV = cast<ValueDecl>(D)->getType()->getLinkageAndVisibility();
418 if (TypeLV.first != ExternalLinkage)
419 LV.first = minLinkage(LV.first, UniqueExternalLinkage);
420 LV.second = minVisibility(LV.second, TypeLV.second);
421 }
422
John McCall8823c652010-08-13 08:35:10 +0000423 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000424 // If this is a method template specialization, use the linkage for
425 // the template parameters and arguments.
426 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000427 = MD->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000428 LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
429 LV = merge(LV, getLVForTemplateParameterList(
430 Spec->getTemplate()->getTemplateParameters()));
John McCall8823c652010-08-13 08:35:10 +0000431 }
432
John McCall457a04e2010-10-22 21:05:15 +0000433 // If -fvisibility-inlines-hidden was provided, then inline C++
434 // member functions get "hidden" visibility if they don't have an
435 // explicit visibility attribute.
436 if (!VA && MD->isInlined() && LV.second > HiddenVisibility &&
437 D->getASTContext().getLangOptions().InlineVisibilityHidden)
438 LV.second = HiddenVisibility;
439
John McCall8823c652010-08-13 08:35:10 +0000440 // Similarly for member class template specializations.
441 } else if (const ClassTemplateSpecializationDecl *Spec
442 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000443 LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
444 LV = merge(LV, getLVForTemplateParameterList(
445 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall8823c652010-08-13 08:35:10 +0000446 }
447
John McCall457a04e2010-10-22 21:05:15 +0000448 return LV;
John McCall8823c652010-08-13 08:35:10 +0000449}
450
John McCall457a04e2010-10-22 21:05:15 +0000451LVPair NamedDecl::getLinkageAndVisibility() const {
Ted Kremenek926d8602010-04-20 23:15:35 +0000452
453 // Objective-C: treat all Objective-C declarations as having external
454 // linkage.
455 switch (getKind()) {
456 default:
457 break;
John McCall457a04e2010-10-22 21:05:15 +0000458 case Decl::TemplateTemplateParm: // count these as external
459 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000460 case Decl::ObjCAtDefsField:
461 case Decl::ObjCCategory:
462 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000463 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000464 case Decl::ObjCForwardProtocol:
465 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000466 case Decl::ObjCMethod:
467 case Decl::ObjCProperty:
468 case Decl::ObjCPropertyImpl:
469 case Decl::ObjCProtocol:
John McCall457a04e2010-10-22 21:05:15 +0000470 return LVPair(ExternalLinkage, DefaultVisibility);
Ted Kremenek926d8602010-04-20 23:15:35 +0000471 }
472
Douglas Gregorf73b2822009-11-25 22:24:25 +0000473 // Handle linkage for namespace-scope names.
Sebastian Redl50c68252010-08-31 00:36:30 +0000474 if (getDeclContext()->getRedeclContext()->isFileContext())
John McCall457a04e2010-10-22 21:05:15 +0000475 return getLVForNamespaceScopeDecl(this);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000476
477 // C++ [basic.link]p5:
478 // In addition, a member function, static data member, a named
479 // class or enumeration of class scope, or an unnamed class or
480 // enumeration defined in a class-scope typedef declaration such
481 // that the class or enumeration has the typedef name for linkage
482 // purposes (7.1.3), has external linkage if the name of the class
483 // has external linkage.
John McCall8823c652010-08-13 08:35:10 +0000484 if (getDeclContext()->isRecord())
John McCall457a04e2010-10-22 21:05:15 +0000485 return getLVForClassMember(this);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000486
487 // C++ [basic.link]p6:
488 // The name of a function declared in block scope and the name of
489 // an object declared by a block scope extern declaration have
490 // linkage. If there is a visible declaration of an entity with
491 // linkage having the same name and type, ignoring entities
492 // declared outside the innermost enclosing namespace scope, the
493 // block scope declaration declares that same entity and receives
494 // the linkage of the previous declaration. If there is more than
495 // one such matching entity, the program is ill-formed. Otherwise,
496 // if no matching entity is found, the block scope entity receives
497 // external linkage.
498 if (getLexicalDeclContext()->isFunctionOrMethod()) {
499 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000500 if (Function->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000501 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000502
John McCall457a04e2010-10-22 21:05:15 +0000503 LVPair LV(ExternalLinkage, DefaultVisibility);
504 if (const VisibilityAttr *VA = Function->getAttr<VisibilityAttr>())
505 LV.second = GetVisibilityFromAttr(VA);
506
507 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
508 LVPair PrevLV = Prev->getLinkageAndVisibility();
509 if (PrevLV.first) LV.first = PrevLV.first;
510 LV.second = minVisibility(LV.second, PrevLV.second);
511 }
512
513 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000514 }
515
516 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
John McCall8e7d6562010-08-26 03:08:43 +0000517 if (Var->getStorageClass() == SC_Extern ||
518 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000519 if (Var->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000520 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000521
John McCall457a04e2010-10-22 21:05:15 +0000522 LVPair LV(ExternalLinkage, DefaultVisibility);
523 if (Var->getStorageClass() == SC_PrivateExtern)
524 LV.second = HiddenVisibility;
525 else if (const VisibilityAttr *VA = Var->getAttr<VisibilityAttr>())
526 LV.second = GetVisibilityFromAttr(VA);
527
528 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
529 LVPair PrevLV = Prev->getLinkageAndVisibility();
530 if (PrevLV.first) LV.first = PrevLV.first;
531 LV.second = minVisibility(LV.second, PrevLV.second);
532 }
533
534 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000535 }
536 }
537
538 // C++ [basic.link]p6:
539 // Names not covered by these rules have no linkage.
John McCall457a04e2010-10-22 21:05:15 +0000540 return LVPair(NoLinkage, DefaultVisibility);
541}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000542
Douglas Gregor2ada0482009-02-04 17:27:36 +0000543std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000544 return getQualifiedNameAsString(getASTContext().getLangOptions());
545}
546
547std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000548 const DeclContext *Ctx = getDeclContext();
549
550 if (Ctx->isFunctionOrMethod())
551 return getNameAsString();
552
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000553 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
554 ContextsTy Contexts;
555
556 // Collect contexts.
557 while (Ctx && isa<NamedDecl>(Ctx)) {
558 Contexts.push_back(Ctx);
559 Ctx = Ctx->getParent();
560 };
561
562 std::string QualName;
563 llvm::raw_string_ostream OS(QualName);
564
565 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
566 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000567 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000568 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000569 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
570 std::string TemplateArgsStr
571 = TemplateSpecializationType::PrintTemplateArgumentList(
572 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000573 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000574 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000575 OS << Spec->getName() << TemplateArgsStr;
576 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000577 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000578 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000579 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000580 OS << ND;
581 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
582 if (!RD->getIdentifier())
583 OS << "<anonymous " << RD->getKindName() << '>';
584 else
585 OS << RD;
586 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000587 const FunctionProtoType *FT = 0;
588 if (FD->hasWrittenPrototype())
589 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
590
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000591 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000592 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000593 unsigned NumParams = FD->getNumParams();
594 for (unsigned i = 0; i < NumParams; ++i) {
595 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000596 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000597 std::string Param;
598 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000599 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000600 }
601
602 if (FT->isVariadic()) {
603 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000604 OS << ", ";
605 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000606 }
607 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000608 OS << ')';
609 } else {
610 OS << cast<NamedDecl>(*I);
611 }
612 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000613 }
614
John McCalla2a3f7d2010-03-16 21:48:18 +0000615 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000616 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000617 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000618 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000619
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000620 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000621}
622
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000623bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000624 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
625
Douglas Gregor889ceb72009-02-03 19:21:40 +0000626 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
627 // We want to keep it, unless it nominates same namespace.
628 if (getKind() == Decl::UsingDirective) {
629 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
630 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000633 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
634 // For function declarations, we keep track of redeclarations.
635 return FD->getPreviousDeclaration() == OldD;
636
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000637 // For function templates, the underlying function declarations are linked.
638 if (const FunctionTemplateDecl *FunctionTemplate
639 = dyn_cast<FunctionTemplateDecl>(this))
640 if (const FunctionTemplateDecl *OldFunctionTemplate
641 = dyn_cast<FunctionTemplateDecl>(OldD))
642 return FunctionTemplate->getTemplatedDecl()
643 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000644
Steve Naroffc4173fa2009-02-22 19:35:57 +0000645 // For method declarations, we keep track of redeclarations.
646 if (isa<ObjCMethodDecl>(this))
647 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000648
John McCall9f3059a2009-10-09 21:13:30 +0000649 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
650 return true;
651
John McCall3f746822009-11-17 05:59:44 +0000652 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
653 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
654 cast<UsingShadowDecl>(OldD)->getTargetDecl();
655
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000656 // For non-function declarations, if the declarations are of the
657 // same kind then this must be a redeclaration, or semantic analysis
658 // would not have given us the new declaration.
659 return this->getKind() == OldD->getKind();
660}
661
Douglas Gregoreddf4332009-02-24 20:03:32 +0000662bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000663 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000664}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000665
Anders Carlsson6915bf62009-06-26 06:29:23 +0000666NamedDecl *NamedDecl::getUnderlyingDecl() {
667 NamedDecl *ND = this;
668 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000669 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000670 ND = UD->getTargetDecl();
671 else if (ObjCCompatibleAliasDecl *AD
672 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
673 return AD->getClassInterface();
674 else
675 return ND;
676 }
677}
678
John McCalla8ae2222010-04-06 21:38:20 +0000679bool NamedDecl::isCXXInstanceMember() const {
680 assert(isCXXClassMember() &&
681 "checking whether non-member is instance member");
682
683 const NamedDecl *D = this;
684 if (isa<UsingShadowDecl>(D))
685 D = cast<UsingShadowDecl>(D)->getTargetDecl();
686
687 if (isa<FieldDecl>(D))
688 return true;
689 if (isa<CXXMethodDecl>(D))
690 return cast<CXXMethodDecl>(D)->isInstance();
691 if (isa<FunctionTemplateDecl>(D))
692 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
693 ->getTemplatedDecl())->isInstance();
694 return false;
695}
696
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000697//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000698// DeclaratorDecl Implementation
699//===----------------------------------------------------------------------===//
700
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000701template <typename DeclT>
702static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
703 if (decl->getNumTemplateParameterLists() > 0)
704 return decl->getTemplateParameterList(0)->getTemplateLoc();
705 else
706 return decl->getInnerLocStart();
707}
708
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000709SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000710 TypeSourceInfo *TSI = getTypeSourceInfo();
711 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000712 return SourceLocation();
713}
714
John McCall3e11ebe2010-03-15 10:12:16 +0000715void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
716 SourceRange QualifierRange) {
717 if (Qualifier) {
718 // Make sure the extended decl info is allocated.
719 if (!hasExtInfo()) {
720 // Save (non-extended) type source info pointer.
721 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
722 // Allocate external info struct.
723 DeclInfo = new (getASTContext()) ExtInfo;
724 // Restore savedTInfo into (extended) decl info.
725 getExtInfo()->TInfo = savedTInfo;
726 }
727 // Set qualifier info.
728 getExtInfo()->NNS = Qualifier;
729 getExtInfo()->NNSRange = QualifierRange;
730 }
731 else {
732 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
733 assert(QualifierRange.isInvalid());
734 if (hasExtInfo()) {
735 // Save type source info pointer.
736 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
737 // Deallocate the extended decl info.
738 getASTContext().Deallocate(getExtInfo());
739 // Restore savedTInfo into (non-extended) decl info.
740 DeclInfo = savedTInfo;
741 }
742 }
743}
744
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000745SourceLocation DeclaratorDecl::getOuterLocStart() const {
746 return getTemplateOrInnerLocStart(this);
747}
748
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000749void
Douglas Gregor20527e22010-06-15 17:44:38 +0000750QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
751 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000752 TemplateParameterList **TPLists) {
753 assert((NumTPLists == 0 || TPLists != 0) &&
754 "Empty array of template parameters with positive size!");
755 assert((NumTPLists == 0 || NNS) &&
756 "Nonempty array of template parameters with no qualifier!");
757
758 // Free previous template parameters (if any).
759 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000760 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000761 TemplParamLists = 0;
762 NumTemplParamLists = 0;
763 }
764 // Set info on matched template parameter lists (if any).
765 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000766 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000767 NumTemplParamLists = NumTPLists;
768 for (unsigned i = NumTPLists; i-- > 0; )
769 TemplParamLists[i] = TPLists[i];
770 }
771}
772
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000773//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000774// VarDecl Implementation
775//===----------------------------------------------------------------------===//
776
Sebastian Redl833ef452010-01-26 22:01:41 +0000777const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
778 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000779 case SC_None: break;
780 case SC_Auto: return "auto"; break;
781 case SC_Extern: return "extern"; break;
782 case SC_PrivateExtern: return "__private_extern__"; break;
783 case SC_Register: return "register"; break;
784 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000785 }
786
787 assert(0 && "Invalid storage class");
788 return 0;
789}
790
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000791VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000792 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000793 StorageClass S, StorageClass SCAsWritten) {
794 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000795}
796
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000797SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000798 SourceLocation Start = getTypeSpecStartLoc();
799 if (Start.isInvalid())
800 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000801 return Start;
802}
803
804SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000805 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000806 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
807 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000808}
809
Sebastian Redl833ef452010-01-26 22:01:41 +0000810bool VarDecl::isExternC() const {
811 ASTContext &Context = getASTContext();
812 if (!Context.getLangOptions().CPlusPlus)
813 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000814 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000815 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
816
817 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
818 DC = DC->getParent()) {
819 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
820 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000821 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000822
823 break;
824 }
825
826 if (DC->isFunctionOrMethod())
827 return false;
828 }
829
830 return false;
831}
832
833VarDecl *VarDecl::getCanonicalDecl() {
834 return getFirstDeclaration();
835}
836
Sebastian Redl35351a92010-01-31 22:27:38 +0000837VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
838 // C++ [basic.def]p2:
839 // A declaration is a definition unless [...] it contains the 'extern'
840 // specifier or a linkage-specification and neither an initializer [...],
841 // it declares a static data member in a class declaration [...].
842 // C++ [temp.expl.spec]p15:
843 // An explicit specialization of a static data member of a template is a
844 // definition if the declaration includes an initializer; otherwise, it is
845 // a declaration.
846 if (isStaticDataMember()) {
847 if (isOutOfLine() && (hasInit() ||
848 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
849 return Definition;
850 else
851 return DeclarationOnly;
852 }
853 // C99 6.7p5:
854 // A definition of an identifier is a declaration for that identifier that
855 // [...] causes storage to be reserved for that object.
856 // Note: that applies for all non-file-scope objects.
857 // C99 6.9.2p1:
858 // If the declaration of an identifier for an object has file scope and an
859 // initializer, the declaration is an external definition for the identifier
860 if (hasInit())
861 return Definition;
862 // AST for 'extern "C" int foo;' is annotated with 'extern'.
863 if (hasExternalStorage())
864 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000865
John McCall8e7d6562010-08-26 03:08:43 +0000866 if (getStorageClassAsWritten() == SC_Extern ||
867 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000868 for (const VarDecl *PrevVar = getPreviousDeclaration();
869 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
870 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
871 return DeclarationOnly;
872 }
873 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000874 // C99 6.9.2p2:
875 // A declaration of an object that has file scope without an initializer,
876 // and without a storage class specifier or the scs 'static', constitutes
877 // a tentative definition.
878 // No such thing in C++.
879 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
880 return TentativeDefinition;
881
882 // What's left is (in C, block-scope) declarations without initializers or
883 // external storage. These are definitions.
884 return Definition;
885}
886
Sebastian Redl35351a92010-01-31 22:27:38 +0000887VarDecl *VarDecl::getActingDefinition() {
888 DefinitionKind Kind = isThisDeclarationADefinition();
889 if (Kind != TentativeDefinition)
890 return 0;
891
Chris Lattner48eb14d2010-06-14 18:31:46 +0000892 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000893 VarDecl *First = getFirstDeclaration();
894 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
895 I != E; ++I) {
896 Kind = (*I)->isThisDeclarationADefinition();
897 if (Kind == Definition)
898 return 0;
899 else if (Kind == TentativeDefinition)
900 LastTentative = *I;
901 }
902 return LastTentative;
903}
904
905bool VarDecl::isTentativeDefinitionNow() const {
906 DefinitionKind Kind = isThisDeclarationADefinition();
907 if (Kind != TentativeDefinition)
908 return false;
909
910 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
911 if ((*I)->isThisDeclarationADefinition() == Definition)
912 return false;
913 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000914 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000915}
916
Sebastian Redl5ca79842010-02-01 20:16:42 +0000917VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000918 VarDecl *First = getFirstDeclaration();
919 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
920 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000921 if ((*I)->isThisDeclarationADefinition() == Definition)
922 return *I;
923 }
924 return 0;
925}
926
927const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000928 redecl_iterator I = redecls_begin(), E = redecls_end();
929 while (I != E && !I->getInit())
930 ++I;
931
932 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000933 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000934 return I->getInit();
935 }
936 return 0;
937}
938
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000939bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000940 if (Decl::isOutOfLine())
941 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000942
943 if (!isStaticDataMember())
944 return false;
945
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000946 // If this static data member was instantiated from a static data member of
947 // a class template, check whether that static data member was defined
948 // out-of-line.
949 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
950 return VD->isOutOfLine();
951
952 return false;
953}
954
Douglas Gregor1d957a32009-10-27 18:42:08 +0000955VarDecl *VarDecl::getOutOfLineDefinition() {
956 if (!isStaticDataMember())
957 return 0;
958
959 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
960 RD != RDEnd; ++RD) {
961 if (RD->getLexicalDeclContext()->isFileContext())
962 return *RD;
963 }
964
965 return 0;
966}
967
Douglas Gregord5058122010-02-11 01:19:42 +0000968void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000969 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
970 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000971 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000972 }
973
974 Init = I;
975}
976
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000977VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000978 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000979 return cast<VarDecl>(MSI->getInstantiatedFrom());
980
981 return 0;
982}
983
Douglas Gregor3c74d412009-10-14 20:14:33 +0000984TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000985 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000986 return MSI->getTemplateSpecializationKind();
987
988 return TSK_Undeclared;
989}
990
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000991MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000992 return getASTContext().getInstantiatedFromStaticDataMember(this);
993}
994
Douglas Gregor3d7e69f2009-10-15 17:21:20 +0000995void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
996 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000997 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +0000998 assert(MSI && "Not an instantiated static data member?");
999 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001000 if (TSK != TSK_ExplicitSpecialization &&
1001 PointOfInstantiation.isValid() &&
1002 MSI->getPointOfInstantiation().isInvalid())
1003 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001004}
1005
Sebastian Redl833ef452010-01-26 22:01:41 +00001006//===----------------------------------------------------------------------===//
1007// ParmVarDecl Implementation
1008//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001009
Sebastian Redl833ef452010-01-26 22:01:41 +00001010ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1011 SourceLocation L, IdentifierInfo *Id,
1012 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001013 StorageClass S, StorageClass SCAsWritten,
1014 Expr *DefArg) {
1015 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1016 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001017}
1018
Sebastian Redl833ef452010-01-26 22:01:41 +00001019Expr *ParmVarDecl::getDefaultArg() {
1020 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1021 assert(!hasUninstantiatedDefaultArg() &&
1022 "Default argument is not yet instantiated!");
1023
1024 Expr *Arg = getInit();
1025 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1026 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001027
Sebastian Redl833ef452010-01-26 22:01:41 +00001028 return Arg;
1029}
1030
1031unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1032 if (const CXXExprWithTemporaries *E =
1033 dyn_cast<CXXExprWithTemporaries>(getInit()))
1034 return E->getNumTemporaries();
1035
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001036 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001037}
1038
Sebastian Redl833ef452010-01-26 22:01:41 +00001039CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1040 assert(getNumDefaultArgTemporaries() &&
1041 "Default arguments does not have any temporaries!");
1042
1043 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1044 return E->getTemporary(i);
1045}
1046
1047SourceRange ParmVarDecl::getDefaultArgRange() const {
1048 if (const Expr *E = getInit())
1049 return E->getSourceRange();
1050
1051 if (hasUninstantiatedDefaultArg())
1052 return getUninstantiatedDefaultArg()->getSourceRange();
1053
1054 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001055}
1056
Nuno Lopes394ec982008-12-17 23:39:55 +00001057//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001058// FunctionDecl Implementation
1059//===----------------------------------------------------------------------===//
1060
John McCalle1f2ec22009-09-11 06:45:03 +00001061void FunctionDecl::getNameForDiagnostic(std::string &S,
1062 const PrintingPolicy &Policy,
1063 bool Qualified) const {
1064 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1065 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1066 if (TemplateArgs)
1067 S += TemplateSpecializationType::PrintTemplateArgumentList(
1068 TemplateArgs->getFlatArgumentList(),
1069 TemplateArgs->flat_size(),
1070 Policy);
1071
1072}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001073
Ted Kremenek186a0742010-04-29 16:49:01 +00001074bool FunctionDecl::isVariadic() const {
1075 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1076 return FT->isVariadic();
1077 return false;
1078}
1079
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001080bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1081 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1082 if (I->Body) {
1083 Definition = *I;
1084 return true;
1085 }
1086 }
1087
1088 return false;
1089}
1090
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001091Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001092 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1093 if (I->Body) {
1094 Definition = *I;
1095 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001096 }
1097 }
1098
1099 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001100}
1101
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001102void FunctionDecl::setBody(Stmt *B) {
1103 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001104 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001105 EndRangeLoc = B->getLocEnd();
1106}
1107
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001108void FunctionDecl::setPure(bool P) {
1109 IsPure = P;
1110 if (P)
1111 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1112 Parent->markedVirtualFunctionPure();
1113}
1114
Douglas Gregor16618f22009-09-12 00:17:51 +00001115bool FunctionDecl::isMain() const {
1116 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001117 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001118 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001119 getIdentifier() && getIdentifier()->isStr("main");
1120}
1121
Douglas Gregor16618f22009-09-12 00:17:51 +00001122bool FunctionDecl::isExternC() const {
1123 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001124 // In C, any non-static, non-overloadable function has external
1125 // linkage.
1126 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001127 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001128
Mike Stump11289f42009-09-09 15:08:12 +00001129 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001130 DC = DC->getParent()) {
1131 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1132 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001133 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001134 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001135
1136 break;
1137 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001138
1139 if (DC->isRecord())
1140 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001141 }
1142
Douglas Gregorbff62032010-10-21 16:57:46 +00001143 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001144}
1145
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001146bool FunctionDecl::isGlobal() const {
1147 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1148 return Method->isStatic();
1149
John McCall8e7d6562010-08-26 03:08:43 +00001150 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001151 return false;
1152
Mike Stump11289f42009-09-09 15:08:12 +00001153 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001154 DC->isNamespace();
1155 DC = DC->getParent()) {
1156 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1157 if (!Namespace->getDeclName())
1158 return false;
1159 break;
1160 }
1161 }
1162
1163 return true;
1164}
1165
Sebastian Redl833ef452010-01-26 22:01:41 +00001166void
1167FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1168 redeclarable_base::setPreviousDeclaration(PrevDecl);
1169
1170 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1171 FunctionTemplateDecl *PrevFunTmpl
1172 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1173 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1174 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1175 }
1176}
1177
1178const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1179 return getFirstDeclaration();
1180}
1181
1182FunctionDecl *FunctionDecl::getCanonicalDecl() {
1183 return getFirstDeclaration();
1184}
1185
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001186/// \brief Returns a value indicating whether this function
1187/// corresponds to a builtin function.
1188///
1189/// The function corresponds to a built-in function if it is
1190/// declared at translation scope or within an extern "C" block and
1191/// its name matches with the name of a builtin. The returned value
1192/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001193/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001194/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001195unsigned FunctionDecl::getBuiltinID() const {
1196 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001197 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1198 return 0;
1199
1200 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1201 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1202 return BuiltinID;
1203
1204 // This function has the name of a known C library
1205 // function. Determine whether it actually refers to the C library
1206 // function or whether it just has the same name.
1207
Douglas Gregora908e7f2009-02-17 03:23:10 +00001208 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001209 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001210 return 0;
1211
Douglas Gregore711f702009-02-14 18:57:46 +00001212 // If this function is at translation-unit scope and we're not in
1213 // C++, it refers to the C library function.
1214 if (!Context.getLangOptions().CPlusPlus &&
1215 getDeclContext()->isTranslationUnit())
1216 return BuiltinID;
1217
1218 // If the function is in an extern "C" linkage specification and is
1219 // not marked "overloadable", it's the real function.
1220 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001221 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001222 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001223 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001224 return BuiltinID;
1225
1226 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001227 return 0;
1228}
1229
1230
Chris Lattner47c0d002009-04-25 06:03:53 +00001231/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001232/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001233/// after it has been created.
1234unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001235 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001236 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001237 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001238 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001239
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001240}
1241
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001242void FunctionDecl::setParams(ASTContext &C,
1243 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001244 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001245 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001246
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001247 // Zero params -> null pointer.
1248 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001249 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001250 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001251 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001252
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001253 // Update source range. The check below allows us to set EndRangeLoc before
1254 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001255 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001256 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001257 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001258}
Chris Lattner41943152007-01-25 04:52:46 +00001259
Chris Lattner58258242008-04-10 02:22:51 +00001260/// getMinRequiredArguments - Returns the minimum number of arguments
1261/// needed to call this function. This may be fewer than the number of
1262/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001263/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001264unsigned FunctionDecl::getMinRequiredArguments() const {
1265 unsigned NumRequiredArgs = getNumParams();
1266 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001267 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001268 --NumRequiredArgs;
1269
1270 return NumRequiredArgs;
1271}
1272
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001273bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001274 // FIXME: This is not enough. Consider:
1275 //
1276 // inline void f();
1277 // void f() { }
1278 //
1279 // f is inlined, but does not have inline specified.
1280 // To fix this we should add an 'inline' flag to FunctionDecl.
1281 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001282 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001283
1284 if (isa<CXXMethodDecl>(this)) {
1285 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1286 return true;
1287 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001288
1289 switch (getTemplateSpecializationKind()) {
1290 case TSK_Undeclared:
1291 case TSK_ExplicitSpecialization:
1292 return false;
1293
1294 case TSK_ImplicitInstantiation:
1295 case TSK_ExplicitInstantiationDeclaration:
1296 case TSK_ExplicitInstantiationDefinition:
1297 // Handle below.
1298 break;
1299 }
1300
1301 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001302 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001303 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001304 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001305
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001306 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001307 return PatternDecl->isInlined();
1308
1309 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001310}
1311
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001312/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001313/// definition will be externally visible.
1314///
1315/// Inline function definitions are always available for inlining optimizations.
1316/// However, depending on the language dialect, declaration specifiers, and
1317/// attributes, the definition of an inline function may or may not be
1318/// "externally" visible to other translation units in the program.
1319///
1320/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001321/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001322/// inline definition becomes externally visible (C99 6.7.4p6).
1323///
1324/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1325/// definition, we use the GNU semantics for inline, which are nearly the
1326/// opposite of C99 semantics. In particular, "inline" by itself will create
1327/// an externally visible symbol, but "extern inline" will not create an
1328/// externally visible symbol.
1329bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1330 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001331 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001332 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001333
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001334 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001335 // GNU inline semantics. Based on a number of examples, we came up with the
1336 // following heuristic: if the "inline" keyword is present on a
1337 // declaration of the function but "extern" is not present on that
1338 // declaration, then the symbol is externally visible. Otherwise, the GNU
1339 // "extern inline" semantics applies and the symbol is not externally
1340 // visible.
1341 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1342 Redecl != RedeclEnd;
1343 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001344 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001345 return true;
1346 }
1347
1348 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001349 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001350 }
1351
1352 // C99 6.7.4p6:
1353 // [...] If all of the file scope declarations for a function in a
1354 // translation unit include the inline function specifier without extern,
1355 // then the definition in that translation unit is an inline definition.
1356 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1357 Redecl != RedeclEnd;
1358 ++Redecl) {
1359 // Only consider file-scope declarations in this test.
1360 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1361 continue;
1362
John McCall8e7d6562010-08-26 03:08:43 +00001363 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001364 return true; // Not an inline definition
1365 }
1366
1367 // C99 6.7.4p6:
1368 // An inline definition does not provide an external definition for the
1369 // function, and does not forbid an external definition in another
1370 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001371 return false;
1372}
1373
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001374/// getOverloadedOperator - Which C++ overloaded operator this
1375/// function represents, if any.
1376OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001377 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1378 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001379 else
1380 return OO_None;
1381}
1382
Alexis Huntc88db062010-01-13 09:01:02 +00001383/// getLiteralIdentifier - The literal suffix identifier this function
1384/// represents, if any.
1385const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1386 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1387 return getDeclName().getCXXLiteralIdentifier();
1388 else
1389 return 0;
1390}
1391
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001392FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1393 if (TemplateOrSpecialization.isNull())
1394 return TK_NonTemplate;
1395 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1396 return TK_FunctionTemplate;
1397 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1398 return TK_MemberSpecialization;
1399 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1400 return TK_FunctionTemplateSpecialization;
1401 if (TemplateOrSpecialization.is
1402 <DependentFunctionTemplateSpecializationInfo*>())
1403 return TK_DependentFunctionTemplateSpecialization;
1404
1405 assert(false && "Did we miss a TemplateOrSpecialization type?");
1406 return TK_NonTemplate;
1407}
1408
Douglas Gregord801b062009-10-07 23:56:10 +00001409FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001410 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001411 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1412
1413 return 0;
1414}
1415
Douglas Gregor06db9f52009-10-12 20:18:28 +00001416MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1417 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1418}
1419
Douglas Gregord801b062009-10-07 23:56:10 +00001420void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001421FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1422 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001423 TemplateSpecializationKind TSK) {
1424 assert(TemplateOrSpecialization.isNull() &&
1425 "Member function is already a specialization");
1426 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001427 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001428 TemplateOrSpecialization = Info;
1429}
1430
Douglas Gregorafca3b42009-10-27 20:53:28 +00001431bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001432 // If the function is invalid, it can't be implicitly instantiated.
1433 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001434 return false;
1435
1436 switch (getTemplateSpecializationKind()) {
1437 case TSK_Undeclared:
1438 case TSK_ExplicitSpecialization:
1439 case TSK_ExplicitInstantiationDefinition:
1440 return false;
1441
1442 case TSK_ImplicitInstantiation:
1443 return true;
1444
1445 case TSK_ExplicitInstantiationDeclaration:
1446 // Handled below.
1447 break;
1448 }
1449
1450 // Find the actual template from which we will instantiate.
1451 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001452 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001453 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001454 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001455
1456 // C++0x [temp.explicit]p9:
1457 // Except for inline functions, other explicit instantiation declarations
1458 // have the effect of suppressing the implicit instantiation of the entity
1459 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001460 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001461 return true;
1462
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001463 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001464}
1465
1466FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1467 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1468 while (Primary->getInstantiatedFromMemberTemplate()) {
1469 // If we have hit a point where the user provided a specialization of
1470 // this template, we're done looking.
1471 if (Primary->isMemberSpecialization())
1472 break;
1473
1474 Primary = Primary->getInstantiatedFromMemberTemplate();
1475 }
1476
1477 return Primary->getTemplatedDecl();
1478 }
1479
1480 return getInstantiatedFromMemberFunction();
1481}
1482
Douglas Gregor70d83e22009-06-29 17:30:29 +00001483FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001484 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001485 = TemplateOrSpecialization
1486 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001487 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001488 }
1489 return 0;
1490}
1491
1492const TemplateArgumentList *
1493FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001494 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001495 = TemplateOrSpecialization
1496 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001497 return Info->TemplateArguments;
1498 }
1499 return 0;
1500}
1501
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001502const TemplateArgumentListInfo *
1503FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1504 if (FunctionTemplateSpecializationInfo *Info
1505 = TemplateOrSpecialization
1506 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1507 return Info->TemplateArgumentsAsWritten;
1508 }
1509 return 0;
1510}
1511
Mike Stump11289f42009-09-09 15:08:12 +00001512void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001513FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1514 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001515 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001516 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001517 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001518 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1519 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001520 assert(TSK != TSK_Undeclared &&
1521 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001522 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001523 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001524 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001525 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1526 TemplateArgs,
1527 TemplateArgsAsWritten,
1528 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001529 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001530
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001531 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001532 // function template specializations.
1533 if (InsertPos)
1534 Template->getSpecializations().InsertNode(Info, InsertPos);
1535 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001536 // Try to insert the new node. If there is an existing node, leave it, the
1537 // set will contain the canonical decls while
1538 // FunctionTemplateDecl::findSpecialization will return
1539 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001540 FunctionTemplateSpecializationInfo *Existing
1541 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001542 (void)Existing;
1543 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1544 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001545 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001546}
1547
John McCallb9c78482010-04-08 09:05:18 +00001548void
1549FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1550 const UnresolvedSetImpl &Templates,
1551 const TemplateArgumentListInfo &TemplateArgs) {
1552 assert(TemplateOrSpecialization.isNull());
1553 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1554 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001555 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001556 void *Buffer = Context.Allocate(Size);
1557 DependentFunctionTemplateSpecializationInfo *Info =
1558 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1559 TemplateArgs);
1560 TemplateOrSpecialization = Info;
1561}
1562
1563DependentFunctionTemplateSpecializationInfo::
1564DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1565 const TemplateArgumentListInfo &TArgs)
1566 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1567
1568 d.NumTemplates = Ts.size();
1569 d.NumArgs = TArgs.size();
1570
1571 FunctionTemplateDecl **TsArray =
1572 const_cast<FunctionTemplateDecl**>(getTemplates());
1573 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1574 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1575
1576 TemplateArgumentLoc *ArgsArray =
1577 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1578 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1579 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1580}
1581
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001582TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001583 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001584 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001585 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001586 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001587 if (FTSInfo)
1588 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001589
Douglas Gregord801b062009-10-07 23:56:10 +00001590 MemberSpecializationInfo *MSInfo
1591 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1592 if (MSInfo)
1593 return MSInfo->getTemplateSpecializationKind();
1594
1595 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001596}
1597
Mike Stump11289f42009-09-09 15:08:12 +00001598void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001599FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1600 SourceLocation PointOfInstantiation) {
1601 if (FunctionTemplateSpecializationInfo *FTSInfo
1602 = TemplateOrSpecialization.dyn_cast<
1603 FunctionTemplateSpecializationInfo*>()) {
1604 FTSInfo->setTemplateSpecializationKind(TSK);
1605 if (TSK != TSK_ExplicitSpecialization &&
1606 PointOfInstantiation.isValid() &&
1607 FTSInfo->getPointOfInstantiation().isInvalid())
1608 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1609 } else if (MemberSpecializationInfo *MSInfo
1610 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1611 MSInfo->setTemplateSpecializationKind(TSK);
1612 if (TSK != TSK_ExplicitSpecialization &&
1613 PointOfInstantiation.isValid() &&
1614 MSInfo->getPointOfInstantiation().isInvalid())
1615 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1616 } else
1617 assert(false && "Function cannot have a template specialization kind");
1618}
1619
1620SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001621 if (FunctionTemplateSpecializationInfo *FTSInfo
1622 = TemplateOrSpecialization.dyn_cast<
1623 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001624 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001625 else if (MemberSpecializationInfo *MSInfo
1626 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001627 return MSInfo->getPointOfInstantiation();
1628
1629 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001630}
1631
Douglas Gregor6411b922009-09-11 20:15:17 +00001632bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001633 if (Decl::isOutOfLine())
1634 return true;
1635
1636 // If this function was instantiated from a member function of a
1637 // class template, check whether that member function was defined out-of-line.
1638 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1639 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001640 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001641 return Definition->isOutOfLine();
1642 }
1643
1644 // If this function was instantiated from a function template,
1645 // check whether that function template was defined out-of-line.
1646 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1647 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001648 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001649 return Definition->isOutOfLine();
1650 }
1651
1652 return false;
1653}
1654
Chris Lattner59a25942008-03-31 00:36:02 +00001655//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001656// FieldDecl Implementation
1657//===----------------------------------------------------------------------===//
1658
1659FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1660 IdentifierInfo *Id, QualType T,
1661 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1662 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1663}
1664
1665bool FieldDecl::isAnonymousStructOrUnion() const {
1666 if (!isImplicit() || getDeclName())
1667 return false;
1668
1669 if (const RecordType *Record = getType()->getAs<RecordType>())
1670 return Record->getDecl()->isAnonymousStructOrUnion();
1671
1672 return false;
1673}
1674
1675//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001676// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001677//===----------------------------------------------------------------------===//
1678
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001679SourceLocation TagDecl::getOuterLocStart() const {
1680 return getTemplateOrInnerLocStart(this);
1681}
1682
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001683SourceRange TagDecl::getSourceRange() const {
1684 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001685 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001686}
1687
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001688TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001689 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001690}
1691
Douglas Gregora72a4e32010-05-19 18:39:18 +00001692void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1693 TypedefDeclOrQualifier = TDD;
1694 if (TypeForDecl)
1695 TypeForDecl->ClearLinkageCache();
1696}
1697
Douglas Gregordee1be82009-01-17 00:42:38 +00001698void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001699 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001700
1701 if (isa<CXXRecordDecl>(this)) {
1702 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1703 struct CXXRecordDecl::DefinitionData *Data =
1704 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001705 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1706 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001707 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001708}
1709
1710void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001711 assert((!isa<CXXRecordDecl>(this) ||
1712 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1713 "definition completed but not started");
1714
Douglas Gregordee1be82009-01-17 00:42:38 +00001715 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001716 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001717
1718 if (ASTMutationListener *L = getASTMutationListener())
1719 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001720}
1721
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001722TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001723 if (isDefinition())
1724 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001725 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1726 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001727
1728 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001729 R != REnd; ++R)
1730 if (R->isDefinition())
1731 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001732
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001733 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001734}
1735
John McCall3e11ebe2010-03-15 10:12:16 +00001736void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1737 SourceRange QualifierRange) {
1738 if (Qualifier) {
1739 // Make sure the extended qualifier info is allocated.
1740 if (!hasExtInfo())
1741 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1742 // Set qualifier info.
1743 getExtInfo()->NNS = Qualifier;
1744 getExtInfo()->NNSRange = QualifierRange;
1745 }
1746 else {
1747 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1748 assert(QualifierRange.isInvalid());
1749 if (hasExtInfo()) {
1750 getASTContext().Deallocate(getExtInfo());
1751 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1752 }
1753 }
1754}
1755
Ted Kremenek21475702008-09-05 17:16:31 +00001756//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001757// EnumDecl Implementation
1758//===----------------------------------------------------------------------===//
1759
1760EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1761 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001762 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1763 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1764 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001765 C.getTypeDeclType(Enum, PrevDecl);
1766 return Enum;
1767}
1768
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001769EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001770 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1771 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001772}
1773
Douglas Gregord5058122010-02-11 01:19:42 +00001774void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001775 QualType NewPromotionType,
1776 unsigned NumPositiveBits,
1777 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001778 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001779 if (!IntegerType)
1780 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001781 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001782 setNumPositiveBits(NumPositiveBits);
1783 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001784 TagDecl::completeDefinition();
1785}
1786
1787//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001788// RecordDecl Implementation
1789//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001790
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001791RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001792 IdentifierInfo *Id, RecordDecl *PrevDecl,
1793 SourceLocation TKL)
1794 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001795 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001796 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001797 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001798 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001799 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001800}
1801
1802RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001803 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001804 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001805
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001806 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001807 C.getTypeDeclType(R, PrevDecl);
1808 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001809}
1810
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001811RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1812 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1813 SourceLocation());
1814}
1815
Douglas Gregordfcad112009-03-25 15:59:44 +00001816bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001817 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001818 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1819}
1820
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001821RecordDecl::field_iterator RecordDecl::field_begin() const {
1822 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1823 LoadFieldsFromExternalStorage();
1824
1825 return field_iterator(decl_iterator(FirstDecl));
1826}
1827
Douglas Gregor91f84212008-12-11 16:49:14 +00001828/// completeDefinition - Notes that the definition of this type is now
1829/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001830void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001831 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001832 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001833}
Steve Naroffcc321422007-03-26 23:09:51 +00001834
John McCall61925b02010-05-21 01:17:40 +00001835ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1836 // Force the decl chain to come into existence properly.
1837 if (!getNextDeclInContext()) getParent()->decls_begin();
1838
1839 assert(isAnonymousStructOrUnion());
1840 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1841 assert(D->getType()->isRecordType());
1842 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1843 return D;
1844}
1845
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001846void RecordDecl::LoadFieldsFromExternalStorage() const {
1847 ExternalASTSource *Source = getASTContext().getExternalSource();
1848 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1849
1850 // Notify that we have a RecordDecl doing some initialization.
1851 ExternalASTSource::Deserializing TheFields(Source);
1852
1853 llvm::SmallVector<Decl*, 64> Decls;
1854 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1855 return;
1856
1857#ifndef NDEBUG
1858 // Check that all decls we got were FieldDecls.
1859 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1860 assert(isa<FieldDecl>(Decls[i]));
1861#endif
1862
1863 LoadedFieldsFromExternalStorage = true;
1864
1865 if (Decls.empty())
1866 return;
1867
1868 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1869}
1870
Steve Naroff415d3d52008-10-08 17:01:13 +00001871//===----------------------------------------------------------------------===//
1872// BlockDecl Implementation
1873//===----------------------------------------------------------------------===//
1874
Douglas Gregord5058122010-02-11 01:19:42 +00001875void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001876 unsigned NParms) {
1877 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001878
Steve Naroffc4b30e52009-03-13 16:56:44 +00001879 // Zero params -> null pointer.
1880 if (NParms) {
1881 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001882 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001883 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1884 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1885 }
1886}
1887
1888unsigned BlockDecl::getNumParams() const {
1889 return NumParams;
1890}
Sebastian Redl833ef452010-01-26 22:01:41 +00001891
1892
1893//===----------------------------------------------------------------------===//
1894// Other Decl Allocation/Deallocation Method Implementations
1895//===----------------------------------------------------------------------===//
1896
1897TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1898 return new (C) TranslationUnitDecl(C);
1899}
1900
1901NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1902 SourceLocation L, IdentifierInfo *Id) {
1903 return new (C) NamespaceDecl(DC, L, Id);
1904}
1905
Sebastian Redl833ef452010-01-26 22:01:41 +00001906ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1907 SourceLocation L, IdentifierInfo *Id, QualType T) {
1908 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1909}
1910
1911FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001912 const DeclarationNameInfo &NameInfo,
1913 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001914 StorageClass S, StorageClass SCAsWritten,
1915 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001916 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001917 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001918 New->HasWrittenPrototype = hasWrittenPrototype;
1919 return New;
1920}
1921
1922BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1923 return new (C) BlockDecl(DC, L);
1924}
1925
1926EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1927 SourceLocation L,
1928 IdentifierInfo *Id, QualType T,
1929 Expr *E, const llvm::APSInt &V) {
1930 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1931}
1932
Douglas Gregorbe996932010-09-01 20:41:53 +00001933SourceRange EnumConstantDecl::getSourceRange() const {
1934 SourceLocation End = getLocation();
1935 if (Init)
1936 End = Init->getLocEnd();
1937 return SourceRange(getLocation(), End);
1938}
1939
Sebastian Redl833ef452010-01-26 22:01:41 +00001940TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1941 SourceLocation L, IdentifierInfo *Id,
1942 TypeSourceInfo *TInfo) {
1943 return new (C) TypedefDecl(DC, L, Id, TInfo);
1944}
1945
Sebastian Redl833ef452010-01-26 22:01:41 +00001946FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1947 SourceLocation L,
1948 StringLiteral *Str) {
1949 return new (C) FileScopeAsmDecl(DC, L, Str);
1950}