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