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