blob: 7ca936abc91b20abbd7af6a3ceda5b4958eb84fe [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"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000028#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000029#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000030
Chris Lattner6d9a6852006-10-25 05:11:20 +000031using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000032
Chris Lattner88f70d62008-03-15 05:43:15 +000033//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000034// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000035//===----------------------------------------------------------------------===//
36
Douglas Gregor1baf38f2011-03-26 12:10:19 +000037static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
38 // If this declaration has an explicit visibility attribute, use it.
39 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
40 switch (A->getVisibility()) {
41 case VisibilityAttr::Default:
42 return DefaultVisibility;
43 case VisibilityAttr::Hidden:
44 return HiddenVisibility;
45 case VisibilityAttr::Protected:
46 return ProtectedVisibility;
47 }
John McCall659a3372010-12-18 03:30:47 +000048
John McCall457a04e2010-10-22 21:05:15 +000049 return DefaultVisibility;
John McCall457a04e2010-10-22 21:05:15 +000050 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +000051
52 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
53 // implies visibility(default).
Daniel Dunbar14ad22f2011-04-19 21:43:27 +000054 if (D->getASTContext().Target.getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +000055 for (specific_attr_iterator<AvailabilityAttr>
56 A = D->specific_attr_begin<AvailabilityAttr>(),
57 AEnd = D->specific_attr_end<AvailabilityAttr>();
58 A != AEnd; ++A)
59 if ((*A)->getPlatform()->getName().equals("macosx"))
60 return DefaultVisibility;
61 }
62
63 return llvm::Optional<Visibility>();
John McCall457a04e2010-10-22 21:05:15 +000064}
65
John McCallc273f242010-10-30 11:50:40 +000066typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall457a04e2010-10-22 21:05:15 +000067typedef std::pair<Linkage,Visibility> LVPair;
John McCallc273f242010-10-30 11:50:40 +000068
John McCall457a04e2010-10-22 21:05:15 +000069static LVPair merge(LVPair L, LVPair R) {
70 return LVPair(minLinkage(L.first, R.first),
71 minVisibility(L.second, R.second));
72}
73
John McCallc273f242010-10-30 11:50:40 +000074static LVPair merge(LVPair L, LinkageInfo R) {
75 return LVPair(minLinkage(L.first, R.linkage()),
76 minVisibility(L.second, R.visibility()));
77}
78
Benjamin Kramer396dcf32010-11-05 19:56:37 +000079namespace {
John McCall07072662010-11-02 01:45:15 +000080/// Flags controlling the computation of linkage and visibility.
81struct LVFlags {
82 bool ConsiderGlobalVisibility;
83 bool ConsiderVisibilityAttributes;
John McCall8bc6d5b2011-03-04 10:39:25 +000084 bool ConsiderTemplateParameterTypes;
John McCall07072662010-11-02 01:45:15 +000085
86 LVFlags() : ConsiderGlobalVisibility(true),
John McCall8bc6d5b2011-03-04 10:39:25 +000087 ConsiderVisibilityAttributes(true),
88 ConsiderTemplateParameterTypes(true) {
John McCall07072662010-11-02 01:45:15 +000089 }
90
Douglas Gregorbf62d642010-12-06 18:36:25 +000091 /// \brief Returns a set of flags that is only useful for computing the
92 /// linkage, not the visibility, of a declaration.
93 static LVFlags CreateOnlyDeclLinkage() {
94 LVFlags F;
95 F.ConsiderGlobalVisibility = false;
96 F.ConsiderVisibilityAttributes = false;
John McCall8bc6d5b2011-03-04 10:39:25 +000097 F.ConsiderTemplateParameterTypes = false;
Douglas Gregorbf62d642010-12-06 18:36:25 +000098 return F;
99 }
100
John McCall07072662010-11-02 01:45:15 +0000101 /// Returns a set of flags, otherwise based on these, which ignores
102 /// off all sources of visibility except template arguments.
103 LVFlags onlyTemplateVisibility() const {
104 LVFlags F = *this;
105 F.ConsiderGlobalVisibility = false;
106 F.ConsiderVisibilityAttributes = false;
John McCall8bc6d5b2011-03-04 10:39:25 +0000107 F.ConsiderTemplateParameterTypes = false;
John McCall07072662010-11-02 01:45:15 +0000108 return F;
109 }
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000110};
Benjamin Kramer396dcf32010-11-05 19:56:37 +0000111} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +0000112
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000113/// \brief Get the most restrictive linkage for the types in the given
114/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +0000115static LVPair
116getLVForTemplateParameterList(const TemplateParameterList *Params) {
117 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000118 for (TemplateParameterList::const_iterator P = Params->begin(),
119 PEnd = Params->end();
120 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000121 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
122 if (NTTP->isExpandedParameterPack()) {
123 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
124 QualType T = NTTP->getExpansionType(I);
125 if (!T->isDependentType())
126 LV = merge(LV, T->getLinkageAndVisibility());
127 }
128 continue;
129 }
130
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000131 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +0000132 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000133 continue;
134 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000135 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000136
137 if (TemplateTemplateParmDecl *TTP
138 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +0000139 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000140 }
141 }
142
John McCall457a04e2010-10-22 21:05:15 +0000143 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000144}
145
Douglas Gregorbf62d642010-12-06 18:36:25 +0000146/// getLVForDecl - Get the linkage and visibility for the given declaration.
147static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
148
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000149/// \brief Get the most restrictive linkage for the types and
150/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +0000151static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregorbf62d642010-12-06 18:36:25 +0000152 unsigned NumArgs,
153 LVFlags &F) {
John McCall457a04e2010-10-22 21:05:15 +0000154 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000155
156 for (unsigned I = 0; I != NumArgs; ++I) {
157 switch (Args[I].getKind()) {
158 case TemplateArgument::Null:
159 case TemplateArgument::Integral:
160 case TemplateArgument::Expression:
161 break;
162
163 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000164 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000165 break;
166
167 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000168 // The decl can validly be null as the representation of nullptr
169 // arguments, valid only in C++0x.
170 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000171 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
172 LV = merge(LV, getLVForDecl(ND, F));
John McCall457a04e2010-10-22 21:05:15 +0000173 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000174 break;
175
176 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000177 case TemplateArgument::TemplateExpansion:
178 if (TemplateDecl *Template
179 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000180 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000181 break;
182
183 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000184 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregorbf62d642010-12-06 18:36:25 +0000185 Args[I].pack_size(),
186 F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000187 break;
188 }
189 }
190
John McCall457a04e2010-10-22 21:05:15 +0000191 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000192}
193
John McCallc273f242010-10-30 11:50:40 +0000194static LVPair
Douglas Gregorbf62d642010-12-06 18:36:25 +0000195getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
196 LVFlags &F) {
197 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall8823c652010-08-13 08:35:10 +0000198}
199
John McCall07072662010-11-02 01:45:15 +0000200static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000201 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000202 "Not a name having namespace scope");
203 ASTContext &Context = D->getASTContext();
204
205 // C++ [basic.link]p3:
206 // A name having namespace scope (3.3.6) has internal linkage if it
207 // is the name of
208 // - an object, reference, function or function template that is
209 // explicitly declared static; or,
210 // (This bullet corresponds to C99 6.2.2p3.)
211 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
212 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000213 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000214 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000215
216 // - an object or reference that is explicitly declared const
217 // and neither explicitly declared extern nor previously
218 // declared to have external linkage; or
219 // (there is no equivalent in C99)
220 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000221 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000222 Var->getStorageClass() != SC_Extern &&
223 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000224 bool FoundExtern = false;
225 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
226 PrevVar && !FoundExtern;
227 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000228 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000229 FoundExtern = true;
230
231 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000232 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000233 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000234 if (Var->getStorageClass() == SC_None) {
235 const VarDecl *PrevVar = Var->getPreviousDeclaration();
236 for (; PrevVar; PrevVar = PrevVar->getPreviousDeclaration())
237 if (PrevVar->getStorageClass() == SC_PrivateExtern)
238 break;
239 if (PrevVar)
240 return PrevVar->getLinkageAndVisibility();
241 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000242 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000243 // C++ [temp]p4:
244 // A non-member function template can have internal linkage; any
245 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000246 const FunctionDecl *Function = 0;
247 if (const FunctionTemplateDecl *FunTmpl
248 = dyn_cast<FunctionTemplateDecl>(D))
249 Function = FunTmpl->getTemplatedDecl();
250 else
251 Function = cast<FunctionDecl>(D);
252
253 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000254 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000255 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000256 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
257 // - a data member of an anonymous union.
258 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000259 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000260 }
261
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000262 if (D->isInAnonymousNamespace()) {
263 const VarDecl *Var = dyn_cast<VarDecl>(D);
264 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
265 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
266 return LinkageInfo::uniqueExternal();
267 }
John McCallb7139c42010-10-28 04:18:25 +0000268
John McCall457a04e2010-10-22 21:05:15 +0000269 // Set up the defaults.
270
271 // C99 6.2.2p5:
272 // If the declaration of an identifier for an object has file
273 // scope and no storage-class specifier, its linkage is
274 // external.
John McCallc273f242010-10-30 11:50:40 +0000275 LinkageInfo LV;
276
John McCall07072662010-11-02 01:45:15 +0000277 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000278 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
279 LV.setVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000280 F.ConsiderGlobalVisibility = false;
John McCall2faf32c2010-12-10 02:59:44 +0000281 } else {
282 // If we're declared in a namespace with a visibility attribute,
283 // use that namespace's visibility, but don't call it explicit.
284 for (const DeclContext *DC = D->getDeclContext();
285 !isa<TranslationUnitDecl>(DC);
286 DC = DC->getParent()) {
287 if (!isa<NamespaceDecl>(DC)) continue;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000288 if (llvm::Optional<Visibility> Vis
289 = cast<NamespaceDecl>(DC)->getExplicitVisibility()) {
290 LV.setVisibility(*Vis, false);
John McCall2faf32c2010-12-10 02:59:44 +0000291 F.ConsiderGlobalVisibility = false;
292 break;
293 }
294 }
John McCall07072662010-11-02 01:45:15 +0000295 }
John McCallc273f242010-10-30 11:50:40 +0000296 }
John McCall457a04e2010-10-22 21:05:15 +0000297
Douglas Gregorf73b2822009-11-25 22:24:25 +0000298 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000299
Douglas Gregorf73b2822009-11-25 22:24:25 +0000300 // A name having namespace scope has external linkage if it is the
301 // name of
302 //
303 // - an object or reference, unless it has internal linkage; or
304 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000305 // GCC applies the following optimization to variables and static
306 // data members, but not to functions:
307 //
John McCall457a04e2010-10-22 21:05:15 +0000308 // Modify the variable's LV by the LV of its type unless this is
309 // C or extern "C". This follows from [basic.link]p9:
310 // A type without linkage shall not be used as the type of a
311 // variable or function with external linkage unless
312 // - the entity has C language linkage, or
313 // - the entity is declared within an unnamed namespace, or
314 // - the entity is not used or is defined in the same
315 // translation unit.
316 // and [basic.link]p10:
317 // ...the types specified by all declarations referring to a
318 // given variable or function shall be identical...
319 // C does not have an equivalent rule.
320 //
John McCall5fe84122010-10-26 04:59:26 +0000321 // Ignore this if we've got an explicit attribute; the user
322 // probably knows what they're doing.
323 //
John McCall457a04e2010-10-22 21:05:15 +0000324 // Note that we don't want to make the variable non-external
325 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000326 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000327 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
328 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000329 return LinkageInfo::uniqueExternal();
330 if (!LV.visibilityExplicit())
331 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000332 }
333
John McCall23032652010-11-02 18:38:13 +0000334 if (Var->getStorageClass() == SC_PrivateExtern)
335 LV.setVisibility(HiddenVisibility, true);
336
Douglas Gregorf73b2822009-11-25 22:24:25 +0000337 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000338 (Var->getStorageClass() == SC_Extern ||
339 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000340
Douglas Gregorf73b2822009-11-25 22:24:25 +0000341 // C99 6.2.2p4:
342 // For an identifier declared with the storage-class specifier
343 // extern in a scope in which a prior declaration of that
344 // identifier is visible, if the prior declaration specifies
345 // internal or external linkage, the linkage of the identifier
346 // at the later declaration is the same as the linkage
347 // specified at the prior declaration. If no prior declaration
348 // is visible, or if the prior declaration specifies no
349 // linkage, then the identifier has external linkage.
350 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000351 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallc273f242010-10-30 11:50:40 +0000352 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
353 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000354 }
355 }
356
Douglas Gregorf73b2822009-11-25 22:24:25 +0000357 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000358 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000359 // In theory, we can modify the function's LV by the LV of its
360 // type unless it has C linkage (see comment above about variables
361 // for justification). In practice, GCC doesn't do this, so it's
362 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000363
John McCall23032652010-11-02 18:38:13 +0000364 if (Function->getStorageClass() == SC_PrivateExtern)
365 LV.setVisibility(HiddenVisibility, true);
366
Douglas Gregorf73b2822009-11-25 22:24:25 +0000367 // C99 6.2.2p5:
368 // If the declaration of an identifier for a function has no
369 // storage-class specifier, its linkage is determined exactly
370 // as if it were declared with the storage-class specifier
371 // extern.
372 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000373 (Function->getStorageClass() == SC_Extern ||
374 Function->getStorageClass() == SC_PrivateExtern ||
375 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000376 // C99 6.2.2p4:
377 // For an identifier declared with the storage-class specifier
378 // extern in a scope in which a prior declaration of that
379 // identifier is visible, if the prior declaration specifies
380 // internal or external linkage, the linkage of the identifier
381 // at the later declaration is the same as the linkage
382 // specified at the prior declaration. If no prior declaration
383 // is visible, or if the prior declaration specifies no
384 // linkage, then the identifier has external linkage.
385 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000386 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallc273f242010-10-30 11:50:40 +0000387 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
388 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000389 }
390 }
391
John McCallf768aa72011-02-10 06:50:24 +0000392 // In C++, then if the type of the function uses a type with
393 // unique-external linkage, it's not legally usable from outside
394 // this translation unit. However, we should use the C linkage
395 // rules instead for extern "C" declarations.
396 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
397 Function->getType()->getLinkage() == UniqueExternalLinkage)
398 return LinkageInfo::uniqueExternal();
399
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000400 if (FunctionTemplateSpecializationInfo *SpecInfo
401 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000402 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
403 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000404 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000405 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000406 }
407
Douglas Gregorf73b2822009-11-25 22:24:25 +0000408 // - a named class (Clause 9), or an unnamed class defined in a
409 // typedef declaration in which the class has the typedef name
410 // for linkage purposes (7.1.3); or
411 // - a named enumeration (7.2), or an unnamed enumeration
412 // defined in a typedef declaration in which the enumeration
413 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000414 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
415 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000416 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000417 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000418
John McCall457a04e2010-10-22 21:05:15 +0000419 // If this is a class template specialization, consider the
420 // linkage of the template and template arguments.
421 if (const ClassTemplateSpecializationDecl *Spec
422 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000423 // From the template.
424 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
425 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000426
John McCall457a04e2010-10-22 21:05:15 +0000427 // The arguments at which the template was instantiated.
428 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000429 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000430 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000431
John McCall5fe84122010-10-26 04:59:26 +0000432 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000433 if (F.ConsiderGlobalVisibility)
434 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000435 (Context.getLangOptions().CPlusPlus &&
436 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000437
Douglas Gregorf73b2822009-11-25 22:24:25 +0000438 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000439 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000440 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000441 if (!isExternalLinkage(EnumLV.linkage()))
442 return LinkageInfo::none();
443 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000444
445 // - a template, unless it is a function template that has
446 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000447 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
448 if (F.ConsiderTemplateParameterTypes)
449 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000450
Douglas Gregorf73b2822009-11-25 22:24:25 +0000451 // - a namespace (7.3), unless it is declared within an unnamed
452 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000453 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
454 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000455
John McCall457a04e2010-10-22 21:05:15 +0000456 // By extension, we assign external linkage to Objective-C
457 // interfaces.
458 } else if (isa<ObjCInterfaceDecl>(D)) {
459 // fallout
460
461 // Everything not covered here has no linkage.
462 } else {
John McCallc273f242010-10-30 11:50:40 +0000463 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000464 }
465
466 // If we ended up with non-external linkage, visibility should
467 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000468 if (LV.linkage() != ExternalLinkage)
469 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000470
471 // If we didn't end up with hidden visibility, consider attributes
472 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000473 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000474 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000475
476 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000477}
478
John McCall07072662010-11-02 01:45:15 +0000479static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000480 // Only certain class members have linkage. Note that fields don't
481 // really have linkage, but it's convenient to say they do for the
482 // purposes of calculating linkage of pointer-to-data-member
483 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000484 if (!(isa<CXXMethodDecl>(D) ||
485 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000486 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000487 (isa<TagDecl>(D) &&
Richard Smithdda56e42011-04-15 14:24:37 +0000488 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000489 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000490
John McCall07072662010-11-02 01:45:15 +0000491 LinkageInfo LV;
492
493 // The flags we're going to use to compute the class's visibility.
494 LVFlags ClassF = F;
495
496 // If we have an explicit visibility attribute, merge that in.
497 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000498 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
499 LV.mergeVisibility(*Vis, true);
John McCall07072662010-11-02 01:45:15 +0000500
501 // Ignore global visibility later, but not this attribute.
502 F.ConsiderGlobalVisibility = false;
503
504 // Ignore both global visibility and attributes when computing our
505 // parent's visibility.
506 ClassF = F.onlyTemplateVisibility();
507 }
508 }
John McCallc273f242010-10-30 11:50:40 +0000509
510 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000511 // linkage.
512 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
513 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000514 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000515
516 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000517 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000518 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000519
John McCall8823c652010-08-13 08:35:10 +0000520 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000521 // If the type of the function uses a type with unique-external
522 // linkage, it's not legally usable from outside this translation unit.
523 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
524 return LinkageInfo::uniqueExternal();
525
John McCall37bb6c92010-10-29 22:22:43 +0000526 TemplateSpecializationKind TSK = TSK_Undeclared;
527
John McCall457a04e2010-10-22 21:05:15 +0000528 // If this is a method template specialization, use the linkage for
529 // the template parameters and arguments.
530 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000531 = MD->getTemplateSpecializationInfo()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000532 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCall8bc6d5b2011-03-04 10:39:25 +0000533 if (F.ConsiderTemplateParameterTypes)
534 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000535 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000536
537 TSK = Spec->getTemplateSpecializationKind();
538 } else if (MemberSpecializationInfo *MSI =
539 MD->getMemberSpecializationInfo()) {
540 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000541 }
542
John McCall37bb6c92010-10-29 22:22:43 +0000543 // If we're paying attention to global visibility, apply
544 // -finline-visibility-hidden if this is an inline method.
545 //
John McCallc273f242010-10-30 11:50:40 +0000546 // Note that ConsiderGlobalVisibility doesn't yet have information
547 // about whether containing classes have visibility attributes,
548 // and that's intentional.
549 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000550 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000551 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
552 // InlineVisibilityHidden only applies to definitions, and
553 // isInlined() only gives meaningful answers on definitions
554 // anyway.
555 const FunctionDecl *Def = 0;
556 if (MD->hasBody(Def) && Def->isInlined())
557 LV.setVisibility(HiddenVisibility);
558 }
John McCall457a04e2010-10-22 21:05:15 +0000559
John McCall37bb6c92010-10-29 22:22:43 +0000560 // Note that in contrast to basically every other situation, we
561 // *do* apply -fvisibility to method declarations.
562
563 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000564 if (const ClassTemplateSpecializationDecl *Spec
565 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
566 // Merge template argument/parameter information for member
567 // class template specializations.
Douglas Gregorbf62d642010-12-06 18:36:25 +0000568 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCall8bc6d5b2011-03-04 10:39:25 +0000569 if (F.ConsiderTemplateParameterTypes)
570 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000571 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000572 }
573
John McCall37bb6c92010-10-29 22:22:43 +0000574 // Static data members.
575 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000576 // Modify the variable's linkage by its type, but ignore the
577 // type's visibility unless it's a definition.
578 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
579 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000580 LV.mergeLinkage(UniqueExternalLinkage);
581 if (!LV.visibilityExplicit())
582 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000583 }
584
John McCall07072662010-11-02 01:45:15 +0000585 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000586
587 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000588 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000589 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000590 }
591
John McCall457a04e2010-10-22 21:05:15 +0000592 return LV;
John McCall8823c652010-08-13 08:35:10 +0000593}
594
John McCalld396b972011-02-08 19:01:05 +0000595static void clearLinkageForClass(const CXXRecordDecl *record) {
596 for (CXXRecordDecl::decl_iterator
597 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
598 Decl *child = *i;
599 if (isa<NamedDecl>(child))
600 cast<NamedDecl>(child)->ClearLinkageCache();
601 }
602}
603
604void NamedDecl::ClearLinkageCache() {
605 // Note that we can't skip clearing the linkage of children just
606 // because the parent doesn't have cached linkage: we don't cache
607 // when computing linkage for parent contexts.
608
609 HasCachedLinkage = 0;
610
611 // If we're changing the linkage of a class, we need to reset the
612 // linkage of child declarations, too.
613 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
614 clearLinkageForClass(record);
615
John McCall83779672011-02-19 02:53:41 +0000616 if (ClassTemplateDecl *temp =
617 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000618 // Clear linkage for the template pattern.
619 CXXRecordDecl *record = temp->getTemplatedDecl();
620 record->HasCachedLinkage = 0;
621 clearLinkageForClass(record);
622
John McCall83779672011-02-19 02:53:41 +0000623 // We need to clear linkage for specializations, too.
624 for (ClassTemplateDecl::spec_iterator
625 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
626 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000627 }
John McCall83779672011-02-19 02:53:41 +0000628
629 // Clear cached linkage for function template decls, too.
630 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000631 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
632 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000633 for (FunctionTemplateDecl::spec_iterator
634 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
635 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000636 }
John McCall83779672011-02-19 02:53:41 +0000637
John McCalld396b972011-02-08 19:01:05 +0000638}
639
Douglas Gregorbf62d642010-12-06 18:36:25 +0000640Linkage NamedDecl::getLinkage() const {
641 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000642 assert(Linkage(CachedLinkage) ==
643 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000644 return Linkage(CachedLinkage);
645 }
646
647 CachedLinkage = getLVForDecl(this,
648 LVFlags::CreateOnlyDeclLinkage()).linkage();
649 HasCachedLinkage = 1;
650 return Linkage(CachedLinkage);
651}
652
John McCallc273f242010-10-30 11:50:40 +0000653LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000654 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000655 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000656 HasCachedLinkage = 1;
657 CachedLinkage = LI.linkage();
658 return LI;
John McCall033caa52010-10-29 00:29:13 +0000659}
Ted Kremenek926d8602010-04-20 23:15:35 +0000660
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000661llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
662 // Use the most recent declaration of a variable.
663 if (const VarDecl *var = dyn_cast<VarDecl>(this))
664 return getVisibilityOf(var->getMostRecentDeclaration());
665
666 // Use the most recent declaration of a function, and also handle
667 // function template specializations.
668 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
669 if (llvm::Optional<Visibility> V
670 = getVisibilityOf(fn->getMostRecentDeclaration()))
671 return V;
672
673 // If the function is a specialization of a template with an
674 // explicit visibility attribute, use that.
675 if (FunctionTemplateSpecializationInfo *templateInfo
676 = fn->getTemplateSpecializationInfo())
677 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
678
679 return llvm::Optional<Visibility>();
680 }
681
682 // Otherwise, just check the declaration itself first.
683 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
684 return V;
685
686 // If there wasn't explicit visibility there, and this is a
687 // specialization of a class template, check for visibility
688 // on the pattern.
689 if (const ClassTemplateSpecializationDecl *spec
690 = dyn_cast<ClassTemplateSpecializationDecl>(this))
691 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
692
693 return llvm::Optional<Visibility>();
694}
695
John McCall07072662010-11-02 01:45:15 +0000696static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000697 // Objective-C: treat all Objective-C declarations as having external
698 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000699 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000700 default:
701 break;
John McCall457a04e2010-10-22 21:05:15 +0000702 case Decl::TemplateTemplateParm: // count these as external
703 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000704 case Decl::ObjCAtDefsField:
705 case Decl::ObjCCategory:
706 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000707 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000708 case Decl::ObjCForwardProtocol:
709 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000710 case Decl::ObjCMethod:
711 case Decl::ObjCProperty:
712 case Decl::ObjCPropertyImpl:
713 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000714 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000715 }
716
Douglas Gregorf73b2822009-11-25 22:24:25 +0000717 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000718 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000719 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000720
721 // C++ [basic.link]p5:
722 // In addition, a member function, static data member, a named
723 // class or enumeration of class scope, or an unnamed class or
724 // enumeration defined in a class-scope typedef declaration such
725 // that the class or enumeration has the typedef name for linkage
726 // purposes (7.1.3), has external linkage if the name of the class
727 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000728 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000729 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000730
731 // C++ [basic.link]p6:
732 // The name of a function declared in block scope and the name of
733 // an object declared by a block scope extern declaration have
734 // linkage. If there is a visible declaration of an entity with
735 // linkage having the same name and type, ignoring entities
736 // declared outside the innermost enclosing namespace scope, the
737 // block scope declaration declares that same entity and receives
738 // the linkage of the previous declaration. If there is more than
739 // one such matching entity, the program is ill-formed. Otherwise,
740 // if no matching entity is found, the block scope entity receives
741 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000742 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
743 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000744 if (Function->isInAnonymousNamespace() && !Function->isExternC())
John McCallc273f242010-10-30 11:50:40 +0000745 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000746
John McCallc273f242010-10-30 11:50:40 +0000747 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000748 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000749 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
750 LV.setVisibility(*Vis);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000751 }
752
John McCall457a04e2010-10-22 21:05:15 +0000753 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000754 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000755 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
756 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000757 }
758
759 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000760 }
761
John McCall033caa52010-10-29 00:29:13 +0000762 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000763 if (Var->getStorageClass() == SC_Extern ||
764 Var->getStorageClass() == SC_PrivateExtern) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000765 if (Var->isInAnonymousNamespace() && !Var->isExternC())
John McCallc273f242010-10-30 11:50:40 +0000766 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000767
John McCallc273f242010-10-30 11:50:40 +0000768 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000769 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000770 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000771 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000772 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
773 LV.setVisibility(*Vis);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000774 }
775
John McCall457a04e2010-10-22 21:05:15 +0000776 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000777 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000778 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
779 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000780 }
781
782 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000783 }
784 }
785
786 // C++ [basic.link]p6:
787 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000788 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000789}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000790
Douglas Gregor2ada0482009-02-04 17:27:36 +0000791std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000792 return getQualifiedNameAsString(getASTContext().getLangOptions());
793}
794
795std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000796 const DeclContext *Ctx = getDeclContext();
797
798 if (Ctx->isFunctionOrMethod())
799 return getNameAsString();
800
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000801 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
802 ContextsTy Contexts;
803
804 // Collect contexts.
805 while (Ctx && isa<NamedDecl>(Ctx)) {
806 Contexts.push_back(Ctx);
807 Ctx = Ctx->getParent();
808 };
809
810 std::string QualName;
811 llvm::raw_string_ostream OS(QualName);
812
813 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
814 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000815 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000816 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000817 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
818 std::string TemplateArgsStr
819 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000820 TemplateArgs.data(),
821 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000822 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000823 OS << Spec->getName() << TemplateArgsStr;
824 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000825 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000826 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000827 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000828 OS << ND;
829 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
830 if (!RD->getIdentifier())
831 OS << "<anonymous " << RD->getKindName() << '>';
832 else
833 OS << RD;
834 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000835 const FunctionProtoType *FT = 0;
836 if (FD->hasWrittenPrototype())
837 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
838
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000839 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000840 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000841 unsigned NumParams = FD->getNumParams();
842 for (unsigned i = 0; i < NumParams; ++i) {
843 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000844 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000845 std::string Param;
846 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000847 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000848 }
849
850 if (FT->isVariadic()) {
851 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000852 OS << ", ";
853 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000854 }
855 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000856 OS << ')';
857 } else {
858 OS << cast<NamedDecl>(*I);
859 }
860 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000861 }
862
John McCalla2a3f7d2010-03-16 21:48:18 +0000863 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000864 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000865 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000866 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000867
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000868 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000869}
870
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000871bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000872 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
873
Douglas Gregor889ceb72009-02-03 19:21:40 +0000874 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
875 // We want to keep it, unless it nominates same namespace.
876 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000877 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
878 ->getOriginalNamespace() ==
879 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
880 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000881 }
Mike Stump11289f42009-09-09 15:08:12 +0000882
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000883 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
884 // For function declarations, we keep track of redeclarations.
885 return FD->getPreviousDeclaration() == OldD;
886
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000887 // For function templates, the underlying function declarations are linked.
888 if (const FunctionTemplateDecl *FunctionTemplate
889 = dyn_cast<FunctionTemplateDecl>(this))
890 if (const FunctionTemplateDecl *OldFunctionTemplate
891 = dyn_cast<FunctionTemplateDecl>(OldD))
892 return FunctionTemplate->getTemplatedDecl()
893 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000894
Steve Naroffc4173fa2009-02-22 19:35:57 +0000895 // For method declarations, we keep track of redeclarations.
896 if (isa<ObjCMethodDecl>(this))
897 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000898
John McCall9f3059a2009-10-09 21:13:30 +0000899 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
900 return true;
901
John McCall3f746822009-11-17 05:59:44 +0000902 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
903 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
904 cast<UsingShadowDecl>(OldD)->getTargetDecl();
905
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000906 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
907 ASTContext &Context = getASTContext();
908 return Context.getCanonicalNestedNameSpecifier(
909 cast<UsingDecl>(this)->getQualifier()) ==
910 Context.getCanonicalNestedNameSpecifier(
911 cast<UsingDecl>(OldD)->getQualifier());
912 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000913
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000914 // For non-function declarations, if the declarations are of the
915 // same kind then this must be a redeclaration, or semantic analysis
916 // would not have given us the new declaration.
917 return this->getKind() == OldD->getKind();
918}
919
Douglas Gregoreddf4332009-02-24 20:03:32 +0000920bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000921 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000922}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000923
Anders Carlsson6915bf62009-06-26 06:29:23 +0000924NamedDecl *NamedDecl::getUnderlyingDecl() {
925 NamedDecl *ND = this;
926 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000927 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000928 ND = UD->getTargetDecl();
929 else if (ObjCCompatibleAliasDecl *AD
930 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
931 return AD->getClassInterface();
932 else
933 return ND;
934 }
935}
936
John McCalla8ae2222010-04-06 21:38:20 +0000937bool NamedDecl::isCXXInstanceMember() const {
938 assert(isCXXClassMember() &&
939 "checking whether non-member is instance member");
940
941 const NamedDecl *D = this;
942 if (isa<UsingShadowDecl>(D))
943 D = cast<UsingShadowDecl>(D)->getTargetDecl();
944
Francois Pichet783dd6e2010-11-21 06:08:52 +0000945 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000946 return true;
947 if (isa<CXXMethodDecl>(D))
948 return cast<CXXMethodDecl>(D)->isInstance();
949 if (isa<FunctionTemplateDecl>(D))
950 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
951 ->getTemplatedDecl())->isInstance();
952 return false;
953}
954
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000955//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000956// DeclaratorDecl Implementation
957//===----------------------------------------------------------------------===//
958
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000959template <typename DeclT>
960static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
961 if (decl->getNumTemplateParameterLists() > 0)
962 return decl->getTemplateParameterList(0)->getTemplateLoc();
963 else
964 return decl->getInnerLocStart();
965}
966
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000967SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000968 TypeSourceInfo *TSI = getTypeSourceInfo();
969 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000970 return SourceLocation();
971}
972
Douglas Gregor14454802011-02-25 02:25:35 +0000973void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
974 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +0000975 // Make sure the extended decl info is allocated.
976 if (!hasExtInfo()) {
977 // Save (non-extended) type source info pointer.
978 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
979 // Allocate external info struct.
980 DeclInfo = new (getASTContext()) ExtInfo;
981 // Restore savedTInfo into (extended) decl info.
982 getExtInfo()->TInfo = savedTInfo;
983 }
984 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +0000985 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +0000986 }
987 else {
988 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +0000989 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +0000990 if (getExtInfo()->NumTemplParamLists == 0) {
991 // Save type source info pointer.
992 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
993 // Deallocate the extended decl info.
994 getASTContext().Deallocate(getExtInfo());
995 // Restore savedTInfo into (non-extended) decl info.
996 DeclInfo = savedTInfo;
997 }
998 else
999 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001000 }
1001 }
1002}
1003
Abramo Bagnara60804e12011-03-18 15:16:37 +00001004void
1005DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1006 unsigned NumTPLists,
1007 TemplateParameterList **TPLists) {
1008 assert(NumTPLists > 0);
1009 // Make sure the extended decl info is allocated.
1010 if (!hasExtInfo()) {
1011 // Save (non-extended) type source info pointer.
1012 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1013 // Allocate external info struct.
1014 DeclInfo = new (getASTContext()) ExtInfo;
1015 // Restore savedTInfo into (extended) decl info.
1016 getExtInfo()->TInfo = savedTInfo;
1017 }
1018 // Set the template parameter lists info.
1019 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1020}
1021
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001022SourceLocation DeclaratorDecl::getOuterLocStart() const {
1023 return getTemplateOrInnerLocStart(this);
1024}
1025
Abramo Bagnaraea947882011-03-08 16:41:52 +00001026namespace {
1027
1028// Helper function: returns true if QT is or contains a type
1029// having a postfix component.
1030bool typeIsPostfix(clang::QualType QT) {
1031 while (true) {
1032 const Type* T = QT.getTypePtr();
1033 switch (T->getTypeClass()) {
1034 default:
1035 return false;
1036 case Type::Pointer:
1037 QT = cast<PointerType>(T)->getPointeeType();
1038 break;
1039 case Type::BlockPointer:
1040 QT = cast<BlockPointerType>(T)->getPointeeType();
1041 break;
1042 case Type::MemberPointer:
1043 QT = cast<MemberPointerType>(T)->getPointeeType();
1044 break;
1045 case Type::LValueReference:
1046 case Type::RValueReference:
1047 QT = cast<ReferenceType>(T)->getPointeeType();
1048 break;
1049 case Type::PackExpansion:
1050 QT = cast<PackExpansionType>(T)->getPattern();
1051 break;
1052 case Type::Paren:
1053 case Type::ConstantArray:
1054 case Type::DependentSizedArray:
1055 case Type::IncompleteArray:
1056 case Type::VariableArray:
1057 case Type::FunctionProto:
1058 case Type::FunctionNoProto:
1059 return true;
1060 }
1061 }
1062}
1063
1064} // namespace
1065
1066SourceRange DeclaratorDecl::getSourceRange() const {
1067 SourceLocation RangeEnd = getLocation();
1068 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1069 if (typeIsPostfix(TInfo->getType()))
1070 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1071 }
1072 return SourceRange(getOuterLocStart(), RangeEnd);
1073}
1074
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001075void
Douglas Gregor20527e22010-06-15 17:44:38 +00001076QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1077 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001078 TemplateParameterList **TPLists) {
1079 assert((NumTPLists == 0 || TPLists != 0) &&
1080 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001081
1082 // Free previous template parameters (if any).
1083 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001084 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001085 TemplParamLists = 0;
1086 NumTemplParamLists = 0;
1087 }
1088 // Set info on matched template parameter lists (if any).
1089 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001090 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001091 NumTemplParamLists = NumTPLists;
1092 for (unsigned i = NumTPLists; i-- > 0; )
1093 TemplParamLists[i] = TPLists[i];
1094 }
1095}
1096
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001097//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001098// VarDecl Implementation
1099//===----------------------------------------------------------------------===//
1100
Sebastian Redl833ef452010-01-26 22:01:41 +00001101const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1102 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +00001103 case SC_None: break;
1104 case SC_Auto: return "auto"; break;
1105 case SC_Extern: return "extern"; break;
1106 case SC_PrivateExtern: return "__private_extern__"; break;
1107 case SC_Register: return "register"; break;
1108 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +00001109 }
1110
1111 assert(0 && "Invalid storage class");
1112 return 0;
1113}
1114
Abramo Bagnaradff19302011-03-08 08:55:46 +00001115VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1116 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001117 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001118 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001119 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001120}
1121
Douglas Gregorbf62d642010-12-06 18:36:25 +00001122void VarDecl::setStorageClass(StorageClass SC) {
1123 assert(isLegalForVariable(SC));
1124 if (getStorageClass() != SC)
1125 ClearLinkageCache();
1126
John McCallbeaa11c2011-05-01 02:13:58 +00001127 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001128}
1129
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001130SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001131 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001132 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001133 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001134}
1135
Sebastian Redl833ef452010-01-26 22:01:41 +00001136bool VarDecl::isExternC() const {
1137 ASTContext &Context = getASTContext();
1138 if (!Context.getLangOptions().CPlusPlus)
1139 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001140 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001141 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1142
Chandler Carruth4322a282011-02-25 00:05:02 +00001143 const DeclContext *DC = getDeclContext();
1144 if (DC->isFunctionOrMethod())
1145 return false;
1146
1147 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001148 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1149 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001150 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001151
1152 break;
1153 }
1154
Sebastian Redl833ef452010-01-26 22:01:41 +00001155 }
1156
1157 return false;
1158}
1159
1160VarDecl *VarDecl::getCanonicalDecl() {
1161 return getFirstDeclaration();
1162}
1163
Sebastian Redl35351a92010-01-31 22:27:38 +00001164VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1165 // C++ [basic.def]p2:
1166 // A declaration is a definition unless [...] it contains the 'extern'
1167 // specifier or a linkage-specification and neither an initializer [...],
1168 // it declares a static data member in a class declaration [...].
1169 // C++ [temp.expl.spec]p15:
1170 // An explicit specialization of a static data member of a template is a
1171 // definition if the declaration includes an initializer; otherwise, it is
1172 // a declaration.
1173 if (isStaticDataMember()) {
1174 if (isOutOfLine() && (hasInit() ||
1175 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1176 return Definition;
1177 else
1178 return DeclarationOnly;
1179 }
1180 // C99 6.7p5:
1181 // A definition of an identifier is a declaration for that identifier that
1182 // [...] causes storage to be reserved for that object.
1183 // Note: that applies for all non-file-scope objects.
1184 // C99 6.9.2p1:
1185 // If the declaration of an identifier for an object has file scope and an
1186 // initializer, the declaration is an external definition for the identifier
1187 if (hasInit())
1188 return Definition;
1189 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1190 if (hasExternalStorage())
1191 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001192
John McCall8e7d6562010-08-26 03:08:43 +00001193 if (getStorageClassAsWritten() == SC_Extern ||
1194 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001195 for (const VarDecl *PrevVar = getPreviousDeclaration();
1196 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1197 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1198 return DeclarationOnly;
1199 }
1200 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001201 // C99 6.9.2p2:
1202 // A declaration of an object that has file scope without an initializer,
1203 // and without a storage class specifier or the scs 'static', constitutes
1204 // a tentative definition.
1205 // No such thing in C++.
1206 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1207 return TentativeDefinition;
1208
1209 // What's left is (in C, block-scope) declarations without initializers or
1210 // external storage. These are definitions.
1211 return Definition;
1212}
1213
Sebastian Redl35351a92010-01-31 22:27:38 +00001214VarDecl *VarDecl::getActingDefinition() {
1215 DefinitionKind Kind = isThisDeclarationADefinition();
1216 if (Kind != TentativeDefinition)
1217 return 0;
1218
Chris Lattner48eb14d2010-06-14 18:31:46 +00001219 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001220 VarDecl *First = getFirstDeclaration();
1221 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1222 I != E; ++I) {
1223 Kind = (*I)->isThisDeclarationADefinition();
1224 if (Kind == Definition)
1225 return 0;
1226 else if (Kind == TentativeDefinition)
1227 LastTentative = *I;
1228 }
1229 return LastTentative;
1230}
1231
1232bool VarDecl::isTentativeDefinitionNow() const {
1233 DefinitionKind Kind = isThisDeclarationADefinition();
1234 if (Kind != TentativeDefinition)
1235 return false;
1236
1237 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1238 if ((*I)->isThisDeclarationADefinition() == Definition)
1239 return false;
1240 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001241 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001242}
1243
Sebastian Redl5ca79842010-02-01 20:16:42 +00001244VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001245 VarDecl *First = getFirstDeclaration();
1246 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1247 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001248 if ((*I)->isThisDeclarationADefinition() == Definition)
1249 return *I;
1250 }
1251 return 0;
1252}
1253
John McCall37bb6c92010-10-29 22:22:43 +00001254VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1255 DefinitionKind Kind = DeclarationOnly;
1256
1257 const VarDecl *First = getFirstDeclaration();
1258 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1259 I != E; ++I)
1260 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1261
1262 return Kind;
1263}
1264
Sebastian Redl5ca79842010-02-01 20:16:42 +00001265const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001266 redecl_iterator I = redecls_begin(), E = redecls_end();
1267 while (I != E && !I->getInit())
1268 ++I;
1269
1270 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001271 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001272 return I->getInit();
1273 }
1274 return 0;
1275}
1276
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001277bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001278 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001279 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001280
1281 if (!isStaticDataMember())
1282 return false;
1283
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001284 // If this static data member was instantiated from a static data member of
1285 // a class template, check whether that static data member was defined
1286 // out-of-line.
1287 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1288 return VD->isOutOfLine();
1289
1290 return false;
1291}
1292
Douglas Gregor1d957a32009-10-27 18:42:08 +00001293VarDecl *VarDecl::getOutOfLineDefinition() {
1294 if (!isStaticDataMember())
1295 return 0;
1296
1297 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1298 RD != RDEnd; ++RD) {
1299 if (RD->getLexicalDeclContext()->isFileContext())
1300 return *RD;
1301 }
1302
1303 return 0;
1304}
1305
Douglas Gregord5058122010-02-11 01:19:42 +00001306void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001307 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1308 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001309 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001310 }
1311
1312 Init = I;
1313}
1314
Douglas Gregorfe314812011-06-21 17:03:29 +00001315bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001316 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001317
1318 const Expr *E = getInit();
1319 if (!E)
1320 return false;
1321
1322 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1323 E = Cleanups->getSubExpr();
1324
1325 return isa<MaterializeTemporaryExpr>(E);
1326}
1327
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001328VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001329 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001330 return cast<VarDecl>(MSI->getInstantiatedFrom());
1331
1332 return 0;
1333}
1334
Douglas Gregor3c74d412009-10-14 20:14:33 +00001335TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001336 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001337 return MSI->getTemplateSpecializationKind();
1338
1339 return TSK_Undeclared;
1340}
1341
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001342MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001343 return getASTContext().getInstantiatedFromStaticDataMember(this);
1344}
1345
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001346void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1347 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001348 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001349 assert(MSI && "Not an instantiated static data member?");
1350 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001351 if (TSK != TSK_ExplicitSpecialization &&
1352 PointOfInstantiation.isValid() &&
1353 MSI->getPointOfInstantiation().isInvalid())
1354 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001355}
1356
Sebastian Redl833ef452010-01-26 22:01:41 +00001357//===----------------------------------------------------------------------===//
1358// ParmVarDecl Implementation
1359//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001360
Sebastian Redl833ef452010-01-26 22:01:41 +00001361ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001362 SourceLocation StartLoc,
1363 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001364 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001365 StorageClass S, StorageClass SCAsWritten,
1366 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001367 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001368 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001369}
1370
Sebastian Redl833ef452010-01-26 22:01:41 +00001371Expr *ParmVarDecl::getDefaultArg() {
1372 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1373 assert(!hasUninstantiatedDefaultArg() &&
1374 "Default argument is not yet instantiated!");
1375
1376 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001377 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001378 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001379
Sebastian Redl833ef452010-01-26 22:01:41 +00001380 return Arg;
1381}
1382
1383unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001384 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001385 return E->getNumTemporaries();
1386
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001387 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001388}
1389
Sebastian Redl833ef452010-01-26 22:01:41 +00001390CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1391 assert(getNumDefaultArgTemporaries() &&
1392 "Default arguments does not have any temporaries!");
1393
John McCall5d413782010-12-06 08:20:24 +00001394 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001395 return E->getTemporary(i);
1396}
1397
1398SourceRange ParmVarDecl::getDefaultArgRange() const {
1399 if (const Expr *E = getInit())
1400 return E->getSourceRange();
1401
1402 if (hasUninstantiatedDefaultArg())
1403 return getUninstantiatedDefaultArg()->getSourceRange();
1404
1405 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001406}
1407
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001408bool ParmVarDecl::isParameterPack() const {
1409 return isa<PackExpansionType>(getType());
1410}
1411
Nuno Lopes394ec982008-12-17 23:39:55 +00001412//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001413// FunctionDecl Implementation
1414//===----------------------------------------------------------------------===//
1415
Douglas Gregorb11aad82011-02-19 18:51:44 +00001416void FunctionDecl::getNameForDiagnostic(std::string &S,
1417 const PrintingPolicy &Policy,
1418 bool Qualified) const {
1419 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1420 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1421 if (TemplateArgs)
1422 S += TemplateSpecializationType::PrintTemplateArgumentList(
1423 TemplateArgs->data(),
1424 TemplateArgs->size(),
1425 Policy);
1426
1427}
1428
Ted Kremenek186a0742010-04-29 16:49:01 +00001429bool FunctionDecl::isVariadic() const {
1430 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1431 return FT->isVariadic();
1432 return false;
1433}
1434
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001435bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1436 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001437 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001438 Definition = *I;
1439 return true;
1440 }
1441 }
1442
1443 return false;
1444}
1445
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001446bool FunctionDecl::hasTrivialBody() const
1447{
1448 Stmt *S = getBody();
1449 if (!S) {
1450 // Since we don't have a body for this function, we don't know if it's
1451 // trivial or not.
1452 return false;
1453 }
1454
1455 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1456 return true;
1457 return false;
1458}
1459
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001460bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1461 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001462 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001463 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1464 return true;
1465 }
1466 }
1467
1468 return false;
1469}
1470
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001471Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001472 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1473 if (I->Body) {
1474 Definition = *I;
1475 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001476 } else if (I->IsLateTemplateParsed) {
1477 Definition = *I;
1478 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001479 }
1480 }
1481
1482 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001483}
1484
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001485void FunctionDecl::setBody(Stmt *B) {
1486 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001487 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001488 EndRangeLoc = B->getLocEnd();
1489}
1490
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001491void FunctionDecl::setPure(bool P) {
1492 IsPure = P;
1493 if (P)
1494 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1495 Parent->markedVirtualFunctionPure();
1496}
1497
Douglas Gregor16618f22009-09-12 00:17:51 +00001498bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001499 const TranslationUnitDecl *tunit =
1500 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1501 return tunit &&
Alexis Huntf9933e82011-05-15 20:59:31 +00001502 !tunit->getASTContext().getLangOptions().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001503 getIdentifier() &&
1504 getIdentifier()->isStr("main");
1505}
1506
1507bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1508 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1509 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1510 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1511 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1512 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1513
1514 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1515 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1516
1517 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1518 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1519
1520 ASTContext &Context =
1521 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1522 ->getASTContext();
1523
1524 // The result type and first argument type are constant across all
1525 // these operators. The second argument must be exactly void*.
1526 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001527}
1528
Douglas Gregor16618f22009-09-12 00:17:51 +00001529bool FunctionDecl::isExternC() const {
1530 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001531 // In C, any non-static, non-overloadable function has external
1532 // linkage.
1533 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001534 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001535
Chandler Carruth4322a282011-02-25 00:05:02 +00001536 const DeclContext *DC = getDeclContext();
1537 if (DC->isRecord())
1538 return false;
1539
1540 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001541 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1542 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001543 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001544 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001545
1546 break;
1547 }
1548 }
1549
Douglas Gregorbff62032010-10-21 16:57:46 +00001550 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001551}
1552
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001553bool FunctionDecl::isGlobal() const {
1554 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1555 return Method->isStatic();
1556
John McCall8e7d6562010-08-26 03:08:43 +00001557 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001558 return false;
1559
Mike Stump11289f42009-09-09 15:08:12 +00001560 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001561 DC->isNamespace();
1562 DC = DC->getParent()) {
1563 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1564 if (!Namespace->getDeclName())
1565 return false;
1566 break;
1567 }
1568 }
1569
1570 return true;
1571}
1572
Sebastian Redl833ef452010-01-26 22:01:41 +00001573void
1574FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1575 redeclarable_base::setPreviousDeclaration(PrevDecl);
1576
1577 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1578 FunctionTemplateDecl *PrevFunTmpl
1579 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1580 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1581 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1582 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001583
1584 if (PrevDecl->IsInline)
1585 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001586}
1587
1588const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1589 return getFirstDeclaration();
1590}
1591
1592FunctionDecl *FunctionDecl::getCanonicalDecl() {
1593 return getFirstDeclaration();
1594}
1595
Douglas Gregorbf62d642010-12-06 18:36:25 +00001596void FunctionDecl::setStorageClass(StorageClass SC) {
1597 assert(isLegalForFunction(SC));
1598 if (getStorageClass() != SC)
1599 ClearLinkageCache();
1600
1601 SClass = SC;
1602}
1603
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001604/// \brief Returns a value indicating whether this function
1605/// corresponds to a builtin function.
1606///
1607/// The function corresponds to a built-in function if it is
1608/// declared at translation scope or within an extern "C" block and
1609/// its name matches with the name of a builtin. The returned value
1610/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001611/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001612/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001613unsigned FunctionDecl::getBuiltinID() const {
1614 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001615 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1616 return 0;
1617
1618 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1619 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1620 return BuiltinID;
1621
1622 // This function has the name of a known C library
1623 // function. Determine whether it actually refers to the C library
1624 // function or whether it just has the same name.
1625
Douglas Gregora908e7f2009-02-17 03:23:10 +00001626 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001627 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001628 return 0;
1629
Douglas Gregore711f702009-02-14 18:57:46 +00001630 // If this function is at translation-unit scope and we're not in
1631 // C++, it refers to the C library function.
1632 if (!Context.getLangOptions().CPlusPlus &&
1633 getDeclContext()->isTranslationUnit())
1634 return BuiltinID;
1635
1636 // If the function is in an extern "C" linkage specification and is
1637 // not marked "overloadable", it's the real function.
1638 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001639 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001640 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001641 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001642 return BuiltinID;
1643
1644 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001645 return 0;
1646}
1647
1648
Chris Lattner47c0d002009-04-25 06:03:53 +00001649/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001650/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001651/// after it has been created.
1652unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001653 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001654 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001655 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001656 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001657
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001658}
1659
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001660void FunctionDecl::setParams(ASTContext &C,
1661 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001662 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001663 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001664
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001665 // Zero params -> null pointer.
1666 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001667 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001668 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001669 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001670
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001671 // Update source range. The check below allows us to set EndRangeLoc before
1672 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001673 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001674 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001675 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001676}
Chris Lattner41943152007-01-25 04:52:46 +00001677
Chris Lattner58258242008-04-10 02:22:51 +00001678/// getMinRequiredArguments - Returns the minimum number of arguments
1679/// needed to call this function. This may be fewer than the number of
1680/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001681/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001682unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001683 if (!getASTContext().getLangOptions().CPlusPlus)
1684 return getNumParams();
1685
Douglas Gregor7825bf32011-01-06 22:09:01 +00001686 unsigned NumRequiredArgs = getNumParams();
1687
1688 // If the last parameter is a parameter pack, we don't need an argument for
1689 // it.
1690 if (NumRequiredArgs > 0 &&
1691 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1692 --NumRequiredArgs;
1693
1694 // If this parameter has a default argument, we don't need an argument for
1695 // it.
1696 while (NumRequiredArgs > 0 &&
1697 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001698 --NumRequiredArgs;
1699
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001700 // We might have parameter packs before the end. These can't be deduced,
1701 // but they can still handle multiple arguments.
1702 unsigned ArgIdx = NumRequiredArgs;
1703 while (ArgIdx > 0) {
1704 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1705 NumRequiredArgs = ArgIdx;
1706
1707 --ArgIdx;
1708 }
1709
Chris Lattner58258242008-04-10 02:22:51 +00001710 return NumRequiredArgs;
1711}
1712
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001713bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001714 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001715 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001716
1717 if (isa<CXXMethodDecl>(this)) {
1718 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1719 return true;
1720 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001721
1722 switch (getTemplateSpecializationKind()) {
1723 case TSK_Undeclared:
1724 case TSK_ExplicitSpecialization:
1725 return false;
1726
1727 case TSK_ImplicitInstantiation:
1728 case TSK_ExplicitInstantiationDeclaration:
1729 case TSK_ExplicitInstantiationDefinition:
1730 // Handle below.
1731 break;
1732 }
1733
1734 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001735 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001736 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001737 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001738
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001739 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001740 return PatternDecl->isInlined();
1741
1742 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001743}
1744
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001745/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001746/// definition will be externally visible.
1747///
1748/// Inline function definitions are always available for inlining optimizations.
1749/// However, depending on the language dialect, declaration specifiers, and
1750/// attributes, the definition of an inline function may or may not be
1751/// "externally" visible to other translation units in the program.
1752///
1753/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001754/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001755/// inline definition becomes externally visible (C99 6.7.4p6).
1756///
1757/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1758/// definition, we use the GNU semantics for inline, which are nearly the
1759/// opposite of C99 semantics. In particular, "inline" by itself will create
1760/// an externally visible symbol, but "extern inline" will not create an
1761/// externally visible symbol.
1762bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001763 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001764 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001765 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001766
Rafael Espindolafb2af642011-06-02 16:13:27 +00001767 if (Context.getLangOptions().GNUInline || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001768 // If it's not the case that both 'inline' and 'extern' are
1769 // specified on the definition, then this inline definition is
1770 // externally visible.
1771 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1772 return true;
1773
1774 // If any declaration is 'inline' but not 'extern', then this definition
1775 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001776 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1777 Redecl != RedeclEnd;
1778 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001779 if (Redecl->isInlineSpecified() &&
1780 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001781 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001782 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001783
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001784 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001785 }
1786
1787 // C99 6.7.4p6:
1788 // [...] If all of the file scope declarations for a function in a
1789 // translation unit include the inline function specifier without extern,
1790 // then the definition in that translation unit is an inline definition.
1791 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1792 Redecl != RedeclEnd;
1793 ++Redecl) {
1794 // Only consider file-scope declarations in this test.
1795 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1796 continue;
1797
John McCall8e7d6562010-08-26 03:08:43 +00001798 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001799 return true; // Not an inline definition
1800 }
1801
1802 // C99 6.7.4p6:
1803 // An inline definition does not provide an external definition for the
1804 // function, and does not forbid an external definition in another
1805 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001806 return false;
1807}
1808
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001809/// getOverloadedOperator - Which C++ overloaded operator this
1810/// function represents, if any.
1811OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001812 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1813 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001814 else
1815 return OO_None;
1816}
1817
Alexis Huntc88db062010-01-13 09:01:02 +00001818/// getLiteralIdentifier - The literal suffix identifier this function
1819/// represents, if any.
1820const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1821 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1822 return getDeclName().getCXXLiteralIdentifier();
1823 else
1824 return 0;
1825}
1826
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001827FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1828 if (TemplateOrSpecialization.isNull())
1829 return TK_NonTemplate;
1830 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1831 return TK_FunctionTemplate;
1832 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1833 return TK_MemberSpecialization;
1834 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1835 return TK_FunctionTemplateSpecialization;
1836 if (TemplateOrSpecialization.is
1837 <DependentFunctionTemplateSpecializationInfo*>())
1838 return TK_DependentFunctionTemplateSpecialization;
1839
1840 assert(false && "Did we miss a TemplateOrSpecialization type?");
1841 return TK_NonTemplate;
1842}
1843
Douglas Gregord801b062009-10-07 23:56:10 +00001844FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001845 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001846 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1847
1848 return 0;
1849}
1850
Douglas Gregor06db9f52009-10-12 20:18:28 +00001851MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1852 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1853}
1854
Douglas Gregord801b062009-10-07 23:56:10 +00001855void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001856FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1857 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001858 TemplateSpecializationKind TSK) {
1859 assert(TemplateOrSpecialization.isNull() &&
1860 "Member function is already a specialization");
1861 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001862 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001863 TemplateOrSpecialization = Info;
1864}
1865
Douglas Gregorafca3b42009-10-27 20:53:28 +00001866bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001867 // If the function is invalid, it can't be implicitly instantiated.
1868 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001869 return false;
1870
1871 switch (getTemplateSpecializationKind()) {
1872 case TSK_Undeclared:
1873 case TSK_ExplicitSpecialization:
1874 case TSK_ExplicitInstantiationDefinition:
1875 return false;
1876
1877 case TSK_ImplicitInstantiation:
1878 return true;
1879
1880 case TSK_ExplicitInstantiationDeclaration:
1881 // Handled below.
1882 break;
1883 }
1884
1885 // Find the actual template from which we will instantiate.
1886 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001887 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001888 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001889 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001890
1891 // C++0x [temp.explicit]p9:
1892 // Except for inline functions, other explicit instantiation declarations
1893 // have the effect of suppressing the implicit instantiation of the entity
1894 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001895 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001896 return true;
1897
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001898 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001899}
1900
1901FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1902 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1903 while (Primary->getInstantiatedFromMemberTemplate()) {
1904 // If we have hit a point where the user provided a specialization of
1905 // this template, we're done looking.
1906 if (Primary->isMemberSpecialization())
1907 break;
1908
1909 Primary = Primary->getInstantiatedFromMemberTemplate();
1910 }
1911
1912 return Primary->getTemplatedDecl();
1913 }
1914
1915 return getInstantiatedFromMemberFunction();
1916}
1917
Douglas Gregor70d83e22009-06-29 17:30:29 +00001918FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001919 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001920 = TemplateOrSpecialization
1921 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001922 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001923 }
1924 return 0;
1925}
1926
1927const TemplateArgumentList *
1928FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001929 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001930 = TemplateOrSpecialization
1931 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001932 return Info->TemplateArguments;
1933 }
1934 return 0;
1935}
1936
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001937const TemplateArgumentListInfo *
1938FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1939 if (FunctionTemplateSpecializationInfo *Info
1940 = TemplateOrSpecialization
1941 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1942 return Info->TemplateArgumentsAsWritten;
1943 }
1944 return 0;
1945}
1946
Mike Stump11289f42009-09-09 15:08:12 +00001947void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001948FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1949 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001950 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001951 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001952 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001953 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1954 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001955 assert(TSK != TSK_Undeclared &&
1956 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001957 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001958 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001959 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001960 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1961 TemplateArgs,
1962 TemplateArgsAsWritten,
1963 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001964 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001965
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001966 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001967 // function template specializations.
1968 if (InsertPos)
Sebastian Redl9ab988f2011-04-14 14:07:59 +00001969 Template->addSpecialization(Info, InsertPos);
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001970 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001971 // Try to insert the new node. If there is an existing node, leave it, the
1972 // set will contain the canonical decls while
1973 // FunctionTemplateDecl::findSpecialization will return
1974 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001975 FunctionTemplateSpecializationInfo *Existing
1976 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001977 (void)Existing;
1978 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1979 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001980 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001981}
1982
John McCallb9c78482010-04-08 09:05:18 +00001983void
1984FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1985 const UnresolvedSetImpl &Templates,
1986 const TemplateArgumentListInfo &TemplateArgs) {
1987 assert(TemplateOrSpecialization.isNull());
1988 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1989 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001990 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001991 void *Buffer = Context.Allocate(Size);
1992 DependentFunctionTemplateSpecializationInfo *Info =
1993 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1994 TemplateArgs);
1995 TemplateOrSpecialization = Info;
1996}
1997
1998DependentFunctionTemplateSpecializationInfo::
1999DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2000 const TemplateArgumentListInfo &TArgs)
2001 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2002
2003 d.NumTemplates = Ts.size();
2004 d.NumArgs = TArgs.size();
2005
2006 FunctionTemplateDecl **TsArray =
2007 const_cast<FunctionTemplateDecl**>(getTemplates());
2008 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2009 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2010
2011 TemplateArgumentLoc *ArgsArray =
2012 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2013 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2014 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2015}
2016
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002017TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002018 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002019 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002020 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002021 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002022 if (FTSInfo)
2023 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002024
Douglas Gregord801b062009-10-07 23:56:10 +00002025 MemberSpecializationInfo *MSInfo
2026 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2027 if (MSInfo)
2028 return MSInfo->getTemplateSpecializationKind();
2029
2030 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002031}
2032
Mike Stump11289f42009-09-09 15:08:12 +00002033void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002034FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2035 SourceLocation PointOfInstantiation) {
2036 if (FunctionTemplateSpecializationInfo *FTSInfo
2037 = TemplateOrSpecialization.dyn_cast<
2038 FunctionTemplateSpecializationInfo*>()) {
2039 FTSInfo->setTemplateSpecializationKind(TSK);
2040 if (TSK != TSK_ExplicitSpecialization &&
2041 PointOfInstantiation.isValid() &&
2042 FTSInfo->getPointOfInstantiation().isInvalid())
2043 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2044 } else if (MemberSpecializationInfo *MSInfo
2045 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2046 MSInfo->setTemplateSpecializationKind(TSK);
2047 if (TSK != TSK_ExplicitSpecialization &&
2048 PointOfInstantiation.isValid() &&
2049 MSInfo->getPointOfInstantiation().isInvalid())
2050 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2051 } else
2052 assert(false && "Function cannot have a template specialization kind");
2053}
2054
2055SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002056 if (FunctionTemplateSpecializationInfo *FTSInfo
2057 = TemplateOrSpecialization.dyn_cast<
2058 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002059 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002060 else if (MemberSpecializationInfo *MSInfo
2061 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002062 return MSInfo->getPointOfInstantiation();
2063
2064 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002065}
2066
Douglas Gregor6411b922009-09-11 20:15:17 +00002067bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002068 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002069 return true;
2070
2071 // If this function was instantiated from a member function of a
2072 // class template, check whether that member function was defined out-of-line.
2073 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2074 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002075 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002076 return Definition->isOutOfLine();
2077 }
2078
2079 // If this function was instantiated from a function template,
2080 // check whether that function template was defined out-of-line.
2081 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2082 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002083 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002084 return Definition->isOutOfLine();
2085 }
2086
2087 return false;
2088}
2089
Abramo Bagnaraea947882011-03-08 16:41:52 +00002090SourceRange FunctionDecl::getSourceRange() const {
2091 return SourceRange(getOuterLocStart(), EndRangeLoc);
2092}
2093
Chris Lattner59a25942008-03-31 00:36:02 +00002094//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002095// FieldDecl Implementation
2096//===----------------------------------------------------------------------===//
2097
Jay Foad39c79802011-01-12 09:06:06 +00002098FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002099 SourceLocation StartLoc, SourceLocation IdLoc,
2100 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002101 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
2102 bool HasInit) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002103 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith938f40b2011-06-11 17:19:42 +00002104 BW, Mutable, HasInit);
Sebastian Redl833ef452010-01-26 22:01:41 +00002105}
2106
2107bool FieldDecl::isAnonymousStructOrUnion() const {
2108 if (!isImplicit() || getDeclName())
2109 return false;
2110
2111 if (const RecordType *Record = getType()->getAs<RecordType>())
2112 return Record->getDecl()->isAnonymousStructOrUnion();
2113
2114 return false;
2115}
2116
John McCall4e819612011-01-20 07:57:12 +00002117unsigned FieldDecl::getFieldIndex() const {
2118 if (CachedFieldIndex) return CachedFieldIndex - 1;
2119
2120 unsigned index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002121 const RecordDecl *RD = getParent();
2122 const FieldDecl *LastFD = 0;
2123 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
2124
2125 RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
John McCall4e819612011-01-20 07:57:12 +00002126 while (true) {
2127 assert(i != e && "failed to find field in parent!");
2128 if (*i == this)
2129 break;
2130
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002131 if (IsMsStruct) {
2132 // Zero-length bitfields following non-bitfield members are ignored.
Fariborz Jahanianfc0fe6e2011-05-03 20:21:04 +00002133 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*i), LastFD)) {
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002134 ++i;
2135 continue;
2136 }
2137 LastFD = (*i);
2138 }
John McCall4e819612011-01-20 07:57:12 +00002139 ++i;
2140 ++index;
2141 }
2142
2143 CachedFieldIndex = index + 1;
2144 return index;
2145}
2146
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002147SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraea947882011-03-08 16:41:52 +00002148 if (isBitField())
Richard Smith938f40b2011-06-11 17:19:42 +00002149 return SourceRange(getInnerLocStart(), getBitWidth()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002150 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002151}
2152
Richard Smith938f40b2011-06-11 17:19:42 +00002153void FieldDecl::setInClassInitializer(Expr *Init) {
2154 assert(!InitializerOrBitWidth.getPointer() &&
2155 "bit width or initializer already set");
2156 InitializerOrBitWidth.setPointer(Init);
2157 InitializerOrBitWidth.setInt(0);
2158}
2159
Sebastian Redl833ef452010-01-26 22:01:41 +00002160//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002161// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002162//===----------------------------------------------------------------------===//
2163
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002164SourceLocation TagDecl::getOuterLocStart() const {
2165 return getTemplateOrInnerLocStart(this);
2166}
2167
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002168SourceRange TagDecl::getSourceRange() const {
2169 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002170 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002171}
2172
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002173TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002174 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002175}
2176
Richard Smithdda56e42011-04-15 14:24:37 +00002177void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2178 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002179 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002180 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002181 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002182}
2183
Douglas Gregordee1be82009-01-17 00:42:38 +00002184void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002185 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002186
2187 if (isa<CXXRecordDecl>(this)) {
2188 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2189 struct CXXRecordDecl::DefinitionData *Data =
2190 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002191 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2192 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002193 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002194}
2195
2196void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002197 assert((!isa<CXXRecordDecl>(this) ||
2198 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2199 "definition completed but not started");
2200
Douglas Gregordee1be82009-01-17 00:42:38 +00002201 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002202 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002203
2204 if (ASTMutationListener *L = getASTMutationListener())
2205 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002206}
2207
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002208TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002209 if (isDefinition())
2210 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002211 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2212 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002213
2214 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002215 R != REnd; ++R)
2216 if (R->isDefinition())
2217 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002218
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002219 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002220}
2221
Douglas Gregor14454802011-02-25 02:25:35 +00002222void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2223 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002224 // Make sure the extended qualifier info is allocated.
2225 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002226 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002227 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002228 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002229 }
2230 else {
2231 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002232 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002233 if (getExtInfo()->NumTemplParamLists == 0) {
2234 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002235 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002236 }
2237 else
2238 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002239 }
2240 }
2241}
2242
Abramo Bagnara60804e12011-03-18 15:16:37 +00002243void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2244 unsigned NumTPLists,
2245 TemplateParameterList **TPLists) {
2246 assert(NumTPLists > 0);
2247 // Make sure the extended decl info is allocated.
2248 if (!hasExtInfo())
2249 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002250 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002251 // Set the template parameter lists info.
2252 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2253}
2254
Ted Kremenek21475702008-09-05 17:16:31 +00002255//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002256// EnumDecl Implementation
2257//===----------------------------------------------------------------------===//
2258
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002259EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2260 SourceLocation StartLoc, SourceLocation IdLoc,
2261 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002262 EnumDecl *PrevDecl, bool IsScoped,
2263 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002264 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002265 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002266 C.getTypeDeclType(Enum, PrevDecl);
2267 return Enum;
2268}
2269
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002270EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002271 return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002272 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002273}
2274
Douglas Gregord5058122010-02-11 01:19:42 +00002275void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002276 QualType NewPromotionType,
2277 unsigned NumPositiveBits,
2278 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002279 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002280 if (!IntegerType)
2281 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002282 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002283 setNumPositiveBits(NumPositiveBits);
2284 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002285 TagDecl::completeDefinition();
2286}
2287
2288//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002289// RecordDecl Implementation
2290//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002291
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002292RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2293 SourceLocation StartLoc, SourceLocation IdLoc,
2294 IdentifierInfo *Id, RecordDecl *PrevDecl)
2295 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002296 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002297 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002298 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002299 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002300 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002301}
2302
Jay Foad39c79802011-01-12 09:06:06 +00002303RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002304 SourceLocation StartLoc, SourceLocation IdLoc,
2305 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2306 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2307 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002308 C.getTypeDeclType(R, PrevDecl);
2309 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002310}
2311
Jay Foad39c79802011-01-12 09:06:06 +00002312RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002313 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2314 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002315}
2316
Douglas Gregordfcad112009-03-25 15:59:44 +00002317bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002318 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002319 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2320}
2321
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002322RecordDecl::field_iterator RecordDecl::field_begin() const {
2323 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2324 LoadFieldsFromExternalStorage();
2325
2326 return field_iterator(decl_iterator(FirstDecl));
2327}
2328
Douglas Gregorb11aad82011-02-19 18:51:44 +00002329/// completeDefinition - Notes that the definition of this type is now
2330/// complete.
2331void RecordDecl::completeDefinition() {
2332 assert(!isDefinition() && "Cannot redefine record!");
2333 TagDecl::completeDefinition();
2334}
2335
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002336void RecordDecl::LoadFieldsFromExternalStorage() const {
2337 ExternalASTSource *Source = getASTContext().getExternalSource();
2338 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2339
2340 // Notify that we have a RecordDecl doing some initialization.
2341 ExternalASTSource::Deserializing TheFields(Source);
2342
2343 llvm::SmallVector<Decl*, 64> Decls;
2344 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2345 return;
2346
2347#ifndef NDEBUG
2348 // Check that all decls we got were FieldDecls.
2349 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2350 assert(isa<FieldDecl>(Decls[i]));
2351#endif
2352
2353 LoadedFieldsFromExternalStorage = true;
2354
2355 if (Decls.empty())
2356 return;
2357
2358 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2359}
2360
Steve Naroff415d3d52008-10-08 17:01:13 +00002361//===----------------------------------------------------------------------===//
2362// BlockDecl Implementation
2363//===----------------------------------------------------------------------===//
2364
Douglas Gregord5058122010-02-11 01:19:42 +00002365void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002366 unsigned NParms) {
2367 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002368
Steve Naroffc4b30e52009-03-13 16:56:44 +00002369 // Zero params -> null pointer.
2370 if (NParms) {
2371 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002372 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002373 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2374 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2375 }
2376}
2377
John McCall351762c2011-02-07 10:33:21 +00002378void BlockDecl::setCaptures(ASTContext &Context,
2379 const Capture *begin,
2380 const Capture *end,
2381 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002382 CapturesCXXThis = capturesCXXThis;
2383
2384 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002385 NumCaptures = 0;
2386 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002387 return;
2388 }
2389
John McCall351762c2011-02-07 10:33:21 +00002390 NumCaptures = end - begin;
2391
2392 // Avoid new Capture[] because we don't want to provide a default
2393 // constructor.
2394 size_t allocationSize = NumCaptures * sizeof(Capture);
2395 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2396 memcpy(buffer, begin, allocationSize);
2397 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002398}
Sebastian Redl833ef452010-01-26 22:01:41 +00002399
John McCallce45f882011-06-15 22:51:16 +00002400bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2401 for (capture_const_iterator
2402 i = capture_begin(), e = capture_end(); i != e; ++i)
2403 // Only auto vars can be captured, so no redeclaration worries.
2404 if (i->getVariable() == variable)
2405 return true;
2406
2407 return false;
2408}
2409
Douglas Gregor70226da2010-12-21 16:27:07 +00002410SourceRange BlockDecl::getSourceRange() const {
2411 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2412}
Sebastian Redl833ef452010-01-26 22:01:41 +00002413
2414//===----------------------------------------------------------------------===//
2415// Other Decl Allocation/Deallocation Method Implementations
2416//===----------------------------------------------------------------------===//
2417
2418TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2419 return new (C) TranslationUnitDecl(C);
2420}
2421
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002422LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002423 SourceLocation IdentL, IdentifierInfo *II) {
2424 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2425}
2426
2427LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2428 SourceLocation IdentL, IdentifierInfo *II,
2429 SourceLocation GnuLabelL) {
2430 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2431 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002432}
2433
2434
Sebastian Redl833ef452010-01-26 22:01:41 +00002435NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002436 SourceLocation StartLoc,
2437 SourceLocation IdLoc, IdentifierInfo *Id) {
2438 return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
Sebastian Redl833ef452010-01-26 22:01:41 +00002439}
2440
Douglas Gregor417e87c2010-10-27 19:49:05 +00002441NamespaceDecl *NamespaceDecl::getNextNamespace() {
2442 return dyn_cast_or_null<NamespaceDecl>(
2443 NextNamespace.get(getASTContext().getExternalSource()));
2444}
2445
Sebastian Redl833ef452010-01-26 22:01:41 +00002446ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002447 SourceLocation IdLoc,
2448 IdentifierInfo *Id,
2449 QualType Type) {
2450 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002451}
2452
2453FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002454 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002455 const DeclarationNameInfo &NameInfo,
2456 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002457 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002458 bool isInlineSpecified,
2459 bool hasWrittenPrototype) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002460 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2461 T, TInfo, SC, SCAsWritten,
2462 isInlineSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002463 New->HasWrittenPrototype = hasWrittenPrototype;
2464 return New;
2465}
2466
2467BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2468 return new (C) BlockDecl(DC, L);
2469}
2470
2471EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2472 SourceLocation L,
2473 IdentifierInfo *Id, QualType T,
2474 Expr *E, const llvm::APSInt &V) {
2475 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2476}
2477
Benjamin Kramer39593702010-11-21 14:11:41 +00002478IndirectFieldDecl *
2479IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2480 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2481 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002482 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2483}
2484
Douglas Gregorbe996932010-09-01 20:41:53 +00002485SourceRange EnumConstantDecl::getSourceRange() const {
2486 SourceLocation End = getLocation();
2487 if (Init)
2488 End = Init->getLocEnd();
2489 return SourceRange(getLocation(), End);
2490}
2491
Sebastian Redl833ef452010-01-26 22:01:41 +00002492TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002493 SourceLocation StartLoc, SourceLocation IdLoc,
2494 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2495 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002496}
2497
Richard Smithdda56e42011-04-15 14:24:37 +00002498TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2499 SourceLocation StartLoc,
2500 SourceLocation IdLoc, IdentifierInfo *Id,
2501 TypeSourceInfo *TInfo) {
2502 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2503}
2504
Abramo Bagnaraea947882011-03-08 16:41:52 +00002505SourceRange TypedefDecl::getSourceRange() const {
2506 SourceLocation RangeEnd = getLocation();
2507 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2508 if (typeIsPostfix(TInfo->getType()))
2509 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2510 }
2511 return SourceRange(getLocStart(), RangeEnd);
2512}
2513
Richard Smithdda56e42011-04-15 14:24:37 +00002514SourceRange TypeAliasDecl::getSourceRange() const {
2515 SourceLocation RangeEnd = getLocStart();
2516 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2517 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2518 return SourceRange(getLocStart(), RangeEnd);
2519}
2520
Sebastian Redl833ef452010-01-26 22:01:41 +00002521FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002522 StringLiteral *Str,
2523 SourceLocation AsmLoc,
2524 SourceLocation RParenLoc) {
2525 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002526}