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