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