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