blob: 73c83fc66b7945578b45e88bb0fdba1e31c34249 [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"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000019#include "clang/AST/ASTMutationListener.h"
20#include "clang/AST/Attr.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"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000024#include "clang/AST/Stmt.h"
25#include "clang/AST/TypeLoc.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000027#include "clang/Basic/IdentifierTable.h"
Douglas Gregorba345522011-12-02 23:23:56 +000028#include "clang/Basic/Module.h"
Abramo Bagnara6150c882010-05-11 21:36:43 +000029#include "clang/Basic/Specifiers.h"
Douglas Gregor1baf38f2011-03-26 12:10:19 +000030#include "clang/Basic/TargetInfo.h"
John McCall06f6fe8d2009-09-04 01:14:41 +000031#include "llvm/Support/ErrorHandling.h"
Ted Kremenekce20e8f2008-05-20 00:43:19 +000032
David Blaikie9c70e042011-09-21 18:16:56 +000033#include <algorithm>
34
Chris Lattner6d9a6852006-10-25 05:11:20 +000035using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000036
Chris Lattner88f70d62008-03-15 05:43:15 +000037//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000038// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000039//===----------------------------------------------------------------------===//
40
Douglas Gregor1baf38f2011-03-26 12:10:19 +000041static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
42 // If this declaration has an explicit visibility attribute, use it.
43 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
44 switch (A->getVisibility()) {
45 case VisibilityAttr::Default:
46 return DefaultVisibility;
47 case VisibilityAttr::Hidden:
48 return HiddenVisibility;
49 case VisibilityAttr::Protected:
50 return ProtectedVisibility;
51 }
John McCall457a04e2010-10-22 21:05:15 +000052 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +000053
54 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
55 // implies visibility(default).
Douglas Gregore8bbc122011-09-02 00:18:52 +000056 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +000057 for (specific_attr_iterator<AvailabilityAttr>
58 A = D->specific_attr_begin<AvailabilityAttr>(),
59 AEnd = D->specific_attr_end<AvailabilityAttr>();
60 A != AEnd; ++A)
61 if ((*A)->getPlatform()->getName().equals("macosx"))
62 return DefaultVisibility;
63 }
64
65 return llvm::Optional<Visibility>();
John McCall457a04e2010-10-22 21:05:15 +000066}
67
John McCallc273f242010-10-30 11:50:40 +000068typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallc273f242010-10-30 11:50:40 +000069
Rafael Espindola2f869a32012-01-14 00:30:36 +000070static LinkageInfo getLVForType(QualType T) {
71 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
72 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
73}
74
Douglas Gregor7dc5c172010-02-03 09:33:45 +000075/// \brief Get the most restrictive linkage for the types in the given
76/// template parameter list.
Rafael Espindola2f869a32012-01-14 00:30:36 +000077static LinkageInfo
John McCall457a04e2010-10-22 21:05:15 +000078getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola2f869a32012-01-14 00:30:36 +000079 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000080 for (TemplateParameterList::const_iterator P = Params->begin(),
81 PEnd = Params->end();
82 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +000083 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
84 if (NTTP->isExpandedParameterPack()) {
85 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
86 QualType T = NTTP->getExpansionType(I);
87 if (!T->isDependentType())
Rafael Espindola2f869a32012-01-14 00:30:36 +000088 LV.merge(getLVForType(T));
Douglas Gregor0231d8d2011-01-19 20:10:05 +000089 }
90 continue;
91 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +000092
Douglas Gregor7dc5c172010-02-03 09:33:45 +000093 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +000094 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +000095 continue;
96 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +000097 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +000098
99 if (TemplateTemplateParmDecl *TTP
100 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000101 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000102 }
103 }
104
John McCall457a04e2010-10-22 21:05:15 +0000105 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000106}
107
Douglas Gregorbf62d642010-12-06 18:36:25 +0000108/// getLVForDecl - Get the linkage and visibility for the given declaration.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000109static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000110
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000111/// \brief Get the most restrictive linkage for the types and
112/// declarations in the given template argument list.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000113static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
114 unsigned NumArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000115 bool OnlyTemplate) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000116 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000117
118 for (unsigned I = 0; I != NumArgs; ++I) {
119 switch (Args[I].getKind()) {
120 case TemplateArgument::Null:
121 case TemplateArgument::Integral:
122 case TemplateArgument::Expression:
123 break;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000124
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000125 case TemplateArgument::Type:
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000126 LV.mergeWithMin(getLVForType(Args[I].getAsType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000127 break;
128
129 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +0000130 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
131 LV.mergeWithMin(getLVForDecl(ND, OnlyTemplate));
132 break;
133
134 case TemplateArgument::NullPtr:
135 LV.mergeWithMin(getLVForType(Args[I].getNullPtrType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000136 break;
137
138 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000139 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000140 if (TemplateDecl *Template
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000141 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000142 LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000143 break;
144
145 case TemplateArgument::Pack:
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000146 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
147 Args[I].pack_size(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000148 OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000149 break;
150 }
151 }
152
John McCall457a04e2010-10-22 21:05:15 +0000153 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000154}
155
Rafael Espindola2f869a32012-01-14 00:30:36 +0000156static LinkageInfo
Douglas Gregorbf62d642010-12-06 18:36:25 +0000157getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000158 bool OnlyTemplate) {
159 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate);
John McCall8823c652010-08-13 08:35:10 +0000160}
161
Rafael Espindola340941d2012-05-25 16:41:35 +0000162static bool shouldConsiderTemplateVis(const FunctionDecl *fn,
Rafael Espindola96dcb8d2012-05-21 20:31:27 +0000163 const FunctionTemplateSpecializationInfo *spec) {
164 return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000165}
166
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000167static bool
168shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola93c289c2012-05-21 20:15:56 +0000169 return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000170}
171
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000172static bool useInlineVisibilityHidden(const NamedDecl *D) {
173 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000174 const LangOptions &Opts = D->getASTContext().getLangOpts();
175 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000176 return false;
177
178 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
179 if (!FD)
180 return false;
181
182 TemplateSpecializationKind TSK = TSK_Undeclared;
183 if (FunctionTemplateSpecializationInfo *spec
184 = FD->getTemplateSpecializationInfo()) {
185 TSK = spec->getTemplateSpecializationKind();
186 } else if (MemberSpecializationInfo *MSI =
187 FD->getMemberSpecializationInfo()) {
188 TSK = MSI->getTemplateSpecializationKind();
189 }
190
191 const FunctionDecl *Def = 0;
192 // InlineVisibilityHidden only applies to definitions, and
193 // isInlined() only gives meaningful answers on definitions
194 // anyway.
195 return TSK != TSK_ExplicitInstantiationDeclaration &&
196 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000197 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000198}
199
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000200static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
201 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000202 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000203 "Not a name having namespace scope");
204 ASTContext &Context = D->getASTContext();
205
206 // C++ [basic.link]p3:
207 // A name having namespace scope (3.3.6) has internal linkage if it
208 // is the name of
209 // - an object, reference, function or function template that is
210 // explicitly declared static; or,
211 // (This bullet corresponds to C99 6.2.2p3.)
212 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
213 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000214 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000215 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000216
Richard Smithdc0ef452012-10-19 06:37:48 +0000217 // - a non-volatile object or reference that is explicitly declared const
218 // or constexpr and neither explicitly declared extern nor previously
219 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000220 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000221 Var->getType().isConstQualified() &&
222 !Var->getType().isVolatileQualified() &&
John McCall8e7d6562010-08-26 03:08:43 +0000223 Var->getStorageClass() != SC_Extern &&
224 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000225 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000226 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000227 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000228 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000229 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 FoundExtern = true;
231
232 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000233 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000234 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000235 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000236 const VarDecl *PrevVar = Var->getPreviousDecl();
237 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000238 if (PrevVar->getStorageClass() == SC_PrivateExtern)
239 break;
Eli Friedmana7137bc2012-10-26 23:05:34 +0000240 if (PrevVar)
241 return PrevVar->getLinkageAndVisibility();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000242 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000243 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000244 // C++ [temp]p4:
245 // A non-member function template can have internal linkage; any
246 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000247 const FunctionDecl *Function = 0;
248 if (const FunctionTemplateDecl *FunTmpl
249 = dyn_cast<FunctionTemplateDecl>(D))
250 Function = FunTmpl->getTemplatedDecl();
251 else
252 Function = cast<FunctionDecl>(D);
253
254 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000255 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000256 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000257 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
258 // - a data member of an anonymous union.
259 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000260 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000261 }
262
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000263 if (D->isInAnonymousNamespace()) {
264 const VarDecl *Var = dyn_cast<VarDecl>(D);
265 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Eli Friedman839192f2012-01-15 01:23:58 +0000266 if ((!Var || !Var->getDeclContext()->isExternCContext()) &&
267 (!Func || !Func->getDeclContext()->isExternCContext()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000268 return LinkageInfo::uniqueExternal();
269 }
John McCallb7139c42010-10-28 04:18:25 +0000270
John McCall457a04e2010-10-22 21:05:15 +0000271 // Set up the defaults.
272
273 // C99 6.2.2p5:
274 // If the declaration of an identifier for an object has file
275 // scope and no storage-class specifier, its linkage is
276 // external.
John McCallc273f242010-10-30 11:50:40 +0000277 LinkageInfo LV;
278
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000279 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000280 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000281 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000282 } else {
283 // If we're declared in a namespace with a visibility attribute,
284 // use that namespace's visibility, but don't call it explicit.
285 for (const DeclContext *DC = D->getDeclContext();
286 !isa<TranslationUnitDecl>(DC);
287 DC = DC->getParent()) {
288 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
289 if (!ND) continue;
290 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000291 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000292 break;
293 }
294 }
295 }
296 }
297
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000298 if (!OnlyTemplate) {
Rafael Espindolab660efd2012-04-19 04:37:16 +0000299 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000300 // If we're paying attention to global visibility, apply
301 // -finline-visibility-hidden if this is an inline method.
302 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
303 LV.mergeVisibility(HiddenVisibility, true);
304 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000305
Douglas Gregorf73b2822009-11-25 22:24:25 +0000306 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000307
Douglas Gregorf73b2822009-11-25 22:24:25 +0000308 // A name having namespace scope has external linkage if it is the
309 // name of
310 //
311 // - an object or reference, unless it has internal linkage; or
312 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000313 // GCC applies the following optimization to variables and static
314 // data members, but not to functions:
315 //
John McCall457a04e2010-10-22 21:05:15 +0000316 // Modify the variable's LV by the LV of its type unless this is
317 // C or extern "C". This follows from [basic.link]p9:
318 // A type without linkage shall not be used as the type of a
319 // variable or function with external linkage unless
320 // - the entity has C language linkage, or
321 // - the entity is declared within an unnamed namespace, or
322 // - the entity is not used or is defined in the same
323 // translation unit.
324 // and [basic.link]p10:
325 // ...the types specified by all declarations referring to a
326 // given variable or function shall be identical...
327 // C does not have an equivalent rule.
328 //
John McCall5fe84122010-10-26 04:59:26 +0000329 // Ignore this if we've got an explicit attribute; the user
330 // probably knows what they're doing.
331 //
John McCall457a04e2010-10-22 21:05:15 +0000332 // Note that we don't want to make the variable non-external
333 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000334 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000335 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000336 LinkageInfo TypeLV = getLVForType(Var->getType());
337 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000338 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000339 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000340 }
341
John McCall23032652010-11-02 18:38:13 +0000342 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000343 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000344
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000345 // Note that Sema::MergeVarDecl already takes care of implementing
346 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
347 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000348
Douglas Gregorf73b2822009-11-25 22:24:25 +0000349 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000350 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000351 // In theory, we can modify the function's LV by the LV of its
352 // type unless it has C linkage (see comment above about variables
353 // for justification). In practice, GCC doesn't do this, so it's
354 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000355
John McCall23032652010-11-02 18:38:13 +0000356 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000357 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000358
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000359 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
360 // merging storage classes and visibility attributes, so we don't have to
361 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000362
John McCallf768aa72011-02-10 06:50:24 +0000363 // In C++, then if the type of the function uses a type with
364 // unique-external linkage, it's not legally usable from outside
365 // this translation unit. However, we should use the C linkage
366 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000367 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000368 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000369 Function->getType()->getLinkage() == UniqueExternalLinkage)
370 return LinkageInfo::uniqueExternal();
371
John McCallb8c604a2011-06-27 23:06:04 +0000372 // Consider LV from the template and the template arguments unless
373 // this is an explicit specialization with a visibility attribute.
374 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000375 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola340941d2012-05-25 16:41:35 +0000376 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
377 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
378 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
379 OnlyTemplate);
380 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000381 LV.mergeWithMin(TempLV);
Rafael Espindola340941d2012-05-25 16:41:35 +0000382 LV.mergeWithMin(ArgsLV);
383 } else {
384 LV.mergeLinkage(TempLV);
385 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000386 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000387 }
388
Douglas Gregorf73b2822009-11-25 22:24:25 +0000389 // - a named class (Clause 9), or an unnamed class defined in a
390 // typedef declaration in which the class has the typedef name
391 // for linkage purposes (7.1.3); or
392 // - a named enumeration (7.2), or an unnamed enumeration
393 // defined in a typedef declaration in which the enumeration
394 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000395 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
396 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000397 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000398 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000399
John McCall457a04e2010-10-22 21:05:15 +0000400 // If this is a class template specialization, consider the
401 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000402 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000403 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000404 // From the template.
405 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000406
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000407 // The arguments at which the template was instantiated.
408 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
409 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
410 OnlyTemplate);
411 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000412 LV.mergeWithMin(TempLV);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000413 LV.mergeWithMin(ArgsLV);
414 } else {
415 LV.mergeLinkage(TempLV);
416 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000417 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000418 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000419
420 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000421 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000422 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
423 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000424 if (!isExternalLinkage(EnumLV.linkage()))
425 return LinkageInfo::none();
426 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000427
428 // - a template, unless it is a function template that has
429 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000430 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000431 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000432 // - a namespace (7.3), unless it is declared within an unnamed
433 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000434 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
435 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000436
John McCall457a04e2010-10-22 21:05:15 +0000437 // By extension, we assign external linkage to Objective-C
438 // interfaces.
439 } else if (isa<ObjCInterfaceDecl>(D)) {
440 // fallout
441
442 // Everything not covered here has no linkage.
443 } else {
John McCallc273f242010-10-30 11:50:40 +0000444 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000445 }
446
447 // If we ended up with non-external linkage, visibility should
448 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000449 if (LV.linkage() != ExternalLinkage)
450 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000451
John McCall457a04e2010-10-22 21:05:15 +0000452 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000453}
454
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000455static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000456 // Only certain class members have linkage. Note that fields don't
457 // really have linkage, but it's convenient to say they do for the
458 // purposes of calculating linkage of pointer-to-data-member
459 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000460 if (!(isa<CXXMethodDecl>(D) ||
461 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000462 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000463 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000464 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000465
John McCall07072662010-11-02 01:45:15 +0000466 LinkageInfo LV;
467
John McCall07072662010-11-02 01:45:15 +0000468 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000469 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000470 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000471 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000472 // If we're paying attention to global visibility, apply
473 // -finline-visibility-hidden if this is an inline method.
474 //
475 // Note that we do this before merging information about
476 // the class visibility.
477 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
478 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000479 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000480
481 // If this class member has an explicit visibility attribute, the only
482 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000483 // only look for them when processing the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000484 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000485
Rafael Espindola53cf2192012-04-19 05:50:08 +0000486 // If this member has an visibility attribute, ClassF will exclude
487 // attributes on the class or command line options, keeping only information
488 // about the template instantiation. If the member has no visibility
489 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
490 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000491 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
492 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000493 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000494 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000495
496 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000497 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000498 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000499
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000500 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000501 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000502
John McCall8823c652010-08-13 08:35:10 +0000503 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000504 // If the type of the function uses a type with unique-external
505 // linkage, it's not legally usable from outside this translation unit.
506 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
507 return LinkageInfo::uniqueExternal();
508
John McCall457a04e2010-10-22 21:05:15 +0000509 // If this is a method template specialization, use the linkage for
510 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000511 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000512 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000513 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
514 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
515 OnlyTemplate);
516 TemplateParameterList *TemplateParams =
517 spec->getTemplate()->getTemplateParameters();
518 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola340941d2012-05-25 16:41:35 +0000519 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000520 LV.mergeWithMin(ArgsLV);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000521 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000522 LV.mergeWithMin(ParamsLV);
Rafael Espindola67a498c2012-05-25 17:22:33 +0000523 } else {
524 LV.mergeLinkage(ArgsLV);
525 if (!OnlyTemplate)
526 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000527 }
John McCalle6e622e2010-11-01 01:29:57 +0000528 }
John McCall457a04e2010-10-22 21:05:15 +0000529
John McCall37bb6c92010-10-29 22:22:43 +0000530 // Note that in contrast to basically every other situation, we
531 // *do* apply -fvisibility to method declarations.
532
533 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000534 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000535 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000536 // Merge template argument/parameter information for member
537 // class template specializations.
538 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
539 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
540 OnlyTemplate);
541 TemplateParameterList *TemplateParams =
542 spec->getSpecializedTemplate()->getTemplateParameters();
543 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000544 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000545 LV.mergeWithMin(ArgsLV);
Rafael Espindola4d71d0f2012-05-25 14:17:45 +0000546 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000547 LV.mergeWithMin(ParamsLV);
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000548 } else {
549 LV.mergeLinkage(ArgsLV);
550 if (!OnlyTemplate)
551 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000552 }
John McCall37bb6c92010-10-29 22:22:43 +0000553 }
554
John McCall37bb6c92010-10-29 22:22:43 +0000555 // Static data members.
556 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000557 // Modify the variable's linkage by its type, but ignore the
558 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000559 LinkageInfo TypeLV = getLVForType(VD->getType());
560 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000561 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000562 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000563 }
564
John McCall457a04e2010-10-22 21:05:15 +0000565 return LV;
John McCall8823c652010-08-13 08:35:10 +0000566}
567
John McCalld396b972011-02-08 19:01:05 +0000568static void clearLinkageForClass(const CXXRecordDecl *record) {
569 for (CXXRecordDecl::decl_iterator
570 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
571 Decl *child = *i;
572 if (isa<NamedDecl>(child))
573 cast<NamedDecl>(child)->ClearLinkageCache();
574 }
575}
576
David Blaikie68e081d2011-12-20 02:48:34 +0000577void NamedDecl::anchor() { }
578
John McCalld396b972011-02-08 19:01:05 +0000579void NamedDecl::ClearLinkageCache() {
580 // Note that we can't skip clearing the linkage of children just
581 // because the parent doesn't have cached linkage: we don't cache
582 // when computing linkage for parent contexts.
583
584 HasCachedLinkage = 0;
585
586 // If we're changing the linkage of a class, we need to reset the
587 // linkage of child declarations, too.
588 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
589 clearLinkageForClass(record);
590
John McCall83779672011-02-19 02:53:41 +0000591 if (ClassTemplateDecl *temp =
592 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000593 // Clear linkage for the template pattern.
594 CXXRecordDecl *record = temp->getTemplatedDecl();
595 record->HasCachedLinkage = 0;
596 clearLinkageForClass(record);
597
John McCall83779672011-02-19 02:53:41 +0000598 // We need to clear linkage for specializations, too.
599 for (ClassTemplateDecl::spec_iterator
600 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
601 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000602 }
John McCall83779672011-02-19 02:53:41 +0000603
604 // Clear cached linkage for function template decls, too.
605 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000606 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
607 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000608 for (FunctionTemplateDecl::spec_iterator
609 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
610 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000611 }
John McCall83779672011-02-19 02:53:41 +0000612
John McCalld396b972011-02-08 19:01:05 +0000613}
614
Douglas Gregorbf62d642010-12-06 18:36:25 +0000615Linkage NamedDecl::getLinkage() const {
616 if (HasCachedLinkage) {
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000617 assert(Linkage(CachedLinkage) ==
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000618 getLVForDecl(this, true).linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000619 return Linkage(CachedLinkage);
620 }
621
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000622 CachedLinkage = getLVForDecl(this, true).linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000623 HasCachedLinkage = 1;
624 return Linkage(CachedLinkage);
625}
626
John McCallc273f242010-10-30 11:50:40 +0000627LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000628 LinkageInfo LI = getLVForDecl(this, false);
Benjamin Kramer87368ac2010-12-07 15:51:48 +0000629 assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage());
Douglas Gregorbf62d642010-12-06 18:36:25 +0000630 HasCachedLinkage = 1;
631 CachedLinkage = LI.linkage();
632 return LI;
John McCall033caa52010-10-29 00:29:13 +0000633}
Ted Kremenek926d8602010-04-20 23:15:35 +0000634
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000635llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
636 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000637 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000638 if (llvm::Optional<Visibility> V = getVisibilityOf(Var))
Rafael Espindola96e68242012-05-16 02:10:38 +0000639 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000640
Rafael Espindola96e68242012-05-16 02:10:38 +0000641 if (Var->isStaticDataMember()) {
642 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
643 if (InstantiatedFrom)
644 return getVisibilityOf(InstantiatedFrom);
645 }
646
647 return llvm::Optional<Visibility>();
648 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000649 // Use the most recent declaration of a function, and also handle
650 // function template specializations.
651 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000652 if (llvm::Optional<Visibility> V = getVisibilityOf(fn))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000653 return V;
654
655 // If the function is a specialization of a template with an
656 // explicit visibility attribute, use that.
657 if (FunctionTemplateSpecializationInfo *templateInfo
658 = fn->getTemplateSpecializationInfo())
659 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
660
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000661 // If the function is a member of a specialization of a class template
662 // and the corresponding decl has explicit visibility, use that.
663 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
664 if (InstantiatedFrom)
665 return getVisibilityOf(InstantiatedFrom);
666
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000667 return llvm::Optional<Visibility>();
668 }
669
670 // Otherwise, just check the declaration itself first.
671 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
672 return V;
673
Rafael Espindolafb4263f2012-07-31 19:02:02 +0000674 // The visibility of a template is stored in the templated decl.
675 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
676 return getVisibilityOf(TD->getTemplatedDecl());
677
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000678 // If there wasn't explicit visibility there, and this is a
679 // specialization of a class template, check for visibility
680 // on the pattern.
681 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolaeca5cd22012-07-13 01:19:08 +0000682 = dyn_cast<ClassTemplateSpecializationDecl>(this))
683 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000684
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000685 // If this is a member class of a specialization of a class template
686 // and the corresponding decl has explicit visibility, use that.
687 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
688 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
689 if (InstantiatedFrom)
690 return getVisibilityOf(InstantiatedFrom);
691 }
692
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000693 return llvm::Optional<Visibility>();
694}
695
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000696static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000697 // Objective-C: treat all Objective-C declarations as having external
698 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000699 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000700 default:
701 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000702 case Decl::ParmVar:
703 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000704 case Decl::TemplateTemplateParm: // count these as external
705 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000706 case Decl::ObjCAtDefsField:
707 case Decl::ObjCCategory:
708 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000709 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000710 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000711 case Decl::ObjCMethod:
712 case Decl::ObjCProperty:
713 case Decl::ObjCPropertyImpl:
714 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000715 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000716
717 case Decl::CXXRecord: {
718 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
719 if (Record->isLambda()) {
720 if (!Record->getLambdaManglingNumber()) {
721 // This lambda has no mangling number, so it's internal.
722 return LinkageInfo::internal();
723 }
724
725 // This lambda has its linkage/visibility determined by its owner.
726 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
727 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
728 if (isa<ParmVarDecl>(ContextDecl))
729 DC = ContextDecl->getDeclContext()->getRedeclContext();
730 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000731 return getLVForDecl(cast<NamedDecl>(ContextDecl),
732 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000733 }
734
735 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000736 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000737
738 return LinkageInfo::external();
739 }
740
741 break;
742 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000743 }
744
Douglas Gregorf73b2822009-11-25 22:24:25 +0000745 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000746 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000747 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000748
749 // C++ [basic.link]p5:
750 // In addition, a member function, static data member, a named
751 // class or enumeration of class scope, or an unnamed class or
752 // enumeration defined in a class-scope typedef declaration such
753 // that the class or enumeration has the typedef name for linkage
754 // purposes (7.1.3), has external linkage if the name of the class
755 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000756 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000757 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000758
759 // C++ [basic.link]p6:
760 // The name of a function declared in block scope and the name of
761 // an object declared by a block scope extern declaration have
762 // linkage. If there is a visible declaration of an entity with
763 // linkage having the same name and type, ignoring entities
764 // declared outside the innermost enclosing namespace scope, the
765 // block scope declaration declares that same entity and receives
766 // the linkage of the previous declaration. If there is more than
767 // one such matching entity, the program is ill-formed. Otherwise,
768 // if no matching entity is found, the block scope entity receives
769 // external linkage.
John McCall033caa52010-10-29 00:29:13 +0000770 if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
771 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000772 if (Function->isInAnonymousNamespace() &&
773 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000774 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000775
John McCallc273f242010-10-30 11:50:40 +0000776 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000777 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000778 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000779 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000780 }
Rafael Espindolac12edd42012-11-29 16:38:22 +0000781
782 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
783 // merging storage classes and visibility attributes, so we don't have to
784 // look at previous decls in here.
John McCall457a04e2010-10-22 21:05:15 +0000785
786 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000787 }
788
John McCall033caa52010-10-29 00:29:13 +0000789 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
John McCall8e7d6562010-08-26 03:08:43 +0000790 if (Var->getStorageClass() == SC_Extern ||
791 Var->getStorageClass() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000792 if (Var->isInAnonymousNamespace() &&
793 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000794 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000795
John McCallc273f242010-10-30 11:50:40 +0000796 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000797 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000798 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000799 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000800 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000801 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000802 }
John McCall457a04e2010-10-22 21:05:15 +0000803
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000804 // Note that Sema::MergeVarDecl already takes care of implementing
805 // C99 6.2.2p4 and propagating the visibility attribute, so we don't
806 // have to do it here.
John McCall457a04e2010-10-22 21:05:15 +0000807 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000808 }
809 }
810
811 // C++ [basic.link]p6:
812 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000813 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000814}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000815
Douglas Gregor2ada0482009-02-04 17:27:36 +0000816std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000817 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000818}
819
820std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000821 const DeclContext *Ctx = getDeclContext();
822
823 if (Ctx->isFunctionOrMethod())
824 return getNameAsString();
825
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000826 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000827 ContextsTy Contexts;
828
829 // Collect contexts.
830 while (Ctx && isa<NamedDecl>(Ctx)) {
831 Contexts.push_back(Ctx);
832 Ctx = Ctx->getParent();
833 };
834
835 std::string QualName;
836 llvm::raw_string_ostream OS(QualName);
837
838 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
839 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000840 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000841 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000842 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
843 std::string TemplateArgsStr
844 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000845 TemplateArgs.data(),
846 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000847 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000848 OS << Spec->getName() << TemplateArgsStr;
849 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000850 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000851 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000852 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000853 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000854 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
855 if (!RD->getIdentifier())
856 OS << "<anonymous " << RD->getKindName() << '>';
857 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000858 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000859 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000860 const FunctionProtoType *FT = 0;
861 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +0000862 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +0000863
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000864 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000865 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000866 unsigned NumParams = FD->getNumParams();
867 for (unsigned i = 0; i < NumParams; ++i) {
868 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000869 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000870 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000871 }
872
873 if (FT->isVariadic()) {
874 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000875 OS << ", ";
876 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000877 }
878 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000879 OS << ')';
880 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000881 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000882 }
883 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000884 }
885
John McCalla2a3f7d2010-03-16 21:48:18 +0000886 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000887 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000888 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000889 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000890
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000891 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000892}
893
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000894bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000895 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
896
Douglas Gregor889ceb72009-02-03 19:21:40 +0000897 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
898 // We want to keep it, unless it nominates same namespace.
899 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000900 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
901 ->getOriginalNamespace() ==
902 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
903 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000904 }
Mike Stump11289f42009-09-09 15:08:12 +0000905
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000906 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
907 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000908 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000909
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000910 // For function templates, the underlying function declarations are linked.
911 if (const FunctionTemplateDecl *FunctionTemplate
912 = dyn_cast<FunctionTemplateDecl>(this))
913 if (const FunctionTemplateDecl *OldFunctionTemplate
914 = dyn_cast<FunctionTemplateDecl>(OldD))
915 return FunctionTemplate->getTemplatedDecl()
916 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000917
Steve Naroffc4173fa2009-02-22 19:35:57 +0000918 // For method declarations, we keep track of redeclarations.
919 if (isa<ObjCMethodDecl>(this))
920 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000921
John McCall9f3059a2009-10-09 21:13:30 +0000922 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
923 return true;
924
John McCall3f746822009-11-17 05:59:44 +0000925 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
926 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
927 cast<UsingShadowDecl>(OldD)->getTargetDecl();
928
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000929 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
930 ASTContext &Context = getASTContext();
931 return Context.getCanonicalNestedNameSpecifier(
932 cast<UsingDecl>(this)->getQualifier()) ==
933 Context.getCanonicalNestedNameSpecifier(
934 cast<UsingDecl>(OldD)->getQualifier());
935 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000936
Douglas Gregorb59643b2012-01-03 23:26:26 +0000937 // A typedef of an Objective-C class type can replace an Objective-C class
938 // declaration or definition, and vice versa.
939 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
940 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
941 return true;
942
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000943 // For non-function declarations, if the declarations are of the
944 // same kind then this must be a redeclaration, or semantic analysis
945 // would not have given us the new declaration.
946 return this->getKind() == OldD->getKind();
947}
948
Douglas Gregoreddf4332009-02-24 20:03:32 +0000949bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000950 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000951}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000952
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000953NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000954 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000955 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
956 ND = UD->getTargetDecl();
957
958 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
959 return AD->getClassInterface();
960
961 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +0000962}
963
John McCalla8ae2222010-04-06 21:38:20 +0000964bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +0000965 if (!isCXXClassMember())
966 return false;
967
John McCalla8ae2222010-04-06 21:38:20 +0000968 const NamedDecl *D = this;
969 if (isa<UsingShadowDecl>(D))
970 D = cast<UsingShadowDecl>(D)->getTargetDecl();
971
Francois Pichet783dd6e2010-11-21 06:08:52 +0000972 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +0000973 return true;
974 if (isa<CXXMethodDecl>(D))
975 return cast<CXXMethodDecl>(D)->isInstance();
976 if (isa<FunctionTemplateDecl>(D))
977 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
978 ->getTemplatedDecl())->isInstance();
979 return false;
980}
981
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +0000982//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000983// DeclaratorDecl Implementation
984//===----------------------------------------------------------------------===//
985
Douglas Gregorec9c6ae2010-07-06 18:42:40 +0000986template <typename DeclT>
987static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
988 if (decl->getNumTemplateParameterLists() > 0)
989 return decl->getTemplateParameterList(0)->getTemplateLoc();
990 else
991 return decl->getInnerLocStart();
992}
993
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000994SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +0000995 TypeSourceInfo *TSI = getTypeSourceInfo();
996 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +0000997 return SourceLocation();
998}
999
Douglas Gregor14454802011-02-25 02:25:35 +00001000void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1001 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001002 // Make sure the extended decl info is allocated.
1003 if (!hasExtInfo()) {
1004 // Save (non-extended) type source info pointer.
1005 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1006 // Allocate external info struct.
1007 DeclInfo = new (getASTContext()) ExtInfo;
1008 // Restore savedTInfo into (extended) decl info.
1009 getExtInfo()->TInfo = savedTInfo;
1010 }
1011 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001012 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001013 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001014 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001015 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001016 if (getExtInfo()->NumTemplParamLists == 0) {
1017 // Save type source info pointer.
1018 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1019 // Deallocate the extended decl info.
1020 getASTContext().Deallocate(getExtInfo());
1021 // Restore savedTInfo into (non-extended) decl info.
1022 DeclInfo = savedTInfo;
1023 }
1024 else
1025 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001026 }
1027 }
1028}
1029
Abramo Bagnara60804e12011-03-18 15:16:37 +00001030void
1031DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1032 unsigned NumTPLists,
1033 TemplateParameterList **TPLists) {
1034 assert(NumTPLists > 0);
1035 // Make sure the extended decl info is allocated.
1036 if (!hasExtInfo()) {
1037 // Save (non-extended) type source info pointer.
1038 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1039 // Allocate external info struct.
1040 DeclInfo = new (getASTContext()) ExtInfo;
1041 // Restore savedTInfo into (extended) decl info.
1042 getExtInfo()->TInfo = savedTInfo;
1043 }
1044 // Set the template parameter lists info.
1045 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1046}
1047
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001048SourceLocation DeclaratorDecl::getOuterLocStart() const {
1049 return getTemplateOrInnerLocStart(this);
1050}
1051
Abramo Bagnaraea947882011-03-08 16:41:52 +00001052namespace {
1053
1054// Helper function: returns true if QT is or contains a type
1055// having a postfix component.
1056bool typeIsPostfix(clang::QualType QT) {
1057 while (true) {
1058 const Type* T = QT.getTypePtr();
1059 switch (T->getTypeClass()) {
1060 default:
1061 return false;
1062 case Type::Pointer:
1063 QT = cast<PointerType>(T)->getPointeeType();
1064 break;
1065 case Type::BlockPointer:
1066 QT = cast<BlockPointerType>(T)->getPointeeType();
1067 break;
1068 case Type::MemberPointer:
1069 QT = cast<MemberPointerType>(T)->getPointeeType();
1070 break;
1071 case Type::LValueReference:
1072 case Type::RValueReference:
1073 QT = cast<ReferenceType>(T)->getPointeeType();
1074 break;
1075 case Type::PackExpansion:
1076 QT = cast<PackExpansionType>(T)->getPattern();
1077 break;
1078 case Type::Paren:
1079 case Type::ConstantArray:
1080 case Type::DependentSizedArray:
1081 case Type::IncompleteArray:
1082 case Type::VariableArray:
1083 case Type::FunctionProto:
1084 case Type::FunctionNoProto:
1085 return true;
1086 }
1087 }
1088}
1089
1090} // namespace
1091
1092SourceRange DeclaratorDecl::getSourceRange() const {
1093 SourceLocation RangeEnd = getLocation();
1094 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1095 if (typeIsPostfix(TInfo->getType()))
1096 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1097 }
1098 return SourceRange(getOuterLocStart(), RangeEnd);
1099}
1100
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001101void
Douglas Gregor20527e22010-06-15 17:44:38 +00001102QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1103 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001104 TemplateParameterList **TPLists) {
1105 assert((NumTPLists == 0 || TPLists != 0) &&
1106 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001107
1108 // Free previous template parameters (if any).
1109 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001110 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001111 TemplParamLists = 0;
1112 NumTemplParamLists = 0;
1113 }
1114 // Set info on matched template parameter lists (if any).
1115 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001116 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001117 NumTemplParamLists = NumTPLists;
1118 for (unsigned i = NumTPLists; i-- > 0; )
1119 TemplParamLists[i] = TPLists[i];
1120 }
1121}
1122
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001123//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001124// VarDecl Implementation
1125//===----------------------------------------------------------------------===//
1126
Sebastian Redl833ef452010-01-26 22:01:41 +00001127const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1128 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001129 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001130 case SC_Auto: return "auto";
1131 case SC_Extern: return "extern";
1132 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1133 case SC_PrivateExtern: return "__private_extern__";
1134 case SC_Register: return "register";
1135 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001136 }
1137
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001138 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001139}
1140
Abramo Bagnaradff19302011-03-08 08:55:46 +00001141VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1142 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001143 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001144 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001145 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001146}
1147
Douglas Gregor72172e92012-01-05 21:55:30 +00001148VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1149 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1150 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1151 QualType(), 0, SC_None, SC_None);
1152}
1153
Douglas Gregorbf62d642010-12-06 18:36:25 +00001154void VarDecl::setStorageClass(StorageClass SC) {
1155 assert(isLegalForVariable(SC));
1156 if (getStorageClass() != SC)
1157 ClearLinkageCache();
1158
John McCallbeaa11c2011-05-01 02:13:58 +00001159 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001160}
1161
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001162SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001163 if (const Expr *Init = getInit()) {
1164 SourceLocation InitEnd = Init->getLocEnd();
1165 if (InitEnd.isValid())
1166 return SourceRange(getOuterLocStart(), InitEnd);
1167 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001168 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001169}
1170
Sebastian Redl833ef452010-01-26 22:01:41 +00001171bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001172 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001173 return false;
1174
Eli Friedman839192f2012-01-15 01:23:58 +00001175 const DeclContext *DC = getDeclContext();
1176 if (DC->isRecord())
1177 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001178
Eli Friedman839192f2012-01-15 01:23:58 +00001179 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001180 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001181 return true;
1182 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001183}
1184
1185VarDecl *VarDecl::getCanonicalDecl() {
1186 return getFirstDeclaration();
1187}
1188
Daniel Dunbar9d355812012-03-09 01:51:51 +00001189VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1190 ASTContext &C) const
1191{
Sebastian Redl35351a92010-01-31 22:27:38 +00001192 // C++ [basic.def]p2:
1193 // A declaration is a definition unless [...] it contains the 'extern'
1194 // specifier or a linkage-specification and neither an initializer [...],
1195 // it declares a static data member in a class declaration [...].
1196 // C++ [temp.expl.spec]p15:
1197 // An explicit specialization of a static data member of a template is a
1198 // definition if the declaration includes an initializer; otherwise, it is
1199 // a declaration.
1200 if (isStaticDataMember()) {
1201 if (isOutOfLine() && (hasInit() ||
1202 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1203 return Definition;
1204 else
1205 return DeclarationOnly;
1206 }
1207 // C99 6.7p5:
1208 // A definition of an identifier is a declaration for that identifier that
1209 // [...] causes storage to be reserved for that object.
1210 // Note: that applies for all non-file-scope objects.
1211 // C99 6.9.2p1:
1212 // If the declaration of an identifier for an object has file scope and an
1213 // initializer, the declaration is an external definition for the identifier
1214 if (hasInit())
1215 return Definition;
1216 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1217 if (hasExternalStorage())
1218 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001219
John McCall8e7d6562010-08-26 03:08:43 +00001220 if (getStorageClassAsWritten() == SC_Extern ||
1221 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001222 for (const VarDecl *PrevVar = getPreviousDecl();
1223 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001224 if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
1225 return DeclarationOnly;
1226 }
1227 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001228 // C99 6.9.2p2:
1229 // A declaration of an object that has file scope without an initializer,
1230 // and without a storage class specifier or the scs 'static', constitutes
1231 // a tentative definition.
1232 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001233 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001234 return TentativeDefinition;
1235
1236 // What's left is (in C, block-scope) declarations without initializers or
1237 // external storage. These are definitions.
1238 return Definition;
1239}
1240
Sebastian Redl35351a92010-01-31 22:27:38 +00001241VarDecl *VarDecl::getActingDefinition() {
1242 DefinitionKind Kind = isThisDeclarationADefinition();
1243 if (Kind != TentativeDefinition)
1244 return 0;
1245
Chris Lattner48eb14d2010-06-14 18:31:46 +00001246 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001247 VarDecl *First = getFirstDeclaration();
1248 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1249 I != E; ++I) {
1250 Kind = (*I)->isThisDeclarationADefinition();
1251 if (Kind == Definition)
1252 return 0;
1253 else if (Kind == TentativeDefinition)
1254 LastTentative = *I;
1255 }
1256 return LastTentative;
1257}
1258
1259bool VarDecl::isTentativeDefinitionNow() const {
1260 DefinitionKind Kind = isThisDeclarationADefinition();
1261 if (Kind != TentativeDefinition)
1262 return false;
1263
1264 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1265 if ((*I)->isThisDeclarationADefinition() == Definition)
1266 return false;
1267 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001268 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001269}
1270
Daniel Dunbar9d355812012-03-09 01:51:51 +00001271VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001272 VarDecl *First = getFirstDeclaration();
1273 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1274 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001275 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001276 return *I;
1277 }
1278 return 0;
1279}
1280
Daniel Dunbar9d355812012-03-09 01:51:51 +00001281VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001282 DefinitionKind Kind = DeclarationOnly;
1283
1284 const VarDecl *First = getFirstDeclaration();
1285 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001286 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001287 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001288 if (Kind == Definition)
1289 break;
1290 }
John McCall37bb6c92010-10-29 22:22:43 +00001291
1292 return Kind;
1293}
1294
Sebastian Redl5ca79842010-02-01 20:16:42 +00001295const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001296 redecl_iterator I = redecls_begin(), E = redecls_end();
1297 while (I != E && !I->getInit())
1298 ++I;
1299
1300 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001301 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001302 return I->getInit();
1303 }
1304 return 0;
1305}
1306
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001307bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001308 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001309 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001310
1311 if (!isStaticDataMember())
1312 return false;
1313
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001314 // If this static data member was instantiated from a static data member of
1315 // a class template, check whether that static data member was defined
1316 // out-of-line.
1317 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1318 return VD->isOutOfLine();
1319
1320 return false;
1321}
1322
Douglas Gregor1d957a32009-10-27 18:42:08 +00001323VarDecl *VarDecl::getOutOfLineDefinition() {
1324 if (!isStaticDataMember())
1325 return 0;
1326
1327 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1328 RD != RDEnd; ++RD) {
1329 if (RD->getLexicalDeclContext()->isFileContext())
1330 return *RD;
1331 }
1332
1333 return 0;
1334}
1335
Douglas Gregord5058122010-02-11 01:19:42 +00001336void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001337 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1338 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001339 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001340 }
1341
1342 Init = I;
1343}
1344
Daniel Dunbar9d355812012-03-09 01:51:51 +00001345bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001346 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001347
Richard Smith35ecb362012-03-02 04:14:40 +00001348 if (!Lang.CPlusPlus)
1349 return false;
1350
1351 // In C++11, any variable of reference type can be used in a constant
1352 // expression if it is initialized by a constant expression.
1353 if (Lang.CPlusPlus0x && getType()->isReferenceType())
1354 return true;
1355
1356 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001357 // not require the variable to be non-volatile, but we consider this to be a
1358 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001359 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001360 return false;
1361
1362 // In C++, const, non-volatile variables of integral or enumeration types
1363 // can be used in constant expressions.
1364 if (getType()->isIntegralOrEnumerationType())
1365 return true;
1366
Richard Smith35ecb362012-03-02 04:14:40 +00001367 // Additionally, in C++11, non-volatile constexpr variables can be used in
1368 // constant expressions.
1369 return Lang.CPlusPlus0x && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001370}
1371
Richard Smithd0b4dd62011-12-19 06:19:21 +00001372/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1373/// form, which contains extra information on the evaluated value of the
1374/// initializer.
1375EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1376 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1377 if (!Eval) {
1378 Stmt *S = Init.get<Stmt *>();
1379 Eval = new (getASTContext()) EvaluatedStmt;
1380 Eval->Value = S;
1381 Init = Eval;
1382 }
1383 return Eval;
1384}
1385
Richard Smithdafff942012-01-14 04:30:29 +00001386APValue *VarDecl::evaluateValue() const {
1387 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1388 return evaluateValue(Notes);
1389}
1390
1391APValue *VarDecl::evaluateValue(
1392 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001393 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1394
1395 // We only produce notes indicating why an initializer is non-constant the
1396 // first time it is evaluated. FIXME: The notes won't always be emitted the
1397 // first time we try evaluation, so might not be produced at all.
1398 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001399 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001400
1401 const Expr *Init = cast<Expr>(Eval->Value);
1402 assert(!Init->isValueDependent());
1403
1404 if (Eval->IsEvaluating) {
1405 // FIXME: Produce a diagnostic for self-initialization.
1406 Eval->CheckedICE = true;
1407 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001408 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001409 }
1410
1411 Eval->IsEvaluating = true;
1412
1413 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1414 this, Notes);
1415
1416 // Ensure the result is an uninitialized APValue if evaluation fails.
1417 if (!Result)
1418 Eval->Evaluated = APValue();
1419
1420 Eval->IsEvaluating = false;
1421 Eval->WasEvaluated = true;
1422
1423 // In C++11, we have determined whether the initializer was a constant
1424 // expression as a side-effect.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001425 if (getASTContext().getLangOpts().CPlusPlus0x && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001426 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001427 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001428 }
1429
Richard Smithdafff942012-01-14 04:30:29 +00001430 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001431}
1432
1433bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001434 // Initializers of weak variables are never ICEs.
1435 if (isWeak())
1436 return false;
1437
Richard Smithd0b4dd62011-12-19 06:19:21 +00001438 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1439 if (Eval->CheckedICE)
1440 // We have already checked whether this subexpression is an
1441 // integral constant expression.
1442 return Eval->IsICE;
1443
1444 const Expr *Init = cast<Expr>(Eval->Value);
1445 assert(!Init->isValueDependent());
1446
1447 // In C++11, evaluate the initializer to check whether it's a constant
1448 // expression.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001449 if (getASTContext().getLangOpts().CPlusPlus0x) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001450 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1451 evaluateValue(Notes);
1452 return Eval->IsICE;
1453 }
1454
1455 // It's an ICE whether or not the definition we found is
1456 // out-of-line. See DR 721 and the discussion in Clang PR
1457 // 6206 for details.
1458
1459 if (Eval->CheckingICE)
1460 return false;
1461 Eval->CheckingICE = true;
1462
1463 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1464 Eval->CheckingICE = false;
1465 Eval->CheckedICE = true;
1466 return Eval->IsICE;
1467}
1468
Douglas Gregorfe314812011-06-21 17:03:29 +00001469bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001470 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001471
1472 const Expr *E = getInit();
1473 if (!E)
1474 return false;
1475
1476 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1477 E = Cleanups->getSubExpr();
1478
1479 return isa<MaterializeTemporaryExpr>(E);
1480}
1481
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001482VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001483 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001484 return cast<VarDecl>(MSI->getInstantiatedFrom());
1485
1486 return 0;
1487}
1488
Douglas Gregor3c74d412009-10-14 20:14:33 +00001489TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001490 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001491 return MSI->getTemplateSpecializationKind();
1492
1493 return TSK_Undeclared;
1494}
1495
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001496MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001497 return getASTContext().getInstantiatedFromStaticDataMember(this);
1498}
1499
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001500void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1501 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001502 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001503 assert(MSI && "Not an instantiated static data member?");
1504 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001505 if (TSK != TSK_ExplicitSpecialization &&
1506 PointOfInstantiation.isValid() &&
1507 MSI->getPointOfInstantiation().isInvalid())
1508 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001509}
1510
Sebastian Redl833ef452010-01-26 22:01:41 +00001511//===----------------------------------------------------------------------===//
1512// ParmVarDecl Implementation
1513//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001514
Sebastian Redl833ef452010-01-26 22:01:41 +00001515ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001516 SourceLocation StartLoc,
1517 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001518 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001519 StorageClass S, StorageClass SCAsWritten,
1520 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001521 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001522 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001523}
1524
Douglas Gregor72172e92012-01-05 21:55:30 +00001525ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1526 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1527 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1528 0, QualType(), 0, SC_None, SC_None, 0);
1529}
1530
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001531SourceRange ParmVarDecl::getSourceRange() const {
1532 if (!hasInheritedDefaultArg()) {
1533 SourceRange ArgRange = getDefaultArgRange();
1534 if (ArgRange.isValid())
1535 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1536 }
1537
1538 return DeclaratorDecl::getSourceRange();
1539}
1540
Sebastian Redl833ef452010-01-26 22:01:41 +00001541Expr *ParmVarDecl::getDefaultArg() {
1542 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1543 assert(!hasUninstantiatedDefaultArg() &&
1544 "Default argument is not yet instantiated!");
1545
1546 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001547 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001548 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001549
Sebastian Redl833ef452010-01-26 22:01:41 +00001550 return Arg;
1551}
1552
Sebastian Redl833ef452010-01-26 22:01:41 +00001553SourceRange ParmVarDecl::getDefaultArgRange() const {
1554 if (const Expr *E = getInit())
1555 return E->getSourceRange();
1556
1557 if (hasUninstantiatedDefaultArg())
1558 return getUninstantiatedDefaultArg()->getSourceRange();
1559
1560 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001561}
1562
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001563bool ParmVarDecl::isParameterPack() const {
1564 return isa<PackExpansionType>(getType());
1565}
1566
Ted Kremenek540017e2011-10-06 05:00:56 +00001567void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1568 getASTContext().setParameterIndex(this, parameterIndex);
1569 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1570}
1571
1572unsigned ParmVarDecl::getParameterIndexLarge() const {
1573 return getASTContext().getParameterIndex(this);
1574}
1575
Nuno Lopes394ec982008-12-17 23:39:55 +00001576//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001577// FunctionDecl Implementation
1578//===----------------------------------------------------------------------===//
1579
Douglas Gregorb11aad82011-02-19 18:51:44 +00001580void FunctionDecl::getNameForDiagnostic(std::string &S,
1581 const PrintingPolicy &Policy,
1582 bool Qualified) const {
1583 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1584 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1585 if (TemplateArgs)
1586 S += TemplateSpecializationType::PrintTemplateArgumentList(
1587 TemplateArgs->data(),
1588 TemplateArgs->size(),
1589 Policy);
1590
1591}
1592
Ted Kremenek186a0742010-04-29 16:49:01 +00001593bool FunctionDecl::isVariadic() const {
1594 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1595 return FT->isVariadic();
1596 return false;
1597}
1598
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001599bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1600 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001601 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001602 Definition = *I;
1603 return true;
1604 }
1605 }
1606
1607 return false;
1608}
1609
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001610bool FunctionDecl::hasTrivialBody() const
1611{
1612 Stmt *S = getBody();
1613 if (!S) {
1614 // Since we don't have a body for this function, we don't know if it's
1615 // trivial or not.
1616 return false;
1617 }
1618
1619 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1620 return true;
1621 return false;
1622}
1623
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001624bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1625 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001626 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001627 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1628 return true;
1629 }
1630 }
1631
1632 return false;
1633}
1634
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001635Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001636 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1637 if (I->Body) {
1638 Definition = *I;
1639 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001640 } else if (I->IsLateTemplateParsed) {
1641 Definition = *I;
1642 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001643 }
1644 }
1645
1646 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001647}
1648
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001649void FunctionDecl::setBody(Stmt *B) {
1650 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001651 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001652 EndRangeLoc = B->getLocEnd();
1653}
1654
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001655void FunctionDecl::setPure(bool P) {
1656 IsPure = P;
1657 if (P)
1658 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1659 Parent->markedVirtualFunctionPure();
1660}
1661
Richard Smith12f247f2012-06-08 21:09:22 +00001662void FunctionDecl::setConstexpr(bool IC) {
1663 IsConstexpr = IC;
1664 CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(this);
1665 if (IC && CD)
1666 CD->getParent()->markedConstructorConstexpr(CD);
1667}
1668
Douglas Gregor16618f22009-09-12 00:17:51 +00001669bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001670 const TranslationUnitDecl *tunit =
1671 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1672 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001673 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001674 getIdentifier() &&
1675 getIdentifier()->isStr("main");
1676}
1677
1678bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1679 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1680 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1681 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1682 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1683 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1684
1685 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1686 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1687
1688 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1689 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1690
1691 ASTContext &Context =
1692 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1693 ->getASTContext();
1694
1695 // The result type and first argument type are constant across all
1696 // these operators. The second argument must be exactly void*.
1697 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001698}
1699
Douglas Gregor16618f22009-09-12 00:17:51 +00001700bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001701 if (getLinkage() != ExternalLinkage)
1702 return false;
1703
1704 if (getAttr<OverloadableAttr>())
1705 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001706
Chandler Carruth4322a282011-02-25 00:05:02 +00001707 const DeclContext *DC = getDeclContext();
1708 if (DC->isRecord())
1709 return false;
1710
Eli Friedman839192f2012-01-15 01:23:58 +00001711 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001712 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001713 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001714
Eli Friedman839192f2012-01-15 01:23:58 +00001715 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001716}
1717
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001718bool FunctionDecl::isGlobal() const {
1719 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1720 return Method->isStatic();
1721
John McCall8e7d6562010-08-26 03:08:43 +00001722 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001723 return false;
1724
Mike Stump11289f42009-09-09 15:08:12 +00001725 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001726 DC->isNamespace();
1727 DC = DC->getParent()) {
1728 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1729 if (!Namespace->getDeclName())
1730 return false;
1731 break;
1732 }
1733 }
1734
1735 return true;
1736}
1737
Sebastian Redl833ef452010-01-26 22:01:41 +00001738void
1739FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1740 redeclarable_base::setPreviousDeclaration(PrevDecl);
1741
1742 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1743 FunctionTemplateDecl *PrevFunTmpl
1744 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1745 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1746 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1747 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001748
Axel Naumannfbc7b982011-11-08 18:21:06 +00001749 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001750 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001751}
1752
1753const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1754 return getFirstDeclaration();
1755}
1756
1757FunctionDecl *FunctionDecl::getCanonicalDecl() {
1758 return getFirstDeclaration();
1759}
1760
Douglas Gregorbf62d642010-12-06 18:36:25 +00001761void FunctionDecl::setStorageClass(StorageClass SC) {
1762 assert(isLegalForFunction(SC));
1763 if (getStorageClass() != SC)
1764 ClearLinkageCache();
1765
1766 SClass = SC;
1767}
1768
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001769/// \brief Returns a value indicating whether this function
1770/// corresponds to a builtin function.
1771///
1772/// The function corresponds to a built-in function if it is
1773/// declared at translation scope or within an extern "C" block and
1774/// its name matches with the name of a builtin. The returned value
1775/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001776/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001777/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001778unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001779 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001780 return 0;
1781
1782 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001783 if (!BuiltinID)
1784 return 0;
1785
1786 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001787 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1788 return BuiltinID;
1789
1790 // This function has the name of a known C library
1791 // function. Determine whether it actually refers to the C library
1792 // function or whether it just has the same name.
1793
Douglas Gregora908e7f2009-02-17 03:23:10 +00001794 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001795 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001796 return 0;
1797
Douglas Gregore711f702009-02-14 18:57:46 +00001798 // If this function is at translation-unit scope and we're not in
1799 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001800 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001801 getDeclContext()->isTranslationUnit())
1802 return BuiltinID;
1803
1804 // If the function is in an extern "C" linkage specification and is
1805 // not marked "overloadable", it's the real function.
1806 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001807 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001808 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001809 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001810 return BuiltinID;
1811
1812 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001813 return 0;
1814}
1815
1816
Chris Lattner47c0d002009-04-25 06:03:53 +00001817/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001818/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001819/// after it has been created.
1820unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001821 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001822 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001823 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001824 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001825
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001826}
1827
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001828void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001829 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001830 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001831 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001832
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001833 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001834 if (!NewParamInfo.empty()) {
1835 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1836 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001837 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001838}
Chris Lattner41943152007-01-25 04:52:46 +00001839
James Molloy6f8780b2012-02-29 10:24:19 +00001840void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1841 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1842
1843 if (!NewDecls.empty()) {
1844 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1845 std::copy(NewDecls.begin(), NewDecls.end(), A);
1846 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1847 }
1848}
1849
Chris Lattner58258242008-04-10 02:22:51 +00001850/// getMinRequiredArguments - Returns the minimum number of arguments
1851/// needed to call this function. This may be fewer than the number of
1852/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001853/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001854unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001855 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001856 return getNumParams();
1857
Douglas Gregor7825bf32011-01-06 22:09:01 +00001858 unsigned NumRequiredArgs = getNumParams();
1859
1860 // If the last parameter is a parameter pack, we don't need an argument for
1861 // it.
1862 if (NumRequiredArgs > 0 &&
1863 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1864 --NumRequiredArgs;
1865
1866 // If this parameter has a default argument, we don't need an argument for
1867 // it.
1868 while (NumRequiredArgs > 0 &&
1869 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001870 --NumRequiredArgs;
1871
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001872 // We might have parameter packs before the end. These can't be deduced,
1873 // but they can still handle multiple arguments.
1874 unsigned ArgIdx = NumRequiredArgs;
1875 while (ArgIdx > 0) {
1876 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1877 NumRequiredArgs = ArgIdx;
1878
1879 --ArgIdx;
1880 }
1881
Chris Lattner58258242008-04-10 02:22:51 +00001882 return NumRequiredArgs;
1883}
1884
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001885bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001886 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001887 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001888
1889 if (isa<CXXMethodDecl>(this)) {
1890 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1891 return true;
1892 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001893
1894 switch (getTemplateSpecializationKind()) {
1895 case TSK_Undeclared:
1896 case TSK_ExplicitSpecialization:
1897 return false;
1898
1899 case TSK_ImplicitInstantiation:
1900 case TSK_ExplicitInstantiationDeclaration:
1901 case TSK_ExplicitInstantiationDefinition:
1902 // Handle below.
1903 break;
1904 }
1905
1906 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001907 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001908 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001909 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001910
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001911 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001912 return PatternDecl->isInlined();
1913
1914 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001915}
1916
Eli Friedman1b125c32012-02-07 03:50:18 +00001917static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1918 // Only consider file-scope declarations in this test.
1919 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1920 return false;
1921
1922 // Only consider explicit declarations; the presence of a builtin for a
1923 // libcall shouldn't affect whether a definition is externally visible.
1924 if (Redecl->isImplicit())
1925 return false;
1926
1927 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1928 return true; // Not an inline definition
1929
1930 return false;
1931}
1932
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001933/// \brief For a function declaration in C or C++, determine whether this
1934/// declaration causes the definition to be externally visible.
1935///
Eli Friedman1b125c32012-02-07 03:50:18 +00001936/// Specifically, this determines if adding the current declaration to the set
1937/// of redeclarations of the given functions causes
1938/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001939bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1940 assert(!doesThisDeclarationHaveABody() &&
1941 "Must have a declaration without a body.");
1942
1943 ASTContext &Context = getASTContext();
1944
David Blaikiebbafb8a2012-03-11 07:00:24 +00001945 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001946 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1947 // an externally visible definition.
1948 //
1949 // FIXME: What happens if gnu_inline gets added on after the first
1950 // declaration?
1951 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1952 return false;
1953
1954 const FunctionDecl *Prev = this;
1955 bool FoundBody = false;
1956 while ((Prev = Prev->getPreviousDecl())) {
1957 FoundBody |= Prev->Body;
1958
1959 if (Prev->Body) {
1960 // If it's not the case that both 'inline' and 'extern' are
1961 // specified on the definition, then it is always externally visible.
1962 if (!Prev->isInlineSpecified() ||
1963 Prev->getStorageClassAsWritten() != SC_Extern)
1964 return false;
1965 } else if (Prev->isInlineSpecified() &&
1966 Prev->getStorageClassAsWritten() != SC_Extern) {
1967 return false;
1968 }
1969 }
1970 return FoundBody;
1971 }
1972
David Blaikiebbafb8a2012-03-11 07:00:24 +00001973 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001974 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001975
1976 // C99 6.7.4p6:
1977 // [...] If all of the file scope declarations for a function in a
1978 // translation unit include the inline function specifier without extern,
1979 // then the definition in that translation unit is an inline definition.
1980 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001981 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00001982 const FunctionDecl *Prev = this;
1983 bool FoundBody = false;
1984 while ((Prev = Prev->getPreviousDecl())) {
1985 FoundBody |= Prev->Body;
1986 if (RedeclForcesDefC99(Prev))
1987 return false;
1988 }
1989 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001990}
1991
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001992/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00001993/// definition will be externally visible.
1994///
1995/// Inline function definitions are always available for inlining optimizations.
1996/// However, depending on the language dialect, declaration specifiers, and
1997/// attributes, the definition of an inline function may or may not be
1998/// "externally" visible to other translation units in the program.
1999///
2000/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002001/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002002/// inline definition becomes externally visible (C99 6.7.4p6).
2003///
2004/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2005/// definition, we use the GNU semantics for inline, which are nearly the
2006/// opposite of C99 semantics. In particular, "inline" by itself will create
2007/// an externally visible symbol, but "extern inline" will not create an
2008/// externally visible symbol.
2009bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002010 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002011 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002012 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002013
David Blaikiebbafb8a2012-03-11 07:00:24 +00002014 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002015 // Note: If you change the logic here, please change
2016 // doesDeclarationForceExternallyVisibleDefinition as well.
2017 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002018 // If it's not the case that both 'inline' and 'extern' are
2019 // specified on the definition, then this inline definition is
2020 // externally visible.
2021 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2022 return true;
2023
2024 // If any declaration is 'inline' but not 'extern', then this definition
2025 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002026 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2027 Redecl != RedeclEnd;
2028 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002029 if (Redecl->isInlineSpecified() &&
2030 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002031 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002032 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002033
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002034 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002035 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002036
Douglas Gregor299d76e2009-09-13 07:46:26 +00002037 // C99 6.7.4p6:
2038 // [...] If all of the file scope declarations for a function in a
2039 // translation unit include the inline function specifier without extern,
2040 // then the definition in that translation unit is an inline definition.
2041 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2042 Redecl != RedeclEnd;
2043 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002044 if (RedeclForcesDefC99(*Redecl))
2045 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002046 }
2047
2048 // C99 6.7.4p6:
2049 // An inline definition does not provide an external definition for the
2050 // function, and does not forbid an external definition in another
2051 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002052 return false;
2053}
2054
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002055/// getOverloadedOperator - Which C++ overloaded operator this
2056/// function represents, if any.
2057OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002058 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2059 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002060 else
2061 return OO_None;
2062}
2063
Alexis Huntc88db062010-01-13 09:01:02 +00002064/// getLiteralIdentifier - The literal suffix identifier this function
2065/// represents, if any.
2066const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2067 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2068 return getDeclName().getCXXLiteralIdentifier();
2069 else
2070 return 0;
2071}
2072
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002073FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2074 if (TemplateOrSpecialization.isNull())
2075 return TK_NonTemplate;
2076 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2077 return TK_FunctionTemplate;
2078 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2079 return TK_MemberSpecialization;
2080 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2081 return TK_FunctionTemplateSpecialization;
2082 if (TemplateOrSpecialization.is
2083 <DependentFunctionTemplateSpecializationInfo*>())
2084 return TK_DependentFunctionTemplateSpecialization;
2085
David Blaikie83d382b2011-09-23 05:06:16 +00002086 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002087}
2088
Douglas Gregord801b062009-10-07 23:56:10 +00002089FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002090 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002091 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2092
2093 return 0;
2094}
2095
Douglas Gregor06db9f52009-10-12 20:18:28 +00002096MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2097 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2098}
2099
Douglas Gregord801b062009-10-07 23:56:10 +00002100void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002101FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2102 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002103 TemplateSpecializationKind TSK) {
2104 assert(TemplateOrSpecialization.isNull() &&
2105 "Member function is already a specialization");
2106 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002107 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002108 TemplateOrSpecialization = Info;
2109}
2110
Douglas Gregorafca3b42009-10-27 20:53:28 +00002111bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002112 // If the function is invalid, it can't be implicitly instantiated.
2113 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002114 return false;
2115
2116 switch (getTemplateSpecializationKind()) {
2117 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002118 case TSK_ExplicitInstantiationDefinition:
2119 return false;
2120
2121 case TSK_ImplicitInstantiation:
2122 return true;
2123
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002124 // It is possible to instantiate TSK_ExplicitSpecialization kind
2125 // if the FunctionDecl has a class scope specialization pattern.
2126 case TSK_ExplicitSpecialization:
2127 return getClassScopeSpecializationPattern() != 0;
2128
Douglas Gregorafca3b42009-10-27 20:53:28 +00002129 case TSK_ExplicitInstantiationDeclaration:
2130 // Handled below.
2131 break;
2132 }
2133
2134 // Find the actual template from which we will instantiate.
2135 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002136 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002137 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002138 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002139
2140 // C++0x [temp.explicit]p9:
2141 // Except for inline functions, other explicit instantiation declarations
2142 // have the effect of suppressing the implicit instantiation of the entity
2143 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002144 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002145 return true;
2146
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002147 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002148}
2149
2150bool FunctionDecl::isTemplateInstantiation() const {
2151 switch (getTemplateSpecializationKind()) {
2152 case TSK_Undeclared:
2153 case TSK_ExplicitSpecialization:
2154 return false;
2155 case TSK_ImplicitInstantiation:
2156 case TSK_ExplicitInstantiationDeclaration:
2157 case TSK_ExplicitInstantiationDefinition:
2158 return true;
2159 }
2160 llvm_unreachable("All TSK values handled.");
2161}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002162
2163FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002164 // Handle class scope explicit specialization special case.
2165 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2166 return getClassScopeSpecializationPattern();
2167
Douglas Gregorafca3b42009-10-27 20:53:28 +00002168 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2169 while (Primary->getInstantiatedFromMemberTemplate()) {
2170 // If we have hit a point where the user provided a specialization of
2171 // this template, we're done looking.
2172 if (Primary->isMemberSpecialization())
2173 break;
2174
2175 Primary = Primary->getInstantiatedFromMemberTemplate();
2176 }
2177
2178 return Primary->getTemplatedDecl();
2179 }
2180
2181 return getInstantiatedFromMemberFunction();
2182}
2183
Douglas Gregor70d83e22009-06-29 17:30:29 +00002184FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002185 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002186 = TemplateOrSpecialization
2187 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002188 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002189 }
2190 return 0;
2191}
2192
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002193FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2194 return getASTContext().getClassScopeSpecializationPattern(this);
2195}
2196
Douglas Gregor70d83e22009-06-29 17:30:29 +00002197const TemplateArgumentList *
2198FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002199 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002200 = TemplateOrSpecialization
2201 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002202 return Info->TemplateArguments;
2203 }
2204 return 0;
2205}
2206
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002207const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002208FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2209 if (FunctionTemplateSpecializationInfo *Info
2210 = TemplateOrSpecialization
2211 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2212 return Info->TemplateArgumentsAsWritten;
2213 }
2214 return 0;
2215}
2216
Mike Stump11289f42009-09-09 15:08:12 +00002217void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002218FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2219 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002220 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002221 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002222 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002223 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2224 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002225 assert(TSK != TSK_Undeclared &&
2226 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002227 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002228 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002229 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002230 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2231 TemplateArgs,
2232 TemplateArgsAsWritten,
2233 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002234 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002235 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002236}
2237
John McCallb9c78482010-04-08 09:05:18 +00002238void
2239FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2240 const UnresolvedSetImpl &Templates,
2241 const TemplateArgumentListInfo &TemplateArgs) {
2242 assert(TemplateOrSpecialization.isNull());
2243 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2244 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002245 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002246 void *Buffer = Context.Allocate(Size);
2247 DependentFunctionTemplateSpecializationInfo *Info =
2248 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2249 TemplateArgs);
2250 TemplateOrSpecialization = Info;
2251}
2252
2253DependentFunctionTemplateSpecializationInfo::
2254DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2255 const TemplateArgumentListInfo &TArgs)
2256 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2257
2258 d.NumTemplates = Ts.size();
2259 d.NumArgs = TArgs.size();
2260
2261 FunctionTemplateDecl **TsArray =
2262 const_cast<FunctionTemplateDecl**>(getTemplates());
2263 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2264 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2265
2266 TemplateArgumentLoc *ArgsArray =
2267 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2268 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2269 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2270}
2271
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002272TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002273 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002274 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002275 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002276 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002277 if (FTSInfo)
2278 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002279
Douglas Gregord801b062009-10-07 23:56:10 +00002280 MemberSpecializationInfo *MSInfo
2281 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2282 if (MSInfo)
2283 return MSInfo->getTemplateSpecializationKind();
2284
2285 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002286}
2287
Mike Stump11289f42009-09-09 15:08:12 +00002288void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002289FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2290 SourceLocation PointOfInstantiation) {
2291 if (FunctionTemplateSpecializationInfo *FTSInfo
2292 = TemplateOrSpecialization.dyn_cast<
2293 FunctionTemplateSpecializationInfo*>()) {
2294 FTSInfo->setTemplateSpecializationKind(TSK);
2295 if (TSK != TSK_ExplicitSpecialization &&
2296 PointOfInstantiation.isValid() &&
2297 FTSInfo->getPointOfInstantiation().isInvalid())
2298 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2299 } else if (MemberSpecializationInfo *MSInfo
2300 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2301 MSInfo->setTemplateSpecializationKind(TSK);
2302 if (TSK != TSK_ExplicitSpecialization &&
2303 PointOfInstantiation.isValid() &&
2304 MSInfo->getPointOfInstantiation().isInvalid())
2305 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2306 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002307 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002308}
2309
2310SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002311 if (FunctionTemplateSpecializationInfo *FTSInfo
2312 = TemplateOrSpecialization.dyn_cast<
2313 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002314 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002315 else if (MemberSpecializationInfo *MSInfo
2316 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002317 return MSInfo->getPointOfInstantiation();
2318
2319 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002320}
2321
Douglas Gregor6411b922009-09-11 20:15:17 +00002322bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002323 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002324 return true;
2325
2326 // If this function was instantiated from a member function of a
2327 // class template, check whether that member function was defined out-of-line.
2328 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2329 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002330 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002331 return Definition->isOutOfLine();
2332 }
2333
2334 // If this function was instantiated from a function template,
2335 // check whether that function template was defined out-of-line.
2336 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2337 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002338 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002339 return Definition->isOutOfLine();
2340 }
2341
2342 return false;
2343}
2344
Abramo Bagnaraea947882011-03-08 16:41:52 +00002345SourceRange FunctionDecl::getSourceRange() const {
2346 return SourceRange(getOuterLocStart(), EndRangeLoc);
2347}
2348
Anna Zaks28db7ce2012-01-18 02:45:01 +00002349unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002350 IdentifierInfo *FnInfo = getIdentifier();
2351
2352 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002353 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002354
2355 // Builtin handling.
2356 switch (getBuiltinID()) {
2357 case Builtin::BI__builtin_memset:
2358 case Builtin::BI__builtin___memset_chk:
2359 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002360 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002361
2362 case Builtin::BI__builtin_memcpy:
2363 case Builtin::BI__builtin___memcpy_chk:
2364 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002365 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002366
2367 case Builtin::BI__builtin_memmove:
2368 case Builtin::BI__builtin___memmove_chk:
2369 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002370 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002371
2372 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002373 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002374 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002375 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002376
2377 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002378 case Builtin::BImemcmp:
2379 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002380
2381 case Builtin::BI__builtin_strncpy:
2382 case Builtin::BI__builtin___strncpy_chk:
2383 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002384 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002385
2386 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002387 case Builtin::BIstrncmp:
2388 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002389
2390 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002391 case Builtin::BIstrncasecmp:
2392 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002393
2394 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002395 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002396 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002397 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002398
2399 case Builtin::BI__builtin_strndup:
2400 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002401 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002402
Anna Zaks314cd092012-02-01 19:08:57 +00002403 case Builtin::BI__builtin_strlen:
2404 case Builtin::BIstrlen:
2405 return Builtin::BIstrlen;
2406
Anna Zaks201d4892012-01-13 21:52:01 +00002407 default:
Eli Friedman839192f2012-01-15 01:23:58 +00002408 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002409 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002410 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002411 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002412 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002413 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002414 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002415 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002416 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002417 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002418 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002419 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002420 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002421 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002422 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002423 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002424 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002425 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002426 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002427 else if (FnInfo->isStr("strlen"))
2428 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002429 }
2430 break;
2431 }
Anna Zaks22122702012-01-17 00:37:07 +00002432 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002433}
2434
Chris Lattner59a25942008-03-31 00:36:02 +00002435//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002436// FieldDecl Implementation
2437//===----------------------------------------------------------------------===//
2438
Jay Foad39c79802011-01-12 09:06:06 +00002439FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002440 SourceLocation StartLoc, SourceLocation IdLoc,
2441 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002442 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002443 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002444 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002445 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002446}
2447
Douglas Gregor72172e92012-01-05 21:55:30 +00002448FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2449 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2450 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002451 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002452}
2453
Sebastian Redl833ef452010-01-26 22:01:41 +00002454bool FieldDecl::isAnonymousStructOrUnion() const {
2455 if (!isImplicit() || getDeclName())
2456 return false;
2457
2458 if (const RecordType *Record = getType()->getAs<RecordType>())
2459 return Record->getDecl()->isAnonymousStructOrUnion();
2460
2461 return false;
2462}
2463
Richard Smithcaf33902011-10-10 18:28:20 +00002464unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2465 assert(isBitField() && "not a bitfield");
2466 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2467 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2468}
2469
John McCall4e819612011-01-20 07:57:12 +00002470unsigned FieldDecl::getFieldIndex() const {
2471 if (CachedFieldIndex) return CachedFieldIndex - 1;
2472
Richard Smithd62306a2011-11-10 06:34:14 +00002473 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002474 const RecordDecl *RD = getParent();
2475 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002476 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002477
2478 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2479 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002480 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002481
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002482 if (IsMsStruct) {
2483 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002484 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002485 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002486 continue;
2487 }
David Blaikie40ed2972012-06-06 20:45:41 +00002488 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002489 }
John McCall4e819612011-01-20 07:57:12 +00002490 }
2491
Richard Smithd62306a2011-11-10 06:34:14 +00002492 assert(CachedFieldIndex && "failed to find field in parent");
2493 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002494}
2495
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002496SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002497 if (const Expr *E = InitializerOrBitWidth.getPointer())
2498 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002499 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002500}
2501
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002502void FieldDecl::setBitWidth(Expr *Width) {
2503 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2504 "bit width or initializer already set");
2505 InitializerOrBitWidth.setPointer(Width);
2506}
2507
Richard Smith938f40b2011-06-11 17:19:42 +00002508void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002509 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002510 "bit width or initializer already set");
2511 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002512}
2513
Sebastian Redl833ef452010-01-26 22:01:41 +00002514//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002515// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002516//===----------------------------------------------------------------------===//
2517
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002518SourceLocation TagDecl::getOuterLocStart() const {
2519 return getTemplateOrInnerLocStart(this);
2520}
2521
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002522SourceRange TagDecl::getSourceRange() const {
2523 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002524 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002525}
2526
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002527TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002528 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002529}
2530
Richard Smithdda56e42011-04-15 14:24:37 +00002531void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2532 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002533 if (TypeForDecl)
John McCall424cec92011-01-19 06:33:43 +00002534 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00002535 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002536}
2537
Douglas Gregordee1be82009-01-17 00:42:38 +00002538void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002539 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002540
David Blaikie095deba2012-11-14 01:52:05 +00002541 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002542 struct CXXRecordDecl::DefinitionData *Data =
2543 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002544 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2545 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002546 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002547}
2548
2549void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002550 assert((!isa<CXXRecordDecl>(this) ||
2551 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2552 "definition completed but not started");
2553
John McCallf937c022011-10-07 06:10:15 +00002554 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002555 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002556
2557 if (ASTMutationListener *L = getASTMutationListener())
2558 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002559}
2560
John McCallf937c022011-10-07 06:10:15 +00002561TagDecl *TagDecl::getDefinition() const {
2562 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002563 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002564 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2565 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002566
2567 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002568 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002569 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002570 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002571
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002572 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002573}
2574
Douglas Gregor14454802011-02-25 02:25:35 +00002575void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2576 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002577 // Make sure the extended qualifier info is allocated.
2578 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002579 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002580 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002581 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002582 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002583 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002584 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002585 if (getExtInfo()->NumTemplParamLists == 0) {
2586 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002587 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002588 }
2589 else
2590 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002591 }
2592 }
2593}
2594
Abramo Bagnara60804e12011-03-18 15:16:37 +00002595void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2596 unsigned NumTPLists,
2597 TemplateParameterList **TPLists) {
2598 assert(NumTPLists > 0);
2599 // Make sure the extended decl info is allocated.
2600 if (!hasExtInfo())
2601 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002602 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002603 // Set the template parameter lists info.
2604 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2605}
2606
Ted Kremenek21475702008-09-05 17:16:31 +00002607//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002608// EnumDecl Implementation
2609//===----------------------------------------------------------------------===//
2610
David Blaikie68e081d2011-12-20 02:48:34 +00002611void EnumDecl::anchor() { }
2612
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002613EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2614 SourceLocation StartLoc, SourceLocation IdLoc,
2615 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002616 EnumDecl *PrevDecl, bool IsScoped,
2617 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002618 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002619 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002620 C.getTypeDeclType(Enum, PrevDecl);
2621 return Enum;
2622}
2623
Douglas Gregor72172e92012-01-05 21:55:30 +00002624EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2625 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2626 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2627 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002628}
2629
Douglas Gregord5058122010-02-11 01:19:42 +00002630void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002631 QualType NewPromotionType,
2632 unsigned NumPositiveBits,
2633 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002634 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002635 if (!IntegerType)
2636 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002637 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002638 setNumPositiveBits(NumPositiveBits);
2639 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002640 TagDecl::completeDefinition();
2641}
2642
Richard Smith7d137e32012-03-23 03:33:32 +00002643TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2644 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2645 return MSI->getTemplateSpecializationKind();
2646
2647 return TSK_Undeclared;
2648}
2649
2650void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2651 SourceLocation PointOfInstantiation) {
2652 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2653 assert(MSI && "Not an instantiated member enumeration?");
2654 MSI->setTemplateSpecializationKind(TSK);
2655 if (TSK != TSK_ExplicitSpecialization &&
2656 PointOfInstantiation.isValid() &&
2657 MSI->getPointOfInstantiation().isInvalid())
2658 MSI->setPointOfInstantiation(PointOfInstantiation);
2659}
2660
Richard Smith4b38ded2012-03-14 23:13:10 +00002661EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2662 if (SpecializationInfo)
2663 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2664
2665 return 0;
2666}
2667
2668void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2669 TemplateSpecializationKind TSK) {
2670 assert(!SpecializationInfo && "Member enum is already a specialization");
2671 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2672}
2673
Sebastian Redl833ef452010-01-26 22:01:41 +00002674//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002675// RecordDecl Implementation
2676//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002677
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002678RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2679 SourceLocation StartLoc, SourceLocation IdLoc,
2680 IdentifierInfo *Id, RecordDecl *PrevDecl)
2681 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002682 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002683 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002684 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002685 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002686 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002687}
2688
Jay Foad39c79802011-01-12 09:06:06 +00002689RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002690 SourceLocation StartLoc, SourceLocation IdLoc,
2691 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2692 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2693 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002694 C.getTypeDeclType(R, PrevDecl);
2695 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002696}
2697
Douglas Gregor72172e92012-01-05 21:55:30 +00002698RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2699 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2700 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2701 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002702}
2703
Douglas Gregordfcad112009-03-25 15:59:44 +00002704bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002705 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002706 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2707}
2708
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002709RecordDecl::field_iterator RecordDecl::field_begin() const {
2710 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2711 LoadFieldsFromExternalStorage();
2712
2713 return field_iterator(decl_iterator(FirstDecl));
2714}
2715
Douglas Gregorb11aad82011-02-19 18:51:44 +00002716/// completeDefinition - Notes that the definition of this type is now
2717/// complete.
2718void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002719 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002720 TagDecl::completeDefinition();
2721}
2722
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002723/// isMsStruct - Get whether or not this record uses ms_struct layout.
2724/// This which can be turned on with an attribute, pragma, or the
2725/// -mms-bitfields command-line option.
2726bool RecordDecl::isMsStruct(const ASTContext &C) const {
2727 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2728}
2729
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002730static bool isFieldOrIndirectField(Decl::Kind K) {
2731 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2732}
2733
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002734void RecordDecl::LoadFieldsFromExternalStorage() const {
2735 ExternalASTSource *Source = getASTContext().getExternalSource();
2736 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2737
2738 // Notify that we have a RecordDecl doing some initialization.
2739 ExternalASTSource::Deserializing TheFields(Source);
2740
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002741 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002742 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002743 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2744 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002745 case ELR_Success:
2746 break;
2747
2748 case ELR_AlreadyLoaded:
2749 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002750 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002751 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002752
2753#ifndef NDEBUG
2754 // Check that all decls we got were FieldDecls.
2755 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002756 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002757#endif
2758
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002759 if (Decls.empty())
2760 return;
2761
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002762 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2763 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002764}
2765
Steve Naroff415d3d52008-10-08 17:01:13 +00002766//===----------------------------------------------------------------------===//
2767// BlockDecl Implementation
2768//===----------------------------------------------------------------------===//
2769
David Blaikie9c70e042011-09-21 18:16:56 +00002770void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002771 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002772
Steve Naroffc4b30e52009-03-13 16:56:44 +00002773 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002774 if (!NewParamInfo.empty()) {
2775 NumParams = NewParamInfo.size();
2776 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2777 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002778 }
2779}
2780
John McCall351762c2011-02-07 10:33:21 +00002781void BlockDecl::setCaptures(ASTContext &Context,
2782 const Capture *begin,
2783 const Capture *end,
2784 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002785 CapturesCXXThis = capturesCXXThis;
2786
2787 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002788 NumCaptures = 0;
2789 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002790 return;
2791 }
2792
John McCall351762c2011-02-07 10:33:21 +00002793 NumCaptures = end - begin;
2794
2795 // Avoid new Capture[] because we don't want to provide a default
2796 // constructor.
2797 size_t allocationSize = NumCaptures * sizeof(Capture);
2798 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2799 memcpy(buffer, begin, allocationSize);
2800 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002801}
Sebastian Redl833ef452010-01-26 22:01:41 +00002802
John McCallce45f882011-06-15 22:51:16 +00002803bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2804 for (capture_const_iterator
2805 i = capture_begin(), e = capture_end(); i != e; ++i)
2806 // Only auto vars can be captured, so no redeclaration worries.
2807 if (i->getVariable() == variable)
2808 return true;
2809
2810 return false;
2811}
2812
Douglas Gregor70226da2010-12-21 16:27:07 +00002813SourceRange BlockDecl::getSourceRange() const {
2814 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2815}
Sebastian Redl833ef452010-01-26 22:01:41 +00002816
2817//===----------------------------------------------------------------------===//
2818// Other Decl Allocation/Deallocation Method Implementations
2819//===----------------------------------------------------------------------===//
2820
David Blaikie68e081d2011-12-20 02:48:34 +00002821void TranslationUnitDecl::anchor() { }
2822
Sebastian Redl833ef452010-01-26 22:01:41 +00002823TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2824 return new (C) TranslationUnitDecl(C);
2825}
2826
David Blaikie68e081d2011-12-20 02:48:34 +00002827void LabelDecl::anchor() { }
2828
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002829LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002830 SourceLocation IdentL, IdentifierInfo *II) {
2831 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2832}
2833
2834LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2835 SourceLocation IdentL, IdentifierInfo *II,
2836 SourceLocation GnuLabelL) {
2837 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2838 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002839}
2840
Douglas Gregor72172e92012-01-05 21:55:30 +00002841LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2842 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2843 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002844}
2845
David Blaikie68e081d2011-12-20 02:48:34 +00002846void ValueDecl::anchor() { }
2847
Benjamin Kramerea70eb32012-12-01 15:09:41 +00002848bool ValueDecl::isWeak() const {
2849 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
2850 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
2851 return true;
2852
2853 return isWeakImported();
2854}
2855
David Blaikie68e081d2011-12-20 02:48:34 +00002856void ImplicitParamDecl::anchor() { }
2857
Sebastian Redl833ef452010-01-26 22:01:41 +00002858ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002859 SourceLocation IdLoc,
2860 IdentifierInfo *Id,
2861 QualType Type) {
2862 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002863}
2864
Douglas Gregor72172e92012-01-05 21:55:30 +00002865ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2866 unsigned ID) {
2867 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2868 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2869}
2870
Sebastian Redl833ef452010-01-26 22:01:41 +00002871FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002872 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002873 const DeclarationNameInfo &NameInfo,
2874 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002875 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002876 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002877 bool hasWrittenPrototype,
2878 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002879 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2880 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002881 isInlineSpecified,
2882 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002883 New->HasWrittenPrototype = hasWrittenPrototype;
2884 return New;
2885}
2886
Douglas Gregor72172e92012-01-05 21:55:30 +00002887FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2888 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2889 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2890 DeclarationNameInfo(), QualType(), 0,
2891 SC_None, SC_None, false, false);
2892}
2893
Sebastian Redl833ef452010-01-26 22:01:41 +00002894BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2895 return new (C) BlockDecl(DC, L);
2896}
2897
Douglas Gregor72172e92012-01-05 21:55:30 +00002898BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2899 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2900 return new (Mem) BlockDecl(0, SourceLocation());
2901}
2902
Sebastian Redl833ef452010-01-26 22:01:41 +00002903EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2904 SourceLocation L,
2905 IdentifierInfo *Id, QualType T,
2906 Expr *E, const llvm::APSInt &V) {
2907 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2908}
2909
Douglas Gregor72172e92012-01-05 21:55:30 +00002910EnumConstantDecl *
2911EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2912 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2913 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2914 llvm::APSInt());
2915}
2916
David Blaikie68e081d2011-12-20 02:48:34 +00002917void IndirectFieldDecl::anchor() { }
2918
Benjamin Kramer39593702010-11-21 14:11:41 +00002919IndirectFieldDecl *
2920IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2921 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2922 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002923 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2924}
2925
Douglas Gregor72172e92012-01-05 21:55:30 +00002926IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2927 unsigned ID) {
2928 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2929 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2930 QualType(), 0, 0);
2931}
2932
Douglas Gregorbe996932010-09-01 20:41:53 +00002933SourceRange EnumConstantDecl::getSourceRange() const {
2934 SourceLocation End = getLocation();
2935 if (Init)
2936 End = Init->getLocEnd();
2937 return SourceRange(getLocation(), End);
2938}
2939
David Blaikie68e081d2011-12-20 02:48:34 +00002940void TypeDecl::anchor() { }
2941
Sebastian Redl833ef452010-01-26 22:01:41 +00002942TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002943 SourceLocation StartLoc, SourceLocation IdLoc,
2944 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2945 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002946}
2947
David Blaikie68e081d2011-12-20 02:48:34 +00002948void TypedefNameDecl::anchor() { }
2949
Douglas Gregor72172e92012-01-05 21:55:30 +00002950TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2951 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2952 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2953}
2954
Richard Smithdda56e42011-04-15 14:24:37 +00002955TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
2956 SourceLocation StartLoc,
2957 SourceLocation IdLoc, IdentifierInfo *Id,
2958 TypeSourceInfo *TInfo) {
2959 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
2960}
2961
Douglas Gregor72172e92012-01-05 21:55:30 +00002962TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
2964 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
2965}
2966
Abramo Bagnaraea947882011-03-08 16:41:52 +00002967SourceRange TypedefDecl::getSourceRange() const {
2968 SourceLocation RangeEnd = getLocation();
2969 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
2970 if (typeIsPostfix(TInfo->getType()))
2971 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2972 }
2973 return SourceRange(getLocStart(), RangeEnd);
2974}
2975
Richard Smithdda56e42011-04-15 14:24:37 +00002976SourceRange TypeAliasDecl::getSourceRange() const {
2977 SourceLocation RangeEnd = getLocStart();
2978 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
2979 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
2980 return SourceRange(getLocStart(), RangeEnd);
2981}
2982
David Blaikie68e081d2011-12-20 02:48:34 +00002983void FileScopeAsmDecl::anchor() { }
2984
Sebastian Redl833ef452010-01-26 22:01:41 +00002985FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00002986 StringLiteral *Str,
2987 SourceLocation AsmLoc,
2988 SourceLocation RParenLoc) {
2989 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00002990}
Douglas Gregorba345522011-12-02 23:23:56 +00002991
Douglas Gregor72172e92012-01-05 21:55:30 +00002992FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
2993 unsigned ID) {
2994 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
2995 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
2996}
2997
Douglas Gregorba345522011-12-02 23:23:56 +00002998//===----------------------------------------------------------------------===//
2999// ImportDecl Implementation
3000//===----------------------------------------------------------------------===//
3001
3002/// \brief Retrieve the number of module identifiers needed to name the given
3003/// module.
3004static unsigned getNumModuleIdentifiers(Module *Mod) {
3005 unsigned Result = 1;
3006 while (Mod->Parent) {
3007 Mod = Mod->Parent;
3008 ++Result;
3009 }
3010 return Result;
3011}
3012
Douglas Gregor22d09742012-01-03 18:04:46 +00003013ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003014 Module *Imported,
3015 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003016 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003017 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003018{
3019 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3020 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3021 memcpy(StoredLocs, IdentifierLocs.data(),
3022 IdentifierLocs.size() * sizeof(SourceLocation));
3023}
3024
Douglas Gregor22d09742012-01-03 18:04:46 +00003025ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003026 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003027 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003028 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003029{
3030 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3031}
3032
3033ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003034 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003035 ArrayRef<SourceLocation> IdentifierLocs) {
3036 void *Mem = C.Allocate(sizeof(ImportDecl) +
3037 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003038 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003039}
3040
3041ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003042 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003043 Module *Imported,
3044 SourceLocation EndLoc) {
3045 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003046 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003047 Import->setImplicit();
3048 return Import;
3049}
3050
Douglas Gregor72172e92012-01-05 21:55:30 +00003051ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3052 unsigned NumLocations) {
3053 void *Mem = AllocateDeserializedDecl(C, ID,
3054 (sizeof(ImportDecl) +
3055 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003056 return new (Mem) ImportDecl(EmptyShell());
3057}
3058
3059ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3060 if (!ImportedAndComplete.getInt())
3061 return ArrayRef<SourceLocation>();
3062
3063 const SourceLocation *StoredLocs
3064 = reinterpret_cast<const SourceLocation *>(this + 1);
3065 return ArrayRef<SourceLocation>(StoredLocs,
3066 getNumModuleIdentifiers(getImportedModule()));
3067}
3068
3069SourceRange ImportDecl::getSourceRange() const {
3070 if (!ImportedAndComplete.getInt())
3071 return SourceRange(getLocation(),
3072 *reinterpret_cast<const SourceLocation *>(this + 1));
3073
3074 return SourceRange(getLocation(), getIdentifierLocs().back());
3075}