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