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