blob: d4cffb9b68268996d07c023805f4b00fa9d959f2 [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) {
538#ifndef NDEBUG
539 assert(CachedLinkage == getLVForDecl(this,
540 LVFlags::CreateOnlyDeclLinkage()).linkage());
541#endif
542 return Linkage(CachedLinkage);
543 }
544
545 CachedLinkage = getLVForDecl(this,
546 LVFlags::CreateOnlyDeclLinkage()).linkage();
547 HasCachedLinkage = 1;
548 return Linkage(CachedLinkage);
549}
550
John McCallc273f242010-10-30 11:50:40 +0000551LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000552 LinkageInfo LI = getLVForDecl(this, LVFlags());
553 assert(!HasCachedLinkage || (CachedLinkage == LI.linkage()));
554 HasCachedLinkage = 1;
555 CachedLinkage = LI.linkage();
556 return LI;
John McCall033caa52010-10-29 00:29:13 +0000557}
Ted Kremenek926d8602010-04-20 23:15:35 +0000558
John McCall07072662010-11-02 01:45:15 +0000559static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000560 // Objective-C: treat all Objective-C declarations as having external
561 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000562 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000563 default:
564 break;
John McCall457a04e2010-10-22 21:05:15 +0000565 case Decl::TemplateTemplateParm: // count these as external
566 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000567 case Decl::ObjCAtDefsField:
568 case Decl::ObjCCategory:
569 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000570 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000571 case Decl::ObjCForwardProtocol:
572 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000573 case Decl::ObjCMethod:
574 case Decl::ObjCProperty:
575 case Decl::ObjCPropertyImpl:
576 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000577 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000578 }
579
Douglas Gregorf73b2822009-11-25 22:24:25 +0000580 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000581 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000582 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000583
584 // C++ [basic.link]p5:
585 // In addition, a member function, static data member, a named
586 // class or enumeration of class scope, or an unnamed class or
587 // enumeration defined in a class-scope typedef declaration such
588 // that the class or enumeration has the typedef name for linkage
589 // purposes (7.1.3), has external linkage if the name of the class
590 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000591 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000592 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000593
594 // C++ [basic.link]p6:
595 // The name of a function declared in block scope and the name of
596 // an object declared by a block scope extern declaration have
597 // linkage. If there is a visible declaration of an entity with
598 // linkage having the same name and type, ignoring entities
599 // declared outside the innermost enclosing namespace scope, the
600 // block scope declaration declares that same entity and receives
601 // the linkage of the previous declaration. If there is more than
602 // one such matching entity, the program is ill-formed. Otherwise,
603 // if no matching entity is found, the block scope entity receives
604 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000605 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
606 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000607 if (Function->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000608 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000609
John McCallc273f242010-10-30 11:50:40 +0000610 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000611 if (Flags.ConsiderVisibilityAttributes) {
612 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
613 LV.setVisibility(GetVisibilityFromAttr(VA));
614 }
615
John McCall457a04e2010-10-22 21:05:15 +0000616 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000617 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000618 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
619 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000620 }
621
622 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000623 }
624
John McCall033caa52010-10-29 00:29:13 +0000625 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000626 if (Var->getStorageClass() == SC_Extern ||
627 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000628 if (Var->isInAnonymousNamespace())
John McCallc273f242010-10-30 11:50:40 +0000629 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000630
John McCallc273f242010-10-30 11:50:40 +0000631 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000632 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000633 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000634 else if (Flags.ConsiderVisibilityAttributes) {
635 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
636 LV.setVisibility(GetVisibilityFromAttr(VA));
637 }
638
John McCall457a04e2010-10-22 21:05:15 +0000639 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000640 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000641 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
642 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000643 }
644
645 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000646 }
647 }
648
649 // C++ [basic.link]p6:
650 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000651 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000652}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000653
Douglas Gregor2ada0482009-02-04 17:27:36 +0000654std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000655 return getQualifiedNameAsString(getASTContext().getLangOptions());
656}
657
658std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000659 const DeclContext *Ctx = getDeclContext();
660
661 if (Ctx->isFunctionOrMethod())
662 return getNameAsString();
663
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000664 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
665 ContextsTy Contexts;
666
667 // Collect contexts.
668 while (Ctx && isa<NamedDecl>(Ctx)) {
669 Contexts.push_back(Ctx);
670 Ctx = Ctx->getParent();
671 };
672
673 std::string QualName;
674 llvm::raw_string_ostream OS(QualName);
675
676 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
677 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000678 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000679 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000680 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
681 std::string TemplateArgsStr
682 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000683 TemplateArgs.data(),
684 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000685 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000686 OS << Spec->getName() << TemplateArgsStr;
687 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000688 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000689 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000690 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000691 OS << ND;
692 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
693 if (!RD->getIdentifier())
694 OS << "<anonymous " << RD->getKindName() << '>';
695 else
696 OS << RD;
697 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000698 const FunctionProtoType *FT = 0;
699 if (FD->hasWrittenPrototype())
700 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
701
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000702 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000703 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000704 unsigned NumParams = FD->getNumParams();
705 for (unsigned i = 0; i < NumParams; ++i) {
706 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000707 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000708 std::string Param;
709 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000710 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000711 }
712
713 if (FT->isVariadic()) {
714 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000715 OS << ", ";
716 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000717 }
718 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000719 OS << ')';
720 } else {
721 OS << cast<NamedDecl>(*I);
722 }
723 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000724 }
725
John McCalla2a3f7d2010-03-16 21:48:18 +0000726 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000727 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000728 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000729 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000730
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000731 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000732}
733
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000734bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000735 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
736
Douglas Gregor889ceb72009-02-03 19:21:40 +0000737 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
738 // We want to keep it, unless it nominates same namespace.
739 if (getKind() == Decl::UsingDirective) {
740 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
741 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
742 }
Mike Stump11289f42009-09-09 15:08:12 +0000743
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000744 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
745 // For function declarations, we keep track of redeclarations.
746 return FD->getPreviousDeclaration() == OldD;
747
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000748 // For function templates, the underlying function declarations are linked.
749 if (const FunctionTemplateDecl *FunctionTemplate
750 = dyn_cast<FunctionTemplateDecl>(this))
751 if (const FunctionTemplateDecl *OldFunctionTemplate
752 = dyn_cast<FunctionTemplateDecl>(OldD))
753 return FunctionTemplate->getTemplatedDecl()
754 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000755
Steve Naroffc4173fa2009-02-22 19:35:57 +0000756 // For method declarations, we keep track of redeclarations.
757 if (isa<ObjCMethodDecl>(this))
758 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000759
John McCall9f3059a2009-10-09 21:13:30 +0000760 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
761 return true;
762
John McCall3f746822009-11-17 05:59:44 +0000763 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
764 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
765 cast<UsingShadowDecl>(OldD)->getTargetDecl();
766
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000767 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD))
768 return cast<UsingDecl>(this)->getTargetNestedNameDecl() ==
769 cast<UsingDecl>(OldD)->getTargetNestedNameDecl();
770
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000771 // For non-function declarations, if the declarations are of the
772 // same kind then this must be a redeclaration, or semantic analysis
773 // would not have given us the new declaration.
774 return this->getKind() == OldD->getKind();
775}
776
Douglas Gregoreddf4332009-02-24 20:03:32 +0000777bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000778 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000779}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000780
Anders Carlsson6915bf62009-06-26 06:29:23 +0000781NamedDecl *NamedDecl::getUnderlyingDecl() {
782 NamedDecl *ND = this;
783 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000784 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000785 ND = UD->getTargetDecl();
786 else if (ObjCCompatibleAliasDecl *AD
787 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
788 return AD->getClassInterface();
789 else
790 return ND;
791 }
792}
793
John McCalla8ae2222010-04-06 21:38:20 +0000794bool NamedDecl::isCXXInstanceMember() const {
795 assert(isCXXClassMember() &&
796 "checking whether non-member is instance member");
797
798 const NamedDecl *D = this;
799 if (isa<UsingShadowDecl>(D))
800 D = cast<UsingShadowDecl>(D)->getTargetDecl();
801
Francois Pichet783dd6e2010-11-21 06:08:52 +0000802 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000803 return true;
804 if (isa<CXXMethodDecl>(D))
805 return cast<CXXMethodDecl>(D)->isInstance();
806 if (isa<FunctionTemplateDecl>(D))
807 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
808 ->getTemplatedDecl())->isInstance();
809 return false;
810}
811
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000812//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000813// DeclaratorDecl Implementation
814//===----------------------------------------------------------------------===//
815
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000816template <typename DeclT>
817static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
818 if (decl->getNumTemplateParameterLists() > 0)
819 return decl->getTemplateParameterList(0)->getTemplateLoc();
820 else
821 return decl->getInnerLocStart();
822}
823
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000824SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000825 TypeSourceInfo *TSI = getTypeSourceInfo();
826 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000827 return SourceLocation();
828}
829
John McCall3e11ebe2010-03-15 10:12:16 +0000830void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
831 SourceRange QualifierRange) {
832 if (Qualifier) {
833 // Make sure the extended decl info is allocated.
834 if (!hasExtInfo()) {
835 // Save (non-extended) type source info pointer.
836 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
837 // Allocate external info struct.
838 DeclInfo = new (getASTContext()) ExtInfo;
839 // Restore savedTInfo into (extended) decl info.
840 getExtInfo()->TInfo = savedTInfo;
841 }
842 // Set qualifier info.
843 getExtInfo()->NNS = Qualifier;
844 getExtInfo()->NNSRange = QualifierRange;
845 }
846 else {
847 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
848 assert(QualifierRange.isInvalid());
849 if (hasExtInfo()) {
850 // Save type source info pointer.
851 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
852 // Deallocate the extended decl info.
853 getASTContext().Deallocate(getExtInfo());
854 // Restore savedTInfo into (non-extended) decl info.
855 DeclInfo = savedTInfo;
856 }
857 }
858}
859
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000860SourceLocation DeclaratorDecl::getOuterLocStart() const {
861 return getTemplateOrInnerLocStart(this);
862}
863
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000864void
Douglas Gregor20527e22010-06-15 17:44:38 +0000865QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
866 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000867 TemplateParameterList **TPLists) {
868 assert((NumTPLists == 0 || TPLists != 0) &&
869 "Empty array of template parameters with positive size!");
870 assert((NumTPLists == 0 || NNS) &&
871 "Nonempty array of template parameters with no qualifier!");
872
873 // Free previous template parameters (if any).
874 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000875 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000876 TemplParamLists = 0;
877 NumTemplParamLists = 0;
878 }
879 // Set info on matched template parameter lists (if any).
880 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000881 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000882 NumTemplParamLists = NumTPLists;
883 for (unsigned i = NumTPLists; i-- > 0; )
884 TemplParamLists[i] = TPLists[i];
885 }
886}
887
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000888//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000889// VarDecl Implementation
890//===----------------------------------------------------------------------===//
891
Sebastian Redl833ef452010-01-26 22:01:41 +0000892const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
893 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000894 case SC_None: break;
895 case SC_Auto: return "auto"; break;
896 case SC_Extern: return "extern"; break;
897 case SC_PrivateExtern: return "__private_extern__"; break;
898 case SC_Register: return "register"; break;
899 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000900 }
901
902 assert(0 && "Invalid storage class");
903 return 0;
904}
905
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000906VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000907 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000908 StorageClass S, StorageClass SCAsWritten) {
909 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000910}
911
Douglas Gregorbf62d642010-12-06 18:36:25 +0000912void VarDecl::setStorageClass(StorageClass SC) {
913 assert(isLegalForVariable(SC));
914 if (getStorageClass() != SC)
915 ClearLinkageCache();
916
917 SClass = SC;
918}
919
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000920SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000921 SourceLocation Start = getTypeSpecStartLoc();
922 if (Start.isInvalid())
923 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000924 return Start;
925}
926
927SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000928 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000929 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
930 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000931}
932
Sebastian Redl833ef452010-01-26 22:01:41 +0000933bool VarDecl::isExternC() const {
934 ASTContext &Context = getASTContext();
935 if (!Context.getLangOptions().CPlusPlus)
936 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000937 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000938 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
939
940 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
941 DC = DC->getParent()) {
942 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
943 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000944 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000945
946 break;
947 }
948
949 if (DC->isFunctionOrMethod())
950 return false;
951 }
952
953 return false;
954}
955
956VarDecl *VarDecl::getCanonicalDecl() {
957 return getFirstDeclaration();
958}
959
Sebastian Redl35351a92010-01-31 22:27:38 +0000960VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
961 // C++ [basic.def]p2:
962 // A declaration is a definition unless [...] it contains the 'extern'
963 // specifier or a linkage-specification and neither an initializer [...],
964 // it declares a static data member in a class declaration [...].
965 // C++ [temp.expl.spec]p15:
966 // An explicit specialization of a static data member of a template is a
967 // definition if the declaration includes an initializer; otherwise, it is
968 // a declaration.
969 if (isStaticDataMember()) {
970 if (isOutOfLine() && (hasInit() ||
971 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
972 return Definition;
973 else
974 return DeclarationOnly;
975 }
976 // C99 6.7p5:
977 // A definition of an identifier is a declaration for that identifier that
978 // [...] causes storage to be reserved for that object.
979 // Note: that applies for all non-file-scope objects.
980 // C99 6.9.2p1:
981 // If the declaration of an identifier for an object has file scope and an
982 // initializer, the declaration is an external definition for the identifier
983 if (hasInit())
984 return Definition;
985 // AST for 'extern "C" int foo;' is annotated with 'extern'.
986 if (hasExternalStorage())
987 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000988
John McCall8e7d6562010-08-26 03:08:43 +0000989 if (getStorageClassAsWritten() == SC_Extern ||
990 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000991 for (const VarDecl *PrevVar = getPreviousDeclaration();
992 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
993 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
994 return DeclarationOnly;
995 }
996 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000997 // C99 6.9.2p2:
998 // A declaration of an object that has file scope without an initializer,
999 // and without a storage class specifier or the scs 'static', constitutes
1000 // a tentative definition.
1001 // No such thing in C++.
1002 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1003 return TentativeDefinition;
1004
1005 // What's left is (in C, block-scope) declarations without initializers or
1006 // external storage. These are definitions.
1007 return Definition;
1008}
1009
Sebastian Redl35351a92010-01-31 22:27:38 +00001010VarDecl *VarDecl::getActingDefinition() {
1011 DefinitionKind Kind = isThisDeclarationADefinition();
1012 if (Kind != TentativeDefinition)
1013 return 0;
1014
Chris Lattner48eb14d2010-06-14 18:31:46 +00001015 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001016 VarDecl *First = getFirstDeclaration();
1017 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1018 I != E; ++I) {
1019 Kind = (*I)->isThisDeclarationADefinition();
1020 if (Kind == Definition)
1021 return 0;
1022 else if (Kind == TentativeDefinition)
1023 LastTentative = *I;
1024 }
1025 return LastTentative;
1026}
1027
1028bool VarDecl::isTentativeDefinitionNow() const {
1029 DefinitionKind Kind = isThisDeclarationADefinition();
1030 if (Kind != TentativeDefinition)
1031 return false;
1032
1033 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1034 if ((*I)->isThisDeclarationADefinition() == Definition)
1035 return false;
1036 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001037 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001038}
1039
Sebastian Redl5ca79842010-02-01 20:16:42 +00001040VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001041 VarDecl *First = getFirstDeclaration();
1042 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1043 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001044 if ((*I)->isThisDeclarationADefinition() == Definition)
1045 return *I;
1046 }
1047 return 0;
1048}
1049
John McCall37bb6c92010-10-29 22:22:43 +00001050VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1051 DefinitionKind Kind = DeclarationOnly;
1052
1053 const VarDecl *First = getFirstDeclaration();
1054 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1055 I != E; ++I)
1056 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1057
1058 return Kind;
1059}
1060
Sebastian Redl5ca79842010-02-01 20:16:42 +00001061const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001062 redecl_iterator I = redecls_begin(), E = redecls_end();
1063 while (I != E && !I->getInit())
1064 ++I;
1065
1066 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001067 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001068 return I->getInit();
1069 }
1070 return 0;
1071}
1072
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001073bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001074 if (Decl::isOutOfLine())
1075 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001076
1077 if (!isStaticDataMember())
1078 return false;
1079
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001080 // If this static data member was instantiated from a static data member of
1081 // a class template, check whether that static data member was defined
1082 // out-of-line.
1083 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1084 return VD->isOutOfLine();
1085
1086 return false;
1087}
1088
Douglas Gregor1d957a32009-10-27 18:42:08 +00001089VarDecl *VarDecl::getOutOfLineDefinition() {
1090 if (!isStaticDataMember())
1091 return 0;
1092
1093 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1094 RD != RDEnd; ++RD) {
1095 if (RD->getLexicalDeclContext()->isFileContext())
1096 return *RD;
1097 }
1098
1099 return 0;
1100}
1101
Douglas Gregord5058122010-02-11 01:19:42 +00001102void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001103 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1104 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001105 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001106 }
1107
1108 Init = I;
1109}
1110
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001111VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001112 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001113 return cast<VarDecl>(MSI->getInstantiatedFrom());
1114
1115 return 0;
1116}
1117
Douglas Gregor3c74d412009-10-14 20:14:33 +00001118TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001119 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001120 return MSI->getTemplateSpecializationKind();
1121
1122 return TSK_Undeclared;
1123}
1124
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001125MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001126 return getASTContext().getInstantiatedFromStaticDataMember(this);
1127}
1128
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001129void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1130 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001131 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001132 assert(MSI && "Not an instantiated static data member?");
1133 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001134 if (TSK != TSK_ExplicitSpecialization &&
1135 PointOfInstantiation.isValid() &&
1136 MSI->getPointOfInstantiation().isInvalid())
1137 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001138}
1139
Sebastian Redl833ef452010-01-26 22:01:41 +00001140//===----------------------------------------------------------------------===//
1141// ParmVarDecl Implementation
1142//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001143
Sebastian Redl833ef452010-01-26 22:01:41 +00001144ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1145 SourceLocation L, IdentifierInfo *Id,
1146 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001147 StorageClass S, StorageClass SCAsWritten,
1148 Expr *DefArg) {
1149 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1150 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001151}
1152
Sebastian Redl833ef452010-01-26 22:01:41 +00001153Expr *ParmVarDecl::getDefaultArg() {
1154 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1155 assert(!hasUninstantiatedDefaultArg() &&
1156 "Default argument is not yet instantiated!");
1157
1158 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001159 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001160 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001161
Sebastian Redl833ef452010-01-26 22:01:41 +00001162 return Arg;
1163}
1164
1165unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001166 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001167 return E->getNumTemporaries();
1168
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001169 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001170}
1171
Sebastian Redl833ef452010-01-26 22:01:41 +00001172CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1173 assert(getNumDefaultArgTemporaries() &&
1174 "Default arguments does not have any temporaries!");
1175
John McCall5d413782010-12-06 08:20:24 +00001176 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001177 return E->getTemporary(i);
1178}
1179
1180SourceRange ParmVarDecl::getDefaultArgRange() const {
1181 if (const Expr *E = getInit())
1182 return E->getSourceRange();
1183
1184 if (hasUninstantiatedDefaultArg())
1185 return getUninstantiatedDefaultArg()->getSourceRange();
1186
1187 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001188}
1189
Nuno Lopes394ec982008-12-17 23:39:55 +00001190//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001191// FunctionDecl Implementation
1192//===----------------------------------------------------------------------===//
1193
John McCalle1f2ec22009-09-11 06:45:03 +00001194void FunctionDecl::getNameForDiagnostic(std::string &S,
1195 const PrintingPolicy &Policy,
1196 bool Qualified) const {
1197 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1198 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1199 if (TemplateArgs)
1200 S += TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +00001201 TemplateArgs->data(),
1202 TemplateArgs->size(),
John McCalle1f2ec22009-09-11 06:45:03 +00001203 Policy);
1204
1205}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001206
Ted Kremenek186a0742010-04-29 16:49:01 +00001207bool FunctionDecl::isVariadic() const {
1208 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1209 return FT->isVariadic();
1210 return false;
1211}
1212
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001213bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1214 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1215 if (I->Body) {
1216 Definition = *I;
1217 return true;
1218 }
1219 }
1220
1221 return false;
1222}
1223
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001224Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001225 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1226 if (I->Body) {
1227 Definition = *I;
1228 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001229 }
1230 }
1231
1232 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001233}
1234
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001235void FunctionDecl::setBody(Stmt *B) {
1236 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001237 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001238 EndRangeLoc = B->getLocEnd();
1239}
1240
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001241void FunctionDecl::setPure(bool P) {
1242 IsPure = P;
1243 if (P)
1244 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1245 Parent->markedVirtualFunctionPure();
1246}
1247
Douglas Gregor16618f22009-09-12 00:17:51 +00001248bool FunctionDecl::isMain() const {
1249 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001250 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001251 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001252 getIdentifier() && getIdentifier()->isStr("main");
1253}
1254
Douglas Gregor16618f22009-09-12 00:17:51 +00001255bool FunctionDecl::isExternC() const {
1256 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001257 // In C, any non-static, non-overloadable function has external
1258 // linkage.
1259 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001260 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001261
Mike Stump11289f42009-09-09 15:08:12 +00001262 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001263 DC = DC->getParent()) {
1264 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1265 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001266 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001267 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001268
1269 break;
1270 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001271
1272 if (DC->isRecord())
1273 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001274 }
1275
Douglas Gregorbff62032010-10-21 16:57:46 +00001276 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001277}
1278
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001279bool FunctionDecl::isGlobal() const {
1280 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1281 return Method->isStatic();
1282
John McCall8e7d6562010-08-26 03:08:43 +00001283 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001284 return false;
1285
Mike Stump11289f42009-09-09 15:08:12 +00001286 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001287 DC->isNamespace();
1288 DC = DC->getParent()) {
1289 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1290 if (!Namespace->getDeclName())
1291 return false;
1292 break;
1293 }
1294 }
1295
1296 return true;
1297}
1298
Sebastian Redl833ef452010-01-26 22:01:41 +00001299void
1300FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1301 redeclarable_base::setPreviousDeclaration(PrevDecl);
1302
1303 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1304 FunctionTemplateDecl *PrevFunTmpl
1305 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1306 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1307 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1308 }
1309}
1310
1311const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1312 return getFirstDeclaration();
1313}
1314
1315FunctionDecl *FunctionDecl::getCanonicalDecl() {
1316 return getFirstDeclaration();
1317}
1318
Douglas Gregorbf62d642010-12-06 18:36:25 +00001319void FunctionDecl::setStorageClass(StorageClass SC) {
1320 assert(isLegalForFunction(SC));
1321 if (getStorageClass() != SC)
1322 ClearLinkageCache();
1323
1324 SClass = SC;
1325}
1326
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001327/// \brief Returns a value indicating whether this function
1328/// corresponds to a builtin function.
1329///
1330/// The function corresponds to a built-in function if it is
1331/// declared at translation scope or within an extern "C" block and
1332/// its name matches with the name of a builtin. The returned value
1333/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001334/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001335/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001336unsigned FunctionDecl::getBuiltinID() const {
1337 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001338 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1339 return 0;
1340
1341 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1342 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1343 return BuiltinID;
1344
1345 // This function has the name of a known C library
1346 // function. Determine whether it actually refers to the C library
1347 // function or whether it just has the same name.
1348
Douglas Gregora908e7f2009-02-17 03:23:10 +00001349 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001350 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001351 return 0;
1352
Douglas Gregore711f702009-02-14 18:57:46 +00001353 // If this function is at translation-unit scope and we're not in
1354 // C++, it refers to the C library function.
1355 if (!Context.getLangOptions().CPlusPlus &&
1356 getDeclContext()->isTranslationUnit())
1357 return BuiltinID;
1358
1359 // If the function is in an extern "C" linkage specification and is
1360 // not marked "overloadable", it's the real function.
1361 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001362 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001363 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001364 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001365 return BuiltinID;
1366
1367 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001368 return 0;
1369}
1370
1371
Chris Lattner47c0d002009-04-25 06:03:53 +00001372/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001373/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001374/// after it has been created.
1375unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001376 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001377 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001378 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001379 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001380
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001381}
1382
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001383void FunctionDecl::setParams(ASTContext &C,
1384 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001385 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001386 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001387
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001388 // Zero params -> null pointer.
1389 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001390 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001391 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001392 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001393
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001394 // Update source range. The check below allows us to set EndRangeLoc before
1395 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001396 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001397 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001398 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001399}
Chris Lattner41943152007-01-25 04:52:46 +00001400
Chris Lattner58258242008-04-10 02:22:51 +00001401/// getMinRequiredArguments - Returns the minimum number of arguments
1402/// needed to call this function. This may be fewer than the number of
1403/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001404/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001405unsigned FunctionDecl::getMinRequiredArguments() const {
1406 unsigned NumRequiredArgs = getNumParams();
1407 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001408 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001409 --NumRequiredArgs;
1410
1411 return NumRequiredArgs;
1412}
1413
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001414bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001415 // FIXME: This is not enough. Consider:
1416 //
1417 // inline void f();
1418 // void f() { }
1419 //
1420 // f is inlined, but does not have inline specified.
1421 // To fix this we should add an 'inline' flag to FunctionDecl.
1422 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001423 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001424
1425 if (isa<CXXMethodDecl>(this)) {
1426 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1427 return true;
1428 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001429
1430 switch (getTemplateSpecializationKind()) {
1431 case TSK_Undeclared:
1432 case TSK_ExplicitSpecialization:
1433 return false;
1434
1435 case TSK_ImplicitInstantiation:
1436 case TSK_ExplicitInstantiationDeclaration:
1437 case TSK_ExplicitInstantiationDefinition:
1438 // Handle below.
1439 break;
1440 }
1441
1442 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001443 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001444 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001445 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001446
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001447 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001448 return PatternDecl->isInlined();
1449
1450 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001451}
1452
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001453/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001454/// definition will be externally visible.
1455///
1456/// Inline function definitions are always available for inlining optimizations.
1457/// However, depending on the language dialect, declaration specifiers, and
1458/// attributes, the definition of an inline function may or may not be
1459/// "externally" visible to other translation units in the program.
1460///
1461/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001462/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001463/// inline definition becomes externally visible (C99 6.7.4p6).
1464///
1465/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1466/// definition, we use the GNU semantics for inline, which are nearly the
1467/// opposite of C99 semantics. In particular, "inline" by itself will create
1468/// an externally visible symbol, but "extern inline" will not create an
1469/// externally visible symbol.
1470bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1471 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001472 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001473 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001474
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001475 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001476 // GNU inline semantics. Based on a number of examples, we came up with the
1477 // following heuristic: if the "inline" keyword is present on a
1478 // declaration of the function but "extern" is not present on that
1479 // declaration, then the symbol is externally visible. Otherwise, the GNU
1480 // "extern inline" semantics applies and the symbol is not externally
1481 // visible.
1482 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1483 Redecl != RedeclEnd;
1484 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001485 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001486 return true;
1487 }
1488
1489 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001490 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001491 }
1492
1493 // C99 6.7.4p6:
1494 // [...] If all of the file scope declarations for a function in a
1495 // translation unit include the inline function specifier without extern,
1496 // then the definition in that translation unit is an inline definition.
1497 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1498 Redecl != RedeclEnd;
1499 ++Redecl) {
1500 // Only consider file-scope declarations in this test.
1501 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1502 continue;
1503
John McCall8e7d6562010-08-26 03:08:43 +00001504 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001505 return true; // Not an inline definition
1506 }
1507
1508 // C99 6.7.4p6:
1509 // An inline definition does not provide an external definition for the
1510 // function, and does not forbid an external definition in another
1511 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001512 return false;
1513}
1514
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001515/// getOverloadedOperator - Which C++ overloaded operator this
1516/// function represents, if any.
1517OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001518 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1519 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001520 else
1521 return OO_None;
1522}
1523
Alexis Huntc88db062010-01-13 09:01:02 +00001524/// getLiteralIdentifier - The literal suffix identifier this function
1525/// represents, if any.
1526const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1527 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1528 return getDeclName().getCXXLiteralIdentifier();
1529 else
1530 return 0;
1531}
1532
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001533FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1534 if (TemplateOrSpecialization.isNull())
1535 return TK_NonTemplate;
1536 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1537 return TK_FunctionTemplate;
1538 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1539 return TK_MemberSpecialization;
1540 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1541 return TK_FunctionTemplateSpecialization;
1542 if (TemplateOrSpecialization.is
1543 <DependentFunctionTemplateSpecializationInfo*>())
1544 return TK_DependentFunctionTemplateSpecialization;
1545
1546 assert(false && "Did we miss a TemplateOrSpecialization type?");
1547 return TK_NonTemplate;
1548}
1549
Douglas Gregord801b062009-10-07 23:56:10 +00001550FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001551 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001552 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1553
1554 return 0;
1555}
1556
Douglas Gregor06db9f52009-10-12 20:18:28 +00001557MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1558 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1559}
1560
Douglas Gregord801b062009-10-07 23:56:10 +00001561void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001562FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1563 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001564 TemplateSpecializationKind TSK) {
1565 assert(TemplateOrSpecialization.isNull() &&
1566 "Member function is already a specialization");
1567 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001568 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001569 TemplateOrSpecialization = Info;
1570}
1571
Douglas Gregorafca3b42009-10-27 20:53:28 +00001572bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001573 // If the function is invalid, it can't be implicitly instantiated.
1574 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001575 return false;
1576
1577 switch (getTemplateSpecializationKind()) {
1578 case TSK_Undeclared:
1579 case TSK_ExplicitSpecialization:
1580 case TSK_ExplicitInstantiationDefinition:
1581 return false;
1582
1583 case TSK_ImplicitInstantiation:
1584 return true;
1585
1586 case TSK_ExplicitInstantiationDeclaration:
1587 // Handled below.
1588 break;
1589 }
1590
1591 // Find the actual template from which we will instantiate.
1592 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001593 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001594 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001595 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001596
1597 // C++0x [temp.explicit]p9:
1598 // Except for inline functions, other explicit instantiation declarations
1599 // have the effect of suppressing the implicit instantiation of the entity
1600 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001601 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001602 return true;
1603
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001604 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001605}
1606
1607FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1608 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1609 while (Primary->getInstantiatedFromMemberTemplate()) {
1610 // If we have hit a point where the user provided a specialization of
1611 // this template, we're done looking.
1612 if (Primary->isMemberSpecialization())
1613 break;
1614
1615 Primary = Primary->getInstantiatedFromMemberTemplate();
1616 }
1617
1618 return Primary->getTemplatedDecl();
1619 }
1620
1621 return getInstantiatedFromMemberFunction();
1622}
1623
Douglas Gregor70d83e22009-06-29 17:30:29 +00001624FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001625 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001626 = TemplateOrSpecialization
1627 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001628 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001629 }
1630 return 0;
1631}
1632
1633const TemplateArgumentList *
1634FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001635 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001636 = TemplateOrSpecialization
1637 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001638 return Info->TemplateArguments;
1639 }
1640 return 0;
1641}
1642
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001643const TemplateArgumentListInfo *
1644FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1645 if (FunctionTemplateSpecializationInfo *Info
1646 = TemplateOrSpecialization
1647 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1648 return Info->TemplateArgumentsAsWritten;
1649 }
1650 return 0;
1651}
1652
Mike Stump11289f42009-09-09 15:08:12 +00001653void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001654FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1655 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001656 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001657 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001658 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001659 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1660 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001661 assert(TSK != TSK_Undeclared &&
1662 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001663 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001664 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001665 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001666 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1667 TemplateArgs,
1668 TemplateArgsAsWritten,
1669 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001670 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001671
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001672 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001673 // function template specializations.
1674 if (InsertPos)
1675 Template->getSpecializations().InsertNode(Info, InsertPos);
1676 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001677 // Try to insert the new node. If there is an existing node, leave it, the
1678 // set will contain the canonical decls while
1679 // FunctionTemplateDecl::findSpecialization will return
1680 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001681 FunctionTemplateSpecializationInfo *Existing
1682 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001683 (void)Existing;
1684 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1685 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001686 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001687}
1688
John McCallb9c78482010-04-08 09:05:18 +00001689void
1690FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1691 const UnresolvedSetImpl &Templates,
1692 const TemplateArgumentListInfo &TemplateArgs) {
1693 assert(TemplateOrSpecialization.isNull());
1694 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1695 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001696 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001697 void *Buffer = Context.Allocate(Size);
1698 DependentFunctionTemplateSpecializationInfo *Info =
1699 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1700 TemplateArgs);
1701 TemplateOrSpecialization = Info;
1702}
1703
1704DependentFunctionTemplateSpecializationInfo::
1705DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1706 const TemplateArgumentListInfo &TArgs)
1707 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1708
1709 d.NumTemplates = Ts.size();
1710 d.NumArgs = TArgs.size();
1711
1712 FunctionTemplateDecl **TsArray =
1713 const_cast<FunctionTemplateDecl**>(getTemplates());
1714 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1715 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1716
1717 TemplateArgumentLoc *ArgsArray =
1718 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1719 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1720 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1721}
1722
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001723TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001724 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001725 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001726 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001727 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001728 if (FTSInfo)
1729 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001730
Douglas Gregord801b062009-10-07 23:56:10 +00001731 MemberSpecializationInfo *MSInfo
1732 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1733 if (MSInfo)
1734 return MSInfo->getTemplateSpecializationKind();
1735
1736 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001737}
1738
Mike Stump11289f42009-09-09 15:08:12 +00001739void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001740FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1741 SourceLocation PointOfInstantiation) {
1742 if (FunctionTemplateSpecializationInfo *FTSInfo
1743 = TemplateOrSpecialization.dyn_cast<
1744 FunctionTemplateSpecializationInfo*>()) {
1745 FTSInfo->setTemplateSpecializationKind(TSK);
1746 if (TSK != TSK_ExplicitSpecialization &&
1747 PointOfInstantiation.isValid() &&
1748 FTSInfo->getPointOfInstantiation().isInvalid())
1749 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1750 } else if (MemberSpecializationInfo *MSInfo
1751 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1752 MSInfo->setTemplateSpecializationKind(TSK);
1753 if (TSK != TSK_ExplicitSpecialization &&
1754 PointOfInstantiation.isValid() &&
1755 MSInfo->getPointOfInstantiation().isInvalid())
1756 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1757 } else
1758 assert(false && "Function cannot have a template specialization kind");
1759}
1760
1761SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001762 if (FunctionTemplateSpecializationInfo *FTSInfo
1763 = TemplateOrSpecialization.dyn_cast<
1764 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001765 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001766 else if (MemberSpecializationInfo *MSInfo
1767 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001768 return MSInfo->getPointOfInstantiation();
1769
1770 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001771}
1772
Douglas Gregor6411b922009-09-11 20:15:17 +00001773bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001774 if (Decl::isOutOfLine())
1775 return true;
1776
1777 // If this function was instantiated from a member function of a
1778 // class template, check whether that member function was defined out-of-line.
1779 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1780 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001781 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001782 return Definition->isOutOfLine();
1783 }
1784
1785 // If this function was instantiated from a function template,
1786 // check whether that function template was defined out-of-line.
1787 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1788 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001789 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001790 return Definition->isOutOfLine();
1791 }
1792
1793 return false;
1794}
1795
Chris Lattner59a25942008-03-31 00:36:02 +00001796//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001797// FieldDecl Implementation
1798//===----------------------------------------------------------------------===//
1799
1800FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1801 IdentifierInfo *Id, QualType T,
1802 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1803 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1804}
1805
1806bool FieldDecl::isAnonymousStructOrUnion() const {
1807 if (!isImplicit() || getDeclName())
1808 return false;
1809
1810 if (const RecordType *Record = getType()->getAs<RecordType>())
1811 return Record->getDecl()->isAnonymousStructOrUnion();
1812
1813 return false;
1814}
1815
1816//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001817// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001818//===----------------------------------------------------------------------===//
1819
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001820SourceLocation TagDecl::getOuterLocStart() const {
1821 return getTemplateOrInnerLocStart(this);
1822}
1823
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001824SourceRange TagDecl::getSourceRange() const {
1825 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001826 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001827}
1828
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001829TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001830 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001831}
1832
Douglas Gregora72a4e32010-05-19 18:39:18 +00001833void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1834 TypedefDeclOrQualifier = TDD;
1835 if (TypeForDecl)
1836 TypeForDecl->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001837 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00001838}
1839
Douglas Gregordee1be82009-01-17 00:42:38 +00001840void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001841 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001842
1843 if (isa<CXXRecordDecl>(this)) {
1844 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1845 struct CXXRecordDecl::DefinitionData *Data =
1846 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001847 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1848 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001849 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001850}
1851
1852void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001853 assert((!isa<CXXRecordDecl>(this) ||
1854 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1855 "definition completed but not started");
1856
Douglas Gregordee1be82009-01-17 00:42:38 +00001857 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001858 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001859
1860 if (ASTMutationListener *L = getASTMutationListener())
1861 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001862}
1863
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001864TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001865 if (isDefinition())
1866 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001867 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1868 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001869
1870 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001871 R != REnd; ++R)
1872 if (R->isDefinition())
1873 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001874
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001875 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001876}
1877
John McCall3e11ebe2010-03-15 10:12:16 +00001878void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1879 SourceRange QualifierRange) {
1880 if (Qualifier) {
1881 // Make sure the extended qualifier info is allocated.
1882 if (!hasExtInfo())
1883 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1884 // Set qualifier info.
1885 getExtInfo()->NNS = Qualifier;
1886 getExtInfo()->NNSRange = QualifierRange;
1887 }
1888 else {
1889 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1890 assert(QualifierRange.isInvalid());
1891 if (hasExtInfo()) {
1892 getASTContext().Deallocate(getExtInfo());
1893 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1894 }
1895 }
1896}
1897
Ted Kremenek21475702008-09-05 17:16:31 +00001898//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001899// EnumDecl Implementation
1900//===----------------------------------------------------------------------===//
1901
1902EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1903 IdentifierInfo *Id, SourceLocation TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001904 EnumDecl *PrevDecl, bool IsScoped,
1905 bool IsScopedUsingClassTag, bool IsFixed) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001906 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001907 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001908 C.getTypeDeclType(Enum, PrevDecl);
1909 return Enum;
1910}
1911
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001912EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001913 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
Abramo Bagnara0e05e242010-12-03 18:54:17 +00001914 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001915}
1916
Douglas Gregord5058122010-02-11 01:19:42 +00001917void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001918 QualType NewPromotionType,
1919 unsigned NumPositiveBits,
1920 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001921 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001922 if (!IntegerType)
1923 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001924 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001925 setNumPositiveBits(NumPositiveBits);
1926 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001927 TagDecl::completeDefinition();
1928}
1929
1930//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001931// RecordDecl Implementation
1932//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001933
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001934RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001935 IdentifierInfo *Id, RecordDecl *PrevDecl,
1936 SourceLocation TKL)
1937 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001938 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001939 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001940 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001941 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001942 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001943}
1944
1945RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001946 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001947 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001948
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001949 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001950 C.getTypeDeclType(R, PrevDecl);
1951 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001952}
1953
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001954RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1955 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1956 SourceLocation());
1957}
1958
Douglas Gregordfcad112009-03-25 15:59:44 +00001959bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001960 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001961 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1962}
1963
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001964RecordDecl::field_iterator RecordDecl::field_begin() const {
1965 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1966 LoadFieldsFromExternalStorage();
1967
1968 return field_iterator(decl_iterator(FirstDecl));
1969}
1970
Douglas Gregor91f84212008-12-11 16:49:14 +00001971/// completeDefinition - Notes that the definition of this type is now
1972/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001973void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001974 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001975 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001976}
Steve Naroffcc321422007-03-26 23:09:51 +00001977
John McCall61925b02010-05-21 01:17:40 +00001978ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1979 // Force the decl chain to come into existence properly.
1980 if (!getNextDeclInContext()) getParent()->decls_begin();
1981
1982 assert(isAnonymousStructOrUnion());
1983 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1984 assert(D->getType()->isRecordType());
1985 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1986 return D;
1987}
1988
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001989void RecordDecl::LoadFieldsFromExternalStorage() const {
1990 ExternalASTSource *Source = getASTContext().getExternalSource();
1991 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1992
1993 // Notify that we have a RecordDecl doing some initialization.
1994 ExternalASTSource::Deserializing TheFields(Source);
1995
1996 llvm::SmallVector<Decl*, 64> Decls;
1997 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1998 return;
1999
2000#ifndef NDEBUG
2001 // Check that all decls we got were FieldDecls.
2002 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2003 assert(isa<FieldDecl>(Decls[i]));
2004#endif
2005
2006 LoadedFieldsFromExternalStorage = true;
2007
2008 if (Decls.empty())
2009 return;
2010
2011 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2012}
2013
Steve Naroff415d3d52008-10-08 17:01:13 +00002014//===----------------------------------------------------------------------===//
2015// BlockDecl Implementation
2016//===----------------------------------------------------------------------===//
2017
Douglas Gregord5058122010-02-11 01:19:42 +00002018void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002019 unsigned NParms) {
2020 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002021
Steve Naroffc4b30e52009-03-13 16:56:44 +00002022 // Zero params -> null pointer.
2023 if (NParms) {
2024 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002025 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002026 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2027 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2028 }
2029}
2030
2031unsigned BlockDecl::getNumParams() const {
2032 return NumParams;
2033}
Sebastian Redl833ef452010-01-26 22:01:41 +00002034
2035
2036//===----------------------------------------------------------------------===//
2037// Other Decl Allocation/Deallocation Method Implementations
2038//===----------------------------------------------------------------------===//
2039
2040TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2041 return new (C) TranslationUnitDecl(C);
2042}
2043
2044NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
2045 SourceLocation L, IdentifierInfo *Id) {
2046 return new (C) NamespaceDecl(DC, L, Id);
2047}
2048
Douglas Gregor417e87c2010-10-27 19:49:05 +00002049NamespaceDecl *NamespaceDecl::getNextNamespace() {
2050 return dyn_cast_or_null<NamespaceDecl>(
2051 NextNamespace.get(getASTContext().getExternalSource()));
2052}
2053
Sebastian Redl833ef452010-01-26 22:01:41 +00002054ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
2055 SourceLocation L, IdentifierInfo *Id, QualType T) {
2056 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
2057}
2058
2059FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002060 const DeclarationNameInfo &NameInfo,
2061 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002062 StorageClass S, StorageClass SCAsWritten,
2063 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002064 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00002065 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00002066 New->HasWrittenPrototype = hasWrittenPrototype;
2067 return New;
2068}
2069
2070BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2071 return new (C) BlockDecl(DC, L);
2072}
2073
2074EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2075 SourceLocation L,
2076 IdentifierInfo *Id, QualType T,
2077 Expr *E, const llvm::APSInt &V) {
2078 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2079}
2080
Benjamin Kramer39593702010-11-21 14:11:41 +00002081IndirectFieldDecl *
2082IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2083 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2084 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002085 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2086}
2087
Douglas Gregorbe996932010-09-01 20:41:53 +00002088SourceRange EnumConstantDecl::getSourceRange() const {
2089 SourceLocation End = getLocation();
2090 if (Init)
2091 End = Init->getLocEnd();
2092 return SourceRange(getLocation(), End);
2093}
2094
Sebastian Redl833ef452010-01-26 22:01:41 +00002095TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
2096 SourceLocation L, IdentifierInfo *Id,
2097 TypeSourceInfo *TInfo) {
2098 return new (C) TypedefDecl(DC, L, Id, TInfo);
2099}
2100
Sebastian Redl833ef452010-01-26 22:01:41 +00002101FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
2102 SourceLocation L,
2103 StringLiteral *Str) {
2104 return new (C) FileScopeAsmDecl(DC, L, Str);
2105}