blob: bf5bfaaef10b3df043523c7f86ed594b9762a930 [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"
Chris Lattnera7b32872008-03-15 06:12:44 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/ASTMutationListener.h"
17#include "clang/AST/Attr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/AST/DeclCXX.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/DeclTemplate.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"
David Blaikie9c70e042011-09-21 18:16:56 +000032#include <algorithm>
33
Chris Lattner6d9a6852006-10-25 05:11:20 +000034using namespace clang;
Chris Lattnera11999d2006-10-15 22:34:45 +000035
Chris Lattner88f70d62008-03-15 05:43:15 +000036//===----------------------------------------------------------------------===//
Douglas Gregor6e6ad602009-01-20 01:17:11 +000037// NamedDecl Implementation
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +000038//===----------------------------------------------------------------------===//
39
Douglas Gregor1baf38f2011-03-26 12:10:19 +000040static llvm::Optional<Visibility> getVisibilityOf(const Decl *D) {
41 // If this declaration has an explicit visibility attribute, use it.
42 if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) {
43 switch (A->getVisibility()) {
44 case VisibilityAttr::Default:
45 return DefaultVisibility;
46 case VisibilityAttr::Hidden:
47 return HiddenVisibility;
48 case VisibilityAttr::Protected:
49 return ProtectedVisibility;
50 }
John McCall457a04e2010-10-22 21:05:15 +000051 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +000052
53 // If we're on Mac OS X, an 'availability' for Mac OS X attribute
54 // implies visibility(default).
Douglas Gregore8bbc122011-09-02 00:18:52 +000055 if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +000056 for (specific_attr_iterator<AvailabilityAttr>
57 A = D->specific_attr_begin<AvailabilityAttr>(),
58 AEnd = D->specific_attr_end<AvailabilityAttr>();
59 A != AEnd; ++A)
60 if ((*A)->getPlatform()->getName().equals("macosx"))
61 return DefaultVisibility;
62 }
63
64 return llvm::Optional<Visibility>();
John McCall457a04e2010-10-22 21:05:15 +000065}
66
John McCallc273f242010-10-30 11:50:40 +000067typedef NamedDecl::LinkageInfo LinkageInfo;
John McCallc273f242010-10-30 11:50:40 +000068
Rafael Espindola2f869a32012-01-14 00:30:36 +000069static LinkageInfo getLVForType(QualType T) {
70 std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility();
71 return LinkageInfo(P.first, P.second, T->isVisibilityExplicit());
72}
73
Douglas Gregor7dc5c172010-02-03 09:33:45 +000074/// \brief Get the most restrictive linkage for the types in the given
75/// template parameter list.
Rafael Espindola2f869a32012-01-14 00:30:36 +000076static LinkageInfo
John McCall457a04e2010-10-22 21:05:15 +000077getLVForTemplateParameterList(const TemplateParameterList *Params) {
Rafael Espindola2f869a32012-01-14 00:30:36 +000078 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +000079 for (TemplateParameterList::const_iterator P = Params->begin(),
80 PEnd = Params->end();
81 P != PEnd; ++P) {
Douglas Gregor0231d8d2011-01-19 20:10:05 +000082 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
83 if (NTTP->isExpandedParameterPack()) {
84 for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) {
85 QualType T = NTTP->getExpansionType(I);
86 if (!T->isDependentType())
Rafael Espindola2f869a32012-01-14 00:30:36 +000087 LV.merge(getLVForType(T));
Douglas Gregor0231d8d2011-01-19 20:10:05 +000088 }
89 continue;
90 }
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +000091
Douglas Gregor7dc5c172010-02-03 09:33:45 +000092 if (!NTTP->getType()->isDependentType()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +000093 LV.merge(getLVForType(NTTP->getType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +000094 continue;
95 }
Douglas Gregor0231d8d2011-01-19 20:10:05 +000096 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +000097
98 if (TemplateTemplateParmDecl *TTP
99 = dyn_cast<TemplateTemplateParmDecl>(*P)) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000100 LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000101 }
102 }
103
John McCall457a04e2010-10-22 21:05:15 +0000104 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000105}
106
Rafael Espindola54606d52012-12-25 07:31:49 +0000107/// Compute the linkage and visibility for the given declaration.
108static LinkageInfo computeLVForDecl(const NamedDecl *D, bool OnlyTemplate);
109
110static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
111 if (!OnlyTemplate)
112 return D->getLinkageAndVisibility();
113 return computeLVForDecl(D, OnlyTemplate);
114}
Douglas Gregorbf62d642010-12-06 18:36:25 +0000115
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000116/// \brief Get the most restrictive linkage for the types and
117/// declarations in the given template argument list.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000118static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
119 unsigned NumArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000120 bool OnlyTemplate) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000121 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000122
123 for (unsigned I = 0; I != NumArgs; ++I) {
124 switch (Args[I].getKind()) {
125 case TemplateArgument::Null:
126 case TemplateArgument::Integral:
127 case TemplateArgument::Expression:
128 break;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000129
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000130 case TemplateArgument::Type:
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000131 LV.mergeWithMin(getLVForType(Args[I].getAsType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000132 break;
133
134 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +0000135 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
136 LV.mergeWithMin(getLVForDecl(ND, OnlyTemplate));
137 break;
138
139 case TemplateArgument::NullPtr:
140 LV.mergeWithMin(getLVForType(Args[I].getNullPtrType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000141 break;
142
143 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000144 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000145 if (TemplateDecl *Template
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000146 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000147 LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000148 break;
149
150 case TemplateArgument::Pack:
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000151 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
152 Args[I].pack_size(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000153 OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000154 break;
155 }
156 }
157
John McCall457a04e2010-10-22 21:05:15 +0000158 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000159}
160
Rafael Espindola2f869a32012-01-14 00:30:36 +0000161static LinkageInfo
Douglas Gregorbf62d642010-12-06 18:36:25 +0000162getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000163 bool OnlyTemplate) {
164 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate);
John McCall8823c652010-08-13 08:35:10 +0000165}
166
Rafael Espindola340941d2012-05-25 16:41:35 +0000167static bool shouldConsiderTemplateVis(const FunctionDecl *fn,
Rafael Espindola96dcb8d2012-05-21 20:31:27 +0000168 const FunctionTemplateSpecializationInfo *spec) {
169 return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000170}
171
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000172static bool
173shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola93c289c2012-05-21 20:15:56 +0000174 return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000175}
176
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000177static bool useInlineVisibilityHidden(const NamedDecl *D) {
178 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000179 const LangOptions &Opts = D->getASTContext().getLangOpts();
180 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000181 return false;
182
183 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
184 if (!FD)
185 return false;
186
187 TemplateSpecializationKind TSK = TSK_Undeclared;
188 if (FunctionTemplateSpecializationInfo *spec
189 = FD->getTemplateSpecializationInfo()) {
190 TSK = spec->getTemplateSpecializationKind();
191 } else if (MemberSpecializationInfo *MSI =
192 FD->getMemberSpecializationInfo()) {
193 TSK = MSI->getTemplateSpecializationKind();
194 }
195
196 const FunctionDecl *Def = 0;
197 // InlineVisibilityHidden only applies to definitions, and
198 // isInlined() only gives meaningful answers on definitions
199 // anyway.
200 return TSK != TSK_ExplicitInstantiationDeclaration &&
201 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000202 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000203}
204
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000205static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
206 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000207 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000208 "Not a name having namespace scope");
209 ASTContext &Context = D->getASTContext();
210
211 // C++ [basic.link]p3:
212 // A name having namespace scope (3.3.6) has internal linkage if it
213 // is the name of
214 // - an object, reference, function or function template that is
215 // explicitly declared static; or,
216 // (This bullet corresponds to C99 6.2.2p3.)
217 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
218 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000219 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000220 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000221
Richard Smithdc0ef452012-10-19 06:37:48 +0000222 // - a non-volatile object or reference that is explicitly declared const
223 // or constexpr and neither explicitly declared extern nor previously
224 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000225 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000226 Var->getType().isConstQualified() &&
227 !Var->getType().isVolatileQualified() &&
John McCall8e7d6562010-08-26 03:08:43 +0000228 Var->getStorageClass() != SC_Extern &&
229 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000231 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000232 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000233 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000234 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000235 FoundExtern = true;
236
237 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000238 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000239 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000240 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000241 const VarDecl *PrevVar = Var->getPreviousDecl();
242 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000243 if (PrevVar->getStorageClass() == SC_PrivateExtern)
244 break;
Eli Friedmana7137bc2012-10-26 23:05:34 +0000245 if (PrevVar)
246 return PrevVar->getLinkageAndVisibility();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000247 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000248 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000249 // C++ [temp]p4:
250 // A non-member function template can have internal linkage; any
251 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000252 const FunctionDecl *Function = 0;
253 if (const FunctionTemplateDecl *FunTmpl
254 = dyn_cast<FunctionTemplateDecl>(D))
255 Function = FunTmpl->getTemplatedDecl();
256 else
257 Function = cast<FunctionDecl>(D);
258
259 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000260 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000261 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000262 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
263 // - a data member of an anonymous union.
264 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000265 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000266 }
267
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000268 if (D->isInAnonymousNamespace()) {
269 const VarDecl *Var = dyn_cast<VarDecl>(D);
270 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000271 if ((!Var || !Var->hasCLanguageLinkage()) &&
272 (!Func || !Func->hasCLanguageLinkage()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000273 return LinkageInfo::uniqueExternal();
274 }
John McCallb7139c42010-10-28 04:18:25 +0000275
John McCall457a04e2010-10-22 21:05:15 +0000276 // Set up the defaults.
277
278 // C99 6.2.2p5:
279 // If the declaration of an identifier for an object has file
280 // scope and no storage-class specifier, its linkage is
281 // external.
John McCallc273f242010-10-30 11:50:40 +0000282 LinkageInfo LV;
283
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000284 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000285 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000286 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000287 } else {
288 // If we're declared in a namespace with a visibility attribute,
289 // use that namespace's visibility, but don't call it explicit.
290 for (const DeclContext *DC = D->getDeclContext();
291 !isa<TranslationUnitDecl>(DC);
292 DC = DC->getParent()) {
293 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
294 if (!ND) continue;
295 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000296 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000297 break;
298 }
299 }
300 }
301 }
302
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000303 if (!OnlyTemplate) {
Rafael Espindolab660efd2012-04-19 04:37:16 +0000304 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000305 // If we're paying attention to global visibility, apply
306 // -finline-visibility-hidden if this is an inline method.
307 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
308 LV.mergeVisibility(HiddenVisibility, true);
309 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000310
Douglas Gregorf73b2822009-11-25 22:24:25 +0000311 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000312
Douglas Gregorf73b2822009-11-25 22:24:25 +0000313 // A name having namespace scope has external linkage if it is the
314 // name of
315 //
316 // - an object or reference, unless it has internal linkage; or
317 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000318 // GCC applies the following optimization to variables and static
319 // data members, but not to functions:
320 //
John McCall457a04e2010-10-22 21:05:15 +0000321 // Modify the variable's LV by the LV of its type unless this is
322 // C or extern "C". This follows from [basic.link]p9:
323 // A type without linkage shall not be used as the type of a
324 // variable or function with external linkage unless
325 // - the entity has C language linkage, or
326 // - the entity is declared within an unnamed namespace, or
327 // - the entity is not used or is defined in the same
328 // translation unit.
329 // and [basic.link]p10:
330 // ...the types specified by all declarations referring to a
331 // given variable or function shall be identical...
332 // C does not have an equivalent rule.
333 //
John McCall5fe84122010-10-26 04:59:26 +0000334 // Ignore this if we've got an explicit attribute; the user
335 // probably knows what they're doing.
336 //
John McCall457a04e2010-10-22 21:05:15 +0000337 // Note that we don't want to make the variable non-external
338 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000339 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000340 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000341 LinkageInfo TypeLV = getLVForType(Var->getType());
342 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000343 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000344 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000345 }
346
John McCall23032652010-11-02 18:38:13 +0000347 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000348 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000349
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000350 // Note that Sema::MergeVarDecl already takes care of implementing
351 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
352 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000353
Douglas Gregorf73b2822009-11-25 22:24:25 +0000354 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000355 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000356 // In theory, we can modify the function's LV by the LV of its
357 // type unless it has C linkage (see comment above about variables
358 // for justification). In practice, GCC doesn't do this, so it's
359 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000360
John McCall23032652010-11-02 18:38:13 +0000361 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000362 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000363
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000364 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
365 // merging storage classes and visibility attributes, so we don't have to
366 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000367
John McCallf768aa72011-02-10 06:50:24 +0000368 // In C++, then if the type of the function uses a type with
369 // unique-external linkage, it's not legally usable from outside
370 // this translation unit. However, we should use the C linkage
371 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000372 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000373 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000374 Function->getType()->getLinkage() == UniqueExternalLinkage)
375 return LinkageInfo::uniqueExternal();
376
John McCallb8c604a2011-06-27 23:06:04 +0000377 // Consider LV from the template and the template arguments unless
378 // this is an explicit specialization with a visibility attribute.
379 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000380 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola340941d2012-05-25 16:41:35 +0000381 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
382 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
383 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
384 OnlyTemplate);
385 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000386 LV.mergeWithMin(TempLV);
Rafael Espindola340941d2012-05-25 16:41:35 +0000387 LV.mergeWithMin(ArgsLV);
388 } else {
389 LV.mergeLinkage(TempLV);
390 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000391 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000392 }
393
Douglas Gregorf73b2822009-11-25 22:24:25 +0000394 // - a named class (Clause 9), or an unnamed class defined in a
395 // typedef declaration in which the class has the typedef name
396 // for linkage purposes (7.1.3); or
397 // - a named enumeration (7.2), or an unnamed enumeration
398 // defined in a typedef declaration in which the enumeration
399 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000400 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
401 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000402 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000403 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000404
John McCall457a04e2010-10-22 21:05:15 +0000405 // If this is a class template specialization, consider the
406 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000407 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000408 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000409 // From the template.
410 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000411
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000412 // The arguments at which the template was instantiated.
413 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
414 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
415 OnlyTemplate);
416 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000417 LV.mergeWithMin(TempLV);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000418 LV.mergeWithMin(ArgsLV);
419 } else {
420 LV.mergeLinkage(TempLV);
421 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000422 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000423 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000424
425 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000426 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000427 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
428 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000429 if (!isExternalLinkage(EnumLV.linkage()))
430 return LinkageInfo::none();
431 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000432
433 // - a template, unless it is a function template that has
434 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000435 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000436 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000437 // - a namespace (7.3), unless it is declared within an unnamed
438 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000439 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
440 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000441
John McCall457a04e2010-10-22 21:05:15 +0000442 // By extension, we assign external linkage to Objective-C
443 // interfaces.
444 } else if (isa<ObjCInterfaceDecl>(D)) {
445 // fallout
446
447 // Everything not covered here has no linkage.
448 } else {
John McCallc273f242010-10-30 11:50:40 +0000449 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000450 }
451
452 // If we ended up with non-external linkage, visibility should
453 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000454 if (LV.linkage() != ExternalLinkage)
455 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000456
John McCall457a04e2010-10-22 21:05:15 +0000457 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000458}
459
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000460static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000461 // Only certain class members have linkage. Note that fields don't
462 // really have linkage, but it's convenient to say they do for the
463 // purposes of calculating linkage of pointer-to-data-member
464 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000465 if (!(isa<CXXMethodDecl>(D) ||
466 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000467 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000468 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000469 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000470
John McCall07072662010-11-02 01:45:15 +0000471 LinkageInfo LV;
472
John McCall07072662010-11-02 01:45:15 +0000473 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000474 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000475 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000476 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000477 // If we're paying attention to global visibility, apply
478 // -finline-visibility-hidden if this is an inline method.
479 //
480 // Note that we do this before merging information about
481 // the class visibility.
482 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
483 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000484 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000485
486 // If this class member has an explicit visibility attribute, the only
487 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000488 // only look for them when processing the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000489 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000490
Rafael Espindola53cf2192012-04-19 05:50:08 +0000491 // If this member has an visibility attribute, ClassF will exclude
492 // attributes on the class or command line options, keeping only information
493 // about the template instantiation. If the member has no visibility
494 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
495 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000496 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
497 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000498 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000499 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000500
501 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000502 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000503 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000504
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000505 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000506 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000507
John McCall8823c652010-08-13 08:35:10 +0000508 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000509 // If the type of the function uses a type with unique-external
510 // linkage, it's not legally usable from outside this translation unit.
511 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
512 return LinkageInfo::uniqueExternal();
513
John McCall457a04e2010-10-22 21:05:15 +0000514 // If this is a method template specialization, use the linkage for
515 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000516 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000517 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000518 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
519 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
520 OnlyTemplate);
521 TemplateParameterList *TemplateParams =
522 spec->getTemplate()->getTemplateParameters();
523 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola340941d2012-05-25 16:41:35 +0000524 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000525 LV.mergeWithMin(ArgsLV);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000526 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000527 LV.mergeWithMin(ParamsLV);
Rafael Espindola67a498c2012-05-25 17:22:33 +0000528 } else {
529 LV.mergeLinkage(ArgsLV);
530 if (!OnlyTemplate)
531 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000532 }
John McCalle6e622e2010-11-01 01:29:57 +0000533 }
John McCall457a04e2010-10-22 21:05:15 +0000534
John McCall37bb6c92010-10-29 22:22:43 +0000535 // Note that in contrast to basically every other situation, we
536 // *do* apply -fvisibility to method declarations.
537
538 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000539 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000540 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000541 // Merge template argument/parameter information for member
542 // class template specializations.
543 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
544 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
545 OnlyTemplate);
546 TemplateParameterList *TemplateParams =
547 spec->getSpecializedTemplate()->getTemplateParameters();
548 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000549 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000550 LV.mergeWithMin(ArgsLV);
Rafael Espindola4d71d0f2012-05-25 14:17:45 +0000551 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000552 LV.mergeWithMin(ParamsLV);
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000553 } else {
554 LV.mergeLinkage(ArgsLV);
555 if (!OnlyTemplate)
556 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000557 }
John McCall37bb6c92010-10-29 22:22:43 +0000558 }
559
John McCall37bb6c92010-10-29 22:22:43 +0000560 // Static data members.
561 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000562 // Modify the variable's linkage by its type, but ignore the
563 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000564 LinkageInfo TypeLV = getLVForType(VD->getType());
565 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000566 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000567 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000568 }
569
John McCall457a04e2010-10-22 21:05:15 +0000570 return LV;
John McCall8823c652010-08-13 08:35:10 +0000571}
572
John McCalld396b972011-02-08 19:01:05 +0000573static void clearLinkageForClass(const CXXRecordDecl *record) {
574 for (CXXRecordDecl::decl_iterator
575 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
576 Decl *child = *i;
577 if (isa<NamedDecl>(child))
Rafael Espindola54606d52012-12-25 07:31:49 +0000578 cast<NamedDecl>(child)->ClearLVCache();
John McCalld396b972011-02-08 19:01:05 +0000579 }
580}
581
David Blaikie68e081d2011-12-20 02:48:34 +0000582void NamedDecl::anchor() { }
583
Rafael Espindola54606d52012-12-25 07:31:49 +0000584void NamedDecl::ClearLVCache() {
John McCalld396b972011-02-08 19:01:05 +0000585 // Note that we can't skip clearing the linkage of children just
586 // because the parent doesn't have cached linkage: we don't cache
587 // when computing linkage for parent contexts.
588
Rafael Espindola54606d52012-12-25 07:31:49 +0000589 CacheValidAndVisibility = 0;
John McCalld396b972011-02-08 19:01:05 +0000590
591 // If we're changing the linkage of a class, we need to reset the
592 // linkage of child declarations, too.
593 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
594 clearLinkageForClass(record);
595
John McCall83779672011-02-19 02:53:41 +0000596 if (ClassTemplateDecl *temp =
597 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000598 // Clear linkage for the template pattern.
599 CXXRecordDecl *record = temp->getTemplatedDecl();
Rafael Espindola54606d52012-12-25 07:31:49 +0000600 record->CacheValidAndVisibility = 0;
John McCalld396b972011-02-08 19:01:05 +0000601 clearLinkageForClass(record);
602
John McCall83779672011-02-19 02:53:41 +0000603 // We need to clear linkage for specializations, too.
604 for (ClassTemplateDecl::spec_iterator
605 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindola54606d52012-12-25 07:31:49 +0000606 i->ClearLVCache();
John McCalld396b972011-02-08 19:01:05 +0000607 }
John McCall83779672011-02-19 02:53:41 +0000608
609 // Clear cached linkage for function template decls, too.
610 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000611 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
Rafael Espindola54606d52012-12-25 07:31:49 +0000612 temp->getTemplatedDecl()->ClearLVCache();
John McCall83779672011-02-19 02:53:41 +0000613 for (FunctionTemplateDecl::spec_iterator
614 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindola54606d52012-12-25 07:31:49 +0000615 i->ClearLVCache();
John McCall8f9a4292011-03-22 06:58:49 +0000616 }
John McCall83779672011-02-19 02:53:41 +0000617
John McCalld396b972011-02-08 19:01:05 +0000618}
619
Douglas Gregorbf62d642010-12-06 18:36:25 +0000620Linkage NamedDecl::getLinkage() const {
Rafael Espindola54606d52012-12-25 07:31:49 +0000621 return getLinkageAndVisibility().linkage();
Douglas Gregorbf62d642010-12-06 18:36:25 +0000622}
623
John McCallc273f242010-10-30 11:50:40 +0000624LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola54606d52012-12-25 07:31:49 +0000625 if (CacheValidAndVisibility) {
626 Linkage L = static_cast<Linkage>(CachedLinkage);
627 Visibility V = static_cast<Visibility>(CacheValidAndVisibility - 1);
628 bool Explicit = CachedVisibilityExplicit;
629 LinkageInfo LV(L, V, Explicit);
630 assert(LV == computeLVForDecl(this, false));
631 return LV;
632 }
633 LinkageInfo LV = computeLVForDecl(this, false);
634 CachedLinkage = LV.linkage();
635 CacheValidAndVisibility = LV.visibility() + 1;
636 CachedVisibilityExplicit = LV.visibilityExplicit();
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000637
638#ifndef NDEBUG
639 // In C (because of gnu inline) and in c++ with microsoft extensions an
640 // static can follow an extern, so we can have two decls with different
641 // linkages.
642 const LangOptions &Opts = getASTContext().getLangOpts();
643 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
644 return LV;
645
646 // We have just computed the linkage for this decl. By induction we know
647 // that all other computed linkages match, check that the one we just computed
648 // also does.
649 NamedDecl *D = NULL;
650 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
651 NamedDecl *T = cast<NamedDecl>(*I);
652 if (T == this)
653 continue;
654 if (T->CacheValidAndVisibility != 0) {
655 D = T;
656 break;
657 }
658 }
659 assert(!D || D->CachedLinkage == CachedLinkage);
660#endif
661
Rafael Espindola54606d52012-12-25 07:31:49 +0000662 return LV;
John McCall033caa52010-10-29 00:29:13 +0000663}
Ted Kremenek926d8602010-04-20 23:15:35 +0000664
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000665llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
666 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000667 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000668 if (llvm::Optional<Visibility> V = getVisibilityOf(Var))
Rafael Espindola96e68242012-05-16 02:10:38 +0000669 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000670
Rafael Espindola96e68242012-05-16 02:10:38 +0000671 if (Var->isStaticDataMember()) {
672 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
673 if (InstantiatedFrom)
674 return getVisibilityOf(InstantiatedFrom);
675 }
676
677 return llvm::Optional<Visibility>();
678 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000679 // Use the most recent declaration of a function, and also handle
680 // function template specializations.
681 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000682 if (llvm::Optional<Visibility> V = getVisibilityOf(fn))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000683 return V;
684
685 // If the function is a specialization of a template with an
686 // explicit visibility attribute, use that.
687 if (FunctionTemplateSpecializationInfo *templateInfo
688 = fn->getTemplateSpecializationInfo())
689 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
690
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000691 // If the function is a member of a specialization of a class template
692 // and the corresponding decl has explicit visibility, use that.
693 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
694 if (InstantiatedFrom)
695 return getVisibilityOf(InstantiatedFrom);
696
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000697 return llvm::Optional<Visibility>();
698 }
699
700 // Otherwise, just check the declaration itself first.
701 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
702 return V;
703
Rafael Espindolafb4263f2012-07-31 19:02:02 +0000704 // The visibility of a template is stored in the templated decl.
705 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
706 return getVisibilityOf(TD->getTemplatedDecl());
707
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000708 // If there wasn't explicit visibility there, and this is a
709 // specialization of a class template, check for visibility
710 // on the pattern.
711 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolaeca5cd22012-07-13 01:19:08 +0000712 = dyn_cast<ClassTemplateSpecializationDecl>(this))
713 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000714
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000715 // If this is a member class of a specialization of a class template
716 // and the corresponding decl has explicit visibility, use that.
717 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
718 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
719 if (InstantiatedFrom)
720 return getVisibilityOf(InstantiatedFrom);
721 }
722
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000723 return llvm::Optional<Visibility>();
724}
725
Rafael Espindola54606d52012-12-25 07:31:49 +0000726static LinkageInfo computeLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000727 // Objective-C: treat all Objective-C declarations as having external
728 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000729 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000730 default:
731 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000732 case Decl::ParmVar:
733 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000734 case Decl::TemplateTemplateParm: // count these as external
735 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000736 case Decl::ObjCAtDefsField:
737 case Decl::ObjCCategory:
738 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000739 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000740 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000741 case Decl::ObjCMethod:
742 case Decl::ObjCProperty:
743 case Decl::ObjCPropertyImpl:
744 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000745 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000746
747 case Decl::CXXRecord: {
748 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
749 if (Record->isLambda()) {
750 if (!Record->getLambdaManglingNumber()) {
751 // This lambda has no mangling number, so it's internal.
752 return LinkageInfo::internal();
753 }
754
755 // This lambda has its linkage/visibility determined by its owner.
756 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
757 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
758 if (isa<ParmVarDecl>(ContextDecl))
759 DC = ContextDecl->getDeclContext()->getRedeclContext();
760 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000761 return getLVForDecl(cast<NamedDecl>(ContextDecl),
762 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000763 }
764
765 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000766 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000767
768 return LinkageInfo::external();
769 }
770
771 break;
772 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000773 }
774
Douglas Gregorf73b2822009-11-25 22:24:25 +0000775 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000776 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000777 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000778
779 // C++ [basic.link]p5:
780 // In addition, a member function, static data member, a named
781 // class or enumeration of class scope, or an unnamed class or
782 // enumeration defined in a class-scope typedef declaration such
783 // that the class or enumeration has the typedef name for linkage
784 // purposes (7.1.3), has external linkage if the name of the class
785 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000786 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000787 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000788
789 // C++ [basic.link]p6:
790 // The name of a function declared in block scope and the name of
791 // an object declared by a block scope extern declaration have
792 // linkage. If there is a visible declaration of an entity with
793 // linkage having the same name and type, ignoring entities
794 // declared outside the innermost enclosing namespace scope, the
795 // block scope declaration declares that same entity and receives
796 // the linkage of the previous declaration. If there is more than
797 // one such matching entity, the program is ill-formed. Otherwise,
798 // if no matching entity is found, the block scope entity receives
799 // external linkage.
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000800 if (D->getDeclContext()->isFunctionOrMethod()) {
John McCall033caa52010-10-29 00:29:13 +0000801 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000802 if (Function->isInAnonymousNamespace() &&
803 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000804 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000805
Rafael Espindola3bd836a2013-01-09 16:34:58 +0000806 // This is a "void f();" which got merged with a file static.
807 if (Function->getStorageClass() == SC_Static)
808 return LinkageInfo::internal();
809
John McCallc273f242010-10-30 11:50:40 +0000810 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000811 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000812 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000813 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000814 }
Rafael Espindolac12edd42012-11-29 16:38:22 +0000815
816 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
817 // merging storage classes and visibility attributes, so we don't have to
818 // look at previous decls in here.
John McCall457a04e2010-10-22 21:05:15 +0000819
820 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000821 }
822
John McCall033caa52010-10-29 00:29:13 +0000823 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
Rafael Espindola74a133f2012-12-18 04:18:55 +0000824 if (Var->getStorageClassAsWritten() == SC_Extern ||
825 Var->getStorageClassAsWritten() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000826 if (Var->isInAnonymousNamespace() &&
827 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000828 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000829
Rafael Espindola74a133f2012-12-18 04:18:55 +0000830 // This is an "extern int foo;" which got merged with a file static.
831 if (Var->getStorageClass() == SC_Static)
832 return LinkageInfo::internal();
833
John McCallc273f242010-10-30 11:50:40 +0000834 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000835 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000836 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000837 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000838 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000839 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000840 }
John McCall457a04e2010-10-22 21:05:15 +0000841
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000842 // Note that Sema::MergeVarDecl already takes care of implementing
843 // C99 6.2.2p4 and propagating the visibility attribute, so we don't
844 // have to do it here.
John McCall457a04e2010-10-22 21:05:15 +0000845 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000846 }
847 }
848
849 // C++ [basic.link]p6:
850 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000851 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000852}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000853
Douglas Gregor2ada0482009-02-04 17:27:36 +0000854std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000855 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000856}
857
858std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000859 const DeclContext *Ctx = getDeclContext();
860
861 if (Ctx->isFunctionOrMethod())
862 return getNameAsString();
863
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000864 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000865 ContextsTy Contexts;
866
867 // Collect contexts.
868 while (Ctx && isa<NamedDecl>(Ctx)) {
869 Contexts.push_back(Ctx);
870 Ctx = Ctx->getParent();
871 };
872
873 std::string QualName;
874 llvm::raw_string_ostream OS(QualName);
875
876 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
877 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000878 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000879 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000880 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
881 std::string TemplateArgsStr
882 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000883 TemplateArgs.data(),
884 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000885 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000886 OS << Spec->getName() << TemplateArgsStr;
887 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000888 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000889 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000890 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000891 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000892 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
893 if (!RD->getIdentifier())
894 OS << "<anonymous " << RD->getKindName() << '>';
895 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000896 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000897 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000898 const FunctionProtoType *FT = 0;
899 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +0000900 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +0000901
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000902 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000903 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000904 unsigned NumParams = FD->getNumParams();
905 for (unsigned i = 0; i < NumParams; ++i) {
906 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000907 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000908 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000909 }
910
911 if (FT->isVariadic()) {
912 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000913 OS << ", ";
914 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000915 }
916 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000917 OS << ')';
918 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000919 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000920 }
921 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000922 }
923
John McCalla2a3f7d2010-03-16 21:48:18 +0000924 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000925 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000926 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000927 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000928
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000929 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000930}
931
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000932bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000933 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
934
Douglas Gregor889ceb72009-02-03 19:21:40 +0000935 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
936 // We want to keep it, unless it nominates same namespace.
937 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000938 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
939 ->getOriginalNamespace() ==
940 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
941 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000942 }
Mike Stump11289f42009-09-09 15:08:12 +0000943
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000944 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
945 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000946 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000947
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000948 // For function templates, the underlying function declarations are linked.
949 if (const FunctionTemplateDecl *FunctionTemplate
950 = dyn_cast<FunctionTemplateDecl>(this))
951 if (const FunctionTemplateDecl *OldFunctionTemplate
952 = dyn_cast<FunctionTemplateDecl>(OldD))
953 return FunctionTemplate->getTemplatedDecl()
954 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000955
Steve Naroffc4173fa2009-02-22 19:35:57 +0000956 // For method declarations, we keep track of redeclarations.
957 if (isa<ObjCMethodDecl>(this))
958 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000959
John McCall9f3059a2009-10-09 21:13:30 +0000960 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
961 return true;
962
John McCall3f746822009-11-17 05:59:44 +0000963 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
964 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
965 cast<UsingShadowDecl>(OldD)->getTargetDecl();
966
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000967 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
968 ASTContext &Context = getASTContext();
969 return Context.getCanonicalNestedNameSpecifier(
970 cast<UsingDecl>(this)->getQualifier()) ==
971 Context.getCanonicalNestedNameSpecifier(
972 cast<UsingDecl>(OldD)->getQualifier());
973 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000974
Douglas Gregorb59643b2012-01-03 23:26:26 +0000975 // A typedef of an Objective-C class type can replace an Objective-C class
976 // declaration or definition, and vice versa.
977 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
978 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
979 return true;
980
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000981 // For non-function declarations, if the declarations are of the
982 // same kind then this must be a redeclaration, or semantic analysis
983 // would not have given us the new declaration.
984 return this->getKind() == OldD->getKind();
985}
986
Douglas Gregoreddf4332009-02-24 20:03:32 +0000987bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000988 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000989}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000990
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000991NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000992 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000993 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
994 ND = UD->getTargetDecl();
995
996 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
997 return AD->getClassInterface();
998
999 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001000}
1001
John McCalla8ae2222010-04-06 21:38:20 +00001002bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001003 if (!isCXXClassMember())
1004 return false;
1005
John McCalla8ae2222010-04-06 21:38:20 +00001006 const NamedDecl *D = this;
1007 if (isa<UsingShadowDecl>(D))
1008 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1009
Francois Pichet783dd6e2010-11-21 06:08:52 +00001010 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001011 return true;
1012 if (isa<CXXMethodDecl>(D))
1013 return cast<CXXMethodDecl>(D)->isInstance();
1014 if (isa<FunctionTemplateDecl>(D))
1015 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1016 ->getTemplatedDecl())->isInstance();
1017 return false;
1018}
1019
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001020//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001021// DeclaratorDecl Implementation
1022//===----------------------------------------------------------------------===//
1023
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001024template <typename DeclT>
1025static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1026 if (decl->getNumTemplateParameterLists() > 0)
1027 return decl->getTemplateParameterList(0)->getTemplateLoc();
1028 else
1029 return decl->getInnerLocStart();
1030}
1031
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001032SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001033 TypeSourceInfo *TSI = getTypeSourceInfo();
1034 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001035 return SourceLocation();
1036}
1037
Douglas Gregor14454802011-02-25 02:25:35 +00001038void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1039 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001040 // Make sure the extended decl info is allocated.
1041 if (!hasExtInfo()) {
1042 // Save (non-extended) type source info pointer.
1043 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1044 // Allocate external info struct.
1045 DeclInfo = new (getASTContext()) ExtInfo;
1046 // Restore savedTInfo into (extended) decl info.
1047 getExtInfo()->TInfo = savedTInfo;
1048 }
1049 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001050 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001051 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001052 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001053 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001054 if (getExtInfo()->NumTemplParamLists == 0) {
1055 // Save type source info pointer.
1056 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1057 // Deallocate the extended decl info.
1058 getASTContext().Deallocate(getExtInfo());
1059 // Restore savedTInfo into (non-extended) decl info.
1060 DeclInfo = savedTInfo;
1061 }
1062 else
1063 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001064 }
1065 }
1066}
1067
Abramo Bagnara60804e12011-03-18 15:16:37 +00001068void
1069DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1070 unsigned NumTPLists,
1071 TemplateParameterList **TPLists) {
1072 assert(NumTPLists > 0);
1073 // Make sure the extended decl info is allocated.
1074 if (!hasExtInfo()) {
1075 // Save (non-extended) type source info pointer.
1076 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1077 // Allocate external info struct.
1078 DeclInfo = new (getASTContext()) ExtInfo;
1079 // Restore savedTInfo into (extended) decl info.
1080 getExtInfo()->TInfo = savedTInfo;
1081 }
1082 // Set the template parameter lists info.
1083 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1084}
1085
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001086SourceLocation DeclaratorDecl::getOuterLocStart() const {
1087 return getTemplateOrInnerLocStart(this);
1088}
1089
Abramo Bagnaraea947882011-03-08 16:41:52 +00001090namespace {
1091
1092// Helper function: returns true if QT is or contains a type
1093// having a postfix component.
1094bool typeIsPostfix(clang::QualType QT) {
1095 while (true) {
1096 const Type* T = QT.getTypePtr();
1097 switch (T->getTypeClass()) {
1098 default:
1099 return false;
1100 case Type::Pointer:
1101 QT = cast<PointerType>(T)->getPointeeType();
1102 break;
1103 case Type::BlockPointer:
1104 QT = cast<BlockPointerType>(T)->getPointeeType();
1105 break;
1106 case Type::MemberPointer:
1107 QT = cast<MemberPointerType>(T)->getPointeeType();
1108 break;
1109 case Type::LValueReference:
1110 case Type::RValueReference:
1111 QT = cast<ReferenceType>(T)->getPointeeType();
1112 break;
1113 case Type::PackExpansion:
1114 QT = cast<PackExpansionType>(T)->getPattern();
1115 break;
1116 case Type::Paren:
1117 case Type::ConstantArray:
1118 case Type::DependentSizedArray:
1119 case Type::IncompleteArray:
1120 case Type::VariableArray:
1121 case Type::FunctionProto:
1122 case Type::FunctionNoProto:
1123 return true;
1124 }
1125 }
1126}
1127
1128} // namespace
1129
1130SourceRange DeclaratorDecl::getSourceRange() const {
1131 SourceLocation RangeEnd = getLocation();
1132 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1133 if (typeIsPostfix(TInfo->getType()))
1134 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1135 }
1136 return SourceRange(getOuterLocStart(), RangeEnd);
1137}
1138
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001139void
Douglas Gregor20527e22010-06-15 17:44:38 +00001140QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1141 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001142 TemplateParameterList **TPLists) {
1143 assert((NumTPLists == 0 || TPLists != 0) &&
1144 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001145
1146 // Free previous template parameters (if any).
1147 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001148 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001149 TemplParamLists = 0;
1150 NumTemplParamLists = 0;
1151 }
1152 // Set info on matched template parameter lists (if any).
1153 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001154 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001155 NumTemplParamLists = NumTPLists;
1156 for (unsigned i = NumTPLists; i-- > 0; )
1157 TemplParamLists[i] = TPLists[i];
1158 }
1159}
1160
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001161//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001162// VarDecl Implementation
1163//===----------------------------------------------------------------------===//
1164
Sebastian Redl833ef452010-01-26 22:01:41 +00001165const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1166 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001167 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001168 case SC_Auto: return "auto";
1169 case SC_Extern: return "extern";
1170 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1171 case SC_PrivateExtern: return "__private_extern__";
1172 case SC_Register: return "register";
1173 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001174 }
1175
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001176 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001177}
1178
Abramo Bagnaradff19302011-03-08 08:55:46 +00001179VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1180 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001181 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001182 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001183 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001184}
1185
Douglas Gregor72172e92012-01-05 21:55:30 +00001186VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1187 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1188 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1189 QualType(), 0, SC_None, SC_None);
1190}
1191
Douglas Gregorbf62d642010-12-06 18:36:25 +00001192void VarDecl::setStorageClass(StorageClass SC) {
1193 assert(isLegalForVariable(SC));
1194 if (getStorageClass() != SC)
Rafael Espindola54606d52012-12-25 07:31:49 +00001195 ClearLVCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001196
John McCallbeaa11c2011-05-01 02:13:58 +00001197 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001198}
1199
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001200SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001201 if (const Expr *Init = getInit()) {
1202 SourceLocation InitEnd = Init->getLocEnd();
1203 if (InitEnd.isValid())
1204 return SourceRange(getOuterLocStart(), InitEnd);
1205 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001206 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001207}
1208
Rafael Espindola88510672013-01-04 21:18:45 +00001209template<typename T>
1210static bool hasCLanguageLinkageTemplate(const T &D) {
Rafael Espindola576127d2012-12-28 14:21:58 +00001211 // Language linkage is a C++ concept, but saying that everything in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001212 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001213 ASTContext &Context = D.getASTContext();
1214 if (!Context.getLangOpts().CPlusPlus)
1215 return true;
1216
1217 // dcl.link 4: A C language linkage is ignored in determining the language
1218 // linkage of the names of class members and the function type of class member
1219 // functions.
1220 const DeclContext *DC = D.getDeclContext();
1221 if (DC->isRecord())
1222 return false;
1223
1224 // If the first decl is in an extern "C" context, any other redeclaration
1225 // will have C language linkage. If the first one is not in an extern "C"
1226 // context, we would have reported an error for any other decl being in one.
Rafael Espindola88510672013-01-04 21:18:45 +00001227 const T *First = D.getFirstDeclaration();
Rafael Espindola576127d2012-12-28 14:21:58 +00001228 return First->getDeclContext()->isExternCContext();
1229}
1230
1231bool VarDecl::hasCLanguageLinkage() const {
1232 return hasCLanguageLinkageTemplate(*this);
1233}
1234
Sebastian Redl833ef452010-01-26 22:01:41 +00001235bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001236 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001237 return false;
1238
Eli Friedman839192f2012-01-15 01:23:58 +00001239 const DeclContext *DC = getDeclContext();
1240 if (DC->isRecord())
1241 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001242
Eli Friedman839192f2012-01-15 01:23:58 +00001243 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001244 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001245 return true;
1246 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001247}
1248
1249VarDecl *VarDecl::getCanonicalDecl() {
1250 return getFirstDeclaration();
1251}
1252
Daniel Dunbar9d355812012-03-09 01:51:51 +00001253VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1254 ASTContext &C) const
1255{
Sebastian Redl35351a92010-01-31 22:27:38 +00001256 // C++ [basic.def]p2:
1257 // A declaration is a definition unless [...] it contains the 'extern'
1258 // specifier or a linkage-specification and neither an initializer [...],
1259 // it declares a static data member in a class declaration [...].
1260 // C++ [temp.expl.spec]p15:
1261 // An explicit specialization of a static data member of a template is a
1262 // definition if the declaration includes an initializer; otherwise, it is
1263 // a declaration.
1264 if (isStaticDataMember()) {
1265 if (isOutOfLine() && (hasInit() ||
1266 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1267 return Definition;
1268 else
1269 return DeclarationOnly;
1270 }
1271 // C99 6.7p5:
1272 // A definition of an identifier is a declaration for that identifier that
1273 // [...] causes storage to be reserved for that object.
1274 // Note: that applies for all non-file-scope objects.
1275 // C99 6.9.2p1:
1276 // If the declaration of an identifier for an object has file scope and an
1277 // initializer, the declaration is an external definition for the identifier
1278 if (hasInit())
1279 return Definition;
1280 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1281 if (hasExternalStorage())
1282 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001283
John McCall8e7d6562010-08-26 03:08:43 +00001284 if (getStorageClassAsWritten() == SC_Extern ||
1285 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001286 for (const VarDecl *PrevVar = getPreviousDecl();
1287 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Rafael Espindola7581f322012-12-17 22:23:47 +00001288 if (PrevVar->getLinkage() == InternalLinkage)
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001289 return DeclarationOnly;
1290 }
1291 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001292 // C99 6.9.2p2:
1293 // A declaration of an object that has file scope without an initializer,
1294 // and without a storage class specifier or the scs 'static', constitutes
1295 // a tentative definition.
1296 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001297 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001298 return TentativeDefinition;
1299
1300 // What's left is (in C, block-scope) declarations without initializers or
1301 // external storage. These are definitions.
1302 return Definition;
1303}
1304
Sebastian Redl35351a92010-01-31 22:27:38 +00001305VarDecl *VarDecl::getActingDefinition() {
1306 DefinitionKind Kind = isThisDeclarationADefinition();
1307 if (Kind != TentativeDefinition)
1308 return 0;
1309
Chris Lattner48eb14d2010-06-14 18:31:46 +00001310 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001311 VarDecl *First = getFirstDeclaration();
1312 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1313 I != E; ++I) {
1314 Kind = (*I)->isThisDeclarationADefinition();
1315 if (Kind == Definition)
1316 return 0;
1317 else if (Kind == TentativeDefinition)
1318 LastTentative = *I;
1319 }
1320 return LastTentative;
1321}
1322
1323bool VarDecl::isTentativeDefinitionNow() const {
1324 DefinitionKind Kind = isThisDeclarationADefinition();
1325 if (Kind != TentativeDefinition)
1326 return false;
1327
1328 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1329 if ((*I)->isThisDeclarationADefinition() == Definition)
1330 return false;
1331 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001332 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001333}
1334
Daniel Dunbar9d355812012-03-09 01:51:51 +00001335VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001336 VarDecl *First = getFirstDeclaration();
1337 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1338 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001339 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001340 return *I;
1341 }
1342 return 0;
1343}
1344
Daniel Dunbar9d355812012-03-09 01:51:51 +00001345VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001346 DefinitionKind Kind = DeclarationOnly;
1347
1348 const VarDecl *First = getFirstDeclaration();
1349 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001350 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001351 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001352 if (Kind == Definition)
1353 break;
1354 }
John McCall37bb6c92010-10-29 22:22:43 +00001355
1356 return Kind;
1357}
1358
Sebastian Redl5ca79842010-02-01 20:16:42 +00001359const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001360 redecl_iterator I = redecls_begin(), E = redecls_end();
1361 while (I != E && !I->getInit())
1362 ++I;
1363
1364 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001365 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001366 return I->getInit();
1367 }
1368 return 0;
1369}
1370
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001371bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001372 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001373 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001374
1375 if (!isStaticDataMember())
1376 return false;
1377
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001378 // If this static data member was instantiated from a static data member of
1379 // a class template, check whether that static data member was defined
1380 // out-of-line.
1381 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1382 return VD->isOutOfLine();
1383
1384 return false;
1385}
1386
Douglas Gregor1d957a32009-10-27 18:42:08 +00001387VarDecl *VarDecl::getOutOfLineDefinition() {
1388 if (!isStaticDataMember())
1389 return 0;
1390
1391 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1392 RD != RDEnd; ++RD) {
1393 if (RD->getLexicalDeclContext()->isFileContext())
1394 return *RD;
1395 }
1396
1397 return 0;
1398}
1399
Douglas Gregord5058122010-02-11 01:19:42 +00001400void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001401 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1402 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001403 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001404 }
1405
1406 Init = I;
1407}
1408
Daniel Dunbar9d355812012-03-09 01:51:51 +00001409bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001410 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001411
Richard Smith35ecb362012-03-02 04:14:40 +00001412 if (!Lang.CPlusPlus)
1413 return false;
1414
1415 // In C++11, any variable of reference type can be used in a constant
1416 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001417 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001418 return true;
1419
1420 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001421 // not require the variable to be non-volatile, but we consider this to be a
1422 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001423 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001424 return false;
1425
1426 // In C++, const, non-volatile variables of integral or enumeration types
1427 // can be used in constant expressions.
1428 if (getType()->isIntegralOrEnumerationType())
1429 return true;
1430
Richard Smith35ecb362012-03-02 04:14:40 +00001431 // Additionally, in C++11, non-volatile constexpr variables can be used in
1432 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001433 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001434}
1435
Richard Smithd0b4dd62011-12-19 06:19:21 +00001436/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1437/// form, which contains extra information on the evaluated value of the
1438/// initializer.
1439EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1440 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1441 if (!Eval) {
1442 Stmt *S = Init.get<Stmt *>();
1443 Eval = new (getASTContext()) EvaluatedStmt;
1444 Eval->Value = S;
1445 Init = Eval;
1446 }
1447 return Eval;
1448}
1449
Richard Smithdafff942012-01-14 04:30:29 +00001450APValue *VarDecl::evaluateValue() const {
1451 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1452 return evaluateValue(Notes);
1453}
1454
1455APValue *VarDecl::evaluateValue(
1456 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001457 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1458
1459 // We only produce notes indicating why an initializer is non-constant the
1460 // first time it is evaluated. FIXME: The notes won't always be emitted the
1461 // first time we try evaluation, so might not be produced at all.
1462 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001463 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001464
1465 const Expr *Init = cast<Expr>(Eval->Value);
1466 assert(!Init->isValueDependent());
1467
1468 if (Eval->IsEvaluating) {
1469 // FIXME: Produce a diagnostic for self-initialization.
1470 Eval->CheckedICE = true;
1471 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001472 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001473 }
1474
1475 Eval->IsEvaluating = true;
1476
1477 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1478 this, Notes);
1479
1480 // Ensure the result is an uninitialized APValue if evaluation fails.
1481 if (!Result)
1482 Eval->Evaluated = APValue();
1483
1484 Eval->IsEvaluating = false;
1485 Eval->WasEvaluated = true;
1486
1487 // In C++11, we have determined whether the initializer was a constant
1488 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001489 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001490 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001491 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001492 }
1493
Richard Smithdafff942012-01-14 04:30:29 +00001494 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001495}
1496
1497bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001498 // Initializers of weak variables are never ICEs.
1499 if (isWeak())
1500 return false;
1501
Richard Smithd0b4dd62011-12-19 06:19:21 +00001502 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1503 if (Eval->CheckedICE)
1504 // We have already checked whether this subexpression is an
1505 // integral constant expression.
1506 return Eval->IsICE;
1507
1508 const Expr *Init = cast<Expr>(Eval->Value);
1509 assert(!Init->isValueDependent());
1510
1511 // In C++11, evaluate the initializer to check whether it's a constant
1512 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001513 if (getASTContext().getLangOpts().CPlusPlus11) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001514 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1515 evaluateValue(Notes);
1516 return Eval->IsICE;
1517 }
1518
1519 // It's an ICE whether or not the definition we found is
1520 // out-of-line. See DR 721 and the discussion in Clang PR
1521 // 6206 for details.
1522
1523 if (Eval->CheckingICE)
1524 return false;
1525 Eval->CheckingICE = true;
1526
1527 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1528 Eval->CheckingICE = false;
1529 Eval->CheckedICE = true;
1530 return Eval->IsICE;
1531}
1532
Douglas Gregorfe314812011-06-21 17:03:29 +00001533bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001534 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001535
1536 const Expr *E = getInit();
1537 if (!E)
1538 return false;
1539
1540 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1541 E = Cleanups->getSubExpr();
1542
1543 return isa<MaterializeTemporaryExpr>(E);
1544}
1545
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001546VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001547 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001548 return cast<VarDecl>(MSI->getInstantiatedFrom());
1549
1550 return 0;
1551}
1552
Douglas Gregor3c74d412009-10-14 20:14:33 +00001553TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001554 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001555 return MSI->getTemplateSpecializationKind();
1556
1557 return TSK_Undeclared;
1558}
1559
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001560MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001561 return getASTContext().getInstantiatedFromStaticDataMember(this);
1562}
1563
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001564void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1565 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001566 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001567 assert(MSI && "Not an instantiated static data member?");
1568 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001569 if (TSK != TSK_ExplicitSpecialization &&
1570 PointOfInstantiation.isValid() &&
1571 MSI->getPointOfInstantiation().isInvalid())
1572 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001573}
1574
Sebastian Redl833ef452010-01-26 22:01:41 +00001575//===----------------------------------------------------------------------===//
1576// ParmVarDecl Implementation
1577//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001578
Sebastian Redl833ef452010-01-26 22:01:41 +00001579ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001580 SourceLocation StartLoc,
1581 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001582 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001583 StorageClass S, StorageClass SCAsWritten,
1584 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001585 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001586 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001587}
1588
Douglas Gregor72172e92012-01-05 21:55:30 +00001589ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1590 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1591 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1592 0, QualType(), 0, SC_None, SC_None, 0);
1593}
1594
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001595SourceRange ParmVarDecl::getSourceRange() const {
1596 if (!hasInheritedDefaultArg()) {
1597 SourceRange ArgRange = getDefaultArgRange();
1598 if (ArgRange.isValid())
1599 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1600 }
1601
1602 return DeclaratorDecl::getSourceRange();
1603}
1604
Sebastian Redl833ef452010-01-26 22:01:41 +00001605Expr *ParmVarDecl::getDefaultArg() {
1606 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1607 assert(!hasUninstantiatedDefaultArg() &&
1608 "Default argument is not yet instantiated!");
1609
1610 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001611 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001612 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001613
Sebastian Redl833ef452010-01-26 22:01:41 +00001614 return Arg;
1615}
1616
Sebastian Redl833ef452010-01-26 22:01:41 +00001617SourceRange ParmVarDecl::getDefaultArgRange() const {
1618 if (const Expr *E = getInit())
1619 return E->getSourceRange();
1620
1621 if (hasUninstantiatedDefaultArg())
1622 return getUninstantiatedDefaultArg()->getSourceRange();
1623
1624 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001625}
1626
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001627bool ParmVarDecl::isParameterPack() const {
1628 return isa<PackExpansionType>(getType());
1629}
1630
Ted Kremenek540017e2011-10-06 05:00:56 +00001631void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1632 getASTContext().setParameterIndex(this, parameterIndex);
1633 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1634}
1635
1636unsigned ParmVarDecl::getParameterIndexLarge() const {
1637 return getASTContext().getParameterIndex(this);
1638}
1639
Nuno Lopes394ec982008-12-17 23:39:55 +00001640//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001641// FunctionDecl Implementation
1642//===----------------------------------------------------------------------===//
1643
Douglas Gregorb11aad82011-02-19 18:51:44 +00001644void FunctionDecl::getNameForDiagnostic(std::string &S,
1645 const PrintingPolicy &Policy,
1646 bool Qualified) const {
1647 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1648 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1649 if (TemplateArgs)
1650 S += TemplateSpecializationType::PrintTemplateArgumentList(
1651 TemplateArgs->data(),
1652 TemplateArgs->size(),
1653 Policy);
1654
1655}
1656
Ted Kremenek186a0742010-04-29 16:49:01 +00001657bool FunctionDecl::isVariadic() const {
1658 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1659 return FT->isVariadic();
1660 return false;
1661}
1662
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001663bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1664 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001665 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001666 Definition = *I;
1667 return true;
1668 }
1669 }
1670
1671 return false;
1672}
1673
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001674bool FunctionDecl::hasTrivialBody() const
1675{
1676 Stmt *S = getBody();
1677 if (!S) {
1678 // Since we don't have a body for this function, we don't know if it's
1679 // trivial or not.
1680 return false;
1681 }
1682
1683 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1684 return true;
1685 return false;
1686}
1687
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001688bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1689 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001690 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001691 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1692 return true;
1693 }
1694 }
1695
1696 return false;
1697}
1698
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001699Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001700 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1701 if (I->Body) {
1702 Definition = *I;
1703 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001704 } else if (I->IsLateTemplateParsed) {
1705 Definition = *I;
1706 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001707 }
1708 }
1709
1710 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001711}
1712
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001713void FunctionDecl::setBody(Stmt *B) {
1714 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001715 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001716 EndRangeLoc = B->getLocEnd();
Rafael Espindola54606d52012-12-25 07:31:49 +00001717 for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); R != REnd;
1718 ++R)
1719 R->ClearLVCache();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001720}
1721
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001722void FunctionDecl::setPure(bool P) {
1723 IsPure = P;
1724 if (P)
1725 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1726 Parent->markedVirtualFunctionPure();
1727}
1728
Douglas Gregor16618f22009-09-12 00:17:51 +00001729bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001730 const TranslationUnitDecl *tunit =
1731 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1732 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001733 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001734 getIdentifier() &&
1735 getIdentifier()->isStr("main");
1736}
1737
1738bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1739 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1740 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1741 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1742 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1743 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1744
1745 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1746 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1747
1748 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1749 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1750
1751 ASTContext &Context =
1752 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1753 ->getASTContext();
1754
1755 // The result type and first argument type are constant across all
1756 // these operators. The second argument must be exactly void*.
1757 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001758}
1759
Rafael Espindola576127d2012-12-28 14:21:58 +00001760bool FunctionDecl::hasCLanguageLinkage() const {
1761 return hasCLanguageLinkageTemplate(*this);
1762}
1763
Douglas Gregor16618f22009-09-12 00:17:51 +00001764bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001765 if (getLinkage() != ExternalLinkage)
1766 return false;
1767
1768 if (getAttr<OverloadableAttr>())
1769 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001770
Chandler Carruth4322a282011-02-25 00:05:02 +00001771 const DeclContext *DC = getDeclContext();
1772 if (DC->isRecord())
1773 return false;
1774
Eli Friedman839192f2012-01-15 01:23:58 +00001775 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001776 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001777 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001778
Eli Friedman839192f2012-01-15 01:23:58 +00001779 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001780}
1781
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001782bool FunctionDecl::isGlobal() const {
1783 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1784 return Method->isStatic();
1785
John McCall8e7d6562010-08-26 03:08:43 +00001786 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001787 return false;
1788
Mike Stump11289f42009-09-09 15:08:12 +00001789 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001790 DC->isNamespace();
1791 DC = DC->getParent()) {
1792 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1793 if (!Namespace->getDeclName())
1794 return false;
1795 break;
1796 }
1797 }
1798
1799 return true;
1800}
1801
Sebastian Redl833ef452010-01-26 22:01:41 +00001802void
1803FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1804 redeclarable_base::setPreviousDeclaration(PrevDecl);
1805
1806 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1807 FunctionTemplateDecl *PrevFunTmpl
1808 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1809 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1810 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1811 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001812
Axel Naumannfbc7b982011-11-08 18:21:06 +00001813 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001814 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001815}
1816
1817const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1818 return getFirstDeclaration();
1819}
1820
1821FunctionDecl *FunctionDecl::getCanonicalDecl() {
1822 return getFirstDeclaration();
1823}
1824
Douglas Gregorbf62d642010-12-06 18:36:25 +00001825void FunctionDecl::setStorageClass(StorageClass SC) {
1826 assert(isLegalForFunction(SC));
1827 if (getStorageClass() != SC)
Rafael Espindola54606d52012-12-25 07:31:49 +00001828 ClearLVCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001829
1830 SClass = SC;
1831}
1832
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001833/// \brief Returns a value indicating whether this function
1834/// corresponds to a builtin function.
1835///
1836/// The function corresponds to a built-in function if it is
1837/// declared at translation scope or within an extern "C" block and
1838/// its name matches with the name of a builtin. The returned value
1839/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001840/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001841/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001842unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001843 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001844 return 0;
1845
1846 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001847 if (!BuiltinID)
1848 return 0;
1849
1850 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001851 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1852 return BuiltinID;
1853
1854 // This function has the name of a known C library
1855 // function. Determine whether it actually refers to the C library
1856 // function or whether it just has the same name.
1857
Douglas Gregora908e7f2009-02-17 03:23:10 +00001858 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001859 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001860 return 0;
1861
Douglas Gregore711f702009-02-14 18:57:46 +00001862 // If this function is at translation-unit scope and we're not in
1863 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001864 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001865 getDeclContext()->isTranslationUnit())
1866 return BuiltinID;
1867
1868 // If the function is in an extern "C" linkage specification and is
1869 // not marked "overloadable", it's the real function.
1870 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001871 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001872 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001873 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001874 return BuiltinID;
1875
1876 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001877 return 0;
1878}
1879
1880
Chris Lattner47c0d002009-04-25 06:03:53 +00001881/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001882/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001883/// after it has been created.
1884unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001885 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001886 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001887 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001888 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001889
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001890}
1891
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001892void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001893 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001894 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001895 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001896
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001897 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001898 if (!NewParamInfo.empty()) {
1899 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1900 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001901 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001902}
Chris Lattner41943152007-01-25 04:52:46 +00001903
James Molloy6f8780b2012-02-29 10:24:19 +00001904void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1905 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1906
1907 if (!NewDecls.empty()) {
1908 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1909 std::copy(NewDecls.begin(), NewDecls.end(), A);
1910 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1911 }
1912}
1913
Chris Lattner58258242008-04-10 02:22:51 +00001914/// getMinRequiredArguments - Returns the minimum number of arguments
1915/// needed to call this function. This may be fewer than the number of
1916/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001917/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001918unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001919 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001920 return getNumParams();
1921
Douglas Gregor7825bf32011-01-06 22:09:01 +00001922 unsigned NumRequiredArgs = getNumParams();
1923
1924 // If the last parameter is a parameter pack, we don't need an argument for
1925 // it.
1926 if (NumRequiredArgs > 0 &&
1927 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1928 --NumRequiredArgs;
1929
1930 // If this parameter has a default argument, we don't need an argument for
1931 // it.
1932 while (NumRequiredArgs > 0 &&
1933 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001934 --NumRequiredArgs;
1935
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001936 // We might have parameter packs before the end. These can't be deduced,
1937 // but they can still handle multiple arguments.
1938 unsigned ArgIdx = NumRequiredArgs;
1939 while (ArgIdx > 0) {
1940 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1941 NumRequiredArgs = ArgIdx;
1942
1943 --ArgIdx;
1944 }
1945
Chris Lattner58258242008-04-10 02:22:51 +00001946 return NumRequiredArgs;
1947}
1948
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001949bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001950 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001951 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001952
1953 if (isa<CXXMethodDecl>(this)) {
1954 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1955 return true;
1956 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001957
1958 switch (getTemplateSpecializationKind()) {
1959 case TSK_Undeclared:
1960 case TSK_ExplicitSpecialization:
1961 return false;
1962
1963 case TSK_ImplicitInstantiation:
1964 case TSK_ExplicitInstantiationDeclaration:
1965 case TSK_ExplicitInstantiationDefinition:
1966 // Handle below.
1967 break;
1968 }
1969
1970 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001971 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001972 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001973 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001974
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001975 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001976 return PatternDecl->isInlined();
1977
1978 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001979}
1980
Eli Friedman1b125c32012-02-07 03:50:18 +00001981static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1982 // Only consider file-scope declarations in this test.
1983 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1984 return false;
1985
1986 // Only consider explicit declarations; the presence of a builtin for a
1987 // libcall shouldn't affect whether a definition is externally visible.
1988 if (Redecl->isImplicit())
1989 return false;
1990
1991 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1992 return true; // Not an inline definition
1993
1994 return false;
1995}
1996
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001997/// \brief For a function declaration in C or C++, determine whether this
1998/// declaration causes the definition to be externally visible.
1999///
Eli Friedman1b125c32012-02-07 03:50:18 +00002000/// Specifically, this determines if adding the current declaration to the set
2001/// of redeclarations of the given functions causes
2002/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002003bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2004 assert(!doesThisDeclarationHaveABody() &&
2005 "Must have a declaration without a body.");
2006
2007 ASTContext &Context = getASTContext();
2008
David Blaikiebbafb8a2012-03-11 07:00:24 +00002009 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002010 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2011 // an externally visible definition.
2012 //
2013 // FIXME: What happens if gnu_inline gets added on after the first
2014 // declaration?
2015 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
2016 return false;
2017
2018 const FunctionDecl *Prev = this;
2019 bool FoundBody = false;
2020 while ((Prev = Prev->getPreviousDecl())) {
2021 FoundBody |= Prev->Body;
2022
2023 if (Prev->Body) {
2024 // If it's not the case that both 'inline' and 'extern' are
2025 // specified on the definition, then it is always externally visible.
2026 if (!Prev->isInlineSpecified() ||
2027 Prev->getStorageClassAsWritten() != SC_Extern)
2028 return false;
2029 } else if (Prev->isInlineSpecified() &&
2030 Prev->getStorageClassAsWritten() != SC_Extern) {
2031 return false;
2032 }
2033 }
2034 return FoundBody;
2035 }
2036
David Blaikiebbafb8a2012-03-11 07:00:24 +00002037 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002038 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002039
2040 // C99 6.7.4p6:
2041 // [...] If all of the file scope declarations for a function in a
2042 // translation unit include the inline function specifier without extern,
2043 // then the definition in that translation unit is an inline definition.
2044 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002045 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002046 const FunctionDecl *Prev = this;
2047 bool FoundBody = false;
2048 while ((Prev = Prev->getPreviousDecl())) {
2049 FoundBody |= Prev->Body;
2050 if (RedeclForcesDefC99(Prev))
2051 return false;
2052 }
2053 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002054}
2055
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002056/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002057/// definition will be externally visible.
2058///
2059/// Inline function definitions are always available for inlining optimizations.
2060/// However, depending on the language dialect, declaration specifiers, and
2061/// attributes, the definition of an inline function may or may not be
2062/// "externally" visible to other translation units in the program.
2063///
2064/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002065/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002066/// inline definition becomes externally visible (C99 6.7.4p6).
2067///
2068/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2069/// definition, we use the GNU semantics for inline, which are nearly the
2070/// opposite of C99 semantics. In particular, "inline" by itself will create
2071/// an externally visible symbol, but "extern inline" will not create an
2072/// externally visible symbol.
2073bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002074 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002075 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002076 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002077
David Blaikiebbafb8a2012-03-11 07:00:24 +00002078 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002079 // Note: If you change the logic here, please change
2080 // doesDeclarationForceExternallyVisibleDefinition as well.
2081 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002082 // If it's not the case that both 'inline' and 'extern' are
2083 // specified on the definition, then this inline definition is
2084 // externally visible.
2085 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2086 return true;
2087
2088 // If any declaration is 'inline' but not 'extern', then this definition
2089 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002090 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2091 Redecl != RedeclEnd;
2092 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002093 if (Redecl->isInlineSpecified() &&
2094 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002095 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002096 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002097
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002098 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002099 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002100
Douglas Gregor299d76e2009-09-13 07:46:26 +00002101 // C99 6.7.4p6:
2102 // [...] If all of the file scope declarations for a function in a
2103 // translation unit include the inline function specifier without extern,
2104 // then the definition in that translation unit is an inline definition.
2105 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2106 Redecl != RedeclEnd;
2107 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002108 if (RedeclForcesDefC99(*Redecl))
2109 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002110 }
2111
2112 // C99 6.7.4p6:
2113 // An inline definition does not provide an external definition for the
2114 // function, and does not forbid an external definition in another
2115 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002116 return false;
2117}
2118
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002119/// getOverloadedOperator - Which C++ overloaded operator this
2120/// function represents, if any.
2121OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002122 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2123 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002124 else
2125 return OO_None;
2126}
2127
Alexis Huntc88db062010-01-13 09:01:02 +00002128/// getLiteralIdentifier - The literal suffix identifier this function
2129/// represents, if any.
2130const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2131 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2132 return getDeclName().getCXXLiteralIdentifier();
2133 else
2134 return 0;
2135}
2136
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002137FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2138 if (TemplateOrSpecialization.isNull())
2139 return TK_NonTemplate;
2140 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2141 return TK_FunctionTemplate;
2142 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2143 return TK_MemberSpecialization;
2144 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2145 return TK_FunctionTemplateSpecialization;
2146 if (TemplateOrSpecialization.is
2147 <DependentFunctionTemplateSpecializationInfo*>())
2148 return TK_DependentFunctionTemplateSpecialization;
2149
David Blaikie83d382b2011-09-23 05:06:16 +00002150 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002151}
2152
Douglas Gregord801b062009-10-07 23:56:10 +00002153FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002154 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002155 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2156
2157 return 0;
2158}
2159
Douglas Gregor06db9f52009-10-12 20:18:28 +00002160MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2161 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2162}
2163
Douglas Gregord801b062009-10-07 23:56:10 +00002164void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002165FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2166 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002167 TemplateSpecializationKind TSK) {
2168 assert(TemplateOrSpecialization.isNull() &&
2169 "Member function is already a specialization");
2170 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002171 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002172 TemplateOrSpecialization = Info;
2173}
2174
Douglas Gregorafca3b42009-10-27 20:53:28 +00002175bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002176 // If the function is invalid, it can't be implicitly instantiated.
2177 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002178 return false;
2179
2180 switch (getTemplateSpecializationKind()) {
2181 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002182 case TSK_ExplicitInstantiationDefinition:
2183 return false;
2184
2185 case TSK_ImplicitInstantiation:
2186 return true;
2187
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002188 // It is possible to instantiate TSK_ExplicitSpecialization kind
2189 // if the FunctionDecl has a class scope specialization pattern.
2190 case TSK_ExplicitSpecialization:
2191 return getClassScopeSpecializationPattern() != 0;
2192
Douglas Gregorafca3b42009-10-27 20:53:28 +00002193 case TSK_ExplicitInstantiationDeclaration:
2194 // Handled below.
2195 break;
2196 }
2197
2198 // Find the actual template from which we will instantiate.
2199 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002200 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002201 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002202 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002203
2204 // C++0x [temp.explicit]p9:
2205 // Except for inline functions, other explicit instantiation declarations
2206 // have the effect of suppressing the implicit instantiation of the entity
2207 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002208 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002209 return true;
2210
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002211 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002212}
2213
2214bool FunctionDecl::isTemplateInstantiation() const {
2215 switch (getTemplateSpecializationKind()) {
2216 case TSK_Undeclared:
2217 case TSK_ExplicitSpecialization:
2218 return false;
2219 case TSK_ImplicitInstantiation:
2220 case TSK_ExplicitInstantiationDeclaration:
2221 case TSK_ExplicitInstantiationDefinition:
2222 return true;
2223 }
2224 llvm_unreachable("All TSK values handled.");
2225}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002226
2227FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002228 // Handle class scope explicit specialization special case.
2229 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2230 return getClassScopeSpecializationPattern();
2231
Douglas Gregorafca3b42009-10-27 20:53:28 +00002232 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2233 while (Primary->getInstantiatedFromMemberTemplate()) {
2234 // If we have hit a point where the user provided a specialization of
2235 // this template, we're done looking.
2236 if (Primary->isMemberSpecialization())
2237 break;
2238
2239 Primary = Primary->getInstantiatedFromMemberTemplate();
2240 }
2241
2242 return Primary->getTemplatedDecl();
2243 }
2244
2245 return getInstantiatedFromMemberFunction();
2246}
2247
Douglas Gregor70d83e22009-06-29 17:30:29 +00002248FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002249 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002250 = TemplateOrSpecialization
2251 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002252 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002253 }
2254 return 0;
2255}
2256
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002257FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2258 return getASTContext().getClassScopeSpecializationPattern(this);
2259}
2260
Douglas Gregor70d83e22009-06-29 17:30:29 +00002261const TemplateArgumentList *
2262FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002263 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002264 = TemplateOrSpecialization
2265 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002266 return Info->TemplateArguments;
2267 }
2268 return 0;
2269}
2270
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002271const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002272FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2273 if (FunctionTemplateSpecializationInfo *Info
2274 = TemplateOrSpecialization
2275 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2276 return Info->TemplateArgumentsAsWritten;
2277 }
2278 return 0;
2279}
2280
Mike Stump11289f42009-09-09 15:08:12 +00002281void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002282FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2283 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002284 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002285 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002286 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002287 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2288 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002289 assert(TSK != TSK_Undeclared &&
2290 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002291 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002292 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002293 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002294 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2295 TemplateArgs,
2296 TemplateArgsAsWritten,
2297 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002298 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002299 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002300}
2301
John McCallb9c78482010-04-08 09:05:18 +00002302void
2303FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2304 const UnresolvedSetImpl &Templates,
2305 const TemplateArgumentListInfo &TemplateArgs) {
2306 assert(TemplateOrSpecialization.isNull());
2307 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2308 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002309 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002310 void *Buffer = Context.Allocate(Size);
2311 DependentFunctionTemplateSpecializationInfo *Info =
2312 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2313 TemplateArgs);
2314 TemplateOrSpecialization = Info;
2315}
2316
2317DependentFunctionTemplateSpecializationInfo::
2318DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2319 const TemplateArgumentListInfo &TArgs)
2320 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2321
2322 d.NumTemplates = Ts.size();
2323 d.NumArgs = TArgs.size();
2324
2325 FunctionTemplateDecl **TsArray =
2326 const_cast<FunctionTemplateDecl**>(getTemplates());
2327 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2328 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2329
2330 TemplateArgumentLoc *ArgsArray =
2331 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2332 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2333 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2334}
2335
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002336TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002337 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002338 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002339 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002340 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002341 if (FTSInfo)
2342 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002343
Douglas Gregord801b062009-10-07 23:56:10 +00002344 MemberSpecializationInfo *MSInfo
2345 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2346 if (MSInfo)
2347 return MSInfo->getTemplateSpecializationKind();
2348
2349 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002350}
2351
Mike Stump11289f42009-09-09 15:08:12 +00002352void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002353FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2354 SourceLocation PointOfInstantiation) {
2355 if (FunctionTemplateSpecializationInfo *FTSInfo
2356 = TemplateOrSpecialization.dyn_cast<
2357 FunctionTemplateSpecializationInfo*>()) {
2358 FTSInfo->setTemplateSpecializationKind(TSK);
2359 if (TSK != TSK_ExplicitSpecialization &&
2360 PointOfInstantiation.isValid() &&
2361 FTSInfo->getPointOfInstantiation().isInvalid())
2362 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2363 } else if (MemberSpecializationInfo *MSInfo
2364 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2365 MSInfo->setTemplateSpecializationKind(TSK);
2366 if (TSK != TSK_ExplicitSpecialization &&
2367 PointOfInstantiation.isValid() &&
2368 MSInfo->getPointOfInstantiation().isInvalid())
2369 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2370 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002371 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002372}
2373
2374SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002375 if (FunctionTemplateSpecializationInfo *FTSInfo
2376 = TemplateOrSpecialization.dyn_cast<
2377 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002378 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002379 else if (MemberSpecializationInfo *MSInfo
2380 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002381 return MSInfo->getPointOfInstantiation();
2382
2383 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002384}
2385
Douglas Gregor6411b922009-09-11 20:15:17 +00002386bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002387 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002388 return true;
2389
2390 // If this function was instantiated from a member function of a
2391 // class template, check whether that member function was defined out-of-line.
2392 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2393 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002394 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002395 return Definition->isOutOfLine();
2396 }
2397
2398 // If this function was instantiated from a function template,
2399 // check whether that function template was defined out-of-line.
2400 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2401 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002402 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002403 return Definition->isOutOfLine();
2404 }
2405
2406 return false;
2407}
2408
Abramo Bagnaraea947882011-03-08 16:41:52 +00002409SourceRange FunctionDecl::getSourceRange() const {
2410 return SourceRange(getOuterLocStart(), EndRangeLoc);
2411}
2412
Anna Zaks28db7ce2012-01-18 02:45:01 +00002413unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002414 IdentifierInfo *FnInfo = getIdentifier();
2415
2416 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002417 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002418
2419 // Builtin handling.
2420 switch (getBuiltinID()) {
2421 case Builtin::BI__builtin_memset:
2422 case Builtin::BI__builtin___memset_chk:
2423 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002424 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002425
2426 case Builtin::BI__builtin_memcpy:
2427 case Builtin::BI__builtin___memcpy_chk:
2428 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002429 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002430
2431 case Builtin::BI__builtin_memmove:
2432 case Builtin::BI__builtin___memmove_chk:
2433 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002434 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002435
2436 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002437 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002438 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002439 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002440
2441 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002442 case Builtin::BImemcmp:
2443 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002444
2445 case Builtin::BI__builtin_strncpy:
2446 case Builtin::BI__builtin___strncpy_chk:
2447 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002448 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002449
2450 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002451 case Builtin::BIstrncmp:
2452 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002453
2454 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002455 case Builtin::BIstrncasecmp:
2456 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002457
2458 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002459 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002460 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002461 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002462
2463 case Builtin::BI__builtin_strndup:
2464 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002465 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002466
Anna Zaks314cd092012-02-01 19:08:57 +00002467 case Builtin::BI__builtin_strlen:
2468 case Builtin::BIstrlen:
2469 return Builtin::BIstrlen;
2470
Anna Zaks201d4892012-01-13 21:52:01 +00002471 default:
Rafael Espindola5cab0292012-12-30 17:23:09 +00002472 if (hasCLanguageLinkage()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002473 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002474 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002475 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002476 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002477 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002478 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002479 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002480 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002481 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002482 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002483 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002484 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002485 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002486 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002487 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002488 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002489 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002490 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002491 else if (FnInfo->isStr("strlen"))
2492 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002493 }
2494 break;
2495 }
Anna Zaks22122702012-01-17 00:37:07 +00002496 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002497}
2498
Chris Lattner59a25942008-03-31 00:36:02 +00002499//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002500// FieldDecl Implementation
2501//===----------------------------------------------------------------------===//
2502
Jay Foad39c79802011-01-12 09:06:06 +00002503FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002504 SourceLocation StartLoc, SourceLocation IdLoc,
2505 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002506 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002507 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002508 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002509 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002510}
2511
Douglas Gregor72172e92012-01-05 21:55:30 +00002512FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2513 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2514 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002515 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002516}
2517
Sebastian Redl833ef452010-01-26 22:01:41 +00002518bool FieldDecl::isAnonymousStructOrUnion() const {
2519 if (!isImplicit() || getDeclName())
2520 return false;
2521
2522 if (const RecordType *Record = getType()->getAs<RecordType>())
2523 return Record->getDecl()->isAnonymousStructOrUnion();
2524
2525 return false;
2526}
2527
Richard Smithcaf33902011-10-10 18:28:20 +00002528unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2529 assert(isBitField() && "not a bitfield");
2530 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2531 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2532}
2533
John McCall4e819612011-01-20 07:57:12 +00002534unsigned FieldDecl::getFieldIndex() const {
2535 if (CachedFieldIndex) return CachedFieldIndex - 1;
2536
Richard Smithd62306a2011-11-10 06:34:14 +00002537 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002538 const RecordDecl *RD = getParent();
2539 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002540 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002541
2542 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2543 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002544 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002545
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002546 if (IsMsStruct) {
2547 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002548 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002549 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002550 continue;
2551 }
David Blaikie40ed2972012-06-06 20:45:41 +00002552 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002553 }
John McCall4e819612011-01-20 07:57:12 +00002554 }
2555
Richard Smithd62306a2011-11-10 06:34:14 +00002556 assert(CachedFieldIndex && "failed to find field in parent");
2557 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002558}
2559
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002560SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002561 if (const Expr *E = InitializerOrBitWidth.getPointer())
2562 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002563 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002564}
2565
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002566void FieldDecl::setBitWidth(Expr *Width) {
2567 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2568 "bit width or initializer already set");
2569 InitializerOrBitWidth.setPointer(Width);
2570}
2571
Richard Smith938f40b2011-06-11 17:19:42 +00002572void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002573 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002574 "bit width or initializer already set");
2575 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002576}
2577
Sebastian Redl833ef452010-01-26 22:01:41 +00002578//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002579// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002580//===----------------------------------------------------------------------===//
2581
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002582SourceLocation TagDecl::getOuterLocStart() const {
2583 return getTemplateOrInnerLocStart(this);
2584}
2585
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002586SourceRange TagDecl::getSourceRange() const {
2587 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002588 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002589}
2590
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002591TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002592 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002593}
2594
Richard Smithdda56e42011-04-15 14:24:37 +00002595void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2596 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002597 if (TypeForDecl)
Rafael Espindola54606d52012-12-25 07:31:49 +00002598 const_cast<Type*>(TypeForDecl)->ClearLVCache();
2599 ClearLVCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002600}
2601
Douglas Gregordee1be82009-01-17 00:42:38 +00002602void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002603 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002604
David Blaikie095deba2012-11-14 01:52:05 +00002605 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002606 struct CXXRecordDecl::DefinitionData *Data =
2607 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002608 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2609 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002610 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002611}
2612
2613void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002614 assert((!isa<CXXRecordDecl>(this) ||
2615 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2616 "definition completed but not started");
2617
John McCallf937c022011-10-07 06:10:15 +00002618 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002619 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002620
2621 if (ASTMutationListener *L = getASTMutationListener())
2622 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002623}
2624
John McCallf937c022011-10-07 06:10:15 +00002625TagDecl *TagDecl::getDefinition() const {
2626 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002627 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002628 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2629 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002630
2631 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002632 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002633 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002634 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002635
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002636 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002637}
2638
Douglas Gregor14454802011-02-25 02:25:35 +00002639void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2640 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002641 // Make sure the extended qualifier info is allocated.
2642 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002643 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002644 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002645 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002646 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002647 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002648 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002649 if (getExtInfo()->NumTemplParamLists == 0) {
2650 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002651 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002652 }
2653 else
2654 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002655 }
2656 }
2657}
2658
Abramo Bagnara60804e12011-03-18 15:16:37 +00002659void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2660 unsigned NumTPLists,
2661 TemplateParameterList **TPLists) {
2662 assert(NumTPLists > 0);
2663 // Make sure the extended decl info is allocated.
2664 if (!hasExtInfo())
2665 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002666 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002667 // Set the template parameter lists info.
2668 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2669}
2670
Ted Kremenek21475702008-09-05 17:16:31 +00002671//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002672// EnumDecl Implementation
2673//===----------------------------------------------------------------------===//
2674
David Blaikie68e081d2011-12-20 02:48:34 +00002675void EnumDecl::anchor() { }
2676
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002677EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2678 SourceLocation StartLoc, SourceLocation IdLoc,
2679 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002680 EnumDecl *PrevDecl, bool IsScoped,
2681 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002682 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002683 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002684 C.getTypeDeclType(Enum, PrevDecl);
2685 return Enum;
2686}
2687
Douglas Gregor72172e92012-01-05 21:55:30 +00002688EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2689 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2690 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2691 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002692}
2693
Douglas Gregord5058122010-02-11 01:19:42 +00002694void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002695 QualType NewPromotionType,
2696 unsigned NumPositiveBits,
2697 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002698 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002699 if (!IntegerType)
2700 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002701 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002702 setNumPositiveBits(NumPositiveBits);
2703 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002704 TagDecl::completeDefinition();
2705}
2706
Richard Smith7d137e32012-03-23 03:33:32 +00002707TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2708 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2709 return MSI->getTemplateSpecializationKind();
2710
2711 return TSK_Undeclared;
2712}
2713
2714void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2715 SourceLocation PointOfInstantiation) {
2716 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2717 assert(MSI && "Not an instantiated member enumeration?");
2718 MSI->setTemplateSpecializationKind(TSK);
2719 if (TSK != TSK_ExplicitSpecialization &&
2720 PointOfInstantiation.isValid() &&
2721 MSI->getPointOfInstantiation().isInvalid())
2722 MSI->setPointOfInstantiation(PointOfInstantiation);
2723}
2724
Richard Smith4b38ded2012-03-14 23:13:10 +00002725EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2726 if (SpecializationInfo)
2727 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2728
2729 return 0;
2730}
2731
2732void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2733 TemplateSpecializationKind TSK) {
2734 assert(!SpecializationInfo && "Member enum is already a specialization");
2735 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2736}
2737
Sebastian Redl833ef452010-01-26 22:01:41 +00002738//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002739// RecordDecl Implementation
2740//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002741
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002742RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2743 SourceLocation StartLoc, SourceLocation IdLoc,
2744 IdentifierInfo *Id, RecordDecl *PrevDecl)
2745 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002746 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002747 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002748 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002749 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002750 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002751}
2752
Jay Foad39c79802011-01-12 09:06:06 +00002753RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002754 SourceLocation StartLoc, SourceLocation IdLoc,
2755 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2756 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2757 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002758 C.getTypeDeclType(R, PrevDecl);
2759 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002760}
2761
Douglas Gregor72172e92012-01-05 21:55:30 +00002762RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2763 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2764 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2765 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002766}
2767
Douglas Gregordfcad112009-03-25 15:59:44 +00002768bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002769 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002770 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2771}
2772
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002773RecordDecl::field_iterator RecordDecl::field_begin() const {
2774 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2775 LoadFieldsFromExternalStorage();
2776
2777 return field_iterator(decl_iterator(FirstDecl));
2778}
2779
Douglas Gregorb11aad82011-02-19 18:51:44 +00002780/// completeDefinition - Notes that the definition of this type is now
2781/// complete.
2782void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002783 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002784 TagDecl::completeDefinition();
2785}
2786
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002787/// isMsStruct - Get whether or not this record uses ms_struct layout.
2788/// This which can be turned on with an attribute, pragma, or the
2789/// -mms-bitfields command-line option.
2790bool RecordDecl::isMsStruct(const ASTContext &C) const {
2791 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2792}
2793
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002794static bool isFieldOrIndirectField(Decl::Kind K) {
2795 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2796}
2797
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002798void RecordDecl::LoadFieldsFromExternalStorage() const {
2799 ExternalASTSource *Source = getASTContext().getExternalSource();
2800 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2801
2802 // Notify that we have a RecordDecl doing some initialization.
2803 ExternalASTSource::Deserializing TheFields(Source);
2804
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002805 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002806 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002807 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2808 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002809 case ELR_Success:
2810 break;
2811
2812 case ELR_AlreadyLoaded:
2813 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002814 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002815 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002816
2817#ifndef NDEBUG
2818 // Check that all decls we got were FieldDecls.
2819 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002820 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002821#endif
2822
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002823 if (Decls.empty())
2824 return;
2825
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002826 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2827 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002828}
2829
Steve Naroff415d3d52008-10-08 17:01:13 +00002830//===----------------------------------------------------------------------===//
2831// BlockDecl Implementation
2832//===----------------------------------------------------------------------===//
2833
David Blaikie9c70e042011-09-21 18:16:56 +00002834void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002835 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002836
Steve Naroffc4b30e52009-03-13 16:56:44 +00002837 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002838 if (!NewParamInfo.empty()) {
2839 NumParams = NewParamInfo.size();
2840 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2841 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002842 }
2843}
2844
John McCall351762c2011-02-07 10:33:21 +00002845void BlockDecl::setCaptures(ASTContext &Context,
2846 const Capture *begin,
2847 const Capture *end,
2848 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002849 CapturesCXXThis = capturesCXXThis;
2850
2851 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002852 NumCaptures = 0;
2853 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002854 return;
2855 }
2856
John McCall351762c2011-02-07 10:33:21 +00002857 NumCaptures = end - begin;
2858
2859 // Avoid new Capture[] because we don't want to provide a default
2860 // constructor.
2861 size_t allocationSize = NumCaptures * sizeof(Capture);
2862 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2863 memcpy(buffer, begin, allocationSize);
2864 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002865}
Sebastian Redl833ef452010-01-26 22:01:41 +00002866
John McCallce45f882011-06-15 22:51:16 +00002867bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2868 for (capture_const_iterator
2869 i = capture_begin(), e = capture_end(); i != e; ++i)
2870 // Only auto vars can be captured, so no redeclaration worries.
2871 if (i->getVariable() == variable)
2872 return true;
2873
2874 return false;
2875}
2876
Douglas Gregor70226da2010-12-21 16:27:07 +00002877SourceRange BlockDecl::getSourceRange() const {
2878 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2879}
Sebastian Redl833ef452010-01-26 22:01:41 +00002880
2881//===----------------------------------------------------------------------===//
2882// Other Decl Allocation/Deallocation Method Implementations
2883//===----------------------------------------------------------------------===//
2884
David Blaikie68e081d2011-12-20 02:48:34 +00002885void TranslationUnitDecl::anchor() { }
2886
Sebastian Redl833ef452010-01-26 22:01:41 +00002887TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2888 return new (C) TranslationUnitDecl(C);
2889}
2890
David Blaikie68e081d2011-12-20 02:48:34 +00002891void LabelDecl::anchor() { }
2892
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002893LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002894 SourceLocation IdentL, IdentifierInfo *II) {
2895 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2896}
2897
2898LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2899 SourceLocation IdentL, IdentifierInfo *II,
2900 SourceLocation GnuLabelL) {
2901 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2902 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002903}
2904
Douglas Gregor72172e92012-01-05 21:55:30 +00002905LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2906 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2907 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002908}
2909
David Blaikie68e081d2011-12-20 02:48:34 +00002910void ValueDecl::anchor() { }
2911
Benjamin Kramerea70eb32012-12-01 15:09:41 +00002912bool ValueDecl::isWeak() const {
2913 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
2914 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
2915 return true;
2916
2917 return isWeakImported();
2918}
2919
David Blaikie68e081d2011-12-20 02:48:34 +00002920void ImplicitParamDecl::anchor() { }
2921
Sebastian Redl833ef452010-01-26 22:01:41 +00002922ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002923 SourceLocation IdLoc,
2924 IdentifierInfo *Id,
2925 QualType Type) {
2926 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002927}
2928
Douglas Gregor72172e92012-01-05 21:55:30 +00002929ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2930 unsigned ID) {
2931 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2932 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2933}
2934
Sebastian Redl833ef452010-01-26 22:01:41 +00002935FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002936 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002937 const DeclarationNameInfo &NameInfo,
2938 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002939 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002940 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002941 bool hasWrittenPrototype,
2942 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002943 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2944 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002945 isInlineSpecified,
2946 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002947 New->HasWrittenPrototype = hasWrittenPrototype;
2948 return New;
2949}
2950
Douglas Gregor72172e92012-01-05 21:55:30 +00002951FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2952 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2953 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2954 DeclarationNameInfo(), QualType(), 0,
2955 SC_None, SC_None, false, false);
2956}
2957
Sebastian Redl833ef452010-01-26 22:01:41 +00002958BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2959 return new (C) BlockDecl(DC, L);
2960}
2961
Douglas Gregor72172e92012-01-05 21:55:30 +00002962BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2963 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2964 return new (Mem) BlockDecl(0, SourceLocation());
2965}
2966
Sebastian Redl833ef452010-01-26 22:01:41 +00002967EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2968 SourceLocation L,
2969 IdentifierInfo *Id, QualType T,
2970 Expr *E, const llvm::APSInt &V) {
2971 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2972}
2973
Douglas Gregor72172e92012-01-05 21:55:30 +00002974EnumConstantDecl *
2975EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2976 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2977 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2978 llvm::APSInt());
2979}
2980
David Blaikie68e081d2011-12-20 02:48:34 +00002981void IndirectFieldDecl::anchor() { }
2982
Benjamin Kramer39593702010-11-21 14:11:41 +00002983IndirectFieldDecl *
2984IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2985 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2986 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002987 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2988}
2989
Douglas Gregor72172e92012-01-05 21:55:30 +00002990IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2991 unsigned ID) {
2992 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2993 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2994 QualType(), 0, 0);
2995}
2996
Douglas Gregorbe996932010-09-01 20:41:53 +00002997SourceRange EnumConstantDecl::getSourceRange() const {
2998 SourceLocation End = getLocation();
2999 if (Init)
3000 End = Init->getLocEnd();
3001 return SourceRange(getLocation(), End);
3002}
3003
David Blaikie68e081d2011-12-20 02:48:34 +00003004void TypeDecl::anchor() { }
3005
Sebastian Redl833ef452010-01-26 22:01:41 +00003006TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003007 SourceLocation StartLoc, SourceLocation IdLoc,
3008 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3009 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00003010}
3011
David Blaikie68e081d2011-12-20 02:48:34 +00003012void TypedefNameDecl::anchor() { }
3013
Douglas Gregor72172e92012-01-05 21:55:30 +00003014TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3015 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3016 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3017}
3018
Richard Smithdda56e42011-04-15 14:24:37 +00003019TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3020 SourceLocation StartLoc,
3021 SourceLocation IdLoc, IdentifierInfo *Id,
3022 TypeSourceInfo *TInfo) {
3023 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3024}
3025
Douglas Gregor72172e92012-01-05 21:55:30 +00003026TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3027 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3028 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3029}
3030
Abramo Bagnaraea947882011-03-08 16:41:52 +00003031SourceRange TypedefDecl::getSourceRange() const {
3032 SourceLocation RangeEnd = getLocation();
3033 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3034 if (typeIsPostfix(TInfo->getType()))
3035 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3036 }
3037 return SourceRange(getLocStart(), RangeEnd);
3038}
3039
Richard Smithdda56e42011-04-15 14:24:37 +00003040SourceRange TypeAliasDecl::getSourceRange() const {
3041 SourceLocation RangeEnd = getLocStart();
3042 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3043 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3044 return SourceRange(getLocStart(), RangeEnd);
3045}
3046
David Blaikie68e081d2011-12-20 02:48:34 +00003047void FileScopeAsmDecl::anchor() { }
3048
Sebastian Redl833ef452010-01-26 22:01:41 +00003049FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003050 StringLiteral *Str,
3051 SourceLocation AsmLoc,
3052 SourceLocation RParenLoc) {
3053 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003054}
Douglas Gregorba345522011-12-02 23:23:56 +00003055
Douglas Gregor72172e92012-01-05 21:55:30 +00003056FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3057 unsigned ID) {
3058 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3059 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3060}
3061
Douglas Gregorba345522011-12-02 23:23:56 +00003062//===----------------------------------------------------------------------===//
3063// ImportDecl Implementation
3064//===----------------------------------------------------------------------===//
3065
3066/// \brief Retrieve the number of module identifiers needed to name the given
3067/// module.
3068static unsigned getNumModuleIdentifiers(Module *Mod) {
3069 unsigned Result = 1;
3070 while (Mod->Parent) {
3071 Mod = Mod->Parent;
3072 ++Result;
3073 }
3074 return Result;
3075}
3076
Douglas Gregor22d09742012-01-03 18:04:46 +00003077ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003078 Module *Imported,
3079 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003080 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003081 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003082{
3083 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3084 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3085 memcpy(StoredLocs, IdentifierLocs.data(),
3086 IdentifierLocs.size() * sizeof(SourceLocation));
3087}
3088
Douglas Gregor22d09742012-01-03 18:04:46 +00003089ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003090 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003091 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003092 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003093{
3094 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3095}
3096
3097ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003098 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003099 ArrayRef<SourceLocation> IdentifierLocs) {
3100 void *Mem = C.Allocate(sizeof(ImportDecl) +
3101 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003102 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003103}
3104
3105ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003106 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003107 Module *Imported,
3108 SourceLocation EndLoc) {
3109 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003110 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003111 Import->setImplicit();
3112 return Import;
3113}
3114
Douglas Gregor72172e92012-01-05 21:55:30 +00003115ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3116 unsigned NumLocations) {
3117 void *Mem = AllocateDeserializedDecl(C, ID,
3118 (sizeof(ImportDecl) +
3119 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003120 return new (Mem) ImportDecl(EmptyShell());
3121}
3122
3123ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3124 if (!ImportedAndComplete.getInt())
3125 return ArrayRef<SourceLocation>();
3126
3127 const SourceLocation *StoredLocs
3128 = reinterpret_cast<const SourceLocation *>(this + 1);
3129 return ArrayRef<SourceLocation>(StoredLocs,
3130 getNumModuleIdentifiers(getImportedModule()));
3131}
3132
3133SourceRange ImportDecl::getSourceRange() const {
3134 if (!ImportedAndComplete.getInt())
3135 return SourceRange(getLocation(),
3136 *reinterpret_cast<const SourceLocation *>(this + 1));
3137
3138 return SourceRange(getLocation(), getIdentifierLocs().back());
3139}