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