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" |
Chris Lattner | 6c2b6eb | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 16 | #include "clang/AST/ASTLambda.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTMutationListener.h" |
| 18 | #include "clang/AST/Attr.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
| 20 | #include "clang/AST/DeclObjC.h" |
| 21 | #include "clang/AST/DeclTemplate.h" |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 22 | #include "clang/AST/Expr.h" |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 23 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | d249e1d1f | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 24 | #include "clang/AST/PrettyPrinter.h" |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 25 | #include "clang/AST/Stmt.h" |
| 26 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 28 | #include "clang/Basic/IdentifierTable.h" |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 29 | #include "clang/Basic/Module.h" |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 30 | #include "clang/Basic/Specifiers.h" |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 32 | #include "clang/Frontend/FrontendDiagnostic.h" |
John McCall | f1bbbb4 | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 33 | #include "llvm/Support/ErrorHandling.h" |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | using namespace clang; |
| 37 | |
Richard Smith | 4ed0122 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 38 | Decl *clang::getPrimaryMergedDecl(Decl *D) { |
| 39 | return D->getASTContext().getPrimaryMergedDecl(D); |
| 40 | } |
| 41 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 42 | // Defined here so that it can be inlined into its direct callers. |
| 43 | bool Decl::isOutOfLine() const { |
| 44 | return !getLexicalDeclContext()->Equals(getDeclContext()); |
| 45 | } |
| 46 | |
Chris Lattner | d3b9065 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 48 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
| 50 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 51 | // Visibility rules aren't rigorously externally specified, but here |
| 52 | // are the basic principles behind what we implement: |
| 53 | // |
| 54 | // 1. An explicit visibility attribute is generally a direct expression |
| 55 | // of the user's intent and should be honored. Only the innermost |
| 56 | // visibility attribute applies. If no visibility attribute applies, |
| 57 | // global visibility settings are considered. |
| 58 | // |
| 59 | // 2. There is one caveat to the above: on or in a template pattern, |
| 60 | // an explicit visibility attribute is just a default rule, and |
| 61 | // visibility can be decreased by the visibility of template |
| 62 | // arguments. But this, too, has an exception: an attribute on an |
| 63 | // explicit specialization or instantiation causes all the visibility |
| 64 | // restrictions of the template arguments to be ignored. |
| 65 | // |
| 66 | // 3. A variable that does not otherwise have explicit visibility can |
| 67 | // be restricted by the visibility of its type. |
| 68 | // |
| 69 | // 4. A visibility restriction is explicit if it comes from an |
| 70 | // attribute (or something like it), not a global visibility setting. |
| 71 | // When emitting a reference to an external symbol, visibility |
| 72 | // restrictions are ignored unless they are explicit. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 73 | // |
| 74 | // 5. When computing the visibility of a non-type, including a |
| 75 | // non-type member of a class, only non-type visibility restrictions |
| 76 | // are considered: the 'visibility' attribute, global value-visibility |
| 77 | // settings, and a few special cases like __private_extern. |
| 78 | // |
| 79 | // 6. When computing the visibility of a type, including a type member |
| 80 | // of a class, only type visibility restrictions are considered: |
| 81 | // the 'type_visibility' attribute and global type-visibility settings. |
| 82 | // However, a 'visibility' attribute counts as a 'type_visibility' |
| 83 | // attribute on any declaration that only has the former. |
| 84 | // |
| 85 | // The visibility of a "secondary" entity, like a template argument, |
| 86 | // is computed using the kind of that entity, not the kind of the |
| 87 | // primary entity for which we are computing visibility. For example, |
| 88 | // the visibility of a specialization of either of these templates: |
| 89 | // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); |
| 90 | // template <class T, bool (&compare)(T, X)> class matcher; |
| 91 | // is restricted according to the type visibility of the argument 'T', |
| 92 | // the type visibility of 'bool(&)(T,X)', and the value visibility of |
| 93 | // the argument function 'compare'. That 'has_match' is a value |
| 94 | // and 'matcher' is a type only matters when looking for attributes |
| 95 | // and settings from the immediate context. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 96 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 97 | const unsigned IgnoreExplicitVisibilityBit = 2; |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 98 | const unsigned IgnoreAllVisibilityBit = 4; |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 99 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 100 | /// Kinds of LV computation. The linkage side of the computation is |
| 101 | /// always the same, but different things can change how visibility is |
| 102 | /// computed. |
| 103 | enum LVComputationKind { |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 104 | /// Do an LV computation for, ultimately, a type. |
| 105 | /// Visibility may be restricted by type visibility settings and |
| 106 | /// the visibility of template arguments. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 107 | LVForType = NamedDecl::VisibilityForType, |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 108 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 109 | /// Do an LV computation for, ultimately, a non-type declaration. |
| 110 | /// Visibility may be restricted by value visibility settings and |
| 111 | /// the visibility of template arguments. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 112 | LVForValue = NamedDecl::VisibilityForValue, |
| 113 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 114 | /// Do an LV computation for, ultimately, a type that already has |
| 115 | /// some sort of explicit visibility. Visibility may only be |
| 116 | /// restricted by the visibility of template arguments. |
| 117 | LVForExplicitType = (LVForType | IgnoreExplicitVisibilityBit), |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 118 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 119 | /// Do an LV computation for, ultimately, a non-type declaration |
| 120 | /// that already has some sort of explicit visibility. Visibility |
| 121 | /// may only be restricted by the visibility of template arguments. |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 122 | LVForExplicitValue = (LVForValue | IgnoreExplicitVisibilityBit), |
| 123 | |
| 124 | /// Do an LV computation when we only care about the linkage. |
| 125 | LVForLinkageOnly = |
| 126 | LVForValue | IgnoreExplicitVisibilityBit | IgnoreAllVisibilityBit |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 129 | /// Does this computation kind permit us to consider additional |
| 130 | /// visibility settings from attributes and the like? |
| 131 | static bool hasExplicitVisibilityAlready(LVComputationKind computation) { |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 132 | return ((unsigned(computation) & IgnoreExplicitVisibilityBit) != 0); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /// Given an LVComputationKind, return one of the same type/value sort |
| 136 | /// that records that it already has explicit visibility. |
| 137 | static LVComputationKind |
| 138 | withExplicitVisibilityAlready(LVComputationKind oldKind) { |
| 139 | LVComputationKind newKind = |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 140 | static_cast<LVComputationKind>(unsigned(oldKind) | |
| 141 | IgnoreExplicitVisibilityBit); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 142 | assert(oldKind != LVForType || newKind == LVForExplicitType); |
| 143 | assert(oldKind != LVForValue || newKind == LVForExplicitValue); |
| 144 | assert(oldKind != LVForExplicitType || newKind == LVForExplicitType); |
| 145 | assert(oldKind != LVForExplicitValue || newKind == LVForExplicitValue); |
| 146 | return newKind; |
| 147 | } |
| 148 | |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 149 | static Optional<Visibility> getExplicitVisibility(const NamedDecl *D, |
| 150 | LVComputationKind kind) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 151 | assert(!hasExplicitVisibilityAlready(kind) && |
| 152 | "asking for explicit visibility when we shouldn't be"); |
| 153 | return D->getExplicitVisibility((NamedDecl::ExplicitVisibilityKind) kind); |
| 154 | } |
| 155 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 156 | /// Is the given declaration a "type" or a "value" for the purposes of |
| 157 | /// visibility computation? |
| 158 | static bool usesTypeVisibility(const NamedDecl *D) { |
John McCall | a880b19 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 159 | return isa<TypeDecl>(D) || |
| 160 | isa<ClassTemplateDecl>(D) || |
| 161 | isa<ObjCInterfaceDecl>(D); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 162 | } |
| 163 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 164 | /// Does the given declaration have member specialization information, |
| 165 | /// and if so, is it an explicit specialization? |
| 166 | template <class T> static typename |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 167 | std::enable_if<!std::is_base_of<RedeclarableTemplateDecl, T>::value, bool>::type |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 168 | isExplicitMemberSpecialization(const T *D) { |
| 169 | if (const MemberSpecializationInfo *member = |
| 170 | D->getMemberSpecializationInfo()) { |
| 171 | return member->isExplicitSpecialization(); |
| 172 | } |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | /// For templates, this question is easier: a member template can't be |
| 177 | /// explicitly instantiated, so there's a single bit indicating whether |
| 178 | /// or not this is an explicit member specialization. |
| 179 | static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { |
| 180 | return D->isMemberSpecialization(); |
| 181 | } |
| 182 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 183 | /// Given a visibility attribute, return the explicit visibility |
| 184 | /// associated with it. |
| 185 | template <class T> |
| 186 | static Visibility getVisibilityFromAttr(const T *attr) { |
| 187 | switch (attr->getVisibility()) { |
| 188 | case T::Default: |
| 189 | return DefaultVisibility; |
| 190 | case T::Hidden: |
| 191 | return HiddenVisibility; |
| 192 | case T::Protected: |
| 193 | return ProtectedVisibility; |
| 194 | } |
| 195 | llvm_unreachable("bad visibility kind"); |
| 196 | } |
| 197 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 198 | /// Return the explicit visibility of the given declaration. |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 199 | static Optional<Visibility> getVisibilityOf(const NamedDecl *D, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 200 | NamedDecl::ExplicitVisibilityKind kind) { |
| 201 | // If we're ultimately computing the visibility of a type, look for |
| 202 | // a 'type_visibility' attribute before looking for 'visibility'. |
| 203 | if (kind == NamedDecl::VisibilityForType) { |
| 204 | if (const TypeVisibilityAttr *A = D->getAttr<TypeVisibilityAttr>()) { |
| 205 | return getVisibilityFromAttr(A); |
| 206 | } |
| 207 | } |
| 208 | |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 209 | // If this declaration has an explicit visibility attribute, use it. |
| 210 | if (const VisibilityAttr *A = D->getAttr<VisibilityAttr>()) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 211 | return getVisibilityFromAttr(A); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 212 | } |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 213 | |
| 214 | // If we're on Mac OS X, an 'availability' for Mac OS X attribute |
| 215 | // implies visibility(default). |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 216 | if (D->getASTContext().getTargetInfo().getTriple().isOSDarwin()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 217 | for (const auto *A : D->specific_attrs<AvailabilityAttr>()) |
| 218 | if (A->getPlatform()->getName().equals("macosx")) |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 219 | return DefaultVisibility; |
| 220 | } |
| 221 | |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 222 | return None; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 225 | static LinkageInfo |
| 226 | getLVForType(const Type &T, LVComputationKind computation) { |
| 227 | if (computation == LVForLinkageOnly) |
| 228 | return LinkageInfo(T.getLinkage(), DefaultVisibility, true); |
| 229 | return T.getLinkageAndVisibility(); |
| 230 | } |
| 231 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 232 | /// \brief Get the most restrictive linkage for the types in the given |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 233 | /// template parameter list. For visibility purposes, template |
| 234 | /// parameters are part of the signature of a template. |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 235 | static LinkageInfo |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 236 | getLVForTemplateParameterList(const TemplateParameterList *Params, |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 237 | LVComputationKind computation) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 238 | LinkageInfo LV; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 239 | for (const NamedDecl *P : *Params) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 240 | // Template type parameters are the most common and never |
| 241 | // contribute to visibility, pack or not. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 242 | if (isa<TemplateTypeParmDecl>(P)) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 243 | continue; |
| 244 | |
| 245 | // Non-type template parameters can be restricted by the value type, e.g. |
| 246 | // template <enum X> class A { ... }; |
| 247 | // We have to be careful here, though, because we can be dealing with |
| 248 | // dependent types. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 249 | if (const NonTypeTemplateParmDecl *NTTP = |
| 250 | dyn_cast<NonTypeTemplateParmDecl>(P)) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 251 | // Handle the non-pack case first. |
| 252 | if (!NTTP->isExpandedParameterPack()) { |
| 253 | if (!NTTP->getType()->isDependentType()) { |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 254 | LV.merge(getLVForType(*NTTP->getType(), computation)); |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 255 | } |
| 256 | continue; |
| 257 | } |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 258 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 259 | // Look at all the types in an expanded pack. |
| 260 | for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { |
| 261 | QualType type = NTTP->getExpansionType(i); |
| 262 | if (!type->isDependentType()) |
Rafael Espindola | 18895dc | 2013-02-27 02:27:19 +0000 | [diff] [blame] | 263 | LV.merge(type->getLinkageAndVisibility()); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 264 | } |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 265 | continue; |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 266 | } |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 267 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 268 | // Template template parameters can be restricted by their |
| 269 | // template parameters, recursively. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 270 | const TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(P); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 271 | |
| 272 | // Handle the non-pack case first. |
| 273 | if (!TTP->isExpandedParameterPack()) { |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 274 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(), |
| 275 | computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 276 | continue; |
| 277 | } |
| 278 | |
| 279 | // Look at all expansions in an expanded pack. |
| 280 | for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); |
| 281 | i != n; ++i) { |
| 282 | LV.merge(getLVForTemplateParameterList( |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 283 | TTP->getExpansionTemplateParameters(i), computation)); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 287 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Rafael Espindola | 838dc59 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 290 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 291 | static LinkageInfo getLVForDecl(const NamedDecl *D, |
| 292 | LVComputationKind computation); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 293 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 294 | static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 295 | const Decl *Ret = nullptr; |
Rafael Espindola | 1229e20 | 2013-05-16 04:30:21 +0000 | [diff] [blame] | 296 | const DeclContext *DC = D->getDeclContext(); |
| 297 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 298 | if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC)) |
| 299 | Ret = cast<Decl>(DC); |
Rafael Espindola | 1229e20 | 2013-05-16 04:30:21 +0000 | [diff] [blame] | 300 | DC = DC->getParent(); |
| 301 | } |
| 302 | return Ret; |
| 303 | } |
| 304 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 305 | /// \brief Get the most restrictive linkage for the types and |
| 306 | /// declarations in the given template argument list. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 307 | /// |
| 308 | /// Note that we don't take an LVComputationKind because we always |
| 309 | /// want to honor the visibility of template arguments in the same way. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 310 | static LinkageInfo getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, |
| 311 | LVComputationKind computation) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 312 | LinkageInfo LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 313 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 314 | for (const TemplateArgument &Arg : Args) { |
| 315 | switch (Arg.getKind()) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 316 | case TemplateArgument::Null: |
| 317 | case TemplateArgument::Integral: |
| 318 | case TemplateArgument::Expression: |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 319 | continue; |
Rafael Espindola | b5d763d | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 320 | |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 321 | case TemplateArgument::Type: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 322 | LV.merge(getLVForType(*Arg.getAsType(), computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 323 | continue; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 324 | |
| 325 | case TemplateArgument::Declaration: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 326 | if (NamedDecl *ND = dyn_cast<NamedDecl>(Arg.getAsDecl())) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 327 | assert(!usesTypeVisibility(ND)); |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 328 | LV.merge(getLVForDecl(ND, computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 329 | } |
| 330 | continue; |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 331 | |
| 332 | case TemplateArgument::NullPtr: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 333 | LV.merge(Arg.getNullPtrType()->getLinkageAndVisibility()); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 334 | continue; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 335 | |
| 336 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 337 | case TemplateArgument::TemplateExpansion: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 338 | if (TemplateDecl *Template = |
| 339 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 340 | LV.merge(getLVForDecl(Template, computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 341 | continue; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 342 | |
| 343 | case TemplateArgument::Pack: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 344 | LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 345 | continue; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 346 | } |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 347 | llvm_unreachable("bad template argument kind"); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 348 | } |
| 349 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 350 | return LV; |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Rafael Espindola | 093ecc9 | 2012-01-14 00:30:36 +0000 | [diff] [blame] | 353 | static LinkageInfo |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 354 | getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
| 355 | LVComputationKind computation) { |
| 356 | return getLVForTemplateArgumentList(TArgs.asArray(), computation); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 357 | } |
| 358 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 359 | static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, |
| 360 | const FunctionTemplateSpecializationInfo *specInfo) { |
| 361 | // Include visibility from the template parameters and arguments |
| 362 | // only if this is not an explicit instantiation or specialization |
| 363 | // with direct explicit visibility. (Implicit instantiations won't |
| 364 | // have a direct attribute.) |
| 365 | if (!specInfo->isExplicitInstantiationOrSpecialization()) |
| 366 | return true; |
| 367 | |
| 368 | return !fn->hasAttr<VisibilityAttr>(); |
| 369 | } |
| 370 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 371 | /// Merge in template-related linkage and visibility for the given |
| 372 | /// function template specialization. |
| 373 | /// |
| 374 | /// We don't need a computation kind here because we can assume |
| 375 | /// LVForValue. |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 376 | /// |
NAKAMURA Takumi | d9bd83e | 2013-02-22 04:06:28 +0000 | [diff] [blame] | 377 | /// \param[out] LV the computation to use for the parent |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 378 | static void |
| 379 | mergeTemplateLV(LinkageInfo &LV, const FunctionDecl *fn, |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 380 | const FunctionTemplateSpecializationInfo *specInfo, |
| 381 | LVComputationKind computation) { |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 382 | bool considerVisibility = |
| 383 | shouldConsiderTemplateVisibility(fn, specInfo); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 384 | |
| 385 | // Merge information from the template parameters. |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 386 | FunctionTemplateDecl *temp = specInfo->getTemplate(); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 387 | LinkageInfo tempLV = |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 388 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 389 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 390 | |
| 391 | // Merge information from the template arguments. |
| 392 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 393 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 394 | LV.mergeMaybeWithVisibility(argsLV, considerVisibility); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 395 | } |
| 396 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 397 | /// Does the given declaration have a direct visibility attribute |
| 398 | /// that would match the given rules? |
| 399 | static bool hasDirectVisibilityAttribute(const NamedDecl *D, |
| 400 | LVComputationKind computation) { |
| 401 | switch (computation) { |
| 402 | case LVForType: |
| 403 | case LVForExplicitType: |
| 404 | if (D->hasAttr<TypeVisibilityAttr>()) |
| 405 | return true; |
| 406 | // fallthrough |
| 407 | case LVForValue: |
| 408 | case LVForExplicitValue: |
| 409 | if (D->hasAttr<VisibilityAttr>()) |
| 410 | return true; |
| 411 | return false; |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 412 | case LVForLinkageOnly: |
| 413 | return false; |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 414 | } |
| 415 | llvm_unreachable("bad visibility computation kind"); |
| 416 | } |
| 417 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 418 | /// Should we consider visibility associated with the template |
| 419 | /// arguments and parameters of the given class template specialization? |
| 420 | static bool shouldConsiderTemplateVisibility( |
| 421 | const ClassTemplateSpecializationDecl *spec, |
| 422 | LVComputationKind computation) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 423 | // Include visibility from the template parameters and arguments |
| 424 | // only if this is not an explicit instantiation or specialization |
| 425 | // with direct explicit visibility (and note that implicit |
| 426 | // instantiations won't have a direct attribute). |
| 427 | // |
| 428 | // Furthermore, we want to ignore template parameters and arguments |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 429 | // for an explicit specialization when computing the visibility of a |
| 430 | // member thereof with explicit visibility. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 431 | // |
| 432 | // This is a bit complex; let's unpack it. |
| 433 | // |
| 434 | // An explicit class specialization is an independent, top-level |
| 435 | // declaration. As such, if it or any of its members has an |
| 436 | // explicit visibility attribute, that must directly express the |
| 437 | // user's intent, and we should honor it. The same logic applies to |
| 438 | // an explicit instantiation of a member of such a thing. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 439 | |
| 440 | // Fast path: if this is not an explicit instantiation or |
| 441 | // specialization, we always want to consider template-related |
| 442 | // visibility restrictions. |
| 443 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 444 | return true; |
| 445 | |
| 446 | // This is the 'member thereof' check. |
| 447 | if (spec->isExplicitSpecialization() && |
| 448 | hasExplicitVisibilityAlready(computation)) |
| 449 | return false; |
| 450 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 451 | return !hasDirectVisibilityAttribute(spec, computation); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /// Merge in template-related linkage and visibility for the given |
| 455 | /// class template specialization. |
| 456 | static void mergeTemplateLV(LinkageInfo &LV, |
| 457 | const ClassTemplateSpecializationDecl *spec, |
| 458 | LVComputationKind computation) { |
| 459 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 460 | |
| 461 | // Merge information from the template parameters, but ignore |
| 462 | // visibility if we're only considering template arguments. |
| 463 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 464 | ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 465 | LinkageInfo tempLV = |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 466 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 467 | LV.mergeMaybeWithVisibility(tempLV, |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 468 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 469 | |
| 470 | // Merge information from the template arguments. We ignore |
| 471 | // template-argument visibility if we've got an explicit |
| 472 | // instantiation with a visibility attribute. |
| 473 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 474 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
Rafael Espindola | e980890 | 2013-05-30 21:23:15 +0000 | [diff] [blame] | 475 | if (considerVisibility) |
| 476 | LV.mergeVisibility(argsLV); |
| 477 | LV.mergeExternalVisibility(argsLV); |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 480 | /// Should we consider visibility associated with the template |
| 481 | /// arguments and parameters of the given variable template |
| 482 | /// specialization? As usual, follow class template specialization |
| 483 | /// logic up to initialization. |
| 484 | static bool shouldConsiderTemplateVisibility( |
| 485 | const VarTemplateSpecializationDecl *spec, |
| 486 | LVComputationKind computation) { |
| 487 | // Include visibility from the template parameters and arguments |
| 488 | // only if this is not an explicit instantiation or specialization |
| 489 | // with direct explicit visibility (and note that implicit |
| 490 | // instantiations won't have a direct attribute). |
| 491 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 492 | return true; |
| 493 | |
| 494 | // An explicit variable specialization is an independent, top-level |
| 495 | // declaration. As such, if it has an explicit visibility attribute, |
| 496 | // that must directly express the user's intent, and we should honor |
| 497 | // it. |
| 498 | if (spec->isExplicitSpecialization() && |
| 499 | hasExplicitVisibilityAlready(computation)) |
| 500 | return false; |
| 501 | |
| 502 | return !hasDirectVisibilityAttribute(spec, computation); |
| 503 | } |
| 504 | |
| 505 | /// Merge in template-related linkage and visibility for the given |
| 506 | /// variable template specialization. As usual, follow class template |
| 507 | /// specialization logic up to initialization. |
| 508 | static void mergeTemplateLV(LinkageInfo &LV, |
| 509 | const VarTemplateSpecializationDecl *spec, |
| 510 | LVComputationKind computation) { |
| 511 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
| 512 | |
| 513 | // Merge information from the template parameters, but ignore |
| 514 | // visibility if we're only considering template arguments. |
| 515 | |
| 516 | VarTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 517 | LinkageInfo tempLV = |
| 518 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| 519 | LV.mergeMaybeWithVisibility(tempLV, |
| 520 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
| 521 | |
| 522 | // Merge information from the template arguments. We ignore |
| 523 | // template-argument visibility if we've got an explicit |
| 524 | // instantiation with a visibility attribute. |
| 525 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| 526 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
| 527 | if (considerVisibility) |
| 528 | LV.mergeVisibility(argsLV); |
| 529 | LV.mergeExternalVisibility(argsLV); |
| 530 | } |
| 531 | |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 532 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
| 533 | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
Rafael Espindola | 0bab9da | 2012-07-13 23:26:43 +0000 | [diff] [blame] | 534 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 535 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 536 | return false; |
| 537 | |
| 538 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 539 | if (!FD) |
| 540 | return false; |
| 541 | |
| 542 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 543 | if (FunctionTemplateSpecializationInfo *spec |
| 544 | = FD->getTemplateSpecializationInfo()) { |
| 545 | TSK = spec->getTemplateSpecializationKind(); |
| 546 | } else if (MemberSpecializationInfo *MSI = |
| 547 | FD->getMemberSpecializationInfo()) { |
| 548 | TSK = MSI->getTemplateSpecializationKind(); |
| 549 | } |
| 550 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 551 | const FunctionDecl *Def = nullptr; |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 552 | // InlineVisibilityHidden only applies to definitions, and |
| 553 | // isInlined() only gives meaningful answers on definitions |
| 554 | // anyway. |
| 555 | return TSK != TSK_ExplicitInstantiationDeclaration && |
| 556 | TSK != TSK_ExplicitInstantiationDefinition && |
Rafael Espindola | 0142f0c | 2012-10-11 16:32:25 +0000 | [diff] [blame] | 557 | FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 560 | template <typename T> static bool isFirstInExternCContext(T *D) { |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 561 | const T *First = D->getFirstDecl(); |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 562 | return First->isInExternCContext(); |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 565 | static bool isSingleLineLanguageLinkage(const Decl &D) { |
Rafael Espindola | e5e575d | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 566 | if (const LinkageSpecDecl *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext())) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 567 | if (!SD->hasBraces()) |
Rafael Espindola | e5e575d | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 568 | return true; |
| 569 | return false; |
| 570 | } |
| 571 | |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 572 | static LinkageInfo getLVForNamespaceScopeDecl(const NamedDecl *D, |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 573 | LVComputationKind computation) { |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 574 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 575 | "Not a name having namespace scope"); |
| 576 | ASTContext &Context = D->getASTContext(); |
| 577 | |
| 578 | // C++ [basic.link]p3: |
| 579 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 580 | // is the name of |
| 581 | // - an object, reference, function or function template that is |
| 582 | // explicitly declared static; or, |
| 583 | // (This bullet corresponds to C99 6.2.2p3.) |
| 584 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 585 | // Explicitly declared static. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 586 | if (Var->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 587 | return LinkageInfo::internal(); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 588 | |
Richard Smith | 820e9a7 | 2012-10-19 06:37:48 +0000 | [diff] [blame] | 589 | // - a non-volatile object or reference that is explicitly declared const |
| 590 | // or constexpr and neither explicitly declared extern nor previously |
| 591 | // declared to have external linkage; or (there is no equivalent in C99) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 592 | if (Context.getLangOpts().CPlusPlus && |
Richard Smith | 820e9a7 | 2012-10-19 06:37:48 +0000 | [diff] [blame] | 593 | Var->getType().isConstQualified() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 594 | !Var->getType().isVolatileQualified()) { |
Rafael Espindola | 4f8a3eb | 2013-04-03 19:22:20 +0000 | [diff] [blame] | 595 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
Rafael Espindola | 4f8a3eb | 2013-04-03 19:22:20 +0000 | [diff] [blame] | 596 | if (PrevVar) |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 597 | return getLVForDecl(PrevVar, computation); |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 598 | |
| 599 | if (Var->getStorageClass() != SC_Extern && |
Rafael Espindola | e5e575d | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 600 | Var->getStorageClass() != SC_PrivateExtern && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 601 | !isSingleLineLanguageLinkage(*Var)) |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 602 | return LinkageInfo::internal(); |
| 603 | } |
| 604 | |
| 605 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; |
| 606 | PrevVar = PrevVar->getPreviousDecl()) { |
| 607 | if (PrevVar->getStorageClass() == SC_PrivateExtern && |
| 608 | Var->getStorageClass() == SC_None) |
| 609 | return PrevVar->getLinkageAndVisibility(); |
| 610 | // Explicitly declared static. |
| 611 | if (PrevVar->getStorageClass() == SC_Static) |
| 612 | return LinkageInfo::internal(); |
Fariborz Jahanian | c7c9058 | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 613 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 614 | } else if (const FunctionDecl *Function = D->getAsFunction()) { |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 615 | // C++ [temp]p4: |
| 616 | // A non-member function template can have internal linkage; any |
| 617 | // other template name shall have external linkage. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 618 | |
| 619 | // Explicitly declared static. |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 620 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 621 | return LinkageInfo(InternalLinkage, DefaultVisibility, false); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 622 | } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) { |
| 623 | // - a data member of an anonymous union. |
| 624 | const VarDecl *VD = IFD->getVarDecl(); |
| 625 | assert(VD && "Expected a VarDecl in this IndirectFieldDecl!"); |
| 626 | return getLVForNamespaceScopeDecl(VD, computation); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 627 | } |
David Majnemer | 885d8bf | 2013-10-23 20:52:43 +0000 | [diff] [blame] | 628 | assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!"); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 629 | |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 630 | if (D->isInAnonymousNamespace()) { |
| 631 | const VarDecl *Var = dyn_cast<VarDecl>(D); |
| 632 | const FunctionDecl *Func = dyn_cast<FunctionDecl>(D); |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 633 | if ((!Var || !isFirstInExternCContext(Var)) && |
| 634 | (!Func || !isFirstInExternCContext(Func))) |
Chandler Carruth | 094b643 | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 635 | return LinkageInfo::uniqueExternal(); |
| 636 | } |
John McCall | e7bc972 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 637 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 638 | // Set up the defaults. |
| 639 | |
| 640 | // C99 6.2.2p5: |
| 641 | // If the declaration of an identifier for an object has file |
| 642 | // scope and no storage-class specifier, its linkage is |
| 643 | // external. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 644 | LinkageInfo LV; |
| 645 | |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 646 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 647 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) { |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 648 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 649 | } else { |
| 650 | // If we're declared in a namespace with a visibility attribute, |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 651 | // use that namespace's visibility, and it still counts as explicit. |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 652 | for (const DeclContext *DC = D->getDeclContext(); |
| 653 | !isa<TranslationUnitDecl>(DC); |
| 654 | DC = DC->getParent()) { |
| 655 | const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC); |
| 656 | if (!ND) continue; |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 657 | if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) { |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 658 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | } |
| 662 | } |
Rafael Espindola | e9836a2 | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 663 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 664 | // Add in global settings if the above didn't give us direct visibility. |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 665 | if (!LV.isVisibilityExplicit()) { |
John McCall | a880b19 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 666 | // Use global type/value visibility as appropriate. |
| 667 | Visibility globalVisibility; |
| 668 | if (computation == LVForValue) { |
| 669 | globalVisibility = Context.getLangOpts().getValueVisibilityMode(); |
| 670 | } else { |
| 671 | assert(computation == LVForType); |
| 672 | globalVisibility = Context.getLangOpts().getTypeVisibilityMode(); |
| 673 | } |
| 674 | LV.mergeVisibility(globalVisibility, /*explicit*/ false); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 675 | |
| 676 | // If we're paying attention to global visibility, apply |
| 677 | // -finline-visibility-hidden if this is an inline method. |
| 678 | if (useInlineVisibilityHidden(D)) |
| 679 | LV.mergeVisibility(HiddenVisibility, true); |
| 680 | } |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 681 | } |
Rafael Espindola | ff25798 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 682 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 683 | // C++ [basic.link]p4: |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 684 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 685 | // A name having namespace scope has external linkage if it is the |
| 686 | // name of |
| 687 | // |
| 688 | // - an object or reference, unless it has internal linkage; or |
| 689 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 690 | // GCC applies the following optimization to variables and static |
| 691 | // data members, but not to functions: |
| 692 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 693 | // Modify the variable's LV by the LV of its type unless this is |
| 694 | // C or extern "C". This follows from [basic.link]p9: |
| 695 | // A type without linkage shall not be used as the type of a |
| 696 | // variable or function with external linkage unless |
| 697 | // - the entity has C language linkage, or |
| 698 | // - the entity is declared within an unnamed namespace, or |
| 699 | // - the entity is not used or is defined in the same |
| 700 | // translation unit. |
| 701 | // and [basic.link]p10: |
| 702 | // ...the types specified by all declarations referring to a |
| 703 | // given variable or function shall be identical... |
| 704 | // C does not have an equivalent rule. |
| 705 | // |
John McCall | ac65c62 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 706 | // Ignore this if we've got an explicit attribute; the user |
| 707 | // probably knows what they're doing. |
| 708 | // |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 709 | // Note that we don't want to make the variable non-external |
| 710 | // because of this, but unique-external linkage suits us. |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 711 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var)) { |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 712 | LinkageInfo TypeLV = getLVForType(*Var->getType(), computation); |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 713 | if (TypeLV.getLinkage() != ExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 714 | return LinkageInfo::uniqueExternal(); |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 715 | if (!LV.isVisibilityExplicit()) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 716 | LV.mergeVisibility(TypeLV); |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 717 | } |
| 718 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 719 | if (Var->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 720 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 721 | |
Rafael Espindola | 538fb98 | 2012-11-12 04:10:23 +0000 | [diff] [blame] | 722 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 723 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't have |
| 724 | // to do it here. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 725 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 726 | // As per function and class template specializations (below), |
| 727 | // consider LV for the template and template arguments. We're at file |
| 728 | // scope, so we do not need to worry about nested specializations. |
| 729 | if (const VarTemplateSpecializationDecl *spec |
| 730 | = dyn_cast<VarTemplateSpecializationDecl>(Var)) { |
| 731 | mergeTemplateLV(LV, spec, computation); |
| 732 | } |
| 733 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 734 | // - a function, unless it has internal linkage; or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 735 | } else if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 67fa6d5 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 736 | // In theory, we can modify the function's LV by the LV of its |
| 737 | // type unless it has C linkage (see comment above about variables |
| 738 | // for justification). In practice, GCC doesn't do this, so it's |
| 739 | // just too painful to make work. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 740 | |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 741 | if (Function->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 5727cf5 | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 742 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 35cebc3 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 743 | |
Rafael Espindola | 5175861 | 2012-11-21 02:47:19 +0000 | [diff] [blame] | 744 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 745 | // merging storage classes and visibility attributes, so we don't have to |
| 746 | // look at previous decls in here. |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 747 | |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 748 | // In C++, then if the type of the function uses a type with |
| 749 | // unique-external linkage, it's not legally usable from outside |
| 750 | // this translation unit. However, we should use the C linkage |
| 751 | // rules instead for extern "C" declarations. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 752 | if (Context.getLangOpts().CPlusPlus && |
Richard Smith | d248e58 | 2013-05-12 23:17:59 +0000 | [diff] [blame] | 753 | !Function->isInExternCContext()) { |
| 754 | // Only look at the type-as-written. If this function has an auto-deduced |
| 755 | // return type, we can't compute the linkage of that type because it could |
| 756 | // require looking at the linkage of this function, and we don't need this |
| 757 | // for correctness because the type is not part of the function's |
| 758 | // signature. |
Faisal Vali | ffc63a8 | 2013-10-08 04:15:04 +0000 | [diff] [blame] | 759 | // FIXME: This is a hack. We should be able to solve this circularity and |
| 760 | // the one in getLVForClassMember for Functions some other way. |
Richard Smith | d248e58 | 2013-05-12 23:17:59 +0000 | [diff] [blame] | 761 | QualType TypeAsWritten = Function->getType(); |
| 762 | if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) |
| 763 | TypeAsWritten = TSI->getType(); |
| 764 | if (TypeAsWritten->getLinkage() == UniqueExternalLinkage) |
| 765 | return LinkageInfo::uniqueExternal(); |
| 766 | } |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 767 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 768 | // Consider LV from the template and the template arguments. |
| 769 | // We're at file scope, so we do not need to worry about nested |
| 770 | // specializations. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 771 | if (FunctionTemplateSpecializationInfo *specInfo |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 772 | = Function->getTemplateSpecializationInfo()) { |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 773 | mergeTemplateLV(LV, Function, specInfo, computation); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 776 | // - a named class (Clause 9), or an unnamed class defined in a |
| 777 | // typedef declaration in which the class has the typedef name |
| 778 | // for linkage purposes (7.1.3); or |
| 779 | // - a named enumeration (7.2), or an unnamed enumeration |
| 780 | // defined in a typedef declaration in which the enumeration |
| 781 | // has the typedef name for linkage purposes (7.1.3); or |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 782 | } else if (const TagDecl *Tag = dyn_cast<TagDecl>(D)) { |
| 783 | // Unnamed tags have no linkage. |
John McCall | 83972f1 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 784 | if (!Tag->hasNameForLinkage()) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 785 | return LinkageInfo::none(); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 786 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 787 | // If this is a class template specialization, consider the |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 788 | // linkage of the template and template arguments. We're at file |
| 789 | // scope, so we do not need to worry about nested specializations. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 790 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 791 | = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 792 | mergeTemplateLV(LV, spec, computation); |
Douglas Gregor | 0b6bc8b | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 793 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 794 | |
| 795 | // - an enumerator belonging to an enumeration with external linkage; |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 796 | } else if (isa<EnumConstantDecl>(D)) { |
Rafael Espindola | 1266b61 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 797 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 798 | computation); |
Rafael Espindola | 88ce12a | 2013-05-27 14:14:42 +0000 | [diff] [blame] | 799 | if (!isExternalFormalLinkage(EnumLV.getLinkage())) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 800 | return LinkageInfo::none(); |
| 801 | LV.merge(EnumLV); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 802 | |
| 803 | // - a template, unless it is a function template that has |
| 804 | // internal linkage (Clause 14); |
John McCall | 1a0918a | 2011-03-04 10:39:25 +0000 | [diff] [blame] | 805 | } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 806 | bool considerVisibility = !hasExplicitVisibilityAlready(computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 807 | LinkageInfo tempLV = |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 808 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 809 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 810 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 811 | // - a namespace (7.3), unless it is declared within an unnamed |
| 812 | // namespace. |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 813 | } else if (isa<NamespaceDecl>(D) && !D->isInAnonymousNamespace()) { |
| 814 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 815 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 816 | // By extension, we assign external linkage to Objective-C |
| 817 | // interfaces. |
| 818 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 819 | // fallout |
| 820 | |
| 821 | // Everything not covered here has no linkage. |
| 822 | } else { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 823 | // FIXME: A typedef declaration has linkage if it gives a type a name for |
| 824 | // linkage purposes. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 825 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | // If we ended up with non-external linkage, visibility should |
| 829 | // always be default. |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 830 | if (LV.getLinkage() != ExternalLinkage) |
| 831 | return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 832 | |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 833 | return LV; |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 834 | } |
| 835 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 836 | static LinkageInfo getLVForClassMember(const NamedDecl *D, |
| 837 | LVComputationKind computation) { |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 838 | // Only certain class members have linkage. Note that fields don't |
| 839 | // really have linkage, but it's convenient to say they do for the |
| 840 | // purposes of calculating linkage of pointer-to-data-member |
| 841 | // template arguments. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 842 | // |
| 843 | // Templates also don't officially have linkage, but since we ignore |
| 844 | // the C++ standard and look at template arguments when determining |
| 845 | // linkage and visibility of a template specialization, we might hit |
| 846 | // a template template argument that way. If we do, we need to |
| 847 | // consider its linkage. |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 848 | if (!(isa<CXXMethodDecl>(D) || |
| 849 | isa<VarDecl>(D) || |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 850 | isa<FieldDecl>(D) || |
David Majnemer | 885d8bf | 2013-10-23 20:52:43 +0000 | [diff] [blame] | 851 | isa<IndirectFieldDecl>(D) || |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 852 | isa<TagDecl>(D) || |
| 853 | isa<TemplateDecl>(D))) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 854 | return LinkageInfo::none(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 855 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 856 | LinkageInfo LV; |
| 857 | |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 858 | // If we have an explicit visibility attribute, merge that in. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 859 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 860 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 861 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 862 | // If we're paying attention to global visibility, apply |
| 863 | // -finline-visibility-hidden if this is an inline method. |
| 864 | // |
| 865 | // Note that we do this before merging information about |
| 866 | // the class visibility. |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 867 | if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) |
Rafael Espindola | b04b731 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 868 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 3698748 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 869 | } |
Rafael Espindola | c7e6060 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 870 | |
| 871 | // If this class member has an explicit visibility attribute, the only |
| 872 | // thing that can change its visibility is the template arguments, so |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 873 | // only look for them when processing the class. |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 874 | LVComputationKind classComputation = computation; |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 875 | if (LV.isVisibilityExplicit()) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 876 | classComputation = withExplicitVisibilityAlready(computation); |
Rafael Espindola | 0f90590 | 2012-04-16 18:25:01 +0000 | [diff] [blame] | 877 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 878 | LinkageInfo classLV = |
| 879 | getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 880 | // If the class already has unique-external linkage, we can't improve. |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 881 | if (classLV.getLinkage() == UniqueExternalLinkage) |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 882 | return LinkageInfo::uniqueExternal(); |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 883 | |
Rafael Espindola | e832854 | 2013-05-28 02:22:10 +0000 | [diff] [blame] | 884 | if (!isExternallyVisible(classLV.getLinkage())) |
| 885 | return LinkageInfo::none(); |
| 886 | |
| 887 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 888 | // Otherwise, don't merge in classLV yet, because in certain cases |
| 889 | // we need to completely ignore the visibility from it. |
| 890 | |
| 891 | // Specifically, if this decl exists and has an explicit attribute. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 892 | const NamedDecl *explicitSpecSuppressor = nullptr; |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 893 | |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 894 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | af8ca37 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 895 | // If the type of the function uses a type with unique-external |
| 896 | // linkage, it's not legally usable from outside this translation unit. |
Faisal Vali | ffc63a8 | 2013-10-08 04:15:04 +0000 | [diff] [blame] | 897 | // But only look at the type-as-written. If this function has an auto-deduced |
| 898 | // return type, we can't compute the linkage of that type because it could |
| 899 | // require looking at the linkage of this function, and we don't need this |
| 900 | // for correctness because the type is not part of the function's |
| 901 | // signature. |
| 902 | // FIXME: This is a hack. We should be able to solve this circularity and the |
| 903 | // one in getLVForNamespaceScopeDecl for Functions some other way. |
| 904 | { |
| 905 | QualType TypeAsWritten = MD->getType(); |
| 906 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 907 | TypeAsWritten = TSI->getType(); |
| 908 | if (TypeAsWritten->getLinkage() == UniqueExternalLinkage) |
| 909 | return LinkageInfo::uniqueExternal(); |
| 910 | } |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 911 | // If this is a method template specialization, use the linkage for |
| 912 | // the template parameters and arguments. |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 913 | if (FunctionTemplateSpecializationInfo *spec |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 914 | = MD->getTemplateSpecializationInfo()) { |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 915 | mergeTemplateLV(LV, MD, spec, computation); |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 916 | if (spec->isExplicitSpecialization()) { |
| 917 | explicitSpecSuppressor = MD; |
| 918 | } else if (isExplicitMemberSpecialization(spec->getTemplate())) { |
| 919 | explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); |
| 920 | } |
| 921 | } else if (isExplicitMemberSpecialization(MD)) { |
| 922 | explicitSpecSuppressor = MD; |
John McCall | 66cbcf3 | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 923 | } |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 924 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 925 | } else if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) { |
John McCall | 6ce51ee | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 926 | if (const ClassTemplateSpecializationDecl *spec |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 927 | = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 928 | mergeTemplateLV(LV, spec, computation); |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 929 | if (spec->isExplicitSpecialization()) { |
| 930 | explicitSpecSuppressor = spec; |
| 931 | } else { |
| 932 | const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 933 | if (isExplicitMemberSpecialization(temp)) { |
| 934 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 935 | } |
| 936 | } |
| 937 | } else if (isExplicitMemberSpecialization(RD)) { |
| 938 | explicitSpecSuppressor = RD; |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 939 | } |
| 940 | |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 941 | // Static data members. |
| 942 | } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 943 | if (const VarTemplateSpecializationDecl *spec |
| 944 | = dyn_cast<VarTemplateSpecializationDecl>(VD)) |
| 945 | mergeTemplateLV(LV, spec, computation); |
| 946 | |
John McCall | ee30102 | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 947 | // Modify the variable's linkage by its type, but ignore the |
| 948 | // type's visibility unless it's a definition. |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 949 | LinkageInfo typeLV = getLVForType(*VD->getType(), computation); |
Rafael Espindola | e980890 | 2013-05-30 21:23:15 +0000 | [diff] [blame] | 950 | if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) |
| 951 | LV.mergeVisibility(typeLV); |
| 952 | LV.mergeExternalVisibility(typeLV); |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 953 | |
| 954 | if (isExplicitMemberSpecialization(VD)) { |
| 955 | explicitSpecSuppressor = VD; |
| 956 | } |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 957 | |
| 958 | // Template members. |
| 959 | } else if (const TemplateDecl *temp = dyn_cast<TemplateDecl>(D)) { |
| 960 | bool considerVisibility = |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 961 | (!LV.isVisibilityExplicit() && |
| 962 | !classLV.isVisibilityExplicit() && |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 963 | !hasExplicitVisibilityAlready(computation)); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 964 | LinkageInfo tempLV = |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 965 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 966 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 967 | |
| 968 | if (const RedeclarableTemplateDecl *redeclTemp = |
| 969 | dyn_cast<RedeclarableTemplateDecl>(temp)) { |
| 970 | if (isExplicitMemberSpecialization(redeclTemp)) { |
| 971 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 972 | } |
| 973 | } |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 974 | } |
| 975 | |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 976 | // We should never be looking for an attribute directly on a template. |
| 977 | assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); |
| 978 | |
| 979 | // If this member is an explicit member specialization, and it has |
| 980 | // an explicit attribute, ignore visibility from the parent. |
| 981 | bool considerClassVisibility = true; |
| 982 | if (explicitSpecSuppressor && |
Rafael Espindola | f127eb8 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 983 | // optimization: hasDVA() is true only with explicit visibility. |
| 984 | LV.isVisibilityExplicit() && |
| 985 | classLV.getVisibility() != DefaultVisibility && |
John McCall | 3892d02 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 986 | hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) { |
| 987 | considerClassVisibility = false; |
| 988 | } |
| 989 | |
| 990 | // Finally, merge in information from the class. |
| 991 | LV.mergeMaybeWithVisibility(classLV, considerClassVisibility); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 992 | return LV; |
John McCall | 3cdfc4d | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 993 | } |
| 994 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 995 | void NamedDecl::anchor() { } |
| 996 | |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 997 | static LinkageInfo computeLVForDecl(const NamedDecl *D, |
| 998 | LVComputationKind computation); |
| 999 | |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1000 | bool NamedDecl::isLinkageValid() const { |
Rafael Espindola | a99ecbc | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1001 | if (!hasCachedLinkage()) |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1002 | return true; |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 1003 | |
Rafael Espindola | 74caf01 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 1004 | return computeLVForDecl(this, LVForLinkageOnly).getLinkage() == |
Rafael Espindola | a99ecbc | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1005 | getCachedLinkage(); |
John McCall | f76b092 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1008 | ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { |
| 1009 | StringRef name = getName(); |
| 1010 | if (name.empty()) return SFF_None; |
| 1011 | |
| 1012 | if (name.front() == 'C') |
| 1013 | if (name == "CFStringCreateWithFormat" || |
| 1014 | name == "CFStringCreateWithFormatAndArguments" || |
| 1015 | name == "CFStringAppendFormat" || |
| 1016 | name == "CFStringAppendFormatAndArguments") |
| 1017 | return SFF_CFString; |
| 1018 | return SFF_None; |
| 1019 | } |
| 1020 | |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1021 | Linkage NamedDecl::getLinkageInternal() const { |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1022 | // We don't care about visibility here, so ask for the cheapest |
| 1023 | // possible visibility analysis. |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1024 | return getLVForDecl(this, LVForLinkageOnly).getLinkage(); |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1027 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1028 | LVComputationKind computation = |
| 1029 | (usesTypeVisibility(this) ? LVForType : LVForValue); |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1030 | return getLVForDecl(this, computation); |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1031 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1032 | |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1033 | static Optional<Visibility> |
| 1034 | getExplicitVisibilityAux(const NamedDecl *ND, |
| 1035 | NamedDecl::ExplicitVisibilityKind kind, |
| 1036 | bool IsMostRecent) { |
| 1037 | assert(!IsMostRecent || ND == ND->getMostRecentDecl()); |
| 1038 | |
Rafael Espindola | d3b2f0a | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1039 | // Check the declaration itself first. |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1040 | if (Optional<Visibility> V = getVisibilityOf(ND, kind)) |
Rafael Espindola | d3b2f0a | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1041 | return V; |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1042 | |
Rafael Espindola | d3b2f0a | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1043 | // If this is a member class of a specialization of a class template |
| 1044 | // and the corresponding decl has explicit visibility, use that. |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1045 | if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(ND)) { |
Rafael Espindola | d3b2f0a | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1046 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 1047 | if (InstantiatedFrom) |
| 1048 | return getVisibilityOf(InstantiatedFrom, kind); |
| 1049 | } |
| 1050 | |
| 1051 | // If there wasn't explicit visibility there, and this is a |
| 1052 | // specialization of a class template, check for visibility |
| 1053 | // on the pattern. |
| 1054 | if (const ClassTemplateSpecializationDecl *spec |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1055 | = dyn_cast<ClassTemplateSpecializationDecl>(ND)) |
Rafael Espindola | d3b2f0a | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1056 | return getVisibilityOf(spec->getSpecializedTemplate()->getTemplatedDecl(), |
| 1057 | kind); |
| 1058 | |
| 1059 | // Use the most recent declaration. |
Bill Wendling | 525f2f5 | 2013-12-09 02:00:10 +0000 | [diff] [blame] | 1060 | if (!IsMostRecent && !isa<NamespaceDecl>(ND)) { |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1061 | const NamedDecl *MostRecent = ND->getMostRecentDecl(); |
| 1062 | if (MostRecent != ND) |
| 1063 | return getExplicitVisibilityAux(MostRecent, kind, true); |
| 1064 | } |
Rafael Espindola | d3b2f0a | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1065 | |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1066 | if (const VarDecl *Var = dyn_cast<VarDecl>(ND)) { |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1067 | if (Var->isStaticDataMember()) { |
| 1068 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
| 1069 | if (InstantiatedFrom) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1070 | return getVisibilityOf(InstantiatedFrom, kind); |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1073 | if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var)) |
| 1074 | return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(), |
| 1075 | kind); |
| 1076 | |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1077 | return None; |
Rafael Espindola | 797105a | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1078 | } |
Rafael Espindola | d3b2f0a | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1079 | // Also handle function template specializations. |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1080 | if (const FunctionDecl *fn = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1081 | // If the function is a specialization of a template with an |
| 1082 | // explicit visibility attribute, use that. |
| 1083 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 1084 | = fn->getTemplateSpecializationInfo()) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1085 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), |
| 1086 | kind); |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1087 | |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 1088 | // If the function is a member of a specialization of a class template |
| 1089 | // and the corresponding decl has explicit visibility, use that. |
| 1090 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 1091 | if (InstantiatedFrom) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1092 | return getVisibilityOf(InstantiatedFrom, kind); |
Rafael Espindola | 860097c | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 1093 | |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1094 | return None; |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Rafael Espindola | 9849901 | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 1097 | // The visibility of a template is stored in the templated decl. |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1098 | if (const TemplateDecl *TD = dyn_cast<TemplateDecl>(ND)) |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1099 | return getVisibilityOf(TD->getTemplatedDecl(), kind); |
Rafael Espindola | 9849901 | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 1100 | |
David Blaikie | 66874fb | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1101 | return None; |
Douglas Gregor | 4421d2b | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Bill Wendling | b8110d4 | 2013-11-27 05:28:46 +0000 | [diff] [blame] | 1104 | Optional<Visibility> |
| 1105 | NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { |
| 1106 | return getExplicitVisibilityAux(this, kind, false); |
| 1107 | } |
| 1108 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1109 | static LinkageInfo getLVForClosure(const DeclContext *DC, Decl *ContextDecl, |
| 1110 | LVComputationKind computation) { |
| 1111 | // This lambda has its linkage/visibility determined by its owner. |
| 1112 | if (ContextDecl) { |
| 1113 | if (isa<ParmVarDecl>(ContextDecl)) |
| 1114 | DC = ContextDecl->getDeclContext()->getRedeclContext(); |
| 1115 | else |
| 1116 | return getLVForDecl(cast<NamedDecl>(ContextDecl), computation); |
| 1117 | } |
Faisal Vali | c6867dd | 2013-09-29 20:27:06 +0000 | [diff] [blame] | 1118 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1119 | if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) |
| 1120 | return getLVForDecl(ND, computation); |
Faisal Vali | c6867dd | 2013-09-29 20:27:06 +0000 | [diff] [blame] | 1121 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1122 | return LinkageInfo::external(); |
| 1123 | } |
| 1124 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1125 | static LinkageInfo getLVForLocalDecl(const NamedDecl *D, |
| 1126 | LVComputationKind computation) { |
| 1127 | if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| 1128 | if (Function->isInAnonymousNamespace() && |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1129 | !Function->isInExternCContext()) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1130 | return LinkageInfo::uniqueExternal(); |
| 1131 | |
| 1132 | // This is a "void f();" which got merged with a file static. |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1133 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1134 | return LinkageInfo::internal(); |
| 1135 | |
| 1136 | LinkageInfo LV; |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1137 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1138 | if (Optional<Visibility> Vis = |
| 1139 | getExplicitVisibility(Function, computation)) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1140 | LV.mergeVisibility(*Vis, true); |
| 1141 | } |
| 1142 | |
| 1143 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 1144 | // merging storage classes and visibility attributes, so we don't have to |
| 1145 | // look at previous decls in here. |
| 1146 | |
| 1147 | return LV; |
| 1148 | } |
| 1149 | |
| 1150 | if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1151 | if (Var->hasExternalStorage()) { |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1152 | if (Var->isInAnonymousNamespace() && !Var->isInExternCContext()) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1153 | return LinkageInfo::uniqueExternal(); |
| 1154 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1155 | LinkageInfo LV; |
| 1156 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 1157 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | d4c3d66 | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1158 | else if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | dc84cd5 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1159 | if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation)) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1160 | LV.mergeVisibility(*Vis, true); |
| 1161 | } |
| 1162 | |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1163 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
| 1164 | LinkageInfo PrevLV = getLVForDecl(Prev, computation); |
| 1165 | if (PrevLV.getLinkage()) |
| 1166 | LV.setLinkage(PrevLV.getLinkage()); |
| 1167 | LV.mergeVisibility(PrevLV); |
| 1168 | } |
| 1169 | |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1170 | return LV; |
| 1171 | } |
Rafael Espindola | 9610d77 | 2013-06-17 20:04:51 +0000 | [diff] [blame] | 1172 | |
| 1173 | if (!Var->isStaticLocal()) |
| 1174 | return LinkageInfo::none(); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Rafael Espindola | 9610d77 | 2013-06-17 20:04:51 +0000 | [diff] [blame] | 1177 | ASTContext &Context = D->getASTContext(); |
| 1178 | if (!Context.getLangOpts().CPlusPlus) |
Rafael Espindola | a99ecbc | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1179 | return LinkageInfo::none(); |
| 1180 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1181 | const Decl *OuterD = getOutermostFuncOrBlockContext(D); |
| 1182 | if (!OuterD) |
Rafael Espindola | a99ecbc | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1183 | return LinkageInfo::none(); |
Rafael Espindola | ec0d96f | 2013-06-04 13:43:35 +0000 | [diff] [blame] | 1184 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1185 | LinkageInfo LV; |
| 1186 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(OuterD)) { |
| 1187 | if (!BD->getBlockManglingNumber()) |
| 1188 | return LinkageInfo::none(); |
Rafael Espindola | ec0d96f | 2013-06-04 13:43:35 +0000 | [diff] [blame] | 1189 | |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1190 | LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(), |
| 1191 | BD->getBlockManglingContextDecl(), computation); |
| 1192 | } else { |
| 1193 | const FunctionDecl *FD = cast<FunctionDecl>(OuterD); |
| 1194 | if (!FD->isInlined() && |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1195 | !isTemplateInstantiation(FD->getTemplateSpecializationKind())) |
Eli Friedman | 07369dd | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1196 | return LinkageInfo::none(); |
| 1197 | |
| 1198 | LV = getLVForDecl(FD, computation); |
| 1199 | } |
Rafael Espindola | bdf2bba | 2013-05-27 14:50:21 +0000 | [diff] [blame] | 1200 | if (!isExternallyVisible(LV.getLinkage())) |
Rafael Espindola | a99ecbc | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1201 | return LinkageInfo::none(); |
| 1202 | return LinkageInfo(VisibleNoLinkage, LV.getVisibility(), |
| 1203 | LV.isVisibilityExplicit()); |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
Faisal Vali | de8eaa2 | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1206 | static inline const CXXRecordDecl* |
| 1207 | getOutermostEnclosingLambda(const CXXRecordDecl *Record) { |
| 1208 | const CXXRecordDecl *Ret = Record; |
| 1209 | while (Record && Record->isLambda()) { |
| 1210 | Ret = Record; |
| 1211 | if (!Record->getParent()) break; |
| 1212 | // Get the Containing Class of this Lambda Class |
| 1213 | Record = dyn_cast_or_null<CXXRecordDecl>( |
| 1214 | Record->getParent()->getParent()); |
| 1215 | } |
| 1216 | return Ret; |
| 1217 | } |
| 1218 | |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1219 | static LinkageInfo computeLVForDecl(const NamedDecl *D, |
| 1220 | LVComputationKind computation) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1221 | // Objective-C: treat all Objective-C declarations as having external |
| 1222 | // linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1223 | switch (D->getKind()) { |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1224 | default: |
| 1225 | break; |
Argyrios Kyrtzidis | f8d34ed | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 1226 | case Decl::ParmVar: |
| 1227 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1228 | case Decl::TemplateTemplateParm: // count these as external |
| 1229 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1230 | case Decl::ObjCAtDefsField: |
| 1231 | case Decl::ObjCCategory: |
| 1232 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1233 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1234 | case Decl::ObjCImplementation: |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1235 | case Decl::ObjCMethod: |
| 1236 | case Decl::ObjCProperty: |
| 1237 | case Decl::ObjCPropertyImpl: |
| 1238 | case Decl::ObjCProtocol: |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1239 | return LinkageInfo::external(); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1240 | |
| 1241 | case Decl::CXXRecord: { |
| 1242 | const CXXRecordDecl *Record = cast<CXXRecordDecl>(D); |
| 1243 | if (Record->isLambda()) { |
| 1244 | if (!Record->getLambdaManglingNumber()) { |
| 1245 | // This lambda has no mangling number, so it's internal. |
| 1246 | return LinkageInfo::internal(); |
| 1247 | } |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1248 | |
Faisal Vali | de8eaa2 | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1249 | // This lambda has its linkage/visibility determined: |
| 1250 | // - either by the outermost lambda if that lambda has no mangling |
| 1251 | // number. |
| 1252 | // - or by the parent of the outer most lambda |
| 1253 | // This prevents infinite recursion in settings such as nested lambdas |
| 1254 | // used in NSDMI's, for e.g. |
| 1255 | // struct L { |
| 1256 | // int t{}; |
| 1257 | // int t2 = ([](int a) { return [](int b) { return b; };})(t)(t); |
| 1258 | // }; |
| 1259 | const CXXRecordDecl *OuterMostLambda = |
| 1260 | getOutermostEnclosingLambda(Record); |
| 1261 | if (!OuterMostLambda->getLambdaManglingNumber()) |
| 1262 | return LinkageInfo::internal(); |
| 1263 | |
| 1264 | return getLVForClosure( |
| 1265 | OuterMostLambda->getDeclContext()->getRedeclContext(), |
| 1266 | OuterMostLambda->getLambdaContextDecl(), computation); |
Douglas Gregor | 5878cbc | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | break; |
| 1270 | } |
Ted Kremenek | becc308 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1273 | // Handle linkage for namespace-scope names. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1274 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1275 | return getLVForNamespaceScopeDecl(D, computation); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1276 | |
| 1277 | // C++ [basic.link]p5: |
| 1278 | // In addition, a member function, static data member, a named |
| 1279 | // class or enumeration of class scope, or an unnamed class or |
| 1280 | // enumeration defined in a class-scope typedef declaration such |
| 1281 | // that the class or enumeration has the typedef name for linkage |
| 1282 | // purposes (7.1.3), has external linkage if the name of the class |
| 1283 | // has external linkage. |
John McCall | 0df9587 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1284 | if (D->getDeclContext()->isRecord()) |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1285 | return getLVForClassMember(D, computation); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1286 | |
| 1287 | // C++ [basic.link]p6: |
| 1288 | // The name of a function declared in block scope and the name of |
| 1289 | // an object declared by a block scope extern declaration have |
| 1290 | // linkage. If there is a visible declaration of an entity with |
| 1291 | // linkage having the same name and type, ignoring entities |
| 1292 | // declared outside the innermost enclosing namespace scope, the |
| 1293 | // block scope declaration declares that same entity and receives |
| 1294 | // the linkage of the previous declaration. If there is more than |
| 1295 | // one such matching entity, the program is ill-formed. Otherwise, |
| 1296 | // if no matching entity is found, the block scope entity receives |
| 1297 | // external linkage. |
John McCall | 5a758de | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1298 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 1299 | return getLVForLocalDecl(D, computation); |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1300 | |
| 1301 | // C++ [basic.link]p6: |
| 1302 | // Names not covered by these rules have no linkage. |
John McCall | af14603 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1303 | return LinkageInfo::none(); |
John McCall | 1fb0caa | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1304 | } |
Douglas Gregor | d85b5b9 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1305 | |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1306 | namespace clang { |
| 1307 | class LinkageComputer { |
| 1308 | public: |
| 1309 | static LinkageInfo getLVForDecl(const NamedDecl *D, |
| 1310 | LVComputationKind computation) { |
| 1311 | if (computation == LVForLinkageOnly && D->hasCachedLinkage()) |
| 1312 | return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); |
| 1313 | |
| 1314 | LinkageInfo LV = computeLVForDecl(D, computation); |
| 1315 | if (D->hasCachedLinkage()) |
| 1316 | assert(D->getCachedLinkage() == LV.getLinkage()); |
| 1317 | |
| 1318 | D->setCachedLinkage(LV.getLinkage()); |
| 1319 | |
| 1320 | #ifndef NDEBUG |
| 1321 | // In C (because of gnu inline) and in c++ with microsoft extensions an |
| 1322 | // static can follow an extern, so we can have two decls with different |
| 1323 | // linkages. |
| 1324 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 1325 | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
| 1326 | return LV; |
| 1327 | |
| 1328 | // We have just computed the linkage for this decl. By induction we know |
| 1329 | // that all other computed linkages match, check that the one we just |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1330 | // computed also does. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1331 | NamedDecl *Old = nullptr; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1332 | for (auto I : D->redecls()) { |
| 1333 | NamedDecl *T = cast<NamedDecl>(I); |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1334 | if (T == D) |
| 1335 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1336 | if (!T->isInvalidDecl() && T->hasCachedLinkage()) { |
Rafael Espindola | dc07056 | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1337 | Old = T; |
| 1338 | break; |
| 1339 | } |
| 1340 | } |
| 1341 | assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); |
| 1342 | #endif |
| 1343 | |
| 1344 | return LV; |
| 1345 | } |
| 1346 | }; |
| 1347 | } |
| 1348 | |
| 1349 | static LinkageInfo getLVForDecl(const NamedDecl *D, |
| 1350 | LVComputationKind computation) { |
| 1351 | return clang::LinkageComputer::getLVForDecl(D, computation); |
| 1352 | } |
| 1353 | |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1354 | std::string NamedDecl::getQualifiedNameAsString() const { |
Benjamin Kramer | b063ef0 | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1355 | std::string QualName; |
| 1356 | llvm::raw_string_ostream OS(QualName); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1357 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
Benjamin Kramer | b063ef0 | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1358 | return OS.str(); |
| 1359 | } |
| 1360 | |
| 1361 | void NamedDecl::printQualifiedName(raw_ostream &OS) const { |
| 1362 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
| 1363 | } |
| 1364 | |
| 1365 | void NamedDecl::printQualifiedName(raw_ostream &OS, |
| 1366 | const PrintingPolicy &P) const { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1367 | const DeclContext *Ctx = getDeclContext(); |
| 1368 | |
Benjamin Kramer | b063ef0 | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1369 | if (Ctx->isFunctionOrMethod()) { |
| 1370 | printName(OS); |
| 1371 | return; |
| 1372 | } |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1374 | typedef SmallVector<const DeclContext *, 8> ContextsTy; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1375 | ContextsTy Contexts; |
| 1376 | |
| 1377 | // Collect contexts. |
| 1378 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 1379 | Contexts.push_back(Ctx); |
| 1380 | Ctx = Ctx->getParent(); |
Benjamin Kramer | b063ef0 | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1381 | } |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1382 | |
| 1383 | for (ContextsTy::reverse_iterator I = Contexts.rbegin(), E = Contexts.rend(); |
| 1384 | I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1385 | if (const ClassTemplateSpecializationDecl *Spec |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1386 | = dyn_cast<ClassTemplateSpecializationDecl>(*I)) { |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 1387 | OS << Spec->getName(); |
Douglas Gregor | f3e7ce4 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1388 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 1389 | TemplateSpecializationType::PrintTemplateArgumentList(OS, |
| 1390 | TemplateArgs.data(), |
| 1391 | TemplateArgs.size(), |
| 1392 | P); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1393 | } else if (const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(*I)) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1394 | if (P.SuppressUnwrittenScope && |
| 1395 | (ND->isAnonymousNamespace() || ND->isInline())) |
| 1396 | continue; |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 1397 | if (ND->isAnonymousNamespace()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1398 | OS << "(anonymous namespace)"; |
Sam Weinig | 6be1120 | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 1399 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1400 | OS << *ND; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1401 | } else if (const RecordDecl *RD = dyn_cast<RecordDecl>(*I)) { |
| 1402 | if (!RD->getIdentifier()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1403 | OS << "(anonymous " << RD->getKindName() << ')'; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1404 | else |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1405 | OS << *RD; |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1406 | } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1407 | const FunctionProtoType *FT = nullptr; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1408 | if (FD->hasWrittenPrototype()) |
Eli Friedman | 482466b | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 1409 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1410 | |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1411 | OS << *FD << '('; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1412 | if (FT) { |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1413 | unsigned NumParams = FD->getNumParams(); |
| 1414 | for (unsigned i = 0; i < NumParams; ++i) { |
| 1415 | if (i) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1416 | OS << ", "; |
Argyrios Kyrtzidis | 7ad5c99 | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 1417 | OS << FD->getParamDecl(i)->getType().stream(P); |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | if (FT->isVariadic()) { |
| 1421 | if (NumParams > 0) |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1422 | OS << ", "; |
| 1423 | OS << "..."; |
Sam Weinig | 3521d01 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1424 | } |
| 1425 | } |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1426 | OS << ')'; |
| 1427 | } else { |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1428 | OS << *cast<NamedDecl>(*I); |
Benjamin Kramer | 68eebbb | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1429 | } |
| 1430 | OS << "::"; |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 1433 | if (getDeclName()) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1434 | OS << *this; |
John McCall | 8472af4 | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 1435 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1436 | OS << "(anonymous)"; |
Benjamin Kramer | b063ef0 | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1437 | } |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1438 | |
Benjamin Kramer | b063ef0 | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1439 | void NamedDecl::getNameForDiagnostic(raw_ostream &OS, |
| 1440 | const PrintingPolicy &Policy, |
| 1441 | bool Qualified) const { |
| 1442 | if (Qualified) |
| 1443 | printQualifiedName(OS, Policy); |
| 1444 | else |
| 1445 | printName(OS); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1448 | static bool isKindReplaceableBy(Decl::Kind OldK, Decl::Kind NewK) { |
| 1449 | // For method declarations, we never replace. |
| 1450 | if (ObjCMethodDecl::classofKind(NewK)) |
| 1451 | return false; |
| 1452 | |
| 1453 | if (OldK == NewK) |
| 1454 | return true; |
| 1455 | |
| 1456 | // A compatibility alias for a class can be replaced by an interface. |
| 1457 | if (ObjCCompatibleAliasDecl::classofKind(OldK) && |
| 1458 | ObjCInterfaceDecl::classofKind(NewK)) |
| 1459 | return true; |
| 1460 | |
| 1461 | // A typedef-declaration, alias-declaration, or Objective-C class declaration |
| 1462 | // can replace another declaration of the same type. Semantic analysis checks |
| 1463 | // that we have matching types. |
| 1464 | if ((TypedefNameDecl::classofKind(OldK) || |
| 1465 | ObjCInterfaceDecl::classofKind(OldK)) && |
| 1466 | (TypedefNameDecl::classofKind(NewK) || |
| 1467 | ObjCInterfaceDecl::classofKind(NewK))) |
| 1468 | return true; |
| 1469 | |
| 1470 | // Otherwise, a kind mismatch implies that the declaration is not replaced. |
| 1471 | return false; |
| 1472 | } |
| 1473 | |
| 1474 | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { |
| 1475 | return true; |
| 1476 | } |
| 1477 | static bool isRedeclarableImpl(...) { return false; } |
| 1478 | static bool isRedeclarable(Decl::Kind K) { |
| 1479 | switch (K) { |
| 1480 | #define DECL(Type, Base) \ |
| 1481 | case Decl::Type: \ |
| 1482 | return isRedeclarableImpl((Type##Decl *)nullptr); |
| 1483 | #define ABSTRACT_DECL(DECL) |
| 1484 | #include "clang/AST/DeclNodes.inc" |
| 1485 | } |
| 1486 | llvm_unreachable("unknown decl kind"); |
| 1487 | } |
| 1488 | |
| 1489 | bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const { |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1490 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 1491 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 1492 | // Never replace one imported declaration with another; we need both results |
| 1493 | // when re-exporting. |
| 1494 | if (OldD->isFromASTFile() && isFromASTFile()) |
| 1495 | return false; |
| 1496 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1497 | if (!isKindReplaceableBy(OldD->getKind(), getKind())) |
| 1498 | return false; |
| 1499 | |
| 1500 | // Inline namespaces can give us two declarations with the same |
| 1501 | // name and kind in the same scope but different contexts; we should |
| 1502 | // keep both declarations in this case. |
| 1503 | if (!this->getDeclContext()->getRedeclContext()->Equals( |
| 1504 | OldD->getDeclContext()->getRedeclContext())) |
| 1505 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1507 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(this)) |
| 1508 | // For function declarations, we keep track of redeclarations. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1509 | // FIXME: This returns false for functions that should in fact be replaced. |
| 1510 | // Instead, perform some kind of type check? |
| 1511 | if (FD->getPreviousDecl() != OldD) |
| 1512 | return false; |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1513 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1514 | // For function templates, the underlying function declarations are linked. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1515 | if (const FunctionTemplateDecl *FunctionTemplate = |
| 1516 | dyn_cast<FunctionTemplateDecl>(this)) |
| 1517 | return FunctionTemplate->getTemplatedDecl()->declarationReplaces( |
| 1518 | cast<FunctionTemplateDecl>(OldD)->getTemplatedDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1520 | // Using shadow declarations can be overloaded on their target declarations |
| 1521 | // if they introduce functions. |
| 1522 | // FIXME: If our target replaces the old target, can we replace the old |
| 1523 | // shadow declaration? |
| 1524 | if (auto *USD = dyn_cast<UsingShadowDecl>(this)) |
| 1525 | if (USD->getTargetDecl() != cast<UsingShadowDecl>(OldD)->getTargetDecl()) |
| 1526 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1527 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1528 | // Using declarations can be overloaded if they introduce functions. |
| 1529 | if (auto *UD = dyn_cast<UsingDecl>(this)) { |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1530 | ASTContext &Context = getASTContext(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1531 | return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1532 | Context.getCanonicalNestedNameSpecifier( |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1533 | cast<UsingDecl>(OldD)->getQualifier()); |
Douglas Gregor | dc35571 | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1534 | } |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1535 | if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { |
Eli Friedman | 13b572c | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1536 | ASTContext &Context = getASTContext(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1537 | return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == |
Eli Friedman | 13b572c | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1538 | Context.getCanonicalNestedNameSpecifier( |
| 1539 | cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); |
| 1540 | } |
| 1541 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1542 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
| 1543 | // We want to keep it, unless it nominates same namespace. |
| 1544 | if (auto *UD = dyn_cast<UsingDirectiveDecl>(this)) |
| 1545 | return UD->getNominatedNamespace()->getOriginalNamespace() == |
| 1546 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace() |
| 1547 | ->getOriginalNamespace(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1548 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1549 | if (!IsKnownNewer && isRedeclarable(getKind())) { |
| 1550 | // Check whether this is actually newer than OldD. We want to keep the |
| 1551 | // newer declaration. This loop will usually only iterate once, because |
| 1552 | // OldD is usually the previous declaration. |
| 1553 | for (auto D : redecls()) { |
| 1554 | if (D == OldD) |
| 1555 | break; |
| 1556 | |
| 1557 | // If we reach the canonical declaration, then OldD is not actually older |
| 1558 | // than this one. |
| 1559 | // |
| 1560 | // FIXME: In this case, we should not add this decl to the lookup table. |
| 1561 | if (D->isCanonicalDecl()) |
| 1562 | return false; |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | // It's a newer declaration of the same kind of declaration in the same scope, |
| 1567 | // and not an overload: we want this decl instead of the existing one. |
| 1568 | return true; |
Douglas Gregor | 6ed40e3 | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1571 | bool NamedDecl::hasLinkage() const { |
Rafael Espindola | a99ecbc | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1572 | return getFormalLinkage() != NoLinkage; |
Douglas Gregor | d6f7e9d | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1573 | } |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1574 | |
Daniel Dunbar | 6daffa5 | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 1575 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1576 | NamedDecl *ND = this; |
Benjamin Kramer | 56757e9 | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1577 | while (UsingShadowDecl *UD = dyn_cast<UsingShadowDecl>(ND)) |
| 1578 | ND = UD->getTargetDecl(); |
| 1579 | |
| 1580 | if (ObjCCompatibleAliasDecl *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| 1581 | return AD->getClassInterface(); |
| 1582 | |
| 1583 | return ND; |
Anders Carlsson | e136e0e | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1586 | bool NamedDecl::isCXXInstanceMember() const { |
Douglas Gregor | 5bc37f6 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 1587 | if (!isCXXClassMember()) |
| 1588 | return false; |
| 1589 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1590 | const NamedDecl *D = this; |
| 1591 | if (isa<UsingShadowDecl>(D)) |
| 1592 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1593 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1594 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1595 | return true; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1596 | if (const CXXMethodDecl *MD = |
| 1597 | dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction())) |
| 1598 | return MD->isInstance(); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1599 | return false; |
| 1600 | } |
| 1601 | |
Argyrios Kyrtzidis | 5239304 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1602 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1603 | // DeclaratorDecl Implementation |
| 1604 | //===----------------------------------------------------------------------===// |
| 1605 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1606 | template <typename DeclT> |
| 1607 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1608 | if (decl->getNumTemplateParameterLists() > 0) |
| 1609 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1610 | else |
| 1611 | return decl->getInnerLocStart(); |
| 1612 | } |
| 1613 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1614 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | 4e44983 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1615 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1616 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1617 | return SourceLocation(); |
| 1618 | } |
| 1619 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1620 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1621 | if (QualifierLoc) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1622 | // Make sure the extended decl info is allocated. |
| 1623 | if (!hasExtInfo()) { |
| 1624 | // Save (non-extended) type source info pointer. |
| 1625 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1626 | // Allocate external info struct. |
| 1627 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1628 | // Restore savedTInfo into (extended) decl info. |
| 1629 | getExtInfo()->TInfo = savedTInfo; |
| 1630 | } |
| 1631 | // Set qualifier info. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1632 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1633 | } else { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1634 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1635 | if (hasExtInfo()) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1636 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 1637 | // Save type source info pointer. |
| 1638 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 1639 | // Deallocate the extended decl info. |
| 1640 | getASTContext().Deallocate(getExtInfo()); |
| 1641 | // Restore savedTInfo into (non-extended) decl info. |
| 1642 | DeclInfo = savedTInfo; |
| 1643 | } |
| 1644 | else |
| 1645 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1650 | void |
| 1651 | DeclaratorDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 1652 | unsigned NumTPLists, |
| 1653 | TemplateParameterList **TPLists) { |
| 1654 | assert(NumTPLists > 0); |
| 1655 | // Make sure the extended decl info is allocated. |
| 1656 | if (!hasExtInfo()) { |
| 1657 | // Save (non-extended) type source info pointer. |
| 1658 | TypeSourceInfo *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1659 | // Allocate external info struct. |
| 1660 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1661 | // Restore savedTInfo into (extended) decl info. |
| 1662 | getExtInfo()->TInfo = savedTInfo; |
| 1663 | } |
| 1664 | // Set the template parameter lists info. |
| 1665 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 1666 | } |
| 1667 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1668 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1669 | return getTemplateOrInnerLocStart(this); |
| 1670 | } |
| 1671 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1672 | namespace { |
| 1673 | |
| 1674 | // Helper function: returns true if QT is or contains a type |
| 1675 | // having a postfix component. |
| 1676 | bool typeIsPostfix(clang::QualType QT) { |
| 1677 | while (true) { |
| 1678 | const Type* T = QT.getTypePtr(); |
| 1679 | switch (T->getTypeClass()) { |
| 1680 | default: |
| 1681 | return false; |
| 1682 | case Type::Pointer: |
| 1683 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1684 | break; |
| 1685 | case Type::BlockPointer: |
| 1686 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1687 | break; |
| 1688 | case Type::MemberPointer: |
| 1689 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1690 | break; |
| 1691 | case Type::LValueReference: |
| 1692 | case Type::RValueReference: |
| 1693 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1694 | break; |
| 1695 | case Type::PackExpansion: |
| 1696 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1697 | break; |
| 1698 | case Type::Paren: |
| 1699 | case Type::ConstantArray: |
| 1700 | case Type::DependentSizedArray: |
| 1701 | case Type::IncompleteArray: |
| 1702 | case Type::VariableArray: |
| 1703 | case Type::FunctionProto: |
| 1704 | case Type::FunctionNoProto: |
| 1705 | return true; |
| 1706 | } |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | } // namespace |
| 1711 | |
| 1712 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1713 | SourceLocation RangeEnd = getLocation(); |
| 1714 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1715 | // If the declaration has no name or the type extends past the name take the |
| 1716 | // end location of the type. |
| 1717 | if (!getDeclName() || typeIsPostfix(TInfo->getType())) |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1718 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1719 | } |
| 1720 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1721 | } |
| 1722 | |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1723 | void |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1724 | QualifierInfo::setTemplateParameterListsInfo(ASTContext &Context, |
| 1725 | unsigned NumTPLists, |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1726 | TemplateParameterList **TPLists) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1727 | assert((NumTPLists == 0 || TPLists != nullptr) && |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1728 | "Empty array of template parameters with positive size!"); |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1729 | |
| 1730 | // Free previous template parameters (if any). |
| 1731 | if (NumTemplParamLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1732 | Context.Deallocate(TemplParamLists); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1733 | TemplParamLists = nullptr; |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1734 | NumTemplParamLists = 0; |
| 1735 | } |
| 1736 | // Set info on matched template parameter lists (if any). |
| 1737 | if (NumTPLists > 0) { |
Douglas Gregor | c722ea4 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1738 | TemplParamLists = new (Context) TemplateParameterList*[NumTPLists]; |
Abramo Bagnara | 9b93488 | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1739 | NumTemplParamLists = NumTPLists; |
| 1740 | for (unsigned i = NumTPLists; i-- > 0; ) |
| 1741 | TemplParamLists[i] = TPLists[i]; |
| 1742 | } |
| 1743 | } |
| 1744 | |
Argyrios Kyrtzidis | a5d8200 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1745 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1746 | // VarDecl Implementation |
| 1747 | //===----------------------------------------------------------------------===// |
| 1748 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1749 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1750 | switch (SC) { |
Peter Collingbourne | 8c25fc5 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1751 | case SC_None: break; |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1752 | case SC_Auto: return "auto"; |
| 1753 | case SC_Extern: return "extern"; |
| 1754 | case SC_OpenCLWorkGroupLocal: return "<<work-group-local>>"; |
| 1755 | case SC_PrivateExtern: return "__private_extern__"; |
| 1756 | case SC_Register: return "register"; |
| 1757 | case SC_Static: return "static"; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Peter Collingbourne | 8be0c74 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1760 | llvm_unreachable("Invalid storage class"); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1763 | VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 1764 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 1765 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| 1766 | StorageClass SC) |
| 1767 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
| 1768 | redeclarable_base(C), Init() { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1769 | static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), |
| 1770 | "VarDeclBitfields too large!"); |
| 1771 | static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), |
| 1772 | "ParmVarDeclBitfields too large!"); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1773 | AllBits = 0; |
| 1774 | VarDeclBits.SClass = SC; |
| 1775 | // Everything else is implicitly initialized to false. |
| 1776 | } |
| 1777 | |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1778 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1779 | SourceLocation StartL, SourceLocation IdL, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1780 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1781 | StorageClass S) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1782 | return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S); |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1785 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1786 | return new (C, ID) |
| 1787 | VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
| 1788 | QualType(), nullptr, SC_None); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1791 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1792 | assert(isLegalForVariable(SC)); |
John McCall | f1e4fbf | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1793 | VarDeclBits.SClass = SC; |
Douglas Gregor | 381d34e | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1796 | VarDecl::TLSKind VarDecl::getTLSKind() const { |
| 1797 | switch (VarDeclBits.TSCSpec) { |
| 1798 | case TSCS_unspecified: |
| 1799 | if (hasAttr<ThreadAttr>()) |
| 1800 | return TLS_Static; |
| 1801 | return TLS_None; |
| 1802 | case TSCS___thread: // Fall through. |
| 1803 | case TSCS__Thread_local: |
| 1804 | return TLS_Static; |
| 1805 | case TSCS_thread_local: |
| 1806 | return TLS_Dynamic; |
| 1807 | } |
| 1808 | llvm_unreachable("Unknown thread storage class specifier!"); |
| 1809 | } |
| 1810 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1811 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | d69f31c | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1812 | if (const Expr *Init = getInit()) { |
| 1813 | SourceLocation InitEnd = Init->getLocEnd(); |
Nico Weber | 3a344f9 | 2013-01-22 17:00:09 +0000 | [diff] [blame] | 1814 | // If Init is implicit, ignore its source range and fallback on |
| 1815 | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
| 1816 | if (InitEnd.isValid() && InitEnd != getLocation()) |
Argyrios Kyrtzidis | d69f31c | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1817 | return SourceRange(getOuterLocStart(), InitEnd); |
| 1818 | } |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1819 | return DeclaratorDecl::getSourceRange(); |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Rafael Espindola | 7ac928b | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 1822 | template<typename T> |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1823 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1824 | // C++ [dcl.link]p1: All function types, function names with external linkage, |
| 1825 | // and variable names with external linkage have a language linkage. |
Rafael Espindola | 181e3ec | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1826 | if (!D.hasExternalFormalLinkage()) |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1827 | return NoLanguageLinkage; |
| 1828 | |
| 1829 | // Language linkage is a C++ concept, but saying that everything else in C has |
Rafael Espindola | 2b721f5 | 2013-01-04 20:41:40 +0000 | [diff] [blame] | 1830 | // C language linkage fits the implementation nicely. |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1831 | ASTContext &Context = D.getASTContext(); |
| 1832 | if (!Context.getLangOpts().CPlusPlus) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1833 | return CLanguageLinkage; |
| 1834 | |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1835 | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
| 1836 | // language linkage of the names of class members and the function type of |
| 1837 | // class member functions. |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1838 | const DeclContext *DC = D.getDeclContext(); |
| 1839 | if (DC->isRecord()) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1840 | return CXXLanguageLinkage; |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1841 | |
| 1842 | // If the first decl is in an extern "C" context, any other redeclaration |
| 1843 | // will have C language linkage. If the first one is not in an extern "C" |
| 1844 | // context, we would have reported an error for any other decl being in one. |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1845 | if (isFirstInExternCContext(&D)) |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1846 | return CLanguageLinkage; |
| 1847 | return CXXLanguageLinkage; |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1850 | template<typename T> |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1851 | static bool isDeclExternC(const T &D) { |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1852 | // Since the context is ignored for class members, they can only have C++ |
| 1853 | // language linkage or no language linkage. |
| 1854 | const DeclContext *DC = D.getDeclContext(); |
| 1855 | if (DC->isRecord()) { |
| 1856 | assert(D.getASTContext().getLangOpts().CPlusPlus); |
| 1857 | return false; |
| 1858 | } |
| 1859 | |
| 1860 | return D.getLanguageLinkage() == CLanguageLinkage; |
| 1861 | } |
| 1862 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1863 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1864 | return getDeclLanguageLinkage(*this); |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1867 | bool VarDecl::isExternC() const { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1868 | return isDeclExternC(*this); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1871 | bool VarDecl::isInExternCContext() const { |
Serge Pavlov | 142ab06 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 1872 | return getLexicalDeclContext()->isExternCContext(); |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | bool VarDecl::isInExternCXXContext() const { |
Serge Pavlov | 142ab06 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 1876 | return getLexicalDeclContext()->isExternCXXContext(); |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1879 | VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1880 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1881 | VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition( |
| 1882 | ASTContext &C) const |
| 1883 | { |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1884 | // C++ [basic.def]p2: |
| 1885 | // A declaration is a definition unless [...] it contains the 'extern' |
| 1886 | // specifier or a linkage-specification and neither an initializer [...], |
| 1887 | // it declares a static data member in a class declaration [...]. |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1888 | // C++1y [temp.expl.spec]p15: |
| 1889 | // An explicit specialization of a static data member or an explicit |
| 1890 | // specialization of a static data member template is a definition if the |
| 1891 | // declaration includes an initializer; otherwise, it is a declaration. |
| 1892 | // |
| 1893 | // FIXME: How do you declare (but not define) a partial specialization of |
| 1894 | // a static data member template outside the containing class? |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1895 | if (isStaticDataMember()) { |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1896 | if (isOutOfLine() && |
| 1897 | (hasInit() || |
| 1898 | // If the first declaration is out-of-line, this may be an |
| 1899 | // instantiation of an out-of-line partial specialization of a variable |
| 1900 | // template for which we have not yet instantiated the initializer. |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1901 | (getFirstDecl()->isOutOfLine() |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1902 | ? getTemplateSpecializationKind() == TSK_Undeclared |
| 1903 | : getTemplateSpecializationKind() != |
| 1904 | TSK_ExplicitSpecialization) || |
| 1905 | isa<VarTemplatePartialSpecializationDecl>(this))) |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1906 | return Definition; |
| 1907 | else |
| 1908 | return DeclarationOnly; |
| 1909 | } |
| 1910 | // C99 6.7p5: |
| 1911 | // A definition of an identifier is a declaration for that identifier that |
| 1912 | // [...] causes storage to be reserved for that object. |
| 1913 | // Note: that applies for all non-file-scope objects. |
| 1914 | // C99 6.9.2p1: |
| 1915 | // If the declaration of an identifier for an object has file scope and an |
| 1916 | // initializer, the declaration is an external definition for the identifier |
| 1917 | if (hasInit()) |
| 1918 | return Definition; |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 1919 | |
Rafael Espindola | b1c0e20 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 1920 | if (hasAttr<AliasAttr>()) |
| 1921 | return Definition; |
| 1922 | |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1923 | // A variable template specialization (other than a static data member |
| 1924 | // template or an explicit specialization) is a declaration until we |
| 1925 | // instantiate its initializer. |
| 1926 | if (isa<VarTemplateSpecializationDecl>(this) && |
| 1927 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
| 1928 | return DeclarationOnly; |
| 1929 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1930 | if (hasExternalStorage()) |
| 1931 | return DeclarationOnly; |
Rafael Espindola | 3778300 | 2013-03-07 01:42:44 +0000 | [diff] [blame] | 1932 | |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 1933 | // [dcl.link] p7: |
| 1934 | // A declaration directly contained in a linkage-specification is treated |
| 1935 | // as if it contains the extern specifier for the purpose of determining |
| 1936 | // the linkage of the declared name and whether it is a definition. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1937 | if (isSingleLineLanguageLinkage(*this)) |
Rafael Espindola | e5e575d | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 1938 | return DeclarationOnly; |
Rafael Espindola | 65dfa2b | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 1939 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1940 | // C99 6.9.2p2: |
| 1941 | // A declaration of an object that has file scope without an initializer, |
| 1942 | // and without a storage class specifier or the scs 'static', constitutes |
| 1943 | // a tentative definition. |
| 1944 | // No such thing in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1945 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1946 | return TentativeDefinition; |
| 1947 | |
| 1948 | // What's left is (in C, block-scope) declarations without initializers or |
| 1949 | // external storage. These are definitions. |
| 1950 | return Definition; |
| 1951 | } |
| 1952 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1953 | VarDecl *VarDecl::getActingDefinition() { |
| 1954 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1955 | if (Kind != TentativeDefinition) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1956 | return nullptr; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1957 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1958 | VarDecl *LastTentative = nullptr; |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1959 | VarDecl *First = getFirstDecl(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1960 | for (auto I : First->redecls()) { |
| 1961 | Kind = I->isThisDeclarationADefinition(); |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1962 | if (Kind == Definition) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1963 | return nullptr; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1964 | else if (Kind == TentativeDefinition) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1965 | LastTentative = I; |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1966 | } |
| 1967 | return LastTentative; |
| 1968 | } |
| 1969 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1970 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1971 | VarDecl *First = getFirstDecl(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1972 | for (auto I : First->redecls()) { |
| 1973 | if (I->isThisDeclarationADefinition(C) == Definition) |
| 1974 | return I; |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1975 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1976 | return nullptr; |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1979 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1980 | DefinitionKind Kind = DeclarationOnly; |
| 1981 | |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1982 | const VarDecl *First = getFirstDecl(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1983 | for (auto I : First->redecls()) { |
| 1984 | Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); |
Daniel Dunbar | 047da19 | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 1985 | if (Kind == Definition) |
| 1986 | break; |
| 1987 | } |
John McCall | 110e8e5 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1988 | |
| 1989 | return Kind; |
| 1990 | } |
| 1991 | |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 1992 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1993 | for (auto I : redecls()) { |
| 1994 | if (auto Expr = I->getInit()) { |
| 1995 | D = I; |
| 1996 | return Expr; |
| 1997 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1998 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 1999 | return nullptr; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2002 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2003 | if (Decl::isOutOfLine()) |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2004 | return true; |
Chandler Carruth | 8761d68 | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 2005 | |
| 2006 | if (!isStaticDataMember()) |
| 2007 | return false; |
| 2008 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2009 | // If this static data member was instantiated from a static data member of |
| 2010 | // a class template, check whether that static data member was defined |
| 2011 | // out-of-line. |
| 2012 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 2013 | return VD->isOutOfLine(); |
| 2014 | |
| 2015 | return false; |
| 2016 | } |
| 2017 | |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2018 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 2019 | if (!isStaticDataMember()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2020 | return nullptr; |
| 2021 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2022 | for (auto RD : redecls()) { |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2023 | if (RD->getLexicalDeclContext()->isFileContext()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2024 | return RD; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2025 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2026 | |
| 2027 | return nullptr; |
Douglas Gregor | 0d03514 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2030 | void VarDecl::setInit(Expr *I) { |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2031 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| 2032 | Eval->~EvaluatedStmt(); |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2033 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | Init = I; |
| 2037 | } |
| 2038 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2039 | bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2040 | const LangOptions &Lang = C.getLangOpts(); |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2041 | |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2042 | if (!Lang.CPlusPlus) |
| 2043 | return false; |
| 2044 | |
| 2045 | // In C++11, any variable of reference type can be used in a constant |
| 2046 | // expression if it is initialized by a constant expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2047 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2048 | return true; |
| 2049 | |
| 2050 | // Only const objects can be used in constant expressions in C++. C++98 does |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2051 | // not require the variable to be non-volatile, but we consider this to be a |
| 2052 | // defect. |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2053 | if (!getType().isConstQualified() || getType().isVolatileQualified()) |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2054 | return false; |
| 2055 | |
| 2056 | // In C++, const, non-volatile variables of integral or enumeration types |
| 2057 | // can be used in constant expressions. |
| 2058 | if (getType()->isIntegralOrEnumerationType()) |
| 2059 | return true; |
| 2060 | |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2061 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 2062 | // constant expressions. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2063 | return Lang.CPlusPlus11 && isConstexpr(); |
Richard Smith | 1d238ea | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2066 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 2067 | /// form, which contains extra information on the evaluated value of the |
| 2068 | /// initializer. |
| 2069 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
| 2070 | EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
| 2071 | if (!Eval) { |
| 2072 | Stmt *S = Init.get<Stmt *>(); |
Manuel Klimek | f0f353b | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2073 | // Note: EvaluatedStmt contains an APValue, which usually holds |
| 2074 | // resources not allocated from the ASTContext. We need to do some |
| 2075 | // work to avoid leaking those, but we do so in VarDecl::evaluateValue |
| 2076 | // where we can detect whether there's anything to clean up or not. |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2077 | Eval = new (getASTContext()) EvaluatedStmt; |
| 2078 | Eval->Value = S; |
| 2079 | Init = Eval; |
| 2080 | } |
| 2081 | return Eval; |
| 2082 | } |
| 2083 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2084 | APValue *VarDecl::evaluateValue() const { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2085 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2086 | return evaluateValue(Notes); |
| 2087 | } |
| 2088 | |
Manuel Klimek | f0f353b | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2089 | namespace { |
| 2090 | // Destroy an APValue that was allocated in an ASTContext. |
| 2091 | void DestroyAPValue(void* UntypedValue) { |
| 2092 | static_cast<APValue*>(UntypedValue)->~APValue(); |
| 2093 | } |
| 2094 | } // namespace |
| 2095 | |
Richard Smith | 2d6a567 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2096 | APValue *VarDecl::evaluateValue( |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2097 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2098 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2099 | |
| 2100 | // We only produce notes indicating why an initializer is non-constant the |
| 2101 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 2102 | // first time we try evaluation, so might not be produced at all. |
| 2103 | if (Eval->WasEvaluated) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2104 | return Eval->Evaluated.isUninit() ? nullptr : &Eval->Evaluated; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2105 | |
| 2106 | const Expr *Init = cast<Expr>(Eval->Value); |
| 2107 | assert(!Init->isValueDependent()); |
| 2108 | |
| 2109 | if (Eval->IsEvaluating) { |
| 2110 | // FIXME: Produce a diagnostic for self-initialization. |
| 2111 | Eval->CheckedICE = true; |
| 2112 | Eval->IsICE = false; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2113 | return nullptr; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
| 2116 | Eval->IsEvaluating = true; |
| 2117 | |
| 2118 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), |
| 2119 | this, Notes); |
| 2120 | |
Manuel Klimek | f0f353b | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2121 | // Ensure the computed APValue is cleaned up later if evaluation succeeded, |
| 2122 | // or that it's empty (so that there's nothing to clean up) if evaluation |
| 2123 | // failed. |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2124 | if (!Result) |
| 2125 | Eval->Evaluated = APValue(); |
Manuel Klimek | f0f353b | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2126 | else if (Eval->Evaluated.needsCleanup()) |
| 2127 | getASTContext().AddDeallocation(DestroyAPValue, &Eval->Evaluated); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2128 | |
| 2129 | Eval->IsEvaluating = false; |
| 2130 | Eval->WasEvaluated = true; |
| 2131 | |
| 2132 | // In C++11, we have determined whether the initializer was a constant |
| 2133 | // expression as a side-effect. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2134 | if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) { |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2135 | Eval->CheckedICE = true; |
Eli Friedman | 210386e | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 2136 | Eval->IsICE = Result && Notes.empty(); |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2139 | return Result ? &Eval->Evaluated : nullptr; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | bool VarDecl::checkInitIsICE() const { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2143 | // Initializers of weak variables are never ICEs. |
| 2144 | if (isWeak()) |
| 2145 | return false; |
| 2146 | |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2147 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2148 | if (Eval->CheckedICE) |
| 2149 | // We have already checked whether this subexpression is an |
| 2150 | // integral constant expression. |
| 2151 | return Eval->IsICE; |
| 2152 | |
| 2153 | const Expr *Init = cast<Expr>(Eval->Value); |
| 2154 | assert(!Init->isValueDependent()); |
| 2155 | |
| 2156 | // In C++11, evaluate the initializer to check whether it's a constant |
| 2157 | // expression. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2158 | if (getASTContext().getLangOpts().CPlusPlus11) { |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2159 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | 099e7f6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2160 | evaluateValue(Notes); |
| 2161 | return Eval->IsICE; |
| 2162 | } |
| 2163 | |
| 2164 | // It's an ICE whether or not the definition we found is |
| 2165 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 2166 | // 6206 for details. |
| 2167 | |
| 2168 | if (Eval->CheckingICE) |
| 2169 | return false; |
| 2170 | Eval->CheckingICE = true; |
| 2171 | |
| 2172 | Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); |
| 2173 | Eval->CheckingICE = false; |
| 2174 | Eval->CheckedICE = true; |
| 2175 | return Eval->IsICE; |
| 2176 | } |
| 2177 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2178 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2179 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2180 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2181 | |
| 2182 | return nullptr; |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Douglas Gregor | 663b5a0 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 2185 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2186 | if (const VarTemplateSpecializationDecl *Spec = |
| 2187 | dyn_cast<VarTemplateSpecializationDecl>(this)) |
| 2188 | return Spec->getSpecializationKind(); |
| 2189 | |
Sebastian Redl | e9d12b6 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2190 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2191 | return MSI->getTemplateSpecializationKind(); |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2192 | |
Douglas Gregor | 251b4ff | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2193 | return TSK_Undeclared; |
| 2194 | } |
| 2195 | |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2196 | SourceLocation VarDecl::getPointOfInstantiation() const { |
| 2197 | if (const VarTemplateSpecializationDecl *Spec = |
| 2198 | dyn_cast<VarTemplateSpecializationDecl>(this)) |
| 2199 | return Spec->getPointOfInstantiation(); |
| 2200 | |
| 2201 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2202 | return MSI->getPointOfInstantiation(); |
| 2203 | |
| 2204 | return SourceLocation(); |
| 2205 | } |
| 2206 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2207 | VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { |
| 2208 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2209 | .dyn_cast<VarTemplateDecl *>(); |
| 2210 | } |
| 2211 | |
| 2212 | void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { |
| 2213 | getASTContext().setTemplateOrSpecializationInfo(this, Template); |
| 2214 | } |
| 2215 | |
Douglas Gregor | 1028c9f | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2216 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Richard Smith | 3f32210 | 2013-08-01 04:12:04 +0000 | [diff] [blame] | 2217 | if (isStaticDataMember()) |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2218 | // FIXME: Remove ? |
| 2219 | // return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 2220 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2221 | .dyn_cast<MemberSpecializationInfo *>(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2222 | return nullptr; |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2225 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2226 | SourceLocation PointOfInstantiation) { |
Richard Smith | d0629eb | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2227 | assert((isa<VarTemplateSpecializationDecl>(this) || |
| 2228 | getMemberSpecializationInfo()) && |
| 2229 | "not a variable or static data member template specialization"); |
| 2230 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2231 | if (VarTemplateSpecializationDecl *Spec = |
| 2232 | dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2233 | Spec->setSpecializationKind(TSK); |
| 2234 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
| 2235 | Spec->getPointOfInstantiation().isInvalid()) |
| 2236 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { |
| 2240 | MSI->setTemplateSpecializationKind(TSK); |
| 2241 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
| 2242 | MSI->getPointOfInstantiation().isInvalid()) |
| 2243 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2244 | } |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | void |
| 2248 | VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, |
| 2249 | TemplateSpecializationKind TSK) { |
| 2250 | assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && |
| 2251 | "Previous template or instantiation?"); |
| 2252 | getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2255 | //===----------------------------------------------------------------------===// |
| 2256 | // ParmVarDecl Implementation |
| 2257 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2258 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2259 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2260 | SourceLocation StartLoc, |
| 2261 | SourceLocation IdLoc, IdentifierInfo *Id, |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2262 | QualType T, TypeSourceInfo *TInfo, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2263 | StorageClass S, Expr *DefArg) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2264 | return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2265 | S, DefArg); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Reid Kleckner | 12df246 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2268 | QualType ParmVarDecl::getOriginalType() const { |
| 2269 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 2270 | QualType T = TSI ? TSI->getType() : getType(); |
| 2271 | if (const DecayedType *DT = dyn_cast<DecayedType>(T)) |
| 2272 | return DT->getOriginalType(); |
| 2273 | return T; |
| 2274 | } |
| 2275 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2276 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2277 | return new (C, ID) |
| 2278 | ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), |
| 2279 | nullptr, QualType(), nullptr, SC_None, nullptr); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Argyrios Kyrtzidis | 0bfe83b | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2282 | SourceRange ParmVarDecl::getSourceRange() const { |
| 2283 | if (!hasInheritedDefaultArg()) { |
| 2284 | SourceRange ArgRange = getDefaultArgRange(); |
| 2285 | if (ArgRange.isValid()) |
| 2286 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 2287 | } |
| 2288 | |
Argyrios Kyrtzidis | 673c5d5 | 2013-04-17 01:56:48 +0000 | [diff] [blame] | 2289 | // DeclaratorDecl considers the range of postfix types as overlapping with the |
| 2290 | // declaration name, but this is not the case with parameters in ObjC methods. |
| 2291 | if (isa<ObjCMethodDecl>(getDeclContext())) |
| 2292 | return SourceRange(DeclaratorDecl::getLocStart(), getLocation()); |
| 2293 | |
Argyrios Kyrtzidis | 0bfe83b | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2294 | return DeclaratorDecl::getSourceRange(); |
| 2295 | } |
| 2296 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2297 | Expr *ParmVarDecl::getDefaultArg() { |
| 2298 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 2299 | assert(!hasUninstantiatedDefaultArg() && |
| 2300 | "Default argument is not yet instantiated!"); |
| 2301 | |
| 2302 | Expr *Arg = getInit(); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 2303 | if (ExprWithCleanups *E = dyn_cast_or_null<ExprWithCleanups>(Arg)) |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2304 | return E->getSubExpr(); |
Douglas Gregor | 275a369 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2305 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2306 | return Arg; |
| 2307 | } |
| 2308 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2309 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 2310 | if (const Expr *E = getInit()) |
| 2311 | return E->getSourceRange(); |
| 2312 | |
| 2313 | if (hasUninstantiatedDefaultArg()) |
| 2314 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 2315 | |
| 2316 | return SourceRange(); |
Argyrios Kyrtzidis | fc7e2a8 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 2319 | bool ParmVarDecl::isParameterPack() const { |
| 2320 | return isa<PackExpansionType>(getType()); |
| 2321 | } |
| 2322 | |
Ted Kremenek | d211cb7 | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 2323 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 2324 | getASTContext().setParameterIndex(this, parameterIndex); |
| 2325 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 2326 | } |
| 2327 | |
| 2328 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 2329 | return getASTContext().getParameterIndex(this); |
| 2330 | } |
| 2331 | |
Nuno Lopes | 99f06ba | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 2332 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2333 | // FunctionDecl Implementation |
| 2334 | //===----------------------------------------------------------------------===// |
| 2335 | |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 2336 | void FunctionDecl::getNameForDiagnostic( |
| 2337 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 2338 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2339 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 2340 | if (TemplateArgs) |
Benjamin Kramer | 5eada84 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 2341 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 2342 | OS, TemplateArgs->data(), TemplateArgs->size(), Policy); |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2343 | } |
| 2344 | |
Ted Kremenek | 9498d38 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2345 | bool FunctionDecl::isVariadic() const { |
| 2346 | if (const FunctionProtoType *FT = getType()->getAs<FunctionProtoType>()) |
| 2347 | return FT->isVariadic(); |
| 2348 | return false; |
| 2349 | } |
| 2350 | |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2351 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2352 | for (auto I : redecls()) { |
Francois Pichet | 8387e2a | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2353 | if (I->Body || I->IsLateTemplateParsed) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2354 | Definition = I; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2355 | return true; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | return false; |
| 2360 | } |
| 2361 | |
Anders Carlsson | ffb945f | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 2362 | bool FunctionDecl::hasTrivialBody() const |
| 2363 | { |
| 2364 | Stmt *S = getBody(); |
| 2365 | if (!S) { |
| 2366 | // Since we don't have a body for this function, we don't know if it's |
| 2367 | // trivial or not. |
| 2368 | return false; |
| 2369 | } |
| 2370 | |
| 2371 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 2372 | return true; |
| 2373 | return false; |
| 2374 | } |
| 2375 | |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2376 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2377 | for (auto I : redecls()) { |
Rafael Espindola | b1c0e20 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2378 | if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed || |
| 2379 | I->hasAttr<AliasAttr>()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2380 | Definition = I->IsDeleted ? I->getCanonicalDecl() : I; |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2381 | return true; |
| 2382 | } |
| 2383 | } |
| 2384 | |
| 2385 | return false; |
| 2386 | } |
| 2387 | |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2388 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Rafael Espindola | 65d1096 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2389 | if (!hasBody(Definition)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2390 | return nullptr; |
Rafael Espindola | 65d1096 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2391 | |
| 2392 | if (Definition->Body) |
| 2393 | return Definition->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | f009795 | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2394 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2395 | return nullptr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2398 | void FunctionDecl::setBody(Stmt *B) { |
| 2399 | Body = B; |
Douglas Gregor | b5f35ba | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 2400 | if (B) |
Argyrios Kyrtzidis | 55d608c | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2401 | EndRangeLoc = B->getLocEnd(); |
| 2402 | } |
| 2403 | |
Douglas Gregor | 2138664 | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2404 | void FunctionDecl::setPure(bool P) { |
| 2405 | IsPure = P; |
| 2406 | if (P) |
| 2407 | if (CXXRecordDecl *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| 2408 | Parent->markedVirtualFunctionPure(); |
| 2409 | } |
| 2410 | |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2411 | template<std::size_t Len> |
| 2412 | static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { |
| 2413 | IdentifierInfo *II = ND->getIdentifier(); |
| 2414 | return II && II->isStr(Str); |
| 2415 | } |
| 2416 | |
Douglas Gregor | 48a83b5 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 2417 | bool FunctionDecl::isMain() const { |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2418 | const TranslationUnitDecl *tunit = |
| 2419 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2420 | return tunit && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2421 | !tunit->getASTContext().getLangOpts().Freestanding && |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2422 | isNamed(this, "main"); |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
David Majnemer | e9f6f33 | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 2425 | bool FunctionDecl::isMSVCRTEntryPoint() const { |
| 2426 | const TranslationUnitDecl *TUnit = |
| 2427 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2428 | if (!TUnit) |
| 2429 | return false; |
| 2430 | |
| 2431 | // Even though we aren't really targeting MSVCRT if we are freestanding, |
| 2432 | // semantic analysis for these functions remains the same. |
| 2433 | |
| 2434 | // MSVCRT entry points only exist on MSVCRT targets. |
| 2435 | if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) |
| 2436 | return false; |
| 2437 | |
| 2438 | // Nameless functions like constructors cannot be entry points. |
| 2439 | if (!getIdentifier()) |
| 2440 | return false; |
| 2441 | |
| 2442 | return llvm::StringSwitch<bool>(getName()) |
| 2443 | .Cases("main", // an ANSI console app |
| 2444 | "wmain", // a Unicode console App |
| 2445 | "WinMain", // an ANSI GUI app |
| 2446 | "wWinMain", // a Unicode GUI app |
| 2447 | "DllMain", // a DLL |
| 2448 | true) |
| 2449 | .Default(false); |
| 2450 | } |
| 2451 | |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2452 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 2453 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 2454 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 2455 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 2456 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 2457 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 2458 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2459 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2460 | return false; |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2461 | |
| 2462 | const FunctionProtoType *proto = getType()->castAs<FunctionProtoType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2463 | if (proto->getNumParams() != 2 || proto->isVariadic()) |
| 2464 | return false; |
John McCall | 23c608d | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2465 | |
| 2466 | ASTContext &Context = |
| 2467 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 2468 | ->getASTContext(); |
| 2469 | |
| 2470 | // The result type and first argument type are constant across all |
| 2471 | // these operators. The second argument must be exactly void*. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2472 | return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); |
Douglas Gregor | 04495c8 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2475 | bool FunctionDecl::isReplaceableGlobalAllocationFunction() const { |
| 2476 | if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) |
| 2477 | return false; |
| 2478 | if (getDeclName().getCXXOverloadedOperator() != OO_New && |
| 2479 | getDeclName().getCXXOverloadedOperator() != OO_Delete && |
| 2480 | getDeclName().getCXXOverloadedOperator() != OO_Array_New && |
| 2481 | getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
| 2482 | return false; |
| 2483 | |
| 2484 | if (isa<CXXRecordDecl>(getDeclContext())) |
| 2485 | return false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2486 | |
| 2487 | // This can only fail for an invalid 'operator new' declaration. |
| 2488 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2489 | return false; |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2490 | |
| 2491 | const FunctionProtoType *FPT = getType()->castAs<FunctionProtoType>(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2492 | if (FPT->getNumParams() == 0 || FPT->getNumParams() > 2 || FPT->isVariadic()) |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2493 | return false; |
| 2494 | |
| 2495 | // If this is a single-parameter function, it must be a replaceable global |
| 2496 | // allocation or deallocation function. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2497 | if (FPT->getNumParams() == 1) |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2498 | return true; |
| 2499 | |
| 2500 | // Otherwise, we're looking for a second parameter whose type is |
Richard Smith | 4cb295d | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2501 | // 'const std::nothrow_t &', or, in C++1y, 'std::size_t'. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2502 | QualType Ty = FPT->getParamType(1); |
Richard Smith | 4cb295d | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2503 | ASTContext &Ctx = getASTContext(); |
Richard Smith | 3cebc73 | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 2504 | if (Ctx.getLangOpts().SizedDeallocation && |
| 2505 | Ctx.hasSameType(Ty, Ctx.getSizeType())) |
Richard Smith | 4cb295d | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2506 | return true; |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2507 | if (!Ty->isReferenceType()) |
| 2508 | return false; |
| 2509 | Ty = Ty->getPointeeType(); |
| 2510 | if (Ty.getCVRQualifiers() != Qualifiers::Const) |
| 2511 | return false; |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2512 | const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2513 | return RD && isNamed(RD, "nothrow_t") && RD->isInStdNamespace(); |
Richard Smith | ddcff1b | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2514 | } |
| 2515 | |
Rafael Espindola | 950fee2 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2516 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2517 | return getDeclLanguageLinkage(*this); |
Rafael Espindola | 78eeba8 | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2520 | bool FunctionDecl::isExternC() const { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2521 | return isDeclExternC(*this); |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2524 | bool FunctionDecl::isInExternCContext() const { |
Serge Pavlov | 142ab06 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2525 | return getLexicalDeclContext()->isExternCContext(); |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | bool FunctionDecl::isInExternCXXContext() const { |
Serge Pavlov | 142ab06 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2529 | return getLexicalDeclContext()->isExternCXXContext(); |
Rafael Espindola | d8ffd0b | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2532 | bool FunctionDecl::isGlobal() const { |
| 2533 | if (const CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(this)) |
| 2534 | return Method->isStatic(); |
| 2535 | |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2536 | if (getCanonicalDecl()->getStorageClass() == SC_Static) |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2537 | return false; |
| 2538 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2540 | DC->isNamespace(); |
| 2541 | DC = DC->getParent()) { |
| 2542 | if (const NamespaceDecl *Namespace = cast<NamespaceDecl>(DC)) { |
| 2543 | if (!Namespace->getDeclName()) |
| 2544 | return false; |
| 2545 | break; |
| 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | return true; |
| 2550 | } |
| 2551 | |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2552 | bool FunctionDecl::isNoReturn() const { |
| 2553 | return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
Richard Smith | 7586a6e | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2554 | hasAttr<C11NoReturnAttr>() || |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2555 | getType()->getAs<FunctionType>()->getNoReturnAttr(); |
| 2556 | } |
| 2557 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2558 | void |
| 2559 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2560 | redeclarable_base::setPreviousDecl(PrevDecl); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2561 | |
| 2562 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 2563 | FunctionTemplateDecl *PrevFunTmpl |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2564 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2565 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2566 | FunTmpl->setPreviousDecl(PrevFunTmpl); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2567 | } |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2568 | |
Axel Naumann | d9d137e | 2011-11-08 18:21:06 +0000 | [diff] [blame] | 2569 | if (PrevDecl && PrevDecl->IsInline) |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2570 | IsInline = true; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | const FunctionDecl *FunctionDecl::getCanonicalDecl() const { |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2574 | return getFirstDecl(); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2577 | FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2578 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2579 | /// \brief Returns a value indicating whether this function |
| 2580 | /// corresponds to a builtin function. |
| 2581 | /// |
| 2582 | /// The function corresponds to a built-in function if it is |
| 2583 | /// declared at translation scope or within an extern "C" block and |
| 2584 | /// its name matches with the name of a builtin. The returned value |
| 2585 | /// 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] | 2586 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2587 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 2588 | unsigned FunctionDecl::getBuiltinID() const { |
Daniel Dunbar | 60d302a | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 2589 | if (!getIdentifier()) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2590 | return 0; |
| 2591 | |
| 2592 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
Daniel Dunbar | 60d302a | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 2593 | if (!BuiltinID) |
| 2594 | return 0; |
| 2595 | |
| 2596 | ASTContext &Context = getASTContext(); |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2597 | if (Context.getLangOpts().CPlusPlus) { |
| 2598 | const LinkageSpecDecl *LinkageDecl = dyn_cast<LinkageSpecDecl>( |
| 2599 | getFirstDecl()->getDeclContext()); |
| 2600 | // In C++, the first declaration of a builtin is always inside an implicit |
| 2601 | // extern "C". |
| 2602 | // FIXME: A recognised library function may not be directly in an extern "C" |
| 2603 | // declaration, for instance "extern "C" { namespace std { decl } }". |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 2604 | if (!LinkageDecl) { |
| 2605 | if (BuiltinID == Builtin::BI__GetExceptionInfo && |
| 2606 | Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 2607 | isInStdNamespace()) |
| 2608 | return Builtin::BI__GetExceptionInfo; |
| 2609 | return 0; |
| 2610 | } |
| 2611 | if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c) |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2612 | return 0; |
| 2613 | } |
| 2614 | |
| 2615 | // If the function is marked "overloadable", it has a different mangled name |
| 2616 | // and is not the C library function. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2617 | if (hasAttr<OverloadableAttr>()) |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2618 | return 0; |
| 2619 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2620 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 2621 | return BuiltinID; |
| 2622 | |
| 2623 | // This function has the name of a known C library |
| 2624 | // function. Determine whether it actually refers to the C library |
| 2625 | // function or whether it just has the same name. |
| 2626 | |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 2627 | // If this is a static function, it's not a builtin. |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2628 | if (getStorageClass() == SC_Static) |
Douglas Gregor | 9add317 | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 2629 | return 0; |
| 2630 | |
Warren Hunt | 2d023ec | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2631 | return BuiltinID; |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2632 | } |
| 2633 | |
| 2634 | |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 2635 | /// getNumParams - Return the number of parameters this function must have |
Bob Wilson | 8dbfbf4 | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 2636 | /// based on its FunctionType. This is the length of the ParamInfo array |
Chris Lattner | 1ad9b28 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 2637 | /// after it has been created. |
| 2638 | unsigned FunctionDecl::getNumParams() const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2639 | const FunctionProtoType *FPT = getType()->getAs<FunctionProtoType>(); |
| 2640 | return FPT ? FPT->getNumParams() : 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2643 | void FunctionDecl::setParams(ASTContext &C, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2644 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2645 | assert(!ParamInfo && "Already has param info!"); |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2646 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2647 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2648 | // Zero params -> null pointer. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2649 | if (!NewParamInfo.empty()) { |
| 2650 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 2651 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2652 | } |
| 2653 | } |
| 2654 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2655 | void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) { |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 2656 | assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!"); |
| 2657 | |
| 2658 | if (!NewDecls.empty()) { |
| 2659 | NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()]; |
| 2660 | std::copy(NewDecls.begin(), NewDecls.end(), A); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2661 | DeclsInPrototypeScope = llvm::makeArrayRef(A, NewDecls.size()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2662 | // Move declarations introduced in prototype to the function context. |
| 2663 | for (auto I : NewDecls) { |
| 2664 | DeclContext *DC = I->getDeclContext(); |
| 2665 | // Forward-declared reference to an enumeration is not added to |
| 2666 | // declaration scope, so skip declaration that is absent from its |
| 2667 | // declaration contexts. |
| 2668 | if (DC->containsDecl(I)) { |
| 2669 | DC->removeDecl(I); |
| 2670 | I->setDeclContext(this); |
| 2671 | addDecl(I); |
| 2672 | } |
| 2673 | } |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 2674 | } |
| 2675 | } |
| 2676 | |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2677 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 2678 | /// needed to call this function. This may be fewer than the number of |
| 2679 | /// function parameters, if some of the parameters have default |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2680 | /// arguments (in C++) or are parameter packs (C++11). |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2681 | unsigned FunctionDecl::getMinRequiredArguments() const { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2682 | if (!getASTContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 2683 | return getNumParams(); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2684 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2685 | unsigned NumRequiredArgs = 0; |
| 2686 | for (auto *Param : params()) |
| 2687 | if (!Param->isParameterPack() && !Param->hasDefaultArg()) |
| 2688 | ++NumRequiredArgs; |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2689 | return NumRequiredArgs; |
| 2690 | } |
| 2691 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2692 | /// \brief The combination of the extern and inline keywords under MSVC forces |
| 2693 | /// the function to be required. |
| 2694 | /// |
| 2695 | /// Note: This function assumes that we will only get called when isInlined() |
| 2696 | /// would return true for this FunctionDecl. |
| 2697 | bool FunctionDecl::isMSExternInline() const { |
| 2698 | assert(isInlined() && "expected to get called on an inlined function!"); |
| 2699 | |
| 2700 | const ASTContext &Context = getASTContext(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2701 | if (!Context.getLangOpts().MSVCCompat && !hasAttr<DLLExportAttr>()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2702 | return false; |
| 2703 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 2704 | for (const FunctionDecl *FD = getMostRecentDecl(); FD; |
| 2705 | FD = FD->getPreviousDecl()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2706 | if (FD->getStorageClass() == SC_Extern) |
| 2707 | return true; |
| 2708 | |
| 2709 | return false; |
| 2710 | } |
| 2711 | |
| 2712 | static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { |
| 2713 | if (Redecl->getStorageClass() != SC_Extern) |
| 2714 | return false; |
| 2715 | |
| 2716 | for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; |
| 2717 | FD = FD->getPreviousDecl()) |
| 2718 | if (FD->getStorageClass() == SC_Extern) |
| 2719 | return false; |
| 2720 | |
| 2721 | return true; |
| 2722 | } |
| 2723 | |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2724 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 2725 | // Only consider file-scope declarations in this test. |
| 2726 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 2727 | return false; |
| 2728 | |
| 2729 | // Only consider explicit declarations; the presence of a builtin for a |
| 2730 | // libcall shouldn't affect whether a definition is externally visible. |
| 2731 | if (Redecl->isImplicit()) |
| 2732 | return false; |
| 2733 | |
| 2734 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| 2735 | return true; // Not an inline definition |
| 2736 | |
| 2737 | return false; |
| 2738 | } |
| 2739 | |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2740 | /// \brief For a function declaration in C or C++, determine whether this |
| 2741 | /// declaration causes the definition to be externally visible. |
| 2742 | /// |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2743 | /// For instance, this determines if adding the current declaration to the set |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2744 | /// of redeclarations of the given functions causes |
| 2745 | /// isInlineDefinitionExternallyVisible to change from false to true. |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2746 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| 2747 | assert(!doesThisDeclarationHaveABody() && |
| 2748 | "Must have a declaration without a body."); |
| 2749 | |
| 2750 | ASTContext &Context = getASTContext(); |
| 2751 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2752 | if (Context.getLangOpts().MSVCCompat) { |
| 2753 | const FunctionDecl *Definition; |
| 2754 | if (hasBody(Definition) && Definition->isInlined() && |
| 2755 | redeclForcesDefMSVC(this)) |
| 2756 | return true; |
| 2757 | } |
| 2758 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2759 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2760 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 2761 | // an externally visible definition. |
| 2762 | // |
| 2763 | // FIXME: What happens if gnu_inline gets added on after the first |
| 2764 | // declaration? |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2765 | if (!isInlineSpecified() || getStorageClass() == SC_Extern) |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2766 | return false; |
| 2767 | |
| 2768 | const FunctionDecl *Prev = this; |
| 2769 | bool FoundBody = false; |
| 2770 | while ((Prev = Prev->getPreviousDecl())) { |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2771 | FoundBody |= Prev->Body.isValid(); |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2772 | |
| 2773 | if (Prev->Body) { |
| 2774 | // If it's not the case that both 'inline' and 'extern' are |
| 2775 | // specified on the definition, then it is always externally visible. |
| 2776 | if (!Prev->isInlineSpecified() || |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2777 | Prev->getStorageClass() != SC_Extern) |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2778 | return false; |
| 2779 | } else if (Prev->isInlineSpecified() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2780 | Prev->getStorageClass() != SC_Extern) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2781 | return false; |
| 2782 | } |
| 2783 | } |
| 2784 | return FoundBody; |
| 2785 | } |
| 2786 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2787 | if (Context.getLangOpts().CPlusPlus) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2788 | return false; |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2789 | |
| 2790 | // C99 6.7.4p6: |
| 2791 | // [...] If all of the file scope declarations for a function in a |
| 2792 | // translation unit include the inline function specifier without extern, |
| 2793 | // then the definition in that translation unit is an inline definition. |
| 2794 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2795 | return false; |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2796 | const FunctionDecl *Prev = this; |
| 2797 | bool FoundBody = false; |
| 2798 | while ((Prev = Prev->getPreviousDecl())) { |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2799 | FoundBody |= Prev->Body.isValid(); |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2800 | if (RedeclForcesDefC99(Prev)) |
| 2801 | return false; |
| 2802 | } |
| 2803 | return FoundBody; |
Nick Lewycky | dce67a7 | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2806 | SourceRange FunctionDecl::getReturnTypeSourceRange() const { |
| 2807 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 2808 | if (!TSI) |
| 2809 | return SourceRange(); |
| 2810 | FunctionTypeLoc FTL = |
| 2811 | TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>(); |
| 2812 | if (!FTL) |
| 2813 | return SourceRange(); |
| 2814 | |
| 2815 | // Skip self-referential return types. |
| 2816 | const SourceManager &SM = getASTContext().getSourceManager(); |
| 2817 | SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); |
| 2818 | SourceLocation Boundary = getNameInfo().getLocStart(); |
| 2819 | if (RTRange.isInvalid() || Boundary.isInvalid() || |
| 2820 | !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) |
| 2821 | return SourceRange(); |
| 2822 | |
| 2823 | return RTRange; |
| 2824 | } |
| 2825 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2826 | /// \brief For an inline function definition in C, or for a gnu_inline function |
| 2827 | /// in C++, determine whether the definition will be externally visible. |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2828 | /// |
| 2829 | /// Inline function definitions are always available for inlining optimizations. |
| 2830 | /// However, depending on the language dialect, declaration specifiers, and |
| 2831 | /// attributes, the definition of an inline function may or may not be |
| 2832 | /// "externally" visible to other translation units in the program. |
| 2833 | /// |
| 2834 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 1e5fd7f | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 2835 | /// 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] | 2836 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 2837 | /// |
| 2838 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 2839 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 2840 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 2841 | /// an externally visible symbol, but "extern inline" will not create an |
| 2842 | /// externally visible symbol. |
| 2843 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2844 | assert(doesThisDeclarationHaveABody() && "Must have the function definition"); |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2845 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | 7d9c3c9 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 2846 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2847 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2848 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2849 | // Note: If you change the logic here, please change |
| 2850 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 2851 | // |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2852 | // If it's not the case that both 'inline' and 'extern' are |
| 2853 | // specified on the definition, then this inline definition is |
| 2854 | // externally visible. |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2855 | if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2856 | return true; |
| 2857 | |
| 2858 | // If any declaration is 'inline' but not 'extern', then this definition |
| 2859 | // is externally visible. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2860 | for (auto Redecl : redecls()) { |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2861 | if (Redecl->isInlineSpecified() && |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2862 | Redecl->getStorageClass() != SC_Extern) |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2863 | return true; |
Douglas Gregor | 8f15094 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2864 | } |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2865 | |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2866 | return false; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2867 | } |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2868 | |
Richard Smith | d4497dd | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2869 | // The rest of this function is C-only. |
| 2870 | assert(!Context.getLangOpts().CPlusPlus && |
| 2871 | "should not use C inline rules in C++"); |
| 2872 | |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2873 | // C99 6.7.4p6: |
| 2874 | // [...] If all of the file scope declarations for a function in a |
| 2875 | // translation unit include the inline function specifier without extern, |
| 2876 | // then the definition in that translation unit is an inline definition. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2877 | for (auto Redecl : redecls()) { |
| 2878 | if (RedeclForcesDefC99(Redecl)) |
Eli Friedman | a3b9fa2 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2879 | return true; |
Douglas Gregor | 1fc09a9 | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
| 2882 | // C99 6.7.4p6: |
| 2883 | // An inline definition does not provide an external definition for the |
| 2884 | // function, and does not forbid an external definition in another |
| 2885 | // translation unit. |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2886 | return false; |
| 2887 | } |
| 2888 | |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2889 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 2890 | /// function represents, if any. |
| 2891 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | e94ca9e4 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 2892 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 2893 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2894 | else |
| 2895 | return OO_None; |
| 2896 | } |
| 2897 | |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2898 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 2899 | /// represents, if any. |
| 2900 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 2901 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 2902 | return getDeclName().getCXXLiteralIdentifier(); |
| 2903 | else |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2904 | return nullptr; |
Sean Hunt | a6c058d | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2907 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 2908 | if (TemplateOrSpecialization.isNull()) |
| 2909 | return TK_NonTemplate; |
| 2910 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 2911 | return TK_FunctionTemplate; |
| 2912 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 2913 | return TK_MemberSpecialization; |
| 2914 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 2915 | return TK_FunctionTemplateSpecialization; |
| 2916 | if (TemplateOrSpecialization.is |
| 2917 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 2918 | return TK_DependentFunctionTemplateSpecialization; |
| 2919 | |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2920 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
Argyrios Kyrtzidis | d091355 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2923 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2924 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2925 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2926 | |
| 2927 | return nullptr; |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2931 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 2932 | FunctionDecl *FD, |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2933 | TemplateSpecializationKind TSK) { |
| 2934 | assert(TemplateOrSpecialization.isNull() && |
| 2935 | "Member function is already a specialization"); |
| 2936 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2937 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2938 | TemplateOrSpecialization = Info; |
| 2939 | } |
| 2940 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2941 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2942 | // If the function is invalid, it can't be implicitly instantiated. |
| 2943 | if (isInvalidDecl()) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2944 | return false; |
| 2945 | |
| 2946 | switch (getTemplateSpecializationKind()) { |
| 2947 | case TSK_Undeclared: |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2948 | case TSK_ExplicitInstantiationDefinition: |
| 2949 | return false; |
| 2950 | |
| 2951 | case TSK_ImplicitInstantiation: |
| 2952 | return true; |
| 2953 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2954 | // It is possible to instantiate TSK_ExplicitSpecialization kind |
| 2955 | // if the FunctionDecl has a class scope specialization pattern. |
| 2956 | case TSK_ExplicitSpecialization: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2957 | return getClassScopeSpecializationPattern() != nullptr; |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2958 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2959 | case TSK_ExplicitInstantiationDeclaration: |
| 2960 | // Handled below. |
| 2961 | break; |
| 2962 | } |
| 2963 | |
| 2964 | // Find the actual template from which we will instantiate. |
| 2965 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2966 | bool HasPattern = false; |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2967 | if (PatternDecl) |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2968 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2969 | |
| 2970 | // C++0x [temp.explicit]p9: |
| 2971 | // Except for inline functions, other explicit instantiation declarations |
| 2972 | // have the effect of suppressing the implicit instantiation of the entity |
| 2973 | // to which they refer. |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2974 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2975 | return true; |
| 2976 | |
Douglas Gregor | 7ced9c8 | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2977 | return PatternDecl->isInlined(); |
Ted Kremenek | 75df4ee | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | bool FunctionDecl::isTemplateInstantiation() const { |
| 2981 | switch (getTemplateSpecializationKind()) { |
| 2982 | case TSK_Undeclared: |
| 2983 | case TSK_ExplicitSpecialization: |
| 2984 | return false; |
| 2985 | case TSK_ImplicitInstantiation: |
| 2986 | case TSK_ExplicitInstantiationDeclaration: |
| 2987 | case TSK_ExplicitInstantiationDefinition: |
| 2988 | return true; |
| 2989 | } |
| 2990 | llvm_unreachable("All TSK values handled."); |
| 2991 | } |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2992 | |
| 2993 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2994 | // Handle class scope explicit specialization special case. |
| 2995 | if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 2996 | return getClassScopeSpecializationPattern(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2997 | |
| 2998 | // If this is a generic lambda call operator specialization, its |
| 2999 | // instantiation pattern is always its primary template's pattern |
| 3000 | // even if its primary template was instantiated from another |
| 3001 | // member template (which happens with nested generic lambdas). |
| 3002 | // Since a lambda's call operator's body is transformed eagerly, |
| 3003 | // we don't have to go hunting for a prototype definition template |
| 3004 | // (i.e. instantiated-from-member-template) to use as an instantiation |
| 3005 | // pattern. |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3006 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3007 | if (isGenericLambdaCallOperatorSpecialization( |
| 3008 | dyn_cast<CXXMethodDecl>(this))) { |
| 3009 | assert(getPrimaryTemplate() && "A generic lambda specialization must be " |
| 3010 | "generated from a primary call operator " |
| 3011 | "template"); |
| 3012 | assert(getPrimaryTemplate()->getTemplatedDecl()->getBody() && |
| 3013 | "A generic lambda call operator template must always have a body - " |
| 3014 | "even if instantiated from a prototype (i.e. as written) member " |
| 3015 | "template"); |
| 3016 | return getPrimaryTemplate()->getTemplatedDecl(); |
| 3017 | } |
| 3018 | |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3019 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 3020 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 3021 | // If we have hit a point where the user provided a specialization of |
| 3022 | // this template, we're done looking. |
| 3023 | if (Primary->isMemberSpecialization()) |
| 3024 | break; |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3025 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 3026 | } |
| 3027 | |
| 3028 | return Primary->getTemplatedDecl(); |
| 3029 | } |
| 3030 | |
| 3031 | return getInstantiatedFromMemberFunction(); |
| 3032 | } |
| 3033 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3034 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3035 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3036 | = TemplateOrSpecialization |
| 3037 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3038 | return Info->Template.getPointer(); |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3039 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3040 | return nullptr; |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3041 | } |
| 3042 | |
Francois Pichet | af0f4d0 | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3043 | FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const { |
| 3044 | return getASTContext().getClassScopeSpecializationPattern(this); |
| 3045 | } |
| 3046 | |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3047 | const TemplateArgumentList * |
| 3048 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3049 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | fd056bc | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 3050 | = TemplateOrSpecialization |
| 3051 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3052 | return Info->TemplateArguments; |
| 3053 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3054 | return nullptr; |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Argyrios Kyrtzidis | 71a7605 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 3057 | const ASTTemplateArgumentListInfo * |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3058 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 3059 | if (FunctionTemplateSpecializationInfo *Info |
| 3060 | = TemplateOrSpecialization |
| 3061 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 3062 | return Info->TemplateArgumentsAsWritten; |
| 3063 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3064 | return nullptr; |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | void |
Argyrios Kyrtzidis | 6b54151 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3068 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 3069 | FunctionTemplateDecl *Template, |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3070 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3071 | void *InsertPos, |
Abramo Bagnara | e03db98 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3072 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 7b081c8 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 3073 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 3074 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | b9aa6b2 | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3075 | assert(TSK != TSK_Undeclared && |
| 3076 | "Must specify the type of function template specialization"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 16e8be2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3078 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3079 | if (!Info) |
Argyrios Kyrtzidis | a626a3d | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 3080 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 3081 | TemplateArgs, |
| 3082 | TemplateArgsAsWritten, |
| 3083 | PointOfInstantiation); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3084 | TemplateOrSpecialization = Info; |
Douglas Gregor | 1e1e972 | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 3085 | Template->addSpecialization(Info, InsertPos); |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3088 | void |
| 3089 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 3090 | const UnresolvedSetImpl &Templates, |
| 3091 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3092 | assert(TemplateOrSpecialization.isNull()); |
| 3093 | size_t Size = sizeof(DependentFunctionTemplateSpecializationInfo); |
| 3094 | Size += Templates.size() * sizeof(FunctionTemplateDecl*); |
John McCall | 21c0160 | 2010-04-13 22:18:28 +0000 | [diff] [blame] | 3095 | Size += TemplateArgs.size() * sizeof(TemplateArgumentLoc); |
John McCall | af2094e | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3096 | void *Buffer = Context.Allocate(Size); |
| 3097 | DependentFunctionTemplateSpecializationInfo *Info = |
| 3098 | new (Buffer) DependentFunctionTemplateSpecializationInfo(Templates, |
| 3099 | TemplateArgs); |
| 3100 | TemplateOrSpecialization = Info; |
| 3101 | } |
| 3102 | |
| 3103 | DependentFunctionTemplateSpecializationInfo:: |
| 3104 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 3105 | const TemplateArgumentListInfo &TArgs) |
| 3106 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| 3107 | |
| 3108 | d.NumTemplates = Ts.size(); |
| 3109 | d.NumArgs = TArgs.size(); |
| 3110 | |
| 3111 | FunctionTemplateDecl **TsArray = |
| 3112 | const_cast<FunctionTemplateDecl**>(getTemplates()); |
| 3113 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 3114 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 3115 | |
| 3116 | TemplateArgumentLoc *ArgsArray = |
| 3117 | const_cast<TemplateArgumentLoc*>(getTemplateArgs()); |
| 3118 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 3119 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 3120 | } |
| 3121 | |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3122 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | // For a function template specialization, query the specialization |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3124 | // information object. |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3125 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3126 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3127 | if (FTSInfo) |
| 3128 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3130 | MemberSpecializationInfo *MSInfo |
| 3131 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 3132 | if (MSInfo) |
| 3133 | return MSInfo->getTemplateSpecializationKind(); |
| 3134 | |
| 3135 | return TSK_Undeclared; |
Douglas Gregor | d0e3daf | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3138 | void |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3139 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 3140 | SourceLocation PointOfInstantiation) { |
| 3141 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3142 | = TemplateOrSpecialization.dyn_cast< |
| 3143 | FunctionTemplateSpecializationInfo*>()) { |
| 3144 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 3145 | if (TSK != TSK_ExplicitSpecialization && |
| 3146 | PointOfInstantiation.isValid() && |
| 3147 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 3148 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 3149 | } else if (MemberSpecializationInfo *MSInfo |
| 3150 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 3151 | MSInfo->setTemplateSpecializationKind(TSK); |
| 3152 | if (TSK != TSK_ExplicitSpecialization && |
| 3153 | PointOfInstantiation.isValid() && |
| 3154 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 3155 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 3156 | } else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3157 | llvm_unreachable("Function cannot have a template specialization kind"); |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3161 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3162 | = TemplateOrSpecialization.dyn_cast< |
| 3163 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3164 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | 2db3232 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3165 | else if (MemberSpecializationInfo *MSInfo |
| 3166 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 0a897e3 | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3167 | return MSInfo->getPointOfInstantiation(); |
| 3168 | |
| 3169 | return SourceLocation(); |
Douglas Gregor | 1fd2dd1 | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3172 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3173 | if (Decl::isOutOfLine()) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3174 | return true; |
| 3175 | |
| 3176 | // If this function was instantiated from a member function of a |
| 3177 | // class template, check whether that member function was defined out-of-line. |
| 3178 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 3179 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3180 | if (FD->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3181 | return Definition->isOutOfLine(); |
| 3182 | } |
| 3183 | |
| 3184 | // If this function was instantiated from a function template, |
| 3185 | // check whether that function template was defined out-of-line. |
| 3186 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 3187 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3188 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 9f18507 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3189 | return Definition->isOutOfLine(); |
| 3190 | } |
| 3191 | |
| 3192 | return false; |
| 3193 | } |
| 3194 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3195 | SourceRange FunctionDecl::getSourceRange() const { |
| 3196 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 3197 | } |
| 3198 | |
Anna Zaks | 9392d4e | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 3199 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3200 | IdentifierInfo *FnInfo = getIdentifier(); |
| 3201 | |
| 3202 | if (!FnInfo) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3203 | return 0; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3204 | |
| 3205 | // Builtin handling. |
| 3206 | switch (getBuiltinID()) { |
| 3207 | case Builtin::BI__builtin_memset: |
| 3208 | case Builtin::BI__builtin___memset_chk: |
| 3209 | case Builtin::BImemset: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3210 | return Builtin::BImemset; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3211 | |
| 3212 | case Builtin::BI__builtin_memcpy: |
| 3213 | case Builtin::BI__builtin___memcpy_chk: |
| 3214 | case Builtin::BImemcpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3215 | return Builtin::BImemcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3216 | |
| 3217 | case Builtin::BI__builtin_memmove: |
| 3218 | case Builtin::BI__builtin___memmove_chk: |
| 3219 | case Builtin::BImemmove: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3220 | return Builtin::BImemmove; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3221 | |
| 3222 | case Builtin::BIstrlcpy: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3223 | case Builtin::BI__builtin___strlcpy_chk: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3224 | return Builtin::BIstrlcpy; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3225 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3226 | case Builtin::BIstrlcat: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3227 | case Builtin::BI__builtin___strlcat_chk: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3228 | return Builtin::BIstrlcat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3229 | |
| 3230 | case Builtin::BI__builtin_memcmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3231 | case Builtin::BImemcmp: |
| 3232 | return Builtin::BImemcmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3233 | |
| 3234 | case Builtin::BI__builtin_strncpy: |
| 3235 | case Builtin::BI__builtin___strncpy_chk: |
| 3236 | case Builtin::BIstrncpy: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3237 | return Builtin::BIstrncpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3238 | |
| 3239 | case Builtin::BI__builtin_strncmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3240 | case Builtin::BIstrncmp: |
| 3241 | return Builtin::BIstrncmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3242 | |
| 3243 | case Builtin::BI__builtin_strncasecmp: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3244 | case Builtin::BIstrncasecmp: |
| 3245 | return Builtin::BIstrncasecmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3246 | |
| 3247 | case Builtin::BI__builtin_strncat: |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3248 | case Builtin::BI__builtin___strncat_chk: |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3249 | case Builtin::BIstrncat: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3250 | return Builtin::BIstrncat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3251 | |
| 3252 | case Builtin::BI__builtin_strndup: |
| 3253 | case Builtin::BIstrndup: |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3254 | return Builtin::BIstrndup; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3255 | |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3256 | case Builtin::BI__builtin_strlen: |
| 3257 | case Builtin::BIstrlen: |
| 3258 | return Builtin::BIstrlen; |
| 3259 | |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3260 | default: |
Rafael Espindola | d2fdd42 | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 3261 | if (isExternC()) { |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3262 | if (FnInfo->isStr("memset")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3263 | return Builtin::BImemset; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3264 | else if (FnInfo->isStr("memcpy")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3265 | return Builtin::BImemcpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3266 | else if (FnInfo->isStr("memmove")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3267 | return Builtin::BImemmove; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3268 | else if (FnInfo->isStr("memcmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3269 | return Builtin::BImemcmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3270 | else if (FnInfo->isStr("strncpy")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3271 | return Builtin::BIstrncpy; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3272 | else if (FnInfo->isStr("strncmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3273 | return Builtin::BIstrncmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3274 | else if (FnInfo->isStr("strncasecmp")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3275 | return Builtin::BIstrncasecmp; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3276 | else if (FnInfo->isStr("strncat")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3277 | return Builtin::BIstrncat; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3278 | else if (FnInfo->isStr("strndup")) |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3279 | return Builtin::BIstrndup; |
Anna Zaks | c36bedc | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3280 | else if (FnInfo->isStr("strlen")) |
| 3281 | return Builtin::BIstrlen; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3282 | } |
| 3283 | break; |
| 3284 | } |
Anna Zaks | 0a151a1 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3285 | return 0; |
Anna Zaks | d9b859a | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 3288 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3289 | // FieldDecl Implementation |
| 3290 | //===----------------------------------------------------------------------===// |
| 3291 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3292 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3293 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3294 | IdentifierInfo *Id, QualType T, |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3295 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3296 | InClassInitStyle InitStyle) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3297 | return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
| 3298 | BW, Mutable, InitStyle); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3299 | } |
| 3300 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3301 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3302 | return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), |
| 3303 | SourceLocation(), nullptr, QualType(), nullptr, |
| 3304 | nullptr, false, ICIS_NoInit); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3307 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 3308 | if (!isImplicit() || getDeclName()) |
| 3309 | return false; |
| 3310 | |
| 3311 | if (const RecordType *Record = getType()->getAs<RecordType>()) |
| 3312 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 3313 | |
| 3314 | return false; |
| 3315 | } |
| 3316 | |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3317 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 3318 | assert(isBitField() && "not a bitfield"); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3319 | Expr *BitWidth = static_cast<Expr *>(InitStorage.getPointer()); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3320 | return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue(); |
| 3321 | } |
| 3322 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3323 | unsigned FieldDecl::getFieldIndex() const { |
Richard Smith | 4ed0122 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 3324 | const FieldDecl *Canonical = getCanonicalDecl(); |
| 3325 | if (Canonical != this) |
| 3326 | return Canonical->getFieldIndex(); |
| 3327 | |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3328 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 3329 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3330 | unsigned Index = 0; |
Fariborz Jahanian | 07a8a21 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 3331 | const RecordDecl *RD = getParent(); |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3332 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3333 | for (auto *Field : RD->fields()) { |
| 3334 | Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; |
| 3335 | ++Index; |
| 3336 | } |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3337 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3338 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 3339 | return CachedFieldIndex - 1; |
John McCall | ba4f5d5 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3340 | } |
| 3341 | |
Abramo Bagnara | f2cf562 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 3342 | SourceRange FieldDecl::getSourceRange() const { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3343 | switch (InitStorage.getInt()) { |
| 3344 | // All three of these cases store an optional Expr*. |
| 3345 | case ISK_BitWidthOrNothing: |
| 3346 | case ISK_InClassCopyInit: |
| 3347 | case ISK_InClassListInit: |
| 3348 | if (const Expr *E = static_cast<const Expr *>(InitStorage.getPointer())) |
| 3349 | return SourceRange(getInnerLocStart(), E->getLocEnd()); |
| 3350 | // FALLTHROUGH |
| 3351 | |
| 3352 | case ISK_CapturedVLAType: |
| 3353 | return DeclaratorDecl::getSourceRange(); |
| 3354 | } |
| 3355 | llvm_unreachable("bad init storage kind"); |
Abramo Bagnara | f2cf562 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3358 | void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { |
| 3359 | assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && |
| 3360 | "capturing type in non-lambda or captured record."); |
| 3361 | assert(InitStorage.getInt() == ISK_BitWidthOrNothing && |
| 3362 | InitStorage.getPointer() == nullptr && |
| 3363 | "bit width, initializer or captured type already set"); |
| 3364 | InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType), |
| 3365 | ISK_CapturedVLAType); |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3368 | //===----------------------------------------------------------------------===// |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3369 | // TagDecl Implementation |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3370 | //===----------------------------------------------------------------------===// |
| 3371 | |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3372 | SourceLocation TagDecl::getOuterLocStart() const { |
| 3373 | return getTemplateOrInnerLocStart(this); |
| 3374 | } |
| 3375 | |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3376 | SourceRange TagDecl::getSourceRange() const { |
| 3377 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | 1693e15 | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3378 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | f602c8b | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
Rafael Espindola | bc65091 | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3381 | TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } |
Argyrios Kyrtzidis | b57a4fe | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 3382 | |
Rafael Espindola | 2d9e883 | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 3383 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3384 | NamedDeclOrQualifier = TDD; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3385 | if (const Type *T = getTypeForDecl()) { |
| 3386 | (void)T; |
| 3387 | assert(T->isLinkageValid()); |
| 3388 | } |
Rafael Espindola | 2d1b096 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 3389 | assert(isLinkageValid()); |
Douglas Gregor | 60e7064 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3392 | void TagDecl::startDefinition() { |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 3393 | IsBeingDefined = true; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3394 | |
David Blaikie | 66cff72 | 2012-11-14 01:52:05 +0000 | [diff] [blame] | 3395 | if (CXXRecordDecl *D = dyn_cast<CXXRecordDecl>(this)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3396 | struct CXXRecordDecl::DefinitionData *Data = |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3397 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3398 | for (auto I : redecls()) |
| 3399 | cast<CXXRecordDecl>(I)->DefinitionData = Data; |
John McCall | 86ff308 | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3400 | } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | void TagDecl::completeDefinition() { |
John McCall | 5cfa011 | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3404 | assert((!isa<CXXRecordDecl>(this) || |
| 3405 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 3406 | "definition completed but not started"); |
| 3407 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3408 | IsCompleteDefinition = true; |
Sebastian Redl | ed48a8f | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 3409 | IsBeingDefined = false; |
Argyrios Kyrtzidis | 565bf30 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 3410 | |
| 3411 | if (ASTMutationListener *L = getASTMutationListener()) |
| 3412 | L->CompletedTagDefinition(this); |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3415 | TagDecl *TagDecl::getDefinition() const { |
| 3416 | if (isCompleteDefinition()) |
Douglas Gregor | 8e9e9ef | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 3417 | return const_cast<TagDecl *>(this); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3418 | |
| 3419 | // If it's possible for us to have an out-of-date definition, check now. |
| 3420 | if (MayHaveOutOfDateDef) { |
| 3421 | if (IdentifierInfo *II = getIdentifier()) { |
| 3422 | if (II->isOutOfDate()) { |
| 3423 | updateOutOfDate(*II); |
| 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | |
Andrew Trick | 220a9c8 | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 3428 | if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| 3429 | return CXXRD->getDefinition(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3430 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3431 | for (auto R : redecls()) |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3432 | if (R->isCompleteDefinition()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3433 | return R; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3435 | return nullptr; |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3438 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 3439 | if (QualifierLoc) { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3440 | // Make sure the extended qualifier info is allocated. |
| 3441 | if (!hasExtInfo()) |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3442 | NamedDeclOrQualifier = new (getASTContext()) ExtInfo; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3443 | // Set qualifier info. |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3444 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 3060178 | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3445 | } else { |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3446 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3447 | if (hasExtInfo()) { |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3448 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 3449 | getASTContext().Deallocate(getExtInfo()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3450 | NamedDeclOrQualifier = (TypedefNameDecl*)nullptr; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3451 | } |
| 3452 | else |
| 3453 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | } |
| 3457 | |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3458 | void TagDecl::setTemplateParameterListsInfo(ASTContext &Context, |
| 3459 | unsigned NumTPLists, |
| 3460 | TemplateParameterList **TPLists) { |
| 3461 | assert(NumTPLists > 0); |
| 3462 | // Make sure the extended decl info is allocated. |
| 3463 | if (!hasExtInfo()) |
| 3464 | // Allocate external info struct. |
David Majnemer | aa82461 | 2013-09-17 23:57:10 +0000 | [diff] [blame] | 3465 | NamedDeclOrQualifier = new (getASTContext()) ExtInfo; |
Abramo Bagnara | 7f0a915 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3466 | // Set the template parameter lists info. |
| 3467 | getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists); |
| 3468 | } |
| 3469 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3470 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3471 | // EnumDecl Implementation |
| 3472 | //===----------------------------------------------------------------------===// |
| 3473 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3474 | void EnumDecl::anchor() { } |
| 3475 | |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3476 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 3477 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3478 | IdentifierInfo *Id, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3479 | EnumDecl *PrevDecl, bool IsScoped, |
| 3480 | bool IsScopedUsingClassTag, bool IsFixed) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3481 | EnumDecl *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3482 | IsScoped, IsScopedUsingClassTag, |
| 3483 | IsFixed); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3484 | Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3485 | C.getTypeDeclType(Enum, PrevDecl); |
| 3486 | return Enum; |
| 3487 | } |
| 3488 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3489 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3490 | EnumDecl *Enum = |
| 3491 | new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 3492 | nullptr, nullptr, false, false, false); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3493 | Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 3494 | return Enum; |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3497 | SourceRange EnumDecl::getIntegerTypeRange() const { |
| 3498 | if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) |
| 3499 | return TI->getTypeLoc().getSourceRange(); |
| 3500 | return SourceRange(); |
| 3501 | } |
| 3502 | |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3503 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 3504 | QualType NewPromotionType, |
| 3505 | unsigned NumPositiveBits, |
| 3506 | unsigned NumNegativeBits) { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3507 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3508 | if (!IntegerType) |
| 3509 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3510 | PromotionType = NewPromotionType; |
John McCall | 1b5a618 | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 3511 | setNumPositiveBits(NumPositiveBits); |
| 3512 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3513 | TagDecl::completeDefinition(); |
| 3514 | } |
| 3515 | |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3516 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 3517 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 3518 | return MSI->getTemplateSpecializationKind(); |
| 3519 | |
| 3520 | return TSK_Undeclared; |
| 3521 | } |
| 3522 | |
| 3523 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 3524 | SourceLocation PointOfInstantiation) { |
| 3525 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 3526 | assert(MSI && "Not an instantiated member enumeration?"); |
| 3527 | MSI->setTemplateSpecializationKind(TSK); |
| 3528 | if (TSK != TSK_ExplicitSpecialization && |
| 3529 | PointOfInstantiation.isValid() && |
| 3530 | MSI->getPointOfInstantiation().isInvalid()) |
| 3531 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 3532 | } |
| 3533 | |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3534 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 3535 | if (SpecializationInfo) |
| 3536 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 3537 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3538 | return nullptr; |
Richard Smith | f1c66b4 | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 3542 | TemplateSpecializationKind TSK) { |
| 3543 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 3544 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 3545 | } |
| 3546 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3547 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 3548 | // RecordDecl Implementation |
| 3549 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3550 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3551 | RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, |
| 3552 | DeclContext *DC, SourceLocation StartLoc, |
| 3553 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 3554 | RecordDecl *PrevDecl) |
| 3555 | : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 3556 | HasFlexibleArrayMember = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3557 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 082b02e | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3558 | HasObjectMember = false; |
Fariborz Jahanian | 3ac83d6 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 3559 | HasVolatileMember = false; |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3560 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 3561 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3564 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Abramo Bagnara | ba877ad | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3565 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3566 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3567 | RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, |
| 3568 | StartLoc, IdLoc, Id, PrevDecl); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3569 | R->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 3570 | |
Ted Kremenek | 4b7c983 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3571 | C.getTypeDeclType(R, PrevDecl); |
| 3572 | return R; |
Ted Kremenek | 6359792 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3575 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3576 | RecordDecl *R = |
| 3577 | new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(), |
| 3578 | SourceLocation(), nullptr, nullptr); |
Douglas Gregor | 6bd9929 | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3579 | R->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 3580 | return R; |
Argyrios Kyrtzidis | b8b03e6 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 3583 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3584 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | c9b5b40 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 3585 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 3586 | } |
| 3587 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3588 | bool RecordDecl::isLambda() const { |
| 3589 | if (auto RD = dyn_cast<CXXRecordDecl>(this)) |
| 3590 | return RD->isLambda(); |
| 3591 | return false; |
| 3592 | } |
| 3593 | |
| 3594 | bool RecordDecl::isCapturedRecord() const { |
| 3595 | return hasAttr<CapturedRecordAttr>(); |
| 3596 | } |
| 3597 | |
| 3598 | void RecordDecl::setCapturedRecord() { |
| 3599 | addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); |
| 3600 | } |
| 3601 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3602 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 3603 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 3604 | LoadFieldsFromExternalStorage(); |
| 3605 | |
| 3606 | return field_iterator(decl_iterator(FirstDecl)); |
| 3607 | } |
| 3608 | |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3609 | /// completeDefinition - Notes that the definition of this type is now |
| 3610 | /// complete. |
| 3611 | void RecordDecl::completeDefinition() { |
John McCall | 5e1cdac | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3612 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
Douglas Gregor | da2142f | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3613 | TagDecl::completeDefinition(); |
| 3614 | } |
| 3615 | |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 3616 | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
| 3617 | /// This which can be turned on with an attribute, pragma, or the |
| 3618 | /// -mms-bitfields command-line option. |
| 3619 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3620 | return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; |
Eli Friedman | 5f608ae | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 3623 | static bool isFieldOrIndirectField(Decl::Kind K) { |
| 3624 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
| 3625 | } |
| 3626 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3627 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 3628 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 3629 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 3630 | |
| 3631 | // Notify that we have a RecordDecl doing some initialization. |
| 3632 | ExternalASTSource::Deserializing TheFields(Source); |
| 3633 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3634 | SmallVector<Decl*, 64> Decls; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 3635 | LoadedFieldsFromExternalStorage = true; |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 3636 | switch (Source->FindExternalLexicalDecls(this, isFieldOrIndirectField, |
| 3637 | Decls)) { |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 3638 | case ELR_Success: |
| 3639 | break; |
| 3640 | |
| 3641 | case ELR_AlreadyLoaded: |
| 3642 | case ELR_Failure: |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3643 | return; |
Douglas Gregor | ba6ffaf | 2011-07-15 21:46:17 +0000 | [diff] [blame] | 3644 | } |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3645 | |
| 3646 | #ifndef NDEBUG |
| 3647 | // Check that all decls we got were FieldDecls. |
| 3648 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
Argyrios Kyrtzidis | 22cd9ac | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 3649 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3650 | #endif |
| 3651 | |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3652 | if (Decls.empty()) |
| 3653 | return; |
| 3654 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3655 | std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
Argyrios Kyrtzidis | ec2ec1f | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 3656 | /*FieldsAlreadyLoaded=*/false); |
Argyrios Kyrtzidis | eb5e998 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3657 | } |
| 3658 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3659 | bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { |
| 3660 | ASTContext &Context = getASTContext(); |
| 3661 | if (!Context.getLangOpts().Sanitize.has(SanitizerKind::Address) || |
| 3662 | !Context.getLangOpts().SanitizeAddressFieldPadding) |
| 3663 | return false; |
| 3664 | const auto &Blacklist = Context.getSanitizerBlacklist(); |
| 3665 | const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(this); |
| 3666 | // We may be able to relax some of these requirements. |
| 3667 | int ReasonToReject = -1; |
| 3668 | if (!CXXRD || CXXRD->isExternCContext()) |
| 3669 | ReasonToReject = 0; // is not C++. |
| 3670 | else if (CXXRD->hasAttr<PackedAttr>()) |
| 3671 | ReasonToReject = 1; // is packed. |
| 3672 | else if (CXXRD->isUnion()) |
| 3673 | ReasonToReject = 2; // is a union. |
| 3674 | else if (CXXRD->isTriviallyCopyable()) |
| 3675 | ReasonToReject = 3; // is trivially copyable. |
| 3676 | else if (CXXRD->hasTrivialDestructor()) |
| 3677 | ReasonToReject = 4; // has trivial destructor. |
| 3678 | else if (CXXRD->isStandardLayout()) |
| 3679 | ReasonToReject = 5; // is standard layout. |
| 3680 | else if (Blacklist.isBlacklistedLocation(getLocation(), "field-padding")) |
| 3681 | ReasonToReject = 6; // is in a blacklisted file. |
| 3682 | else if (Blacklist.isBlacklistedType(getQualifiedNameAsString(), |
| 3683 | "field-padding")) |
| 3684 | ReasonToReject = 7; // is blacklisted. |
| 3685 | |
| 3686 | if (EmitRemark) { |
| 3687 | if (ReasonToReject >= 0) |
| 3688 | Context.getDiagnostics().Report( |
| 3689 | getLocation(), |
| 3690 | diag::remark_sanitize_address_insert_extra_padding_rejected) |
| 3691 | << getQualifiedNameAsString() << ReasonToReject; |
| 3692 | else |
| 3693 | Context.getDiagnostics().Report( |
| 3694 | getLocation(), |
| 3695 | diag::remark_sanitize_address_insert_extra_padding_accepted) |
| 3696 | << getQualifiedNameAsString(); |
| 3697 | } |
| 3698 | return ReasonToReject < 0; |
| 3699 | } |
| 3700 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3701 | const FieldDecl *RecordDecl::findFirstNamedDataMember() const { |
| 3702 | for (const auto *I : fields()) { |
| 3703 | if (I->getIdentifier()) |
| 3704 | return I; |
| 3705 | |
| 3706 | if (const RecordType *RT = I->getType()->getAs<RecordType>()) |
| 3707 | if (const FieldDecl *NamedDataMember = |
| 3708 | RT->getDecl()->findFirstNamedDataMember()) |
| 3709 | return NamedDataMember; |
| 3710 | } |
| 3711 | |
| 3712 | // We didn't find a named data member. |
| 3713 | return nullptr; |
| 3714 | } |
| 3715 | |
| 3716 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 3717 | //===----------------------------------------------------------------------===// |
| 3718 | // BlockDecl Implementation |
| 3719 | //===----------------------------------------------------------------------===// |
| 3720 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3721 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3722 | assert(!ParamInfo && "Already has param info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3723 | |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3724 | // Zero params -> null pointer. |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3725 | if (!NewParamInfo.empty()) { |
| 3726 | NumParams = NewParamInfo.size(); |
| 3727 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 3728 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3729 | } |
| 3730 | } |
| 3731 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3732 | void BlockDecl::setCaptures(ASTContext &Context, |
| 3733 | const Capture *begin, |
| 3734 | const Capture *end, |
| 3735 | bool capturesCXXThis) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3736 | CapturesCXXThis = capturesCXXThis; |
| 3737 | |
| 3738 | if (begin == end) { |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3739 | NumCaptures = 0; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3740 | Captures = nullptr; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3741 | return; |
| 3742 | } |
| 3743 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 3744 | NumCaptures = end - begin; |
| 3745 | |
| 3746 | // Avoid new Capture[] because we don't want to provide a default |
| 3747 | // constructor. |
| 3748 | size_t allocationSize = NumCaptures * sizeof(Capture); |
| 3749 | void *buffer = Context.Allocate(allocationSize, /*alignment*/sizeof(void*)); |
| 3750 | memcpy(buffer, begin, allocationSize); |
| 3751 | Captures = static_cast<Capture*>(buffer); |
Steve Naroff | e78b809 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3752 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3753 | |
John McCall | 204e133 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 3754 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3755 | for (const auto &I : captures()) |
John McCall | 204e133 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 3756 | // Only auto vars can be captured, so no redeclaration worries. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3757 | if (I.getVariable() == variable) |
John McCall | 204e133 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 3758 | return true; |
| 3759 | |
| 3760 | return false; |
| 3761 | } |
| 3762 | |
Douglas Gregor | 2fcbcef | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 3763 | SourceRange BlockDecl::getSourceRange() const { |
| 3764 | return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); |
| 3765 | } |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3766 | |
| 3767 | //===----------------------------------------------------------------------===// |
| 3768 | // Other Decl Allocation/Deallocation Method Implementations |
| 3769 | //===----------------------------------------------------------------------===// |
| 3770 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3771 | void TranslationUnitDecl::anchor() { } |
| 3772 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3773 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3774 | return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 3777 | void ExternCContextDecl::anchor() { } |
| 3778 | |
| 3779 | ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, |
| 3780 | TranslationUnitDecl *DC) { |
| 3781 | return new (C, DC) ExternCContextDecl(DC); |
| 3782 | } |
| 3783 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3784 | void LabelDecl::anchor() { } |
| 3785 | |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3786 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 6784304 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3787 | SourceLocation IdentL, IdentifierInfo *II) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3788 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); |
Abramo Bagnara | 6784304 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 3792 | SourceLocation IdentL, IdentifierInfo *II, |
| 3793 | SourceLocation GnuLabelL) { |
| 3794 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3795 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3798 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3799 | return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, |
| 3800 | SourceLocation()); |
Douglas Gregor | 06c9193 | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 3801 | } |
| 3802 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3803 | void LabelDecl::setMSAsmLabel(StringRef Name) { |
| 3804 | char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; |
| 3805 | memcpy(Buffer, Name.data(), Name.size()); |
| 3806 | Buffer[Name.size()] = '\0'; |
| 3807 | MSAsmName = Buffer; |
| 3808 | } |
| 3809 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3810 | void ValueDecl::anchor() { } |
| 3811 | |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 3812 | bool ValueDecl::isWeak() const { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3813 | for (const auto *I : attrs()) |
| 3814 | if (isa<WeakAttr>(I) || isa<WeakRefAttr>(I)) |
Benjamin Kramer | 2fa67ef | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 3815 | return true; |
| 3816 | |
| 3817 | return isWeakImported(); |
| 3818 | } |
| 3819 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3820 | void ImplicitParamDecl::anchor() { } |
| 3821 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3822 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3823 | SourceLocation IdLoc, |
| 3824 | IdentifierInfo *Id, |
| 3825 | QualType Type) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3826 | return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3829 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3830 | unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3831 | return new (C, ID) ImplicitParamDecl(C, nullptr, SourceLocation(), nullptr, |
| 3832 | QualType()); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3835 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3836 | SourceLocation StartLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3837 | const DeclarationNameInfo &NameInfo, |
| 3838 | QualType T, TypeSourceInfo *TInfo, |
Rafael Espindola | d2615cc | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3839 | StorageClass SC, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3840 | bool isInlineSpecified, |
Richard Smith | af1fc7a | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3841 | bool hasWrittenPrototype, |
| 3842 | bool isConstexprSpecified) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3843 | FunctionDecl *New = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3844 | new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo, |
| 3845 | SC, isInlineSpecified, isConstexprSpecified); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3846 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 3847 | return New; |
| 3848 | } |
| 3849 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3850 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3851 | return new (C, ID) FunctionDecl(Function, C, nullptr, SourceLocation(), |
| 3852 | DeclarationNameInfo(), QualType(), nullptr, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3853 | SC_None, false, false); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3856 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3857 | return new (C, DC) BlockDecl(DC, L); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3860 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3861 | return new (C, ID) BlockDecl(nullptr, SourceLocation()); |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
Ben Langmuir | 8c045ac | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 3864 | CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, |
| 3865 | unsigned NumParams) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3866 | return new (C, DC, NumParams * sizeof(ImplicitParamDecl *)) |
| 3867 | CapturedDecl(DC, NumParams); |
Tareq A. Siraj | 6afcf88 | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
Ben Langmuir | dc5be4f | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 3870 | CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3871 | unsigned NumParams) { |
| 3872 | return new (C, ID, NumParams * sizeof(ImplicitParamDecl *)) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3873 | CapturedDecl(nullptr, NumParams); |
Ben Langmuir | dc5be4f | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3876 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 3877 | SourceLocation L, |
| 3878 | IdentifierInfo *Id, QualType T, |
| 3879 | Expr *E, const llvm::APSInt &V) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3880 | return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3883 | EnumConstantDecl * |
| 3884 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3885 | return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr, |
| 3886 | QualType(), nullptr, llvm::APSInt()); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3889 | void IndirectFieldDecl::anchor() { } |
| 3890 | |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 3891 | IndirectFieldDecl * |
| 3892 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 3893 | IdentifierInfo *Id, QualType T, NamedDecl **CH, |
| 3894 | unsigned CHS) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3895 | return new (C, DC) IndirectFieldDecl(DC, L, Id, T, CH, CHS); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3896 | } |
| 3897 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3898 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 3899 | unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3900 | return new (C, ID) IndirectFieldDecl(nullptr, SourceLocation(), |
| 3901 | DeclarationName(), QualType(), nullptr, |
| 3902 | 0); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Douglas Gregor | 8e7139c | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 3905 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 3906 | SourceLocation End = getLocation(); |
| 3907 | if (Init) |
| 3908 | End = Init->getLocEnd(); |
| 3909 | return SourceRange(getLocation(), End); |
| 3910 | } |
| 3911 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3912 | void TypeDecl::anchor() { } |
| 3913 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3914 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 344577e | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3915 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3916 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3917 | return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3920 | void TypedefNameDecl::anchor() { } |
| 3921 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 3922 | TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName() const { |
| 3923 | if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) |
| 3924 | if (TT->getDecl()->getTypedefNameForAnonDecl() == this) |
| 3925 | return TT->getDecl(); |
| 3926 | |
| 3927 | return nullptr; |
| 3928 | } |
| 3929 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3930 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3931 | return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 3932 | nullptr, nullptr); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3935 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 3936 | SourceLocation StartLoc, |
| 3937 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 3938 | TypeSourceInfo *TInfo) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3939 | return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3942 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3943 | return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), |
| 3944 | SourceLocation(), nullptr, nullptr); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Abramo Bagnara | a2026c9 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3947 | SourceRange TypedefDecl::getSourceRange() const { |
| 3948 | SourceLocation RangeEnd = getLocation(); |
| 3949 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 3950 | if (typeIsPostfix(TInfo->getType())) |
| 3951 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 3952 | } |
| 3953 | return SourceRange(getLocStart(), RangeEnd); |
| 3954 | } |
| 3955 | |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3956 | SourceRange TypeAliasDecl::getSourceRange() const { |
| 3957 | SourceLocation RangeEnd = getLocStart(); |
| 3958 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 3959 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 3960 | return SourceRange(getLocStart(), RangeEnd); |
| 3961 | } |
| 3962 | |
David Blaikie | 99ba9e3 | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3963 | void FileScopeAsmDecl::anchor() { } |
| 3964 | |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3965 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 21e006e | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 3966 | StringLiteral *Str, |
| 3967 | SourceLocation AsmLoc, |
| 3968 | SourceLocation RParenLoc) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3969 | return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
Sebastian Redl | 7783bfc | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3970 | } |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3971 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3972 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3973 | unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3974 | return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), |
| 3975 | SourceLocation()); |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 3978 | void EmptyDecl::anchor() {} |
| 3979 | |
| 3980 | EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3981 | return new (C, DC) EmptyDecl(DC, L); |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3985 | return new (C, ID) EmptyDecl(nullptr, SourceLocation()); |
Michael Han | 684aa73 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 3986 | } |
| 3987 | |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3988 | //===----------------------------------------------------------------------===// |
| 3989 | // ImportDecl Implementation |
| 3990 | //===----------------------------------------------------------------------===// |
| 3991 | |
| 3992 | /// \brief Retrieve the number of module identifiers needed to name the given |
| 3993 | /// module. |
| 3994 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 3995 | unsigned Result = 1; |
| 3996 | while (Mod->Parent) { |
| 3997 | Mod = Mod->Parent; |
| 3998 | ++Result; |
| 3999 | } |
| 4000 | return Result; |
| 4001 | } |
| 4002 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4003 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4004 | Module *Imported, |
| 4005 | ArrayRef<SourceLocation> IdentifierLocs) |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4006 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 4007 | NextLocalImport() |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4008 | { |
| 4009 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
| 4010 | SourceLocation *StoredLocs = reinterpret_cast<SourceLocation *>(this + 1); |
| 4011 | memcpy(StoredLocs, IdentifierLocs.data(), |
| 4012 | IdentifierLocs.size() * sizeof(SourceLocation)); |
| 4013 | } |
| 4014 | |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4015 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4016 | Module *Imported, SourceLocation EndLoc) |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4017 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false), |
Douglas Gregor | e664977 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 4018 | NextLocalImport() |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4019 | { |
| 4020 | *reinterpret_cast<SourceLocation *>(this + 1) = EndLoc; |
| 4021 | } |
| 4022 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4023 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4024 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4025 | ArrayRef<SourceLocation> IdentifierLocs) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4026 | return new (C, DC, IdentifierLocs.size() * sizeof(SourceLocation)) |
| 4027 | ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4030 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4031 | SourceLocation StartLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4032 | Module *Imported, |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4033 | SourceLocation EndLoc) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4034 | ImportDecl *Import = |
| 4035 | new (C, DC, sizeof(SourceLocation)) ImportDecl(DC, StartLoc, |
| 4036 | Imported, EndLoc); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4037 | Import->setImplicit(); |
| 4038 | return Import; |
| 4039 | } |
| 4040 | |
Douglas Gregor | 1e68ecc | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4041 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 4042 | unsigned NumLocations) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4043 | return new (C, ID, NumLocations * sizeof(SourceLocation)) |
| 4044 | ImportDecl(EmptyShell()); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 4048 | if (!ImportedAndComplete.getInt()) |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4049 | return None; |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4050 | |
| 4051 | const SourceLocation *StoredLocs |
| 4052 | = reinterpret_cast<const SourceLocation *>(this + 1); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4053 | return llvm::makeArrayRef(StoredLocs, |
| 4054 | getNumModuleIdentifiers(getImportedModule())); |
Douglas Gregor | 15de72c | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4055 | } |
| 4056 | |
| 4057 | SourceRange ImportDecl::getSourceRange() const { |
| 4058 | if (!ImportedAndComplete.getInt()) |
| 4059 | return SourceRange(getLocation(), |
| 4060 | *reinterpret_cast<const SourceLocation *>(this + 1)); |
| 4061 | |
| 4062 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 4063 | } |