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