Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Decl.cpp - Declaration AST Node Implementation -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Argyrios Kyrtzidis | e184bae | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 10 | // This file implements the Decl subclasses. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/Decl.h" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.h" |
| 17 | #include "clang/AST/Attr.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
| 20 | #include "clang/AST/DeclTemplate.h" |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 23 | #include "clang/AST/PrettyPrinter.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 24 | #include "clang/AST/Stmt.h" |
| 25 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 27 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 28 | #include "clang/Basic/Module.h" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 29 | #include "clang/Basic/Specifiers.h" |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ErrorHandling.h" |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 32 | #include <algorithm> |
| 33 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 36 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 37 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 40 | static 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 McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 51 | } |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 52 | |
| 53 | // If we're on Mac OS X, an 'availability' for Mac OS X attribute |
| 54 | // implies visibility(default). |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 55 | if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 56 | 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 McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 65 | } |
| 66 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 67 | typedef NamedDecl::LinkageInfo LinkageInfo; |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 68 | |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 69 | static LinkageInfo getLVForType(QualType T) { |
| 70 | std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility(); |
| 71 | return LinkageInfo(P.first, P.second, T->isVisibilityExplicit()); |
| 72 | } |
| 73 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 74 | /// \brief Get the most restrictive linkage for the types in the given |
| 75 | /// template parameter list. |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 76 | static LinkageInfo |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 77 | getLVForTemplateParameterList(const TemplateParameterList *Params) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 78 | LinkageInfo LV(ExternalLinkage, DefaultVisibility, false); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 79 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 80 | PEnd = Params->end(); |
| 81 | P != PEnd; ++P) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 82 | 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 Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 87 | LV.merge(getLVForType(T)); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 88 | } |
| 89 | continue; |
| 90 | } |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 92 | if (!NTTP->getType()->isDependentType()) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 93 | LV.merge(getLVForType(NTTP->getType())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 94 | continue; |
| 95 | } |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 96 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 97 | |
| 98 | if (TemplateTemplateParmDecl *TTP |
| 99 | = dyn_cast<TemplateTemplateParmDecl>(*P)) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 100 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 104 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 107 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
| 108 | static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 110 | /// \brief Get the most restrictive linkage for the types and |
| 111 | /// declarations in the given template argument list. |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 112 | static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args, |
| 113 | unsigned NumArgs, |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 114 | bool OnlyTemplate) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 115 | LinkageInfo LV(ExternalLinkage, DefaultVisibility, false); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 116 | |
| 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 Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 123 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 124 | case TemplateArgument::Type: |
Rafael Espindola | 923b0c9 | 2012-04-23 17:51:55 +0000 | [diff] [blame] | 125 | LV.mergeWithMin(getLVForType(Args[I].getAsType())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 126 | break; |
| 127 | |
| 128 | case TemplateArgument::Declaration: |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 129 | 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 Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 135 | break; |
| 136 | |
| 137 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 138 | case TemplateArgument::TemplateExpansion: |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 139 | if (TemplateDecl *Template |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 140 | = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
Rafael Espindola | 923b0c9 | 2012-04-23 17:51:55 +0000 | [diff] [blame] | 141 | LV.mergeWithMin(getLVForDecl(Template, OnlyTemplate)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | case TemplateArgument::Pack: |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 145 | LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(), |
| 146 | Args[I].pack_size(), |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 147 | OnlyTemplate)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | } |
| 151 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 152 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 155 | static LinkageInfo |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 156 | getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 157 | bool OnlyTemplate) { |
| 158 | return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), OnlyTemplate); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Rafael Espindola | 9db614f | 2012-05-25 16:41:35 +0000 | [diff] [blame] | 161 | static bool shouldConsiderTemplateVis(const FunctionDecl *fn, |
Rafael Espindola | cae1c62 | 2012-05-21 20:31:27 +0000 | [diff] [blame] | 162 | const FunctionTemplateSpecializationInfo *spec) { |
| 163 | return !fn->hasAttr<VisibilityAttr>() || spec->isExplicitSpecialization(); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Rafael Espindola | ad359be | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 166 | static bool |
| 167 | shouldConsiderTemplateVis(const ClassTemplateSpecializationDecl *d) { |
Rafael Espindola | 0b0ad0a | 2012-05-21 20:15:56 +0000 | [diff] [blame] | 168 | return !d->hasAttr<VisibilityAttr>() || d->isExplicitSpecialization(); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 171 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
| 172 | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
Rafael Espindola | 0bab9da | 2012-07-13 23:26:43 +0000 | [diff] [blame] | 173 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 174 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 175 | 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 Espindola | 0142f0c | 2012-10-11 16:32:25 +0000 | [diff] [blame] | 196 | FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame^] | 199 | template <typename T> static bool isInExternCContext(T *D) { |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 200 | const T *First = D->getFirstDeclaration(); |
| 201 | return First->getDeclContext()->isExternCContext(); |
| 202 | } |
| 203 | |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 204 | static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, |
| 205 | bool OnlyTemplate) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 206 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 207 | "Not a name having namespace scope"); |
| 208 | ASTContext &Context = D->getASTContext(); |
| 209 | |
| 210 | // C++ [basic.link]p3: |
| 211 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 212 | // is the name of |
| 213 | // - an object, reference, function or function template that is |
| 214 | // explicitly declared static; or, |
| 215 | // (This bullet corresponds to C99 6.2.2p3.) |
| 216 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 217 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 218 | if (Var->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 219 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 220 | |
Richard Smith | 820e9a7 | 2012-10-19 06:37:48 +0000 | [diff] [blame] | 221 | // - a non-volatile object or reference that is explicitly declared const |
| 222 | // or constexpr and neither explicitly declared extern nor previously |
| 223 | // declared to have external linkage; or (there is no equivalent in C99) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 224 | if (Context.getLangOpts().CPlusPlus && |
Richard Smith | 820e9a7 | 2012-10-19 06:37:48 +0000 | [diff] [blame] | 225 | Var->getType().isConstQualified() && |
| 226 | !Var->getType().isVolatileQualified() && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 227 | Var->getStorageClass() != SC_Extern && |
| 228 | Var->getStorageClass() != SC_PrivateExtern) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 229 | bool FoundExtern = false; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 230 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 231 | PrevVar && !FoundExtern; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 232 | PrevVar = PrevVar->getPreviousDecl()) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 233 | if (isExternalLinkage(PrevVar->getLinkage())) |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 234 | FoundExtern = true; |
| 235 | |
| 236 | if (!FoundExtern) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 237 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 238 | } |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 239 | if (Var->getStorageClass() == SC_None) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 240 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
| 241 | for (; PrevVar; PrevVar = PrevVar->getPreviousDecl()) |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 242 | if (PrevVar->getStorageClass() == SC_PrivateExtern) |
| 243 | break; |
Eli Friedman | 8c7a185 | 2012-10-26 23:05:34 +0000 | [diff] [blame] | 244 | if (PrevVar) |
| 245 | return PrevVar->getLinkageAndVisibility(); |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 246 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 247 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 248 | // C++ [temp]p4: |
| 249 | // A non-member function template can have internal linkage; any |
| 250 | // other template name shall have external linkage. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 251 | const FunctionDecl *Function = 0; |
| 252 | if (const FunctionTemplateDecl *FunTmpl |
| 253 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 254 | Function = FunTmpl->getTemplatedDecl(); |
| 255 | else |
| 256 | Function = cast<FunctionDecl>(D); |
| 257 | |
| 258 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 259 | if (Function->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 260 | return LinkageInfo(InternalLinkage, DefaultVisibility, false); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 261 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 262 | // - a data member of an anonymous union. |
| 263 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 264 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 267 | if (D->isInAnonymousNamespace()) { |
| 268 | const VarDecl *Var = dyn_cast<VarDecl>(D); |
| 269 | const FunctionDecl *Func = dyn_cast<FunctionDecl>(D); |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 270 | if ((!Var || !isInExternCContext(Var)) && |
| 271 | (!Func || !isInExternCContext(Func))) |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 272 | return LinkageInfo::uniqueExternal(); |
| 273 | } |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 274 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 275 | // Set up the defaults. |
| 276 | |
| 277 | // C99 6.2.2p5: |
| 278 | // If the declaration of an identifier for an object has file |
| 279 | // scope and no storage-class specifier, its linkage is |
| 280 | // external. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 281 | LinkageInfo LV; |
| 282 | |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 283 | if (!OnlyTemplate) { |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 284 | if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) { |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 285 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 286 | } else { |
| 287 | // If we're declared in a namespace with a visibility attribute, |
| 288 | // use that namespace's visibility, but don't call it explicit. |
| 289 | for (const DeclContext *DC = D->getDeclContext(); |
| 290 | !isa<TranslationUnitDecl>(DC); |
| 291 | DC = DC->getParent()) { |
| 292 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC); |
| 293 | if (!ND) continue; |
| 294 | if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) { |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 295 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 296 | break; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 302 | if (!OnlyTemplate) { |
Rafael Espindola | 4fc1490 | 2012-04-19 04:37:16 +0000 | [diff] [blame] | 303 | LV.mergeVisibility(Context.getLangOpts().getVisibilityMode()); |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 304 | // If we're paying attention to global visibility, apply |
| 305 | // -finline-visibility-hidden if this is an inline method. |
| 306 | if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D)) |
| 307 | LV.mergeVisibility(HiddenVisibility, true); |
| 308 | } |
Rafael Espindola | ff25798 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 309 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 310 | // C++ [basic.link]p4: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 312 | // A name having namespace scope has external linkage if it is the |
| 313 | // name of |
| 314 | // |
| 315 | // - an object or reference, unless it has internal linkage; or |
| 316 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 317 | // GCC applies the following optimization to variables and static |
| 318 | // data members, but not to functions: |
| 319 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 320 | // Modify the variable's LV by the LV of its type unless this is |
| 321 | // C or extern "C". This follows from [basic.link]p9: |
| 322 | // A type without linkage shall not be used as the type of a |
| 323 | // variable or function with external linkage unless |
| 324 | // - the entity has C language linkage, or |
| 325 | // - the entity is declared within an unnamed namespace, or |
| 326 | // - the entity is not used or is defined in the same |
| 327 | // translation unit. |
| 328 | // and [basic.link]p10: |
| 329 | // ...the types specified by all declarations referring to a |
| 330 | // given variable or function shall be identical... |
| 331 | // C does not have an equivalent rule. |
| 332 | // |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 333 | // Ignore this if we've got an explicit attribute; the user |
| 334 | // probably knows what they're doing. |
| 335 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 336 | // Note that we don't want to make the variable non-external |
| 337 | // because of this, but unique-external linkage suits us. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 338 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 339 | !Var->getDeclContext()->isExternCContext()) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 340 | LinkageInfo TypeLV = getLVForType(Var->getType()); |
| 341 | if (TypeLV.linkage() != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 342 | return LinkageInfo::uniqueExternal(); |
Rafael Espindola | d70d20a | 2012-04-19 05:24:05 +0000 | [diff] [blame] | 343 | LV.mergeVisibility(TypeLV); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 344 | } |
| 345 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 346 | if (Var->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 347 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 348 | |
Rafael Espindola | 538fb98 | 2012-11-12 04:10:23 +0000 | [diff] [blame] | 349 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 350 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't have |
| 351 | // to do it here. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 353 | // - a function, unless it has internal linkage; or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 354 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 67fa6d5 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 355 | // In theory, we can modify the function's LV by the LV of its |
| 356 | // type unless it has C linkage (see comment above about variables |
| 357 | // for justification). In practice, GCC doesn't do this, so it's |
| 358 | // just too painful to make work. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 359 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 360 | if (Function->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 361 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 362 | |
Rafael Espindola | 5175861 | 2012-11-21 02:47:19 +0000 | [diff] [blame] | 363 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 364 | // merging storage classes and visibility attributes, so we don't have to |
| 365 | // look at previous decls in here. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 366 | |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 367 | // In C++, then if the type of the function uses a type with |
| 368 | // unique-external linkage, it's not legally usable from outside |
| 369 | // this translation unit. However, we should use the C linkage |
| 370 | // rules instead for extern "C" declarations. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 371 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 372 | !Function->getDeclContext()->isExternCContext() && |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 373 | Function->getType()->getLinkage() == UniqueExternalLinkage) |
| 374 | return LinkageInfo::uniqueExternal(); |
| 375 | |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 376 | // Consider LV from the template and the template arguments unless |
| 377 | // this is an explicit specialization with a visibility attribute. |
| 378 | if (FunctionTemplateSpecializationInfo *specInfo |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 379 | = Function->getTemplateSpecializationInfo()) { |
Rafael Espindola | 9db614f | 2012-05-25 16:41:35 +0000 | [diff] [blame] | 380 | LinkageInfo TempLV = getLVForDecl(specInfo->getTemplate(), true); |
| 381 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
| 382 | LinkageInfo ArgsLV = getLVForTemplateArgumentList(templateArgs, |
| 383 | OnlyTemplate); |
| 384 | if (shouldConsiderTemplateVis(Function, specInfo)) { |
Rafael Espindola | edb4b62 | 2012-06-11 14:29:58 +0000 | [diff] [blame] | 385 | LV.mergeWithMin(TempLV); |
Rafael Espindola | 9db614f | 2012-05-25 16:41:35 +0000 | [diff] [blame] | 386 | LV.mergeWithMin(ArgsLV); |
| 387 | } else { |
| 388 | LV.mergeLinkage(TempLV); |
| 389 | LV.mergeLinkage(ArgsLV); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 390 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 393 | // - a named class (Clause 9), or an unnamed class defined in a |
| 394 | // typedef declaration in which the class has the typedef name |
| 395 | // for linkage purposes (7.1.3); or |
| 396 | // - a named enumeration (7.2), or an unnamed enumeration |
| 397 | // defined in a typedef declaration in which the enumeration |
| 398 | // has the typedef name for linkage purposes (7.1.3); or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 399 | } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 400 | // Unnamed tags have no linkage. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 401 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 402 | return LinkageInfo::none(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 403 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 404 | // If this is a class template specialization, consider the |
| 405 | // linkage of the template and template arguments. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 406 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 407 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
Rafael Espindola | ad359be | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 408 | // From the template. |
| 409 | LinkageInfo TempLV = getLVForDecl(spec->getSpecializedTemplate(), true); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 410 | |
Rafael Espindola | ad359be | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 411 | // The arguments at which the template was instantiated. |
| 412 | const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs(); |
| 413 | LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs, |
| 414 | OnlyTemplate); |
| 415 | if (shouldConsiderTemplateVis(spec)) { |
Rafael Espindola | edb4b62 | 2012-06-11 14:29:58 +0000 | [diff] [blame] | 416 | LV.mergeWithMin(TempLV); |
Rafael Espindola | ad359be | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 417 | LV.mergeWithMin(ArgsLV); |
| 418 | } else { |
| 419 | LV.mergeLinkage(TempLV); |
| 420 | LV.mergeLinkage(ArgsLV); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 421 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 422 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 423 | |
| 424 | // - an enumerator belonging to an enumeration with external linkage; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 425 | } else if (isa<EnumConstantDecl>(D)) { |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 426 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
| 427 | OnlyTemplate); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 428 | if (!isExternalLinkage(EnumLV.linkage())) |
| 429 | return LinkageInfo::none(); |
| 430 | LV.merge(EnumLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 431 | |
| 432 | // - a template, unless it is a function template that has |
| 433 | // internal linkage (Clause 14); |
John McCall | 1a0918a | 2011-03-04 10:39:25 +0000 | [diff] [blame] | 434 | } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { |
Rafael Espindola | 60115a0 | 2012-04-22 00:43:48 +0000 | [diff] [blame] | 435 | LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters())); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 436 | // - a namespace (7.3), unless it is declared within an unnamed |
| 437 | // namespace. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 438 | } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { |
| 439 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 440 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 441 | // By extension, we assign external linkage to Objective-C |
| 442 | // interfaces. |
| 443 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 444 | // fallout |
| 445 | |
| 446 | // Everything not covered here has no linkage. |
| 447 | } else { |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 448 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | // If we ended up with non-external linkage, visibility should |
| 452 | // always be default. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 453 | if (LV.linkage() != ExternalLinkage) |
| 454 | return LinkageInfo(LV.linkage(), DefaultVisibility, false); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 455 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 456 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 459 | static LinkageInfo getLVForClassMember(const NamedDecl *D, bool OnlyTemplate) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 460 | // Only certain class members have linkage. Note that fields don't |
| 461 | // really have linkage, but it's convenient to say they do for the |
| 462 | // purposes of calculating linkage of pointer-to-data-member |
| 463 | // template arguments. |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 464 | if (!(isa<CXXMethodDecl>(D) || |
| 465 | isa<VarDecl>(D) || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 466 | isa<FieldDecl>(D) || |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 467 | isa<TagDecl>(D))) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 468 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 469 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 470 | LinkageInfo LV; |
| 471 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 472 | // If we have an explicit visibility attribute, merge that in. |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 473 | if (!OnlyTemplate) { |
Rafael Espindola | 4157454 | 2012-04-19 04:27:47 +0000 | [diff] [blame] | 474 | if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 475 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 476 | // If we're paying attention to global visibility, apply |
| 477 | // -finline-visibility-hidden if this is an inline method. |
| 478 | // |
| 479 | // Note that we do this before merging information about |
| 480 | // the class visibility. |
| 481 | if (!LV.visibilityExplicit() && useInlineVisibilityHidden(D)) |
| 482 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 483 | } |
Rafael Espindola | c7e6060 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 484 | |
| 485 | // If this class member has an explicit visibility attribute, the only |
| 486 | // thing that can change its visibility is the template arguments, so |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 487 | // only look for them when processing the class. |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 488 | bool ClassOnlyTemplate = LV.visibilityExplicit() ? true : OnlyTemplate; |
Rafael Espindola | 0f90590 | 2012-04-16 18:25:01 +0000 | [diff] [blame] | 489 | |
Rafael Espindola | c7e6060 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 490 | // If this member has an visibility attribute, ClassF will exclude |
| 491 | // attributes on the class or command line options, keeping only information |
| 492 | // about the template instantiation. If the member has no visibility |
| 493 | // attributes, mergeWithMin behaves like merge, so in both cases mergeWithMin |
| 494 | // produces the desired result. |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 495 | LV.mergeWithMin(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), |
| 496 | ClassOnlyTemplate)); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 497 | if (!isExternalLinkage(LV.linkage())) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 498 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 499 | |
| 500 | // If the class already has unique-external linkage, we can't improve. |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 501 | if (LV.linkage() == UniqueExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 502 | return LinkageInfo::uniqueExternal(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 503 | |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 504 | if (!OnlyTemplate) |
Rafael Espindola | 4fc1490 | 2012-04-19 04:37:16 +0000 | [diff] [blame] | 505 | LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode()); |
Rafael Espindola | ff25798 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 506 | |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 507 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 508 | // If the type of the function uses a type with unique-external |
| 509 | // linkage, it's not legally usable from outside this translation unit. |
| 510 | if (MD->getType()->getLinkage() == UniqueExternalLinkage) |
| 511 | return LinkageInfo::uniqueExternal(); |
| 512 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 513 | // If this is a method template specialization, use the linkage for |
| 514 | // the template parameters and arguments. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 515 | if (FunctionTemplateSpecializationInfo *spec |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 516 | = MD->getTemplateSpecializationInfo()) { |
Rafael Espindola | 41be8cd | 2012-05-25 17:22:33 +0000 | [diff] [blame] | 517 | const TemplateArgumentList &TemplateArgs = *spec->TemplateArguments; |
| 518 | LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs, |
| 519 | OnlyTemplate); |
| 520 | TemplateParameterList *TemplateParams = |
| 521 | spec->getTemplate()->getTemplateParameters(); |
| 522 | LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams); |
Rafael Espindola | 9db614f | 2012-05-25 16:41:35 +0000 | [diff] [blame] | 523 | if (shouldConsiderTemplateVis(MD, spec)) { |
Rafael Espindola | 41be8cd | 2012-05-25 17:22:33 +0000 | [diff] [blame] | 524 | LV.mergeWithMin(ArgsLV); |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 525 | if (!OnlyTemplate) |
Rafael Espindola | edb4b62 | 2012-06-11 14:29:58 +0000 | [diff] [blame] | 526 | LV.mergeWithMin(ParamsLV); |
Rafael Espindola | 41be8cd | 2012-05-25 17:22:33 +0000 | [diff] [blame] | 527 | } else { |
| 528 | LV.mergeLinkage(ArgsLV); |
| 529 | if (!OnlyTemplate) |
| 530 | LV.mergeLinkage(ParamsLV); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 531 | } |
John McCall | 66cbcf3 | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 532 | } |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 533 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 534 | // Note that in contrast to basically every other situation, we |
| 535 | // *do* apply -fvisibility to method declarations. |
| 536 | |
| 537 | } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 538 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 539 | = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
Rafael Espindola | 20831e2 | 2012-05-25 15:51:26 +0000 | [diff] [blame] | 540 | // Merge template argument/parameter information for member |
| 541 | // class template specializations. |
| 542 | const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs(); |
| 543 | LinkageInfo ArgsLV = getLVForTemplateArgumentList(TemplateArgs, |
| 544 | OnlyTemplate); |
| 545 | TemplateParameterList *TemplateParams = |
| 546 | spec->getSpecializedTemplate()->getTemplateParameters(); |
| 547 | LinkageInfo ParamsLV = getLVForTemplateParameterList(TemplateParams); |
Rafael Espindola | ad359be | 2012-05-25 14:47:05 +0000 | [diff] [blame] | 548 | if (shouldConsiderTemplateVis(spec)) { |
Rafael Espindola | 20831e2 | 2012-05-25 15:51:26 +0000 | [diff] [blame] | 549 | LV.mergeWithMin(ArgsLV); |
Rafael Espindola | 59073bb | 2012-05-25 14:17:45 +0000 | [diff] [blame] | 550 | if (!OnlyTemplate) |
Rafael Espindola | edb4b62 | 2012-06-11 14:29:58 +0000 | [diff] [blame] | 551 | LV.mergeWithMin(ParamsLV); |
Rafael Espindola | 20831e2 | 2012-05-25 15:51:26 +0000 | [diff] [blame] | 552 | } else { |
| 553 | LV.mergeLinkage(ArgsLV); |
| 554 | if (!OnlyTemplate) |
| 555 | LV.mergeLinkage(ParamsLV); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 556 | } |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 557 | } |
| 558 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 559 | // Static data members. |
| 560 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | ee30102 | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 561 | // Modify the variable's linkage by its type, but ignore the |
| 562 | // type's visibility unless it's a definition. |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 563 | LinkageInfo TypeLV = getLVForType(VD->getType()); |
| 564 | if (TypeLV.linkage() != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 565 | LV.mergeLinkage(UniqueExternalLinkage); |
Rafael Espindola | c7e6060 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 566 | LV.mergeVisibility(TypeLV); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 567 | } |
| 568 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 569 | return LV; |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 570 | } |
| 571 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 572 | static void clearLinkageForClass(const CXXRecordDecl *record) { |
| 573 | for (CXXRecordDecl::decl_iterator |
| 574 | i = record->decls_begin(), e = record->decls_end(); i != e; ++i) { |
| 575 | Decl *child = *i; |
| 576 | if (isa<NamedDecl>(child)) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 577 | cast<NamedDecl>(child)->ClearLinkageCache(); |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 581 | void NamedDecl::anchor() { } |
| 582 | |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 583 | void NamedDecl::ClearLinkageCache() { |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 584 | // Note that we can't skip clearing the linkage of children just |
| 585 | // because the parent doesn't have cached linkage: we don't cache |
| 586 | // when computing linkage for parent contexts. |
| 587 | |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 588 | HasCachedLinkage = 0; |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 589 | |
| 590 | // If we're changing the linkage of a class, we need to reset the |
| 591 | // linkage of child declarations, too. |
| 592 | if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this)) |
| 593 | clearLinkageForClass(record); |
| 594 | |
Dmitri Gribenko | f8c1214 | 2013-02-03 16:10:26 +0000 | [diff] [blame] | 595 | if (ClassTemplateDecl *temp = dyn_cast<ClassTemplateDecl>(this)) { |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 596 | // Clear linkage for the template pattern. |
| 597 | CXXRecordDecl *record = temp->getTemplatedDecl(); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 598 | record->HasCachedLinkage = 0; |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 599 | clearLinkageForClass(record); |
| 600 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 601 | // We need to clear linkage for specializations, too. |
| 602 | for (ClassTemplateDecl::spec_iterator |
| 603 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 604 | i->ClearLinkageCache(); |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 605 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 606 | |
| 607 | // Clear cached linkage for function template decls, too. |
Dmitri Gribenko | f8c1214 | 2013-02-03 16:10:26 +0000 | [diff] [blame] | 608 | if (FunctionTemplateDecl *temp = dyn_cast<FunctionTemplateDecl>(this)) { |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 609 | temp->getTemplatedDecl()->ClearLinkageCache(); |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 610 | for (FunctionTemplateDecl::spec_iterator |
| 611 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 612 | i->ClearLinkageCache(); |
John McCall | 7895194 | 2011-03-22 06:58:49 +0000 | [diff] [blame] | 613 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 614 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 617 | Linkage NamedDecl::getLinkage() const { |
Richard Smith | ad0e27b | 2013-02-12 05:48:23 +0000 | [diff] [blame] | 618 | if (HasCachedLinkage) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 619 | return Linkage(CachedLinkage); |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 620 | |
| 621 | CachedLinkage = getLVForDecl(this, true).linkage(); |
| 622 | HasCachedLinkage = 1; |
| 623 | |
| 624 | #ifndef NDEBUG |
| 625 | verifyLinkage(); |
| 626 | #endif |
| 627 | |
| 628 | return Linkage(CachedLinkage); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 629 | } |
| 630 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 631 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 632 | LinkageInfo LI = getLVForDecl(this, false); |
| 633 | if (HasCachedLinkage) { |
| 634 | assert(Linkage(CachedLinkage) == LI.linkage()); |
| 635 | return LI; |
Rafael Espindola | 140aadf | 2012-12-25 07:31:49 +0000 | [diff] [blame] | 636 | } |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 637 | HasCachedLinkage = 1; |
| 638 | CachedLinkage = LI.linkage(); |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 639 | |
| 640 | #ifndef NDEBUG |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 641 | verifyLinkage(); |
| 642 | #endif |
| 643 | |
| 644 | return LI; |
| 645 | } |
| 646 | |
| 647 | void NamedDecl::verifyLinkage() const { |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 648 | // In C (because of gnu inline) and in c++ with microsoft extensions an |
| 649 | // static can follow an extern, so we can have two decls with different |
| 650 | // linkages. |
| 651 | const LangOptions &Opts = getASTContext().getLangOpts(); |
| 652 | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 653 | return; |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 654 | |
| 655 | // We have just computed the linkage for this decl. By induction we know |
| 656 | // that all other computed linkages match, check that the one we just computed |
| 657 | // also does. |
| 658 | NamedDecl *D = NULL; |
| 659 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 660 | NamedDecl *T = cast<NamedDecl>(*I); |
| 661 | if (T == this) |
| 662 | continue; |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 663 | if (T->HasCachedLinkage != 0) { |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 664 | D = T; |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | assert(!D || D->CachedLinkage == CachedLinkage); |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 669 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 670 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 671 | llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const { |
| 672 | // Use the most recent declaration of a variable. |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 673 | if (const VarDecl *Var = dyn_cast<VarDecl>(this)) { |
Rafael Espindola | c6b82c3 | 2012-11-12 04:32:23 +0000 | [diff] [blame] | 674 | if (llvm::Optional<Visibility> V = getVisibilityOf(Var)) |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 675 | return V; |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 676 | |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 677 | if (Var->isStaticDataMember()) { |
| 678 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
| 679 | if (InstantiatedFrom) |
| 680 | return getVisibilityOf(InstantiatedFrom); |
| 681 | } |
| 682 | |
| 683 | return llvm::Optional<Visibility>(); |
| 684 | } |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 685 | // Use the most recent declaration of a function, and also handle |
| 686 | // function template specializations. |
| 687 | if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) { |
Rafael Espindola | c6b82c3 | 2012-11-12 04:32:23 +0000 | [diff] [blame] | 688 | if (llvm::Optional<Visibility> V = getVisibilityOf(fn)) |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 689 | return V; |
| 690 | |
| 691 | // If the function is a specialization of a template with an |
| 692 | // explicit visibility attribute, use that. |
| 693 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 694 | = fn->getTemplateSpecializationInfo()) |
| 695 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl()); |
| 696 | |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 697 | // If the function is a member of a specialization of a class template |
| 698 | // and the corresponding decl has explicit visibility, use that. |
| 699 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 700 | if (InstantiatedFrom) |
| 701 | return getVisibilityOf(InstantiatedFrom); |
| 702 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 703 | return llvm::Optional<Visibility>(); |
| 704 | } |
| 705 | |
| 706 | // Otherwise, just check the declaration itself first. |
| 707 | if (llvm::Optional<Visibility> V = getVisibilityOf(this)) |
| 708 | return V; |
| 709 | |
Rafael Espindola | 9849901 | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 710 | // The visibility of a template is stored in the templated decl. |
| 711 | if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(this)) |
| 712 | return getVisibilityOf(TD->getTemplatedDecl()); |
| 713 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 714 | // If there wasn't explicit visibility there, and this is a |
| 715 | // specialization of a class template, check for visibility |
| 716 | // on the pattern. |
| 717 | if (const ClassTemplateSpecializationDecl *spec |
Rafael Espindola | d3d02dd | 2012-07-13 01:19:08 +0000 | [diff] [blame] | 718 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 719 | return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl()); |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 720 | |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 721 | // If this is a member class of a specialization of a class template |
| 722 | // and the corresponding decl has explicit visibility, use that. |
| 723 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) { |
| 724 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 725 | if (InstantiatedFrom) |
| 726 | return getVisibilityOf(InstantiatedFrom); |
| 727 | } |
| 728 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 729 | return llvm::Optional<Visibility>(); |
| 730 | } |
| 731 | |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 732 | static LinkageInfo getLVForDecl(const NamedDecl *D, bool OnlyTemplate) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 733 | // Objective-C: treat all Objective-C declarations as having external |
| 734 | // linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 735 | switch (D->getKind()) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 736 | default: |
| 737 | break; |
Argyrios Kyrtzidis | f8d34ed | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 738 | case Decl::ParmVar: |
| 739 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 740 | case Decl::TemplateTemplateParm: // count these as external |
| 741 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 742 | case Decl::ObjCAtDefsField: |
| 743 | case Decl::ObjCCategory: |
| 744 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 745 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 746 | case Decl::ObjCImplementation: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 747 | case Decl::ObjCMethod: |
| 748 | case Decl::ObjCProperty: |
| 749 | case Decl::ObjCPropertyImpl: |
| 750 | case Decl::ObjCProtocol: |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 751 | return LinkageInfo::external(); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 752 | |
| 753 | case Decl::CXXRecord: { |
| 754 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(D); |
| 755 | if (Record->isLambda()) { |
| 756 | if (!Record->getLambdaManglingNumber()) { |
| 757 | // This lambda has no mangling number, so it's internal. |
| 758 | return LinkageInfo::internal(); |
| 759 | } |
| 760 | |
| 761 | // This lambda has its linkage/visibility determined by its owner. |
| 762 | const DeclContext *DC = D->getDeclContext()->getRedeclContext(); |
| 763 | if (Decl *ContextDecl = Record->getLambdaContextDecl()) { |
| 764 | if (isa<ParmVarDecl>(ContextDecl)) |
| 765 | DC = ContextDecl->getDeclContext()->getRedeclContext(); |
| 766 | else |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 767 | return getLVForDecl(cast<NamedDecl>(ContextDecl), |
| 768 | OnlyTemplate); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 772 | return getLVForDecl(ND, OnlyTemplate); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 773 | |
| 774 | return LinkageInfo::external(); |
| 775 | } |
| 776 | |
| 777 | break; |
| 778 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 781 | // Handle linkage for namespace-scope names. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 782 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 783 | return getLVForNamespaceScopeDecl(D, OnlyTemplate); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 784 | |
| 785 | // C++ [basic.link]p5: |
| 786 | // In addition, a member function, static data member, a named |
| 787 | // class or enumeration of class scope, or an unnamed class or |
| 788 | // enumeration defined in a class-scope typedef declaration such |
| 789 | // that the class or enumeration has the typedef name for linkage |
| 790 | // purposes (7.1.3), has external linkage if the name of the class |
| 791 | // has external linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 792 | if (D->getDeclContext()->isRecord()) |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 793 | return getLVForClassMember(D, OnlyTemplate); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 794 | |
| 795 | // C++ [basic.link]p6: |
| 796 | // The name of a function declared in block scope and the name of |
| 797 | // an object declared by a block scope extern declaration have |
| 798 | // linkage. If there is a visible declaration of an entity with |
| 799 | // linkage having the same name and type, ignoring entities |
| 800 | // declared outside the innermost enclosing namespace scope, the |
| 801 | // block scope declaration declares that same entity and receives |
| 802 | // the linkage of the previous declaration. If there is more than |
| 803 | // one such matching entity, the program is ill-formed. Otherwise, |
| 804 | // if no matching entity is found, the block scope entity receives |
| 805 | // external linkage. |
Rafael Espindola | 6acc4bc | 2013-01-05 01:28:37 +0000 | [diff] [blame] | 806 | if (D->getDeclContext()->isFunctionOrMethod()) { |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 807 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 808 | if (Function->isInAnonymousNamespace() && |
| 809 | !Function->getDeclContext()->isExternCContext()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 810 | return LinkageInfo::uniqueExternal(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 811 | |
Rafael Espindola | abe75ef | 2013-01-09 16:34:58 +0000 | [diff] [blame] | 812 | // This is a "void f();" which got merged with a file static. |
| 813 | if (Function->getStorageClass() == SC_Static) |
| 814 | return LinkageInfo::internal(); |
| 815 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 816 | LinkageInfo LV; |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 817 | if (!OnlyTemplate) { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 818 | if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility()) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 819 | LV.mergeVisibility(*Vis, true); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 820 | } |
Rafael Espindola | b282920 | 2012-11-29 16:38:22 +0000 | [diff] [blame] | 821 | |
| 822 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 823 | // merging storage classes and visibility attributes, so we don't have to |
| 824 | // look at previous decls in here. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 825 | |
| 826 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 827 | } |
| 828 | |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 829 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) |
Rafael Espindola | a7a2f2a | 2012-12-18 04:18:55 +0000 | [diff] [blame] | 830 | if (Var->getStorageClassAsWritten() == SC_Extern || |
| 831 | Var->getStorageClassAsWritten() == SC_PrivateExtern) { |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 832 | if (Var->isInAnonymousNamespace() && |
| 833 | !Var->getDeclContext()->isExternCContext()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 834 | return LinkageInfo::uniqueExternal(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 835 | |
Rafael Espindola | a7a2f2a | 2012-12-18 04:18:55 +0000 | [diff] [blame] | 836 | // This is an "extern int foo;" which got merged with a file static. |
| 837 | if (Var->getStorageClass() == SC_Static) |
| 838 | return LinkageInfo::internal(); |
| 839 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 840 | LinkageInfo LV; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 841 | if (Var->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 842 | LV.mergeVisibility(HiddenVisibility, true); |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 843 | else if (!OnlyTemplate) { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 844 | if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility()) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 845 | LV.mergeVisibility(*Vis, true); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 846 | } |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 847 | |
Rafael Espindola | 538fb98 | 2012-11-12 04:10:23 +0000 | [diff] [blame] | 848 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 849 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't |
| 850 | // have to do it here. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 851 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | // C++ [basic.link]p6: |
| 856 | // Names not covered by these rules have no linkage. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 857 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 858 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 860 | std::string NamedDecl::getQualifiedNameAsString() const { |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 861 | return getQualifiedNameAsString(getASTContext().getPrintingPolicy()); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 865 | const DeclContext *Ctx = getDeclContext(); |
| 866 | |
| 867 | if (Ctx->isFunctionOrMethod()) |
| 868 | return getNameAsString(); |
| 869 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 870 | typedef SmallVector<const DeclContext *, 8> ContextsTy; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 871 | ContextsTy Contexts; |
| 872 | |
| 873 | // Collect contexts. |
| 874 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 875 | Contexts.push_back(Ctx); |
| 876 | Ctx = Ctx->getParent(); |
| 877 | }; |
| 878 | |
| 879 | std::string QualName; |
| 880 | llvm::raw_string_ostream OS(QualName); |
| 881 | |
| 882 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 883 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 885 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 886 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 887 | std::string TemplateArgsStr |
| 888 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 889 | TemplateArgs.data(), |
| 890 | TemplateArgs.size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 891 | P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 892 | OS << Spec->getName() << TemplateArgsStr; |
| 893 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 894 | if (ND->isAnonymousNamespace()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 895 | OS << "<anonymous namespace>"; |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 896 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 897 | OS << *ND; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 898 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 899 | if (!RD->getIdentifier()) |
| 900 | OS << "<anonymous " << RD->getKindName() << '>'; |
| 901 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 902 | OS << *RD; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 903 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 904 | const FunctionProtoType *FT = 0; |
| 905 | if (FD->hasWrittenPrototype()) |
Eli Friedman | 482466b | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 906 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 907 | |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 908 | OS << *FD << '('; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 909 | if (FT) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 910 | unsigned NumParams = FD->getNumParams(); |
| 911 | for (unsigned i = 0; i < NumParams; ++i) { |
| 912 | if (i) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 913 | OS << ", "; |
Argyrios Kyrtzidis | 7ad5c99 | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 914 | OS << FD->getParamDecl(i)->getType().stream(P); |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | if (FT->isVariadic()) { |
| 918 | if (NumParams > 0) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 919 | OS << ", "; |
| 920 | OS << "..."; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 923 | OS << ')'; |
| 924 | } else { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 925 | OS << *cast<NamedDecl>(*I); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 926 | } |
| 927 | OS << "::"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 928 | } |
| 929 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 930 | if (getDeclName()) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 931 | OS << *this; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 932 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 933 | OS << "<anonymous>"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 934 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 935 | return OS.str(); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 938 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 939 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 940 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 941 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 942 | // We want to keep it, unless it nominates same namespace. |
| 943 | if (getKind() == Decl::UsingDirective) { |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 944 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() |
| 945 | ->getOriginalNamespace() == |
| 946 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace() |
| 947 | ->getOriginalNamespace(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 948 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 950 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 951 | // For function declarations, we keep track of redeclarations. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 952 | return FD->getPreviousDecl() == OldD; |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 953 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 954 | // For function templates, the underlying function declarations are linked. |
| 955 | if (const FunctionTemplateDecl *FunctionTemplate |
| 956 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 957 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 958 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 959 | return FunctionTemplate->getTemplatedDecl() |
| 960 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 962 | // For method declarations, we keep track of redeclarations. |
| 963 | if (isa<ObjCMethodDecl>(this)) |
| 964 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 966 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 967 | return true; |
| 968 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 969 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 970 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 971 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 972 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 973 | if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) { |
| 974 | ASTContext &Context = getASTContext(); |
| 975 | return Context.getCanonicalNestedNameSpecifier( |
| 976 | cast<UsingDecl>(this)->getQualifier()) == |
| 977 | Context.getCanonicalNestedNameSpecifier( |
| 978 | cast<UsingDecl>(OldD)->getQualifier()); |
| 979 | } |
Argyrios Kyrtzidis | c80117e | 2010-11-04 08:48:52 +0000 | [diff] [blame] | 980 | |
Douglas Gregor | 7a53740 | 2012-01-03 23:26:26 +0000 | [diff] [blame] | 981 | // A typedef of an Objective-C class type can replace an Objective-C class |
| 982 | // declaration or definition, and vice versa. |
| 983 | if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) || |
| 984 | (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD))) |
| 985 | return true; |
| 986 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 987 | // For non-function declarations, if the declarations are of the |
| 988 | // same kind then this must be a redeclaration, or semantic analysis |
| 989 | // would not have given us the new declaration. |
| 990 | return this->getKind() == OldD->getKind(); |
| 991 | } |
| 992 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 993 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 994 | return getLinkage() != NoLinkage; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 995 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 996 | |
Daniel Dunbar | 6daffa5 | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 997 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 998 | NamedDecl *ND = this; |
Benjamin Kramer | 56757e9 | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 999 | while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
| 1000 | ND = UD->getTargetDecl(); |
| 1001 | |
| 1002 | if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 1003 | return AD->getClassInterface(); |
| 1004 | |
| 1005 | return ND; |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1008 | bool NamedDecl::isCXXInstanceMember() const { |
Douglas Gregor | 5bc37f6 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 1009 | if (!isCXXClassMember()) |
| 1010 | return false; |
| 1011 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1012 | const NamedDecl *D = this; |
| 1013 | if (isa<UsingShadowDecl>(D)) |
| 1014 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1015 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1016 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1017 | return true; |
| 1018 | if (isa<CXXMethodDecl>(D)) |
| 1019 | return cast<CXXMethodDecl>(D)->isInstance(); |
| 1020 | if (isa<FunctionTemplateDecl>(D)) |
| 1021 | return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) |
| 1022 | ->getTemplatedDecl())->isInstance(); |
| 1023 | return false; |
| 1024 | } |
| 1025 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1026 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1027 | // DeclaratorDecl Implementation |
| 1028 | //===----------------------------------------------------------------------===// |
| 1029 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1030 | template <typename DeclT> |
| 1031 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1032 | if (decl->getNumTemplateParameterLists() > 0) |
| 1033 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1034 | else |
| 1035 | return decl->getInnerLocStart(); |
| 1036 | } |
| 1037 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1038 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1039 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1040 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1041 | return SourceLocation(); |
| 1042 | } |
| 1043 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1044 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1045 | if (QualifierLoc) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1046 | // Make sure the extended decl info is allocated. |
| 1047 | if (!hasExtInfo()) { |
| 1048 | // Save (non-extended) type source info pointer. |
| 1049 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1050 | // Allocate external info struct. |
| 1051 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1052 | // Restore savedTInfo into (extended) decl info. |
| 1053 | getExtInfo()->TInfo = savedTInfo; |
| 1054 | } |
| 1055 | // Set qualifier info. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1056 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1057 | } else { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1058 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1059 | if (hasExtInfo()) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1060 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 1061 | // Save type source info pointer. |
| 1062 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 1063 | // Deallocate the extended decl info. |
| 1064 | getASTContext().Deallocate(getExtInfo()); |
| 1065 | // Restore savedTInfo into (non-extended) decl info. |
| 1066 | DeclInfo = savedTInfo; |
| 1067 | } |
| 1068 | else |
| 1069 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1074 | void |
| 1075 | DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 1076 | unsigned NumTPLists, |
| 1077 | TemplateParameterList **TPLists) { |
| 1078 | assert(NumTPLists > 0); |
| 1079 | // Make sure the extended decl info is allocated. |
| 1080 | if (!hasExtInfo()) { |
| 1081 | // Save (non-extended) type source info pointer. |
| 1082 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1083 | // Allocate external info struct. |
| 1084 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1085 | // Restore savedTInfo into (extended) decl info. |
| 1086 | getExtInfo()->TInfo = savedTInfo; |
| 1087 | } |
| 1088 | // Set the template parameter lists info. |
| 1089 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 1090 | } |
| 1091 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1092 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1093 | return getTemplateOrInnerLocStart(this); |
| 1094 | } |
| 1095 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1096 | namespace { |
| 1097 | |
| 1098 | // Helper function: returns true if QT is or contains a type |
| 1099 | // having a postfix component. |
| 1100 | bool typeIsPostfix(clang::QualType QT) { |
| 1101 | while (true) { |
| 1102 | const Type* T = QT.getTypePtr(); |
| 1103 | switch (T->getTypeClass()) { |
| 1104 | default: |
| 1105 | return false; |
| 1106 | case Type::Pointer: |
| 1107 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1108 | break; |
| 1109 | case Type::BlockPointer: |
| 1110 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1111 | break; |
| 1112 | case Type::MemberPointer: |
| 1113 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1114 | break; |
| 1115 | case Type::LValueReference: |
| 1116 | case Type::RValueReference: |
| 1117 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1118 | break; |
| 1119 | case Type::PackExpansion: |
| 1120 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1121 | break; |
| 1122 | case Type::Paren: |
| 1123 | case Type::ConstantArray: |
| 1124 | case Type::DependentSizedArray: |
| 1125 | case Type::IncompleteArray: |
| 1126 | case Type::VariableArray: |
| 1127 | case Type::FunctionProto: |
| 1128 | case Type::FunctionNoProto: |
| 1129 | return true; |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | |
| 1134 | } // namespace |
| 1135 | |
| 1136 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1137 | SourceLocation RangeEnd = getLocation(); |
| 1138 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 1139 | if (typeIsPostfix(TInfo->getType())) |
| 1140 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1141 | } |
| 1142 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1143 | } |
| 1144 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1145 | void |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1146 | QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, |
| 1147 | unsigned NumTPLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1148 | TemplateParameterList **TPLists) { |
| 1149 | assert((NumTPLists == 0 || TPLists != 0) && |
| 1150 | "Empty array of template parameters with positive size!"); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1151 | |
| 1152 | // Free previous template parameters (if any). |
| 1153 | if (NumTemplParamLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1154 | Context.Deallocate(TemplParamLists); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1155 | TemplParamLists = 0; |
| 1156 | NumTemplParamLists = 0; |
| 1157 | } |
| 1158 | // Set info on matched template parameter lists (if any). |
| 1159 | if (NumTPLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1160 | TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1161 | NumTemplParamLists = NumTPLists; |
| 1162 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 1163 | TemplParamLists[i] = TPLists[i]; |
| 1164 | } |
| 1165 | } |
| 1166 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1167 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1168 | // VarDecl Implementation |
| 1169 | //===----------------------------------------------------------------------===// |
| 1170 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1171 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1172 | switch (SC) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1173 | case SC_None: break; |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1174 | case SC_Auto: return "auto"; |
| 1175 | case SC_Extern: return "extern"; |
| 1176 | case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>"; |
| 1177 | case SC_PrivateExtern: return "__private_extern__"; |
| 1178 | case SC_Register: return "register"; |
| 1179 | case SC_Static: return "static"; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1182 | llvm_unreachable("Invalid storage class"); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1185 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1186 | SourceLocation StartL, SourceLocation IdL, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1187 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1188 | StorageClass S, StorageClass SCAsWritten) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1189 | return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1192 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1193 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl)); |
| 1194 | return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0, |
| 1195 | QualType(), 0, SC_None, SC_None); |
| 1196 | } |
| 1197 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1198 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1199 | assert(isLegalForVariable(SC)); |
| 1200 | if (getStorageClass() != SC) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1201 | ClearLinkageCache(); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1202 | |
John McCall | f1e4fbf | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1203 | VarDeclBits.SClass = SC; |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1206 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | d69f31c | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1207 | if (const Expr *Init = getInit()) { |
| 1208 | SourceLocation InitEnd = Init->getLocEnd(); |
Nico Weber | 3a344f9 | 2013-01-22 17:00:09 +0000 | [diff] [blame] | 1209 | // If Init is implicit, ignore its source range and fallback on |
| 1210 | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
| 1211 | if (InitEnd.isValid() && InitEnd != getLocation()) |
Argyrios Kyrtzidis | d69f31c | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1212 | return SourceRange(getOuterLocStart(), InitEnd); |
| 1213 | } |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1214 | return DeclaratorDecl::getSourceRange(); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Rafael Espindola | 7ac928b | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 1217 | template<typename T> |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1218 | static LanguageLinkage getLanguageLinkageTemplate(const T &D) { |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1219 | // C++ [dcl.link]p1: All function types, function names with external linkage, |
| 1220 | // and variable names with external linkage have a language linkage. |
| 1221 | if (!isExternalLinkage(D.getLinkage())) |
| 1222 | return NoLanguageLinkage; |
| 1223 | |
| 1224 | // Language linkage is a C++ concept, but saying that everything else in C has |
Rafael Espindola | 2b721f5 | 2013-01-04 20:41:40 +0000 | [diff] [blame] | 1225 | // C language linkage fits the implementation nicely. |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1226 | ASTContext &Context = D.getASTContext(); |
| 1227 | if (!Context.getLangOpts().CPlusPlus) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1228 | return CLanguageLinkage; |
| 1229 | |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1230 | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
| 1231 | // language linkage of the names of class members and the function type of |
| 1232 | // class member functions. |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1233 | const DeclContext *DC = D.getDeclContext(); |
| 1234 | if (DC->isRecord()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1235 | return CXXLanguageLinkage; |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1236 | |
| 1237 | // If the first decl is in an extern "C" context, any other redeclaration |
| 1238 | // will have C language linkage. If the first one is not in an extern "C" |
| 1239 | // context, we would have reported an error for any other decl being in one. |
Rafael Espindola | 7ac928b | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 1240 | const T *First = D.getFirstDeclaration(); |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1241 | if (First->getDeclContext()->isExternCContext()) |
| 1242 | return CLanguageLinkage; |
| 1243 | return CXXLanguageLinkage; |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1246 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
| 1247 | return getLanguageLinkageTemplate(*this); |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1250 | VarDecl *VarDecl::getCanonicalDecl() { |
| 1251 | return getFirstDeclaration(); |
| 1252 | } |
| 1253 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1254 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition( |
| 1255 | ASTContext &C) const |
| 1256 | { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1257 | // C++ [basic.def]p2: |
| 1258 | // A declaration is a definition unless [...] it contains the 'extern' |
| 1259 | // specifier or a linkage-specification and neither an initializer [...], |
| 1260 | // it declares a static data member in a class declaration [...]. |
| 1261 | // C++ [temp.expl.spec]p15: |
| 1262 | // An explicit specialization of a static data member of a template is a |
| 1263 | // definition if the declaration includes an initializer; otherwise, it is |
| 1264 | // a declaration. |
| 1265 | if (isStaticDataMember()) { |
| 1266 | if (isOutOfLine() && (hasInit() || |
| 1267 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization)) |
| 1268 | return Definition; |
| 1269 | else |
| 1270 | return DeclarationOnly; |
| 1271 | } |
| 1272 | // C99 6.7p5: |
| 1273 | // A definition of an identifier is a declaration for that identifier that |
| 1274 | // [...] causes storage to be reserved for that object. |
| 1275 | // Note: that applies for all non-file-scope objects. |
| 1276 | // C99 6.9.2p1: |
| 1277 | // If the declaration of an identifier for an object has file scope and an |
| 1278 | // initializer, the declaration is an external definition for the identifier |
| 1279 | if (hasInit()) |
| 1280 | return Definition; |
| 1281 | // AST for 'extern "C" int foo;' is annotated with 'extern'. |
| 1282 | if (hasExternalStorage()) |
| 1283 | return DeclarationOnly; |
Fariborz Jahanian | 2bf6d7b | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 1284 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1285 | if (getStorageClassAsWritten() == SC_Extern || |
| 1286 | getStorageClassAsWritten() == SC_PrivateExtern) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 1287 | for (const VarDecl *PrevVar = getPreviousDecl(); |
| 1288 | PrevVar; PrevVar = PrevVar->getPreviousDecl()) { |
Rafael Espindola | 372df45 | 2012-12-17 22:23:47 +0000 | [diff] [blame] | 1289 | if (PrevVar->getLinkage() == InternalLinkage) |
Fariborz Jahanian | 2bf6d7b | 2010-06-21 16:08:37 +0000 | [diff] [blame] | 1290 | return DeclarationOnly; |
| 1291 | } |
| 1292 | } |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1293 | // C99 6.9.2p2: |
| 1294 | // A declaration of an object that has file scope without an initializer, |
| 1295 | // and without a storage class specifier or the scs 'static', constitutes |
| 1296 | // a tentative definition. |
| 1297 | // No such thing in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1298 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1299 | return TentativeDefinition; |
| 1300 | |
| 1301 | // What's left is (in C, block-scope) declarations without initializers or |
| 1302 | // external storage. These are definitions. |
| 1303 | return Definition; |
| 1304 | } |
| 1305 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1306 | VarDecl *VarDecl::getActingDefinition() { |
| 1307 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1308 | if (Kind != TentativeDefinition) |
| 1309 | return 0; |
| 1310 | |
Chris Lattner | f0ed9ef | 2010-06-14 18:31:46 +0000 | [diff] [blame] | 1311 | VarDecl *LastTentative = 0; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1312 | VarDecl *First = getFirstDeclaration(); |
| 1313 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1314 | I != E; ++I) { |
| 1315 | Kind = (*I)->isThisDeclarationADefinition(); |
| 1316 | if (Kind == Definition) |
| 1317 | return 0; |
| 1318 | else if (Kind == TentativeDefinition) |
| 1319 | LastTentative = *I; |
| 1320 | } |
| 1321 | return LastTentative; |
| 1322 | } |
| 1323 | |
| 1324 | bool VarDecl::isTentativeDefinitionNow() const { |
| 1325 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1326 | if (Kind != TentativeDefinition) |
| 1327 | return false; |
| 1328 | |
| 1329 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1330 | if ((*I)->isThisDeclarationADefinition() == Definition) |
| 1331 | return false; |
| 1332 | } |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1333 | return true; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1336 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
Sebastian Redl | e2c52d2 | 2010-02-02 17:55:12 +0000 | [diff] [blame] | 1337 | VarDecl *First = getFirstDeclaration(); |
| 1338 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
| 1339 | I != E; ++I) { |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1340 | if ((*I)->isThisDeclarationADefinition(C) == Definition) |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1341 | return *I; |
| 1342 | } |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1346 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1347 | DefinitionKind Kind = DeclarationOnly; |
| 1348 | |
| 1349 | const VarDecl *First = getFirstDeclaration(); |
| 1350 | for (redecl_iterator I = First->redecls_begin(), E = First->redecls_end(); |
Daniel Dunbar | 047da19 | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 1351 | I != E; ++I) { |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1352 | Kind = std::max(Kind, (*I)->isThisDeclarationADefinition(C)); |
Daniel Dunbar | 047da19 | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 1353 | if (Kind == Definition) |
| 1354 | break; |
| 1355 | } |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1356 | |
| 1357 | return Kind; |
| 1358 | } |
| 1359 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1360 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1361 | redecl_iterator I = redecls_begin(), E = redecls_end(); |
| 1362 | while (I != E && !I->getInit()) |
| 1363 | ++I; |
| 1364 | |
| 1365 | if (I != E) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1366 | D = *I; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1367 | return I->getInit(); |
| 1368 | } |
| 1369 | return 0; |
| 1370 | } |
| 1371 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1372 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1373 | if (Decl::isOutOfLine()) |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1374 | return true; |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 1375 | |
| 1376 | if (!isStaticDataMember()) |
| 1377 | return false; |
| 1378 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1379 | // If this static data member was instantiated from a static data member of |
| 1380 | // a class template, check whether that static data member was defined |
| 1381 | // out-of-line. |
| 1382 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 1383 | return VD->isOutOfLine(); |
| 1384 | |
| 1385 | return false; |
| 1386 | } |
| 1387 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 1388 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 1389 | if (!isStaticDataMember()) |
| 1390 | return 0; |
| 1391 | |
| 1392 | for (VarDecl::redecl_iterator RD = redecls_begin(), RDEnd = redecls_end(); |
| 1393 | RD != RDEnd; ++RD) { |
| 1394 | if (RD->getLexicalDeclContext()->isFileContext()) |
| 1395 | return *RD; |
| 1396 | } |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1401 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1402 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 1403 | Eval->~EvaluatedStmt(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 1404 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | Init = I; |
| 1408 | } |
| 1409 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1410 | bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1411 | const LangOptions &Lang = C.getLangOpts(); |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1412 | |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1413 | if (!Lang.CPlusPlus) |
| 1414 | return false; |
| 1415 | |
| 1416 | // In C++11, any variable of reference type can be used in a constant |
| 1417 | // expression if it is initialized by a constant expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1418 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1419 | return true; |
| 1420 | |
| 1421 | // Only const objects can be used in constant expressions in C++. C++98 does |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1422 | // not require the variable to be non-volatile, but we consider this to be a |
| 1423 | // defect. |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1424 | if (!getType().isConstQualified() || getType().isVolatileQualified()) |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1425 | return false; |
| 1426 | |
| 1427 | // In C++, const, non-volatile variables of integral or enumeration types |
| 1428 | // can be used in constant expressions. |
| 1429 | if (getType()->isIntegralOrEnumerationType()) |
| 1430 | return true; |
| 1431 | |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 1432 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 1433 | // constant expressions. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1434 | return Lang.CPlusPlus11 && isConstexpr(); |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1437 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 1438 | /// form, which contains extra information on the evaluated value of the |
| 1439 | /// initializer. |
| 1440 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
| 1441 | EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
| 1442 | if (!Eval) { |
| 1443 | Stmt *S = Init.get<Stmt *>(); |
| 1444 | Eval = new (getASTContext()) EvaluatedStmt; |
| 1445 | Eval->Value = S; |
| 1446 | Init = Eval; |
| 1447 | } |
| 1448 | return Eval; |
| 1449 | } |
| 1450 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1451 | APValue *VarDecl::evaluateValue() const { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1452 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1453 | return evaluateValue(Notes); |
| 1454 | } |
| 1455 | |
| 1456 | APValue *VarDecl::evaluateValue( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1457 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1458 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 1459 | |
| 1460 | // We only produce notes indicating why an initializer is non-constant the |
| 1461 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 1462 | // first time we try evaluation, so might not be produced at all. |
| 1463 | if (Eval->WasEvaluated) |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1464 | return Eval->Evaluated.isUninit() ? 0 : &Eval->Evaluated; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1465 | |
| 1466 | const Expr *Init = cast<Expr>(Eval->Value); |
| 1467 | assert(!Init->isValueDependent()); |
| 1468 | |
| 1469 | if (Eval->IsEvaluating) { |
| 1470 | // FIXME: Produce a diagnostic for self-initialization. |
| 1471 | Eval->CheckedICE = true; |
| 1472 | Eval->IsICE = false; |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1473 | return 0; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | Eval->IsEvaluating = true; |
| 1477 | |
| 1478 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), |
| 1479 | this, Notes); |
| 1480 | |
| 1481 | // Ensure the result is an uninitialized APValue if evaluation fails. |
| 1482 | if (!Result) |
| 1483 | Eval->Evaluated = APValue(); |
| 1484 | |
| 1485 | Eval->IsEvaluating = false; |
| 1486 | Eval->WasEvaluated = true; |
| 1487 | |
| 1488 | // In C++11, we have determined whether the initializer was a constant |
| 1489 | // expression as a side-effect. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1490 | if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1491 | Eval->CheckedICE = true; |
Eli Friedman | 210386e | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 1492 | Eval->IsICE = Result && Notes.empty(); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 1495 | return Result ? &Eval->Evaluated : 0; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | bool VarDecl::checkInitIsICE() const { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 1499 | // Initializers of weak variables are never ICEs. |
| 1500 | if (isWeak()) |
| 1501 | return false; |
| 1502 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1503 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 1504 | if (Eval->CheckedICE) |
| 1505 | // We have already checked whether this subexpression is an |
| 1506 | // integral constant expression. |
| 1507 | return Eval->IsICE; |
| 1508 | |
| 1509 | const Expr *Init = cast<Expr>(Eval->Value); |
| 1510 | assert(!Init->isValueDependent()); |
| 1511 | |
| 1512 | // In C++11, evaluate the initializer to check whether it's a constant |
| 1513 | // expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1514 | if (getASTContext().getLangOpts().CPlusPlus11) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1515 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 1516 | evaluateValue(Notes); |
| 1517 | return Eval->IsICE; |
| 1518 | } |
| 1519 | |
| 1520 | // It's an ICE whether or not the definition we found is |
| 1521 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 1522 | // 6206 for details. |
| 1523 | |
| 1524 | if (Eval->CheckingICE) |
| 1525 | return false; |
| 1526 | Eval->CheckingICE = true; |
| 1527 | |
| 1528 | Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); |
| 1529 | Eval->CheckingICE = false; |
| 1530 | Eval->CheckedICE = true; |
| 1531 | return Eval->IsICE; |
| 1532 | } |
| 1533 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1534 | bool VarDecl::extendsLifetimeOfTemporary() const { |
Douglas Gregor | 0b58108 | 2011-06-21 18:20:46 +0000 | [diff] [blame] | 1535 | assert(getType()->isReferenceType() &&"Non-references never extend lifetime"); |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1536 | |
| 1537 | const Expr *E = getInit(); |
| 1538 | if (!E) |
| 1539 | return false; |
| 1540 | |
| 1541 | if (const ExprWithCleanups *Cleanups = dyn_cast<ExprWithCleanups>(E)) |
| 1542 | E = Cleanups->getSubExpr(); |
| 1543 | |
| 1544 | return isa<MaterializeTemporaryExpr>(E); |
| 1545 | } |
| 1546 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1547 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1548 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1549 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| 1550 | |
| 1551 | return 0; |
| 1552 | } |
| 1553 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 1554 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1555 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1556 | return MSI->getTemplateSpecializationKind(); |
| 1557 | |
| 1558 | return TSK_Undeclared; |
| 1559 | } |
| 1560 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 1561 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1562 | return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 1563 | } |
| 1564 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1565 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 1566 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 1567 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 1568 | assert(MSI && "Not an instantiated static data member?"); |
| 1569 | MSI->setTemplateSpecializationKind(TSK); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 1570 | if (TSK != TSK_ExplicitSpecialization && |
| 1571 | PointOfInstantiation.isValid() && |
| 1572 | MSI->getPointOfInstantiation().isInvalid()) |
| 1573 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1576 | //===----------------------------------------------------------------------===// |
| 1577 | // ParmVarDecl Implementation |
| 1578 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1579 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1580 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1581 | SourceLocation StartLoc, |
| 1582 | SourceLocation IdLoc, IdentifierInfo *Id, |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1583 | QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1584 | StorageClass S, StorageClass SCAsWritten, |
| 1585 | Expr *DefArg) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1586 | return new (C) ParmVarDecl(ParmVar, DC, StartLoc, IdLoc, Id, T, TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1587 | S, SCAsWritten, DefArg); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1590 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1591 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ParmVarDecl)); |
| 1592 | return new (Mem) ParmVarDecl(ParmVar, 0, SourceLocation(), SourceLocation(), |
| 1593 | 0, QualType(), 0, SC_None, SC_None, 0); |
| 1594 | } |
| 1595 | |
Argyrios Kyrtzidis | 0bfe83b | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 1596 | SourceRange ParmVarDecl::getSourceRange() const { |
| 1597 | if (!hasInheritedDefaultArg()) { |
| 1598 | SourceRange ArgRange = getDefaultArgRange(); |
| 1599 | if (ArgRange.isValid()) |
| 1600 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 1601 | } |
| 1602 | |
| 1603 | return DeclaratorDecl::getSourceRange(); |
| 1604 | } |
| 1605 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1606 | Expr *ParmVarDecl::getDefaultArg() { |
| 1607 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 1608 | assert(!hasUninstantiatedDefaultArg() && |
| 1609 | "Default argument is not yet instantiated!"); |
| 1610 | |
| 1611 | Expr *Arg = getInit(); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1612 | if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg)) |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1613 | return E->getSubExpr(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 1614 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1615 | return Arg; |
| 1616 | } |
| 1617 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1618 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 1619 | if (const Expr *E = getInit()) |
| 1620 | return E->getSourceRange(); |
| 1621 | |
| 1622 | if (hasUninstantiatedDefaultArg()) |
| 1623 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 1624 | |
| 1625 | return SourceRange(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 1628 | bool ParmVarDecl::isParameterPack() const { |
| 1629 | return isa<PackExpansionType>(getType()); |
| 1630 | } |
| 1631 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 1632 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 1633 | getASTContext().setParameterIndex(this, parameterIndex); |
| 1634 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 1635 | } |
| 1636 | |
| 1637 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 1638 | return getASTContext().getParameterIndex(this); |
| 1639 | } |
| 1640 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1641 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1642 | // FunctionDecl Implementation |
| 1643 | //===----------------------------------------------------------------------===// |
| 1644 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 1645 | void FunctionDecl::getNameForDiagnostic(std::string &S, |
| 1646 | const PrintingPolicy &Policy, |
| 1647 | bool Qualified) const { |
| 1648 | NamedDecl::getNameForDiagnostic(S, Policy, Qualified); |
| 1649 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 1650 | if (TemplateArgs) |
| 1651 | S += TemplateSpecializationType::PrintTemplateArgumentList( |
| 1652 | TemplateArgs->data(), |
| 1653 | TemplateArgs->size(), |
| 1654 | Policy); |
| 1655 | |
| 1656 | } |
| 1657 | |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 1658 | bool FunctionDecl::isVariadic() const { |
| 1659 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 1660 | return FT->isVariadic(); |
| 1661 | return false; |
| 1662 | } |
| 1663 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1664 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| 1665 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 1666 | if (I->Body || I->IsLateTemplateParsed) { |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1667 | Definition = *I; |
| 1668 | return true; |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | return false; |
| 1673 | } |
| 1674 | |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 1675 | bool FunctionDecl::hasTrivialBody() const |
| 1676 | { |
| 1677 | Stmt *S = getBody(); |
| 1678 | if (!S) { |
| 1679 | // Since we don't have a body for this function, we don't know if it's |
| 1680 | // trivial or not. |
| 1681 | return false; |
| 1682 | } |
| 1683 | |
| 1684 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 1685 | return true; |
| 1686 | return false; |
| 1687 | } |
| 1688 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1689 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { |
| 1690 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
Sean Hunt | cd10dec | 2011-05-23 23:14:04 +0000 | [diff] [blame] | 1691 | if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed) { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 1692 | Definition = I->IsDeleted ? I->getCanonicalDecl() : *I; |
| 1693 | return true; |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | return false; |
| 1698 | } |
| 1699 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 1700 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Argyrios Kyrtzidis | c37929c | 2009-07-14 03:20:21 +0000 | [diff] [blame] | 1701 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) { |
| 1702 | if (I->Body) { |
| 1703 | Definition = *I; |
| 1704 | return I->Body.get(getASTContext().getExternalSource()); |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 1705 | } else if (I->IsLateTemplateParsed) { |
| 1706 | Definition = *I; |
| 1707 | return 0; |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1714 | void FunctionDecl::setBody(Stmt *B) { |
| 1715 | Body = B; |
Douglas Gregor | b5f35ba | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 1716 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1717 | EndRangeLoc = B->getLocEnd(); |
| 1718 | } |
| 1719 | |
Douglas Gregor | 2138664 | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 1720 | void FunctionDecl::setPure(bool P) { |
| 1721 | IsPure = P; |
| 1722 | if (P) |
| 1723 | if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| 1724 | Parent->markedVirtualFunctionPure(); |
| 1725 | } |
| 1726 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1727 | bool FunctionDecl::isMain() const { |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 1728 | const TranslationUnitDecl *tunit = |
| 1729 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 1730 | return tunit && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1731 | !tunit->getASTContext().getLangOpts().Freestanding && |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 1732 | getIdentifier() && |
| 1733 | getIdentifier()->isStr("main"); |
| 1734 | } |
| 1735 | |
| 1736 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 1737 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 1738 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 1739 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 1740 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 1741 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 1742 | |
| 1743 | if (isa<CXXRecordDecl>(getDeclContext())) return false; |
| 1744 | assert(getDeclContext()->getRedeclContext()->isTranslationUnit()); |
| 1745 | |
| 1746 | const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>(); |
| 1747 | if (proto->getNumArgs() != 2 || proto->isVariadic()) return false; |
| 1748 | |
| 1749 | ASTContext &Context = |
| 1750 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 1751 | ->getASTContext(); |
| 1752 | |
| 1753 | // The result type and first argument type are constant across all |
| 1754 | // these operators. The second argument must be exactly void*. |
| 1755 | return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1758 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
Rafael Espindola | 6dcea67 | 2013-01-12 15:27:44 +0000 | [diff] [blame] | 1759 | // Users expect to be able to write |
| 1760 | // extern "C" void *__builtin_alloca (size_t); |
| 1761 | // so consider builtins as having C language linkage. |
Rafael Espindola | 508276c | 2013-01-12 15:27:43 +0000 | [diff] [blame] | 1762 | if (getBuiltinID()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1763 | return CLanguageLinkage; |
Rafael Espindola | 508276c | 2013-01-12 15:27:43 +0000 | [diff] [blame] | 1764 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1765 | return getLanguageLinkageTemplate(*this); |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1768 | bool FunctionDecl::isGlobal() const { |
| 1769 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 1770 | return Method->isStatic(); |
| 1771 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1772 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1773 | return false; |
| 1774 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1776 | DC->isNamespace(); |
| 1777 | DC = DC->getParent()) { |
| 1778 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 1779 | if (!Namespace->getDeclName()) |
| 1780 | return false; |
| 1781 | break; |
| 1782 | } |
| 1783 | } |
| 1784 | |
| 1785 | return true; |
| 1786 | } |
| 1787 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1788 | bool FunctionDecl::isNoReturn() const { |
| 1789 | return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 1790 | hasAttr<C11NoReturnAttr>() || |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1791 | getType()->getAs<FunctionType>()->getNoReturnAttr(); |
| 1792 | } |
| 1793 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1794 | void |
| 1795 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 1796 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 1797 | |
| 1798 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 1799 | FunctionTemplateDecl *PrevFunTmpl |
| 1800 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 1801 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 1802 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 1803 | } |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1804 | |
Axel Naumann | d9d137e | 2011-11-08 18:21:06 +0000 | [diff] [blame] | 1805 | if (PrevDecl && PrevDecl->IsInline) |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1806 | IsInline = true; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 1810 | return getFirstDeclaration(); |
| 1811 | } |
| 1812 | |
| 1813 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 1814 | return getFirstDeclaration(); |
| 1815 | } |
| 1816 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1817 | void FunctionDecl::setStorageClass(StorageClass SC) { |
| 1818 | assert(isLegalForFunction(SC)); |
| 1819 | if (getStorageClass() != SC) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1820 | ClearLinkageCache(); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1821 | |
| 1822 | SClass = SC; |
| 1823 | } |
| 1824 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1825 | /// \brief Returns a value indicating whether this function |
| 1826 | /// corresponds to a builtin function. |
| 1827 | /// |
| 1828 | /// The function corresponds to a built-in function if it is |
| 1829 | /// declared at translation scope or within an extern "C" block and |
| 1830 | /// its name matches with the name of a builtin. The returned value |
| 1831 | /// will be 0 for functions that do not correspond to a builtin, a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1833 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1834 | unsigned FunctionDecl::getBuiltinID() const { |
Daniel Dunbar | 60d302a | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 1835 | if (!getIdentifier()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1836 | return 0; |
| 1837 | |
| 1838 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
Daniel Dunbar | 60d302a | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 1839 | if (!BuiltinID) |
| 1840 | return 0; |
| 1841 | |
| 1842 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1843 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 1844 | return BuiltinID; |
| 1845 | |
| 1846 | // This function has the name of a known C library |
| 1847 | // function. Determine whether it actually refers to the C library |
| 1848 | // function or whether it just has the same name. |
| 1849 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1850 | // If this is a static function, it's not a builtin. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1851 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1852 | return 0; |
| 1853 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1854 | // If this function is at translation-unit scope and we're not in |
| 1855 | // C++, it refers to the C library function. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1856 | if (!Context.getLangOpts().CPlusPlus && |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1857 | getDeclContext()->isTranslationUnit()) |
| 1858 | return BuiltinID; |
| 1859 | |
| 1860 | // If the function is in an extern "C" linkage specification and is |
| 1861 | // not marked "overloadable", it's the real function. |
| 1862 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1863 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1864 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1865 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1866 | return BuiltinID; |
| 1867 | |
| 1868 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1873 | /// getNumParams - Return the number of parameters this function must have |
Bob Wilson | 8dbfbf4 | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 1874 | /// based on its FunctionType. This is the length of the ParamInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1875 | /// after it has been created. |
| 1876 | unsigned FunctionDecl::getNumParams() const { |
Eli Friedman | 482466b | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 1877 | const FunctionType *FT = getType()->castAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1878 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 1879 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1880 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1881 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1884 | void FunctionDecl::setParams(ASTContext &C, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1885 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1886 | assert(ParamInfo == 0 && "Already has param info!"); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1887 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1888 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1889 | // Zero params -> null pointer. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1890 | if (!NewParamInfo.empty()) { |
| 1891 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 1892 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1896 | void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) { |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 1897 | assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!"); |
| 1898 | |
| 1899 | if (!NewDecls.empty()) { |
| 1900 | NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()]; |
| 1901 | std::copy(NewDecls.begin(), NewDecls.end(), A); |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1902 | DeclsInPrototypeScope = ArrayRef<NamedDecl *>(A, NewDecls.size()); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 1903 | } |
| 1904 | } |
| 1905 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1906 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 1907 | /// needed to call this function. This may be fewer than the number of |
| 1908 | /// function parameters, if some of the parameters have default |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1909 | /// arguments (in C++) or the last parameter is a parameter pack. |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1910 | unsigned FunctionDecl::getMinRequiredArguments() const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1911 | if (!getASTContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1912 | return getNumParams(); |
| 1913 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1914 | unsigned NumRequiredArgs = getNumParams(); |
| 1915 | |
| 1916 | // If the last parameter is a parameter pack, we don't need an argument for |
| 1917 | // it. |
| 1918 | if (NumRequiredArgs > 0 && |
| 1919 | getParamDecl(NumRequiredArgs - 1)->isParameterPack()) |
| 1920 | --NumRequiredArgs; |
| 1921 | |
| 1922 | // If this parameter has a default argument, we don't need an argument for |
| 1923 | // it. |
| 1924 | while (NumRequiredArgs > 0 && |
| 1925 | getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1926 | --NumRequiredArgs; |
| 1927 | |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1928 | // We might have parameter packs before the end. These can't be deduced, |
| 1929 | // but they can still handle multiple arguments. |
| 1930 | unsigned ArgIdx = NumRequiredArgs; |
| 1931 | while (ArgIdx > 0) { |
| 1932 | if (getParamDecl(ArgIdx - 1)->isParameterPack()) |
| 1933 | NumRequiredArgs = ArgIdx; |
| 1934 | |
| 1935 | --ArgIdx; |
| 1936 | } |
| 1937 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1938 | return NumRequiredArgs; |
| 1939 | } |
| 1940 | |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1941 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 1942 | // Only consider file-scope declarations in this test. |
| 1943 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 1944 | return false; |
| 1945 | |
| 1946 | // Only consider explicit declarations; the presence of a builtin for a |
| 1947 | // libcall shouldn't affect whether a definition is externally visible. |
| 1948 | if (Redecl->isImplicit()) |
| 1949 | return false; |
| 1950 | |
| 1951 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| 1952 | return true; // Not an inline definition |
| 1953 | |
| 1954 | return false; |
| 1955 | } |
| 1956 | |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1957 | /// \brief For a function declaration in C or C++, determine whether this |
| 1958 | /// declaration causes the definition to be externally visible. |
| 1959 | /// |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1960 | /// Specifically, this determines if adding the current declaration to the set |
| 1961 | /// of redeclarations of the given functions causes |
| 1962 | /// isInlineDefinitionExternallyVisible to change from false to true. |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1963 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| 1964 | assert(!doesThisDeclarationHaveABody() && |
| 1965 | "Must have a declaration without a body."); |
| 1966 | |
| 1967 | ASTContext &Context = getASTContext(); |
| 1968 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1969 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1970 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 1971 | // an externally visible definition. |
| 1972 | // |
| 1973 | // FIXME: What happens if gnu_inline gets added on after the first |
| 1974 | // declaration? |
| 1975 | if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern) |
| 1976 | return false; |
| 1977 | |
| 1978 | const FunctionDecl *Prev = this; |
| 1979 | bool FoundBody = false; |
| 1980 | while ((Prev = Prev->getPreviousDecl())) { |
| 1981 | FoundBody |= Prev->Body; |
| 1982 | |
| 1983 | if (Prev->Body) { |
| 1984 | // If it's not the case that both 'inline' and 'extern' are |
| 1985 | // specified on the definition, then it is always externally visible. |
| 1986 | if (!Prev->isInlineSpecified() || |
| 1987 | Prev->getStorageClassAsWritten() != SC_Extern) |
| 1988 | return false; |
| 1989 | } else if (Prev->isInlineSpecified() && |
| 1990 | Prev->getStorageClassAsWritten() != SC_Extern) { |
| 1991 | return false; |
| 1992 | } |
| 1993 | } |
| 1994 | return FoundBody; |
| 1995 | } |
| 1996 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1997 | if (Context.getLangOpts().CPlusPlus) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1998 | return false; |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1999 | |
| 2000 | // C99 6.7.4p6: |
| 2001 | // [...] If all of the file scope declarations for a function in a |
| 2002 | // translation unit include the inline function specifier without extern, |
| 2003 | // then the definition in that translation unit is an inline definition. |
| 2004 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2005 | return false; |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2006 | const FunctionDecl *Prev = this; |
| 2007 | bool FoundBody = false; |
| 2008 | while ((Prev = Prev->getPreviousDecl())) { |
| 2009 | FoundBody |= Prev->Body; |
| 2010 | if (RedeclForcesDefC99(Prev)) |
| 2011 | return false; |
| 2012 | } |
| 2013 | return FoundBody; |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2016 | /// \brief For an inline function definition in C, or for a gnu_inline function |
| 2017 | /// in C++, determine whether the definition will be externally visible. |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2018 | /// |
| 2019 | /// Inline function definitions are always available for inlining optimizations. |
| 2020 | /// However, depending on the language dialect, declaration specifiers, and |
| 2021 | /// attributes, the definition of an inline function may or may not be |
| 2022 | /// "externally" visible to other translation units in the program. |
| 2023 | /// |
| 2024 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 1e5fd7f | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 2025 | /// if even one of the global-scope declarations is marked "extern inline", the |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2026 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 2027 | /// |
| 2028 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 2029 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 2030 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 2031 | /// an externally visible symbol, but "extern inline" will not create an |
| 2032 | /// externally visible symbol. |
| 2033 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2034 | assert(doesThisDeclarationHaveABody() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2035 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 2036 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2037 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2038 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2039 | // Note: If you change the logic here, please change |
| 2040 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 2041 | // |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2042 | // If it's not the case that both 'inline' and 'extern' are |
| 2043 | // specified on the definition, then this inline definition is |
| 2044 | // externally visible. |
| 2045 | if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern)) |
| 2046 | return true; |
| 2047 | |
| 2048 | // If any declaration is 'inline' but not 'extern', then this definition |
| 2049 | // is externally visible. |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2050 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 2051 | Redecl != RedeclEnd; |
| 2052 | ++Redecl) { |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2053 | if (Redecl->isInlineSpecified() && |
| 2054 | Redecl->getStorageClassAsWritten() != SC_Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2055 | return true; |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2056 | } |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2057 | |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2058 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2059 | } |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2060 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2061 | // The rest of this function is C-only. |
| 2062 | assert(!Context.getLangOpts().CPlusPlus && |
| 2063 | "should not use C inline rules in C++"); |
| 2064 | |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2065 | // C99 6.7.4p6: |
| 2066 | // [...] If all of the file scope declarations for a function in a |
| 2067 | // translation unit include the inline function specifier without extern, |
| 2068 | // then the definition in that translation unit is an inline definition. |
| 2069 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 2070 | Redecl != RedeclEnd; |
| 2071 | ++Redecl) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2072 | if (RedeclForcesDefC99(*Redecl)) |
| 2073 | return true; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | // C99 6.7.4p6: |
| 2077 | // An inline definition does not provide an external definition for the |
| 2078 | // function, and does not forbid an external definition in another |
| 2079 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2080 | return false; |
| 2081 | } |
| 2082 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2083 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 2084 | /// function represents, if any. |
| 2085 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 2086 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 2087 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2088 | else |
| 2089 | return OO_None; |
| 2090 | } |
| 2091 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2092 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 2093 | /// represents, if any. |
| 2094 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 2095 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 2096 | return getDeclName().getCXXLiteralIdentifier(); |
| 2097 | else |
| 2098 | return 0; |
| 2099 | } |
| 2100 | |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2101 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 2102 | if (TemplateOrSpecialization.isNull()) |
| 2103 | return TK_NonTemplate; |
| 2104 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 2105 | return TK_FunctionTemplate; |
| 2106 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 2107 | return TK_MemberSpecialization; |
| 2108 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 2109 | return TK_FunctionTemplateSpecialization; |
| 2110 | if (TemplateOrSpecialization.is |
| 2111 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 2112 | return TK_DependentFunctionTemplateSpecialization; |
| 2113 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2114 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2117 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2118 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2119 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 2120 | |
| 2121 | return 0; |
| 2122 | } |
| 2123 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2124 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 2125 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 2126 | } |
| 2127 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2128 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2129 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 2130 | FunctionDecl *FD, |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2131 | TemplateSpecializationKind TSK) { |
| 2132 | assert(TemplateOrSpecialization.isNull() && |
| 2133 | "Member function is already a specialization"); |
| 2134 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2135 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2136 | TemplateOrSpecialization = Info; |
| 2137 | } |
| 2138 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2139 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2140 | // If the function is invalid, it can't be implicitly instantiated. |
| 2141 | if (isInvalidDecl()) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2142 | return false; |
| 2143 | |
| 2144 | switch (getTemplateSpecializationKind()) { |
| 2145 | case TSK_Undeclared: |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2146 | case TSK_ExplicitInstantiationDefinition: |
| 2147 | return false; |
| 2148 | |
| 2149 | case TSK_ImplicitInstantiation: |
| 2150 | return true; |
| 2151 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2152 | // It is possible to instantiate TSK_ExplicitSpecialization kind |
| 2153 | // if the FunctionDecl has a class scope specialization pattern. |
| 2154 | case TSK_ExplicitSpecialization: |
| 2155 | return getClassScopeSpecializationPattern() != 0; |
| 2156 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2157 | case TSK_ExplicitInstantiationDeclaration: |
| 2158 | // Handled below. |
| 2159 | break; |
| 2160 | } |
| 2161 | |
| 2162 | // Find the actual template from which we will instantiate. |
| 2163 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2164 | bool HasPattern = false; |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2165 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2166 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2167 | |
| 2168 | // C++0x [temp.explicit]p9: |
| 2169 | // Except for inline functions, other explicit instantiation declarations |
| 2170 | // have the effect of suppressing the implicit instantiation of the entity |
| 2171 | // to which they refer. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2172 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2173 | return true; |
| 2174 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2175 | return PatternDecl->isInlined(); |
Ted Kremenek | 75df4ee | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
| 2178 | bool FunctionDecl::isTemplateInstantiation() const { |
| 2179 | switch (getTemplateSpecializationKind()) { |
| 2180 | case TSK_Undeclared: |
| 2181 | case TSK_ExplicitSpecialization: |
| 2182 | return false; |
| 2183 | case TSK_ImplicitInstantiation: |
| 2184 | case TSK_ExplicitInstantiationDeclaration: |
| 2185 | case TSK_ExplicitInstantiationDefinition: |
| 2186 | return true; |
| 2187 | } |
| 2188 | llvm_unreachable("All TSK values handled."); |
| 2189 | } |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2190 | |
| 2191 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2192 | // Handle class scope explicit specialization special case. |
| 2193 | if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 2194 | return getClassScopeSpecializationPattern(); |
| 2195 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2196 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 2197 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 2198 | // If we have hit a point where the user provided a specialization of |
| 2199 | // this template, we're done looking. |
| 2200 | if (Primary->isMemberSpecialization()) |
| 2201 | break; |
| 2202 | |
| 2203 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 2204 | } |
| 2205 | |
| 2206 | return Primary->getTemplatedDecl(); |
| 2207 | } |
| 2208 | |
| 2209 | return getInstantiatedFromMemberFunction(); |
| 2210 | } |
| 2211 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2212 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2213 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2214 | = TemplateOrSpecialization |
| 2215 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2216 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2217 | } |
| 2218 | return 0; |
| 2219 | } |
| 2220 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2221 | FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const { |
| 2222 | return getASTContext().getClassScopeSpecializationPattern(this); |
| 2223 | } |
| 2224 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2225 | const TemplateArgumentList * |
| 2226 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2227 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2228 | = TemplateOrSpecialization |
| 2229 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2230 | return Info->TemplateArguments; |
| 2231 | } |
| 2232 | return 0; |
| 2233 | } |
| 2234 | |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 2235 | const ASTTemplateArgumentListInfo * |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2236 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 2237 | if (FunctionTemplateSpecializationInfo *Info |
| 2238 | = TemplateOrSpecialization |
| 2239 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 2240 | return Info->TemplateArgumentsAsWritten; |
| 2241 | } |
| 2242 | return 0; |
| 2243 | } |
| 2244 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2245 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2246 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 2247 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 2248 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2249 | void *InsertPos, |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2250 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 7b081c8 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 2251 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 2252 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2253 | assert(TSK != TSK_Undeclared && |
| 2254 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2256 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2257 | if (!Info) |
Argyrios Kyrtzidis | a626a3d | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 2258 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 2259 | TemplateArgs, |
| 2260 | TemplateArgsAsWritten, |
| 2261 | PointOfInstantiation); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2262 | TemplateOrSpecialization = Info; |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 2263 | Template->addSpecialization(Info, InsertPos); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2266 | void |
| 2267 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 2268 | const UnresolvedSetImpl &Templates, |
| 2269 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2270 | assert(TemplateOrSpecialization.isNull()); |
| 2271 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 2272 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 21c0160 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 2273 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2274 | void *Buffer = Context.Allocate(Size); |
| 2275 | DependentFunctionTemplateSpecializationInfo *Info = |
| 2276 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 2277 | TemplateArgs); |
| 2278 | TemplateOrSpecialization = Info; |
| 2279 | } |
| 2280 | |
| 2281 | DependentFunctionTemplateSpecializationInfo:: |
| 2282 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 2283 | const TemplateArgumentListInfo &TArgs) |
| 2284 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 2285 | |
| 2286 | d.NumTemplates = Ts.size(); |
| 2287 | d.NumArgs = TArgs.size(); |
| 2288 | |
| 2289 | FunctionTemplateDecl **TsArray = |
| 2290 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 2291 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 2292 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 2293 | |
| 2294 | TemplateArgumentLoc *ArgsArray = |
| 2295 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 2296 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 2297 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 2298 | } |
| 2299 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2300 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2302 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2303 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2304 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2305 | if (FTSInfo) |
| 2306 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2308 | MemberSpecializationInfo *MSInfo |
| 2309 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 2310 | if (MSInfo) |
| 2311 | return MSInfo->getTemplateSpecializationKind(); |
| 2312 | |
| 2313 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2316 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2317 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2318 | SourceLocation PointOfInstantiation) { |
| 2319 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 2320 | = TemplateOrSpecialization.dyn_cast< |
| 2321 | FunctionTemplateSpecializationInfo*>()) { |
| 2322 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 2323 | if (TSK != TSK_ExplicitSpecialization && |
| 2324 | PointOfInstantiation.isValid() && |
| 2325 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 2326 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2327 | } else if (MemberSpecializationInfo *MSInfo |
| 2328 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 2329 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2330 | if (TSK != TSK_ExplicitSpecialization && |
| 2331 | PointOfInstantiation.isValid() && |
| 2332 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 2333 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2334 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2335 | llvm_unreachable("Function cannot have a template specialization kind"); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2336 | } |
| 2337 | |
| 2338 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2339 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 2340 | = TemplateOrSpecialization.dyn_cast< |
| 2341 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2342 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2343 | else if (MemberSpecializationInfo *MSInfo |
| 2344 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2345 | return MSInfo->getPointOfInstantiation(); |
| 2346 | |
| 2347 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2348 | } |
| 2349 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2350 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2351 | if (Decl::isOutOfLine()) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2352 | return true; |
| 2353 | |
| 2354 | // If this function was instantiated from a member function of a |
| 2355 | // class template, check whether that member function was defined out-of-line. |
| 2356 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 2357 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2358 | if (FD->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2359 | return Definition->isOutOfLine(); |
| 2360 | } |
| 2361 | |
| 2362 | // If this function was instantiated from a function template, |
| 2363 | // check whether that function template was defined out-of-line. |
| 2364 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 2365 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2366 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2367 | return Definition->isOutOfLine(); |
| 2368 | } |
| 2369 | |
| 2370 | return false; |
| 2371 | } |
| 2372 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2373 | SourceRange FunctionDecl::getSourceRange() const { |
| 2374 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 2375 | } |
| 2376 | |
Anna Zaks | 9392d4e | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 2377 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2378 | IdentifierInfo *FnInfo = getIdentifier(); |
| 2379 | |
| 2380 | if (!FnInfo) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2381 | return 0; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2382 | |
| 2383 | // Builtin handling. |
| 2384 | switch (getBuiltinID()) { |
| 2385 | case Builtin::BI__builtin_memset: |
| 2386 | case Builtin::BI__builtin___memset_chk: |
| 2387 | case Builtin::BImemset: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2388 | return Builtin::BImemset; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2389 | |
| 2390 | case Builtin::BI__builtin_memcpy: |
| 2391 | case Builtin::BI__builtin___memcpy_chk: |
| 2392 | case Builtin::BImemcpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2393 | return Builtin::BImemcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2394 | |
| 2395 | case Builtin::BI__builtin_memmove: |
| 2396 | case Builtin::BI__builtin___memmove_chk: |
| 2397 | case Builtin::BImemmove: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2398 | return Builtin::BImemmove; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2399 | |
| 2400 | case Builtin::BIstrlcpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2401 | return Builtin::BIstrlcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2402 | case Builtin::BIstrlcat: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2403 | return Builtin::BIstrlcat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2404 | |
| 2405 | case Builtin::BI__builtin_memcmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2406 | case Builtin::BImemcmp: |
| 2407 | return Builtin::BImemcmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2408 | |
| 2409 | case Builtin::BI__builtin_strncpy: |
| 2410 | case Builtin::BI__builtin___strncpy_chk: |
| 2411 | case Builtin::BIstrncpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2412 | return Builtin::BIstrncpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2413 | |
| 2414 | case Builtin::BI__builtin_strncmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2415 | case Builtin::BIstrncmp: |
| 2416 | return Builtin::BIstrncmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2417 | |
| 2418 | case Builtin::BI__builtin_strncasecmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2419 | case Builtin::BIstrncasecmp: |
| 2420 | return Builtin::BIstrncasecmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2421 | |
| 2422 | case Builtin::BI__builtin_strncat: |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2423 | case Builtin::BI__builtin___strncat_chk: |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2424 | case Builtin::BIstrncat: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2425 | return Builtin::BIstrncat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2426 | |
| 2427 | case Builtin::BI__builtin_strndup: |
| 2428 | case Builtin::BIstrndup: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2429 | return Builtin::BIstrndup; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2430 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2431 | case Builtin::BI__builtin_strlen: |
| 2432 | case Builtin::BIstrlen: |
| 2433 | return Builtin::BIstrlen; |
| 2434 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2435 | default: |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2436 | if (isExternC()) { |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2437 | if (FnInfo->isStr("memset")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2438 | return Builtin::BImemset; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2439 | else if (FnInfo->isStr("memcpy")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2440 | return Builtin::BImemcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2441 | else if (FnInfo->isStr("memmove")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2442 | return Builtin::BImemmove; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2443 | else if (FnInfo->isStr("memcmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2444 | return Builtin::BImemcmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2445 | else if (FnInfo->isStr("strncpy")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2446 | return Builtin::BIstrncpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2447 | else if (FnInfo->isStr("strncmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2448 | return Builtin::BIstrncmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2449 | else if (FnInfo->isStr("strncasecmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2450 | return Builtin::BIstrncasecmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2451 | else if (FnInfo->isStr("strncat")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2452 | return Builtin::BIstrncat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2453 | else if (FnInfo->isStr("strndup")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2454 | return Builtin::BIstrndup; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2455 | else if (FnInfo->isStr("strlen")) |
| 2456 | return Builtin::BIstrlen; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2457 | } |
| 2458 | break; |
| 2459 | } |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2460 | return 0; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2463 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2464 | // FieldDecl Implementation |
| 2465 | //===----------------------------------------------------------------------===// |
| 2466 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2467 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2468 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2469 | IdentifierInfo *Id, QualType T, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2470 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2471 | InClassInitStyle InitStyle) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2472 | return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2473 | BW, Mutable, InitStyle); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2476 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2477 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl)); |
| 2478 | return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2479 | 0, QualType(), 0, 0, false, ICIS_NoInit); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2482 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 2483 | if (!isImplicit() || getDeclName()) |
| 2484 | return false; |
| 2485 | |
| 2486 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 2487 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 2488 | |
| 2489 | return false; |
| 2490 | } |
| 2491 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2492 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 2493 | assert(isBitField() && "not a bitfield"); |
| 2494 | Expr *BitWidth = InitializerOrBitWidth.getPointer(); |
| 2495 | return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue(); |
| 2496 | } |
| 2497 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2498 | unsigned FieldDecl::getFieldIndex() const { |
| 2499 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 2500 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2501 | unsigned Index = 0; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2502 | const RecordDecl *RD = getParent(); |
| 2503 | const FieldDecl *LastFD = 0; |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 2504 | bool IsMsStruct = RD->isMsStruct(getASTContext()); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2505 | |
| 2506 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 2507 | I != E; ++I, ++Index) { |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 2508 | I->CachedFieldIndex = Index + 1; |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2509 | |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2510 | if (IsMsStruct) { |
| 2511 | // Zero-length bitfields following non-bitfield members are ignored. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2512 | if (getASTContext().ZeroBitfieldFollowsNonBitfield(*I, LastFD)) { |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2513 | --Index; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2514 | continue; |
| 2515 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2516 | LastFD = *I; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2517 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2520 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 2521 | return CachedFieldIndex - 1; |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Abramo Bagnara | f2cf562 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 2524 | SourceRange FieldDecl::getSourceRange() const { |
Abramo Bagnara | d330e23 | 2011-08-05 08:02:55 +0000 | [diff] [blame] | 2525 | if (const Expr *E = InitializerOrBitWidth.getPointer()) |
| 2526 | return SourceRange(getInnerLocStart(), E->getLocEnd()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2527 | return DeclaratorDecl::getSourceRange(); |
Abramo Bagnara | f2cf562 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Abramo Bagnara | a533576 | 2012-07-02 20:35:48 +0000 | [diff] [blame] | 2530 | void FieldDecl::setBitWidth(Expr *Width) { |
| 2531 | assert(!InitializerOrBitWidth.getPointer() && !hasInClassInitializer() && |
| 2532 | "bit width or initializer already set"); |
| 2533 | InitializerOrBitWidth.setPointer(Width); |
| 2534 | } |
| 2535 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2536 | void FieldDecl::setInClassInitializer(Expr *Init) { |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 2537 | assert(!InitializerOrBitWidth.getPointer() && hasInClassInitializer() && |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2538 | "bit width or initializer already set"); |
| 2539 | InitializerOrBitWidth.setPointer(Init); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2540 | } |
| 2541 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2542 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2543 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2544 | //===----------------------------------------------------------------------===// |
| 2545 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2546 | SourceLocation TagDecl::getOuterLocStart() const { |
| 2547 | return getTemplateOrInnerLocStart(this); |
| 2548 | } |
| 2549 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 2550 | SourceRange TagDecl::getSourceRange() const { |
| 2551 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2552 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 2555 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2556 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2559 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
| 2560 | TypedefNameDeclOrQualifier = TDD; |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 2561 | if (TypeForDecl) |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2562 | const_cast<Type*>(TypeForDecl)->ClearLinkageCache(); |
| 2563 | ClearLinkageCache(); |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2566 | void TagDecl::startDefinition() { |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2567 | IsBeingDefined = true; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2568 | |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 2569 | if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) { |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2570 | struct CXXRecordDecl::DefinitionData *Data = |
| 2571 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 2243288 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 2572 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 2573 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2574 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | void TagDecl::completeDefinition() { |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2578 | assert((!isa<CXXRecordDecl>(this) || |
| 2579 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 2580 | "definition completed but not started"); |
| 2581 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2582 | IsCompleteDefinition = true; |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2583 | IsBeingDefined = false; |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 2584 | |
| 2585 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2586 | L->CompletedTagDefinition(this); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2589 | TagDecl *TagDecl::getDefinition() const { |
| 2590 | if (isCompleteDefinition()) |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2591 | return const_cast<TagDecl *>(this); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2592 | |
| 2593 | // If it's possible for us to have an out-of-date definition, check now. |
| 2594 | if (MayHaveOutOfDateDef) { |
| 2595 | if (IdentifierInfo *II = getIdentifier()) { |
| 2596 | if (II->isOutOfDate()) { |
| 2597 | updateOutOfDate(*II); |
| 2598 | } |
| 2599 | } |
| 2600 | } |
| 2601 | |
Andrew Trick | 220a9c8 | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 2602 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| 2603 | return CXXRD->getDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | |
| 2605 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2606 | R != REnd; ++R) |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2607 | if (R->isCompleteDefinition()) |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2608 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2610 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2613 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 2614 | if (QualifierLoc) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2615 | // Make sure the extended qualifier info is allocated. |
| 2616 | if (!hasExtInfo()) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2617 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2618 | // Set qualifier info. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2619 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2620 | } else { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2621 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2622 | if (hasExtInfo()) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2623 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 2624 | getASTContext().Deallocate(getExtInfo()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2625 | TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2626 | } |
| 2627 | else |
| 2628 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2629 | } |
| 2630 | } |
| 2631 | } |
| 2632 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2633 | void TagDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 2634 | unsigned NumTPLists, |
| 2635 | TemplateParameterList **TPLists) { |
| 2636 | assert(NumTPLists > 0); |
| 2637 | // Make sure the extended decl info is allocated. |
| 2638 | if (!hasExtInfo()) |
| 2639 | // Allocate external info struct. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2640 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2641 | // Set the template parameter lists info. |
| 2642 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 2643 | } |
| 2644 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2645 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2646 | // EnumDecl Implementation |
| 2647 | //===----------------------------------------------------------------------===// |
| 2648 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2649 | void EnumDecl::anchor() { } |
| 2650 | |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2651 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 2652 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2653 | IdentifierInfo *Id, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2654 | EnumDecl *PrevDecl, bool IsScoped, |
| 2655 | bool IsScopedUsingClassTag, bool IsFixed) { |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2656 | EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2657 | IsScoped, IsScopedUsingClassTag, IsFixed); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2658 | Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2659 | C.getTypeDeclType(Enum, PrevDecl); |
| 2660 | return Enum; |
| 2661 | } |
| 2662 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2663 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2664 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl)); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2665 | EnumDecl *Enum = new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), |
| 2666 | 0, 0, false, false, false); |
| 2667 | Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 2668 | return Enum; |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2669 | } |
| 2670 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2671 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2672 | QualType NewPromotionType, |
| 2673 | unsigned NumPositiveBits, |
| 2674 | unsigned NumNegativeBits) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2675 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2676 | if (!IntegerType) |
| 2677 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2678 | PromotionType = NewPromotionType; |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2679 | setNumPositiveBits(NumPositiveBits); |
| 2680 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2681 | TagDecl::completeDefinition(); |
| 2682 | } |
| 2683 | |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2684 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 2685 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2686 | return MSI->getTemplateSpecializationKind(); |
| 2687 | |
| 2688 | return TSK_Undeclared; |
| 2689 | } |
| 2690 | |
| 2691 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2692 | SourceLocation PointOfInstantiation) { |
| 2693 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 2694 | assert(MSI && "Not an instantiated member enumeration?"); |
| 2695 | MSI->setTemplateSpecializationKind(TSK); |
| 2696 | if (TSK != TSK_ExplicitSpecialization && |
| 2697 | PointOfInstantiation.isValid() && |
| 2698 | MSI->getPointOfInstantiation().isInvalid()) |
| 2699 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 2700 | } |
| 2701 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2702 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 2703 | if (SpecializationInfo) |
| 2704 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 2705 | |
| 2706 | return 0; |
| 2707 | } |
| 2708 | |
| 2709 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 2710 | TemplateSpecializationKind TSK) { |
| 2711 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 2712 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 2713 | } |
| 2714 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2715 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2716 | // RecordDecl Implementation |
| 2717 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2718 | |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2719 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, |
| 2720 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2721 | IdentifierInfo *Id, RecordDecl *PrevDecl) |
| 2722 | : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2723 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2724 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2725 | HasObjectMember = false; |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 2726 | HasVolatileMember = false; |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2727 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2728 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2731 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2732 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2733 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
| 2734 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id, |
| 2735 | PrevDecl); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2736 | R->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 2737 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2738 | C.getTypeDeclType(R, PrevDecl); |
| 2739 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2742 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| 2743 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl)); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 2744 | RecordDecl *R = new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), |
| 2745 | SourceLocation(), 0, 0); |
| 2746 | R->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 2747 | return R; |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2750 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2751 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2752 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 2753 | } |
| 2754 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2755 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 2756 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 2757 | LoadFieldsFromExternalStorage(); |
| 2758 | |
| 2759 | return field_iterator(decl_iterator(FirstDecl)); |
| 2760 | } |
| 2761 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2762 | /// completeDefinition - Notes that the definition of this type is now |
| 2763 | /// complete. |
| 2764 | void RecordDecl::completeDefinition() { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2765 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2766 | TagDecl::completeDefinition(); |
| 2767 | } |
| 2768 | |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 2769 | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
| 2770 | /// This which can be turned on with an attribute, pragma, or the |
| 2771 | /// -mms-bitfields command-line option. |
| 2772 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
| 2773 | return hasAttr<MsStructAttr>() || C.getLangOpts().MSBitfields == 1; |
| 2774 | } |
| 2775 | |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 2776 | static bool isFieldOrIndirectField(Decl::Kind K) { |
| 2777 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
| 2778 | } |
| 2779 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2780 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 2781 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 2782 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 2783 | |
| 2784 | // Notify that we have a RecordDecl doing some initialization. |
| 2785 | ExternalASTSource::Deserializing TheFields(Source); |
| 2786 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2787 | SmallVector<Decl*, 64> Decls; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 2788 | LoadedFieldsFromExternalStorage = true; |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 2789 | switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField, |
| 2790 | Decls)) { |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 2791 | case ELR_Success: |
| 2792 | break; |
| 2793 | |
| 2794 | case ELR_AlreadyLoaded: |
| 2795 | case ELR_Failure: |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2796 | return; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 2797 | } |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2798 | |
| 2799 | #ifndef NDEBUG |
| 2800 | // Check that all decls we got were FieldDecls. |
| 2801 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 2802 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2803 | #endif |
| 2804 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2805 | if (Decls.empty()) |
| 2806 | return; |
| 2807 | |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 2808 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
| 2809 | /*FieldsAlreadyLoaded=*/false); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 2812 | //===----------------------------------------------------------------------===// |
| 2813 | // BlockDecl Implementation |
| 2814 | //===----------------------------------------------------------------------===// |
| 2815 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2816 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2817 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2819 | // Zero params -> null pointer. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2820 | if (!NewParamInfo.empty()) { |
| 2821 | NumParams = NewParamInfo.size(); |
| 2822 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 2823 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2824 | } |
| 2825 | } |
| 2826 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2827 | void BlockDecl::setCaptures(ASTContext &Context, |
| 2828 | const Capture *begin, |
| 2829 | const Capture *end, |
| 2830 | bool capturesCXXThis) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2831 | CapturesCXXThis = capturesCXXThis; |
| 2832 | |
| 2833 | if (begin == end) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2834 | NumCaptures = 0; |
| 2835 | Captures = 0; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2836 | return; |
| 2837 | } |
| 2838 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2839 | NumCaptures = end - begin; |
| 2840 | |
| 2841 | // Avoid new Capture[] because we don't want to provide a default |
| 2842 | // constructor. |
| 2843 | size_t allocationSize = NumCaptures * sizeof(Capture); |
| 2844 | void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*)); |
| 2845 | memcpy(buffer, begin, allocationSize); |
| 2846 | Captures = static_cast<Capture*>(buffer); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2847 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2848 | |
John McCall | 204e133 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 2849 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
| 2850 | for (capture_const_iterator |
| 2851 | i = capture_begin(), e = capture_end(); i != e; ++i) |
| 2852 | // Only auto vars can be captured, so no redeclaration worries. |
| 2853 | if (i->getVariable() == variable) |
| 2854 | return true; |
| 2855 | |
| 2856 | return false; |
| 2857 | } |
| 2858 | |
Douglas Gregor | 2fcbcef | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 2859 | SourceRange BlockDecl::getSourceRange() const { |
| 2860 | return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); |
| 2861 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2862 | |
| 2863 | //===----------------------------------------------------------------------===// |
| 2864 | // Other Decl Allocation/Deallocation Method Implementations |
| 2865 | //===----------------------------------------------------------------------===// |
| 2866 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2867 | void TranslationUnitDecl::anchor() { } |
| 2868 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2869 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 2870 | return new (C) TranslationUnitDecl(C); |
| 2871 | } |
| 2872 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2873 | void LabelDecl::anchor() { } |
| 2874 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2875 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 6784304 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 2876 | SourceLocation IdentL, IdentifierInfo *II) { |
| 2877 | return new (C) LabelDecl(DC, IdentL, II, 0, IdentL); |
| 2878 | } |
| 2879 | |
| 2880 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 2881 | SourceLocation IdentL, IdentifierInfo *II, |
| 2882 | SourceLocation GnuLabelL) { |
| 2883 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
| 2884 | return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL); |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2887 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2888 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl)); |
| 2889 | return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation()); |
Douglas Gregor | 06c9193 | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 2890 | } |
| 2891 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2892 | void ValueDecl::anchor() { } |
| 2893 | |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 2894 | bool ValueDecl::isWeak() const { |
| 2895 | for (attr_iterator I = attr_begin(), E = attr_end(); I != E; ++I) |
| 2896 | if (isa<WeakAttr>(*I) || isa<WeakRefAttr>(*I)) |
| 2897 | return true; |
| 2898 | |
| 2899 | return isWeakImported(); |
| 2900 | } |
| 2901 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2902 | void ImplicitParamDecl::anchor() { } |
| 2903 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2904 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2905 | SourceLocation IdLoc, |
| 2906 | IdentifierInfo *Id, |
| 2907 | QualType Type) { |
| 2908 | return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2909 | } |
| 2910 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2911 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
| 2912 | unsigned ID) { |
| 2913 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl)); |
| 2914 | return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType()); |
| 2915 | } |
| 2916 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2917 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2918 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2919 | const DeclarationNameInfo &NameInfo, |
| 2920 | QualType T, TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2921 | StorageClass SC, StorageClass SCAsWritten, |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2922 | bool isInlineSpecified, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2923 | bool hasWrittenPrototype, |
| 2924 | bool isConstexprSpecified) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2925 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo, |
| 2926 | T, TInfo, SC, SCAsWritten, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2927 | isInlineSpecified, |
| 2928 | isConstexprSpecified); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2929 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 2930 | return New; |
| 2931 | } |
| 2932 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2933 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2934 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl)); |
| 2935 | return new (Mem) FunctionDecl(Function, 0, SourceLocation(), |
| 2936 | DeclarationNameInfo(), QualType(), 0, |
| 2937 | SC_None, SC_None, false, false); |
| 2938 | } |
| 2939 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2940 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 2941 | return new (C) BlockDecl(DC, L); |
| 2942 | } |
| 2943 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2944 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2945 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl)); |
| 2946 | return new (Mem) BlockDecl(0, SourceLocation()); |
| 2947 | } |
| 2948 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2949 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 2950 | SourceLocation L, |
| 2951 | IdentifierInfo *Id, QualType T, |
| 2952 | Expr *E, const llvm::APSInt &V) { |
| 2953 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 2954 | } |
| 2955 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2956 | EnumConstantDecl * |
| 2957 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2958 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl)); |
| 2959 | return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0, |
| 2960 | llvm::APSInt()); |
| 2961 | } |
| 2962 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2963 | void IndirectFieldDecl::anchor() { } |
| 2964 | |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 2965 | IndirectFieldDecl * |
| 2966 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 2967 | IdentifierInfo *Id, QualType T, NamedDecl **CH, |
| 2968 | unsigned CHS) { |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2969 | return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS); |
| 2970 | } |
| 2971 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2972 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 2973 | unsigned ID) { |
| 2974 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl)); |
| 2975 | return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(), |
| 2976 | QualType(), 0, 0); |
| 2977 | } |
| 2978 | |
Douglas Gregor | 8e7139c | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 2979 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 2980 | SourceLocation End = getLocation(); |
| 2981 | if (Init) |
| 2982 | End = Init->getLocEnd(); |
| 2983 | return SourceRange(getLocation(), End); |
| 2984 | } |
| 2985 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2986 | void TypeDecl::anchor() { } |
| 2987 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2988 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2989 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2990 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
| 2991 | return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2994 | void TypedefNameDecl::anchor() { } |
| 2995 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2996 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2997 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl)); |
| 2998 | return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0); |
| 2999 | } |
| 3000 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3001 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 3002 | SourceLocation StartLoc, |
| 3003 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 3004 | TypeSourceInfo *TInfo) { |
| 3005 | return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo); |
| 3006 | } |
| 3007 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3008 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 3009 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl)); |
| 3010 | return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0); |
| 3011 | } |
| 3012 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3013 | SourceRange TypedefDecl::getSourceRange() const { |
| 3014 | SourceLocation RangeEnd = getLocation(); |
| 3015 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 3016 | if (typeIsPostfix(TInfo->getType())) |
| 3017 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 3018 | } |
| 3019 | return SourceRange(getLocStart(), RangeEnd); |
| 3020 | } |
| 3021 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3022 | SourceRange TypeAliasDecl::getSourceRange() const { |
| 3023 | SourceLocation RangeEnd = getLocStart(); |
| 3024 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 3025 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 3026 | return SourceRange(getLocStart(), RangeEnd); |
| 3027 | } |
| 3028 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3029 | void FileScopeAsmDecl::anchor() { } |
| 3030 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3031 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 3032 | StringLiteral *Str, |
| 3033 | SourceLocation AsmLoc, |
| 3034 | SourceLocation RParenLoc) { |
| 3035 | return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3036 | } |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3037 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3038 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
| 3039 | unsigned ID) { |
| 3040 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl)); |
| 3041 | return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation()); |
| 3042 | } |
| 3043 | |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3044 | //===----------------------------------------------------------------------===// |
| 3045 | // ImportDecl Implementation |
| 3046 | //===----------------------------------------------------------------------===// |
| 3047 | |
| 3048 | /// \brief Retrieve the number of module identifiers needed to name the given |
| 3049 | /// module. |
| 3050 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 3051 | unsigned Result = 1; |
| 3052 | while (Mod->Parent) { |
| 3053 | Mod = Mod->Parent; |
| 3054 | ++Result; |
| 3055 | } |
| 3056 | return Result; |
| 3057 | } |
| 3058 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3059 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3060 | Module *Imported, |
| 3061 | ArrayRef<SourceLocation> IdentifierLocs) |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3062 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 3063 | NextLocalImport() |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3064 | { |
| 3065 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
| 3066 | SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1); |
| 3067 | memcpy(StoredLocs, IdentifierLocs.data(), |
| 3068 | IdentifierLocs.size() * sizeof(SourceLocation)); |
| 3069 | } |
| 3070 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3071 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3072 | Module *Imported, SourceLocation EndLoc) |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3073 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 3074 | NextLocalImport() |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3075 | { |
| 3076 | *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc; |
| 3077 | } |
| 3078 | |
| 3079 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3080 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3081 | ArrayRef<SourceLocation> IdentifierLocs) { |
| 3082 | void *Mem = C.Allocate(sizeof(ImportDecl) + |
| 3083 | IdentifierLocs.size() * sizeof(SourceLocation)); |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3084 | return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3088 | SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3089 | Module *Imported, |
| 3090 | SourceLocation EndLoc) { |
| 3091 | void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation)); |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3092 | ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3093 | Import->setImplicit(); |
| 3094 | return Import; |
| 3095 | } |
| 3096 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3097 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 3098 | unsigned NumLocations) { |
| 3099 | void *Mem = AllocateDeserializedDecl(C, ID, |
| 3100 | (sizeof(ImportDecl) + |
| 3101 | NumLocations * sizeof(SourceLocation))); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3102 | return new (Mem) ImportDecl(EmptyShell()); |
| 3103 | } |
| 3104 | |
| 3105 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 3106 | if (!ImportedAndComplete.getInt()) |
| 3107 | return ArrayRef<SourceLocation>(); |
| 3108 | |
| 3109 | const SourceLocation *StoredLocs |
| 3110 | = reinterpret_cast<const SourceLocation *>(this + 1); |
| 3111 | return ArrayRef<SourceLocation>(StoredLocs, |
| 3112 | getNumModuleIdentifiers(getImportedModule())); |
| 3113 | } |
| 3114 | |
| 3115 | SourceRange ImportDecl::getSourceRange() const { |
| 3116 | if (!ImportedAndComplete.getInt()) |
| 3117 | return SourceRange(getLocation(), |
| 3118 | *reinterpret_cast<const SourceLocation *>(this + 1)); |
| 3119 | |
| 3120 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 3121 | } |