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