blob: 5cc745d40ded9363ca781b5b4b33a1c21e976cae [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 McCall457a04e2010-10-22 21:05:15 +000036static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
37 switch (A->getVisibility()) {
38 case VisibilityAttr::Default:
39 return DefaultVisibility;
40 case VisibilityAttr::Hidden:
41 return HiddenVisibility;
42 case VisibilityAttr::Protected:
43 return ProtectedVisibility;
44 }
45 return DefaultVisibility;
46}
47
48typedef std::pair<Linkage,Visibility> LVPair;
49static LVPair merge(LVPair L, LVPair R) {
50 return LVPair(minLinkage(L.first, R.first),
51 minVisibility(L.second, R.second));
52}
53
Douglas Gregor7dc5c172010-02-03 09:33:45 +000054/// \brief Get the most restrictive linkage for the types in the given
55/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +000056static LVPair
57getLVForTemplateParameterList(const TemplateParameterList *Params) {
58 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000059 for (TemplateParameterList::const_iterator P = Params->begin(),
60 PEnd = Params->end();
61 P != PEnd; ++P) {
62 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P))
63 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +000064 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +000065 continue;
66 }
67
68 if (TemplateTemplateParmDecl *TTP
69 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCall457a04e2010-10-22 21:05:15 +000070 LV =
71 merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +000072 }
73 }
74
John McCall457a04e2010-10-22 21:05:15 +000075 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +000076}
77
78/// \brief Get the most restrictive linkage for the types and
79/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +000080static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
81 unsigned NumArgs) {
82 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000083
84 for (unsigned I = 0; I != NumArgs; ++I) {
85 switch (Args[I].getKind()) {
86 case TemplateArgument::Null:
87 case TemplateArgument::Integral:
88 case TemplateArgument::Expression:
89 break;
90
91 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +000092 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +000093 break;
94
95 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +000096 // The decl can validly be null as the representation of nullptr
97 // arguments, valid only in C++0x.
98 if (Decl *D = Args[I].getAsDecl()) {
99 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
100 LV = merge(LV, ND->getLinkageAndVisibility());
101 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
102 LV = merge(LV, VD->getType()->getLinkageAndVisibility());
103 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000104 break;
105
106 case TemplateArgument::Template:
John McCall457a04e2010-10-22 21:05:15 +0000107 if (TemplateDecl *Template = Args[I].getAsTemplate().getAsTemplateDecl())
108 LV = merge(LV, Template->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000109 break;
110
111 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000112 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
113 Args[I].pack_size()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000114 break;
115 }
116 }
117
John McCall457a04e2010-10-22 21:05:15 +0000118 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000119}
120
John McCall457a04e2010-10-22 21:05:15 +0000121static LVPair getLVForTemplateArgumentList(const TemplateArgumentList &TArgs) {
122 return getLVForTemplateArgumentList(TArgs.getFlatArgumentList(),
123 TArgs.flat_size());
John McCall8823c652010-08-13 08:35:10 +0000124}
125
John McCall457a04e2010-10-22 21:05:15 +0000126static LVPair getLVForNamespaceScopeDecl(const NamedDecl *D) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000127 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000128 "Not a name having namespace scope");
129 ASTContext &Context = D->getASTContext();
130
131 // C++ [basic.link]p3:
132 // A name having namespace scope (3.3.6) has internal linkage if it
133 // is the name of
134 // - an object, reference, function or function template that is
135 // explicitly declared static; or,
136 // (This bullet corresponds to C99 6.2.2p3.)
137 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
138 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000139 if (Var->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000140 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000141
142 // - an object or reference that is explicitly declared const
143 // and neither explicitly declared extern nor previously
144 // declared to have external linkage; or
145 // (there is no equivalent in C99)
146 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000147 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000148 Var->getStorageClass() != SC_Extern &&
149 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000150 bool FoundExtern = false;
151 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
152 PrevVar && !FoundExtern;
153 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000154 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000155 FoundExtern = true;
156
157 if (!FoundExtern)
John McCall457a04e2010-10-22 21:05:15 +0000158 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000159 }
160 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000161 // C++ [temp]p4:
162 // A non-member function template can have internal linkage; any
163 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000164 const FunctionDecl *Function = 0;
165 if (const FunctionTemplateDecl *FunTmpl
166 = dyn_cast<FunctionTemplateDecl>(D))
167 Function = FunTmpl->getTemplatedDecl();
168 else
169 Function = cast<FunctionDecl>(D);
170
171 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000172 if (Function->getStorageClass() == SC_Static)
John McCall457a04e2010-10-22 21:05:15 +0000173 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000174 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
175 // - a data member of an anonymous union.
176 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCall457a04e2010-10-22 21:05:15 +0000177 return LVPair(InternalLinkage, DefaultVisibility);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000178 }
179
John McCall457a04e2010-10-22 21:05:15 +0000180 if (D->isInAnonymousNamespace())
181 return LVPair(UniqueExternalLinkage, DefaultVisibility);
182
183 // Set up the defaults.
184
185 // C99 6.2.2p5:
186 // If the declaration of an identifier for an object has file
187 // scope and no storage-class specifier, its linkage is
188 // external.
189 LVPair LV(ExternalLinkage, DefaultVisibility);
190
191 // We ignore -fvisibility on non-definitions and explicit
192 // instantiation declarations.
193 bool ConsiderDashFVisibility = true;
194
Douglas Gregorf73b2822009-11-25 22:24:25 +0000195 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000196
Douglas Gregorf73b2822009-11-25 22:24:25 +0000197 // A name having namespace scope has external linkage if it is the
198 // name of
199 //
200 // - an object or reference, unless it has internal linkage; or
201 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000202 // Modify the variable's LV by the LV of its type unless this is
203 // C or extern "C". This follows from [basic.link]p9:
204 // A type without linkage shall not be used as the type of a
205 // variable or function with external linkage unless
206 // - the entity has C language linkage, or
207 // - the entity is declared within an unnamed namespace, or
208 // - the entity is not used or is defined in the same
209 // translation unit.
210 // and [basic.link]p10:
211 // ...the types specified by all declarations referring to a
212 // given variable or function shall be identical...
213 // C does not have an equivalent rule.
214 //
John McCall5fe84122010-10-26 04:59:26 +0000215 // Ignore this if we've got an explicit attribute; the user
216 // probably knows what they're doing.
217 //
John McCall457a04e2010-10-22 21:05:15 +0000218 // Note that we don't want to make the variable non-external
219 // because of this, but unique-external linkage suits us.
John McCall5fe84122010-10-26 04:59:26 +0000220 if (Context.getLangOptions().CPlusPlus && !Var->isExternC() &&
221 !Var->hasAttr<VisibilityAttr>()) {
John McCall457a04e2010-10-22 21:05:15 +0000222 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
223 if (TypeLV.first != ExternalLinkage)
224 return LVPair(UniqueExternalLinkage, DefaultVisibility);
225 LV.second = minVisibility(LV.second, TypeLV.second);
226 }
227
Douglas Gregorf73b2822009-11-25 22:24:25 +0000228 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000229 (Var->getStorageClass() == SC_Extern ||
230 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000231 if (Var->getStorageClass() == SC_PrivateExtern)
232 LV.second = HiddenVisibility;
233
Douglas Gregorf73b2822009-11-25 22:24:25 +0000234 // C99 6.2.2p4:
235 // For an identifier declared with the storage-class specifier
236 // extern in a scope in which a prior declaration of that
237 // identifier is visible, if the prior declaration specifies
238 // internal or external linkage, the linkage of the identifier
239 // at the later declaration is the same as the linkage
240 // specified at the prior declaration. If no prior declaration
241 // is visible, or if the prior declaration specifies no
242 // linkage, then the identifier has external linkage.
243 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000244 LVPair PrevLV = PrevVar->getLinkageAndVisibility();
245 if (PrevLV.first) LV.first = PrevLV.first;
246 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000247 }
248 }
249
Douglas Gregorf73b2822009-11-25 22:24:25 +0000250 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000251 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
252 // Modify the function's LV by the LV of its type unless this is
253 // C or extern "C". See the comment above about variables.
John McCall5fe84122010-10-26 04:59:26 +0000254 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
255 !Function->hasAttr<VisibilityAttr>()) {
John McCall457a04e2010-10-22 21:05:15 +0000256 LVPair TypeLV = Function->getType()->getLinkageAndVisibility();
257 if (TypeLV.first != ExternalLinkage)
258 return LVPair(UniqueExternalLinkage, DefaultVisibility);
259 LV.second = minVisibility(LV.second, TypeLV.second);
260 }
261
Douglas Gregorf73b2822009-11-25 22:24:25 +0000262 // C99 6.2.2p5:
263 // If the declaration of an identifier for a function has no
264 // storage-class specifier, its linkage is determined exactly
265 // as if it were declared with the storage-class specifier
266 // extern.
267 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000268 (Function->getStorageClass() == SC_Extern ||
269 Function->getStorageClass() == SC_PrivateExtern ||
270 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000271 // C99 6.2.2p4:
272 // For an identifier declared with the storage-class specifier
273 // extern in a scope in which a prior declaration of that
274 // identifier is visible, if the prior declaration specifies
275 // internal or external linkage, the linkage of the identifier
276 // at the later declaration is the same as the linkage
277 // specified at the prior declaration. If no prior declaration
278 // is visible, or if the prior declaration specifies no
279 // linkage, then the identifier has external linkage.
280 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
John McCall457a04e2010-10-22 21:05:15 +0000281 LVPair PrevLV = PrevFunc->getLinkageAndVisibility();
282 if (PrevLV.first) LV.first = PrevLV.first;
283 LV.second = minVisibility(LV.second, PrevLV.second);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000284 }
285 }
286
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000287 if (FunctionTemplateSpecializationInfo *SpecInfo
288 = Function->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000289 LV = merge(LV, SpecInfo->getTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000290 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
John McCall457a04e2010-10-22 21:05:15 +0000291 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
292
293 if (SpecInfo->getTemplateSpecializationKind()
294 == TSK_ExplicitInstantiationDeclaration)
295 ConsiderDashFVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000296 }
297
John McCall457a04e2010-10-22 21:05:15 +0000298 if (ConsiderDashFVisibility)
299 ConsiderDashFVisibility = Function->hasBody();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000300
301 // - a named class (Clause 9), or an unnamed class defined in a
302 // typedef declaration in which the class has the typedef name
303 // for linkage purposes (7.1.3); or
304 // - a named enumeration (7.2), or an unnamed enumeration
305 // defined in a typedef declaration in which the enumeration
306 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000307 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
308 // Unnamed tags have no linkage.
309 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
310 return LVPair(NoLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000311
John McCall457a04e2010-10-22 21:05:15 +0000312 // If this is a class template specialization, consider the
313 // linkage of the template and template arguments.
314 if (const ClassTemplateSpecializationDecl *Spec
315 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
316 // From the template. Note below the restrictions on how we
317 // compute template visibility.
318 LV = merge(LV, Spec->getSpecializedTemplate()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000319
John McCall457a04e2010-10-22 21:05:15 +0000320 // The arguments at which the template was instantiated.
321 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
322 LV = merge(LV, getLVForTemplateArgumentList(TemplateArgs));
323
324 if (Spec->getTemplateSpecializationKind()
325 == TSK_ExplicitInstantiationDeclaration)
326 ConsiderDashFVisibility = false;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000327 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000328
John McCall5fe84122010-10-26 04:59:26 +0000329 // Consider -fvisibility unless the type has C linkage.
John McCall457a04e2010-10-22 21:05:15 +0000330 if (ConsiderDashFVisibility)
John McCall5fe84122010-10-26 04:59:26 +0000331 ConsiderDashFVisibility =
332 (Context.getLangOptions().CPlusPlus &&
333 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000334
Douglas Gregorf73b2822009-11-25 22:24:25 +0000335 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000336 } else if (isa<EnumConstantDecl>(D)) {
337 LVPair EnumLV =
338 cast<NamedDecl>(D->getDeclContext())->getLinkageAndVisibility();
339 if (!isExternalLinkage(EnumLV.first))
340 return LVPair(NoLinkage, DefaultVisibility);
341 LV = merge(LV, EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000342
343 // - a template, unless it is a function template that has
344 // internal linkage (Clause 14);
John McCall457a04e2010-10-22 21:05:15 +0000345 } else if (const TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) {
346 LV = merge(LV, getLVForTemplateParameterList(
347 Template->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000348
John McCall457a04e2010-10-22 21:05:15 +0000349 // We do not want to consider attributes or global settings when
350 // computing template visibility.
351 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000352
353 // - a namespace (7.3), unless it is declared within an unnamed
354 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000355 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
356 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000357
John McCall457a04e2010-10-22 21:05:15 +0000358 // By extension, we assign external linkage to Objective-C
359 // interfaces.
360 } else if (isa<ObjCInterfaceDecl>(D)) {
361 // fallout
362
363 // Everything not covered here has no linkage.
364 } else {
365 return LVPair(NoLinkage, DefaultVisibility);
366 }
367
368 // If we ended up with non-external linkage, visibility should
369 // always be default.
370 if (LV.first != ExternalLinkage)
371 return LVPair(LV.first, DefaultVisibility);
372
373 // If we didn't end up with hidden visibility, consider attributes
374 // and -fvisibility.
375 if (LV.second != HiddenVisibility) {
376 Visibility StandardV;
377
378 // If we have an explicit visibility attribute, merge that in.
379 const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
380 if (VA)
381 StandardV = GetVisibilityFromAttr(VA);
382 else if (ConsiderDashFVisibility)
383 StandardV = Context.getLangOptions().getVisibilityMode();
384 else
385 StandardV = DefaultVisibility; // no-op
386
387 LV.second = minVisibility(LV.second, StandardV);
388 }
389
390 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000391}
392
John McCall457a04e2010-10-22 21:05:15 +0000393static LVPair getLVForClassMember(const NamedDecl *D) {
394 // Only certain class members have linkage. Note that fields don't
395 // really have linkage, but it's convenient to say they do for the
396 // purposes of calculating linkage of pointer-to-data-member
397 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000398 if (!(isa<CXXMethodDecl>(D) ||
399 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000400 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000401 (isa<TagDecl>(D) &&
402 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCall457a04e2010-10-22 21:05:15 +0000403 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000404
405 // Class members only have linkage if their class has external linkage.
John McCall457a04e2010-10-22 21:05:15 +0000406 LVPair ClassLV =
407 cast<RecordDecl>(D->getDeclContext())->getLinkageAndVisibility();
408 if (!isExternalLinkage(ClassLV.first))
409 return LVPair(NoLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000410
411 // If the class already has unique-external linkage, we can't improve.
John McCall457a04e2010-10-22 21:05:15 +0000412 if (ClassLV.first == UniqueExternalLinkage)
413 return LVPair(UniqueExternalLinkage, DefaultVisibility);
John McCall8823c652010-08-13 08:35:10 +0000414
John McCall457a04e2010-10-22 21:05:15 +0000415 // Start with the class's linkage and visibility.
416 LVPair LV = ClassLV;
417
418 // If we have an explicit visibility attribute, merge that in.
419 const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
420 if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
421
422 // If it's a value declaration, apply the LV from its type.
423 // See the comment about namespace-scope variable decls above.
424 if (isa<ValueDecl>(D)) {
425 LVPair TypeLV = cast<ValueDecl>(D)->getType()->getLinkageAndVisibility();
426 if (TypeLV.first != ExternalLinkage)
427 LV.first = minLinkage(LV.first, UniqueExternalLinkage);
428 LV.second = minVisibility(LV.second, TypeLV.second);
429 }
430
John McCall8823c652010-08-13 08:35:10 +0000431 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000432 // If this is a method template specialization, use the linkage for
433 // the template parameters and arguments.
434 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000435 = MD->getTemplateSpecializationInfo()) {
John McCall457a04e2010-10-22 21:05:15 +0000436 LV = merge(LV, getLVForTemplateArgumentList(*Spec->TemplateArguments));
437 LV = merge(LV, getLVForTemplateParameterList(
438 Spec->getTemplate()->getTemplateParameters()));
John McCall8823c652010-08-13 08:35:10 +0000439 }
440
John McCall457a04e2010-10-22 21:05:15 +0000441 // If -fvisibility-inlines-hidden was provided, then inline C++
442 // member functions get "hidden" visibility if they don't have an
443 // explicit visibility attribute.
444 if (!VA && MD->isInlined() && LV.second > HiddenVisibility &&
445 D->getASTContext().getLangOptions().InlineVisibilityHidden)
446 LV.second = HiddenVisibility;
447
John McCall8823c652010-08-13 08:35:10 +0000448 // Similarly for member class template specializations.
449 } else if (const ClassTemplateSpecializationDecl *Spec
450 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
John McCall457a04e2010-10-22 21:05:15 +0000451 LV = merge(LV, getLVForTemplateArgumentList(Spec->getTemplateArgs()));
452 LV = merge(LV, getLVForTemplateParameterList(
453 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall8823c652010-08-13 08:35:10 +0000454 }
455
John McCall457a04e2010-10-22 21:05:15 +0000456 return LV;
John McCall8823c652010-08-13 08:35:10 +0000457}
458
John McCall457a04e2010-10-22 21:05:15 +0000459LVPair NamedDecl::getLinkageAndVisibility() const {
Ted Kremenek926d8602010-04-20 23:15:35 +0000460
461 // Objective-C: treat all Objective-C declarations as having external
462 // linkage.
463 switch (getKind()) {
464 default:
465 break;
John McCall457a04e2010-10-22 21:05:15 +0000466 case Decl::TemplateTemplateParm: // count these as external
467 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000468 case Decl::ObjCAtDefsField:
469 case Decl::ObjCCategory:
470 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000471 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000472 case Decl::ObjCForwardProtocol:
473 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000474 case Decl::ObjCMethod:
475 case Decl::ObjCProperty:
476 case Decl::ObjCPropertyImpl:
477 case Decl::ObjCProtocol:
John McCall457a04e2010-10-22 21:05:15 +0000478 return LVPair(ExternalLinkage, DefaultVisibility);
Ted Kremenek926d8602010-04-20 23:15:35 +0000479 }
480
Douglas Gregorf73b2822009-11-25 22:24:25 +0000481 // Handle linkage for namespace-scope names.
Sebastian Redl50c68252010-08-31 00:36:30 +0000482 if (getDeclContext()->getRedeclContext()->isFileContext())
John McCall457a04e2010-10-22 21:05:15 +0000483 return getLVForNamespaceScopeDecl(this);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000484
485 // C++ [basic.link]p5:
486 // In addition, a member function, static data member, a named
487 // class or enumeration of class scope, or an unnamed class or
488 // enumeration defined in a class-scope typedef declaration such
489 // that the class or enumeration has the typedef name for linkage
490 // purposes (7.1.3), has external linkage if the name of the class
491 // has external linkage.
John McCall8823c652010-08-13 08:35:10 +0000492 if (getDeclContext()->isRecord())
John McCall457a04e2010-10-22 21:05:15 +0000493 return getLVForClassMember(this);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000494
495 // C++ [basic.link]p6:
496 // The name of a function declared in block scope and the name of
497 // an object declared by a block scope extern declaration have
498 // linkage. If there is a visible declaration of an entity with
499 // linkage having the same name and type, ignoring entities
500 // declared outside the innermost enclosing namespace scope, the
501 // block scope declaration declares that same entity and receives
502 // the linkage of the previous declaration. If there is more than
503 // one such matching entity, the program is ill-formed. Otherwise,
504 // if no matching entity is found, the block scope entity receives
505 // external linkage.
506 if (getLexicalDeclContext()->isFunctionOrMethod()) {
507 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000508 if (Function->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000509 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000510
John McCall457a04e2010-10-22 21:05:15 +0000511 LVPair LV(ExternalLinkage, DefaultVisibility);
512 if (const VisibilityAttr *VA = Function->getAttr<VisibilityAttr>())
513 LV.second = GetVisibilityFromAttr(VA);
514
515 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
516 LVPair PrevLV = Prev->getLinkageAndVisibility();
517 if (PrevLV.first) LV.first = PrevLV.first;
518 LV.second = minVisibility(LV.second, PrevLV.second);
519 }
520
521 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000522 }
523
524 if (const VarDecl *Var = dyn_cast<VarDecl>(this))
John McCall8e7d6562010-08-26 03:08:43 +0000525 if (Var->getStorageClass() == SC_Extern ||
526 Var->getStorageClass() == SC_PrivateExtern) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000527 if (Var->isInAnonymousNamespace())
John McCall457a04e2010-10-22 21:05:15 +0000528 return LVPair(UniqueExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000529
John McCall457a04e2010-10-22 21:05:15 +0000530 LVPair LV(ExternalLinkage, DefaultVisibility);
531 if (Var->getStorageClass() == SC_PrivateExtern)
532 LV.second = HiddenVisibility;
533 else if (const VisibilityAttr *VA = Var->getAttr<VisibilityAttr>())
534 LV.second = GetVisibilityFromAttr(VA);
535
536 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
537 LVPair PrevLV = Prev->getLinkageAndVisibility();
538 if (PrevLV.first) LV.first = PrevLV.first;
539 LV.second = minVisibility(LV.second, PrevLV.second);
540 }
541
542 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000543 }
544 }
545
546 // C++ [basic.link]p6:
547 // Names not covered by these rules have no linkage.
John McCall457a04e2010-10-22 21:05:15 +0000548 return LVPair(NoLinkage, DefaultVisibility);
549}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000550
Douglas Gregor2ada0482009-02-04 17:27:36 +0000551std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000552 return getQualifiedNameAsString(getASTContext().getLangOptions());
553}
554
555std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000556 const DeclContext *Ctx = getDeclContext();
557
558 if (Ctx->isFunctionOrMethod())
559 return getNameAsString();
560
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000561 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
562 ContextsTy Contexts;
563
564 // Collect contexts.
565 while (Ctx && isa<NamedDecl>(Ctx)) {
566 Contexts.push_back(Ctx);
567 Ctx = Ctx->getParent();
568 };
569
570 std::string QualName;
571 llvm::raw_string_ostream OS(QualName);
572
573 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
574 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000575 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000576 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000577 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
578 std::string TemplateArgsStr
579 = TemplateSpecializationType::PrintTemplateArgumentList(
580 TemplateArgs.getFlatArgumentList(),
Douglas Gregor7de59662009-05-29 20:38:28 +0000581 TemplateArgs.flat_size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000582 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000583 OS << Spec->getName() << TemplateArgsStr;
584 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000585 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000586 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000587 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000588 OS << ND;
589 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
590 if (!RD->getIdentifier())
591 OS << "<anonymous " << RD->getKindName() << '>';
592 else
593 OS << RD;
594 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000595 const FunctionProtoType *FT = 0;
596 if (FD->hasWrittenPrototype())
597 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
598
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000599 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000600 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000601 unsigned NumParams = FD->getNumParams();
602 for (unsigned i = 0; i < NumParams; ++i) {
603 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000604 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000605 std::string Param;
606 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000607 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000608 }
609
610 if (FT->isVariadic()) {
611 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000612 OS << ", ";
613 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000614 }
615 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000616 OS << ')';
617 } else {
618 OS << cast<NamedDecl>(*I);
619 }
620 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000621 }
622
John McCalla2a3f7d2010-03-16 21:48:18 +0000623 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000624 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000625 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000626 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000627
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000628 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000629}
630
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000631bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000632 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
633
Douglas Gregor889ceb72009-02-03 19:21:40 +0000634 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
635 // We want to keep it, unless it nominates same namespace.
636 if (getKind() == Decl::UsingDirective) {
637 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() ==
638 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace();
639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000641 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
642 // For function declarations, we keep track of redeclarations.
643 return FD->getPreviousDeclaration() == OldD;
644
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000645 // For function templates, the underlying function declarations are linked.
646 if (const FunctionTemplateDecl *FunctionTemplate
647 = dyn_cast<FunctionTemplateDecl>(this))
648 if (const FunctionTemplateDecl *OldFunctionTemplate
649 = dyn_cast<FunctionTemplateDecl>(OldD))
650 return FunctionTemplate->getTemplatedDecl()
651 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000652
Steve Naroffc4173fa2009-02-22 19:35:57 +0000653 // For method declarations, we keep track of redeclarations.
654 if (isa<ObjCMethodDecl>(this))
655 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000656
John McCall9f3059a2009-10-09 21:13:30 +0000657 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
658 return true;
659
John McCall3f746822009-11-17 05:59:44 +0000660 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
661 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
662 cast<UsingShadowDecl>(OldD)->getTargetDecl();
663
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000664 // For non-function declarations, if the declarations are of the
665 // same kind then this must be a redeclaration, or semantic analysis
666 // would not have given us the new declaration.
667 return this->getKind() == OldD->getKind();
668}
669
Douglas Gregoreddf4332009-02-24 20:03:32 +0000670bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000671 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000672}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000673
Anders Carlsson6915bf62009-06-26 06:29:23 +0000674NamedDecl *NamedDecl::getUnderlyingDecl() {
675 NamedDecl *ND = this;
676 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000677 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000678 ND = UD->getTargetDecl();
679 else if (ObjCCompatibleAliasDecl *AD
680 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
681 return AD->getClassInterface();
682 else
683 return ND;
684 }
685}
686
John McCalla8ae2222010-04-06 21:38:20 +0000687bool NamedDecl::isCXXInstanceMember() const {
688 assert(isCXXClassMember() &&
689 "checking whether non-member is instance member");
690
691 const NamedDecl *D = this;
692 if (isa<UsingShadowDecl>(D))
693 D = cast<UsingShadowDecl>(D)->getTargetDecl();
694
695 if (isa<FieldDecl>(D))
696 return true;
697 if (isa<CXXMethodDecl>(D))
698 return cast<CXXMethodDecl>(D)->isInstance();
699 if (isa<FunctionTemplateDecl>(D))
700 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
701 ->getTemplatedDecl())->isInstance();
702 return false;
703}
704
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000705//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000706// DeclaratorDecl Implementation
707//===----------------------------------------------------------------------===//
708
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000709template <typename DeclT>
710static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
711 if (decl->getNumTemplateParameterLists() > 0)
712 return decl->getTemplateParameterList(0)->getTemplateLoc();
713 else
714 return decl->getInnerLocStart();
715}
716
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000717SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000718 TypeSourceInfo *TSI = getTypeSourceInfo();
719 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000720 return SourceLocation();
721}
722
John McCall3e11ebe2010-03-15 10:12:16 +0000723void DeclaratorDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
724 SourceRange QualifierRange) {
725 if (Qualifier) {
726 // Make sure the extended decl info is allocated.
727 if (!hasExtInfo()) {
728 // Save (non-extended) type source info pointer.
729 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
730 // Allocate external info struct.
731 DeclInfo = new (getASTContext()) ExtInfo;
732 // Restore savedTInfo into (extended) decl info.
733 getExtInfo()->TInfo = savedTInfo;
734 }
735 // Set qualifier info.
736 getExtInfo()->NNS = Qualifier;
737 getExtInfo()->NNSRange = QualifierRange;
738 }
739 else {
740 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
741 assert(QualifierRange.isInvalid());
742 if (hasExtInfo()) {
743 // Save type source info pointer.
744 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
745 // Deallocate the extended decl info.
746 getASTContext().Deallocate(getExtInfo());
747 // Restore savedTInfo into (non-extended) decl info.
748 DeclInfo = savedTInfo;
749 }
750 }
751}
752
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000753SourceLocation DeclaratorDecl::getOuterLocStart() const {
754 return getTemplateOrInnerLocStart(this);
755}
756
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000757void
Douglas Gregor20527e22010-06-15 17:44:38 +0000758QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
759 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000760 TemplateParameterList **TPLists) {
761 assert((NumTPLists == 0 || TPLists != 0) &&
762 "Empty array of template parameters with positive size!");
763 assert((NumTPLists == 0 || NNS) &&
764 "Nonempty array of template parameters with no qualifier!");
765
766 // Free previous template parameters (if any).
767 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000768 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000769 TemplParamLists = 0;
770 NumTemplParamLists = 0;
771 }
772 // Set info on matched template parameter lists (if any).
773 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +0000774 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +0000775 NumTemplParamLists = NumTPLists;
776 for (unsigned i = NumTPLists; i-- > 0; )
777 TemplParamLists[i] = TPLists[i];
778 }
779}
780
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000781//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +0000782// VarDecl Implementation
783//===----------------------------------------------------------------------===//
784
Sebastian Redl833ef452010-01-26 22:01:41 +0000785const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
786 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +0000787 case SC_None: break;
788 case SC_Auto: return "auto"; break;
789 case SC_Extern: return "extern"; break;
790 case SC_PrivateExtern: return "__private_extern__"; break;
791 case SC_Register: return "register"; break;
792 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +0000793 }
794
795 assert(0 && "Invalid storage class");
796 return 0;
797}
798
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000799VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
John McCallbcd03502009-12-07 02:54:59 +0000800 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +0000801 StorageClass S, StorageClass SCAsWritten) {
802 return new (C) VarDecl(Var, DC, L, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +0000803}
804
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000805SourceLocation VarDecl::getInnerLocStart() const {
Douglas Gregor562c1f92010-01-22 19:49:59 +0000806 SourceLocation Start = getTypeSpecStartLoc();
807 if (Start.isInvalid())
808 Start = getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000809 return Start;
810}
811
812SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000813 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000814 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
815 return SourceRange(getOuterLocStart(), getLocation());
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +0000816}
817
Sebastian Redl833ef452010-01-26 22:01:41 +0000818bool VarDecl::isExternC() const {
819 ASTContext &Context = getASTContext();
820 if (!Context.getLangOptions().CPlusPlus)
821 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +0000822 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +0000823 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
824
825 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
826 DC = DC->getParent()) {
827 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
828 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +0000829 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +0000830
831 break;
832 }
833
834 if (DC->isFunctionOrMethod())
835 return false;
836 }
837
838 return false;
839}
840
841VarDecl *VarDecl::getCanonicalDecl() {
842 return getFirstDeclaration();
843}
844
Sebastian Redl35351a92010-01-31 22:27:38 +0000845VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
846 // C++ [basic.def]p2:
847 // A declaration is a definition unless [...] it contains the 'extern'
848 // specifier or a linkage-specification and neither an initializer [...],
849 // it declares a static data member in a class declaration [...].
850 // C++ [temp.expl.spec]p15:
851 // An explicit specialization of a static data member of a template is a
852 // definition if the declaration includes an initializer; otherwise, it is
853 // a declaration.
854 if (isStaticDataMember()) {
855 if (isOutOfLine() && (hasInit() ||
856 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
857 return Definition;
858 else
859 return DeclarationOnly;
860 }
861 // C99 6.7p5:
862 // A definition of an identifier is a declaration for that identifier that
863 // [...] causes storage to be reserved for that object.
864 // Note: that applies for all non-file-scope objects.
865 // C99 6.9.2p1:
866 // If the declaration of an identifier for an object has file scope and an
867 // initializer, the declaration is an external definition for the identifier
868 if (hasInit())
869 return Definition;
870 // AST for 'extern "C" int foo;' is annotated with 'extern'.
871 if (hasExternalStorage())
872 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000873
John McCall8e7d6562010-08-26 03:08:43 +0000874 if (getStorageClassAsWritten() == SC_Extern ||
875 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +0000876 for (const VarDecl *PrevVar = getPreviousDeclaration();
877 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
878 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
879 return DeclarationOnly;
880 }
881 }
Sebastian Redl35351a92010-01-31 22:27:38 +0000882 // C99 6.9.2p2:
883 // A declaration of an object that has file scope without an initializer,
884 // and without a storage class specifier or the scs 'static', constitutes
885 // a tentative definition.
886 // No such thing in C++.
887 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
888 return TentativeDefinition;
889
890 // What's left is (in C, block-scope) declarations without initializers or
891 // external storage. These are definitions.
892 return Definition;
893}
894
Sebastian Redl35351a92010-01-31 22:27:38 +0000895VarDecl *VarDecl::getActingDefinition() {
896 DefinitionKind Kind = isThisDeclarationADefinition();
897 if (Kind != TentativeDefinition)
898 return 0;
899
Chris Lattner48eb14d2010-06-14 18:31:46 +0000900 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +0000901 VarDecl *First = getFirstDeclaration();
902 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
903 I != E; ++I) {
904 Kind = (*I)->isThisDeclarationADefinition();
905 if (Kind == Definition)
906 return 0;
907 else if (Kind == TentativeDefinition)
908 LastTentative = *I;
909 }
910 return LastTentative;
911}
912
913bool VarDecl::isTentativeDefinitionNow() const {
914 DefinitionKind Kind = isThisDeclarationADefinition();
915 if (Kind != TentativeDefinition)
916 return false;
917
918 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
919 if ((*I)->isThisDeclarationADefinition() == Definition)
920 return false;
921 }
Sebastian Redl5ca79842010-02-01 20:16:42 +0000922 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +0000923}
924
Sebastian Redl5ca79842010-02-01 20:16:42 +0000925VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +0000926 VarDecl *First = getFirstDeclaration();
927 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
928 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000929 if ((*I)->isThisDeclarationADefinition() == Definition)
930 return *I;
931 }
932 return 0;
933}
934
935const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +0000936 redecl_iterator I = redecls_begin(), E = redecls_end();
937 while (I != E && !I->getInit())
938 ++I;
939
940 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000941 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +0000942 return I->getInit();
943 }
944 return 0;
945}
946
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000947bool VarDecl::isOutOfLine() const {
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000948 if (Decl::isOutOfLine())
949 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +0000950
951 if (!isStaticDataMember())
952 return false;
953
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000954 // If this static data member was instantiated from a static data member of
955 // a class template, check whether that static data member was defined
956 // out-of-line.
957 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
958 return VD->isOutOfLine();
959
960 return false;
961}
962
Douglas Gregor1d957a32009-10-27 18:42:08 +0000963VarDecl *VarDecl::getOutOfLineDefinition() {
964 if (!isStaticDataMember())
965 return 0;
966
967 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
968 RD != RDEnd; ++RD) {
969 if (RD->getLexicalDeclContext()->isFileContext())
970 return *RD;
971 }
972
973 return 0;
974}
975
Douglas Gregord5058122010-02-11 01:19:42 +0000976void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +0000977 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
978 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +0000979 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +0000980 }
981
982 Init = I;
983}
984
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000985VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +0000986 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000987 return cast<VarDecl>(MSI->getInstantiatedFrom());
988
989 return 0;
990}
991
Douglas Gregor3c74d412009-10-14 20:14:33 +0000992TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +0000993 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +0000994 return MSI->getTemplateSpecializationKind();
995
996 return TSK_Undeclared;
997}
998
Douglas Gregor3cc3cde2009-10-14 21:29:40 +0000999MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001000 return getASTContext().getInstantiatedFromStaticDataMember(this);
1001}
1002
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001003void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1004 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001005 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001006 assert(MSI && "Not an instantiated static data member?");
1007 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001008 if (TSK != TSK_ExplicitSpecialization &&
1009 PointOfInstantiation.isValid() &&
1010 MSI->getPointOfInstantiation().isInvalid())
1011 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001012}
1013
Sebastian Redl833ef452010-01-26 22:01:41 +00001014//===----------------------------------------------------------------------===//
1015// ParmVarDecl Implementation
1016//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001017
Sebastian Redl833ef452010-01-26 22:01:41 +00001018ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
1019 SourceLocation L, IdentifierInfo *Id,
1020 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001021 StorageClass S, StorageClass SCAsWritten,
1022 Expr *DefArg) {
1023 return new (C) ParmVarDecl(ParmVar, DC, L, Id, T, TInfo,
1024 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001025}
1026
Sebastian Redl833ef452010-01-26 22:01:41 +00001027Expr *ParmVarDecl::getDefaultArg() {
1028 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1029 assert(!hasUninstantiatedDefaultArg() &&
1030 "Default argument is not yet instantiated!");
1031
1032 Expr *Arg = getInit();
1033 if (CXXExprWithTemporaries *E = dyn_cast_or_null<CXXExprWithTemporaries>(Arg))
1034 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001035
Sebastian Redl833ef452010-01-26 22:01:41 +00001036 return Arg;
1037}
1038
1039unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
1040 if (const CXXExprWithTemporaries *E =
1041 dyn_cast<CXXExprWithTemporaries>(getInit()))
1042 return E->getNumTemporaries();
1043
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001044 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001045}
1046
Sebastian Redl833ef452010-01-26 22:01:41 +00001047CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1048 assert(getNumDefaultArgTemporaries() &&
1049 "Default arguments does not have any temporaries!");
1050
1051 CXXExprWithTemporaries *E = cast<CXXExprWithTemporaries>(getInit());
1052 return E->getTemporary(i);
1053}
1054
1055SourceRange ParmVarDecl::getDefaultArgRange() const {
1056 if (const Expr *E = getInit())
1057 return E->getSourceRange();
1058
1059 if (hasUninstantiatedDefaultArg())
1060 return getUninstantiatedDefaultArg()->getSourceRange();
1061
1062 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001063}
1064
Nuno Lopes394ec982008-12-17 23:39:55 +00001065//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001066// FunctionDecl Implementation
1067//===----------------------------------------------------------------------===//
1068
John McCalle1f2ec22009-09-11 06:45:03 +00001069void FunctionDecl::getNameForDiagnostic(std::string &S,
1070 const PrintingPolicy &Policy,
1071 bool Qualified) const {
1072 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1073 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1074 if (TemplateArgs)
1075 S += TemplateSpecializationType::PrintTemplateArgumentList(
1076 TemplateArgs->getFlatArgumentList(),
1077 TemplateArgs->flat_size(),
1078 Policy);
1079
1080}
Ted Kremenekce20e8f2008-05-20 00:43:19 +00001081
Ted Kremenek186a0742010-04-29 16:49:01 +00001082bool FunctionDecl::isVariadic() const {
1083 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1084 return FT->isVariadic();
1085 return false;
1086}
1087
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001088bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1089 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1090 if (I->Body) {
1091 Definition = *I;
1092 return true;
1093 }
1094 }
1095
1096 return false;
1097}
1098
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001099Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001100 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1101 if (I->Body) {
1102 Definition = *I;
1103 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001104 }
1105 }
1106
1107 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001108}
1109
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001110void FunctionDecl::setBody(Stmt *B) {
1111 Body = B;
Argyrios Kyrtzidis49abd4d2009-06-22 17:13:31 +00001112 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001113 EndRangeLoc = B->getLocEnd();
1114}
1115
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001116void FunctionDecl::setPure(bool P) {
1117 IsPure = P;
1118 if (P)
1119 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1120 Parent->markedVirtualFunctionPure();
1121}
1122
Douglas Gregor16618f22009-09-12 00:17:51 +00001123bool FunctionDecl::isMain() const {
1124 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001125 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001126 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001127 getIdentifier() && getIdentifier()->isStr("main");
1128}
1129
Douglas Gregor16618f22009-09-12 00:17:51 +00001130bool FunctionDecl::isExternC() const {
1131 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001132 // In C, any non-static, non-overloadable function has external
1133 // linkage.
1134 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001135 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001136
Mike Stump11289f42009-09-09 15:08:12 +00001137 for (const DeclContext *DC = getDeclContext(); !DC->isTranslationUnit();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001138 DC = DC->getParent()) {
1139 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1140 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001141 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001142 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001143
1144 break;
1145 }
Douglas Gregor175ea042010-08-17 16:09:23 +00001146
1147 if (DC->isRecord())
1148 break;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001149 }
1150
Douglas Gregorbff62032010-10-21 16:57:46 +00001151 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001152}
1153
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001154bool FunctionDecl::isGlobal() const {
1155 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1156 return Method->isStatic();
1157
John McCall8e7d6562010-08-26 03:08:43 +00001158 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001159 return false;
1160
Mike Stump11289f42009-09-09 15:08:12 +00001161 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001162 DC->isNamespace();
1163 DC = DC->getParent()) {
1164 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1165 if (!Namespace->getDeclName())
1166 return false;
1167 break;
1168 }
1169 }
1170
1171 return true;
1172}
1173
Sebastian Redl833ef452010-01-26 22:01:41 +00001174void
1175FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1176 redeclarable_base::setPreviousDeclaration(PrevDecl);
1177
1178 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1179 FunctionTemplateDecl *PrevFunTmpl
1180 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1181 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1182 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1183 }
1184}
1185
1186const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1187 return getFirstDeclaration();
1188}
1189
1190FunctionDecl *FunctionDecl::getCanonicalDecl() {
1191 return getFirstDeclaration();
1192}
1193
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001194/// \brief Returns a value indicating whether this function
1195/// corresponds to a builtin function.
1196///
1197/// The function corresponds to a built-in function if it is
1198/// declared at translation scope or within an extern "C" block and
1199/// its name matches with the name of a builtin. The returned value
1200/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001201/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001202/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001203unsigned FunctionDecl::getBuiltinID() const {
1204 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001205 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1206 return 0;
1207
1208 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1209 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1210 return BuiltinID;
1211
1212 // This function has the name of a known C library
1213 // function. Determine whether it actually refers to the C library
1214 // function or whether it just has the same name.
1215
Douglas Gregora908e7f2009-02-17 03:23:10 +00001216 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001217 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001218 return 0;
1219
Douglas Gregore711f702009-02-14 18:57:46 +00001220 // If this function is at translation-unit scope and we're not in
1221 // C++, it refers to the C library function.
1222 if (!Context.getLangOptions().CPlusPlus &&
1223 getDeclContext()->isTranslationUnit())
1224 return BuiltinID;
1225
1226 // If the function is in an extern "C" linkage specification and is
1227 // not marked "overloadable", it's the real function.
1228 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001229 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001230 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001231 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001232 return BuiltinID;
1233
1234 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001235 return 0;
1236}
1237
1238
Chris Lattner47c0d002009-04-25 06:03:53 +00001239/// getNumParams - Return the number of parameters this function must have
Chris Lattner9af40c12009-04-25 06:12:16 +00001240/// based on its FunctionType. This is the length of the PararmInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001241/// after it has been created.
1242unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001243 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001244 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001245 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001246 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001247
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001248}
1249
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001250void FunctionDecl::setParams(ASTContext &C,
1251 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001252 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001253 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001254
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001255 // Zero params -> null pointer.
1256 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001257 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001258 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001259 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001260
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001261 // Update source range. The check below allows us to set EndRangeLoc before
1262 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001263 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001264 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001265 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001266}
Chris Lattner41943152007-01-25 04:52:46 +00001267
Chris Lattner58258242008-04-10 02:22:51 +00001268/// getMinRequiredArguments - Returns the minimum number of arguments
1269/// needed to call this function. This may be fewer than the number of
1270/// function parameters, if some of the parameters have default
Chris Lattnerb0d38442008-04-12 23:52:44 +00001271/// arguments (in C++).
Chris Lattner58258242008-04-10 02:22:51 +00001272unsigned FunctionDecl::getMinRequiredArguments() const {
1273 unsigned NumRequiredArgs = getNumParams();
1274 while (NumRequiredArgs > 0
Anders Carlsson85446472009-06-06 04:14:07 +00001275 && getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001276 --NumRequiredArgs;
1277
1278 return NumRequiredArgs;
1279}
1280
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001281bool FunctionDecl::isInlined() const {
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001282 // FIXME: This is not enough. Consider:
1283 //
1284 // inline void f();
1285 // void f() { }
1286 //
1287 // f is inlined, but does not have inline specified.
1288 // To fix this we should add an 'inline' flag to FunctionDecl.
1289 if (isInlineSpecified())
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001290 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001291
1292 if (isa<CXXMethodDecl>(this)) {
1293 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1294 return true;
1295 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001296
1297 switch (getTemplateSpecializationKind()) {
1298 case TSK_Undeclared:
1299 case TSK_ExplicitSpecialization:
1300 return false;
1301
1302 case TSK_ImplicitInstantiation:
1303 case TSK_ExplicitInstantiationDeclaration:
1304 case TSK_ExplicitInstantiationDefinition:
1305 // Handle below.
1306 break;
1307 }
1308
1309 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001310 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001311 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001312 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001313
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001314 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001315 return PatternDecl->isInlined();
1316
1317 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001318}
1319
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001320/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001321/// definition will be externally visible.
1322///
1323/// Inline function definitions are always available for inlining optimizations.
1324/// However, depending on the language dialect, declaration specifiers, and
1325/// attributes, the definition of an inline function may or may not be
1326/// "externally" visible to other translation units in the program.
1327///
1328/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001329/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001330/// inline definition becomes externally visible (C99 6.7.4p6).
1331///
1332/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1333/// definition, we use the GNU semantics for inline, which are nearly the
1334/// opposite of C99 semantics. In particular, "inline" by itself will create
1335/// an externally visible symbol, but "extern inline" will not create an
1336/// externally visible symbol.
1337bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1338 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001339 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001340 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001341
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001342 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor299d76e2009-09-13 07:46:26 +00001343 // GNU inline semantics. Based on a number of examples, we came up with the
1344 // following heuristic: if the "inline" keyword is present on a
1345 // declaration of the function but "extern" is not present on that
1346 // declaration, then the symbol is externally visible. Otherwise, the GNU
1347 // "extern inline" semantics applies and the symbol is not externally
1348 // visible.
1349 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1350 Redecl != RedeclEnd;
1351 ++Redecl) {
John McCall8e7d6562010-08-26 03:08:43 +00001352 if (Redecl->isInlineSpecified() && Redecl->getStorageClass() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001353 return true;
1354 }
1355
1356 // GNU "extern inline" semantics; no externally visible symbol.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001357 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001358 }
1359
1360 // C99 6.7.4p6:
1361 // [...] If all of the file scope declarations for a function in a
1362 // translation unit include the inline function specifier without extern,
1363 // then the definition in that translation unit is an inline definition.
1364 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1365 Redecl != RedeclEnd;
1366 ++Redecl) {
1367 // Only consider file-scope declarations in this test.
1368 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1369 continue;
1370
John McCall8e7d6562010-08-26 03:08:43 +00001371 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001372 return true; // Not an inline definition
1373 }
1374
1375 // C99 6.7.4p6:
1376 // An inline definition does not provide an external definition for the
1377 // function, and does not forbid an external definition in another
1378 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001379 return false;
1380}
1381
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001382/// getOverloadedOperator - Which C++ overloaded operator this
1383/// function represents, if any.
1384OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001385 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1386 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001387 else
1388 return OO_None;
1389}
1390
Alexis Huntc88db062010-01-13 09:01:02 +00001391/// getLiteralIdentifier - The literal suffix identifier this function
1392/// represents, if any.
1393const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1394 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1395 return getDeclName().getCXXLiteralIdentifier();
1396 else
1397 return 0;
1398}
1399
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001400FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1401 if (TemplateOrSpecialization.isNull())
1402 return TK_NonTemplate;
1403 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1404 return TK_FunctionTemplate;
1405 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1406 return TK_MemberSpecialization;
1407 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1408 return TK_FunctionTemplateSpecialization;
1409 if (TemplateOrSpecialization.is
1410 <DependentFunctionTemplateSpecializationInfo*>())
1411 return TK_DependentFunctionTemplateSpecialization;
1412
1413 assert(false && "Did we miss a TemplateOrSpecialization type?");
1414 return TK_NonTemplate;
1415}
1416
Douglas Gregord801b062009-10-07 23:56:10 +00001417FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001418 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001419 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1420
1421 return 0;
1422}
1423
Douglas Gregor06db9f52009-10-12 20:18:28 +00001424MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1425 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1426}
1427
Douglas Gregord801b062009-10-07 23:56:10 +00001428void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001429FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1430 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001431 TemplateSpecializationKind TSK) {
1432 assert(TemplateOrSpecialization.isNull() &&
1433 "Member function is already a specialization");
1434 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001435 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001436 TemplateOrSpecialization = Info;
1437}
1438
Douglas Gregorafca3b42009-10-27 20:53:28 +00001439bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001440 // If the function is invalid, it can't be implicitly instantiated.
1441 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001442 return false;
1443
1444 switch (getTemplateSpecializationKind()) {
1445 case TSK_Undeclared:
1446 case TSK_ExplicitSpecialization:
1447 case TSK_ExplicitInstantiationDefinition:
1448 return false;
1449
1450 case TSK_ImplicitInstantiation:
1451 return true;
1452
1453 case TSK_ExplicitInstantiationDeclaration:
1454 // Handled below.
1455 break;
1456 }
1457
1458 // Find the actual template from which we will instantiate.
1459 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001460 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001461 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001462 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001463
1464 // C++0x [temp.explicit]p9:
1465 // Except for inline functions, other explicit instantiation declarations
1466 // have the effect of suppressing the implicit instantiation of the entity
1467 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001468 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001469 return true;
1470
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001471 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001472}
1473
1474FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1475 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1476 while (Primary->getInstantiatedFromMemberTemplate()) {
1477 // If we have hit a point where the user provided a specialization of
1478 // this template, we're done looking.
1479 if (Primary->isMemberSpecialization())
1480 break;
1481
1482 Primary = Primary->getInstantiatedFromMemberTemplate();
1483 }
1484
1485 return Primary->getTemplatedDecl();
1486 }
1487
1488 return getInstantiatedFromMemberFunction();
1489}
1490
Douglas Gregor70d83e22009-06-29 17:30:29 +00001491FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001492 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001493 = TemplateOrSpecialization
1494 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001495 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001496 }
1497 return 0;
1498}
1499
1500const TemplateArgumentList *
1501FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001502 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001503 = TemplateOrSpecialization
1504 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001505 return Info->TemplateArguments;
1506 }
1507 return 0;
1508}
1509
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001510const TemplateArgumentListInfo *
1511FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1512 if (FunctionTemplateSpecializationInfo *Info
1513 = TemplateOrSpecialization
1514 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1515 return Info->TemplateArgumentsAsWritten;
1516 }
1517 return 0;
1518}
1519
Mike Stump11289f42009-09-09 15:08:12 +00001520void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001521FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1522 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001523 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001524 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001525 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001526 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1527 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001528 assert(TSK != TSK_Undeclared &&
1529 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001530 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001531 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001532 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001533 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1534 TemplateArgs,
1535 TemplateArgsAsWritten,
1536 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001537 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001538
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001539 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001540 // function template specializations.
1541 if (InsertPos)
1542 Template->getSpecializations().InsertNode(Info, InsertPos);
1543 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001544 // Try to insert the new node. If there is an existing node, leave it, the
1545 // set will contain the canonical decls while
1546 // FunctionTemplateDecl::findSpecialization will return
1547 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001548 FunctionTemplateSpecializationInfo *Existing
1549 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001550 (void)Existing;
1551 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1552 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001553 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001554}
1555
John McCallb9c78482010-04-08 09:05:18 +00001556void
1557FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1558 const UnresolvedSetImpl &Templates,
1559 const TemplateArgumentListInfo &TemplateArgs) {
1560 assert(TemplateOrSpecialization.isNull());
1561 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1562 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001563 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001564 void *Buffer = Context.Allocate(Size);
1565 DependentFunctionTemplateSpecializationInfo *Info =
1566 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1567 TemplateArgs);
1568 TemplateOrSpecialization = Info;
1569}
1570
1571DependentFunctionTemplateSpecializationInfo::
1572DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1573 const TemplateArgumentListInfo &TArgs)
1574 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1575
1576 d.NumTemplates = Ts.size();
1577 d.NumArgs = TArgs.size();
1578
1579 FunctionTemplateDecl **TsArray =
1580 const_cast<FunctionTemplateDecl**>(getTemplates());
1581 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1582 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1583
1584 TemplateArgumentLoc *ArgsArray =
1585 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1586 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1587 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1588}
1589
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001590TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001591 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001592 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001593 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001594 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001595 if (FTSInfo)
1596 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001597
Douglas Gregord801b062009-10-07 23:56:10 +00001598 MemberSpecializationInfo *MSInfo
1599 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1600 if (MSInfo)
1601 return MSInfo->getTemplateSpecializationKind();
1602
1603 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001604}
1605
Mike Stump11289f42009-09-09 15:08:12 +00001606void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001607FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1608 SourceLocation PointOfInstantiation) {
1609 if (FunctionTemplateSpecializationInfo *FTSInfo
1610 = TemplateOrSpecialization.dyn_cast<
1611 FunctionTemplateSpecializationInfo*>()) {
1612 FTSInfo->setTemplateSpecializationKind(TSK);
1613 if (TSK != TSK_ExplicitSpecialization &&
1614 PointOfInstantiation.isValid() &&
1615 FTSInfo->getPointOfInstantiation().isInvalid())
1616 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1617 } else if (MemberSpecializationInfo *MSInfo
1618 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1619 MSInfo->setTemplateSpecializationKind(TSK);
1620 if (TSK != TSK_ExplicitSpecialization &&
1621 PointOfInstantiation.isValid() &&
1622 MSInfo->getPointOfInstantiation().isInvalid())
1623 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1624 } else
1625 assert(false && "Function cannot have a template specialization kind");
1626}
1627
1628SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001629 if (FunctionTemplateSpecializationInfo *FTSInfo
1630 = TemplateOrSpecialization.dyn_cast<
1631 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001632 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001633 else if (MemberSpecializationInfo *MSInfo
1634 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001635 return MSInfo->getPointOfInstantiation();
1636
1637 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001638}
1639
Douglas Gregor6411b922009-09-11 20:15:17 +00001640bool FunctionDecl::isOutOfLine() const {
Douglas Gregor6411b922009-09-11 20:15:17 +00001641 if (Decl::isOutOfLine())
1642 return true;
1643
1644 // If this function was instantiated from a member function of a
1645 // class template, check whether that member function was defined out-of-line.
1646 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1647 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001648 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001649 return Definition->isOutOfLine();
1650 }
1651
1652 // If this function was instantiated from a function template,
1653 // check whether that function template was defined out-of-line.
1654 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1655 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001656 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001657 return Definition->isOutOfLine();
1658 }
1659
1660 return false;
1661}
1662
Chris Lattner59a25942008-03-31 00:36:02 +00001663//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001664// FieldDecl Implementation
1665//===----------------------------------------------------------------------===//
1666
1667FieldDecl *FieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1668 IdentifierInfo *Id, QualType T,
1669 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
1670 return new (C) FieldDecl(Decl::Field, DC, L, Id, T, TInfo, BW, Mutable);
1671}
1672
1673bool FieldDecl::isAnonymousStructOrUnion() const {
1674 if (!isImplicit() || getDeclName())
1675 return false;
1676
1677 if (const RecordType *Record = getType()->getAs<RecordType>())
1678 return Record->getDecl()->isAnonymousStructOrUnion();
1679
1680 return false;
1681}
1682
1683//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001684// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00001685//===----------------------------------------------------------------------===//
1686
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001687SourceLocation TagDecl::getOuterLocStart() const {
1688 return getTemplateOrInnerLocStart(this);
1689}
1690
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001691SourceRange TagDecl::getSourceRange() const {
1692 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001693 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00001694}
1695
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001696TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001697 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00001698}
1699
Douglas Gregora72a4e32010-05-19 18:39:18 +00001700void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
1701 TypedefDeclOrQualifier = TDD;
1702 if (TypeForDecl)
1703 TypeForDecl->ClearLinkageCache();
1704}
1705
Douglas Gregordee1be82009-01-17 00:42:38 +00001706void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001707 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00001708
1709 if (isa<CXXRecordDecl>(this)) {
1710 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
1711 struct CXXRecordDecl::DefinitionData *Data =
1712 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00001713 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
1714 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00001715 }
Douglas Gregordee1be82009-01-17 00:42:38 +00001716}
1717
1718void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00001719 assert((!isa<CXXRecordDecl>(this) ||
1720 cast<CXXRecordDecl>(this)->hasDefinition()) &&
1721 "definition completed but not started");
1722
Douglas Gregordee1be82009-01-17 00:42:38 +00001723 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00001724 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00001725
1726 if (ASTMutationListener *L = getASTMutationListener())
1727 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00001728}
1729
Douglas Gregor0a5a2212010-02-11 01:04:33 +00001730TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001731 if (isDefinition())
1732 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00001733 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
1734 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00001735
1736 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001737 R != REnd; ++R)
1738 if (R->isDefinition())
1739 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00001740
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001741 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00001742}
1743
John McCall3e11ebe2010-03-15 10:12:16 +00001744void TagDecl::setQualifierInfo(NestedNameSpecifier *Qualifier,
1745 SourceRange QualifierRange) {
1746 if (Qualifier) {
1747 // Make sure the extended qualifier info is allocated.
1748 if (!hasExtInfo())
1749 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
1750 // Set qualifier info.
1751 getExtInfo()->NNS = Qualifier;
1752 getExtInfo()->NNSRange = QualifierRange;
1753 }
1754 else {
1755 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
1756 assert(QualifierRange.isInvalid());
1757 if (hasExtInfo()) {
1758 getASTContext().Deallocate(getExtInfo());
1759 TypedefDeclOrQualifier = (TypedefDecl*) 0;
1760 }
1761 }
1762}
1763
Ted Kremenek21475702008-09-05 17:16:31 +00001764//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00001765// EnumDecl Implementation
1766//===----------------------------------------------------------------------===//
1767
1768EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1769 IdentifierInfo *Id, SourceLocation TKL,
Douglas Gregor0bf31402010-10-08 23:50:27 +00001770 EnumDecl *PrevDecl, bool IsScoped, bool IsFixed) {
1771 EnumDecl *Enum = new (C) EnumDecl(DC, L, Id, PrevDecl, TKL,
1772 IsScoped, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00001773 C.getTypeDeclType(Enum, PrevDecl);
1774 return Enum;
1775}
1776
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001777EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00001778 return new (C) EnumDecl(0, SourceLocation(), 0, 0, SourceLocation(),
1779 false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001780}
1781
Douglas Gregord5058122010-02-11 01:19:42 +00001782void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00001783 QualType NewPromotionType,
1784 unsigned NumPositiveBits,
1785 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001786 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00001787 if (!IntegerType)
1788 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00001789 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00001790 setNumPositiveBits(NumPositiveBits);
1791 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00001792 TagDecl::completeDefinition();
1793}
1794
1795//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001796// RecordDecl Implementation
1797//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00001798
Argyrios Kyrtzidis88e1b972008-10-15 00:42:39 +00001799RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L,
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001800 IdentifierInfo *Id, RecordDecl *PrevDecl,
1801 SourceLocation TKL)
1802 : TagDecl(DK, TK, DC, L, Id, PrevDecl, TKL) {
Ted Kremenek52baf502008-09-02 21:12:32 +00001803 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00001804 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001805 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001806 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00001807 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00001808}
1809
1810RecordDecl *RecordDecl::Create(ASTContext &C, TagKind TK, DeclContext *DC,
Ted Kremenek21475702008-09-05 17:16:31 +00001811 SourceLocation L, IdentifierInfo *Id,
Douglas Gregor82fe3e32009-07-21 14:46:17 +00001812 SourceLocation TKL, RecordDecl* PrevDecl) {
Mike Stump11289f42009-09-09 15:08:12 +00001813
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00001814 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, L, Id, PrevDecl, TKL);
Ted Kremenek21475702008-09-05 17:16:31 +00001815 C.getTypeDeclType(R, PrevDecl);
1816 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00001817}
1818
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001819RecordDecl *RecordDecl::Create(ASTContext &C, EmptyShell Empty) {
1820 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), 0, 0,
1821 SourceLocation());
1822}
1823
Douglas Gregordfcad112009-03-25 15:59:44 +00001824bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00001825 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00001826 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
1827}
1828
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001829RecordDecl::field_iterator RecordDecl::field_begin() const {
1830 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
1831 LoadFieldsFromExternalStorage();
1832
1833 return field_iterator(decl_iterator(FirstDecl));
1834}
1835
Douglas Gregor91f84212008-12-11 16:49:14 +00001836/// completeDefinition - Notes that the definition of this type is now
1837/// complete.
Douglas Gregord5058122010-02-11 01:19:42 +00001838void RecordDecl::completeDefinition() {
Chris Lattner41943152007-01-25 04:52:46 +00001839 assert(!isDefinition() && "Cannot redefine record!");
Douglas Gregordee1be82009-01-17 00:42:38 +00001840 TagDecl::completeDefinition();
Chris Lattner41943152007-01-25 04:52:46 +00001841}
Steve Naroffcc321422007-03-26 23:09:51 +00001842
John McCall61925b02010-05-21 01:17:40 +00001843ValueDecl *RecordDecl::getAnonymousStructOrUnionObject() {
1844 // Force the decl chain to come into existence properly.
1845 if (!getNextDeclInContext()) getParent()->decls_begin();
1846
1847 assert(isAnonymousStructOrUnion());
1848 ValueDecl *D = cast<ValueDecl>(getNextDeclInContext());
1849 assert(D->getType()->isRecordType());
1850 assert(D->getType()->getAs<RecordType>()->getDecl() == this);
1851 return D;
1852}
1853
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00001854void RecordDecl::LoadFieldsFromExternalStorage() const {
1855 ExternalASTSource *Source = getASTContext().getExternalSource();
1856 assert(hasExternalLexicalStorage() && Source && "No external storage?");
1857
1858 // Notify that we have a RecordDecl doing some initialization.
1859 ExternalASTSource::Deserializing TheFields(Source);
1860
1861 llvm::SmallVector<Decl*, 64> Decls;
1862 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
1863 return;
1864
1865#ifndef NDEBUG
1866 // Check that all decls we got were FieldDecls.
1867 for (unsigned i=0, e=Decls.size(); i != e; ++i)
1868 assert(isa<FieldDecl>(Decls[i]));
1869#endif
1870
1871 LoadedFieldsFromExternalStorage = true;
1872
1873 if (Decls.empty())
1874 return;
1875
1876 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
1877}
1878
Steve Naroff415d3d52008-10-08 17:01:13 +00001879//===----------------------------------------------------------------------===//
1880// BlockDecl Implementation
1881//===----------------------------------------------------------------------===//
1882
Douglas Gregord5058122010-02-11 01:19:42 +00001883void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00001884 unsigned NParms) {
1885 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00001886
Steve Naroffc4b30e52009-03-13 16:56:44 +00001887 // Zero params -> null pointer.
1888 if (NParms) {
1889 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00001890 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00001891 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
1892 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
1893 }
1894}
1895
1896unsigned BlockDecl::getNumParams() const {
1897 return NumParams;
1898}
Sebastian Redl833ef452010-01-26 22:01:41 +00001899
1900
1901//===----------------------------------------------------------------------===//
1902// Other Decl Allocation/Deallocation Method Implementations
1903//===----------------------------------------------------------------------===//
1904
1905TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
1906 return new (C) TranslationUnitDecl(C);
1907}
1908
1909NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
1910 SourceLocation L, IdentifierInfo *Id) {
1911 return new (C) NamespaceDecl(DC, L, Id);
1912}
1913
Sebastian Redl833ef452010-01-26 22:01:41 +00001914ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
1915 SourceLocation L, IdentifierInfo *Id, QualType T) {
1916 return new (C) ImplicitParamDecl(ImplicitParam, DC, L, Id, T);
1917}
1918
1919FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001920 const DeclarationNameInfo &NameInfo,
1921 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001922 StorageClass S, StorageClass SCAsWritten,
1923 bool isInline, bool hasWrittenPrototype) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001924 FunctionDecl *New = new (C) FunctionDecl(Function, DC, NameInfo, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001925 S, SCAsWritten, isInline);
Sebastian Redl833ef452010-01-26 22:01:41 +00001926 New->HasWrittenPrototype = hasWrittenPrototype;
1927 return New;
1928}
1929
1930BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
1931 return new (C) BlockDecl(DC, L);
1932}
1933
1934EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
1935 SourceLocation L,
1936 IdentifierInfo *Id, QualType T,
1937 Expr *E, const llvm::APSInt &V) {
1938 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
1939}
1940
Douglas Gregorbe996932010-09-01 20:41:53 +00001941SourceRange EnumConstantDecl::getSourceRange() const {
1942 SourceLocation End = getLocation();
1943 if (Init)
1944 End = Init->getLocEnd();
1945 return SourceRange(getLocation(), End);
1946}
1947
Sebastian Redl833ef452010-01-26 22:01:41 +00001948TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
1949 SourceLocation L, IdentifierInfo *Id,
1950 TypeSourceInfo *TInfo) {
1951 return new (C) TypedefDecl(DC, L, Id, TInfo);
1952}
1953
Sebastian Redl833ef452010-01-26 22:01:41 +00001954FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
1955 SourceLocation L,
1956 StringLiteral *Str) {
1957 return new (C) FileScopeAsmDecl(DC, L, Str);
1958}