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 | |
Benjamin Kramer | 752c2e9 | 2010-11-05 19:56:37 +0000 | [diff] [blame] | 69 | namespace { |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 70 | /// Flags controlling the computation of linkage and visibility. |
| 71 | struct LVFlags { |
| 72 | bool ConsiderGlobalVisibility; |
| 73 | bool ConsiderVisibilityAttributes; |
John McCall | 1a0918a | 2011-03-04 10:39:25 +0000 | [diff] [blame] | 74 | bool ConsiderTemplateParameterTypes; |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 75 | |
| 76 | LVFlags() : ConsiderGlobalVisibility(true), |
John McCall | 1a0918a | 2011-03-04 10:39:25 +0000 | [diff] [blame] | 77 | ConsiderVisibilityAttributes(true), |
| 78 | ConsiderTemplateParameterTypes(true) { |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Rafael Espindola | 62d9f11 | 2012-04-16 13:44:41 +0000 | [diff] [blame^] | 81 | LVFlags(bool Global, bool Attributes, bool Parameters) : |
| 82 | ConsiderGlobalVisibility(Global), |
| 83 | ConsiderVisibilityAttributes(Attributes), |
| 84 | ConsiderTemplateParameterTypes(Parameters) { |
| 85 | } |
| 86 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 87 | /// \brief Returns a set of flags that is only useful for computing the |
| 88 | /// linkage, not the visibility, of a declaration. |
| 89 | static LVFlags CreateOnlyDeclLinkage() { |
Rafael Espindola | 62d9f11 | 2012-04-16 13:44:41 +0000 | [diff] [blame^] | 90 | return LVFlags(false, false, false); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 91 | } |
Douglas Gregor | 89d63e5 | 2010-12-06 18:50:56 +0000 | [diff] [blame] | 92 | }; |
Benjamin Kramer | 752c2e9 | 2010-11-05 19:56:37 +0000 | [diff] [blame] | 93 | } // end anonymous namespace |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 94 | |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 95 | static LinkageInfo getLVForType(QualType T) { |
| 96 | std::pair<Linkage,Visibility> P = T->getLinkageAndVisibility(); |
| 97 | return LinkageInfo(P.first, P.second, T->isVisibilityExplicit()); |
| 98 | } |
| 99 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 100 | /// \brief Get the most restrictive linkage for the types in the given |
| 101 | /// template parameter list. |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 102 | static LinkageInfo |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 103 | getLVForTemplateParameterList(const TemplateParameterList *Params) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 104 | LinkageInfo LV(ExternalLinkage, DefaultVisibility, false); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 105 | for (TemplateParameterList::const_iterator P = Params->begin(), |
| 106 | PEnd = Params->end(); |
| 107 | P != PEnd; ++P) { |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 108 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { |
| 109 | if (NTTP->isExpandedParameterPack()) { |
| 110 | for (unsigned I = 0, N = NTTP->getNumExpansionTypes(); I != N; ++I) { |
| 111 | QualType T = NTTP->getExpansionType(I); |
| 112 | if (!T->isDependentType()) |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 113 | LV.merge(getLVForType(T)); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 114 | } |
| 115 | continue; |
| 116 | } |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 118 | if (!NTTP->getType()->isDependentType()) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 119 | LV.merge(getLVForType(NTTP->getType())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 120 | continue; |
| 121 | } |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 122 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 123 | |
| 124 | if (TemplateTemplateParmDecl *TTP |
| 125 | = dyn_cast<TemplateTemplateParmDecl>(*P)) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 126 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 130 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 133 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
| 134 | static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags F); |
| 135 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 136 | /// \brief Get the most restrictive linkage for the types and |
| 137 | /// declarations in the given template argument list. |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 138 | static LinkageInfo getLVForTemplateArgumentList(const TemplateArgument *Args, |
| 139 | unsigned NumArgs, |
| 140 | LVFlags &F) { |
| 141 | LinkageInfo LV(ExternalLinkage, DefaultVisibility, false); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 142 | |
| 143 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 144 | switch (Args[I].getKind()) { |
| 145 | case TemplateArgument::Null: |
| 146 | case TemplateArgument::Integral: |
| 147 | case TemplateArgument::Expression: |
| 148 | break; |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 149 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 150 | case TemplateArgument::Type: |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 151 | LV.merge(getLVForType(Args[I].getAsType())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 152 | break; |
| 153 | |
| 154 | case TemplateArgument::Declaration: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 155 | // The decl can validly be null as the representation of nullptr |
| 156 | // arguments, valid only in C++0x. |
| 157 | if (Decl *D = Args[I].getAsDecl()) { |
Douglas Gregor | 89d63e5 | 2010-12-06 18:50:56 +0000 | [diff] [blame] | 158 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) |
| 159 | LV = merge(LV, getLVForDecl(ND, F)); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 160 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 161 | break; |
| 162 | |
| 163 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 164 | case TemplateArgument::TemplateExpansion: |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 165 | if (TemplateDecl *Template |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 166 | = Args[I].getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 167 | LV.merge(getLVForDecl(Template, F)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 168 | break; |
| 169 | |
| 170 | case TemplateArgument::Pack: |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 171 | LV.mergeWithMin(getLVForTemplateArgumentList(Args[I].pack_begin(), |
| 172 | Args[I].pack_size(), |
| 173 | F)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 178 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 181 | static LinkageInfo |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 182 | getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
| 183 | LVFlags &F) { |
| 184 | return getLVForTemplateArgumentList(TArgs.data(), TArgs.size(), F); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 185 | } |
| 186 | |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 187 | static bool shouldConsiderTemplateLV(const FunctionDecl *fn, |
| 188 | const FunctionTemplateSpecializationInfo *spec) { |
| 189 | return !(spec->isExplicitSpecialization() && |
| 190 | fn->hasAttr<VisibilityAttr>()); |
| 191 | } |
| 192 | |
| 193 | static bool shouldConsiderTemplateLV(const ClassTemplateSpecializationDecl *d) { |
| 194 | return !(d->isExplicitSpecialization() && d->hasAttr<VisibilityAttr>()); |
| 195 | } |
| 196 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 197 | static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, LVFlags F) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 198 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 199 | "Not a name having namespace scope"); |
| 200 | ASTContext &Context = D->getASTContext(); |
| 201 | |
| 202 | // C++ [basic.link]p3: |
| 203 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 204 | // is the name of |
| 205 | // - an object, reference, function or function template that is |
| 206 | // explicitly declared static; or, |
| 207 | // (This bullet corresponds to C99 6.2.2p3.) |
| 208 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 209 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 210 | if (Var->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 211 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 212 | |
| 213 | // - an object or reference that is explicitly declared const |
| 214 | // and neither explicitly declared extern nor previously |
| 215 | // declared to have external linkage; or |
| 216 | // (there is no equivalent in C99) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 217 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | e9d6554 | 2009-11-26 03:04:01 +0000 | [diff] [blame] | 218 | Var->getType().isConstant(Context) && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 219 | Var->getStorageClass() != SC_Extern && |
| 220 | Var->getStorageClass() != SC_PrivateExtern) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 221 | bool FoundExtern = false; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 222 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 223 | PrevVar && !FoundExtern; |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 224 | PrevVar = PrevVar->getPreviousDecl()) |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 225 | if (isExternalLinkage(PrevVar->getLinkage())) |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 226 | FoundExtern = true; |
| 227 | |
| 228 | if (!FoundExtern) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 229 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 230 | } |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 231 | if (Var->getStorageClass() == SC_None) { |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 232 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
| 233 | for (; PrevVar; PrevVar = PrevVar->getPreviousDecl()) |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 234 | if (PrevVar->getStorageClass() == SC_PrivateExtern) |
| 235 | break; |
| 236 | if (PrevVar) |
| 237 | return PrevVar->getLinkageAndVisibility(); |
| 238 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 239 | } else if (isa<FunctionDecl>(D) || isa<FunctionTemplateDecl>(D)) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 240 | // C++ [temp]p4: |
| 241 | // A non-member function template can have internal linkage; any |
| 242 | // other template name shall have external linkage. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 243 | const FunctionDecl *Function = 0; |
| 244 | if (const FunctionTemplateDecl *FunTmpl |
| 245 | = dyn_cast<FunctionTemplateDecl>(D)) |
| 246 | Function = FunTmpl->getTemplatedDecl(); |
| 247 | else |
| 248 | Function = cast<FunctionDecl>(D); |
| 249 | |
| 250 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 251 | if (Function->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 252 | return LinkageInfo(InternalLinkage, DefaultVisibility, false); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 253 | } else if (const FieldDecl *Field = dyn_cast<FieldDecl>(D)) { |
| 254 | // - a data member of an anonymous union. |
| 255 | if (cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 256 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 259 | if (D->isInAnonymousNamespace()) { |
| 260 | const VarDecl *Var = dyn_cast<VarDecl>(D); |
| 261 | const FunctionDecl *Func = dyn_cast<FunctionDecl>(D); |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 262 | if ((!Var || !Var->getDeclContext()->isExternCContext()) && |
| 263 | (!Func || !Func->getDeclContext()->isExternCContext())) |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 264 | return LinkageInfo::uniqueExternal(); |
| 265 | } |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 266 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 267 | // Set up the defaults. |
| 268 | |
| 269 | // C99 6.2.2p5: |
| 270 | // If the declaration of an identifier for an object has file |
| 271 | // scope and no storage-class specifier, its linkage is |
| 272 | // external. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 273 | LinkageInfo LV; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 274 | LV.mergeVisibility(Context.getLangOpts().getVisibilityMode()); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 275 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 276 | // C++ [basic.link]p4: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 277 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 278 | // A name having namespace scope has external linkage if it is the |
| 279 | // name of |
| 280 | // |
| 281 | // - an object or reference, unless it has internal linkage; or |
| 282 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 283 | // GCC applies the following optimization to variables and static |
| 284 | // data members, but not to functions: |
| 285 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 286 | // Modify the variable's LV by the LV of its type unless this is |
| 287 | // C or extern "C". This follows from [basic.link]p9: |
| 288 | // A type without linkage shall not be used as the type of a |
| 289 | // variable or function with external linkage unless |
| 290 | // - the entity has C language linkage, or |
| 291 | // - the entity is declared within an unnamed namespace, or |
| 292 | // - the entity is not used or is defined in the same |
| 293 | // translation unit. |
| 294 | // and [basic.link]p10: |
| 295 | // ...the types specified by all declarations referring to a |
| 296 | // given variable or function shall be identical... |
| 297 | // C does not have an equivalent rule. |
| 298 | // |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 299 | // Ignore this if we've got an explicit attribute; the user |
| 300 | // probably knows what they're doing. |
| 301 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 302 | // Note that we don't want to make the variable non-external |
| 303 | // because of this, but unique-external linkage suits us. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 304 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 305 | !Var->getDeclContext()->isExternCContext()) { |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 306 | LinkageInfo TypeLV = getLVForType(Var->getType()); |
| 307 | if (TypeLV.linkage() != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 308 | return LinkageInfo::uniqueExternal(); |
Rafael Espindola | 2f47c36 | 2012-03-10 13:01:40 +0000 | [diff] [blame] | 309 | LV.mergeVisibilityWithMin(TypeLV.visibility(), |
| 310 | TypeLV.visibilityExplicit()); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 311 | } |
| 312 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 313 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 314 | LV.setVisibility(HiddenVisibility, true); |
| 315 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 316 | if (!Context.getLangOpts().CPlusPlus && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 317 | (Var->getStorageClass() == SC_Extern || |
| 318 | Var->getStorageClass() == SC_PrivateExtern)) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 319 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 320 | // C99 6.2.2p4: |
| 321 | // For an identifier declared with the storage-class specifier |
| 322 | // extern in a scope in which a prior declaration of that |
| 323 | // identifier is visible, if the prior declaration specifies |
| 324 | // internal or external linkage, the linkage of the identifier |
| 325 | // at the later declaration is the same as the linkage |
| 326 | // specified at the prior declaration. If no prior declaration |
| 327 | // is visible, or if the prior declaration specifies no |
| 328 | // linkage, then the identifier has external linkage. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 329 | if (const VarDecl *PrevVar = Var->getPreviousDecl()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 330 | LinkageInfo PrevLV = getLVForDecl(PrevVar, F); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 331 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 332 | LV.mergeVisibility(PrevLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 336 | // - a function, unless it has internal linkage; or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 337 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 67fa6d5 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 338 | // In theory, we can modify the function's LV by the LV of its |
| 339 | // type unless it has C linkage (see comment above about variables |
| 340 | // for justification). In practice, GCC doesn't do this, so it's |
| 341 | // just too painful to make work. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 342 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 343 | if (Function->getStorageClass() == SC_PrivateExtern) |
| 344 | LV.setVisibility(HiddenVisibility, true); |
| 345 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 346 | // C99 6.2.2p5: |
| 347 | // If the declaration of an identifier for a function has no |
| 348 | // storage-class specifier, its linkage is determined exactly |
| 349 | // as if it were declared with the storage-class specifier |
| 350 | // extern. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 351 | if (!Context.getLangOpts().CPlusPlus && |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 352 | (Function->getStorageClass() == SC_Extern || |
| 353 | Function->getStorageClass() == SC_PrivateExtern || |
| 354 | Function->getStorageClass() == SC_None)) { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 355 | // C99 6.2.2p4: |
| 356 | // For an identifier declared with the storage-class specifier |
| 357 | // extern in a scope in which a prior declaration of that |
| 358 | // identifier is visible, if the prior declaration specifies |
| 359 | // internal or external linkage, the linkage of the identifier |
| 360 | // at the later declaration is the same as the linkage |
| 361 | // specified at the prior declaration. If no prior declaration |
| 362 | // is visible, or if the prior declaration specifies no |
| 363 | // linkage, then the identifier has external linkage. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 364 | if (const FunctionDecl *PrevFunc = Function->getPreviousDecl()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 365 | LinkageInfo PrevLV = getLVForDecl(PrevFunc, F); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 366 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 367 | LV.mergeVisibility(PrevLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 371 | // In C++, then if the type of the function uses a type with |
| 372 | // unique-external linkage, it's not legally usable from outside |
| 373 | // this translation unit. However, we should use the C linkage |
| 374 | // rules instead for extern "C" declarations. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 375 | if (Context.getLangOpts().CPlusPlus && |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 376 | !Function->getDeclContext()->isExternCContext() && |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 377 | Function->getType()->getLinkage() == UniqueExternalLinkage) |
| 378 | return LinkageInfo::uniqueExternal(); |
| 379 | |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 380 | // Consider LV from the template and the template arguments unless |
| 381 | // this is an explicit specialization with a visibility attribute. |
| 382 | if (FunctionTemplateSpecializationInfo *specInfo |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 383 | = Function->getTemplateSpecializationInfo()) { |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 384 | if (shouldConsiderTemplateLV(Function, specInfo)) { |
| 385 | LV.merge(getLVForDecl(specInfo->getTemplate(), |
Rafael Espindola | 62d9f11 | 2012-04-16 13:44:41 +0000 | [diff] [blame^] | 386 | LVFlags::CreateOnlyDeclLinkage())); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 387 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 388 | LV.mergeWithMin(getLVForTemplateArgumentList(templateArgs, F)); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 389 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 392 | // - a named class (Clause 9), or an unnamed class defined in a |
| 393 | // typedef declaration in which the class has the typedef name |
| 394 | // for linkage purposes (7.1.3); or |
| 395 | // - a named enumeration (7.2), or an unnamed enumeration |
| 396 | // defined in a typedef declaration in which the enumeration |
| 397 | // has the typedef name for linkage purposes (7.1.3); or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 398 | } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 399 | // Unnamed tags have no linkage. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 400 | if (!Tag->getDeclName() && !Tag->getTypedefNameForAnonDecl()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 401 | return LinkageInfo::none(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 402 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 403 | // If this is a class template specialization, consider the |
| 404 | // linkage of the template and template arguments. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 405 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 406 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 407 | if (shouldConsiderTemplateLV(spec)) { |
| 408 | // From the template. |
| 409 | LV.merge(getLVForDecl(spec->getSpecializedTemplate(), |
Rafael Espindola | 62d9f11 | 2012-04-16 13:44:41 +0000 | [diff] [blame^] | 410 | LVFlags::CreateOnlyDeclLinkage())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 411 | |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 412 | // The arguments at which the template was instantiated. |
| 413 | const TemplateArgumentList &TemplateArgs = spec->getTemplateArgs(); |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 414 | LV.mergeWithMin(getLVForTemplateArgumentList(TemplateArgs, F)); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 415 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 416 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 417 | |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 418 | // Consider -fvisibility unless the type has C linkage. |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 419 | if (F.ConsiderGlobalVisibility) |
| 420 | F.ConsiderGlobalVisibility = |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 421 | (Context.getLangOpts().CPlusPlus && |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 422 | !Tag->getDeclContext()->isExternCContext()); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 423 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 424 | // - an enumerator belonging to an enumeration with external linkage; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 425 | } else if (isa<EnumConstantDecl>(D)) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 426 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), F); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 427 | if (!isExternalLinkage(EnumLV.linkage())) |
| 428 | return LinkageInfo::none(); |
| 429 | LV.merge(EnumLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 430 | |
| 431 | // - a template, unless it is a function template that has |
| 432 | // internal linkage (Clause 14); |
John McCall | 1a0918a | 2011-03-04 10:39:25 +0000 | [diff] [blame] | 433 | } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { |
| 434 | if (F.ConsiderTemplateParameterTypes) |
| 435 | LV.merge(getLVForTemplateParameterList(temp->getTemplateParameters())); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 436 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 437 | // - a namespace (7.3), unless it is declared within an unnamed |
| 438 | // namespace. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 439 | } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { |
| 440 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 441 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 442 | // By extension, we assign external linkage to Objective-C |
| 443 | // interfaces. |
| 444 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 445 | // fallout |
| 446 | |
| 447 | // Everything not covered here has no linkage. |
| 448 | } else { |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 449 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Rafael Espindola | 767f7c7 | 2012-04-14 15:21:19 +0000 | [diff] [blame] | 452 | if (F.ConsiderVisibilityAttributes) { |
| 453 | if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) { |
| 454 | LV.setVisibility(*Vis, true); |
| 455 | F.ConsiderGlobalVisibility = false; |
| 456 | } else { |
| 457 | // If we're declared in a namespace with a visibility attribute, |
| 458 | // use that namespace's visibility, but don't call it explicit. |
| 459 | for (const DeclContext *DC = D->getDeclContext(); |
| 460 | !isa<TranslationUnitDecl>(DC); |
| 461 | DC = DC->getParent()) { |
| 462 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC); |
| 463 | if (!ND) continue; |
| 464 | if (llvm::Optional<Visibility> Vis = ND->getExplicitVisibility()) { |
| 465 | LV.setVisibility(*Vis, true); |
| 466 | F.ConsiderGlobalVisibility = false; |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 473 | // If we ended up with non-external linkage, visibility should |
| 474 | // always be default. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 475 | if (LV.linkage() != ExternalLinkage) |
| 476 | return LinkageInfo(LV.linkage(), DefaultVisibility, false); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 477 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 478 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 479 | } |
| 480 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 481 | static LinkageInfo getLVForClassMember(const NamedDecl *D, LVFlags F) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 482 | // Only certain class members have linkage. Note that fields don't |
| 483 | // really have linkage, but it's convenient to say they do for the |
| 484 | // purposes of calculating linkage of pointer-to-data-member |
| 485 | // template arguments. |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 486 | if (!(isa<CXXMethodDecl>(D) || |
| 487 | isa<VarDecl>(D) || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 488 | isa<FieldDecl>(D) || |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 489 | (isa<TagDecl>(D) && |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 490 | (D->getDeclName() || cast<TagDecl>(D)->getTypedefNameForAnonDecl())))) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 491 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 492 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 493 | LinkageInfo LV; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 494 | LV.mergeVisibility(D->getASTContext().getLangOpts().getVisibilityMode()); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 495 | |
| 496 | // The flags we're going to use to compute the class's visibility. |
| 497 | LVFlags ClassF = F; |
| 498 | |
| 499 | // If we have an explicit visibility attribute, merge that in. |
| 500 | if (F.ConsiderVisibilityAttributes) { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 501 | if (llvm::Optional<Visibility> Vis = D->getExplicitVisibility()) { |
| 502 | LV.mergeVisibility(*Vis, true); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 503 | |
| 504 | // Ignore global visibility later, but not this attribute. |
| 505 | F.ConsiderGlobalVisibility = false; |
| 506 | |
| 507 | // Ignore both global visibility and attributes when computing our |
| 508 | // parent's visibility. |
Rafael Espindola | 62d9f11 | 2012-04-16 13:44:41 +0000 | [diff] [blame^] | 509 | ClassF = LVFlags::CreateOnlyDeclLinkage(); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 512 | |
| 513 | // Class members only have linkage if their class has external |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 514 | // linkage. |
| 515 | LV.merge(getLVForDecl(cast<RecordDecl>(D->getDeclContext()), ClassF)); |
| 516 | if (!isExternalLinkage(LV.linkage())) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 517 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 518 | |
| 519 | // If the class already has unique-external linkage, we can't improve. |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 520 | if (LV.linkage() == UniqueExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 521 | return LinkageInfo::uniqueExternal(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +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 | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 529 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 530 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 531 | // If this is a method template specialization, use the linkage for |
| 532 | // the template parameters and arguments. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 533 | if (FunctionTemplateSpecializationInfo *spec |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 534 | = MD->getTemplateSpecializationInfo()) { |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 535 | if (shouldConsiderTemplateLV(MD, spec)) { |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 536 | LV.mergeWithMin(getLVForTemplateArgumentList(*spec->TemplateArguments, |
| 537 | F)); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 538 | if (F.ConsiderTemplateParameterTypes) |
| 539 | LV.merge(getLVForTemplateParameterList( |
| 540 | spec->getTemplate()->getTemplateParameters())); |
| 541 | } |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 542 | |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 543 | TSK = spec->getTemplateSpecializationKind(); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 544 | } else if (MemberSpecializationInfo *MSI = |
| 545 | MD->getMemberSpecializationInfo()) { |
| 546 | TSK = MSI->getTemplateSpecializationKind(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 547 | } |
| 548 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 549 | // If we're paying attention to global visibility, apply |
| 550 | // -finline-visibility-hidden if this is an inline method. |
| 551 | // |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 552 | // Note that ConsiderGlobalVisibility doesn't yet have information |
| 553 | // about whether containing classes have visibility attributes, |
| 554 | // and that's intentional. |
| 555 | if (TSK != TSK_ExplicitInstantiationDeclaration && |
Rafael Espindola | fedb6ec | 2011-12-27 21:15:28 +0000 | [diff] [blame] | 556 | TSK != TSK_ExplicitInstantiationDefinition && |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 557 | F.ConsiderGlobalVisibility && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 558 | MD->getASTContext().getLangOpts().InlineVisibilityHidden) { |
John McCall | 66cbcf3 | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 559 | // InlineVisibilityHidden only applies to definitions, and |
| 560 | // isInlined() only gives meaningful answers on definitions |
| 561 | // anyway. |
| 562 | const FunctionDecl *Def = 0; |
| 563 | if (MD->hasBody(Def) && Def->isInlined()) |
| 564 | LV.setVisibility(HiddenVisibility); |
| 565 | } |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 566 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 567 | // Note that in contrast to basically every other situation, we |
| 568 | // *do* apply -fvisibility to method declarations. |
| 569 | |
| 570 | } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 571 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 572 | = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 573 | if (shouldConsiderTemplateLV(spec)) { |
| 574 | // Merge template argument/parameter information for member |
| 575 | // class template specializations. |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 576 | LV.mergeWithMin(getLVForTemplateArgumentList(spec->getTemplateArgs(), |
| 577 | F)); |
John McCall | 1a0918a | 2011-03-04 10:39:25 +0000 | [diff] [blame] | 578 | if (F.ConsiderTemplateParameterTypes) |
| 579 | LV.merge(getLVForTemplateParameterList( |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 580 | spec->getSpecializedTemplate()->getTemplateParameters())); |
| 581 | } |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 582 | } |
| 583 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 584 | // Static data members. |
| 585 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
John McCall | ee30102 | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 586 | // Modify the variable's linkage by its type, but ignore the |
| 587 | // type's visibility unless it's a definition. |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 588 | LinkageInfo TypeLV = getLVForType(VD->getType()); |
| 589 | if (TypeLV.linkage() != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 590 | LV.mergeLinkage(UniqueExternalLinkage); |
| 591 | if (!LV.visibilityExplicit()) |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 592 | LV.mergeVisibility(TypeLV.visibility(), TypeLV.visibilityExplicit()); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 593 | } |
| 594 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 595 | return LV; |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 596 | } |
| 597 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 598 | static void clearLinkageForClass(const CXXRecordDecl *record) { |
| 599 | for (CXXRecordDecl::decl_iterator |
| 600 | i = record->decls_begin(), e = record->decls_end(); i != e; ++i) { |
| 601 | Decl *child = *i; |
| 602 | if (isa<NamedDecl>(child)) |
| 603 | cast<NamedDecl>(child)->ClearLinkageCache(); |
| 604 | } |
| 605 | } |
| 606 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 607 | void NamedDecl::anchor() { } |
| 608 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 609 | void NamedDecl::ClearLinkageCache() { |
| 610 | // Note that we can't skip clearing the linkage of children just |
| 611 | // because the parent doesn't have cached linkage: we don't cache |
| 612 | // when computing linkage for parent contexts. |
| 613 | |
| 614 | HasCachedLinkage = 0; |
| 615 | |
| 616 | // If we're changing the linkage of a class, we need to reset the |
| 617 | // linkage of child declarations, too. |
| 618 | if (const CXXRecordDecl *record = dyn_cast<CXXRecordDecl>(this)) |
| 619 | clearLinkageForClass(record); |
| 620 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 621 | if (ClassTemplateDecl *temp = |
| 622 | dyn_cast<ClassTemplateDecl>(const_cast<NamedDecl*>(this))) { |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 623 | // Clear linkage for the template pattern. |
| 624 | CXXRecordDecl *record = temp->getTemplatedDecl(); |
| 625 | record->HasCachedLinkage = 0; |
| 626 | clearLinkageForClass(record); |
| 627 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 628 | // We need to clear linkage for specializations, too. |
| 629 | for (ClassTemplateDecl::spec_iterator |
| 630 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
| 631 | i->ClearLinkageCache(); |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 632 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 633 | |
| 634 | // Clear cached linkage for function template decls, too. |
| 635 | if (FunctionTemplateDecl *temp = |
John McCall | 7895194 | 2011-03-22 06:58:49 +0000 | [diff] [blame] | 636 | dyn_cast<FunctionTemplateDecl>(const_cast<NamedDecl*>(this))) { |
| 637 | temp->getTemplatedDecl()->ClearLinkageCache(); |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 638 | for (FunctionTemplateDecl::spec_iterator |
| 639 | i = temp->spec_begin(), e = temp->spec_end(); i != e; ++i) |
| 640 | i->ClearLinkageCache(); |
John McCall | 7895194 | 2011-03-22 06:58:49 +0000 | [diff] [blame] | 641 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 642 | |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 645 | Linkage NamedDecl::getLinkage() const { |
| 646 | if (HasCachedLinkage) { |
Benjamin Kramer | 56ed792 | 2010-12-07 15:51:48 +0000 | [diff] [blame] | 647 | assert(Linkage(CachedLinkage) == |
| 648 | getLVForDecl(this, LVFlags::CreateOnlyDeclLinkage()).linkage()); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 649 | return Linkage(CachedLinkage); |
| 650 | } |
| 651 | |
| 652 | CachedLinkage = getLVForDecl(this, |
| 653 | LVFlags::CreateOnlyDeclLinkage()).linkage(); |
| 654 | HasCachedLinkage = 1; |
| 655 | return Linkage(CachedLinkage); |
| 656 | } |
| 657 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 658 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 659 | LinkageInfo LI = getLVForDecl(this, LVFlags()); |
Benjamin Kramer | 56ed792 | 2010-12-07 15:51:48 +0000 | [diff] [blame] | 660 | assert(!HasCachedLinkage || Linkage(CachedLinkage) == LI.linkage()); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 661 | HasCachedLinkage = 1; |
| 662 | CachedLinkage = LI.linkage(); |
| 663 | return LI; |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 664 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 666 | llvm::Optional<Visibility> NamedDecl::getExplicitVisibility() const { |
| 667 | // Use the most recent declaration of a variable. |
| 668 | if (const VarDecl *var = dyn_cast<VarDecl>(this)) |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 669 | return getVisibilityOf(var->getMostRecentDecl()); |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 670 | |
| 671 | // Use the most recent declaration of a function, and also handle |
| 672 | // function template specializations. |
| 673 | if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(this)) { |
| 674 | if (llvm::Optional<Visibility> V |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 675 | = getVisibilityOf(fn->getMostRecentDecl())) |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 676 | return V; |
| 677 | |
| 678 | // If the function is a specialization of a template with an |
| 679 | // explicit visibility attribute, use that. |
| 680 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 681 | = fn->getTemplateSpecializationInfo()) |
| 682 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl()); |
| 683 | |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 684 | // If the function is a member of a specialization of a class template |
| 685 | // and the corresponding decl has explicit visibility, use that. |
| 686 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 687 | if (InstantiatedFrom) |
| 688 | return getVisibilityOf(InstantiatedFrom); |
| 689 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 690 | return llvm::Optional<Visibility>(); |
| 691 | } |
| 692 | |
| 693 | // Otherwise, just check the declaration itself first. |
| 694 | if (llvm::Optional<Visibility> V = getVisibilityOf(this)) |
| 695 | return V; |
| 696 | |
| 697 | // If there wasn't explicit visibility there, and this is a |
| 698 | // specialization of a class template, check for visibility |
| 699 | // on the pattern. |
| 700 | if (const ClassTemplateSpecializationDecl *spec |
| 701 | = dyn_cast<ClassTemplateSpecializationDecl>(this)) |
| 702 | return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl()); |
| 703 | |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 704 | // If this is a member class of a specialization of a class template |
| 705 | // and the corresponding decl has explicit visibility, use that. |
| 706 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(this)) { |
| 707 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 708 | if (InstantiatedFrom) |
| 709 | return getVisibilityOf(InstantiatedFrom); |
| 710 | } |
| 711 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 712 | return llvm::Optional<Visibility>(); |
| 713 | } |
| 714 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 715 | static LinkageInfo getLVForDecl(const NamedDecl *D, LVFlags Flags) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 716 | // Objective-C: treat all Objective-C declarations as having external |
| 717 | // linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 718 | switch (D->getKind()) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 719 | default: |
| 720 | break; |
Argyrios Kyrtzidis | f8d34ed | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 721 | case Decl::ParmVar: |
| 722 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 723 | case Decl::TemplateTemplateParm: // count these as external |
| 724 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 725 | case Decl::ObjCAtDefsField: |
| 726 | case Decl::ObjCCategory: |
| 727 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 728 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 729 | case Decl::ObjCImplementation: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 730 | case Decl::ObjCMethod: |
| 731 | case Decl::ObjCProperty: |
| 732 | case Decl::ObjCPropertyImpl: |
| 733 | case Decl::ObjCProtocol: |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 734 | return LinkageInfo::external(); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 735 | |
| 736 | case Decl::CXXRecord: { |
| 737 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(D); |
| 738 | if (Record->isLambda()) { |
| 739 | if (!Record->getLambdaManglingNumber()) { |
| 740 | // This lambda has no mangling number, so it's internal. |
| 741 | return LinkageInfo::internal(); |
| 742 | } |
| 743 | |
| 744 | // This lambda has its linkage/visibility determined by its owner. |
| 745 | const DeclContext *DC = D->getDeclContext()->getRedeclContext(); |
| 746 | if (Decl *ContextDecl = Record->getLambdaContextDecl()) { |
| 747 | if (isa<ParmVarDecl>(ContextDecl)) |
| 748 | DC = ContextDecl->getDeclContext()->getRedeclContext(); |
| 749 | else |
| 750 | return getLVForDecl(cast<NamedDecl>(ContextDecl), Flags); |
| 751 | } |
| 752 | |
| 753 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) |
| 754 | return getLVForDecl(ND, Flags); |
| 755 | |
| 756 | return LinkageInfo::external(); |
| 757 | } |
| 758 | |
| 759 | break; |
| 760 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 763 | // Handle linkage for namespace-scope names. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 764 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 765 | return getLVForNamespaceScopeDecl(D, Flags); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 766 | |
| 767 | // C++ [basic.link]p5: |
| 768 | // In addition, a member function, static data member, a named |
| 769 | // class or enumeration of class scope, or an unnamed class or |
| 770 | // enumeration defined in a class-scope typedef declaration such |
| 771 | // that the class or enumeration has the typedef name for linkage |
| 772 | // purposes (7.1.3), has external linkage if the name of the class |
| 773 | // has external linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 774 | if (D->getDeclContext()->isRecord()) |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 775 | return getLVForClassMember(D, Flags); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 776 | |
| 777 | // C++ [basic.link]p6: |
| 778 | // The name of a function declared in block scope and the name of |
| 779 | // an object declared by a block scope extern declaration have |
| 780 | // linkage. If there is a visible declaration of an entity with |
| 781 | // linkage having the same name and type, ignoring entities |
| 782 | // declared outside the innermost enclosing namespace scope, the |
| 783 | // block scope declaration declares that same entity and receives |
| 784 | // the linkage of the previous declaration. If there is more than |
| 785 | // one such matching entity, the program is ill-formed. Otherwise, |
| 786 | // if no matching entity is found, the block scope entity receives |
| 787 | // external linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 788 | if (D->getLexicalDeclContext()->isFunctionOrMethod()) { |
| 789 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 790 | if (Function->isInAnonymousNamespace() && |
| 791 | !Function->getDeclContext()->isExternCContext()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 792 | return LinkageInfo::uniqueExternal(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 793 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 794 | LinkageInfo LV; |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 795 | if (Flags.ConsiderVisibilityAttributes) { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 796 | if (llvm::Optional<Visibility> Vis = Function->getExplicitVisibility()) |
| 797 | LV.setVisibility(*Vis); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 800 | if (const FunctionDecl *Prev = Function->getPreviousDecl()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 801 | LinkageInfo PrevLV = getLVForDecl(Prev, Flags); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 802 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 803 | LV.mergeVisibility(PrevLV); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 807 | } |
| 808 | |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 809 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 810 | if (Var->getStorageClass() == SC_Extern || |
| 811 | Var->getStorageClass() == SC_PrivateExtern) { |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 812 | if (Var->isInAnonymousNamespace() && |
| 813 | !Var->getDeclContext()->isExternCContext()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 814 | return LinkageInfo::uniqueExternal(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 815 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 816 | LinkageInfo LV; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 817 | if (Var->getStorageClass() == SC_PrivateExtern) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 818 | LV.setVisibility(HiddenVisibility); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 819 | else if (Flags.ConsiderVisibilityAttributes) { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 820 | if (llvm::Optional<Visibility> Vis = Var->getExplicitVisibility()) |
| 821 | LV.setVisibility(*Vis); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 824 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 825 | LinkageInfo PrevLV = getLVForDecl(Prev, Flags); |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 826 | if (PrevLV.linkage()) LV.setLinkage(PrevLV.linkage()); |
| 827 | LV.mergeVisibility(PrevLV); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
| 834 | // C++ [basic.link]p6: |
| 835 | // Names not covered by these rules have no linkage. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 836 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 837 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 839 | std::string NamedDecl::getQualifiedNameAsString() const { |
Douglas Gregor | ba10306 | 2012-03-27 23:34:16 +0000 | [diff] [blame] | 840 | return getQualifiedNameAsString(getASTContext().getPrintingPolicy()); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | std::string NamedDecl::getQualifiedNameAsString(const PrintingPolicy &P) const { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 844 | const DeclContext *Ctx = getDeclContext(); |
| 845 | |
| 846 | if (Ctx->isFunctionOrMethod()) |
| 847 | return getNameAsString(); |
| 848 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 849 | typedef SmallVector<const DeclContext *, 8> ContextsTy; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 850 | ContextsTy Contexts; |
| 851 | |
| 852 | // Collect contexts. |
| 853 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 854 | Contexts.push_back(Ctx); |
| 855 | Ctx = Ctx->getParent(); |
| 856 | }; |
| 857 | |
| 858 | std::string QualName; |
| 859 | llvm::raw_string_ostream OS(QualName); |
| 860 | |
| 861 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 862 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 864 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 865 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 866 | std::string TemplateArgsStr |
| 867 | = TemplateSpecializationType::PrintTemplateArgumentList( |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 868 | TemplateArgs.data(), |
| 869 | TemplateArgs.size(), |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 870 | P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 871 | OS << Spec->getName() << TemplateArgsStr; |
| 872 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 873 | if (ND->isAnonymousNamespace()) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 874 | OS << "<anonymous namespace>"; |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 875 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 876 | OS << *ND; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 877 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 878 | if (!RD->getIdentifier()) |
| 879 | OS << "<anonymous " << RD->getKindName() << '>'; |
| 880 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 881 | OS << *RD; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 882 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 883 | const FunctionProtoType *FT = 0; |
| 884 | if (FD->hasWrittenPrototype()) |
| 885 | FT = dyn_cast<FunctionProtoType>(FD->getType()->getAs<FunctionType>()); |
| 886 | |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 887 | OS << *FD << '('; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 888 | if (FT) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 889 | unsigned NumParams = FD->getNumParams(); |
| 890 | for (unsigned i = 0; i < NumParams; ++i) { |
| 891 | if (i) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 892 | OS << ", "; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 893 | std::string Param; |
| 894 | FD->getParamDecl(i)->getType().getAsStringInternal(Param, P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 895 | OS << Param; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 896 | } |
| 897 | |
| 898 | if (FT->isVariadic()) { |
| 899 | if (NumParams > 0) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 900 | OS << ", "; |
| 901 | OS << "..."; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 904 | OS << ')'; |
| 905 | } else { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 906 | OS << *cast<NamedDecl>(*I); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 907 | } |
| 908 | OS << "::"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 909 | } |
| 910 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 911 | if (getDeclName()) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 912 | OS << *this; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 913 | else |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 914 | OS << "<anonymous>"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 915 | |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 916 | return OS.str(); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 917 | } |
| 918 | |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 919 | bool NamedDecl::declarationReplaces(NamedDecl *OldD) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 920 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 921 | |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 922 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 923 | // We want to keep it, unless it nominates same namespace. |
| 924 | if (getKind() == Decl::UsingDirective) { |
Douglas Gregor | db99241 | 2011-02-25 16:33:46 +0000 | [diff] [blame] | 925 | return cast<UsingDirectiveDecl>(this)->getNominatedNamespace() |
| 926 | ->getOriginalNamespace() == |
| 927 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace() |
| 928 | ->getOriginalNamespace(); |
Douglas Gregor | 2a3009a | 2009-02-03 19:21:40 +0000 | [diff] [blame] | 929 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 931 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 932 | // For function declarations, we keep track of redeclarations. |
Douglas Gregor | ef96ee0 | 2012-01-14 16:38:05 +0000 | [diff] [blame] | 933 | return FD->getPreviousDecl() == OldD; |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 935 | // For function templates, the underlying function declarations are linked. |
| 936 | if (const FunctionTemplateDecl *FunctionTemplate |
| 937 | = dyn_cast<FunctionTemplateDecl>(this)) |
| 938 | if (const FunctionTemplateDecl *OldFunctionTemplate |
| 939 | = dyn_cast<FunctionTemplateDecl>(OldD)) |
| 940 | return FunctionTemplate->getTemplatedDecl() |
| 941 | ->declarationReplaces(OldFunctionTemplate->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 943 | // For method declarations, we keep track of redeclarations. |
| 944 | if (isa<ObjCMethodDecl>(this)) |
| 945 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 947 | if (isa<ObjCInterfaceDecl>(this) && isa<ObjCCompatibleAliasDecl>(OldD)) |
| 948 | return true; |
| 949 | |
John McCall | 9488ea1 | 2009-11-17 05:59:44 +0000 | [diff] [blame] | 950 | if (isa<UsingShadowDecl>(this) && isa<UsingShadowDecl>(OldD)) |
| 951 | return cast<UsingShadowDecl>(this)->getTargetDecl() == |
| 952 | cast<UsingShadowDecl>(OldD)->getTargetDecl(); |
| 953 | |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 954 | if (isa<UsingDecl>(this) && isa<UsingDecl>(OldD)) { |
| 955 | ASTContext &Context = getASTContext(); |
| 956 | return Context.getCanonicalNestedNameSpecifier( |
| 957 | cast<UsingDecl>(this)->getQualifier()) == |
| 958 | Context.getCanonicalNestedNameSpecifier( |
| 959 | cast<UsingDecl>(OldD)->getQualifier()); |
| 960 | } |
Argyrios Kyrtzidis | c80117e | 2010-11-04 08:48:52 +0000 | [diff] [blame] | 961 | |
Douglas Gregor | 7a53740 | 2012-01-03 23:26:26 +0000 | [diff] [blame] | 962 | // A typedef of an Objective-C class type can replace an Objective-C class |
| 963 | // declaration or definition, and vice versa. |
| 964 | if ((isa<TypedefNameDecl>(this) && isa<ObjCInterfaceDecl>(OldD)) || |
| 965 | (isa<ObjCInterfaceDecl>(this) && isa<TypedefNameDecl>(OldD))) |
| 966 | return true; |
| 967 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 968 | // For non-function declarations, if the declarations are of the |
| 969 | // same kind then this must be a redeclaration, or semantic analysis |
| 970 | // would not have given us the new declaration. |
| 971 | return this->getKind() == OldD->getKind(); |
| 972 | } |
| 973 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 974 | bool NamedDecl::hasLinkage() const { |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 975 | return getLinkage() != NoLinkage; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 976 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 977 | |
Daniel Dunbar | 6daffa5 | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 978 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 979 | NamedDecl *ND = this; |
Benjamin Kramer | 56757e9 | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 980 | while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
| 981 | ND = UD->getTargetDecl(); |
| 982 | |
| 983 | if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 984 | return AD->getClassInterface(); |
| 985 | |
| 986 | return ND; |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 987 | } |
| 988 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 989 | bool NamedDecl::isCXXInstanceMember() const { |
Douglas Gregor | 5bc37f6 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 990 | if (!isCXXClassMember()) |
| 991 | return false; |
| 992 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 993 | const NamedDecl *D = this; |
| 994 | if (isa<UsingShadowDecl>(D)) |
| 995 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 996 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 997 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 998 | return true; |
| 999 | if (isa<CXXMethodDecl>(D)) |
| 1000 | return cast<CXXMethodDecl>(D)->isInstance(); |
| 1001 | if (isa<FunctionTemplateDecl>(D)) |
| 1002 | return cast<CXXMethodDecl>(cast<FunctionTemplateDecl>(D) |
| 1003 | ->getTemplatedDecl())->isInstance(); |
| 1004 | return false; |
| 1005 | } |
| 1006 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1007 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1008 | // DeclaratorDecl Implementation |
| 1009 | //===----------------------------------------------------------------------===// |
| 1010 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1011 | template <typename DeclT> |
| 1012 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1013 | if (decl->getNumTemplateParameterLists() > 0) |
| 1014 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1015 | else |
| 1016 | return decl->getInnerLocStart(); |
| 1017 | } |
| 1018 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1019 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1020 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1021 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1022 | return SourceLocation(); |
| 1023 | } |
| 1024 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1025 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1026 | if (QualifierLoc) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1027 | // Make sure the extended decl info is allocated. |
| 1028 | if (!hasExtInfo()) { |
| 1029 | // Save (non-extended) type source info pointer. |
| 1030 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1031 | // Allocate external info struct. |
| 1032 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1033 | // Restore savedTInfo into (extended) decl info. |
| 1034 | getExtInfo()->TInfo = savedTInfo; |
| 1035 | } |
| 1036 | // Set qualifier info. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1037 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1038 | } else { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1039 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1040 | if (hasExtInfo()) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1041 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 1042 | // Save type source info pointer. |
| 1043 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 1044 | // Deallocate the extended decl info. |
| 1045 | getASTContext().Deallocate(getExtInfo()); |
| 1046 | // Restore savedTInfo into (non-extended) decl info. |
| 1047 | DeclInfo = savedTInfo; |
| 1048 | } |
| 1049 | else |
| 1050 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1051 | } |
| 1052 | } |
| 1053 | } |
| 1054 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1055 | void |
| 1056 | DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 1057 | unsigned NumTPLists, |
| 1058 | TemplateParameterList **TPLists) { |
| 1059 | assert(NumTPLists > 0); |
| 1060 | // Make sure the extended decl info is allocated. |
| 1061 | if (!hasExtInfo()) { |
| 1062 | // Save (non-extended) type source info pointer. |
| 1063 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1064 | // Allocate external info struct. |
| 1065 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1066 | // Restore savedTInfo into (extended) decl info. |
| 1067 | getExtInfo()->TInfo = savedTInfo; |
| 1068 | } |
| 1069 | // Set the template parameter lists info. |
| 1070 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 1071 | } |
| 1072 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1073 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1074 | return getTemplateOrInnerLocStart(this); |
| 1075 | } |
| 1076 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1077 | namespace { |
| 1078 | |
| 1079 | // Helper function: returns true if QT is or contains a type |
| 1080 | // having a postfix component. |
| 1081 | bool typeIsPostfix(clang::QualType QT) { |
| 1082 | while (true) { |
| 1083 | const Type* T = QT.getTypePtr(); |
| 1084 | switch (T->getTypeClass()) { |
| 1085 | default: |
| 1086 | return false; |
| 1087 | case Type::Pointer: |
| 1088 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1089 | break; |
| 1090 | case Type::BlockPointer: |
| 1091 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1092 | break; |
| 1093 | case Type::MemberPointer: |
| 1094 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1095 | break; |
| 1096 | case Type::LValueReference: |
| 1097 | case Type::RValueReference: |
| 1098 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1099 | break; |
| 1100 | case Type::PackExpansion: |
| 1101 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1102 | break; |
| 1103 | case Type::Paren: |
| 1104 | case Type::ConstantArray: |
| 1105 | case Type::DependentSizedArray: |
| 1106 | case Type::IncompleteArray: |
| 1107 | case Type::VariableArray: |
| 1108 | case Type::FunctionProto: |
| 1109 | case Type::FunctionNoProto: |
| 1110 | return true; |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | } // namespace |
| 1116 | |
| 1117 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1118 | SourceLocation RangeEnd = getLocation(); |
| 1119 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 1120 | if (typeIsPostfix(TInfo->getType())) |
| 1121 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1122 | } |
| 1123 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1124 | } |
| 1125 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1126 | void |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1127 | QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, |
| 1128 | unsigned NumTPLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1129 | TemplateParameterList **TPLists) { |
| 1130 | assert((NumTPLists == 0 || TPLists != 0) && |
| 1131 | "Empty array of template parameters with positive size!"); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1132 | |
| 1133 | // Free previous template parameters (if any). |
| 1134 | if (NumTemplParamLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1135 | Context.Deallocate(TemplParamLists); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1136 | TemplParamLists = 0; |
| 1137 | NumTemplParamLists = 0; |
| 1138 | } |
| 1139 | // Set info on matched template parameter lists (if any). |
| 1140 | if (NumTPLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1141 | TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1142 | NumTemplParamLists = NumTPLists; |
| 1143 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 1144 | TemplParamLists[i] = TPLists[i]; |
| 1145 | } |
| 1146 | } |
| 1147 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1148 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1149 | // VarDecl Implementation |
| 1150 | //===----------------------------------------------------------------------===// |
| 1151 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1152 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1153 | switch (SC) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1154 | case SC_None: break; |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1155 | case SC_Auto: return "auto"; |
| 1156 | case SC_Extern: return "extern"; |
| 1157 | case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>"; |
| 1158 | case SC_PrivateExtern: return "__private_extern__"; |
| 1159 | case SC_Register: return "register"; |
| 1160 | case SC_Static: return "static"; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1163 | llvm_unreachable("Invalid storage class"); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1166 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1167 | SourceLocation StartL, SourceLocation IdL, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1168 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 1169 | StorageClass S, StorageClass SCAsWritten) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1170 | return new (C) VarDecl(Var, DC, StartL, IdL, Id, T, TInfo, S, SCAsWritten); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1173 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 1174 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(VarDecl)); |
| 1175 | return new (Mem) VarDecl(Var, 0, SourceLocation(), SourceLocation(), 0, |
| 1176 | QualType(), 0, SC_None, SC_None); |
| 1177 | } |
| 1178 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1179 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1180 | assert(isLegalForVariable(SC)); |
| 1181 | if (getStorageClass() != SC) |
| 1182 | ClearLinkageCache(); |
| 1183 | |
John McCall | f1e4fbf | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1184 | VarDeclBits.SClass = SC; |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1187 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1188 | if (getInit()) |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1189 | return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); |
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 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1684 | bool FunctionDecl::isMain() const { |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 1685 | const TranslationUnitDecl *tunit = |
| 1686 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 1687 | return tunit && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1688 | !tunit->getASTContext().getLangOpts().Freestanding && |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 1689 | getIdentifier() && |
| 1690 | getIdentifier()->isStr("main"); |
| 1691 | } |
| 1692 | |
| 1693 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 1694 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 1695 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 1696 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 1697 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 1698 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 1699 | |
| 1700 | if (isa<CXXRecordDecl>(getDeclContext())) return false; |
| 1701 | assert(getDeclContext()->getRedeclContext()->isTranslationUnit()); |
| 1702 | |
| 1703 | const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>(); |
| 1704 | if (proto->getNumArgs() != 2 || proto->isVariadic()) return false; |
| 1705 | |
| 1706 | ASTContext &Context = |
| 1707 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 1708 | ->getASTContext(); |
| 1709 | |
| 1710 | // The result type and first argument type are constant across all |
| 1711 | // these operators. The second argument must be exactly void*. |
| 1712 | return (proto->getArgType(1).getCanonicalType() == Context.VoidPtrTy); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 1715 | bool FunctionDecl::isExternC() const { |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1716 | if (getLinkage() != ExternalLinkage) |
| 1717 | return false; |
| 1718 | |
| 1719 | if (getAttr<OverloadableAttr>()) |
| 1720 | return false; |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1721 | |
Chandler Carruth | 10aad44 | 2011-02-25 00:05:02 +0000 | [diff] [blame] | 1722 | const DeclContext *DC = getDeclContext(); |
| 1723 | if (DC->isRecord()) |
| 1724 | return false; |
| 1725 | |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1726 | ASTContext &Context = getASTContext(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1727 | if (!Context.getLangOpts().CPlusPlus) |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1728 | return true; |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1729 | |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 1730 | return isMain() || DC->isExternCContext(); |
Douglas Gregor | 6393519 | 2009-03-02 00:19:53 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1733 | bool FunctionDecl::isGlobal() const { |
| 1734 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 1735 | return Method->isStatic(); |
| 1736 | |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1737 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1738 | return false; |
| 1739 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 1741 | DC->isNamespace(); |
| 1742 | DC = DC->getParent()) { |
| 1743 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 1744 | if (!Namespace->getDeclName()) |
| 1745 | return false; |
| 1746 | break; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1753 | void |
| 1754 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 1755 | redeclarable_base::setPreviousDeclaration(PrevDecl); |
| 1756 | |
| 1757 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 1758 | FunctionTemplateDecl *PrevFunTmpl |
| 1759 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : 0; |
| 1760 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| 1761 | FunTmpl->setPreviousDeclaration(PrevFunTmpl); |
| 1762 | } |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1763 | |
Axel Naumann | d9d137e | 2011-11-08 18:21:06 +0000 | [diff] [blame] | 1764 | if (PrevDecl && PrevDecl->IsInline) |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1765 | IsInline = true; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
| 1769 | return getFirstDeclaration(); |
| 1770 | } |
| 1771 | |
| 1772 | FunctionDecl *FunctionDecl::getCanonicalDecl() { |
| 1773 | return getFirstDeclaration(); |
| 1774 | } |
| 1775 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1776 | void FunctionDecl::setStorageClass(StorageClass SC) { |
| 1777 | assert(isLegalForFunction(SC)); |
| 1778 | if (getStorageClass() != SC) |
| 1779 | ClearLinkageCache(); |
| 1780 | |
| 1781 | SClass = SC; |
| 1782 | } |
| 1783 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1784 | /// \brief Returns a value indicating whether this function |
| 1785 | /// corresponds to a builtin function. |
| 1786 | /// |
| 1787 | /// The function corresponds to a built-in function if it is |
| 1788 | /// declared at translation scope or within an extern "C" block and |
| 1789 | /// its name matches with the name of a builtin. The returned value |
| 1790 | /// 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] | 1791 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1792 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1793 | unsigned FunctionDecl::getBuiltinID() const { |
Daniel Dunbar | 60d302a | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 1794 | if (!getIdentifier()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1795 | return 0; |
| 1796 | |
| 1797 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
Daniel Dunbar | 60d302a | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 1798 | if (!BuiltinID) |
| 1799 | return 0; |
| 1800 | |
| 1801 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1802 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 1803 | return BuiltinID; |
| 1804 | |
| 1805 | // This function has the name of a known C library |
| 1806 | // function. Determine whether it actually refers to the C library |
| 1807 | // function or whether it just has the same name. |
| 1808 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1809 | // If this is a static function, it's not a builtin. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 1810 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 1811 | return 0; |
| 1812 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1813 | // If this function is at translation-unit scope and we're not in |
| 1814 | // C++, it refers to the C library function. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1815 | if (!Context.getLangOpts().CPlusPlus && |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1816 | getDeclContext()->isTranslationUnit()) |
| 1817 | return BuiltinID; |
| 1818 | |
| 1819 | // If the function is in an extern "C" linkage specification and is |
| 1820 | // not marked "overloadable", it's the real function. |
| 1821 | if (isa<LinkageSpecDecl>(getDeclContext()) && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | cast<LinkageSpecDecl>(getDeclContext())->getLanguage() |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1823 | == LinkageSpecDecl::lang_c && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1824 | !getAttr<OverloadableAttr>()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1825 | return BuiltinID; |
| 1826 | |
| 1827 | // Not a builtin |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 1828 | return 0; |
| 1829 | } |
| 1830 | |
| 1831 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1832 | /// getNumParams - Return the number of parameters this function must have |
Bob Wilson | 8dbfbf4 | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 1833 | /// based on its FunctionType. This is the length of the ParamInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 1834 | /// after it has been created. |
| 1835 | unsigned FunctionDecl::getNumParams() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1836 | const FunctionType *FT = getType()->getAs<FunctionType>(); |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1837 | if (isa<FunctionNoProtoType>(FT)) |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 1838 | return 0; |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 1839 | return cast<FunctionProtoType>(FT)->getNumArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 1843 | void FunctionDecl::setParams(ASTContext &C, |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1844 | llvm::ArrayRef<ParmVarDecl *> NewParamInfo) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1845 | assert(ParamInfo == 0 && "Already has param info!"); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1846 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
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 | // Zero params -> null pointer. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 1849 | if (!NewParamInfo.empty()) { |
| 1850 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 1851 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1852 | } |
| 1853 | } |
| 1854 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 1855 | void FunctionDecl::setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls) { |
| 1856 | assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!"); |
| 1857 | |
| 1858 | if (!NewDecls.empty()) { |
| 1859 | NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()]; |
| 1860 | std::copy(NewDecls.begin(), NewDecls.end(), A); |
| 1861 | DeclsInPrototypeScope = llvm::ArrayRef<NamedDecl*>(A, NewDecls.size()); |
| 1862 | } |
| 1863 | } |
| 1864 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1865 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 1866 | /// needed to call this function. This may be fewer than the number of |
| 1867 | /// function parameters, if some of the parameters have default |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1868 | /// arguments (in C++) or the last parameter is a parameter pack. |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1869 | unsigned FunctionDecl::getMinRequiredArguments() const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1870 | if (!getASTContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1871 | return getNumParams(); |
| 1872 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 1873 | unsigned NumRequiredArgs = getNumParams(); |
| 1874 | |
| 1875 | // If the last parameter is a parameter pack, we don't need an argument for |
| 1876 | // it. |
| 1877 | if (NumRequiredArgs > 0 && |
| 1878 | getParamDecl(NumRequiredArgs - 1)->isParameterPack()) |
| 1879 | --NumRequiredArgs; |
| 1880 | |
| 1881 | // If this parameter has a default argument, we don't need an argument for |
| 1882 | // it. |
| 1883 | while (NumRequiredArgs > 0 && |
| 1884 | getParamDecl(NumRequiredArgs-1)->hasDefaultArg()) |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1885 | --NumRequiredArgs; |
| 1886 | |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 1887 | // We might have parameter packs before the end. These can't be deduced, |
| 1888 | // but they can still handle multiple arguments. |
| 1889 | unsigned ArgIdx = NumRequiredArgs; |
| 1890 | while (ArgIdx > 0) { |
| 1891 | if (getParamDecl(ArgIdx - 1)->isParameterPack()) |
| 1892 | NumRequiredArgs = ArgIdx; |
| 1893 | |
| 1894 | --ArgIdx; |
| 1895 | } |
| 1896 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1897 | return NumRequiredArgs; |
| 1898 | } |
| 1899 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1900 | bool FunctionDecl::isInlined() const { |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 1901 | if (IsInline) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1902 | return true; |
Anders Carlsson | 48eda2c | 2009-12-04 22:35:50 +0000 | [diff] [blame] | 1903 | |
| 1904 | if (isa<CXXMethodDecl>(this)) { |
| 1905 | if (!isOutOfLine() || getCanonicalDecl()->isInlineSpecified()) |
| 1906 | return true; |
| 1907 | } |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1908 | |
| 1909 | switch (getTemplateSpecializationKind()) { |
| 1910 | case TSK_Undeclared: |
| 1911 | case TSK_ExplicitSpecialization: |
| 1912 | return false; |
| 1913 | |
| 1914 | case TSK_ImplicitInstantiation: |
| 1915 | case TSK_ExplicitInstantiationDeclaration: |
| 1916 | case TSK_ExplicitInstantiationDefinition: |
| 1917 | // Handle below. |
| 1918 | break; |
| 1919 | } |
| 1920 | |
| 1921 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1922 | bool HasPattern = false; |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1923 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1924 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1925 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1926 | if (HasPattern && PatternDecl) |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 1927 | return PatternDecl->isInlined(); |
| 1928 | |
| 1929 | return false; |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1932 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 1933 | // Only consider file-scope declarations in this test. |
| 1934 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 1935 | return false; |
| 1936 | |
| 1937 | // Only consider explicit declarations; the presence of a builtin for a |
| 1938 | // libcall shouldn't affect whether a definition is externally visible. |
| 1939 | if (Redecl->isImplicit()) |
| 1940 | return false; |
| 1941 | |
| 1942 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| 1943 | return true; // Not an inline definition |
| 1944 | |
| 1945 | return false; |
| 1946 | } |
| 1947 | |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1948 | /// \brief For a function declaration in C or C++, determine whether this |
| 1949 | /// declaration causes the definition to be externally visible. |
| 1950 | /// |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1951 | /// Specifically, this determines if adding the current declaration to the set |
| 1952 | /// of redeclarations of the given functions causes |
| 1953 | /// isInlineDefinitionExternallyVisible to change from false to true. |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1954 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| 1955 | assert(!doesThisDeclarationHaveABody() && |
| 1956 | "Must have a declaration without a body."); |
| 1957 | |
| 1958 | ASTContext &Context = getASTContext(); |
| 1959 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1960 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1961 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 1962 | // an externally visible definition. |
| 1963 | // |
| 1964 | // FIXME: What happens if gnu_inline gets added on after the first |
| 1965 | // declaration? |
| 1966 | if (!isInlineSpecified() || getStorageClassAsWritten() == SC_Extern) |
| 1967 | return false; |
| 1968 | |
| 1969 | const FunctionDecl *Prev = this; |
| 1970 | bool FoundBody = false; |
| 1971 | while ((Prev = Prev->getPreviousDecl())) { |
| 1972 | FoundBody |= Prev->Body; |
| 1973 | |
| 1974 | if (Prev->Body) { |
| 1975 | // If it's not the case that both 'inline' and 'extern' are |
| 1976 | // specified on the definition, then it is always externally visible. |
| 1977 | if (!Prev->isInlineSpecified() || |
| 1978 | Prev->getStorageClassAsWritten() != SC_Extern) |
| 1979 | return false; |
| 1980 | } else if (Prev->isInlineSpecified() && |
| 1981 | Prev->getStorageClassAsWritten() != SC_Extern) { |
| 1982 | return false; |
| 1983 | } |
| 1984 | } |
| 1985 | return FoundBody; |
| 1986 | } |
| 1987 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1988 | if (Context.getLangOpts().CPlusPlus) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 1989 | return false; |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 1990 | |
| 1991 | // C99 6.7.4p6: |
| 1992 | // [...] If all of the file scope declarations for a function in a |
| 1993 | // translation unit include the inline function specifier without extern, |
| 1994 | // then the definition in that translation unit is an inline definition. |
| 1995 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
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 | const FunctionDecl *Prev = this; |
| 1998 | bool FoundBody = false; |
| 1999 | while ((Prev = Prev->getPreviousDecl())) { |
| 2000 | FoundBody |= Prev->Body; |
| 2001 | if (RedeclForcesDefC99(Prev)) |
| 2002 | return false; |
| 2003 | } |
| 2004 | return FoundBody; |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 2007 | /// \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] | 2008 | /// definition will be externally visible. |
| 2009 | /// |
| 2010 | /// Inline function definitions are always available for inlining optimizations. |
| 2011 | /// However, depending on the language dialect, declaration specifiers, and |
| 2012 | /// attributes, the definition of an inline function may or may not be |
| 2013 | /// "externally" visible to other translation units in the program. |
| 2014 | /// |
| 2015 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 1e5fd7f | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 2016 | /// 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] | 2017 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 2018 | /// |
| 2019 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 2020 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 2021 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 2022 | /// an externally visible symbol, but "extern inline" will not create an |
| 2023 | /// externally visible symbol. |
| 2024 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2025 | assert(doesThisDeclarationHaveABody() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2026 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 2027 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2028 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2029 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2030 | // Note: If you change the logic here, please change |
| 2031 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 2032 | // |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2033 | // If it's not the case that both 'inline' and 'extern' are |
| 2034 | // specified on the definition, then this inline definition is |
| 2035 | // externally visible. |
| 2036 | if (!(isInlineSpecified() && getStorageClassAsWritten() == SC_Extern)) |
| 2037 | return true; |
| 2038 | |
| 2039 | // If any declaration is 'inline' but not 'extern', then this definition |
| 2040 | // is externally visible. |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2041 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 2042 | Redecl != RedeclEnd; |
| 2043 | ++Redecl) { |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2044 | if (Redecl->isInlineSpecified() && |
| 2045 | Redecl->getStorageClassAsWritten() != SC_Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2046 | return true; |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2047 | } |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2048 | |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2049 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2050 | } |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2051 | |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2052 | // C99 6.7.4p6: |
| 2053 | // [...] If all of the file scope declarations for a function in a |
| 2054 | // translation unit include the inline function specifier without extern, |
| 2055 | // then the definition in that translation unit is an inline definition. |
| 2056 | for (redecl_iterator Redecl = redecls_begin(), RedeclEnd = redecls_end(); |
| 2057 | Redecl != RedeclEnd; |
| 2058 | ++Redecl) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2059 | if (RedeclForcesDefC99(*Redecl)) |
| 2060 | return true; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | // C99 6.7.4p6: |
| 2064 | // An inline definition does not provide an external definition for the |
| 2065 | // function, and does not forbid an external definition in another |
| 2066 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2067 | return false; |
| 2068 | } |
| 2069 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2070 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 2071 | /// function represents, if any. |
| 2072 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 2073 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 2074 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2075 | else |
| 2076 | return OO_None; |
| 2077 | } |
| 2078 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2079 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 2080 | /// represents, if any. |
| 2081 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 2082 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 2083 | return getDeclName().getCXXLiteralIdentifier(); |
| 2084 | else |
| 2085 | return 0; |
| 2086 | } |
| 2087 | |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2088 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 2089 | if (TemplateOrSpecialization.isNull()) |
| 2090 | return TK_NonTemplate; |
| 2091 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 2092 | return TK_FunctionTemplate; |
| 2093 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 2094 | return TK_MemberSpecialization; |
| 2095 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 2096 | return TK_FunctionTemplateSpecialization; |
| 2097 | if (TemplateOrSpecialization.is |
| 2098 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 2099 | return TK_DependentFunctionTemplateSpecialization; |
| 2100 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2101 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2104 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2105 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2106 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| 2107 | |
| 2108 | return 0; |
| 2109 | } |
| 2110 | |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2111 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 2112 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 2113 | } |
| 2114 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2115 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2116 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 2117 | FunctionDecl *FD, |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2118 | TemplateSpecializationKind TSK) { |
| 2119 | assert(TemplateOrSpecialization.isNull() && |
| 2120 | "Member function is already a specialization"); |
| 2121 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2122 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2123 | TemplateOrSpecialization = Info; |
| 2124 | } |
| 2125 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2126 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2127 | // If the function is invalid, it can't be implicitly instantiated. |
| 2128 | if (isInvalidDecl()) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2129 | return false; |
| 2130 | |
| 2131 | switch (getTemplateSpecializationKind()) { |
| 2132 | case TSK_Undeclared: |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2133 | case TSK_ExplicitInstantiationDefinition: |
| 2134 | return false; |
| 2135 | |
| 2136 | case TSK_ImplicitInstantiation: |
| 2137 | return true; |
| 2138 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2139 | // It is possible to instantiate TSK_ExplicitSpecialization kind |
| 2140 | // if the FunctionDecl has a class scope specialization pattern. |
| 2141 | case TSK_ExplicitSpecialization: |
| 2142 | return getClassScopeSpecializationPattern() != 0; |
| 2143 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2144 | case TSK_ExplicitInstantiationDeclaration: |
| 2145 | // Handled below. |
| 2146 | break; |
| 2147 | } |
| 2148 | |
| 2149 | // Find the actual template from which we will instantiate. |
| 2150 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2151 | bool HasPattern = false; |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2152 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2153 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2154 | |
| 2155 | // C++0x [temp.explicit]p9: |
| 2156 | // Except for inline functions, other explicit instantiation declarations |
| 2157 | // have the effect of suppressing the implicit instantiation of the entity |
| 2158 | // to which they refer. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2159 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2160 | return true; |
| 2161 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2162 | return PatternDecl->isInlined(); |
Ted Kremenek | 75df4ee | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
| 2165 | bool FunctionDecl::isTemplateInstantiation() const { |
| 2166 | switch (getTemplateSpecializationKind()) { |
| 2167 | case TSK_Undeclared: |
| 2168 | case TSK_ExplicitSpecialization: |
| 2169 | return false; |
| 2170 | case TSK_ImplicitInstantiation: |
| 2171 | case TSK_ExplicitInstantiationDeclaration: |
| 2172 | case TSK_ExplicitInstantiationDefinition: |
| 2173 | return true; |
| 2174 | } |
| 2175 | llvm_unreachable("All TSK values handled."); |
| 2176 | } |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2177 | |
| 2178 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2179 | // Handle class scope explicit specialization special case. |
| 2180 | if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 2181 | return getClassScopeSpecializationPattern(); |
| 2182 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2183 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 2184 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 2185 | // If we have hit a point where the user provided a specialization of |
| 2186 | // this template, we're done looking. |
| 2187 | if (Primary->isMemberSpecialization()) |
| 2188 | break; |
| 2189 | |
| 2190 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 2191 | } |
| 2192 | |
| 2193 | return Primary->getTemplatedDecl(); |
| 2194 | } |
| 2195 | |
| 2196 | return getInstantiatedFromMemberFunction(); |
| 2197 | } |
| 2198 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2199 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2201 | = TemplateOrSpecialization |
| 2202 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2203 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2204 | } |
| 2205 | return 0; |
| 2206 | } |
| 2207 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2208 | FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const { |
| 2209 | return getASTContext().getClassScopeSpecializationPattern(this); |
| 2210 | } |
| 2211 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2212 | const TemplateArgumentList * |
| 2213 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 2215 | = TemplateOrSpecialization |
| 2216 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2217 | return Info->TemplateArguments; |
| 2218 | } |
| 2219 | return 0; |
| 2220 | } |
| 2221 | |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 2222 | const ASTTemplateArgumentListInfo * |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2223 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 2224 | if (FunctionTemplateSpecializationInfo *Info |
| 2225 | = TemplateOrSpecialization |
| 2226 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 2227 | return Info->TemplateArgumentsAsWritten; |
| 2228 | } |
| 2229 | return 0; |
| 2230 | } |
| 2231 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2233 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 2234 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 2235 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2236 | void *InsertPos, |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 2237 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 7b081c8 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 2238 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 2239 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 2240 | assert(TSK != TSK_Undeclared && |
| 2241 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2242 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 2243 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2244 | if (!Info) |
Argyrios Kyrtzidis | a626a3d | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 2245 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 2246 | TemplateArgs, |
| 2247 | TemplateArgsAsWritten, |
| 2248 | PointOfInstantiation); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2249 | TemplateOrSpecialization = Info; |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 2250 | Template->addSpecialization(Info, InsertPos); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2253 | void |
| 2254 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 2255 | const UnresolvedSetImpl &Templates, |
| 2256 | const TemplateArgumentListInfo &TemplateArgs) { |
| 2257 | assert(TemplateOrSpecialization.isNull()); |
| 2258 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 2259 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 21c0160 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 2260 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 2261 | void *Buffer = Context.Allocate(Size); |
| 2262 | DependentFunctionTemplateSpecializationInfo *Info = |
| 2263 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 2264 | TemplateArgs); |
| 2265 | TemplateOrSpecialization = Info; |
| 2266 | } |
| 2267 | |
| 2268 | DependentFunctionTemplateSpecializationInfo:: |
| 2269 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 2270 | const TemplateArgumentListInfo &TArgs) |
| 2271 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 2272 | |
| 2273 | d.NumTemplates = Ts.size(); |
| 2274 | d.NumArgs = TArgs.size(); |
| 2275 | |
| 2276 | FunctionTemplateDecl **TsArray = |
| 2277 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 2278 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 2279 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 2280 | |
| 2281 | TemplateArgumentLoc *ArgsArray = |
| 2282 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 2283 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 2284 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 2285 | } |
| 2286 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2287 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2288 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2289 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2290 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2291 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2292 | if (FTSInfo) |
| 2293 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2295 | MemberSpecializationInfo *MSInfo |
| 2296 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 2297 | if (MSInfo) |
| 2298 | return MSInfo->getTemplateSpecializationKind(); |
| 2299 | |
| 2300 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2304 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2305 | SourceLocation PointOfInstantiation) { |
| 2306 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 2307 | = TemplateOrSpecialization.dyn_cast< |
| 2308 | FunctionTemplateSpecializationInfo*>()) { |
| 2309 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 2310 | if (TSK != TSK_ExplicitSpecialization && |
| 2311 | PointOfInstantiation.isValid() && |
| 2312 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 2313 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2314 | } else if (MemberSpecializationInfo *MSInfo |
| 2315 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 2316 | MSInfo->setTemplateSpecializationKind(TSK); |
| 2317 | if (TSK != TSK_ExplicitSpecialization && |
| 2318 | PointOfInstantiation.isValid() && |
| 2319 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 2320 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 2321 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2322 | llvm_unreachable("Function cannot have a template specialization kind"); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2326 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 2327 | = TemplateOrSpecialization.dyn_cast< |
| 2328 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2329 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2330 | else if (MemberSpecializationInfo *MSInfo |
| 2331 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2332 | return MSInfo->getPointOfInstantiation(); |
| 2333 | |
| 2334 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2337 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2338 | if (Decl::isOutOfLine()) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2339 | return true; |
| 2340 | |
| 2341 | // If this function was instantiated from a member function of a |
| 2342 | // class template, check whether that member function was defined out-of-line. |
| 2343 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 2344 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2345 | if (FD->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2346 | return Definition->isOutOfLine(); |
| 2347 | } |
| 2348 | |
| 2349 | // If this function was instantiated from a function template, |
| 2350 | // check whether that function template was defined out-of-line. |
| 2351 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 2352 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2353 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 2354 | return Definition->isOutOfLine(); |
| 2355 | } |
| 2356 | |
| 2357 | return false; |
| 2358 | } |
| 2359 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2360 | SourceRange FunctionDecl::getSourceRange() const { |
| 2361 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 2362 | } |
| 2363 | |
Anna Zaks | 9392d4e | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 2364 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2365 | IdentifierInfo *FnInfo = getIdentifier(); |
| 2366 | |
| 2367 | if (!FnInfo) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2368 | return 0; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2369 | |
| 2370 | // Builtin handling. |
| 2371 | switch (getBuiltinID()) { |
| 2372 | case Builtin::BI__builtin_memset: |
| 2373 | case Builtin::BI__builtin___memset_chk: |
| 2374 | case Builtin::BImemset: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2375 | return Builtin::BImemset; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2376 | |
| 2377 | case Builtin::BI__builtin_memcpy: |
| 2378 | case Builtin::BI__builtin___memcpy_chk: |
| 2379 | case Builtin::BImemcpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2380 | return Builtin::BImemcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2381 | |
| 2382 | case Builtin::BI__builtin_memmove: |
| 2383 | case Builtin::BI__builtin___memmove_chk: |
| 2384 | case Builtin::BImemmove: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2385 | return Builtin::BImemmove; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2386 | |
| 2387 | case Builtin::BIstrlcpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2388 | return Builtin::BIstrlcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2389 | case Builtin::BIstrlcat: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2390 | return Builtin::BIstrlcat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2391 | |
| 2392 | case Builtin::BI__builtin_memcmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2393 | case Builtin::BImemcmp: |
| 2394 | return Builtin::BImemcmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2395 | |
| 2396 | case Builtin::BI__builtin_strncpy: |
| 2397 | case Builtin::BI__builtin___strncpy_chk: |
| 2398 | case Builtin::BIstrncpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2399 | return Builtin::BIstrncpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2400 | |
| 2401 | case Builtin::BI__builtin_strncmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2402 | case Builtin::BIstrncmp: |
| 2403 | return Builtin::BIstrncmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2404 | |
| 2405 | case Builtin::BI__builtin_strncasecmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2406 | case Builtin::BIstrncasecmp: |
| 2407 | return Builtin::BIstrncasecmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2408 | |
| 2409 | case Builtin::BI__builtin_strncat: |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2410 | case Builtin::BI__builtin___strncat_chk: |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2411 | case Builtin::BIstrncat: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2412 | return Builtin::BIstrncat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2413 | |
| 2414 | case Builtin::BI__builtin_strndup: |
| 2415 | case Builtin::BIstrndup: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2416 | return Builtin::BIstrndup; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2417 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2418 | case Builtin::BI__builtin_strlen: |
| 2419 | case Builtin::BIstrlen: |
| 2420 | return Builtin::BIstrlen; |
| 2421 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2422 | default: |
Eli Friedman | 750dc2b | 2012-01-15 01:23:58 +0000 | [diff] [blame] | 2423 | if (isExternC()) { |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2424 | if (FnInfo->isStr("memset")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2425 | return Builtin::BImemset; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2426 | else if (FnInfo->isStr("memcpy")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2427 | return Builtin::BImemcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2428 | else if (FnInfo->isStr("memmove")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2429 | return Builtin::BImemmove; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2430 | else if (FnInfo->isStr("memcmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2431 | return Builtin::BImemcmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2432 | else if (FnInfo->isStr("strncpy")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2433 | return Builtin::BIstrncpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2434 | else if (FnInfo->isStr("strncmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2435 | return Builtin::BIstrncmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2436 | else if (FnInfo->isStr("strncasecmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2437 | return Builtin::BIstrncasecmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2438 | else if (FnInfo->isStr("strncat")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2439 | return Builtin::BIstrncat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2440 | else if (FnInfo->isStr("strndup")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2441 | return Builtin::BIstrndup; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 2442 | else if (FnInfo->isStr("strlen")) |
| 2443 | return Builtin::BIstrlen; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2444 | } |
| 2445 | break; |
| 2446 | } |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 2447 | return 0; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2450 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2451 | // FieldDecl Implementation |
| 2452 | //===----------------------------------------------------------------------===// |
| 2453 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2454 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2455 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2456 | IdentifierInfo *Id, QualType T, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2457 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
| 2458 | bool HasInit) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2459 | return new (C) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2460 | BW, Mutable, HasInit); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2461 | } |
| 2462 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2463 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2464 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FieldDecl)); |
| 2465 | return new (Mem) FieldDecl(Field, 0, SourceLocation(), SourceLocation(), |
| 2466 | 0, QualType(), 0, 0, false, false); |
| 2467 | } |
| 2468 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2469 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 2470 | if (!isImplicit() || getDeclName()) |
| 2471 | return false; |
| 2472 | |
| 2473 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 2474 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 2475 | |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2479 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 2480 | assert(isBitField() && "not a bitfield"); |
| 2481 | Expr *BitWidth = InitializerOrBitWidth.getPointer(); |
| 2482 | return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue(); |
| 2483 | } |
| 2484 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2485 | unsigned FieldDecl::getFieldIndex() const { |
| 2486 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 2487 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2488 | unsigned Index = 0; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2489 | const RecordDecl *RD = getParent(); |
| 2490 | const FieldDecl *LastFD = 0; |
| 2491 | bool IsMsStruct = RD->hasAttr<MsStructAttr>(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2492 | |
| 2493 | for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end(); |
| 2494 | I != E; ++I, ++Index) { |
| 2495 | (*I)->CachedFieldIndex = Index + 1; |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2496 | |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2497 | if (IsMsStruct) { |
| 2498 | // Zero-length bitfields following non-bitfield members are ignored. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2499 | if (getASTContext().ZeroBitfieldFollowsNonBitfield((*I), LastFD)) { |
| 2500 | --Index; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2501 | continue; |
| 2502 | } |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2503 | LastFD = (*I); |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 2504 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2507 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 2508 | return CachedFieldIndex - 1; |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 2509 | } |
| 2510 | |
Abramo Bagnara | f2cf562 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 2511 | SourceRange FieldDecl::getSourceRange() const { |
Abramo Bagnara | d330e23 | 2011-08-05 08:02:55 +0000 | [diff] [blame] | 2512 | if (const Expr *E = InitializerOrBitWidth.getPointer()) |
| 2513 | return SourceRange(getInnerLocStart(), E->getLocEnd()); |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2514 | return DeclaratorDecl::getSourceRange(); |
Abramo Bagnara | f2cf562 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2517 | void FieldDecl::setInClassInitializer(Expr *Init) { |
| 2518 | assert(!InitializerOrBitWidth.getPointer() && |
| 2519 | "bit width or initializer already set"); |
| 2520 | InitializerOrBitWidth.setPointer(Init); |
| 2521 | InitializerOrBitWidth.setInt(0); |
| 2522 | } |
| 2523 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2524 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2525 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2526 | //===----------------------------------------------------------------------===// |
| 2527 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2528 | SourceLocation TagDecl::getOuterLocStart() const { |
| 2529 | return getTemplateOrInnerLocStart(this); |
| 2530 | } |
| 2531 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 2532 | SourceRange TagDecl::getSourceRange() const { |
| 2533 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2534 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 2537 | TagDecl* TagDecl::getCanonicalDecl() { |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2538 | return getFirstDeclaration(); |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2541 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
| 2542 | TypedefNameDeclOrQualifier = TDD; |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 2543 | if (TypeForDecl) |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2544 | const_cast<Type*>(TypeForDecl)->ClearLinkageCache(); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 2545 | ClearLinkageCache(); |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2548 | void TagDecl::startDefinition() { |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2549 | IsBeingDefined = true; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2550 | |
| 2551 | if (isa<CXXRecordDecl>(this)) { |
| 2552 | CXXRecordDecl *D = cast<CXXRecordDecl>(this); |
| 2553 | struct CXXRecordDecl::DefinitionData *Data = |
| 2554 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
John McCall | 2243288 | 2010-03-26 21:56:38 +0000 | [diff] [blame] | 2555 | for (redecl_iterator I = redecls_begin(), E = redecls_end(); I != E; ++I) |
| 2556 | cast<CXXRecordDecl>(*I)->DefinitionData = Data; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 2557 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | void TagDecl::completeDefinition() { |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 2561 | assert((!isa<CXXRecordDecl>(this) || |
| 2562 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 2563 | "definition completed but not started"); |
| 2564 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2565 | IsCompleteDefinition = true; |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 2566 | IsBeingDefined = false; |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 2567 | |
| 2568 | if (ASTMutationListener *L = getASTMutationListener()) |
| 2569 | L->CompletedTagDefinition(this); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2572 | TagDecl *TagDecl::getDefinition() const { |
| 2573 | if (isCompleteDefinition()) |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2574 | return const_cast<TagDecl *>(this); |
Andrew Trick | 220a9c8 | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 2575 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| 2576 | return CXXRD->getDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | |
| 2578 | for (redecl_iterator R = redecls_begin(), REnd = redecls_end(); |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2579 | R != REnd; ++R) |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2580 | if (R->isCompleteDefinition()) |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2581 | return *R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 2583 | return 0; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2586 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 2587 | if (QualifierLoc) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2588 | // Make sure the extended qualifier info is allocated. |
| 2589 | if (!hasExtInfo()) |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2590 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2591 | // Set qualifier info. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2592 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 2593 | } else { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2594 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2595 | if (hasExtInfo()) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2596 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 2597 | getASTContext().Deallocate(getExtInfo()); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2598 | TypedefNameDeclOrQualifier = (TypedefNameDecl*) 0; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2599 | } |
| 2600 | else |
| 2601 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | } |
| 2605 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2606 | void TagDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 2607 | unsigned NumTPLists, |
| 2608 | TemplateParameterList **TPLists) { |
| 2609 | assert(NumTPLists > 0); |
| 2610 | // Make sure the extended decl info is allocated. |
| 2611 | if (!hasExtInfo()) |
| 2612 | // Allocate external info struct. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2613 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 2614 | // Set the template parameter lists info. |
| 2615 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 2616 | } |
| 2617 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2618 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2619 | // EnumDecl Implementation |
| 2620 | //===----------------------------------------------------------------------===// |
| 2621 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2622 | void EnumDecl::anchor() { } |
| 2623 | |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2624 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 2625 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2626 | IdentifierInfo *Id, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2627 | EnumDecl *PrevDecl, bool IsScoped, |
| 2628 | bool IsScopedUsingClassTag, bool IsFixed) { |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2629 | EnumDecl *Enum = new (C) EnumDecl(DC, StartLoc, IdLoc, Id, PrevDecl, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2630 | IsScoped, IsScopedUsingClassTag, IsFixed); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2631 | C.getTypeDeclType(Enum, PrevDecl); |
| 2632 | return Enum; |
| 2633 | } |
| 2634 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2635 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2636 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumDecl)); |
| 2637 | return new (Mem) EnumDecl(0, SourceLocation(), SourceLocation(), 0, 0, |
| 2638 | false, false, false); |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2641 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2642 | QualType NewPromotionType, |
| 2643 | unsigned NumPositiveBits, |
| 2644 | unsigned NumNegativeBits) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2645 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2646 | if (!IntegerType) |
| 2647 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2648 | PromotionType = NewPromotionType; |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 2649 | setNumPositiveBits(NumPositiveBits); |
| 2650 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2651 | TagDecl::completeDefinition(); |
| 2652 | } |
| 2653 | |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2654 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 2655 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2656 | return MSI->getTemplateSpecializationKind(); |
| 2657 | |
| 2658 | return TSK_Undeclared; |
| 2659 | } |
| 2660 | |
| 2661 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2662 | SourceLocation PointOfInstantiation) { |
| 2663 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 2664 | assert(MSI && "Not an instantiated member enumeration?"); |
| 2665 | MSI->setTemplateSpecializationKind(TSK); |
| 2666 | if (TSK != TSK_ExplicitSpecialization && |
| 2667 | PointOfInstantiation.isValid() && |
| 2668 | MSI->getPointOfInstantiation().isInvalid()) |
| 2669 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 2670 | } |
| 2671 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 2672 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 2673 | if (SpecializationInfo) |
| 2674 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 2675 | |
| 2676 | return 0; |
| 2677 | } |
| 2678 | |
| 2679 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 2680 | TemplateSpecializationKind TSK) { |
| 2681 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 2682 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 2683 | } |
| 2684 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2685 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2686 | // RecordDecl Implementation |
| 2687 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2688 | |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2689 | RecordDecl::RecordDecl(Kind DK, TagKind TK, DeclContext *DC, |
| 2690 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2691 | IdentifierInfo *Id, RecordDecl *PrevDecl) |
| 2692 | : TagDecl(DK, TK, DC, IdLoc, Id, PrevDecl, StartLoc) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2693 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2694 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 2695 | HasObjectMember = false; |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2696 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2697 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2700 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 2701 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2702 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
| 2703 | RecordDecl* R = new (C) RecordDecl(Record, TK, DC, StartLoc, IdLoc, Id, |
| 2704 | PrevDecl); |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 2705 | C.getTypeDeclType(R, PrevDecl); |
| 2706 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2709 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| 2710 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(RecordDecl)); |
| 2711 | return new (Mem) RecordDecl(Record, TTK_Struct, 0, SourceLocation(), |
| 2712 | SourceLocation(), 0, 0); |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2715 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2716 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 2717 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 2718 | } |
| 2719 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2720 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 2721 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 2722 | LoadFieldsFromExternalStorage(); |
| 2723 | |
| 2724 | return field_iterator(decl_iterator(FirstDecl)); |
| 2725 | } |
| 2726 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2727 | /// completeDefinition - Notes that the definition of this type is now |
| 2728 | /// complete. |
| 2729 | void RecordDecl::completeDefinition() { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2730 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2731 | TagDecl::completeDefinition(); |
| 2732 | } |
| 2733 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2734 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 2735 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 2736 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 2737 | |
| 2738 | // Notify that we have a RecordDecl doing some initialization. |
| 2739 | ExternalASTSource::Deserializing TheFields(Source); |
| 2740 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2741 | SmallVector<Decl*, 64> Decls; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 2742 | LoadedFieldsFromExternalStorage = true; |
| 2743 | switch (Source->FindExternalLexicalDeclsBy<FieldDecl>(this, Decls)) { |
| 2744 | case ELR_Success: |
| 2745 | break; |
| 2746 | |
| 2747 | case ELR_AlreadyLoaded: |
| 2748 | case ELR_Failure: |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2749 | return; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 2750 | } |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2751 | |
| 2752 | #ifndef NDEBUG |
| 2753 | // Check that all decls we got were FieldDecls. |
| 2754 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
| 2755 | assert(isa<FieldDecl>(Decls[i])); |
| 2756 | #endif |
| 2757 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2758 | if (Decls.empty()) |
| 2759 | return; |
| 2760 | |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 2761 | llvm::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
| 2762 | /*FieldsAlreadyLoaded=*/false); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 2765 | //===----------------------------------------------------------------------===// |
| 2766 | // BlockDecl Implementation |
| 2767 | //===----------------------------------------------------------------------===// |
| 2768 | |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2769 | void BlockDecl::setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) { |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2770 | assert(ParamInfo == 0 && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2772 | // Zero params -> null pointer. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2773 | if (!NewParamInfo.empty()) { |
| 2774 | NumParams = NewParamInfo.size(); |
| 2775 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 2776 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2777 | } |
| 2778 | } |
| 2779 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2780 | void BlockDecl::setCaptures(ASTContext &Context, |
| 2781 | const Capture *begin, |
| 2782 | const Capture *end, |
| 2783 | bool capturesCXXThis) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2784 | CapturesCXXThis = capturesCXXThis; |
| 2785 | |
| 2786 | if (begin == end) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2787 | NumCaptures = 0; |
| 2788 | Captures = 0; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2789 | return; |
| 2790 | } |
| 2791 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2792 | NumCaptures = end - begin; |
| 2793 | |
| 2794 | // Avoid new Capture[] because we don't want to provide a default |
| 2795 | // constructor. |
| 2796 | size_t allocationSize = NumCaptures * sizeof(Capture); |
| 2797 | void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*)); |
| 2798 | memcpy(buffer, begin, allocationSize); |
| 2799 | Captures = static_cast<Capture*>(buffer); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 2800 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2801 | |
John McCall | 204e133 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 2802 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
| 2803 | for (capture_const_iterator |
| 2804 | i = capture_begin(), e = capture_end(); i != e; ++i) |
| 2805 | // Only auto vars can be captured, so no redeclaration worries. |
| 2806 | if (i->getVariable() == variable) |
| 2807 | return true; |
| 2808 | |
| 2809 | return false; |
| 2810 | } |
| 2811 | |
Douglas Gregor | 2fcbcef | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 2812 | SourceRange BlockDecl::getSourceRange() const { |
| 2813 | return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); |
| 2814 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2815 | |
| 2816 | //===----------------------------------------------------------------------===// |
| 2817 | // Other Decl Allocation/Deallocation Method Implementations |
| 2818 | //===----------------------------------------------------------------------===// |
| 2819 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2820 | void TranslationUnitDecl::anchor() { } |
| 2821 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2822 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 2823 | return new (C) TranslationUnitDecl(C); |
| 2824 | } |
| 2825 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2826 | void LabelDecl::anchor() { } |
| 2827 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2828 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 6784304 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 2829 | SourceLocation IdentL, IdentifierInfo *II) { |
| 2830 | return new (C) LabelDecl(DC, IdentL, II, 0, IdentL); |
| 2831 | } |
| 2832 | |
| 2833 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 2834 | SourceLocation IdentL, IdentifierInfo *II, |
| 2835 | SourceLocation GnuLabelL) { |
| 2836 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
| 2837 | return new (C) LabelDecl(DC, IdentL, II, 0, GnuLabelL); |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2840 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2841 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(LabelDecl)); |
| 2842 | return new (Mem) LabelDecl(0, SourceLocation(), 0, 0, SourceLocation()); |
Douglas Gregor | 06c9193 | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2845 | void ValueDecl::anchor() { } |
| 2846 | |
| 2847 | void ImplicitParamDecl::anchor() { } |
| 2848 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2849 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2850 | SourceLocation IdLoc, |
| 2851 | IdentifierInfo *Id, |
| 2852 | QualType Type) { |
| 2853 | return new (C) ImplicitParamDecl(DC, IdLoc, Id, Type); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2856 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
| 2857 | unsigned ID) { |
| 2858 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(ImplicitParamDecl)); |
| 2859 | return new (Mem) ImplicitParamDecl(0, SourceLocation(), 0, QualType()); |
| 2860 | } |
| 2861 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2862 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2863 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2864 | const DeclarationNameInfo &NameInfo, |
| 2865 | QualType T, TypeSourceInfo *TInfo, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2866 | StorageClass SC, StorageClass SCAsWritten, |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2867 | bool isInlineSpecified, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2868 | bool hasWrittenPrototype, |
| 2869 | bool isConstexprSpecified) { |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2870 | FunctionDecl *New = new (C) FunctionDecl(Function, DC, StartLoc, NameInfo, |
| 2871 | T, TInfo, SC, SCAsWritten, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 2872 | isInlineSpecified, |
| 2873 | isConstexprSpecified); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2874 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 2875 | return New; |
| 2876 | } |
| 2877 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2878 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2879 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FunctionDecl)); |
| 2880 | return new (Mem) FunctionDecl(Function, 0, SourceLocation(), |
| 2881 | DeclarationNameInfo(), QualType(), 0, |
| 2882 | SC_None, SC_None, false, false); |
| 2883 | } |
| 2884 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2885 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 2886 | return new (C) BlockDecl(DC, L); |
| 2887 | } |
| 2888 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2889 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2890 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(BlockDecl)); |
| 2891 | return new (Mem) BlockDecl(0, SourceLocation()); |
| 2892 | } |
| 2893 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2894 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 2895 | SourceLocation L, |
| 2896 | IdentifierInfo *Id, QualType T, |
| 2897 | Expr *E, const llvm::APSInt &V) { |
| 2898 | return new (C) EnumConstantDecl(CD, L, Id, T, E, V); |
| 2899 | } |
| 2900 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2901 | EnumConstantDecl * |
| 2902 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2903 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(EnumConstantDecl)); |
| 2904 | return new (Mem) EnumConstantDecl(0, SourceLocation(), 0, QualType(), 0, |
| 2905 | llvm::APSInt()); |
| 2906 | } |
| 2907 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2908 | void IndirectFieldDecl::anchor() { } |
| 2909 | |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 2910 | IndirectFieldDecl * |
| 2911 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 2912 | IdentifierInfo *Id, QualType T, NamedDecl **CH, |
| 2913 | unsigned CHS) { |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2914 | return new (C) IndirectFieldDecl(DC, L, Id, T, CH, CHS); |
| 2915 | } |
| 2916 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2917 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 2918 | unsigned ID) { |
| 2919 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(IndirectFieldDecl)); |
| 2920 | return new (Mem) IndirectFieldDecl(0, SourceLocation(), DeclarationName(), |
| 2921 | QualType(), 0, 0); |
| 2922 | } |
| 2923 | |
Douglas Gregor | 8e7139c | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 2924 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 2925 | SourceLocation End = getLocation(); |
| 2926 | if (Init) |
| 2927 | End = Init->getLocEnd(); |
| 2928 | return SourceRange(getLocation(), End); |
| 2929 | } |
| 2930 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2931 | void TypeDecl::anchor() { } |
| 2932 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2933 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 2934 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2935 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
| 2936 | return new (C) TypedefDecl(DC, StartLoc, IdLoc, Id, TInfo); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2939 | void TypedefNameDecl::anchor() { } |
| 2940 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2941 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2942 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypedefDecl)); |
| 2943 | return new (Mem) TypedefDecl(0, SourceLocation(), SourceLocation(), 0, 0); |
| 2944 | } |
| 2945 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2946 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 2947 | SourceLocation StartLoc, |
| 2948 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 2949 | TypeSourceInfo *TInfo) { |
| 2950 | return new (C) TypeAliasDecl(DC, StartLoc, IdLoc, Id, TInfo); |
| 2951 | } |
| 2952 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2953 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 2954 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(TypeAliasDecl)); |
| 2955 | return new (Mem) TypeAliasDecl(0, SourceLocation(), SourceLocation(), 0, 0); |
| 2956 | } |
| 2957 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2958 | SourceRange TypedefDecl::getSourceRange() const { |
| 2959 | SourceLocation RangeEnd = getLocation(); |
| 2960 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 2961 | if (typeIsPostfix(TInfo->getType())) |
| 2962 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 2963 | } |
| 2964 | return SourceRange(getLocStart(), RangeEnd); |
| 2965 | } |
| 2966 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2967 | SourceRange TypeAliasDecl::getSourceRange() const { |
| 2968 | SourceLocation RangeEnd = getLocStart(); |
| 2969 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 2970 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 2971 | return SourceRange(getLocStart(), RangeEnd); |
| 2972 | } |
| 2973 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 2974 | void FileScopeAsmDecl::anchor() { } |
| 2975 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2976 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 2977 | StringLiteral *Str, |
| 2978 | SourceLocation AsmLoc, |
| 2979 | SourceLocation RParenLoc) { |
| 2980 | return new (C) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2981 | } |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 2982 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2983 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
| 2984 | unsigned ID) { |
| 2985 | void *Mem = AllocateDeserializedDecl(C, ID, sizeof(FileScopeAsmDecl)); |
| 2986 | return new (Mem) FileScopeAsmDecl(0, 0, SourceLocation(), SourceLocation()); |
| 2987 | } |
| 2988 | |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 2989 | //===----------------------------------------------------------------------===// |
| 2990 | // ImportDecl Implementation |
| 2991 | //===----------------------------------------------------------------------===// |
| 2992 | |
| 2993 | /// \brief Retrieve the number of module identifiers needed to name the given |
| 2994 | /// module. |
| 2995 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 2996 | unsigned Result = 1; |
| 2997 | while (Mod->Parent) { |
| 2998 | Mod = Mod->Parent; |
| 2999 | ++Result; |
| 3000 | } |
| 3001 | return Result; |
| 3002 | } |
| 3003 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3004 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3005 | Module *Imported, |
| 3006 | ArrayRef<SourceLocation> IdentifierLocs) |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3007 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 3008 | NextLocalImport() |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3009 | { |
| 3010 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
| 3011 | SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1); |
| 3012 | memcpy(StoredLocs, IdentifierLocs.data(), |
| 3013 | IdentifierLocs.size() * sizeof(SourceLocation)); |
| 3014 | } |
| 3015 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3016 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3017 | Module *Imported, SourceLocation EndLoc) |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3018 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 3019 | NextLocalImport() |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3020 | { |
| 3021 | *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc; |
| 3022 | } |
| 3023 | |
| 3024 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3025 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3026 | ArrayRef<SourceLocation> IdentifierLocs) { |
| 3027 | void *Mem = C.Allocate(sizeof(ImportDecl) + |
| 3028 | IdentifierLocs.size() * sizeof(SourceLocation)); |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3029 | return new (Mem) ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3030 | } |
| 3031 | |
| 3032 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3033 | SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3034 | Module *Imported, |
| 3035 | SourceLocation EndLoc) { |
| 3036 | void *Mem = C.Allocate(sizeof(ImportDecl) + sizeof(SourceLocation)); |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 3037 | ImportDecl *Import = new (Mem) ImportDecl(DC, StartLoc, Imported, EndLoc); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3038 | Import->setImplicit(); |
| 3039 | return Import; |
| 3040 | } |
| 3041 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3042 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 3043 | unsigned NumLocations) { |
| 3044 | void *Mem = AllocateDeserializedDecl(C, ID, |
| 3045 | (sizeof(ImportDecl) + |
| 3046 | NumLocations * sizeof(SourceLocation))); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3047 | return new (Mem) ImportDecl(EmptyShell()); |
| 3048 | } |
| 3049 | |
| 3050 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 3051 | if (!ImportedAndComplete.getInt()) |
| 3052 | return ArrayRef<SourceLocation>(); |
| 3053 | |
| 3054 | const SourceLocation *StoredLocs |
| 3055 | = reinterpret_cast<const SourceLocation *>(this + 1); |
| 3056 | return ArrayRef<SourceLocation>(StoredLocs, |
| 3057 | getNumModuleIdentifiers(getImportedModule())); |
| 3058 | } |
| 3059 | |
| 3060 | SourceRange ImportDecl::getSourceRange() const { |
| 3061 | if (!ImportedAndComplete.getInt()) |
| 3062 | return SourceRange(getLocation(), |
| 3063 | *reinterpret_cast<const SourceLocation *>(this + 1)); |
| 3064 | |
| 3065 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 3066 | } |