blob: 1861c8c9b6061419315b9694c32287f1d54ce4d5 [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 }
1307}
1308
1309const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1310 return getFirstDeclaration();
1311}
1312
1313FunctionDecl *FunctionDecl::getCanonicalDecl() {
1314 return getFirstDeclaration();
1315}
1316
Douglas Gregorbf62d642010-12-06 18:36:25 +00001317void FunctionDecl::setStorageClass(StorageClass SC) {
1318 assert(isLegalForFunction(SC));
1319 if (getStorageClass() != SC)
1320 ClearLinkageCache();
1321
1322 SClass = SC;
1323}
1324
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001325/// \brief Returns a value indicating whether this function
1326/// corresponds to a builtin function.
1327///
1328/// The function corresponds to a built-in function if it is
1329/// declared at translation scope or within an extern "C" block and
1330/// its name matches with the name of a builtin. The returned value
1331/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001332/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001333/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001334unsigned FunctionDecl::getBuiltinID() const {
1335 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001336 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1337 return 0;
1338
1339 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1340 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1341 return BuiltinID;
1342
1343 // This function has the name of a known C library
1344 // function. Determine whether it actually refers to the C library
1345 // function or whether it just has the same name.
1346
Douglas Gregora908e7f2009-02-17 03:23:10 +00001347 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001348 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001349 return 0;
1350
Douglas Gregore711f702009-02-14 18:57:46 +00001351 // If this function is at translation-unit scope and we're not in
1352 // C++, it refers to the C library function.
1353 if (!Context.getLangOptions().CPlusPlus &&
1354 getDeclContext()->isTranslationUnit())
1355 return BuiltinID;
1356
1357 // If the function is in an extern "C" linkage specification and is
1358 // not marked "overloadable", it's the real function.
1359 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001360 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001361 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001362 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001363 return BuiltinID;
1364
1365 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001366 return 0;
1367}
1368
1369
Chris Lattner47c0d002009-04-25 06:03:53 +00001370/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001371/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001372/// after it has been created.
1373unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001374 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001375 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001376 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001377 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001379}
1380
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001381void FunctionDecl::setParams(ASTContext &C,
1382 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001383 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001384 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001385
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001386 // Zero params -> null pointer.
1387 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001388 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001389 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001390 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001391
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001392 // Update source range. The check below allows us to set EndRangeLoc before
1393 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001394 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001395 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001396 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001397}
Chris Lattner41943152007-01-25 04:52:46 +00001398
Chris Lattner58258242008-04-10 02:22:51 +00001399/// getMinRequiredArguments - Returns the minimum number of arguments
1400/// needed to call this function. This may be fewer than the number of
1401/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001402/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001403unsigned FunctionDecl::getMinRequiredArguments() const {
1404 unsigned NumRequiredArgs = getNumParams();
1405 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001406 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001407 --NumRequiredArgs;
1408
1409 return NumRequiredArgs;
1410}
1411
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001412bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001413 // FIXME: This is not enough. Consider:
1414 //
1415 // inline void f();
1416 // void f() { }
1417 //
1418 // f is inlined, but does not have inline specified.
1419 // To fix this we should add an 'inline' flag to FunctionDecl.
1420 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001421 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001422
1423 if (isa<CXXMethodDecl>(this)) {
1424 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1425 return true;
1426 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001427
1428 switch (getTemplateSpecializationKind()) {
1429 case TSK_Undeclared:
1430 case TSK_ExplicitSpecialization:
1431 return false;
1432
1433 case TSK_ImplicitInstantiation:
1434 case TSK_ExplicitInstantiationDeclaration:
1435 case TSK_ExplicitInstantiationDefinition:
1436 // Handle below.
1437 break;
1438 }
1439
1440 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001441 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001442 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001443 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001444
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001445 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001446 return PatternDecl->isInlined();
1447
1448 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001449}
1450
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001451/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001452/// definition will be externally visible.
1453///
1454/// Inline function definitions are always available for inlining optimizations.
1455/// However, depending on the language dialect, declaration specifiers, and
1456/// attributes, the definition of an inline function may or may not be
1457/// "externally" visible to other translation units in the program.
1458///
1459/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001460/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001461/// inline definition becomes externally visible (C99 6.7.4p6).
1462///
1463/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1464/// definition, we use the GNU semantics for inline, which are nearly the
1465/// opposite of C99 semantics. In particular, "inline" by itself will create
1466/// an externally visible symbol, but "extern inline" will not create an
1467/// externally visible symbol.
1468bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1469 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001470 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001471 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001472
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001473 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001474 // GNU inline semantics. Based on a number of examples, we came up with the
1475 // following heuristic: if the "inline" keyword is present on a
1476 // declaration of the function but "extern" is not present on that
1477 // declaration, then the symbol is externally visible. Otherwise, the GNU
1478 // "extern inline" semantics applies and the symbol is not externally
1479 // visible.
1480 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1481 Redecl != RedeclEnd;
1482 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001483 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001484 return true;
1485 }
1486
1487 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001488 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001489 }
1490
1491 // C99 6.7.4p6:
1492 // [...] If all of the file scope declarations for a function in a
1493 // translation unit include the inline function specifier without extern,
1494 // then the definition in that translation unit is an inline definition.
1495 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1496 Redecl != RedeclEnd;
1497 ++Redecl) {
1498 // Only consider file-scope declarations in this test.
1499 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1500 continue;
1501
John McCall8e7d6562010-08-26 03:08:43 +00001502 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001503 return true; // Not an inline definition
1504 }
1505
1506 // C99 6.7.4p6:
1507 // An inline definition does not provide an external definition for the
1508 // function, and does not forbid an external definition in another
1509 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001510 return false;
1511}
1512
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001513/// getOverloadedOperator - Which C++ overloaded operator this
1514/// function represents, if any.
1515OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001516 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1517 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001518 else
1519 return OO_None;
1520}
1521
Alexis Huntc88db062010-01-13 09:01:02 +00001522/// getLiteralIdentifier - The literal suffix identifier this function
1523/// represents, if any.
1524const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1525 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1526 return getDeclName().getCXXLiteralIdentifier();
1527 else
1528 return 0;
1529}
1530
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001531FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1532 if (TemplateOrSpecialization.isNull())
1533 return TK_NonTemplate;
1534 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1535 return TK_FunctionTemplate;
1536 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1537 return TK_MemberSpecialization;
1538 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1539 return TK_FunctionTemplateSpecialization;
1540 if (TemplateOrSpecialization.is
1541 <DependentFunctionTemplateSpecializationInfo*>())
1542 return TK_DependentFunctionTemplateSpecialization;
1543
1544 assert(false && "Did we miss a TemplateOrSpecialization type?");
1545 return TK_NonTemplate;
1546}
1547
Douglas Gregord801b062009-10-07 23:56:10 +00001548FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001549 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001550 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1551
1552 return 0;
1553}
1554
Douglas Gregor06db9f52009-10-12 20:18:28 +00001555MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1556 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1557}
1558
Douglas Gregord801b062009-10-07 23:56:10 +00001559void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001560FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1561 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001562 TemplateSpecializationKind TSK) {
1563 assert(TemplateOrSpecialization.isNull() &&
1564 "Member function is already a specialization");
1565 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001566 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001567 TemplateOrSpecialization = Info;
1568}
1569
Douglas Gregorafca3b42009-10-27 20:53:28 +00001570bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001571 // If the function is invalid, it can't be implicitly instantiated.
1572 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001573 return false;
1574
1575 switch (getTemplateSpecializationKind()) {
1576 case TSK_Undeclared:
1577 case TSK_ExplicitSpecialization:
1578 case TSK_ExplicitInstantiationDefinition:
1579 return false;
1580
1581 case TSK_ImplicitInstantiation:
1582 return true;
1583
1584 case TSK_ExplicitInstantiationDeclaration:
1585 // Handled below.
1586 break;
1587 }
1588
1589 // Find the actual template from which we will instantiate.
1590 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001591 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001592 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001593 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001594
1595 // C++0x [temp.explicit]p9:
1596 // Except for inline functions, other explicit instantiation declarations
1597 // have the effect of suppressing the implicit instantiation of the entity
1598 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001599 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001600 return true;
1601
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001602 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001603}
1604
1605FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1606 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1607 while (Primary->getInstantiatedFromMemberTemplate()) {
1608 // If we have hit a point where the user provided a specialization of
1609 // this template, we're done looking.
1610 if (Primary->isMemberSpecialization())
1611 break;
1612
1613 Primary = Primary->getInstantiatedFromMemberTemplate();
1614 }
1615
1616 return Primary->getTemplatedDecl();
1617 }
1618
1619 return getInstantiatedFromMemberFunction();
1620}
1621
Douglas Gregor70d83e22009-06-29 17:30:29 +00001622FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001623 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001624 = TemplateOrSpecialization
1625 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001626 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001627 }
1628 return 0;
1629}
1630
1631const TemplateArgumentList *
1632FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001633 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001634 = TemplateOrSpecialization
1635 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001636 return Info->TemplateArguments;
1637 }
1638 return 0;
1639}
1640
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001641const TemplateArgumentListInfo *
1642FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1643 if (FunctionTemplateSpecializationInfo *Info
1644 = TemplateOrSpecialization
1645 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1646 return Info->TemplateArgumentsAsWritten;
1647 }
1648 return 0;
1649}
1650
Mike Stump11289f42009-09-09 15:08:12 +00001651void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001652FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1653 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001654 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001655 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001656 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001657 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1658 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001659 assert(TSK != TSK_Undeclared &&
1660 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001661 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001662 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001663 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001664 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1665 TemplateArgs,
1666 TemplateArgsAsWritten,
1667 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001668 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001669
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001670 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001671 // function template specializations.
1672 if (InsertPos)
1673 Template->getSpecializations().InsertNode(Info, InsertPos);
1674 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001675 // Try to insert the new node. If there is an existing node, leave it, the
1676 // set will contain the canonical decls while
1677 // FunctionTemplateDecl::findSpecialization will return
1678 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001679 FunctionTemplateSpecializationInfo *Existing
1680 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001681 (void)Existing;
1682 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1683 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001684 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001685}
1686
John McCallb9c78482010-04-08 09:05:18 +00001687void
1688FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1689 const UnresolvedSetImpl &Templates,
1690 const TemplateArgumentListInfo &TemplateArgs) {
1691 assert(TemplateOrSpecialization.isNull());
1692 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1693 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001694 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001695 void *Buffer = Context.Allocate(Size);
1696 DependentFunctionTemplateSpecializationInfo *Info =
1697 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1698 TemplateArgs);
1699 TemplateOrSpecialization = Info;
1700}
1701
1702DependentFunctionTemplateSpecializationInfo::
1703DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1704 const TemplateArgumentListInfo &TArgs)
1705 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1706
1707 d.NumTemplates = Ts.size();
1708 d.NumArgs = TArgs.size();
1709
1710 FunctionTemplateDecl **TsArray =
1711 const_cast<FunctionTemplateDecl**>(getTemplates());
1712 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1713 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1714
1715 TemplateArgumentLoc *ArgsArray =
1716 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1717 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1718 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1719}
1720
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001721TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001722 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001723 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001724 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001725 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001726 if (FTSInfo)
1727 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001728
Douglas Gregord801b062009-10-07 23:56:10 +00001729 MemberSpecializationInfo *MSInfo
1730 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1731 if (MSInfo)
1732 return MSInfo->getTemplateSpecializationKind();
1733
1734 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001735}
1736
Mike Stump11289f42009-09-09 15:08:12 +00001737void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001738FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1739 SourceLocation PointOfInstantiation) {
1740 if (FunctionTemplateSpecializationInfo *FTSInfo
1741 = TemplateOrSpecialization.dyn_cast<
1742 FunctionTemplateSpecializationInfo*>()) {
1743 FTSInfo->setTemplateSpecializationKind(TSK);
1744 if (TSK != TSK_ExplicitSpecialization &&
1745 PointOfInstantiation.isValid() &&
1746 FTSInfo->getPointOfInstantiation().isInvalid())
1747 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1748 } else if (MemberSpecializationInfo *MSInfo
1749 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1750 MSInfo->setTemplateSpecializationKind(TSK);
1751 if (TSK != TSK_ExplicitSpecialization &&
1752 PointOfInstantiation.isValid() &&
1753 MSInfo->getPointOfInstantiation().isInvalid())
1754 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1755 } else
1756 assert(false && "Function cannot have a template specialization kind");
1757}
1758
1759SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001760 if (FunctionTemplateSpecializationInfo *FTSInfo
1761 = TemplateOrSpecialization.dyn_cast<
1762 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001763 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001764 else if (MemberSpecializationInfo *MSInfo
1765 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001766 return MSInfo->getPointOfInstantiation();
1767
1768 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001769}
1770
Douglas Gregor6411b922009-09-11 20:15:17 +00001771bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001772 if (Decl::isOutOfLine())
1773 return true;
1774
1775 // If this function was instantiated from a member function of a
1776 // class template, check whether that member function was defined out-of-line.
1777 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1778 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001779 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001780 return Definition->isOutOfLine();
1781 }
1782
1783 // If this function was instantiated from a function template,
1784 // check whether that function template was defined out-of-line.
1785 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1786 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001787 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001788 return Definition->isOutOfLine();
1789 }
1790
1791 return false;
1792}
1793
Chris Lattner59a25942008-03-31 00:36:02 +00001794//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001795// FieldDecl Implementation
1796//===----------------------------------------------------------------------===//
1797
1798FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1799 IdentifierInfo *Id, QualType T,
1800 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1801 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1802}
1803
1804bool FieldDecl::isAnonymousStructOrUnion() const {
1805 if (!isImplicit() || getDeclName())
1806 return false;
1807
1808 if (const RecordType *Record = getType()->getAs<RecordType>())
1809 return Record->getDecl()->isAnonymousStructOrUnion();
1810
1811 return false;
1812}
1813
1814//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001815// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001816//===----------------------------------------------------------------------===//
1817
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001818SourceLocation TagDecl::getOuterLocStart() const {
1819 return getTemplateOrInnerLocStart(this);
1820}
1821
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001822SourceRange TagDecl::getSourceRange() const {
1823 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001824 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001825}
1826
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001827TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001828 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001829}
1830
Douglas Gregora72a4e32010-05-19 18:39:18 +00001831void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1832 TypedefDeclOrQualifier = TDD;
1833 if (TypeForDecl)
1834 TypeForDecl->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001835 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00001836}
1837
Douglas Gregordee1be82009-01-17 00:42:38 +00001838void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001839 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001840
1841 if (isa<CXXRecordDecl>(this)) {
1842 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1843 struct CXXRecordDecl::DefinitionData *Data =
1844 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001845 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1846 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001847 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001848}
1849
1850void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001851 assert((!isa<CXXRecordDecl>(this) ||
1852 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1853 "definition completed but not started");
1854
Douglas Gregordee1be82009-01-17 00:42:38 +00001855 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001856 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001857
1858 if (ASTMutationListener *L = getASTMutationListener())
1859 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001860}
1861
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001862TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001863 if (isDefinition())
1864 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001865 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1866 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001867
1868 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001869 R != REnd; ++R)
1870 if (R->isDefinition())
1871 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001872
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001873 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001874}
1875
John McCall3e11ebe2010-03-15 10:12:16 +00001876void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1877 SourceRange QualifierRange) {
1878 if (Qualifier) {
1879 // Make sure the extended qualifier info is allocated.
1880 if (!hasExtInfo())
1881 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1882 // Set qualifier info.
1883 getExtInfo()->NNS = Qualifier;
1884 getExtInfo()->NNSRange = QualifierRange;
1885 }
1886 else {
1887 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1888 assert(QualifierRange.isInvalid());
1889 if (hasExtInfo()) {
1890 getASTContext().Deallocate(getExtInfo());
1891 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1892 }
1893 }
1894}
1895
Ted Kremenek21475702008-09-05 17:16:31 +00001896//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001897// EnumDecl Implementation
1898//===----------------------------------------------------------------------===//
1899
1900EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1901 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001902 EnumDecl *PrevDecl, bool IsScoped,
1903 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001904 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001905 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001906 C.getTypeDeclType(Enum, PrevDecl);
1907 return Enum;
1908}
1909
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001910EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001911 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001912 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001913}
1914
Douglas Gregord5058122010-02-11 01:19:42 +00001915void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001916 QualType NewPromotionType,
1917 unsigned NumPositiveBits,
1918 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001919 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001920 if (!IntegerType)
1921 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001922 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001923 setNumPositiveBits(NumPositiveBits);
1924 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001925 TagDecl::completeDefinition();
1926}
1927
1928//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001929// RecordDecl Implementation
1930//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001931
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001932RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001933 IdentifierInfo *Id, RecordDecl *PrevDecl,
1934 SourceLocation TKL)
1935 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001936 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001937 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001938 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001939 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001940 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001941}
1942
1943RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001944 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001945 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001946
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001947 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001948 C.getTypeDeclType(R, PrevDecl);
1949 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001950}
1951
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001952RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1953 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1954 SourceLocation());
1955}
1956
Douglas Gregordfcad112009-03-25 15:59:44 +00001957bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001958 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001959 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1960}
1961
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001962RecordDecl::field_iterator RecordDecl::field_begin() const {
1963 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1964 LoadFieldsFromExternalStorage();
1965
1966 return field_iterator(decl_iterator(FirstDecl));
1967}
1968
Douglas Gregor91f84212008-12-11 16:49:14 +00001969/// completeDefinition - Notes that the definition of this type is now
1970/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001971void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001972 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001973 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001974}
Steve Naroffcc321422007-03-26 23:09:51 +00001975
John McCall61925b02010-05-21 01:17:40 +00001976ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1977 // Force the decl chain to come into existence properly.
1978 if (!getNextDeclInContext()) getParent()->decls_begin();
1979
1980 assert(isAnonymousStructOrUnion());
1981 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1982 assert(D->getType()->isRecordType());
1983 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1984 return D;
1985}
1986
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001987void RecordDecl::LoadFieldsFromExternalStorage() const {
1988 ExternalASTSource *Source = getASTContext().getExternalSource();
1989 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1990
1991 // Notify that we have a RecordDecl doing some initialization.
1992 ExternalASTSource::Deserializing TheFields(Source);
1993
1994 llvm::SmallVector<Decl*, 64> Decls;
1995 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1996 return;
1997
1998#ifndef NDEBUG
1999 // Check that all decls we got were FieldDecls.
2000 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2001 assert(isa<FieldDecl>(Decls[i]));
2002#endif
2003
2004 LoadedFieldsFromExternalStorage = true;
2005
2006 if (Decls.empty())
2007 return;
2008
2009 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2010}
2011
Steve Naroff415d3d52008-10-08 17:01:13 +00002012//===----------------------------------------------------------------------===//
2013// BlockDecl Implementation
2014//===----------------------------------------------------------------------===//
2015
Douglas Gregord5058122010-02-11 01:19:42 +00002016void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002017 unsigned NParms) {
2018 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002019
Steve Naroffc4b30e52009-03-13 16:56:44 +00002020 // Zero params -> null pointer.
2021 if (NParms) {
2022 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002023 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002024 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2025 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2026 }
2027}
2028
2029unsigned BlockDecl::getNumParams() const {
2030 return NumParams;
2031}
Sebastian Redl833ef452010-01-26 22:01:41 +00002032
2033
2034//===----------------------------------------------------------------------===//
2035// Other Decl Allocation/Deallocation Method Implementations
2036//===----------------------------------------------------------------------===//
2037
2038TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2039 return new (C) TranslationUnitDecl(C);
2040}
2041
2042NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2043 SourceLocation L, IdentifierInfo *Id) {
2044 return new (C) NamespaceDecl(DC, L, Id);
2045}
2046
Douglas Gregor417e87c2010-10-27 19:49:05 +00002047NamespaceDecl *NamespaceDecl::getNextNamespace() {
2048 return dyn_cast_or_null<NamespaceDecl>(
2049 NextNamespace.get(getASTContext().getExternalSource()));
2050}
2051
Sebastian Redl833ef452010-01-26 22:01:41 +00002052ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2053 SourceLocation L, IdentifierInfo *Id, QualType T) {
2054 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2055}
2056
2057FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002058 const DeclarationNameInfo &NameInfo,
2059 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002060 StorageClass S, StorageClass SCAsWritten,
2061 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002062 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002063 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00002064 New->HasWrittenPrototype = hasWrittenPrototype;
2065 return New;
2066}
2067
2068BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2069 return new (C) BlockDecl(DC, L);
2070}
2071
2072EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2073 SourceLocation L,
2074 IdentifierInfo *Id, QualType T,
2075 Expr *E, const llvm::APSInt &V) {
2076 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2077}
2078
Benjamin Kramer39593702010-11-21 14:11:41 +00002079IndirectFieldDecl *
2080IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2081 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2082 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002083 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2084}
2085
Douglas Gregorbe996932010-09-01 20:41:53 +00002086SourceRange EnumConstantDecl::getSourceRange() const {
2087 SourceLocation End = getLocation();
2088 if (Init)
2089 End = Init->getLocEnd();
2090 return SourceRange(getLocation(), End);
2091}
2092
Sebastian Redl833ef452010-01-26 22:01:41 +00002093TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2094 SourceLocation L, IdentifierInfo *Id,
2095 TypeSourceInfo *TInfo) {
2096 return new (C) TypedefDecl(DC, L, Id, TInfo);
2097}
2098
Sebastian Redl833ef452010-01-26 22:01:41 +00002099FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2100 SourceLocation L,
2101 StringLiteral *Str) {
2102 return new (C) FileScopeAsmDecl(DC, L, Str);
2103}