blob: fc5b57f1485b9850e327263dbff5959917e16f6a [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"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000024#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000025#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000026#include "clang/Basic/Specifiers.h"
John McCallf1bbbb42009-09-04 01:14:41 +000027#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029using namespace clang;
30
Chris Lattnerd3b90652008-03-15 05:43:15 +000031//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000032// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000033//===----------------------------------------------------------------------===//
34
John McCall1fb0caa2010-10-22 21:05:15 +000035static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
36 switch (A->getVisibility()) {
37 case VisibilityAttr::Default:
38 return DefaultVisibility;
39 case VisibilityAttr::Hidden:
40 return HiddenVisibility;
41 case VisibilityAttr::Protected:
42 return ProtectedVisibility;
43 }
44 return DefaultVisibility;
45}
46
47typedef std::pair<Linkage,Visibility> LVPair;
48static LVPair merge(LVPair L, LVPair R) {
49 return LVPair(minLinkage(L.first, R.first),
50 minVisibility(L.second, R.second));
51}
52
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000053/// \brief Get the most restrictive linkage for the types in the given
54/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +000055static LVPair
56getLVForTemplateParameterList(const TemplateParameterList *Params) {
57 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000058 for (TemplateParameterList::const_iterator P = Params->begin(),
59 PEnd = Params->end();
60 P != PEnd; ++P) {
61 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
62 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +000063 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000064 continue;
65 }
66
67 if (TemplateTemplateParmDecl *TTP
68 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCall1fb0caa2010-10-22 21:05:15 +000069 LV =
70 merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000071 }
72 }
73
John McCall1fb0caa2010-10-22 21:05:15 +000074 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000075}
76
77/// \brief Get the most restrictive linkage for the types and
78/// declarations in the given template argument list.
John McCall1fb0caa2010-10-22 21:05:15 +000079static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
80 unsigned NumArgs) {
81 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000082
83 for (unsigned I = 0; I != NumArgs; ++I) {
84 switch (Args[I].getKind()) {
85 case TemplateArgument::Null:
86 case TemplateArgument::Integral:
87 case TemplateArgument::Expression:
88 break;
89
90 case TemplateArgument::Type:
John McCall1fb0caa2010-10-22 21:05:15 +000091 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +000092 break;
93
94 case TemplateArgument::Declaration:
John McCall1fb0caa2010-10-22 21:05:15 +000095 // The decl can validly be null as the representation of nullptr
96 // arguments, valid only in C++0x.
97 if (Decl *D = Args[I].getAsDecl()) {
98 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
99 LV = merge(LV, ND->getLinkageAndVisibility());
100 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
101 LV = merge(LV, VD->getType()->getLinkageAndVisibility());
102 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000103 break;
104
105 case TemplateArgument::Template:
John McCall1fb0caa2010-10-22 21:05:15 +0000106 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
107 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000108 break;
109
110 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000111 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
112 Args[I].pack_size()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000113 break;
114 }
115 }
116
John McCall1fb0caa2010-10-22 21:05:15 +0000117 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000118}
119
John McCall1fb0caa2010-10-22 21:05:15 +0000120static LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
121 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
122 TArgs.flat_size());
John McCall3cdfc4d2010-08-13 08:35:10 +0000123}
124
John McCall1fb0caa2010-10-22 21:05:15 +0000125static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000126 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-11-25 22:24:25 +0000127 "Not a name having namespace scope");
128 ASTContext &Context = D->getASTContext();
129
130 // C++ [basic.link]p3:
131 // A name having namespace scope (3.3.6) has internal linkage if it
132 // is the name of
133 // - an object, reference, function or function template that is
134 // explicitly declared static; or,
135 // (This bullet corresponds to C99 6.2.2p3.)
136 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
137 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000138 if (Var->getStorageClass() == SC_Static)
John McCall1fb0caa2010-10-22 21:05:15 +0000139 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000140
141 // - an object or reference that is explicitly declared const
142 // and neither explicitly declared extern nor previously
143 // declared to have external linkage; or
144 // (there is no equivalent in C99)
145 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmane9d65542009-11-26 03:04:01 +0000146 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000147 Var->getStorageClass() != SC_Extern &&
148 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000149 bool FoundExtern = false;
150 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
151 PrevVar && !FoundExtern;
152 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000153 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000154 FoundExtern = true;
155
156 if (!FoundExtern)
John McCall1fb0caa2010-10-22 21:05:15 +0000157 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000158 }
159 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000160 // C++ [temp]p4:
161 // A non-member function template can have internal linkage; any
162 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000163 const FunctionDecl *Function = 0;
164 if (const FunctionTemplateDecl *FunTmpl
165 = dyn_cast<FunctionTemplateDecl>(D))
166 Function = FunTmpl->getTemplatedDecl();
167 else
168 Function = cast<FunctionDecl>(D);
169
170 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000171 if (Function->getStorageClass() == SC_Static)
John McCall1fb0caa2010-10-22 21:05:15 +0000172 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000173 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
174 // - a data member of an anonymous union.
175 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCall1fb0caa2010-10-22 21:05:15 +0000176 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000177 }
178
John McCall1fb0caa2010-10-22 21:05:15 +0000179 if (D->isInAnonymousNamespace())
180 return LVPair(UniqueExternalLinkage, DefaultVisibility);
181
182 // Set up the defaults.
183
184 // C99 6.2.2p5:
185 // If the declaration of an identifier for an object has file
186 // scope and no storage-class specifier, its linkage is
187 // external.
188 LVPair LV(ExternalLinkage, DefaultVisibility);
189
190 // We ignore -fvisibility on non-definitions and explicit
191 // instantiation declarations.
192 bool ConsiderDashFVisibility = true;
193
Douglas Gregord85b5b92009-11-25 22:24:25 +0000194 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000195
Douglas Gregord85b5b92009-11-25 22:24:25 +0000196 // A name having namespace scope has external linkage if it is the
197 // name of
198 //
199 // - an object or reference, unless it has internal linkage; or
200 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000201 // Modify the variable's LV by the LV of its type unless this is
202 // C or extern "C". This follows from [basic.link]p9:
203 // A type without linkage shall not be used as the type of a
204 // variable or function with external linkage unless
205 // - the entity has C language linkage, or
206 // - the entity is declared within an unnamed namespace, or
207 // - the entity is not used or is defined in the same
208 // translation unit.
209 // and [basic.link]p10:
210 // ...the types specified by all declarations referring to a
211 // given variable or function shall be identical...
212 // C does not have an equivalent rule.
213 //
214 // Note that we don't want to make the variable non-external
215 // because of this, but unique-external linkage suits us.
216 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
217 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
218 if (TypeLV.first != ExternalLinkage)
219 return LVPair(UniqueExternalLinkage, DefaultVisibility);
220 LV.second = minVisibility(LV.second, TypeLV.second);
221 }
222
Douglas Gregord85b5b92009-11-25 22:24:25 +0000223 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000224 (Var->getStorageClass() == SC_Extern ||
225 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000226 if (Var->getStorageClass() == SC_PrivateExtern)
227 LV.second = HiddenVisibility;
228
Douglas Gregord85b5b92009-11-25 22:24:25 +0000229 // C99 6.2.2p4:
230 // For an identifier declared with the storage-class specifier
231 // extern in a scope in which a prior declaration of that
232 // identifier is visible, if the prior declaration specifies
233 // internal or external linkage, the linkage of the identifier
234 // at the later declaration is the same as the linkage
235 // specified at the prior declaration. If no prior declaration
236 // is visible, or if the prior declaration specifies no
237 // linkage, then the identifier has external linkage.
238 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000239 LVPair PrevLV = PrevVar->getLinkageAndVisibility();
240 if (PrevLV.first) LV.first = PrevLV.first;
241 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000242 }
243 }
244
Douglas Gregord85b5b92009-11-25 22:24:25 +0000245 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000246 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
247 // Modify the function's LV by the LV of its type unless this is
248 // C or extern "C". See the comment above about variables.
249 if (Context.getLangOptions().CPlusPlus && !Function->isExternC()) {
250 LVPair TypeLV = Function->getType()->getLinkageAndVisibility();
251 if (TypeLV.first != ExternalLinkage)
252 return LVPair(UniqueExternalLinkage, DefaultVisibility);
253 LV.second = minVisibility(LV.second, TypeLV.second);
254 }
255
Douglas Gregord85b5b92009-11-25 22:24:25 +0000256 // C99 6.2.2p5:
257 // If the declaration of an identifier for a function has no
258 // storage-class specifier, its linkage is determined exactly
259 // as if it were declared with the storage-class specifier
260 // extern.
261 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000262 (Function->getStorageClass() == SC_Extern ||
263 Function->getStorageClass() == SC_PrivateExtern ||
264 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000265 // C99 6.2.2p4:
266 // For an identifier declared with the storage-class specifier
267 // extern in a scope in which a prior declaration of that
268 // identifier is visible, if the prior declaration specifies
269 // internal or external linkage, the linkage of the identifier
270 // at the later declaration is the same as the linkage
271 // specified at the prior declaration. If no prior declaration
272 // is visible, or if the prior declaration specifies no
273 // linkage, then the identifier has external linkage.
274 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000275 LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
276 if (PrevLV.first) LV.first = PrevLV.first;
277 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000278 }
279 }
280
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000281 if (FunctionTemplateSpecializationInfo *SpecInfo
282 = Function->getTemplateSpecializationInfo()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000283 LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000284 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCall1fb0caa2010-10-22 21:05:15 +0000285 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
286
287 if (SpecInfo->getTemplateSpecializationKind()
288 == TSK_ExplicitInstantiationDeclaration)
289 ConsiderDashFVisibility = false;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000290 }
291
John McCall1fb0caa2010-10-22 21:05:15 +0000292 if (ConsiderDashFVisibility)
293 ConsiderDashFVisibility = Function->hasBody();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000294
295 // - a named class (Clause 9), or an unnamed class defined in a
296 // typedef declaration in which the class has the typedef name
297 // for linkage purposes (7.1.3); or
298 // - a named enumeration (7.2), or an unnamed enumeration
299 // defined in a typedef declaration in which the enumeration
300 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000301 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
302 // Unnamed tags have no linkage.
303 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
304 return LVPair(NoLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000305
John McCall1fb0caa2010-10-22 21:05:15 +0000306 // If this is a class template specialization, consider the
307 // linkage of the template and template arguments.
308 if (const ClassTemplateSpecializationDecl *Spec
309 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
310 // From the template. Note below the restrictions on how we
311 // compute template visibility.
312 LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000313
John McCall1fb0caa2010-10-22 21:05:15 +0000314 // The arguments at which the template was instantiated.
315 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
316 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
317
318 if (Spec->getTemplateSpecializationKind()
319 == TSK_ExplicitInstantiationDeclaration)
320 ConsiderDashFVisibility = false;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000321 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000322
John McCall1fb0caa2010-10-22 21:05:15 +0000323 if (ConsiderDashFVisibility)
324 ConsiderDashFVisibility = Tag->isDefinition();
325
Douglas Gregord85b5b92009-11-25 22:24:25 +0000326 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000327 } else if (isa<EnumConstantDecl>(D)) {
328 LVPair EnumLV =
329 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
330 if (!isExternalLinkage(EnumLV.first))
331 return LVPair(NoLinkage, DefaultVisibility);
332 LV = merge(LV, EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000333
334 // - a template, unless it is a function template that has
335 // internal linkage (Clause 14);
John McCall1fb0caa2010-10-22 21:05:15 +0000336 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
337 LV = merge(LV, getLVForTemplateParameterList(
338 Template->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000339
John McCall1fb0caa2010-10-22 21:05:15 +0000340 // We do not want to consider attributes or global settings when
341 // computing template visibility.
342 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000343
344 // - a namespace (7.3), unless it is declared within an unnamed
345 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000346 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
347 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000348
John McCall1fb0caa2010-10-22 21:05:15 +0000349 // By extension, we assign external linkage to Objective-C
350 // interfaces.
351 } else if (isa<ObjCInterfaceDecl>(D)) {
352 // fallout
353
354 // Everything not covered here has no linkage.
355 } else {
356 return LVPair(NoLinkage, DefaultVisibility);
357 }
358
359 // If we ended up with non-external linkage, visibility should
360 // always be default.
361 if (LV.first != ExternalLinkage)
362 return LVPair(LV.first, DefaultVisibility);
363
364 // If we didn't end up with hidden visibility, consider attributes
365 // and -fvisibility.
366 if (LV.second != HiddenVisibility) {
367 Visibility StandardV;
368
369 // If we have an explicit visibility attribute, merge that in.
370 const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
371 if (VA)
372 StandardV = GetVisibilityFromAttr(VA);
373 else if (ConsiderDashFVisibility)
374 StandardV = Context.getLangOptions().getVisibilityMode();
375 else
376 StandardV = DefaultVisibility; // no-op
377
378 LV.second = minVisibility(LV.second, StandardV);
379 }
380
381 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000382}
383
John McCall1fb0caa2010-10-22 21:05:15 +0000384static LVPair getLVForClassMember(const NamedDecl *D) {
385 // Only certain class members have linkage. Note that fields don't
386 // really have linkage, but it's convenient to say they do for the
387 // purposes of calculating linkage of pointer-to-data-member
388 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000389 if (!(isa<CXXMethodDecl>(D) ||
390 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000391 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000392 (isa<TagDecl>(D) &&
393 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCall1fb0caa2010-10-22 21:05:15 +0000394 return LVPair(NoLinkage, DefaultVisibility);
John McCall3cdfc4d2010-08-13 08:35:10 +0000395
396 // Class members only have linkage if their class has external linkage.
John McCall1fb0caa2010-10-22 21:05:15 +0000397 LVPair ClassLV =
398 cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility();
399 if (!isExternalLinkage(ClassLV.first))
400 return LVPair(NoLinkage, DefaultVisibility);
John McCall3cdfc4d2010-08-13 08:35:10 +0000401
402 // If the class already has unique-external linkage, we can't improve.
John McCall1fb0caa2010-10-22 21:05:15 +0000403 if (ClassLV.first == UniqueExternalLinkage)
404 return LVPair(UniqueExternalLinkage, DefaultVisibility);
John McCall3cdfc4d2010-08-13 08:35:10 +0000405
John McCall1fb0caa2010-10-22 21:05:15 +0000406 // Start with the class's linkage and visibility.
407 LVPair LV = ClassLV;
408
409 // If we have an explicit visibility attribute, merge that in.
410 const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
411 if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
412
413 // If it's a value declaration, apply the LV from its type.
414 // See the comment about namespace-scope variable decls above.
415 if (isa<ValueDecl>(D)) {
416 LVPair TypeLV = cast<ValueDecl>(D)->getType()->getLinkageAndVisibility();
417 if (TypeLV.first != ExternalLinkage)
418 LV.first = minLinkage(LV.first, UniqueExternalLinkage);
419 LV.second = minVisibility(LV.second, TypeLV.second);
420 }
421
John McCall3cdfc4d2010-08-13 08:35:10 +0000422 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000423 // If this is a method template specialization, use the linkage for
424 // the template parameters and arguments.
425 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000426 = MD->getTemplateSpecializationInfo()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000427 LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
428 LV = merge(LV, getLVForTemplateParameterList(
429 Spec->getTemplate()->getTemplateParameters()));
John McCall3cdfc4d2010-08-13 08:35:10 +0000430 }
431
John McCall1fb0caa2010-10-22 21:05:15 +0000432 // If -fvisibility-inlines-hidden was provided, then inline C++
433 // member functions get "hidden" visibility if they don't have an
434 // explicit visibility attribute.
435 if (!VA && MD->isInlined() && LV.second > HiddenVisibility &&
436 D->getASTContext().getLangOptions().InlineVisibilityHidden)
437 LV.second = HiddenVisibility;
438
John McCall3cdfc4d2010-08-13 08:35:10 +0000439 // Similarly for member class template specializations.
440 } else if (const ClassTemplateSpecializationDecl *Spec
441 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000442 LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
443 LV = merge(LV, getLVForTemplateParameterList(
444 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall3cdfc4d2010-08-13 08:35:10 +0000445 }
446
John McCall1fb0caa2010-10-22 21:05:15 +0000447 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000448}
449
John McCall1fb0caa2010-10-22 21:05:15 +0000450LVPair NamedDecl::getLinkageAndVisibility() const {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000451
452 // Objective-C: treat all Objective-C declarations as having external
453 // linkage.
454 switch (getKind()) {
455 default:
456 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000457 case Decl::TemplateTemplateParm: // count these as external
458 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000459 case Decl::ObjCAtDefsField:
460 case Decl::ObjCCategory:
461 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000462 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000463 case Decl::ObjCForwardProtocol:
464 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000465 case Decl::ObjCMethod:
466 case Decl::ObjCProperty:
467 case Decl::ObjCPropertyImpl:
468 case Decl::ObjCProtocol:
John McCall1fb0caa2010-10-22 21:05:15 +0000469 return LVPair(ExternalLinkage, DefaultVisibility);
Ted Kremenekbecc3082010-04-20 23:15:35 +0000470 }
471
Douglas Gregord85b5b92009-11-25 22:24:25 +0000472 // Handle linkage for namespace-scope names.
Sebastian Redl7a126a42010-08-31 00:36:30 +0000473 if (getDeclContext()->getRedeclContext()->isFileContext())
John McCall1fb0caa2010-10-22 21:05:15 +0000474 return getLVForNamespaceScopeDecl(this);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000475
476 // C++ [basic.link]p5:
477 // In addition, a member function, static data member, a named
478 // class or enumeration of class scope, or an unnamed class or
479 // enumeration defined in a class-scope typedef declaration such
480 // that the class or enumeration has the typedef name for linkage
481 // purposes (7.1.3), has external linkage if the name of the class
482 // has external linkage.
John McCall3cdfc4d2010-08-13 08:35:10 +0000483 if (getDeclContext()->isRecord())
John McCall1fb0caa2010-10-22 21:05:15 +0000484 return getLVForClassMember(this);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000485
486 // C++ [basic.link]p6:
487 // The name of a function declared in block scope and the name of
488 // an object declared by a block scope extern declaration have
489 // linkage. If there is a visible declaration of an entity with
490 // linkage having the same name and type, ignoring entities
491 // declared outside the innermost enclosing namespace scope, the
492 // block scope declaration declares that same entity and receives
493 // the linkage of the previous declaration. If there is more than
494 // one such matching entity, the program is ill-formed. Otherwise,
495 // if no matching entity is found, the block scope entity receives
496 // external linkage.
497 if (getLexicalDeclContext()->isFunctionOrMethod()) {
498 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000499 if (Function->isInAnonymousNamespace())
John McCall1fb0caa2010-10-22 21:05:15 +0000500 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000501
John McCall1fb0caa2010-10-22 21:05:15 +0000502 LVPair LV(ExternalLinkage, DefaultVisibility);
503 if (const VisibilityAttr *VA = Function->getAttr<VisibilityAttr>())
504 LV.second = GetVisibilityFromAttr(VA);
505
506 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
507 LVPair PrevLV = Prev->getLinkageAndVisibility();
508 if (PrevLV.first) LV.first = PrevLV.first;
509 LV.second = minVisibility(LV.second, PrevLV.second);
510 }
511
512 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000513 }
514
515 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
John McCalld931b082010-08-26 03:08:43 +0000516 if (Var->getStorageClass() == SC_Extern ||
517 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000518 if (Var->isInAnonymousNamespace())
John McCall1fb0caa2010-10-22 21:05:15 +0000519 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000520
John McCall1fb0caa2010-10-22 21:05:15 +0000521 LVPair LV(ExternalLinkage, DefaultVisibility);
522 if (Var->getStorageClass() == SC_PrivateExtern)
523 LV.second = HiddenVisibility;
524 else if (const VisibilityAttr *VA = Var->getAttr<VisibilityAttr>())
525 LV.second = GetVisibilityFromAttr(VA);
526
527 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
528 LVPair PrevLV = Prev->getLinkageAndVisibility();
529 if (PrevLV.first) LV.first = PrevLV.first;
530 LV.second = minVisibility(LV.second, PrevLV.second);
531 }
532
533 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000534 }
535 }
536
537 // C++ [basic.link]p6:
538 // Names not covered by these rules have no linkage.
John McCall1fb0caa2010-10-22 21:05:15 +0000539 return LVPair(NoLinkage, DefaultVisibility);
540}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000541
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000542std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000543 return getQualifiedNameAsString(getASTContext().getLangOptions());
544}
545
546std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000547 const DeclContext *Ctx = getDeclContext();
548
549 if (Ctx->isFunctionOrMethod())
550 return getNameAsString();
551
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000552 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
553 ContextsTy Contexts;
554
555 // Collect contexts.
556 while (Ctx && isa<NamedDecl>(Ctx)) {
557 Contexts.push_back(Ctx);
558 Ctx = Ctx->getParent();
559 };
560
561 std::string QualName;
562 llvm::raw_string_ostream OS(QualName);
563
564 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
565 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000566 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000567 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000568 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
569 std::string TemplateArgsStr
570 = TemplateSpecializationType::PrintTemplateArgumentList(
571 TemplateArgs.getFlatArgumentList(),
Douglas Gregord249e1d1f2009-05-29 20:38:28 +0000572 TemplateArgs.flat_size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000573 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000574 OS << Spec->getName() << TemplateArgsStr;
575 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000576 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000577 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000578 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000579 OS << ND;
580 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
581 if (!RD->getIdentifier())
582 OS << "<anonymous " << RD->getKindName() << '>';
583 else
584 OS << RD;
585 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000586 const FunctionProtoType *FT = 0;
587 if (FD->hasWrittenPrototype())
588 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
589
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000590 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000591 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000592 unsigned NumParams = FD->getNumParams();
593 for (unsigned i = 0; i < NumParams; ++i) {
594 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000595 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000596 std::string Param;
597 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000598 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000599 }
600
601 if (FT->isVariadic()) {
602 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000603 OS << ", ";
604 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000605 }
606 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000607 OS << ')';
608 } else {
609 OS << cast<NamedDecl>(*I);
610 }
611 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000612 }
613
John McCall8472af42010-03-16 21:48:18 +0000614 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000615 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000616 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000617 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000618
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000619 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000620}
621
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000622bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000623 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
624
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000625 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
626 // We want to keep it, unless it nominates same namespace.
627 if (getKind() == Decl::UsingDirective) {
628 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
629 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000632 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
633 // For function declarations, we keep track of redeclarations.
634 return FD->getPreviousDeclaration() == OldD;
635
Douglas Gregore53060f2009-06-25 22:08:12 +0000636 // For function templates, the underlying function declarations are linked.
637 if (const FunctionTemplateDecl *FunctionTemplate
638 = dyn_cast<FunctionTemplateDecl>(this))
639 if (const FunctionTemplateDecl *OldFunctionTemplate
640 = dyn_cast<FunctionTemplateDecl>(OldD))
641 return FunctionTemplate->getTemplatedDecl()
642 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000643
Steve Naroff0de21fd2009-02-22 19:35:57 +0000644 // For method declarations, we keep track of redeclarations.
645 if (isa<ObjCMethodDecl>(this))
646 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000647
John McCallf36e02d2009-10-09 21:13:30 +0000648 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
649 return true;
650
John McCall9488ea12009-11-17 05:59:44 +0000651 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
652 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
653 cast<UsingShadowDecl>(OldD)->getTargetDecl();
654
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000655 // For non-function declarations, if the declarations are of the
656 // same kind then this must be a redeclaration, or semantic analysis
657 // would not have given us the new declaration.
658 return this->getKind() == OldD->getKind();
659}
660
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000661bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000662 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000663}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000664
Anders Carlssone136e0e2009-06-26 06:29:23 +0000665NamedDecl *NamedDecl::getUnderlyingDecl() {
666 NamedDecl *ND = this;
667 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000668 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000669 ND = UD->getTargetDecl();
670 else if (ObjCCompatibleAliasDecl *AD
671 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
672 return AD->getClassInterface();
673 else
674 return ND;
675 }
676}
677
John McCall161755a2010-04-06 21:38:20 +0000678bool NamedDecl::isCXXInstanceMember() const {
679 assert(isCXXClassMember() &&
680 "checking whether non-member is instance member");
681
682 const NamedDecl *D = this;
683 if (isa<UsingShadowDecl>(D))
684 D = cast<UsingShadowDecl>(D)->getTargetDecl();
685
686 if (isa<FieldDecl>(D))
687 return true;
688 if (isa<CXXMethodDecl>(D))
689 return cast<CXXMethodDecl>(D)->isInstance();
690 if (isa<FunctionTemplateDecl>(D))
691 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
692 ->getTemplatedDecl())->isInstance();
693 return false;
694}
695
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000696//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000697// DeclaratorDecl Implementation
698//===----------------------------------------------------------------------===//
699
Douglas Gregor1693e152010-07-06 18:42:40 +0000700template <typename DeclT>
701static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
702 if (decl->getNumTemplateParameterLists() > 0)
703 return decl->getTemplateParameterList(0)->getTemplateLoc();
704 else
705 return decl->getInnerLocStart();
706}
707
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000708SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000709 TypeSourceInfo *TSI = getTypeSourceInfo();
710 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000711 return SourceLocation();
712}
713
John McCallb6217662010-03-15 10:12:16 +0000714void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
715 SourceRange QualifierRange) {
716 if (Qualifier) {
717 // Make sure the extended decl info is allocated.
718 if (!hasExtInfo()) {
719 // Save (non-extended) type source info pointer.
720 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
721 // Allocate external info struct.
722 DeclInfo = new (getASTContext()) ExtInfo;
723 // Restore savedTInfo into (extended) decl info.
724 getExtInfo()->TInfo = savedTInfo;
725 }
726 // Set qualifier info.
727 getExtInfo()->NNS = Qualifier;
728 getExtInfo()->NNSRange = QualifierRange;
729 }
730 else {
731 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
732 assert(QualifierRange.isInvalid());
733 if (hasExtInfo()) {
734 // Save type source info pointer.
735 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
736 // Deallocate the extended decl info.
737 getASTContext().Deallocate(getExtInfo());
738 // Restore savedTInfo into (non-extended) decl info.
739 DeclInfo = savedTInfo;
740 }
741 }
742}
743
Douglas Gregor1693e152010-07-06 18:42:40 +0000744SourceLocation DeclaratorDecl::getOuterLocStart() const {
745 return getTemplateOrInnerLocStart(this);
746}
747
Abramo Bagnara9b934882010-06-12 08:15:14 +0000748void
Douglas Gregorc722ea42010-06-15 17:44:38 +0000749QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
750 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +0000751 TemplateParameterList **TPLists) {
752 assert((NumTPLists == 0 || TPLists != 0) &&
753 "Empty array of template parameters with positive size!");
754 assert((NumTPLists == 0 || NNS) &&
755 "Nonempty array of template parameters with no qualifier!");
756
757 // Free previous template parameters (if any).
758 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000759 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +0000760 TemplParamLists = 0;
761 NumTemplParamLists = 0;
762 }
763 // Set info on matched template parameter lists (if any).
764 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +0000765 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +0000766 NumTemplParamLists = NumTPLists;
767 for (unsigned i = NumTPLists; i-- > 0; )
768 TemplParamLists[i] = TPLists[i];
769 }
770}
771
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000772//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000773// VarDecl Implementation
774//===----------------------------------------------------------------------===//
775
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000776const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
777 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +0000778 case SC_None: break;
779 case SC_Auto: return "auto"; break;
780 case SC_Extern: return "extern"; break;
781 case SC_PrivateExtern: return "__private_extern__"; break;
782 case SC_Register: return "register"; break;
783 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000784 }
785
786 assert(0 && "Invalid storage class");
787 return 0;
788}
789
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000790VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCalla93c9342009-12-07 02:54:59 +0000791 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +0000792 StorageClass S, StorageClass SCAsWritten) {
793 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +0000794}
795
Douglas Gregor1693e152010-07-06 18:42:40 +0000796SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000797 SourceLocation Start = getTypeSpecStartLoc();
798 if (Start.isInvalid())
799 Start = getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +0000800 return Start;
801}
802
803SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000804 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +0000805 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
806 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +0000807}
808
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000809bool VarDecl::isExternC() const {
810 ASTContext &Context = getASTContext();
811 if (!Context.getLangOptions().CPlusPlus)
812 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +0000813 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000814 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
815
816 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
817 DC = DC->getParent()) {
818 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
819 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +0000820 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000821
822 break;
823 }
824
825 if (DC->isFunctionOrMethod())
826 return false;
827 }
828
829 return false;
830}
831
832VarDecl *VarDecl::getCanonicalDecl() {
833 return getFirstDeclaration();
834}
835
Sebastian Redle9d12b62010-01-31 22:27:38 +0000836VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
837 // C++ [basic.def]p2:
838 // A declaration is a definition unless [...] it contains the 'extern'
839 // specifier or a linkage-specification and neither an initializer [...],
840 // it declares a static data member in a class declaration [...].
841 // C++ [temp.expl.spec]p15:
842 // An explicit specialization of a static data member of a template is a
843 // definition if the declaration includes an initializer; otherwise, it is
844 // a declaration.
845 if (isStaticDataMember()) {
846 if (isOutOfLine() && (hasInit() ||
847 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
848 return Definition;
849 else
850 return DeclarationOnly;
851 }
852 // C99 6.7p5:
853 // A definition of an identifier is a declaration for that identifier that
854 // [...] causes storage to be reserved for that object.
855 // Note: that applies for all non-file-scope objects.
856 // C99 6.9.2p1:
857 // If the declaration of an identifier for an object has file scope and an
858 // initializer, the declaration is an external definition for the identifier
859 if (hasInit())
860 return Definition;
861 // AST for 'extern "C" int foo;' is annotated with 'extern'.
862 if (hasExternalStorage())
863 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000864
John McCalld931b082010-08-26 03:08:43 +0000865 if (getStorageClassAsWritten() == SC_Extern ||
866 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +0000867 for (const VarDecl *PrevVar = getPreviousDeclaration();
868 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
869 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
870 return DeclarationOnly;
871 }
872 }
Sebastian Redle9d12b62010-01-31 22:27:38 +0000873 // C99 6.9.2p2:
874 // A declaration of an object that has file scope without an initializer,
875 // and without a storage class specifier or the scs 'static', constitutes
876 // a tentative definition.
877 // No such thing in C++.
878 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
879 return TentativeDefinition;
880
881 // What's left is (in C, block-scope) declarations without initializers or
882 // external storage. These are definitions.
883 return Definition;
884}
885
Sebastian Redle9d12b62010-01-31 22:27:38 +0000886VarDecl *VarDecl::getActingDefinition() {
887 DefinitionKind Kind = isThisDeclarationADefinition();
888 if (Kind != TentativeDefinition)
889 return 0;
890
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +0000891 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000892 VarDecl *First = getFirstDeclaration();
893 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
894 I != E; ++I) {
895 Kind = (*I)->isThisDeclarationADefinition();
896 if (Kind == Definition)
897 return 0;
898 else if (Kind == TentativeDefinition)
899 LastTentative = *I;
900 }
901 return LastTentative;
902}
903
904bool VarDecl::isTentativeDefinitionNow() const {
905 DefinitionKind Kind = isThisDeclarationADefinition();
906 if (Kind != TentativeDefinition)
907 return false;
908
909 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
910 if ((*I)->isThisDeclarationADefinition() == Definition)
911 return false;
912 }
Sebastian Redl31310a22010-02-01 20:16:42 +0000913 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +0000914}
915
Sebastian Redl31310a22010-02-01 20:16:42 +0000916VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +0000917 VarDecl *First = getFirstDeclaration();
918 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
919 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000920 if ((*I)->isThisDeclarationADefinition() == Definition)
921 return *I;
922 }
923 return 0;
924}
925
926const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000927 redecl_iterator I = redecls_begin(), E = redecls_end();
928 while (I != E && !I->getInit())
929 ++I;
930
931 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000932 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000933 return I->getInit();
934 }
935 return 0;
936}
937
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000938bool VarDecl::isOutOfLine() const {
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000939 if (Decl::isOutOfLine())
940 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +0000941
942 if (!isStaticDataMember())
943 return false;
944
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000945 // If this static data member was instantiated from a static data member of
946 // a class template, check whether that static data member was defined
947 // out-of-line.
948 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
949 return VD->isOutOfLine();
950
951 return false;
952}
953
Douglas Gregor0d035142009-10-27 18:42:08 +0000954VarDecl *VarDecl::getOutOfLineDefinition() {
955 if (!isStaticDataMember())
956 return 0;
957
958 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
959 RD != RDEnd; ++RD) {
960 if (RD->getLexicalDeclContext()->isFileContext())
961 return *RD;
962 }
963
964 return 0;
965}
966
Douglas Gregor838db382010-02-11 01:19:42 +0000967void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000968 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
969 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +0000970 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +0000971 }
972
973 Init = I;
974}
975
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000976VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000977 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000978 return cast<VarDecl>(MSI->getInstantiatedFrom());
979
980 return 0;
981}
982
Douglas Gregor663b5a02009-10-14 20:14:33 +0000983TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +0000984 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000985 return MSI->getTemplateSpecializationKind();
986
987 return TSK_Undeclared;
988}
989
Douglas Gregor1028c9f2009-10-14 21:29:40 +0000990MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000991 return getASTContext().getInstantiatedFromStaticDataMember(this);
992}
993
Douglas Gregor0a897e32009-10-15 17:21:20 +0000994void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
995 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +0000996 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +0000997 assert(MSI && "Not an instantiated static data member?");
998 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +0000999 if (TSK != TSK_ExplicitSpecialization &&
1000 PointOfInstantiation.isValid() &&
1001 MSI->getPointOfInstantiation().isInvalid())
1002 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001003}
1004
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001005//===----------------------------------------------------------------------===//
1006// ParmVarDecl Implementation
1007//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001008
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001009ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1010 SourceLocation L, IdentifierInfo *Id,
1011 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001012 StorageClass S, StorageClass SCAsWritten,
1013 Expr *DefArg) {
1014 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1015 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001016}
1017
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001018Expr *ParmVarDecl::getDefaultArg() {
1019 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1020 assert(!hasUninstantiatedDefaultArg() &&
1021 "Default argument is not yet instantiated!");
1022
1023 Expr *Arg = getInit();
1024 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1025 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001026
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001027 return Arg;
1028}
1029
1030unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1031 if (const CXXExprWithTemporaries *E =
1032 dyn_cast<CXXExprWithTemporaries>(getInit()))
1033 return E->getNumTemporaries();
1034
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001035 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001036}
1037
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001038CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1039 assert(getNumDefaultArgTemporaries() &&
1040 "Default arguments does not have any temporaries!");
1041
1042 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1043 return E->getTemporary(i);
1044}
1045
1046SourceRange ParmVarDecl::getDefaultArgRange() const {
1047 if (const Expr *E = getInit())
1048 return E->getSourceRange();
1049
1050 if (hasUninstantiatedDefaultArg())
1051 return getUninstantiatedDefaultArg()->getSourceRange();
1052
1053 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001054}
1055
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001056//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001057// FunctionDecl Implementation
1058//===----------------------------------------------------------------------===//
1059
John McCall136a6982009-09-11 06:45:03 +00001060void FunctionDecl::getNameForDiagnostic(std::string &S,
1061 const PrintingPolicy &Policy,
1062 bool Qualified) const {
1063 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1064 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1065 if (TemplateArgs)
1066 S += TemplateSpecializationType::PrintTemplateArgumentList(
1067 TemplateArgs->getFlatArgumentList(),
1068 TemplateArgs->flat_size(),
1069 Policy);
1070
1071}
Ted Kremenek27f8a282008-05-20 00:43:19 +00001072
Ted Kremenek9498d382010-04-29 16:49:01 +00001073bool FunctionDecl::isVariadic() const {
1074 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1075 return FT->isVariadic();
1076 return false;
1077}
1078
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001079bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1080 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1081 if (I->Body) {
1082 Definition = *I;
1083 return true;
1084 }
1085 }
1086
1087 return false;
1088}
1089
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001090Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001091 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1092 if (I->Body) {
1093 Definition = *I;
1094 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregorf0097952008-04-21 02:02:58 +00001095 }
1096 }
1097
1098 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001099}
1100
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001101void FunctionDecl::setBody(Stmt *B) {
1102 Body = B;
Argyrios Kyrtzidis1a5364e2009-06-22 17:13:31 +00001103 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001104 EndRangeLoc = B->getLocEnd();
1105}
1106
Douglas Gregor21386642010-09-28 21:55:22 +00001107void FunctionDecl::setPure(bool P) {
1108 IsPure = P;
1109 if (P)
1110 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1111 Parent->markedVirtualFunctionPure();
1112}
1113
Douglas Gregor48a83b52009-09-12 00:17:51 +00001114bool FunctionDecl::isMain() const {
1115 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001116 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001117 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001118 getIdentifier() && getIdentifier()->isStr("main");
1119}
1120
Douglas Gregor48a83b52009-09-12 00:17:51 +00001121bool FunctionDecl::isExternC() const {
1122 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001123 // In C, any non-static, non-overloadable function has external
1124 // linkage.
1125 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001126 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001127
Mike Stump1eb44332009-09-09 15:08:12 +00001128 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor63935192009-03-02 00:19:53 +00001129 DC = DC->getParent()) {
1130 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1131 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001132 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001133 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001134
1135 break;
1136 }
Douglas Gregor45975532010-08-17 16:09:23 +00001137
1138 if (DC->isRecord())
1139 break;
Douglas Gregor63935192009-03-02 00:19:53 +00001140 }
1141
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001142 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001143}
1144
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001145bool FunctionDecl::isGlobal() const {
1146 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1147 return Method->isStatic();
1148
John McCalld931b082010-08-26 03:08:43 +00001149 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001150 return false;
1151
Mike Stump1eb44332009-09-09 15:08:12 +00001152 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001153 DC->isNamespace();
1154 DC = DC->getParent()) {
1155 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1156 if (!Namespace->getDeclName())
1157 return false;
1158 break;
1159 }
1160 }
1161
1162 return true;
1163}
1164
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001165void
1166FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1167 redeclarable_base::setPreviousDeclaration(PrevDecl);
1168
1169 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1170 FunctionTemplateDecl *PrevFunTmpl
1171 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1172 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1173 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1174 }
1175}
1176
1177const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1178 return getFirstDeclaration();
1179}
1180
1181FunctionDecl *FunctionDecl::getCanonicalDecl() {
1182 return getFirstDeclaration();
1183}
1184
Douglas Gregor3e41d602009-02-13 23:20:09 +00001185/// \brief Returns a value indicating whether this function
1186/// corresponds to a builtin function.
1187///
1188/// The function corresponds to a built-in function if it is
1189/// declared at translation scope or within an extern "C" block and
1190/// its name matches with the name of a builtin. The returned value
1191/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001192/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001193/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001194unsigned FunctionDecl::getBuiltinID() const {
1195 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001196 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1197 return 0;
1198
1199 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1200 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1201 return BuiltinID;
1202
1203 // This function has the name of a known C library
1204 // function. Determine whether it actually refers to the C library
1205 // function or whether it just has the same name.
1206
Douglas Gregor9add3172009-02-17 03:23:10 +00001207 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001208 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001209 return 0;
1210
Douglas Gregor3c385e52009-02-14 18:57:46 +00001211 // If this function is at translation-unit scope and we're not in
1212 // C++, it refers to the C library function.
1213 if (!Context.getLangOptions().CPlusPlus &&
1214 getDeclContext()->isTranslationUnit())
1215 return BuiltinID;
1216
1217 // If the function is in an extern "C" linkage specification and is
1218 // not marked "overloadable", it's the real function.
1219 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001220 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001221 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001222 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001223 return BuiltinID;
1224
1225 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001226 return 0;
1227}
1228
1229
Chris Lattner1ad9b282009-04-25 06:03:53 +00001230/// getNumParams - Return the number of parameters this function must have
Chris Lattner2dbd2852009-04-25 06:12:16 +00001231/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001232/// after it has been created.
1233unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001234 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001235 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001236 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001237 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001238
Reid Spencer5f016e22007-07-11 17:01:13 +00001239}
1240
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001241void FunctionDecl::setParams(ASTContext &C,
1242 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001243 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001244 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 // Zero params -> null pointer.
1247 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001248 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001249 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001251
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001252 // Update source range. The check below allows us to set EndRangeLoc before
1253 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001254 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001255 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 }
1257}
1258
Chris Lattner8123a952008-04-10 02:22:51 +00001259/// getMinRequiredArguments - Returns the minimum number of arguments
1260/// needed to call this function. This may be fewer than the number of
1261/// function parameters, if some of the parameters have default
Chris Lattner9e979552008-04-12 23:52:44 +00001262/// arguments (in C++).
Chris Lattner8123a952008-04-10 02:22:51 +00001263unsigned FunctionDecl::getMinRequiredArguments() const {
1264 unsigned NumRequiredArgs = getNumParams();
1265 while (NumRequiredArgs > 0
Anders Carlssonae0b4e72009-06-06 04:14:07 +00001266 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001267 --NumRequiredArgs;
1268
1269 return NumRequiredArgs;
1270}
1271
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001272bool FunctionDecl::isInlined() const {
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001273 // FIXME: This is not enough. Consider:
1274 //
1275 // inline void f();
1276 // void f() { }
1277 //
1278 // f is inlined, but does not have inline specified.
1279 // To fix this we should add an 'inline' flag to FunctionDecl.
1280 if (isInlineSpecified())
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001281 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001282
1283 if (isa<CXXMethodDecl>(this)) {
1284 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1285 return true;
1286 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001287
1288 switch (getTemplateSpecializationKind()) {
1289 case TSK_Undeclared:
1290 case TSK_ExplicitSpecialization:
1291 return false;
1292
1293 case TSK_ImplicitInstantiation:
1294 case TSK_ExplicitInstantiationDeclaration:
1295 case TSK_ExplicitInstantiationDefinition:
1296 // Handle below.
1297 break;
1298 }
1299
1300 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001301 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001302 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001303 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001304
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001305 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001306 return PatternDecl->isInlined();
1307
1308 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001309}
1310
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001311/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001312/// definition will be externally visible.
1313///
1314/// Inline function definitions are always available for inlining optimizations.
1315/// However, depending on the language dialect, declaration specifiers, and
1316/// attributes, the definition of an inline function may or may not be
1317/// "externally" visible to other translation units in the program.
1318///
1319/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001320/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001321/// inline definition becomes externally visible (C99 6.7.4p6).
1322///
1323/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1324/// definition, we use the GNU semantics for inline, which are nearly the
1325/// opposite of C99 semantics. In particular, "inline" by itself will create
1326/// an externally visible symbol, but "extern inline" will not create an
1327/// externally visible symbol.
1328bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1329 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001330 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001331 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001332
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001333 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001334 // GNU inline semantics. Based on a number of examples, we came up with the
1335 // following heuristic: if the "inline" keyword is present on a
1336 // declaration of the function but "extern" is not present on that
1337 // declaration, then the symbol is externally visible. Otherwise, the GNU
1338 // "extern inline" semantics applies and the symbol is not externally
1339 // visible.
1340 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1341 Redecl != RedeclEnd;
1342 ++Redecl) {
John McCalld931b082010-08-26 03:08:43 +00001343 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001344 return true;
1345 }
1346
1347 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001348 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001349 }
1350
1351 // C99 6.7.4p6:
1352 // [...] If all of the file scope declarations for a function in a
1353 // translation unit include the inline function specifier without extern,
1354 // then the definition in that translation unit is an inline definition.
1355 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1356 Redecl != RedeclEnd;
1357 ++Redecl) {
1358 // Only consider file-scope declarations in this test.
1359 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1360 continue;
1361
John McCalld931b082010-08-26 03:08:43 +00001362 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001363 return true; // Not an inline definition
1364 }
1365
1366 // C99 6.7.4p6:
1367 // An inline definition does not provide an external definition for the
1368 // function, and does not forbid an external definition in another
1369 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001370 return false;
1371}
1372
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001373/// getOverloadedOperator - Which C++ overloaded operator this
1374/// function represents, if any.
1375OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001376 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1377 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001378 else
1379 return OO_None;
1380}
1381
Sean Hunta6c058d2010-01-13 09:01:02 +00001382/// getLiteralIdentifier - The literal suffix identifier this function
1383/// represents, if any.
1384const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1385 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1386 return getDeclName().getCXXLiteralIdentifier();
1387 else
1388 return 0;
1389}
1390
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001391FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1392 if (TemplateOrSpecialization.isNull())
1393 return TK_NonTemplate;
1394 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1395 return TK_FunctionTemplate;
1396 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1397 return TK_MemberSpecialization;
1398 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1399 return TK_FunctionTemplateSpecialization;
1400 if (TemplateOrSpecialization.is
1401 <DependentFunctionTemplateSpecializationInfo*>())
1402 return TK_DependentFunctionTemplateSpecialization;
1403
1404 assert(false && "Did we miss a TemplateOrSpecialization type?");
1405 return TK_NonTemplate;
1406}
1407
Douglas Gregor2db32322009-10-07 23:56:10 +00001408FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001409 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001410 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1411
1412 return 0;
1413}
1414
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001415MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1416 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1417}
1418
Douglas Gregor2db32322009-10-07 23:56:10 +00001419void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001420FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1421 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001422 TemplateSpecializationKind TSK) {
1423 assert(TemplateOrSpecialization.isNull() &&
1424 "Member function is already a specialization");
1425 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001426 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001427 TemplateOrSpecialization = Info;
1428}
1429
Douglas Gregor3b846b62009-10-27 20:53:28 +00001430bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001431 // If the function is invalid, it can't be implicitly instantiated.
1432 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001433 return false;
1434
1435 switch (getTemplateSpecializationKind()) {
1436 case TSK_Undeclared:
1437 case TSK_ExplicitSpecialization:
1438 case TSK_ExplicitInstantiationDefinition:
1439 return false;
1440
1441 case TSK_ImplicitInstantiation:
1442 return true;
1443
1444 case TSK_ExplicitInstantiationDeclaration:
1445 // Handled below.
1446 break;
1447 }
1448
1449 // Find the actual template from which we will instantiate.
1450 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001451 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001452 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001453 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001454
1455 // C++0x [temp.explicit]p9:
1456 // Except for inline functions, other explicit instantiation declarations
1457 // have the effect of suppressing the implicit instantiation of the entity
1458 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001459 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001460 return true;
1461
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001462 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001463}
1464
1465FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1466 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1467 while (Primary->getInstantiatedFromMemberTemplate()) {
1468 // If we have hit a point where the user provided a specialization of
1469 // this template, we're done looking.
1470 if (Primary->isMemberSpecialization())
1471 break;
1472
1473 Primary = Primary->getInstantiatedFromMemberTemplate();
1474 }
1475
1476 return Primary->getTemplatedDecl();
1477 }
1478
1479 return getInstantiatedFromMemberFunction();
1480}
1481
Douglas Gregor16e8be22009-06-29 17:30:29 +00001482FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001483 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001484 = TemplateOrSpecialization
1485 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001486 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001487 }
1488 return 0;
1489}
1490
1491const TemplateArgumentList *
1492FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001493 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001494 = TemplateOrSpecialization
1495 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001496 return Info->TemplateArguments;
1497 }
1498 return 0;
1499}
1500
Abramo Bagnarae03db982010-05-20 15:32:11 +00001501const TemplateArgumentListInfo *
1502FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1503 if (FunctionTemplateSpecializationInfo *Info
1504 = TemplateOrSpecialization
1505 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1506 return Info->TemplateArgumentsAsWritten;
1507 }
1508 return 0;
1509}
1510
Mike Stump1eb44332009-09-09 15:08:12 +00001511void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001512FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1513 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001514 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001515 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001516 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001517 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1518 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001519 assert(TSK != TSK_Undeclared &&
1520 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001521 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001522 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001523 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001524 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1525 TemplateArgs,
1526 TemplateArgsAsWritten,
1527 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001528 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Douglas Gregor127102b2009-06-29 20:59:39 +00001530 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001531 // function template specializations.
1532 if (InsertPos)
1533 Template->getSpecializations().InsertNode(Info, InsertPos);
1534 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001535 // Try to insert the new node. If there is an existing node, leave it, the
1536 // set will contain the canonical decls while
1537 // FunctionTemplateDecl::findSpecialization will return
1538 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001539 FunctionTemplateSpecializationInfo *Existing
1540 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001541 (void)Existing;
1542 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1543 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001544 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001545}
1546
John McCallaf2094e2010-04-08 09:05:18 +00001547void
1548FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1549 const UnresolvedSetImpl &Templates,
1550 const TemplateArgumentListInfo &TemplateArgs) {
1551 assert(TemplateOrSpecialization.isNull());
1552 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1553 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001554 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001555 void *Buffer = Context.Allocate(Size);
1556 DependentFunctionTemplateSpecializationInfo *Info =
1557 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1558 TemplateArgs);
1559 TemplateOrSpecialization = Info;
1560}
1561
1562DependentFunctionTemplateSpecializationInfo::
1563DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1564 const TemplateArgumentListInfo &TArgs)
1565 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1566
1567 d.NumTemplates = Ts.size();
1568 d.NumArgs = TArgs.size();
1569
1570 FunctionTemplateDecl **TsArray =
1571 const_cast<FunctionTemplateDecl**>(getTemplates());
1572 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1573 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1574
1575 TemplateArgumentLoc *ArgsArray =
1576 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1577 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1578 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1579}
1580
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001581TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001582 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001583 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001584 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001585 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001586 if (FTSInfo)
1587 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001588
Douglas Gregor2db32322009-10-07 23:56:10 +00001589 MemberSpecializationInfo *MSInfo
1590 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1591 if (MSInfo)
1592 return MSInfo->getTemplateSpecializationKind();
1593
1594 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001595}
1596
Mike Stump1eb44332009-09-09 15:08:12 +00001597void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001598FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1599 SourceLocation PointOfInstantiation) {
1600 if (FunctionTemplateSpecializationInfo *FTSInfo
1601 = TemplateOrSpecialization.dyn_cast<
1602 FunctionTemplateSpecializationInfo*>()) {
1603 FTSInfo->setTemplateSpecializationKind(TSK);
1604 if (TSK != TSK_ExplicitSpecialization &&
1605 PointOfInstantiation.isValid() &&
1606 FTSInfo->getPointOfInstantiation().isInvalid())
1607 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1608 } else if (MemberSpecializationInfo *MSInfo
1609 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1610 MSInfo->setTemplateSpecializationKind(TSK);
1611 if (TSK != TSK_ExplicitSpecialization &&
1612 PointOfInstantiation.isValid() &&
1613 MSInfo->getPointOfInstantiation().isInvalid())
1614 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1615 } else
1616 assert(false && "Function cannot have a template specialization kind");
1617}
1618
1619SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001620 if (FunctionTemplateSpecializationInfo *FTSInfo
1621 = TemplateOrSpecialization.dyn_cast<
1622 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001623 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001624 else if (MemberSpecializationInfo *MSInfo
1625 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001626 return MSInfo->getPointOfInstantiation();
1627
1628 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001629}
1630
Douglas Gregor9f185072009-09-11 20:15:17 +00001631bool FunctionDecl::isOutOfLine() const {
Douglas Gregor9f185072009-09-11 20:15:17 +00001632 if (Decl::isOutOfLine())
1633 return true;
1634
1635 // If this function was instantiated from a member function of a
1636 // class template, check whether that member function was defined out-of-line.
1637 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1638 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001639 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001640 return Definition->isOutOfLine();
1641 }
1642
1643 // If this function was instantiated from a function template,
1644 // check whether that function template was defined out-of-line.
1645 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1646 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001647 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00001648 return Definition->isOutOfLine();
1649 }
1650
1651 return false;
1652}
1653
Chris Lattner8a934232008-03-31 00:36:02 +00001654//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001655// FieldDecl Implementation
1656//===----------------------------------------------------------------------===//
1657
1658FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1659 IdentifierInfo *Id, QualType T,
1660 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1661 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1662}
1663
1664bool FieldDecl::isAnonymousStructOrUnion() const {
1665 if (!isImplicit() || getDeclName())
1666 return false;
1667
1668 if (const RecordType *Record = getType()->getAs<RecordType>())
1669 return Record->getDecl()->isAnonymousStructOrUnion();
1670
1671 return false;
1672}
1673
1674//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001675// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001676//===----------------------------------------------------------------------===//
1677
Douglas Gregor1693e152010-07-06 18:42:40 +00001678SourceLocation TagDecl::getOuterLocStart() const {
1679 return getTemplateOrInnerLocStart(this);
1680}
1681
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001682SourceRange TagDecl::getSourceRange() const {
1683 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00001684 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00001685}
1686
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001687TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001688 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00001689}
1690
Douglas Gregor60e70642010-05-19 18:39:18 +00001691void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1692 TypedefDeclOrQualifier = TDD;
1693 if (TypeForDecl)
1694 TypeForDecl->ClearLinkageCache();
1695}
1696
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001697void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00001698 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00001699
1700 if (isa<CXXRecordDecl>(this)) {
1701 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1702 struct CXXRecordDecl::DefinitionData *Data =
1703 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00001704 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1705 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00001706 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001707}
1708
1709void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00001710 assert((!isa<CXXRecordDecl>(this) ||
1711 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1712 "definition completed but not started");
1713
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001714 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00001715 IsBeingDefined = false;
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001716}
1717
Douglas Gregor952b0172010-02-11 01:04:33 +00001718TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001719 if (isDefinition())
1720 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00001721 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1722 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00001723
1724 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001725 R != REnd; ++R)
1726 if (R->isDefinition())
1727 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001729 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001730}
1731
John McCallb6217662010-03-15 10:12:16 +00001732void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1733 SourceRange QualifierRange) {
1734 if (Qualifier) {
1735 // Make sure the extended qualifier info is allocated.
1736 if (!hasExtInfo())
1737 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1738 // Set qualifier info.
1739 getExtInfo()->NNS = Qualifier;
1740 getExtInfo()->NNSRange = QualifierRange;
1741 }
1742 else {
1743 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1744 assert(QualifierRange.isInvalid());
1745 if (hasExtInfo()) {
1746 getASTContext().Deallocate(getExtInfo());
1747 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1748 }
1749 }
1750}
1751
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001752//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001753// EnumDecl Implementation
1754//===----------------------------------------------------------------------===//
1755
1756EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1757 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001758 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1759 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1760 IsScoped, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001761 C.getTypeDeclType(Enum, PrevDecl);
1762 return Enum;
1763}
1764
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001765EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001766 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1767 false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001768}
1769
Douglas Gregor838db382010-02-11 01:19:42 +00001770void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00001771 QualType NewPromotionType,
1772 unsigned NumPositiveBits,
1773 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001774 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00001775 if (!IntegerType)
1776 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001777 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00001778 setNumPositiveBits(NumPositiveBits);
1779 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001780 TagDecl::completeDefinition();
1781}
1782
1783//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001784// RecordDecl Implementation
1785//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00001786
Argyrios Kyrtzidis35bc0822008-10-15 00:42:39 +00001787RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001788 IdentifierInfo *Id, RecordDecl *PrevDecl,
1789 SourceLocation TKL)
1790 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek63597922008-09-02 21:12:32 +00001791 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00001792 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001793 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001794 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00001795 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00001796}
1797
1798RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001799 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor741dd9a2009-07-21 14:46:17 +00001800 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00001802 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001803 C.getTypeDeclType(R, PrevDecl);
1804 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00001805}
1806
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001807RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1808 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1809 SourceLocation());
1810}
1811
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001812bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001813 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00001814 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1815}
1816
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001817RecordDecl::field_iterator RecordDecl::field_begin() const {
1818 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1819 LoadFieldsFromExternalStorage();
1820
1821 return field_iterator(decl_iterator(FirstDecl));
1822}
1823
Douglas Gregor44b43212008-12-11 16:49:14 +00001824/// completeDefinition - Notes that the definition of this type is now
1825/// complete.
Douglas Gregor838db382010-02-11 01:19:42 +00001826void RecordDecl::completeDefinition() {
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregor0b7a1582009-01-17 00:42:38 +00001828 TagDecl::completeDefinition();
Reid Spencer5f016e22007-07-11 17:01:13 +00001829}
1830
John McCallbc365c52010-05-21 01:17:40 +00001831ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1832 // Force the decl chain to come into existence properly.
1833 if (!getNextDeclInContext()) getParent()->decls_begin();
1834
1835 assert(isAnonymousStructOrUnion());
1836 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1837 assert(D->getType()->isRecordType());
1838 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1839 return D;
1840}
1841
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00001842void RecordDecl::LoadFieldsFromExternalStorage() const {
1843 ExternalASTSource *Source = getASTContext().getExternalSource();
1844 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1845
1846 // Notify that we have a RecordDecl doing some initialization.
1847 ExternalASTSource::Deserializing TheFields(Source);
1848
1849 llvm::SmallVector<Decl*, 64> Decls;
1850 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1851 return;
1852
1853#ifndef NDEBUG
1854 // Check that all decls we got were FieldDecls.
1855 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1856 assert(isa<FieldDecl>(Decls[i]));
1857#endif
1858
1859 LoadedFieldsFromExternalStorage = true;
1860
1861 if (Decls.empty())
1862 return;
1863
1864 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1865}
1866
Steve Naroff56ee6892008-10-08 17:01:13 +00001867//===----------------------------------------------------------------------===//
1868// BlockDecl Implementation
1869//===----------------------------------------------------------------------===//
1870
Douglas Gregor838db382010-02-11 01:19:42 +00001871void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00001872 unsigned NParms) {
1873 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Steve Naroffe78b8092009-03-13 16:56:44 +00001875 // Zero params -> null pointer.
1876 if (NParms) {
1877 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00001878 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00001879 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1880 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1881 }
1882}
1883
1884unsigned BlockDecl::getNumParams() const {
1885 return NumParams;
1886}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001887
1888
1889//===----------------------------------------------------------------------===//
1890// Other Decl Allocation/Deallocation Method Implementations
1891//===----------------------------------------------------------------------===//
1892
1893TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1894 return new (C) TranslationUnitDecl(C);
1895}
1896
1897NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1898 SourceLocation L, IdentifierInfo *Id) {
1899 return new (C) NamespaceDecl(DC, L, Id);
1900}
1901
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001902ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1903 SourceLocation L, IdentifierInfo *Id, QualType T) {
1904 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1905}
1906
1907FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara25777432010-08-11 22:01:17 +00001908 const DeclarationNameInfo &NameInfo,
1909 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001910 StorageClass S, StorageClass SCAsWritten,
1911 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnara25777432010-08-11 22:01:17 +00001912 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001913 S, SCAsWritten, isInline);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001914 New->HasWrittenPrototype = hasWrittenPrototype;
1915 return New;
1916}
1917
1918BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1919 return new (C) BlockDecl(DC, L);
1920}
1921
1922EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1923 SourceLocation L,
1924 IdentifierInfo *Id, QualType T,
1925 Expr *E, const llvm::APSInt &V) {
1926 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1927}
1928
Douglas Gregor8e7139c2010-09-01 20:41:53 +00001929SourceRange EnumConstantDecl::getSourceRange() const {
1930 SourceLocation End = getLocation();
1931 if (Init)
1932 End = Init->getLocEnd();
1933 return SourceRange(getLocation(), End);
1934}
1935
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001936TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1937 SourceLocation L, IdentifierInfo *Id,
1938 TypeSourceInfo *TInfo) {
1939 return new (C) TypedefDecl(DC, L, Id, TInfo);
1940}
1941
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001942FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1943 SourceLocation L,
1944 StringLiteral *Str) {
1945 return new (C) FileScopeAsmDecl(DC, L, Str);
1946}