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; |
Reid Kleckner | 6010338 | 2015-12-16 02:04:40 +0000 | [diff] [blame^] | 1437 | if (ND->isAnonymousNamespace()) { |
| 1438 | OS << (P.MSVCFormatting ? "`anonymous namespace\'" |
| 1439 | : "(anonymous namespace)"); |
| 1440 | } |
Sam Weinig | 07d211e | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 1441 | else |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1442 | OS << *ND; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1443 | } else if (const auto *RD = dyn_cast<RecordDecl>(*I)) { |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1444 | if (!RD->getIdentifier()) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 1445 | OS << "(anonymous " << RD->getKindName() << ')'; |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1446 | else |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1447 | OS << *RD; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1448 | } else if (const auto *FD = dyn_cast<FunctionDecl>(*I)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1449 | const FunctionProtoType *FT = nullptr; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1450 | if (FD->hasWrittenPrototype()) |
Eli Friedman | 5c27c4c | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 1451 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1452 | |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1453 | OS << *FD << '('; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1454 | if (FT) { |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1455 | unsigned NumParams = FD->getNumParams(); |
| 1456 | for (unsigned i = 0; i < NumParams; ++i) { |
| 1457 | if (i) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1458 | OS << ", "; |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 1459 | OS << FD->getParamDecl(i)->getType().stream(P); |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | if (FT->isVariadic()) { |
| 1463 | if (NumParams > 0) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1464 | OS << ", "; |
| 1465 | OS << "..."; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1468 | OS << ')'; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1469 | } else if (const auto *ED = dyn_cast<EnumDecl>(*I)) { |
Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 1470 | // C++ [dcl.enum]p10: Each enum-name and each unscoped |
| 1471 | // enumerator is declared in the scope that immediately contains |
| 1472 | // the enum-specifier. Each scoped enumerator is declared in the |
| 1473 | // scope of the enumeration. |
| 1474 | if (ED->isScoped() || ED->getIdentifier()) |
| 1475 | OS << *ED; |
| 1476 | else |
| 1477 | continue; |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1478 | } else { |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1479 | OS << *cast<NamedDecl>(*I); |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1480 | } |
| 1481 | OS << "::"; |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 1484 | if (getDeclName()) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1485 | OS << *this; |
John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 1486 | else |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 1487 | OS << "(anonymous)"; |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1488 | } |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1489 | |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1490 | void NamedDecl::getNameForDiagnostic(raw_ostream &OS, |
| 1491 | const PrintingPolicy &Policy, |
| 1492 | bool Qualified) const { |
| 1493 | if (Qualified) |
| 1494 | printQualifiedName(OS, Policy); |
| 1495 | else |
| 1496 | printName(OS); |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1499 | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { |
| 1500 | return true; |
| 1501 | } |
| 1502 | static bool isRedeclarableImpl(...) { return false; } |
| 1503 | static bool isRedeclarable(Decl::Kind K) { |
| 1504 | switch (K) { |
| 1505 | #define DECL(Type, Base) \ |
| 1506 | case Decl::Type: \ |
| 1507 | return isRedeclarableImpl((Type##Decl *)nullptr); |
| 1508 | #define ABSTRACT_DECL(DECL) |
| 1509 | #include "clang/AST/DeclNodes.inc" |
| 1510 | } |
| 1511 | llvm_unreachable("unknown decl kind"); |
| 1512 | } |
| 1513 | |
| 1514 | bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const { |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1515 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 1516 | |
Richard Smith | fe620d2 | 2015-03-05 23:24:12 +0000 | [diff] [blame] | 1517 | // Never replace one imported declaration with another; we need both results |
| 1518 | // when re-exporting. |
| 1519 | if (OldD->isFromASTFile() && isFromASTFile()) |
| 1520 | return false; |
| 1521 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1522 | // A kind mismatch implies that the declaration is not replaced. |
| 1523 | if (OldD->getKind() != getKind()) |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1524 | return false; |
| 1525 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1526 | // For method declarations, we never replace. (Why?) |
| 1527 | if (isa<ObjCMethodDecl>(this)) |
| 1528 | return false; |
| 1529 | |
| 1530 | // For parameters, pick the newer one. This is either an error or (in |
| 1531 | // Objective-C) permitted as an extension. |
| 1532 | if (isa<ParmVarDecl>(this)) |
| 1533 | return true; |
| 1534 | |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1535 | // Inline namespaces can give us two declarations with the same |
| 1536 | // name and kind in the same scope but different contexts; we should |
| 1537 | // keep both declarations in this case. |
| 1538 | if (!this->getDeclContext()->getRedeclContext()->Equals( |
| 1539 | OldD->getDeclContext()->getRedeclContext())) |
| 1540 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1542 | // Using declarations can be replaced if they import the same name from the |
| 1543 | // same context. |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1544 | if (auto *UD = dyn_cast<UsingDecl>(this)) { |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1545 | ASTContext &Context = getASTContext(); |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1546 | return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1547 | Context.getCanonicalNestedNameSpecifier( |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1548 | cast<UsingDecl>(OldD)->getQualifier()); |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1549 | } |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1550 | if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { |
Eli Friedman | 0eaf10b | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1551 | ASTContext &Context = getASTContext(); |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1552 | return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == |
Eli Friedman | 0eaf10b | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1553 | Context.getCanonicalNestedNameSpecifier( |
| 1554 | cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); |
| 1555 | } |
| 1556 | |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1557 | // UsingDirectiveDecl's are not really NamedDecl's, and all have same name. |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1558 | // They can be replaced if they nominate the same namespace. |
| 1559 | // FIXME: Is this true even if they have different module visibility? |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1560 | if (auto *UD = dyn_cast<UsingDirectiveDecl>(this)) |
| 1561 | return UD->getNominatedNamespace()->getOriginalNamespace() == |
| 1562 | cast<UsingDirectiveDecl>(OldD)->getNominatedNamespace() |
| 1563 | ->getOriginalNamespace(); |
Richard Smith | baf3ca5 | 2014-03-17 21:46:03 +0000 | [diff] [blame] | 1564 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1565 | if (isRedeclarable(getKind())) { |
| 1566 | if (getCanonicalDecl() != OldD->getCanonicalDecl()) |
| 1567 | return false; |
| 1568 | |
| 1569 | if (IsKnownNewer) |
| 1570 | return true; |
| 1571 | |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1572 | // Check whether this is actually newer than OldD. We want to keep the |
| 1573 | // newer declaration. This loop will usually only iterate once, because |
| 1574 | // OldD is usually the previous declaration. |
| 1575 | for (auto D : redecls()) { |
| 1576 | if (D == OldD) |
| 1577 | break; |
| 1578 | |
| 1579 | // If we reach the canonical declaration, then OldD is not actually older |
| 1580 | // than this one. |
| 1581 | // |
| 1582 | // FIXME: In this case, we should not add this decl to the lookup table. |
| 1583 | if (D->isCanonicalDecl()) |
| 1584 | return false; |
| 1585 | } |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1586 | |
| 1587 | // It's a newer declaration of the same kind of declaration in the same |
| 1588 | // scope: we want this decl instead of the existing one. |
| 1589 | return true; |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1592 | // In all other cases, we need to keep both declarations in case they have |
| 1593 | // different visibility. Any attempt to use the name will result in an |
| 1594 | // ambiguity if more than one is visible. |
| 1595 | return false; |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1598 | bool NamedDecl::hasLinkage() const { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1599 | return getFormalLinkage() != NoLinkage; |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1600 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1601 | |
Daniel Dunbar | 166ea9ad | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 1602 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1603 | NamedDecl *ND = this; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1604 | while (auto *UD = dyn_cast<UsingShadowDecl>(ND)) |
Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1605 | ND = UD->getTargetDecl(); |
| 1606 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1607 | if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1608 | return AD->getClassInterface(); |
| 1609 | |
| 1610 | return ND; |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1613 | bool NamedDecl::isCXXInstanceMember() const { |
Douglas Gregor | 3f28ec2 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 1614 | if (!isCXXClassMember()) |
| 1615 | return false; |
| 1616 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1617 | const NamedDecl *D = this; |
| 1618 | if (isa<UsingShadowDecl>(D)) |
| 1619 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1620 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1621 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1622 | return true; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1623 | if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction())) |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1624 | return MD->isInstance(); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1625 | return false; |
| 1626 | } |
| 1627 | |
Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1628 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1629 | // DeclaratorDecl Implementation |
| 1630 | //===----------------------------------------------------------------------===// |
| 1631 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1632 | template <typename DeclT> |
| 1633 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1634 | if (decl->getNumTemplateParameterLists() > 0) |
| 1635 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1636 | else |
| 1637 | return decl->getInnerLocStart(); |
| 1638 | } |
| 1639 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1640 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1641 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1642 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1643 | return SourceLocation(); |
| 1644 | } |
| 1645 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1646 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1647 | if (QualifierLoc) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1648 | // Make sure the extended decl info is allocated. |
| 1649 | if (!hasExtInfo()) { |
| 1650 | // Save (non-extended) type source info pointer. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1651 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1652 | // Allocate external info struct. |
| 1653 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1654 | // Restore savedTInfo into (extended) decl info. |
| 1655 | getExtInfo()->TInfo = savedTInfo; |
| 1656 | } |
| 1657 | // Set qualifier info. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1658 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1659 | } else { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1660 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1661 | if (hasExtInfo()) { |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1662 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 1663 | // Save type source info pointer. |
| 1664 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 1665 | // Deallocate the extended decl info. |
| 1666 | getASTContext().Deallocate(getExtInfo()); |
| 1667 | // Restore savedTInfo into (non-extended) decl info. |
| 1668 | DeclInfo = savedTInfo; |
| 1669 | } |
| 1670 | else |
| 1671 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1676 | void DeclaratorDecl::setTemplateParameterListsInfo( |
| 1677 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 1678 | assert(!TPLists.empty()); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1679 | // Make sure the extended decl info is allocated. |
| 1680 | if (!hasExtInfo()) { |
| 1681 | // Save (non-extended) type source info pointer. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1682 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1683 | // Allocate external info struct. |
| 1684 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1685 | // Restore savedTInfo into (extended) decl info. |
| 1686 | getExtInfo()->TInfo = savedTInfo; |
| 1687 | } |
| 1688 | // Set the template parameter lists info. |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1689 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1692 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1693 | return getTemplateOrInnerLocStart(this); |
| 1694 | } |
| 1695 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1696 | namespace { |
| 1697 | |
| 1698 | // Helper function: returns true if QT is or contains a type |
| 1699 | // having a postfix component. |
| 1700 | bool typeIsPostfix(clang::QualType QT) { |
| 1701 | while (true) { |
| 1702 | const Type* T = QT.getTypePtr(); |
| 1703 | switch (T->getTypeClass()) { |
| 1704 | default: |
| 1705 | return false; |
| 1706 | case Type::Pointer: |
| 1707 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1708 | break; |
| 1709 | case Type::BlockPointer: |
| 1710 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1711 | break; |
| 1712 | case Type::MemberPointer: |
| 1713 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1714 | break; |
| 1715 | case Type::LValueReference: |
| 1716 | case Type::RValueReference: |
| 1717 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1718 | break; |
| 1719 | case Type::PackExpansion: |
| 1720 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1721 | break; |
| 1722 | case Type::Paren: |
| 1723 | case Type::ConstantArray: |
| 1724 | case Type::DependentSizedArray: |
| 1725 | case Type::IncompleteArray: |
| 1726 | case Type::VariableArray: |
| 1727 | case Type::FunctionProto: |
| 1728 | case Type::FunctionNoProto: |
| 1729 | return true; |
| 1730 | } |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | } // namespace |
| 1735 | |
| 1736 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1737 | SourceLocation RangeEnd = getLocation(); |
| 1738 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
Benjamin Kramer | 3a7cc81 | 2014-02-02 15:28:46 +0000 | [diff] [blame] | 1739 | // If the declaration has no name or the type extends past the name take the |
| 1740 | // end location of the type. |
| 1741 | if (!getDeclName() || typeIsPostfix(TInfo->getType())) |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1742 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1743 | } |
| 1744 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1745 | } |
| 1746 | |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1747 | void QualifierInfo::setTemplateParameterListsInfo( |
| 1748 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1749 | // Free previous template parameters (if any). |
| 1750 | if (NumTemplParamLists > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1751 | Context.Deallocate(TemplParamLists); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1752 | TemplParamLists = nullptr; |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1753 | NumTemplParamLists = 0; |
| 1754 | } |
| 1755 | // Set info on matched template parameter lists (if any). |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1756 | if (!TPLists.empty()) { |
| 1757 | TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; |
| 1758 | NumTemplParamLists = TPLists.size(); |
| 1759 | std::copy(TPLists.begin(), TPLists.end(), TemplParamLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1760 | } |
| 1761 | } |
| 1762 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1763 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1764 | // VarDecl Implementation |
| 1765 | //===----------------------------------------------------------------------===// |
| 1766 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1767 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1768 | switch (SC) { |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1769 | case SC_None: break; |
Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1770 | case SC_Auto: return "auto"; |
| 1771 | case SC_Extern: return "extern"; |
Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1772 | case SC_PrivateExtern: return "__private_extern__"; |
| 1773 | case SC_Register: return "register"; |
| 1774 | case SC_Static: return "static"; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1777 | llvm_unreachable("Invalid storage class"); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1780 | VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 1781 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 1782 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| 1783 | StorageClass SC) |
| 1784 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
| 1785 | redeclarable_base(C), Init() { |
Benjamin Kramer | a939c23 | 2014-03-15 18:54:13 +0000 | [diff] [blame] | 1786 | static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), |
| 1787 | "VarDeclBitfields too large!"); |
| 1788 | static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), |
| 1789 | "ParmVarDeclBitfields too large!"); |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1790 | static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), |
| 1791 | "NonParmVarDeclBitfields too large!"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1792 | AllBits = 0; |
| 1793 | VarDeclBits.SClass = SC; |
| 1794 | // Everything else is implicitly initialized to false. |
| 1795 | } |
| 1796 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1797 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1798 | SourceLocation StartL, SourceLocation IdL, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1799 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1800 | StorageClass S) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1801 | 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] | 1802 | } |
| 1803 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1804 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1805 | return new (C, ID) |
| 1806 | VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
| 1807 | QualType(), nullptr, SC_None); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1810 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1811 | assert(isLegalForVariable(SC)); |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1812 | VarDeclBits.SClass = SC; |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1815 | VarDecl::TLSKind VarDecl::getTLSKind() const { |
| 1816 | switch (VarDeclBits.TSCSpec) { |
| 1817 | case TSCS_unspecified: |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1818 | if (!hasAttr<ThreadAttr>() && |
| 1819 | !(getASTContext().getLangOpts().OpenMPUseTLS && |
| 1820 | getASTContext().getTargetInfo().isTLSSupported() && |
| 1821 | hasAttr<OMPThreadPrivateDeclAttr>())) |
David Majnemer | 1b5baff | 2015-05-14 05:19:23 +0000 | [diff] [blame] | 1822 | return TLS_None; |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1823 | return ((getASTContext().getLangOpts().isCompatibleWithMSVC( |
| 1824 | LangOptions::MSVC2015)) || |
| 1825 | hasAttr<OMPThreadPrivateDeclAttr>()) |
David Majnemer | 1b5baff | 2015-05-14 05:19:23 +0000 | [diff] [blame] | 1826 | ? TLS_Dynamic |
| 1827 | : TLS_Static; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1828 | case TSCS___thread: // Fall through. |
| 1829 | case TSCS__Thread_local: |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1830 | return TLS_Static; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1831 | case TSCS_thread_local: |
| 1832 | return TLS_Dynamic; |
| 1833 | } |
| 1834 | llvm_unreachable("Unknown thread storage class specifier!"); |
| 1835 | } |
| 1836 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1837 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | e0d6497 | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1838 | if (const Expr *Init = getInit()) { |
| 1839 | SourceLocation InitEnd = Init->getLocEnd(); |
Nico Weber | bbe1394 | 2013-01-22 17:00:09 +0000 | [diff] [blame] | 1840 | // If Init is implicit, ignore its source range and fallback on |
| 1841 | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
| 1842 | if (InitEnd.isValid() && InitEnd != getLocation()) |
Argyrios Kyrtzidis | e0d6497 | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1843 | return SourceRange(getOuterLocStart(), InitEnd); |
| 1844 | } |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1845 | return DeclaratorDecl::getSourceRange(); |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Rafael Espindola | 8851067 | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 1848 | template<typename T> |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 1849 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1850 | // C++ [dcl.link]p1: All function types, function names with external linkage, |
| 1851 | // and variable names with external linkage have a language linkage. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1852 | if (!D.hasExternalFormalLinkage()) |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1853 | return NoLanguageLinkage; |
| 1854 | |
| 1855 | // 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] | 1856 | // C language linkage fits the implementation nicely. |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1857 | ASTContext &Context = D.getASTContext(); |
| 1858 | if (!Context.getLangOpts().CPlusPlus) |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1859 | return CLanguageLinkage; |
| 1860 | |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1861 | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
| 1862 | // language linkage of the names of class members and the function type of |
| 1863 | // class member functions. |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1864 | const DeclContext *DC = D.getDeclContext(); |
| 1865 | if (DC->isRecord()) |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1866 | return CXXLanguageLinkage; |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1867 | |
| 1868 | // If the first decl is in an extern "C" context, any other redeclaration |
| 1869 | // will have C language linkage. If the first one is not in an extern "C" |
| 1870 | // 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] | 1871 | if (isFirstInExternCContext(&D)) |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1872 | return CLanguageLinkage; |
| 1873 | return CXXLanguageLinkage; |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1876 | template<typename T> |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 1877 | static bool isDeclExternC(const T &D) { |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1878 | // Since the context is ignored for class members, they can only have C++ |
| 1879 | // language linkage or no language linkage. |
| 1880 | const DeclContext *DC = D.getDeclContext(); |
| 1881 | if (DC->isRecord()) { |
| 1882 | assert(D.getASTContext().getLangOpts().CPlusPlus); |
| 1883 | return false; |
| 1884 | } |
| 1885 | |
| 1886 | return D.getLanguageLinkage() == CLanguageLinkage; |
| 1887 | } |
| 1888 | |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1889 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 1890 | return getDeclLanguageLinkage(*this); |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1893 | bool VarDecl::isExternC() const { |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 1894 | return isDeclExternC(*this); |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1897 | bool VarDecl::isInExternCContext() const { |
Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 1898 | return getLexicalDeclContext()->isExternCContext(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | bool VarDecl::isInExternCXXContext() const { |
Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 1902 | return getLexicalDeclContext()->isExternCXXContext(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1905 | VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1906 | |
Nico Weber | 7f5015a | 2015-04-15 21:53:00 +0000 | [diff] [blame] | 1907 | VarDecl::DefinitionKind |
| 1908 | VarDecl::isThisDeclarationADefinition(ASTContext &C) const { |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1909 | // C++ [basic.def]p2: |
| 1910 | // A declaration is a definition unless [...] it contains the 'extern' |
| 1911 | // specifier or a linkage-specification and neither an initializer [...], |
| 1912 | // it declares a static data member in a class declaration [...]. |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1913 | // C++1y [temp.expl.spec]p15: |
| 1914 | // An explicit specialization of a static data member or an explicit |
| 1915 | // specialization of a static data member template is a definition if the |
| 1916 | // declaration includes an initializer; otherwise, it is a declaration. |
| 1917 | // |
| 1918 | // FIXME: How do you declare (but not define) a partial specialization of |
| 1919 | // a static data member template outside the containing class? |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1920 | if (isStaticDataMember()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1921 | if (isOutOfLine() && |
| 1922 | (hasInit() || |
| 1923 | // If the first declaration is out-of-line, this may be an |
| 1924 | // instantiation of an out-of-line partial specialization of a variable |
| 1925 | // template for which we have not yet instantiated the initializer. |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1926 | (getFirstDecl()->isOutOfLine() |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1927 | ? getTemplateSpecializationKind() == TSK_Undeclared |
| 1928 | : getTemplateSpecializationKind() != |
| 1929 | TSK_ExplicitSpecialization) || |
| 1930 | isa<VarTemplatePartialSpecializationDecl>(this))) |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1931 | return Definition; |
| 1932 | else |
| 1933 | return DeclarationOnly; |
| 1934 | } |
| 1935 | // C99 6.7p5: |
| 1936 | // A definition of an identifier is a declaration for that identifier that |
| 1937 | // [...] causes storage to be reserved for that object. |
| 1938 | // Note: that applies for all non-file-scope objects. |
| 1939 | // C99 6.9.2p1: |
| 1940 | // If the declaration of an identifier for an object has file scope and an |
| 1941 | // initializer, the declaration is an external definition for the identifier |
| 1942 | if (hasInit()) |
| 1943 | return Definition; |
Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 1944 | |
David Majnemer | dd0bed1 | 2015-05-14 05:19:20 +0000 | [diff] [blame] | 1945 | if (hasAttr<AliasAttr>()) |
Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 1946 | return Definition; |
| 1947 | |
David Majnemer | dd0bed1 | 2015-05-14 05:19:20 +0000 | [diff] [blame] | 1948 | if (const auto *SAA = getAttr<SelectAnyAttr>()) |
| 1949 | if (!SAA->isInherited()) |
| 1950 | return Definition; |
| 1951 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 1952 | // A variable template specialization (other than a static data member |
| 1953 | // template or an explicit specialization) is a declaration until we |
| 1954 | // instantiate its initializer. |
| 1955 | if (isa<VarTemplateSpecializationDecl>(this) && |
| 1956 | getTemplateSpecializationKind() != TSK_ExplicitSpecialization) |
| 1957 | return DeclarationOnly; |
| 1958 | |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 1959 | if (hasExternalStorage()) |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1960 | return DeclarationOnly; |
Rafael Espindola | 8f326a5 | 2013-03-07 01:42:44 +0000 | [diff] [blame] | 1961 | |
Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 1962 | // [dcl.link] p7: |
| 1963 | // A declaration directly contained in a linkage-specification is treated |
| 1964 | // as if it contains the extern specifier for the purpose of determining |
| 1965 | // the linkage of the declared name and whether it is a definition. |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 1966 | if (isSingleLineLanguageLinkage(*this)) |
Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 1967 | return DeclarationOnly; |
Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 1968 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1969 | // C99 6.9.2p2: |
| 1970 | // A declaration of an object that has file scope without an initializer, |
| 1971 | // and without a storage class specifier or the scs 'static', constitutes |
| 1972 | // a tentative definition. |
| 1973 | // No such thing in C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1974 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1975 | return TentativeDefinition; |
| 1976 | |
| 1977 | // What's left is (in C, block-scope) declarations without initializers or |
| 1978 | // external storage. These are definitions. |
| 1979 | return Definition; |
| 1980 | } |
| 1981 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1982 | VarDecl *VarDecl::getActingDefinition() { |
| 1983 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 1984 | if (Kind != TentativeDefinition) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1985 | return nullptr; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1986 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1987 | VarDecl *LastTentative = nullptr; |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 1988 | VarDecl *First = getFirstDecl(); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 1989 | for (auto I : First->redecls()) { |
| 1990 | Kind = I->isThisDeclarationADefinition(); |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1991 | if (Kind == Definition) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1992 | return nullptr; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1993 | else if (Kind == TentativeDefinition) |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 1994 | LastTentative = I; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 1995 | } |
| 1996 | return LastTentative; |
| 1997 | } |
| 1998 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 1999 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2000 | VarDecl *First = getFirstDecl(); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2001 | for (auto I : First->redecls()) { |
| 2002 | if (I->isThisDeclarationADefinition(C) == Definition) |
| 2003 | return I; |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2004 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2005 | return nullptr; |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2008 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 2009 | DefinitionKind Kind = DeclarationOnly; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2010 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2011 | const VarDecl *First = getFirstDecl(); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2012 | for (auto I : First->redecls()) { |
| 2013 | Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); |
Daniel Dunbar | 082c62d | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 2014 | if (Kind == Definition) |
| 2015 | break; |
| 2016 | } |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 2017 | |
| 2018 | return Kind; |
| 2019 | } |
| 2020 | |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2021 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2022 | for (auto I : redecls()) { |
| 2023 | if (auto Expr = I->getInit()) { |
| 2024 | D = I; |
| 2025 | return Expr; |
| 2026 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2027 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2028 | return nullptr; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2031 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2032 | if (Decl::isOutOfLine()) |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2033 | return true; |
Chandler Carruth | f50ef6e | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 2034 | |
| 2035 | if (!isStaticDataMember()) |
| 2036 | return false; |
| 2037 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2038 | // If this static data member was instantiated from a static data member of |
| 2039 | // a class template, check whether that static data member was defined |
| 2040 | // out-of-line. |
| 2041 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 2042 | return VD->isOutOfLine(); |
| 2043 | |
| 2044 | return false; |
| 2045 | } |
| 2046 | |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2047 | VarDecl *VarDecl::getOutOfLineDefinition() { |
| 2048 | if (!isStaticDataMember()) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2049 | return nullptr; |
| 2050 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2051 | for (auto RD : redecls()) { |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2052 | if (RD->getLexicalDeclContext()->isFileContext()) |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2053 | return RD; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2054 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2055 | |
| 2056 | return nullptr; |
Douglas Gregor | 1d957a3 | 2009-10-27 18:42:08 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2059 | void VarDecl::setInit(Expr *I) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2060 | if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2061 | Eval->~EvaluatedStmt(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2062 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
| 2065 | Init = I; |
| 2066 | } |
| 2067 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2068 | bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2069 | const LangOptions &Lang = C.getLangOpts(); |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2070 | |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2071 | if (!Lang.CPlusPlus) |
| 2072 | return false; |
| 2073 | |
| 2074 | // In C++11, any variable of reference type can be used in a constant |
| 2075 | // expression if it is initialized by a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2076 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2077 | return true; |
| 2078 | |
| 2079 | // 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] | 2080 | // not require the variable to be non-volatile, but we consider this to be a |
| 2081 | // defect. |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2082 | if (!getType().isConstQualified() || getType().isVolatileQualified()) |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2083 | return false; |
| 2084 | |
| 2085 | // In C++, const, non-volatile variables of integral or enumeration types |
| 2086 | // can be used in constant expressions. |
| 2087 | if (getType()->isIntegralOrEnumerationType()) |
| 2088 | return true; |
| 2089 | |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2090 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 2091 | // constant expressions. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2092 | return Lang.CPlusPlus11 && isConstexpr(); |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2095 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 2096 | /// form, which contains extra information on the evaluated value of the |
| 2097 | /// initializer. |
| 2098 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2099 | auto *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2100 | if (!Eval) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2101 | auto *S = Init.get<Stmt *>(); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2102 | // Note: EvaluatedStmt contains an APValue, which usually holds |
| 2103 | // resources not allocated from the ASTContext. We need to do some |
| 2104 | // work to avoid leaking those, but we do so in VarDecl::evaluateValue |
| 2105 | // 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] | 2106 | Eval = new (getASTContext()) EvaluatedStmt; |
| 2107 | Eval->Value = S; |
| 2108 | Init = Eval; |
| 2109 | } |
| 2110 | return Eval; |
| 2111 | } |
| 2112 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2113 | APValue *VarDecl::evaluateValue() const { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2114 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2115 | return evaluateValue(Notes); |
| 2116 | } |
| 2117 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2118 | namespace { |
| 2119 | // Destroy an APValue that was allocated in an ASTContext. |
| 2120 | void DestroyAPValue(void* UntypedValue) { |
| 2121 | static_cast<APValue*>(UntypedValue)->~APValue(); |
| 2122 | } |
| 2123 | } // namespace |
| 2124 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2125 | APValue *VarDecl::evaluateValue( |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2126 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2127 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2128 | |
| 2129 | // We only produce notes indicating why an initializer is non-constant the |
| 2130 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 2131 | // first time we try evaluation, so might not be produced at all. |
| 2132 | if (Eval->WasEvaluated) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2133 | return Eval->Evaluated.isUninit() ? nullptr : &Eval->Evaluated; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2134 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2135 | const auto *Init = cast<Expr>(Eval->Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2136 | assert(!Init->isValueDependent()); |
| 2137 | |
| 2138 | if (Eval->IsEvaluating) { |
| 2139 | // FIXME: Produce a diagnostic for self-initialization. |
| 2140 | Eval->CheckedICE = true; |
| 2141 | Eval->IsICE = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2142 | return nullptr; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | Eval->IsEvaluating = true; |
| 2146 | |
| 2147 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), |
| 2148 | this, Notes); |
| 2149 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2150 | // Ensure the computed APValue is cleaned up later if evaluation succeeded, |
| 2151 | // or that it's empty (so that there's nothing to clean up) if evaluation |
| 2152 | // failed. |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2153 | if (!Result) |
| 2154 | Eval->Evaluated = APValue(); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2155 | else if (Eval->Evaluated.needsCleanup()) |
| 2156 | getASTContext().AddDeallocation(DestroyAPValue, &Eval->Evaluated); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2157 | |
| 2158 | Eval->IsEvaluating = false; |
| 2159 | Eval->WasEvaluated = true; |
| 2160 | |
| 2161 | // In C++11, we have determined whether the initializer was a constant |
| 2162 | // expression as a side-effect. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2163 | if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) { |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2164 | Eval->CheckedICE = true; |
Eli Friedman | 8f66cdf | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 2165 | Eval->IsICE = Result && Notes.empty(); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2168 | return Result ? &Eval->Evaluated : nullptr; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | bool VarDecl::checkInitIsICE() const { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2172 | // Initializers of weak variables are never ICEs. |
| 2173 | if (isWeak()) |
| 2174 | return false; |
| 2175 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2176 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2177 | if (Eval->CheckedICE) |
| 2178 | // We have already checked whether this subexpression is an |
| 2179 | // integral constant expression. |
| 2180 | return Eval->IsICE; |
| 2181 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2182 | const auto *Init = cast<Expr>(Eval->Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2183 | assert(!Init->isValueDependent()); |
| 2184 | |
| 2185 | // In C++11, evaluate the initializer to check whether it's a constant |
| 2186 | // expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2187 | if (getASTContext().getLangOpts().CPlusPlus11) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2188 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2189 | evaluateValue(Notes); |
| 2190 | return Eval->IsICE; |
| 2191 | } |
| 2192 | |
| 2193 | // It's an ICE whether or not the definition we found is |
| 2194 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 2195 | // 6206 for details. |
| 2196 | |
| 2197 | if (Eval->CheckingICE) |
| 2198 | return false; |
| 2199 | Eval->CheckingICE = true; |
| 2200 | |
| 2201 | Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); |
| 2202 | Eval->CheckingICE = false; |
| 2203 | Eval->CheckedICE = true; |
| 2204 | return Eval->IsICE; |
| 2205 | } |
| 2206 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2207 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2208 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2209 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2210 | |
| 2211 | return nullptr; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 2214 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2215 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2216 | return Spec->getSpecializationKind(); |
| 2217 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2218 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2219 | return MSI->getTemplateSpecializationKind(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2221 | return TSK_Undeclared; |
| 2222 | } |
| 2223 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2224 | SourceLocation VarDecl::getPointOfInstantiation() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2225 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2226 | return Spec->getPointOfInstantiation(); |
| 2227 | |
| 2228 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2229 | return MSI->getPointOfInstantiation(); |
| 2230 | |
| 2231 | return SourceLocation(); |
| 2232 | } |
| 2233 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2234 | VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { |
| 2235 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2236 | .dyn_cast<VarTemplateDecl *>(); |
| 2237 | } |
| 2238 | |
| 2239 | void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { |
| 2240 | getASTContext().setTemplateOrSpecializationInfo(this, Template); |
| 2241 | } |
| 2242 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2243 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Richard Smith | b71782b | 2013-08-01 04:12:04 +0000 | [diff] [blame] | 2244 | if (isStaticDataMember()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2245 | // FIXME: Remove ? |
| 2246 | // return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 2247 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2248 | .dyn_cast<MemberSpecializationInfo *>(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2249 | return nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2252 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2253 | SourceLocation PointOfInstantiation) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2254 | assert((isa<VarTemplateSpecializationDecl>(this) || |
| 2255 | getMemberSpecializationInfo()) && |
| 2256 | "not a variable or static data member template specialization"); |
| 2257 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2258 | if (VarTemplateSpecializationDecl *Spec = |
| 2259 | dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2260 | Spec->setSpecializationKind(TSK); |
| 2261 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
| 2262 | Spec->getPointOfInstantiation().isInvalid()) |
| 2263 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { |
| 2267 | MSI->setTemplateSpecializationKind(TSK); |
| 2268 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
| 2269 | MSI->getPointOfInstantiation().isInvalid()) |
| 2270 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2271 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | void |
| 2275 | VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, |
| 2276 | TemplateSpecializationKind TSK) { |
| 2277 | assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && |
| 2278 | "Previous template or instantiation?"); |
| 2279 | getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2282 | //===----------------------------------------------------------------------===// |
| 2283 | // ParmVarDecl Implementation |
| 2284 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2285 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2286 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2287 | SourceLocation StartLoc, |
| 2288 | SourceLocation IdLoc, IdentifierInfo *Id, |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2289 | QualType T, TypeSourceInfo *TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2290 | StorageClass S, Expr *DefArg) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2291 | 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] | 2292 | S, DefArg); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2295 | QualType ParmVarDecl::getOriginalType() const { |
| 2296 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 2297 | QualType T = TSI ? TSI->getType() : getType(); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2298 | if (const auto *DT = dyn_cast<DecayedType>(T)) |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2299 | return DT->getOriginalType(); |
| 2300 | return T; |
| 2301 | } |
| 2302 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2303 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2304 | return new (C, ID) |
| 2305 | ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), |
| 2306 | nullptr, QualType(), nullptr, SC_None, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2309 | SourceRange ParmVarDecl::getSourceRange() const { |
| 2310 | if (!hasInheritedDefaultArg()) { |
| 2311 | SourceRange ArgRange = getDefaultArgRange(); |
| 2312 | if (ArgRange.isValid()) |
| 2313 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 2314 | } |
| 2315 | |
Argyrios Kyrtzidis | a077279 | 2013-04-17 01:56:48 +0000 | [diff] [blame] | 2316 | // DeclaratorDecl considers the range of postfix types as overlapping with the |
| 2317 | // declaration name, but this is not the case with parameters in ObjC methods. |
| 2318 | if (isa<ObjCMethodDecl>(getDeclContext())) |
| 2319 | return SourceRange(DeclaratorDecl::getLocStart(), getLocation()); |
| 2320 | |
Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2321 | return DeclaratorDecl::getSourceRange(); |
| 2322 | } |
| 2323 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2324 | Expr *ParmVarDecl::getDefaultArg() { |
| 2325 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 2326 | assert(!hasUninstantiatedDefaultArg() && |
| 2327 | "Default argument is not yet instantiated!"); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2328 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2329 | Expr *Arg = getInit(); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2330 | if (auto *E = dyn_cast_or_null<ExprWithCleanups>(Arg)) |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2331 | return E->getSubExpr(); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2332 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2333 | return Arg; |
| 2334 | } |
| 2335 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2336 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 2337 | if (const Expr *E = getInit()) |
| 2338 | return E->getSourceRange(); |
| 2339 | |
| 2340 | if (hasUninstantiatedDefaultArg()) |
| 2341 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 2342 | |
| 2343 | return SourceRange(); |
Argyrios Kyrtzidis | 02dd4f9 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 2344 | } |
| 2345 | |
Douglas Gregor | 3c6bd2a | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 2346 | bool ParmVarDecl::isParameterPack() const { |
| 2347 | return isa<PackExpansionType>(getType()); |
| 2348 | } |
| 2349 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 2350 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 2351 | getASTContext().setParameterIndex(this, parameterIndex); |
| 2352 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 2353 | } |
| 2354 | |
| 2355 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 2356 | return getASTContext().getParameterIndex(this); |
| 2357 | } |
| 2358 | |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 2359 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2360 | // FunctionDecl Implementation |
| 2361 | //===----------------------------------------------------------------------===// |
| 2362 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 2363 | void FunctionDecl::getNameForDiagnostic( |
| 2364 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 2365 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2366 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 2367 | if (TemplateArgs) |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 2368 | TemplateSpecializationType::PrintTemplateArgumentList( |
| 2369 | OS, TemplateArgs->data(), TemplateArgs->size(), Policy); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2372 | bool FunctionDecl::isVariadic() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2373 | if (const auto *FT = getType()->getAs<FunctionProtoType>()) |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2374 | return FT->isVariadic(); |
| 2375 | return false; |
| 2376 | } |
| 2377 | |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2378 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2379 | for (auto I : redecls()) { |
Francois Pichet | 1c229c0 | 2011-04-22 22:18:13 +0000 | [diff] [blame] | 2380 | if (I->Body || I->IsLateTemplateParsed) { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2381 | Definition = I; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2382 | return true; |
| 2383 | } |
| 2384 | } |
| 2385 | |
| 2386 | return false; |
| 2387 | } |
| 2388 | |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 2389 | bool FunctionDecl::hasTrivialBody() const |
| 2390 | { |
| 2391 | Stmt *S = getBody(); |
| 2392 | if (!S) { |
| 2393 | // Since we don't have a body for this function, we don't know if it's |
| 2394 | // trivial or not. |
| 2395 | return false; |
| 2396 | } |
| 2397 | |
| 2398 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 2399 | return true; |
| 2400 | return false; |
| 2401 | } |
| 2402 | |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2403 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2404 | for (auto I : redecls()) { |
Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2405 | if (I->IsDeleted || I->IsDefaulted || I->Body || I->IsLateTemplateParsed || |
| 2406 | I->hasAttr<AliasAttr>()) { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2407 | Definition = I->IsDeleted ? I->getCanonicalDecl() : I; |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2408 | return true; |
| 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | return false; |
| 2413 | } |
| 2414 | |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2415 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2416 | if (!hasBody(Definition)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2417 | return nullptr; |
Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2418 | |
| 2419 | if (Definition->Body) |
| 2420 | return Definition->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2421 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2422 | return nullptr; |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2425 | void FunctionDecl::setBody(Stmt *B) { |
| 2426 | Body = B; |
Douglas Gregor | 027ba50 | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 2427 | if (B) |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2428 | EndRangeLoc = B->getLocEnd(); |
| 2429 | } |
| 2430 | |
Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2431 | void FunctionDecl::setPure(bool P) { |
| 2432 | IsPure = P; |
| 2433 | if (P) |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2434 | if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2435 | Parent->markedVirtualFunctionPure(); |
| 2436 | } |
| 2437 | |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2438 | template<std::size_t Len> |
| 2439 | static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { |
| 2440 | IdentifierInfo *II = ND->getIdentifier(); |
| 2441 | return II && II->isStr(Str); |
| 2442 | } |
| 2443 | |
Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 2444 | bool FunctionDecl::isMain() const { |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2445 | const TranslationUnitDecl *tunit = |
| 2446 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2447 | return tunit && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2448 | !tunit->getASTContext().getLangOpts().Freestanding && |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2449 | isNamed(this, "main"); |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 2452 | bool FunctionDecl::isMSVCRTEntryPoint() const { |
| 2453 | const TranslationUnitDecl *TUnit = |
| 2454 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2455 | if (!TUnit) |
| 2456 | return false; |
| 2457 | |
| 2458 | // Even though we aren't really targeting MSVCRT if we are freestanding, |
| 2459 | // semantic analysis for these functions remains the same. |
| 2460 | |
| 2461 | // MSVCRT entry points only exist on MSVCRT targets. |
| 2462 | if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) |
| 2463 | return false; |
| 2464 | |
| 2465 | // Nameless functions like constructors cannot be entry points. |
| 2466 | if (!getIdentifier()) |
| 2467 | return false; |
| 2468 | |
| 2469 | return llvm::StringSwitch<bool>(getName()) |
| 2470 | .Cases("main", // an ANSI console app |
| 2471 | "wmain", // a Unicode console App |
| 2472 | "WinMain", // an ANSI GUI app |
| 2473 | "wWinMain", // a Unicode GUI app |
| 2474 | "DllMain", // a DLL |
| 2475 | true) |
| 2476 | .Default(false); |
| 2477 | } |
| 2478 | |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2479 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 2480 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 2481 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 2482 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 2483 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 2484 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 2485 | |
Richard Smith | f600441 | 2014-01-19 23:25:37 +0000 | [diff] [blame] | 2486 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2487 | return false; |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2488 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2489 | const auto *proto = getType()->castAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2490 | if (proto->getNumParams() != 2 || proto->isVariadic()) |
| 2491 | return false; |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2492 | |
| 2493 | ASTContext &Context = |
| 2494 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 2495 | ->getASTContext(); |
| 2496 | |
| 2497 | // The result type and first argument type are constant across all |
| 2498 | // these operators. The second argument must be exactly void*. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2499 | return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); |
Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2502 | bool FunctionDecl::isReplaceableGlobalAllocationFunction() const { |
| 2503 | if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) |
| 2504 | return false; |
| 2505 | if (getDeclName().getCXXOverloadedOperator() != OO_New && |
| 2506 | getDeclName().getCXXOverloadedOperator() != OO_Delete && |
| 2507 | getDeclName().getCXXOverloadedOperator() != OO_Array_New && |
| 2508 | getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
| 2509 | return false; |
| 2510 | |
| 2511 | if (isa<CXXRecordDecl>(getDeclContext())) |
| 2512 | return false; |
Richard Smith | f600441 | 2014-01-19 23:25:37 +0000 | [diff] [blame] | 2513 | |
| 2514 | // This can only fail for an invalid 'operator new' declaration. |
| 2515 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2516 | return false; |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2517 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2518 | const auto *FPT = getType()->castAs<FunctionProtoType>(); |
Nick Lewycky | 6fb99b9 | 2014-06-07 00:43:57 +0000 | [diff] [blame] | 2519 | if (FPT->getNumParams() == 0 || FPT->getNumParams() > 2 || FPT->isVariadic()) |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2520 | return false; |
| 2521 | |
| 2522 | // If this is a single-parameter function, it must be a replaceable global |
| 2523 | // allocation or deallocation function. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2524 | if (FPT->getNumParams() == 1) |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2525 | return true; |
| 2526 | |
| 2527 | // Otherwise, we're looking for a second parameter whose type is |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2528 | // 'const std::nothrow_t &', or, in C++1y, 'std::size_t'. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2529 | QualType Ty = FPT->getParamType(1); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2530 | ASTContext &Ctx = getASTContext(); |
Richard Smith | b47c36f | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 2531 | if (Ctx.getLangOpts().SizedDeallocation && |
| 2532 | Ctx.hasSameType(Ty, Ctx.getSizeType())) |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2533 | return true; |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2534 | if (!Ty->isReferenceType()) |
| 2535 | return false; |
| 2536 | Ty = Ty->getPointeeType(); |
| 2537 | if (Ty.getCVRQualifiers() != Qualifiers::Const) |
| 2538 | return false; |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2539 | const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
Richard Trieu | c771d5d | 2014-05-28 02:16:01 +0000 | [diff] [blame] | 2540 | return RD && isNamed(RD, "nothrow_t") && RD->isInStdNamespace(); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2543 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2544 | return getDeclLanguageLinkage(*this); |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2547 | bool FunctionDecl::isExternC() const { |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2548 | return isDeclExternC(*this); |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2551 | bool FunctionDecl::isInExternCContext() const { |
Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2552 | return getLexicalDeclContext()->isExternCContext(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | bool FunctionDecl::isInExternCXXContext() const { |
Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2556 | return getLexicalDeclContext()->isExternCXXContext(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2559 | bool FunctionDecl::isGlobal() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2560 | if (const auto *Method = dyn_cast<CXXMethodDecl>(this)) |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2561 | return Method->isStatic(); |
| 2562 | |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2563 | if (getCanonicalDecl()->getStorageClass() == SC_Static) |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2564 | return false; |
| 2565 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2566 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2567 | DC->isNamespace(); |
| 2568 | DC = DC->getParent()) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2569 | if (const auto *Namespace = cast<NamespaceDecl>(DC)) { |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2570 | if (!Namespace->getDeclName()) |
| 2571 | return false; |
| 2572 | break; |
| 2573 | } |
| 2574 | } |
| 2575 | |
| 2576 | return true; |
| 2577 | } |
| 2578 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2579 | bool FunctionDecl::isNoReturn() const { |
| 2580 | return hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
Richard Smith | debc59d | 2013-01-30 05:45:05 +0000 | [diff] [blame] | 2581 | hasAttr<C11NoReturnAttr>() || |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2582 | getType()->getAs<FunctionType>()->getNoReturnAttr(); |
| 2583 | } |
| 2584 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2585 | void |
| 2586 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2587 | redeclarable_base::setPreviousDecl(PrevDecl); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2588 | |
| 2589 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 2590 | FunctionTemplateDecl *PrevFunTmpl |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2591 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2592 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2593 | FunTmpl->setPreviousDecl(PrevFunTmpl); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2594 | } |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2595 | |
Axel Naumann | fbc7b98 | 2011-11-08 18:21:06 +0000 | [diff] [blame] | 2596 | if (PrevDecl && PrevDecl->IsInline) |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2597 | IsInline = true; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2600 | FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2601 | |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2602 | /// \brief Returns a value indicating whether this function |
| 2603 | /// corresponds to a builtin function. |
| 2604 | /// |
| 2605 | /// The function corresponds to a built-in function if it is |
| 2606 | /// declared at translation scope or within an extern "C" block and |
| 2607 | /// its name matches with the name of a builtin. The returned value |
| 2608 | /// 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] | 2609 | /// value of type \c Builtin::ID if in the target-independent range |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2610 | /// \c [1,Builtin::First), or a target-specific builtin value. |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 2611 | unsigned FunctionDecl::getBuiltinID() const { |
Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 2612 | if (!getIdentifier()) |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2613 | return 0; |
| 2614 | |
| 2615 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 2616 | if (!BuiltinID) |
| 2617 | return 0; |
| 2618 | |
| 2619 | ASTContext &Context = getASTContext(); |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2620 | if (Context.getLangOpts().CPlusPlus) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2621 | const auto *LinkageDecl = |
| 2622 | dyn_cast<LinkageSpecDecl>(getFirstDecl()->getDeclContext()); |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2623 | // In C++, the first declaration of a builtin is always inside an implicit |
| 2624 | // extern "C". |
| 2625 | // FIXME: A recognised library function may not be directly in an extern "C" |
| 2626 | // declaration, for instance "extern "C" { namespace std { decl } }". |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 2627 | if (!LinkageDecl) { |
| 2628 | if (BuiltinID == Builtin::BI__GetExceptionInfo && |
| 2629 | Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 2630 | isInStdNamespace()) |
| 2631 | return Builtin::BI__GetExceptionInfo; |
| 2632 | return 0; |
| 2633 | } |
| 2634 | if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c) |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2635 | return 0; |
| 2636 | } |
| 2637 | |
| 2638 | // If the function is marked "overloadable", it has a different mangled name |
| 2639 | // and is not the C library function. |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 2640 | if (hasAttr<OverloadableAttr>()) |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2641 | return 0; |
| 2642 | |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2643 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 2644 | return BuiltinID; |
| 2645 | |
| 2646 | // This function has the name of a known C library |
| 2647 | // function. Determine whether it actually refers to the C library |
| 2648 | // function or whether it just has the same name. |
| 2649 | |
Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 2650 | // If this is a static function, it's not a builtin. |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 2651 | if (getStorageClass() == SC_Static) |
Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 2652 | return 0; |
| 2653 | |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 2654 | return BuiltinID; |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | |
Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 2658 | /// getNumParams - Return the number of parameters this function must have |
Bob Wilson | b39017a | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 2659 | /// based on its FunctionType. This is the length of the ParamInfo array |
Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 2660 | /// after it has been created. |
| 2661 | unsigned FunctionDecl::getNumParams() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2662 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2663 | return FPT ? FPT->getNumParams() : 0; |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2666 | void FunctionDecl::setParams(ASTContext &C, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2667 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2668 | assert(!ParamInfo && "Already has param info!"); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2669 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | |
Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 2671 | // Zero params -> null pointer. |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 2672 | if (!NewParamInfo.empty()) { |
| 2673 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 2674 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 2675 | } |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 2676 | } |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 2677 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2678 | void FunctionDecl::setDeclsInPrototypeScope(ArrayRef<NamedDecl *> NewDecls) { |
James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 2679 | assert(DeclsInPrototypeScope.empty() && "Already has prototype decls!"); |
| 2680 | |
| 2681 | if (!NewDecls.empty()) { |
| 2682 | NamedDecl **A = new (getASTContext()) NamedDecl*[NewDecls.size()]; |
| 2683 | std::copy(NewDecls.begin(), NewDecls.end(), A); |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 2684 | DeclsInPrototypeScope = llvm::makeArrayRef(A, NewDecls.size()); |
Serge Pavlov | a826147 | 2014-06-25 17:09:41 +0000 | [diff] [blame] | 2685 | // Move declarations introduced in prototype to the function context. |
| 2686 | for (auto I : NewDecls) { |
| 2687 | DeclContext *DC = I->getDeclContext(); |
| 2688 | // Forward-declared reference to an enumeration is not added to |
| 2689 | // declaration scope, so skip declaration that is absent from its |
| 2690 | // declaration contexts. |
| 2691 | if (DC->containsDecl(I)) { |
| 2692 | DC->removeDecl(I); |
| 2693 | I->setDeclContext(this); |
| 2694 | addDecl(I); |
| 2695 | } |
| 2696 | } |
James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 2697 | } |
| 2698 | } |
| 2699 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2700 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 2701 | /// needed to call this function. This may be fewer than the number of |
| 2702 | /// function parameters, if some of the parameters have default |
Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 2703 | /// arguments (in C++) or are parameter packs (C++11). |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2704 | unsigned FunctionDecl::getMinRequiredArguments() const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2705 | if (!getASTContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 2706 | return getNumParams(); |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2707 | |
Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 2708 | unsigned NumRequiredArgs = 0; |
| 2709 | for (auto *Param : params()) |
| 2710 | if (!Param->isParameterPack() && !Param->hasDefaultArg()) |
| 2711 | ++NumRequiredArgs; |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2712 | return NumRequiredArgs; |
| 2713 | } |
| 2714 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 2715 | /// \brief The combination of the extern and inline keywords under MSVC forces |
| 2716 | /// the function to be required. |
| 2717 | /// |
| 2718 | /// Note: This function assumes that we will only get called when isInlined() |
| 2719 | /// would return true for this FunctionDecl. |
| 2720 | bool FunctionDecl::isMSExternInline() const { |
| 2721 | assert(isInlined() && "expected to get called on an inlined function!"); |
| 2722 | |
| 2723 | const ASTContext &Context = getASTContext(); |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 2724 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 2725 | !hasAttr<DLLExportAttr>()) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 2726 | return false; |
| 2727 | |
David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 2728 | for (const FunctionDecl *FD = getMostRecentDecl(); FD; |
| 2729 | FD = FD->getPreviousDecl()) |
David Majnemer | c0c42f3 | 2015-07-10 20:55:38 +0000 | [diff] [blame] | 2730 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 2731 | return true; |
| 2732 | |
| 2733 | return false; |
| 2734 | } |
| 2735 | |
| 2736 | static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { |
| 2737 | if (Redecl->getStorageClass() != SC_Extern) |
| 2738 | return false; |
| 2739 | |
| 2740 | for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; |
| 2741 | FD = FD->getPreviousDecl()) |
David Majnemer | c0c42f3 | 2015-07-10 20:55:38 +0000 | [diff] [blame] | 2742 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 2743 | return false; |
| 2744 | |
| 2745 | return true; |
| 2746 | } |
| 2747 | |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2748 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 2749 | // Only consider file-scope declarations in this test. |
| 2750 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 2751 | return false; |
| 2752 | |
| 2753 | // Only consider explicit declarations; the presence of a builtin for a |
| 2754 | // libcall shouldn't affect whether a definition is externally visible. |
| 2755 | if (Redecl->isImplicit()) |
| 2756 | return false; |
| 2757 | |
| 2758 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| 2759 | return true; // Not an inline definition |
| 2760 | |
| 2761 | return false; |
| 2762 | } |
| 2763 | |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2764 | /// \brief For a function declaration in C or C++, determine whether this |
| 2765 | /// declaration causes the definition to be externally visible. |
| 2766 | /// |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 2767 | /// For instance, this determines if adding the current declaration to the set |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2768 | /// of redeclarations of the given functions causes |
| 2769 | /// isInlineDefinitionExternallyVisible to change from false to true. |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2770 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| 2771 | assert(!doesThisDeclarationHaveABody() && |
| 2772 | "Must have a declaration without a body."); |
| 2773 | |
| 2774 | ASTContext &Context = getASTContext(); |
| 2775 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 2776 | if (Context.getLangOpts().MSVCCompat) { |
| 2777 | const FunctionDecl *Definition; |
| 2778 | if (hasBody(Definition) && Definition->isInlined() && |
| 2779 | redeclForcesDefMSVC(this)) |
| 2780 | return true; |
| 2781 | } |
| 2782 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2783 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2784 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 2785 | // an externally visible definition. |
| 2786 | // |
| 2787 | // FIXME: What happens if gnu_inline gets added on after the first |
| 2788 | // declaration? |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2789 | if (!isInlineSpecified() || getStorageClass() == SC_Extern) |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2790 | return false; |
| 2791 | |
| 2792 | const FunctionDecl *Prev = this; |
| 2793 | bool FoundBody = false; |
| 2794 | while ((Prev = Prev->getPreviousDecl())) { |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2795 | FoundBody |= Prev->Body.isValid(); |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2796 | |
| 2797 | if (Prev->Body) { |
| 2798 | // If it's not the case that both 'inline' and 'extern' are |
| 2799 | // specified on the definition, then it is always externally visible. |
| 2800 | if (!Prev->isInlineSpecified() || |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2801 | Prev->getStorageClass() != SC_Extern) |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2802 | return false; |
| 2803 | } else if (Prev->isInlineSpecified() && |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2804 | Prev->getStorageClass() != SC_Extern) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2805 | return false; |
| 2806 | } |
| 2807 | } |
| 2808 | return FoundBody; |
| 2809 | } |
| 2810 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2811 | if (Context.getLangOpts().CPlusPlus) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2812 | return false; |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2813 | |
| 2814 | // C99 6.7.4p6: |
| 2815 | // [...] If all of the file scope declarations for a function in a |
| 2816 | // translation unit include the inline function specifier without extern, |
| 2817 | // then the definition in that translation unit is an inline definition. |
| 2818 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2819 | return false; |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2820 | const FunctionDecl *Prev = this; |
| 2821 | bool FoundBody = false; |
| 2822 | while ((Prev = Prev->getPreviousDecl())) { |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2823 | FoundBody |= Prev->Body.isValid(); |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2824 | if (RedeclForcesDefC99(Prev)) |
| 2825 | return false; |
| 2826 | } |
| 2827 | return FoundBody; |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 2828 | } |
| 2829 | |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 2830 | SourceRange FunctionDecl::getReturnTypeSourceRange() const { |
| 2831 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 2832 | if (!TSI) |
| 2833 | return SourceRange(); |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 2834 | FunctionTypeLoc FTL = |
| 2835 | TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>(); |
| 2836 | if (!FTL) |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 2837 | return SourceRange(); |
| 2838 | |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 2839 | // Skip self-referential return types. |
| 2840 | const SourceManager &SM = getASTContext().getSourceManager(); |
| 2841 | SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); |
| 2842 | SourceLocation Boundary = getNameInfo().getLocStart(); |
| 2843 | if (RTRange.isInvalid() || Boundary.isInvalid() || |
| 2844 | !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) |
| 2845 | return SourceRange(); |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 2846 | |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 2847 | return RTRange; |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
Kaelyn Takata | 0a2e84c | 2015-04-09 19:43:04 +0000 | [diff] [blame] | 2850 | bool FunctionDecl::hasUnusedResultAttr() const { |
| 2851 | QualType RetType = getReturnType(); |
| 2852 | if (RetType->isRecordType()) { |
| 2853 | const CXXRecordDecl *Ret = RetType->getAsCXXRecordDecl(); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2854 | const auto *MD = dyn_cast<CXXMethodDecl>(this); |
Kaelyn Takata | 0a2e84c | 2015-04-09 19:43:04 +0000 | [diff] [blame] | 2855 | if (Ret && Ret->hasAttr<WarnUnusedResultAttr>() && |
| 2856 | !(MD && MD->getCorrespondingMethodInClass(Ret, true))) |
| 2857 | return true; |
| 2858 | } |
| 2859 | return hasAttr<WarnUnusedResultAttr>(); |
| 2860 | } |
| 2861 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2862 | /// \brief For an inline function definition in C, or for a gnu_inline function |
| 2863 | /// in C++, determine whether the definition will be externally visible. |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2864 | /// |
| 2865 | /// Inline function definitions are always available for inlining optimizations. |
| 2866 | /// However, depending on the language dialect, declaration specifiers, and |
| 2867 | /// attributes, the definition of an inline function may or may not be |
| 2868 | /// "externally" visible to other translation units in the program. |
| 2869 | /// |
| 2870 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 13c6670 | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 2871 | /// 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] | 2872 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 2873 | /// |
| 2874 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 2875 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 2876 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 2877 | /// an externally visible symbol, but "extern inline" will not create an |
| 2878 | /// externally visible symbol. |
| 2879 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2880 | assert(doesThisDeclarationHaveABody() && "Must have the function definition"); |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 2881 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 2882 | ASTContext &Context = getASTContext(); |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2883 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2884 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2885 | // Note: If you change the logic here, please change |
| 2886 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 2887 | // |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2888 | // If it's not the case that both 'inline' and 'extern' are |
| 2889 | // specified on the definition, then this inline definition is |
| 2890 | // externally visible. |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2891 | if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2892 | return true; |
| 2893 | |
| 2894 | // If any declaration is 'inline' but not 'extern', then this definition |
| 2895 | // is externally visible. |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2896 | for (auto Redecl : redecls()) { |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2897 | if (Redecl->isInlineSpecified() && |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2898 | Redecl->getStorageClass() != SC_Extern) |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2899 | return true; |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 2900 | } |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2901 | |
Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2902 | return false; |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2903 | } |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2904 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 2905 | // The rest of this function is C-only. |
| 2906 | assert(!Context.getLangOpts().CPlusPlus && |
| 2907 | "should not use C inline rules in C++"); |
| 2908 | |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2909 | // C99 6.7.4p6: |
| 2910 | // [...] If all of the file scope declarations for a function in a |
| 2911 | // translation unit include the inline function specifier without extern, |
| 2912 | // then the definition in that translation unit is an inline definition. |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2913 | for (auto Redecl : redecls()) { |
| 2914 | if (RedeclForcesDefC99(Redecl)) |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 2915 | return true; |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | // C99 6.7.4p6: |
| 2919 | // An inline definition does not provide an external definition for the |
| 2920 | // function, and does not forbid an external definition in another |
| 2921 | // translation unit. |
Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 2922 | return false; |
| 2923 | } |
| 2924 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2925 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 2926 | /// function represents, if any. |
| 2927 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 2928 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 2929 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 2930 | else |
| 2931 | return OO_None; |
| 2932 | } |
| 2933 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2934 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 2935 | /// represents, if any. |
| 2936 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 2937 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 2938 | return getDeclName().getCXXLiteralIdentifier(); |
| 2939 | else |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2940 | return nullptr; |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2943 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 2944 | if (TemplateOrSpecialization.isNull()) |
| 2945 | return TK_NonTemplate; |
| 2946 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 2947 | return TK_FunctionTemplate; |
| 2948 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 2949 | return TK_MemberSpecialization; |
| 2950 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 2951 | return TK_FunctionTemplateSpecialization; |
| 2952 | if (TemplateOrSpecialization.is |
| 2953 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 2954 | return TK_DependentFunctionTemplateSpecialization; |
| 2955 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2956 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 2957 | } |
| 2958 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2959 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2960 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2961 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2962 | |
| 2963 | return nullptr; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | void |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2967 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 2968 | FunctionDecl *FD, |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2969 | TemplateSpecializationKind TSK) { |
| 2970 | assert(TemplateOrSpecialization.isNull() && |
| 2971 | "Member function is already a specialization"); |
| 2972 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 2973 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 2974 | TemplateOrSpecialization = Info; |
| 2975 | } |
| 2976 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2977 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 2978 | // If the function is invalid, it can't be implicitly instantiated. |
| 2979 | if (isInvalidDecl()) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2980 | return false; |
| 2981 | |
| 2982 | switch (getTemplateSpecializationKind()) { |
| 2983 | case TSK_Undeclared: |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2984 | case TSK_ExplicitInstantiationDefinition: |
| 2985 | return false; |
| 2986 | |
| 2987 | case TSK_ImplicitInstantiation: |
| 2988 | return true; |
| 2989 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2990 | // It is possible to instantiate TSK_ExplicitSpecialization kind |
| 2991 | // if the FunctionDecl has a class scope specialization pattern. |
| 2992 | case TSK_ExplicitSpecialization: |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2993 | return getClassScopeSpecializationPattern() != nullptr; |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 2994 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 2995 | case TSK_ExplicitInstantiationDeclaration: |
| 2996 | // Handled below. |
| 2997 | break; |
| 2998 | } |
| 2999 | |
| 3000 | // Find the actual template from which we will instantiate. |
| 3001 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3002 | bool HasPattern = false; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3003 | if (PatternDecl) |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3004 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3005 | |
| 3006 | // C++0x [temp.explicit]p9: |
| 3007 | // Except for inline functions, other explicit instantiation declarations |
| 3008 | // have the effect of suppressing the implicit instantiation of the entity |
| 3009 | // to which they refer. |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3010 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3011 | return true; |
| 3012 | |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 3013 | return PatternDecl->isInlined(); |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | bool FunctionDecl::isTemplateInstantiation() const { |
| 3017 | switch (getTemplateSpecializationKind()) { |
| 3018 | case TSK_Undeclared: |
| 3019 | case TSK_ExplicitSpecialization: |
| 3020 | return false; |
| 3021 | case TSK_ImplicitInstantiation: |
| 3022 | case TSK_ExplicitInstantiationDeclaration: |
| 3023 | case TSK_ExplicitInstantiationDefinition: |
| 3024 | return true; |
| 3025 | } |
| 3026 | llvm_unreachable("All TSK values handled."); |
| 3027 | } |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3028 | |
| 3029 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3030 | // Handle class scope explicit specialization special case. |
| 3031 | if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) |
| 3032 | return getClassScopeSpecializationPattern(); |
Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3033 | |
| 3034 | // If this is a generic lambda call operator specialization, its |
| 3035 | // instantiation pattern is always its primary template's pattern |
| 3036 | // even if its primary template was instantiated from another |
| 3037 | // member template (which happens with nested generic lambdas). |
| 3038 | // Since a lambda's call operator's body is transformed eagerly, |
| 3039 | // we don't have to go hunting for a prototype definition template |
| 3040 | // (i.e. instantiated-from-member-template) to use as an instantiation |
| 3041 | // pattern. |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3042 | |
Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3043 | if (isGenericLambdaCallOperatorSpecialization( |
| 3044 | dyn_cast<CXXMethodDecl>(this))) { |
| 3045 | assert(getPrimaryTemplate() && "A generic lambda specialization must be " |
| 3046 | "generated from a primary call operator " |
| 3047 | "template"); |
| 3048 | assert(getPrimaryTemplate()->getTemplatedDecl()->getBody() && |
| 3049 | "A generic lambda call operator template must always have a body - " |
| 3050 | "even if instantiated from a prototype (i.e. as written) member " |
| 3051 | "template"); |
| 3052 | return getPrimaryTemplate()->getTemplatedDecl(); |
| 3053 | } |
| 3054 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3055 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 3056 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 3057 | // If we have hit a point where the user provided a specialization of |
| 3058 | // this template, we're done looking. |
| 3059 | if (Primary->isMemberSpecialization()) |
| 3060 | break; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3061 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 3062 | } |
| 3063 | |
| 3064 | return Primary->getTemplatedDecl(); |
| 3065 | } |
| 3066 | |
| 3067 | return getInstantiatedFromMemberFunction(); |
| 3068 | } |
| 3069 | |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3070 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3072 | = TemplateOrSpecialization |
| 3073 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3074 | return Info->Template.getPointer(); |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3075 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3076 | return nullptr; |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3079 | FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const { |
| 3080 | return getASTContext().getClassScopeSpecializationPattern(this); |
| 3081 | } |
| 3082 | |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3083 | const TemplateArgumentList * |
| 3084 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3085 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 3086 | = TemplateOrSpecialization |
| 3087 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3088 | return Info->TemplateArguments; |
| 3089 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3090 | return nullptr; |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 3093 | const ASTTemplateArgumentListInfo * |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3094 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 3095 | if (FunctionTemplateSpecializationInfo *Info |
| 3096 | = TemplateOrSpecialization |
| 3097 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 3098 | return Info->TemplateArgumentsAsWritten; |
| 3099 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3100 | return nullptr; |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3103 | void |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3104 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 3105 | FunctionTemplateDecl *Template, |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3106 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3107 | void *InsertPos, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3108 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 927d8e0 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 3109 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 3110 | SourceLocation PointOfInstantiation) { |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3111 | assert(TSK != TSK_Undeclared && |
| 3112 | "Must specify the type of function template specialization"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3113 | FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3114 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3115 | if (!Info) |
Argyrios Kyrtzidis | e262a95 | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 3116 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 3117 | TemplateArgs, |
| 3118 | TemplateArgsAsWritten, |
| 3119 | PointOfInstantiation); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3120 | TemplateOrSpecialization = Info; |
Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 3121 | Template->addSpecialization(Info, InsertPos); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3122 | } |
| 3123 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3124 | void |
| 3125 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 3126 | const UnresolvedSetImpl &Templates, |
| 3127 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3128 | assert(TemplateOrSpecialization.isNull()); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3129 | DependentFunctionTemplateSpecializationInfo *Info = |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3130 | DependentFunctionTemplateSpecializationInfo::Create(Context, Templates, |
| 3131 | TemplateArgs); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3132 | TemplateOrSpecialization = Info; |
| 3133 | } |
| 3134 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3135 | DependentFunctionTemplateSpecializationInfo * |
| 3136 | DependentFunctionTemplateSpecializationInfo::Create( |
| 3137 | ASTContext &Context, const UnresolvedSetImpl &Ts, |
| 3138 | const TemplateArgumentListInfo &TArgs) { |
| 3139 | void *Buffer = Context.Allocate( |
| 3140 | totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>( |
| 3141 | TArgs.size(), Ts.size())); |
| 3142 | return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs); |
| 3143 | } |
| 3144 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3145 | DependentFunctionTemplateSpecializationInfo:: |
| 3146 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 3147 | const TemplateArgumentListInfo &TArgs) |
Benjamin Kramer | 3ebba52 | 2015-04-02 12:43:31 +0000 | [diff] [blame] | 3148 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3149 | |
James Y Knight | 53c7616 | 2015-07-17 18:21:37 +0000 | [diff] [blame] | 3150 | NumTemplates = Ts.size(); |
| 3151 | NumArgs = TArgs.size(); |
Benjamin Kramer | 3ebba52 | 2015-04-02 12:43:31 +0000 | [diff] [blame] | 3152 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3153 | FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3154 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 3155 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 3156 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3157 | TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3158 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 3159 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 3160 | } |
| 3161 | |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3162 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | // For a function template specialization, query the specialization |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3164 | // information object. |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3165 | FunctionTemplateSpecializationInfo *FTSInfo |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3166 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3167 | if (FTSInfo) |
| 3168 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3170 | MemberSpecializationInfo *MSInfo |
| 3171 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 3172 | if (MSInfo) |
| 3173 | return MSInfo->getTemplateSpecializationKind(); |
| 3174 | |
| 3175 | return TSK_Undeclared; |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3176 | } |
| 3177 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3178 | void |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3179 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 3180 | SourceLocation PointOfInstantiation) { |
| 3181 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3182 | = TemplateOrSpecialization.dyn_cast< |
| 3183 | FunctionTemplateSpecializationInfo*>()) { |
| 3184 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 3185 | if (TSK != TSK_ExplicitSpecialization && |
| 3186 | PointOfInstantiation.isValid() && |
| 3187 | FTSInfo->getPointOfInstantiation().isInvalid()) |
| 3188 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 3189 | } else if (MemberSpecializationInfo *MSInfo |
| 3190 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 3191 | MSInfo->setTemplateSpecializationKind(TSK); |
| 3192 | if (TSK != TSK_ExplicitSpecialization && |
| 3193 | PointOfInstantiation.isValid() && |
| 3194 | MSInfo->getPointOfInstantiation().isInvalid()) |
| 3195 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 3196 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3197 | llvm_unreachable("Function cannot have a template specialization kind"); |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3201 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3202 | = TemplateOrSpecialization.dyn_cast< |
| 3203 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3204 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3205 | else if (MemberSpecializationInfo *MSInfo |
| 3206 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3207 | return MSInfo->getPointOfInstantiation(); |
| 3208 | |
| 3209 | return SourceLocation(); |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3212 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3213 | if (Decl::isOutOfLine()) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3214 | return true; |
| 3215 | |
| 3216 | // If this function was instantiated from a member function of a |
| 3217 | // class template, check whether that member function was defined out-of-line. |
| 3218 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 3219 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3220 | if (FD->hasBody(Definition)) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3221 | return Definition->isOutOfLine(); |
| 3222 | } |
| 3223 | |
| 3224 | // If this function was instantiated from a function template, |
| 3225 | // check whether that function template was defined out-of-line. |
| 3226 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 3227 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3228 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3229 | return Definition->isOutOfLine(); |
| 3230 | } |
| 3231 | |
| 3232 | return false; |
| 3233 | } |
| 3234 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3235 | SourceRange FunctionDecl::getSourceRange() const { |
| 3236 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 3237 | } |
| 3238 | |
Anna Zaks | 28db7ce | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 3239 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3240 | IdentifierInfo *FnInfo = getIdentifier(); |
| 3241 | |
| 3242 | if (!FnInfo) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3243 | return 0; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3244 | |
| 3245 | // Builtin handling. |
| 3246 | switch (getBuiltinID()) { |
| 3247 | case Builtin::BI__builtin_memset: |
| 3248 | case Builtin::BI__builtin___memset_chk: |
| 3249 | case Builtin::BImemset: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3250 | return Builtin::BImemset; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3251 | |
| 3252 | case Builtin::BI__builtin_memcpy: |
| 3253 | case Builtin::BI__builtin___memcpy_chk: |
| 3254 | case Builtin::BImemcpy: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3255 | return Builtin::BImemcpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3256 | |
| 3257 | case Builtin::BI__builtin_memmove: |
| 3258 | case Builtin::BI__builtin___memmove_chk: |
| 3259 | case Builtin::BImemmove: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3260 | return Builtin::BImemmove; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3261 | |
| 3262 | case Builtin::BIstrlcpy: |
Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3263 | case Builtin::BI__builtin___strlcpy_chk: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3264 | return Builtin::BIstrlcpy; |
Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3265 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3266 | case Builtin::BIstrlcat: |
Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3267 | case Builtin::BI__builtin___strlcat_chk: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3268 | return Builtin::BIstrlcat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3269 | |
| 3270 | case Builtin::BI__builtin_memcmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3271 | case Builtin::BImemcmp: |
| 3272 | return Builtin::BImemcmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3273 | |
| 3274 | case Builtin::BI__builtin_strncpy: |
| 3275 | case Builtin::BI__builtin___strncpy_chk: |
| 3276 | case Builtin::BIstrncpy: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3277 | return Builtin::BIstrncpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3278 | |
| 3279 | case Builtin::BI__builtin_strncmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3280 | case Builtin::BIstrncmp: |
| 3281 | return Builtin::BIstrncmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3282 | |
| 3283 | case Builtin::BI__builtin_strncasecmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3284 | case Builtin::BIstrncasecmp: |
| 3285 | return Builtin::BIstrncasecmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3286 | |
| 3287 | case Builtin::BI__builtin_strncat: |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3288 | case Builtin::BI__builtin___strncat_chk: |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3289 | case Builtin::BIstrncat: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3290 | return Builtin::BIstrncat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3291 | |
| 3292 | case Builtin::BI__builtin_strndup: |
| 3293 | case Builtin::BIstrndup: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3294 | return Builtin::BIstrndup; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3295 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3296 | case Builtin::BI__builtin_strlen: |
| 3297 | case Builtin::BIstrlen: |
| 3298 | return Builtin::BIstrlen; |
| 3299 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3300 | default: |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 3301 | if (isExternC()) { |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3302 | if (FnInfo->isStr("memset")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3303 | return Builtin::BImemset; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3304 | else if (FnInfo->isStr("memcpy")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3305 | return Builtin::BImemcpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3306 | else if (FnInfo->isStr("memmove")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3307 | return Builtin::BImemmove; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3308 | else if (FnInfo->isStr("memcmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3309 | return Builtin::BImemcmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3310 | else if (FnInfo->isStr("strncpy")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3311 | return Builtin::BIstrncpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3312 | else if (FnInfo->isStr("strncmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3313 | return Builtin::BIstrncmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3314 | else if (FnInfo->isStr("strncasecmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3315 | return Builtin::BIstrncasecmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3316 | else if (FnInfo->isStr("strncat")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3317 | return Builtin::BIstrncat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3318 | else if (FnInfo->isStr("strndup")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3319 | return Builtin::BIstrndup; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3320 | else if (FnInfo->isStr("strlen")) |
| 3321 | return Builtin::BIstrlen; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3322 | } |
| 3323 | break; |
| 3324 | } |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3325 | return 0; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 3328 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3329 | // FieldDecl Implementation |
| 3330 | //===----------------------------------------------------------------------===// |
| 3331 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3332 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3333 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3334 | IdentifierInfo *Id, QualType T, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3335 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3336 | InClassInitStyle InitStyle) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3337 | return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
| 3338 | BW, Mutable, InitStyle); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3339 | } |
| 3340 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3341 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3342 | return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), |
| 3343 | SourceLocation(), nullptr, QualType(), nullptr, |
| 3344 | nullptr, false, ICIS_NoInit); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3347 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 3348 | if (!isImplicit() || getDeclName()) |
| 3349 | return false; |
| 3350 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3351 | if (const auto *Record = getType()->getAs<RecordType>()) |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3352 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 3353 | |
| 3354 | return false; |
| 3355 | } |
| 3356 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3357 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 3358 | assert(isBitField() && "not a bitfield"); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3359 | auto *BitWidth = static_cast<Expr *>(InitStorage.getPointer()); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3360 | return BitWidth->EvaluateKnownConstInt(Ctx).getZExtValue(); |
| 3361 | } |
| 3362 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3363 | unsigned FieldDecl::getFieldIndex() const { |
Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 3364 | const FieldDecl *Canonical = getCanonicalDecl(); |
| 3365 | if (Canonical != this) |
| 3366 | return Canonical->getFieldIndex(); |
| 3367 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3368 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 3369 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3370 | unsigned Index = 0; |
Fariborz Jahanian | 8409bce4 | 2011-04-28 22:49:46 +0000 | [diff] [blame] | 3371 | const RecordDecl *RD = getParent(); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3372 | |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 3373 | for (auto *Field : RD->fields()) { |
| 3374 | Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; |
| 3375 | ++Index; |
| 3376 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3377 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3378 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 3379 | return CachedFieldIndex - 1; |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 3382 | SourceRange FieldDecl::getSourceRange() const { |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3383 | switch (InitStorage.getInt()) { |
| 3384 | // All three of these cases store an optional Expr*. |
| 3385 | case ISK_BitWidthOrNothing: |
| 3386 | case ISK_InClassCopyInit: |
| 3387 | case ISK_InClassListInit: |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3388 | if (const auto *E = static_cast<const Expr *>(InitStorage.getPointer())) |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3389 | return SourceRange(getInnerLocStart(), E->getLocEnd()); |
| 3390 | // FALLTHROUGH |
Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 3391 | |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3392 | case ISK_CapturedVLAType: |
| 3393 | return DeclaratorDecl::getSourceRange(); |
| 3394 | } |
| 3395 | llvm_unreachable("bad init storage kind"); |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { |
David Blaikie | 11ab078 | 2014-10-31 17:18:09 +0000 | [diff] [blame] | 3399 | assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 3400 | "capturing type in non-lambda or captured record."); |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3401 | assert(InitStorage.getInt() == ISK_BitWidthOrNothing && |
| 3402 | InitStorage.getPointer() == nullptr && |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3403 | "bit width, initializer or captured type already set"); |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3404 | InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType), |
| 3405 | ISK_CapturedVLAType); |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3408 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3409 | // TagDecl Implementation |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3410 | //===----------------------------------------------------------------------===// |
| 3411 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3412 | SourceLocation TagDecl::getOuterLocStart() const { |
| 3413 | return getTemplateOrInnerLocStart(this); |
| 3414 | } |
| 3415 | |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3416 | SourceRange TagDecl::getSourceRange() const { |
| 3417 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3418 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3419 | } |
| 3420 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3421 | TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } |
Argyrios Kyrtzidis | 5614aef | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 3422 | |
Rafael Espindola | bf5c33b | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 3423 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3424 | TypedefNameDeclOrQualifier = TDD; |
Reid Kleckner | cae82a2 | 2014-04-23 22:03:04 +0000 | [diff] [blame] | 3425 | if (const Type *T = getTypeForDecl()) { |
| 3426 | (void)T; |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 3427 | assert(T->isLinkageValid()); |
Reid Kleckner | cae82a2 | 2014-04-23 22:03:04 +0000 | [diff] [blame] | 3428 | } |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 3429 | assert(isLinkageValid()); |
Douglas Gregor | a72a4e3 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3432 | void TagDecl::startDefinition() { |
Sebastian Redl | 9d8854e | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 3433 | IsBeingDefined = true; |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3434 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3435 | if (auto *D = dyn_cast<CXXRecordDecl>(this)) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3436 | struct CXXRecordDecl::DefinitionData *Data = |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3437 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3438 | for (auto I : redecls()) |
Richard Smith | 64c0630 | 2014-05-22 23:19:02 +0000 | [diff] [blame] | 3439 | cast<CXXRecordDecl>(I)->DefinitionData = Data; |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3440 | } |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | void TagDecl::completeDefinition() { |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3444 | assert((!isa<CXXRecordDecl>(this) || |
| 3445 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 3446 | "definition completed but not started"); |
| 3447 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3448 | IsCompleteDefinition = true; |
Sebastian Redl | 9d8854e | 2010-08-02 18:27:05 +0000 | [diff] [blame] | 3449 | IsBeingDefined = false; |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 3450 | |
| 3451 | if (ASTMutationListener *L = getASTMutationListener()) |
| 3452 | L->CompletedTagDefinition(this); |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3455 | TagDecl *TagDecl::getDefinition() const { |
| 3456 | if (isCompleteDefinition()) |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 3457 | return const_cast<TagDecl *>(this); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3458 | |
| 3459 | // If it's possible for us to have an out-of-date definition, check now. |
| 3460 | if (MayHaveOutOfDateDef) { |
| 3461 | if (IdentifierInfo *II = getIdentifier()) { |
| 3462 | if (II->isOutOfDate()) { |
| 3463 | updateOutOfDate(*II); |
| 3464 | } |
| 3465 | } |
| 3466 | } |
| 3467 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3468 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
Andrew Trick | ba266ee | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 3469 | return CXXRD->getDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3470 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3471 | for (auto R : redecls()) |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3472 | if (R->isCompleteDefinition()) |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3473 | return R; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3475 | return nullptr; |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3476 | } |
| 3477 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3478 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 3479 | if (QualifierLoc) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3480 | // Make sure the extended qualifier info is allocated. |
| 3481 | if (!hasExtInfo()) |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3482 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3483 | // Set qualifier info. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3484 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3485 | } else { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3486 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3487 | if (hasExtInfo()) { |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3488 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 3489 | getASTContext().Deallocate(getExtInfo()); |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3490 | TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3491 | } |
| 3492 | else |
| 3493 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3494 | } |
| 3495 | } |
| 3496 | } |
| 3497 | |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 3498 | void TagDecl::setTemplateParameterListsInfo( |
| 3499 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 3500 | assert(!TPLists.empty()); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3501 | // Make sure the extended decl info is allocated. |
| 3502 | if (!hasExtInfo()) |
| 3503 | // Allocate external info struct. |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3504 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3505 | // Set the template parameter lists info. |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 3506 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3509 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3510 | // EnumDecl Implementation |
| 3511 | //===----------------------------------------------------------------------===// |
| 3512 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3513 | void EnumDecl::anchor() { } |
| 3514 | |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3515 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 3516 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3517 | IdentifierInfo *Id, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3518 | EnumDecl *PrevDecl, bool IsScoped, |
| 3519 | bool IsScopedUsingClassTag, bool IsFixed) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3520 | auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, |
| 3521 | IsScoped, IsScopedUsingClassTag, IsFixed); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3522 | Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3523 | C.getTypeDeclType(Enum, PrevDecl); |
| 3524 | return Enum; |
| 3525 | } |
| 3526 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3527 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3528 | EnumDecl *Enum = |
| 3529 | new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 3530 | nullptr, nullptr, false, false, false); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3531 | Enum->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 3532 | return Enum; |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 3535 | SourceRange EnumDecl::getIntegerTypeRange() const { |
| 3536 | if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) |
| 3537 | return TI->getTypeLoc().getSourceRange(); |
| 3538 | return SourceRange(); |
| 3539 | } |
| 3540 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 3541 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 3542 | QualType NewPromotionType, |
| 3543 | unsigned NumPositiveBits, |
| 3544 | unsigned NumNegativeBits) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3545 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3546 | if (!IntegerType) |
| 3547 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3548 | PromotionType = NewPromotionType; |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 3549 | setNumPositiveBits(NumPositiveBits); |
| 3550 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3551 | TagDecl::completeDefinition(); |
| 3552 | } |
| 3553 | |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3554 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 3555 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 3556 | return MSI->getTemplateSpecializationKind(); |
| 3557 | |
| 3558 | return TSK_Undeclared; |
| 3559 | } |
| 3560 | |
| 3561 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 3562 | SourceLocation PointOfInstantiation) { |
| 3563 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 3564 | assert(MSI && "Not an instantiated member enumeration?"); |
| 3565 | MSI->setTemplateSpecializationKind(TSK); |
| 3566 | if (TSK != TSK_ExplicitSpecialization && |
| 3567 | PointOfInstantiation.isValid() && |
| 3568 | MSI->getPointOfInstantiation().isInvalid()) |
| 3569 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 3570 | } |
| 3571 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3572 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 3573 | if (SpecializationInfo) |
| 3574 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 3575 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3576 | return nullptr; |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 3580 | TemplateSpecializationKind TSK) { |
| 3581 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 3582 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 3583 | } |
| 3584 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3585 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 3586 | // RecordDecl Implementation |
| 3587 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 3588 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3589 | RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, |
| 3590 | DeclContext *DC, SourceLocation StartLoc, |
| 3591 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 3592 | RecordDecl *PrevDecl) |
| 3593 | : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 3594 | HasFlexibleArrayMember = false; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3595 | AnonymousStructOrUnion = false; |
Fariborz Jahanian | 5f21d2f | 2009-07-08 01:18:33 +0000 | [diff] [blame] | 3596 | HasObjectMember = false; |
Fariborz Jahanian | 7865220 | 2013-01-25 23:57:05 +0000 | [diff] [blame] | 3597 | HasVolatileMember = false; |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3598 | LoadedFieldsFromExternalStorage = false; |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 3599 | assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 3600 | } |
| 3601 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3602 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3603 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3604 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3605 | RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, |
| 3606 | StartLoc, IdLoc, Id, PrevDecl); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3607 | R->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 3608 | |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3609 | C.getTypeDeclType(R, PrevDecl); |
| 3610 | return R; |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3613 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3614 | RecordDecl *R = |
| 3615 | new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(), |
| 3616 | SourceLocation(), nullptr, nullptr); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3617 | R->MayHaveOutOfDateDef = C.getLangOpts().Modules; |
| 3618 | return R; |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 3621 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3622 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 3623 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 3624 | } |
| 3625 | |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3626 | bool RecordDecl::isLambda() const { |
| 3627 | if (auto RD = dyn_cast<CXXRecordDecl>(this)) |
| 3628 | return RD->isLambda(); |
| 3629 | return false; |
| 3630 | } |
| 3631 | |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 3632 | bool RecordDecl::isCapturedRecord() const { |
| 3633 | return hasAttr<CapturedRecordAttr>(); |
| 3634 | } |
| 3635 | |
| 3636 | void RecordDecl::setCapturedRecord() { |
| 3637 | addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); |
| 3638 | } |
| 3639 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3640 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 3641 | if (hasExternalLexicalStorage() && !LoadedFieldsFromExternalStorage) |
| 3642 | LoadFieldsFromExternalStorage(); |
| 3643 | |
| 3644 | return field_iterator(decl_iterator(FirstDecl)); |
| 3645 | } |
| 3646 | |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3647 | /// completeDefinition - Notes that the definition of this type is now |
| 3648 | /// complete. |
| 3649 | void RecordDecl::completeDefinition() { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3650 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3651 | TagDecl::completeDefinition(); |
| 3652 | } |
| 3653 | |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 3654 | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
| 3655 | /// This which can be turned on with an attribute, pragma, or the |
| 3656 | /// -mms-bitfields command-line option. |
| 3657 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 3658 | return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3661 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 3662 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 3663 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 3664 | |
| 3665 | // Notify that we have a RecordDecl doing some initialization. |
| 3666 | ExternalASTSource::Deserializing TheFields(Source); |
| 3667 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3668 | SmallVector<Decl*, 64> Decls; |
Richard Smith | 3cb1572 | 2015-08-05 22:41:45 +0000 | [diff] [blame] | 3669 | LoadedFieldsFromExternalStorage = true; |
| 3670 | Source->FindExternalLexicalDecls(this, [](Decl::Kind K) { |
| 3671 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
| 3672 | }, Decls); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3673 | |
| 3674 | #ifndef NDEBUG |
| 3675 | // Check that all decls we got were FieldDecls. |
| 3676 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
Argyrios Kyrtzidis | f89a927 | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 3677 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3678 | #endif |
| 3679 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3680 | if (Decls.empty()) |
| 3681 | return; |
| 3682 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 3683 | std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
Argyrios Kyrtzidis | 094da73 | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 3684 | /*FieldsAlreadyLoaded=*/false); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 3687 | bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { |
| 3688 | ASTContext &Context = getASTContext(); |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 3689 | if (!Context.getLangOpts().Sanitize.hasOneOf( |
| 3690 | SanitizerKind::Address | SanitizerKind::KernelAddress) || |
Alexey Samsonov | a041610 | 2014-11-11 01:26:14 +0000 | [diff] [blame] | 3691 | !Context.getLangOpts().SanitizeAddressFieldPadding) |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 3692 | return false; |
Alexey Samsonov | 33e00e2 | 2014-10-16 23:50:26 +0000 | [diff] [blame] | 3693 | const auto &Blacklist = Context.getSanitizerBlacklist(); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3694 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(this); |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 3695 | // We may be able to relax some of these requirements. |
| 3696 | int ReasonToReject = -1; |
| 3697 | if (!CXXRD || CXXRD->isExternCContext()) |
| 3698 | ReasonToReject = 0; // is not C++. |
| 3699 | else if (CXXRD->hasAttr<PackedAttr>()) |
| 3700 | ReasonToReject = 1; // is packed. |
| 3701 | else if (CXXRD->isUnion()) |
| 3702 | ReasonToReject = 2; // is a union. |
| 3703 | else if (CXXRD->isTriviallyCopyable()) |
| 3704 | ReasonToReject = 3; // is trivially copyable. |
| 3705 | else if (CXXRD->hasTrivialDestructor()) |
| 3706 | ReasonToReject = 4; // has trivial destructor. |
| 3707 | else if (CXXRD->isStandardLayout()) |
| 3708 | ReasonToReject = 5; // is standard layout. |
Alexey Samsonov | 33e00e2 | 2014-10-16 23:50:26 +0000 | [diff] [blame] | 3709 | else if (Blacklist.isBlacklistedLocation(getLocation(), "field-padding")) |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 3710 | ReasonToReject = 6; // is in a blacklisted file. |
| 3711 | else if (Blacklist.isBlacklistedType(getQualifiedNameAsString(), |
| 3712 | "field-padding")) |
| 3713 | ReasonToReject = 7; // is blacklisted. |
| 3714 | |
| 3715 | if (EmitRemark) { |
| 3716 | if (ReasonToReject >= 0) |
| 3717 | Context.getDiagnostics().Report( |
| 3718 | getLocation(), |
| 3719 | diag::remark_sanitize_address_insert_extra_padding_rejected) |
| 3720 | << getQualifiedNameAsString() << ReasonToReject; |
| 3721 | else |
| 3722 | Context.getDiagnostics().Report( |
| 3723 | getLocation(), |
| 3724 | diag::remark_sanitize_address_insert_extra_padding_accepted) |
| 3725 | << getQualifiedNameAsString(); |
| 3726 | } |
| 3727 | return ReasonToReject < 0; |
| 3728 | } |
| 3729 | |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 3730 | const FieldDecl *RecordDecl::findFirstNamedDataMember() const { |
| 3731 | for (const auto *I : fields()) { |
| 3732 | if (I->getIdentifier()) |
| 3733 | return I; |
| 3734 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3735 | if (const auto *RT = I->getType()->getAs<RecordType>()) |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 3736 | if (const FieldDecl *NamedDataMember = |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3737 | RT->getDecl()->findFirstNamedDataMember()) |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 3738 | return NamedDataMember; |
| 3739 | } |
| 3740 | |
| 3741 | // We didn't find a named data member. |
| 3742 | return nullptr; |
| 3743 | } |
| 3744 | |
| 3745 | |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 3746 | //===----------------------------------------------------------------------===// |
| 3747 | // BlockDecl Implementation |
| 3748 | //===----------------------------------------------------------------------===// |
| 3749 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3750 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3751 | assert(!ParamInfo && "Already has param info!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3752 | |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3753 | // Zero params -> null pointer. |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3754 | if (!NewParamInfo.empty()) { |
| 3755 | NumParams = NewParamInfo.size(); |
| 3756 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 3757 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3758 | } |
| 3759 | } |
| 3760 | |
Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 3761 | void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
| 3762 | bool CapturesCXXThis) { |
| 3763 | this->CapturesCXXThis = CapturesCXXThis; |
| 3764 | this->NumCaptures = Captures.size(); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3765 | |
Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 3766 | if (Captures.empty()) { |
| 3767 | this->Captures = nullptr; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 3768 | return; |
| 3769 | } |
| 3770 | |
Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 3771 | this->Captures = Captures.copy(Context).data(); |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 3772 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3773 | |
John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 3774 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 3775 | for (const auto &I : captures()) |
John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 3776 | // Only auto vars can be captured, so no redeclaration worries. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 3777 | if (I.getVariable() == variable) |
John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 3778 | return true; |
| 3779 | |
| 3780 | return false; |
| 3781 | } |
| 3782 | |
Douglas Gregor | 70226da | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 3783 | SourceRange BlockDecl::getSourceRange() const { |
| 3784 | return SourceRange(getLocation(), Body? Body->getLocEnd() : getLocation()); |
| 3785 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3786 | |
| 3787 | //===----------------------------------------------------------------------===// |
| 3788 | // Other Decl Allocation/Deallocation Method Implementations |
| 3789 | //===----------------------------------------------------------------------===// |
| 3790 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3791 | void TranslationUnitDecl::anchor() { } |
| 3792 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3793 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3794 | return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3795 | } |
| 3796 | |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 3797 | void ExternCContextDecl::anchor() { } |
| 3798 | |
| 3799 | ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, |
| 3800 | TranslationUnitDecl *DC) { |
| 3801 | return new (C, DC) ExternCContextDecl(DC); |
| 3802 | } |
| 3803 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3804 | void LabelDecl::anchor() { } |
| 3805 | |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3806 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3807 | SourceLocation IdentL, IdentifierInfo *II) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3808 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
| 3811 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 3812 | SourceLocation IdentL, IdentifierInfo *II, |
| 3813 | SourceLocation GnuLabelL) { |
| 3814 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3815 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 3816 | } |
| 3817 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3818 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3819 | return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, |
| 3820 | SourceLocation()); |
Douglas Gregor | 417e87c | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 3823 | void LabelDecl::setMSAsmLabel(StringRef Name) { |
| 3824 | char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; |
| 3825 | memcpy(Buffer, Name.data(), Name.size()); |
| 3826 | Buffer[Name.size()] = '\0'; |
| 3827 | MSAsmName = Buffer; |
| 3828 | } |
| 3829 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3830 | void ValueDecl::anchor() { } |
| 3831 | |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 3832 | bool ValueDecl::isWeak() const { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 3833 | for (const auto *I : attrs()) |
| 3834 | if (isa<WeakAttr>(I) || isa<WeakRefAttr>(I)) |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 3835 | return true; |
| 3836 | |
| 3837 | return isWeakImported(); |
| 3838 | } |
| 3839 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3840 | void ImplicitParamDecl::anchor() { } |
| 3841 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3842 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3843 | SourceLocation IdLoc, |
| 3844 | IdentifierInfo *Id, |
| 3845 | QualType Type) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3846 | return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3849 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3850 | unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3851 | return new (C, ID) ImplicitParamDecl(C, nullptr, SourceLocation(), nullptr, |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3852 | QualType()); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3855 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3856 | SourceLocation StartLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3857 | const DeclarationNameInfo &NameInfo, |
| 3858 | QualType T, TypeSourceInfo *TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3859 | StorageClass SC, |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3860 | bool isInlineSpecified, |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 3861 | bool hasWrittenPrototype, |
| 3862 | bool isConstexprSpecified) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3863 | FunctionDecl *New = |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3864 | new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo, |
| 3865 | SC, isInlineSpecified, isConstexprSpecified); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3866 | New->HasWrittenPrototype = hasWrittenPrototype; |
| 3867 | return New; |
| 3868 | } |
| 3869 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3870 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3871 | return new (C, ID) FunctionDecl(Function, C, nullptr, SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3872 | DeclarationNameInfo(), QualType(), nullptr, |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3873 | SC_None, false, false); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3876 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3877 | return new (C, DC) BlockDecl(DC, L); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3880 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3881 | return new (C, ID) BlockDecl(nullptr, SourceLocation()); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 3884 | CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, |
| 3885 | unsigned NumParams) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3886 | return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3887 | CapturedDecl(DC, NumParams); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 3890 | CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3891 | unsigned NumParams) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3892 | return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3893 | CapturedDecl(nullptr, NumParams); |
Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3896 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 3897 | SourceLocation L, |
| 3898 | IdentifierInfo *Id, QualType T, |
| 3899 | Expr *E, const llvm::APSInt &V) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3900 | return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3903 | EnumConstantDecl * |
| 3904 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3905 | return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr, |
| 3906 | QualType(), nullptr, llvm::APSInt()); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3909 | void IndirectFieldDecl::anchor() { } |
| 3910 | |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 3911 | IndirectFieldDecl * |
| 3912 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| 3913 | IdentifierInfo *Id, QualType T, NamedDecl **CH, |
| 3914 | unsigned CHS) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3915 | return new (C, DC) IndirectFieldDecl(DC, L, Id, T, CH, CHS); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3916 | } |
| 3917 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3918 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 3919 | unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3920 | return new (C, ID) IndirectFieldDecl(nullptr, SourceLocation(), |
| 3921 | DeclarationName(), QualType(), nullptr, |
| 3922 | 0); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 3925 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 3926 | SourceLocation End = getLocation(); |
| 3927 | if (Init) |
| 3928 | End = Init->getLocEnd(); |
| 3929 | return SourceRange(getLocation(), End); |
| 3930 | } |
| 3931 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3932 | void TypeDecl::anchor() { } |
| 3933 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3934 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 3935 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3936 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3937 | return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3940 | void TypedefNameDecl::anchor() { } |
| 3941 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 3942 | TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { |
| 3943 | if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { |
| 3944 | auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); |
| 3945 | auto *ThisTypedef = this; |
| 3946 | if (AnyRedecl && OwningTypedef) { |
| 3947 | OwningTypedef = OwningTypedef->getCanonicalDecl(); |
| 3948 | ThisTypedef = ThisTypedef->getCanonicalDecl(); |
| 3949 | } |
| 3950 | if (OwningTypedef == ThisTypedef) |
Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 3951 | return TT->getDecl(); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 3952 | } |
Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 3953 | |
| 3954 | return nullptr; |
| 3955 | } |
| 3956 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3957 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3958 | return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3959 | nullptr, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3962 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 3963 | SourceLocation StartLoc, |
| 3964 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 3965 | TypeSourceInfo *TInfo) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3966 | return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3967 | } |
| 3968 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3969 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3970 | return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), |
| 3971 | SourceLocation(), nullptr, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3974 | SourceRange TypedefDecl::getSourceRange() const { |
| 3975 | SourceLocation RangeEnd = getLocation(); |
| 3976 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 3977 | if (typeIsPostfix(TInfo->getType())) |
| 3978 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 3979 | } |
| 3980 | return SourceRange(getLocStart(), RangeEnd); |
| 3981 | } |
| 3982 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3983 | SourceRange TypeAliasDecl::getSourceRange() const { |
| 3984 | SourceLocation RangeEnd = getLocStart(); |
| 3985 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 3986 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 3987 | return SourceRange(getLocStart(), RangeEnd); |
| 3988 | } |
| 3989 | |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3990 | void FileScopeAsmDecl::anchor() { } |
| 3991 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3992 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 3993 | StringLiteral *Str, |
| 3994 | SourceLocation AsmLoc, |
| 3995 | SourceLocation RParenLoc) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3996 | return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3997 | } |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 3998 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3999 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4000 | unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4001 | return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), |
| 4002 | SourceLocation()); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4003 | } |
| 4004 | |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4005 | void EmptyDecl::anchor() {} |
| 4006 | |
| 4007 | EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4008 | return new (C, DC) EmptyDecl(DC, L); |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
| 4011 | EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4012 | return new (C, ID) EmptyDecl(nullptr, SourceLocation()); |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4013 | } |
| 4014 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4015 | //===----------------------------------------------------------------------===// |
| 4016 | // ImportDecl Implementation |
| 4017 | //===----------------------------------------------------------------------===// |
| 4018 | |
| 4019 | /// \brief Retrieve the number of module identifiers needed to name the given |
| 4020 | /// module. |
| 4021 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 4022 | unsigned Result = 1; |
| 4023 | while (Mod->Parent) { |
| 4024 | Mod = Mod->Parent; |
| 4025 | ++Result; |
| 4026 | } |
| 4027 | return Result; |
| 4028 | } |
| 4029 | |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4030 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4031 | Module *Imported, |
| 4032 | ArrayRef<SourceLocation> IdentifierLocs) |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4033 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true), |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 4034 | NextLocalImport() |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4035 | { |
| 4036 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4037 | auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4038 | std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(), |
| 4039 | StoredLocs); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4042 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4043 | Module *Imported, SourceLocation EndLoc) |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4044 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false), |
Douglas Gregor | 0f2a360 | 2011-12-03 00:30:27 +0000 | [diff] [blame] | 4045 | NextLocalImport() |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4046 | { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4047 | *getTrailingObjects<SourceLocation>() = EndLoc; |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4050 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4051 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4052 | ArrayRef<SourceLocation> IdentifierLocs) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4053 | return new (C, DC, |
| 4054 | additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size())) |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4055 | ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4058 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4059 | SourceLocation StartLoc, |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4060 | Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4061 | SourceLocation EndLoc) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4062 | ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1)) |
| 4063 | ImportDecl(DC, StartLoc, Imported, EndLoc); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4064 | Import->setImplicit(); |
| 4065 | return Import; |
| 4066 | } |
| 4067 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4068 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 4069 | unsigned NumLocations) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4070 | return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations)) |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4071 | ImportDecl(EmptyShell()); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 4075 | if (!ImportedAndComplete.getInt()) |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4076 | return None; |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4077 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4078 | const auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4079 | return llvm::makeArrayRef(StoredLocs, |
| 4080 | getNumModuleIdentifiers(getImportedModule())); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | SourceRange ImportDecl::getSourceRange() const { |
| 4084 | if (!ImportedAndComplete.getInt()) |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4085 | return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>()); |
| 4086 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4087 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 4088 | } |