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