blob: b21ba9a65e761a1b3a1e3ece04097e5039bdae6b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidise184bae2008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor2a3009a2009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff0de21fd2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor7da97d02009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattner6c2b6eb2008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidisb17166c2009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes99f06ba2008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson337cba42009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara465d41b2010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
Douglas Gregor4421d2b2011-03-26 12:10:19 +000028#include "clang/Basic/TargetInfo.h"
John McCallf1bbbb42009-09-04 01:14:41 +000029#include "llvm/Support/ErrorHandling.h"
Ted Kremenek27f8a282008-05-20 00:43:19 +000030
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
32
Chris Lattnerd3b90652008-03-15 05:43:15 +000033//===----------------------------------------------------------------------===//
Douglas Gregor4afa39d2009-01-20 01:17:11 +000034// NamedDecl Implementation
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +000035//===----------------------------------------------------------------------===//
36
Douglas Gregor4421d2b2011-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 McCall7f1b9872010-12-18 03:30:47 +000048
John McCall1fb0caa2010-10-22 21:05:15 +000049 return DefaultVisibility;
John McCall1fb0caa2010-10-22 21:05:15 +000050 }
Douglas Gregor4421d2b2011-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 Dunbardb57a4c2011-04-19 21:43:27 +000054 if (D->getASTContext().Target.getTriple().isOSDarwin()) {
Douglas Gregor4421d2b2011-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 McCall1fb0caa2010-10-22 21:05:15 +000064}
65
John McCallaf146032010-10-30 11:50:40 +000066typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall1fb0caa2010-10-22 21:05:15 +000067typedef std::pair<Linkage,Visibility> LVPair;
John McCallaf146032010-10-30 11:50:40 +000068
John McCall1fb0caa2010-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 McCallaf146032010-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 Kramer752c2e92010-11-05 19:56:37 +000079namespace {
John McCall36987482010-11-02 01:45:15 +000080/// Flags controlling the computation of linkage and visibility.
81struct LVFlags {
82 bool ConsiderGlobalVisibility;
83 bool ConsiderVisibilityAttributes;
John McCall1a0918a2011-03-04 10:39:25 +000084 bool ConsiderTemplateParameterTypes;
John McCall36987482010-11-02 01:45:15 +000085
86 LVFlags() : ConsiderGlobalVisibility(true),
John McCall1a0918a2011-03-04 10:39:25 +000087 ConsiderVisibilityAttributes(true),
88 ConsiderTemplateParameterTypes(true) {
John McCall36987482010-11-02 01:45:15 +000089 }
90
Douglas Gregor381d34e2010-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 McCall1a0918a2011-03-04 10:39:25 +000097 F.ConsiderTemplateParameterTypes = false;
Douglas Gregor381d34e2010-12-06 18:36:25 +000098 return F;
99 }
100
John McCall36987482010-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 McCall1a0918a2011-03-04 10:39:25 +0000107 F.ConsiderTemplateParameterTypes = false;
John McCall36987482010-11-02 01:45:15 +0000108 return F;
109 }
Douglas Gregor89d63e52010-12-06 18:50:56 +0000110};
Benjamin Kramer752c2e92010-11-05 19:56:37 +0000111} // end anonymous namespace
John McCall36987482010-11-02 01:45:15 +0000112
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000113/// \brief Get the most restrictive linkage for the types in the given
114/// template parameter list.
John McCall1fb0caa2010-10-22 21:05:15 +0000115static LVPair
116getLVForTemplateParameterList(const TemplateParameterList *Params) {
117 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000118 for (TemplateParameterList::const_iterator P = Params->begin(),
119 PEnd = Params->end();
120 P != PEnd; ++P) {
Douglas Gregor6952f1e2011-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 Gregor0b6bc8b2010-02-03 09:33:45 +0000131 if (!NTTP->getType()->isDependentType()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000132 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000133 continue;
134 }
Douglas Gregor6952f1e2011-01-19 20:10:05 +0000135 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000136
137 if (TemplateTemplateParmDecl *TTP
138 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallaf146032010-10-30 11:50:40 +0000139 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000140 }
141 }
142
John McCall1fb0caa2010-10-22 21:05:15 +0000143 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000144}
145
Douglas Gregor381d34e2010-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 Gregor0b6bc8b2010-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 McCall1fb0caa2010-10-22 21:05:15 +0000151static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregor381d34e2010-12-06 18:36:25 +0000152 unsigned NumArgs,
153 LVFlags &F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000154 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor0b6bc8b2010-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 McCall1fb0caa2010-10-22 21:05:15 +0000164 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000165 break;
166
167 case TemplateArgument::Declaration:
John McCall1fb0caa2010-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 Gregor89d63e52010-12-06 18:50:56 +0000171 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
172 LV = merge(LV, getLVForDecl(ND, F));
John McCall1fb0caa2010-10-22 21:05:15 +0000173 }
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000174 break;
175
176 case TemplateArgument::Template:
Douglas Gregora7fc9012011-01-05 18:58:31 +0000177 case TemplateArgument::TemplateExpansion:
178 if (TemplateDecl *Template
179 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor89d63e52010-12-06 18:50:56 +0000180 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000181 break;
182
183 case TemplateArgument::Pack:
John McCall1fb0caa2010-10-22 21:05:15 +0000184 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregor381d34e2010-12-06 18:36:25 +0000185 Args[I].pack_size(),
186 F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000187 break;
188 }
189 }
190
John McCall1fb0caa2010-10-22 21:05:15 +0000191 return LV;
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000192}
193
John McCallaf146032010-10-30 11:50:40 +0000194static LVPair
Douglas Gregor381d34e2010-12-06 18:36:25 +0000195getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
196 LVFlags &F) {
197 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall3cdfc4d2010-08-13 08:35:10 +0000198}
199
John McCall36987482010-11-02 01:45:15 +0000200static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl7a126a42010-08-31 00:36:30 +0000201 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregord85b5b92009-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 McCalld931b082010-08-26 03:08:43 +0000213 if (Var->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000214 return LinkageInfo::internal();
Douglas Gregord85b5b92009-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 Friedmane9d65542009-11-26 03:04:01 +0000221 Var->getType().isConstant(Context) &&
John McCalld931b082010-08-26 03:08:43 +0000222 Var->getStorageClass() != SC_Extern &&
223 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000224 bool FoundExtern = false;
225 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
226 PrevVar && !FoundExtern;
227 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000228 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregord85b5b92009-11-25 22:24:25 +0000229 FoundExtern = true;
230
231 if (!FoundExtern)
John McCallaf146032010-10-30 11:50:40 +0000232 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000233 }
234 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000235 // C++ [temp]p4:
236 // A non-member function template can have internal linkage; any
237 // other template name shall have external linkage.
Douglas Gregord85b5b92009-11-25 22:24:25 +0000238 const FunctionDecl *Function = 0;
239 if (const FunctionTemplateDecl *FunTmpl
240 = dyn_cast<FunctionTemplateDecl>(D))
241 Function = FunTmpl->getTemplatedDecl();
242 else
243 Function = cast<FunctionDecl>(D);
244
245 // Explicitly declared static.
John McCalld931b082010-08-26 03:08:43 +0000246 if (Function->getStorageClass() == SC_Static)
John McCallaf146032010-10-30 11:50:40 +0000247 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000248 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
249 // - a data member of an anonymous union.
250 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallaf146032010-10-30 11:50:40 +0000251 return LinkageInfo::internal();
Douglas Gregord85b5b92009-11-25 22:24:25 +0000252 }
253
Chandler Carruth094b6432011-02-24 19:03:39 +0000254 if (D->isInAnonymousNamespace()) {
255 const VarDecl *Var = dyn_cast<VarDecl>(D);
256 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
257 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
258 return LinkageInfo::uniqueExternal();
259 }
John McCalle7bc9722010-10-28 04:18:25 +0000260
John McCall1fb0caa2010-10-22 21:05:15 +0000261 // Set up the defaults.
262
263 // C99 6.2.2p5:
264 // If the declaration of an identifier for an object has file
265 // scope and no storage-class specifier, its linkage is
266 // external.
John McCallaf146032010-10-30 11:50:40 +0000267 LinkageInfo LV;
268
John McCall36987482010-11-02 01:45:15 +0000269 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000270 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
271 LV.setVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000272 F.ConsiderGlobalVisibility = false;
John McCall90f14502010-12-10 02:59:44 +0000273 } else {
274 // If we're declared in a namespace with a visibility attribute,
275 // use that namespace's visibility, but don't call it explicit.
276 for (const DeclContext *DC = D->getDeclContext();
277 !isa<TranslationUnitDecl>(DC);
278 DC = DC->getParent()) {
279 if (!isa<NamespaceDecl>(DC)) continue;
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000280 if (llvm::Optional<Visibility> Vis
281 = cast<NamespaceDecl>(DC)->getExplicitVisibility()) {
282 LV.setVisibility(*Vis, false);
John McCall90f14502010-12-10 02:59:44 +0000283 F.ConsiderGlobalVisibility = false;
284 break;
285 }
286 }
John McCall36987482010-11-02 01:45:15 +0000287 }
John McCallaf146032010-10-30 11:50:40 +0000288 }
John McCall1fb0caa2010-10-22 21:05:15 +0000289
Douglas Gregord85b5b92009-11-25 22:24:25 +0000290 // C++ [basic.link]p4:
John McCall1fb0caa2010-10-22 21:05:15 +0000291
Douglas Gregord85b5b92009-11-25 22:24:25 +0000292 // A name having namespace scope has external linkage if it is the
293 // name of
294 //
295 // - an object or reference, unless it has internal linkage; or
296 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000297 // GCC applies the following optimization to variables and static
298 // data members, but not to functions:
299 //
John McCall1fb0caa2010-10-22 21:05:15 +0000300 // Modify the variable's LV by the LV of its type unless this is
301 // C or extern "C". This follows from [basic.link]p9:
302 // A type without linkage shall not be used as the type of a
303 // variable or function with external linkage unless
304 // - the entity has C language linkage, or
305 // - the entity is declared within an unnamed namespace, or
306 // - the entity is not used or is defined in the same
307 // translation unit.
308 // and [basic.link]p10:
309 // ...the types specified by all declarations referring to a
310 // given variable or function shall be identical...
311 // C does not have an equivalent rule.
312 //
John McCallac65c622010-10-26 04:59:26 +0000313 // Ignore this if we've got an explicit attribute; the user
314 // probably knows what they're doing.
315 //
John McCall1fb0caa2010-10-22 21:05:15 +0000316 // Note that we don't want to make the variable non-external
317 // because of this, but unique-external linkage suits us.
John McCallee301022010-10-30 09:18:49 +0000318 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall1fb0caa2010-10-22 21:05:15 +0000319 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
320 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000321 return LinkageInfo::uniqueExternal();
322 if (!LV.visibilityExplicit())
323 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000324 }
325
John McCall35cebc32010-11-02 18:38:13 +0000326 if (Var->getStorageClass() == SC_PrivateExtern)
327 LV.setVisibility(HiddenVisibility, true);
328
Douglas Gregord85b5b92009-11-25 22:24:25 +0000329 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000330 (Var->getStorageClass() == SC_Extern ||
331 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall1fb0caa2010-10-22 21:05:15 +0000332
Douglas Gregord85b5b92009-11-25 22:24:25 +0000333 // C99 6.2.2p4:
334 // For an identifier declared with the storage-class specifier
335 // extern in a scope in which a prior declaration of that
336 // identifier is visible, if the prior declaration specifies
337 // internal or external linkage, the linkage of the identifier
338 // at the later declaration is the same as the linkage
339 // specified at the prior declaration. If no prior declaration
340 // is visible, or if the prior declaration specifies no
341 // linkage, then the identifier has external linkage.
342 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000343 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallaf146032010-10-30 11:50:40 +0000344 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
345 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000346 }
347 }
348
Douglas Gregord85b5b92009-11-25 22:24:25 +0000349 // - a function, unless it has internal linkage; or
John McCall1fb0caa2010-10-22 21:05:15 +0000350 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall67fa6d52010-10-28 07:07:52 +0000351 // In theory, we can modify the function's LV by the LV of its
352 // type unless it has C linkage (see comment above about variables
353 // for justification). In practice, GCC doesn't do this, so it's
354 // just too painful to make work.
John McCall1fb0caa2010-10-22 21:05:15 +0000355
John McCall35cebc32010-11-02 18:38:13 +0000356 if (Function->getStorageClass() == SC_PrivateExtern)
357 LV.setVisibility(HiddenVisibility, true);
358
Douglas Gregord85b5b92009-11-25 22:24:25 +0000359 // C99 6.2.2p5:
360 // If the declaration of an identifier for a function has no
361 // storage-class specifier, its linkage is determined exactly
362 // as if it were declared with the storage-class specifier
363 // extern.
364 if (!Context.getLangOptions().CPlusPlus &&
John McCalld931b082010-08-26 03:08:43 +0000365 (Function->getStorageClass() == SC_Extern ||
366 Function->getStorageClass() == SC_PrivateExtern ||
367 Function->getStorageClass() == SC_None)) {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000368 // C99 6.2.2p4:
369 // For an identifier declared with the storage-class specifier
370 // extern in a scope in which a prior declaration of that
371 // identifier is visible, if the prior declaration specifies
372 // internal or external linkage, the linkage of the identifier
373 // at the later declaration is the same as the linkage
374 // specified at the prior declaration. If no prior declaration
375 // is visible, or if the prior declaration specifies no
376 // linkage, then the identifier has external linkage.
377 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000378 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallaf146032010-10-30 11:50:40 +0000379 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
380 LV.mergeVisibility(PrevLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000381 }
382 }
383
John McCallaf8ca372011-02-10 06:50:24 +0000384 // In C++, then if the type of the function uses a type with
385 // unique-external linkage, it's not legally usable from outside
386 // this translation unit. However, we should use the C linkage
387 // rules instead for extern "C" declarations.
388 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
389 Function->getType()->getLinkage() == UniqueExternalLinkage)
390 return LinkageInfo::uniqueExternal();
391
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000392 if (FunctionTemplateSpecializationInfo *SpecInfo
393 = Function->getTemplateSpecializationInfo()) {
John McCall36987482010-11-02 01:45:15 +0000394 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
395 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000396 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000397 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000398 }
399
Douglas Gregord85b5b92009-11-25 22:24:25 +0000400 // - a named class (Clause 9), or an unnamed class defined in a
401 // typedef declaration in which the class has the typedef name
402 // for linkage purposes (7.1.3); or
403 // - a named enumeration (7.2), or an unnamed enumeration
404 // defined in a typedef declaration in which the enumeration
405 // has the typedef name for linkage purposes (7.1.3); or
John McCall1fb0caa2010-10-22 21:05:15 +0000406 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
407 // Unnamed tags have no linkage.
Richard Smith162e1c12011-04-15 14:24:37 +0000408 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallaf146032010-10-30 11:50:40 +0000409 return LinkageInfo::none();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000410
John McCall1fb0caa2010-10-22 21:05:15 +0000411 // If this is a class template specialization, consider the
412 // linkage of the template and template arguments.
413 if (const ClassTemplateSpecializationDecl *Spec
414 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall36987482010-11-02 01:45:15 +0000415 // From the template.
416 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
417 F.onlyTemplateVisibility()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000418
John McCall1fb0caa2010-10-22 21:05:15 +0000419 // The arguments at which the template was instantiated.
420 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregor381d34e2010-12-06 18:36:25 +0000421 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000422 }
Douglas Gregord85b5b92009-11-25 22:24:25 +0000423
John McCallac65c622010-10-26 04:59:26 +0000424 // Consider -fvisibility unless the type has C linkage.
John McCall36987482010-11-02 01:45:15 +0000425 if (F.ConsiderGlobalVisibility)
426 F.ConsiderGlobalVisibility =
John McCallac65c622010-10-26 04:59:26 +0000427 (Context.getLangOptions().CPlusPlus &&
428 !Tag->getDeclContext()->isExternCContext());
John McCall1fb0caa2010-10-22 21:05:15 +0000429
Douglas Gregord85b5b92009-11-25 22:24:25 +0000430 // - an enumerator belonging to an enumeration with external linkage;
John McCall1fb0caa2010-10-22 21:05:15 +0000431 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000432 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallaf146032010-10-30 11:50:40 +0000433 if (!isExternalLinkage(EnumLV.linkage()))
434 return LinkageInfo::none();
435 LV.merge(EnumLV);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000436
437 // - a template, unless it is a function template that has
438 // internal linkage (Clause 14);
John McCall1a0918a2011-03-04 10:39:25 +0000439 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
440 if (F.ConsiderTemplateParameterTypes)
441 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000442
Douglas Gregord85b5b92009-11-25 22:24:25 +0000443 // - a namespace (7.3), unless it is declared within an unnamed
444 // namespace.
John McCall1fb0caa2010-10-22 21:05:15 +0000445 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
446 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000447
John McCall1fb0caa2010-10-22 21:05:15 +0000448 // By extension, we assign external linkage to Objective-C
449 // interfaces.
450 } else if (isa<ObjCInterfaceDecl>(D)) {
451 // fallout
452
453 // Everything not covered here has no linkage.
454 } else {
John McCallaf146032010-10-30 11:50:40 +0000455 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000456 }
457
458 // If we ended up with non-external linkage, visibility should
459 // always be default.
John McCallaf146032010-10-30 11:50:40 +0000460 if (LV.linkage() != ExternalLinkage)
461 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall1fb0caa2010-10-22 21:05:15 +0000462
463 // If we didn't end up with hidden visibility, consider attributes
464 // and -fvisibility.
John McCall36987482010-11-02 01:45:15 +0000465 if (F.ConsiderGlobalVisibility)
John McCallaf146032010-10-30 11:50:40 +0000466 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall1fb0caa2010-10-22 21:05:15 +0000467
468 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000469}
470
John McCall36987482010-11-02 01:45:15 +0000471static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall1fb0caa2010-10-22 21:05:15 +0000472 // Only certain class members have linkage. Note that fields don't
473 // really have linkage, but it's convenient to say they do for the
474 // purposes of calculating linkage of pointer-to-data-member
475 // template arguments.
John McCall3cdfc4d2010-08-13 08:35:10 +0000476 if (!(isa<CXXMethodDecl>(D) ||
477 isa<VarDecl>(D) ||
John McCall1fb0caa2010-10-22 21:05:15 +0000478 isa<FieldDecl>(D) ||
John McCall3cdfc4d2010-08-13 08:35:10 +0000479 (isa<TagDecl>(D) &&
Richard Smith162e1c12011-04-15 14:24:37 +0000480 (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl()))))
John McCallaf146032010-10-30 11:50:40 +0000481 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000482
John McCall36987482010-11-02 01:45:15 +0000483 LinkageInfo LV;
484
485 // The flags we're going to use to compute the class's visibility.
486 LVFlags ClassF = F;
487
488 // If we have an explicit visibility attribute, merge that in.
489 if (F.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000490 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
491 LV.mergeVisibility(*Vis, true);
John McCall36987482010-11-02 01:45:15 +0000492
493 // Ignore global visibility later, but not this attribute.
494 F.ConsiderGlobalVisibility = false;
495
496 // Ignore both global visibility and attributes when computing our
497 // parent's visibility.
498 ClassF = F.onlyTemplateVisibility();
499 }
500 }
John McCallaf146032010-10-30 11:50:40 +0000501
502 // Class members only have linkage if their class has external
John McCall36987482010-11-02 01:45:15 +0000503 // linkage.
504 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
505 if (!isExternalLinkage(LV.linkage()))
John McCallaf146032010-10-30 11:50:40 +0000506 return LinkageInfo::none();
John McCall3cdfc4d2010-08-13 08:35:10 +0000507
508 // If the class already has unique-external linkage, we can't improve.
John McCall36987482010-11-02 01:45:15 +0000509 if (LV.linkage() == UniqueExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000510 return LinkageInfo::uniqueExternal();
John McCall3cdfc4d2010-08-13 08:35:10 +0000511
John McCall3cdfc4d2010-08-13 08:35:10 +0000512 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallaf8ca372011-02-10 06:50:24 +0000513 // If the type of the function uses a type with unique-external
514 // linkage, it's not legally usable from outside this translation unit.
515 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
516 return LinkageInfo::uniqueExternal();
517
John McCall110e8e52010-10-29 22:22:43 +0000518 TemplateSpecializationKind TSK = TSK_Undeclared;
519
John McCall1fb0caa2010-10-22 21:05:15 +0000520 // If this is a method template specialization, use the linkage for
521 // the template parameters and arguments.
522 if (FunctionTemplateSpecializationInfo *Spec
John McCall3cdfc4d2010-08-13 08:35:10 +0000523 = MD->getTemplateSpecializationInfo()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000524 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCall1a0918a2011-03-04 10:39:25 +0000525 if (F.ConsiderTemplateParameterTypes)
526 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000527 Spec->getTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000528
529 TSK = Spec->getTemplateSpecializationKind();
530 } else if (MemberSpecializationInfo *MSI =
531 MD->getMemberSpecializationInfo()) {
532 TSK = MSI->getTemplateSpecializationKind();
John McCall3cdfc4d2010-08-13 08:35:10 +0000533 }
534
John McCall110e8e52010-10-29 22:22:43 +0000535 // If we're paying attention to global visibility, apply
536 // -finline-visibility-hidden if this is an inline method.
537 //
John McCallaf146032010-10-30 11:50:40 +0000538 // Note that ConsiderGlobalVisibility doesn't yet have information
539 // about whether containing classes have visibility attributes,
540 // and that's intentional.
541 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall36987482010-11-02 01:45:15 +0000542 F.ConsiderGlobalVisibility &&
John McCall66cbcf32010-11-01 01:29:57 +0000543 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
544 // InlineVisibilityHidden only applies to definitions, and
545 // isInlined() only gives meaningful answers on definitions
546 // anyway.
547 const FunctionDecl *Def = 0;
548 if (MD->hasBody(Def) && Def->isInlined())
549 LV.setVisibility(HiddenVisibility);
550 }
John McCall1fb0caa2010-10-22 21:05:15 +0000551
John McCall110e8e52010-10-29 22:22:43 +0000552 // Note that in contrast to basically every other situation, we
553 // *do* apply -fvisibility to method declarations.
554
555 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall110e8e52010-10-29 22:22:43 +0000556 if (const ClassTemplateSpecializationDecl *Spec
557 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
558 // Merge template argument/parameter information for member
559 // class template specializations.
Douglas Gregor381d34e2010-12-06 18:36:25 +0000560 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCall1a0918a2011-03-04 10:39:25 +0000561 if (F.ConsiderTemplateParameterTypes)
562 LV.merge(getLVForTemplateParameterList(
John McCall1fb0caa2010-10-22 21:05:15 +0000563 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall110e8e52010-10-29 22:22:43 +0000564 }
565
John McCall110e8e52010-10-29 22:22:43 +0000566 // Static data members.
567 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCallee301022010-10-30 09:18:49 +0000568 // Modify the variable's linkage by its type, but ignore the
569 // type's visibility unless it's a definition.
570 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
571 if (TypeLV.first != ExternalLinkage)
John McCallaf146032010-10-30 11:50:40 +0000572 LV.mergeLinkage(UniqueExternalLinkage);
573 if (!LV.visibilityExplicit())
574 LV.mergeVisibility(TypeLV.second);
John McCall110e8e52010-10-29 22:22:43 +0000575 }
576
John McCall36987482010-11-02 01:45:15 +0000577 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall110e8e52010-10-29 22:22:43 +0000578
579 // Apply -fvisibility if desired.
John McCall36987482010-11-02 01:45:15 +0000580 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallaf146032010-10-30 11:50:40 +0000581 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall3cdfc4d2010-08-13 08:35:10 +0000582 }
583
John McCall1fb0caa2010-10-22 21:05:15 +0000584 return LV;
John McCall3cdfc4d2010-08-13 08:35:10 +0000585}
586
John McCallf76b0922011-02-08 19:01:05 +0000587static void clearLinkageForClass(const CXXRecordDecl *record) {
588 for (CXXRecordDecl::decl_iterator
589 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
590 Decl *child = *i;
591 if (isa<NamedDecl>(child))
592 cast<NamedDecl>(child)->ClearLinkageCache();
593 }
594}
595
596void NamedDecl::ClearLinkageCache() {
597 // Note that we can't skip clearing the linkage of children just
598 // because the parent doesn't have cached linkage: we don't cache
599 // when computing linkage for parent contexts.
600
601 HasCachedLinkage = 0;
602
603 // If we're changing the linkage of a class, we need to reset the
604 // linkage of child declarations, too.
605 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
606 clearLinkageForClass(record);
607
John McCall15e310a2011-02-19 02:53:41 +0000608 if (ClassTemplateDecl *temp =
609 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCallf76b0922011-02-08 19:01:05 +0000610 // Clear linkage for the template pattern.
611 CXXRecordDecl *record = temp->getTemplatedDecl();
612 record->HasCachedLinkage = 0;
613 clearLinkageForClass(record);
614
John McCall15e310a2011-02-19 02:53:41 +0000615 // We need to clear linkage for specializations, too.
616 for (ClassTemplateDecl::spec_iterator
617 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
618 i->ClearLinkageCache();
John McCallf76b0922011-02-08 19:01:05 +0000619 }
John McCall15e310a2011-02-19 02:53:41 +0000620
621 // Clear cached linkage for function template decls, too.
622 if (FunctionTemplateDecl *temp =
John McCall78951942011-03-22 06:58:49 +0000623 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
624 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall15e310a2011-02-19 02:53:41 +0000625 for (FunctionTemplateDecl::spec_iterator
626 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
627 i->ClearLinkageCache();
John McCall78951942011-03-22 06:58:49 +0000628 }
John McCall15e310a2011-02-19 02:53:41 +0000629
John McCallf76b0922011-02-08 19:01:05 +0000630}
631
Douglas Gregor381d34e2010-12-06 18:36:25 +0000632Linkage NamedDecl::getLinkage() const {
633 if (HasCachedLinkage) {
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000634 assert(Linkage(CachedLinkage) ==
635 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000636 return Linkage(CachedLinkage);
637 }
638
639 CachedLinkage = getLVForDecl(this,
640 LVFlags::CreateOnlyDeclLinkage()).linkage();
641 HasCachedLinkage = 1;
642 return Linkage(CachedLinkage);
643}
644
John McCallaf146032010-10-30 11:50:40 +0000645LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000646 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer56ed7922010-12-07 15:51:48 +0000647 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregor381d34e2010-12-06 18:36:25 +0000648 HasCachedLinkage = 1;
649 CachedLinkage = LI.linkage();
650 return LI;
John McCall0df95872010-10-29 00:29:13 +0000651}
Ted Kremenekbecc3082010-04-20 23:15:35 +0000652
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000653llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
654 // Use the most recent declaration of a variable.
655 if (const VarDecl *var = dyn_cast<VarDecl>(this))
656 return getVisibilityOf(var->getMostRecentDeclaration());
657
658 // Use the most recent declaration of a function, and also handle
659 // function template specializations.
660 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
661 if (llvm::Optional<Visibility> V
662 = getVisibilityOf(fn->getMostRecentDeclaration()))
663 return V;
664
665 // If the function is a specialization of a template with an
666 // explicit visibility attribute, use that.
667 if (FunctionTemplateSpecializationInfo *templateInfo
668 = fn->getTemplateSpecializationInfo())
669 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
670
671 return llvm::Optional<Visibility>();
672 }
673
674 // Otherwise, just check the declaration itself first.
675 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
676 return V;
677
678 // If there wasn't explicit visibility there, and this is a
679 // specialization of a class template, check for visibility
680 // on the pattern.
681 if (const ClassTemplateSpecializationDecl *spec
682 = dyn_cast<ClassTemplateSpecializationDecl>(this))
683 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
684
685 return llvm::Optional<Visibility>();
686}
687
John McCall36987482010-11-02 01:45:15 +0000688static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000689 // Objective-C: treat all Objective-C declarations as having external
690 // linkage.
John McCall0df95872010-10-29 00:29:13 +0000691 switch (D->getKind()) {
Ted Kremenekbecc3082010-04-20 23:15:35 +0000692 default:
693 break;
John McCall1fb0caa2010-10-22 21:05:15 +0000694 case Decl::TemplateTemplateParm: // count these as external
695 case Decl::NonTypeTemplateParm:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000696 case Decl::ObjCAtDefsField:
697 case Decl::ObjCCategory:
698 case Decl::ObjCCategoryImpl:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000699 case Decl::ObjCCompatibleAlias:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000700 case Decl::ObjCForwardProtocol:
701 case Decl::ObjCImplementation:
Ted Kremenekbecc3082010-04-20 23:15:35 +0000702 case Decl::ObjCMethod:
703 case Decl::ObjCProperty:
704 case Decl::ObjCPropertyImpl:
705 case Decl::ObjCProtocol:
John McCallaf146032010-10-30 11:50:40 +0000706 return LinkageInfo::external();
Ted Kremenekbecc3082010-04-20 23:15:35 +0000707 }
708
Douglas Gregord85b5b92009-11-25 22:24:25 +0000709 // Handle linkage for namespace-scope names.
John McCall0df95872010-10-29 00:29:13 +0000710 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall36987482010-11-02 01:45:15 +0000711 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000712
713 // C++ [basic.link]p5:
714 // In addition, a member function, static data member, a named
715 // class or enumeration of class scope, or an unnamed class or
716 // enumeration defined in a class-scope typedef declaration such
717 // that the class or enumeration has the typedef name for linkage
718 // purposes (7.1.3), has external linkage if the name of the class
719 // has external linkage.
John McCall0df95872010-10-29 00:29:13 +0000720 if (D->getDeclContext()->isRecord())
John McCall36987482010-11-02 01:45:15 +0000721 return getLVForClassMember(D, Flags);
Douglas Gregord85b5b92009-11-25 22:24:25 +0000722
723 // C++ [basic.link]p6:
724 // The name of a function declared in block scope and the name of
725 // an object declared by a block scope extern declaration have
726 // linkage. If there is a visible declaration of an entity with
727 // linkage having the same name and type, ignoring entities
728 // declared outside the innermost enclosing namespace scope, the
729 // block scope declaration declares that same entity and receives
730 // the linkage of the previous declaration. If there is more than
731 // one such matching entity, the program is ill-formed. Otherwise,
732 // if no matching entity is found, the block scope entity receives
733 // external linkage.
John McCall0df95872010-10-29 00:29:13 +0000734 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
735 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Chandler Carruth10aad442011-02-25 00:05:02 +0000736 if (Function->isInAnonymousNamespace() && !Function->isExternC())
John McCallaf146032010-10-30 11:50:40 +0000737 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000738
John McCallaf146032010-10-30 11:50:40 +0000739 LinkageInfo LV;
Douglas Gregor381d34e2010-12-06 18:36:25 +0000740 if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000741 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
742 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000743 }
744
John McCall1fb0caa2010-10-22 21:05:15 +0000745 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000746 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000747 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
748 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000749 }
750
751 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000752 }
753
John McCall0df95872010-10-29 00:29:13 +0000754 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCalld931b082010-08-26 03:08:43 +0000755 if (Var->getStorageClass() == SC_Extern ||
756 Var->getStorageClass() == SC_PrivateExtern) {
Chandler Carruth10aad442011-02-25 00:05:02 +0000757 if (Var->isInAnonymousNamespace() && !Var->isExternC())
John McCallaf146032010-10-30 11:50:40 +0000758 return LinkageInfo::uniqueExternal();
Douglas Gregor0b6bc8b2010-02-03 09:33:45 +0000759
John McCallaf146032010-10-30 11:50:40 +0000760 LinkageInfo LV;
John McCall1fb0caa2010-10-22 21:05:15 +0000761 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallaf146032010-10-30 11:50:40 +0000762 LV.setVisibility(HiddenVisibility);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000763 else if (Flags.ConsiderVisibilityAttributes) {
Douglas Gregor4421d2b2011-03-26 12:10:19 +0000764 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
765 LV.setVisibility(*Vis);
Douglas Gregor381d34e2010-12-06 18:36:25 +0000766 }
767
John McCall1fb0caa2010-10-22 21:05:15 +0000768 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregor381d34e2010-12-06 18:36:25 +0000769 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallaf146032010-10-30 11:50:40 +0000770 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
771 LV.mergeVisibility(PrevLV);
John McCall1fb0caa2010-10-22 21:05:15 +0000772 }
773
774 return LV;
Douglas Gregord85b5b92009-11-25 22:24:25 +0000775 }
776 }
777
778 // C++ [basic.link]p6:
779 // Names not covered by these rules have no linkage.
John McCallaf146032010-10-30 11:50:40 +0000780 return LinkageInfo::none();
John McCall1fb0caa2010-10-22 21:05:15 +0000781}
Douglas Gregord85b5b92009-11-25 22:24:25 +0000782
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000783std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000784 return getQualifiedNameAsString(getASTContext().getLangOptions());
785}
786
787std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000788 const DeclContext *Ctx = getDeclContext();
789
790 if (Ctx->isFunctionOrMethod())
791 return getNameAsString();
792
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000793 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
794 ContextsTy Contexts;
795
796 // Collect contexts.
797 while (Ctx && isa<NamedDecl>(Ctx)) {
798 Contexts.push_back(Ctx);
799 Ctx = Ctx->getParent();
800 };
801
802 std::string QualName;
803 llvm::raw_string_ostream OS(QualName);
804
805 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
806 I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000807 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000808 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregorf3e7ce42009-05-18 17:01:57 +0000809 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
810 std::string TemplateArgsStr
811 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor910f8002010-11-07 23:05:16 +0000812 TemplateArgs.data(),
813 TemplateArgs.size(),
Anders Carlsson3a082d82009-09-08 18:24:21 +0000814 P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000815 OS << Spec->getName() << TemplateArgsStr;
816 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig6be11202009-12-24 23:15:03 +0000817 if (ND->isAnonymousNamespace())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000818 OS << "<anonymous namespace>";
Sam Weinig6be11202009-12-24 23:15:03 +0000819 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000820 OS << ND;
821 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
822 if (!RD->getIdentifier())
823 OS << "<anonymous " << RD->getKindName() << '>';
824 else
825 OS << RD;
826 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinig3521d012009-12-28 03:19:38 +0000827 const FunctionProtoType *FT = 0;
828 if (FD->hasWrittenPrototype())
829 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
830
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000831 OS << FD << '(';
Sam Weinig3521d012009-12-28 03:19:38 +0000832 if (FT) {
Sam Weinig3521d012009-12-28 03:19:38 +0000833 unsigned NumParams = FD->getNumParams();
834 for (unsigned i = 0; i < NumParams; ++i) {
835 if (i)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000836 OS << ", ";
Sam Weinig3521d012009-12-28 03:19:38 +0000837 std::string Param;
838 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000839 OS << Param;
Sam Weinig3521d012009-12-28 03:19:38 +0000840 }
841
842 if (FT->isVariadic()) {
843 if (NumParams > 0)
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000844 OS << ", ";
845 OS << "...";
Sam Weinig3521d012009-12-28 03:19:38 +0000846 }
847 }
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000848 OS << ')';
849 } else {
850 OS << cast<NamedDecl>(*I);
851 }
852 OS << "::";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000853 }
854
John McCall8472af42010-03-16 21:48:18 +0000855 if (getDeclName())
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000856 OS << this;
John McCall8472af42010-03-16 21:48:18 +0000857 else
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000858 OS << "<anonymous>";
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000859
Benjamin Kramer68eebbb2010-04-28 14:33:51 +0000860 return OS.str();
Douglas Gregor47b9a1c2009-02-04 17:27:36 +0000861}
862
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000863bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000864 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
865
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000866 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
867 // We want to keep it, unless it nominates same namespace.
868 if (getKind() == Decl::UsingDirective) {
Douglas Gregordb992412011-02-25 16:33:46 +0000869 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
870 ->getOriginalNamespace() ==
871 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
872 ->getOriginalNamespace();
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000875 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
876 // For function declarations, we keep track of redeclarations.
877 return FD->getPreviousDeclaration() == OldD;
878
Douglas Gregore53060f2009-06-25 22:08:12 +0000879 // For function templates, the underlying function declarations are linked.
880 if (const FunctionTemplateDecl *FunctionTemplate
881 = dyn_cast<FunctionTemplateDecl>(this))
882 if (const FunctionTemplateDecl *OldFunctionTemplate
883 = dyn_cast<FunctionTemplateDecl>(OldD))
884 return FunctionTemplate->getTemplatedDecl()
885 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Steve Naroff0de21fd2009-02-22 19:35:57 +0000887 // For method declarations, we keep track of redeclarations.
888 if (isa<ObjCMethodDecl>(this))
889 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000890
John McCallf36e02d2009-10-09 21:13:30 +0000891 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
892 return true;
893
John McCall9488ea12009-11-17 05:59:44 +0000894 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
895 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
896 cast<UsingShadowDecl>(OldD)->getTargetDecl();
897
Douglas Gregordc355712011-02-25 00:36:19 +0000898 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
899 ASTContext &Context = getASTContext();
900 return Context.getCanonicalNestedNameSpecifier(
901 cast<UsingDecl>(this)->getQualifier()) ==
902 Context.getCanonicalNestedNameSpecifier(
903 cast<UsingDecl>(OldD)->getQualifier());
904 }
Argyrios Kyrtzidisc80117e2010-11-04 08:48:52 +0000905
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000906 // For non-function declarations, if the declarations are of the
907 // same kind then this must be a redeclaration, or semantic analysis
908 // would not have given us the new declaration.
909 return this->getKind() == OldD->getKind();
910}
911
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000912bool NamedDecl::hasLinkage() const {
Douglas Gregord85b5b92009-11-25 22:24:25 +0000913 return getLinkage() != NoLinkage;
Douglas Gregord6f7e9d2009-02-24 20:03:32 +0000914}
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000915
Anders Carlssone136e0e2009-06-26 06:29:23 +0000916NamedDecl *NamedDecl::getUnderlyingDecl() {
917 NamedDecl *ND = this;
918 while (true) {
John McCall9488ea12009-11-17 05:59:44 +0000919 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlssone136e0e2009-06-26 06:29:23 +0000920 ND = UD->getTargetDecl();
921 else if (ObjCCompatibleAliasDecl *AD
922 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
923 return AD->getClassInterface();
924 else
925 return ND;
926 }
927}
928
John McCall161755a2010-04-06 21:38:20 +0000929bool NamedDecl::isCXXInstanceMember() const {
930 assert(isCXXClassMember() &&
931 "checking whether non-member is instance member");
932
933 const NamedDecl *D = this;
934 if (isa<UsingShadowDecl>(D))
935 D = cast<UsingShadowDecl>(D)->getTargetDecl();
936
Francois Pichet87c2e122010-11-21 06:08:52 +0000937 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCall161755a2010-04-06 21:38:20 +0000938 return true;
939 if (isa<CXXMethodDecl>(D))
940 return cast<CXXMethodDecl>(D)->isInstance();
941 if (isa<FunctionTemplateDecl>(D))
942 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
943 ->getTemplatedDecl())->isInstance();
944 return false;
945}
946
Argyrios Kyrtzidis52393042008-11-09 23:41:00 +0000947//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000948// DeclaratorDecl Implementation
949//===----------------------------------------------------------------------===//
950
Douglas Gregor1693e152010-07-06 18:42:40 +0000951template <typename DeclT>
952static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
953 if (decl->getNumTemplateParameterLists() > 0)
954 return decl->getTemplateParameterList(0)->getTemplateLoc();
955 else
956 return decl->getInnerLocStart();
957}
958
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000959SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCall4e449832010-05-28 23:32:21 +0000960 TypeSourceInfo *TSI = getTypeSourceInfo();
961 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +0000962 return SourceLocation();
963}
964
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000965void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
966 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +0000967 // Make sure the extended decl info is allocated.
968 if (!hasExtInfo()) {
969 // Save (non-extended) type source info pointer.
970 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
971 // Allocate external info struct.
972 DeclInfo = new (getASTContext()) ExtInfo;
973 // Restore savedTInfo into (extended) decl info.
974 getExtInfo()->TInfo = savedTInfo;
975 }
976 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +0000977 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +0000978 }
979 else {
980 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +0000981 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +0000982 if (getExtInfo()->NumTemplParamLists == 0) {
983 // Save type source info pointer.
984 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
985 // Deallocate the extended decl info.
986 getASTContext().Deallocate(getExtInfo());
987 // Restore savedTInfo into (non-extended) decl info.
988 DeclInfo = savedTInfo;
989 }
990 else
991 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +0000992 }
993 }
994}
995
Abramo Bagnara7f0a9152011-03-18 15:16:37 +0000996void
997DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
998 unsigned NumTPLists,
999 TemplateParameterList **TPLists) {
1000 assert(NumTPLists > 0);
1001 // Make sure the extended decl info is allocated.
1002 if (!hasExtInfo()) {
1003 // Save (non-extended) type source info pointer.
1004 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1005 // Allocate external info struct.
1006 DeclInfo = new (getASTContext()) ExtInfo;
1007 // Restore savedTInfo into (extended) decl info.
1008 getExtInfo()->TInfo = savedTInfo;
1009 }
1010 // Set the template parameter lists info.
1011 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1012}
1013
Douglas Gregor1693e152010-07-06 18:42:40 +00001014SourceLocation DeclaratorDecl::getOuterLocStart() const {
1015 return getTemplateOrInnerLocStart(this);
1016}
1017
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001018namespace {
1019
1020// Helper function: returns true if QT is or contains a type
1021// having a postfix component.
1022bool typeIsPostfix(clang::QualType QT) {
1023 while (true) {
1024 const Type* T = QT.getTypePtr();
1025 switch (T->getTypeClass()) {
1026 default:
1027 return false;
1028 case Type::Pointer:
1029 QT = cast<PointerType>(T)->getPointeeType();
1030 break;
1031 case Type::BlockPointer:
1032 QT = cast<BlockPointerType>(T)->getPointeeType();
1033 break;
1034 case Type::MemberPointer:
1035 QT = cast<MemberPointerType>(T)->getPointeeType();
1036 break;
1037 case Type::LValueReference:
1038 case Type::RValueReference:
1039 QT = cast<ReferenceType>(T)->getPointeeType();
1040 break;
1041 case Type::PackExpansion:
1042 QT = cast<PackExpansionType>(T)->getPattern();
1043 break;
1044 case Type::Paren:
1045 case Type::ConstantArray:
1046 case Type::DependentSizedArray:
1047 case Type::IncompleteArray:
1048 case Type::VariableArray:
1049 case Type::FunctionProto:
1050 case Type::FunctionNoProto:
1051 return true;
1052 }
1053 }
1054}
1055
1056} // namespace
1057
1058SourceRange DeclaratorDecl::getSourceRange() const {
1059 SourceLocation RangeEnd = getLocation();
1060 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1061 if (typeIsPostfix(TInfo->getType()))
1062 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1063 }
1064 return SourceRange(getOuterLocStart(), RangeEnd);
1065}
1066
Abramo Bagnara9b934882010-06-12 08:15:14 +00001067void
Douglas Gregorc722ea42010-06-15 17:44:38 +00001068QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1069 unsigned NumTPLists,
Abramo Bagnara9b934882010-06-12 08:15:14 +00001070 TemplateParameterList **TPLists) {
1071 assert((NumTPLists == 0 || TPLists != 0) &&
1072 "Empty array of template parameters with positive size!");
Abramo Bagnara9b934882010-06-12 08:15:14 +00001073
1074 // Free previous template parameters (if any).
1075 if (NumTemplParamLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001076 Context.Deallocate(TemplParamLists);
Abramo Bagnara9b934882010-06-12 08:15:14 +00001077 TemplParamLists = 0;
1078 NumTemplParamLists = 0;
1079 }
1080 // Set info on matched template parameter lists (if any).
1081 if (NumTPLists > 0) {
Douglas Gregorc722ea42010-06-15 17:44:38 +00001082 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnara9b934882010-06-12 08:15:14 +00001083 NumTemplParamLists = NumTPLists;
1084 for (unsigned i = NumTPLists; i-- > 0; )
1085 TemplParamLists[i] = TPLists[i];
1086 }
1087}
1088
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001089//===----------------------------------------------------------------------===//
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001090// VarDecl Implementation
1091//===----------------------------------------------------------------------===//
1092
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001093const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1094 switch (SC) {
John McCalld931b082010-08-26 03:08:43 +00001095 case SC_None: break;
1096 case SC_Auto: return "auto"; break;
1097 case SC_Extern: return "extern"; break;
1098 case SC_PrivateExtern: return "__private_extern__"; break;
1099 case SC_Register: return "register"; break;
1100 case SC_Static: return "static"; break;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001101 }
1102
1103 assert(0 && "Invalid storage class");
1104 return 0;
1105}
1106
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001107VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1108 SourceLocation StartL, SourceLocation IdL,
John McCalla93c9342009-12-07 02:54:59 +00001109 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001110 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001111 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001112}
1113
Douglas Gregor381d34e2010-12-06 18:36:25 +00001114void VarDecl::setStorageClass(StorageClass SC) {
1115 assert(isLegalForVariable(SC));
1116 if (getStorageClass() != SC)
1117 ClearLinkageCache();
1118
John McCallf1e4fbf2011-05-01 02:13:58 +00001119 VarDeclBits.SClass = SC;
Douglas Gregor381d34e2010-12-06 18:36:25 +00001120}
1121
Douglas Gregor1693e152010-07-06 18:42:40 +00001122SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001123 if (getInit())
Douglas Gregor1693e152010-07-06 18:42:40 +00001124 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00001125 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001126}
1127
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001128bool VarDecl::isExternC() const {
1129 ASTContext &Context = getASTContext();
1130 if (!Context.getLangOptions().CPlusPlus)
1131 return (getDeclContext()->isTranslationUnit() &&
John McCalld931b082010-08-26 03:08:43 +00001132 getStorageClass() != SC_Static) ||
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001133 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1134
Chandler Carruth10aad442011-02-25 00:05:02 +00001135 const DeclContext *DC = getDeclContext();
1136 if (DC->isFunctionOrMethod())
1137 return false;
1138
1139 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001140 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1141 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001142 return getStorageClass() != SC_Static;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001143
1144 break;
1145 }
1146
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001147 }
1148
1149 return false;
1150}
1151
1152VarDecl *VarDecl::getCanonicalDecl() {
1153 return getFirstDeclaration();
1154}
1155
Sebastian Redle9d12b62010-01-31 22:27:38 +00001156VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1157 // C++ [basic.def]p2:
1158 // A declaration is a definition unless [...] it contains the 'extern'
1159 // specifier or a linkage-specification and neither an initializer [...],
1160 // it declares a static data member in a class declaration [...].
1161 // C++ [temp.expl.spec]p15:
1162 // An explicit specialization of a static data member of a template is a
1163 // definition if the declaration includes an initializer; otherwise, it is
1164 // a declaration.
1165 if (isStaticDataMember()) {
1166 if (isOutOfLine() && (hasInit() ||
1167 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1168 return Definition;
1169 else
1170 return DeclarationOnly;
1171 }
1172 // C99 6.7p5:
1173 // A definition of an identifier is a declaration for that identifier that
1174 // [...] causes storage to be reserved for that object.
1175 // Note: that applies for all non-file-scope objects.
1176 // C99 6.9.2p1:
1177 // If the declaration of an identifier for an object has file scope and an
1178 // initializer, the declaration is an external definition for the identifier
1179 if (hasInit())
1180 return Definition;
1181 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1182 if (hasExternalStorage())
1183 return DeclarationOnly;
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001184
John McCalld931b082010-08-26 03:08:43 +00001185 if (getStorageClassAsWritten() == SC_Extern ||
1186 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahanian2bf6d7b2010-06-21 16:08:37 +00001187 for (const VarDecl *PrevVar = getPreviousDeclaration();
1188 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1189 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1190 return DeclarationOnly;
1191 }
1192 }
Sebastian Redle9d12b62010-01-31 22:27:38 +00001193 // C99 6.9.2p2:
1194 // A declaration of an object that has file scope without an initializer,
1195 // and without a storage class specifier or the scs 'static', constitutes
1196 // a tentative definition.
1197 // No such thing in C++.
1198 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1199 return TentativeDefinition;
1200
1201 // What's left is (in C, block-scope) declarations without initializers or
1202 // external storage. These are definitions.
1203 return Definition;
1204}
1205
Sebastian Redle9d12b62010-01-31 22:27:38 +00001206VarDecl *VarDecl::getActingDefinition() {
1207 DefinitionKind Kind = isThisDeclarationADefinition();
1208 if (Kind != TentativeDefinition)
1209 return 0;
1210
Chris Lattnerf0ed9ef2010-06-14 18:31:46 +00001211 VarDecl *LastTentative = 0;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001212 VarDecl *First = getFirstDeclaration();
1213 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1214 I != E; ++I) {
1215 Kind = (*I)->isThisDeclarationADefinition();
1216 if (Kind == Definition)
1217 return 0;
1218 else if (Kind == TentativeDefinition)
1219 LastTentative = *I;
1220 }
1221 return LastTentative;
1222}
1223
1224bool VarDecl::isTentativeDefinitionNow() const {
1225 DefinitionKind Kind = isThisDeclarationADefinition();
1226 if (Kind != TentativeDefinition)
1227 return false;
1228
1229 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1230 if ((*I)->isThisDeclarationADefinition() == Definition)
1231 return false;
1232 }
Sebastian Redl31310a22010-02-01 20:16:42 +00001233 return true;
Sebastian Redle9d12b62010-01-31 22:27:38 +00001234}
1235
Sebastian Redl31310a22010-02-01 20:16:42 +00001236VarDecl *VarDecl::getDefinition() {
Sebastian Redle2c52d22010-02-02 17:55:12 +00001237 VarDecl *First = getFirstDeclaration();
1238 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1239 I != E; ++I) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001240 if ((*I)->isThisDeclarationADefinition() == Definition)
1241 return *I;
1242 }
1243 return 0;
1244}
1245
John McCall110e8e52010-10-29 22:22:43 +00001246VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1247 DefinitionKind Kind = DeclarationOnly;
1248
1249 const VarDecl *First = getFirstDeclaration();
1250 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1251 I != E; ++I)
1252 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1253
1254 return Kind;
1255}
1256
Sebastian Redl31310a22010-02-01 20:16:42 +00001257const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001258 redecl_iterator I = redecls_begin(), E = redecls_end();
1259 while (I != E && !I->getInit())
1260 ++I;
1261
1262 if (I != E) {
Sebastian Redl31310a22010-02-01 20:16:42 +00001263 D = *I;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001264 return I->getInit();
1265 }
1266 return 0;
1267}
1268
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001269bool VarDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001270 if (Decl::isOutOfLine())
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001271 return true;
Chandler Carruth8761d682010-02-21 07:08:09 +00001272
1273 if (!isStaticDataMember())
1274 return false;
1275
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001276 // If this static data member was instantiated from a static data member of
1277 // a class template, check whether that static data member was defined
1278 // out-of-line.
1279 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1280 return VD->isOutOfLine();
1281
1282 return false;
1283}
1284
Douglas Gregor0d035142009-10-27 18:42:08 +00001285VarDecl *VarDecl::getOutOfLineDefinition() {
1286 if (!isStaticDataMember())
1287 return 0;
1288
1289 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1290 RD != RDEnd; ++RD) {
1291 if (RD->getLexicalDeclContext()->isFileContext())
1292 return *RD;
1293 }
1294
1295 return 0;
1296}
1297
Douglas Gregor838db382010-02-11 01:19:42 +00001298void VarDecl::setInit(Expr *I) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001299 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1300 Eval->~EvaluatedStmt();
Douglas Gregor838db382010-02-11 01:19:42 +00001301 getASTContext().Deallocate(Eval);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001302 }
1303
1304 Init = I;
1305}
1306
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001307VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001308 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001309 return cast<VarDecl>(MSI->getInstantiatedFrom());
1310
1311 return 0;
1312}
1313
Douglas Gregor663b5a02009-10-14 20:14:33 +00001314TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redle9d12b62010-01-31 22:27:38 +00001315 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001316 return MSI->getTemplateSpecializationKind();
1317
1318 return TSK_Undeclared;
1319}
1320
Douglas Gregor1028c9f2009-10-14 21:29:40 +00001321MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001322 return getASTContext().getInstantiatedFromStaticDataMember(this);
1323}
1324
Douglas Gregor0a897e32009-10-15 17:21:20 +00001325void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1326 SourceLocation PointOfInstantiation) {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001327 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor251b4ff2009-10-08 07:24:58 +00001328 assert(MSI && "Not an instantiated static data member?");
1329 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor0a897e32009-10-15 17:21:20 +00001330 if (TSK != TSK_ExplicitSpecialization &&
1331 PointOfInstantiation.isValid() &&
1332 MSI->getPointOfInstantiation().isInvalid())
1333 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregor7caa6822009-07-24 20:34:43 +00001334}
1335
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001336//===----------------------------------------------------------------------===//
1337// ParmVarDecl Implementation
1338//===----------------------------------------------------------------------===//
Douglas Gregor275a3692009-03-10 23:43:53 +00001339
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001340ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001341 SourceLocation StartLoc,
1342 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001343 QualType T, TypeSourceInfo *TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001344 StorageClass S, StorageClass SCAsWritten,
1345 Expr *DefArg) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00001346 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001347 S, SCAsWritten, DefArg);
Douglas Gregor275a3692009-03-10 23:43:53 +00001348}
1349
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001350Expr *ParmVarDecl::getDefaultArg() {
1351 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1352 assert(!hasUninstantiatedDefaultArg() &&
1353 "Default argument is not yet instantiated!");
1354
1355 Expr *Arg = getInit();
John McCall4765fa02010-12-06 08:20:24 +00001356 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001357 return E->getSubExpr();
Douglas Gregor275a3692009-03-10 23:43:53 +00001358
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001359 return Arg;
1360}
1361
1362unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall4765fa02010-12-06 08:20:24 +00001363 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001364 return E->getNumTemporaries();
1365
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001366 return 0;
Douglas Gregor275a3692009-03-10 23:43:53 +00001367}
1368
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001369CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1370 assert(getNumDefaultArgTemporaries() &&
1371 "Default arguments does not have any temporaries!");
1372
John McCall4765fa02010-12-06 08:20:24 +00001373 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001374 return E->getTemporary(i);
1375}
1376
1377SourceRange ParmVarDecl::getDefaultArgRange() const {
1378 if (const Expr *E = getInit())
1379 return E->getSourceRange();
1380
1381 if (hasUninstantiatedDefaultArg())
1382 return getUninstantiatedDefaultArg()->getSourceRange();
1383
1384 return SourceRange();
Argyrios Kyrtzidisfc7e2a82009-07-05 22:21:56 +00001385}
1386
Douglas Gregor1fe85ea2011-01-05 21:11:38 +00001387bool ParmVarDecl::isParameterPack() const {
1388 return isa<PackExpansionType>(getType());
1389}
1390
Nuno Lopes99f06ba2008-12-17 23:39:55 +00001391//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00001392// FunctionDecl Implementation
1393//===----------------------------------------------------------------------===//
1394
Douglas Gregorda2142f2011-02-19 18:51:44 +00001395void FunctionDecl::getNameForDiagnostic(std::string &S,
1396 const PrintingPolicy &Policy,
1397 bool Qualified) const {
1398 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1399 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1400 if (TemplateArgs)
1401 S += TemplateSpecializationType::PrintTemplateArgumentList(
1402 TemplateArgs->data(),
1403 TemplateArgs->size(),
1404 Policy);
1405
1406}
1407
Ted Kremenek9498d382010-04-29 16:49:01 +00001408bool FunctionDecl::isVariadic() const {
1409 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1410 return FT->isVariadic();
1411 return false;
1412}
1413
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001414bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1415 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet8387e2a2011-04-22 22:18:13 +00001416 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001417 Definition = *I;
1418 return true;
1419 }
1420 }
1421
1422 return false;
1423}
1424
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +00001425Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidisc37929c2009-07-14 03:20:21 +00001426 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1427 if (I->Body) {
1428 Definition = *I;
1429 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet8387e2a2011-04-22 22:18:13 +00001430 } else if (I->IsLateTemplateParsed) {
1431 Definition = *I;
1432 return 0;
Douglas Gregorf0097952008-04-21 02:02:58 +00001433 }
1434 }
1435
1436 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001437}
1438
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001439void FunctionDecl::setBody(Stmt *B) {
1440 Body = B;
Douglas Gregorb5f35ba2010-12-06 17:49:01 +00001441 if (B)
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001442 EndRangeLoc = B->getLocEnd();
1443}
1444
Douglas Gregor21386642010-09-28 21:55:22 +00001445void FunctionDecl::setPure(bool P) {
1446 IsPure = P;
1447 if (P)
1448 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1449 Parent->markedVirtualFunctionPure();
1450}
1451
Douglas Gregor48a83b52009-09-12 00:17:51 +00001452bool FunctionDecl::isMain() const {
1453 ASTContext &Context = getASTContext();
John McCall07a5c222009-08-15 02:09:25 +00001454 return !Context.getLangOptions().Freestanding &&
Sebastian Redl7a126a42010-08-31 00:36:30 +00001455 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregor04495c82009-02-24 01:23:02 +00001456 getIdentifier() && getIdentifier()->isStr("main");
1457}
1458
Douglas Gregor48a83b52009-09-12 00:17:51 +00001459bool FunctionDecl::isExternC() const {
1460 ASTContext &Context = getASTContext();
Douglas Gregor63935192009-03-02 00:19:53 +00001461 // In C, any non-static, non-overloadable function has external
1462 // linkage.
1463 if (!Context.getLangOptions().CPlusPlus)
John McCalld931b082010-08-26 03:08:43 +00001464 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001465
Chandler Carruth10aad442011-02-25 00:05:02 +00001466 const DeclContext *DC = getDeclContext();
1467 if (DC->isRecord())
1468 return false;
1469
1470 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor63935192009-03-02 00:19:53 +00001471 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1472 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCalld931b082010-08-26 03:08:43 +00001473 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001474 !getAttr<OverloadableAttr>();
Douglas Gregor63935192009-03-02 00:19:53 +00001475
1476 break;
1477 }
1478 }
1479
Douglas Gregor0bab54c2010-10-21 16:57:46 +00001480 return isMain();
Douglas Gregor63935192009-03-02 00:19:53 +00001481}
1482
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001483bool FunctionDecl::isGlobal() const {
1484 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1485 return Method->isStatic();
1486
John McCalld931b082010-08-26 03:08:43 +00001487 if (getStorageClass() == SC_Static)
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001488 return false;
1489
Mike Stump1eb44332009-09-09 15:08:12 +00001490 for (const DeclContext *DC = getDeclContext();
Douglas Gregor8499f3f2009-03-31 16:35:03 +00001491 DC->isNamespace();
1492 DC = DC->getParent()) {
1493 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1494 if (!Namespace->getDeclName())
1495 return false;
1496 break;
1497 }
1498 }
1499
1500 return true;
1501}
1502
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001503void
1504FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1505 redeclarable_base::setPreviousDeclaration(PrevDecl);
1506
1507 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1508 FunctionTemplateDecl *PrevFunTmpl
1509 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1510 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1511 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1512 }
Douglas Gregor8f150942010-12-09 16:59:22 +00001513
1514 if (PrevDecl->IsInline)
1515 IsInline = true;
Sebastian Redl7783bfc2010-01-26 22:01:41 +00001516}
1517
1518const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1519 return getFirstDeclaration();
1520}
1521
1522FunctionDecl *FunctionDecl::getCanonicalDecl() {
1523 return getFirstDeclaration();
1524}
1525
Douglas Gregor381d34e2010-12-06 18:36:25 +00001526void FunctionDecl::setStorageClass(StorageClass SC) {
1527 assert(isLegalForFunction(SC));
1528 if (getStorageClass() != SC)
1529 ClearLinkageCache();
1530
1531 SClass = SC;
1532}
1533
Douglas Gregor3e41d602009-02-13 23:20:09 +00001534/// \brief Returns a value indicating whether this function
1535/// corresponds to a builtin function.
1536///
1537/// The function corresponds to a built-in function if it is
1538/// declared at translation scope or within an extern "C" block and
1539/// its name matches with the name of a builtin. The returned value
1540/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump1eb44332009-09-09 15:08:12 +00001541/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregor3e41d602009-02-13 23:20:09 +00001542/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001543unsigned FunctionDecl::getBuiltinID() const {
1544 ASTContext &Context = getASTContext();
Douglas Gregor3c385e52009-02-14 18:57:46 +00001545 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1546 return 0;
1547
1548 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1549 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1550 return BuiltinID;
1551
1552 // This function has the name of a known C library
1553 // function. Determine whether it actually refers to the C library
1554 // function or whether it just has the same name.
1555
Douglas Gregor9add3172009-02-17 03:23:10 +00001556 // If this is a static function, it's not a builtin.
John McCalld931b082010-08-26 03:08:43 +00001557 if (getStorageClass() == SC_Static)
Douglas Gregor9add3172009-02-17 03:23:10 +00001558 return 0;
1559
Douglas Gregor3c385e52009-02-14 18:57:46 +00001560 // If this function is at translation-unit scope and we're not in
1561 // C++, it refers to the C library function.
1562 if (!Context.getLangOptions().CPlusPlus &&
1563 getDeclContext()->isTranslationUnit())
1564 return BuiltinID;
1565
1566 // If the function is in an extern "C" linkage specification and is
1567 // not marked "overloadable", it's the real function.
1568 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001569 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregor3c385e52009-02-14 18:57:46 +00001570 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001571 !getAttr<OverloadableAttr>())
Douglas Gregor3c385e52009-02-14 18:57:46 +00001572 return BuiltinID;
1573
1574 // Not a builtin
Douglas Gregor3e41d602009-02-13 23:20:09 +00001575 return 0;
1576}
1577
1578
Chris Lattner1ad9b282009-04-25 06:03:53 +00001579/// getNumParams - Return the number of parameters this function must have
Bob Wilson8dbfbf42011-01-10 18:23:55 +00001580/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner1ad9b282009-04-25 06:03:53 +00001581/// after it has been created.
1582unsigned FunctionDecl::getNumParams() const {
John McCall183700f2009-09-21 23:43:11 +00001583 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregor72564e72009-02-26 23:50:07 +00001584 if (isa<FunctionNoProtoType>(FT))
Chris Lattnerd3b90652008-03-15 05:43:15 +00001585 return 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001586 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump1eb44332009-09-09 15:08:12 +00001587
Reid Spencer5f016e22007-07-11 17:01:13 +00001588}
1589
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001590void FunctionDecl::setParams(ASTContext &C,
1591 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner2dbd2852009-04-25 06:12:16 +00001593 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump1eb44332009-09-09 15:08:12 +00001594
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 // Zero params -> null pointer.
1596 if (NumParams) {
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001597 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenekfc767612009-01-14 00:42:25 +00001598 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001600
Argyrios Kyrtzidis96888cc2009-06-23 00:42:00 +00001601 // Update source range. The check below allows us to set EndRangeLoc before
1602 // setting the parameters.
Argyrios Kyrtzidiscb5f8f52009-06-23 00:42:15 +00001603 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidis55d608c2009-06-20 08:09:14 +00001604 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 }
1606}
1607
Chris Lattner8123a952008-04-10 02:22:51 +00001608/// getMinRequiredArguments - Returns the minimum number of arguments
1609/// needed to call this function. This may be fewer than the number of
1610/// function parameters, if some of the parameters have default
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001611/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner8123a952008-04-10 02:22:51 +00001612unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001613 if (!getASTContext().getLangOptions().CPlusPlus)
1614 return getNumParams();
1615
Douglas Gregorf5c65ff2011-01-06 22:09:01 +00001616 unsigned NumRequiredArgs = getNumParams();
1617
1618 // If the last parameter is a parameter pack, we don't need an argument for
1619 // it.
1620 if (NumRequiredArgs > 0 &&
1621 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1622 --NumRequiredArgs;
1623
1624 // If this parameter has a default argument, we don't need an argument for
1625 // it.
1626 while (NumRequiredArgs > 0 &&
1627 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner8123a952008-04-10 02:22:51 +00001628 --NumRequiredArgs;
1629
Douglas Gregor7d5c0c12011-01-11 01:52:23 +00001630 // We might have parameter packs before the end. These can't be deduced,
1631 // but they can still handle multiple arguments.
1632 unsigned ArgIdx = NumRequiredArgs;
1633 while (ArgIdx > 0) {
1634 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1635 NumRequiredArgs = ArgIdx;
1636
1637 --ArgIdx;
1638 }
1639
Chris Lattner8123a952008-04-10 02:22:51 +00001640 return NumRequiredArgs;
1641}
1642
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001643bool FunctionDecl::isInlined() const {
Douglas Gregor8f150942010-12-09 16:59:22 +00001644 if (IsInline)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001645 return true;
Anders Carlsson48eda2c2009-12-04 22:35:50 +00001646
1647 if (isa<CXXMethodDecl>(this)) {
1648 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1649 return true;
1650 }
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001651
1652 switch (getTemplateSpecializationKind()) {
1653 case TSK_Undeclared:
1654 case TSK_ExplicitSpecialization:
1655 return false;
1656
1657 case TSK_ImplicitInstantiation:
1658 case TSK_ExplicitInstantiationDeclaration:
1659 case TSK_ExplicitInstantiationDefinition:
1660 // Handle below.
1661 break;
1662 }
1663
1664 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001665 bool HasPattern = false;
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001666 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001667 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001668
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001669 if (HasPattern && PatternDecl)
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001670 return PatternDecl->isInlined();
1671
1672 return false;
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001673}
1674
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001675/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001676/// definition will be externally visible.
1677///
1678/// Inline function definitions are always available for inlining optimizations.
1679/// However, depending on the language dialect, declaration specifiers, and
1680/// attributes, the definition of an inline function may or may not be
1681/// "externally" visible to other translation units in the program.
1682///
1683/// In C99, inline definitions are not externally visible by default. However,
Mike Stump1e5fd7f2010-01-06 02:05:39 +00001684/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001685/// inline definition becomes externally visible (C99 6.7.4p6).
1686///
1687/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1688/// definition, we use the GNU semantics for inline, which are nearly the
1689/// opposite of C99 semantics. In particular, "inline" by itself will create
1690/// an externally visible symbol, but "extern inline" will not create an
1691/// externally visible symbol.
1692bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1693 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001694 assert(isInlined() && "Function must be inline");
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001695 ASTContext &Context = getASTContext();
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001696
Douglas Gregor7d9c3c92009-10-27 23:26:40 +00001697 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001698 // If it's not the case that both 'inline' and 'extern' are
1699 // specified on the definition, then this inline definition is
1700 // externally visible.
1701 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1702 return true;
1703
1704 // If any declaration is 'inline' but not 'extern', then this definition
1705 // is externally visible.
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001706 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1707 Redecl != RedeclEnd;
1708 ++Redecl) {
Douglas Gregor8f150942010-12-09 16:59:22 +00001709 if (Redecl->isInlineSpecified() &&
1710 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001711 return true;
Douglas Gregor8f150942010-12-09 16:59:22 +00001712 }
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001713
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001714 return false;
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001715 }
1716
1717 // C99 6.7.4p6:
1718 // [...] If all of the file scope declarations for a function in a
1719 // translation unit include the inline function specifier without extern,
1720 // then the definition in that translation unit is an inline definition.
1721 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1722 Redecl != RedeclEnd;
1723 ++Redecl) {
1724 // Only consider file-scope declarations in this test.
1725 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1726 continue;
1727
John McCalld931b082010-08-26 03:08:43 +00001728 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor1fc09a92009-09-13 07:46:26 +00001729 return true; // Not an inline definition
1730 }
1731
1732 // C99 6.7.4p6:
1733 // An inline definition does not provide an external definition for the
1734 // function, and does not forbid an external definition in another
1735 // translation unit.
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001736 return false;
1737}
1738
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001739/// getOverloadedOperator - Which C++ overloaded operator this
1740/// function represents, if any.
1741OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregore94ca9e42008-11-18 14:39:36 +00001742 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1743 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00001744 else
1745 return OO_None;
1746}
1747
Sean Hunta6c058d2010-01-13 09:01:02 +00001748/// getLiteralIdentifier - The literal suffix identifier this function
1749/// represents, if any.
1750const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1751 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1752 return getDeclName().getCXXLiteralIdentifier();
1753 else
1754 return 0;
1755}
1756
Argyrios Kyrtzidisd0913552010-06-22 09:54:51 +00001757FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1758 if (TemplateOrSpecialization.isNull())
1759 return TK_NonTemplate;
1760 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1761 return TK_FunctionTemplate;
1762 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1763 return TK_MemberSpecialization;
1764 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1765 return TK_FunctionTemplateSpecialization;
1766 if (TemplateOrSpecialization.is
1767 <DependentFunctionTemplateSpecializationInfo*>())
1768 return TK_DependentFunctionTemplateSpecialization;
1769
1770 assert(false && "Did we miss a TemplateOrSpecialization type?");
1771 return TK_NonTemplate;
1772}
1773
Douglas Gregor2db32322009-10-07 23:56:10 +00001774FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001775 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregor2db32322009-10-07 23:56:10 +00001776 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1777
1778 return 0;
1779}
1780
Douglas Gregorb3ae4fc2009-10-12 20:18:28 +00001781MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1782 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1783}
1784
Douglas Gregor2db32322009-10-07 23:56:10 +00001785void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001786FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1787 FunctionDecl *FD,
Douglas Gregor2db32322009-10-07 23:56:10 +00001788 TemplateSpecializationKind TSK) {
1789 assert(TemplateOrSpecialization.isNull() &&
1790 "Member function is already a specialization");
1791 MemberSpecializationInfo *Info
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001792 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregor2db32322009-10-07 23:56:10 +00001793 TemplateOrSpecialization = Info;
1794}
1795
Douglas Gregor3b846b62009-10-27 20:53:28 +00001796bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor6cfacfe2010-05-17 17:34:56 +00001797 // If the function is invalid, it can't be implicitly instantiated.
1798 if (isInvalidDecl())
Douglas Gregor3b846b62009-10-27 20:53:28 +00001799 return false;
1800
1801 switch (getTemplateSpecializationKind()) {
1802 case TSK_Undeclared:
1803 case TSK_ExplicitSpecialization:
1804 case TSK_ExplicitInstantiationDefinition:
1805 return false;
1806
1807 case TSK_ImplicitInstantiation:
1808 return true;
1809
1810 case TSK_ExplicitInstantiationDeclaration:
1811 // Handled below.
1812 break;
1813 }
1814
1815 // Find the actual template from which we will instantiate.
1816 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001817 bool HasPattern = false;
Douglas Gregor3b846b62009-10-27 20:53:28 +00001818 if (PatternDecl)
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001819 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregor3b846b62009-10-27 20:53:28 +00001820
1821 // C++0x [temp.explicit]p9:
1822 // Except for inline functions, other explicit instantiation declarations
1823 // have the effect of suppressing the implicit instantiation of the entity
1824 // to which they refer.
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00001825 if (!HasPattern || !PatternDecl)
Douglas Gregor3b846b62009-10-27 20:53:28 +00001826 return true;
1827
Douglas Gregor7ced9c82009-10-27 21:11:48 +00001828 return PatternDecl->isInlined();
Douglas Gregor3b846b62009-10-27 20:53:28 +00001829}
1830
1831FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1832 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1833 while (Primary->getInstantiatedFromMemberTemplate()) {
1834 // If we have hit a point where the user provided a specialization of
1835 // this template, we're done looking.
1836 if (Primary->isMemberSpecialization())
1837 break;
1838
1839 Primary = Primary->getInstantiatedFromMemberTemplate();
1840 }
1841
1842 return Primary->getTemplatedDecl();
1843 }
1844
1845 return getInstantiatedFromMemberFunction();
1846}
1847
Douglas Gregor16e8be22009-06-29 17:30:29 +00001848FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001849 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001850 = TemplateOrSpecialization
1851 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001852 return Info->Template.getPointer();
Douglas Gregor16e8be22009-06-29 17:30:29 +00001853 }
1854 return 0;
1855}
1856
1857const TemplateArgumentList *
1858FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001859 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorfd056bc2009-10-13 16:30:37 +00001860 = TemplateOrSpecialization
1861 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor16e8be22009-06-29 17:30:29 +00001862 return Info->TemplateArguments;
1863 }
1864 return 0;
1865}
1866
Abramo Bagnarae03db982010-05-20 15:32:11 +00001867const TemplateArgumentListInfo *
1868FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1869 if (FunctionTemplateSpecializationInfo *Info
1870 = TemplateOrSpecialization
1871 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1872 return Info->TemplateArgumentsAsWritten;
1873 }
1874 return 0;
1875}
1876
Mike Stump1eb44332009-09-09 15:08:12 +00001877void
Argyrios Kyrtzidis6b541512010-09-08 19:31:22 +00001878FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1879 FunctionTemplateDecl *Template,
Douglas Gregor127102b2009-06-29 20:59:39 +00001880 const TemplateArgumentList *TemplateArgs,
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001881 void *InsertPos,
Abramo Bagnarae03db982010-05-20 15:32:11 +00001882 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +00001883 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1884 SourceLocation PointOfInstantiation) {
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001885 assert(TSK != TSK_Undeclared &&
1886 "Must specify the type of function template specialization");
Mike Stump1eb44332009-09-09 15:08:12 +00001887 FunctionTemplateSpecializationInfo *Info
Douglas Gregor16e8be22009-06-29 17:30:29 +00001888 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor1637be72009-06-26 00:10:03 +00001889 if (!Info)
Argyrios Kyrtzidisa626a3d2010-09-09 11:28:23 +00001890 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1891 TemplateArgs,
1892 TemplateArgsAsWritten,
1893 PointOfInstantiation);
Douglas Gregor1637be72009-06-26 00:10:03 +00001894 TemplateOrSpecialization = Info;
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Douglas Gregor127102b2009-06-29 20:59:39 +00001896 // Insert this function template specialization into the set of known
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001897 // function template specializations.
1898 if (InsertPos)
Sebastian Redl5bbcdbf2011-04-14 14:07:59 +00001899 Template->addSpecialization(Info, InsertPos);
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001900 else {
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001901 // Try to insert the new node. If there is an existing node, leave it, the
1902 // set will contain the canonical decls while
1903 // FunctionTemplateDecl::findSpecialization will return
1904 // the most recent redeclarations.
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001905 FunctionTemplateSpecializationInfo *Existing
1906 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +00001907 (void)Existing;
1908 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1909 "Set is supposed to only contain canonical decls");
Douglas Gregorb9aa6b22009-09-24 23:14:47 +00001910 }
Douglas Gregor1637be72009-06-26 00:10:03 +00001911}
1912
John McCallaf2094e2010-04-08 09:05:18 +00001913void
1914FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1915 const UnresolvedSetImpl &Templates,
1916 const TemplateArgumentListInfo &TemplateArgs) {
1917 assert(TemplateOrSpecialization.isNull());
1918 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1919 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall21c01602010-04-13 22:18:28 +00001920 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallaf2094e2010-04-08 09:05:18 +00001921 void *Buffer = Context.Allocate(Size);
1922 DependentFunctionTemplateSpecializationInfo *Info =
1923 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1924 TemplateArgs);
1925 TemplateOrSpecialization = Info;
1926}
1927
1928DependentFunctionTemplateSpecializationInfo::
1929DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1930 const TemplateArgumentListInfo &TArgs)
1931 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1932
1933 d.NumTemplates = Ts.size();
1934 d.NumArgs = TArgs.size();
1935
1936 FunctionTemplateDecl **TsArray =
1937 const_cast<FunctionTemplateDecl**>(getTemplates());
1938 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1939 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1940
1941 TemplateArgumentLoc *ArgsArray =
1942 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1943 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1944 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1945}
1946
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001947TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump1eb44332009-09-09 15:08:12 +00001948 // For a function template specialization, query the specialization
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001949 // information object.
Douglas Gregor2db32322009-10-07 23:56:10 +00001950 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001951 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor2db32322009-10-07 23:56:10 +00001952 if (FTSInfo)
1953 return FTSInfo->getTemplateSpecializationKind();
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Douglas Gregor2db32322009-10-07 23:56:10 +00001955 MemberSpecializationInfo *MSInfo
1956 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1957 if (MSInfo)
1958 return MSInfo->getTemplateSpecializationKind();
1959
1960 return TSK_Undeclared;
Douglas Gregord0e3daf2009-09-04 22:48:11 +00001961}
1962
Mike Stump1eb44332009-09-09 15:08:12 +00001963void
Douglas Gregor0a897e32009-10-15 17:21:20 +00001964FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1965 SourceLocation PointOfInstantiation) {
1966 if (FunctionTemplateSpecializationInfo *FTSInfo
1967 = TemplateOrSpecialization.dyn_cast<
1968 FunctionTemplateSpecializationInfo*>()) {
1969 FTSInfo->setTemplateSpecializationKind(TSK);
1970 if (TSK != TSK_ExplicitSpecialization &&
1971 PointOfInstantiation.isValid() &&
1972 FTSInfo->getPointOfInstantiation().isInvalid())
1973 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1974 } else if (MemberSpecializationInfo *MSInfo
1975 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1976 MSInfo->setTemplateSpecializationKind(TSK);
1977 if (TSK != TSK_ExplicitSpecialization &&
1978 PointOfInstantiation.isValid() &&
1979 MSInfo->getPointOfInstantiation().isInvalid())
1980 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1981 } else
1982 assert(false && "Function cannot have a template specialization kind");
1983}
1984
1985SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregor2db32322009-10-07 23:56:10 +00001986 if (FunctionTemplateSpecializationInfo *FTSInfo
1987 = TemplateOrSpecialization.dyn_cast<
1988 FunctionTemplateSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001989 return FTSInfo->getPointOfInstantiation();
Douglas Gregor2db32322009-10-07 23:56:10 +00001990 else if (MemberSpecializationInfo *MSInfo
1991 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor0a897e32009-10-15 17:21:20 +00001992 return MSInfo->getPointOfInstantiation();
1993
1994 return SourceLocation();
Douglas Gregor1fd2dd12009-06-29 22:39:32 +00001995}
1996
Douglas Gregor9f185072009-09-11 20:15:17 +00001997bool FunctionDecl::isOutOfLine() const {
Douglas Gregorda2142f2011-02-19 18:51:44 +00001998 if (Decl::isOutOfLine())
Douglas Gregor9f185072009-09-11 20:15:17 +00001999 return true;
2000
2001 // If this function was instantiated from a member function of a
2002 // class template, check whether that member function was defined out-of-line.
2003 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2004 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002005 if (FD->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002006 return Definition->isOutOfLine();
2007 }
2008
2009 // If this function was instantiated from a function template,
2010 // check whether that function template was defined out-of-line.
2011 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2012 const FunctionDecl *Definition;
Argyrios Kyrtzidis06a54a32010-07-07 11:31:19 +00002013 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor9f185072009-09-11 20:15:17 +00002014 return Definition->isOutOfLine();
2015 }
2016
2017 return false;
2018}
2019
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002020SourceRange FunctionDecl::getSourceRange() const {
2021 return SourceRange(getOuterLocStart(), EndRangeLoc);
2022}
2023
Chris Lattner8a934232008-03-31 00:36:02 +00002024//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002025// FieldDecl Implementation
2026//===----------------------------------------------------------------------===//
2027
Jay Foad4ba2a172011-01-12 09:06:06 +00002028FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002029 SourceLocation StartLoc, SourceLocation IdLoc,
2030 IdentifierInfo *Id, QualType T,
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002031 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002032 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
2033 BW, Mutable);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002034}
2035
2036bool FieldDecl::isAnonymousStructOrUnion() const {
2037 if (!isImplicit() || getDeclName())
2038 return false;
2039
2040 if (const RecordType *Record = getType()->getAs<RecordType>())
2041 return Record->getDecl()->isAnonymousStructOrUnion();
2042
2043 return false;
2044}
2045
John McCallba4f5d52011-01-20 07:57:12 +00002046unsigned FieldDecl::getFieldIndex() const {
2047 if (CachedFieldIndex) return CachedFieldIndex - 1;
2048
2049 unsigned index = 0;
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002050 const RecordDecl *RD = getParent();
2051 const FieldDecl *LastFD = 0;
2052 bool IsMsStruct = RD->hasAttr<MsStructAttr>();
2053
2054 RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
John McCallba4f5d52011-01-20 07:57:12 +00002055 while (true) {
2056 assert(i != e && "failed to find field in parent!");
2057 if (*i == this)
2058 break;
2059
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002060 if (IsMsStruct) {
2061 // Zero-length bitfields following non-bitfield members are ignored.
Fariborz Jahanian340fa242011-05-02 17:20:56 +00002062 if (getASTContext().ZeroBitfieldFollowsNonBitfield((*i), LastFD) ||
2063 getASTContext().ZeroBitfieldFollowsBitfield((*i), LastFD)) {
Fariborz Jahanian07a8a212011-04-28 22:49:46 +00002064 ++i;
2065 continue;
2066 }
2067 LastFD = (*i);
2068 }
John McCallba4f5d52011-01-20 07:57:12 +00002069 ++i;
2070 ++index;
2071 }
2072
2073 CachedFieldIndex = index + 1;
2074 return index;
2075}
2076
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002077SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002078 if (isBitField())
2079 return SourceRange(getInnerLocStart(), BitWidth->getLocEnd());
2080 return DeclaratorDecl::getSourceRange();
Abramo Bagnaraf2cf5622011-03-08 11:07:11 +00002081}
2082
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002083//===----------------------------------------------------------------------===//
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002084// TagDecl Implementation
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002085//===----------------------------------------------------------------------===//
2086
Douglas Gregor1693e152010-07-06 18:42:40 +00002087SourceLocation TagDecl::getOuterLocStart() const {
2088 return getTemplateOrInnerLocStart(this);
2089}
2090
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002091SourceRange TagDecl::getSourceRange() const {
2092 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregor1693e152010-07-06 18:42:40 +00002093 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidisf602c8b2009-07-14 03:17:17 +00002094}
2095
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002096TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002097 return getFirstDeclaration();
Argyrios Kyrtzidisb57a4fe2009-07-18 00:34:07 +00002098}
2099
Richard Smith162e1c12011-04-15 14:24:37 +00002100void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2101 TypedefNameDeclOrQualifier = TDD;
Douglas Gregor60e70642010-05-19 18:39:18 +00002102 if (TypeForDecl)
John McCallf4c73712011-01-19 06:33:43 +00002103 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregor381d34e2010-12-06 18:36:25 +00002104 ClearLinkageCache();
Douglas Gregor60e70642010-05-19 18:39:18 +00002105}
2106
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002107void TagDecl::startDefinition() {
Sebastian Redled48a8f2010-08-02 18:27:05 +00002108 IsBeingDefined = true;
John McCall86ff3082010-02-04 22:26:26 +00002109
2110 if (isa<CXXRecordDecl>(this)) {
2111 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2112 struct CXXRecordDecl::DefinitionData *Data =
2113 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall22432882010-03-26 21:56:38 +00002114 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2115 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall86ff3082010-02-04 22:26:26 +00002116 }
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002117}
2118
2119void TagDecl::completeDefinition() {
John McCall5cfa0112010-02-05 01:33:36 +00002120 assert((!isa<CXXRecordDecl>(this) ||
2121 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2122 "definition completed but not started");
2123
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002124 IsDefinition = true;
Sebastian Redled48a8f2010-08-02 18:27:05 +00002125 IsBeingDefined = false;
Argyrios Kyrtzidis565bf302010-10-24 17:26:50 +00002126
2127 if (ASTMutationListener *L = getASTMutationListener())
2128 L->CompletedTagDefinition(this);
Douglas Gregor0b7a1582009-01-17 00:42:38 +00002129}
2130
Douglas Gregor952b0172010-02-11 01:04:33 +00002131TagDecl* TagDecl::getDefinition() const {
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002132 if (isDefinition())
2133 return const_cast<TagDecl *>(this);
Andrew Trick220a9c82010-10-19 21:54:32 +00002134 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2135 return CXXRD->getDefinition();
Mike Stump1eb44332009-09-09 15:08:12 +00002136
2137 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002138 R != REnd; ++R)
2139 if (R->isDefinition())
2140 return *R;
Mike Stump1eb44332009-09-09 15:08:12 +00002141
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +00002142 return 0;
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002143}
2144
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002145void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2146 if (QualifierLoc) {
John McCallb6217662010-03-15 10:12:16 +00002147 // Make sure the extended qualifier info is allocated.
2148 if (!hasExtInfo())
Richard Smith162e1c12011-04-15 14:24:37 +00002149 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCallb6217662010-03-15 10:12:16 +00002150 // Set qualifier info.
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002151 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002152 }
2153 else {
2154 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCallb6217662010-03-15 10:12:16 +00002155 if (hasExtInfo()) {
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002156 if (getExtInfo()->NumTemplParamLists == 0) {
2157 getASTContext().Deallocate(getExtInfo());
Richard Smith162e1c12011-04-15 14:24:37 +00002158 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002159 }
2160 else
2161 getExtInfo()->QualifierLoc = QualifierLoc;
John McCallb6217662010-03-15 10:12:16 +00002162 }
2163 }
2164}
2165
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002166void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2167 unsigned NumTPLists,
2168 TemplateParameterList **TPLists) {
2169 assert(NumTPLists > 0);
2170 // Make sure the extended decl info is allocated.
2171 if (!hasExtInfo())
2172 // Allocate external info struct.
Richard Smith162e1c12011-04-15 14:24:37 +00002173 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara7f0a9152011-03-18 15:16:37 +00002174 // Set the template parameter lists info.
2175 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2176}
2177
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002178//===----------------------------------------------------------------------===//
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002179// EnumDecl Implementation
2180//===----------------------------------------------------------------------===//
2181
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002182EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2183 SourceLocation StartLoc, SourceLocation IdLoc,
2184 IdentifierInfo *Id,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002185 EnumDecl *PrevDecl, bool IsScoped,
2186 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002187 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002188 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002189 C.getTypeDeclType(Enum, PrevDecl);
2190 return Enum;
2191}
2192
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002193EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002194 return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00002195 false, false, false);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002196}
2197
Douglas Gregor838db382010-02-11 01:19:42 +00002198void EnumDecl::completeDefinition(QualType NewType,
John McCall1b5a6182010-05-06 08:49:23 +00002199 QualType NewPromotionType,
2200 unsigned NumPositiveBits,
2201 unsigned NumNegativeBits) {
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002202 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002203 if (!IntegerType)
2204 IntegerType = NewType.getTypePtr();
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002205 PromotionType = NewPromotionType;
John McCall1b5a6182010-05-06 08:49:23 +00002206 setNumPositiveBits(NumPositiveBits);
2207 setNumNegativeBits(NumNegativeBits);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002208 TagDecl::completeDefinition();
2209}
2210
2211//===----------------------------------------------------------------------===//
Chris Lattner8a934232008-03-31 00:36:02 +00002212// RecordDecl Implementation
2213//===----------------------------------------------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002214
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002215RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2216 SourceLocation StartLoc, SourceLocation IdLoc,
2217 IdentifierInfo *Id, RecordDecl *PrevDecl)
2218 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek63597922008-09-02 21:12:32 +00002219 HasFlexibleArrayMember = false;
Douglas Gregorbcbffc42009-01-07 00:43:41 +00002220 AnonymousStructOrUnion = false;
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00002221 HasObjectMember = false;
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002222 LoadedFieldsFromExternalStorage = false;
Ted Kremenek63597922008-09-02 21:12:32 +00002223 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek63597922008-09-02 21:12:32 +00002224}
2225
Jay Foad4ba2a172011-01-12 09:06:06 +00002226RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002227 SourceLocation StartLoc, SourceLocation IdLoc,
2228 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2229 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2230 PrevDecl);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00002231 C.getTypeDeclType(R, PrevDecl);
2232 return R;
Ted Kremenek63597922008-09-02 21:12:32 +00002233}
2234
Jay Foad4ba2a172011-01-12 09:06:06 +00002235RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00002236 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2237 SourceLocation(), 0, 0);
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00002238}
2239
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002240bool RecordDecl::isInjectedClassName() const {
Mike Stump1eb44332009-09-09 15:08:12 +00002241 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregorc9b5b402009-03-25 15:59:44 +00002242 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2243}
2244
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002245RecordDecl::field_iterator RecordDecl::field_begin() const {
2246 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2247 LoadFieldsFromExternalStorage();
2248
2249 return field_iterator(decl_iterator(FirstDecl));
2250}
2251
Douglas Gregorda2142f2011-02-19 18:51:44 +00002252/// completeDefinition - Notes that the definition of this type is now
2253/// complete.
2254void RecordDecl::completeDefinition() {
2255 assert(!isDefinition() && "Cannot redefine record!");
2256 TagDecl::completeDefinition();
2257}
2258
Argyrios Kyrtzidiseb5e9982010-10-14 20:14:34 +00002259void RecordDecl::LoadFieldsFromExternalStorage() const {
2260 ExternalASTSource *Source = getASTContext().getExternalSource();
2261 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2262
2263 // Notify that we have a RecordDecl doing some initialization.
2264 ExternalASTSource::Deserializing TheFields(Source);
2265
2266 llvm::SmallVector<Decl*, 64> Decls;
2267 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2268 return;
2269
2270#ifndef NDEBUG
2271 // Check that all decls we got were FieldDecls.
2272 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2273 assert(isa<FieldDecl>(Decls[i]));
2274#endif
2275
2276 LoadedFieldsFromExternalStorage = true;
2277
2278 if (Decls.empty())
2279 return;
2280
2281 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2282}
2283
Steve Naroff56ee6892008-10-08 17:01:13 +00002284//===----------------------------------------------------------------------===//
2285// BlockDecl Implementation
2286//===----------------------------------------------------------------------===//
2287
Douglas Gregor838db382010-02-11 01:19:42 +00002288void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffe78b8092009-03-13 16:56:44 +00002289 unsigned NParms) {
2290 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump1eb44332009-09-09 15:08:12 +00002291
Steve Naroffe78b8092009-03-13 16:56:44 +00002292 // Zero params -> null pointer.
2293 if (NParms) {
2294 NumParams = NParms;
Douglas Gregor838db382010-02-11 01:19:42 +00002295 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffe78b8092009-03-13 16:56:44 +00002296 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2297 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2298 }
2299}
2300
John McCall6b5a61b2011-02-07 10:33:21 +00002301void BlockDecl::setCaptures(ASTContext &Context,
2302 const Capture *begin,
2303 const Capture *end,
2304 bool capturesCXXThis) {
John McCall469a1eb2011-02-02 13:00:07 +00002305 CapturesCXXThis = capturesCXXThis;
2306
2307 if (begin == end) {
John McCall6b5a61b2011-02-07 10:33:21 +00002308 NumCaptures = 0;
2309 Captures = 0;
John McCall469a1eb2011-02-02 13:00:07 +00002310 return;
2311 }
2312
John McCall6b5a61b2011-02-07 10:33:21 +00002313 NumCaptures = end - begin;
2314
2315 // Avoid new Capture[] because we don't want to provide a default
2316 // constructor.
2317 size_t allocationSize = NumCaptures * sizeof(Capture);
2318 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2319 memcpy(buffer, begin, allocationSize);
2320 Captures = static_cast<Capture*>(buffer);
Steve Naroffe78b8092009-03-13 16:56:44 +00002321}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002322
Douglas Gregor2fcbcef2010-12-21 16:27:07 +00002323SourceRange BlockDecl::getSourceRange() const {
2324 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2325}
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002326
2327//===----------------------------------------------------------------------===//
2328// Other Decl Allocation/Deallocation Method Implementations
2329//===----------------------------------------------------------------------===//
2330
2331TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2332 return new (C) TranslationUnitDecl(C);
2333}
2334
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002335LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara67843042011-03-05 18:21:20 +00002336 SourceLocation IdentL, IdentifierInfo *II) {
2337 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2338}
2339
2340LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2341 SourceLocation IdentL, IdentifierInfo *II,
2342 SourceLocation GnuLabelL) {
2343 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2344 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerad8dcf42011-02-17 07:39:24 +00002345}
2346
2347
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002348NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraacba90f2011-03-08 12:38:20 +00002349 SourceLocation StartLoc,
2350 SourceLocation IdLoc, IdentifierInfo *Id) {
2351 return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002352}
2353
Douglas Gregor06c91932010-10-27 19:49:05 +00002354NamespaceDecl *NamespaceDecl::getNextNamespace() {
2355 return dyn_cast_or_null<NamespaceDecl>(
2356 NextNamespace.get(getASTContext().getExternalSource()));
2357}
2358
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002359ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002360 SourceLocation IdLoc,
2361 IdentifierInfo *Id,
2362 QualType Type) {
2363 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002364}
2365
2366FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002367 SourceLocation StartLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +00002368 const DeclarationNameInfo &NameInfo,
2369 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002370 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregor8f150942010-12-09 16:59:22 +00002371 bool isInlineSpecified,
2372 bool hasWrittenPrototype) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002373 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2374 T, TInfo, SC, SCAsWritten,
2375 isInlineSpecified);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002376 New->HasWrittenPrototype = hasWrittenPrototype;
2377 return New;
2378}
2379
2380BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2381 return new (C) BlockDecl(DC, L);
2382}
2383
2384EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2385 SourceLocation L,
2386 IdentifierInfo *Id, QualType T,
2387 Expr *E, const llvm::APSInt &V) {
2388 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2389}
2390
Benjamin Kramerd9811462010-11-21 14:11:41 +00002391IndirectFieldDecl *
2392IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2393 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2394 unsigned CHS) {
Francois Pichet87c2e122010-11-21 06:08:52 +00002395 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2396}
2397
Douglas Gregor8e7139c2010-09-01 20:41:53 +00002398SourceRange EnumConstantDecl::getSourceRange() const {
2399 SourceLocation End = getLocation();
2400 if (Init)
2401 End = Init->getLocEnd();
2402 return SourceRange(getLocation(), End);
2403}
2404
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002405TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara344577e2011-03-06 15:48:19 +00002406 SourceLocation StartLoc, SourceLocation IdLoc,
2407 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2408 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002409}
2410
Richard Smith162e1c12011-04-15 14:24:37 +00002411TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2412 SourceLocation StartLoc,
2413 SourceLocation IdLoc, IdentifierInfo *Id,
2414 TypeSourceInfo *TInfo) {
2415 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2416}
2417
Abramo Bagnaraa2026c92011-03-08 16:41:52 +00002418SourceRange TypedefDecl::getSourceRange() const {
2419 SourceLocation RangeEnd = getLocation();
2420 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2421 if (typeIsPostfix(TInfo->getType()))
2422 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2423 }
2424 return SourceRange(getLocStart(), RangeEnd);
2425}
2426
Richard Smith162e1c12011-04-15 14:24:37 +00002427SourceRange TypeAliasDecl::getSourceRange() const {
2428 SourceLocation RangeEnd = getLocStart();
2429 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2430 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2431 return SourceRange(getLocStart(), RangeEnd);
2432}
2433
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002434FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara21e006e2011-03-03 14:20:18 +00002435 StringLiteral *Str,
2436 SourceLocation AsmLoc,
2437 SourceLocation RParenLoc) {
2438 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl7783bfc2010-01-26 22:01:41 +00002439}