blob: d59fc7a344f34c165d545049373a57bde103d32d [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000029
Chris Lattner6d9a6852006-10-25 05:11:20 +000030using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000031
Chris Lattner88f70d62008-03-15 05:43:15 +000032//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000033// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000034//===----------------------------------------------------------------------===//
35
John McCallb7139c42010-10-28 04:18:25 +000036static const VisibilityAttr *GetExplicitVisibility(const Decl *D) {
37 // If the decl is redeclarable, make sure we use the explicit
38 // visibility attribute from the most recent declaration.
39 //
40 // Note that this isn't necessary for tags, which can't have their
41 // visibility adjusted.
42 if (isa<VarDecl>(D)) {
43 return cast<VarDecl>(D)->getMostRecentDeclaration()
44 ->getAttr<VisibilityAttr>();
45 } else if (isa<FunctionDecl>(D)) {
46 return cast<FunctionDecl>(D)->getMostRecentDeclaration()
47 ->getAttr<VisibilityAttr>();
48 } else {
49 return D->getAttr<VisibilityAttr>();
50 }
51}
52
John McCall457a04e2010-10-22 21:05:15 +000053static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
54 switch (A->getVisibility()) {
55 case VisibilityAttr::Default:
56 return DefaultVisibility;
57 case VisibilityAttr::Hidden:
58 return HiddenVisibility;
59 case VisibilityAttr::Protected:
60 return ProtectedVisibility;
61 }
62 return DefaultVisibility;
63}
64
John McCallc273f242010-10-30 11:50:40 +000065typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall457a04e2010-10-22 21:05:15 +000066typedef std::pair<Linkage,Visibility> LVPair;
John McCallc273f242010-10-30 11:50:40 +000067
John McCall457a04e2010-10-22 21:05:15 +000068static LVPair merge(LVPair L, LVPair R) {
69 return LVPair(minLinkage(L.first, R.first),
70 minVisibility(L.second, R.second));
71}
72
John McCallc273f242010-10-30 11:50:40 +000073static LVPair merge(LVPair L, LinkageInfo R) {
74 return LVPair(minLinkage(L.first, R.linkage()),
75 minVisibility(L.second, R.visibility()));
76}
77
Benjamin Kramer396dcf32010-11-05 19:56:37 +000078namespace {
John McCall07072662010-11-02 01:45:15 +000079/// Flags controlling the computation of linkage and visibility.
80struct LVFlags {
81 bool ConsiderGlobalVisibility;
82 bool ConsiderVisibilityAttributes;
83
84 LVFlags() : ConsiderGlobalVisibility(true),
85 ConsiderVisibilityAttributes(true) {
86 }
87
Douglas Gregorbf62d642010-12-06 18:36:25 +000088 /// \brief Returns a set of flags that is only useful for computing the
89 /// linkage, not the visibility, of a declaration.
90 static LVFlags CreateOnlyDeclLinkage() {
91 LVFlags F;
92 F.ConsiderGlobalVisibility = false;
93 F.ConsiderVisibilityAttributes = false;
94 return F;
95 }
96
John McCall07072662010-11-02 01:45:15 +000097 /// Returns a set of flags, otherwise based on these, which ignores
98 /// off all sources of visibility except template arguments.
99 LVFlags onlyTemplateVisibility() const {
100 LVFlags F = *this;
101 F.ConsiderGlobalVisibility = false;
102 F.ConsiderVisibilityAttributes = false;
103 return F;
104 }
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000105};
Benjamin Kramer396dcf32010-11-05 19:56:37 +0000106} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +0000107
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000108/// \brief Get the most restrictive linkage for the types in the given
109/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +0000110static LVPair
111getLVForTemplateParameterList(const TemplateParameterList *Params) {
112 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000113 for (TemplateParameterList::const_iterator P = Params->begin(),
114 PEnd = Params->end();
115 P != PEnd; ++P) {
116 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
117 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +0000118 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000119 continue;
120 }
121
122 if (TemplateTemplateParmDecl *TTP
123 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +0000124 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000125 }
126 }
127
John McCall457a04e2010-10-22 21:05:15 +0000128 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000129}
130
Douglas Gregorbf62d642010-12-06 18:36:25 +0000131/// getLVForDecl - Get the linkage and visibility for the given declaration.
132static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
133
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000134/// \brief Get the most restrictive linkage for the types and
135/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +0000136static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregorbf62d642010-12-06 18:36:25 +0000137 unsigned NumArgs,
138 LVFlags &F) {
John McCall457a04e2010-10-22 21:05:15 +0000139 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000140
141 for (unsigned I = 0; I != NumArgs; ++I) {
142 switch (Args[I].getKind()) {
143 case TemplateArgument::Null:
144 case TemplateArgument::Integral:
145 case TemplateArgument::Expression:
146 break;
147
148 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000149 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000150 break;
151
152 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000153 // The decl can validly be null as the representation of nullptr
154 // arguments, valid only in C++0x.
155 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000156 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
157 LV = merge(LV, getLVForDecl(ND, F));
John McCall457a04e2010-10-22 21:05:15 +0000158 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000159 break;
160
161 case TemplateArgument::Template:
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000162 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
163 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000164 break;
165
166 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000167 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregorbf62d642010-12-06 18:36:25 +0000168 Args[I].pack_size(),
169 F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000170 break;
171 }
172 }
173
John McCall457a04e2010-10-22 21:05:15 +0000174 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000175}
176
John McCallc273f242010-10-30 11:50:40 +0000177static LVPair
Douglas Gregorbf62d642010-12-06 18:36:25 +0000178getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
179 LVFlags &F) {
180 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall8823c652010-08-13 08:35:10 +0000181}
182
John McCall07072662010-11-02 01:45:15 +0000183static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000184 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000185 "Not a name having namespace scope");
186 ASTContext &Context = D->getASTContext();
187
188 // C++ [basic.link]p3:
189 // A name having namespace scope (3.3.6) has internal linkage if it
190 // is the name of
191 // - an object, reference, function or function template that is
192 // explicitly declared static; or,
193 // (This bullet corresponds to C99 6.2.2p3.)
194 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
195 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000196 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000197 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000198
199 // - an object or reference that is explicitly declared const
200 // and neither explicitly declared extern nor previously
201 // declared to have external linkage; or
202 // (there is no equivalent in C99)
203 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000204 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000205 Var->getStorageClass() != SC_Extern &&
206 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000207 bool FoundExtern = false;
208 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
209 PrevVar && !FoundExtern;
210 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000211 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000212 FoundExtern = true;
213
214 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000215 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000216 }
217 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000218 // C++ [temp]p4:
219 // A non-member function template can have internal linkage; any
220 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000221 const FunctionDecl *Function = 0;
222 if (const FunctionTemplateDecl *FunTmpl
223 = dyn_cast<FunctionTemplateDecl>(D))
224 Function = FunTmpl->getTemplatedDecl();
225 else
226 Function = cast<FunctionDecl>(D);
227
228 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000229 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000230 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000231 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
232 // - a data member of an anonymous union.
233 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000234 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000235 }
236
John McCall457a04e2010-10-22 21:05:15 +0000237 if (D->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000238 return LinkageInfo::uniqueExternal();
John McCallb7139c42010-10-28 04:18:25 +0000239
John McCall457a04e2010-10-22 21:05:15 +0000240 // Set up the defaults.
241
242 // C99 6.2.2p5:
243 // If the declaration of an identifier for an object has file
244 // scope and no storage-class specifier, its linkage is
245 // external.
John McCallc273f242010-10-30 11:50:40 +0000246 LinkageInfo LV;
247
John McCall07072662010-11-02 01:45:15 +0000248 if (F.ConsiderVisibilityAttributes) {
249 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
250 LV.setVisibility(GetVisibilityFromAttr(VA), true);
251 F.ConsiderGlobalVisibility = false;
252 }
John McCallc273f242010-10-30 11:50:40 +0000253 }
John McCall457a04e2010-10-22 21:05:15 +0000254
Douglas Gregorf73b2822009-11-25 22:24:25 +0000255 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000256
Douglas Gregorf73b2822009-11-25 22:24:25 +0000257 // A name having namespace scope has external linkage if it is the
258 // name of
259 //
260 // - an object or reference, unless it has internal linkage; or
261 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000262 // GCC applies the following optimization to variables and static
263 // data members, but not to functions:
264 //
John McCall457a04e2010-10-22 21:05:15 +0000265 // Modify the variable's LV by the LV of its type unless this is
266 // C or extern "C". This follows from [basic.link]p9:
267 // A type without linkage shall not be used as the type of a
268 // variable or function with external linkage unless
269 // - the entity has C language linkage, or
270 // - the entity is declared within an unnamed namespace, or
271 // - the entity is not used or is defined in the same
272 // translation unit.
273 // and [basic.link]p10:
274 // ...the types specified by all declarations referring to a
275 // given variable or function shall be identical...
276 // C does not have an equivalent rule.
277 //
John McCall5fe84122010-10-26 04:59:26 +0000278 // Ignore this if we've got an explicit attribute; the user
279 // probably knows what they're doing.
280 //
John McCall457a04e2010-10-22 21:05:15 +0000281 // Note that we don't want to make the variable non-external
282 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000283 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000284 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
285 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000286 return LinkageInfo::uniqueExternal();
287 if (!LV.visibilityExplicit())
288 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000289 }
290
John McCall23032652010-11-02 18:38:13 +0000291 if (Var->getStorageClass() == SC_PrivateExtern)
292 LV.setVisibility(HiddenVisibility, true);
293
Douglas Gregorf73b2822009-11-25 22:24:25 +0000294 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000295 (Var->getStorageClass() == SC_Extern ||
296 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000297
Douglas Gregorf73b2822009-11-25 22:24:25 +0000298 // C99 6.2.2p4:
299 // For an identifier declared with the storage-class specifier
300 // extern in a scope in which a prior declaration of that
301 // identifier is visible, if the prior declaration specifies
302 // internal or external linkage, the linkage of the identifier
303 // at the later declaration is the same as the linkage
304 // specified at the prior declaration. If no prior declaration
305 // is visible, or if the prior declaration specifies no
306 // linkage, then the identifier has external linkage.
307 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000308 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallc273f242010-10-30 11:50:40 +0000309 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
310 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000311 }
312 }
313
Douglas Gregorf73b2822009-11-25 22:24:25 +0000314 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000315 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000316 // In theory, we can modify the function's LV by the LV of its
317 // type unless it has C linkage (see comment above about variables
318 // for justification). In practice, GCC doesn't do this, so it's
319 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000320
John McCall23032652010-11-02 18:38:13 +0000321 if (Function->getStorageClass() == SC_PrivateExtern)
322 LV.setVisibility(HiddenVisibility, true);
323
Douglas Gregorf73b2822009-11-25 22:24:25 +0000324 // C99 6.2.2p5:
325 // If the declaration of an identifier for a function has no
326 // storage-class specifier, its linkage is determined exactly
327 // as if it were declared with the storage-class specifier
328 // extern.
329 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000330 (Function->getStorageClass() == SC_Extern ||
331 Function->getStorageClass() == SC_PrivateExtern ||
332 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000333 // C99 6.2.2p4:
334 // For an identifier declared with the storage-class specifier
335 // extern in a scope in which a prior declaration of that
336 // identifier is visible, if the prior declaration specifies
337 // internal or external linkage, the linkage of the identifier
338 // at the later declaration is the same as the linkage
339 // specified at the prior declaration. If no prior declaration
340 // is visible, or if the prior declaration specifies no
341 // linkage, then the identifier has external linkage.
342 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000343 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallc273f242010-10-30 11:50:40 +0000344 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
345 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000346 }
347 }
348
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000349 if (FunctionTemplateSpecializationInfo *SpecInfo
350 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000351 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
352 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000353 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000354 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000355 }
356
Douglas Gregorf73b2822009-11-25 22:24:25 +0000357 // - a named class (Clause 9), or an unnamed class defined in a
358 // typedef declaration in which the class has the typedef name
359 // for linkage purposes (7.1.3); or
360 // - a named enumeration (7.2), or an unnamed enumeration
361 // defined in a typedef declaration in which the enumeration
362 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000363 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
364 // Unnamed tags have no linkage.
365 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000366 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000367
John McCall457a04e2010-10-22 21:05:15 +0000368 // If this is a class template specialization, consider the
369 // linkage of the template and template arguments.
370 if (const ClassTemplateSpecializationDecl *Spec
371 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000372 // From the template.
373 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
374 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000375
John McCall457a04e2010-10-22 21:05:15 +0000376 // The arguments at which the template was instantiated.
377 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000378 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000379 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000380
John McCall5fe84122010-10-26 04:59:26 +0000381 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000382 if (F.ConsiderGlobalVisibility)
383 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000384 (Context.getLangOptions().CPlusPlus &&
385 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000386
Douglas Gregorf73b2822009-11-25 22:24:25 +0000387 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000388 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000389 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000390 if (!isExternalLinkage(EnumLV.linkage()))
391 return LinkageInfo::none();
392 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000393
394 // - a template, unless it is a function template that has
395 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000396 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
John McCallc273f242010-10-30 11:50:40 +0000397 LV.merge(getLVForTemplateParameterList(Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000398
Douglas Gregorf73b2822009-11-25 22:24:25 +0000399 // - a namespace (7.3), unless it is declared within an unnamed
400 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000401 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
402 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000403
John McCall457a04e2010-10-22 21:05:15 +0000404 // By extension, we assign external linkage to Objective-C
405 // interfaces.
406 } else if (isa<ObjCInterfaceDecl>(D)) {
407 // fallout
408
409 // Everything not covered here has no linkage.
410 } else {
John McCallc273f242010-10-30 11:50:40 +0000411 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000412 }
413
414 // If we ended up with non-external linkage, visibility should
415 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000416 if (LV.linkage() != ExternalLinkage)
417 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000418
419 // If we didn't end up with hidden visibility, consider attributes
420 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000421 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000422 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000423
424 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000425}
426
John McCall07072662010-11-02 01:45:15 +0000427static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000428 // Only certain class members have linkage. Note that fields don't
429 // really have linkage, but it's convenient to say they do for the
430 // purposes of calculating linkage of pointer-to-data-member
431 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000432 if (!(isa<CXXMethodDecl>(D) ||
433 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000434 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000435 (isa<TagDecl>(D) &&
436 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000437 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000438
John McCall07072662010-11-02 01:45:15 +0000439 LinkageInfo LV;
440
441 // The flags we're going to use to compute the class's visibility.
442 LVFlags ClassF = F;
443
444 // If we have an explicit visibility attribute, merge that in.
445 if (F.ConsiderVisibilityAttributes) {
446 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
447 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
448
449 // Ignore global visibility later, but not this attribute.
450 F.ConsiderGlobalVisibility = false;
451
452 // Ignore both global visibility and attributes when computing our
453 // parent's visibility.
454 ClassF = F.onlyTemplateVisibility();
455 }
456 }
John McCallc273f242010-10-30 11:50:40 +0000457
458 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000459 // linkage.
460 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
461 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000462 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000463
464 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000465 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000466 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000467
John McCall8823c652010-08-13 08:35:10 +0000468 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000469 TemplateSpecializationKind TSK = TSK_Undeclared;
470
John McCall457a04e2010-10-22 21:05:15 +0000471 // If this is a method template specialization, use the linkage for
472 // the template parameters and arguments.
473 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000474 = MD->getTemplateSpecializationInfo()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000475 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCallc273f242010-10-30 11:50:40 +0000476 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000477 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000478
479 TSK = Spec->getTemplateSpecializationKind();
480 } else if (MemberSpecializationInfo *MSI =
481 MD->getMemberSpecializationInfo()) {
482 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000483 }
484
John McCall37bb6c92010-10-29 22:22:43 +0000485 // If we're paying attention to global visibility, apply
486 // -finline-visibility-hidden if this is an inline method.
487 //
John McCallc273f242010-10-30 11:50:40 +0000488 // Note that ConsiderGlobalVisibility doesn't yet have information
489 // about whether containing classes have visibility attributes,
490 // and that's intentional.
491 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000492 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000493 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
494 // InlineVisibilityHidden only applies to definitions, and
495 // isInlined() only gives meaningful answers on definitions
496 // anyway.
497 const FunctionDecl *Def = 0;
498 if (MD->hasBody(Def) && Def->isInlined())
499 LV.setVisibility(HiddenVisibility);
500 }
John McCall457a04e2010-10-22 21:05:15 +0000501
John McCall37bb6c92010-10-29 22:22:43 +0000502 // Note that in contrast to basically every other situation, we
503 // *do* apply -fvisibility to method declarations.
504
505 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000506 if (const ClassTemplateSpecializationDecl *Spec
507 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
508 // Merge template argument/parameter information for member
509 // class template specializations.
Douglas Gregorbf62d642010-12-06 18:36:25 +0000510 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCallc273f242010-10-30 11:50:40 +0000511 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000512 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000513 }
514
John McCall37bb6c92010-10-29 22:22:43 +0000515 // Static data members.
516 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000517 // Modify the variable's linkage by its type, but ignore the
518 // type's visibility unless it's a definition.
519 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
520 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000521 LV.mergeLinkage(UniqueExternalLinkage);
522 if (!LV.visibilityExplicit())
523 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000524 }
525
John McCall07072662010-11-02 01:45:15 +0000526 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000527
528 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000529 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000530 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000531 }
532
John McCall457a04e2010-10-22 21:05:15 +0000533 return LV;
John McCall8823c652010-08-13 08:35:10 +0000534}
535
Douglas Gregorbf62d642010-12-06 18:36:25 +0000536Linkage NamedDecl::getLinkage() const {
537 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000538 assert(Linkage(CachedLinkage) ==
539 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000540 return Linkage(CachedLinkage);
541 }
542
543 CachedLinkage = getLVForDecl(this,
544 LVFlags::CreateOnlyDeclLinkage()).linkage();
545 HasCachedLinkage = 1;
546 return Linkage(CachedLinkage);
547}
548
John McCallc273f242010-10-30 11:50:40 +0000549LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000550 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000551 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000552 HasCachedLinkage = 1;
553 CachedLinkage = LI.linkage();
554 return LI;
John McCall033caa52010-10-29 00:29:13 +0000555}
Ted Kremenek926d8602010-04-20 23:15:35 +0000556
John McCall07072662010-11-02 01:45:15 +0000557static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000558 // Objective-C: treat all Objective-C declarations as having external
559 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000560 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000561 default:
562 break;
John McCall457a04e2010-10-22 21:05:15 +0000563 case Decl::TemplateTemplateParm: // count these as external
564 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000565 case Decl::ObjCAtDefsField:
566 case Decl::ObjCCategory:
567 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000568 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000569 case Decl::ObjCForwardProtocol:
570 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000571 case Decl::ObjCMethod:
572 case Decl::ObjCProperty:
573 case Decl::ObjCPropertyImpl:
574 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000575 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000576 }
577
Douglas Gregorf73b2822009-11-25 22:24:25 +0000578 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000579 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000580 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000581
582 // C++ [basic.link]p5:
583 // In addition, a member function, static data member, a named
584 // class or enumeration of class scope, or an unnamed class or
585 // enumeration defined in a class-scope typedef declaration such
586 // that the class or enumeration has the typedef name for linkage
587 // purposes (7.1.3), has external linkage if the name of the class
588 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000589 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000590 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000591
592 // C++ [basic.link]p6:
593 // The name of a function declared in block scope and the name of
594 // an object declared by a block scope extern declaration have
595 // linkage. If there is a visible declaration of an entity with
596 // linkage having the same name and type, ignoring entities
597 // declared outside the innermost enclosing namespace scope, the
598 // block scope declaration declares that same entity and receives
599 // the linkage of the previous declaration. If there is more than
600 // one such matching entity, the program is ill-formed. Otherwise,
601 // if no matching entity is found, the block scope entity receives
602 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000603 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
604 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000605 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000606 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000607
John McCallc273f242010-10-30 11:50:40 +0000608 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000609 if (Flags.ConsiderVisibilityAttributes) {
610 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
611 LV.setVisibility(GetVisibilityFromAttr(VA));
612 }
613
John McCall457a04e2010-10-22 21:05:15 +0000614 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000615 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000616 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
617 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000618 }
619
620 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000621 }
622
John McCall033caa52010-10-29 00:29:13 +0000623 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000624 if (Var->getStorageClass() == SC_Extern ||
625 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000626 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000627 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000628
John McCallc273f242010-10-30 11:50:40 +0000629 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000630 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000631 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000632 else if (Flags.ConsiderVisibilityAttributes) {
633 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
634 LV.setVisibility(GetVisibilityFromAttr(VA));
635 }
636
John McCall457a04e2010-10-22 21:05:15 +0000637 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000638 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000639 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
640 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000641 }
642
643 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000644 }
645 }
646
647 // C++ [basic.link]p6:
648 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000649 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000650}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000651
Douglas Gregor2ada0482009-02-04 17:27:36 +0000652std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000653 return getQualifiedNameAsString(getASTContext().getLangOptions());
654}
655
656std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000657 const DeclContext *Ctx = getDeclContext();
658
659 if (Ctx->isFunctionOrMethod())
660 return getNameAsString();
661
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000662 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
663 ContextsTy Contexts;
664
665 // Collect contexts.
666 while (Ctx && isa<NamedDecl>(Ctx)) {
667 Contexts.push_back(Ctx);
668 Ctx = Ctx->getParent();
669 };
670
671 std::string QualName;
672 llvm::raw_string_ostream OS(QualName);
673
674 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
675 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000676 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000677 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000678 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
679 std::string TemplateArgsStr
680 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000681 TemplateArgs.data(),
682 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000683 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000684 OS << Spec->getName() << TemplateArgsStr;
685 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000686 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000687 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000688 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000689 OS << ND;
690 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
691 if (!RD->getIdentifier())
692 OS << "<anonymous " << RD->getKindName() << '>';
693 else
694 OS << RD;
695 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000696 const FunctionProtoType *FT = 0;
697 if (FD->hasWrittenPrototype())
698 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
699
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000700 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000701 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000702 unsigned NumParams = FD->getNumParams();
703 for (unsigned i = 0; i < NumParams; ++i) {
704 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000705 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000706 std::string Param;
707 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000708 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000709 }
710
711 if (FT->isVariadic()) {
712 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000713 OS << ", ";
714 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000715 }
716 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000717 OS << ')';
718 } else {
719 OS << cast<NamedDecl>(*I);
720 }
721 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000722 }
723
John McCalla2a3f7d2010-03-16 21:48:18 +0000724 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000725 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000726 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000727 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000728
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000729 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000730}
731
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000732bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000733 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
734
Douglas Gregor889ceb72009-02-03 19:21:40 +0000735 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
736 // We want to keep it, unless it nominates same namespace.
737 if (getKind() == Decl::UsingDirective) {
738 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
739 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
740 }
Mike Stump11289f42009-09-09 15:08:12 +0000741
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000742 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
743 // For function declarations, we keep track of redeclarations.
744 return FD->getPreviousDeclaration() == OldD;
745
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000746 // For function templates, the underlying function declarations are linked.
747 if (const FunctionTemplateDecl *FunctionTemplate
748 = dyn_cast<FunctionTemplateDecl>(this))
749 if (const FunctionTemplateDecl *OldFunctionTemplate
750 = dyn_cast<FunctionTemplateDecl>(OldD))
751 return FunctionTemplate->getTemplatedDecl()
752 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000753
Steve Naroffc4173fa2009-02-22 19:35:57 +0000754 // For method declarations, we keep track of redeclarations.
755 if (isa<ObjCMethodDecl>(this))
756 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000757
John McCall9f3059a2009-10-09 21:13:30 +0000758 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
759 return true;
760
John McCall3f746822009-11-17 05:59:44 +0000761 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
762 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
763 cast<UsingShadowDecl>(OldD)->getTargetDecl();
764
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000765 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
766 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
767 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
768
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000769 // For non-function declarations, if the declarations are of the
770 // same kind then this must be a redeclaration, or semantic analysis
771 // would not have given us the new declaration.
772 return this->getKind() == OldD->getKind();
773}
774
Douglas Gregoreddf4332009-02-24 20:03:32 +0000775bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000776 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000777}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000778
Anders Carlsson6915bf62009-06-26 06:29:23 +0000779NamedDecl *NamedDecl::getUnderlyingDecl() {
780 NamedDecl *ND = this;
781 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000782 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000783 ND = UD->getTargetDecl();
784 else if (ObjCCompatibleAliasDecl *AD
785 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
786 return AD->getClassInterface();
787 else
788 return ND;
789 }
790}
791
John McCalla8ae2222010-04-06 21:38:20 +0000792bool NamedDecl::isCXXInstanceMember() const {
793 assert(isCXXClassMember() &&
794 "checking whether non-member is instance member");
795
796 const NamedDecl *D = this;
797 if (isa<UsingShadowDecl>(D))
798 D = cast<UsingShadowDecl>(D)->getTargetDecl();
799
Francois Pichet783dd6e2010-11-21 06:08:52 +0000800 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000801 return true;
802 if (isa<CXXMethodDecl>(D))
803 return cast<CXXMethodDecl>(D)->isInstance();
804 if (isa<FunctionTemplateDecl>(D))
805 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
806 ->getTemplatedDecl())->isInstance();
807 return false;
808}
809
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000810//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000811// DeclaratorDecl Implementation
812//===----------------------------------------------------------------------===//
813
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000814template <typename DeclT>
815static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
816 if (decl->getNumTemplateParameterLists() > 0)
817 return decl->getTemplateParameterList(0)->getTemplateLoc();
818 else
819 return decl->getInnerLocStart();
820}
821
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000822SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000823 TypeSourceInfo *TSI = getTypeSourceInfo();
824 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000825 return SourceLocation();
826}
827
John McCall3e11ebe2010-03-15 10:12:16 +0000828void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
829 SourceRange QualifierRange) {
830 if (Qualifier) {
831 // Make sure the extended decl info is allocated.
832 if (!hasExtInfo()) {
833 // Save (non-extended) type source info pointer.
834 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
835 // Allocate external info struct.
836 DeclInfo = new (getASTContext()) ExtInfo;
837 // Restore savedTInfo into (extended) decl info.
838 getExtInfo()->TInfo = savedTInfo;
839 }
840 // Set qualifier info.
841 getExtInfo()->NNS = Qualifier;
842 getExtInfo()->NNSRange = QualifierRange;
843 }
844 else {
845 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
846 assert(QualifierRange.isInvalid());
847 if (hasExtInfo()) {
848 // Save type source info pointer.
849 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
850 // Deallocate the extended decl info.
851 getASTContext().Deallocate(getExtInfo());
852 // Restore savedTInfo into (non-extended) decl info.
853 DeclInfo = savedTInfo;
854 }
855 }
856}
857
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000858SourceLocation DeclaratorDecl::getOuterLocStart() const {
859 return getTemplateOrInnerLocStart(this);
860}
861
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000862void
Douglas Gregor20527e22010-06-15 17:44:38 +0000863QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
864 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000865 TemplateParameterList **TPLists) {
866 assert((NumTPLists == 0 || TPLists != 0) &&
867 "Empty array of template parameters with positive size!");
868 assert((NumTPLists == 0 || NNS) &&
869 "Nonempty array of template parameters with no qualifier!");
870
871 // Free previous template parameters (if any).
872 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000873 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000874 TemplParamLists = 0;
875 NumTemplParamLists = 0;
876 }
877 // Set info on matched template parameter lists (if any).
878 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000879 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000880 NumTemplParamLists = NumTPLists;
881 for (unsigned i = NumTPLists; i-- > 0; )
882 TemplParamLists[i] = TPLists[i];
883 }
884}
885
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000886//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000887// VarDecl Implementation
888//===----------------------------------------------------------------------===//
889
Sebastian Redl833ef452010-01-26 22:01:41 +0000890const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
891 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000892 case SC_None: break;
893 case SC_Auto: return "auto"; break;
894 case SC_Extern: return "extern"; break;
895 case SC_PrivateExtern: return "__private_extern__"; break;
896 case SC_Register: return "register"; break;
897 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000898 }
899
900 assert(0 && "Invalid storage class");
901 return 0;
902}
903
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000904VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000905 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000906 StorageClass S, StorageClass SCAsWritten) {
907 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000908}
909
Douglas Gregorbf62d642010-12-06 18:36:25 +0000910void VarDecl::setStorageClass(StorageClass SC) {
911 assert(isLegalForVariable(SC));
912 if (getStorageClass() != SC)
913 ClearLinkageCache();
914
915 SClass = SC;
916}
917
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000918SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000919 SourceLocation Start = getTypeSpecStartLoc();
920 if (Start.isInvalid())
921 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000922 return Start;
923}
924
925SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000926 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000927 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
928 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000929}
930
Sebastian Redl833ef452010-01-26 22:01:41 +0000931bool VarDecl::isExternC() const {
932 ASTContext &Context = getASTContext();
933 if (!Context.getLangOptions().CPlusPlus)
934 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000935 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000936 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
937
938 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
939 DC = DC->getParent()) {
940 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
941 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000942 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000943
944 break;
945 }
946
947 if (DC->isFunctionOrMethod())
948 return false;
949 }
950
951 return false;
952}
953
954VarDecl *VarDecl::getCanonicalDecl() {
955 return getFirstDeclaration();
956}
957
Sebastian Redl35351a92010-01-31 22:27:38 +0000958VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
959 // C++ [basic.def]p2:
960 // A declaration is a definition unless [...] it contains the 'extern'
961 // specifier or a linkage-specification and neither an initializer [...],
962 // it declares a static data member in a class declaration [...].
963 // C++ [temp.expl.spec]p15:
964 // An explicit specialization of a static data member of a template is a
965 // definition if the declaration includes an initializer; otherwise, it is
966 // a declaration.
967 if (isStaticDataMember()) {
968 if (isOutOfLine() && (hasInit() ||
969 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
970 return Definition;
971 else
972 return DeclarationOnly;
973 }
974 // C99 6.7p5:
975 // A definition of an identifier is a declaration for that identifier that
976 // [...] causes storage to be reserved for that object.
977 // Note: that applies for all non-file-scope objects.
978 // C99 6.9.2p1:
979 // If the declaration of an identifier for an object has file scope and an
980 // initializer, the declaration is an external definition for the identifier
981 if (hasInit())
982 return Definition;
983 // AST for 'extern "C" int foo;' is annotated with 'extern'.
984 if (hasExternalStorage())
985 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000986
John McCall8e7d6562010-08-26 03:08:43 +0000987 if (getStorageClassAsWritten() == SC_Extern ||
988 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000989 for (const VarDecl *PrevVar = getPreviousDeclaration();
990 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
991 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
992 return DeclarationOnly;
993 }
994 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000995 // C99 6.9.2p2:
996 // A declaration of an object that has file scope without an initializer,
997 // and without a storage class specifier or the scs 'static', constitutes
998 // a tentative definition.
999 // No such thing in C++.
1000 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1001 return TentativeDefinition;
1002
1003 // What's left is (in C, block-scope) declarations without initializers or
1004 // external storage. These are definitions.
1005 return Definition;
1006}
1007
Sebastian Redl35351a92010-01-31 22:27:38 +00001008VarDecl *VarDecl::getActingDefinition() {
1009 DefinitionKind Kind = isThisDeclarationADefinition();
1010 if (Kind != TentativeDefinition)
1011 return 0;
1012
Chris Lattner48eb14d2010-06-14 18:31:46 +00001013 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001014 VarDecl *First = getFirstDeclaration();
1015 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1016 I != E; ++I) {
1017 Kind = (*I)->isThisDeclarationADefinition();
1018 if (Kind == Definition)
1019 return 0;
1020 else if (Kind == TentativeDefinition)
1021 LastTentative = *I;
1022 }
1023 return LastTentative;
1024}
1025
1026bool VarDecl::isTentativeDefinitionNow() const {
1027 DefinitionKind Kind = isThisDeclarationADefinition();
1028 if (Kind != TentativeDefinition)
1029 return false;
1030
1031 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1032 if ((*I)->isThisDeclarationADefinition() == Definition)
1033 return false;
1034 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001035 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001036}
1037
Sebastian Redl5ca79842010-02-01 20:16:42 +00001038VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001039 VarDecl *First = getFirstDeclaration();
1040 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1041 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001042 if ((*I)->isThisDeclarationADefinition() == Definition)
1043 return *I;
1044 }
1045 return 0;
1046}
1047
John McCall37bb6c92010-10-29 22:22:43 +00001048VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1049 DefinitionKind Kind = DeclarationOnly;
1050
1051 const VarDecl *First = getFirstDeclaration();
1052 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1053 I != E; ++I)
1054 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1055
1056 return Kind;
1057}
1058
Sebastian Redl5ca79842010-02-01 20:16:42 +00001059const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001060 redecl_iterator I = redecls_begin(), E = redecls_end();
1061 while (I != E && !I->getInit())
1062 ++I;
1063
1064 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001065 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001066 return I->getInit();
1067 }
1068 return 0;
1069}
1070
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001071bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001072 if (Decl::isOutOfLine())
1073 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001074
1075 if (!isStaticDataMember())
1076 return false;
1077
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001078 // If this static data member was instantiated from a static data member of
1079 // a class template, check whether that static data member was defined
1080 // out-of-line.
1081 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1082 return VD->isOutOfLine();
1083
1084 return false;
1085}
1086
Douglas Gregor1d957a32009-10-27 18:42:08 +00001087VarDecl *VarDecl::getOutOfLineDefinition() {
1088 if (!isStaticDataMember())
1089 return 0;
1090
1091 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1092 RD != RDEnd; ++RD) {
1093 if (RD->getLexicalDeclContext()->isFileContext())
1094 return *RD;
1095 }
1096
1097 return 0;
1098}
1099
Douglas Gregord5058122010-02-11 01:19:42 +00001100void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001101 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1102 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001103 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001104 }
1105
1106 Init = I;
1107}
1108
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001109VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001110 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001111 return cast<VarDecl>(MSI->getInstantiatedFrom());
1112
1113 return 0;
1114}
1115
Douglas Gregor3c74d412009-10-14 20:14:33 +00001116TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001117 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001118 return MSI->getTemplateSpecializationKind();
1119
1120 return TSK_Undeclared;
1121}
1122
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001123MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001124 return getASTContext().getInstantiatedFromStaticDataMember(this);
1125}
1126
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001127void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1128 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001129 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001130 assert(MSI && "Not an instantiated static data member?");
1131 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001132 if (TSK != TSK_ExplicitSpecialization &&
1133 PointOfInstantiation.isValid() &&
1134 MSI->getPointOfInstantiation().isInvalid())
1135 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001136}
1137
Sebastian Redl833ef452010-01-26 22:01:41 +00001138//===----------------------------------------------------------------------===//
1139// ParmVarDecl Implementation
1140//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001141
Sebastian Redl833ef452010-01-26 22:01:41 +00001142ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1143 SourceLocation L, IdentifierInfo *Id,
1144 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001145 StorageClass S, StorageClass SCAsWritten,
1146 Expr *DefArg) {
1147 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1148 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001149}
1150
Sebastian Redl833ef452010-01-26 22:01:41 +00001151Expr *ParmVarDecl::getDefaultArg() {
1152 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1153 assert(!hasUninstantiatedDefaultArg() &&
1154 "Default argument is not yet instantiated!");
1155
1156 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001157 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001158 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001159
Sebastian Redl833ef452010-01-26 22:01:41 +00001160 return Arg;
1161}
1162
1163unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001164 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001165 return E->getNumTemporaries();
1166
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001167 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001168}
1169
Sebastian Redl833ef452010-01-26 22:01:41 +00001170CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1171 assert(getNumDefaultArgTemporaries() &&
1172 "Default arguments does not have any temporaries!");
1173
John McCall5d413782010-12-06 08:20:24 +00001174 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001175 return E->getTemporary(i);
1176}
1177
1178SourceRange ParmVarDecl::getDefaultArgRange() const {
1179 if (const Expr *E = getInit())
1180 return E->getSourceRange();
1181
1182 if (hasUninstantiatedDefaultArg())
1183 return getUninstantiatedDefaultArg()->getSourceRange();
1184
1185 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001186}
1187
Nuno Lopes394ec982008-12-17 23:39:55 +00001188//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001189// FunctionDecl Implementation
1190//===----------------------------------------------------------------------===//
1191
John McCalle1f2ec22009-09-11 06:45:03 +00001192void FunctionDecl::getNameForDiagnostic(std::string &S,
1193 const PrintingPolicy &Policy,
1194 bool Qualified) const {
1195 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1196 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1197 if (TemplateArgs)
1198 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001199 TemplateArgs->data(),
1200 TemplateArgs->size(),
John McCalle1f2ec22009-09-11 06:45:03 +00001201 Policy);
1202
1203}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001204
Ted Kremenek186a0742010-04-29 16:49:01 +00001205bool FunctionDecl::isVariadic() const {
1206 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1207 return FT->isVariadic();
1208 return false;
1209}
1210
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001211bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1212 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1213 if (I->Body) {
1214 Definition = *I;
1215 return true;
1216 }
1217 }
1218
1219 return false;
1220}
1221
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001222Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001223 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1224 if (I->Body) {
1225 Definition = *I;
1226 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001227 }
1228 }
1229
1230 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001231}
1232
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001233void FunctionDecl::setBody(Stmt *B) {
1234 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001235 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001236 EndRangeLoc = B->getLocEnd();
1237}
1238
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001239void FunctionDecl::setPure(bool P) {
1240 IsPure = P;
1241 if (P)
1242 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1243 Parent->markedVirtualFunctionPure();
1244}
1245
Douglas Gregor16618f22009-09-12 00:17:51 +00001246bool FunctionDecl::isMain() const {
1247 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001248 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001249 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001250 getIdentifier() && getIdentifier()->isStr("main");
1251}
1252
Douglas Gregor16618f22009-09-12 00:17:51 +00001253bool FunctionDecl::isExternC() const {
1254 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001255 // In C, any non-static, non-overloadable function has external
1256 // linkage.
1257 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001258 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001259
Mike Stump11289f42009-09-09 15:08:12 +00001260 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001261 DC = DC->getParent()) {
1262 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1263 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001264 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001265 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001266
1267 break;
1268 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001269
1270 if (DC->isRecord())
1271 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001272 }
1273
Douglas Gregorbff62032010-10-21 16:57:46 +00001274 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001275}
1276
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001277bool FunctionDecl::isGlobal() const {
1278 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1279 return Method->isStatic();
1280
John McCall8e7d6562010-08-26 03:08:43 +00001281 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001282 return false;
1283
Mike Stump11289f42009-09-09 15:08:12 +00001284 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001285 DC->isNamespace();
1286 DC = DC->getParent()) {
1287 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1288 if (!Namespace->getDeclName())
1289 return false;
1290 break;
1291 }
1292 }
1293
1294 return true;
1295}
1296
Sebastian Redl833ef452010-01-26 22:01:41 +00001297void
1298FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1299 redeclarable_base::setPreviousDeclaration(PrevDecl);
1300
1301 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1302 FunctionTemplateDecl *PrevFunTmpl
1303 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1304 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1305 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1306 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001307
1308 if (PrevDecl->IsInline)
1309 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001310}
1311
1312const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1313 return getFirstDeclaration();
1314}
1315
1316FunctionDecl *FunctionDecl::getCanonicalDecl() {
1317 return getFirstDeclaration();
1318}
1319
Douglas Gregorbf62d642010-12-06 18:36:25 +00001320void FunctionDecl::setStorageClass(StorageClass SC) {
1321 assert(isLegalForFunction(SC));
1322 if (getStorageClass() != SC)
1323 ClearLinkageCache();
1324
1325 SClass = SC;
1326}
1327
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001328/// \brief Returns a value indicating whether this function
1329/// corresponds to a builtin function.
1330///
1331/// The function corresponds to a built-in function if it is
1332/// declared at translation scope or within an extern "C" block and
1333/// its name matches with the name of a builtin. The returned value
1334/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001335/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001336/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001337unsigned FunctionDecl::getBuiltinID() const {
1338 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001339 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1340 return 0;
1341
1342 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1343 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1344 return BuiltinID;
1345
1346 // This function has the name of a known C library
1347 // function. Determine whether it actually refers to the C library
1348 // function or whether it just has the same name.
1349
Douglas Gregora908e7f2009-02-17 03:23:10 +00001350 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001351 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001352 return 0;
1353
Douglas Gregore711f702009-02-14 18:57:46 +00001354 // If this function is at translation-unit scope and we're not in
1355 // C++, it refers to the C library function.
1356 if (!Context.getLangOptions().CPlusPlus &&
1357 getDeclContext()->isTranslationUnit())
1358 return BuiltinID;
1359
1360 // If the function is in an extern "C" linkage specification and is
1361 // not marked "overloadable", it's the real function.
1362 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001363 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001364 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001365 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001366 return BuiltinID;
1367
1368 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001369 return 0;
1370}
1371
1372
Chris Lattner47c0d002009-04-25 06:03:53 +00001373/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001374/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001375/// after it has been created.
1376unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001377 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001378 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001379 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001380 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001381
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001382}
1383
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001384void FunctionDecl::setParams(ASTContext &C,
1385 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001386 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001387 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001388
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001389 // Zero params -> null pointer.
1390 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001391 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001392 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001393 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001394
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001395 // Update source range. The check below allows us to set EndRangeLoc before
1396 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001397 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001398 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001399 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001400}
Chris Lattner41943152007-01-25 04:52:46 +00001401
Chris Lattner58258242008-04-10 02:22:51 +00001402/// getMinRequiredArguments - Returns the minimum number of arguments
1403/// needed to call this function. This may be fewer than the number of
1404/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001405/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001406unsigned FunctionDecl::getMinRequiredArguments() const {
1407 unsigned NumRequiredArgs = getNumParams();
1408 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001409 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001410 --NumRequiredArgs;
1411
1412 return NumRequiredArgs;
1413}
1414
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001415bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001416 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001417 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001418
1419 if (isa<CXXMethodDecl>(this)) {
1420 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1421 return true;
1422 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001423
1424 switch (getTemplateSpecializationKind()) {
1425 case TSK_Undeclared:
1426 case TSK_ExplicitSpecialization:
1427 return false;
1428
1429 case TSK_ImplicitInstantiation:
1430 case TSK_ExplicitInstantiationDeclaration:
1431 case TSK_ExplicitInstantiationDefinition:
1432 // Handle below.
1433 break;
1434 }
1435
1436 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001437 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001438 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001439 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001440
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001441 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001442 return PatternDecl->isInlined();
1443
1444 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001445}
1446
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001447/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001448/// definition will be externally visible.
1449///
1450/// Inline function definitions are always available for inlining optimizations.
1451/// However, depending on the language dialect, declaration specifiers, and
1452/// attributes, the definition of an inline function may or may not be
1453/// "externally" visible to other translation units in the program.
1454///
1455/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001456/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001457/// inline definition becomes externally visible (C99 6.7.4p6).
1458///
1459/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1460/// definition, we use the GNU semantics for inline, which are nearly the
1461/// opposite of C99 semantics. In particular, "inline" by itself will create
1462/// an externally visible symbol, but "extern inline" will not create an
1463/// externally visible symbol.
1464bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1465 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001466 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001467 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001468
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001469 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001470 // If it's not the case that both 'inline' and 'extern' are
1471 // specified on the definition, then this inline definition is
1472 // externally visible.
1473 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1474 return true;
1475
1476 // If any declaration is 'inline' but not 'extern', then this definition
1477 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001478 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1479 Redecl != RedeclEnd;
1480 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001481 if (Redecl->isInlineSpecified() &&
1482 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001483 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001484 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001485
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001486 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001487 }
1488
1489 // C99 6.7.4p6:
1490 // [...] If all of the file scope declarations for a function in a
1491 // translation unit include the inline function specifier without extern,
1492 // then the definition in that translation unit is an inline definition.
1493 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1494 Redecl != RedeclEnd;
1495 ++Redecl) {
1496 // Only consider file-scope declarations in this test.
1497 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1498 continue;
1499
John McCall8e7d6562010-08-26 03:08:43 +00001500 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001501 return true; // Not an inline definition
1502 }
1503
1504 // C99 6.7.4p6:
1505 // An inline definition does not provide an external definition for the
1506 // function, and does not forbid an external definition in another
1507 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001508 return false;
1509}
1510
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001511/// getOverloadedOperator - Which C++ overloaded operator this
1512/// function represents, if any.
1513OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001514 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1515 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001516 else
1517 return OO_None;
1518}
1519
Alexis Huntc88db062010-01-13 09:01:02 +00001520/// getLiteralIdentifier - The literal suffix identifier this function
1521/// represents, if any.
1522const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1523 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1524 return getDeclName().getCXXLiteralIdentifier();
1525 else
1526 return 0;
1527}
1528
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001529FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1530 if (TemplateOrSpecialization.isNull())
1531 return TK_NonTemplate;
1532 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1533 return TK_FunctionTemplate;
1534 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1535 return TK_MemberSpecialization;
1536 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1537 return TK_FunctionTemplateSpecialization;
1538 if (TemplateOrSpecialization.is
1539 <DependentFunctionTemplateSpecializationInfo*>())
1540 return TK_DependentFunctionTemplateSpecialization;
1541
1542 assert(false && "Did we miss a TemplateOrSpecialization type?");
1543 return TK_NonTemplate;
1544}
1545
Douglas Gregord801b062009-10-07 23:56:10 +00001546FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001547 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001548 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1549
1550 return 0;
1551}
1552
Douglas Gregor06db9f52009-10-12 20:18:28 +00001553MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1554 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1555}
1556
Douglas Gregord801b062009-10-07 23:56:10 +00001557void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001558FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1559 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001560 TemplateSpecializationKind TSK) {
1561 assert(TemplateOrSpecialization.isNull() &&
1562 "Member function is already a specialization");
1563 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001564 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001565 TemplateOrSpecialization = Info;
1566}
1567
Douglas Gregorafca3b42009-10-27 20:53:28 +00001568bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001569 // If the function is invalid, it can't be implicitly instantiated.
1570 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001571 return false;
1572
1573 switch (getTemplateSpecializationKind()) {
1574 case TSK_Undeclared:
1575 case TSK_ExplicitSpecialization:
1576 case TSK_ExplicitInstantiationDefinition:
1577 return false;
1578
1579 case TSK_ImplicitInstantiation:
1580 return true;
1581
1582 case TSK_ExplicitInstantiationDeclaration:
1583 // Handled below.
1584 break;
1585 }
1586
1587 // Find the actual template from which we will instantiate.
1588 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001589 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001590 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001591 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001592
1593 // C++0x [temp.explicit]p9:
1594 // Except for inline functions, other explicit instantiation declarations
1595 // have the effect of suppressing the implicit instantiation of the entity
1596 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001597 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001598 return true;
1599
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001600 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001601}
1602
1603FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1604 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1605 while (Primary->getInstantiatedFromMemberTemplate()) {
1606 // If we have hit a point where the user provided a specialization of
1607 // this template, we're done looking.
1608 if (Primary->isMemberSpecialization())
1609 break;
1610
1611 Primary = Primary->getInstantiatedFromMemberTemplate();
1612 }
1613
1614 return Primary->getTemplatedDecl();
1615 }
1616
1617 return getInstantiatedFromMemberFunction();
1618}
1619
Douglas Gregor70d83e22009-06-29 17:30:29 +00001620FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001621 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001622 = TemplateOrSpecialization
1623 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001624 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001625 }
1626 return 0;
1627}
1628
1629const TemplateArgumentList *
1630FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001631 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001632 = TemplateOrSpecialization
1633 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001634 return Info->TemplateArguments;
1635 }
1636 return 0;
1637}
1638
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001639const TemplateArgumentListInfo *
1640FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1641 if (FunctionTemplateSpecializationInfo *Info
1642 = TemplateOrSpecialization
1643 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1644 return Info->TemplateArgumentsAsWritten;
1645 }
1646 return 0;
1647}
1648
Mike Stump11289f42009-09-09 15:08:12 +00001649void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001650FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1651 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001652 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001653 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001654 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001655 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1656 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001657 assert(TSK != TSK_Undeclared &&
1658 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001659 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001660 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001661 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001662 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1663 TemplateArgs,
1664 TemplateArgsAsWritten,
1665 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001666 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001667
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001668 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001669 // function template specializations.
1670 if (InsertPos)
1671 Template->getSpecializations().InsertNode(Info, InsertPos);
1672 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001673 // Try to insert the new node. If there is an existing node, leave it, the
1674 // set will contain the canonical decls while
1675 // FunctionTemplateDecl::findSpecialization will return
1676 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001677 FunctionTemplateSpecializationInfo *Existing
1678 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001679 (void)Existing;
1680 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1681 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001682 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001683}
1684
John McCallb9c78482010-04-08 09:05:18 +00001685void
1686FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1687 const UnresolvedSetImpl &Templates,
1688 const TemplateArgumentListInfo &TemplateArgs) {
1689 assert(TemplateOrSpecialization.isNull());
1690 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1691 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001692 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001693 void *Buffer = Context.Allocate(Size);
1694 DependentFunctionTemplateSpecializationInfo *Info =
1695 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1696 TemplateArgs);
1697 TemplateOrSpecialization = Info;
1698}
1699
1700DependentFunctionTemplateSpecializationInfo::
1701DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1702 const TemplateArgumentListInfo &TArgs)
1703 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1704
1705 d.NumTemplates = Ts.size();
1706 d.NumArgs = TArgs.size();
1707
1708 FunctionTemplateDecl **TsArray =
1709 const_cast<FunctionTemplateDecl**>(getTemplates());
1710 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1711 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1712
1713 TemplateArgumentLoc *ArgsArray =
1714 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1715 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1716 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1717}
1718
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001719TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001720 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001721 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001722 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001723 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001724 if (FTSInfo)
1725 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001726
Douglas Gregord801b062009-10-07 23:56:10 +00001727 MemberSpecializationInfo *MSInfo
1728 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1729 if (MSInfo)
1730 return MSInfo->getTemplateSpecializationKind();
1731
1732 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001733}
1734
Mike Stump11289f42009-09-09 15:08:12 +00001735void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001736FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1737 SourceLocation PointOfInstantiation) {
1738 if (FunctionTemplateSpecializationInfo *FTSInfo
1739 = TemplateOrSpecialization.dyn_cast<
1740 FunctionTemplateSpecializationInfo*>()) {
1741 FTSInfo->setTemplateSpecializationKind(TSK);
1742 if (TSK != TSK_ExplicitSpecialization &&
1743 PointOfInstantiation.isValid() &&
1744 FTSInfo->getPointOfInstantiation().isInvalid())
1745 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1746 } else if (MemberSpecializationInfo *MSInfo
1747 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1748 MSInfo->setTemplateSpecializationKind(TSK);
1749 if (TSK != TSK_ExplicitSpecialization &&
1750 PointOfInstantiation.isValid() &&
1751 MSInfo->getPointOfInstantiation().isInvalid())
1752 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1753 } else
1754 assert(false && "Function cannot have a template specialization kind");
1755}
1756
1757SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001758 if (FunctionTemplateSpecializationInfo *FTSInfo
1759 = TemplateOrSpecialization.dyn_cast<
1760 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001761 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001762 else if (MemberSpecializationInfo *MSInfo
1763 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001764 return MSInfo->getPointOfInstantiation();
1765
1766 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001767}
1768
Douglas Gregor6411b922009-09-11 20:15:17 +00001769bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001770 if (Decl::isOutOfLine())
1771 return true;
1772
1773 // If this function was instantiated from a member function of a
1774 // class template, check whether that member function was defined out-of-line.
1775 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1776 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001777 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001778 return Definition->isOutOfLine();
1779 }
1780
1781 // If this function was instantiated from a function template,
1782 // check whether that function template was defined out-of-line.
1783 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1784 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001785 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001786 return Definition->isOutOfLine();
1787 }
1788
1789 return false;
1790}
1791
Chris Lattner59a25942008-03-31 00:36:02 +00001792//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001793// FieldDecl Implementation
1794//===----------------------------------------------------------------------===//
1795
1796FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1797 IdentifierInfo *Id, QualType T,
1798 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1799 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1800}
1801
1802bool FieldDecl::isAnonymousStructOrUnion() const {
1803 if (!isImplicit() || getDeclName())
1804 return false;
1805
1806 if (const RecordType *Record = getType()->getAs<RecordType>())
1807 return Record->getDecl()->isAnonymousStructOrUnion();
1808
1809 return false;
1810}
1811
1812//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001813// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001814//===----------------------------------------------------------------------===//
1815
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001816SourceLocation TagDecl::getOuterLocStart() const {
1817 return getTemplateOrInnerLocStart(this);
1818}
1819
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001820SourceRange TagDecl::getSourceRange() const {
1821 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001822 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001823}
1824
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001825TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001826 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001827}
1828
Douglas Gregora72a4e32010-05-19 18:39:18 +00001829void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1830 TypedefDeclOrQualifier = TDD;
1831 if (TypeForDecl)
1832 TypeForDecl->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001833 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00001834}
1835
Douglas Gregordee1be82009-01-17 00:42:38 +00001836void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001837 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001838
1839 if (isa<CXXRecordDecl>(this)) {
1840 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1841 struct CXXRecordDecl::DefinitionData *Data =
1842 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001843 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1844 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001845 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001846}
1847
1848void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001849 assert((!isa<CXXRecordDecl>(this) ||
1850 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1851 "definition completed but not started");
1852
Douglas Gregordee1be82009-01-17 00:42:38 +00001853 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001854 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001855
1856 if (ASTMutationListener *L = getASTMutationListener())
1857 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001858}
1859
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001860TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001861 if (isDefinition())
1862 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001863 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1864 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001865
1866 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001867 R != REnd; ++R)
1868 if (R->isDefinition())
1869 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001870
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001871 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001872}
1873
John McCall3e11ebe2010-03-15 10:12:16 +00001874void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1875 SourceRange QualifierRange) {
1876 if (Qualifier) {
1877 // Make sure the extended qualifier info is allocated.
1878 if (!hasExtInfo())
1879 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1880 // Set qualifier info.
1881 getExtInfo()->NNS = Qualifier;
1882 getExtInfo()->NNSRange = QualifierRange;
1883 }
1884 else {
1885 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1886 assert(QualifierRange.isInvalid());
1887 if (hasExtInfo()) {
1888 getASTContext().Deallocate(getExtInfo());
1889 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1890 }
1891 }
1892}
1893
Ted Kremenek21475702008-09-05 17:16:31 +00001894//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001895// EnumDecl Implementation
1896//===----------------------------------------------------------------------===//
1897
1898EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1899 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001900 EnumDecl *PrevDecl, bool IsScoped,
1901 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001902 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001903 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001904 C.getTypeDeclType(Enum, PrevDecl);
1905 return Enum;
1906}
1907
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001908EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001909 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001910 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001911}
1912
Douglas Gregord5058122010-02-11 01:19:42 +00001913void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001914 QualType NewPromotionType,
1915 unsigned NumPositiveBits,
1916 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001917 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001918 if (!IntegerType)
1919 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001920 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001921 setNumPositiveBits(NumPositiveBits);
1922 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001923 TagDecl::completeDefinition();
1924}
1925
1926//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001927// RecordDecl Implementation
1928//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001929
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001930RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001931 IdentifierInfo *Id, RecordDecl *PrevDecl,
1932 SourceLocation TKL)
1933 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001934 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001935 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001936 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001937 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001938 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001939}
1940
1941RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001942 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001943 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001944
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001945 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001946 C.getTypeDeclType(R, PrevDecl);
1947 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001948}
1949
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001950RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1951 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1952 SourceLocation());
1953}
1954
Douglas Gregordfcad112009-03-25 15:59:44 +00001955bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001956 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001957 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1958}
1959
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001960RecordDecl::field_iterator RecordDecl::field_begin() const {
1961 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1962 LoadFieldsFromExternalStorage();
1963
1964 return field_iterator(decl_iterator(FirstDecl));
1965}
1966
Douglas Gregor91f84212008-12-11 16:49:14 +00001967/// completeDefinition - Notes that the definition of this type is now
1968/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001969void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001970 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001971 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001972}
Steve Naroffcc321422007-03-26 23:09:51 +00001973
John McCall61925b02010-05-21 01:17:40 +00001974ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1975 // Force the decl chain to come into existence properly.
1976 if (!getNextDeclInContext()) getParent()->decls_begin();
1977
1978 assert(isAnonymousStructOrUnion());
1979 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1980 assert(D->getType()->isRecordType());
1981 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1982 return D;
1983}
1984
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001985void RecordDecl::LoadFieldsFromExternalStorage() const {
1986 ExternalASTSource *Source = getASTContext().getExternalSource();
1987 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1988
1989 // Notify that we have a RecordDecl doing some initialization.
1990 ExternalASTSource::Deserializing TheFields(Source);
1991
1992 llvm::SmallVector<Decl*, 64> Decls;
1993 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1994 return;
1995
1996#ifndef NDEBUG
1997 // Check that all decls we got were FieldDecls.
1998 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1999 assert(isa<FieldDecl>(Decls[i]));
2000#endif
2001
2002 LoadedFieldsFromExternalStorage = true;
2003
2004 if (Decls.empty())
2005 return;
2006
2007 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2008}
2009
Steve Naroff415d3d52008-10-08 17:01:13 +00002010//===----------------------------------------------------------------------===//
2011// BlockDecl Implementation
2012//===----------------------------------------------------------------------===//
2013
Douglas Gregord5058122010-02-11 01:19:42 +00002014void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002015 unsigned NParms) {
2016 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002017
Steve Naroffc4b30e52009-03-13 16:56:44 +00002018 // Zero params -> null pointer.
2019 if (NParms) {
2020 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002021 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002022 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2023 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2024 }
2025}
2026
2027unsigned BlockDecl::getNumParams() const {
2028 return NumParams;
2029}
Sebastian Redl833ef452010-01-26 22:01:41 +00002030
2031
2032//===----------------------------------------------------------------------===//
2033// Other Decl Allocation/Deallocation Method Implementations
2034//===----------------------------------------------------------------------===//
2035
2036TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2037 return new (C) TranslationUnitDecl(C);
2038}
2039
2040NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2041 SourceLocation L, IdentifierInfo *Id) {
2042 return new (C) NamespaceDecl(DC, L, Id);
2043}
2044
Douglas Gregor417e87c2010-10-27 19:49:05 +00002045NamespaceDecl *NamespaceDecl::getNextNamespace() {
2046 return dyn_cast_or_null<NamespaceDecl>(
2047 NextNamespace.get(getASTContext().getExternalSource()));
2048}
2049
Sebastian Redl833ef452010-01-26 22:01:41 +00002050ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2051 SourceLocation L, IdentifierInfo *Id, QualType T) {
2052 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2053}
2054
2055FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002056 const DeclarationNameInfo &NameInfo,
2057 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002058 StorageClass S, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002059 bool isInlineSpecified,
2060 bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002061 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002062 S, SCAsWritten, isInlineSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002063 New->HasWrittenPrototype = hasWrittenPrototype;
2064 return New;
2065}
2066
2067BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2068 return new (C) BlockDecl(DC, L);
2069}
2070
2071EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2072 SourceLocation L,
2073 IdentifierInfo *Id, QualType T,
2074 Expr *E, const llvm::APSInt &V) {
2075 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2076}
2077
Benjamin Kramer39593702010-11-21 14:11:41 +00002078IndirectFieldDecl *
2079IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2080 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2081 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002082 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2083}
2084
Douglas Gregorbe996932010-09-01 20:41:53 +00002085SourceRange EnumConstantDecl::getSourceRange() const {
2086 SourceLocation End = getLocation();
2087 if (Init)
2088 End = Init->getLocEnd();
2089 return SourceRange(getLocation(), End);
2090}
2091
Sebastian Redl833ef452010-01-26 22:01:41 +00002092TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2093 SourceLocation L, IdentifierInfo *Id,
2094 TypeSourceInfo *TInfo) {
2095 return new (C) TypedefDecl(DC, L, Id, TInfo);
2096}
2097
Sebastian Redl833ef452010-01-26 22:01:41 +00002098FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2099 SourceLocation L,
2100 StringLiteral *Str) {
2101 return new (C) FileScopeAsmDecl(DC, L, Str);
2102}