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