blob: 433e76767c449e98255b93e7d2ef0be5829f7029 [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 Espindolaf4187652013-02-14 01:18:37 +0000199template<typename T>
200bool isInExternCContext(T *D) {
201 const T *First = D->getFirstDeclaration();
202 return First->getDeclContext()->isExternCContext();
203}
204
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000205static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D,
206 bool OnlyTemplate) {
Sebastian Redl50c68252010-08-31 00:36:30 +0000207 assert(D->getDeclContext()->getRedeclContext()->isFileContext() &&
Douglas Gregorf73b2822009-11-25 22:24:25 +0000208 "Not a name having namespace scope");
209 ASTContext &Context = D->getASTContext();
210
211 // C++ [basic.link]p3:
212 // A name having namespace scope (3.3.6) has internal linkage if it
213 // is the name of
214 // - an object, reference, function or function template that is
215 // explicitly declared static; or,
216 // (This bullet corresponds to C99 6.2.2p3.)
217 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
218 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000219 if (Var->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000220 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000221
Richard Smithdc0ef452012-10-19 06:37:48 +0000222 // - a non-volatile object or reference that is explicitly declared const
223 // or constexpr and neither explicitly declared extern nor previously
224 // declared to have external linkage; or (there is no equivalent in C99)
David Blaikiebbafb8a2012-03-11 07:00:24 +0000225 if (Context.getLangOpts().CPlusPlus &&
Richard Smithdc0ef452012-10-19 06:37:48 +0000226 Var->getType().isConstQualified() &&
227 !Var->getType().isVolatileQualified() &&
John McCall8e7d6562010-08-26 03:08:43 +0000228 Var->getStorageClass() != SC_Extern &&
229 Var->getStorageClass() != SC_PrivateExtern) {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000230 bool FoundExtern = false;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000231 for (const VarDecl *PrevVar = Var->getPreviousDecl();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000232 PrevVar && !FoundExtern;
Douglas Gregorec9fd132012-01-14 16:38:05 +0000233 PrevVar = PrevVar->getPreviousDecl())
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000234 if (isExternalLinkage(PrevVar->getLinkage()))
Douglas Gregorf73b2822009-11-25 22:24:25 +0000235 FoundExtern = true;
236
237 if (!FoundExtern)
John McCallc273f242010-10-30 11:50:40 +0000238 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000239 }
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000240 if (Var->getStorageClass() == SC_None) {
Douglas Gregorec9fd132012-01-14 16:38:05 +0000241 const VarDecl *PrevVar = Var->getPreviousDecl();
242 for (; PrevVar; PrevVar = PrevVar->getPreviousDecl())
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000243 if (PrevVar->getStorageClass() == SC_PrivateExtern)
244 break;
Eli Friedmana7137bc2012-10-26 23:05:34 +0000245 if (PrevVar)
246 return PrevVar->getLinkageAndVisibility();
Fariborz Jahanian8feee2d2011-06-16 20:14:50 +0000247 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000248 } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) {
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000249 // C++ [temp]p4:
250 // A non-member function template can have internal linkage; any
251 // other template name shall have external linkage.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000252 const FunctionDecl *Function = 0;
253 if (const FunctionTemplateDecl *FunTmpl
254 = dyn_cast<FunctionTemplateDecl>(D))
255 Function = FunTmpl->getTemplatedDecl();
256 else
257 Function = cast<FunctionDecl>(D);
258
259 // Explicitly declared static.
John McCall8e7d6562010-08-26 03:08:43 +0000260 if (Function->getStorageClass() == SC_Static)
John McCallc273f242010-10-30 11:50:40 +0000261 return LinkageInfo(InternalLinkage, DefaultVisibility, false);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000262 } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) {
263 // - a data member of an anonymous union.
264 if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion())
John McCallc273f242010-10-30 11:50:40 +0000265 return LinkageInfo::internal();
Douglas Gregorf73b2822009-11-25 22:24:25 +0000266 }
267
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000268 if (D->isInAnonymousNamespace()) {
269 const VarDecl *Var = dyn_cast<VarDecl>(D);
270 const FunctionDecl *Func = dyn_cast<FunctionDecl>(D);
Rafael Espindolaf4187652013-02-14 01:18:37 +0000271 if ((!Var || !isInExternCContext(Var)) &&
272 (!Func || !isInExternCContext(Func)))
Chandler Carruth9682a2fd2011-02-24 19:03:39 +0000273 return LinkageInfo::uniqueExternal();
274 }
John McCallb7139c42010-10-28 04:18:25 +0000275
John McCall457a04e2010-10-22 21:05:15 +0000276 // Set up the defaults.
277
278 // C99 6.2.2p5:
279 // If the declaration of an identifier for an object has file
280 // scope and no storage-class specifier, its linkage is
281 // external.
John McCallc273f242010-10-30 11:50:40 +0000282 LinkageInfo LV;
283
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000284 if (!OnlyTemplate) {
Rafael Espindola78158af2012-04-16 18:46:26 +0000285 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000286 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000287 } else {
288 // If we're declared in a namespace with a visibility attribute,
289 // use that namespace's visibility, but don't call it explicit.
290 for (const DeclContext *DC = D->getDeclContext();
291 !isa<TranslationUnitDecl>(DC);
292 DC = DC->getParent()) {
293 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
294 if (!ND) continue;
295 if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) {
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000296 LV.mergeVisibility(*Vis, true);
Rafael Espindola78158af2012-04-16 18:46:26 +0000297 break;
298 }
299 }
300 }
301 }
302
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000303 if (!OnlyTemplate) {
Rafael Espindolab660efd2012-04-19 04:37:16 +0000304 LV.mergeVisibility(Context.getLangOpts().getVisibilityMode());
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000305 // If we're paying attention to global visibility, apply
306 // -finline-visibility-hidden if this is an inline method.
307 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
308 LV.mergeVisibility(HiddenVisibility, true);
309 }
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000310
Douglas Gregorf73b2822009-11-25 22:24:25 +0000311 // C++ [basic.link]p4:
John McCall457a04e2010-10-22 21:05:15 +0000312
Douglas Gregorf73b2822009-11-25 22:24:25 +0000313 // A name having namespace scope has external linkage if it is the
314 // name of
315 //
316 // - an object or reference, unless it has internal linkage; or
317 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
John McCall37bb6c92010-10-29 22:22:43 +0000318 // GCC applies the following optimization to variables and static
319 // data members, but not to functions:
320 //
John McCall457a04e2010-10-22 21:05:15 +0000321 // Modify the variable's LV by the LV of its type unless this is
322 // C or extern "C". This follows from [basic.link]p9:
323 // A type without linkage shall not be used as the type of a
324 // variable or function with external linkage unless
325 // - the entity has C language linkage, or
326 // - the entity is declared within an unnamed namespace, or
327 // - the entity is not used or is defined in the same
328 // translation unit.
329 // and [basic.link]p10:
330 // ...the types specified by all declarations referring to a
331 // given variable or function shall be identical...
332 // C does not have an equivalent rule.
333 //
John McCall5fe84122010-10-26 04:59:26 +0000334 // Ignore this if we've got an explicit attribute; the user
335 // probably knows what they're doing.
336 //
John McCall457a04e2010-10-22 21:05:15 +0000337 // Note that we don't want to make the variable non-external
338 // because of this, but unique-external linkage suits us.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000339 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000340 !Var->getDeclContext()->isExternCContext()) {
Rafael Espindola2f869a32012-01-14 00:30:36 +0000341 LinkageInfo TypeLV = getLVForType(Var->getType());
342 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000343 return LinkageInfo::uniqueExternal();
Rafael Espindola1f073332012-04-19 05:24:05 +0000344 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000345 }
346
John McCall23032652010-11-02 18:38:13 +0000347 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000348 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000349
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000350 // Note that Sema::MergeVarDecl already takes care of implementing
351 // C99 6.2.2p4 and propagating the visibility attribute, so we don't have
352 // to do it here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000353
Douglas Gregorf73b2822009-11-25 22:24:25 +0000354 // - a function, unless it has internal linkage; or
John McCall457a04e2010-10-22 21:05:15 +0000355 } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
John McCall2efaf112010-10-28 07:07:52 +0000356 // In theory, we can modify the function's LV by the LV of its
357 // type unless it has C linkage (see comment above about variables
358 // for justification). In practice, GCC doesn't do this, so it's
359 // just too painful to make work.
John McCall457a04e2010-10-22 21:05:15 +0000360
John McCall23032652010-11-02 18:38:13 +0000361 if (Function->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000362 LV.mergeVisibility(HiddenVisibility, true);
John McCall23032652010-11-02 18:38:13 +0000363
Rafael Espindolaa508c5d2012-11-21 02:47:19 +0000364 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
365 // merging storage classes and visibility attributes, so we don't have to
366 // look at previous decls in here.
Douglas Gregorf73b2822009-11-25 22:24:25 +0000367
John McCallf768aa72011-02-10 06:50:24 +0000368 // In C++, then if the type of the function uses a type with
369 // unique-external linkage, it's not legally usable from outside
370 // this translation unit. However, we should use the C linkage
371 // rules instead for extern "C" declarations.
David Blaikiebbafb8a2012-03-11 07:00:24 +0000372 if (Context.getLangOpts().CPlusPlus &&
Eli Friedman839192f2012-01-15 01:23:58 +0000373 !Function->getDeclContext()->isExternCContext() &&
John McCallf768aa72011-02-10 06:50:24 +0000374 Function->getType()->getLinkage() == UniqueExternalLinkage)
375 return LinkageInfo::uniqueExternal();
376
John McCallb8c604a2011-06-27 23:06:04 +0000377 // Consider LV from the template and the template arguments unless
378 // this is an explicit specialization with a visibility attribute.
379 if (FunctionTemplateSpecializationInfo *specInfo
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000380 = Function->getTemplateSpecializationInfo()) {
Rafael Espindola340941d2012-05-25 16:41:35 +0000381 LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true);
382 const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments;
383 LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs,
384 OnlyTemplate);
385 if (shouldConsiderTemplateVis(Function, specInfo)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000386 LV.mergeWithMin(TempLV);
Rafael Espindola340941d2012-05-25 16:41:35 +0000387 LV.mergeWithMin(ArgsLV);
388 } else {
389 LV.mergeLinkage(TempLV);
390 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000391 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000392 }
393
Douglas Gregorf73b2822009-11-25 22:24:25 +0000394 // - a named class (Clause 9), or an unnamed class defined in a
395 // typedef declaration in which the class has the typedef name
396 // for linkage purposes (7.1.3); or
397 // - a named enumeration (7.2), or an unnamed enumeration
398 // defined in a typedef declaration in which the enumeration
399 // has the typedef name for linkage purposes (7.1.3); or
John McCall457a04e2010-10-22 21:05:15 +0000400 } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) {
401 // Unnamed tags have no linkage.
Richard Smithdda56e42011-04-15 14:24:37 +0000402 if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl())
John McCallc273f242010-10-30 11:50:40 +0000403 return LinkageInfo::none();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000404
John McCall457a04e2010-10-22 21:05:15 +0000405 // If this is a class template specialization, consider the
406 // linkage of the template and template arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000407 if (const ClassTemplateSpecializationDecl *spec
John McCall457a04e2010-10-22 21:05:15 +0000408 = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) {
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000409 // From the template.
410 LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true);
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000411
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000412 // The arguments at which the template was instantiated.
413 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
414 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
415 OnlyTemplate);
416 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa486f482012-06-11 14:29:58 +0000417 LV.mergeWithMin(TempLV);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000418 LV.mergeWithMin(ArgsLV);
419 } else {
420 LV.mergeLinkage(TempLV);
421 LV.mergeLinkage(ArgsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000422 }
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000423 }
Douglas Gregorf73b2822009-11-25 22:24:25 +0000424
425 // - an enumerator belonging to an enumeration with external linkage;
John McCall457a04e2010-10-22 21:05:15 +0000426 } else if (isa<EnumConstantDecl>(D)) {
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000427 LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()),
428 OnlyTemplate);
John McCallc273f242010-10-30 11:50:40 +0000429 if (!isExternalLinkage(EnumLV.linkage()))
430 return LinkageInfo::none();
431 LV.merge(EnumLV);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000432
433 // - a template, unless it is a function template that has
434 // internal linkage (Clause 14);
John McCall8bc6d5b2011-03-04 10:39:25 +0000435 } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) {
Rafael Espindola8add48e2012-04-22 00:43:48 +0000436 LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters()));
Douglas Gregorf73b2822009-11-25 22:24:25 +0000437 // - a namespace (7.3), unless it is declared within an unnamed
438 // namespace.
John McCall457a04e2010-10-22 21:05:15 +0000439 } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) {
440 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000441
John McCall457a04e2010-10-22 21:05:15 +0000442 // By extension, we assign external linkage to Objective-C
443 // interfaces.
444 } else if (isa<ObjCInterfaceDecl>(D)) {
445 // fallout
446
447 // Everything not covered here has no linkage.
448 } else {
John McCallc273f242010-10-30 11:50:40 +0000449 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000450 }
451
452 // If we ended up with non-external linkage, visibility should
453 // always be default.
John McCallc273f242010-10-30 11:50:40 +0000454 if (LV.linkage() != ExternalLinkage)
455 return LinkageInfo(LV.linkage(), DefaultVisibility, false);
John McCall457a04e2010-10-22 21:05:15 +0000456
John McCall457a04e2010-10-22 21:05:15 +0000457 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000458}
459
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000460static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) {
John McCall457a04e2010-10-22 21:05:15 +0000461 // Only certain class members have linkage. Note that fields don't
462 // really have linkage, but it's convenient to say they do for the
463 // purposes of calculating linkage of pointer-to-data-member
464 // template arguments.
John McCall8823c652010-08-13 08:35:10 +0000465 if (!(isa<CXXMethodDecl>(D) ||
466 isa<VarDecl>(D) ||
John McCall457a04e2010-10-22 21:05:15 +0000467 isa<FieldDecl>(D) ||
David Blaikie095deba2012-11-14 01:52:05 +0000468 isa<TagDecl>(D)))
John McCallc273f242010-10-30 11:50:40 +0000469 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000470
John McCall07072662010-11-02 01:45:15 +0000471 LinkageInfo LV;
472
John McCall07072662010-11-02 01:45:15 +0000473 // If we have an explicit visibility attribute, merge that in.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000474 if (!OnlyTemplate) {
Rafael Espindola3d3d3392012-04-19 04:27:47 +0000475 if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility())
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000476 LV.mergeVisibility(*Vis, true);
Rafael Espindolac7c7ad52012-07-13 14:25:36 +0000477 // If we're paying attention to global visibility, apply
478 // -finline-visibility-hidden if this is an inline method.
479 //
480 // Note that we do this before merging information about
481 // the class visibility.
482 if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D))
483 LV.mergeVisibility(HiddenVisibility, true);
John McCall07072662010-11-02 01:45:15 +0000484 }
Rafael Espindola53cf2192012-04-19 05:50:08 +0000485
486 // If this class member has an explicit visibility attribute, the only
487 // thing that can change its visibility is the template arguments, so
Sylvestre Ledru830885c2012-07-23 08:59:39 +0000488 // only look for them when processing the class.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000489 bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate;
Rafael Espindola505a7c82012-04-16 18:25:01 +0000490
Rafael Espindola53cf2192012-04-19 05:50:08 +0000491 // If this member has an visibility attribute, ClassF will exclude
492 // attributes on the class or command line options, keeping only information
493 // about the template instantiation. If the member has no visibility
494 // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin
495 // produces the desired result.
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000496 LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()),
497 ClassOnlyTemplate));
John McCall07072662010-11-02 01:45:15 +0000498 if (!isExternalLinkage(LV.linkage()))
John McCallc273f242010-10-30 11:50:40 +0000499 return LinkageInfo::none();
John McCall8823c652010-08-13 08:35:10 +0000500
501 // If the class already has unique-external linkage, we can't improve.
John McCall07072662010-11-02 01:45:15 +0000502 if (LV.linkage() == UniqueExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000503 return LinkageInfo::uniqueExternal();
John McCall8823c652010-08-13 08:35:10 +0000504
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000505 if (!OnlyTemplate)
Rafael Espindolab660efd2012-04-19 04:37:16 +0000506 LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode());
Rafael Espindolaaf690f52012-04-19 02:55:01 +0000507
John McCall8823c652010-08-13 08:35:10 +0000508 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
John McCallf768aa72011-02-10 06:50:24 +0000509 // If the type of the function uses a type with unique-external
510 // linkage, it's not legally usable from outside this translation unit.
511 if (MD->getType()->getLinkage() == UniqueExternalLinkage)
512 return LinkageInfo::uniqueExternal();
513
John McCall457a04e2010-10-22 21:05:15 +0000514 // If this is a method template specialization, use the linkage for
515 // the template parameters and arguments.
John McCallb8c604a2011-06-27 23:06:04 +0000516 if (FunctionTemplateSpecializationInfo *spec
John McCall8823c652010-08-13 08:35:10 +0000517 = MD->getTemplateSpecializationInfo()) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000518 const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments;
519 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
520 OnlyTemplate);
521 TemplateParameterList *TemplateParams =
522 spec->getTemplate()->getTemplateParameters();
523 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola340941d2012-05-25 16:41:35 +0000524 if (shouldConsiderTemplateVis(MD, spec)) {
Rafael Espindola67a498c2012-05-25 17:22:33 +0000525 LV.mergeWithMin(ArgsLV);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000526 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000527 LV.mergeWithMin(ParamsLV);
Rafael Espindola67a498c2012-05-25 17:22:33 +0000528 } else {
529 LV.mergeLinkage(ArgsLV);
530 if (!OnlyTemplate)
531 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000532 }
John McCalle6e622e2010-11-01 01:29:57 +0000533 }
John McCall457a04e2010-10-22 21:05:15 +0000534
John McCall37bb6c92010-10-29 22:22:43 +0000535 // Note that in contrast to basically every other situation, we
536 // *do* apply -fvisibility to method declarations.
537
538 } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
John McCallb8c604a2011-06-27 23:06:04 +0000539 if (const ClassTemplateSpecializationDecl *spec
John McCall37bb6c92010-10-29 22:22:43 +0000540 = dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000541 // Merge template argument/parameter information for member
542 // class template specializations.
543 const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs();
544 LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs,
545 OnlyTemplate);
546 TemplateParameterList *TemplateParams =
547 spec->getSpecializedTemplate()->getTemplateParameters();
548 LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams);
Rafael Espindola0cf10ac2012-05-25 14:47:05 +0000549 if (shouldConsiderTemplateVis(spec)) {
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000550 LV.mergeWithMin(ArgsLV);
Rafael Espindola4d71d0f2012-05-25 14:17:45 +0000551 if (!OnlyTemplate)
Rafael Espindolaa486f482012-06-11 14:29:58 +0000552 LV.mergeWithMin(ParamsLV);
Rafael Espindolaa28bf632012-05-25 15:51:26 +0000553 } else {
554 LV.mergeLinkage(ArgsLV);
555 if (!OnlyTemplate)
556 LV.mergeLinkage(ParamsLV);
John McCallb8c604a2011-06-27 23:06:04 +0000557 }
John McCall37bb6c92010-10-29 22:22:43 +0000558 }
559
John McCall37bb6c92010-10-29 22:22:43 +0000560 // Static data members.
561 } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
John McCall36cd5cc2010-10-30 09:18:49 +0000562 // Modify the variable's linkage by its type, but ignore the
563 // type's visibility unless it's a definition.
Rafael Espindola2f869a32012-01-14 00:30:36 +0000564 LinkageInfo TypeLV = getLVForType(VD->getType());
565 if (TypeLV.linkage() != ExternalLinkage)
John McCallc273f242010-10-30 11:50:40 +0000566 LV.mergeLinkage(UniqueExternalLinkage);
Rafael Espindola53cf2192012-04-19 05:50:08 +0000567 LV.mergeVisibility(TypeLV);
John McCall37bb6c92010-10-29 22:22:43 +0000568 }
569
John McCall457a04e2010-10-22 21:05:15 +0000570 return LV;
John McCall8823c652010-08-13 08:35:10 +0000571}
572
John McCalld396b972011-02-08 19:01:05 +0000573static void clearLinkageForClass(const CXXRecordDecl *record) {
574 for (CXXRecordDecl::decl_iterator
575 i = record->decls_begin(), e = record->decls_end(); i != e; ++i) {
576 Decl *child = *i;
577 if (isa<NamedDecl>(child))
Rafael Espindola19de5612013-01-12 06:42:30 +0000578 cast<NamedDecl>(child)->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000579 }
580}
581
David Blaikie68e081d2011-12-20 02:48:34 +0000582void NamedDecl::anchor() { }
583
Rafael Espindola19de5612013-01-12 06:42:30 +0000584void NamedDecl::ClearLinkageCache() {
John McCalld396b972011-02-08 19:01:05 +0000585 // Note that we can't skip clearing the linkage of children just
586 // because the parent doesn't have cached linkage: we don't cache
587 // when computing linkage for parent contexts.
588
Rafael Espindola19de5612013-01-12 06:42:30 +0000589 HasCachedLinkage = 0;
John McCalld396b972011-02-08 19:01:05 +0000590
591 // If we're changing the linkage of a class, we need to reset the
592 // linkage of child declarations, too.
593 if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this))
594 clearLinkageForClass(record);
595
Dmitri Gribenkofb04e1b2013-02-03 16:10:26 +0000596 if (ClassTemplateDecl *temp = dyn_cast<ClassTemplateDecl>(this)) {
John McCalld396b972011-02-08 19:01:05 +0000597 // Clear linkage for the template pattern.
598 CXXRecordDecl *record = temp->getTemplatedDecl();
Rafael Espindola19de5612013-01-12 06:42:30 +0000599 record->HasCachedLinkage = 0;
John McCalld396b972011-02-08 19:01:05 +0000600 clearLinkageForClass(record);
601
John McCall83779672011-02-19 02:53:41 +0000602 // We need to clear linkage for specializations, too.
603 for (ClassTemplateDecl::spec_iterator
604 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindola19de5612013-01-12 06:42:30 +0000605 i->ClearLinkageCache();
John McCalld396b972011-02-08 19:01:05 +0000606 }
John McCall83779672011-02-19 02:53:41 +0000607
608 // Clear cached linkage for function template decls, too.
Dmitri Gribenkofb04e1b2013-02-03 16:10:26 +0000609 if (FunctionTemplateDecl *temp = dyn_cast<FunctionTemplateDecl>(this)) {
Rafael Espindola19de5612013-01-12 06:42:30 +0000610 temp->getTemplatedDecl()->ClearLinkageCache();
John McCall83779672011-02-19 02:53:41 +0000611 for (FunctionTemplateDecl::spec_iterator
612 i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i)
Rafael Espindola19de5612013-01-12 06:42:30 +0000613 i->ClearLinkageCache();
John McCall8f9a4292011-03-22 06:58:49 +0000614 }
John McCall83779672011-02-19 02:53:41 +0000615
John McCalld396b972011-02-08 19:01:05 +0000616}
617
Douglas Gregorbf62d642010-12-06 18:36:25 +0000618Linkage NamedDecl::getLinkage() const {
Richard Smith88581592013-02-12 05:48:23 +0000619 if (HasCachedLinkage)
Rafael Espindola19de5612013-01-12 06:42:30 +0000620 return Linkage(CachedLinkage);
Rafael Espindola19de5612013-01-12 06:42:30 +0000621
622 CachedLinkage = getLVForDecl(this, true).linkage();
623 HasCachedLinkage = 1;
624
625#ifndef NDEBUG
626 verifyLinkage();
627#endif
628
629 return Linkage(CachedLinkage);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000630}
631
John McCallc273f242010-10-30 11:50:40 +0000632LinkageInfo NamedDecl::getLinkageAndVisibility() const {
Rafael Espindola19de5612013-01-12 06:42:30 +0000633 LinkageInfo LI = getLVForDecl(this, false);
634 if (HasCachedLinkage) {
635 assert(Linkage(CachedLinkage) == LI.linkage());
636 return LI;
Rafael Espindola54606d52012-12-25 07:31:49 +0000637 }
Rafael Espindola19de5612013-01-12 06:42:30 +0000638 HasCachedLinkage = 1;
639 CachedLinkage = LI.linkage();
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000640
641#ifndef NDEBUG
Rafael Espindola19de5612013-01-12 06:42:30 +0000642 verifyLinkage();
643#endif
644
645 return LI;
646}
647
648void NamedDecl::verifyLinkage() const {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000649 // In C (because of gnu inline) and in c++ with microsoft extensions an
650 // static can follow an extern, so we can have two decls with different
651 // linkages.
652 const LangOptions &Opts = getASTContext().getLangOpts();
653 if (!Opts.CPlusPlus || Opts.MicrosoftExt)
Rafael Espindola19de5612013-01-12 06:42:30 +0000654 return;
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000655
656 // We have just computed the linkage for this decl. By induction we know
657 // that all other computed linkages match, check that the one we just computed
658 // also does.
659 NamedDecl *D = NULL;
660 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
661 NamedDecl *T = cast<NamedDecl>(*I);
662 if (T == this)
663 continue;
Rafael Espindola19de5612013-01-12 06:42:30 +0000664 if (T->HasCachedLinkage != 0) {
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000665 D = T;
666 break;
667 }
668 }
669 assert(!D || D->CachedLinkage == CachedLinkage);
John McCall033caa52010-10-29 00:29:13 +0000670}
Ted Kremenek926d8602010-04-20 23:15:35 +0000671
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000672llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const {
673 // Use the most recent declaration of a variable.
Rafael Espindola96e68242012-05-16 02:10:38 +0000674 if (const VarDecl *Var = dyn_cast<VarDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000675 if (llvm::Optional<Visibility> V = getVisibilityOf(Var))
Rafael Espindola96e68242012-05-16 02:10:38 +0000676 return V;
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000677
Rafael Espindola96e68242012-05-16 02:10:38 +0000678 if (Var->isStaticDataMember()) {
679 VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember();
680 if (InstantiatedFrom)
681 return getVisibilityOf(InstantiatedFrom);
682 }
683
684 return llvm::Optional<Visibility>();
685 }
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000686 // Use the most recent declaration of a function, and also handle
687 // function template specializations.
688 if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) {
Rafael Espindolaab53a722012-11-12 04:32:23 +0000689 if (llvm::Optional<Visibility> V = getVisibilityOf(fn))
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000690 return V;
691
692 // If the function is a specialization of a template with an
693 // explicit visibility attribute, use that.
694 if (FunctionTemplateSpecializationInfo *templateInfo
695 = fn->getTemplateSpecializationInfo())
696 return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl());
697
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000698 // If the function is a member of a specialization of a class template
699 // and the corresponding decl has explicit visibility, use that.
700 FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction();
701 if (InstantiatedFrom)
702 return getVisibilityOf(InstantiatedFrom);
703
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000704 return llvm::Optional<Visibility>();
705 }
706
707 // Otherwise, just check the declaration itself first.
708 if (llvm::Optional<Visibility> V = getVisibilityOf(this))
709 return V;
710
Rafael Espindolafb4263f2012-07-31 19:02:02 +0000711 // The visibility of a template is stored in the templated decl.
712 if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this))
713 return getVisibilityOf(TD->getTemplatedDecl());
714
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000715 // If there wasn't explicit visibility there, and this is a
716 // specialization of a class template, check for visibility
717 // on the pattern.
718 if (const ClassTemplateSpecializationDecl *spec
Rafael Espindolaeca5cd22012-07-13 01:19:08 +0000719 = dyn_cast<ClassTemplateSpecializationDecl>(this))
720 return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl());
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000721
Rafael Espindola8093fdf2012-02-23 04:17:32 +0000722 // If this is a member class of a specialization of a class template
723 // and the corresponding decl has explicit visibility, use that.
724 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) {
725 CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass();
726 if (InstantiatedFrom)
727 return getVisibilityOf(InstantiatedFrom);
728 }
729
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000730 return llvm::Optional<Visibility>();
731}
732
Rafael Espindola19de5612013-01-12 06:42:30 +0000733static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000734 // Objective-C: treat all Objective-C declarations as having external
735 // linkage.
John McCall033caa52010-10-29 00:29:13 +0000736 switch (D->getKind()) {
Ted Kremenek926d8602010-04-20 23:15:35 +0000737 default:
738 break;
Argyrios Kyrtzidis79d04282011-12-01 01:28:21 +0000739 case Decl::ParmVar:
740 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000741 case Decl::TemplateTemplateParm: // count these as external
742 case Decl::NonTypeTemplateParm:
Ted Kremenek926d8602010-04-20 23:15:35 +0000743 case Decl::ObjCAtDefsField:
744 case Decl::ObjCCategory:
745 case Decl::ObjCCategoryImpl:
Ted Kremenek926d8602010-04-20 23:15:35 +0000746 case Decl::ObjCCompatibleAlias:
Ted Kremenek926d8602010-04-20 23:15:35 +0000747 case Decl::ObjCImplementation:
Ted Kremenek926d8602010-04-20 23:15:35 +0000748 case Decl::ObjCMethod:
749 case Decl::ObjCProperty:
750 case Decl::ObjCPropertyImpl:
751 case Decl::ObjCProtocol:
John McCallc273f242010-10-30 11:50:40 +0000752 return LinkageInfo::external();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000753
754 case Decl::CXXRecord: {
755 const CXXRecordDecl *Record = cast<CXXRecordDecl>(D);
756 if (Record->isLambda()) {
757 if (!Record->getLambdaManglingNumber()) {
758 // This lambda has no mangling number, so it's internal.
759 return LinkageInfo::internal();
760 }
761
762 // This lambda has its linkage/visibility determined by its owner.
763 const DeclContext *DC = D->getDeclContext()->getRedeclContext();
764 if (Decl *ContextDecl = Record->getLambdaContextDecl()) {
765 if (isa<ParmVarDecl>(ContextDecl))
766 DC = ContextDecl->getDeclContext()->getRedeclContext();
767 else
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000768 return getLVForDecl(cast<NamedDecl>(ContextDecl),
769 OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000770 }
771
772 if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC))
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000773 return getLVForDecl(ND, OnlyTemplate);
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000774
775 return LinkageInfo::external();
776 }
777
778 break;
779 }
Ted Kremenek926d8602010-04-20 23:15:35 +0000780 }
781
Douglas Gregorf73b2822009-11-25 22:24:25 +0000782 // Handle linkage for namespace-scope names.
John McCall033caa52010-10-29 00:29:13 +0000783 if (D->getDeclContext()->getRedeclContext()->isFileContext())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000784 return getLVForNamespaceScopeDecl(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000785
786 // C++ [basic.link]p5:
787 // In addition, a member function, static data member, a named
788 // class or enumeration of class scope, or an unnamed class or
789 // enumeration defined in a class-scope typedef declaration such
790 // that the class or enumeration has the typedef name for linkage
791 // purposes (7.1.3), has external linkage if the name of the class
792 // has external linkage.
John McCall033caa52010-10-29 00:29:13 +0000793 if (D->getDeclContext()->isRecord())
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000794 return getLVForClassMember(D, OnlyTemplate);
Douglas Gregorf73b2822009-11-25 22:24:25 +0000795
796 // C++ [basic.link]p6:
797 // The name of a function declared in block scope and the name of
798 // an object declared by a block scope extern declaration have
799 // linkage. If there is a visible declaration of an entity with
800 // linkage having the same name and type, ignoring entities
801 // declared outside the innermost enclosing namespace scope, the
802 // block scope declaration declares that same entity and receives
803 // the linkage of the previous declaration. If there is more than
804 // one such matching entity, the program is ill-formed. Otherwise,
805 // if no matching entity is found, the block scope entity receives
806 // external linkage.
Rafael Espindola3c98afe2013-01-05 01:28:37 +0000807 if (D->getDeclContext()->isFunctionOrMethod()) {
John McCall033caa52010-10-29 00:29:13 +0000808 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
Eli Friedman839192f2012-01-15 01:23:58 +0000809 if (Function->isInAnonymousNamespace() &&
810 !Function->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000811 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000812
Rafael Espindola3bd836a2013-01-09 16:34:58 +0000813 // This is a "void f();" which got merged with a file static.
814 if (Function->getStorageClass() == SC_Static)
815 return LinkageInfo::internal();
816
John McCallc273f242010-10-30 11:50:40 +0000817 LinkageInfo LV;
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000818 if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000819 if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000820 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000821 }
Rafael Espindolac12edd42012-11-29 16:38:22 +0000822
823 // Note that Sema::MergeCompatibleFunctionDecls already takes care of
824 // merging storage classes and visibility attributes, so we don't have to
825 // look at previous decls in here.
John McCall457a04e2010-10-22 21:05:15 +0000826
827 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000828 }
829
John McCall033caa52010-10-29 00:29:13 +0000830 if (const VarDecl *Var = dyn_cast<VarDecl>(D))
Rafael Espindola74a133f2012-12-18 04:18:55 +0000831 if (Var->getStorageClassAsWritten() == SC_Extern ||
832 Var->getStorageClassAsWritten() == SC_PrivateExtern) {
Eli Friedman839192f2012-01-15 01:23:58 +0000833 if (Var->isInAnonymousNamespace() &&
834 !Var->getDeclContext()->isExternCContext())
John McCallc273f242010-10-30 11:50:40 +0000835 return LinkageInfo::uniqueExternal();
Douglas Gregor7dc5c172010-02-03 09:33:45 +0000836
Rafael Espindola74a133f2012-12-18 04:18:55 +0000837 // This is an "extern int foo;" which got merged with a file static.
838 if (Var->getStorageClass() == SC_Static)
839 return LinkageInfo::internal();
840
John McCallc273f242010-10-30 11:50:40 +0000841 LinkageInfo LV;
John McCall457a04e2010-10-22 21:05:15 +0000842 if (Var->getStorageClass() == SC_PrivateExtern)
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000843 LV.mergeVisibility(HiddenVisibility, true);
Rafael Espindola46cb6f12012-04-21 23:28:21 +0000844 else if (!OnlyTemplate) {
Douglas Gregor1baf38f2011-03-26 12:10:19 +0000845 if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility())
Rafael Espindola7a5543d2012-04-19 02:22:07 +0000846 LV.mergeVisibility(*Vis, true);
Douglas Gregorbf62d642010-12-06 18:36:25 +0000847 }
John McCall457a04e2010-10-22 21:05:15 +0000848
Rafael Espindolad5ed0332012-11-12 04:10:23 +0000849 // Note that Sema::MergeVarDecl already takes care of implementing
850 // C99 6.2.2p4 and propagating the visibility attribute, so we don't
851 // have to do it here.
John McCall457a04e2010-10-22 21:05:15 +0000852 return LV;
Douglas Gregorf73b2822009-11-25 22:24:25 +0000853 }
854 }
855
856 // C++ [basic.link]p6:
857 // Names not covered by these rules have no linkage.
John McCallc273f242010-10-30 11:50:40 +0000858 return LinkageInfo::none();
John McCall457a04e2010-10-22 21:05:15 +0000859}
Douglas Gregorf73b2822009-11-25 22:24:25 +0000860
Douglas Gregor2ada0482009-02-04 17:27:36 +0000861std::string NamedDecl::getQualifiedNameAsString() const {
Douglas Gregor78254c82012-03-27 23:34:16 +0000862 return getQualifiedNameAsString(getASTContext().getPrintingPolicy());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000863}
864
865std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const {
Douglas Gregor2ada0482009-02-04 17:27:36 +0000866 const DeclContext *Ctx = getDeclContext();
867
868 if (Ctx->isFunctionOrMethod())
869 return getNameAsString();
870
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000871 typedef SmallVector<const DeclContext *, 8> ContextsTy;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000872 ContextsTy Contexts;
873
874 // Collect contexts.
875 while (Ctx && isa<NamedDecl>(Ctx)) {
876 Contexts.push_back(Ctx);
877 Ctx = Ctx->getParent();
878 };
879
880 std::string QualName;
881 llvm::raw_string_ostream OS(QualName);
882
883 for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend();
884 I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000885 if (const ClassTemplateSpecializationDecl *Spec
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000886 = dyn_cast<ClassTemplateSpecializationDecl>(*I)) {
Douglas Gregor85673582009-05-18 17:01:57 +0000887 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
888 std::string TemplateArgsStr
889 = TemplateSpecializationType::PrintTemplateArgumentList(
Douglas Gregor1ccc8412010-11-07 23:05:16 +0000890 TemplateArgs.data(),
891 TemplateArgs.size(),
Anders Carlsson2fb08242009-09-08 18:24:21 +0000892 P);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000893 OS << Spec->getName() << TemplateArgsStr;
894 } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) {
Sam Weinig07d211e2009-12-24 23:15:03 +0000895 if (ND->isAnonymousNamespace())
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000896 OS << "<anonymous namespace>";
Sam Weinig07d211e2009-12-24 23:15:03 +0000897 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000898 OS << *ND;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000899 } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) {
900 if (!RD->getIdentifier())
901 OS << "<anonymous " << RD->getKindName() << '>';
902 else
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000903 OS << *RD;
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000904 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
Sam Weinigb999f682009-12-28 03:19:38 +0000905 const FunctionProtoType *FT = 0;
906 if (FD->hasWrittenPrototype())
Eli Friedman5c27c4c2012-08-30 22:22:09 +0000907 FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>());
Sam Weinigb999f682009-12-28 03:19:38 +0000908
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000909 OS << *FD << '(';
Sam Weinigb999f682009-12-28 03:19:38 +0000910 if (FT) {
Sam Weinigb999f682009-12-28 03:19:38 +0000911 unsigned NumParams = FD->getNumParams();
912 for (unsigned i = 0; i < NumParams; ++i) {
913 if (i)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000914 OS << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000915 OS << FD->getParamDecl(i)->getType().stream(P);
Sam Weinigb999f682009-12-28 03:19:38 +0000916 }
917
918 if (FT->isVariadic()) {
919 if (NumParams > 0)
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000920 OS << ", ";
921 OS << "...";
Sam Weinigb999f682009-12-28 03:19:38 +0000922 }
923 }
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000924 OS << ')';
925 } else {
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000926 OS << *cast<NamedDecl>(*I);
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000927 }
928 OS << "::";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000929 }
930
John McCalla2a3f7d2010-03-16 21:48:18 +0000931 if (getDeclName())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000932 OS << *this;
John McCalla2a3f7d2010-03-16 21:48:18 +0000933 else
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000934 OS << "<anonymous>";
Douglas Gregor2ada0482009-02-04 17:27:36 +0000935
Benjamin Kramerd76b6982010-04-28 14:33:51 +0000936 return OS.str();
Douglas Gregor2ada0482009-02-04 17:27:36 +0000937}
938
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000939bool NamedDecl::declarationReplaces(NamedDecl *OldD) const {
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000940 assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch");
941
Douglas Gregor889ceb72009-02-03 19:21:40 +0000942 // UsingDirectiveDecl's are not really NamedDecl's, and all have same name.
943 // We want to keep it, unless it nominates same namespace.
944 if (getKind() == Decl::UsingDirective) {
Douglas Gregor12441b32011-02-25 16:33:46 +0000945 return cast<UsingDirectiveDecl>(this)->getNominatedNamespace()
946 ->getOriginalNamespace() ==
947 cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace()
948 ->getOriginalNamespace();
Douglas Gregor889ceb72009-02-03 19:21:40 +0000949 }
Mike Stump11289f42009-09-09 15:08:12 +0000950
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000951 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this))
952 // For function declarations, we keep track of redeclarations.
Douglas Gregorec9fd132012-01-14 16:38:05 +0000953 return FD->getPreviousDecl() == OldD;
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000954
Douglas Gregorad3f2fc2009-06-25 22:08:12 +0000955 // For function templates, the underlying function declarations are linked.
956 if (const FunctionTemplateDecl *FunctionTemplate
957 = dyn_cast<FunctionTemplateDecl>(this))
958 if (const FunctionTemplateDecl *OldFunctionTemplate
959 = dyn_cast<FunctionTemplateDecl>(OldD))
960 return FunctionTemplate->getTemplatedDecl()
961 ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl());
Mike Stump11289f42009-09-09 15:08:12 +0000962
Steve Naroffc4173fa2009-02-22 19:35:57 +0000963 // For method declarations, we keep track of redeclarations.
964 if (isa<ObjCMethodDecl>(this))
965 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000966
John McCall9f3059a2009-10-09 21:13:30 +0000967 if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD))
968 return true;
969
John McCall3f746822009-11-17 05:59:44 +0000970 if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD))
971 return cast<UsingShadowDecl>(this)->getTargetDecl() ==
972 cast<UsingShadowDecl>(OldD)->getTargetDecl();
973
Douglas Gregora9d87bc2011-02-25 00:36:19 +0000974 if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) {
975 ASTContext &Context = getASTContext();
976 return Context.getCanonicalNestedNameSpecifier(
977 cast<UsingDecl>(this)->getQualifier()) ==
978 Context.getCanonicalNestedNameSpecifier(
979 cast<UsingDecl>(OldD)->getQualifier());
980 }
Argyrios Kyrtzidis4b520072010-11-04 08:48:52 +0000981
Douglas Gregorb59643b2012-01-03 23:26:26 +0000982 // A typedef of an Objective-C class type can replace an Objective-C class
983 // declaration or definition, and vice versa.
984 if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) ||
985 (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD)))
986 return true;
987
Douglas Gregor8b9ccca2008-12-23 21:05:05 +0000988 // For non-function declarations, if the declarations are of the
989 // same kind then this must be a redeclaration, or semantic analysis
990 // would not have given us the new declaration.
991 return this->getKind() == OldD->getKind();
992}
993
Douglas Gregoreddf4332009-02-24 20:03:32 +0000994bool NamedDecl::hasLinkage() const {
Douglas Gregorf73b2822009-11-25 22:24:25 +0000995 return getLinkage() != NoLinkage;
Douglas Gregoreddf4332009-02-24 20:03:32 +0000996}
Douglas Gregor6e6ad602009-01-20 01:17:11 +0000997
Daniel Dunbar166ea9ad2012-03-08 18:20:41 +0000998NamedDecl *NamedDecl::getUnderlyingDeclImpl() {
Anders Carlsson6915bf62009-06-26 06:29:23 +0000999 NamedDecl *ND = this;
Benjamin Kramerba0495a2012-03-08 21:00:45 +00001000 while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND))
1001 ND = UD->getTargetDecl();
1002
1003 if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND))
1004 return AD->getClassInterface();
1005
1006 return ND;
Anders Carlsson6915bf62009-06-26 06:29:23 +00001007}
1008
John McCalla8ae2222010-04-06 21:38:20 +00001009bool NamedDecl::isCXXInstanceMember() const {
Douglas Gregor3f28ec22012-03-08 02:08:05 +00001010 if (!isCXXClassMember())
1011 return false;
1012
John McCalla8ae2222010-04-06 21:38:20 +00001013 const NamedDecl *D = this;
1014 if (isa<UsingShadowDecl>(D))
1015 D = cast<UsingShadowDecl>(D)->getTargetDecl();
1016
Francois Pichet783dd6e2010-11-21 06:08:52 +00001017 if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D))
John McCalla8ae2222010-04-06 21:38:20 +00001018 return true;
1019 if (isa<CXXMethodDecl>(D))
1020 return cast<CXXMethodDecl>(D)->isInstance();
1021 if (isa<FunctionTemplateDecl>(D))
1022 return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D)
1023 ->getTemplatedDecl())->isInstance();
1024 return false;
1025}
1026
Argyrios Kyrtzidis9e59b572008-11-09 23:41:00 +00001027//===----------------------------------------------------------------------===//
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001028// DeclaratorDecl Implementation
1029//===----------------------------------------------------------------------===//
1030
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001031template <typename DeclT>
1032static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) {
1033 if (decl->getNumTemplateParameterLists() > 0)
1034 return decl->getTemplateParameterList(0)->getTemplateLoc();
1035 else
1036 return decl->getInnerLocStart();
1037}
1038
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001039SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const {
John McCallf7bcc812010-05-28 23:32:21 +00001040 TypeSourceInfo *TSI = getTypeSourceInfo();
1041 if (TSI) return TSI->getTypeLoc().getBeginLoc();
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001042 return SourceLocation();
1043}
1044
Douglas Gregor14454802011-02-25 02:25:35 +00001045void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
1046 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00001047 // Make sure the extended decl info is allocated.
1048 if (!hasExtInfo()) {
1049 // Save (non-extended) type source info pointer.
1050 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1051 // Allocate external info struct.
1052 DeclInfo = new (getASTContext()) ExtInfo;
1053 // Restore savedTInfo into (extended) decl info.
1054 getExtInfo()->TInfo = savedTInfo;
1055 }
1056 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00001057 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00001058 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00001059 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00001060 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00001061 if (getExtInfo()->NumTemplParamLists == 0) {
1062 // Save type source info pointer.
1063 TypeSourceInfo *savedTInfo = getExtInfo()->TInfo;
1064 // Deallocate the extended decl info.
1065 getASTContext().Deallocate(getExtInfo());
1066 // Restore savedTInfo into (non-extended) decl info.
1067 DeclInfo = savedTInfo;
1068 }
1069 else
1070 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00001071 }
1072 }
1073}
1074
Abramo Bagnara60804e12011-03-18 15:16:37 +00001075void
1076DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context,
1077 unsigned NumTPLists,
1078 TemplateParameterList **TPLists) {
1079 assert(NumTPLists > 0);
1080 // Make sure the extended decl info is allocated.
1081 if (!hasExtInfo()) {
1082 // Save (non-extended) type source info pointer.
1083 TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>();
1084 // Allocate external info struct.
1085 DeclInfo = new (getASTContext()) ExtInfo;
1086 // Restore savedTInfo into (extended) decl info.
1087 getExtInfo()->TInfo = savedTInfo;
1088 }
1089 // Set the template parameter lists info.
1090 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1091}
1092
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001093SourceLocation DeclaratorDecl::getOuterLocStart() const {
1094 return getTemplateOrInnerLocStart(this);
1095}
1096
Abramo Bagnaraea947882011-03-08 16:41:52 +00001097namespace {
1098
1099// Helper function: returns true if QT is or contains a type
1100// having a postfix component.
1101bool typeIsPostfix(clang::QualType QT) {
1102 while (true) {
1103 const Type* T = QT.getTypePtr();
1104 switch (T->getTypeClass()) {
1105 default:
1106 return false;
1107 case Type::Pointer:
1108 QT = cast<PointerType>(T)->getPointeeType();
1109 break;
1110 case Type::BlockPointer:
1111 QT = cast<BlockPointerType>(T)->getPointeeType();
1112 break;
1113 case Type::MemberPointer:
1114 QT = cast<MemberPointerType>(T)->getPointeeType();
1115 break;
1116 case Type::LValueReference:
1117 case Type::RValueReference:
1118 QT = cast<ReferenceType>(T)->getPointeeType();
1119 break;
1120 case Type::PackExpansion:
1121 QT = cast<PackExpansionType>(T)->getPattern();
1122 break;
1123 case Type::Paren:
1124 case Type::ConstantArray:
1125 case Type::DependentSizedArray:
1126 case Type::IncompleteArray:
1127 case Type::VariableArray:
1128 case Type::FunctionProto:
1129 case Type::FunctionNoProto:
1130 return true;
1131 }
1132 }
1133}
1134
1135} // namespace
1136
1137SourceRange DeclaratorDecl::getSourceRange() const {
1138 SourceLocation RangeEnd = getLocation();
1139 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
1140 if (typeIsPostfix(TInfo->getType()))
1141 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
1142 }
1143 return SourceRange(getOuterLocStart(), RangeEnd);
1144}
1145
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001146void
Douglas Gregor20527e22010-06-15 17:44:38 +00001147QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context,
1148 unsigned NumTPLists,
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001149 TemplateParameterList **TPLists) {
1150 assert((NumTPLists == 0 || TPLists != 0) &&
1151 "Empty array of template parameters with positive size!");
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001152
1153 // Free previous template parameters (if any).
1154 if (NumTemplParamLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001155 Context.Deallocate(TemplParamLists);
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001156 TemplParamLists = 0;
1157 NumTemplParamLists = 0;
1158 }
1159 // Set info on matched template parameter lists (if any).
1160 if (NumTPLists > 0) {
Douglas Gregor20527e22010-06-15 17:44:38 +00001161 TemplParamLists = new (Context) TemplateParameterList*[NumTPLists];
Abramo Bagnarada41d0c2010-06-12 08:15:14 +00001162 NumTemplParamLists = NumTPLists;
1163 for (unsigned i = NumTPLists; i-- > 0; )
1164 TemplParamLists[i] = TPLists[i];
1165 }
1166}
1167
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001168//===----------------------------------------------------------------------===//
Nuno Lopes394ec982008-12-17 23:39:55 +00001169// VarDecl Implementation
1170//===----------------------------------------------------------------------===//
1171
Sebastian Redl833ef452010-01-26 22:01:41 +00001172const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) {
1173 switch (SC) {
Peter Collingbourne2dbb7082011-09-19 21:14:35 +00001174 case SC_None: break;
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001175 case SC_Auto: return "auto";
1176 case SC_Extern: return "extern";
1177 case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>";
1178 case SC_PrivateExtern: return "__private_extern__";
1179 case SC_Register: return "register";
1180 case SC_Static: return "static";
Sebastian Redl833ef452010-01-26 22:01:41 +00001181 }
1182
Peter Collingbourne9a8f1532011-09-20 12:40:26 +00001183 llvm_unreachable("Invalid storage class");
Sebastian Redl833ef452010-01-26 22:01:41 +00001184}
1185
Abramo Bagnaradff19302011-03-08 08:55:46 +00001186VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC,
1187 SourceLocation StartL, SourceLocation IdL,
John McCallbcd03502009-12-07 02:54:59 +00001188 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001189 StorageClass S, StorageClass SCAsWritten) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001190 return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten);
Nuno Lopes394ec982008-12-17 23:39:55 +00001191}
1192
Douglas Gregor72172e92012-01-05 21:55:30 +00001193VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1194 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl));
1195 return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0,
1196 QualType(), 0, SC_None, SC_None);
1197}
1198
Douglas Gregorbf62d642010-12-06 18:36:25 +00001199void VarDecl::setStorageClass(StorageClass SC) {
1200 assert(isLegalForVariable(SC));
1201 if (getStorageClass() != SC)
Rafael Espindola19de5612013-01-12 06:42:30 +00001202 ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001203
John McCallbeaa11c2011-05-01 02:13:58 +00001204 VarDeclBits.SClass = SC;
Douglas Gregorbf62d642010-12-06 18:36:25 +00001205}
1206
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00001207SourceRange VarDecl::getSourceRange() const {
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001208 if (const Expr *Init = getInit()) {
1209 SourceLocation InitEnd = Init->getLocEnd();
Nico Weberbbe13942013-01-22 17:00:09 +00001210 // If Init is implicit, ignore its source range and fallback on
1211 // DeclaratorDecl::getSourceRange() to handle postfix elements.
1212 if (InitEnd.isValid() && InitEnd != getLocation())
Argyrios Kyrtzidise0d64972012-10-08 23:08:41 +00001213 return SourceRange(getOuterLocStart(), InitEnd);
1214 }
Abramo Bagnaraea947882011-03-08 16:41:52 +00001215 return DeclaratorDecl::getSourceRange();
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001216}
1217
Rafael Espindola88510672013-01-04 21:18:45 +00001218template<typename T>
Rafael Espindolaf4187652013-02-14 01:18:37 +00001219static LanguageLinkage getLanguageLinkageTemplate(const T &D) {
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001220 // C++ [dcl.link]p1: All function types, function names with external linkage,
1221 // and variable names with external linkage have a language linkage.
1222 if (!isExternalLinkage(D.getLinkage()))
1223 return NoLanguageLinkage;
1224
1225 // Language linkage is a C++ concept, but saying that everything else in C has
Rafael Espindola66748e92013-01-04 20:41:40 +00001226 // C language linkage fits the implementation nicely.
Rafael Espindola576127d2012-12-28 14:21:58 +00001227 ASTContext &Context = D.getASTContext();
1228 if (!Context.getLangOpts().CPlusPlus)
Rafael Espindolaf4187652013-02-14 01:18:37 +00001229 return CLanguageLinkage;
1230
Rafael Espindola5bda63f2013-02-14 01:47:04 +00001231 // C++ [dcl.link]p4: A C language linkage is ignored in determining the
1232 // language linkage of the names of class members and the function type of
1233 // class member functions.
Rafael Espindola576127d2012-12-28 14:21:58 +00001234 const DeclContext *DC = D.getDeclContext();
1235 if (DC->isRecord())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001236 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001237
1238 // If the first decl is in an extern "C" context, any other redeclaration
1239 // will have C language linkage. If the first one is not in an extern "C"
1240 // context, we would have reported an error for any other decl being in one.
Rafael Espindola88510672013-01-04 21:18:45 +00001241 const T *First = D.getFirstDeclaration();
Rafael Espindolaf4187652013-02-14 01:18:37 +00001242 if (First->getDeclContext()->isExternCContext())
1243 return CLanguageLinkage;
1244 return CXXLanguageLinkage;
Rafael Espindola576127d2012-12-28 14:21:58 +00001245}
1246
Rafael Espindolaf4187652013-02-14 01:18:37 +00001247LanguageLinkage VarDecl::getLanguageLinkage() const {
1248 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001249}
1250
Sebastian Redl833ef452010-01-26 22:01:41 +00001251VarDecl *VarDecl::getCanonicalDecl() {
1252 return getFirstDeclaration();
1253}
1254
Daniel Dunbar9d355812012-03-09 01:51:51 +00001255VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition(
1256 ASTContext &C) const
1257{
Sebastian Redl35351a92010-01-31 22:27:38 +00001258 // C++ [basic.def]p2:
1259 // A declaration is a definition unless [...] it contains the 'extern'
1260 // specifier or a linkage-specification and neither an initializer [...],
1261 // it declares a static data member in a class declaration [...].
1262 // C++ [temp.expl.spec]p15:
1263 // An explicit specialization of a static data member of a template is a
1264 // definition if the declaration includes an initializer; otherwise, it is
1265 // a declaration.
1266 if (isStaticDataMember()) {
1267 if (isOutOfLine() && (hasInit() ||
1268 getTemplateSpecializationKind() != TSK_ExplicitSpecialization))
1269 return Definition;
1270 else
1271 return DeclarationOnly;
1272 }
1273 // C99 6.7p5:
1274 // A definition of an identifier is a declaration for that identifier that
1275 // [...] causes storage to be reserved for that object.
1276 // Note: that applies for all non-file-scope objects.
1277 // C99 6.9.2p1:
1278 // If the declaration of an identifier for an object has file scope and an
1279 // initializer, the declaration is an external definition for the identifier
1280 if (hasInit())
1281 return Definition;
1282 // AST for 'extern "C" int foo;' is annotated with 'extern'.
1283 if (hasExternalStorage())
1284 return DeclarationOnly;
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001285
John McCall8e7d6562010-08-26 03:08:43 +00001286 if (getStorageClassAsWritten() == SC_Extern ||
1287 getStorageClassAsWritten() == SC_PrivateExtern) {
Douglas Gregorec9fd132012-01-14 16:38:05 +00001288 for (const VarDecl *PrevVar = getPreviousDecl();
1289 PrevVar; PrevVar = PrevVar->getPreviousDecl()) {
Rafael Espindola7581f322012-12-17 22:23:47 +00001290 if (PrevVar->getLinkage() == InternalLinkage)
Fariborz Jahaniancc99b3c2010-06-21 16:08:37 +00001291 return DeclarationOnly;
1292 }
1293 }
Sebastian Redl35351a92010-01-31 22:27:38 +00001294 // C99 6.9.2p2:
1295 // A declaration of an object that has file scope without an initializer,
1296 // and without a storage class specifier or the scs 'static', constitutes
1297 // a tentative definition.
1298 // No such thing in C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001299 if (!C.getLangOpts().CPlusPlus && isFileVarDecl())
Sebastian Redl35351a92010-01-31 22:27:38 +00001300 return TentativeDefinition;
1301
1302 // What's left is (in C, block-scope) declarations without initializers or
1303 // external storage. These are definitions.
1304 return Definition;
1305}
1306
Sebastian Redl35351a92010-01-31 22:27:38 +00001307VarDecl *VarDecl::getActingDefinition() {
1308 DefinitionKind Kind = isThisDeclarationADefinition();
1309 if (Kind != TentativeDefinition)
1310 return 0;
1311
Chris Lattner48eb14d2010-06-14 18:31:46 +00001312 VarDecl *LastTentative = 0;
Sebastian Redl35351a92010-01-31 22:27:38 +00001313 VarDecl *First = getFirstDeclaration();
1314 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1315 I != E; ++I) {
1316 Kind = (*I)->isThisDeclarationADefinition();
1317 if (Kind == Definition)
1318 return 0;
1319 else if (Kind == TentativeDefinition)
1320 LastTentative = *I;
1321 }
1322 return LastTentative;
1323}
1324
1325bool VarDecl::isTentativeDefinitionNow() const {
1326 DefinitionKind Kind = isThisDeclarationADefinition();
1327 if (Kind != TentativeDefinition)
1328 return false;
1329
1330 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1331 if ((*I)->isThisDeclarationADefinition() == Definition)
1332 return false;
1333 }
Sebastian Redl5ca79842010-02-01 20:16:42 +00001334 return true;
Sebastian Redl35351a92010-01-31 22:27:38 +00001335}
1336
Daniel Dunbar9d355812012-03-09 01:51:51 +00001337VarDecl *VarDecl::getDefinition(ASTContext &C) {
Sebastian Redlccdb5ff2010-02-02 17:55:12 +00001338 VarDecl *First = getFirstDeclaration();
1339 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
1340 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001341 if ((*I)->isThisDeclarationADefinition(C) == Definition)
Sebastian Redl5ca79842010-02-01 20:16:42 +00001342 return *I;
1343 }
1344 return 0;
1345}
1346
Daniel Dunbar9d355812012-03-09 01:51:51 +00001347VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const {
John McCall37bb6c92010-10-29 22:22:43 +00001348 DefinitionKind Kind = DeclarationOnly;
1349
1350 const VarDecl *First = getFirstDeclaration();
1351 for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end();
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001352 I != E; ++I) {
Daniel Dunbar9d355812012-03-09 01:51:51 +00001353 Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C));
Daniel Dunbar082c62d2012-03-06 23:52:46 +00001354 if (Kind == Definition)
1355 break;
1356 }
John McCall37bb6c92010-10-29 22:22:43 +00001357
1358 return Kind;
1359}
1360
Sebastian Redl5ca79842010-02-01 20:16:42 +00001361const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const {
Sebastian Redl833ef452010-01-26 22:01:41 +00001362 redecl_iterator I = redecls_begin(), E = redecls_end();
1363 while (I != E && !I->getInit())
1364 ++I;
1365
1366 if (I != E) {
Sebastian Redl5ca79842010-02-01 20:16:42 +00001367 D = *I;
Sebastian Redl833ef452010-01-26 22:01:41 +00001368 return I->getInit();
1369 }
1370 return 0;
1371}
1372
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001373bool VarDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00001374 if (Decl::isOutOfLine())
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001375 return true;
Chandler Carruthf50ef6e2010-02-21 07:08:09 +00001376
1377 if (!isStaticDataMember())
1378 return false;
1379
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001380 // If this static data member was instantiated from a static data member of
1381 // a class template, check whether that static data member was defined
1382 // out-of-line.
1383 if (VarDecl *VD = getInstantiatedFromStaticDataMember())
1384 return VD->isOutOfLine();
1385
1386 return false;
1387}
1388
Douglas Gregor1d957a32009-10-27 18:42:08 +00001389VarDecl *VarDecl::getOutOfLineDefinition() {
1390 if (!isStaticDataMember())
1391 return 0;
1392
1393 for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end();
1394 RD != RDEnd; ++RD) {
1395 if (RD->getLexicalDeclContext()->isFileContext())
1396 return *RD;
1397 }
1398
1399 return 0;
1400}
1401
Douglas Gregord5058122010-02-11 01:19:42 +00001402void VarDecl::setInit(Expr *I) {
Sebastian Redl833ef452010-01-26 22:01:41 +00001403 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
1404 Eval->~EvaluatedStmt();
Douglas Gregord5058122010-02-11 01:19:42 +00001405 getASTContext().Deallocate(Eval);
Sebastian Redl833ef452010-01-26 22:01:41 +00001406 }
1407
1408 Init = I;
1409}
1410
Daniel Dunbar9d355812012-03-09 01:51:51 +00001411bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001412 const LangOptions &Lang = C.getLangOpts();
Richard Smith242ad892011-12-21 02:55:12 +00001413
Richard Smith35ecb362012-03-02 04:14:40 +00001414 if (!Lang.CPlusPlus)
1415 return false;
1416
1417 // In C++11, any variable of reference type can be used in a constant
1418 // expression if it is initialized by a constant expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001419 if (Lang.CPlusPlus11 && getType()->isReferenceType())
Richard Smith35ecb362012-03-02 04:14:40 +00001420 return true;
1421
1422 // Only const objects can be used in constant expressions in C++. C++98 does
Richard Smith242ad892011-12-21 02:55:12 +00001423 // not require the variable to be non-volatile, but we consider this to be a
1424 // defect.
Richard Smith35ecb362012-03-02 04:14:40 +00001425 if (!getType().isConstQualified() || getType().isVolatileQualified())
Richard Smith242ad892011-12-21 02:55:12 +00001426 return false;
1427
1428 // In C++, const, non-volatile variables of integral or enumeration types
1429 // can be used in constant expressions.
1430 if (getType()->isIntegralOrEnumerationType())
1431 return true;
1432
Richard Smith35ecb362012-03-02 04:14:40 +00001433 // Additionally, in C++11, non-volatile constexpr variables can be used in
1434 // constant expressions.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001435 return Lang.CPlusPlus11 && isConstexpr();
Richard Smith242ad892011-12-21 02:55:12 +00001436}
1437
Richard Smithd0b4dd62011-12-19 06:19:21 +00001438/// Convert the initializer for this declaration to the elaborated EvaluatedStmt
1439/// form, which contains extra information on the evaluated value of the
1440/// initializer.
1441EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const {
1442 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
1443 if (!Eval) {
1444 Stmt *S = Init.get<Stmt *>();
1445 Eval = new (getASTContext()) EvaluatedStmt;
1446 Eval->Value = S;
1447 Init = Eval;
1448 }
1449 return Eval;
1450}
1451
Richard Smithdafff942012-01-14 04:30:29 +00001452APValue *VarDecl::evaluateValue() const {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001453 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithdafff942012-01-14 04:30:29 +00001454 return evaluateValue(Notes);
1455}
1456
1457APValue *VarDecl::evaluateValue(
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001458 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001459 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1460
1461 // We only produce notes indicating why an initializer is non-constant the
1462 // first time it is evaluated. FIXME: The notes won't always be emitted the
1463 // first time we try evaluation, so might not be produced at all.
1464 if (Eval->WasEvaluated)
Richard Smithdafff942012-01-14 04:30:29 +00001465 return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001466
1467 const Expr *Init = cast<Expr>(Eval->Value);
1468 assert(!Init->isValueDependent());
1469
1470 if (Eval->IsEvaluating) {
1471 // FIXME: Produce a diagnostic for self-initialization.
1472 Eval->CheckedICE = true;
1473 Eval->IsICE = false;
Richard Smithdafff942012-01-14 04:30:29 +00001474 return 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001475 }
1476
1477 Eval->IsEvaluating = true;
1478
1479 bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(),
1480 this, Notes);
1481
1482 // Ensure the result is an uninitialized APValue if evaluation fails.
1483 if (!Result)
1484 Eval->Evaluated = APValue();
1485
1486 Eval->IsEvaluating = false;
1487 Eval->WasEvaluated = true;
1488
1489 // In C++11, we have determined whether the initializer was a constant
1490 // expression as a side-effect.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001491 if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) {
Richard Smithd0b4dd62011-12-19 06:19:21 +00001492 Eval->CheckedICE = true;
Eli Friedman8f66cdf2012-02-06 21:50:18 +00001493 Eval->IsICE = Result && Notes.empty();
Richard Smithd0b4dd62011-12-19 06:19:21 +00001494 }
1495
Richard Smithdafff942012-01-14 04:30:29 +00001496 return Result ? &Eval->Evaluated : 0;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001497}
1498
1499bool VarDecl::checkInitIsICE() const {
John McCalla59dc2f2012-01-05 00:13:19 +00001500 // Initializers of weak variables are never ICEs.
1501 if (isWeak())
1502 return false;
1503
Richard Smithd0b4dd62011-12-19 06:19:21 +00001504 EvaluatedStmt *Eval = ensureEvaluatedStmt();
1505 if (Eval->CheckedICE)
1506 // We have already checked whether this subexpression is an
1507 // integral constant expression.
1508 return Eval->IsICE;
1509
1510 const Expr *Init = cast<Expr>(Eval->Value);
1511 assert(!Init->isValueDependent());
1512
1513 // In C++11, evaluate the initializer to check whether it's a constant
1514 // expression.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001515 if (getASTContext().getLangOpts().CPlusPlus11) {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001516 SmallVector<PartialDiagnosticAt, 8> Notes;
Richard Smithd0b4dd62011-12-19 06:19:21 +00001517 evaluateValue(Notes);
1518 return Eval->IsICE;
1519 }
1520
1521 // It's an ICE whether or not the definition we found is
1522 // out-of-line. See DR 721 and the discussion in Clang PR
1523 // 6206 for details.
1524
1525 if (Eval->CheckingICE)
1526 return false;
1527 Eval->CheckingICE = true;
1528
1529 Eval->IsICE = Init->isIntegerConstantExpr(getASTContext());
1530 Eval->CheckingICE = false;
1531 Eval->CheckedICE = true;
1532 return Eval->IsICE;
1533}
1534
Douglas Gregorfe314812011-06-21 17:03:29 +00001535bool VarDecl::extendsLifetimeOfTemporary() const {
Douglas Gregord410c082011-06-21 18:20:46 +00001536 assert(getType()->isReferenceType() &&"Non-references never extend lifetime");
Douglas Gregorfe314812011-06-21 17:03:29 +00001537
1538 const Expr *E = getInit();
1539 if (!E)
1540 return false;
1541
1542 if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E))
1543 E = Cleanups->getSubExpr();
1544
1545 return isa<MaterializeTemporaryExpr>(E);
1546}
1547
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001548VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001549 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001550 return cast<VarDecl>(MSI->getInstantiatedFrom());
1551
1552 return 0;
1553}
1554
Douglas Gregor3c74d412009-10-14 20:14:33 +00001555TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const {
Sebastian Redl35351a92010-01-31 22:27:38 +00001556 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
Douglas Gregor86d142a2009-10-08 07:24:58 +00001557 return MSI->getTemplateSpecializationKind();
1558
1559 return TSK_Undeclared;
1560}
1561
Douglas Gregor3cc3cde2009-10-14 21:29:40 +00001562MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001563 return getASTContext().getInstantiatedFromStaticDataMember(this);
1564}
1565
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001566void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1567 SourceLocation PointOfInstantiation) {
Douglas Gregor06db9f52009-10-12 20:18:28 +00001568 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
Douglas Gregor86d142a2009-10-08 07:24:58 +00001569 assert(MSI && "Not an instantiated static data member?");
1570 MSI->setTemplateSpecializationKind(TSK);
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00001571 if (TSK != TSK_ExplicitSpecialization &&
1572 PointOfInstantiation.isValid() &&
1573 MSI->getPointOfInstantiation().isInvalid())
1574 MSI->setPointOfInstantiation(PointOfInstantiation);
Douglas Gregora6ef8f02009-07-24 20:34:43 +00001575}
1576
Sebastian Redl833ef452010-01-26 22:01:41 +00001577//===----------------------------------------------------------------------===//
1578// ParmVarDecl Implementation
1579//===----------------------------------------------------------------------===//
Douglas Gregor0760fa12009-03-10 23:43:53 +00001580
Sebastian Redl833ef452010-01-26 22:01:41 +00001581ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00001582 SourceLocation StartLoc,
1583 SourceLocation IdLoc, IdentifierInfo *Id,
Sebastian Redl833ef452010-01-26 22:01:41 +00001584 QualType T, TypeSourceInfo *TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001585 StorageClass S, StorageClass SCAsWritten,
1586 Expr *DefArg) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00001587 return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001588 S, SCAsWritten, DefArg);
Douglas Gregor0760fa12009-03-10 23:43:53 +00001589}
1590
Douglas Gregor72172e92012-01-05 21:55:30 +00001591ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1592 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl));
1593 return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(),
1594 0, QualType(), 0, SC_None, SC_None, 0);
1595}
1596
Argyrios Kyrtzidis4c6efa622011-07-30 17:23:26 +00001597SourceRange ParmVarDecl::getSourceRange() const {
1598 if (!hasInheritedDefaultArg()) {
1599 SourceRange ArgRange = getDefaultArgRange();
1600 if (ArgRange.isValid())
1601 return SourceRange(getOuterLocStart(), ArgRange.getEnd());
1602 }
1603
1604 return DeclaratorDecl::getSourceRange();
1605}
1606
Sebastian Redl833ef452010-01-26 22:01:41 +00001607Expr *ParmVarDecl::getDefaultArg() {
1608 assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!");
1609 assert(!hasUninstantiatedDefaultArg() &&
1610 "Default argument is not yet instantiated!");
1611
1612 Expr *Arg = getInit();
John McCall5d413782010-12-06 08:20:24 +00001613 if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg))
Sebastian Redl833ef452010-01-26 22:01:41 +00001614 return E->getSubExpr();
Douglas Gregor0760fa12009-03-10 23:43:53 +00001615
Sebastian Redl833ef452010-01-26 22:01:41 +00001616 return Arg;
1617}
1618
Sebastian Redl833ef452010-01-26 22:01:41 +00001619SourceRange ParmVarDecl::getDefaultArgRange() const {
1620 if (const Expr *E = getInit())
1621 return E->getSourceRange();
1622
1623 if (hasUninstantiatedDefaultArg())
1624 return getUninstantiatedDefaultArg()->getSourceRange();
1625
1626 return SourceRange();
Argyrios Kyrtzidis02dd4f92009-07-05 22:21:56 +00001627}
1628
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +00001629bool ParmVarDecl::isParameterPack() const {
1630 return isa<PackExpansionType>(getType());
1631}
1632
Ted Kremenek540017e2011-10-06 05:00:56 +00001633void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) {
1634 getASTContext().setParameterIndex(this, parameterIndex);
1635 ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel;
1636}
1637
1638unsigned ParmVarDecl::getParameterIndexLarge() const {
1639 return getASTContext().getParameterIndex(this);
1640}
1641
Nuno Lopes394ec982008-12-17 23:39:55 +00001642//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00001643// FunctionDecl Implementation
1644//===----------------------------------------------------------------------===//
1645
Douglas Gregorb11aad82011-02-19 18:51:44 +00001646void FunctionDecl::getNameForDiagnostic(std::string &S,
1647 const PrintingPolicy &Policy,
1648 bool Qualified) const {
1649 NamedDecl::getNameForDiagnostic(S, Policy, Qualified);
1650 const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs();
1651 if (TemplateArgs)
1652 S += TemplateSpecializationType::PrintTemplateArgumentList(
1653 TemplateArgs->data(),
1654 TemplateArgs->size(),
1655 Policy);
1656
1657}
1658
Ted Kremenek186a0742010-04-29 16:49:01 +00001659bool FunctionDecl::isVariadic() const {
1660 if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>())
1661 return FT->isVariadic();
1662 return false;
1663}
1664
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001665bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const {
1666 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Francois Pichet1c229c02011-04-22 22:18:13 +00001667 if (I->Body || I->IsLateTemplateParsed) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001668 Definition = *I;
1669 return true;
1670 }
1671 }
1672
1673 return false;
1674}
1675
Anders Carlsson9bd7d162011-05-14 23:26:09 +00001676bool FunctionDecl::hasTrivialBody() const
1677{
1678 Stmt *S = getBody();
1679 if (!S) {
1680 // Since we don't have a body for this function, we don't know if it's
1681 // trivial or not.
1682 return false;
1683 }
1684
1685 if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty())
1686 return true;
1687 return false;
1688}
1689
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001690bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const {
1691 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
Alexis Hunt61ae8d32011-05-23 23:14:04 +00001692 if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00001693 Definition = I->IsDeleted ? I->getCanonicalDecl() : *I;
1694 return true;
1695 }
1696 }
1697
1698 return false;
1699}
1700
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +00001701Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const {
Argyrios Kyrtzidis1506d9b2009-07-14 03:20:21 +00001702 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) {
1703 if (I->Body) {
1704 Definition = *I;
1705 return I->Body.get(getASTContext().getExternalSource());
Francois Pichet1c229c02011-04-22 22:18:13 +00001706 } else if (I->IsLateTemplateParsed) {
1707 Definition = *I;
1708 return 0;
Douglas Gregor89f238c2008-04-21 02:02:58 +00001709 }
1710 }
1711
1712 return 0;
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001713}
1714
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001715void FunctionDecl::setBody(Stmt *B) {
1716 Body = B;
Douglas Gregor027ba502010-12-06 17:49:01 +00001717 if (B)
Argyrios Kyrtzidisa3aeb5a2009-06-20 08:09:14 +00001718 EndRangeLoc = B->getLocEnd();
1719}
1720
Douglas Gregor7d9120c2010-09-28 21:55:22 +00001721void FunctionDecl::setPure(bool P) {
1722 IsPure = P;
1723 if (P)
1724 if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext()))
1725 Parent->markedVirtualFunctionPure();
1726}
1727
Douglas Gregor16618f22009-09-12 00:17:51 +00001728bool FunctionDecl::isMain() const {
John McCall53ffd372011-05-15 17:49:20 +00001729 const TranslationUnitDecl *tunit =
1730 dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
1731 return tunit &&
David Blaikiebbafb8a2012-03-11 07:00:24 +00001732 !tunit->getASTContext().getLangOpts().Freestanding &&
John McCall53ffd372011-05-15 17:49:20 +00001733 getIdentifier() &&
1734 getIdentifier()->isStr("main");
1735}
1736
1737bool FunctionDecl::isReservedGlobalPlacementOperator() const {
1738 assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName);
1739 assert(getDeclName().getCXXOverloadedOperator() == OO_New ||
1740 getDeclName().getCXXOverloadedOperator() == OO_Delete ||
1741 getDeclName().getCXXOverloadedOperator() == OO_Array_New ||
1742 getDeclName().getCXXOverloadedOperator() == OO_Array_Delete);
1743
1744 if (isa<CXXRecordDecl>(getDeclContext())) return false;
1745 assert(getDeclContext()->getRedeclContext()->isTranslationUnit());
1746
1747 const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>();
1748 if (proto->getNumArgs() != 2 || proto->isVariadic()) return false;
1749
1750 ASTContext &Context =
1751 cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext())
1752 ->getASTContext();
1753
1754 // The result type and first argument type are constant across all
1755 // these operators. The second argument must be exactly void*.
1756 return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy);
Douglas Gregore62c0a42009-02-24 01:23:02 +00001757}
1758
Rafael Espindolaf4187652013-02-14 01:18:37 +00001759LanguageLinkage FunctionDecl::getLanguageLinkage() const {
Rafael Espindola6239e052013-01-12 15:27:44 +00001760 // Users expect to be able to write
1761 // extern "C" void *__builtin_alloca (size_t);
1762 // so consider builtins as having C language linkage.
Rafael Espindolac48f7342013-01-12 15:27:43 +00001763 if (getBuiltinID())
Rafael Espindolaf4187652013-02-14 01:18:37 +00001764 return CLanguageLinkage;
Rafael Espindolac48f7342013-01-12 15:27:43 +00001765
Rafael Espindolaf4187652013-02-14 01:18:37 +00001766 return getLanguageLinkageTemplate(*this);
Rafael Espindola576127d2012-12-28 14:21:58 +00001767}
1768
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001769bool FunctionDecl::isGlobal() const {
1770 if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this))
1771 return Method->isStatic();
1772
John McCall8e7d6562010-08-26 03:08:43 +00001773 if (getStorageClass() == SC_Static)
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001774 return false;
1775
Mike Stump11289f42009-09-09 15:08:12 +00001776 for (const DeclContext *DC = getDeclContext();
Douglas Gregorf1b876d2009-03-31 16:35:03 +00001777 DC->isNamespace();
1778 DC = DC->getParent()) {
1779 if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) {
1780 if (!Namespace->getDeclName())
1781 return false;
1782 break;
1783 }
1784 }
1785
1786 return true;
1787}
1788
Richard Smith10876ef2013-01-17 01:30:42 +00001789bool FunctionDecl::isNoReturn() const {
1790 return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() ||
Richard Smithdebc59d2013-01-30 05:45:05 +00001791 hasAttr<C11NoReturnAttr>() ||
Richard Smith10876ef2013-01-17 01:30:42 +00001792 getType()->getAs<FunctionType>()->getNoReturnAttr();
1793}
1794
Sebastian Redl833ef452010-01-26 22:01:41 +00001795void
1796FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) {
1797 redeclarable_base::setPreviousDeclaration(PrevDecl);
1798
1799 if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) {
1800 FunctionTemplateDecl *PrevFunTmpl
1801 = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0;
1802 assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch");
1803 FunTmpl->setPreviousDeclaration(PrevFunTmpl);
1804 }
Douglas Gregorff76cb92010-12-09 16:59:22 +00001805
Axel Naumannfbc7b982011-11-08 18:21:06 +00001806 if (PrevDecl && PrevDecl->IsInline)
Douglas Gregorff76cb92010-12-09 16:59:22 +00001807 IsInline = true;
Sebastian Redl833ef452010-01-26 22:01:41 +00001808}
1809
1810const FunctionDecl *FunctionDecl::getCanonicalDecl() const {
1811 return getFirstDeclaration();
1812}
1813
1814FunctionDecl *FunctionDecl::getCanonicalDecl() {
1815 return getFirstDeclaration();
1816}
1817
Douglas Gregorbf62d642010-12-06 18:36:25 +00001818void FunctionDecl::setStorageClass(StorageClass SC) {
1819 assert(isLegalForFunction(SC));
1820 if (getStorageClass() != SC)
Rafael Espindola19de5612013-01-12 06:42:30 +00001821 ClearLinkageCache();
Douglas Gregorbf62d642010-12-06 18:36:25 +00001822
1823 SClass = SC;
1824}
1825
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001826/// \brief Returns a value indicating whether this function
1827/// corresponds to a builtin function.
1828///
1829/// The function corresponds to a built-in function if it is
1830/// declared at translation scope or within an extern "C" block and
1831/// its name matches with the name of a builtin. The returned value
1832/// will be 0 for functions that do not correspond to a builtin, a
Mike Stump11289f42009-09-09 15:08:12 +00001833/// value of type \c Builtin::ID if in the target-independent range
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001834/// \c [1,Builtin::First), or a target-specific builtin value.
Douglas Gregor15fc9562009-09-12 00:22:50 +00001835unsigned FunctionDecl::getBuiltinID() const {
Daniel Dunbar304314d2012-03-06 23:52:37 +00001836 if (!getIdentifier())
Douglas Gregore711f702009-02-14 18:57:46 +00001837 return 0;
1838
1839 unsigned BuiltinID = getIdentifier()->getBuiltinID();
Daniel Dunbar304314d2012-03-06 23:52:37 +00001840 if (!BuiltinID)
1841 return 0;
1842
1843 ASTContext &Context = getASTContext();
Douglas Gregore711f702009-02-14 18:57:46 +00001844 if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
1845 return BuiltinID;
1846
1847 // This function has the name of a known C library
1848 // function. Determine whether it actually refers to the C library
1849 // function or whether it just has the same name.
1850
Douglas Gregora908e7f2009-02-17 03:23:10 +00001851 // If this is a static function, it's not a builtin.
John McCall8e7d6562010-08-26 03:08:43 +00001852 if (getStorageClass() == SC_Static)
Douglas Gregora908e7f2009-02-17 03:23:10 +00001853 return 0;
1854
Douglas Gregore711f702009-02-14 18:57:46 +00001855 // If this function is at translation-unit scope and we're not in
1856 // C++, it refers to the C library function.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001857 if (!Context.getLangOpts().CPlusPlus &&
Douglas Gregore711f702009-02-14 18:57:46 +00001858 getDeclContext()->isTranslationUnit())
1859 return BuiltinID;
1860
1861 // If the function is in an extern "C" linkage specification and is
1862 // not marked "overloadable", it's the real function.
1863 if (isa<LinkageSpecDecl>(getDeclContext()) &&
Mike Stump11289f42009-09-09 15:08:12 +00001864 cast<LinkageSpecDecl>(getDeclContext())->getLanguage()
Douglas Gregore711f702009-02-14 18:57:46 +00001865 == LinkageSpecDecl::lang_c &&
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001866 !getAttr<OverloadableAttr>())
Douglas Gregore711f702009-02-14 18:57:46 +00001867 return BuiltinID;
1868
1869 // Not a builtin
Douglas Gregorb9063fc2009-02-13 23:20:09 +00001870 return 0;
1871}
1872
1873
Chris Lattner47c0d002009-04-25 06:03:53 +00001874/// getNumParams - Return the number of parameters this function must have
Bob Wilsonb39017a2011-01-10 18:23:55 +00001875/// based on its FunctionType. This is the length of the ParamInfo array
Chris Lattner47c0d002009-04-25 06:03:53 +00001876/// after it has been created.
1877unsigned FunctionDecl::getNumParams() const {
Eli Friedman5c27c4c2012-08-30 22:22:09 +00001878 const FunctionType *FT = getType()->castAs<FunctionType>();
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001879 if (isa<FunctionNoProtoType>(FT))
Chris Lattner88f70d62008-03-15 05:43:15 +00001880 return 0;
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001881 return cast<FunctionProtoType>(FT)->getNumArgs();
Mike Stump11289f42009-09-09 15:08:12 +00001882
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001883}
1884
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00001885void FunctionDecl::setParams(ASTContext &C,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001886 ArrayRef<ParmVarDecl *> NewParamInfo) {
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001887 assert(ParamInfo == 0 && "Already has param info!");
David Blaikie9c70e042011-09-21 18:16:56 +00001888 assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!");
Mike Stump11289f42009-09-09 15:08:12 +00001889
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001890 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00001891 if (!NewParamInfo.empty()) {
1892 ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()];
1893 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Chris Lattner8f5bf2f2007-01-21 19:04:10 +00001894 }
Chris Lattnerc5cdf4d2007-01-21 07:42:07 +00001895}
Chris Lattner41943152007-01-25 04:52:46 +00001896
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001897void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) {
James Molloy6f8780b2012-02-29 10:24:19 +00001898 assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!");
1899
1900 if (!NewDecls.empty()) {
1901 NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()];
1902 std::copy(NewDecls.begin(), NewDecls.end(), A);
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001903 DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size());
James Molloy6f8780b2012-02-29 10:24:19 +00001904 }
1905}
1906
Chris Lattner58258242008-04-10 02:22:51 +00001907/// getMinRequiredArguments - Returns the minimum number of arguments
1908/// needed to call this function. This may be fewer than the number of
1909/// function parameters, if some of the parameters have default
Douglas Gregor7825bf32011-01-06 22:09:01 +00001910/// arguments (in C++) or the last parameter is a parameter pack.
Chris Lattner58258242008-04-10 02:22:51 +00001911unsigned FunctionDecl::getMinRequiredArguments() const {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001912 if (!getASTContext().getLangOpts().CPlusPlus)
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001913 return getNumParams();
1914
Douglas Gregor7825bf32011-01-06 22:09:01 +00001915 unsigned NumRequiredArgs = getNumParams();
1916
1917 // If the last parameter is a parameter pack, we don't need an argument for
1918 // it.
1919 if (NumRequiredArgs > 0 &&
1920 getParamDecl(NumRequiredArgs - 1)->isParameterPack())
1921 --NumRequiredArgs;
1922
1923 // If this parameter has a default argument, we don't need an argument for
1924 // it.
1925 while (NumRequiredArgs > 0 &&
1926 getParamDecl(NumRequiredArgs-1)->hasDefaultArg())
Chris Lattner58258242008-04-10 02:22:51 +00001927 --NumRequiredArgs;
1928
Douglas Gregor0dd423e2011-01-11 01:52:23 +00001929 // We might have parameter packs before the end. These can't be deduced,
1930 // but they can still handle multiple arguments.
1931 unsigned ArgIdx = NumRequiredArgs;
1932 while (ArgIdx > 0) {
1933 if (getParamDecl(ArgIdx - 1)->isParameterPack())
1934 NumRequiredArgs = ArgIdx;
1935
1936 --ArgIdx;
1937 }
1938
Chris Lattner58258242008-04-10 02:22:51 +00001939 return NumRequiredArgs;
1940}
1941
Eli Friedman1b125c32012-02-07 03:50:18 +00001942static bool RedeclForcesDefC99(const FunctionDecl *Redecl) {
1943 // Only consider file-scope declarations in this test.
1944 if (!Redecl->getLexicalDeclContext()->isTranslationUnit())
1945 return false;
1946
1947 // Only consider explicit declarations; the presence of a builtin for a
1948 // libcall shouldn't affect whether a definition is externally visible.
1949 if (Redecl->isImplicit())
1950 return false;
1951
1952 if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern)
1953 return true; // Not an inline definition
1954
1955 return false;
1956}
1957
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001958/// \brief For a function declaration in C or C++, determine whether this
1959/// declaration causes the definition to be externally visible.
1960///
Eli Friedman1b125c32012-02-07 03:50:18 +00001961/// Specifically, this determines if adding the current declaration to the set
1962/// of redeclarations of the given functions causes
1963/// isInlineDefinitionExternallyVisible to change from false to true.
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001964bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const {
1965 assert(!doesThisDeclarationHaveABody() &&
1966 "Must have a declaration without a body.");
1967
1968 ASTContext &Context = getASTContext();
1969
David Blaikiebbafb8a2012-03-11 07:00:24 +00001970 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00001971 // With GNU inlining, a declaration with 'inline' but not 'extern', forces
1972 // an externally visible definition.
1973 //
1974 // FIXME: What happens if gnu_inline gets added on after the first
1975 // declaration?
1976 if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern)
1977 return false;
1978
1979 const FunctionDecl *Prev = this;
1980 bool FoundBody = false;
1981 while ((Prev = Prev->getPreviousDecl())) {
1982 FoundBody |= Prev->Body;
1983
1984 if (Prev->Body) {
1985 // If it's not the case that both 'inline' and 'extern' are
1986 // specified on the definition, then it is always externally visible.
1987 if (!Prev->isInlineSpecified() ||
1988 Prev->getStorageClassAsWritten() != SC_Extern)
1989 return false;
1990 } else if (Prev->isInlineSpecified() &&
1991 Prev->getStorageClassAsWritten() != SC_Extern) {
1992 return false;
1993 }
1994 }
1995 return FoundBody;
1996 }
1997
David Blaikiebbafb8a2012-03-11 07:00:24 +00001998 if (Context.getLangOpts().CPlusPlus)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00001999 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002000
2001 // C99 6.7.4p6:
2002 // [...] If all of the file scope declarations for a function in a
2003 // translation unit include the inline function specifier without extern,
2004 // then the definition in that translation unit is an inline definition.
2005 if (isInlineSpecified() && getStorageClass() != SC_Extern)
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002006 return false;
Eli Friedman1b125c32012-02-07 03:50:18 +00002007 const FunctionDecl *Prev = this;
2008 bool FoundBody = false;
2009 while ((Prev = Prev->getPreviousDecl())) {
2010 FoundBody |= Prev->Body;
2011 if (RedeclForcesDefC99(Prev))
2012 return false;
2013 }
2014 return FoundBody;
Nick Lewycky26da4dd2011-07-18 05:26:13 +00002015}
2016
Richard Smithf3814ad2013-01-25 00:08:28 +00002017/// \brief For an inline function definition in C, or for a gnu_inline function
2018/// in C++, determine whether the definition will be externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002019///
2020/// Inline function definitions are always available for inlining optimizations.
2021/// However, depending on the language dialect, declaration specifiers, and
2022/// attributes, the definition of an inline function may or may not be
2023/// "externally" visible to other translation units in the program.
2024///
2025/// In C99, inline definitions are not externally visible by default. However,
Mike Stump13c66702010-01-06 02:05:39 +00002026/// if even one of the global-scope declarations is marked "extern inline", the
Douglas Gregor299d76e2009-09-13 07:46:26 +00002027/// inline definition becomes externally visible (C99 6.7.4p6).
2028///
2029/// In GNU89 mode, or if the gnu_inline attribute is attached to the function
2030/// definition, we use the GNU semantics for inline, which are nearly the
2031/// opposite of C99 semantics. In particular, "inline" by itself will create
2032/// an externally visible symbol, but "extern inline" will not create an
2033/// externally visible symbol.
2034bool FunctionDecl::isInlineDefinitionExternallyVisible() const {
Alexis Hunt4a8ea102011-05-06 20:44:56 +00002035 assert(doesThisDeclarationHaveABody() && "Must have the function definition");
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002036 assert(isInlined() && "Function must be inline");
Douglas Gregorb7e5c842009-10-27 23:26:40 +00002037 ASTContext &Context = getASTContext();
Douglas Gregor299d76e2009-09-13 07:46:26 +00002038
David Blaikiebbafb8a2012-03-11 07:00:24 +00002039 if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002040 // Note: If you change the logic here, please change
2041 // doesDeclarationForceExternallyVisibleDefinition as well.
2042 //
Douglas Gregorff76cb92010-12-09 16:59:22 +00002043 // If it's not the case that both 'inline' and 'extern' are
2044 // specified on the definition, then this inline definition is
2045 // externally visible.
2046 if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern))
2047 return true;
2048
2049 // If any declaration is 'inline' but not 'extern', then this definition
2050 // is externally visible.
Douglas Gregor299d76e2009-09-13 07:46:26 +00002051 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2052 Redecl != RedeclEnd;
2053 ++Redecl) {
Douglas Gregorff76cb92010-12-09 16:59:22 +00002054 if (Redecl->isInlineSpecified() &&
2055 Redecl->getStorageClassAsWritten() != SC_Extern)
Douglas Gregor299d76e2009-09-13 07:46:26 +00002056 return true;
Douglas Gregorff76cb92010-12-09 16:59:22 +00002057 }
Douglas Gregor299d76e2009-09-13 07:46:26 +00002058
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002059 return false;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002060 }
Eli Friedman1b125c32012-02-07 03:50:18 +00002061
Richard Smithf3814ad2013-01-25 00:08:28 +00002062 // The rest of this function is C-only.
2063 assert(!Context.getLangOpts().CPlusPlus &&
2064 "should not use C inline rules in C++");
2065
Douglas Gregor299d76e2009-09-13 07:46:26 +00002066 // C99 6.7.4p6:
2067 // [...] If all of the file scope declarations for a function in a
2068 // translation unit include the inline function specifier without extern,
2069 // then the definition in that translation unit is an inline definition.
2070 for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end();
2071 Redecl != RedeclEnd;
2072 ++Redecl) {
Eli Friedman1b125c32012-02-07 03:50:18 +00002073 if (RedeclForcesDefC99(*Redecl))
2074 return true;
Douglas Gregor299d76e2009-09-13 07:46:26 +00002075 }
2076
2077 // C99 6.7.4p6:
2078 // An inline definition does not provide an external definition for the
2079 // function, and does not forbid an external definition in another
2080 // translation unit.
Douglas Gregor76fe50c2009-04-28 06:37:30 +00002081 return false;
2082}
2083
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002084/// getOverloadedOperator - Which C++ overloaded operator this
2085/// function represents, if any.
2086OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const {
Douglas Gregor163c5852008-11-18 14:39:36 +00002087 if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName)
2088 return getDeclName().getCXXOverloadedOperator();
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00002089 else
2090 return OO_None;
2091}
2092
Alexis Huntc88db062010-01-13 09:01:02 +00002093/// getLiteralIdentifier - The literal suffix identifier this function
2094/// represents, if any.
2095const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const {
2096 if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName)
2097 return getDeclName().getCXXLiteralIdentifier();
2098 else
2099 return 0;
2100}
2101
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002102FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const {
2103 if (TemplateOrSpecialization.isNull())
2104 return TK_NonTemplate;
2105 if (TemplateOrSpecialization.is<FunctionTemplateDecl *>())
2106 return TK_FunctionTemplate;
2107 if (TemplateOrSpecialization.is<MemberSpecializationInfo *>())
2108 return TK_MemberSpecialization;
2109 if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>())
2110 return TK_FunctionTemplateSpecialization;
2111 if (TemplateOrSpecialization.is
2112 <DependentFunctionTemplateSpecializationInfo*>())
2113 return TK_DependentFunctionTemplateSpecialization;
2114
David Blaikie83d382b2011-09-23 05:06:16 +00002115 llvm_unreachable("Did we miss a TemplateOrSpecialization type?");
Argyrios Kyrtzidiscb6f3462010-06-22 09:54:51 +00002116}
2117
Douglas Gregord801b062009-10-07 23:56:10 +00002118FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const {
Douglas Gregor06db9f52009-10-12 20:18:28 +00002119 if (MemberSpecializationInfo *Info = getMemberSpecializationInfo())
Douglas Gregord801b062009-10-07 23:56:10 +00002120 return cast<FunctionDecl>(Info->getInstantiatedFrom());
2121
2122 return 0;
2123}
2124
Douglas Gregor06db9f52009-10-12 20:18:28 +00002125MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const {
2126 return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2127}
2128
Douglas Gregord801b062009-10-07 23:56:10 +00002129void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002130FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C,
2131 FunctionDecl *FD,
Douglas Gregord801b062009-10-07 23:56:10 +00002132 TemplateSpecializationKind TSK) {
2133 assert(TemplateOrSpecialization.isNull() &&
2134 "Member function is already a specialization");
2135 MemberSpecializationInfo *Info
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002136 = new (C) MemberSpecializationInfo(FD, TSK);
Douglas Gregord801b062009-10-07 23:56:10 +00002137 TemplateOrSpecialization = Info;
2138}
2139
Douglas Gregorafca3b42009-10-27 20:53:28 +00002140bool FunctionDecl::isImplicitlyInstantiable() const {
Douglas Gregor69f6a362010-05-17 17:34:56 +00002141 // If the function is invalid, it can't be implicitly instantiated.
2142 if (isInvalidDecl())
Douglas Gregorafca3b42009-10-27 20:53:28 +00002143 return false;
2144
2145 switch (getTemplateSpecializationKind()) {
2146 case TSK_Undeclared:
Douglas Gregorafca3b42009-10-27 20:53:28 +00002147 case TSK_ExplicitInstantiationDefinition:
2148 return false;
2149
2150 case TSK_ImplicitInstantiation:
2151 return true;
2152
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002153 // It is possible to instantiate TSK_ExplicitSpecialization kind
2154 // if the FunctionDecl has a class scope specialization pattern.
2155 case TSK_ExplicitSpecialization:
2156 return getClassScopeSpecializationPattern() != 0;
2157
Douglas Gregorafca3b42009-10-27 20:53:28 +00002158 case TSK_ExplicitInstantiationDeclaration:
2159 // Handled below.
2160 break;
2161 }
2162
2163 // Find the actual template from which we will instantiate.
2164 const FunctionDecl *PatternDecl = getTemplateInstantiationPattern();
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002165 bool HasPattern = false;
Douglas Gregorafca3b42009-10-27 20:53:28 +00002166 if (PatternDecl)
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002167 HasPattern = PatternDecl->hasBody(PatternDecl);
Douglas Gregorafca3b42009-10-27 20:53:28 +00002168
2169 // C++0x [temp.explicit]p9:
2170 // Except for inline functions, other explicit instantiation declarations
2171 // have the effect of suppressing the implicit instantiation of the entity
2172 // to which they refer.
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002173 if (!HasPattern || !PatternDecl)
Douglas Gregorafca3b42009-10-27 20:53:28 +00002174 return true;
2175
Douglas Gregor583dcaf2009-10-27 21:11:48 +00002176 return PatternDecl->isInlined();
Ted Kremenek85825ae2011-12-01 00:59:17 +00002177}
2178
2179bool FunctionDecl::isTemplateInstantiation() const {
2180 switch (getTemplateSpecializationKind()) {
2181 case TSK_Undeclared:
2182 case TSK_ExplicitSpecialization:
2183 return false;
2184 case TSK_ImplicitInstantiation:
2185 case TSK_ExplicitInstantiationDeclaration:
2186 case TSK_ExplicitInstantiationDefinition:
2187 return true;
2188 }
2189 llvm_unreachable("All TSK values handled.");
2190}
Douglas Gregorafca3b42009-10-27 20:53:28 +00002191
2192FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const {
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002193 // Handle class scope explicit specialization special case.
2194 if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization)
2195 return getClassScopeSpecializationPattern();
2196
Douglas Gregorafca3b42009-10-27 20:53:28 +00002197 if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) {
2198 while (Primary->getInstantiatedFromMemberTemplate()) {
2199 // If we have hit a point where the user provided a specialization of
2200 // this template, we're done looking.
2201 if (Primary->isMemberSpecialization())
2202 break;
2203
2204 Primary = Primary->getInstantiatedFromMemberTemplate();
2205 }
2206
2207 return Primary->getTemplatedDecl();
2208 }
2209
2210 return getInstantiatedFromMemberFunction();
2211}
2212
Douglas Gregor70d83e22009-06-29 17:30:29 +00002213FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const {
Mike Stump11289f42009-09-09 15:08:12 +00002214 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002215 = TemplateOrSpecialization
2216 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregore8925db2009-06-29 22:39:32 +00002217 return Info->Template.getPointer();
Douglas Gregor70d83e22009-06-29 17:30:29 +00002218 }
2219 return 0;
2220}
2221
Francois Pichet00c7e6c2011-08-14 03:52:19 +00002222FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const {
2223 return getASTContext().getClassScopeSpecializationPattern(this);
2224}
2225
Douglas Gregor70d83e22009-06-29 17:30:29 +00002226const TemplateArgumentList *
2227FunctionDecl::getTemplateSpecializationArgs() const {
Mike Stump11289f42009-09-09 15:08:12 +00002228 if (FunctionTemplateSpecializationInfo *Info
Douglas Gregorcf915552009-10-13 16:30:37 +00002229 = TemplateOrSpecialization
2230 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
Douglas Gregor70d83e22009-06-29 17:30:29 +00002231 return Info->TemplateArguments;
2232 }
2233 return 0;
2234}
2235
Argyrios Kyrtzidise9a24432011-09-22 20:07:09 +00002236const ASTTemplateArgumentListInfo *
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002237FunctionDecl::getTemplateSpecializationArgsAsWritten() const {
2238 if (FunctionTemplateSpecializationInfo *Info
2239 = TemplateOrSpecialization
2240 .dyn_cast<FunctionTemplateSpecializationInfo*>()) {
2241 return Info->TemplateArgumentsAsWritten;
2242 }
2243 return 0;
2244}
2245
Mike Stump11289f42009-09-09 15:08:12 +00002246void
Argyrios Kyrtzidisf4bc0d82010-09-08 19:31:22 +00002247FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C,
2248 FunctionTemplateDecl *Template,
Douglas Gregor8f5d4422009-06-29 20:59:39 +00002249 const TemplateArgumentList *TemplateArgs,
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002250 void *InsertPos,
Abramo Bagnara02ccd282010-05-20 15:32:11 +00002251 TemplateSpecializationKind TSK,
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +00002252 const TemplateArgumentListInfo *TemplateArgsAsWritten,
2253 SourceLocation PointOfInstantiation) {
Douglas Gregor3a923c2d2009-09-24 23:14:47 +00002254 assert(TSK != TSK_Undeclared &&
2255 "Must specify the type of function template specialization");
Mike Stump11289f42009-09-09 15:08:12 +00002256 FunctionTemplateSpecializationInfo *Info
Douglas Gregor70d83e22009-06-29 17:30:29 +00002257 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002258 if (!Info)
Argyrios Kyrtzidise262a952010-09-09 11:28:23 +00002259 Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK,
2260 TemplateArgs,
2261 TemplateArgsAsWritten,
2262 PointOfInstantiation);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002263 TemplateOrSpecialization = Info;
Douglas Gregorce9978f2012-03-28 14:34:23 +00002264 Template->addSpecialization(Info, InsertPos);
Douglas Gregor4adbc6d2009-06-26 00:10:03 +00002265}
2266
John McCallb9c78482010-04-08 09:05:18 +00002267void
2268FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context,
2269 const UnresolvedSetImpl &Templates,
2270 const TemplateArgumentListInfo &TemplateArgs) {
2271 assert(TemplateOrSpecialization.isNull());
2272 size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo);
2273 Size += Templates.size() * sizeof(FunctionTemplateDecl*);
John McCall900d9802010-04-13 22:18:28 +00002274 Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc);
John McCallb9c78482010-04-08 09:05:18 +00002275 void *Buffer = Context.Allocate(Size);
2276 DependentFunctionTemplateSpecializationInfo *Info =
2277 new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates,
2278 TemplateArgs);
2279 TemplateOrSpecialization = Info;
2280}
2281
2282DependentFunctionTemplateSpecializationInfo::
2283DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts,
2284 const TemplateArgumentListInfo &TArgs)
2285 : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) {
2286
2287 d.NumTemplates = Ts.size();
2288 d.NumArgs = TArgs.size();
2289
2290 FunctionTemplateDecl **TsArray =
2291 const_cast<FunctionTemplateDecl**>(getTemplates());
2292 for (unsigned I = 0, E = Ts.size(); I != E; ++I)
2293 TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl());
2294
2295 TemplateArgumentLoc *ArgsArray =
2296 const_cast<TemplateArgumentLoc*>(getTemplateArgs());
2297 for (unsigned I = 0, E = TArgs.size(); I != E; ++I)
2298 new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]);
2299}
2300
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002301TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const {
Mike Stump11289f42009-09-09 15:08:12 +00002302 // For a function template specialization, query the specialization
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002303 // information object.
Douglas Gregord801b062009-10-07 23:56:10 +00002304 FunctionTemplateSpecializationInfo *FTSInfo
Douglas Gregore8925db2009-06-29 22:39:32 +00002305 = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>();
Douglas Gregord801b062009-10-07 23:56:10 +00002306 if (FTSInfo)
2307 return FTSInfo->getTemplateSpecializationKind();
Mike Stump11289f42009-09-09 15:08:12 +00002308
Douglas Gregord801b062009-10-07 23:56:10 +00002309 MemberSpecializationInfo *MSInfo
2310 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>();
2311 if (MSInfo)
2312 return MSInfo->getTemplateSpecializationKind();
2313
2314 return TSK_Undeclared;
Douglas Gregor34ec2ef2009-09-04 22:48:11 +00002315}
2316
Mike Stump11289f42009-09-09 15:08:12 +00002317void
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002318FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2319 SourceLocation PointOfInstantiation) {
2320 if (FunctionTemplateSpecializationInfo *FTSInfo
2321 = TemplateOrSpecialization.dyn_cast<
2322 FunctionTemplateSpecializationInfo*>()) {
2323 FTSInfo->setTemplateSpecializationKind(TSK);
2324 if (TSK != TSK_ExplicitSpecialization &&
2325 PointOfInstantiation.isValid() &&
2326 FTSInfo->getPointOfInstantiation().isInvalid())
2327 FTSInfo->setPointOfInstantiation(PointOfInstantiation);
2328 } else if (MemberSpecializationInfo *MSInfo
2329 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) {
2330 MSInfo->setTemplateSpecializationKind(TSK);
2331 if (TSK != TSK_ExplicitSpecialization &&
2332 PointOfInstantiation.isValid() &&
2333 MSInfo->getPointOfInstantiation().isInvalid())
2334 MSInfo->setPointOfInstantiation(PointOfInstantiation);
2335 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002336 llvm_unreachable("Function cannot have a template specialization kind");
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002337}
2338
2339SourceLocation FunctionDecl::getPointOfInstantiation() const {
Douglas Gregord801b062009-10-07 23:56:10 +00002340 if (FunctionTemplateSpecializationInfo *FTSInfo
2341 = TemplateOrSpecialization.dyn_cast<
2342 FunctionTemplateSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002343 return FTSInfo->getPointOfInstantiation();
Douglas Gregord801b062009-10-07 23:56:10 +00002344 else if (MemberSpecializationInfo *MSInfo
2345 = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>())
Douglas Gregor3d7e69f2009-10-15 17:21:20 +00002346 return MSInfo->getPointOfInstantiation();
2347
2348 return SourceLocation();
Douglas Gregore8925db2009-06-29 22:39:32 +00002349}
2350
Douglas Gregor6411b922009-09-11 20:15:17 +00002351bool FunctionDecl::isOutOfLine() const {
Douglas Gregorb11aad82011-02-19 18:51:44 +00002352 if (Decl::isOutOfLine())
Douglas Gregor6411b922009-09-11 20:15:17 +00002353 return true;
2354
2355 // If this function was instantiated from a member function of a
2356 // class template, check whether that member function was defined out-of-line.
2357 if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) {
2358 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002359 if (FD->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002360 return Definition->isOutOfLine();
2361 }
2362
2363 // If this function was instantiated from a function template,
2364 // check whether that function template was defined out-of-line.
2365 if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) {
2366 const FunctionDecl *Definition;
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00002367 if (FunTmpl->getTemplatedDecl()->hasBody(Definition))
Douglas Gregor6411b922009-09-11 20:15:17 +00002368 return Definition->isOutOfLine();
2369 }
2370
2371 return false;
2372}
2373
Abramo Bagnaraea947882011-03-08 16:41:52 +00002374SourceRange FunctionDecl::getSourceRange() const {
2375 return SourceRange(getOuterLocStart(), EndRangeLoc);
2376}
2377
Anna Zaks28db7ce2012-01-18 02:45:01 +00002378unsigned FunctionDecl::getMemoryFunctionKind() const {
Anna Zaks201d4892012-01-13 21:52:01 +00002379 IdentifierInfo *FnInfo = getIdentifier();
2380
2381 if (!FnInfo)
Anna Zaks22122702012-01-17 00:37:07 +00002382 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002383
2384 // Builtin handling.
2385 switch (getBuiltinID()) {
2386 case Builtin::BI__builtin_memset:
2387 case Builtin::BI__builtin___memset_chk:
2388 case Builtin::BImemset:
Anna Zaks22122702012-01-17 00:37:07 +00002389 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002390
2391 case Builtin::BI__builtin_memcpy:
2392 case Builtin::BI__builtin___memcpy_chk:
2393 case Builtin::BImemcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002394 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002395
2396 case Builtin::BI__builtin_memmove:
2397 case Builtin::BI__builtin___memmove_chk:
2398 case Builtin::BImemmove:
Anna Zaks22122702012-01-17 00:37:07 +00002399 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002400
2401 case Builtin::BIstrlcpy:
Anna Zaks22122702012-01-17 00:37:07 +00002402 return Builtin::BIstrlcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002403 case Builtin::BIstrlcat:
Anna Zaks22122702012-01-17 00:37:07 +00002404 return Builtin::BIstrlcat;
Anna Zaks201d4892012-01-13 21:52:01 +00002405
2406 case Builtin::BI__builtin_memcmp:
Anna Zaks22122702012-01-17 00:37:07 +00002407 case Builtin::BImemcmp:
2408 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002409
2410 case Builtin::BI__builtin_strncpy:
2411 case Builtin::BI__builtin___strncpy_chk:
2412 case Builtin::BIstrncpy:
Anna Zaks22122702012-01-17 00:37:07 +00002413 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002414
2415 case Builtin::BI__builtin_strncmp:
Anna Zaks22122702012-01-17 00:37:07 +00002416 case Builtin::BIstrncmp:
2417 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002418
2419 case Builtin::BI__builtin_strncasecmp:
Anna Zaks22122702012-01-17 00:37:07 +00002420 case Builtin::BIstrncasecmp:
2421 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002422
2423 case Builtin::BI__builtin_strncat:
Anna Zaks314cd092012-02-01 19:08:57 +00002424 case Builtin::BI__builtin___strncat_chk:
Anna Zaks201d4892012-01-13 21:52:01 +00002425 case Builtin::BIstrncat:
Anna Zaks22122702012-01-17 00:37:07 +00002426 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002427
2428 case Builtin::BI__builtin_strndup:
2429 case Builtin::BIstrndup:
Anna Zaks22122702012-01-17 00:37:07 +00002430 return Builtin::BIstrndup;
Anna Zaks201d4892012-01-13 21:52:01 +00002431
Anna Zaks314cd092012-02-01 19:08:57 +00002432 case Builtin::BI__builtin_strlen:
2433 case Builtin::BIstrlen:
2434 return Builtin::BIstrlen;
2435
Anna Zaks201d4892012-01-13 21:52:01 +00002436 default:
Rafael Espindola5bda63f2013-02-14 01:47:04 +00002437 if (isExternC()) {
Anna Zaks201d4892012-01-13 21:52:01 +00002438 if (FnInfo->isStr("memset"))
Anna Zaks22122702012-01-17 00:37:07 +00002439 return Builtin::BImemset;
Anna Zaks201d4892012-01-13 21:52:01 +00002440 else if (FnInfo->isStr("memcpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002441 return Builtin::BImemcpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002442 else if (FnInfo->isStr("memmove"))
Anna Zaks22122702012-01-17 00:37:07 +00002443 return Builtin::BImemmove;
Anna Zaks201d4892012-01-13 21:52:01 +00002444 else if (FnInfo->isStr("memcmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002445 return Builtin::BImemcmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002446 else if (FnInfo->isStr("strncpy"))
Anna Zaks22122702012-01-17 00:37:07 +00002447 return Builtin::BIstrncpy;
Anna Zaks201d4892012-01-13 21:52:01 +00002448 else if (FnInfo->isStr("strncmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002449 return Builtin::BIstrncmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002450 else if (FnInfo->isStr("strncasecmp"))
Anna Zaks22122702012-01-17 00:37:07 +00002451 return Builtin::BIstrncasecmp;
Anna Zaks201d4892012-01-13 21:52:01 +00002452 else if (FnInfo->isStr("strncat"))
Anna Zaks22122702012-01-17 00:37:07 +00002453 return Builtin::BIstrncat;
Anna Zaks201d4892012-01-13 21:52:01 +00002454 else if (FnInfo->isStr("strndup"))
Anna Zaks22122702012-01-17 00:37:07 +00002455 return Builtin::BIstrndup;
Anna Zaks314cd092012-02-01 19:08:57 +00002456 else if (FnInfo->isStr("strlen"))
2457 return Builtin::BIstrlen;
Anna Zaks201d4892012-01-13 21:52:01 +00002458 }
2459 break;
2460 }
Anna Zaks22122702012-01-17 00:37:07 +00002461 return 0;
Anna Zaks201d4892012-01-13 21:52:01 +00002462}
2463
Chris Lattner59a25942008-03-31 00:36:02 +00002464//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002465// FieldDecl Implementation
2466//===----------------------------------------------------------------------===//
2467
Jay Foad39c79802011-01-12 09:06:06 +00002468FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002469 SourceLocation StartLoc, SourceLocation IdLoc,
2470 IdentifierInfo *Id, QualType T,
Richard Smith938f40b2011-06-11 17:19:42 +00002471 TypeSourceInfo *TInfo, Expr *BW, bool Mutable,
Richard Smith2b013182012-06-10 03:12:00 +00002472 InClassInitStyle InitStyle) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002473 return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo,
Richard Smith2b013182012-06-10 03:12:00 +00002474 BW, Mutable, InitStyle);
Sebastian Redl833ef452010-01-26 22:01:41 +00002475}
2476
Douglas Gregor72172e92012-01-05 21:55:30 +00002477FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2478 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl));
2479 return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(),
Richard Smith2b013182012-06-10 03:12:00 +00002480 0, QualType(), 0, 0, false, ICIS_NoInit);
Douglas Gregor72172e92012-01-05 21:55:30 +00002481}
2482
Sebastian Redl833ef452010-01-26 22:01:41 +00002483bool FieldDecl::isAnonymousStructOrUnion() const {
2484 if (!isImplicit() || getDeclName())
2485 return false;
2486
2487 if (const RecordType *Record = getType()->getAs<RecordType>())
2488 return Record->getDecl()->isAnonymousStructOrUnion();
2489
2490 return false;
2491}
2492
Richard Smithcaf33902011-10-10 18:28:20 +00002493unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
2494 assert(isBitField() && "not a bitfield");
2495 Expr *BitWidth = InitializerOrBitWidth.getPointer();
2496 return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue();
2497}
2498
John McCall4e819612011-01-20 07:57:12 +00002499unsigned FieldDecl::getFieldIndex() const {
2500 if (CachedFieldIndex) return CachedFieldIndex - 1;
2501
Richard Smithd62306a2011-11-10 06:34:14 +00002502 unsigned Index = 0;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002503 const RecordDecl *RD = getParent();
2504 const FieldDecl *LastFD = 0;
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002505 bool IsMsStruct = RD->isMsStruct(getASTContext());
Richard Smithd62306a2011-11-10 06:34:14 +00002506
2507 for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
2508 I != E; ++I, ++Index) {
David Blaikie2d7c57e2012-04-30 02:36:29 +00002509 I->CachedFieldIndex = Index + 1;
John McCall4e819612011-01-20 07:57:12 +00002510
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002511 if (IsMsStruct) {
2512 // Zero-length bitfields following non-bitfield members are ignored.
David Blaikie40ed2972012-06-06 20:45:41 +00002513 if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) {
Richard Smithd62306a2011-11-10 06:34:14 +00002514 --Index;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002515 continue;
2516 }
David Blaikie40ed2972012-06-06 20:45:41 +00002517 LastFD = *I;
Fariborz Jahanian8409bce42011-04-28 22:49:46 +00002518 }
John McCall4e819612011-01-20 07:57:12 +00002519 }
2520
Richard Smithd62306a2011-11-10 06:34:14 +00002521 assert(CachedFieldIndex && "failed to find field in parent");
2522 return CachedFieldIndex - 1;
John McCall4e819612011-01-20 07:57:12 +00002523}
2524
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002525SourceRange FieldDecl::getSourceRange() const {
Abramo Bagnaraff371ac2011-08-05 08:02:55 +00002526 if (const Expr *E = InitializerOrBitWidth.getPointer())
2527 return SourceRange(getInnerLocStart(), E->getLocEnd());
Abramo Bagnaraea947882011-03-08 16:41:52 +00002528 return DeclaratorDecl::getSourceRange();
Abramo Bagnara20c9e242011-03-08 11:07:11 +00002529}
2530
Abramo Bagnarab1cdde72012-07-02 20:35:48 +00002531void FieldDecl::setBitWidth(Expr *Width) {
2532 assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() &&
2533 "bit width or initializer already set");
2534 InitializerOrBitWidth.setPointer(Width);
2535}
2536
Richard Smith938f40b2011-06-11 17:19:42 +00002537void FieldDecl::setInClassInitializer(Expr *Init) {
Richard Smith2b013182012-06-10 03:12:00 +00002538 assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() &&
Richard Smith938f40b2011-06-11 17:19:42 +00002539 "bit width or initializer already set");
2540 InitializerOrBitWidth.setPointer(Init);
Richard Smith938f40b2011-06-11 17:19:42 +00002541}
2542
Sebastian Redl833ef452010-01-26 22:01:41 +00002543//===----------------------------------------------------------------------===//
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002544// TagDecl Implementation
Ted Kremenek21475702008-09-05 17:16:31 +00002545//===----------------------------------------------------------------------===//
2546
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002547SourceLocation TagDecl::getOuterLocStart() const {
2548 return getTemplateOrInnerLocStart(this);
2549}
2550
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002551SourceRange TagDecl::getSourceRange() const {
2552 SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation();
Douglas Gregorec9c6ae2010-07-06 18:42:40 +00002553 return SourceRange(getOuterLocStart(), E);
Argyrios Kyrtzidis575fa052009-07-14 03:17:17 +00002554}
2555
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002556TagDecl* TagDecl::getCanonicalDecl() {
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002557 return getFirstDeclaration();
Argyrios Kyrtzidis5614aef2009-07-18 00:34:07 +00002558}
2559
Richard Smithdda56e42011-04-15 14:24:37 +00002560void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) {
2561 TypedefNameDeclOrQualifier = TDD;
Douglas Gregora72a4e32010-05-19 18:39:18 +00002562 if (TypeForDecl)
Rafael Espindola19de5612013-01-12 06:42:30 +00002563 const_cast<Type*>(TypeForDecl)->ClearLinkageCache();
2564 ClearLinkageCache();
Douglas Gregora72a4e32010-05-19 18:39:18 +00002565}
2566
Douglas Gregordee1be82009-01-17 00:42:38 +00002567void TagDecl::startDefinition() {
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002568 IsBeingDefined = true;
John McCall67da35c2010-02-04 22:26:26 +00002569
David Blaikie095deba2012-11-14 01:52:05 +00002570 if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) {
John McCall67da35c2010-02-04 22:26:26 +00002571 struct CXXRecordDecl::DefinitionData *Data =
2572 new (getASTContext()) struct CXXRecordDecl::DefinitionData(D);
John McCall93cc7322010-03-26 21:56:38 +00002573 for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I)
2574 cast<CXXRecordDecl>(*I)->DefinitionData = Data;
John McCall67da35c2010-02-04 22:26:26 +00002575 }
Douglas Gregordee1be82009-01-17 00:42:38 +00002576}
2577
2578void TagDecl::completeDefinition() {
John McCallae580fe2010-02-05 01:33:36 +00002579 assert((!isa<CXXRecordDecl>(this) ||
2580 cast<CXXRecordDecl>(this)->hasDefinition()) &&
2581 "definition completed but not started");
2582
John McCallf937c022011-10-07 06:10:15 +00002583 IsCompleteDefinition = true;
Sebastian Redl9d8854e2010-08-02 18:27:05 +00002584 IsBeingDefined = false;
Argyrios Kyrtzidisd170d842010-10-24 17:26:50 +00002585
2586 if (ASTMutationListener *L = getASTMutationListener())
2587 L->CompletedTagDefinition(this);
Douglas Gregordee1be82009-01-17 00:42:38 +00002588}
2589
John McCallf937c022011-10-07 06:10:15 +00002590TagDecl *TagDecl::getDefinition() const {
2591 if (isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002592 return const_cast<TagDecl *>(this);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002593
2594 // If it's possible for us to have an out-of-date definition, check now.
2595 if (MayHaveOutOfDateDef) {
2596 if (IdentifierInfo *II = getIdentifier()) {
2597 if (II->isOutOfDate()) {
2598 updateOutOfDate(*II);
2599 }
2600 }
2601 }
2602
Andrew Trickba266ee2010-10-19 21:54:32 +00002603 if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this))
2604 return CXXRD->getDefinition();
Mike Stump11289f42009-09-09 15:08:12 +00002605
2606 for (redecl_iterator R = redecls_begin(), REnd = redecls_end();
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002607 R != REnd; ++R)
John McCallf937c022011-10-07 06:10:15 +00002608 if (R->isCompleteDefinition())
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002609 return *R;
Mike Stump11289f42009-09-09 15:08:12 +00002610
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +00002611 return 0;
Ted Kremenek21475702008-09-05 17:16:31 +00002612}
2613
Douglas Gregor14454802011-02-25 02:25:35 +00002614void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) {
2615 if (QualifierLoc) {
John McCall3e11ebe2010-03-15 10:12:16 +00002616 // Make sure the extended qualifier info is allocated.
2617 if (!hasExtInfo())
Richard Smithdda56e42011-04-15 14:24:37 +00002618 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
John McCall3e11ebe2010-03-15 10:12:16 +00002619 // Set qualifier info.
Douglas Gregor14454802011-02-25 02:25:35 +00002620 getExtInfo()->QualifierLoc = QualifierLoc;
Chad Rosier6fdf38b2011-08-17 23:08:45 +00002621 } else {
John McCall3e11ebe2010-03-15 10:12:16 +00002622 // Here Qualifier == 0, i.e., we are removing the qualifier (if any).
John McCall3e11ebe2010-03-15 10:12:16 +00002623 if (hasExtInfo()) {
Abramo Bagnara60804e12011-03-18 15:16:37 +00002624 if (getExtInfo()->NumTemplParamLists == 0) {
2625 getASTContext().Deallocate(getExtInfo());
Richard Smithdda56e42011-04-15 14:24:37 +00002626 TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002627 }
2628 else
2629 getExtInfo()->QualifierLoc = QualifierLoc;
John McCall3e11ebe2010-03-15 10:12:16 +00002630 }
2631 }
2632}
2633
Abramo Bagnara60804e12011-03-18 15:16:37 +00002634void TagDecl::setTemplateParameterListsInfo(ASTContext &Context,
2635 unsigned NumTPLists,
2636 TemplateParameterList **TPLists) {
2637 assert(NumTPLists > 0);
2638 // Make sure the extended decl info is allocated.
2639 if (!hasExtInfo())
2640 // Allocate external info struct.
Richard Smithdda56e42011-04-15 14:24:37 +00002641 TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo;
Abramo Bagnara60804e12011-03-18 15:16:37 +00002642 // Set the template parameter lists info.
2643 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
2644}
2645
Ted Kremenek21475702008-09-05 17:16:31 +00002646//===----------------------------------------------------------------------===//
Sebastian Redl833ef452010-01-26 22:01:41 +00002647// EnumDecl Implementation
2648//===----------------------------------------------------------------------===//
2649
David Blaikie68e081d2011-12-20 02:48:34 +00002650void EnumDecl::anchor() { }
2651
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002652EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC,
2653 SourceLocation StartLoc, SourceLocation IdLoc,
2654 IdentifierInfo *Id,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002655 EnumDecl *PrevDecl, bool IsScoped,
2656 bool IsScopedUsingClassTag, bool IsFixed) {
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002657 EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00002658 IsScoped, IsScopedUsingClassTag, IsFixed);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002659 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
Sebastian Redl833ef452010-01-26 22:01:41 +00002660 C.getTypeDeclType(Enum, PrevDecl);
2661 return Enum;
2662}
2663
Douglas Gregor72172e92012-01-05 21:55:30 +00002664EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2665 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002666 EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(),
2667 0, 0, false, false, false);
2668 Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules;
2669 return Enum;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002670}
2671
Douglas Gregord5058122010-02-11 01:19:42 +00002672void EnumDecl::completeDefinition(QualType NewType,
John McCall9aa35be2010-05-06 08:49:23 +00002673 QualType NewPromotionType,
2674 unsigned NumPositiveBits,
2675 unsigned NumNegativeBits) {
John McCallf937c022011-10-07 06:10:15 +00002676 assert(!isCompleteDefinition() && "Cannot redefine enums!");
Douglas Gregor0bf31402010-10-08 23:50:27 +00002677 if (!IntegerType)
2678 IntegerType = NewType.getTypePtr();
Sebastian Redl833ef452010-01-26 22:01:41 +00002679 PromotionType = NewPromotionType;
John McCall9aa35be2010-05-06 08:49:23 +00002680 setNumPositiveBits(NumPositiveBits);
2681 setNumNegativeBits(NumNegativeBits);
Sebastian Redl833ef452010-01-26 22:01:41 +00002682 TagDecl::completeDefinition();
2683}
2684
Richard Smith7d137e32012-03-23 03:33:32 +00002685TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const {
2686 if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo())
2687 return MSI->getTemplateSpecializationKind();
2688
2689 return TSK_Undeclared;
2690}
2691
2692void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK,
2693 SourceLocation PointOfInstantiation) {
2694 MemberSpecializationInfo *MSI = getMemberSpecializationInfo();
2695 assert(MSI && "Not an instantiated member enumeration?");
2696 MSI->setTemplateSpecializationKind(TSK);
2697 if (TSK != TSK_ExplicitSpecialization &&
2698 PointOfInstantiation.isValid() &&
2699 MSI->getPointOfInstantiation().isInvalid())
2700 MSI->setPointOfInstantiation(PointOfInstantiation);
2701}
2702
Richard Smith4b38ded2012-03-14 23:13:10 +00002703EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const {
2704 if (SpecializationInfo)
2705 return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom());
2706
2707 return 0;
2708}
2709
2710void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED,
2711 TemplateSpecializationKind TSK) {
2712 assert(!SpecializationInfo && "Member enum is already a specialization");
2713 SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK);
2714}
2715
Sebastian Redl833ef452010-01-26 22:01:41 +00002716//===----------------------------------------------------------------------===//
Chris Lattner59a25942008-03-31 00:36:02 +00002717// RecordDecl Implementation
2718//===----------------------------------------------------------------------===//
Chris Lattner41943152007-01-25 04:52:46 +00002719
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002720RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2721 SourceLocation StartLoc, SourceLocation IdLoc,
2722 IdentifierInfo *Id, RecordDecl *PrevDecl)
2723 : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) {
Ted Kremenek52baf502008-09-02 21:12:32 +00002724 HasFlexibleArrayMember = false;
Douglas Gregor9ac7a072009-01-07 00:43:41 +00002725 AnonymousStructOrUnion = false;
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00002726 HasObjectMember = false;
Fariborz Jahanian78652202013-01-25 23:57:05 +00002727 HasVolatileMember = false;
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002728 LoadedFieldsFromExternalStorage = false;
Ted Kremenek52baf502008-09-02 21:12:32 +00002729 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!");
Ted Kremenek52baf502008-09-02 21:12:32 +00002730}
2731
Jay Foad39c79802011-01-12 09:06:06 +00002732RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC,
Abramo Bagnara29c2d462011-03-09 14:09:51 +00002733 SourceLocation StartLoc, SourceLocation IdLoc,
2734 IdentifierInfo *Id, RecordDecl* PrevDecl) {
2735 RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id,
2736 PrevDecl);
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002737 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
2738
Ted Kremenek21475702008-09-05 17:16:31 +00002739 C.getTypeDeclType(R, PrevDecl);
2740 return R;
Ted Kremenek52baf502008-09-02 21:12:32 +00002741}
2742
Douglas Gregor72172e92012-01-05 21:55:30 +00002743RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) {
2744 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl));
Douglas Gregor7dab26b2013-02-09 01:35:03 +00002745 RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(),
2746 SourceLocation(), 0, 0);
2747 R->MayHaveOutOfDateDef = C.getLangOpts().Modules;
2748 return R;
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00002749}
2750
Douglas Gregordfcad112009-03-25 15:59:44 +00002751bool RecordDecl::isInjectedClassName() const {
Mike Stump11289f42009-09-09 15:08:12 +00002752 return isImplicit() && getDeclName() && getDeclContext()->isRecord() &&
Douglas Gregordfcad112009-03-25 15:59:44 +00002753 cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName();
2754}
2755
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002756RecordDecl::field_iterator RecordDecl::field_begin() const {
2757 if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage)
2758 LoadFieldsFromExternalStorage();
2759
2760 return field_iterator(decl_iterator(FirstDecl));
2761}
2762
Douglas Gregorb11aad82011-02-19 18:51:44 +00002763/// completeDefinition - Notes that the definition of this type is now
2764/// complete.
2765void RecordDecl::completeDefinition() {
John McCallf937c022011-10-07 06:10:15 +00002766 assert(!isCompleteDefinition() && "Cannot redefine record!");
Douglas Gregorb11aad82011-02-19 18:51:44 +00002767 TagDecl::completeDefinition();
2768}
2769
Eli Friedman9ee2d0472012-10-12 23:29:20 +00002770/// isMsStruct - Get whether or not this record uses ms_struct layout.
2771/// This which can be turned on with an attribute, pragma, or the
2772/// -mms-bitfields command-line option.
2773bool RecordDecl::isMsStruct(const ASTContext &C) const {
2774 return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1;
2775}
2776
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002777static bool isFieldOrIndirectField(Decl::Kind K) {
2778 return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K);
2779}
2780
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002781void RecordDecl::LoadFieldsFromExternalStorage() const {
2782 ExternalASTSource *Source = getASTContext().getExternalSource();
2783 assert(hasExternalLexicalStorage() && Source && "No external storage?");
2784
2785 // Notify that we have a RecordDecl doing some initialization.
2786 ExternalASTSource::Deserializing TheFields(Source);
2787
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002788 SmallVector<Decl*, 64> Decls;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002789 LoadedFieldsFromExternalStorage = true;
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002790 switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField,
2791 Decls)) {
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002792 case ELR_Success:
2793 break;
2794
2795 case ELR_AlreadyLoaded:
2796 case ELR_Failure:
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002797 return;
Douglas Gregor3d0adb32011-07-15 21:46:17 +00002798 }
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002799
2800#ifndef NDEBUG
2801 // Check that all decls we got were FieldDecls.
2802 for (unsigned i=0, e=Decls.size(); i != e; ++i)
Argyrios Kyrtzidisf89a9272012-09-10 22:04:22 +00002803 assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i]));
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002804#endif
2805
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002806 if (Decls.empty())
2807 return;
2808
Argyrios Kyrtzidis094da732011-10-07 21:55:43 +00002809 llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls,
2810 /*FieldsAlreadyLoaded=*/false);
Argyrios Kyrtzidis0e88a562010-10-14 20:14:34 +00002811}
2812
Steve Naroff415d3d52008-10-08 17:01:13 +00002813//===----------------------------------------------------------------------===//
2814// BlockDecl Implementation
2815//===----------------------------------------------------------------------===//
2816
Dmitri Gribenkof8579502013-01-12 19:30:44 +00002817void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) {
Steve Naroffc4b30e52009-03-13 16:56:44 +00002818 assert(ParamInfo == 0 && "Already has param info!");
Mike Stump11289f42009-09-09 15:08:12 +00002819
Steve Naroffc4b30e52009-03-13 16:56:44 +00002820 // Zero params -> null pointer.
David Blaikie9c70e042011-09-21 18:16:56 +00002821 if (!NewParamInfo.empty()) {
2822 NumParams = NewParamInfo.size();
2823 ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()];
2824 std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002825 }
2826}
2827
John McCall351762c2011-02-07 10:33:21 +00002828void BlockDecl::setCaptures(ASTContext &Context,
2829 const Capture *begin,
2830 const Capture *end,
2831 bool capturesCXXThis) {
John McCallc63de662011-02-02 13:00:07 +00002832 CapturesCXXThis = capturesCXXThis;
2833
2834 if (begin == end) {
John McCall351762c2011-02-07 10:33:21 +00002835 NumCaptures = 0;
2836 Captures = 0;
John McCallc63de662011-02-02 13:00:07 +00002837 return;
2838 }
2839
John McCall351762c2011-02-07 10:33:21 +00002840 NumCaptures = end - begin;
2841
2842 // Avoid new Capture[] because we don't want to provide a default
2843 // constructor.
2844 size_t allocationSize = NumCaptures * sizeof(Capture);
2845 void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*));
2846 memcpy(buffer, begin, allocationSize);
2847 Captures = static_cast<Capture*>(buffer);
Steve Naroffc4b30e52009-03-13 16:56:44 +00002848}
Sebastian Redl833ef452010-01-26 22:01:41 +00002849
John McCallce45f882011-06-15 22:51:16 +00002850bool BlockDecl::capturesVariable(const VarDecl *variable) const {
2851 for (capture_const_iterator
2852 i = capture_begin(), e = capture_end(); i != e; ++i)
2853 // Only auto vars can be captured, so no redeclaration worries.
2854 if (i->getVariable() == variable)
2855 return true;
2856
2857 return false;
2858}
2859
Douglas Gregor70226da2010-12-21 16:27:07 +00002860SourceRange BlockDecl::getSourceRange() const {
2861 return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation());
2862}
Sebastian Redl833ef452010-01-26 22:01:41 +00002863
2864//===----------------------------------------------------------------------===//
2865// Other Decl Allocation/Deallocation Method Implementations
2866//===----------------------------------------------------------------------===//
2867
David Blaikie68e081d2011-12-20 02:48:34 +00002868void TranslationUnitDecl::anchor() { }
2869
Sebastian Redl833ef452010-01-26 22:01:41 +00002870TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) {
2871 return new (C) TranslationUnitDecl(C);
2872}
2873
David Blaikie68e081d2011-12-20 02:48:34 +00002874void LabelDecl::anchor() { }
2875
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002876LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara1c3af962011-03-05 18:21:20 +00002877 SourceLocation IdentL, IdentifierInfo *II) {
2878 return new (C) LabelDecl(DC, IdentL, II, 0, IdentL);
2879}
2880
2881LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC,
2882 SourceLocation IdentL, IdentifierInfo *II,
2883 SourceLocation GnuLabelL) {
2884 assert(GnuLabelL != IdentL && "Use this only for GNU local labels");
2885 return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL);
Chris Lattnerc8e630e2011-02-17 07:39:24 +00002886}
2887
Douglas Gregor72172e92012-01-05 21:55:30 +00002888LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2889 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl));
2890 return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation());
Douglas Gregor417e87c2010-10-27 19:49:05 +00002891}
2892
David Blaikie68e081d2011-12-20 02:48:34 +00002893void ValueDecl::anchor() { }
2894
Benjamin Kramerea70eb32012-12-01 15:09:41 +00002895bool ValueDecl::isWeak() const {
2896 for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I)
2897 if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I))
2898 return true;
2899
2900 return isWeakImported();
2901}
2902
David Blaikie68e081d2011-12-20 02:48:34 +00002903void ImplicitParamDecl::anchor() { }
2904
Sebastian Redl833ef452010-01-26 22:01:41 +00002905ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002906 SourceLocation IdLoc,
2907 IdentifierInfo *Id,
2908 QualType Type) {
2909 return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type);
Sebastian Redl833ef452010-01-26 22:01:41 +00002910}
2911
Douglas Gregor72172e92012-01-05 21:55:30 +00002912ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C,
2913 unsigned ID) {
2914 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl));
2915 return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType());
2916}
2917
Sebastian Redl833ef452010-01-26 22:01:41 +00002918FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002919 SourceLocation StartLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00002920 const DeclarationNameInfo &NameInfo,
2921 QualType T, TypeSourceInfo *TInfo,
Abramo Bagnaradff19302011-03-08 08:55:46 +00002922 StorageClass SC, StorageClass SCAsWritten,
Douglas Gregorff76cb92010-12-09 16:59:22 +00002923 bool isInlineSpecified,
Richard Smitha77a0a62011-08-15 21:04:07 +00002924 bool hasWrittenPrototype,
2925 bool isConstexprSpecified) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00002926 FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo,
2927 T, TInfo, SC, SCAsWritten,
Richard Smitha77a0a62011-08-15 21:04:07 +00002928 isInlineSpecified,
2929 isConstexprSpecified);
Sebastian Redl833ef452010-01-26 22:01:41 +00002930 New->HasWrittenPrototype = hasWrittenPrototype;
2931 return New;
2932}
2933
Douglas Gregor72172e92012-01-05 21:55:30 +00002934FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2935 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl));
2936 return new (Mem) FunctionDecl(Function, 0, SourceLocation(),
2937 DeclarationNameInfo(), QualType(), 0,
2938 SC_None, SC_None, false, false);
2939}
2940
Sebastian Redl833ef452010-01-26 22:01:41 +00002941BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) {
2942 return new (C) BlockDecl(DC, L);
2943}
2944
Douglas Gregor72172e92012-01-05 21:55:30 +00002945BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2946 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl));
2947 return new (Mem) BlockDecl(0, SourceLocation());
2948}
2949
Sebastian Redl833ef452010-01-26 22:01:41 +00002950EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD,
2951 SourceLocation L,
2952 IdentifierInfo *Id, QualType T,
2953 Expr *E, const llvm::APSInt &V) {
2954 return new (C) EnumConstantDecl(CD, L, Id, T, E, V);
2955}
2956
Douglas Gregor72172e92012-01-05 21:55:30 +00002957EnumConstantDecl *
2958EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2959 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl));
2960 return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0,
2961 llvm::APSInt());
2962}
2963
David Blaikie68e081d2011-12-20 02:48:34 +00002964void IndirectFieldDecl::anchor() { }
2965
Benjamin Kramer39593702010-11-21 14:11:41 +00002966IndirectFieldDecl *
2967IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L,
2968 IdentifierInfo *Id, QualType T, NamedDecl **CH,
2969 unsigned CHS) {
Francois Pichet783dd6e2010-11-21 06:08:52 +00002970 return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS);
2971}
2972
Douglas Gregor72172e92012-01-05 21:55:30 +00002973IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C,
2974 unsigned ID) {
2975 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl));
2976 return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(),
2977 QualType(), 0, 0);
2978}
2979
Douglas Gregorbe996932010-09-01 20:41:53 +00002980SourceRange EnumConstantDecl::getSourceRange() const {
2981 SourceLocation End = getLocation();
2982 if (Init)
2983 End = Init->getLocEnd();
2984 return SourceRange(getLocation(), End);
2985}
2986
David Blaikie68e081d2011-12-20 02:48:34 +00002987void TypeDecl::anchor() { }
2988
Sebastian Redl833ef452010-01-26 22:01:41 +00002989TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnarab3185b02011-03-06 15:48:19 +00002990 SourceLocation StartLoc, SourceLocation IdLoc,
2991 IdentifierInfo *Id, TypeSourceInfo *TInfo) {
2992 return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo);
Sebastian Redl833ef452010-01-26 22:01:41 +00002993}
2994
David Blaikie68e081d2011-12-20 02:48:34 +00002995void TypedefNameDecl::anchor() { }
2996
Douglas Gregor72172e92012-01-05 21:55:30 +00002997TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2998 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl));
2999 return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3000}
3001
Richard Smithdda56e42011-04-15 14:24:37 +00003002TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC,
3003 SourceLocation StartLoc,
3004 SourceLocation IdLoc, IdentifierInfo *Id,
3005 TypeSourceInfo *TInfo) {
3006 return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo);
3007}
3008
Douglas Gregor72172e92012-01-05 21:55:30 +00003009TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
3010 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl));
3011 return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0);
3012}
3013
Abramo Bagnaraea947882011-03-08 16:41:52 +00003014SourceRange TypedefDecl::getSourceRange() const {
3015 SourceLocation RangeEnd = getLocation();
3016 if (TypeSourceInfo *TInfo = getTypeSourceInfo()) {
3017 if (typeIsPostfix(TInfo->getType()))
3018 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3019 }
3020 return SourceRange(getLocStart(), RangeEnd);
3021}
3022
Richard Smithdda56e42011-04-15 14:24:37 +00003023SourceRange TypeAliasDecl::getSourceRange() const {
3024 SourceLocation RangeEnd = getLocStart();
3025 if (TypeSourceInfo *TInfo = getTypeSourceInfo())
3026 RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd();
3027 return SourceRange(getLocStart(), RangeEnd);
3028}
3029
David Blaikie68e081d2011-12-20 02:48:34 +00003030void FileScopeAsmDecl::anchor() { }
3031
Sebastian Redl833ef452010-01-26 22:01:41 +00003032FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
Abramo Bagnara348823a2011-03-03 14:20:18 +00003033 StringLiteral *Str,
3034 SourceLocation AsmLoc,
3035 SourceLocation RParenLoc) {
3036 return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
Sebastian Redl833ef452010-01-26 22:01:41 +00003037}
Douglas Gregorba345522011-12-02 23:23:56 +00003038
Douglas Gregor72172e92012-01-05 21:55:30 +00003039FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
3040 unsigned ID) {
3041 void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl));
3042 return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation());
3043}
3044
Douglas Gregorba345522011-12-02 23:23:56 +00003045//===----------------------------------------------------------------------===//
3046// ImportDecl Implementation
3047//===----------------------------------------------------------------------===//
3048
3049/// \brief Retrieve the number of module identifiers needed to name the given
3050/// module.
3051static unsigned getNumModuleIdentifiers(Module *Mod) {
3052 unsigned Result = 1;
3053 while (Mod->Parent) {
3054 Mod = Mod->Parent;
3055 ++Result;
3056 }
3057 return Result;
3058}
3059
Douglas Gregor22d09742012-01-03 18:04:46 +00003060ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003061 Module *Imported,
3062 ArrayRef<SourceLocation> IdentifierLocs)
Douglas Gregor22d09742012-01-03 18:04:46 +00003063 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003064 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003065{
3066 assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size());
3067 SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1);
3068 memcpy(StoredLocs, IdentifierLocs.data(),
3069 IdentifierLocs.size() * sizeof(SourceLocation));
3070}
3071
Douglas Gregor22d09742012-01-03 18:04:46 +00003072ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003073 Module *Imported, SourceLocation EndLoc)
Douglas Gregor22d09742012-01-03 18:04:46 +00003074 : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false),
Douglas Gregor0f2a3602011-12-03 00:30:27 +00003075 NextLocalImport()
Douglas Gregorba345522011-12-02 23:23:56 +00003076{
3077 *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc;
3078}
3079
3080ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003081 SourceLocation StartLoc, Module *Imported,
Douglas Gregorba345522011-12-02 23:23:56 +00003082 ArrayRef<SourceLocation> IdentifierLocs) {
3083 void *Mem = C.Allocate(sizeof(ImportDecl) +
3084 IdentifierLocs.size() * sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003085 return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs);
Douglas Gregorba345522011-12-02 23:23:56 +00003086}
3087
3088ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC,
Douglas Gregor22d09742012-01-03 18:04:46 +00003089 SourceLocation StartLoc,
Douglas Gregorba345522011-12-02 23:23:56 +00003090 Module *Imported,
3091 SourceLocation EndLoc) {
3092 void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation));
Douglas Gregor22d09742012-01-03 18:04:46 +00003093 ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc);
Douglas Gregorba345522011-12-02 23:23:56 +00003094 Import->setImplicit();
3095 return Import;
3096}
3097
Douglas Gregor72172e92012-01-05 21:55:30 +00003098ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID,
3099 unsigned NumLocations) {
3100 void *Mem = AllocateDeserializedDecl(C, ID,
3101 (sizeof(ImportDecl) +
3102 NumLocations * sizeof(SourceLocation)));
Douglas Gregorba345522011-12-02 23:23:56 +00003103 return new (Mem) ImportDecl(EmptyShell());
3104}
3105
3106ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const {
3107 if (!ImportedAndComplete.getInt())
3108 return ArrayRef<SourceLocation>();
3109
3110 const SourceLocation *StoredLocs
3111 = reinterpret_cast<const SourceLocation *>(this + 1);
3112 return ArrayRef<SourceLocation>(StoredLocs,
3113 getNumModuleIdentifiers(getImportedModule()));
3114}
3115
3116SourceRange ImportDecl::getSourceRange() const {
3117 if (!ImportedAndComplete.getInt())
3118 return SourceRange(getLocation(),
3119 *reinterpret_cast<const SourceLocation *>(this + 1));
3120
3121 return SourceRange(getLocation(), getIdentifierLocs().back());
3122}