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