blob: 986430595bd61e228b2f95d907658e5a2d7a9229 [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 Espindola19de5612013-01-12 06:42:30 +0000107/// getLVForDecl - Get the linkage and visibility for the given declaration.
108static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000109
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000110/// \brief Get the most restrictive linkage for the types and
111/// declarations in the given template argument list.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000112static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args,
113 unsigned NumArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000114 bool OnlyTemplate) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000115 LinkageInfo LV(ExternalLinkage, DefaultVisibility, false);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000116
117 for (unsigned I = 0; I != NumArgs; ++I) {
118 switch (Args[I].getKind()) {
119 case TemplateArgument::Null:
120 case TemplateArgument::Integral:
121 case TemplateArgument::Expression:
122 break;
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000123
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000124 case TemplateArgument::Type:
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000125 LV.mergeWithMin(getLVForType(Args[I].getAsType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000126 break;
127
128 case TemplateArgument::Declaration:
Eli Friedmanb826a002012-09-26 02:36:12 +0000129 if (NamedDecl *ND = dyn_cast<NamedDecl>(Args[I].getAsDecl()))
130 LV.mergeWithMin(getLVForDecl(ND, OnlyTemplate));
131 break;
132
133 case TemplateArgument::NullPtr:
134 LV.mergeWithMin(getLVForType(Args[I].getNullPtrType()));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000135 break;
136
137 case TemplateArgument::Template:
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000138 case TemplateArgument::TemplateExpansion:
Rafael Espindolaeeb9d9f2012-01-02 06:26:22 +0000139 if (TemplateDecl *Template
Douglas Gregore4ff4b52011-01-05 18:58:31 +0000140 = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl())
Rafael Espindolab522a5f2012-04-23 17:51:55 +0000141 LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000142 break;
143
144 case TemplateArgument::Pack:
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000145 LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(),
146 Args[I].pack_size(),
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000147 OnlyTemplate));
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000148 break;
149 }
150 }
151
John McCall457a04e2010-10-22 21:05:15 +0000152 return LV;
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000153}
154
Rafael Espindola2f869a32012-01-14 00:30:36 +0000155static LinkageInfo
Douglas Gregorbf62d642010-12-06 18:36:25 +0000156getLVForTemplateArgumentList(const TemplateArgumentList &TArgs,
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000157 bool OnlyTemplate) {
158 return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate);
John McCall8823c652010-08-13 08:35:10 +0000159}
160
Rafael Espindola340941d2012-05-25 16:41:35 +0000161static bool shouldConsiderTemplateVis(const FunctionDecl *fn,
Rafael Espindola96dcb8d2012-05-21 20:31:27 +0000162 const FunctionTemplateSpecializationInfo *spec) {
163 return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000164}
165
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000166static bool
167shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) {
Rafael Espindola93c289c2012-05-21 20:15:56 +0000168 return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization();
John McCallb8c604a2011-06-27 23:06:04 +0000169}
170
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000171static bool useInlineVisibilityHidden(const NamedDecl *D) {
172 // FIXME: we should warn if -fvisibility-inlines-hidden is used with c.
Rafael Espindola5cc78902012-07-13 23:26:43 +0000173 const LangOptions &Opts = D->getASTContext().getLangOpts();
174 if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden)
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000175 return false;
176
177 const FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
178 if (!FD)
179 return false;
180
181 TemplateSpecializationKind TSK = TSK_Undeclared;
182 if (FunctionTemplateSpecializationInfo *spec
183 = FD->getTemplateSpecializationInfo()) {
184 TSK = spec->getTemplateSpecializationKind();
185 } else if (MemberSpecializationInfo *MSI =
186 FD->getMemberSpecializationInfo()) {
187 TSK = MSI->getTemplateSpecializationKind();
188 }
189
190 const FunctionDecl *Def = 0;
191 // InlineVisibilityHidden only applies to definitions, and
192 // isInlined() only gives meaningful answers on definitions
193 // anyway.
194 return TSK != TSK_ExplicitInstantiationDeclaration &&
195 TSK != TSK_ExplicitInstantiationDefinition &&
Rafael Espindolafb9d4b42012-10-11 16:32:25 +0000196 FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>();
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000197}
198
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000199static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
200 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000201 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000202 "Not a name having namespace scope");
203 ASTContext &Context = D->getASTContext();
204
205 // C++ [basic.link]p3:
206 // A name having namespace scope (3.3.6) has internal linkage if it
207 // is the name of
208 // - an object, reference, function or function template that is
209 // explicitly declared static; or,
210 // (This bullet corresponds to C99 6.2.2p3.)
211 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
212 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000213 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000214 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000215
Richard Smithdc0ef452012-10-19 06:37:48 +0000216 // - a non-volatile object or reference that is explicitly declared const
217 // or constexpr and neither explicitly declared extern nor previously
218 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000219 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000220 Var->getType().isConstQualified() &&
221 !Var->getType().isVolatileQualified() &&
John McCall8e7d6562010-08-26 03:08:43 +0000222 Var->getStorageClass() != SC_Extern &&
223 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000224 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000225 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000226 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000227 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000228 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000229 FoundExtern = true;
230
231 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000232 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000233 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000234 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000235 const VarDecl *PrevVar = Var->getPreviousDecl();
236 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000237 if (PrevVar->getStorageClass() == SC_PrivateExtern)
238 break;
Eli Friedmana7137bc2012-10-26 23:05:34 +0000239 if (PrevVar)
240 return PrevVar->getLinkageAndVisibility();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000241 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000242 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000243 // C++ [temp]p4:
244 // A non-member function template can have internal linkage; any
245 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000246 const FunctionDecl *Function = 0;
247 if (const FunctionTemplateDecl *FunTmpl
248 = dyn_cast<FunctionTemplateDecl>(D))
249 Function = FunTmpl->getTemplatedDecl();
250 else
251 Function = cast<FunctionDecl>(D);
252
253 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000254 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000255 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000256 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
257 // - a data member of an anonymous union.
258 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000259 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000260 }
261
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000262 if (D->isInAnonymousNamespace()) {
263 const VarDecl *Var = dyn_cast<VarDecl>(D);
264 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000265 if ((!Var || !Var->hasCLanguageLinkage()) &&
266 (!Func || !Func->hasCLanguageLinkage()))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000267 return LinkageInfo::uniqueExternal();
268 }
John McCallb7139c42010-10-28 04:18:25 +0000269
John McCall457a04e2010-10-22 21:05:15 +0000270 // Set up the defaults.
271
272 // C99 6.2.2p5:
273 // If the declaration of an identifier for an object has file
274 // scope and no storage-class specifier, its linkage is
275 // external.
John McCallc273f242010-10-30 11:50:40 +0000276 LinkageInfo LV;
277
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000278 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000279 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000280 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000281 } else {
282 // If we're declared in a namespace with a visibility attribute,
283 // use that namespace's visibility, but don't call it explicit.
284 for (const DeclContext *DC = D->getDeclContext();
285 !isa<TranslationUnitDecl>(DC);
286 DC = DC->getParent()) {
287 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
288 if (!ND) continue;
289 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000290 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000291 break;
292 }
293 }
294 }
295 }
296
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000297 if (!OnlyTemplate) {
Rafael Espindolab660efd2012-04-19 04:37:16 +0000298 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000299 // If we're paying attention to global visibility, apply
300 // -finline-visibility-hidden if this is an inline method.
301 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
302 LV.mergeVisibility(HiddenVisibility, true);
303 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000304
Douglas Gregorf73b2822009-11-25 22:24:25 +0000305 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000306
Douglas Gregorf73b2822009-11-25 22:24:25 +0000307 // A name having namespace scope has external linkage if it is the
308 // name of
309 //
310 // - an object or reference, unless it has internal linkage; or
311 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000312 // GCC applies the following optimization to variables and static
313 // data members, but not to functions:
314 //
John McCall457a04e2010-10-22 21:05:15 +0000315 // Modify the variable's LV by the LV of its type unless this is
316 // C or extern "C". This follows from [basic.link]p9:
317 // A type without linkage shall not be used as the type of a
318 // variable or function with external linkage unless
319 // - the entity has C language linkage, or
320 // - the entity is declared within an unnamed namespace, or
321 // - the entity is not used or is defined in the same
322 // translation unit.
323 // and [basic.link]p10:
324 // ...the types specified by all declarations referring to a
325 // given variable or function shall be identical...
326 // C does not have an equivalent rule.
327 //
John McCall5fe84122010-10-26 04:59:26 +0000328 // Ignore this if we've got an explicit attribute; the user
329 // probably knows what they're doing.
330 //
John McCall457a04e2010-10-22 21:05:15 +0000331 // Note that we don't want to make the variable non-external
332 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000333 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000334 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000335 LinkageInfo TypeLV = getLVForType(Var->getType());
336 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000337 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000338 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000339 }
340
John McCall23032652010-11-02 18:38:13 +0000341 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000342 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000343
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000344 // Note that Sema::MergeVarDecl already takes care of implementing
345 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
346 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000347
Douglas Gregorf73b2822009-11-25 22:24:25 +0000348 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000349 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000350 // In theory, we can modify the function's LV by the LV of its
351 // type unless it has C linkage (see comment above about variables
352 // for justification). In practice, GCC doesn't do this, so it's
353 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000354
John McCall23032652010-11-02 18:38:13 +0000355 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000356 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000357
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000358 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
359 // merging storage classes and visibility attributes, so we don't have to
360 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000361
John McCallf768aa72011-02-10 06:50:24 +0000362 // In C++, then if the type of the function uses a type with
363 // unique-external linkage, it's not legally usable from outside
364 // this translation unit. However, we should use the C linkage
365 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000366 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000367 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000368 Function->getType()->getLinkage() == UniqueExternalLinkage)
369 return LinkageInfo::uniqueExternal();
370
John McCallb8c604a2011-06-27 23:06:04 +0000371 // Consider LV from the template and the template arguments unless
372 // this is an explicit specialization with a visibility attribute.
373 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000374 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola340941d2012-05-25 16:41:35 +0000375 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
376 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
377 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
378 OnlyTemplate);
379 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000380 LV.mergeWithMin(TempLV);
Rafael Espindola340941d2012-05-25 16:41:35 +0000381 LV.mergeWithMin(ArgsLV);
382 } else {
383 LV.mergeLinkage(TempLV);
384 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000385 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000386 }
387
Douglas Gregorf73b2822009-11-25 22:24:25 +0000388 // - a named class (Clause 9), or an unnamed class defined in a
389 // typedef declaration in which the class has the typedef name
390 // for linkage purposes (7.1.3); or
391 // - a named enumeration (7.2), or an unnamed enumeration
392 // defined in a typedef declaration in which the enumeration
393 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000394 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
395 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000396 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000397 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000398
John McCall457a04e2010-10-22 21:05:15 +0000399 // If this is a class template specialization, consider the
400 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000401 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000402 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000403 // From the template.
404 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000405
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000406 // The arguments at which the template was instantiated.
407 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
408 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
409 OnlyTemplate);
410 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000411 LV.mergeWithMin(TempLV);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000412 LV.mergeWithMin(ArgsLV);
413 } else {
414 LV.mergeLinkage(TempLV);
415 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000416 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000417 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000418
419 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000420 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000421 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
422 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000423 if (!isExternalLinkage(EnumLV.linkage()))
424 return LinkageInfo::none();
425 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000426
427 // - a template, unless it is a function template that has
428 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000429 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000430 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000431 // - a namespace (7.3), unless it is declared within an unnamed
432 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000433 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
434 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000435
John McCall457a04e2010-10-22 21:05:15 +0000436 // By extension, we assign external linkage to Objective-C
437 // interfaces.
438 } else if (isa<ObjCInterfaceDecl>(D)) {
439 // fallout
440
441 // Everything not covered here has no linkage.
442 } else {
John McCallc273f242010-10-30 11:50:40 +0000443 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000444 }
445
446 // If we ended up with non-external linkage, visibility should
447 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000448 if (LV.linkage() != ExternalLinkage)
449 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000450
John McCall457a04e2010-10-22 21:05:15 +0000451 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000452}
453
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000454static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000455 // Only certain class members have linkage. Note that fields don't
456 // really have linkage, but it's convenient to say they do for the
457 // purposes of calculating linkage of pointer-to-data-member
458 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000459 if (!(isa<CXXMethodDecl>(D) ||
460 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000461 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000462 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000463 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000464
John McCall07072662010-11-02 01:45:15 +0000465 LinkageInfo LV;
466
John McCall07072662010-11-02 01:45:15 +0000467 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000468 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000469 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000470 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000471 // If we're paying attention to global visibility, apply
472 // -finline-visibility-hidden if this is an inline method.
473 //
474 // Note that we do this before merging information about
475 // the class visibility.
476 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
477 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000478 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000479
480 // If this class member has an explicit visibility attribute, the only
481 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000482 // only look for them when processing the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000483 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000484
Rafael Espindola53cf2192012-04-19 05:50:08 +0000485 // If this member has an visibility attribute, ClassF will exclude
486 // attributes on the class or command line options, keeping only information
487 // about the template instantiation. If the member has no visibility
488 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
489 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000490 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
491 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000492 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000493 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000494
495 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000496 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000497 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000498
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000499 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000500 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000501
John McCall8823c652010-08-13 08:35:10 +0000502 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000503 // If the type of the function uses a type with unique-external
504 // linkage, it's not legally usable from outside this translation unit.
505 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
506 return LinkageInfo::uniqueExternal();
507
John McCall457a04e2010-10-22 21:05:15 +0000508 // If this is a method template specialization, use the linkage for
509 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000510 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000511 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000512 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
513 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
514 OnlyTemplate);
515 TemplateParameterList *TemplateParams =
516 spec->getTemplate()->getTemplateParameters();
517 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola340941d2012-05-25 16:41:35 +0000518 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000519 LV.mergeWithMin(ArgsLV);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000520 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000521 LV.mergeWithMin(ParamsLV);
Rafael Espindola67a498c2012-05-25 17:22:33 +0000522 } else {
523 LV.mergeLinkage(ArgsLV);
524 if (!OnlyTemplate)
525 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000526 }
John McCalle6e622e2010-11-01 01:29:57 +0000527 }
John McCall457a04e2010-10-22 21:05:15 +0000528
John McCall37bb6c92010-10-29 22:22:43 +0000529 // Note that in contrast to basically every other situation, we
530 // *do* apply -fvisibility to method declarations.
531
532 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000533 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000534 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000535 // Merge template argument/parameter information for member
536 // class template specializations.
537 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
538 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
539 OnlyTemplate);
540 TemplateParameterList *TemplateParams =
541 spec->getSpecializedTemplate()->getTemplateParameters();
542 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000543 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000544 LV.mergeWithMin(ArgsLV);
Rafael Espindola4d71d0f2012-05-25 14:17:45 +0000545 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000546 LV.mergeWithMin(ParamsLV);
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000547 } else {
548 LV.mergeLinkage(ArgsLV);
549 if (!OnlyTemplate)
550 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000551 }
John McCall37bb6c92010-10-29 22:22:43 +0000552 }
553
John McCall37bb6c92010-10-29 22:22:43 +0000554 // Static data members.
555 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000556 // Modify the variable's linkage by its type, but ignore the
557 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000558 LinkageInfo TypeLV = getLVForType(VD->getType());
559 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000560 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000561 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000562 }
563
John McCall457a04e2010-10-22 21:05:15 +0000564 return LV;
John McCall8823c652010-08-13 08:35:10 +0000565}
566
John McCalld396b972011-02-08 19:01:05 +0000567static void clearLinkageForClass(const CXXRecordDecl *record) {
568 for (CXXRecordDecl::decl_iterator
569 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
570 Decl *child = *i;
571 if (isa<NamedDecl>(child))
Rafael Espindola19de5612013-01-12 06:42:30 +0000572 cast<NamedDecl>(child)->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000573 }
574}
575
David Blaikie68e081d2011-12-20 02:48:34 +0000576void NamedDecl::anchor() { }
577
Rafael Espindola19de5612013-01-12 06:42:30 +0000578void NamedDecl::ClearLinkageCache() {
John McCalld396b972011-02-08 19:01:05 +0000579 // Note that we can't skip clearing the linkage of children just
580 // because the parent doesn't have cached linkage: we don't cache
581 // when computing linkage for parent contexts.
582
Rafael Espindola19de5612013-01-12 06:42:30 +0000583 HasCachedLinkage = 0;
John McCalld396b972011-02-08 19:01:05 +0000584
585 // If we're changing the linkage of a class, we need to reset the
586 // linkage of child declarations, too.
587 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
588 clearLinkageForClass(record);
589
John McCall83779672011-02-19 02:53:41 +0000590 if (ClassTemplateDecl *temp =
591 dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) {
John McCalld396b972011-02-08 19:01:05 +0000592 // Clear linkage for the template pattern.
593 CXXRecordDecl *record = temp->getTemplatedDecl();
Rafael Espindola19de5612013-01-12 06:42:30 +0000594 record->HasCachedLinkage = 0;
John McCalld396b972011-02-08 19:01:05 +0000595 clearLinkageForClass(record);
596
John McCall83779672011-02-19 02:53:41 +0000597 // We need to clear linkage for specializations, too.
598 for (ClassTemplateDecl::spec_iterator
599 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindola19de5612013-01-12 06:42:30 +0000600 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000601 }
John McCall83779672011-02-19 02:53:41 +0000602
603 // Clear cached linkage for function template decls, too.
604 if (FunctionTemplateDecl *temp =
John McCall8f9a4292011-03-22 06:58:49 +0000605 dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) {
Rafael Espindola19de5612013-01-12 06:42:30 +0000606 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000607 for (FunctionTemplateDecl::spec_iterator
608 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindola19de5612013-01-12 06:42:30 +0000609 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000610 }
John McCall83779672011-02-19 02:53:41 +0000611
John McCalld396b972011-02-08 19:01:05 +0000612}
613
Douglas Gregorbf62d642010-12-06 18:36:25 +0000614Linkage NamedDecl::getLinkage() const {
Rafael Espindola19de5612013-01-12 06:42:30 +0000615 if (HasCachedLinkage) {
616 assert(Linkage(CachedLinkage) ==
617 getLVForDecl(this, true).linkage());
618 return Linkage(CachedLinkage);
619 }
620
621 CachedLinkage = getLVForDecl(this, true).linkage();
622 HasCachedLinkage = 1;
623
624#ifndef NDEBUG
625 verifyLinkage();
626#endif
627
628 return Linkage(CachedLinkage);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000629}
630
John McCallc273f242010-10-30 11:50:40 +0000631LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola19de5612013-01-12 06:42:30 +0000632 LinkageInfo LI = getLVForDecl(this, false);
633 if (HasCachedLinkage) {
634 assert(Linkage(CachedLinkage) == LI.linkage());
635 return LI;
Rafael Espindola54606d52012-12-25 07:31:49 +0000636 }
Rafael Espindola19de5612013-01-12 06:42:30 +0000637 HasCachedLinkage = 1;
638 CachedLinkage = LI.linkage();
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000639
640#ifndef NDEBUG
Rafael Espindola19de5612013-01-12 06:42:30 +0000641 verifyLinkage();
642#endif
643
644 return LI;
645}
646
647void NamedDecl::verifyLinkage() const {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000648 // In C (because of gnu inline) and in c++ with microsoft extensions an
649 // static can follow an extern, so we can have two decls with different
650 // linkages.
651 const LangOptions &Opts = getASTContext().getLangOpts();
652 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
Rafael Espindola19de5612013-01-12 06:42:30 +0000653 return;
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000654
655 // We have just computed the linkage for this decl. By induction we know
656 // that all other computed linkages match, check that the one we just computed
657 // also does.
658 NamedDecl *D = NULL;
659 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
660 NamedDecl *T = cast<NamedDecl>(*I);
661 if (T == this)
662 continue;
Rafael Espindola19de5612013-01-12 06:42:30 +0000663 if (T->HasCachedLinkage != 0) {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000664 D = T;
665 break;
666 }
667 }
668 assert(!D || D->CachedLinkage == CachedLinkage);
John McCall033caa52010-10-29 00:29:13 +0000669}
Ted Kremenek926d8602010-04-20 23:15:35 +0000670
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000671llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
672 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000673 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000674 if (llvm::Optional<Visibility> V = getVisibilityOf(Var))
Rafael Espindola96e68242012-05-16 02:10:38 +0000675 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000676
Rafael Espindola96e68242012-05-16 02:10:38 +0000677 if (Var->isStaticDataMember()) {
678 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
679 if (InstantiatedFrom)
680 return getVisibilityOf(InstantiatedFrom);
681 }
682
683 return llvm::Optional<Visibility>();
684 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000685 // Use the most recent declaration of a function, and also handle
686 // function template specializations.
687 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000688 if (llvm::Optional<Visibility> V = getVisibilityOf(fn))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000689 return V;
690
691 // If the function is a specialization of a template with an
692 // explicit visibility attribute, use that.
693 if (FunctionTemplateSpecializationInfo *templateInfo
694 = fn->getTemplateSpecializationInfo())
695 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
696
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000697 // If the function is a member of a specialization of a class template
698 // and the corresponding decl has explicit visibility, use that.
699 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
700 if (InstantiatedFrom)
701 return getVisibilityOf(InstantiatedFrom);
702
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000703 return llvm::Optional<Visibility>();
704 }
705
706 // Otherwise, just check the declaration itself first.
707 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
708 return V;
709
Rafael Espindolafb4263f2012-07-31 19:02:02 +0000710 // The visibility of a template is stored in the templated decl.
711 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
712 return getVisibilityOf(TD->getTemplatedDecl());
713
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000714 // If there wasn't explicit visibility there, and this is a
715 // specialization of a class template, check for visibility
716 // on the pattern.
717 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolaeca5cd22012-07-13 01:19:08 +0000718 = dyn_cast<ClassTemplateSpecializationDecl>(this))
719 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000720
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000721 // If this is a member class of a specialization of a class template
722 // and the corresponding decl has explicit visibility, use that.
723 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
724 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
725 if (InstantiatedFrom)
726 return getVisibilityOf(InstantiatedFrom);
727 }
728
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000729 return llvm::Optional<Visibility>();
730}
731
Rafael Espindola19de5612013-01-12 06:42:30 +0000732static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000733 // Objective-C: treat all Objective-C declarations as having external
734 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000735 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000736 default:
737 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000738 case Decl::ParmVar:
739 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000740 case Decl::TemplateTemplateParm: // count these as external
741 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000742 case Decl::ObjCAtDefsField:
743 case Decl::ObjCCategory:
744 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000745 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000746 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000747 case Decl::ObjCMethod:
748 case Decl::ObjCProperty:
749 case Decl::ObjCPropertyImpl:
750 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000751 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000752
753 case Decl::CXXRecord: {
754 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
755 if (Record->isLambda()) {
756 if (!Record->getLambdaManglingNumber()) {
757 // This lambda has no mangling number, so it's internal.
758 return LinkageInfo::internal();
759 }
760
761 // This lambda has its linkage/visibility determined by its owner.
762 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
763 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
764 if (isa<ParmVarDecl>(ContextDecl))
765 DC = ContextDecl->getDeclContext()->getRedeclContext();
766 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000767 return getLVForDecl(cast<NamedDecl>(ContextDecl),
768 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000769 }
770
771 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000772 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000773
774 return LinkageInfo::external();
775 }
776
777 break;
778 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000779 }
780
Douglas Gregorf73b2822009-11-25 22:24:25 +0000781 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000782 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000783 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000784
785 // C++ [basic.link]p5:
786 // In addition, a member function, static data member, a named
787 // class or enumeration of class scope, or an unnamed class or
788 // enumeration defined in a class-scope typedef declaration such
789 // that the class or enumeration has the typedef name for linkage
790 // purposes (7.1.3), has external linkage if the name of the class
791 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000792 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000793 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000794
795 // C++ [basic.link]p6:
796 // The name of a function declared in block scope and the name of
797 // an object declared by a block scope extern declaration have
798 // linkage. If there is a visible declaration of an entity with
799 // linkage having the same name and type, ignoring entities
800 // declared outside the innermost enclosing namespace scope, the
801 // block scope declaration declares that same entity and receives
802 // the linkage of the previous declaration. If there is more than
803 // one such matching entity, the program is ill-formed. Otherwise,
804 // if no matching entity is found, the block scope entity receives
805 // external linkage.
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000806 if (D->getDeclContext()->isFunctionOrMethod()) {
John McCall033caa52010-10-29 00:29:13 +0000807 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000808 if (Function->isInAnonymousNamespace() &&
809 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000810 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000811
Rafael Espindola3bd836a2013-01-09 16:34:58 +0000812 // This is a "void f();" which got merged with a file static.
813 if (Function->getStorageClass() == SC_Static)
814 return LinkageInfo::internal();
815
John McCallc273f242010-10-30 11:50:40 +0000816 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000817 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000818 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000819 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000820 }
Rafael Espindolac12edd42012-11-29 16:38:22 +0000821
822 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
823 // merging storage classes and visibility attributes, so we don't have to
824 // look at previous decls in here.
John McCall457a04e2010-10-22 21:05:15 +0000825
826 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000827 }
828
John McCall033caa52010-10-29 00:29:13 +0000829 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
Rafael Espindola74a133f2012-12-18 04:18:55 +0000830 if (Var->getStorageClassAsWritten() == SC_Extern ||
831 Var->getStorageClassAsWritten() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000832 if (Var->isInAnonymousNamespace() &&
833 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000834 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000835
Rafael Espindola74a133f2012-12-18 04:18:55 +0000836 // This is an "extern int foo;" which got merged with a file static.
837 if (Var->getStorageClass() == SC_Static)
838 return LinkageInfo::internal();
839
John McCallc273f242010-10-30 11:50:40 +0000840 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000841 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000842 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000843 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000844 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000845 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000846 }
John McCall457a04e2010-10-22 21:05:15 +0000847
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000848 // Note that Sema::MergeVarDecl already takes care of implementing
849 // C99 6.2.2p4 and propagating the visibility attribute, so we don't
850 // have to do it here.
John McCall457a04e2010-10-22 21:05:15 +0000851 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000852 }
853 }
854
855 // C++ [basic.link]p6:
856 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000857 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000858}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000859
Douglas Gregor2ada0482009-02-04 17:27:36 +0000860std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000861 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000862}
863
864std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000865 const DeclContext *Ctx = getDeclContext();
866
867 if (Ctx->isFunctionOrMethod())
868 return getNameAsString();
869
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000870 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000871 ContextsTy Contexts;
872
873 // Collect contexts.
874 while (Ctx && isa<NamedDecl>(Ctx)) {
875 Contexts.push_back(Ctx);
876 Ctx = Ctx->getParent();
877 };
878
879 std::string QualName;
880 llvm::raw_string_ostream OS(QualName);
881
882 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
883 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000884 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000885 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000886 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
887 std::string TemplateArgsStr
888 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000889 TemplateArgs.data(),
890 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000891 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000892 OS << Spec->getName() << TemplateArgsStr;
893 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000894 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000895 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000896 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000897 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000898 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
899 if (!RD->getIdentifier())
900 OS << "<anonymous " << RD->getKindName() << '>';
901 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000902 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000903 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000904 const FunctionProtoType *FT = 0;
905 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +0000906 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +0000907
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000908 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000909 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000910 unsigned NumParams = FD->getNumParams();
911 for (unsigned i = 0; i < NumParams; ++i) {
912 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000913 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000914 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000915 }
916
917 if (FT->isVariadic()) {
918 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000919 OS << ", ";
920 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000921 }
922 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000923 OS << ')';
924 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000925 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000926 }
927 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000928 }
929
John McCalla2a3f7d2010-03-16 21:48:18 +0000930 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000931 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000932 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000933 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000934
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000935 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000936}
937
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000938bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000939 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
940
Douglas Gregor889ceb72009-02-03 19:21:40 +0000941 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
942 // We want to keep it, unless it nominates same namespace.
943 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000944 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
945 ->getOriginalNamespace() ==
946 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
947 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000948 }
Mike Stump11289f42009-09-09 15:08:12 +0000949
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000950 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
951 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000952 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000953
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000954 // For function templates, the underlying function declarations are linked.
955 if (const FunctionTemplateDecl *FunctionTemplate
956 = dyn_cast<FunctionTemplateDecl>(this))
957 if (const FunctionTemplateDecl *OldFunctionTemplate
958 = dyn_cast<FunctionTemplateDecl>(OldD))
959 return FunctionTemplate->getTemplatedDecl()
960 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000961
Steve Naroffc4173fa2009-02-22 19:35:57 +0000962 // For method declarations, we keep track of redeclarations.
963 if (isa<ObjCMethodDecl>(this))
964 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000965
John McCall9f3059a2009-10-09 21:13:30 +0000966 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
967 return true;
968
John McCall3f746822009-11-17 05:59:44 +0000969 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
970 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
971 cast<UsingShadowDecl>(OldD)->getTargetDecl();
972
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000973 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
974 ASTContext &Context = getASTContext();
975 return Context.getCanonicalNestedNameSpecifier(
976 cast<UsingDecl>(this)->getQualifier()) ==
977 Context.getCanonicalNestedNameSpecifier(
978 cast<UsingDecl>(OldD)->getQualifier());
979 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000980
Douglas Gregorb59643b2012-01-03 23:26:26 +0000981 // A typedef of an Objective-C class type can replace an Objective-C class
982 // declaration or definition, and vice versa.
983 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
984 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
985 return true;
986
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000987 // For non-function declarations, if the declarations are of the
988 // same kind then this must be a redeclaration, or semantic analysis
989 // would not have given us the new declaration.
990 return this->getKind() == OldD->getKind();
991}
992
Douglas Gregoreddf4332009-02-24 20:03:32 +0000993bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000994 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000995}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000996
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000997NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000998 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +0000999 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1000 ND = UD->getTargetDecl();
1001
1002 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1003 return AD->getClassInterface();
1004
1005 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001006}
1007
John McCalla8ae2222010-04-06 21:38:20 +00001008bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001009 if (!isCXXClassMember())
1010 return false;
1011
John McCalla8ae2222010-04-06 21:38:20 +00001012 const NamedDecl *D = this;
1013 if (isa<UsingShadowDecl>(D))
1014 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1015
Francois Pichet783dd6e2010-11-21 06:08:52 +00001016 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001017 return true;
1018 if (isa<CXXMethodDecl>(D))
1019 return cast<CXXMethodDecl>(D)->isInstance();
1020 if (isa<FunctionTemplateDecl>(D))
1021 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1022 ->getTemplatedDecl())->isInstance();
1023 return false;
1024}
1025
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001026//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001027// DeclaratorDecl Implementation
1028//===----------------------------------------------------------------------===//
1029
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001030template <typename DeclT>
1031static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1032 if (decl->getNumTemplateParameterLists() > 0)
1033 return decl->getTemplateParameterList(0)->getTemplateLoc();
1034 else
1035 return decl->getInnerLocStart();
1036}
1037
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001038SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001039 TypeSourceInfo *TSI = getTypeSourceInfo();
1040 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001041 return SourceLocation();
1042}
1043
Douglas Gregor14454802011-02-25 02:25:35 +00001044void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1045 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001046 // Make sure the extended decl info is allocated.
1047 if (!hasExtInfo()) {
1048 // Save (non-extended) type source info pointer.
1049 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1050 // Allocate external info struct.
1051 DeclInfo = new (getASTContext()) ExtInfo;
1052 // Restore savedTInfo into (extended) decl info.
1053 getExtInfo()->TInfo = savedTInfo;
1054 }
1055 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001056 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001057 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001058 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001059 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001060 if (getExtInfo()->NumTemplParamLists == 0) {
1061 // Save type source info pointer.
1062 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1063 // Deallocate the extended decl info.
1064 getASTContext().Deallocate(getExtInfo());
1065 // Restore savedTInfo into (non-extended) decl info.
1066 DeclInfo = savedTInfo;
1067 }
1068 else
1069 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001070 }
1071 }
1072}
1073
Abramo Bagnara60804e12011-03-18 15:16:37 +00001074void
1075DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1076 unsigned NumTPLists,
1077 TemplateParameterList **TPLists) {
1078 assert(NumTPLists > 0);
1079 // Make sure the extended decl info is allocated.
1080 if (!hasExtInfo()) {
1081 // Save (non-extended) type source info pointer.
1082 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1083 // Allocate external info struct.
1084 DeclInfo = new (getASTContext()) ExtInfo;
1085 // Restore savedTInfo into (extended) decl info.
1086 getExtInfo()->TInfo = savedTInfo;
1087 }
1088 // Set the template parameter lists info.
1089 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1090}
1091
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001092SourceLocation DeclaratorDecl::getOuterLocStart() const {
1093 return getTemplateOrInnerLocStart(this);
1094}
1095
Abramo Bagnaraea947882011-03-08 16:41:52 +00001096namespace {
1097
1098// Helper function: returns true if QT is or contains a type
1099// having a postfix component.
1100bool typeIsPostfix(clang::QualType QT) {
1101 while (true) {
1102 const Type* T = QT.getTypePtr();
1103 switch (T->getTypeClass()) {
1104 default:
1105 return false;
1106 case Type::Pointer:
1107 QT = cast<PointerType>(T)->getPointeeType();
1108 break;
1109 case Type::BlockPointer:
1110 QT = cast<BlockPointerType>(T)->getPointeeType();
1111 break;
1112 case Type::MemberPointer:
1113 QT = cast<MemberPointerType>(T)->getPointeeType();
1114 break;
1115 case Type::LValueReference:
1116 case Type::RValueReference:
1117 QT = cast<ReferenceType>(T)->getPointeeType();
1118 break;
1119 case Type::PackExpansion:
1120 QT = cast<PackExpansionType>(T)->getPattern();
1121 break;
1122 case Type::Paren:
1123 case Type::ConstantArray:
1124 case Type::DependentSizedArray:
1125 case Type::IncompleteArray:
1126 case Type::VariableArray:
1127 case Type::FunctionProto:
1128 case Type::FunctionNoProto:
1129 return true;
1130 }
1131 }
1132}
1133
1134} // namespace
1135
1136SourceRange DeclaratorDecl::getSourceRange() const {
1137 SourceLocation RangeEnd = getLocation();
1138 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1139 if (typeIsPostfix(TInfo->getType()))
1140 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1141 }
1142 return SourceRange(getOuterLocStart(), RangeEnd);
1143}
1144
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001145void
Douglas Gregor20527e22010-06-15 17:44:38 +00001146QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1147 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001148 TemplateParameterList **TPLists) {
1149 assert((NumTPLists == 0 || TPLists != 0) &&
1150 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001151
1152 // Free previous template parameters (if any).
1153 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001154 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001155 TemplParamLists = 0;
1156 NumTemplParamLists = 0;
1157 }
1158 // Set info on matched template parameter lists (if any).
1159 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001160 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001161 NumTemplParamLists = NumTPLists;
1162 for (unsigned i = NumTPLists; i-- > 0; )
1163 TemplParamLists[i] = TPLists[i];
1164 }
1165}
1166
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001167//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001168// VarDecl Implementation
1169//===----------------------------------------------------------------------===//
1170
Sebastian Redl833ef452010-01-26 22:01:41 +00001171const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1172 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001173 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001174 case SC_Auto: return "auto";
1175 case SC_Extern: return "extern";
1176 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1177 case SC_PrivateExtern: return "__private_extern__";
1178 case SC_Register: return "register";
1179 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001180 }
1181
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001182 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001183}
1184
Abramo Bagnaradff19302011-03-08 08:55:46 +00001185VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1186 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001187 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001188 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001189 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001190}
1191
Douglas Gregor72172e92012-01-05 21:55:30 +00001192VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1193 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1194 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1195 QualType(), 0, SC_None, SC_None);
1196}
1197
Douglas Gregorbf62d642010-12-06 18:36:25 +00001198void VarDecl::setStorageClass(StorageClass SC) {
1199 assert(isLegalForVariable(SC));
1200 if (getStorageClass() != SC)
Rafael Espindola19de5612013-01-12 06:42:30 +00001201 ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001202
John McCallbeaa11c2011-05-01 02:13:58 +00001203 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001204}
1205
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001206SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001207 if (const Expr *Init = getInit()) {
1208 SourceLocation InitEnd = Init->getLocEnd();
1209 if (InitEnd.isValid())
1210 return SourceRange(getOuterLocStart(), InitEnd);
1211 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001212 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001213}
1214
Rafael Espindola88510672013-01-04 21:18:45 +00001215template<typename T>
1216static bool hasCLanguageLinkageTemplate(const T &D) {
Rafael Espindola576127d2012-12-28 14:21:58 +00001217 // Language linkage is a C++ concept, but saying that everything in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001218 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001219 ASTContext &Context = D.getASTContext();
1220 if (!Context.getLangOpts().CPlusPlus)
1221 return true;
1222
1223 // dcl.link 4: A C language linkage is ignored in determining the language
1224 // linkage of the names of class members and the function type of class member
1225 // functions.
1226 const DeclContext *DC = D.getDeclContext();
1227 if (DC->isRecord())
1228 return false;
1229
1230 // If the first decl is in an extern "C" context, any other redeclaration
1231 // will have C language linkage. If the first one is not in an extern "C"
1232 // context, we would have reported an error for any other decl being in one.
Rafael Espindola88510672013-01-04 21:18:45 +00001233 const T *First = D.getFirstDeclaration();
Rafael Espindola576127d2012-12-28 14:21:58 +00001234 return First->getDeclContext()->isExternCContext();
1235}
1236
1237bool VarDecl::hasCLanguageLinkage() const {
1238 return hasCLanguageLinkageTemplate(*this);
1239}
1240
Sebastian Redl833ef452010-01-26 22:01:41 +00001241bool VarDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001242 if (getLinkage() != ExternalLinkage)
Chandler Carruth4322a282011-02-25 00:05:02 +00001243 return false;
1244
Eli Friedman839192f2012-01-15 01:23:58 +00001245 const DeclContext *DC = getDeclContext();
1246 if (DC->isRecord())
1247 return false;
Sebastian Redl833ef452010-01-26 22:01:41 +00001248
Eli Friedman839192f2012-01-15 01:23:58 +00001249 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001250 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001251 return true;
1252 return DC->isExternCContext();
Sebastian Redl833ef452010-01-26 22:01:41 +00001253}
1254
1255VarDecl *VarDecl::getCanonicalDecl() {
1256 return getFirstDeclaration();
1257}
1258
Daniel Dunbar9d355812012-03-09 01:51:51 +00001259VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1260 ASTContext &C) const
1261{
Sebastian Redl35351a92010-01-31 22:27:38 +00001262 // C++ [basic.def]p2:
1263 // A declaration is a definition unless [...] it contains the 'extern'
1264 // specifier or a linkage-specification and neither an initializer [...],
1265 // it declares a static data member in a class declaration [...].
1266 // C++ [temp.expl.spec]p15:
1267 // An explicit specialization of a static data member of a template is a
1268 // definition if the declaration includes an initializer; otherwise, it is
1269 // a declaration.
1270 if (isStaticDataMember()) {
1271 if (isOutOfLine() && (hasInit() ||
1272 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1273 return Definition;
1274 else
1275 return DeclarationOnly;
1276 }
1277 // C99 6.7p5:
1278 // A definition of an identifier is a declaration for that identifier that
1279 // [...] causes storage to be reserved for that object.
1280 // Note: that applies for all non-file-scope objects.
1281 // C99 6.9.2p1:
1282 // If the declaration of an identifier for an object has file scope and an
1283 // initializer, the declaration is an external definition for the identifier
1284 if (hasInit())
1285 return Definition;
1286 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1287 if (hasExternalStorage())
1288 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001289
John McCall8e7d6562010-08-26 03:08:43 +00001290 if (getStorageClassAsWritten() == SC_Extern ||
1291 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001292 for (const VarDecl *PrevVar = getPreviousDecl();
1293 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Rafael Espindola7581f322012-12-17 22:23:47 +00001294 if (PrevVar->getLinkage() == InternalLinkage)
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001295 return DeclarationOnly;
1296 }
1297 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001298 // C99 6.9.2p2:
1299 // A declaration of an object that has file scope without an initializer,
1300 // and without a storage class specifier or the scs 'static', constitutes
1301 // a tentative definition.
1302 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001303 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001304 return TentativeDefinition;
1305
1306 // What's left is (in C, block-scope) declarations without initializers or
1307 // external storage. These are definitions.
1308 return Definition;
1309}
1310
Sebastian Redl35351a92010-01-31 22:27:38 +00001311VarDecl *VarDecl::getActingDefinition() {
1312 DefinitionKind Kind = isThisDeclarationADefinition();
1313 if (Kind != TentativeDefinition)
1314 return 0;
1315
Chris Lattner48eb14d2010-06-14 18:31:46 +00001316 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001317 VarDecl *First = getFirstDeclaration();
1318 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1319 I != E; ++I) {
1320 Kind = (*I)->isThisDeclarationADefinition();
1321 if (Kind == Definition)
1322 return 0;
1323 else if (Kind == TentativeDefinition)
1324 LastTentative = *I;
1325 }
1326 return LastTentative;
1327}
1328
1329bool VarDecl::isTentativeDefinitionNow() const {
1330 DefinitionKind Kind = isThisDeclarationADefinition();
1331 if (Kind != TentativeDefinition)
1332 return false;
1333
1334 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1335 if ((*I)->isThisDeclarationADefinition() == Definition)
1336 return false;
1337 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001338 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001339}
1340
Daniel Dunbar9d355812012-03-09 01:51:51 +00001341VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001342 VarDecl *First = getFirstDeclaration();
1343 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1344 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001345 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001346 return *I;
1347 }
1348 return 0;
1349}
1350
Daniel Dunbar9d355812012-03-09 01:51:51 +00001351VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001352 DefinitionKind Kind = DeclarationOnly;
1353
1354 const VarDecl *First = getFirstDeclaration();
1355 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001356 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001357 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001358 if (Kind == Definition)
1359 break;
1360 }
John McCall37bb6c92010-10-29 22:22:43 +00001361
1362 return Kind;
1363}
1364
Sebastian Redl5ca79842010-02-01 20:16:42 +00001365const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001366 redecl_iterator I = redecls_begin(), E = redecls_end();
1367 while (I != E && !I->getInit())
1368 ++I;
1369
1370 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001371 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001372 return I->getInit();
1373 }
1374 return 0;
1375}
1376
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001377bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001378 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001379 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001380
1381 if (!isStaticDataMember())
1382 return false;
1383
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001384 // If this static data member was instantiated from a static data member of
1385 // a class template, check whether that static data member was defined
1386 // out-of-line.
1387 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1388 return VD->isOutOfLine();
1389
1390 return false;
1391}
1392
Douglas Gregor1d957a32009-10-27 18:42:08 +00001393VarDecl *VarDecl::getOutOfLineDefinition() {
1394 if (!isStaticDataMember())
1395 return 0;
1396
1397 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1398 RD != RDEnd; ++RD) {
1399 if (RD->getLexicalDeclContext()->isFileContext())
1400 return *RD;
1401 }
1402
1403 return 0;
1404}
1405
Douglas Gregord5058122010-02-11 01:19:42 +00001406void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001407 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1408 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001409 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001410 }
1411
1412 Init = I;
1413}
1414
Daniel Dunbar9d355812012-03-09 01:51:51 +00001415bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001416 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001417
Richard Smith35ecb362012-03-02 04:14:40 +00001418 if (!Lang.CPlusPlus)
1419 return false;
1420
1421 // In C++11, any variable of reference type can be used in a constant
1422 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001423 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001424 return true;
1425
1426 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001427 // not require the variable to be non-volatile, but we consider this to be a
1428 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001429 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001430 return false;
1431
1432 // In C++, const, non-volatile variables of integral or enumeration types
1433 // can be used in constant expressions.
1434 if (getType()->isIntegralOrEnumerationType())
1435 return true;
1436
Richard Smith35ecb362012-03-02 04:14:40 +00001437 // Additionally, in C++11, non-volatile constexpr variables can be used in
1438 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001439 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001440}
1441
Richard Smithd0b4dd62011-12-19 06:19:21 +00001442/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1443/// form, which contains extra information on the evaluated value of the
1444/// initializer.
1445EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1446 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1447 if (!Eval) {
1448 Stmt *S = Init.get<Stmt *>();
1449 Eval = new (getASTContext()) EvaluatedStmt;
1450 Eval->Value = S;
1451 Init = Eval;
1452 }
1453 return Eval;
1454}
1455
Richard Smithdafff942012-01-14 04:30:29 +00001456APValue *VarDecl::evaluateValue() const {
1457 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1458 return evaluateValue(Notes);
1459}
1460
1461APValue *VarDecl::evaluateValue(
1462 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001463 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1464
1465 // We only produce notes indicating why an initializer is non-constant the
1466 // first time it is evaluated. FIXME: The notes won't always be emitted the
1467 // first time we try evaluation, so might not be produced at all.
1468 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001469 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001470
1471 const Expr *Init = cast<Expr>(Eval->Value);
1472 assert(!Init->isValueDependent());
1473
1474 if (Eval->IsEvaluating) {
1475 // FIXME: Produce a diagnostic for self-initialization.
1476 Eval->CheckedICE = true;
1477 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001478 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001479 }
1480
1481 Eval->IsEvaluating = true;
1482
1483 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1484 this, Notes);
1485
1486 // Ensure the result is an uninitialized APValue if evaluation fails.
1487 if (!Result)
1488 Eval->Evaluated = APValue();
1489
1490 Eval->IsEvaluating = false;
1491 Eval->WasEvaluated = true;
1492
1493 // In C++11, we have determined whether the initializer was a constant
1494 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001495 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001496 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001497 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001498 }
1499
Richard Smithdafff942012-01-14 04:30:29 +00001500 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001501}
1502
1503bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001504 // Initializers of weak variables are never ICEs.
1505 if (isWeak())
1506 return false;
1507
Richard Smithd0b4dd62011-12-19 06:19:21 +00001508 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1509 if (Eval->CheckedICE)
1510 // We have already checked whether this subexpression is an
1511 // integral constant expression.
1512 return Eval->IsICE;
1513
1514 const Expr *Init = cast<Expr>(Eval->Value);
1515 assert(!Init->isValueDependent());
1516
1517 // In C++11, evaluate the initializer to check whether it's a constant
1518 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001519 if (getASTContext().getLangOpts().CPlusPlus11) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001520 llvm::SmallVector<PartialDiagnosticAt, 8> Notes;
1521 evaluateValue(Notes);
1522 return Eval->IsICE;
1523 }
1524
1525 // It's an ICE whether or not the definition we found is
1526 // out-of-line. See DR 721 and the discussion in Clang PR
1527 // 6206 for details.
1528
1529 if (Eval->CheckingICE)
1530 return false;
1531 Eval->CheckingICE = true;
1532
1533 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1534 Eval->CheckingICE = false;
1535 Eval->CheckedICE = true;
1536 return Eval->IsICE;
1537}
1538
Douglas Gregorfe314812011-06-21 17:03:29 +00001539bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001540 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001541
1542 const Expr *E = getInit();
1543 if (!E)
1544 return false;
1545
1546 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1547 E = Cleanups->getSubExpr();
1548
1549 return isa<MaterializeTemporaryExpr>(E);
1550}
1551
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001552VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001553 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001554 return cast<VarDecl>(MSI->getInstantiatedFrom());
1555
1556 return 0;
1557}
1558
Douglas Gregor3c74d412009-10-14 20:14:33 +00001559TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001560 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001561 return MSI->getTemplateSpecializationKind();
1562
1563 return TSK_Undeclared;
1564}
1565
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001566MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001567 return getASTContext().getInstantiatedFromStaticDataMember(this);
1568}
1569
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001570void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1571 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001572 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001573 assert(MSI && "Not an instantiated static data member?");
1574 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001575 if (TSK != TSK_ExplicitSpecialization &&
1576 PointOfInstantiation.isValid() &&
1577 MSI->getPointOfInstantiation().isInvalid())
1578 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001579}
1580
Sebastian Redl833ef452010-01-26 22:01:41 +00001581//===----------------------------------------------------------------------===//
1582// ParmVarDecl Implementation
1583//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001584
Sebastian Redl833ef452010-01-26 22:01:41 +00001585ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001586 SourceLocation StartLoc,
1587 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001588 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001589 StorageClass S, StorageClass SCAsWritten,
1590 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001591 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001592 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001593}
1594
Douglas Gregor72172e92012-01-05 21:55:30 +00001595ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1596 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1597 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1598 0, QualType(), 0, SC_None, SC_None, 0);
1599}
1600
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001601SourceRange ParmVarDecl::getSourceRange() const {
1602 if (!hasInheritedDefaultArg()) {
1603 SourceRange ArgRange = getDefaultArgRange();
1604 if (ArgRange.isValid())
1605 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1606 }
1607
1608 return DeclaratorDecl::getSourceRange();
1609}
1610
Sebastian Redl833ef452010-01-26 22:01:41 +00001611Expr *ParmVarDecl::getDefaultArg() {
1612 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1613 assert(!hasUninstantiatedDefaultArg() &&
1614 "Default argument is not yet instantiated!");
1615
1616 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001617 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001618 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001619
Sebastian Redl833ef452010-01-26 22:01:41 +00001620 return Arg;
1621}
1622
Sebastian Redl833ef452010-01-26 22:01:41 +00001623SourceRange ParmVarDecl::getDefaultArgRange() const {
1624 if (const Expr *E = getInit())
1625 return E->getSourceRange();
1626
1627 if (hasUninstantiatedDefaultArg())
1628 return getUninstantiatedDefaultArg()->getSourceRange();
1629
1630 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001631}
1632
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001633bool ParmVarDecl::isParameterPack() const {
1634 return isa<PackExpansionType>(getType());
1635}
1636
Ted Kremenek540017e2011-10-06 05:00:56 +00001637void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1638 getASTContext().setParameterIndex(this, parameterIndex);
1639 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1640}
1641
1642unsigned ParmVarDecl::getParameterIndexLarge() const {
1643 return getASTContext().getParameterIndex(this);
1644}
1645
Nuno Lopes394ec982008-12-17 23:39:55 +00001646//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001647// FunctionDecl Implementation
1648//===----------------------------------------------------------------------===//
1649
Douglas Gregorb11aad82011-02-19 18:51:44 +00001650void FunctionDecl::getNameForDiagnostic(std::string &S,
1651 const PrintingPolicy &Policy,
1652 bool Qualified) const {
1653 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1654 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1655 if (TemplateArgs)
1656 S += TemplateSpecializationType::PrintTemplateArgumentList(
1657 TemplateArgs->data(),
1658 TemplateArgs->size(),
1659 Policy);
1660
1661}
1662
Ted Kremenek186a0742010-04-29 16:49:01 +00001663bool FunctionDecl::isVariadic() const {
1664 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1665 return FT->isVariadic();
1666 return false;
1667}
1668
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001669bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1670 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001671 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001672 Definition = *I;
1673 return true;
1674 }
1675 }
1676
1677 return false;
1678}
1679
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001680bool FunctionDecl::hasTrivialBody() const
1681{
1682 Stmt *S = getBody();
1683 if (!S) {
1684 // Since we don't have a body for this function, we don't know if it's
1685 // trivial or not.
1686 return false;
1687 }
1688
1689 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1690 return true;
1691 return false;
1692}
1693
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001694bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1695 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001696 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001697 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1698 return true;
1699 }
1700 }
1701
1702 return false;
1703}
1704
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001705Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001706 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1707 if (I->Body) {
1708 Definition = *I;
1709 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001710 } else if (I->IsLateTemplateParsed) {
1711 Definition = *I;
1712 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001713 }
1714 }
1715
1716 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001717}
1718
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001719void FunctionDecl::setBody(Stmt *B) {
1720 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001721 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001722 EndRangeLoc = B->getLocEnd();
1723}
1724
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001725void FunctionDecl::setPure(bool P) {
1726 IsPure = P;
1727 if (P)
1728 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1729 Parent->markedVirtualFunctionPure();
1730}
1731
Douglas Gregor16618f22009-09-12 00:17:51 +00001732bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001733 const TranslationUnitDecl *tunit =
1734 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1735 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001736 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001737 getIdentifier() &&
1738 getIdentifier()->isStr("main");
1739}
1740
1741bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1742 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1743 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1744 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1745 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1746 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1747
1748 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1749 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1750
1751 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1752 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1753
1754 ASTContext &Context =
1755 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1756 ->getASTContext();
1757
1758 // The result type and first argument type are constant across all
1759 // these operators. The second argument must be exactly void*.
1760 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001761}
1762
Rafael Espindola576127d2012-12-28 14:21:58 +00001763bool FunctionDecl::hasCLanguageLinkage() const {
1764 return hasCLanguageLinkageTemplate(*this);
1765}
1766
Douglas Gregor16618f22009-09-12 00:17:51 +00001767bool FunctionDecl::isExternC() const {
Eli Friedman839192f2012-01-15 01:23:58 +00001768 if (getLinkage() != ExternalLinkage)
1769 return false;
1770
1771 if (getAttr<OverloadableAttr>())
1772 return false;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001773
Chandler Carruth4322a282011-02-25 00:05:02 +00001774 const DeclContext *DC = getDeclContext();
1775 if (DC->isRecord())
1776 return false;
1777
Eli Friedman839192f2012-01-15 01:23:58 +00001778 ASTContext &Context = getASTContext();
David Blaikiebbafb8a2012-03-11 07:00:24 +00001779 if (!Context.getLangOpts().CPlusPlus)
Eli Friedman839192f2012-01-15 01:23:58 +00001780 return true;
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001781
Eli Friedman839192f2012-01-15 01:23:58 +00001782 return isMain() || DC->isExternCContext();
Douglas Gregor5a80bd12009-03-02 00:19:53 +00001783}
1784
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001785bool FunctionDecl::isGlobal() const {
1786 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1787 return Method->isStatic();
1788
John McCall8e7d6562010-08-26 03:08:43 +00001789 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001790 return false;
1791
Mike Stump11289f42009-09-09 15:08:12 +00001792 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001793 DC->isNamespace();
1794 DC = DC->getParent()) {
1795 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1796 if (!Namespace->getDeclName())
1797 return false;
1798 break;
1799 }
1800 }
1801
1802 return true;
1803}
1804
Sebastian Redl833ef452010-01-26 22:01:41 +00001805void
1806FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1807 redeclarable_base::setPreviousDeclaration(PrevDecl);
1808
1809 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1810 FunctionTemplateDecl *PrevFunTmpl
1811 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1812 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1813 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1814 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001815
Axel Naumannfbc7b982011-11-08 18:21:06 +00001816 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001817 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001818}
1819
1820const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1821 return getFirstDeclaration();
1822}
1823
1824FunctionDecl *FunctionDecl::getCanonicalDecl() {
1825 return getFirstDeclaration();
1826}
1827
Douglas Gregorbf62d642010-12-06 18:36:25 +00001828void FunctionDecl::setStorageClass(StorageClass SC) {
1829 assert(isLegalForFunction(SC));
1830 if (getStorageClass() != SC)
Rafael Espindola19de5612013-01-12 06:42:30 +00001831 ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001832
1833 SClass = SC;
1834}
1835
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001836/// \brief Returns a value indicating whether this function
1837/// corresponds to a builtin function.
1838///
1839/// The function corresponds to a built-in function if it is
1840/// declared at translation scope or within an extern "C" block and
1841/// its name matches with the name of a builtin. The returned value
1842/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001843/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001844/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001845unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001846 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001847 return 0;
1848
1849 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001850 if (!BuiltinID)
1851 return 0;
1852
1853 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001854 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1855 return BuiltinID;
1856
1857 // This function has the name of a known C library
1858 // function. Determine whether it actually refers to the C library
1859 // function or whether it just has the same name.
1860
Douglas Gregora908e7f2009-02-17 03:23:10 +00001861 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001862 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001863 return 0;
1864
Douglas Gregore711f702009-02-14 18:57:46 +00001865 // If this function is at translation-unit scope and we're not in
1866 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001867 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001868 getDeclContext()->isTranslationUnit())
1869 return BuiltinID;
1870
1871 // If the function is in an extern "C" linkage specification and is
1872 // not marked "overloadable", it's the real function.
1873 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001874 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001875 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001876 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001877 return BuiltinID;
1878
1879 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001880 return 0;
1881}
1882
1883
Chris Lattner47c0d002009-04-25 06:03:53 +00001884/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001885/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001886/// after it has been created.
1887unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001888 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001889 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001890 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001891 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001892
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001893}
1894
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001895void FunctionDecl::setParams(ASTContext &C,
David Blaikie9c70e042011-09-21 18:16:56 +00001896 llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001897 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001898 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001899
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001900 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001901 if (!NewParamInfo.empty()) {
1902 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1903 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001904 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001905}
Chris Lattner41943152007-01-25 04:52:46 +00001906
James Molloy6f8780b2012-02-29 10:24:19 +00001907void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) {
1908 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1909
1910 if (!NewDecls.empty()) {
1911 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1912 std::copy(NewDecls.begin(), NewDecls.end(), A);
1913 DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size());
1914 }
1915}
1916
Chris Lattner58258242008-04-10 02:22:51 +00001917/// getMinRequiredArguments - Returns the minimum number of arguments
1918/// needed to call this function. This may be fewer than the number of
1919/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001920/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001921unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001922 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001923 return getNumParams();
1924
Douglas Gregor7825bf32011-01-06 22:09:01 +00001925 unsigned NumRequiredArgs = getNumParams();
1926
1927 // If the last parameter is a parameter pack, we don't need an argument for
1928 // it.
1929 if (NumRequiredArgs > 0 &&
1930 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1931 --NumRequiredArgs;
1932
1933 // If this parameter has a default argument, we don't need an argument for
1934 // it.
1935 while (NumRequiredArgs > 0 &&
1936 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001937 --NumRequiredArgs;
1938
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001939 // We might have parameter packs before the end. These can't be deduced,
1940 // but they can still handle multiple arguments.
1941 unsigned ArgIdx = NumRequiredArgs;
1942 while (ArgIdx > 0) {
1943 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1944 NumRequiredArgs = ArgIdx;
1945
1946 --ArgIdx;
1947 }
1948
Chris Lattner58258242008-04-10 02:22:51 +00001949 return NumRequiredArgs;
1950}
1951
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001952bool FunctionDecl::isInlined() const {
Douglas Gregorff76cb92010-12-09 16:59:22 +00001953 if (IsInline)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001954 return true;
Anders Carlssoncfb65d72009-12-04 22:35:50 +00001955
1956 if (isa<CXXMethodDecl>(this)) {
1957 if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified())
1958 return true;
1959 }
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001960
1961 switch (getTemplateSpecializationKind()) {
1962 case TSK_Undeclared:
1963 case TSK_ExplicitSpecialization:
1964 return false;
1965
1966 case TSK_ImplicitInstantiation:
1967 case TSK_ExplicitInstantiationDeclaration:
1968 case TSK_ExplicitInstantiationDefinition:
1969 // Handle below.
1970 break;
1971 }
1972
1973 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001974 bool HasPattern = false;
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001975 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001976 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001977
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001978 if (HasPattern && PatternDecl)
Douglas Gregorb7e5c842009-10-27 23:26:40 +00001979 return PatternDecl->isInlined();
1980
1981 return false;
Douglas Gregor583dcaf2009-10-27 21:11:48 +00001982}
1983
Eli Friedman1b125c32012-02-07 03:50:18 +00001984static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1985 // Only consider file-scope declarations in this test.
1986 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1987 return false;
1988
1989 // Only consider explicit declarations; the presence of a builtin for a
1990 // libcall shouldn't affect whether a definition is externally visible.
1991 if (Redecl->isImplicit())
1992 return false;
1993
1994 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1995 return true; // Not an inline definition
1996
1997 return false;
1998}
1999
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002000/// \brief For a function declaration in C or C++, determine whether this
2001/// declaration causes the definition to be externally visible.
2002///
Eli Friedman1b125c32012-02-07 03:50:18 +00002003/// Specifically, this determines if adding the current declaration to the set
2004/// of redeclarations of the given functions causes
2005/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002006bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
2007 assert(!doesThisDeclarationHaveABody() &&
2008 "Must have a declaration without a body.");
2009
2010 ASTContext &Context = getASTContext();
2011
David Blaikiebbafb8a2012-03-11 07:00:24 +00002012 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002013 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
2014 // an externally visible definition.
2015 //
2016 // FIXME: What happens if gnu_inline gets added on after the first
2017 // declaration?
2018 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
2019 return false;
2020
2021 const FunctionDecl *Prev = this;
2022 bool FoundBody = false;
2023 while ((Prev = Prev->getPreviousDecl())) {
2024 FoundBody |= Prev->Body;
2025
2026 if (Prev->Body) {
2027 // If it's not the case that both 'inline' and 'extern' are
2028 // specified on the definition, then it is always externally visible.
2029 if (!Prev->isInlineSpecified() ||
2030 Prev->getStorageClassAsWritten() != SC_Extern)
2031 return false;
2032 } else if (Prev->isInlineSpecified() &&
2033 Prev->getStorageClassAsWritten() != SC_Extern) {
2034 return false;
2035 }
2036 }
2037 return FoundBody;
2038 }
2039
David Blaikiebbafb8a2012-03-11 07:00:24 +00002040 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002041 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002042
2043 // C99 6.7.4p6:
2044 // [...] If all of the file scope declarations for a function in a
2045 // translation unit include the inline function specifier without extern,
2046 // then the definition in that translation unit is an inline definition.
2047 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002048 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002049 const FunctionDecl *Prev = this;
2050 bool FoundBody = false;
2051 while ((Prev = Prev->getPreviousDecl())) {
2052 FoundBody |= Prev->Body;
2053 if (RedeclForcesDefC99(Prev))
2054 return false;
2055 }
2056 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002057}
2058
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002059/// \brief For an inline function definition in C or C++, determine whether the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002060/// definition will be externally visible.
2061///
2062/// Inline function definitions are always available for inlining optimizations.
2063/// However, depending on the language dialect, declaration specifiers, and
2064/// attributes, the definition of an inline function may or may not be
2065/// "externally" visible to other translation units in the program.
2066///
2067/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002068/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002069/// inline definition becomes externally visible (C99 6.7.4p6).
2070///
2071/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2072/// definition, we use the GNU semantics for inline, which are nearly the
2073/// opposite of C99 semantics. In particular, "inline" by itself will create
2074/// an externally visible symbol, but "extern inline" will not create an
2075/// externally visible symbol.
2076bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002077 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002078 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002079 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002080
David Blaikiebbafb8a2012-03-11 07:00:24 +00002081 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002082 // Note: If you change the logic here, please change
2083 // doesDeclarationForceExternallyVisibleDefinition as well.
2084 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002085 // If it's not the case that both 'inline' and 'extern' are
2086 // specified on the definition, then this inline definition is
2087 // externally visible.
2088 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2089 return true;
2090
2091 // If any declaration is 'inline' but not 'extern', then this definition
2092 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002093 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2094 Redecl != RedeclEnd;
2095 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002096 if (Redecl->isInlineSpecified() &&
2097 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002098 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002099 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002100
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002101 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002102 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002103
Douglas Gregor299d76e2009-09-13 07:46:26 +00002104 // C99 6.7.4p6:
2105 // [...] If all of the file scope declarations for a function in a
2106 // translation unit include the inline function specifier without extern,
2107 // then the definition in that translation unit is an inline definition.
2108 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2109 Redecl != RedeclEnd;
2110 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002111 if (RedeclForcesDefC99(*Redecl))
2112 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002113 }
2114
2115 // C99 6.7.4p6:
2116 // An inline definition does not provide an external definition for the
2117 // function, and does not forbid an external definition in another
2118 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002119 return false;
2120}
2121
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002122/// getOverloadedOperator - Which C++ overloaded operator this
2123/// function represents, if any.
2124OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002125 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2126 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002127 else
2128 return OO_None;
2129}
2130
Alexis Huntc88db062010-01-13 09:01:02 +00002131/// getLiteralIdentifier - The literal suffix identifier this function
2132/// represents, if any.
2133const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2134 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2135 return getDeclName().getCXXLiteralIdentifier();
2136 else
2137 return 0;
2138}
2139
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002140FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2141 if (TemplateOrSpecialization.isNull())
2142 return TK_NonTemplate;
2143 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2144 return TK_FunctionTemplate;
2145 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2146 return TK_MemberSpecialization;
2147 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2148 return TK_FunctionTemplateSpecialization;
2149 if (TemplateOrSpecialization.is
2150 <DependentFunctionTemplateSpecializationInfo*>())
2151 return TK_DependentFunctionTemplateSpecialization;
2152
David Blaikie83d382b2011-09-23 05:06:16 +00002153 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002154}
2155
Douglas Gregord801b062009-10-07 23:56:10 +00002156FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002157 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002158 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2159
2160 return 0;
2161}
2162
Douglas Gregor06db9f52009-10-12 20:18:28 +00002163MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2164 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2165}
2166
Douglas Gregord801b062009-10-07 23:56:10 +00002167void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002168FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2169 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002170 TemplateSpecializationKind TSK) {
2171 assert(TemplateOrSpecialization.isNull() &&
2172 "Member function is already a specialization");
2173 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002174 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002175 TemplateOrSpecialization = Info;
2176}
2177
Douglas Gregorafca3b42009-10-27 20:53:28 +00002178bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002179 // If the function is invalid, it can't be implicitly instantiated.
2180 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002181 return false;
2182
2183 switch (getTemplateSpecializationKind()) {
2184 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002185 case TSK_ExplicitInstantiationDefinition:
2186 return false;
2187
2188 case TSK_ImplicitInstantiation:
2189 return true;
2190
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002191 // It is possible to instantiate TSK_ExplicitSpecialization kind
2192 // if the FunctionDecl has a class scope specialization pattern.
2193 case TSK_ExplicitSpecialization:
2194 return getClassScopeSpecializationPattern() != 0;
2195
Douglas Gregorafca3b42009-10-27 20:53:28 +00002196 case TSK_ExplicitInstantiationDeclaration:
2197 // Handled below.
2198 break;
2199 }
2200
2201 // Find the actual template from which we will instantiate.
2202 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002203 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002204 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002205 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002206
2207 // C++0x [temp.explicit]p9:
2208 // Except for inline functions, other explicit instantiation declarations
2209 // have the effect of suppressing the implicit instantiation of the entity
2210 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002211 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002212 return true;
2213
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002214 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002215}
2216
2217bool FunctionDecl::isTemplateInstantiation() const {
2218 switch (getTemplateSpecializationKind()) {
2219 case TSK_Undeclared:
2220 case TSK_ExplicitSpecialization:
2221 return false;
2222 case TSK_ImplicitInstantiation:
2223 case TSK_ExplicitInstantiationDeclaration:
2224 case TSK_ExplicitInstantiationDefinition:
2225 return true;
2226 }
2227 llvm_unreachable("All TSK values handled.");
2228}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002229
2230FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002231 // Handle class scope explicit specialization special case.
2232 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2233 return getClassScopeSpecializationPattern();
2234
Douglas Gregorafca3b42009-10-27 20:53:28 +00002235 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2236 while (Primary->getInstantiatedFromMemberTemplate()) {
2237 // If we have hit a point where the user provided a specialization of
2238 // this template, we're done looking.
2239 if (Primary->isMemberSpecialization())
2240 break;
2241
2242 Primary = Primary->getInstantiatedFromMemberTemplate();
2243 }
2244
2245 return Primary->getTemplatedDecl();
2246 }
2247
2248 return getInstantiatedFromMemberFunction();
2249}
2250
Douglas Gregor70d83e22009-06-29 17:30:29 +00002251FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002252 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002253 = TemplateOrSpecialization
2254 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002255 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002256 }
2257 return 0;
2258}
2259
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002260FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2261 return getASTContext().getClassScopeSpecializationPattern(this);
2262}
2263
Douglas Gregor70d83e22009-06-29 17:30:29 +00002264const TemplateArgumentList *
2265FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002266 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002267 = TemplateOrSpecialization
2268 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002269 return Info->TemplateArguments;
2270 }
2271 return 0;
2272}
2273
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002274const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002275FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2276 if (FunctionTemplateSpecializationInfo *Info
2277 = TemplateOrSpecialization
2278 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2279 return Info->TemplateArgumentsAsWritten;
2280 }
2281 return 0;
2282}
2283
Mike Stump11289f42009-09-09 15:08:12 +00002284void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002285FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2286 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002287 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002288 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002289 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002290 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2291 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002292 assert(TSK != TSK_Undeclared &&
2293 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002294 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002295 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002296 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002297 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2298 TemplateArgs,
2299 TemplateArgsAsWritten,
2300 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002301 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002302 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002303}
2304
John McCallb9c78482010-04-08 09:05:18 +00002305void
2306FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2307 const UnresolvedSetImpl &Templates,
2308 const TemplateArgumentListInfo &TemplateArgs) {
2309 assert(TemplateOrSpecialization.isNull());
2310 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2311 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002312 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002313 void *Buffer = Context.Allocate(Size);
2314 DependentFunctionTemplateSpecializationInfo *Info =
2315 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2316 TemplateArgs);
2317 TemplateOrSpecialization = Info;
2318}
2319
2320DependentFunctionTemplateSpecializationInfo::
2321DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2322 const TemplateArgumentListInfo &TArgs)
2323 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2324
2325 d.NumTemplates = Ts.size();
2326 d.NumArgs = TArgs.size();
2327
2328 FunctionTemplateDecl **TsArray =
2329 const_cast<FunctionTemplateDecl**>(getTemplates());
2330 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2331 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2332
2333 TemplateArgumentLoc *ArgsArray =
2334 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2335 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2336 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2337}
2338
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002339TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002340 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002341 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002342 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002343 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002344 if (FTSInfo)
2345 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002346
Douglas Gregord801b062009-10-07 23:56:10 +00002347 MemberSpecializationInfo *MSInfo
2348 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2349 if (MSInfo)
2350 return MSInfo->getTemplateSpecializationKind();
2351
2352 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002353}
2354
Mike Stump11289f42009-09-09 15:08:12 +00002355void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002356FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2357 SourceLocation PointOfInstantiation) {
2358 if (FunctionTemplateSpecializationInfo *FTSInfo
2359 = TemplateOrSpecialization.dyn_cast<
2360 FunctionTemplateSpecializationInfo*>()) {
2361 FTSInfo->setTemplateSpecializationKind(TSK);
2362 if (TSK != TSK_ExplicitSpecialization &&
2363 PointOfInstantiation.isValid() &&
2364 FTSInfo->getPointOfInstantiation().isInvalid())
2365 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2366 } else if (MemberSpecializationInfo *MSInfo
2367 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2368 MSInfo->setTemplateSpecializationKind(TSK);
2369 if (TSK != TSK_ExplicitSpecialization &&
2370 PointOfInstantiation.isValid() &&
2371 MSInfo->getPointOfInstantiation().isInvalid())
2372 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2373 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002374 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002375}
2376
2377SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002378 if (FunctionTemplateSpecializationInfo *FTSInfo
2379 = TemplateOrSpecialization.dyn_cast<
2380 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002381 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002382 else if (MemberSpecializationInfo *MSInfo
2383 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002384 return MSInfo->getPointOfInstantiation();
2385
2386 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002387}
2388
Douglas Gregor6411b922009-09-11 20:15:17 +00002389bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002390 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002391 return true;
2392
2393 // If this function was instantiated from a member function of a
2394 // class template, check whether that member function was defined out-of-line.
2395 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2396 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002397 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002398 return Definition->isOutOfLine();
2399 }
2400
2401 // If this function was instantiated from a function template,
2402 // check whether that function template was defined out-of-line.
2403 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2404 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002405 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002406 return Definition->isOutOfLine();
2407 }
2408
2409 return false;
2410}
2411
Abramo Bagnaraea947882011-03-08 16:41:52 +00002412SourceRange FunctionDecl::getSourceRange() const {
2413 return SourceRange(getOuterLocStart(), EndRangeLoc);
2414}
2415
Anna Zaks28db7ce2012-01-18 02:45:01 +00002416unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002417 IdentifierInfo *FnInfo = getIdentifier();
2418
2419 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002420 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002421
2422 // Builtin handling.
2423 switch (getBuiltinID()) {
2424 case Builtin::BI__builtin_memset:
2425 case Builtin::BI__builtin___memset_chk:
2426 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002427 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002428
2429 case Builtin::BI__builtin_memcpy:
2430 case Builtin::BI__builtin___memcpy_chk:
2431 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002432 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002433
2434 case Builtin::BI__builtin_memmove:
2435 case Builtin::BI__builtin___memmove_chk:
2436 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002437 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002438
2439 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002440 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002441 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002442 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002443
2444 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002445 case Builtin::BImemcmp:
2446 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002447
2448 case Builtin::BI__builtin_strncpy:
2449 case Builtin::BI__builtin___strncpy_chk:
2450 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002451 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002452
2453 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002454 case Builtin::BIstrncmp:
2455 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002456
2457 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002458 case Builtin::BIstrncasecmp:
2459 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002460
2461 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002462 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002463 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002464 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002465
2466 case Builtin::BI__builtin_strndup:
2467 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002468 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002469
Anna Zaks314cd092012-02-01 19:08:57 +00002470 case Builtin::BI__builtin_strlen:
2471 case Builtin::BIstrlen:
2472 return Builtin::BIstrlen;
2473
Anna Zaks201d4892012-01-13 21:52:01 +00002474 default:
Rafael Espindola5cab0292012-12-30 17:23:09 +00002475 if (hasCLanguageLinkage()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002476 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002477 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002478 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002479 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002480 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002481 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002482 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002483 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002484 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002485 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002486 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002487 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002488 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002489 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002490 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002491 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002492 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002493 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002494 else if (FnInfo->isStr("strlen"))
2495 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002496 }
2497 break;
2498 }
Anna Zaks22122702012-01-17 00:37:07 +00002499 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002500}
2501
Chris Lattner59a25942008-03-31 00:36:02 +00002502//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002503// FieldDecl Implementation
2504//===----------------------------------------------------------------------===//
2505
Jay Foad39c79802011-01-12 09:06:06 +00002506FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002507 SourceLocation StartLoc, SourceLocation IdLoc,
2508 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002509 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002510 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002511 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002512 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002513}
2514
Douglas Gregor72172e92012-01-05 21:55:30 +00002515FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2516 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2517 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002518 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002519}
2520
Sebastian Redl833ef452010-01-26 22:01:41 +00002521bool FieldDecl::isAnonymousStructOrUnion() const {
2522 if (!isImplicit() || getDeclName())
2523 return false;
2524
2525 if (const RecordType *Record = getType()->getAs<RecordType>())
2526 return Record->getDecl()->isAnonymousStructOrUnion();
2527
2528 return false;
2529}
2530
Richard Smithcaf33902011-10-10 18:28:20 +00002531unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2532 assert(isBitField() && "not a bitfield");
2533 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2534 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2535}
2536
John McCall4e819612011-01-20 07:57:12 +00002537unsigned FieldDecl::getFieldIndex() const {
2538 if (CachedFieldIndex) return CachedFieldIndex - 1;
2539
Richard Smithd62306a2011-11-10 06:34:14 +00002540 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002541 const RecordDecl *RD = getParent();
2542 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002543 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002544
2545 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2546 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002547 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002548
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002549 if (IsMsStruct) {
2550 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002551 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002552 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002553 continue;
2554 }
David Blaikie40ed2972012-06-06 20:45:41 +00002555 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002556 }
John McCall4e819612011-01-20 07:57:12 +00002557 }
2558
Richard Smithd62306a2011-11-10 06:34:14 +00002559 assert(CachedFieldIndex && "failed to find field in parent");
2560 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002561}
2562
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002563SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002564 if (const Expr *E = InitializerOrBitWidth.getPointer())
2565 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002566 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002567}
2568
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002569void FieldDecl::setBitWidth(Expr *Width) {
2570 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2571 "bit width or initializer already set");
2572 InitializerOrBitWidth.setPointer(Width);
2573}
2574
Richard Smith938f40b2011-06-11 17:19:42 +00002575void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002576 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002577 "bit width or initializer already set");
2578 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002579}
2580
Sebastian Redl833ef452010-01-26 22:01:41 +00002581//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002582// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002583//===----------------------------------------------------------------------===//
2584
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002585SourceLocation TagDecl::getOuterLocStart() const {
2586 return getTemplateOrInnerLocStart(this);
2587}
2588
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002589SourceRange TagDecl::getSourceRange() const {
2590 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002591 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002592}
2593
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002594TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002595 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002596}
2597
Richard Smithdda56e42011-04-15 14:24:37 +00002598void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2599 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002600 if (TypeForDecl)
Rafael Espindola19de5612013-01-12 06:42:30 +00002601 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
2602 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002603}
2604
Douglas Gregordee1be82009-01-17 00:42:38 +00002605void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002606 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002607
David Blaikie095deba2012-11-14 01:52:05 +00002608 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002609 struct CXXRecordDecl::DefinitionData *Data =
2610 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002611 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2612 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002613 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002614}
2615
2616void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002617 assert((!isa<CXXRecordDecl>(this) ||
2618 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2619 "definition completed but not started");
2620
John McCallf937c022011-10-07 06:10:15 +00002621 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002622 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002623
2624 if (ASTMutationListener *L = getASTMutationListener())
2625 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002626}
2627
John McCallf937c022011-10-07 06:10:15 +00002628TagDecl *TagDecl::getDefinition() const {
2629 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002630 return const_cast<TagDecl *>(this);
Andrew Trickba266ee2010-10-19 21:54:32 +00002631 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2632 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002633
2634 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002635 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002636 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002637 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002638
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002639 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002640}
2641
Douglas Gregor14454802011-02-25 02:25:35 +00002642void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2643 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002644 // Make sure the extended qualifier info is allocated.
2645 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002646 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002647 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002648 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002649 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002650 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002651 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002652 if (getExtInfo()->NumTemplParamLists == 0) {
2653 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002654 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002655 }
2656 else
2657 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002658 }
2659 }
2660}
2661
Abramo Bagnara60804e12011-03-18 15:16:37 +00002662void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2663 unsigned NumTPLists,
2664 TemplateParameterList **TPLists) {
2665 assert(NumTPLists > 0);
2666 // Make sure the extended decl info is allocated.
2667 if (!hasExtInfo())
2668 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002669 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002670 // Set the template parameter lists info.
2671 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2672}
2673
Ted Kremenek21475702008-09-05 17:16:31 +00002674//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002675// EnumDecl Implementation
2676//===----------------------------------------------------------------------===//
2677
David Blaikie68e081d2011-12-20 02:48:34 +00002678void EnumDecl::anchor() { }
2679
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002680EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2681 SourceLocation StartLoc, SourceLocation IdLoc,
2682 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002683 EnumDecl *PrevDecl, bool IsScoped,
2684 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002685 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002686 IsScoped, IsScopedUsingClassTag, IsFixed);
Sebastian Redl833ef452010-01-26 22:01:41 +00002687 C.getTypeDeclType(Enum, PrevDecl);
2688 return Enum;
2689}
2690
Douglas Gregor72172e92012-01-05 21:55:30 +00002691EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2692 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
2693 return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0,
2694 false, false, false);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002695}
2696
Douglas Gregord5058122010-02-11 01:19:42 +00002697void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002698 QualType NewPromotionType,
2699 unsigned NumPositiveBits,
2700 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002701 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002702 if (!IntegerType)
2703 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002704 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002705 setNumPositiveBits(NumPositiveBits);
2706 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002707 TagDecl::completeDefinition();
2708}
2709
Richard Smith7d137e32012-03-23 03:33:32 +00002710TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2711 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2712 return MSI->getTemplateSpecializationKind();
2713
2714 return TSK_Undeclared;
2715}
2716
2717void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2718 SourceLocation PointOfInstantiation) {
2719 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2720 assert(MSI && "Not an instantiated member enumeration?");
2721 MSI->setTemplateSpecializationKind(TSK);
2722 if (TSK != TSK_ExplicitSpecialization &&
2723 PointOfInstantiation.isValid() &&
2724 MSI->getPointOfInstantiation().isInvalid())
2725 MSI->setPointOfInstantiation(PointOfInstantiation);
2726}
2727
Richard Smith4b38ded2012-03-14 23:13:10 +00002728EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2729 if (SpecializationInfo)
2730 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2731
2732 return 0;
2733}
2734
2735void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2736 TemplateSpecializationKind TSK) {
2737 assert(!SpecializationInfo && "Member enum is already a specialization");
2738 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2739}
2740
Sebastian Redl833ef452010-01-26 22:01:41 +00002741//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002742// RecordDecl Implementation
2743//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002744
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002745RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2746 SourceLocation StartLoc, SourceLocation IdLoc,
2747 IdentifierInfo *Id, RecordDecl *PrevDecl)
2748 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002749 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002750 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002751 HasObjectMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002752 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002753 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002754}
2755
Jay Foad39c79802011-01-12 09:06:06 +00002756RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002757 SourceLocation StartLoc, SourceLocation IdLoc,
2758 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2759 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2760 PrevDecl);
Ted Kremenek21475702008-09-05 17:16:31 +00002761 C.getTypeDeclType(R, PrevDecl);
2762 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002763}
2764
Douglas Gregor72172e92012-01-05 21:55:30 +00002765RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2766 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
2767 return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2768 SourceLocation(), 0, 0);
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002769}
2770
Douglas Gregordfcad112009-03-25 15:59:44 +00002771bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002772 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002773 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2774}
2775
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002776RecordDecl::field_iterator RecordDecl::field_begin() const {
2777 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2778 LoadFieldsFromExternalStorage();
2779
2780 return field_iterator(decl_iterator(FirstDecl));
2781}
2782
Douglas Gregorb11aad82011-02-19 18:51:44 +00002783/// completeDefinition - Notes that the definition of this type is now
2784/// complete.
2785void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002786 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002787 TagDecl::completeDefinition();
2788}
2789
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002790/// isMsStruct - Get whether or not this record uses ms_struct layout.
2791/// This which can be turned on with an attribute, pragma, or the
2792/// -mms-bitfields command-line option.
2793bool RecordDecl::isMsStruct(const ASTContext &C) const {
2794 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2795}
2796
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002797static bool isFieldOrIndirectField(Decl::Kind K) {
2798 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2799}
2800
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002801void RecordDecl::LoadFieldsFromExternalStorage() const {
2802 ExternalASTSource *Source = getASTContext().getExternalSource();
2803 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2804
2805 // Notify that we have a RecordDecl doing some initialization.
2806 ExternalASTSource::Deserializing TheFields(Source);
2807
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002808 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002809 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002810 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2811 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002812 case ELR_Success:
2813 break;
2814
2815 case ELR_AlreadyLoaded:
2816 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002817 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002818 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002819
2820#ifndef NDEBUG
2821 // Check that all decls we got were FieldDecls.
2822 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002823 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002824#endif
2825
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002826 if (Decls.empty())
2827 return;
2828
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002829 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2830 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002831}
2832
Steve Naroff415d3d52008-10-08 17:01:13 +00002833//===----------------------------------------------------------------------===//
2834// BlockDecl Implementation
2835//===----------------------------------------------------------------------===//
2836
David Blaikie9c70e042011-09-21 18:16:56 +00002837void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002838 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002839
Steve Naroffc4b30e52009-03-13 16:56:44 +00002840 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002841 if (!NewParamInfo.empty()) {
2842 NumParams = NewParamInfo.size();
2843 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2844 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002845 }
2846}
2847
John McCall351762c2011-02-07 10:33:21 +00002848void BlockDecl::setCaptures(ASTContext &Context,
2849 const Capture *begin,
2850 const Capture *end,
2851 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002852 CapturesCXXThis = capturesCXXThis;
2853
2854 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002855 NumCaptures = 0;
2856 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002857 return;
2858 }
2859
John McCall351762c2011-02-07 10:33:21 +00002860 NumCaptures = end - begin;
2861
2862 // Avoid new Capture[] because we don't want to provide a default
2863 // constructor.
2864 size_t allocationSize = NumCaptures * sizeof(Capture);
2865 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2866 memcpy(buffer, begin, allocationSize);
2867 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002868}
Sebastian Redl833ef452010-01-26 22:01:41 +00002869
John McCallce45f882011-06-15 22:51:16 +00002870bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2871 for (capture_const_iterator
2872 i = capture_begin(), e = capture_end(); i != e; ++i)
2873 // Only auto vars can be captured, so no redeclaration worries.
2874 if (i->getVariable() == variable)
2875 return true;
2876
2877 return false;
2878}
2879
Douglas Gregor70226da2010-12-21 16:27:07 +00002880SourceRange BlockDecl::getSourceRange() const {
2881 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2882}
Sebastian Redl833ef452010-01-26 22:01:41 +00002883
2884//===----------------------------------------------------------------------===//
2885// Other Decl Allocation/Deallocation Method Implementations
2886//===----------------------------------------------------------------------===//
2887
David Blaikie68e081d2011-12-20 02:48:34 +00002888void TranslationUnitDecl::anchor() { }
2889
Sebastian Redl833ef452010-01-26 22:01:41 +00002890TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2891 return new (C) TranslationUnitDecl(C);
2892}
2893
David Blaikie68e081d2011-12-20 02:48:34 +00002894void LabelDecl::anchor() { }
2895
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002896LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002897 SourceLocation IdentL, IdentifierInfo *II) {
2898 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2899}
2900
2901LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2902 SourceLocation IdentL, IdentifierInfo *II,
2903 SourceLocation GnuLabelL) {
2904 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2905 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002906}
2907
Douglas Gregor72172e92012-01-05 21:55:30 +00002908LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2909 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2910 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002911}
2912
David Blaikie68e081d2011-12-20 02:48:34 +00002913void ValueDecl::anchor() { }
2914
Benjamin Kramerea70eb32012-12-01 15:09:41 +00002915bool ValueDecl::isWeak() const {
2916 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
2917 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
2918 return true;
2919
2920 return isWeakImported();
2921}
2922
David Blaikie68e081d2011-12-20 02:48:34 +00002923void ImplicitParamDecl::anchor() { }
2924
Sebastian Redl833ef452010-01-26 22:01:41 +00002925ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002926 SourceLocation IdLoc,
2927 IdentifierInfo *Id,
2928 QualType Type) {
2929 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002930}
2931
Douglas Gregor72172e92012-01-05 21:55:30 +00002932ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2933 unsigned ID) {
2934 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2935 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2936}
2937
Sebastian Redl833ef452010-01-26 22:01:41 +00002938FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002939 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002940 const DeclarationNameInfo &NameInfo,
2941 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002942 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002943 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002944 bool hasWrittenPrototype,
2945 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002946 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2947 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002948 isInlineSpecified,
2949 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002950 New->HasWrittenPrototype = hasWrittenPrototype;
2951 return New;
2952}
2953
Douglas Gregor72172e92012-01-05 21:55:30 +00002954FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2955 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2956 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2957 DeclarationNameInfo(), QualType(), 0,
2958 SC_None, SC_None, false, false);
2959}
2960
Sebastian Redl833ef452010-01-26 22:01:41 +00002961BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2962 return new (C) BlockDecl(DC, L);
2963}
2964
Douglas Gregor72172e92012-01-05 21:55:30 +00002965BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2966 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2967 return new (Mem) BlockDecl(0, SourceLocation());
2968}
2969
Sebastian Redl833ef452010-01-26 22:01:41 +00002970EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2971 SourceLocation L,
2972 IdentifierInfo *Id, QualType T,
2973 Expr *E, const llvm::APSInt &V) {
2974 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2975}
2976
Douglas Gregor72172e92012-01-05 21:55:30 +00002977EnumConstantDecl *
2978EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2979 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2980 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2981 llvm::APSInt());
2982}
2983
David Blaikie68e081d2011-12-20 02:48:34 +00002984void IndirectFieldDecl::anchor() { }
2985
Benjamin Kramer39593702010-11-21 14:11:41 +00002986IndirectFieldDecl *
2987IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2988 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2989 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002990 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2991}
2992
Douglas Gregor72172e92012-01-05 21:55:30 +00002993IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2994 unsigned ID) {
2995 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2996 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2997 QualType(), 0, 0);
2998}
2999
Douglas Gregorbe996932010-09-01 20:41:53 +00003000SourceRange EnumConstantDecl::getSourceRange() const {
3001 SourceLocation End = getLocation();
3002 if (Init)
3003 End = Init->getLocEnd();
3004 return SourceRange(getLocation(), End);
3005}
3006
David Blaikie68e081d2011-12-20 02:48:34 +00003007void TypeDecl::anchor() { }
3008
Sebastian Redl833ef452010-01-26 22:01:41 +00003009TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00003010 SourceLocation StartLoc, SourceLocation IdLoc,
3011 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
3012 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00003013}
3014
David Blaikie68e081d2011-12-20 02:48:34 +00003015void TypedefNameDecl::anchor() { }
3016
Douglas Gregor72172e92012-01-05 21:55:30 +00003017TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3018 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
3019 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3020}
3021
Richard Smithdda56e42011-04-15 14:24:37 +00003022TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3023 SourceLocation StartLoc,
3024 SourceLocation IdLoc, IdentifierInfo *Id,
3025 TypeSourceInfo *TInfo) {
3026 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3027}
3028
Douglas Gregor72172e92012-01-05 21:55:30 +00003029TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3030 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3031 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3032}
3033
Abramo Bagnaraea947882011-03-08 16:41:52 +00003034SourceRange TypedefDecl::getSourceRange() const {
3035 SourceLocation RangeEnd = getLocation();
3036 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3037 if (typeIsPostfix(TInfo->getType()))
3038 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3039 }
3040 return SourceRange(getLocStart(), RangeEnd);
3041}
3042
Richard Smithdda56e42011-04-15 14:24:37 +00003043SourceRange TypeAliasDecl::getSourceRange() const {
3044 SourceLocation RangeEnd = getLocStart();
3045 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3046 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3047 return SourceRange(getLocStart(), RangeEnd);
3048}
3049
David Blaikie68e081d2011-12-20 02:48:34 +00003050void FileScopeAsmDecl::anchor() { }
3051
Sebastian Redl833ef452010-01-26 22:01:41 +00003052FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003053 StringLiteral *Str,
3054 SourceLocation AsmLoc,
3055 SourceLocation RParenLoc) {
3056 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003057}
Douglas Gregorba345522011-12-02 23:23:56 +00003058
Douglas Gregor72172e92012-01-05 21:55:30 +00003059FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3060 unsigned ID) {
3061 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3062 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3063}
3064
Douglas Gregorba345522011-12-02 23:23:56 +00003065//===----------------------------------------------------------------------===//
3066// ImportDecl Implementation
3067//===----------------------------------------------------------------------===//
3068
3069/// \brief Retrieve the number of module identifiers needed to name the given
3070/// module.
3071static unsigned getNumModuleIdentifiers(Module *Mod) {
3072 unsigned Result = 1;
3073 while (Mod->Parent) {
3074 Mod = Mod->Parent;
3075 ++Result;
3076 }
3077 return Result;
3078}
3079
Douglas Gregor22d09742012-01-03 18:04:46 +00003080ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003081 Module *Imported,
3082 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003083 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003084 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003085{
3086 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3087 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3088 memcpy(StoredLocs, IdentifierLocs.data(),
3089 IdentifierLocs.size() * sizeof(SourceLocation));
3090}
3091
Douglas Gregor22d09742012-01-03 18:04:46 +00003092ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003093 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003094 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003095 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003096{
3097 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3098}
3099
3100ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003101 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003102 ArrayRef<SourceLocation> IdentifierLocs) {
3103 void *Mem = C.Allocate(sizeof(ImportDecl) +
3104 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003105 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003106}
3107
3108ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003109 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003110 Module *Imported,
3111 SourceLocation EndLoc) {
3112 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003113 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003114 Import->setImplicit();
3115 return Import;
3116}
3117
Douglas Gregor72172e92012-01-05 21:55:30 +00003118ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3119 unsigned NumLocations) {
3120 void *Mem = AllocateDeserializedDecl(C, ID,
3121 (sizeof(ImportDecl) +
3122 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003123 return new (Mem) ImportDecl(EmptyShell());
3124}
3125
3126ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3127 if (!ImportedAndComplete.getInt())
3128 return ArrayRef<SourceLocation>();
3129
3130 const SourceLocation *StoredLocs
3131 = reinterpret_cast<const SourceLocation *>(this + 1);
3132 return ArrayRef<SourceLocation>(StoredLocs,
3133 getNumModuleIdentifiers(getImportedModule()));
3134}
3135
3136SourceRange ImportDecl::getSourceRange() const {
3137 if (!ImportedAndComplete.getInt())
3138 return SourceRange(getLocation(),
3139 *reinterpret_cast<const SourceLocation *>(this + 1));
3140
3141 return SourceRange(getLocation(), getIdentifierLocs().back());
3142}