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