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