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