blob: 8526eccd6581145da5d9db1ddc3ab7ca95a4be7d [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
John McCallf1bbbb42009-09-04 01:14:41 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000029
Reid Spencer5f016e22007-07-11 17:01:13 +000030using namespace clang;
31
Chris Lattnerd3b90652008-03-15 05:43:15 +000032//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000033// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000034//===----------------------------------------------------------------------===//
35
John McCalle7bc9722010-10-28 04:18:25 +000036static const VisibilityAttr *GetExplicitVisibility(const Decl *D) {
37 // If the decl is redeclarable, make sure we use the explicit
38 // visibility attribute from the most recent declaration.
39 //
40 // Note that this isn't necessary for tags, which can't have their
41 // visibility adjusted.
42 if (isa<VarDecl>(D)) {
43 return cast<VarDecl>(D)->getMostRecentDeclaration()
44 ->getAttr<VisibilityAttr>();
45 } else if (isa<FunctionDecl>(D)) {
46 return cast<FunctionDecl>(D)->getMostRecentDeclaration()
47 ->getAttr<VisibilityAttr>();
48 } else {
49 return D->getAttr<VisibilityAttr>();
50 }
51}
52
John McCall1fb0caa2010-10-22 21:05:15 +000053static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
54 switch (A->getVisibility()) {
55 case VisibilityAttr::Default:
56 return DefaultVisibility;
57 case VisibilityAttr::Hidden:
58 return HiddenVisibility;
59 case VisibilityAttr::Protected:
60 return ProtectedVisibility;
61 }
62 return DefaultVisibility;
63}
64
65typedef std::pair<Linkage,Visibility> LVPair;
66static LVPair merge(LVPair L, LVPair R) {
67 return LVPair(minLinkage(L.first, R.first),
68 minVisibility(L.second, R.second));
69}
70
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000071/// \brief Get the most restrictive linkage for the types in the given
72/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +000073static LVPair
74getLVForTemplateParameterList(const TemplateParameterList *Params) {
75 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000076 for (TemplateParameterList::const_iterator P = Params->begin(),
77 PEnd = Params->end();
78 P != PEnd; ++P) {
79 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
80 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +000081 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000082 continue;
83 }
84
85 if (TemplateTemplateParmDecl *TTP
86 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCall1fb0caa2010-10-22 21:05:15 +000087 LV =
88 merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000089 }
90 }
91
John McCall1fb0caa2010-10-22 21:05:15 +000092 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000093}
94
95/// \brief Get the most restrictive linkage for the types and
96/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +000097static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
98 unsigned NumArgs) {
99 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000100
101 for (unsigned I = 0; I != NumArgs; ++I) {
102 switch (Args[I].getKind()) {
103 case TemplateArgument::Null:
104 case TemplateArgument::Integral:
105 case TemplateArgument::Expression:
106 break;
107
108 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +0000109 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000110 break;
111
112 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +0000113 // The decl can validly be null as the representation of nullptr
114 // arguments, valid only in C++0x.
115 if (Decl *D = Args[I].getAsDecl()) {
116 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
117 LV = merge(LV, ND->getLinkageAndVisibility());
118 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
119 LV = merge(LV, VD->getType()->getLinkageAndVisibility());
120 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000121 break;
122
123 case TemplateArgument::Template:
John McCall1fb0caa2010-10-22 21:05:15 +0000124 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
125 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000126 break;
127
128 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000129 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
130 Args[I].pack_size()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000131 break;
132 }
133 }
134
John McCall1fb0caa2010-10-22 21:05:15 +0000135 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000136}
137
John McCall1fb0caa2010-10-22 21:05:15 +0000138static LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
139 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
140 TArgs.flat_size());
John McCall3cdfc4d2010-08-13 08:35:10 +0000141}
142
John McCall1fb0caa2010-10-22 21:05:15 +0000143static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000144 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000145 "Not a name having namespace scope");
146 ASTContext &Context = D->getASTContext();
147
148 // C++ [basic.link]p3:
149 // A name having namespace scope (3.3.6) has internal linkage if it
150 // is the name of
151 // - an object, reference, function or function template that is
152 // explicitly declared static; or,
153 // (This bullet corresponds to C99 6.2.2p3.)
154 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
155 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000156 if (Var->getStorageClass() == SC_Static)
John McCall1fb0caa2010-10-22 21:05:15 +0000157 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000158
159 // - an object or reference that is explicitly declared const
160 // and neither explicitly declared extern nor previously
161 // declared to have external linkage; or
162 // (there is no equivalent in C99)
163 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000164 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000165 Var->getStorageClass() != SC_Extern &&
166 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000167 bool FoundExtern = false;
168 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
169 PrevVar && !FoundExtern;
170 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000171 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000172 FoundExtern = true;
173
174 if (!FoundExtern)
John McCall1fb0caa2010-10-22 21:05:15 +0000175 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000176 }
177 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000178 // C++ [temp]p4:
179 // A non-member function template can have internal linkage; any
180 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000181 const FunctionDecl *Function = 0;
182 if (const FunctionTemplateDecl *FunTmpl
183 = dyn_cast<FunctionTemplateDecl>(D))
184 Function = FunTmpl->getTemplatedDecl();
185 else
186 Function = cast<FunctionDecl>(D);
187
188 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000189 if (Function->getStorageClass() == SC_Static)
John McCall1fb0caa2010-10-22 21:05:15 +0000190 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000191 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
192 // - a data member of an anonymous union.
193 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCall1fb0caa2010-10-22 21:05:15 +0000194 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000195 }
196
John McCall1fb0caa2010-10-22 21:05:15 +0000197 if (D->isInAnonymousNamespace())
198 return LVPair(UniqueExternalLinkage, DefaultVisibility);
199
John McCalle7bc9722010-10-28 04:18:25 +0000200 const VisibilityAttr *ExplicitVisibility = GetExplicitVisibility(D);
201
John McCall1fb0caa2010-10-22 21:05:15 +0000202 // Set up the defaults.
203
204 // C99 6.2.2p5:
205 // If the declaration of an identifier for an object has file
206 // scope and no storage-class specifier, its linkage is
207 // external.
208 LVPair LV(ExternalLinkage, DefaultVisibility);
209
210 // We ignore -fvisibility on non-definitions and explicit
211 // instantiation declarations.
212 bool ConsiderDashFVisibility = true;
213
Douglas Gregord85b5b92009-11-25 22:24:25 +0000214 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000215
Douglas Gregord85b5b92009-11-25 22:24:25 +0000216 // A name having namespace scope has external linkage if it is the
217 // name of
218 //
219 // - an object or reference, unless it has internal linkage; or
220 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000221 // Modify the variable's LV by the LV of its type unless this is
222 // C or extern "C". This follows from [basic.link]p9:
223 // A type without linkage shall not be used as the type of a
224 // variable or function with external linkage unless
225 // - the entity has C language linkage, or
226 // - the entity is declared within an unnamed namespace, or
227 // - the entity is not used or is defined in the same
228 // translation unit.
229 // and [basic.link]p10:
230 // ...the types specified by all declarations referring to a
231 // given variable or function shall be identical...
232 // C does not have an equivalent rule.
233 //
John McCallac65c622010-10-26 04:59:26 +0000234 // Ignore this if we've got an explicit attribute; the user
235 // probably knows what they're doing.
236 //
John McCall1fb0caa2010-10-22 21:05:15 +0000237 // Note that we don't want to make the variable non-external
238 // because of this, but unique-external linkage suits us.
John McCallac65c622010-10-26 04:59:26 +0000239 if (Context.getLangOptions().CPlusPlus && !Var->isExternC() &&
John McCalle7bc9722010-10-28 04:18:25 +0000240 !ExplicitVisibility) {
John McCall1fb0caa2010-10-22 21:05:15 +0000241 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
242 if (TypeLV.first != ExternalLinkage)
243 return LVPair(UniqueExternalLinkage, DefaultVisibility);
244 LV.second = minVisibility(LV.second, TypeLV.second);
245 }
246
Douglas Gregord85b5b92009-11-25 22:24:25 +0000247 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000248 (Var->getStorageClass() == SC_Extern ||
249 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000250 if (Var->getStorageClass() == SC_PrivateExtern)
251 LV.second = HiddenVisibility;
252
Douglas Gregord85b5b92009-11-25 22:24:25 +0000253 // C99 6.2.2p4:
254 // For an identifier declared with the storage-class specifier
255 // extern in a scope in which a prior declaration of that
256 // identifier is visible, if the prior declaration specifies
257 // internal or external linkage, the linkage of the identifier
258 // at the later declaration is the same as the linkage
259 // specified at the prior declaration. If no prior declaration
260 // is visible, or if the prior declaration specifies no
261 // linkage, then the identifier has external linkage.
262 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000263 LVPair PrevLV = PrevVar->getLinkageAndVisibility();
264 if (PrevLV.first) LV.first = PrevLV.first;
265 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000266 }
267 }
268
Douglas Gregord85b5b92009-11-25 22:24:25 +0000269 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000270 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000271 // In theory, we can modify the function's LV by the LV of its
272 // type unless it has C linkage (see comment above about variables
273 // for justification). In practice, GCC doesn't do this, so it's
274 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000275
Douglas Gregord85b5b92009-11-25 22:24:25 +0000276 // C99 6.2.2p5:
277 // If the declaration of an identifier for a function has no
278 // storage-class specifier, its linkage is determined exactly
279 // as if it were declared with the storage-class specifier
280 // extern.
281 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000282 (Function->getStorageClass() == SC_Extern ||
283 Function->getStorageClass() == SC_PrivateExtern ||
284 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000285 // C99 6.2.2p4:
286 // For an identifier declared with the storage-class specifier
287 // extern in a scope in which a prior declaration of that
288 // identifier is visible, if the prior declaration specifies
289 // internal or external linkage, the linkage of the identifier
290 // at the later declaration is the same as the linkage
291 // specified at the prior declaration. If no prior declaration
292 // is visible, or if the prior declaration specifies no
293 // linkage, then the identifier has external linkage.
294 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000295 LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
296 if (PrevLV.first) LV.first = PrevLV.first;
297 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000298 }
299 }
300
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000301 if (FunctionTemplateSpecializationInfo *SpecInfo
302 = Function->getTemplateSpecializationInfo()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000303 LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000304 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCall1fb0caa2010-10-22 21:05:15 +0000305 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
306
307 if (SpecInfo->getTemplateSpecializationKind()
308 == TSK_ExplicitInstantiationDeclaration)
309 ConsiderDashFVisibility = false;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000310 }
311
John McCall1fb0caa2010-10-22 21:05:15 +0000312 if (ConsiderDashFVisibility)
313 ConsiderDashFVisibility = Function->hasBody();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000314
315 // - a named class (Clause 9), or an unnamed class defined in a
316 // typedef declaration in which the class has the typedef name
317 // for linkage purposes (7.1.3); or
318 // - a named enumeration (7.2), or an unnamed enumeration
319 // defined in a typedef declaration in which the enumeration
320 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000321 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
322 // Unnamed tags have no linkage.
323 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
324 return LVPair(NoLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000325
John McCall1fb0caa2010-10-22 21:05:15 +0000326 // If this is a class template specialization, consider the
327 // linkage of the template and template arguments.
328 if (const ClassTemplateSpecializationDecl *Spec
329 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
330 // From the template. Note below the restrictions on how we
331 // compute template visibility.
332 LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000333
John McCall1fb0caa2010-10-22 21:05:15 +0000334 // The arguments at which the template was instantiated.
335 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
336 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
337
338 if (Spec->getTemplateSpecializationKind()
339 == TSK_ExplicitInstantiationDeclaration)
340 ConsiderDashFVisibility = false;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000341 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000342
John McCallac65c622010-10-26 04:59:26 +0000343 // Consider -fvisibility unless the type has C linkage.
John McCall1fb0caa2010-10-22 21:05:15 +0000344 if (ConsiderDashFVisibility)
John McCallac65c622010-10-26 04:59:26 +0000345 ConsiderDashFVisibility =
346 (Context.getLangOptions().CPlusPlus &&
347 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000348
Douglas Gregord85b5b92009-11-25 22:24:25 +0000349 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000350 } else if (isa<EnumConstantDecl>(D)) {
351 LVPair EnumLV =
352 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
353 if (!isExternalLinkage(EnumLV.first))
354 return LVPair(NoLinkage, DefaultVisibility);
355 LV = merge(LV, EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000356
357 // - a template, unless it is a function template that has
358 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000359 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
360 LV = merge(LV, getLVForTemplateParameterList(
361 Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000362
John McCall1fb0caa2010-10-22 21:05:15 +0000363 // We do not want to consider attributes or global settings when
364 // computing template visibility.
365 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000366
367 // - a namespace (7.3), unless it is declared within an unnamed
368 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000369 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
370 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000371
John McCall1fb0caa2010-10-22 21:05:15 +0000372 // By extension, we assign external linkage to Objective-C
373 // interfaces.
374 } else if (isa<ObjCInterfaceDecl>(D)) {
375 // fallout
376
377 // Everything not covered here has no linkage.
378 } else {
379 return LVPair(NoLinkage, DefaultVisibility);
380 }
381
382 // If we ended up with non-external linkage, visibility should
383 // always be default.
384 if (LV.first != ExternalLinkage)
385 return LVPair(LV.first, DefaultVisibility);
386
387 // If we didn't end up with hidden visibility, consider attributes
388 // and -fvisibility.
389 if (LV.second != HiddenVisibility) {
390 Visibility StandardV;
391
392 // If we have an explicit visibility attribute, merge that in.
John McCalle7bc9722010-10-28 04:18:25 +0000393 if (ExplicitVisibility)
394 StandardV = GetVisibilityFromAttr(ExplicitVisibility);
John McCall1fb0caa2010-10-22 21:05:15 +0000395 else if (ConsiderDashFVisibility)
396 StandardV = Context.getLangOptions().getVisibilityMode();
397 else
398 StandardV = DefaultVisibility; // no-op
399
400 LV.second = minVisibility(LV.second, StandardV);
401 }
402
403 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000404}
405
John McCall1fb0caa2010-10-22 21:05:15 +0000406static LVPair getLVForClassMember(const NamedDecl *D) {
407 // Only certain class members have linkage. Note that fields don't
408 // really have linkage, but it's convenient to say they do for the
409 // purposes of calculating linkage of pointer-to-data-member
410 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000411 if (!(isa<CXXMethodDecl>(D) ||
412 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000413 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000414 (isa<TagDecl>(D) &&
415 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCall1fb0caa2010-10-22 21:05:15 +0000416 return LVPair(NoLinkage, DefaultVisibility);
John McCall3cdfc4d2010-08-13 08:35:10 +0000417
418 // Class members only have linkage if their class has external linkage.
John McCall1fb0caa2010-10-22 21:05:15 +0000419 LVPair ClassLV =
420 cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility();
421 if (!isExternalLinkage(ClassLV.first))
422 return LVPair(NoLinkage, DefaultVisibility);
John McCall3cdfc4d2010-08-13 08:35:10 +0000423
424 // If the class already has unique-external linkage, we can't improve.
John McCall1fb0caa2010-10-22 21:05:15 +0000425 if (ClassLV.first == UniqueExternalLinkage)
426 return LVPair(UniqueExternalLinkage, DefaultVisibility);
John McCall3cdfc4d2010-08-13 08:35:10 +0000427
John McCall1fb0caa2010-10-22 21:05:15 +0000428 // Start with the class's linkage and visibility.
429 LVPair LV = ClassLV;
430
431 // If we have an explicit visibility attribute, merge that in.
John McCalle7bc9722010-10-28 04:18:25 +0000432 const VisibilityAttr *VA = GetExplicitVisibility(D);
John McCall1fb0caa2010-10-22 21:05:15 +0000433 if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
434
John McCall67fa6d52010-10-28 07:07:52 +0000435 // If it's a variable declaration and we don't have an explicit
436 // visibility attribute, apply the LV from its type.
John McCall1fb0caa2010-10-22 21:05:15 +0000437 // See the comment about namespace-scope variable decls above.
John McCall67fa6d52010-10-28 07:07:52 +0000438 if (!VA && isa<VarDecl>(D)) {
439 LVPair TypeLV = cast<VarDecl>(D)->getType()->getLinkageAndVisibility();
John McCall1fb0caa2010-10-22 21:05:15 +0000440 if (TypeLV.first != ExternalLinkage)
441 LV.first = minLinkage(LV.first, UniqueExternalLinkage);
442 LV.second = minVisibility(LV.second, TypeLV.second);
443 }
444
John McCall3cdfc4d2010-08-13 08:35:10 +0000445 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000446 // If this is a method template specialization, use the linkage for
447 // the template parameters and arguments.
448 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000449 = MD->getTemplateSpecializationInfo()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000450 LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
451 LV = merge(LV, getLVForTemplateParameterList(
452 Spec->getTemplate()->getTemplateParameters()));
John McCall3cdfc4d2010-08-13 08:35:10 +0000453 }
454
John McCall1fb0caa2010-10-22 21:05:15 +0000455 // If -fvisibility-inlines-hidden was provided, then inline C++
456 // member functions get "hidden" visibility if they don't have an
457 // explicit visibility attribute.
458 if (!VA && MD->isInlined() && LV.second > HiddenVisibility &&
459 D->getASTContext().getLangOptions().InlineVisibilityHidden)
460 LV.second = HiddenVisibility;
461
John McCall3cdfc4d2010-08-13 08:35:10 +0000462 // Similarly for member class template specializations.
463 } else if (const ClassTemplateSpecializationDecl *Spec
464 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000465 LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
466 LV = merge(LV, getLVForTemplateParameterList(
467 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall3cdfc4d2010-08-13 08:35:10 +0000468 }
469
John McCall1fb0caa2010-10-22 21:05:15 +0000470 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000471}
472
John McCall1fb0caa2010-10-22 21:05:15 +0000473LVPair NamedDecl::getLinkageAndVisibility() const {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000474
475 // Objective-C: treat all Objective-C declarations as having external
476 // linkage.
477 switch (getKind()) {
478 default:
479 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000480 case Decl::TemplateTemplateParm: // count these as external
481 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000482 case Decl::ObjCAtDefsField:
483 case Decl::ObjCCategory:
484 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000485 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000486 case Decl::ObjCForwardProtocol:
487 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000488 case Decl::ObjCMethod:
489 case Decl::ObjCProperty:
490 case Decl::ObjCPropertyImpl:
491 case Decl::ObjCProtocol:
John McCall1fb0caa2010-10-22 21:05:15 +0000492 return LVPair(ExternalLinkage, DefaultVisibility);
Ted Kremenekbecc3082010-04-20 23:15:35 +0000493 }
494
Douglas Gregord85b5b92009-11-25 22:24:25 +0000495 // Handle linkage for namespace-scope names.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000496 if (getDeclContext()->getRedeclContext()->isFileContext())
John McCall1fb0caa2010-10-22 21:05:15 +0000497 return getLVForNamespaceScopeDecl(this);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000498
499 // C++ [basic.link]p5:
500 // In addition, a member function, static data member, a named
501 // class or enumeration of class scope, or an unnamed class or
502 // enumeration defined in a class-scope typedef declaration such
503 // that the class or enumeration has the typedef name for linkage
504 // purposes (7.1.3), has external linkage if the name of the class
505 // has external linkage.
John McCall3cdfc4d2010-08-13 08:35:10 +0000506 if (getDeclContext()->isRecord())
John McCall1fb0caa2010-10-22 21:05:15 +0000507 return getLVForClassMember(this);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000508
509 // C++ [basic.link]p6:
510 // The name of a function declared in block scope and the name of
511 // an object declared by a block scope extern declaration have
512 // linkage. If there is a visible declaration of an entity with
513 // linkage having the same name and type, ignoring entities
514 // declared outside the innermost enclosing namespace scope, the
515 // block scope declaration declares that same entity and receives
516 // the linkage of the previous declaration. If there is more than
517 // one such matching entity, the program is ill-formed. Otherwise,
518 // if no matching entity is found, the block scope entity receives
519 // external linkage.
520 if (getLexicalDeclContext()->isFunctionOrMethod()) {
521 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000522 if (Function->isInAnonymousNamespace())
John McCall1fb0caa2010-10-22 21:05:15 +0000523 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000524
John McCall1fb0caa2010-10-22 21:05:15 +0000525 LVPair LV(ExternalLinkage, DefaultVisibility);
John McCalle7bc9722010-10-28 04:18:25 +0000526 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
John McCall1fb0caa2010-10-22 21:05:15 +0000527 LV.second = GetVisibilityFromAttr(VA);
528
529 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
530 LVPair PrevLV = Prev->getLinkageAndVisibility();
531 if (PrevLV.first) LV.first = PrevLV.first;
532 LV.second = minVisibility(LV.second, PrevLV.second);
533 }
534
535 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000536 }
537
538 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
John McCalld931b082010-08-26 03:08:43 +0000539 if (Var->getStorageClass() == SC_Extern ||
540 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000541 if (Var->isInAnonymousNamespace())
John McCall1fb0caa2010-10-22 21:05:15 +0000542 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000543
John McCall1fb0caa2010-10-22 21:05:15 +0000544 LVPair LV(ExternalLinkage, DefaultVisibility);
545 if (Var->getStorageClass() == SC_PrivateExtern)
546 LV.second = HiddenVisibility;
John McCalle7bc9722010-10-28 04:18:25 +0000547 else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
John McCall1fb0caa2010-10-22 21:05:15 +0000548 LV.second = GetVisibilityFromAttr(VA);
549
550 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
551 LVPair PrevLV = Prev->getLinkageAndVisibility();
552 if (PrevLV.first) LV.first = PrevLV.first;
553 LV.second = minVisibility(LV.second, PrevLV.second);
554 }
555
556 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000557 }
558 }
559
560 // C++ [basic.link]p6:
561 // Names not covered by these rules have no linkage.
John McCall1fb0caa2010-10-22 21:05:15 +0000562 return LVPair(NoLinkage, DefaultVisibility);
563}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000564
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000565std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000566 return getQualifiedNameAsString(getASTContext().getLangOptions());
567}
568
569std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000570 const DeclContext *Ctx = getDeclContext();
571
572 if (Ctx->isFunctionOrMethod())
573 return getNameAsString();
574
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000575 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
576 ContextsTy Contexts;
577
578 // Collect contexts.
579 while (Ctx && isa<NamedDecl>(Ctx)) {
580 Contexts.push_back(Ctx);
581 Ctx = Ctx->getParent();
582 };
583
584 std::string QualName;
585 llvm::raw_string_ostream OS(QualName);
586
587 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
588 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000589 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000590 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000591 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
592 std::string TemplateArgsStr
593 = TemplateSpecializationType::PrintTemplateArgumentList(
594 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000595 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000596 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000597 OS << Spec->getName() << TemplateArgsStr;
598 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000599 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000600 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000601 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000602 OS << ND;
603 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
604 if (!RD->getIdentifier())
605 OS << "<anonymous " << RD->getKindName() << '>';
606 else
607 OS << RD;
608 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000609 const FunctionProtoType *FT = 0;
610 if (FD->hasWrittenPrototype())
611 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
612
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000613 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000614 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000615 unsigned NumParams = FD->getNumParams();
616 for (unsigned i = 0; i < NumParams; ++i) {
617 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000618 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000619 std::string Param;
620 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000621 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000622 }
623
624 if (FT->isVariadic()) {
625 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000626 OS << ", ";
627 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000628 }
629 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000630 OS << ')';
631 } else {
632 OS << cast<NamedDecl>(*I);
633 }
634 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000635 }
636
John McCall8472af42010-03-16 21:48:18 +0000637 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000638 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000639 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000640 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000641
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000642 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000643}
644
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000645bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000646 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
647
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000648 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
649 // We want to keep it, unless it nominates same namespace.
650 if (getKind() == Decl::UsingDirective) {
651 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
652 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000655 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
656 // For function declarations, we keep track of redeclarations.
657 return FD->getPreviousDeclaration() == OldD;
658
Douglas Gregore53060f2009-06-25 22:08:12 +0000659 // For function templates, the underlying function declarations are linked.
660 if (const FunctionTemplateDecl *FunctionTemplate
661 = dyn_cast<FunctionTemplateDecl>(this))
662 if (const FunctionTemplateDecl *OldFunctionTemplate
663 = dyn_cast<FunctionTemplateDecl>(OldD))
664 return FunctionTemplate->getTemplatedDecl()
665 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000666
Steve Naroff0de21fd2009-02-22 19:35:57 +0000667 // For method declarations, we keep track of redeclarations.
668 if (isa<ObjCMethodDecl>(this))
669 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000670
John McCallf36e02d2009-10-09 21:13:30 +0000671 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
672 return true;
673
John McCall9488ea12009-11-17 05:59:44 +0000674 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
675 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
676 cast<UsingShadowDecl>(OldD)->getTargetDecl();
677
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000678 // For non-function declarations, if the declarations are of the
679 // same kind then this must be a redeclaration, or semantic analysis
680 // would not have given us the new declaration.
681 return this->getKind() == OldD->getKind();
682}
683
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000684bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000685 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000686}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000687
Anders Carlssone136e0e2009-06-26 06:29:23 +0000688NamedDecl *NamedDecl::getUnderlyingDecl() {
689 NamedDecl *ND = this;
690 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000691 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000692 ND = UD->getTargetDecl();
693 else if (ObjCCompatibleAliasDecl *AD
694 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
695 return AD->getClassInterface();
696 else
697 return ND;
698 }
699}
700
John McCall161755a2010-04-06 21:38:20 +0000701bool NamedDecl::isCXXInstanceMember() const {
702 assert(isCXXClassMember() &&
703 "checking whether non-member is instance member");
704
705 const NamedDecl *D = this;
706 if (isa<UsingShadowDecl>(D))
707 D = cast<UsingShadowDecl>(D)->getTargetDecl();
708
709 if (isa<FieldDecl>(D))
710 return true;
711 if (isa<CXXMethodDecl>(D))
712 return cast<CXXMethodDecl>(D)->isInstance();
713 if (isa<FunctionTemplateDecl>(D))
714 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
715 ->getTemplatedDecl())->isInstance();
716 return false;
717}
718
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000719//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000720// DeclaratorDecl Implementation
721//===----------------------------------------------------------------------===//
722
Douglas Gregor1693e152010-07-06 18:42:40 +0000723template <typename DeclT>
724static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
725 if (decl->getNumTemplateParameterLists() > 0)
726 return decl->getTemplateParameterList(0)->getTemplateLoc();
727 else
728 return decl->getInnerLocStart();
729}
730
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000731SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000732 TypeSourceInfo *TSI = getTypeSourceInfo();
733 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000734 return SourceLocation();
735}
736
John McCallb6217662010-03-15 10:12:16 +0000737void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
738 SourceRange QualifierRange) {
739 if (Qualifier) {
740 // Make sure the extended decl info is allocated.
741 if (!hasExtInfo()) {
742 // Save (non-extended) type source info pointer.
743 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
744 // Allocate external info struct.
745 DeclInfo = new (getASTContext()) ExtInfo;
746 // Restore savedTInfo into (extended) decl info.
747 getExtInfo()->TInfo = savedTInfo;
748 }
749 // Set qualifier info.
750 getExtInfo()->NNS = Qualifier;
751 getExtInfo()->NNSRange = QualifierRange;
752 }
753 else {
754 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
755 assert(QualifierRange.isInvalid());
756 if (hasExtInfo()) {
757 // Save type source info pointer.
758 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
759 // Deallocate the extended decl info.
760 getASTContext().Deallocate(getExtInfo());
761 // Restore savedTInfo into (non-extended) decl info.
762 DeclInfo = savedTInfo;
763 }
764 }
765}
766
Douglas Gregor1693e152010-07-06 18:42:40 +0000767SourceLocation DeclaratorDecl::getOuterLocStart() const {
768 return getTemplateOrInnerLocStart(this);
769}
770
Abramo Bagnara9b934882010-06-12 08:15:14 +0000771void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000772QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
773 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000774 TemplateParameterList **TPLists) {
775 assert((NumTPLists == 0 || TPLists != 0) &&
776 "Empty array of template parameters with positive size!");
777 assert((NumTPLists == 0 || NNS) &&
778 "Nonempty array of template parameters with no qualifier!");
779
780 // Free previous template parameters (if any).
781 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000782 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000783 TemplParamLists = 0;
784 NumTemplParamLists = 0;
785 }
786 // Set info on matched template parameter lists (if any).
787 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000788 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000789 NumTemplParamLists = NumTPLists;
790 for (unsigned i = NumTPLists; i-- > 0; )
791 TemplParamLists[i] = TPLists[i];
792 }
793}
794
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000795//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000796// VarDecl Implementation
797//===----------------------------------------------------------------------===//
798
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000799const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
800 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000801 case SC_None: break;
802 case SC_Auto: return "auto"; break;
803 case SC_Extern: return "extern"; break;
804 case SC_PrivateExtern: return "__private_extern__"; break;
805 case SC_Register: return "register"; break;
806 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000807 }
808
809 assert(0 && "Invalid storage class");
810 return 0;
811}
812
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000813VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000814 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000815 StorageClass S, StorageClass SCAsWritten) {
816 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000817}
818
Douglas Gregor1693e152010-07-06 18:42:40 +0000819SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000820 SourceLocation Start = getTypeSpecStartLoc();
821 if (Start.isInvalid())
822 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000823 return Start;
824}
825
826SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000827 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000828 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
829 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000830}
831
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000832bool VarDecl::isExternC() const {
833 ASTContext &Context = getASTContext();
834 if (!Context.getLangOptions().CPlusPlus)
835 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +0000836 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000837 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
838
839 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
840 DC = DC->getParent()) {
841 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
842 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000843 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000844
845 break;
846 }
847
848 if (DC->isFunctionOrMethod())
849 return false;
850 }
851
852 return false;
853}
854
855VarDecl *VarDecl::getCanonicalDecl() {
856 return getFirstDeclaration();
857}
858
Sebastian Redle9d12b62010-01-31 22:27:38 +0000859VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
860 // C++ [basic.def]p2:
861 // A declaration is a definition unless [...] it contains the 'extern'
862 // specifier or a linkage-specification and neither an initializer [...],
863 // it declares a static data member in a class declaration [...].
864 // C++ [temp.expl.spec]p15:
865 // An explicit specialization of a static data member of a template is a
866 // definition if the declaration includes an initializer; otherwise, it is
867 // a declaration.
868 if (isStaticDataMember()) {
869 if (isOutOfLine() && (hasInit() ||
870 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
871 return Definition;
872 else
873 return DeclarationOnly;
874 }
875 // C99 6.7p5:
876 // A definition of an identifier is a declaration for that identifier that
877 // [...] causes storage to be reserved for that object.
878 // Note: that applies for all non-file-scope objects.
879 // C99 6.9.2p1:
880 // If the declaration of an identifier for an object has file scope and an
881 // initializer, the declaration is an external definition for the identifier
882 if (hasInit())
883 return Definition;
884 // AST for 'extern "C" int foo;' is annotated with 'extern'.
885 if (hasExternalStorage())
886 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000887
John McCalld931b082010-08-26 03:08:43 +0000888 if (getStorageClassAsWritten() == SC_Extern ||
889 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000890 for (const VarDecl *PrevVar = getPreviousDeclaration();
891 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
892 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
893 return DeclarationOnly;
894 }
895 }
Sebastian Redle9d12b62010-01-31 22:27:38 +0000896 // C99 6.9.2p2:
897 // A declaration of an object that has file scope without an initializer,
898 // and without a storage class specifier or the scs 'static', constitutes
899 // a tentative definition.
900 // No such thing in C++.
901 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
902 return TentativeDefinition;
903
904 // What's left is (in C, block-scope) declarations without initializers or
905 // external storage. These are definitions.
906 return Definition;
907}
908
Sebastian Redle9d12b62010-01-31 22:27:38 +0000909VarDecl *VarDecl::getActingDefinition() {
910 DefinitionKind Kind = isThisDeclarationADefinition();
911 if (Kind != TentativeDefinition)
912 return 0;
913
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +0000914 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000915 VarDecl *First = getFirstDeclaration();
916 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
917 I != E; ++I) {
918 Kind = (*I)->isThisDeclarationADefinition();
919 if (Kind == Definition)
920 return 0;
921 else if (Kind == TentativeDefinition)
922 LastTentative = *I;
923 }
924 return LastTentative;
925}
926
927bool VarDecl::isTentativeDefinitionNow() const {
928 DefinitionKind Kind = isThisDeclarationADefinition();
929 if (Kind != TentativeDefinition)
930 return false;
931
932 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
933 if ((*I)->isThisDeclarationADefinition() == Definition)
934 return false;
935 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000936 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000937}
938
Sebastian Redl31310a22010-02-01 20:16:42 +0000939VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +0000940 VarDecl *First = getFirstDeclaration();
941 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
942 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000943 if ((*I)->isThisDeclarationADefinition() == Definition)
944 return *I;
945 }
946 return 0;
947}
948
949const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000950 redecl_iterator I = redecls_begin(), E = redecls_end();
951 while (I != E && !I->getInit())
952 ++I;
953
954 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000955 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000956 return I->getInit();
957 }
958 return 0;
959}
960
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000961bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000962 if (Decl::isOutOfLine())
963 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +0000964
965 if (!isStaticDataMember())
966 return false;
967
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000968 // If this static data member was instantiated from a static data member of
969 // a class template, check whether that static data member was defined
970 // out-of-line.
971 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
972 return VD->isOutOfLine();
973
974 return false;
975}
976
Douglas Gregor0d035142009-10-27 18:42:08 +0000977VarDecl *VarDecl::getOutOfLineDefinition() {
978 if (!isStaticDataMember())
979 return 0;
980
981 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
982 RD != RDEnd; ++RD) {
983 if (RD->getLexicalDeclContext()->isFileContext())
984 return *RD;
985 }
986
987 return 0;
988}
989
Douglas Gregor838db382010-02-11 01:19:42 +0000990void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000991 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
992 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +0000993 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000994 }
995
996 Init = I;
997}
998
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000999VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001000 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001001 return cast<VarDecl>(MSI->getInstantiatedFrom());
1002
1003 return 0;
1004}
1005
Douglas Gregor663b5a02009-10-14 20:14:33 +00001006TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001007 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001008 return MSI->getTemplateSpecializationKind();
1009
1010 return TSK_Undeclared;
1011}
1012
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001013MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001014 return getASTContext().getInstantiatedFromStaticDataMember(this);
1015}
1016
Douglas Gregor0a897e32009-10-15 17:21:20 +00001017void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1018 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001019 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001020 assert(MSI && "Not an instantiated static data member?");
1021 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001022 if (TSK != TSK_ExplicitSpecialization &&
1023 PointOfInstantiation.isValid() &&
1024 MSI->getPointOfInstantiation().isInvalid())
1025 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001026}
1027
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001028//===----------------------------------------------------------------------===//
1029// ParmVarDecl Implementation
1030//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001031
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001032ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1033 SourceLocation L, IdentifierInfo *Id,
1034 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001035 StorageClass S, StorageClass SCAsWritten,
1036 Expr *DefArg) {
1037 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1038 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001039}
1040
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001041Expr *ParmVarDecl::getDefaultArg() {
1042 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1043 assert(!hasUninstantiatedDefaultArg() &&
1044 "Default argument is not yet instantiated!");
1045
1046 Expr *Arg = getInit();
1047 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1048 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001049
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001050 return Arg;
1051}
1052
1053unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1054 if (const CXXExprWithTemporaries *E =
1055 dyn_cast<CXXExprWithTemporaries>(getInit()))
1056 return E->getNumTemporaries();
1057
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001058 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001059}
1060
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001061CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1062 assert(getNumDefaultArgTemporaries() &&
1063 "Default arguments does not have any temporaries!");
1064
1065 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1066 return E->getTemporary(i);
1067}
1068
1069SourceRange ParmVarDecl::getDefaultArgRange() const {
1070 if (const Expr *E = getInit())
1071 return E->getSourceRange();
1072
1073 if (hasUninstantiatedDefaultArg())
1074 return getUninstantiatedDefaultArg()->getSourceRange();
1075
1076 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001077}
1078
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001079//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001080// FunctionDecl Implementation
1081//===----------------------------------------------------------------------===//
1082
John McCall136a6982009-09-11 06:45:03 +00001083void FunctionDecl::getNameForDiagnostic(std::string &S,
1084 const PrintingPolicy &Policy,
1085 bool Qualified) const {
1086 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1087 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1088 if (TemplateArgs)
1089 S += TemplateSpecializationType::PrintTemplateArgumentList(
1090 TemplateArgs->getFlatArgumentList(),
1091 TemplateArgs->flat_size(),
1092 Policy);
1093
1094}
Ted Kremenek27f8a282008-05-20 00:43:19 +00001095
Ted Kremenek9498d382010-04-29 16:49:01 +00001096bool FunctionDecl::isVariadic() const {
1097 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1098 return FT->isVariadic();
1099 return false;
1100}
1101
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001102bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1103 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1104 if (I->Body) {
1105 Definition = *I;
1106 return true;
1107 }
1108 }
1109
1110 return false;
1111}
1112
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001113Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001114 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1115 if (I->Body) {
1116 Definition = *I;
1117 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001118 }
1119 }
1120
1121 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001122}
1123
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001124void FunctionDecl::setBody(Stmt *B) {
1125 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +00001126 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001127 EndRangeLoc = B->getLocEnd();
1128}
1129
Douglas Gregor21386642010-09-28 21:55:22 +00001130void FunctionDecl::setPure(bool P) {
1131 IsPure = P;
1132 if (P)
1133 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1134 Parent->markedVirtualFunctionPure();
1135}
1136
Douglas Gregor48a83b52009-09-12 00:17:51 +00001137bool FunctionDecl::isMain() const {
1138 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001139 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001140 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001141 getIdentifier() && getIdentifier()->isStr("main");
1142}
1143
Douglas Gregor48a83b52009-09-12 00:17:51 +00001144bool FunctionDecl::isExternC() const {
1145 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001146 // In C, any non-static, non-overloadable function has external
1147 // linkage.
1148 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001149 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001150
Mike Stump1eb44332009-09-09 15:08:12 +00001151 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001152 DC = DC->getParent()) {
1153 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1154 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001155 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001156 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001157
1158 break;
1159 }
Douglas Gregor45975532010-08-17 16:09:23 +00001160
1161 if (DC->isRecord())
1162 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001163 }
1164
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001165 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001166}
1167
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001168bool FunctionDecl::isGlobal() const {
1169 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1170 return Method->isStatic();
1171
John McCalld931b082010-08-26 03:08:43 +00001172 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001173 return false;
1174
Mike Stump1eb44332009-09-09 15:08:12 +00001175 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001176 DC->isNamespace();
1177 DC = DC->getParent()) {
1178 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1179 if (!Namespace->getDeclName())
1180 return false;
1181 break;
1182 }
1183 }
1184
1185 return true;
1186}
1187
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001188void
1189FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1190 redeclarable_base::setPreviousDeclaration(PrevDecl);
1191
1192 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1193 FunctionTemplateDecl *PrevFunTmpl
1194 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1195 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1196 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1197 }
1198}
1199
1200const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1201 return getFirstDeclaration();
1202}
1203
1204FunctionDecl *FunctionDecl::getCanonicalDecl() {
1205 return getFirstDeclaration();
1206}
1207
Douglas Gregor3e41d602009-02-13 23:20:09 +00001208/// \brief Returns a value indicating whether this function
1209/// corresponds to a builtin function.
1210///
1211/// The function corresponds to a built-in function if it is
1212/// declared at translation scope or within an extern "C" block and
1213/// its name matches with the name of a builtin. The returned value
1214/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001215/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001216/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001217unsigned FunctionDecl::getBuiltinID() const {
1218 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001219 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1220 return 0;
1221
1222 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1223 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1224 return BuiltinID;
1225
1226 // This function has the name of a known C library
1227 // function. Determine whether it actually refers to the C library
1228 // function or whether it just has the same name.
1229
Douglas Gregor9add3172009-02-17 03:23:10 +00001230 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001231 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001232 return 0;
1233
Douglas Gregor3c385e52009-02-14 18:57:46 +00001234 // If this function is at translation-unit scope and we're not in
1235 // C++, it refers to the C library function.
1236 if (!Context.getLangOptions().CPlusPlus &&
1237 getDeclContext()->isTranslationUnit())
1238 return BuiltinID;
1239
1240 // If the function is in an extern "C" linkage specification and is
1241 // not marked "overloadable", it's the real function.
1242 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001243 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001244 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001245 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001246 return BuiltinID;
1247
1248 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001249 return 0;
1250}
1251
1252
Chris Lattner1ad9b282009-04-25 06:03:53 +00001253/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001254/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001255/// after it has been created.
1256unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001257 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001258 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001259 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001260 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001261
Reid Spencer5f016e22007-07-11 17:01:13 +00001262}
1263
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001264void FunctionDecl::setParams(ASTContext &C,
1265 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001267 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001268
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 // Zero params -> null pointer.
1270 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001271 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001272 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001274
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001275 // Update source range. The check below allows us to set EndRangeLoc before
1276 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001277 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001278 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 }
1280}
1281
Chris Lattner8123a952008-04-10 02:22:51 +00001282/// getMinRequiredArguments - Returns the minimum number of arguments
1283/// needed to call this function. This may be fewer than the number of
1284/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001285/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001286unsigned FunctionDecl::getMinRequiredArguments() const {
1287 unsigned NumRequiredArgs = getNumParams();
1288 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001289 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001290 --NumRequiredArgs;
1291
1292 return NumRequiredArgs;
1293}
1294
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001295bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001296 // FIXME: This is not enough. Consider:
1297 //
1298 // inline void f();
1299 // void f() { }
1300 //
1301 // f is inlined, but does not have inline specified.
1302 // To fix this we should add an 'inline' flag to FunctionDecl.
1303 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001304 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001305
1306 if (isa<CXXMethodDecl>(this)) {
1307 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1308 return true;
1309 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001310
1311 switch (getTemplateSpecializationKind()) {
1312 case TSK_Undeclared:
1313 case TSK_ExplicitSpecialization:
1314 return false;
1315
1316 case TSK_ImplicitInstantiation:
1317 case TSK_ExplicitInstantiationDeclaration:
1318 case TSK_ExplicitInstantiationDefinition:
1319 // Handle below.
1320 break;
1321 }
1322
1323 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001324 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001325 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001326 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001327
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001328 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001329 return PatternDecl->isInlined();
1330
1331 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001332}
1333
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001334/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001335/// definition will be externally visible.
1336///
1337/// Inline function definitions are always available for inlining optimizations.
1338/// However, depending on the language dialect, declaration specifiers, and
1339/// attributes, the definition of an inline function may or may not be
1340/// "externally" visible to other translation units in the program.
1341///
1342/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001343/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001344/// inline definition becomes externally visible (C99 6.7.4p6).
1345///
1346/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1347/// definition, we use the GNU semantics for inline, which are nearly the
1348/// opposite of C99 semantics. In particular, "inline" by itself will create
1349/// an externally visible symbol, but "extern inline" will not create an
1350/// externally visible symbol.
1351bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1352 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001353 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001354 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001355
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001356 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001357 // GNU inline semantics. Based on a number of examples, we came up with the
1358 // following heuristic: if the "inline" keyword is present on a
1359 // declaration of the function but "extern" is not present on that
1360 // declaration, then the symbol is externally visible. Otherwise, the GNU
1361 // "extern inline" semantics applies and the symbol is not externally
1362 // visible.
1363 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1364 Redecl != RedeclEnd;
1365 ++Redecl) {
John McCalld931b082010-08-26 03:08:43 +00001366 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001367 return true;
1368 }
1369
1370 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001371 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001372 }
1373
1374 // C99 6.7.4p6:
1375 // [...] If all of the file scope declarations for a function in a
1376 // translation unit include the inline function specifier without extern,
1377 // then the definition in that translation unit is an inline definition.
1378 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1379 Redecl != RedeclEnd;
1380 ++Redecl) {
1381 // Only consider file-scope declarations in this test.
1382 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1383 continue;
1384
John McCalld931b082010-08-26 03:08:43 +00001385 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001386 return true; // Not an inline definition
1387 }
1388
1389 // C99 6.7.4p6:
1390 // An inline definition does not provide an external definition for the
1391 // function, and does not forbid an external definition in another
1392 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001393 return false;
1394}
1395
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001396/// getOverloadedOperator - Which C++ overloaded operator this
1397/// function represents, if any.
1398OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001399 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1400 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001401 else
1402 return OO_None;
1403}
1404
Sean Hunta6c058d2010-01-13 09:01:02 +00001405/// getLiteralIdentifier - The literal suffix identifier this function
1406/// represents, if any.
1407const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1408 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1409 return getDeclName().getCXXLiteralIdentifier();
1410 else
1411 return 0;
1412}
1413
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001414FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1415 if (TemplateOrSpecialization.isNull())
1416 return TK_NonTemplate;
1417 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1418 return TK_FunctionTemplate;
1419 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1420 return TK_MemberSpecialization;
1421 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1422 return TK_FunctionTemplateSpecialization;
1423 if (TemplateOrSpecialization.is
1424 <DependentFunctionTemplateSpecializationInfo*>())
1425 return TK_DependentFunctionTemplateSpecialization;
1426
1427 assert(false && "Did we miss a TemplateOrSpecialization type?");
1428 return TK_NonTemplate;
1429}
1430
Douglas Gregor2db32322009-10-07 23:56:10 +00001431FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001432 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001433 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1434
1435 return 0;
1436}
1437
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001438MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1439 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1440}
1441
Douglas Gregor2db32322009-10-07 23:56:10 +00001442void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001443FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1444 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001445 TemplateSpecializationKind TSK) {
1446 assert(TemplateOrSpecialization.isNull() &&
1447 "Member function is already a specialization");
1448 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001449 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001450 TemplateOrSpecialization = Info;
1451}
1452
Douglas Gregor3b846b62009-10-27 20:53:28 +00001453bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001454 // If the function is invalid, it can't be implicitly instantiated.
1455 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001456 return false;
1457
1458 switch (getTemplateSpecializationKind()) {
1459 case TSK_Undeclared:
1460 case TSK_ExplicitSpecialization:
1461 case TSK_ExplicitInstantiationDefinition:
1462 return false;
1463
1464 case TSK_ImplicitInstantiation:
1465 return true;
1466
1467 case TSK_ExplicitInstantiationDeclaration:
1468 // Handled below.
1469 break;
1470 }
1471
1472 // Find the actual template from which we will instantiate.
1473 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001474 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001475 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001476 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001477
1478 // C++0x [temp.explicit]p9:
1479 // Except for inline functions, other explicit instantiation declarations
1480 // have the effect of suppressing the implicit instantiation of the entity
1481 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001482 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001483 return true;
1484
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001485 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001486}
1487
1488FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1489 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1490 while (Primary->getInstantiatedFromMemberTemplate()) {
1491 // If we have hit a point where the user provided a specialization of
1492 // this template, we're done looking.
1493 if (Primary->isMemberSpecialization())
1494 break;
1495
1496 Primary = Primary->getInstantiatedFromMemberTemplate();
1497 }
1498
1499 return Primary->getTemplatedDecl();
1500 }
1501
1502 return getInstantiatedFromMemberFunction();
1503}
1504
Douglas Gregor16e8be22009-06-29 17:30:29 +00001505FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001506 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001507 = TemplateOrSpecialization
1508 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001509 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001510 }
1511 return 0;
1512}
1513
1514const TemplateArgumentList *
1515FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001516 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001517 = TemplateOrSpecialization
1518 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001519 return Info->TemplateArguments;
1520 }
1521 return 0;
1522}
1523
Abramo Bagnarae03db982010-05-20 15:32:11 +00001524const TemplateArgumentListInfo *
1525FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1526 if (FunctionTemplateSpecializationInfo *Info
1527 = TemplateOrSpecialization
1528 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1529 return Info->TemplateArgumentsAsWritten;
1530 }
1531 return 0;
1532}
1533
Mike Stump1eb44332009-09-09 15:08:12 +00001534void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001535FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1536 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001537 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001538 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001539 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001540 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1541 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001542 assert(TSK != TSK_Undeclared &&
1543 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001544 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001545 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001546 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001547 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1548 TemplateArgs,
1549 TemplateArgsAsWritten,
1550 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001551 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001552
Douglas Gregor127102b2009-06-29 20:59:39 +00001553 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001554 // function template specializations.
1555 if (InsertPos)
1556 Template->getSpecializations().InsertNode(Info, InsertPos);
1557 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001558 // Try to insert the new node. If there is an existing node, leave it, the
1559 // set will contain the canonical decls while
1560 // FunctionTemplateDecl::findSpecialization will return
1561 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001562 FunctionTemplateSpecializationInfo *Existing
1563 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001564 (void)Existing;
1565 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1566 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001567 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001568}
1569
John McCallaf2094e2010-04-08 09:05:18 +00001570void
1571FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1572 const UnresolvedSetImpl &Templates,
1573 const TemplateArgumentListInfo &TemplateArgs) {
1574 assert(TemplateOrSpecialization.isNull());
1575 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1576 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001577 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001578 void *Buffer = Context.Allocate(Size);
1579 DependentFunctionTemplateSpecializationInfo *Info =
1580 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1581 TemplateArgs);
1582 TemplateOrSpecialization = Info;
1583}
1584
1585DependentFunctionTemplateSpecializationInfo::
1586DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1587 const TemplateArgumentListInfo &TArgs)
1588 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1589
1590 d.NumTemplates = Ts.size();
1591 d.NumArgs = TArgs.size();
1592
1593 FunctionTemplateDecl **TsArray =
1594 const_cast<FunctionTemplateDecl**>(getTemplates());
1595 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1596 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1597
1598 TemplateArgumentLoc *ArgsArray =
1599 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1600 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1601 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1602}
1603
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001604TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001605 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001606 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001607 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001608 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001609 if (FTSInfo)
1610 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001611
Douglas Gregor2db32322009-10-07 23:56:10 +00001612 MemberSpecializationInfo *MSInfo
1613 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1614 if (MSInfo)
1615 return MSInfo->getTemplateSpecializationKind();
1616
1617 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001618}
1619
Mike Stump1eb44332009-09-09 15:08:12 +00001620void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001621FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1622 SourceLocation PointOfInstantiation) {
1623 if (FunctionTemplateSpecializationInfo *FTSInfo
1624 = TemplateOrSpecialization.dyn_cast<
1625 FunctionTemplateSpecializationInfo*>()) {
1626 FTSInfo->setTemplateSpecializationKind(TSK);
1627 if (TSK != TSK_ExplicitSpecialization &&
1628 PointOfInstantiation.isValid() &&
1629 FTSInfo->getPointOfInstantiation().isInvalid())
1630 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1631 } else if (MemberSpecializationInfo *MSInfo
1632 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1633 MSInfo->setTemplateSpecializationKind(TSK);
1634 if (TSK != TSK_ExplicitSpecialization &&
1635 PointOfInstantiation.isValid() &&
1636 MSInfo->getPointOfInstantiation().isInvalid())
1637 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1638 } else
1639 assert(false && "Function cannot have a template specialization kind");
1640}
1641
1642SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001643 if (FunctionTemplateSpecializationInfo *FTSInfo
1644 = TemplateOrSpecialization.dyn_cast<
1645 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001646 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001647 else if (MemberSpecializationInfo *MSInfo
1648 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001649 return MSInfo->getPointOfInstantiation();
1650
1651 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001652}
1653
Douglas Gregor9f185072009-09-11 20:15:17 +00001654bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001655 if (Decl::isOutOfLine())
1656 return true;
1657
1658 // If this function was instantiated from a member function of a
1659 // class template, check whether that member function was defined out-of-line.
1660 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1661 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001662 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001663 return Definition->isOutOfLine();
1664 }
1665
1666 // If this function was instantiated from a function template,
1667 // check whether that function template was defined out-of-line.
1668 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1669 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001670 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001671 return Definition->isOutOfLine();
1672 }
1673
1674 return false;
1675}
1676
Chris Lattner8a934232008-03-31 00:36:02 +00001677//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001678// FieldDecl Implementation
1679//===----------------------------------------------------------------------===//
1680
1681FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1682 IdentifierInfo *Id, QualType T,
1683 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1684 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1685}
1686
1687bool FieldDecl::isAnonymousStructOrUnion() const {
1688 if (!isImplicit() || getDeclName())
1689 return false;
1690
1691 if (const RecordType *Record = getType()->getAs<RecordType>())
1692 return Record->getDecl()->isAnonymousStructOrUnion();
1693
1694 return false;
1695}
1696
1697//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001698// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001699//===----------------------------------------------------------------------===//
1700
Douglas Gregor1693e152010-07-06 18:42:40 +00001701SourceLocation TagDecl::getOuterLocStart() const {
1702 return getTemplateOrInnerLocStart(this);
1703}
1704
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001705SourceRange TagDecl::getSourceRange() const {
1706 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001707 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001708}
1709
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001710TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001711 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001712}
1713
Douglas Gregor60e70642010-05-19 18:39:18 +00001714void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1715 TypedefDeclOrQualifier = TDD;
1716 if (TypeForDecl)
1717 TypeForDecl->ClearLinkageCache();
1718}
1719
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001720void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001721 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001722
1723 if (isa<CXXRecordDecl>(this)) {
1724 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1725 struct CXXRecordDecl::DefinitionData *Data =
1726 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001727 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1728 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001729 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001730}
1731
1732void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001733 assert((!isa<CXXRecordDecl>(this) ||
1734 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1735 "definition completed but not started");
1736
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001737 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001738 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00001739
1740 if (ASTMutationListener *L = getASTMutationListener())
1741 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001742}
1743
Douglas Gregor952b0172010-02-11 01:04:33 +00001744TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001745 if (isDefinition())
1746 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00001747 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1748 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001749
1750 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001751 R != REnd; ++R)
1752 if (R->isDefinition())
1753 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001755 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001756}
1757
John McCallb6217662010-03-15 10:12:16 +00001758void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1759 SourceRange QualifierRange) {
1760 if (Qualifier) {
1761 // Make sure the extended qualifier info is allocated.
1762 if (!hasExtInfo())
1763 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1764 // Set qualifier info.
1765 getExtInfo()->NNS = Qualifier;
1766 getExtInfo()->NNSRange = QualifierRange;
1767 }
1768 else {
1769 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1770 assert(QualifierRange.isInvalid());
1771 if (hasExtInfo()) {
1772 getASTContext().Deallocate(getExtInfo());
1773 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1774 }
1775 }
1776}
1777
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001778//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001779// EnumDecl Implementation
1780//===----------------------------------------------------------------------===//
1781
1782EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1783 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001784 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1785 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1786 IsScoped, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001787 C.getTypeDeclType(Enum, PrevDecl);
1788 return Enum;
1789}
1790
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001791EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001792 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1793 false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001794}
1795
Douglas Gregor838db382010-02-11 01:19:42 +00001796void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001797 QualType NewPromotionType,
1798 unsigned NumPositiveBits,
1799 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001800 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001801 if (!IntegerType)
1802 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001803 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001804 setNumPositiveBits(NumPositiveBits);
1805 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001806 TagDecl::completeDefinition();
1807}
1808
1809//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001810// RecordDecl Implementation
1811//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001812
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001813RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001814 IdentifierInfo *Id, RecordDecl *PrevDecl,
1815 SourceLocation TKL)
1816 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001817 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001818 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001819 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001820 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001821 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001822}
1823
1824RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001825 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001826 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001827
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001828 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001829 C.getTypeDeclType(R, PrevDecl);
1830 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001831}
1832
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001833RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1834 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1835 SourceLocation());
1836}
1837
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001838bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001839 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001840 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1841}
1842
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001843RecordDecl::field_iterator RecordDecl::field_begin() const {
1844 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1845 LoadFieldsFromExternalStorage();
1846
1847 return field_iterator(decl_iterator(FirstDecl));
1848}
1849
Douglas Gregor44b43212008-12-11 16:49:14 +00001850/// completeDefinition - Notes that the definition of this type is now
1851/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001852void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001854 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001855}
1856
John McCallbc365c52010-05-21 01:17:40 +00001857ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1858 // Force the decl chain to come into existence properly.
1859 if (!getNextDeclInContext()) getParent()->decls_begin();
1860
1861 assert(isAnonymousStructOrUnion());
1862 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1863 assert(D->getType()->isRecordType());
1864 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1865 return D;
1866}
1867
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001868void RecordDecl::LoadFieldsFromExternalStorage() const {
1869 ExternalASTSource *Source = getASTContext().getExternalSource();
1870 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1871
1872 // Notify that we have a RecordDecl doing some initialization.
1873 ExternalASTSource::Deserializing TheFields(Source);
1874
1875 llvm::SmallVector<Decl*, 64> Decls;
1876 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1877 return;
1878
1879#ifndef NDEBUG
1880 // Check that all decls we got were FieldDecls.
1881 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1882 assert(isa<FieldDecl>(Decls[i]));
1883#endif
1884
1885 LoadedFieldsFromExternalStorage = true;
1886
1887 if (Decls.empty())
1888 return;
1889
1890 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1891}
1892
Steve Naroff56ee6892008-10-08 17:01:13 +00001893//===----------------------------------------------------------------------===//
1894// BlockDecl Implementation
1895//===----------------------------------------------------------------------===//
1896
Douglas Gregor838db382010-02-11 01:19:42 +00001897void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001898 unsigned NParms) {
1899 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Steve Naroffe78b8092009-03-13 16:56:44 +00001901 // Zero params -> null pointer.
1902 if (NParms) {
1903 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001904 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001905 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1906 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1907 }
1908}
1909
1910unsigned BlockDecl::getNumParams() const {
1911 return NumParams;
1912}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001913
1914
1915//===----------------------------------------------------------------------===//
1916// Other Decl Allocation/Deallocation Method Implementations
1917//===----------------------------------------------------------------------===//
1918
1919TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1920 return new (C) TranslationUnitDecl(C);
1921}
1922
1923NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1924 SourceLocation L, IdentifierInfo *Id) {
1925 return new (C) NamespaceDecl(DC, L, Id);
1926}
1927
Douglas Gregor06c91932010-10-27 19:49:05 +00001928NamespaceDecl *NamespaceDecl::getNextNamespace() {
1929 return dyn_cast_or_null<NamespaceDecl>(
1930 NextNamespace.get(getASTContext().getExternalSource()));
1931}
1932
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001933ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1934 SourceLocation L, IdentifierInfo *Id, QualType T) {
1935 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1936}
1937
1938FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00001939 const DeclarationNameInfo &NameInfo,
1940 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001941 StorageClass S, StorageClass SCAsWritten,
1942 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001943 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001944 S, SCAsWritten, isInline);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001945 New->HasWrittenPrototype = hasWrittenPrototype;
1946 return New;
1947}
1948
1949BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1950 return new (C) BlockDecl(DC, L);
1951}
1952
1953EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1954 SourceLocation L,
1955 IdentifierInfo *Id, QualType T,
1956 Expr *E, const llvm::APSInt &V) {
1957 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1958}
1959
Douglas Gregor8e7139c2010-09-01 20:41:53 +00001960SourceRange EnumConstantDecl::getSourceRange() const {
1961 SourceLocation End = getLocation();
1962 if (Init)
1963 End = Init->getLocEnd();
1964 return SourceRange(getLocation(), End);
1965}
1966
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001967TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1968 SourceLocation L, IdentifierInfo *Id,
1969 TypeSourceInfo *TInfo) {
1970 return new (C) TypedefDecl(DC, L, Id, TInfo);
1971}
1972
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001973FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1974 SourceLocation L,
1975 StringLiteral *Str) {
1976 return new (C) FileScopeAsmDecl(DC, L, Str);
1977}