blob: 0dce211c136a0013de4e848484ed110dedf229a5 [file] [log] [blame]
Chris Lattnera11999d2006-10-15 22:34:45 +00001//===--- Decl.cpp - Declaration AST Node Implementation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera11999d2006-10-15 22:34:45 +00007//
8//===----------------------------------------------------------------------===//
9//
Argyrios Kyrtzidis63018842008-06-04 13:04:04 +000010// This file implements the Decl subclasses.
Chris Lattnera11999d2006-10-15 22:34:45 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/Decl.h"
Douglas Gregor889ceb72009-02-03 19:21:40 +000015#include "clang/AST/DeclCXX.h"
Steve Naroffc4173fa2009-02-22 19:35:57 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregore362cea2009-05-10 22:57:19 +000017#include "clang/AST/DeclTemplate.h"
Chris Lattnera7b32872008-03-15 06:12:44 +000018#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis3f79ad72009-08-19 01:27:32 +000019#include "clang/AST/TypeLoc.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000020#include "clang/AST/Stmt.h"
Nuno Lopes394ec982008-12-17 23:39:55 +000021#include "clang/AST/Expr.h"
Anders Carlsson714d0962009-12-15 19:16:31 +000022#include "clang/AST/ExprCXX.h"
Douglas Gregor7de59662009-05-29 20:38:28 +000023#include "clang/AST/PrettyPrinter.h"
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +000024#include "clang/AST/ASTMutationListener.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000026#include "clang/Basic/IdentifierTable.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000027#include "clang/Basic/Specifiers.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000029
Chris Lattner6d9a6852006-10-25 05:11:20 +000030using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000031
Chris Lattner88f70d62008-03-15 05:43:15 +000032//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000033// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000034//===----------------------------------------------------------------------===//
35
John McCall659a3372010-12-18 03:30:47 +000036static const VisibilityAttr *GetExplicitVisibility(const Decl *d) {
37 // Use the most recent declaration of a variable.
38 if (const VarDecl *var = dyn_cast<VarDecl>(d))
39 return var->getMostRecentDeclaration()->getAttr<VisibilityAttr>();
40
41 // Use the most recent declaration of a function, and also handle
42 // function template specializations.
43 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(d)) {
44 if (const VisibilityAttr *attr
45 = fn->getMostRecentDeclaration()->getAttr<VisibilityAttr>())
46 return attr;
47
48 // If the function is a specialization of a template with an
49 // explicit visibility attribute, use that.
50 if (FunctionTemplateSpecializationInfo *templateInfo
51 = fn->getTemplateSpecializationInfo())
52 return templateInfo->getTemplate()->getTemplatedDecl()
53 ->getAttr<VisibilityAttr>();
54
55 return 0;
John McCallb7139c42010-10-28 04:18:25 +000056 }
John McCall659a3372010-12-18 03:30:47 +000057
58 // Otherwise, just check the declaration itself first.
59 if (const VisibilityAttr *attr = d->getAttr<VisibilityAttr>())
60 return attr;
61
62 // If there wasn't explicit visibility there, and this is a
63 // specialization of a class template, check for visibility
64 // on the pattern.
65 if (const ClassTemplateSpecializationDecl *spec
66 = dyn_cast<ClassTemplateSpecializationDecl>(d))
67 return spec->getSpecializedTemplate()->getTemplatedDecl()
68 ->getAttr<VisibilityAttr>();
69
70 return 0;
John McCallb7139c42010-10-28 04:18:25 +000071}
72
John McCall457a04e2010-10-22 21:05:15 +000073static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
74 switch (A->getVisibility()) {
75 case VisibilityAttr::Default:
76 return DefaultVisibility;
77 case VisibilityAttr::Hidden:
78 return HiddenVisibility;
79 case VisibilityAttr::Protected:
80 return ProtectedVisibility;
81 }
82 return DefaultVisibility;
83}
84
John McCallc273f242010-10-30 11:50:40 +000085typedef NamedDecl::LinkageInfo LinkageInfo;
John McCall457a04e2010-10-22 21:05:15 +000086typedef std::pair<Linkage,Visibility> LVPair;
John McCallc273f242010-10-30 11:50:40 +000087
John McCall457a04e2010-10-22 21:05:15 +000088static LVPair merge(LVPair L, LVPair R) {
89 return LVPair(minLinkage(L.first, R.first),
90 minVisibility(L.second, R.second));
91}
92
John McCallc273f242010-10-30 11:50:40 +000093static LVPair merge(LVPair L, LinkageInfo R) {
94 return LVPair(minLinkage(L.first, R.linkage()),
95 minVisibility(L.second, R.visibility()));
96}
97
Benjamin Kramer396dcf32010-11-05 19:56:37 +000098namespace {
John McCall07072662010-11-02 01:45:15 +000099/// Flags controlling the computation of linkage and visibility.
100struct LVFlags {
101 bool ConsiderGlobalVisibility;
102 bool ConsiderVisibilityAttributes;
John McCall8bc6d5b2011-03-04 10:39:25 +0000103 bool ConsiderTemplateParameterTypes;
John McCall07072662010-11-02 01:45:15 +0000104
105 LVFlags() : ConsiderGlobalVisibility(true),
John McCall8bc6d5b2011-03-04 10:39:25 +0000106 ConsiderVisibilityAttributes(true),
107 ConsiderTemplateParameterTypes(true) {
John McCall07072662010-11-02 01:45:15 +0000108 }
109
Douglas Gregorbf62d642010-12-06 18:36:25 +0000110 /// \brief Returns a set of flags that is only useful for computing the
111 /// linkage, not the visibility, of a declaration.
112 static LVFlags CreateOnlyDeclLinkage() {
113 LVFlags F;
114 F.ConsiderGlobalVisibility = false;
115 F.ConsiderVisibilityAttributes = false;
John McCall8bc6d5b2011-03-04 10:39:25 +0000116 F.ConsiderTemplateParameterTypes = false;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000117 return F;
118 }
119
John McCall07072662010-11-02 01:45:15 +0000120 /// Returns a set of flags, otherwise based on these, which ignores
121 /// off all sources of visibility except template arguments.
122 LVFlags onlyTemplateVisibility() const {
123 LVFlags F = *this;
124 F.ConsiderGlobalVisibility = false;
125 F.ConsiderVisibilityAttributes = false;
John McCall8bc6d5b2011-03-04 10:39:25 +0000126 F.ConsiderTemplateParameterTypes = false;
John McCall07072662010-11-02 01:45:15 +0000127 return F;
128 }
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000129};
Benjamin Kramer396dcf32010-11-05 19:56:37 +0000130} // end anonymous namespace
John McCall07072662010-11-02 01:45:15 +0000131
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000132/// \brief Get the most restrictive linkage for the types in the given
133/// template parameter list.
John McCall457a04e2010-10-22 21:05:15 +0000134static LVPair
135getLVForTemplateParameterList(const TemplateParameterList *Params) {
136 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000137 for (TemplateParameterList::const_iterator P = Params->begin(),
138 PEnd = Params->end();
139 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000140 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
141 if (NTTP->isExpandedParameterPack()) {
142 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
143 QualType T = NTTP->getExpansionType(I);
144 if (!T->isDependentType())
145 LV = merge(LV, T->getLinkageAndVisibility());
146 }
147 continue;
148 }
149
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000150 if (!NTTP->getType()->isDependentType()) {
John McCall457a04e2010-10-22 21:05:15 +0000151 LV = merge(LV, NTTP->getType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000152 continue;
153 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +0000154 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000155
156 if (TemplateTemplateParmDecl *TTP
157 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
John McCallc273f242010-10-30 11:50:40 +0000158 LV = merge(LV, getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000159 }
160 }
161
John McCall457a04e2010-10-22 21:05:15 +0000162 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000163}
164
Douglas Gregorbf62d642010-12-06 18:36:25 +0000165/// getLVForDecl - Get the linkage and visibility for the given declaration.
166static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F);
167
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000168/// \brief Get the most restrictive linkage for the types and
169/// declarations in the given template argument list.
John McCall457a04e2010-10-22 21:05:15 +0000170static LVPair getLVForTemplateArgumentList(const TemplateArgument *Args,
Douglas Gregorbf62d642010-12-06 18:36:25 +0000171 unsigned NumArgs,
172 LVFlags &F) {
John McCall457a04e2010-10-22 21:05:15 +0000173 LVPair LV(ExternalLinkage, DefaultVisibility);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000174
175 for (unsigned I = 0; I != NumArgs; ++I) {
176 switch (Args[I].getKind()) {
177 case TemplateArgument::Null:
178 case TemplateArgument::Integral:
179 case TemplateArgument::Expression:
180 break;
181
182 case TemplateArgument::Type:
John McCall457a04e2010-10-22 21:05:15 +0000183 LV = merge(LV, Args[I].getAsType()->getLinkageAndVisibility());
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000184 break;
185
186 case TemplateArgument::Declaration:
John McCall457a04e2010-10-22 21:05:15 +0000187 // The decl can validly be null as the representation of nullptr
188 // arguments, valid only in C++0x.
189 if (Decl *D = Args[I].getAsDecl()) {
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000190 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
191 LV = merge(LV, getLVForDecl(ND, F));
John McCall457a04e2010-10-22 21:05:15 +0000192 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000193 break;
194
195 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000196 case TemplateArgument::TemplateExpansion:
197 if (TemplateDecl *Template
198 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Douglas Gregor91df6cf2010-12-06 18:50:56 +0000199 LV = merge(LV, getLVForDecl(Template, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000200 break;
201
202 case TemplateArgument::Pack:
John McCall457a04e2010-10-22 21:05:15 +0000203 LV = merge(LV, getLVForTemplateArgumentList(Args[I].pack_begin(),
Douglas Gregorbf62d642010-12-06 18:36:25 +0000204 Args[I].pack_size(),
205 F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000206 break;
207 }
208 }
209
John McCall457a04e2010-10-22 21:05:15 +0000210 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000211}
212
John McCallc273f242010-10-30 11:50:40 +0000213static LVPair
Douglas Gregorbf62d642010-12-06 18:36:25 +0000214getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
215 LVFlags &F) {
216 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F);
John McCall8823c652010-08-13 08:35:10 +0000217}
218
John McCall07072662010-11-02 01:45:15 +0000219static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000220 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000221 "Not a name having namespace scope");
222 ASTContext &Context = D->getASTContext();
223
224 // C++ [basic.link]p3:
225 // A name having namespace scope (3.3.6) has internal linkage if it
226 // is the name of
227 // - an object, reference, function or function template that is
228 // explicitly declared static; or,
229 // (This bullet corresponds to C99 6.2.2p3.)
230 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
231 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000232 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000233 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000234
235 // - an object or reference that is explicitly declared const
236 // and neither explicitly declared extern nor previously
237 // declared to have external linkage; or
238 // (there is no equivalent in C99)
239 if (Context.getLangOptions().CPlusPlus &&
Eli Friedmanf873c2f2009-11-26 03:04:01 +0000240 Var->getType().isConstant(Context) &&
John McCall8e7d6562010-08-26 03:08:43 +0000241 Var->getStorageClass() != SC_Extern &&
242 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000243 bool FoundExtern = false;
244 for (const VarDecl *PrevVar = Var->getPreviousDeclaration();
245 PrevVar && !FoundExtern;
246 PrevVar = PrevVar->getPreviousDeclaration())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000247 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000248 FoundExtern = true;
249
250 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000251 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000252 }
253 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000254 // C++ [temp]p4:
255 // A non-member function template can have internal linkage; any
256 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000257 const FunctionDecl *Function = 0;
258 if (const FunctionTemplateDecl *FunTmpl
259 = dyn_cast<FunctionTemplateDecl>(D))
260 Function = FunTmpl->getTemplatedDecl();
261 else
262 Function = cast<FunctionDecl>(D);
263
264 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000265 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000266 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000267 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
268 // - a data member of an anonymous union.
269 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000270 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000271 }
272
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000273 if (D->isInAnonymousNamespace()) {
274 const VarDecl *Var = dyn_cast<VarDecl>(D);
275 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
276 if ((!Var || !Var->isExternC()) && (!Func || !Func->isExternC()))
277 return LinkageInfo::uniqueExternal();
278 }
John McCallb7139c42010-10-28 04:18:25 +0000279
John McCall457a04e2010-10-22 21:05:15 +0000280 // Set up the defaults.
281
282 // C99 6.2.2p5:
283 // If the declaration of an identifier for an object has file
284 // scope and no storage-class specifier, its linkage is
285 // external.
John McCallc273f242010-10-30 11:50:40 +0000286 LinkageInfo LV;
287
John McCall07072662010-11-02 01:45:15 +0000288 if (F.ConsiderVisibilityAttributes) {
289 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
290 LV.setVisibility(GetVisibilityFromAttr(VA), true);
291 F.ConsiderGlobalVisibility = false;
John McCall2faf32c2010-12-10 02:59:44 +0000292 } else {
293 // If we're declared in a namespace with a visibility attribute,
294 // use that namespace's visibility, but don't call it explicit.
295 for (const DeclContext *DC = D->getDeclContext();
296 !isa<TranslationUnitDecl>(DC);
297 DC = DC->getParent()) {
298 if (!isa<NamespaceDecl>(DC)) continue;
299 if (const VisibilityAttr *VA =
300 cast<NamespaceDecl>(DC)->getAttr<VisibilityAttr>()) {
301 LV.setVisibility(GetVisibilityFromAttr(VA), false);
302 F.ConsiderGlobalVisibility = false;
303 break;
304 }
305 }
John McCall07072662010-11-02 01:45:15 +0000306 }
John McCallc273f242010-10-30 11:50:40 +0000307 }
John McCall457a04e2010-10-22 21:05:15 +0000308
Douglas Gregorf73b2822009-11-25 22:24:25 +0000309 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000310
Douglas Gregorf73b2822009-11-25 22:24:25 +0000311 // A name having namespace scope has external linkage if it is the
312 // name of
313 //
314 // - an object or reference, unless it has internal linkage; or
315 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000316 // GCC applies the following optimization to variables and static
317 // data members, but not to functions:
318 //
John McCall457a04e2010-10-22 21:05:15 +0000319 // Modify the variable's LV by the LV of its type unless this is
320 // C or extern "C". This follows from [basic.link]p9:
321 // A type without linkage shall not be used as the type of a
322 // variable or function with external linkage unless
323 // - the entity has C language linkage, or
324 // - the entity is declared within an unnamed namespace, or
325 // - the entity is not used or is defined in the same
326 // translation unit.
327 // and [basic.link]p10:
328 // ...the types specified by all declarations referring to a
329 // given variable or function shall be identical...
330 // C does not have an equivalent rule.
331 //
John McCall5fe84122010-10-26 04:59:26 +0000332 // Ignore this if we've got an explicit attribute; the user
333 // probably knows what they're doing.
334 //
John McCall457a04e2010-10-22 21:05:15 +0000335 // Note that we don't want to make the variable non-external
336 // because of this, but unique-external linkage suits us.
John McCall36cd5cc2010-10-30 09:18:49 +0000337 if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
John McCall457a04e2010-10-22 21:05:15 +0000338 LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
339 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000340 return LinkageInfo::uniqueExternal();
341 if (!LV.visibilityExplicit())
342 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000343 }
344
John McCall23032652010-11-02 18:38:13 +0000345 if (Var->getStorageClass() == SC_PrivateExtern)
346 LV.setVisibility(HiddenVisibility, true);
347
Douglas Gregorf73b2822009-11-25 22:24:25 +0000348 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000349 (Var->getStorageClass() == SC_Extern ||
350 Var->getStorageClass() == SC_PrivateExtern)) {
John McCall457a04e2010-10-22 21:05:15 +0000351
Douglas Gregorf73b2822009-11-25 22:24:25 +0000352 // C99 6.2.2p4:
353 // For an identifier declared with the storage-class specifier
354 // extern in a scope in which a prior declaration of that
355 // identifier is visible, if the prior declaration specifies
356 // internal or external linkage, the linkage of the identifier
357 // at the later declaration is the same as the linkage
358 // specified at the prior declaration. If no prior declaration
359 // is visible, or if the prior declaration specifies no
360 // linkage, then the identifier has external linkage.
361 if (const VarDecl *PrevVar = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000362 LinkageInfo PrevLV = getLVForDecl(PrevVar, F);
John McCallc273f242010-10-30 11:50:40 +0000363 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
364 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000365 }
366 }
367
Douglas Gregorf73b2822009-11-25 22:24:25 +0000368 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000369 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000370 // In theory, we can modify the function's LV by the LV of its
371 // type unless it has C linkage (see comment above about variables
372 // for justification). In practice, GCC doesn't do this, so it's
373 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000374
John McCall23032652010-11-02 18:38:13 +0000375 if (Function->getStorageClass() == SC_PrivateExtern)
376 LV.setVisibility(HiddenVisibility, true);
377
Douglas Gregorf73b2822009-11-25 22:24:25 +0000378 // C99 6.2.2p5:
379 // If the declaration of an identifier for a function has no
380 // storage-class specifier, its linkage is determined exactly
381 // as if it were declared with the storage-class specifier
382 // extern.
383 if (!Context.getLangOptions().CPlusPlus &&
John McCall8e7d6562010-08-26 03:08:43 +0000384 (Function->getStorageClass() == SC_Extern ||
385 Function->getStorageClass() == SC_PrivateExtern ||
386 Function->getStorageClass() == SC_None)) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000387 // C99 6.2.2p4:
388 // For an identifier declared with the storage-class specifier
389 // extern in a scope in which a prior declaration of that
390 // identifier is visible, if the prior declaration specifies
391 // internal or external linkage, the linkage of the identifier
392 // at the later declaration is the same as the linkage
393 // specified at the prior declaration. If no prior declaration
394 // is visible, or if the prior declaration specifies no
395 // linkage, then the identifier has external linkage.
396 if (const FunctionDecl *PrevFunc = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000397 LinkageInfo PrevLV = getLVForDecl(PrevFunc, F);
John McCallc273f242010-10-30 11:50:40 +0000398 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
399 LV.mergeVisibility(PrevLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000400 }
401 }
402
John McCallf768aa72011-02-10 06:50:24 +0000403 // In C++, then if the type of the function uses a type with
404 // unique-external linkage, it's not legally usable from outside
405 // this translation unit. However, we should use the C linkage
406 // rules instead for extern "C" declarations.
407 if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
408 Function->getType()->getLinkage() == UniqueExternalLinkage)
409 return LinkageInfo::uniqueExternal();
410
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000411 if (FunctionTemplateSpecializationInfo *SpecInfo
412 = Function->getTemplateSpecializationInfo()) {
John McCall07072662010-11-02 01:45:15 +0000413 LV.merge(getLVForDecl(SpecInfo->getTemplate(),
414 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000415 const TemplateArgumentList &TemplateArgs = *SpecInfo->TemplateArguments;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000416 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000417 }
418
Douglas Gregorf73b2822009-11-25 22:24:25 +0000419 // - a named class (Clause 9), or an unnamed class defined in a
420 // typedef declaration in which the class has the typedef name
421 // for linkage purposes (7.1.3); or
422 // - a named enumeration (7.2), or an unnamed enumeration
423 // defined in a typedef declaration in which the enumeration
424 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000425 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
426 // Unnamed tags have no linkage.
427 if (!Tag->getDeclName() && !Tag->getTypedefForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000428 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000429
John McCall457a04e2010-10-22 21:05:15 +0000430 // If this is a class template specialization, consider the
431 // linkage of the template and template arguments.
432 if (const ClassTemplateSpecializationDecl *Spec
433 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
John McCall07072662010-11-02 01:45:15 +0000434 // From the template.
435 LV.merge(getLVForDecl(Spec->getSpecializedTemplate(),
436 F.onlyTemplateVisibility()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000437
John McCall457a04e2010-10-22 21:05:15 +0000438 // The arguments at which the template was instantiated.
439 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000440 LV.merge(getLVForTemplateArgumentList(TemplateArgs, F));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000441 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000442
John McCall5fe84122010-10-26 04:59:26 +0000443 // Consider -fvisibility unless the type has C linkage.
John McCall07072662010-11-02 01:45:15 +0000444 if (F.ConsiderGlobalVisibility)
445 F.ConsiderGlobalVisibility =
John McCall5fe84122010-10-26 04:59:26 +0000446 (Context.getLangOptions().CPlusPlus &&
447 !Tag->getDeclContext()->isExternCContext());
John McCall457a04e2010-10-22 21:05:15 +0000448
Douglas Gregorf73b2822009-11-25 22:24:25 +0000449 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000450 } else if (isa<EnumConstantDecl>(D)) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000451 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F);
John McCallc273f242010-10-30 11:50:40 +0000452 if (!isExternalLinkage(EnumLV.linkage()))
453 return LinkageInfo::none();
454 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000455
456 // - a template, unless it is a function template that has
457 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000458 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
459 if (F.ConsiderTemplateParameterTypes)
460 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000461
Douglas Gregorf73b2822009-11-25 22:24:25 +0000462 // - a namespace (7.3), unless it is declared within an unnamed
463 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000464 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
465 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000466
John McCall457a04e2010-10-22 21:05:15 +0000467 // By extension, we assign external linkage to Objective-C
468 // interfaces.
469 } else if (isa<ObjCInterfaceDecl>(D)) {
470 // fallout
471
472 // Everything not covered here has no linkage.
473 } else {
John McCallc273f242010-10-30 11:50:40 +0000474 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000475 }
476
477 // If we ended up with non-external linkage, visibility should
478 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000479 if (LV.linkage() != ExternalLinkage)
480 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000481
482 // If we didn't end up with hidden visibility, consider attributes
483 // and -fvisibility.
John McCall07072662010-11-02 01:45:15 +0000484 if (F.ConsiderGlobalVisibility)
John McCallc273f242010-10-30 11:50:40 +0000485 LV.mergeVisibility(Context.getLangOptions().getVisibilityMode());
John McCall457a04e2010-10-22 21:05:15 +0000486
487 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000488}
489
John McCall07072662010-11-02 01:45:15 +0000490static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) {
John McCall457a04e2010-10-22 21:05:15 +0000491 // Only certain class members have linkage. Note that fields don't
492 // really have linkage, but it's convenient to say they do for the
493 // purposes of calculating linkage of pointer-to-data-member
494 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000495 if (!(isa<CXXMethodDecl>(D) ||
496 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000497 isa<FieldDecl>(D) ||
John McCall8823c652010-08-13 08:35:10 +0000498 (isa<TagDecl>(D) &&
499 (D->getDeclName() || cast<TagDecl>(D)->getTypedefForAnonDecl()))))
John McCallc273f242010-10-30 11:50:40 +0000500 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000501
John McCall07072662010-11-02 01:45:15 +0000502 LinkageInfo LV;
503
504 // The flags we're going to use to compute the class's visibility.
505 LVFlags ClassF = F;
506
507 // If we have an explicit visibility attribute, merge that in.
508 if (F.ConsiderVisibilityAttributes) {
509 if (const VisibilityAttr *VA = GetExplicitVisibility(D)) {
510 LV.mergeVisibility(GetVisibilityFromAttr(VA), true);
511
512 // Ignore global visibility later, but not this attribute.
513 F.ConsiderGlobalVisibility = false;
514
515 // Ignore both global visibility and attributes when computing our
516 // parent's visibility.
517 ClassF = F.onlyTemplateVisibility();
518 }
519 }
John McCallc273f242010-10-30 11:50:40 +0000520
521 // Class members only have linkage if their class has external
John McCall07072662010-11-02 01:45:15 +0000522 // linkage.
523 LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF));
524 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000525 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000526
527 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000528 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000529 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000530
John McCall8823c652010-08-13 08:35:10 +0000531 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000532 // If the type of the function uses a type with unique-external
533 // linkage, it's not legally usable from outside this translation unit.
534 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
535 return LinkageInfo::uniqueExternal();
536
John McCall37bb6c92010-10-29 22:22:43 +0000537 TemplateSpecializationKind TSK = TSK_Undeclared;
538
John McCall457a04e2010-10-22 21:05:15 +0000539 // If this is a method template specialization, use the linkage for
540 // the template parameters and arguments.
541 if (FunctionTemplateSpecializationInfo *Spec
John McCall8823c652010-08-13 08:35:10 +0000542 = MD->getTemplateSpecializationInfo()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000543 LV.merge(getLVForTemplateArgumentList(*Spec->TemplateArguments, F));
John McCall8bc6d5b2011-03-04 10:39:25 +0000544 if (F.ConsiderTemplateParameterTypes)
545 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000546 Spec->getTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000547
548 TSK = Spec->getTemplateSpecializationKind();
549 } else if (MemberSpecializationInfo *MSI =
550 MD->getMemberSpecializationInfo()) {
551 TSK = MSI->getTemplateSpecializationKind();
John McCall8823c652010-08-13 08:35:10 +0000552 }
553
John McCall37bb6c92010-10-29 22:22:43 +0000554 // If we're paying attention to global visibility, apply
555 // -finline-visibility-hidden if this is an inline method.
556 //
John McCallc273f242010-10-30 11:50:40 +0000557 // Note that ConsiderGlobalVisibility doesn't yet have information
558 // about whether containing classes have visibility attributes,
559 // and that's intentional.
560 if (TSK != TSK_ExplicitInstantiationDeclaration &&
John McCall07072662010-11-02 01:45:15 +0000561 F.ConsiderGlobalVisibility &&
John McCalle6e622e2010-11-01 01:29:57 +0000562 MD->getASTContext().getLangOptions().InlineVisibilityHidden) {
563 // InlineVisibilityHidden only applies to definitions, and
564 // isInlined() only gives meaningful answers on definitions
565 // anyway.
566 const FunctionDecl *Def = 0;
567 if (MD->hasBody(Def) && Def->isInlined())
568 LV.setVisibility(HiddenVisibility);
569 }
John McCall457a04e2010-10-22 21:05:15 +0000570
John McCall37bb6c92010-10-29 22:22:43 +0000571 // Note that in contrast to basically every other situation, we
572 // *do* apply -fvisibility to method declarations.
573
574 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000575 if (const ClassTemplateSpecializationDecl *Spec
576 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
577 // Merge template argument/parameter information for member
578 // class template specializations.
Douglas Gregorbf62d642010-12-06 18:36:25 +0000579 LV.merge(getLVForTemplateArgumentList(Spec->getTemplateArgs(), F));
John McCall8bc6d5b2011-03-04 10:39:25 +0000580 if (F.ConsiderTemplateParameterTypes)
581 LV.merge(getLVForTemplateParameterList(
John McCall457a04e2010-10-22 21:05:15 +0000582 Spec->getSpecializedTemplate()->getTemplateParameters()));
John McCall37bb6c92010-10-29 22:22:43 +0000583 }
584
John McCall37bb6c92010-10-29 22:22:43 +0000585 // Static data members.
586 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000587 // Modify the variable's linkage by its type, but ignore the
588 // type's visibility unless it's a definition.
589 LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
590 if (TypeLV.first != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000591 LV.mergeLinkage(UniqueExternalLinkage);
592 if (!LV.visibilityExplicit())
593 LV.mergeVisibility(TypeLV.second);
John McCall37bb6c92010-10-29 22:22:43 +0000594 }
595
John McCall07072662010-11-02 01:45:15 +0000596 F.ConsiderGlobalVisibility &= !LV.visibilityExplicit();
John McCall37bb6c92010-10-29 22:22:43 +0000597
598 // Apply -fvisibility if desired.
John McCall07072662010-11-02 01:45:15 +0000599 if (F.ConsiderGlobalVisibility && LV.visibility() != HiddenVisibility) {
John McCallc273f242010-10-30 11:50:40 +0000600 LV.mergeVisibility(D->getASTContext().getLangOptions().getVisibilityMode());
John McCall8823c652010-08-13 08:35:10 +0000601 }
602
John McCall457a04e2010-10-22 21:05:15 +0000603 return LV;
John McCall8823c652010-08-13 08:35:10 +0000604}
605
John McCalld396b972011-02-08 19:01:05 +0000606static void clearLinkageForClass(const CXXRecordDecl *record) {
607 for (CXXRecordDecl::decl_iterator
608 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
609 Decl *child = *i;
610 if (isa<NamedDecl>(child))
611 cast<NamedDecl>(child)->ClearLinkageCache();
612 }
613}
614
615void NamedDecl::ClearLinkageCache() {
616 // Note that we can't skip clearing the linkage of children just
617 // because the parent doesn't have cached linkage: we don't cache
618 // when computing linkage for parent contexts.
619
620 HasCachedLinkage = 0;
621
622 // If we're changing the linkage of a class, we need to reset the
623 // linkage of child declarations, too.
624 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
625 clearLinkageForClass(record);
626
John McCall83779672011-02-19 02:53:41 +0000627 if (ClassTemplateDecl *temp =
628 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000629 // Clear linkage for the template pattern.
630 CXXRecordDecl *record = temp->getTemplatedDecl();
631 record->HasCachedLinkage = 0;
632 clearLinkageForClass(record);
633
John McCall83779672011-02-19 02:53:41 +0000634 // We need to clear linkage for specializations, too.
635 for (ClassTemplateDecl::spec_iterator
636 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
637 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000638 }
John McCall83779672011-02-19 02:53:41 +0000639
640 // Clear cached linkage for function template decls, too.
641 if (FunctionTemplateDecl *temp =
642 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this)))
643 for (FunctionTemplateDecl::spec_iterator
644 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
645 i->ClearLinkageCache();
646
John McCalld396b972011-02-08 19:01:05 +0000647}
648
Douglas Gregorbf62d642010-12-06 18:36:25 +0000649Linkage NamedDecl::getLinkage() const {
650 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000651 assert(Linkage(CachedLinkage) ==
652 getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000653 return Linkage(CachedLinkage);
654 }
655
656 CachedLinkage = getLVForDecl(this,
657 LVFlags::CreateOnlyDeclLinkage()).linkage();
658 HasCachedLinkage = 1;
659 return Linkage(CachedLinkage);
660}
661
John McCallc273f242010-10-30 11:50:40 +0000662LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000663 LinkageInfo LI = getLVForDecl(this, LVFlags());
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000664 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000665 HasCachedLinkage = 1;
666 CachedLinkage = LI.linkage();
667 return LI;
John McCall033caa52010-10-29 00:29:13 +0000668}
Ted Kremenek926d8602010-04-20 23:15:35 +0000669
John McCall07072662010-11-02 01:45:15 +0000670static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000671 // Objective-C: treat all Objective-C declarations as having external
672 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000673 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000674 default:
675 break;
John McCall457a04e2010-10-22 21:05:15 +0000676 case Decl::TemplateTemplateParm: // count these as external
677 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000678 case Decl::ObjCAtDefsField:
679 case Decl::ObjCCategory:
680 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000681 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000682 case Decl::ObjCForwardProtocol:
683 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000684 case Decl::ObjCMethod:
685 case Decl::ObjCProperty:
686 case Decl::ObjCPropertyImpl:
687 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000688 return LinkageInfo::external();
Ted Kremenek926d8602010-04-20 23:15:35 +0000689 }
690
Douglas Gregorf73b2822009-11-25 22:24:25 +0000691 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000692 if (D->getDeclContext()->getRedeclContext()->isFileContext())
John McCall07072662010-11-02 01:45:15 +0000693 return getLVForNamespaceScopeDecl(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000694
695 // C++ [basic.link]p5:
696 // In addition, a member function, static data member, a named
697 // class or enumeration of class scope, or an unnamed class or
698 // enumeration defined in a class-scope typedef declaration such
699 // that the class or enumeration has the typedef name for linkage
700 // purposes (7.1.3), has external linkage if the name of the class
701 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000702 if (D->getDeclContext()->isRecord())
John McCall07072662010-11-02 01:45:15 +0000703 return getLVForClassMember(D, Flags);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000704
705 // C++ [basic.link]p6:
706 // The name of a function declared in block scope and the name of
707 // an object declared by a block scope extern declaration have
708 // linkage. If there is a visible declaration of an entity with
709 // linkage having the same name and type, ignoring entities
710 // declared outside the innermost enclosing namespace scope, the
711 // block scope declaration declares that same entity and receives
712 // the linkage of the previous declaration. If there is more than
713 // one such matching entity, the program is ill-formed. Otherwise,
714 // if no matching entity is found, the block scope entity receives
715 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000716 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
717 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000718 if (Function->isInAnonymousNamespace() && !Function->isExternC())
John McCallc273f242010-10-30 11:50:40 +0000719 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000720
John McCallc273f242010-10-30 11:50:40 +0000721 LinkageInfo LV;
Douglas Gregorbf62d642010-12-06 18:36:25 +0000722 if (Flags.ConsiderVisibilityAttributes) {
723 if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
724 LV.setVisibility(GetVisibilityFromAttr(VA));
725 }
726
John McCall457a04e2010-10-22 21:05:15 +0000727 if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000728 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000729 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
730 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000731 }
732
733 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000734 }
735
John McCall033caa52010-10-29 00:29:13 +0000736 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000737 if (Var->getStorageClass() == SC_Extern ||
738 Var->getStorageClass() == SC_PrivateExtern) {
Chandler Carruth4322a282011-02-25 00:05:02 +0000739 if (Var->isInAnonymousNamespace() && !Var->isExternC())
John McCallc273f242010-10-30 11:50:40 +0000740 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000741
John McCallc273f242010-10-30 11:50:40 +0000742 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000743 if (Var->getStorageClass() == SC_PrivateExtern)
John McCallc273f242010-10-30 11:50:40 +0000744 LV.setVisibility(HiddenVisibility);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000745 else if (Flags.ConsiderVisibilityAttributes) {
746 if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
747 LV.setVisibility(GetVisibilityFromAttr(VA));
748 }
749
John McCall457a04e2010-10-22 21:05:15 +0000750 if (const VarDecl *Prev = Var->getPreviousDeclaration()) {
Douglas Gregorbf62d642010-12-06 18:36:25 +0000751 LinkageInfo PrevLV = getLVForDecl(Prev, Flags);
John McCallc273f242010-10-30 11:50:40 +0000752 if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage());
753 LV.mergeVisibility(PrevLV);
John McCall457a04e2010-10-22 21:05:15 +0000754 }
755
756 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000757 }
758 }
759
760 // C++ [basic.link]p6:
761 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000762 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000763}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000764
Douglas Gregor2ada0482009-02-04 17:27:36 +0000765std::string NamedDecl::getQualifiedNameAsString() const {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000766 return getQualifiedNameAsString(getASTContext().getLangOptions());
767}
768
769std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000770 const DeclContext *Ctx = getDeclContext();
771
772 if (Ctx->isFunctionOrMethod())
773 return getNameAsString();
774
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000775 typedef llvm::SmallVector<const DeclContext *, 8> ContextsTy;
776 ContextsTy Contexts;
777
778 // Collect contexts.
779 while (Ctx && isa<NamedDecl>(Ctx)) {
780 Contexts.push_back(Ctx);
781 Ctx = Ctx->getParent();
782 };
783
784 std::string QualName;
785 llvm::raw_string_ostream OS(QualName);
786
787 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
788 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000789 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000790 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000791 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
792 std::string TemplateArgsStr
793 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000794 TemplateArgs.data(),
795 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000796 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000797 OS << Spec->getName() << TemplateArgsStr;
798 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000799 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000800 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000801 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000802 OS << ND;
803 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
804 if (!RD->getIdentifier())
805 OS << "<anonymous " << RD->getKindName() << '>';
806 else
807 OS << RD;
808 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000809 const FunctionProtoType *FT = 0;
810 if (FD->hasWrittenPrototype())
811 FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>());
812
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000813 OS << FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000814 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000815 unsigned NumParams = FD->getNumParams();
816 for (unsigned i = 0; i < NumParams; ++i) {
817 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000818 OS << ", ";
Sam Weinigb999f682009-12-28 03:19:38 +0000819 std::string Param;
820 FD->getParamDecl(i)->getType().getAsStringInternal(Param, P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000821 OS << Param;
Sam Weinigb999f682009-12-28 03:19:38 +0000822 }
823
824 if (FT->isVariadic()) {
825 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000826 OS << ", ";
827 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000828 }
829 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000830 OS << ')';
831 } else {
832 OS << cast<NamedDecl>(*I);
833 }
834 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000835 }
836
John McCalla2a3f7d2010-03-16 21:48:18 +0000837 if (getDeclName())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000838 OS << this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000839 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000840 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000841
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000842 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000843}
844
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000845bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000846 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
847
Douglas Gregor889ceb72009-02-03 19:21:40 +0000848 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
849 // We want to keep it, unless it nominates same namespace.
850 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000851 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
852 ->getOriginalNamespace() ==
853 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
854 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000855 }
Mike Stump11289f42009-09-09 15:08:12 +0000856
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000857 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
858 // For function declarations, we keep track of redeclarations.
859 return FD->getPreviousDeclaration() == OldD;
860
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000861 // For function templates, the underlying function declarations are linked.
862 if (const FunctionTemplateDecl *FunctionTemplate
863 = dyn_cast<FunctionTemplateDecl>(this))
864 if (const FunctionTemplateDecl *OldFunctionTemplate
865 = dyn_cast<FunctionTemplateDecl>(OldD))
866 return FunctionTemplate->getTemplatedDecl()
867 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000868
Steve Naroffc4173fa2009-02-22 19:35:57 +0000869 // For method declarations, we keep track of redeclarations.
870 if (isa<ObjCMethodDecl>(this))
871 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000872
John McCall9f3059a2009-10-09 21:13:30 +0000873 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
874 return true;
875
John McCall3f746822009-11-17 05:59:44 +0000876 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
877 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
878 cast<UsingShadowDecl>(OldD)->getTargetDecl();
879
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000880 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
881 ASTContext &Context = getASTContext();
882 return Context.getCanonicalNestedNameSpecifier(
883 cast<UsingDecl>(this)->getQualifier()) ==
884 Context.getCanonicalNestedNameSpecifier(
885 cast<UsingDecl>(OldD)->getQualifier());
886 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000887
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000888 // For non-function declarations, if the declarations are of the
889 // same kind then this must be a redeclaration, or semantic analysis
890 // would not have given us the new declaration.
891 return this->getKind() == OldD->getKind();
892}
893
Douglas Gregoreddf4332009-02-24 20:03:32 +0000894bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000895 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000896}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000897
Anders Carlsson6915bf62009-06-26 06:29:23 +0000898NamedDecl *NamedDecl::getUnderlyingDecl() {
899 NamedDecl *ND = this;
900 while (true) {
John McCall3f746822009-11-17 05:59:44 +0000901 if (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
Anders Carlsson6915bf62009-06-26 06:29:23 +0000902 ND = UD->getTargetDecl();
903 else if (ObjCCompatibleAliasDecl *AD
904 = dyn_cast<ObjCCompatibleAliasDecl>(ND))
905 return AD->getClassInterface();
906 else
907 return ND;
908 }
909}
910
John McCalla8ae2222010-04-06 21:38:20 +0000911bool NamedDecl::isCXXInstanceMember() const {
912 assert(isCXXClassMember() &&
913 "checking whether non-member is instance member");
914
915 const NamedDecl *D = this;
916 if (isa<UsingShadowDecl>(D))
917 D = cast<UsingShadowDecl>(D)->getTargetDecl();
918
Francois Pichet783dd6e2010-11-21 06:08:52 +0000919 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000920 return true;
921 if (isa<CXXMethodDecl>(D))
922 return cast<CXXMethodDecl>(D)->isInstance();
923 if (isa<FunctionTemplateDecl>(D))
924 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
925 ->getTemplatedDecl())->isInstance();
926 return false;
927}
928
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000929//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000930// DeclaratorDecl Implementation
931//===----------------------------------------------------------------------===//
932
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000933template <typename DeclT>
934static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
935 if (decl->getNumTemplateParameterLists() > 0)
936 return decl->getTemplateParameterList(0)->getTemplateLoc();
937 else
938 return decl->getInnerLocStart();
939}
940
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000941SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000942 TypeSourceInfo *TSI = getTypeSourceInfo();
943 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000944 return SourceLocation();
945}
946
Douglas Gregor14454802011-02-25 02:25:35 +0000947void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
948 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +0000949 // Make sure the extended decl info is allocated.
950 if (!hasExtInfo()) {
951 // Save (non-extended) type source info pointer.
952 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
953 // Allocate external info struct.
954 DeclInfo = new (getASTContext()) ExtInfo;
955 // Restore savedTInfo into (extended) decl info.
956 getExtInfo()->TInfo = savedTInfo;
957 }
958 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +0000959 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +0000960 }
961 else {
962 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +0000963 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +0000964 if (getExtInfo()->NumTemplParamLists == 0) {
965 // Save type source info pointer.
966 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
967 // Deallocate the extended decl info.
968 getASTContext().Deallocate(getExtInfo());
969 // Restore savedTInfo into (non-extended) decl info.
970 DeclInfo = savedTInfo;
971 }
972 else
973 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +0000974 }
975 }
976}
977
Abramo Bagnara60804e12011-03-18 15:16:37 +0000978void
979DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
980 unsigned NumTPLists,
981 TemplateParameterList **TPLists) {
982 assert(NumTPLists > 0);
983 // Make sure the extended decl info is allocated.
984 if (!hasExtInfo()) {
985 // Save (non-extended) type source info pointer.
986 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
987 // Allocate external info struct.
988 DeclInfo = new (getASTContext()) ExtInfo;
989 // Restore savedTInfo into (extended) decl info.
990 getExtInfo()->TInfo = savedTInfo;
991 }
992 // Set the template parameter lists info.
993 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
994}
995
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000996SourceLocation DeclaratorDecl::getOuterLocStart() const {
997 return getTemplateOrInnerLocStart(this);
998}
999
Abramo Bagnaraea947882011-03-08 16:41:52 +00001000namespace {
1001
1002// Helper function: returns true if QT is or contains a type
1003// having a postfix component.
1004bool typeIsPostfix(clang::QualType QT) {
1005 while (true) {
1006 const Type* T = QT.getTypePtr();
1007 switch (T->getTypeClass()) {
1008 default:
1009 return false;
1010 case Type::Pointer:
1011 QT = cast<PointerType>(T)->getPointeeType();
1012 break;
1013 case Type::BlockPointer:
1014 QT = cast<BlockPointerType>(T)->getPointeeType();
1015 break;
1016 case Type::MemberPointer:
1017 QT = cast<MemberPointerType>(T)->getPointeeType();
1018 break;
1019 case Type::LValueReference:
1020 case Type::RValueReference:
1021 QT = cast<ReferenceType>(T)->getPointeeType();
1022 break;
1023 case Type::PackExpansion:
1024 QT = cast<PackExpansionType>(T)->getPattern();
1025 break;
1026 case Type::Paren:
1027 case Type::ConstantArray:
1028 case Type::DependentSizedArray:
1029 case Type::IncompleteArray:
1030 case Type::VariableArray:
1031 case Type::FunctionProto:
1032 case Type::FunctionNoProto:
1033 return true;
1034 }
1035 }
1036}
1037
1038} // namespace
1039
1040SourceRange DeclaratorDecl::getSourceRange() const {
1041 SourceLocation RangeEnd = getLocation();
1042 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1043 if (typeIsPostfix(TInfo->getType()))
1044 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1045 }
1046 return SourceRange(getOuterLocStart(), RangeEnd);
1047}
1048
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001049void
Douglas Gregor20527e22010-06-15 17:44:38 +00001050QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1051 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001052 TemplateParameterList **TPLists) {
1053 assert((NumTPLists == 0 || TPLists != 0) &&
1054 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001055
1056 // Free previous template parameters (if any).
1057 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001058 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001059 TemplParamLists = 0;
1060 NumTemplParamLists = 0;
1061 }
1062 // Set info on matched template parameter lists (if any).
1063 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001064 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001065 NumTemplParamLists = NumTPLists;
1066 for (unsigned i = NumTPLists; i-- > 0; )
1067 TemplParamLists[i] = TPLists[i];
1068 }
1069}
1070
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001071//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001072// VarDecl Implementation
1073//===----------------------------------------------------------------------===//
1074
Sebastian Redl833ef452010-01-26 22:01:41 +00001075const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1076 switch (SC) {
John McCall8e7d6562010-08-26 03:08:43 +00001077 case SC_None: break;
1078 case SC_Auto: return "auto"; break;
1079 case SC_Extern: return "extern"; break;
1080 case SC_PrivateExtern: return "__private_extern__"; break;
1081 case SC_Register: return "register"; break;
1082 case SC_Static: return "static"; break;
Sebastian Redl833ef452010-01-26 22:01:41 +00001083 }
1084
1085 assert(0 && "Invalid storage class");
1086 return 0;
1087}
1088
Abramo Bagnaradff19302011-03-08 08:55:46 +00001089VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1090 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001091 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001092 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001093 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001094}
1095
Douglas Gregorbf62d642010-12-06 18:36:25 +00001096void VarDecl::setStorageClass(StorageClass SC) {
1097 assert(isLegalForVariable(SC));
1098 if (getStorageClass() != SC)
1099 ClearLinkageCache();
1100
1101 SClass = SC;
1102}
1103
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001104SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001105 if (getInit())
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001106 return SourceRange(getOuterLocStart(), getInit()->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00001107 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001108}
1109
Sebastian Redl833ef452010-01-26 22:01:41 +00001110bool VarDecl::isExternC() const {
1111 ASTContext &Context = getASTContext();
1112 if (!Context.getLangOptions().CPlusPlus)
1113 return (getDeclContext()->isTranslationUnit() &&
John McCall8e7d6562010-08-26 03:08:43 +00001114 getStorageClass() != SC_Static) ||
Sebastian Redl833ef452010-01-26 22:01:41 +00001115 (getDeclContext()->isFunctionOrMethod() && hasExternalStorage());
1116
Chandler Carruth4322a282011-02-25 00:05:02 +00001117 const DeclContext *DC = getDeclContext();
1118 if (DC->isFunctionOrMethod())
1119 return false;
1120
1121 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001122 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1123 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001124 return getStorageClass() != SC_Static;
Sebastian Redl833ef452010-01-26 22:01:41 +00001125
1126 break;
1127 }
1128
Sebastian Redl833ef452010-01-26 22:01:41 +00001129 }
1130
1131 return false;
1132}
1133
1134VarDecl *VarDecl::getCanonicalDecl() {
1135 return getFirstDeclaration();
1136}
1137
Sebastian Redl35351a92010-01-31 22:27:38 +00001138VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
1139 // C++ [basic.def]p2:
1140 // A declaration is a definition unless [...] it contains the 'extern'
1141 // specifier or a linkage-specification and neither an initializer [...],
1142 // it declares a static data member in a class declaration [...].
1143 // C++ [temp.expl.spec]p15:
1144 // An explicit specialization of a static data member of a template is a
1145 // definition if the declaration includes an initializer; otherwise, it is
1146 // a declaration.
1147 if (isStaticDataMember()) {
1148 if (isOutOfLine() && (hasInit() ||
1149 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1150 return Definition;
1151 else
1152 return DeclarationOnly;
1153 }
1154 // C99 6.7p5:
1155 // A definition of an identifier is a declaration for that identifier that
1156 // [...] causes storage to be reserved for that object.
1157 // Note: that applies for all non-file-scope objects.
1158 // C99 6.9.2p1:
1159 // If the declaration of an identifier for an object has file scope and an
1160 // initializer, the declaration is an external definition for the identifier
1161 if (hasInit())
1162 return Definition;
1163 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1164 if (hasExternalStorage())
1165 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001166
John McCall8e7d6562010-08-26 03:08:43 +00001167 if (getStorageClassAsWritten() == SC_Extern ||
1168 getStorageClassAsWritten() == SC_PrivateExtern) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001169 for (const VarDecl *PrevVar = getPreviousDeclaration();
1170 PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
1171 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1172 return DeclarationOnly;
1173 }
1174 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001175 // C99 6.9.2p2:
1176 // A declaration of an object that has file scope without an initializer,
1177 // and without a storage class specifier or the scs 'static', constitutes
1178 // a tentative definition.
1179 // No such thing in C++.
1180 if (!getASTContext().getLangOptions().CPlusPlus && isFileVarDecl())
1181 return TentativeDefinition;
1182
1183 // What's left is (in C, block-scope) declarations without initializers or
1184 // external storage. These are definitions.
1185 return Definition;
1186}
1187
Sebastian Redl35351a92010-01-31 22:27:38 +00001188VarDecl *VarDecl::getActingDefinition() {
1189 DefinitionKind Kind = isThisDeclarationADefinition();
1190 if (Kind != TentativeDefinition)
1191 return 0;
1192
Chris Lattner48eb14d2010-06-14 18:31:46 +00001193 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001194 VarDecl *First = getFirstDeclaration();
1195 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1196 I != E; ++I) {
1197 Kind = (*I)->isThisDeclarationADefinition();
1198 if (Kind == Definition)
1199 return 0;
1200 else if (Kind == TentativeDefinition)
1201 LastTentative = *I;
1202 }
1203 return LastTentative;
1204}
1205
1206bool VarDecl::isTentativeDefinitionNow() const {
1207 DefinitionKind Kind = isThisDeclarationADefinition();
1208 if (Kind != TentativeDefinition)
1209 return false;
1210
1211 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1212 if ((*I)->isThisDeclarationADefinition() == Definition)
1213 return false;
1214 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001215 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001216}
1217
Sebastian Redl5ca79842010-02-01 20:16:42 +00001218VarDecl *VarDecl::getDefinition() {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001219 VarDecl *First = getFirstDeclaration();
1220 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1221 I != E; ++I) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001222 if ((*I)->isThisDeclarationADefinition() == Definition)
1223 return *I;
1224 }
1225 return 0;
1226}
1227
John McCall37bb6c92010-10-29 22:22:43 +00001228VarDecl::DefinitionKind VarDecl::hasDefinition() const {
1229 DefinitionKind Kind = DeclarationOnly;
1230
1231 const VarDecl *First = getFirstDeclaration();
1232 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1233 I != E; ++I)
1234 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition());
1235
1236 return Kind;
1237}
1238
Sebastian Redl5ca79842010-02-01 20:16:42 +00001239const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001240 redecl_iterator I = redecls_begin(), E = redecls_end();
1241 while (I != E && !I->getInit())
1242 ++I;
1243
1244 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001245 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001246 return I->getInit();
1247 }
1248 return 0;
1249}
1250
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001251bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001252 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001253 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001254
1255 if (!isStaticDataMember())
1256 return false;
1257
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001258 // If this static data member was instantiated from a static data member of
1259 // a class template, check whether that static data member was defined
1260 // out-of-line.
1261 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1262 return VD->isOutOfLine();
1263
1264 return false;
1265}
1266
Douglas Gregor1d957a32009-10-27 18:42:08 +00001267VarDecl *VarDecl::getOutOfLineDefinition() {
1268 if (!isStaticDataMember())
1269 return 0;
1270
1271 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1272 RD != RDEnd; ++RD) {
1273 if (RD->getLexicalDeclContext()->isFileContext())
1274 return *RD;
1275 }
1276
1277 return 0;
1278}
1279
Douglas Gregord5058122010-02-11 01:19:42 +00001280void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001281 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1282 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001283 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001284 }
1285
1286 Init = I;
1287}
1288
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001289VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001290 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001291 return cast<VarDecl>(MSI->getInstantiatedFrom());
1292
1293 return 0;
1294}
1295
Douglas Gregor3c74d412009-10-14 20:14:33 +00001296TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001297 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001298 return MSI->getTemplateSpecializationKind();
1299
1300 return TSK_Undeclared;
1301}
1302
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001303MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001304 return getASTContext().getInstantiatedFromStaticDataMember(this);
1305}
1306
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001307void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1308 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001309 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001310 assert(MSI && "Not an instantiated static data member?");
1311 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001312 if (TSK != TSK_ExplicitSpecialization &&
1313 PointOfInstantiation.isValid() &&
1314 MSI->getPointOfInstantiation().isInvalid())
1315 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001316}
1317
Sebastian Redl833ef452010-01-26 22:01:41 +00001318//===----------------------------------------------------------------------===//
1319// ParmVarDecl Implementation
1320//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001321
Sebastian Redl833ef452010-01-26 22:01:41 +00001322ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001323 SourceLocation StartLoc,
1324 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001325 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001326 StorageClass S, StorageClass SCAsWritten,
1327 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001328 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001329 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001330}
1331
Sebastian Redl833ef452010-01-26 22:01:41 +00001332Expr *ParmVarDecl::getDefaultArg() {
1333 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1334 assert(!hasUninstantiatedDefaultArg() &&
1335 "Default argument is not yet instantiated!");
1336
1337 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001338 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001339 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001340
Sebastian Redl833ef452010-01-26 22:01:41 +00001341 return Arg;
1342}
1343
1344unsigned ParmVarDecl::getNumDefaultArgTemporaries() const {
John McCall5d413782010-12-06 08:20:24 +00001345 if (const ExprWithCleanups *E = dyn_cast<ExprWithCleanups>(getInit()))
Sebastian Redl833ef452010-01-26 22:01:41 +00001346 return E->getNumTemporaries();
1347
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001348 return 0;
Douglas Gregor0760fa12009-03-10 23:43:53 +00001349}
1350
Sebastian Redl833ef452010-01-26 22:01:41 +00001351CXXTemporary *ParmVarDecl::getDefaultArgTemporary(unsigned i) {
1352 assert(getNumDefaultArgTemporaries() &&
1353 "Default arguments does not have any temporaries!");
1354
John McCall5d413782010-12-06 08:20:24 +00001355 ExprWithCleanups *E = cast<ExprWithCleanups>(getInit());
Sebastian Redl833ef452010-01-26 22:01:41 +00001356 return E->getTemporary(i);
1357}
1358
1359SourceRange ParmVarDecl::getDefaultArgRange() const {
1360 if (const Expr *E = getInit())
1361 return E->getSourceRange();
1362
1363 if (hasUninstantiatedDefaultArg())
1364 return getUninstantiatedDefaultArg()->getSourceRange();
1365
1366 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001367}
1368
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001369bool ParmVarDecl::isParameterPack() const {
1370 return isa<PackExpansionType>(getType());
1371}
1372
Nuno Lopes394ec982008-12-17 23:39:55 +00001373//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001374// FunctionDecl Implementation
1375//===----------------------------------------------------------------------===//
1376
Douglas Gregorb11aad82011-02-19 18:51:44 +00001377void FunctionDecl::getNameForDiagnostic(std::string &S,
1378 const PrintingPolicy &Policy,
1379 bool Qualified) const {
1380 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1381 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1382 if (TemplateArgs)
1383 S += TemplateSpecializationType::PrintTemplateArgumentList(
1384 TemplateArgs->data(),
1385 TemplateArgs->size(),
1386 Policy);
1387
1388}
1389
Ted Kremenek186a0742010-04-29 16:49:01 +00001390bool FunctionDecl::isVariadic() const {
1391 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1392 return FT->isVariadic();
1393 return false;
1394}
1395
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001396bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1397 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1398 if (I->Body) {
1399 Definition = *I;
1400 return true;
1401 }
1402 }
1403
1404 return false;
1405}
1406
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001407Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001408 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1409 if (I->Body) {
1410 Definition = *I;
1411 return I->Body.get(getASTContext().getExternalSource());
Douglas Gregor89f238c2008-04-21 02:02:58 +00001412 }
1413 }
1414
1415 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001416}
1417
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001418void FunctionDecl::setBody(Stmt *B) {
1419 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001420 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001421 EndRangeLoc = B->getLocEnd();
1422}
1423
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001424void FunctionDecl::setPure(bool P) {
1425 IsPure = P;
1426 if (P)
1427 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1428 Parent->markedVirtualFunctionPure();
1429}
1430
Douglas Gregor16618f22009-09-12 00:17:51 +00001431bool FunctionDecl::isMain() const {
1432 ASTContext &Context = getASTContext();
John McCalldeb84482009-08-15 02:09:25 +00001433 return !Context.getLangOptions().Freestanding &&
Sebastian Redl50c68252010-08-31 00:36:30 +00001434 getDeclContext()->getRedeclContext()->isTranslationUnit() &&
Douglas Gregore62c0a42009-02-24 01:23:02 +00001435 getIdentifier() && getIdentifier()->isStr("main");
1436}
1437
Douglas Gregor16618f22009-09-12 00:17:51 +00001438bool FunctionDecl::isExternC() const {
1439 ASTContext &Context = getASTContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001440 // In C, any non-static, non-overloadable function has external
1441 // linkage.
1442 if (!Context.getLangOptions().CPlusPlus)
John McCall8e7d6562010-08-26 03:08:43 +00001443 return getStorageClass() != SC_Static && !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001444
Chandler Carruth4322a282011-02-25 00:05:02 +00001445 const DeclContext *DC = getDeclContext();
1446 if (DC->isRecord())
1447 return false;
1448
1449 for (; !DC->isTranslationUnit(); DC = DC->getParent()) {
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001450 if (const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(DC)) {
1451 if (Linkage->getLanguage() == LinkageSpecDecl::lang_c)
John McCall8e7d6562010-08-26 03:08:43 +00001452 return getStorageClass() != SC_Static &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001453 !getAttr<OverloadableAttr>();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001454
1455 break;
1456 }
1457 }
1458
Douglas Gregorbff62032010-10-21 16:57:46 +00001459 return isMain();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001460}
1461
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001462bool FunctionDecl::isGlobal() const {
1463 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1464 return Method->isStatic();
1465
John McCall8e7d6562010-08-26 03:08:43 +00001466 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001467 return false;
1468
Mike Stump11289f42009-09-09 15:08:12 +00001469 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001470 DC->isNamespace();
1471 DC = DC->getParent()) {
1472 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1473 if (!Namespace->getDeclName())
1474 return false;
1475 break;
1476 }
1477 }
1478
1479 return true;
1480}
1481
Sebastian Redl833ef452010-01-26 22:01:41 +00001482void
1483FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1484 redeclarable_base::setPreviousDeclaration(PrevDecl);
1485
1486 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1487 FunctionTemplateDecl *PrevFunTmpl
1488 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1489 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1490 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1491 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001492
1493 if (PrevDecl->IsInline)
1494 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001495}
1496
1497const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1498 return getFirstDeclaration();
1499}
1500
1501FunctionDecl *FunctionDecl::getCanonicalDecl() {
1502 return getFirstDeclaration();
1503}
1504
Douglas Gregorbf62d642010-12-06 18:36:25 +00001505void FunctionDecl::setStorageClass(StorageClass SC) {
1506 assert(isLegalForFunction(SC));
1507 if (getStorageClass() != SC)
1508 ClearLinkageCache();
1509
1510 SClass = SC;
1511}
1512
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001513/// \brief Returns a value indicating whether this function
1514/// corresponds to a builtin function.
1515///
1516/// The function corresponds to a built-in function if it is
1517/// declared at translation scope or within an extern "C" block and
1518/// its name matches with the name of a builtin. The returned value
1519/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001520/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001521/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001522unsigned FunctionDecl::getBuiltinID() const {
1523 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001524 if (!getIdentifier() || !getIdentifier()->getBuiltinID())
1525 return 0;
1526
1527 unsigned BuiltinID = getIdentifier()->getBuiltinID();
1528 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1529 return BuiltinID;
1530
1531 // This function has the name of a known C library
1532 // function. Determine whether it actually refers to the C library
1533 // function or whether it just has the same name.
1534
Douglas Gregora908e7f2009-02-17 03:23:10 +00001535 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001536 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001537 return 0;
1538
Douglas Gregore711f702009-02-14 18:57:46 +00001539 // If this function is at translation-unit scope and we're not in
1540 // C++, it refers to the C library function.
1541 if (!Context.getLangOptions().CPlusPlus &&
1542 getDeclContext()->isTranslationUnit())
1543 return BuiltinID;
1544
1545 // If the function is in an extern "C" linkage specification and is
1546 // not marked "overloadable", it's the real function.
1547 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001548 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001549 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001550 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001551 return BuiltinID;
1552
1553 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001554 return 0;
1555}
1556
1557
Chris Lattner47c0d002009-04-25 06:03:53 +00001558/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001559/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001560/// after it has been created.
1561unsigned FunctionDecl::getNumParams() const {
John McCall9dd450b2009-09-21 23:43:11 +00001562 const FunctionType *FT = getType()->getAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001563 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001564 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001565 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001566
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001567}
1568
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001569void FunctionDecl::setParams(ASTContext &C,
1570 ParmVarDecl **NewParamInfo, unsigned NumParams) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001571 assert(ParamInfo == 0 && "Already has param info!");
Chris Lattner9af40c12009-04-25 06:12:16 +00001572 assert(NumParams == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001573
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001574 // Zero params -> null pointer.
1575 if (NumParams) {
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001576 void *Mem = C.Allocate(sizeof(ParmVarDecl*)*NumParams);
Ted Kremenek4ba36fc2009-01-14 00:42:25 +00001577 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
Chris Lattner53621a52007-06-13 20:44:40 +00001578 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001579
Argyrios Kyrtzidis53aeec32009-06-23 00:42:00 +00001580 // Update source range. The check below allows us to set EndRangeLoc before
1581 // setting the parameters.
Argyrios Kyrtzidisdfc5dca2009-06-23 00:42:15 +00001582 if (EndRangeLoc.isInvalid() || EndRangeLoc == getLocation())
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001583 EndRangeLoc = NewParamInfo[NumParams-1]->getLocEnd();
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001584 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001585}
Chris Lattner41943152007-01-25 04:52:46 +00001586
Chris Lattner58258242008-04-10 02:22:51 +00001587/// getMinRequiredArguments - Returns the minimum number of arguments
1588/// needed to call this function. This may be fewer than the number of
1589/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001590/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001591unsigned FunctionDecl::getMinRequiredArguments() const {
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001592 if (!getASTContext().getLangOptions().CPlusPlus)
1593 return getNumParams();
1594
Douglas Gregor7825bf32011-01-06 22:09:01 +00001595 unsigned NumRequiredArgs = getNumParams();
1596
1597 // If the last parameter is a parameter pack, we don't need an argument for
1598 // it.
1599 if (NumRequiredArgs > 0 &&
1600 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1601 --NumRequiredArgs;
1602
1603 // If this parameter has a default argument, we don't need an argument for
1604 // it.
1605 while (NumRequiredArgs > 0 &&
1606 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001607 --NumRequiredArgs;
1608
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001609 // We might have parameter packs before the end. These can't be deduced,
1610 // but they can still handle multiple arguments.
1611 unsigned ArgIdx = NumRequiredArgs;
1612 while (ArgIdx > 0) {
1613 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1614 NumRequiredArgs = ArgIdx;
1615
1616 --ArgIdx;
1617 }
1618
Chris Lattner58258242008-04-10 02:22:51 +00001619 return NumRequiredArgs;
1620}
1621
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001622bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001623 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001624 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001625
1626 if (isa<CXXMethodDecl>(this)) {
1627 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1628 return true;
1629 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001630
1631 switch (getTemplateSpecializationKind()) {
1632 case TSK_Undeclared:
1633 case TSK_ExplicitSpecialization:
1634 return false;
1635
1636 case TSK_ImplicitInstantiation:
1637 case TSK_ExplicitInstantiationDeclaration:
1638 case TSK_ExplicitInstantiationDefinition:
1639 // Handle below.
1640 break;
1641 }
1642
1643 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001644 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001645 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001646 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001647
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001648 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001649 return PatternDecl->isInlined();
1650
1651 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001652}
1653
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001654/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001655/// definition will be externally visible.
1656///
1657/// Inline function definitions are always available for inlining optimizations.
1658/// However, depending on the language dialect, declaration specifiers, and
1659/// attributes, the definition of an inline function may or may not be
1660/// "externally" visible to other translation units in the program.
1661///
1662/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00001663/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001664/// inline definition becomes externally visible (C99 6.7.4p6).
1665///
1666/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
1667/// definition, we use the GNU semantics for inline, which are nearly the
1668/// opposite of C99 semantics. In particular, "inline" by itself will create
1669/// an externally visible symbol, but "extern inline" will not create an
1670/// externally visible symbol.
1671bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
1672 assert(isThisDeclarationADefinition() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001673 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001674 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00001675
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001676 if (!Context.getLangOptions().C99 || hasAttr<GNUInlineAttr>()) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001677 // If it's not the case that both 'inline' and 'extern' are
1678 // specified on the definition, then this inline definition is
1679 // externally visible.
1680 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
1681 return true;
1682
1683 // If any declaration is 'inline' but not 'extern', then this definition
1684 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00001685 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1686 Redecl != RedeclEnd;
1687 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001688 if (Redecl->isInlineSpecified() &&
1689 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001690 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00001691 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00001692
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001693 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00001694 }
1695
1696 // C99 6.7.4p6:
1697 // [...] If all of the file scope declarations for a function in a
1698 // translation unit include the inline function specifier without extern,
1699 // then the definition in that translation unit is an inline definition.
1700 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
1701 Redecl != RedeclEnd;
1702 ++Redecl) {
1703 // Only consider file-scope declarations in this test.
1704 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1705 continue;
1706
John McCall8e7d6562010-08-26 03:08:43 +00001707 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00001708 return true; // Not an inline definition
1709 }
1710
1711 // C99 6.7.4p6:
1712 // An inline definition does not provide an external definition for the
1713 // function, and does not forbid an external definition in another
1714 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00001715 return false;
1716}
1717
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001718/// getOverloadedOperator - Which C++ overloaded operator this
1719/// function represents, if any.
1720OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00001721 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
1722 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00001723 else
1724 return OO_None;
1725}
1726
Alexis Huntc88db062010-01-13 09:01:02 +00001727/// getLiteralIdentifier - The literal suffix identifier this function
1728/// represents, if any.
1729const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
1730 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
1731 return getDeclName().getCXXLiteralIdentifier();
1732 else
1733 return 0;
1734}
1735
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00001736FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
1737 if (TemplateOrSpecialization.isNull())
1738 return TK_NonTemplate;
1739 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
1740 return TK_FunctionTemplate;
1741 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
1742 return TK_MemberSpecialization;
1743 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
1744 return TK_FunctionTemplateSpecialization;
1745 if (TemplateOrSpecialization.is
1746 <DependentFunctionTemplateSpecializationInfo*>())
1747 return TK_DependentFunctionTemplateSpecialization;
1748
1749 assert(false && "Did we miss a TemplateOrSpecialization type?");
1750 return TK_NonTemplate;
1751}
1752
Douglas Gregord801b062009-10-07 23:56:10 +00001753FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001754 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00001755 return cast<FunctionDecl>(Info->getInstantiatedFrom());
1756
1757 return 0;
1758}
1759
Douglas Gregor06db9f52009-10-12 20:18:28 +00001760MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
1761 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1762}
1763
Douglas Gregord801b062009-10-07 23:56:10 +00001764void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001765FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
1766 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00001767 TemplateSpecializationKind TSK) {
1768 assert(TemplateOrSpecialization.isNull() &&
1769 "Member function is already a specialization");
1770 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001771 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00001772 TemplateOrSpecialization = Info;
1773}
1774
Douglas Gregorafca3b42009-10-27 20:53:28 +00001775bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00001776 // If the function is invalid, it can't be implicitly instantiated.
1777 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00001778 return false;
1779
1780 switch (getTemplateSpecializationKind()) {
1781 case TSK_Undeclared:
1782 case TSK_ExplicitSpecialization:
1783 case TSK_ExplicitInstantiationDefinition:
1784 return false;
1785
1786 case TSK_ImplicitInstantiation:
1787 return true;
1788
1789 case TSK_ExplicitInstantiationDeclaration:
1790 // Handled below.
1791 break;
1792 }
1793
1794 // Find the actual template from which we will instantiate.
1795 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001796 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00001797 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001798 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00001799
1800 // C++0x [temp.explicit]p9:
1801 // Except for inline functions, other explicit instantiation declarations
1802 // have the effect of suppressing the implicit instantiation of the entity
1803 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001804 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00001805 return true;
1806
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001807 return PatternDecl->isInlined();
Douglas Gregorafca3b42009-10-27 20:53:28 +00001808}
1809
1810FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
1811 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
1812 while (Primary->getInstantiatedFromMemberTemplate()) {
1813 // If we have hit a point where the user provided a specialization of
1814 // this template, we're done looking.
1815 if (Primary->isMemberSpecialization())
1816 break;
1817
1818 Primary = Primary->getInstantiatedFromMemberTemplate();
1819 }
1820
1821 return Primary->getTemplatedDecl();
1822 }
1823
1824 return getInstantiatedFromMemberFunction();
1825}
1826
Douglas Gregor70d83e22009-06-29 17:30:29 +00001827FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00001828 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001829 = TemplateOrSpecialization
1830 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00001831 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00001832 }
1833 return 0;
1834}
1835
1836const TemplateArgumentList *
1837FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00001838 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00001839 = TemplateOrSpecialization
1840 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00001841 return Info->TemplateArguments;
1842 }
1843 return 0;
1844}
1845
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001846const TemplateArgumentListInfo *
1847FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
1848 if (FunctionTemplateSpecializationInfo *Info
1849 = TemplateOrSpecialization
1850 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
1851 return Info->TemplateArgumentsAsWritten;
1852 }
1853 return 0;
1854}
1855
Mike Stump11289f42009-09-09 15:08:12 +00001856void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001857FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
1858 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001859 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001860 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00001861 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00001862 const TemplateArgumentListInfo *TemplateArgsAsWritten,
1863 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001864 assert(TSK != TSK_Undeclared &&
1865 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00001866 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00001867 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001868 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00001869 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
1870 TemplateArgs,
1871 TemplateArgsAsWritten,
1872 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001873 TemplateOrSpecialization = Info;
Mike Stump11289f42009-09-09 15:08:12 +00001874
Douglas Gregor8f5d4422009-06-29 20:59:39 +00001875 // Insert this function template specialization into the set of known
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001876 // function template specializations.
1877 if (InsertPos)
1878 Template->getSpecializations().InsertNode(Info, InsertPos);
1879 else {
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001880 // Try to insert the new node. If there is an existing node, leave it, the
1881 // set will contain the canonical decls while
1882 // FunctionTemplateDecl::findSpecialization will return
1883 // the most recent redeclarations.
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001884 FunctionTemplateSpecializationInfo *Existing
1885 = Template->getSpecializations().GetOrInsertNode(Info);
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +00001886 (void)Existing;
1887 assert((!Existing || Existing->Function->isCanonicalDecl()) &&
1888 "Set is supposed to only contain canonical decls");
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00001889 }
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00001890}
1891
John McCallb9c78482010-04-08 09:05:18 +00001892void
1893FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
1894 const UnresolvedSetImpl &Templates,
1895 const TemplateArgumentListInfo &TemplateArgs) {
1896 assert(TemplateOrSpecialization.isNull());
1897 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
1898 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00001899 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00001900 void *Buffer = Context.Allocate(Size);
1901 DependentFunctionTemplateSpecializationInfo *Info =
1902 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
1903 TemplateArgs);
1904 TemplateOrSpecialization = Info;
1905}
1906
1907DependentFunctionTemplateSpecializationInfo::
1908DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
1909 const TemplateArgumentListInfo &TArgs)
1910 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
1911
1912 d.NumTemplates = Ts.size();
1913 d.NumArgs = TArgs.size();
1914
1915 FunctionTemplateDecl **TsArray =
1916 const_cast<FunctionTemplateDecl**>(getTemplates());
1917 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
1918 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
1919
1920 TemplateArgumentLoc *ArgsArray =
1921 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
1922 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
1923 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
1924}
1925
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001926TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00001927 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001928 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00001929 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00001930 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00001931 if (FTSInfo)
1932 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00001933
Douglas Gregord801b062009-10-07 23:56:10 +00001934 MemberSpecializationInfo *MSInfo
1935 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
1936 if (MSInfo)
1937 return MSInfo->getTemplateSpecializationKind();
1938
1939 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00001940}
1941
Mike Stump11289f42009-09-09 15:08:12 +00001942void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001943FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1944 SourceLocation PointOfInstantiation) {
1945 if (FunctionTemplateSpecializationInfo *FTSInfo
1946 = TemplateOrSpecialization.dyn_cast<
1947 FunctionTemplateSpecializationInfo*>()) {
1948 FTSInfo->setTemplateSpecializationKind(TSK);
1949 if (TSK != TSK_ExplicitSpecialization &&
1950 PointOfInstantiation.isValid() &&
1951 FTSInfo->getPointOfInstantiation().isInvalid())
1952 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
1953 } else if (MemberSpecializationInfo *MSInfo
1954 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
1955 MSInfo->setTemplateSpecializationKind(TSK);
1956 if (TSK != TSK_ExplicitSpecialization &&
1957 PointOfInstantiation.isValid() &&
1958 MSInfo->getPointOfInstantiation().isInvalid())
1959 MSInfo->setPointOfInstantiation(PointOfInstantiation);
1960 } else
1961 assert(false && "Function cannot have a template specialization kind");
1962}
1963
1964SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00001965 if (FunctionTemplateSpecializationInfo *FTSInfo
1966 = TemplateOrSpecialization.dyn_cast<
1967 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001968 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00001969 else if (MemberSpecializationInfo *MSInfo
1970 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001971 return MSInfo->getPointOfInstantiation();
1972
1973 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00001974}
1975
Douglas Gregor6411b922009-09-11 20:15:17 +00001976bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001977 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00001978 return true;
1979
1980 // If this function was instantiated from a member function of a
1981 // class template, check whether that member function was defined out-of-line.
1982 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
1983 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001984 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001985 return Definition->isOutOfLine();
1986 }
1987
1988 // If this function was instantiated from a function template,
1989 // check whether that function template was defined out-of-line.
1990 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
1991 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001992 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00001993 return Definition->isOutOfLine();
1994 }
1995
1996 return false;
1997}
1998
Abramo Bagnaraea947882011-03-08 16:41:52 +00001999SourceRange FunctionDecl::getSourceRange() const {
2000 return SourceRange(getOuterLocStart(), EndRangeLoc);
2001}
2002
Chris Lattner59a25942008-03-31 00:36:02 +00002003//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002004// FieldDecl Implementation
2005//===----------------------------------------------------------------------===//
2006
Jay Foad39c79802011-01-12 09:06:06 +00002007FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002008 SourceLocation StartLoc, SourceLocation IdLoc,
2009 IdentifierInfo *Id, QualType T,
Sebastian Redl833ef452010-01-26 22:01:41 +00002010 TypeSourceInfo *TInfo, Expr *BW, bool Mutable) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002011 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
2012 BW, Mutable);
Sebastian Redl833ef452010-01-26 22:01:41 +00002013}
2014
2015bool FieldDecl::isAnonymousStructOrUnion() const {
2016 if (!isImplicit() || getDeclName())
2017 return false;
2018
2019 if (const RecordType *Record = getType()->getAs<RecordType>())
2020 return Record->getDecl()->isAnonymousStructOrUnion();
2021
2022 return false;
2023}
2024
John McCall4e819612011-01-20 07:57:12 +00002025unsigned FieldDecl::getFieldIndex() const {
2026 if (CachedFieldIndex) return CachedFieldIndex - 1;
2027
2028 unsigned index = 0;
2029 RecordDecl::field_iterator
2030 i = getParent()->field_begin(), e = getParent()->field_end();
2031 while (true) {
2032 assert(i != e && "failed to find field in parent!");
2033 if (*i == this)
2034 break;
2035
2036 ++i;
2037 ++index;
2038 }
2039
2040 CachedFieldIndex = index + 1;
2041 return index;
2042}
2043
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002044SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraea947882011-03-08 16:41:52 +00002045 if (isBitField())
2046 return SourceRange(getInnerLocStart(), BitWidth->getLocEnd());
2047 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002048}
2049
Sebastian Redl833ef452010-01-26 22:01:41 +00002050//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002051// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002052//===----------------------------------------------------------------------===//
2053
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002054SourceLocation TagDecl::getOuterLocStart() const {
2055 return getTemplateOrInnerLocStart(this);
2056}
2057
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002058SourceRange TagDecl::getSourceRange() const {
2059 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002060 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002061}
2062
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002063TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002064 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002065}
2066
Douglas Gregora72a4e32010-05-19 18:39:18 +00002067void TagDecl::setTypedefForAnonDecl(TypedefDecl *TDD) {
2068 TypedefDeclOrQualifier = TDD;
2069 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002070 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002071 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002072}
2073
Douglas Gregordee1be82009-01-17 00:42:38 +00002074void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002075 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002076
2077 if (isa<CXXRecordDecl>(this)) {
2078 CXXRecordDecl *D = cast<CXXRecordDecl>(this);
2079 struct CXXRecordDecl::DefinitionData *Data =
2080 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002081 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2082 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002083 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002084}
2085
2086void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002087 assert((!isa<CXXRecordDecl>(this) ||
2088 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2089 "definition completed but not started");
2090
Douglas Gregordee1be82009-01-17 00:42:38 +00002091 IsDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002092 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002093
2094 if (ASTMutationListener *L = getASTMutationListener())
2095 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002096}
2097
Douglas Gregor0a5a2212010-02-11 01:04:33 +00002098TagDecl* TagDecl::getDefinition() const {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002099 if (isDefinition())
2100 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002101 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2102 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002103
2104 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002105 R != REnd; ++R)
2106 if (R->isDefinition())
2107 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002108
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002109 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002110}
2111
Douglas Gregor14454802011-02-25 02:25:35 +00002112void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2113 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002114 // Make sure the extended qualifier info is allocated.
2115 if (!hasExtInfo())
2116 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2117 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002118 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002119 }
2120 else {
2121 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002122 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002123 if (getExtInfo()->NumTemplParamLists == 0) {
2124 getASTContext().Deallocate(getExtInfo());
2125 TypedefDeclOrQualifier = (TypedefDecl*) 0;
2126 }
2127 else
2128 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002129 }
2130 }
2131}
2132
Abramo Bagnara60804e12011-03-18 15:16:37 +00002133void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2134 unsigned NumTPLists,
2135 TemplateParameterList **TPLists) {
2136 assert(NumTPLists > 0);
2137 // Make sure the extended decl info is allocated.
2138 if (!hasExtInfo())
2139 // Allocate external info struct.
2140 TypedefDeclOrQualifier = new (getASTContext()) ExtInfo;
2141 // Set the template parameter lists info.
2142 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2143}
2144
Ted Kremenek21475702008-09-05 17:16:31 +00002145//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002146// EnumDecl Implementation
2147//===----------------------------------------------------------------------===//
2148
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002149EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2150 SourceLocation StartLoc, SourceLocation IdLoc,
2151 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002152 EnumDecl *PrevDecl, bool IsScoped,
2153 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002154 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002155 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002156 C.getTypeDeclType(Enum, PrevDecl);
2157 return Enum;
2158}
2159
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002160EnumDecl *EnumDecl::Create(ASTContext &C, EmptyShell Empty) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002161 return new (C) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002162 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002163}
2164
Douglas Gregord5058122010-02-11 01:19:42 +00002165void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002166 QualType NewPromotionType,
2167 unsigned NumPositiveBits,
2168 unsigned NumNegativeBits) {
Sebastian Redl833ef452010-01-26 22:01:41 +00002169 assert(!isDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002170 if (!IntegerType)
2171 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002172 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002173 setNumPositiveBits(NumPositiveBits);
2174 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002175 TagDecl::completeDefinition();
2176}
2177
2178//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002179// RecordDecl Implementation
2180//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002181
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002182RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2183 SourceLocation StartLoc, SourceLocation IdLoc,
2184 IdentifierInfo *Id, RecordDecl *PrevDecl)
2185 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002186 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002187 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002188 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002189 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002190 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002191}
2192
Jay Foad39c79802011-01-12 09:06:06 +00002193RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002194 SourceLocation StartLoc, SourceLocation IdLoc,
2195 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2196 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2197 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002198 C.getTypeDeclType(R, PrevDecl);
2199 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002200}
2201
Jay Foad39c79802011-01-12 09:06:06 +00002202RecordDecl *RecordDecl::Create(const ASTContext &C, EmptyShell Empty) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002203 return new (C) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2204 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002205}
2206
Douglas Gregordfcad112009-03-25 15:59:44 +00002207bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002208 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002209 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2210}
2211
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002212RecordDecl::field_iterator RecordDecl::field_begin() const {
2213 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2214 LoadFieldsFromExternalStorage();
2215
2216 return field_iterator(decl_iterator(FirstDecl));
2217}
2218
Douglas Gregorb11aad82011-02-19 18:51:44 +00002219/// completeDefinition - Notes that the definition of this type is now
2220/// complete.
2221void RecordDecl::completeDefinition() {
2222 assert(!isDefinition() && "Cannot redefine record!");
2223 TagDecl::completeDefinition();
2224}
2225
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002226void RecordDecl::LoadFieldsFromExternalStorage() const {
2227 ExternalASTSource *Source = getASTContext().getExternalSource();
2228 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2229
2230 // Notify that we have a RecordDecl doing some initialization.
2231 ExternalASTSource::Deserializing TheFields(Source);
2232
2233 llvm::SmallVector<Decl*, 64> Decls;
2234 if (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls))
2235 return;
2236
2237#ifndef NDEBUG
2238 // Check that all decls we got were FieldDecls.
2239 for (unsigned i=0, e=Decls.size(); i != e; ++i)
2240 assert(isa<FieldDecl>(Decls[i]));
2241#endif
2242
2243 LoadedFieldsFromExternalStorage = true;
2244
2245 if (Decls.empty())
2246 return;
2247
2248 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls);
2249}
2250
Steve Naroff415d3d52008-10-08 17:01:13 +00002251//===----------------------------------------------------------------------===//
2252// BlockDecl Implementation
2253//===----------------------------------------------------------------------===//
2254
Douglas Gregord5058122010-02-11 01:19:42 +00002255void BlockDecl::setParams(ParmVarDecl **NewParamInfo,
Steve Naroffc4b30e52009-03-13 16:56:44 +00002256 unsigned NParms) {
2257 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002258
Steve Naroffc4b30e52009-03-13 16:56:44 +00002259 // Zero params -> null pointer.
2260 if (NParms) {
2261 NumParams = NParms;
Douglas Gregord5058122010-02-11 01:19:42 +00002262 void *Mem = getASTContext().Allocate(sizeof(ParmVarDecl*)*NumParams);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002263 ParamInfo = new (Mem) ParmVarDecl*[NumParams];
2264 memcpy(ParamInfo, NewParamInfo, sizeof(ParmVarDecl*)*NumParams);
2265 }
2266}
2267
John McCall351762c2011-02-07 10:33:21 +00002268void BlockDecl::setCaptures(ASTContext &Context,
2269 const Capture *begin,
2270 const Capture *end,
2271 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002272 CapturesCXXThis = capturesCXXThis;
2273
2274 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002275 NumCaptures = 0;
2276 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002277 return;
2278 }
2279
John McCall351762c2011-02-07 10:33:21 +00002280 NumCaptures = end - begin;
2281
2282 // Avoid new Capture[] because we don't want to provide a default
2283 // constructor.
2284 size_t allocationSize = NumCaptures * sizeof(Capture);
2285 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2286 memcpy(buffer, begin, allocationSize);
2287 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002288}
Sebastian Redl833ef452010-01-26 22:01:41 +00002289
Douglas Gregor70226da2010-12-21 16:27:07 +00002290SourceRange BlockDecl::getSourceRange() const {
2291 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2292}
Sebastian Redl833ef452010-01-26 22:01:41 +00002293
2294//===----------------------------------------------------------------------===//
2295// Other Decl Allocation/Deallocation Method Implementations
2296//===----------------------------------------------------------------------===//
2297
2298TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2299 return new (C) TranslationUnitDecl(C);
2300}
2301
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002302LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002303 SourceLocation IdentL, IdentifierInfo *II) {
2304 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2305}
2306
2307LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2308 SourceLocation IdentL, IdentifierInfo *II,
2309 SourceLocation GnuLabelL) {
2310 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2311 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002312}
2313
2314
Sebastian Redl833ef452010-01-26 22:01:41 +00002315NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab5545be2011-03-08 12:38:20 +00002316 SourceLocation StartLoc,
2317 SourceLocation IdLoc, IdentifierInfo *Id) {
2318 return new (C) NamespaceDecl(DC, StartLoc, IdLoc, Id);
Sebastian Redl833ef452010-01-26 22:01:41 +00002319}
2320
Douglas Gregor417e87c2010-10-27 19:49:05 +00002321NamespaceDecl *NamespaceDecl::getNextNamespace() {
2322 return dyn_cast_or_null<NamespaceDecl>(
2323 NextNamespace.get(getASTContext().getExternalSource()));
2324}
2325
Sebastian Redl833ef452010-01-26 22:01:41 +00002326ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002327 SourceLocation IdLoc,
2328 IdentifierInfo *Id,
2329 QualType Type) {
2330 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002331}
2332
2333FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002334 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002335 const DeclarationNameInfo &NameInfo,
2336 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002337 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002338 bool isInlineSpecified,
2339 bool hasWrittenPrototype) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002340 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2341 T, TInfo, SC, SCAsWritten,
2342 isInlineSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002343 New->HasWrittenPrototype = hasWrittenPrototype;
2344 return New;
2345}
2346
2347BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2348 return new (C) BlockDecl(DC, L);
2349}
2350
2351EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2352 SourceLocation L,
2353 IdentifierInfo *Id, QualType T,
2354 Expr *E, const llvm::APSInt &V) {
2355 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2356}
2357
Benjamin Kramer39593702010-11-21 14:11:41 +00002358IndirectFieldDecl *
2359IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2360 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2361 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002362 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2363}
2364
Douglas Gregorbe996932010-09-01 20:41:53 +00002365SourceRange EnumConstantDecl::getSourceRange() const {
2366 SourceLocation End = getLocation();
2367 if (Init)
2368 End = Init->getLocEnd();
2369 return SourceRange(getLocation(), End);
2370}
2371
Sebastian Redl833ef452010-01-26 22:01:41 +00002372TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002373 SourceLocation StartLoc, SourceLocation IdLoc,
2374 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2375 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002376}
2377
Abramo Bagnaraea947882011-03-08 16:41:52 +00002378SourceRange TypedefDecl::getSourceRange() const {
2379 SourceLocation RangeEnd = getLocation();
2380 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2381 if (typeIsPostfix(TInfo->getType()))
2382 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2383 }
2384 return SourceRange(getLocStart(), RangeEnd);
2385}
2386
Sebastian Redl833ef452010-01-26 22:01:41 +00002387FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002388 StringLiteral *Str,
2389 SourceLocation AsmLoc,
2390 SourceLocation RParenLoc) {
2391 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002392}