| 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 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| Argyrios Kyrtzidis | 6301884 | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 9 | // This file implements the Decl subclasses. |
| Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/Decl.h" |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 14 | #include "Linkage.h" |
| Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| Richard Trieu | 0f25c74 | 2018-12-14 03:35:10 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTDiagnostic.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" |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 29 | #include "clang/AST/ODRHash.h" |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 30 | #include "clang/AST/PrettyDeclStackTrace.h" |
| Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 31 | #include "clang/AST/PrettyPrinter.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 32 | #include "clang/AST/Redeclarable.h" |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 33 | #include "clang/AST/Stmt.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 34 | #include "clang/AST/TemplateBase.h" |
| 35 | #include "clang/AST/Type.h" |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 36 | #include "clang/AST/TypeLoc.h" |
| Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 37 | #include "clang/Basic/Builtins.h" |
| Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 38 | #include "clang/Basic/IdentifierTable.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 39 | #include "clang/Basic/LLVM.h" |
| 40 | #include "clang/Basic/LangOptions.h" |
| 41 | #include "clang/Basic/Linkage.h" |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 42 | #include "clang/Basic/Module.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 43 | #include "clang/Basic/PartialDiagnostic.h" |
| 44 | #include "clang/Basic/SanitizerBlacklist.h" |
| 45 | #include "clang/Basic/Sanitizers.h" |
| 46 | #include "clang/Basic/SourceLocation.h" |
| 47 | #include "clang/Basic/SourceManager.h" |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 48 | #include "clang/Basic/Specifiers.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 49 | #include "clang/Basic/TargetCXXABI.h" |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 50 | #include "clang/Basic/TargetInfo.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 51 | #include "clang/Basic/Visibility.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/APSInt.h" |
| 53 | #include "llvm/ADT/ArrayRef.h" |
| 54 | #include "llvm/ADT/None.h" |
| 55 | #include "llvm/ADT/Optional.h" |
| 56 | #include "llvm/ADT/STLExtras.h" |
| 57 | #include "llvm/ADT/SmallVector.h" |
| 58 | #include "llvm/ADT/StringSwitch.h" |
| 59 | #include "llvm/ADT/StringRef.h" |
| 60 | #include "llvm/ADT/Triple.h" |
| 61 | #include "llvm/Support/Casting.h" |
| John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 62 | #include "llvm/Support/ErrorHandling.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 63 | #include "llvm/Support/raw_ostream.h" |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 64 | #include <algorithm> |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 65 | #include <cassert> |
| 66 | #include <cstddef> |
| 67 | #include <cstring> |
| 68 | #include <memory> |
| 69 | #include <string> |
| 70 | #include <tuple> |
| 71 | #include <type_traits> |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 72 | |
| Chris Lattner | 6d9a685 | 2006-10-25 05:11:20 +0000 | [diff] [blame] | 73 | using namespace clang; |
| Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 74 | |
| Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 75 | Decl *clang::getPrimaryMergedDecl(Decl *D) { |
| 76 | return D->getASTContext().getPrimaryMergedDecl(D); |
| 77 | } |
| 78 | |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 79 | void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { |
| 80 | SourceLocation Loc = this->Loc; |
| 81 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
| 82 | if (Loc.isValid()) { |
| 83 | Loc.print(OS, Context.getSourceManager()); |
| 84 | OS << ": "; |
| 85 | } |
| 86 | OS << Message; |
| 87 | |
| 88 | if (auto *ND = dyn_cast_or_null<NamedDecl>(TheDecl)) { |
| 89 | OS << " '"; |
| 90 | ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true); |
| 91 | OS << "'"; |
| 92 | } |
| 93 | |
| 94 | OS << '\n'; |
| 95 | } |
| 96 | |
| Richard Smith | 73b21d8 | 2014-09-03 02:33:22 +0000 | [diff] [blame] | 97 | // Defined here so that it can be inlined into its direct callers. |
| 98 | bool Decl::isOutOfLine() const { |
| 99 | return !getLexicalDeclContext()->Equals(getDeclContext()); |
| 100 | } |
| 101 | |
| Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 102 | TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx) |
| 103 | : Decl(TranslationUnit, nullptr, SourceLocation()), |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 104 | DeclContext(TranslationUnit), Ctx(ctx) {} |
| Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 105 | |
| Chris Lattner | 88f70d6 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 107 | // NamedDecl Implementation |
| Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 108 | //===----------------------------------------------------------------------===// |
| 109 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 110 | // Visibility rules aren't rigorously externally specified, but here |
| 111 | // are the basic principles behind what we implement: |
| 112 | // |
| 113 | // 1. An explicit visibility attribute is generally a direct expression |
| 114 | // of the user's intent and should be honored. Only the innermost |
| 115 | // visibility attribute applies. If no visibility attribute applies, |
| 116 | // global visibility settings are considered. |
| 117 | // |
| 118 | // 2. There is one caveat to the above: on or in a template pattern, |
| 119 | // an explicit visibility attribute is just a default rule, and |
| 120 | // visibility can be decreased by the visibility of template |
| 121 | // arguments. But this, too, has an exception: an attribute on an |
| 122 | // explicit specialization or instantiation causes all the visibility |
| 123 | // restrictions of the template arguments to be ignored. |
| 124 | // |
| 125 | // 3. A variable that does not otherwise have explicit visibility can |
| 126 | // be restricted by the visibility of its type. |
| 127 | // |
| 128 | // 4. A visibility restriction is explicit if it comes from an |
| 129 | // attribute (or something like it), not a global visibility setting. |
| 130 | // When emitting a reference to an external symbol, visibility |
| 131 | // restrictions are ignored unless they are explicit. |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 132 | // |
| 133 | // 5. When computing the visibility of a non-type, including a |
| 134 | // non-type member of a class, only non-type visibility restrictions |
| 135 | // are considered: the 'visibility' attribute, global value-visibility |
| 136 | // settings, and a few special cases like __private_extern. |
| 137 | // |
| 138 | // 6. When computing the visibility of a type, including a type member |
| 139 | // of a class, only type visibility restrictions are considered: |
| 140 | // the 'type_visibility' attribute and global type-visibility settings. |
| 141 | // However, a 'visibility' attribute counts as a 'type_visibility' |
| 142 | // attribute on any declaration that only has the former. |
| 143 | // |
| 144 | // The visibility of a "secondary" entity, like a template argument, |
| 145 | // is computed using the kind of that entity, not the kind of the |
| 146 | // primary entity for which we are computing visibility. For example, |
| 147 | // the visibility of a specialization of either of these templates: |
| 148 | // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); |
| 149 | // template <class T, bool (&compare)(T, X)> class matcher; |
| 150 | // is restricted according to the type visibility of the argument 'T', |
| 151 | // the type visibility of 'bool(&)(T,X)', and the value visibility of |
| 152 | // the argument function 'compare'. That 'has_match' is a value |
| 153 | // and 'matcher' is a type only matters when looking for attributes |
| 154 | // and settings from the immediate context. |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 155 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 156 | /// Does this computation kind permit us to consider additional |
| 157 | /// visibility settings from attributes and the like? |
| 158 | static bool hasExplicitVisibilityAlready(LVComputationKind computation) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 159 | return computation.IgnoreExplicitVisibility; |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /// Given an LVComputationKind, return one of the same type/value sort |
| 163 | /// that records that it already has explicit visibility. |
| 164 | static LVComputationKind |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 165 | withExplicitVisibilityAlready(LVComputationKind Kind) { |
| 166 | Kind.IgnoreExplicitVisibility = true; |
| 167 | return Kind; |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 170 | static Optional<Visibility> getExplicitVisibility(const NamedDecl *D, |
| 171 | LVComputationKind kind) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 172 | assert(!kind.IgnoreExplicitVisibility && |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 173 | "asking for explicit visibility when we shouldn't be"); |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 174 | return D->getExplicitVisibility(kind.getExplicitVisibilityKind()); |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 177 | /// Is the given declaration a "type" or a "value" for the purposes of |
| 178 | /// visibility computation? |
| 179 | static bool usesTypeVisibility(const NamedDecl *D) { |
| John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 180 | return isa<TypeDecl>(D) || |
| 181 | isa<ClassTemplateDecl>(D) || |
| 182 | isa<ObjCInterfaceDecl>(D); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 185 | /// Does the given declaration have member specialization information, |
| 186 | /// and if so, is it an explicit specialization? |
| 187 | template <class T> static typename |
| Benjamin Kramer | ed2f476 | 2014-03-07 14:30:23 +0000 | [diff] [blame] | 188 | std::enable_if<!std::is_base_of<RedeclarableTemplateDecl, T>::value, bool>::type |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 189 | isExplicitMemberSpecialization(const T *D) { |
| 190 | if (const MemberSpecializationInfo *member = |
| 191 | D->getMemberSpecializationInfo()) { |
| 192 | return member->isExplicitSpecialization(); |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | /// For templates, this question is easier: a member template can't be |
| 198 | /// explicitly instantiated, so there's a single bit indicating whether |
| 199 | /// or not this is an explicit member specialization. |
| 200 | static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { |
| 201 | return D->isMemberSpecialization(); |
| 202 | } |
| 203 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 204 | /// Given a visibility attribute, return the explicit visibility |
| 205 | /// associated with it. |
| 206 | template <class T> |
| 207 | static Visibility getVisibilityFromAttr(const T *attr) { |
| 208 | switch (attr->getVisibility()) { |
| 209 | case T::Default: |
| 210 | return DefaultVisibility; |
| 211 | case T::Hidden: |
| 212 | return HiddenVisibility; |
| 213 | case T::Protected: |
| 214 | return ProtectedVisibility; |
| 215 | } |
| 216 | llvm_unreachable("bad visibility kind"); |
| 217 | } |
| 218 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 219 | /// Return the explicit visibility of the given declaration. |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 220 | static Optional<Visibility> getVisibilityOf(const NamedDecl *D, |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 221 | NamedDecl::ExplicitVisibilityKind kind) { |
| 222 | // If we're ultimately computing the visibility of a type, look for |
| 223 | // a 'type_visibility' attribute before looking for 'visibility'. |
| 224 | if (kind == NamedDecl::VisibilityForType) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 225 | if (const auto *A = D->getAttr<TypeVisibilityAttr>()) { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 226 | return getVisibilityFromAttr(A); |
| 227 | } |
| 228 | } |
| 229 | |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 230 | // If this declaration has an explicit visibility attribute, use it. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 231 | if (const auto *A = D->getAttr<VisibilityAttr>()) { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 232 | return getVisibilityFromAttr(A); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 233 | } |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 234 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 235 | return None; |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 238 | LinkageInfo LinkageComputer::getLVForType(const Type &T, |
| 239 | LVComputationKind computation) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 240 | if (computation.IgnoreAllVisibility) |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 241 | return LinkageInfo(T.getLinkage(), DefaultVisibility, true); |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 242 | return getTypeLinkageAndVisibility(&T); |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 245 | /// Get the most restrictive linkage for the types in the given |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 246 | /// template parameter list. For visibility purposes, template |
| 247 | /// parameters are part of the signature of a template. |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 248 | LinkageInfo LinkageComputer::getLVForTemplateParameterList( |
| 249 | const TemplateParameterList *Params, LVComputationKind computation) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 250 | LinkageInfo LV; |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 251 | for (const NamedDecl *P : *Params) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 252 | // Template type parameters are the most common and never |
| 253 | // contribute to visibility, pack or not. |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 254 | if (isa<TemplateTypeParmDecl>(P)) |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 255 | continue; |
| 256 | |
| 257 | // Non-type template parameters can be restricted by the value type, e.g. |
| 258 | // template <enum X> class A { ... }; |
| 259 | // We have to be careful here, though, because we can be dealing with |
| 260 | // dependent types. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 261 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 262 | // Handle the non-pack case first. |
| 263 | if (!NTTP->isExpandedParameterPack()) { |
| 264 | if (!NTTP->getType()->isDependentType()) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 265 | LV.merge(getLVForType(*NTTP->getType(), computation)); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 266 | } |
| 267 | continue; |
| 268 | } |
| Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 269 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 270 | // Look at all the types in an expanded pack. |
| 271 | for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { |
| 272 | QualType type = NTTP->getExpansionType(i); |
| 273 | if (!type->isDependentType()) |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 274 | LV.merge(getTypeLinkageAndVisibility(type)); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 275 | } |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 276 | continue; |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 277 | } |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 278 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 279 | // Template template parameters can be restricted by their |
| 280 | // template parameters, recursively. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 281 | const auto *TTP = cast<TemplateTemplateParmDecl>(P); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 282 | |
| 283 | // Handle the non-pack case first. |
| 284 | if (!TTP->isExpandedParameterPack()) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 285 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(), |
| 286 | computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | |
| 290 | // Look at all expansions in an expanded pack. |
| 291 | for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); |
| 292 | i != n; ++i) { |
| 293 | LV.merge(getLVForTemplateParameterList( |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 294 | TTP->getExpansionTemplateParameters(i), computation)); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 298 | return LV; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 301 | static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 302 | const Decl *Ret = nullptr; |
| Rafael Espindola | c1b38a2 | 2013-05-16 04:30:21 +0000 | [diff] [blame] | 303 | const DeclContext *DC = D->getDeclContext(); |
| 304 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 305 | if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC)) |
| 306 | Ret = cast<Decl>(DC); |
| Rafael Espindola | c1b38a2 | 2013-05-16 04:30:21 +0000 | [diff] [blame] | 307 | DC = DC->getParent(); |
| 308 | } |
| 309 | return Ret; |
| 310 | } |
| 311 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 312 | /// Get the most restrictive linkage for the types and |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 313 | /// declarations in the given template argument list. |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 314 | /// |
| 315 | /// Note that we don't take an LVComputationKind because we always |
| 316 | /// 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] | 317 | LinkageInfo |
| 318 | LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, |
| 319 | LVComputationKind computation) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 320 | LinkageInfo LV; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 321 | |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 322 | for (const TemplateArgument &Arg : Args) { |
| 323 | switch (Arg.getKind()) { |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 324 | case TemplateArgument::Null: |
| 325 | case TemplateArgument::Integral: |
| 326 | case TemplateArgument::Expression: |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 327 | continue; |
| Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 328 | |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 329 | case TemplateArgument::Type: |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 330 | LV.merge(getLVForType(*Arg.getAsType(), computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 331 | continue; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 332 | |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 333 | case TemplateArgument::Declaration: { |
| 334 | const NamedDecl *ND = Arg.getAsDecl(); |
| 335 | assert(!usesTypeVisibility(ND)); |
| 336 | LV.merge(getLVForDecl(ND, computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 337 | continue; |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 338 | } |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 339 | |
| 340 | case TemplateArgument::NullPtr: |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 341 | LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType())); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 342 | continue; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 343 | |
| 344 | case TemplateArgument::Template: |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 345 | case TemplateArgument::TemplateExpansion: |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 346 | if (TemplateDecl *Template = |
| 347 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 348 | LV.merge(getLVForDecl(Template, computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 349 | continue; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 350 | |
| 351 | case TemplateArgument::Pack: |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 352 | LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 353 | continue; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 354 | } |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 355 | llvm_unreachable("bad template argument kind"); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 358 | return LV; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 361 | LinkageInfo |
| 362 | LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
| 363 | LVComputationKind computation) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 364 | return getLVForTemplateArgumentList(TArgs.asArray(), computation); |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 367 | static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, |
| 368 | const FunctionTemplateSpecializationInfo *specInfo) { |
| 369 | // Include visibility from the template parameters and arguments |
| 370 | // only if this is not an explicit instantiation or specialization |
| 371 | // with direct explicit visibility. (Implicit instantiations won't |
| 372 | // have a direct attribute.) |
| 373 | if (!specInfo->isExplicitInstantiationOrSpecialization()) |
| 374 | return true; |
| 375 | |
| 376 | return !fn->hasAttr<VisibilityAttr>(); |
| 377 | } |
| 378 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 379 | /// Merge in template-related linkage and visibility for the given |
| 380 | /// function template specialization. |
| 381 | /// |
| 382 | /// We don't need a computation kind here because we can assume |
| 383 | /// LVForValue. |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 384 | /// |
| NAKAMURA Takumi | 62eae08 | 2013-02-22 04:06:28 +0000 | [diff] [blame] | 385 | /// \param[out] LV the computation to use for the parent |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 386 | void LinkageComputer::mergeTemplateLV( |
| 387 | LinkageInfo &LV, const FunctionDecl *fn, |
| 388 | const FunctionTemplateSpecializationInfo *specInfo, |
| 389 | LVComputationKind computation) { |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 390 | bool considerVisibility = |
| 391 | shouldConsiderTemplateVisibility(fn, specInfo); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 392 | |
| 393 | // Merge information from the template parameters. |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 394 | FunctionTemplateDecl *temp = specInfo->getTemplate(); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 395 | LinkageInfo tempLV = |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 396 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 397 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 398 | |
| 399 | // Merge information from the template arguments. |
| 400 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 401 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 402 | LV.mergeMaybeWithVisibility(argsLV, considerVisibility); |
| John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 405 | /// Does the given declaration have a direct visibility attribute |
| 406 | /// that would match the given rules? |
| 407 | static bool hasDirectVisibilityAttribute(const NamedDecl *D, |
| 408 | LVComputationKind computation) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 409 | if (computation.IgnoreAllVisibility) |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 410 | return false; |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 411 | |
| 412 | return (computation.isTypeVisibility() && D->hasAttr<TypeVisibilityAttr>()) || |
| 413 | D->hasAttr<VisibilityAttr>(); |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 416 | /// Should we consider visibility associated with the template |
| 417 | /// arguments and parameters of the given class template specialization? |
| 418 | static bool shouldConsiderTemplateVisibility( |
| 419 | const ClassTemplateSpecializationDecl *spec, |
| 420 | LVComputationKind computation) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 421 | // Include visibility from the template parameters and arguments |
| 422 | // only if this is not an explicit instantiation or specialization |
| 423 | // with direct explicit visibility (and note that implicit |
| 424 | // instantiations won't have a direct attribute). |
| 425 | // |
| 426 | // Furthermore, we want to ignore template parameters and arguments |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 427 | // for an explicit specialization when computing the visibility of a |
| 428 | // member thereof with explicit visibility. |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 429 | // |
| 430 | // This is a bit complex; let's unpack it. |
| 431 | // |
| 432 | // An explicit class specialization is an independent, top-level |
| 433 | // declaration. As such, if it or any of its members has an |
| 434 | // explicit visibility attribute, that must directly express the |
| 435 | // user's intent, and we should honor it. The same logic applies to |
| 436 | // an explicit instantiation of a member of such a thing. |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 437 | |
| 438 | // Fast path: if this is not an explicit instantiation or |
| 439 | // specialization, we always want to consider template-related |
| 440 | // visibility restrictions. |
| 441 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 442 | return true; |
| 443 | |
| 444 | // This is the 'member thereof' check. |
| 445 | if (spec->isExplicitSpecialization() && |
| 446 | hasExplicitVisibilityAlready(computation)) |
| 447 | return false; |
| 448 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 449 | return !hasDirectVisibilityAttribute(spec, computation); |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /// Merge in template-related linkage and visibility for the given |
| 453 | /// class template specialization. |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 454 | void LinkageComputer::mergeTemplateLV( |
| 455 | LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec, |
| 456 | LVComputationKind computation) { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 457 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 458 | |
| 459 | // Merge information from the template parameters, but ignore |
| 460 | // visibility if we're only considering template arguments. |
| 461 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 462 | ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 463 | LinkageInfo tempLV = |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 464 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 465 | LV.mergeMaybeWithVisibility(tempLV, |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 466 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 467 | |
| 468 | // Merge information from the template arguments. We ignore |
| 469 | // template-argument visibility if we've got an explicit |
| 470 | // instantiation with a visibility attribute. |
| 471 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 472 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
| Rafael Espindola | 503276b | 2013-05-30 21:23:15 +0000 | [diff] [blame] | 473 | if (considerVisibility) |
| 474 | LV.mergeVisibility(argsLV); |
| 475 | LV.mergeExternalVisibility(argsLV); |
| John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 478 | /// Should we consider visibility associated with the template |
| 479 | /// arguments and parameters of the given variable template |
| 480 | /// specialization? As usual, follow class template specialization |
| 481 | /// logic up to initialization. |
| 482 | static bool shouldConsiderTemplateVisibility( |
| 483 | const VarTemplateSpecializationDecl *spec, |
| 484 | LVComputationKind computation) { |
| 485 | // Include visibility from the template parameters and arguments |
| 486 | // only if this is not an explicit instantiation or specialization |
| 487 | // with direct explicit visibility (and note that implicit |
| 488 | // instantiations won't have a direct attribute). |
| 489 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 490 | return true; |
| 491 | |
| 492 | // An explicit variable specialization is an independent, top-level |
| 493 | // declaration. As such, if it has an explicit visibility attribute, |
| 494 | // that must directly express the user's intent, and we should honor |
| 495 | // it. |
| 496 | if (spec->isExplicitSpecialization() && |
| 497 | hasExplicitVisibilityAlready(computation)) |
| 498 | return false; |
| 499 | |
| 500 | return !hasDirectVisibilityAttribute(spec, computation); |
| 501 | } |
| 502 | |
| 503 | /// Merge in template-related linkage and visibility for the given |
| 504 | /// variable template specialization. As usual, follow class template |
| 505 | /// specialization logic up to initialization. |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 506 | void LinkageComputer::mergeTemplateLV(LinkageInfo &LV, |
| 507 | const VarTemplateSpecializationDecl *spec, |
| 508 | LVComputationKind computation) { |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 509 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
| 510 | |
| 511 | // Merge information from the template parameters, but ignore |
| 512 | // visibility if we're only considering template arguments. |
| 513 | |
| 514 | VarTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 515 | LinkageInfo tempLV = |
| 516 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| 517 | LV.mergeMaybeWithVisibility(tempLV, |
| 518 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
| 519 | |
| 520 | // Merge information from the template arguments. We ignore |
| 521 | // template-argument visibility if we've got an explicit |
| 522 | // instantiation with a visibility attribute. |
| 523 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| 524 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
| 525 | if (considerVisibility) |
| 526 | LV.mergeVisibility(argsLV); |
| 527 | LV.mergeExternalVisibility(argsLV); |
| 528 | } |
| 529 | |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 530 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
| 531 | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
| Rafael Espindola | 5cc7890 | 2012-07-13 23:26:43 +0000 | [diff] [blame] | 532 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 533 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 534 | return false; |
| 535 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 536 | const auto *FD = dyn_cast<FunctionDecl>(D); |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 537 | if (!FD) |
| 538 | return false; |
| 539 | |
| 540 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 541 | if (FunctionTemplateSpecializationInfo *spec |
| 542 | = FD->getTemplateSpecializationInfo()) { |
| 543 | TSK = spec->getTemplateSpecializationKind(); |
| 544 | } else if (MemberSpecializationInfo *MSI = |
| 545 | FD->getMemberSpecializationInfo()) { |
| 546 | TSK = MSI->getTemplateSpecializationKind(); |
| 547 | } |
| 548 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 549 | const FunctionDecl *Def = nullptr; |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 550 | // InlineVisibilityHidden only applies to definitions, and |
| 551 | // isInlined() only gives meaningful answers on definitions |
| 552 | // anyway. |
| 553 | return TSK != TSK_ExplicitInstantiationDeclaration && |
| 554 | TSK != TSK_ExplicitInstantiationDefinition && |
| Rafael Espindola | fb9d4b4 | 2012-10-11 16:32:25 +0000 | [diff] [blame] | 555 | FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 558 | template <typename T> static bool isFirstInExternCContext(T *D) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 559 | const T *First = D->getFirstDecl(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 560 | return First->isInExternCContext(); |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 563 | static bool isSingleLineLanguageLinkage(const Decl &D) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 564 | if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext())) |
| Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 565 | if (!SD->hasBraces()) |
| Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 566 | return true; |
| 567 | return false; |
| 568 | } |
| 569 | |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 570 | /// Determine whether D is declared in the purview of a named module. |
| 571 | static bool isInModulePurview(const NamedDecl *D) { |
| 572 | if (auto *M = D->getOwningModule()) |
| 573 | return M->isModulePurview(); |
| 574 | return false; |
| 575 | } |
| 576 | |
| Ali Tamur | b6a8a6c | 2019-04-19 02:15:57 +0000 | [diff] [blame] | 577 | static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 578 | // FIXME: Handle isModulePrivate. |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 579 | switch (D->getModuleOwnershipKind()) { |
| 580 | case Decl::ModuleOwnershipKind::Unowned: |
| 581 | case Decl::ModuleOwnershipKind::ModulePrivate: |
| 582 | return false; |
| 583 | case Decl::ModuleOwnershipKind::Visible: |
| 584 | case Decl::ModuleOwnershipKind::VisibleWhenImported: |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 585 | return isInModulePurview(D); |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 586 | } |
| 587 | llvm_unreachable("unexpected module ownership kind"); |
| 588 | } |
| 589 | |
| 590 | static LinkageInfo getInternalLinkageFor(const NamedDecl *D) { |
| 591 | // Internal linkage declarations within a module interface unit are modeled |
| 592 | // as "module-internal linkage", which means that they have internal linkage |
| 593 | // formally but can be indirectly accessed from outside the module via inline |
| 594 | // functions and templates defined within the module. |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 595 | if (isInModulePurview(D)) |
| 596 | return LinkageInfo(ModuleInternalLinkage, DefaultVisibility, false); |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 597 | |
| 598 | return LinkageInfo::internal(); |
| 599 | } |
| 600 | |
| 601 | static LinkageInfo getExternalLinkageFor(const NamedDecl *D) { |
| 602 | // C++ Modules TS [basic.link]/6.8: |
| 603 | // - A name declared at namespace scope that does not have internal linkage |
| 604 | // by the previous rules and that is introduced by a non-exported |
| 605 | // declaration has module linkage. |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 606 | if (isInModulePurview(D) && |
| Ali Tamur | b6a8a6c | 2019-04-19 02:15:57 +0000 | [diff] [blame] | 607 | !isExportedFromModuleInterfaceUnit(cast<NamedDecl>(D->getCanonicalDecl()))) |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 608 | return LinkageInfo(ModuleLinkage, DefaultVisibility, false); |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 609 | |
| 610 | return LinkageInfo::external(); |
| 611 | } |
| 612 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 613 | LinkageInfo |
| 614 | LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 615 | LVComputationKind computation, |
| 616 | bool IgnoreVarTypeLinkage) { |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 617 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 618 | "Not a name having namespace scope"); |
| 619 | ASTContext &Context = D->getASTContext(); |
| 620 | |
| 621 | // C++ [basic.link]p3: |
| 622 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 623 | // is the name of |
| 624 | // - an object, reference, function or function template that is |
| 625 | // explicitly declared static; or, |
| 626 | // (This bullet corresponds to C99 6.2.2p3.) |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 627 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 628 | // Explicitly declared static. |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 629 | if (Var->getStorageClass() == SC_Static) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 630 | return getInternalLinkageFor(Var); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 631 | |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 632 | // - a non-inline, non-volatile object or reference that is explicitly |
| 633 | // declared const or constexpr and neither explicitly declared extern |
| 634 | // nor previously declared to have external linkage; or (there is no |
| 635 | // equivalent in C99) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 636 | // The C++ modules TS adds "non-exported" to this list. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 637 | if (Context.getLangOpts().CPlusPlus && |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 638 | Var->getType().isConstQualified() && |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 639 | !Var->getType().isVolatileQualified() && |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 640 | !Var->isInline() && |
| Ali Tamur | b6a8a6c | 2019-04-19 02:15:57 +0000 | [diff] [blame] | 641 | !isExportedFromModuleInterfaceUnit(Var)) { |
| Rafael Espindola | 985a3ab | 2013-04-03 19:22:20 +0000 | [diff] [blame] | 642 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
| Rafael Espindola | 985a3ab | 2013-04-03 19:22:20 +0000 | [diff] [blame] | 643 | if (PrevVar) |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 644 | return getLVForDecl(PrevVar, computation); |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 645 | |
| 646 | if (Var->getStorageClass() != SC_Extern && |
| Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 647 | Var->getStorageClass() != SC_PrivateExtern && |
| Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 648 | !isSingleLineLanguageLinkage(*Var)) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 649 | return getInternalLinkageFor(Var); |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; |
| 653 | PrevVar = PrevVar->getPreviousDecl()) { |
| 654 | if (PrevVar->getStorageClass() == SC_PrivateExtern && |
| 655 | Var->getStorageClass() == SC_None) |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 656 | return getDeclLinkageAndVisibility(PrevVar); |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 657 | // Explicitly declared static. |
| 658 | if (PrevVar->getStorageClass() == SC_Static) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 659 | return getInternalLinkageFor(Var); |
| Fariborz Jahanian | 8feee2d | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 660 | } |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 661 | } else if (const FunctionDecl *Function = D->getAsFunction()) { |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 662 | // C++ [temp]p4: |
| 663 | // A non-member function template can have internal linkage; any |
| 664 | // other template name shall have external linkage. |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 665 | |
| 666 | // Explicitly declared static. |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 667 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 668 | return getInternalLinkageFor(Function); |
| David Majnemer | 18fac3b | 2014-12-10 22:58:14 +0000 | [diff] [blame] | 669 | } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) { |
| 670 | // - a data member of an anonymous union. |
| 671 | const VarDecl *VD = IFD->getVarDecl(); |
| 672 | assert(VD && "Expected a VarDecl in this IndirectFieldDecl!"); |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 673 | return getLVForNamespaceScopeDecl(VD, computation, IgnoreVarTypeLinkage); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 674 | } |
| David Majnemer | 0eb8bbd | 2013-10-23 20:52:43 +0000 | [diff] [blame] | 675 | assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!"); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 676 | |
| Chandler Carruth | 9682a2fd | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 677 | if (D->isInAnonymousNamespace()) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 678 | const auto *Var = dyn_cast<VarDecl>(D); |
| 679 | const auto *Func = dyn_cast<FunctionDecl>(D); |
| Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 680 | // FIXME: The check for extern "C" here is not justified by the standard |
| 681 | // wording, but we retain it from the pre-DR1113 model to avoid breaking |
| 682 | // code. |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 683 | // |
| 684 | // C++11 [basic.link]p4: |
| 685 | // An unnamed namespace or a namespace declared directly or indirectly |
| 686 | // within an unnamed namespace has internal linkage. |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 687 | if ((!Var || !isFirstInExternCContext(Var)) && |
| 688 | (!Func || !isFirstInExternCContext(Func))) |
| Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 689 | return getInternalLinkageFor(D); |
| Chandler Carruth | 9682a2fd | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 690 | } |
| John McCall | b7139c4 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 691 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 692 | // Set up the defaults. |
| 693 | |
| 694 | // C99 6.2.2p5: |
| 695 | // If the declaration of an identifier for an object has file |
| 696 | // scope and no storage-class specifier, its linkage is |
| 697 | // external. |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 698 | LinkageInfo LV = getExternalLinkageFor(D); |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 699 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 700 | if (!hasExplicitVisibilityAlready(computation)) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 701 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) { |
| Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 702 | LV.mergeVisibility(*Vis, true); |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 703 | } else { |
| 704 | // If we're declared in a namespace with a visibility attribute, |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 705 | // use that namespace's visibility, and it still counts as explicit. |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 706 | for (const DeclContext *DC = D->getDeclContext(); |
| 707 | !isa<TranslationUnitDecl>(DC); |
| 708 | DC = DC->getParent()) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 709 | const auto *ND = dyn_cast<NamespaceDecl>(DC); |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 710 | if (!ND) continue; |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 711 | if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) { |
| Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 712 | LV.mergeVisibility(*Vis, true); |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | } |
| 716 | } |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 717 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 718 | // 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] | 719 | if (!LV.isVisibilityExplicit()) { |
| John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 720 | // Use global type/value visibility as appropriate. |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 721 | Visibility globalVisibility = |
| 722 | computation.isValueVisibility() |
| 723 | ? Context.getLangOpts().getValueVisibilityMode() |
| 724 | : Context.getLangOpts().getTypeVisibilityMode(); |
| John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 725 | LV.mergeVisibility(globalVisibility, /*explicit*/ false); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 726 | |
| 727 | // If we're paying attention to global visibility, apply |
| 728 | // -finline-visibility-hidden if this is an inline method. |
| 729 | if (useInlineVisibilityHidden(D)) |
| Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 730 | LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 731 | } |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 732 | } |
| Rafael Espindola | af690f5 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 733 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 734 | // C++ [basic.link]p4: |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 735 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 736 | // A name having namespace scope has external linkage if it is the |
| 737 | // name of |
| 738 | // |
| 739 | // - an object or reference, unless it has internal linkage; or |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 740 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 741 | // GCC applies the following optimization to variables and static |
| 742 | // data members, but not to functions: |
| 743 | // |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 744 | // Modify the variable's LV by the LV of its type unless this is |
| 745 | // C or extern "C". This follows from [basic.link]p9: |
| 746 | // A type without linkage shall not be used as the type of a |
| 747 | // variable or function with external linkage unless |
| 748 | // - the entity has C language linkage, or |
| 749 | // - the entity is declared within an unnamed namespace, or |
| 750 | // - the entity is not used or is defined in the same |
| 751 | // translation unit. |
| 752 | // and [basic.link]p10: |
| 753 | // ...the types specified by all declarations referring to a |
| 754 | // given variable or function shall be identical... |
| 755 | // C does not have an equivalent rule. |
| 756 | // |
| John McCall | 5fe8412 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 757 | // Ignore this if we've got an explicit attribute; the user |
| 758 | // probably knows what they're doing. |
| 759 | // |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 760 | // Note that we don't want to make the variable non-external |
| 761 | // because of this, but unique-external linkage suits us. |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 762 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var) && |
| 763 | !IgnoreVarTypeLinkage) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 764 | LinkageInfo TypeLV = getLVForType(*Var->getType(), computation); |
| Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 765 | if (!isExternallyVisible(TypeLV.getLinkage())) |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 766 | return LinkageInfo::uniqueExternal(); |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 767 | if (!LV.isVisibilityExplicit()) |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 768 | LV.mergeVisibility(TypeLV); |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 771 | if (Var->getStorageClass() == SC_PrivateExtern) |
| Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 772 | LV.mergeVisibility(HiddenVisibility, true); |
| John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 773 | |
| Rafael Espindola | d5ed033 | 2012-11-12 04:10:23 +0000 | [diff] [blame] | 774 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 775 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't have |
| 776 | // to do it here. |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 777 | |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 778 | // As per function and class template specializations (below), |
| 779 | // consider LV for the template and template arguments. We're at file |
| 780 | // scope, so we do not need to worry about nested specializations. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 781 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) { |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 782 | mergeTemplateLV(LV, spec, computation); |
| 783 | } |
| 784 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 785 | // - a function, unless it has internal linkage; or |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 786 | } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
| John McCall | 2efaf11 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 787 | // In theory, we can modify the function's LV by the LV of its |
| 788 | // type unless it has C linkage (see comment above about variables |
| 789 | // for justification). In practice, GCC doesn't do this, so it's |
| 790 | // just too painful to make work. |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 791 | |
| John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 792 | if (Function->getStorageClass() == SC_PrivateExtern) |
| Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 793 | LV.mergeVisibility(HiddenVisibility, true); |
| John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 794 | |
| Rafael Espindola | a508c5d | 2012-11-21 02:47:19 +0000 | [diff] [blame] | 795 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 796 | // merging storage classes and visibility attributes, so we don't have to |
| 797 | // look at previous decls in here. |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 798 | |
| John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 799 | // In C++, then if the type of the function uses a type with |
| 800 | // unique-external linkage, it's not legally usable from outside |
| 801 | // this translation unit. However, we should use the C linkage |
| 802 | // rules instead for extern "C" declarations. |
| Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 803 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Function)) { |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 804 | // Only look at the type-as-written. Otherwise, deducing the return type |
| 805 | // of a function could change its linkage. |
| Richard Smith | 50f4afc | 2013-05-12 23:17:59 +0000 | [diff] [blame] | 806 | QualType TypeAsWritten = Function->getType(); |
| 807 | if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) |
| 808 | TypeAsWritten = TSI->getType(); |
| Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 809 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
| Richard Smith | 50f4afc | 2013-05-12 23:17:59 +0000 | [diff] [blame] | 810 | return LinkageInfo::uniqueExternal(); |
| 811 | } |
| John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 812 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 813 | // Consider LV from the template and the template arguments. |
| 814 | // We're at file scope, so we do not need to worry about nested |
| 815 | // specializations. |
| John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 816 | if (FunctionTemplateSpecializationInfo *specInfo |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 817 | = Function->getTemplateSpecializationInfo()) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 818 | mergeTemplateLV(LV, Function, specInfo, computation); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 821 | // - a named class (Clause 9), or an unnamed class defined in a |
| 822 | // typedef declaration in which the class has the typedef name |
| 823 | // for linkage purposes (7.1.3); or |
| 824 | // - a named enumeration (7.2), or an unnamed enumeration |
| 825 | // defined in a typedef declaration in which the enumeration |
| 826 | // has the typedef name for linkage purposes (7.1.3); or |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 827 | } else if (const auto *Tag = dyn_cast<TagDecl>(D)) { |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 828 | // Unnamed tags have no linkage. |
| John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 829 | if (!Tag->hasNameForLinkage()) |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 830 | return LinkageInfo::none(); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 831 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 832 | // If this is a class template specialization, consider the |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 833 | // linkage of the template and template arguments. We're at file |
| 834 | // scope, so we do not need to worry about nested specializations. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 835 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 836 | mergeTemplateLV(LV, spec, computation); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 837 | } |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 838 | |
| 839 | // - an enumerator belonging to an enumeration with external linkage; |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 840 | } else if (isa<EnumConstantDecl>(D)) { |
| Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 841 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 842 | computation); |
| Rafael Espindola | b97e896 | 2013-05-27 14:14:42 +0000 | [diff] [blame] | 843 | if (!isExternalFormalLinkage(EnumLV.getLinkage())) |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 844 | return LinkageInfo::none(); |
| 845 | LV.merge(EnumLV); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 846 | |
| 847 | // - a template, unless it is a function template that has |
| 848 | // internal linkage (Clause 14); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 849 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 850 | bool considerVisibility = !hasExplicitVisibilityAlready(computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 851 | LinkageInfo tempLV = |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 852 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 853 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 854 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 855 | // - a namespace (7.3), unless it is declared within an unnamed |
| 856 | // namespace. |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 857 | // |
| 858 | // We handled names in anonymous namespaces above. |
| 859 | } else if (isa<NamespaceDecl>(D)) { |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 860 | return LV; |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 861 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 862 | // By extension, we assign external linkage to Objective-C |
| 863 | // interfaces. |
| 864 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 865 | // fallout |
| 866 | |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 867 | } else if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 868 | // A typedef declaration has linkage if it gives a type a name for |
| 869 | // linkage purposes. |
| 870 | if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
| 871 | return LinkageInfo::none(); |
| 872 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 873 | // Everything not covered here has no linkage. |
| 874 | } else { |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 875 | return LinkageInfo::none(); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| Richard Smith | c445d5d | 2017-10-03 01:58:15 +0000 | [diff] [blame] | 878 | // If we ended up with non-externally-visible linkage, visibility should |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 879 | // always be default. |
| Richard Smith | c445d5d | 2017-10-03 01:58:15 +0000 | [diff] [blame] | 880 | if (!isExternallyVisible(LV.getLinkage())) |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 881 | return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 882 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 883 | return LV; |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 886 | LinkageInfo |
| 887 | LinkageComputer::getLVForClassMember(const NamedDecl *D, |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 888 | LVComputationKind computation, |
| 889 | bool IgnoreVarTypeLinkage) { |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 890 | // Only certain class members have linkage. Note that fields don't |
| 891 | // really have linkage, but it's convenient to say they do for the |
| 892 | // purposes of calculating linkage of pointer-to-data-member |
| 893 | // template arguments. |
| Richard Smith | 26d11be | 2014-01-08 01:51:59 +0000 | [diff] [blame] | 894 | // |
| 895 | // Templates also don't officially have linkage, but since we ignore |
| 896 | // the C++ standard and look at template arguments when determining |
| 897 | // linkage and visibility of a template specialization, we might hit |
| 898 | // a template template argument that way. If we do, we need to |
| 899 | // consider its linkage. |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 900 | if (!(isa<CXXMethodDecl>(D) || |
| 901 | isa<VarDecl>(D) || |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 902 | isa<FieldDecl>(D) || |
| David Majnemer | 0eb8bbd | 2013-10-23 20:52:43 +0000 | [diff] [blame] | 903 | isa<IndirectFieldDecl>(D) || |
| Richard Smith | 26d11be | 2014-01-08 01:51:59 +0000 | [diff] [blame] | 904 | isa<TagDecl>(D) || |
| 905 | isa<TemplateDecl>(D))) |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 906 | return LinkageInfo::none(); |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 907 | |
| John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 908 | LinkageInfo LV; |
| 909 | |
| John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 910 | // If we have an explicit visibility attribute, merge that in. |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 911 | if (!hasExplicitVisibilityAlready(computation)) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 912 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 913 | LV.mergeVisibility(*Vis, true); |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 914 | // If we're paying attention to global visibility, apply |
| 915 | // -finline-visibility-hidden if this is an inline method. |
| 916 | // |
| 917 | // Note that we do this before merging information about |
| 918 | // the class visibility. |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 919 | if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) |
| Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 920 | LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); |
| John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 921 | } |
| Rafael Espindola | 53cf219 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 922 | |
| 923 | // If this class member has an explicit visibility attribute, the only |
| 924 | // thing that can change its visibility is the template arguments, so |
| Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 925 | // only look for them when processing the class. |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 926 | LVComputationKind classComputation = computation; |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 927 | if (LV.isVisibilityExplicit()) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 928 | classComputation = withExplicitVisibilityAlready(computation); |
| Rafael Espindola | 505a7c8 | 2012-04-16 18:25:01 +0000 | [diff] [blame] | 929 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 930 | LinkageInfo classLV = |
| 931 | getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation); |
| Richard Smith | cdb06f2 | 2017-09-23 04:02:17 +0000 | [diff] [blame] | 932 | // The member has the same linkage as the class. If that's not externally |
| 933 | // visible, we don't need to compute anything about the linkage. |
| 934 | // FIXME: If we're only computing linkage, can we bail out here? |
| Rafael Espindola | e6190db | 2013-05-28 02:22:10 +0000 | [diff] [blame] | 935 | if (!isExternallyVisible(classLV.getLinkage())) |
| Richard Smith | cdb06f2 | 2017-09-23 04:02:17 +0000 | [diff] [blame] | 936 | return classLV; |
| Rafael Espindola | e6190db | 2013-05-28 02:22:10 +0000 | [diff] [blame] | 937 | |
| 938 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 939 | // Otherwise, don't merge in classLV yet, because in certain cases |
| 940 | // we need to completely ignore the visibility from it. |
| 941 | |
| 942 | // Specifically, if this decl exists and has an explicit attribute. |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 943 | const NamedDecl *explicitSpecSuppressor = nullptr; |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 944 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 945 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 946 | // Only look at the type-as-written. Otherwise, deducing the return type |
| 947 | // of a function could change its linkage. |
| 948 | QualType TypeAsWritten = MD->getType(); |
| 949 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 950 | TypeAsWritten = TSI->getType(); |
| 951 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
| 952 | return LinkageInfo::uniqueExternal(); |
| 953 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 954 | // If this is a method template specialization, use the linkage for |
| 955 | // the template parameters and arguments. |
| John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 956 | if (FunctionTemplateSpecializationInfo *spec |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 957 | = MD->getTemplateSpecializationInfo()) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 958 | mergeTemplateLV(LV, MD, spec, computation); |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 959 | if (spec->isExplicitSpecialization()) { |
| 960 | explicitSpecSuppressor = MD; |
| 961 | } else if (isExplicitMemberSpecialization(spec->getTemplate())) { |
| 962 | explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); |
| 963 | } |
| 964 | } else if (isExplicitMemberSpecialization(MD)) { |
| 965 | explicitSpecSuppressor = MD; |
| John McCall | e6e622e | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 966 | } |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 967 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 968 | } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 969 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 970 | mergeTemplateLV(LV, spec, computation); |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 971 | if (spec->isExplicitSpecialization()) { |
| 972 | explicitSpecSuppressor = spec; |
| 973 | } else { |
| 974 | const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 975 | if (isExplicitMemberSpecialization(temp)) { |
| 976 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 977 | } |
| 978 | } |
| 979 | } else if (isExplicitMemberSpecialization(RD)) { |
| 980 | explicitSpecSuppressor = RD; |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 983 | // Static data members. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 984 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 985 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD)) |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 986 | mergeTemplateLV(LV, spec, computation); |
| 987 | |
| John McCall | 36cd5cc | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 988 | // Modify the variable's linkage by its type, but ignore the |
| 989 | // type's visibility unless it's a definition. |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 990 | if (!IgnoreVarTypeLinkage) { |
| 991 | LinkageInfo typeLV = getLVForType(*VD->getType(), computation); |
| 992 | // FIXME: If the type's linkage is not externally visible, we can |
| 993 | // give this static data member UniqueExternalLinkage. |
| 994 | if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) |
| 995 | LV.mergeVisibility(typeLV); |
| 996 | LV.mergeExternalVisibility(typeLV); |
| 997 | } |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 998 | |
| 999 | if (isExplicitMemberSpecialization(VD)) { |
| 1000 | explicitSpecSuppressor = VD; |
| 1001 | } |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1002 | |
| 1003 | // Template members. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1004 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1005 | bool considerVisibility = |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 1006 | (!LV.isVisibilityExplicit() && |
| 1007 | !classLV.isVisibilityExplicit() && |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1008 | !hasExplicitVisibilityAlready(computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1009 | LinkageInfo tempLV = |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 1010 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1011 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1012 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1013 | if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) { |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1014 | if (isExplicitMemberSpecialization(redeclTemp)) { |
| 1015 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 1016 | } |
| 1017 | } |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1020 | // We should never be looking for an attribute directly on a template. |
| 1021 | assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); |
| 1022 | |
| 1023 | // If this member is an explicit member specialization, and it has |
| 1024 | // an explicit attribute, ignore visibility from the parent. |
| 1025 | bool considerClassVisibility = true; |
| 1026 | if (explicitSpecSuppressor && |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 1027 | // optimization: hasDVA() is true only with explicit visibility. |
| 1028 | LV.isVisibilityExplicit() && |
| 1029 | classLV.getVisibility() != DefaultVisibility && |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1030 | hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) { |
| 1031 | considerClassVisibility = false; |
| 1032 | } |
| 1033 | |
| 1034 | // Finally, merge in information from the class. |
| 1035 | LV.mergeMaybeWithVisibility(classLV, considerClassVisibility); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1036 | return LV; |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1039 | void NamedDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1040 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1041 | bool NamedDecl::isLinkageValid() const { |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1042 | if (!hasCachedLinkage()) |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1043 | return true; |
| John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 1044 | |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1045 | Linkage L = LinkageComputer{} |
| 1046 | .computeLVForDecl(this, LVComputationKind::forLinkageOnly()) |
| 1047 | .getLinkage(); |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1048 | return L == getCachedLinkage(); |
| John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1051 | ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { |
| 1052 | StringRef name = getName(); |
| 1053 | if (name.empty()) return SFF_None; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1054 | |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1055 | if (name.front() == 'C') |
| 1056 | if (name == "CFStringCreateWithFormat" || |
| 1057 | name == "CFStringCreateWithFormatAndArguments" || |
| 1058 | name == "CFStringAppendFormat" || |
| 1059 | name == "CFStringAppendFormatAndArguments") |
| 1060 | return SFF_CFString; |
| 1061 | return SFF_None; |
| 1062 | } |
| 1063 | |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1064 | Linkage NamedDecl::getLinkageInternal() const { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1065 | // We don't care about visibility here, so ask for the cheapest |
| 1066 | // possible visibility analysis. |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1067 | return LinkageComputer{} |
| 1068 | .getLVForDecl(this, LVComputationKind::forLinkageOnly()) |
| 1069 | .getLinkage(); |
| Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1072 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1073 | return LinkageComputer{}.getDeclLinkageAndVisibility(this); |
| John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1074 | } |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1075 | |
| Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1076 | static Optional<Visibility> |
| 1077 | getExplicitVisibilityAux(const NamedDecl *ND, |
| 1078 | NamedDecl::ExplicitVisibilityKind kind, |
| 1079 | bool IsMostRecent) { |
| 1080 | assert(!IsMostRecent || ND == ND->getMostRecentDecl()); |
| 1081 | |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1082 | // Check the declaration itself first. |
| Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1083 | if (Optional<Visibility> V = getVisibilityOf(ND, kind)) |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1084 | return V; |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1085 | |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1086 | // If this is a member class of a specialization of a class template |
| 1087 | // and the corresponding decl has explicit visibility, use that. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1088 | if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) { |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1089 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 1090 | if (InstantiatedFrom) |
| 1091 | return getVisibilityOf(InstantiatedFrom, kind); |
| 1092 | } |
| 1093 | |
| 1094 | // If there wasn't explicit visibility there, and this is a |
| 1095 | // specialization of a class template, check for visibility |
| 1096 | // on the pattern. |
| Steven Wu | 79cbd11 | 2018-04-19 15:46:43 +0000 | [diff] [blame] | 1097 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 1098 | // Walk all the template decl till this point to see if there are |
| 1099 | // explicit visibility attributes. |
| 1100 | const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl(); |
| 1101 | while (TD != nullptr) { |
| 1102 | auto Vis = getVisibilityOf(TD, kind); |
| 1103 | if (Vis != None) |
| 1104 | return Vis; |
| 1105 | TD = TD->getPreviousDecl(); |
| 1106 | } |
| 1107 | return None; |
| 1108 | } |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1109 | |
| 1110 | // Use the most recent declaration. |
| Rafael Espindola | 7ab1ce0 | 2013-12-08 01:13:22 +0000 | [diff] [blame] | 1111 | if (!IsMostRecent && !isa<NamespaceDecl>(ND)) { |
| Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1112 | const NamedDecl *MostRecent = ND->getMostRecentDecl(); |
| 1113 | if (MostRecent != ND) |
| 1114 | return getExplicitVisibilityAux(MostRecent, kind, true); |
| 1115 | } |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1116 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1117 | if (const auto *Var = dyn_cast<VarDecl>(ND)) { |
| Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1118 | if (Var->isStaticDataMember()) { |
| 1119 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
| 1120 | if (InstantiatedFrom) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1121 | return getVisibilityOf(InstantiatedFrom, kind); |
| Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| David Majnemer | 35b02bc | 2014-04-29 07:32:26 +0000 | [diff] [blame] | 1124 | if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var)) |
| 1125 | return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(), |
| 1126 | kind); |
| 1127 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1128 | return None; |
| Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1129 | } |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1130 | // Also handle function template specializations. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1131 | if (const auto *fn = dyn_cast<FunctionDecl>(ND)) { |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1132 | // If the function is a specialization of a template with an |
| 1133 | // explicit visibility attribute, use that. |
| 1134 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 1135 | = fn->getTemplateSpecializationInfo()) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1136 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), |
| 1137 | kind); |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1138 | |
| Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 1139 | // If the function is a member of a specialization of a class template |
| 1140 | // and the corresponding decl has explicit visibility, use that. |
| 1141 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 1142 | if (InstantiatedFrom) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1143 | return getVisibilityOf(InstantiatedFrom, kind); |
| Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 1144 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1145 | return None; |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
| Rafael Espindola | fb4263f | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 1148 | // The visibility of a template is stored in the templated decl. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1149 | if (const auto *TD = dyn_cast<TemplateDecl>(ND)) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1150 | return getVisibilityOf(TD->getTemplatedDecl(), kind); |
| Rafael Espindola | fb4263f | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 1151 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1152 | return None; |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
| Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1155 | Optional<Visibility> |
| 1156 | NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { |
| 1157 | return getExplicitVisibilityAux(this, kind, false); |
| 1158 | } |
| 1159 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1160 | LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC, |
| 1161 | Decl *ContextDecl, |
| 1162 | LVComputationKind computation) { |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1163 | // This lambda has its linkage/visibility determined by its owner. |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1164 | const NamedDecl *Owner; |
| 1165 | if (!ContextDecl) |
| 1166 | Owner = dyn_cast<NamedDecl>(DC); |
| 1167 | else if (isa<ParmVarDecl>(ContextDecl)) |
| 1168 | Owner = |
| 1169 | dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext()); |
| 1170 | else |
| 1171 | Owner = cast<NamedDecl>(ContextDecl); |
| 1172 | |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1173 | if (!Owner) |
| Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1174 | return LinkageInfo::none(); |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1175 | |
| 1176 | // If the owner has a deduced type, we need to skip querying the linkage and |
| 1177 | // visibility of that type, because it might involve this closure type. The |
| 1178 | // only effect of this is that we might give a lambda VisibleNoLinkage rather |
| 1179 | // than NoLinkage when we don't strictly need to, which is benign. |
| 1180 | auto *VD = dyn_cast<VarDecl>(Owner); |
| Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1181 | LinkageInfo OwnerLV = |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1182 | VD && VD->getType()->getContainedDeducedType() |
| 1183 | ? computeLVForDecl(Owner, computation, /*IgnoreVarTypeLinkage*/true) |
| 1184 | : getLVForDecl(Owner, computation); |
| 1185 | |
| Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1186 | // A lambda never formally has linkage. But if the owner is externally |
| 1187 | // visible, then the lambda is too. We apply the same rules to blocks. |
| 1188 | if (!isExternallyVisible(OwnerLV.getLinkage())) |
| 1189 | return LinkageInfo::none(); |
| 1190 | return LinkageInfo(VisibleNoLinkage, OwnerLV.getVisibility(), |
| 1191 | OwnerLV.isVisibilityExplicit()); |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1194 | LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D, |
| 1195 | LVComputationKind computation) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1196 | if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1197 | if (Function->isInAnonymousNamespace() && |
| Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 1198 | !isFirstInExternCContext(Function)) |
| Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 1199 | return getInternalLinkageFor(Function); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1200 | |
| 1201 | // This is a "void f();" which got merged with a file static. |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1202 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1203 | return getInternalLinkageFor(Function); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1204 | |
| 1205 | LinkageInfo LV; |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1206 | if (!hasExplicitVisibilityAlready(computation)) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1207 | if (Optional<Visibility> Vis = |
| 1208 | getExplicitVisibility(Function, computation)) |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1209 | LV.mergeVisibility(*Vis, true); |
| 1210 | } |
| 1211 | |
| 1212 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 1213 | // merging storage classes and visibility attributes, so we don't have to |
| 1214 | // look at previous decls in here. |
| 1215 | |
| 1216 | return LV; |
| 1217 | } |
| 1218 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1219 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1220 | if (Var->hasExternalStorage()) { |
| Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 1221 | if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(Var)) |
| Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 1222 | return getInternalLinkageFor(Var); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1223 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1224 | LinkageInfo LV; |
| 1225 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 1226 | LV.mergeVisibility(HiddenVisibility, true); |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1227 | else if (!hasExplicitVisibilityAlready(computation)) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1228 | if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation)) |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1229 | LV.mergeVisibility(*Vis, true); |
| 1230 | } |
| 1231 | |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1232 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
| 1233 | LinkageInfo PrevLV = getLVForDecl(Prev, computation); |
| 1234 | if (PrevLV.getLinkage()) |
| 1235 | LV.setLinkage(PrevLV.getLinkage()); |
| 1236 | LV.mergeVisibility(PrevLV); |
| 1237 | } |
| 1238 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1239 | return LV; |
| 1240 | } |
| Rafael Espindola | a418418 | 2013-06-17 20:04:51 +0000 | [diff] [blame] | 1241 | |
| 1242 | if (!Var->isStaticLocal()) |
| 1243 | return LinkageInfo::none(); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| Rafael Espindola | a418418 | 2013-06-17 20:04:51 +0000 | [diff] [blame] | 1246 | ASTContext &Context = D->getASTContext(); |
| 1247 | if (!Context.getLangOpts().CPlusPlus) |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1248 | return LinkageInfo::none(); |
| 1249 | |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1250 | const Decl *OuterD = getOutermostFuncOrBlockContext(D); |
| Alexey Bataev | 0a47e65 | 2016-01-12 09:01:25 +0000 | [diff] [blame] | 1251 | if (!OuterD || OuterD->isInvalidDecl()) |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1252 | return LinkageInfo::none(); |
| Rafael Espindola | 5218936 | 2013-06-04 13:43:35 +0000 | [diff] [blame] | 1253 | |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1254 | LinkageInfo LV; |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1255 | if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) { |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1256 | if (!BD->getBlockManglingNumber()) |
| 1257 | return LinkageInfo::none(); |
| Rafael Espindola | 5218936 | 2013-06-04 13:43:35 +0000 | [diff] [blame] | 1258 | |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1259 | LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(), |
| 1260 | BD->getBlockManglingContextDecl(), computation); |
| 1261 | } else { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1262 | const auto *FD = cast<FunctionDecl>(OuterD); |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1263 | if (!FD->isInlined() && |
| David Majnemer | 9963ade | 2014-12-16 04:52:14 +0000 | [diff] [blame] | 1264 | !isTemplateInstantiation(FD->getTemplateSpecializationKind())) |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1265 | return LinkageInfo::none(); |
| 1266 | |
| Hiroshi Inoue | 03ff37d | 2018-08-22 05:43:27 +0000 | [diff] [blame] | 1267 | // If a function is hidden by -fvisibility-inlines-hidden option and |
| 1268 | // is not explicitly attributed as a hidden function, |
| 1269 | // we should not make static local variables in the function hidden. |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1270 | LV = getLVForDecl(FD, computation); |
| Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 1271 | if (isa<VarDecl>(D) && useInlineVisibilityHidden(FD) && |
| 1272 | !LV.isVisibilityExplicit()) { |
| 1273 | assert(cast<VarDecl>(D)->isStaticLocal()); |
| 1274 | // If this was an implicitly hidden inline method, check again for |
| 1275 | // explicit visibility on the parent class, and use that for static locals |
| 1276 | // if present. |
| 1277 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1278 | LV = getLVForDecl(MD->getParent(), computation); |
| 1279 | if (!LV.isVisibilityExplicit()) { |
| 1280 | Visibility globalVisibility = |
| 1281 | computation.isValueVisibility() |
| 1282 | ? Context.getLangOpts().getValueVisibilityMode() |
| 1283 | : Context.getLangOpts().getTypeVisibilityMode(); |
| 1284 | return LinkageInfo(VisibleNoLinkage, globalVisibility, |
| 1285 | /*visibilityExplicit=*/false); |
| 1286 | } |
| 1287 | } |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1288 | } |
| Rafael Espindola | 111bb2e | 2013-05-27 14:50:21 +0000 | [diff] [blame] | 1289 | if (!isExternallyVisible(LV.getLinkage())) |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1290 | return LinkageInfo::none(); |
| 1291 | return LinkageInfo(VisibleNoLinkage, LV.getVisibility(), |
| 1292 | LV.isVisibilityExplicit()); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1295 | static inline const CXXRecordDecl* |
| 1296 | getOutermostEnclosingLambda(const CXXRecordDecl *Record) { |
| 1297 | const CXXRecordDecl *Ret = Record; |
| 1298 | while (Record && Record->isLambda()) { |
| 1299 | Ret = Record; |
| 1300 | if (!Record->getParent()) break; |
| 1301 | // Get the Containing Class of this Lambda Class |
| 1302 | Record = dyn_cast_or_null<CXXRecordDecl>( |
| 1303 | Record->getParent()->getParent()); |
| 1304 | } |
| 1305 | return Ret; |
| 1306 | } |
| 1307 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1308 | LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D, |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1309 | LVComputationKind computation, |
| 1310 | bool IgnoreVarTypeLinkage) { |
| Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1311 | // Internal_linkage attribute overrides other considerations. |
| 1312 | if (D->hasAttr<InternalLinkageAttr>()) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1313 | return getInternalLinkageFor(D); |
| Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1314 | |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1315 | // Objective-C: treat all Objective-C declarations as having external |
| 1316 | // linkage. |
| John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1317 | switch (D->getKind()) { |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1318 | default: |
| 1319 | break; |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1320 | |
| 1321 | // Per C++ [basic.link]p2, only the names of objects, references, |
| 1322 | // functions, types, templates, namespaces, and values ever have linkage. |
| 1323 | // |
| 1324 | // Note that the name of a typedef, namespace alias, using declaration, |
| 1325 | // and so on are not the name of the corresponding type, namespace, or |
| 1326 | // declaration, so they do *not* have linkage. |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1327 | case Decl::ImplicitParam: |
| 1328 | case Decl::Label: |
| 1329 | case Decl::NamespaceAlias: |
| Argyrios Kyrtzidis | 79d0428 | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 1330 | case Decl::ParmVar: |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1331 | case Decl::Using: |
| 1332 | case Decl::UsingShadow: |
| 1333 | case Decl::UsingDirective: |
| Argyrios Kyrtzidis | 79d0428 | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 1334 | return LinkageInfo::none(); |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1335 | |
| Richard Smith | 26210db | 2015-11-13 03:52:13 +0000 | [diff] [blame] | 1336 | case Decl::EnumConstant: |
| 1337 | // 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] | 1338 | if (D->getASTContext().getLangOpts().CPlusPlus) |
| 1339 | return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation); |
| 1340 | return LinkageInfo::visible_none(); |
| Richard Smith | 26210db | 2015-11-13 03:52:13 +0000 | [diff] [blame] | 1341 | |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1342 | case Decl::Typedef: |
| 1343 | case Decl::TypeAlias: |
| 1344 | // A typedef declaration has linkage if it gives a type a name for |
| 1345 | // linkage purposes. |
| Vassil Vassilev | b00ea08 | 2017-07-01 20:44:49 +0000 | [diff] [blame] | 1346 | if (!cast<TypedefNameDecl>(D) |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1347 | ->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
| 1348 | return LinkageInfo::none(); |
| 1349 | break; |
| 1350 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1351 | case Decl::TemplateTemplateParm: // count these as external |
| 1352 | case Decl::NonTypeTemplateParm: |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1353 | case Decl::ObjCAtDefsField: |
| 1354 | case Decl::ObjCCategory: |
| 1355 | case Decl::ObjCCategoryImpl: |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1356 | case Decl::ObjCCompatibleAlias: |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1357 | case Decl::ObjCImplementation: |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1358 | case Decl::ObjCMethod: |
| 1359 | case Decl::ObjCProperty: |
| 1360 | case Decl::ObjCPropertyImpl: |
| 1361 | case Decl::ObjCProtocol: |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1362 | return getExternalLinkageFor(D); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1363 | |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1364 | case Decl::CXXRecord: { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1365 | const auto *Record = cast<CXXRecordDecl>(D); |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1366 | if (Record->isLambda()) { |
| 1367 | if (!Record->getLambdaManglingNumber()) { |
| 1368 | // This lambda has no mangling number, so it's internal. |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1369 | return getInternalLinkageFor(D); |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1370 | } |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1371 | |
| Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1372 | // This lambda has its linkage/visibility determined: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1373 | // - either by the outermost lambda if that lambda has no mangling |
| 1374 | // number. |
| Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1375 | // - or by the parent of the outer most lambda |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1376 | // This prevents infinite recursion in settings such as nested lambdas |
| 1377 | // used in NSDMI's, for e.g. |
| Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1378 | // struct L { |
| 1379 | // int t{}; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1380 | // int t2 = ([](int a) { return [](int b) { return b; };})(t)(t); |
| Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1381 | // }; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1382 | const CXXRecordDecl *OuterMostLambda = |
| Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1383 | getOutermostEnclosingLambda(Record); |
| 1384 | if (!OuterMostLambda->getLambdaManglingNumber()) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1385 | return getInternalLinkageFor(D); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1386 | |
| Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1387 | return getLVForClosure( |
| 1388 | OuterMostLambda->getDeclContext()->getRedeclContext(), |
| 1389 | OuterMostLambda->getLambdaContextDecl(), computation); |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1390 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1391 | |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1392 | break; |
| 1393 | } |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1396 | // Handle linkage for namespace-scope names. |
| John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1397 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1398 | return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1399 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1400 | // C++ [basic.link]p5: |
| 1401 | // In addition, a member function, static data member, a named |
| 1402 | // class or enumeration of class scope, or an unnamed class or |
| 1403 | // enumeration defined in a class-scope typedef declaration such |
| 1404 | // that the class or enumeration has the typedef name for linkage |
| 1405 | // purposes (7.1.3), has external linkage if the name of the class |
| 1406 | // has external linkage. |
| John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1407 | if (D->getDeclContext()->isRecord()) |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1408 | return getLVForClassMember(D, computation, IgnoreVarTypeLinkage); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1409 | |
| 1410 | // C++ [basic.link]p6: |
| 1411 | // The name of a function declared in block scope and the name of |
| 1412 | // an object declared by a block scope extern declaration have |
| 1413 | // linkage. If there is a visible declaration of an entity with |
| 1414 | // linkage having the same name and type, ignoring entities |
| 1415 | // declared outside the innermost enclosing namespace scope, the |
| 1416 | // block scope declaration declares that same entity and receives |
| 1417 | // the linkage of the previous declaration. If there is more than |
| 1418 | // one such matching entity, the program is ill-formed. Otherwise, |
| 1419 | // if no matching entity is found, the block scope entity receives |
| 1420 | // external linkage. |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1421 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 1422 | return getLVForLocalDecl(D, computation); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1423 | |
| 1424 | // C++ [basic.link]p6: |
| 1425 | // Names not covered by these rules have no linkage. |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1426 | return LinkageInfo::none(); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1427 | } |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1428 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1429 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
| 1430 | LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D, |
| 1431 | LVComputationKind computation) { |
| 1432 | // Internal_linkage attribute overrides other considerations. |
| 1433 | if (D->hasAttr<InternalLinkageAttr>()) |
| 1434 | return getInternalLinkageFor(D); |
| Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1435 | |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1436 | if (computation.IgnoreAllVisibility && D->hasCachedLinkage()) |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1437 | return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1438 | |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 1439 | if (llvm::Optional<LinkageInfo> LI = lookup(D, computation)) |
| 1440 | return *LI; |
| 1441 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1442 | LinkageInfo LV = computeLVForDecl(D, computation); |
| 1443 | if (D->hasCachedLinkage()) |
| 1444 | assert(D->getCachedLinkage() == LV.getLinkage()); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1445 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1446 | D->setCachedLinkage(LV.getLinkage()); |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 1447 | cache(D, computation, LV); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1448 | |
| 1449 | #ifndef NDEBUG |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1450 | // In C (because of gnu inline) and in c++ with microsoft extensions an |
| 1451 | // static can follow an extern, so we can have two decls with different |
| 1452 | // linkages. |
| 1453 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 1454 | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
| 1455 | return LV; |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1456 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1457 | // We have just computed the linkage for this decl. By induction we know |
| 1458 | // that all other computed linkages match, check that the one we just |
| 1459 | // computed also does. |
| 1460 | NamedDecl *Old = nullptr; |
| 1461 | for (auto I : D->redecls()) { |
| 1462 | auto *T = cast<NamedDecl>(I); |
| 1463 | if (T == D) |
| 1464 | continue; |
| 1465 | if (!T->isInvalidDecl() && T->hasCachedLinkage()) { |
| 1466 | Old = T; |
| 1467 | break; |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1468 | } |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1469 | } |
| 1470 | assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1471 | #endif |
| 1472 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1473 | return LV; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1474 | } |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1475 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1476 | LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1477 | return getLVForDecl(D, |
| 1478 | LVComputationKind(usesTypeVisibility(D) |
| 1479 | ? NamedDecl::VisibilityForType |
| 1480 | : NamedDecl::VisibilityForValue)); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1483 | Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1484 | Module *M = getOwningModule(); |
| 1485 | if (!M) |
| 1486 | return nullptr; |
| 1487 | |
| 1488 | switch (M->Kind) { |
| 1489 | case Module::ModuleMapModule: |
| 1490 | // Module map modules have no special linkage semantics. |
| 1491 | return nullptr; |
| 1492 | |
| 1493 | case Module::ModuleInterfaceUnit: |
| 1494 | return M; |
| 1495 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1496 | case Module::GlobalModuleFragment: { |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1497 | // External linkage declarations in the global module have no owning module |
| 1498 | // for linkage purposes. But internal linkage declarations in the global |
| 1499 | // module fragment of a particular module are owned by that module for |
| 1500 | // linkage purposes. |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1501 | if (IgnoreLinkage) |
| 1502 | return nullptr; |
| 1503 | bool InternalLinkage; |
| 1504 | if (auto *ND = dyn_cast<NamedDecl>(this)) |
| 1505 | InternalLinkage = !ND->hasExternalFormalLinkage(); |
| 1506 | else { |
| 1507 | auto *NSD = dyn_cast<NamespaceDecl>(this); |
| 1508 | InternalLinkage = (NSD && NSD->isAnonymousNamespace()) || |
| 1509 | isInAnonymousNamespace(); |
| 1510 | } |
| 1511 | return InternalLinkage ? M->Parent : nullptr; |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1512 | } |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 1513 | |
| 1514 | case Module::PrivateModuleFragment: |
| 1515 | // The private module fragment is part of its containing module for linkage |
| 1516 | // purposes. |
| 1517 | return M->Parent; |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1518 | } |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1519 | |
| 1520 | llvm_unreachable("unknown module kind"); |
| 1521 | } |
| 1522 | |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1523 | void NamedDecl::printName(raw_ostream &os) const { |
| 1524 | os << Name; |
| 1525 | } |
| 1526 | |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1527 | std::string NamedDecl::getQualifiedNameAsString() const { |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1528 | std::string QualName; |
| 1529 | llvm::raw_string_ostream OS(QualName); |
| Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 1530 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1531 | return OS.str(); |
| 1532 | } |
| 1533 | |
| 1534 | void NamedDecl::printQualifiedName(raw_ostream &OS) const { |
| 1535 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
| 1536 | } |
| 1537 | |
| 1538 | void NamedDecl::printQualifiedName(raw_ostream &OS, |
| 1539 | const PrintingPolicy &P) const { |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1540 | const DeclContext *Ctx = getDeclContext(); |
| 1541 | |
| David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 1542 | // For ObjC methods and properties, look through categories and use the |
| 1543 | // interface as context. |
| Argyrios Kyrtzidis | a166a2b | 2017-03-07 09:26:07 +0000 | [diff] [blame] | 1544 | if (auto *MD = dyn_cast<ObjCMethodDecl>(this)) |
| 1545 | if (auto *ID = MD->getClassInterface()) |
| 1546 | Ctx = ID; |
| David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 1547 | if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) { |
| 1548 | if (auto *MD = PD->getGetterMethodDecl()) |
| 1549 | if (auto *ID = MD->getClassInterface()) |
| 1550 | Ctx = ID; |
| 1551 | } |
| Argyrios Kyrtzidis | a166a2b | 2017-03-07 09:26:07 +0000 | [diff] [blame] | 1552 | |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1553 | if (Ctx->isFunctionOrMethod()) { |
| 1554 | printName(OS); |
| 1555 | return; |
| 1556 | } |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1557 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1558 | using ContextsTy = SmallVector<const DeclContext *, 8>; |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1559 | ContextsTy Contexts; |
| 1560 | |
| Sam McCall | 34f9d3f | 2018-02-02 13:34:47 +0000 | [diff] [blame] | 1561 | // Collect named contexts. |
| 1562 | while (Ctx) { |
| 1563 | if (isa<NamedDecl>(Ctx)) |
| 1564 | Contexts.push_back(Ctx); |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1565 | Ctx = Ctx->getParent(); |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1566 | } |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1567 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1568 | for (const DeclContext *DC : llvm::reverse(Contexts)) { |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1569 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
| Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 1570 | OS << Spec->getName(); |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1571 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 1572 | printTemplateArgumentList(OS, TemplateArgs.asArray(), P); |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1573 | } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { |
| Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 1574 | if (P.SuppressUnwrittenScope && |
| 1575 | (ND->isAnonymousNamespace() || ND->isInline())) |
| 1576 | continue; |
| Reid Kleckner | 6010338 | 2015-12-16 02:04:40 +0000 | [diff] [blame] | 1577 | if (ND->isAnonymousNamespace()) { |
| 1578 | OS << (P.MSVCFormatting ? "`anonymous namespace\'" |
| 1579 | : "(anonymous namespace)"); |
| 1580 | } |
| Sam Weinig | 07d211e | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 1581 | else |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1582 | OS << *ND; |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1583 | } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) { |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1584 | if (!RD->getIdentifier()) |
| David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 1585 | OS << "(anonymous " << RD->getKindName() << ')'; |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1586 | else |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1587 | OS << *RD; |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1588 | } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1589 | const FunctionProtoType *FT = nullptr; |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1590 | if (FD->hasWrittenPrototype()) |
| Eli Friedman | 5c27c4c | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 1591 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1592 | |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1593 | OS << *FD << '('; |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1594 | if (FT) { |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1595 | unsigned NumParams = FD->getNumParams(); |
| 1596 | for (unsigned i = 0; i < NumParams; ++i) { |
| 1597 | if (i) |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1598 | OS << ", "; |
| Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 1599 | OS << FD->getParamDecl(i)->getType().stream(P); |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | if (FT->isVariadic()) { |
| 1603 | if (NumParams > 0) |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1604 | OS << ", "; |
| 1605 | OS << "..."; |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1608 | OS << ')'; |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1609 | } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) { |
| Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 1610 | // C++ [dcl.enum]p10: Each enum-name and each unscoped |
| 1611 | // enumerator is declared in the scope that immediately contains |
| 1612 | // the enum-specifier. Each scoped enumerator is declared in the |
| 1613 | // scope of the enumeration. |
| Paul Robinson | f183848 | 2017-12-21 21:47:22 +0000 | [diff] [blame] | 1614 | // For the case of unscoped enumerator, do not include in the qualified |
| Paul Robinson | d1c3dd8 | 2017-12-26 18:01:19 +0000 | [diff] [blame] | 1615 | // name any information about its enum enclosing scope, as its visibility |
| Paul Robinson | f183848 | 2017-12-21 21:47:22 +0000 | [diff] [blame] | 1616 | // is global. |
| 1617 | if (ED->isScoped()) |
| Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 1618 | OS << *ED; |
| 1619 | else |
| 1620 | continue; |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1621 | } else { |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1622 | OS << *cast<NamedDecl>(DC); |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1623 | } |
| 1624 | OS << "::"; |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1627 | if (getDeclName() || isa<DecompositionDecl>(this)) |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1628 | OS << *this; |
| John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 1629 | else |
| David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 1630 | OS << "(anonymous)"; |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1631 | } |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1632 | |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1633 | void NamedDecl::getNameForDiagnostic(raw_ostream &OS, |
| 1634 | const PrintingPolicy &Policy, |
| 1635 | bool Qualified) const { |
| 1636 | if (Qualified) |
| 1637 | printQualifiedName(OS, Policy); |
| 1638 | else |
| 1639 | printName(OS); |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1642 | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { |
| 1643 | return true; |
| 1644 | } |
| 1645 | static bool isRedeclarableImpl(...) { return false; } |
| 1646 | static bool isRedeclarable(Decl::Kind K) { |
| 1647 | switch (K) { |
| 1648 | #define DECL(Type, Base) \ |
| 1649 | case Decl::Type: \ |
| 1650 | return isRedeclarableImpl((Type##Decl *)nullptr); |
| 1651 | #define ABSTRACT_DECL(DECL) |
| 1652 | #include "clang/AST/DeclNodes.inc" |
| 1653 | } |
| 1654 | llvm_unreachable("unknown decl kind"); |
| 1655 | } |
| 1656 | |
| 1657 | bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const { |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1658 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 1659 | |
| Richard Smith | fe620d2 | 2015-03-05 23:24:12 +0000 | [diff] [blame] | 1660 | // Never replace one imported declaration with another; we need both results |
| 1661 | // when re-exporting. |
| 1662 | if (OldD->isFromASTFile() && isFromASTFile()) |
| 1663 | return false; |
| 1664 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1665 | // A kind mismatch implies that the declaration is not replaced. |
| 1666 | if (OldD->getKind() != getKind()) |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1667 | return false; |
| 1668 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1669 | // For method declarations, we never replace. (Why?) |
| 1670 | if (isa<ObjCMethodDecl>(this)) |
| 1671 | return false; |
| 1672 | |
| 1673 | // For parameters, pick the newer one. This is either an error or (in |
| 1674 | // Objective-C) permitted as an extension. |
| 1675 | if (isa<ParmVarDecl>(this)) |
| 1676 | return true; |
| 1677 | |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1678 | // Inline namespaces can give us two declarations with the same |
| 1679 | // name and kind in the same scope but different contexts; we should |
| 1680 | // keep both declarations in this case. |
| 1681 | if (!this->getDeclContext()->getRedeclContext()->Equals( |
| 1682 | OldD->getDeclContext()->getRedeclContext())) |
| 1683 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1685 | // Using declarations can be replaced if they import the same name from the |
| 1686 | // same context. |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1687 | if (auto *UD = dyn_cast<UsingDecl>(this)) { |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1688 | ASTContext &Context = getASTContext(); |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1689 | return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1690 | Context.getCanonicalNestedNameSpecifier( |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1691 | cast<UsingDecl>(OldD)->getQualifier()); |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1692 | } |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1693 | if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { |
| Eli Friedman | 0eaf10b | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1694 | ASTContext &Context = getASTContext(); |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1695 | return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == |
| Eli Friedman | 0eaf10b | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1696 | Context.getCanonicalNestedNameSpecifier( |
| 1697 | cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); |
| 1698 | } |
| 1699 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1700 | if (isRedeclarable(getKind())) { |
| 1701 | if (getCanonicalDecl() != OldD->getCanonicalDecl()) |
| 1702 | return false; |
| 1703 | |
| 1704 | if (IsKnownNewer) |
| 1705 | return true; |
| 1706 | |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1707 | // Check whether this is actually newer than OldD. We want to keep the |
| 1708 | // newer declaration. This loop will usually only iterate once, because |
| 1709 | // OldD is usually the previous declaration. |
| 1710 | for (auto D : redecls()) { |
| 1711 | if (D == OldD) |
| 1712 | break; |
| 1713 | |
| 1714 | // If we reach the canonical declaration, then OldD is not actually older |
| 1715 | // than this one. |
| 1716 | // |
| 1717 | // FIXME: In this case, we should not add this decl to the lookup table. |
| 1718 | if (D->isCanonicalDecl()) |
| 1719 | return false; |
| 1720 | } |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1721 | |
| 1722 | // It's a newer declaration of the same kind of declaration in the same |
| 1723 | // scope: we want this decl instead of the existing one. |
| 1724 | return true; |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1727 | // In all other cases, we need to keep both declarations in case they have |
| 1728 | // different visibility. Any attempt to use the name will result in an |
| 1729 | // ambiguity if more than one is visible. |
| 1730 | return false; |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1733 | bool NamedDecl::hasLinkage() const { |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1734 | return getFormalLinkage() != NoLinkage; |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1735 | } |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1736 | |
| Daniel Dunbar | 166ea9ad | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 1737 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
| Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1738 | NamedDecl *ND = this; |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1739 | while (auto *UD = dyn_cast<UsingShadowDecl>(ND)) |
| Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1740 | ND = UD->getTargetDecl(); |
| 1741 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1742 | if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1743 | return AD->getClassInterface(); |
| 1744 | |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1745 | if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND)) |
| 1746 | return AD->getNamespace(); |
| 1747 | |
| Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1748 | return ND; |
| Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1751 | bool NamedDecl::isCXXInstanceMember() const { |
| Douglas Gregor | 3f28ec2 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 1752 | if (!isCXXClassMember()) |
| 1753 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1754 | |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1755 | const NamedDecl *D = this; |
| 1756 | if (isa<UsingShadowDecl>(D)) |
| 1757 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1758 | |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1759 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1760 | return true; |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1761 | if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction())) |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1762 | return MD->isInstance(); |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1763 | return false; |
| 1764 | } |
| 1765 | |
| Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1766 | //===----------------------------------------------------------------------===// |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1767 | // DeclaratorDecl Implementation |
| 1768 | //===----------------------------------------------------------------------===// |
| 1769 | |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1770 | template <typename DeclT> |
| 1771 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1772 | if (decl->getNumTemplateParameterLists() > 0) |
| 1773 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1774 | else |
| 1775 | return decl->getInnerLocStart(); |
| 1776 | } |
| 1777 | |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1778 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
| John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1779 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1780 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1781 | return SourceLocation(); |
| 1782 | } |
| 1783 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1784 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1785 | if (QualifierLoc) { |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1786 | // Make sure the extended decl info is allocated. |
| 1787 | if (!hasExtInfo()) { |
| 1788 | // Save (non-extended) type source info pointer. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1789 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1790 | // Allocate external info struct. |
| 1791 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1792 | // Restore savedTInfo into (extended) decl info. |
| 1793 | getExtInfo()->TInfo = savedTInfo; |
| 1794 | } |
| 1795 | // Set qualifier info. |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1796 | getExtInfo()->QualifierLoc = QualifierLoc; |
| Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1797 | } else { |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1798 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1799 | if (hasExtInfo()) { |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1800 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 1801 | // Save type source info pointer. |
| 1802 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 1803 | // Deallocate the extended decl info. |
| 1804 | getASTContext().Deallocate(getExtInfo()); |
| 1805 | // Restore savedTInfo into (non-extended) decl info. |
| 1806 | DeclInfo = savedTInfo; |
| 1807 | } |
| 1808 | else |
| 1809 | getExtInfo()->QualifierLoc = QualifierLoc; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1814 | void DeclaratorDecl::setTemplateParameterListsInfo( |
| 1815 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 1816 | assert(!TPLists.empty()); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1817 | // Make sure the extended decl info is allocated. |
| 1818 | if (!hasExtInfo()) { |
| 1819 | // Save (non-extended) type source info pointer. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1820 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1821 | // Allocate external info struct. |
| 1822 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1823 | // Restore savedTInfo into (extended) decl info. |
| 1824 | getExtInfo()->TInfo = savedTInfo; |
| 1825 | } |
| 1826 | // Set the template parameter lists info. |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1827 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1830 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1831 | return getTemplateOrInnerLocStart(this); |
| 1832 | } |
| 1833 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1834 | // Helper function: returns true if QT is or contains a type |
| 1835 | // having a postfix component. |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1836 | static bool typeIsPostfix(QualType QT) { |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1837 | while (true) { |
| 1838 | const Type* T = QT.getTypePtr(); |
| 1839 | switch (T->getTypeClass()) { |
| 1840 | default: |
| 1841 | return false; |
| 1842 | case Type::Pointer: |
| 1843 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1844 | break; |
| 1845 | case Type::BlockPointer: |
| 1846 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1847 | break; |
| 1848 | case Type::MemberPointer: |
| 1849 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1850 | break; |
| 1851 | case Type::LValueReference: |
| 1852 | case Type::RValueReference: |
| 1853 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1854 | break; |
| 1855 | case Type::PackExpansion: |
| 1856 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1857 | break; |
| 1858 | case Type::Paren: |
| 1859 | case Type::ConstantArray: |
| 1860 | case Type::DependentSizedArray: |
| 1861 | case Type::IncompleteArray: |
| 1862 | case Type::VariableArray: |
| 1863 | case Type::FunctionProto: |
| 1864 | case Type::FunctionNoProto: |
| 1865 | return true; |
| 1866 | } |
| 1867 | } |
| 1868 | } |
| 1869 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1870 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1871 | SourceLocation RangeEnd = getLocation(); |
| 1872 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| Benjamin Kramer | 3a7cc81 | 2014-02-02 15:28:46 +0000 | [diff] [blame] | 1873 | // If the declaration has no name or the type extends past the name take the |
| 1874 | // end location of the type. |
| 1875 | if (!getDeclName() || typeIsPostfix(TInfo->getType())) |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1876 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1877 | } |
| 1878 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1879 | } |
| 1880 | |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1881 | void QualifierInfo::setTemplateParameterListsInfo( |
| 1882 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1883 | // Free previous template parameters (if any). |
| 1884 | if (NumTemplParamLists > 0) { |
| Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1885 | Context.Deallocate(TemplParamLists); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1886 | TemplParamLists = nullptr; |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1887 | NumTemplParamLists = 0; |
| 1888 | } |
| 1889 | // Set info on matched template parameter lists (if any). |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1890 | if (!TPLists.empty()) { |
| 1891 | TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; |
| 1892 | NumTemplParamLists = TPLists.size(); |
| 1893 | std::copy(TPLists.begin(), TPLists.end(), TemplParamLists); |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1897 | //===----------------------------------------------------------------------===// |
| Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1898 | // VarDecl Implementation |
| 1899 | //===----------------------------------------------------------------------===// |
| 1900 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1901 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1902 | switch (SC) { |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1903 | case SC_None: break; |
| Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1904 | case SC_Auto: return "auto"; |
| 1905 | case SC_Extern: return "extern"; |
| Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1906 | case SC_PrivateExtern: return "__private_extern__"; |
| 1907 | case SC_Register: return "register"; |
| 1908 | case SC_Static: return "static"; |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
| Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1911 | llvm_unreachable("Invalid storage class"); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1914 | VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 1915 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 1916 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| 1917 | StorageClass SC) |
| 1918 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1919 | redeclarable_base(C) { |
| Benjamin Kramer | a939c23 | 2014-03-15 18:54:13 +0000 | [diff] [blame] | 1920 | static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), |
| 1921 | "VarDeclBitfields too large!"); |
| 1922 | static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), |
| 1923 | "ParmVarDeclBitfields too large!"); |
| David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1924 | static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), |
| 1925 | "NonParmVarDeclBitfields too large!"); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1926 | AllBits = 0; |
| 1927 | VarDeclBits.SClass = SC; |
| 1928 | // Everything else is implicitly initialized to false. |
| 1929 | } |
| 1930 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1931 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1932 | SourceLocation StartL, SourceLocation IdL, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1933 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1934 | StorageClass S) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1935 | 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] | 1936 | } |
| 1937 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1938 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1939 | return new (C, ID) |
| 1940 | VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
| 1941 | QualType(), nullptr, SC_None); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
| Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1944 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1945 | assert(isLegalForVariable(SC)); |
| John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1946 | VarDeclBits.SClass = SC; |
| Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
| Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1949 | VarDecl::TLSKind VarDecl::getTLSKind() const { |
| 1950 | switch (VarDeclBits.TSCSpec) { |
| 1951 | case TSCS_unspecified: |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1952 | if (!hasAttr<ThreadAttr>() && |
| 1953 | !(getASTContext().getLangOpts().OpenMPUseTLS && |
| 1954 | getASTContext().getTargetInfo().isTLSSupported() && |
| 1955 | hasAttr<OMPThreadPrivateDeclAttr>())) |
| David Majnemer | 1b5baff | 2015-05-14 05:19:23 +0000 | [diff] [blame] | 1956 | return TLS_None; |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1957 | return ((getASTContext().getLangOpts().isCompatibleWithMSVC( |
| 1958 | LangOptions::MSVC2015)) || |
| 1959 | hasAttr<OMPThreadPrivateDeclAttr>()) |
| David Majnemer | 1b5baff | 2015-05-14 05:19:23 +0000 | [diff] [blame] | 1960 | ? TLS_Dynamic |
| 1961 | : TLS_Static; |
| Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1962 | case TSCS___thread: // Fall through. |
| 1963 | case TSCS__Thread_local: |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1964 | return TLS_Static; |
| Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1965 | case TSCS_thread_local: |
| 1966 | return TLS_Dynamic; |
| 1967 | } |
| 1968 | llvm_unreachable("Unknown thread storage class specifier!"); |
| 1969 | } |
| 1970 | |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1971 | SourceRange VarDecl::getSourceRange() const { |
| Argyrios Kyrtzidis | e0d6497 | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1972 | if (const Expr *Init = getInit()) { |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1973 | SourceLocation InitEnd = Init->getEndLoc(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1974 | // If Init is implicit, ignore its source range and fallback on |
| Nico Weber | bbe1394 | 2013-01-22 17:00:09 +0000 | [diff] [blame] | 1975 | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
| 1976 | if (InitEnd.isValid() && InitEnd != getLocation()) |
| Argyrios Kyrtzidis | e0d6497 | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1977 | return SourceRange(getOuterLocStart(), InitEnd); |
| 1978 | } |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1979 | return DeclaratorDecl::getSourceRange(); |
| Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
| Rafael Espindola | 8851067 | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 1982 | template<typename T> |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 1983 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { |
| Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1984 | // C++ [dcl.link]p1: All function types, function names with external linkage, |
| 1985 | // and variable names with external linkage have a language linkage. |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1986 | if (!D.hasExternalFormalLinkage()) |
| Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1987 | return NoLanguageLinkage; |
| 1988 | |
| 1989 | // 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] | 1990 | // C language linkage fits the implementation nicely. |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1991 | ASTContext &Context = D.getASTContext(); |
| 1992 | if (!Context.getLangOpts().CPlusPlus) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 1993 | return CLanguageLinkage; |
| 1994 | |
| Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 1995 | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
| 1996 | // language linkage of the names of class members and the function type of |
| 1997 | // class member functions. |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 1998 | const DeclContext *DC = D.getDeclContext(); |
| 1999 | if (DC->isRecord()) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2000 | return CXXLanguageLinkage; |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2001 | |
| 2002 | // If the first decl is in an extern "C" context, any other redeclaration |
| 2003 | // will have C language linkage. If the first one is not in an extern "C" |
| 2004 | // 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] | 2005 | if (isFirstInExternCContext(&D)) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2006 | return CLanguageLinkage; |
| 2007 | return CXXLanguageLinkage; |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2008 | } |
| 2009 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2010 | template<typename T> |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2011 | static bool isDeclExternC(const T &D) { |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2012 | // Since the context is ignored for class members, they can only have C++ |
| 2013 | // language linkage or no language linkage. |
| 2014 | const DeclContext *DC = D.getDeclContext(); |
| 2015 | if (DC->isRecord()) { |
| 2016 | assert(D.getASTContext().getLangOpts().CPlusPlus); |
| 2017 | return false; |
| 2018 | } |
| 2019 | |
| 2020 | return D.getLanguageLinkage() == CLanguageLinkage; |
| 2021 | } |
| 2022 | |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2023 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2024 | return getDeclLanguageLinkage(*this); |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2027 | bool VarDecl::isExternC() const { |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2028 | return isDeclExternC(*this); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2031 | bool VarDecl::isInExternCContext() const { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2032 | return getLexicalDeclContext()->isExternCContext(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | bool VarDecl::isInExternCXXContext() const { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2036 | return getLexicalDeclContext()->isExternCXXContext(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2039 | VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2040 | |
| Nico Weber | 7f5015a | 2015-04-15 21:53:00 +0000 | [diff] [blame] | 2041 | VarDecl::DefinitionKind |
| 2042 | VarDecl::isThisDeclarationADefinition(ASTContext &C) const { |
| Richard Smith | a22d9a5 | 2017-12-02 00:55:48 +0000 | [diff] [blame] | 2043 | if (isThisDeclarationADemotedDefinition()) |
| 2044 | return DeclarationOnly; |
| 2045 | |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2046 | // C++ [basic.def]p2: |
| 2047 | // A declaration is a definition unless [...] it contains the 'extern' |
| 2048 | // specifier or a linkage-specification and neither an initializer [...], |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2049 | // it declares a non-inline static data member in a class declaration [...], |
| 2050 | // it declares a static data member outside a class definition and the variable |
| 2051 | // was defined within the class with the constexpr specifier [...], |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2052 | // C++1y [temp.expl.spec]p15: |
| 2053 | // An explicit specialization of a static data member or an explicit |
| 2054 | // specialization of a static data member template is a definition if the |
| 2055 | // declaration includes an initializer; otherwise, it is a declaration. |
| 2056 | // |
| 2057 | // FIXME: How do you declare (but not define) a partial specialization of |
| 2058 | // a static data member template outside the containing class? |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2059 | if (isStaticDataMember()) { |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2060 | if (isOutOfLine() && |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2061 | !(getCanonicalDecl()->isInline() && |
| 2062 | getCanonicalDecl()->isConstexpr()) && |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2063 | (hasInit() || |
| 2064 | // If the first declaration is out-of-line, this may be an |
| 2065 | // instantiation of an out-of-line partial specialization of a variable |
| 2066 | // template for which we have not yet instantiated the initializer. |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2067 | (getFirstDecl()->isOutOfLine() |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2068 | ? getTemplateSpecializationKind() == TSK_Undeclared |
| 2069 | : getTemplateSpecializationKind() != |
| 2070 | TSK_ExplicitSpecialization) || |
| 2071 | isa<VarTemplatePartialSpecializationDecl>(this))) |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2072 | return Definition; |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2073 | else if (!isOutOfLine() && isInline()) |
| 2074 | return Definition; |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2075 | else |
| 2076 | return DeclarationOnly; |
| 2077 | } |
| 2078 | // C99 6.7p5: |
| 2079 | // A definition of an identifier is a declaration for that identifier that |
| 2080 | // [...] causes storage to be reserved for that object. |
| 2081 | // Note: that applies for all non-file-scope objects. |
| 2082 | // C99 6.9.2p1: |
| 2083 | // If the declaration of an identifier for an object has file scope and an |
| 2084 | // initializer, the declaration is an external definition for the identifier |
| 2085 | if (hasInit()) |
| 2086 | return Definition; |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2087 | |
| Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 2088 | if (hasDefiningAttr()) |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2089 | return Definition; |
| 2090 | |
| David Majnemer | dd0bed1 | 2015-05-14 05:19:20 +0000 | [diff] [blame] | 2091 | if (const auto *SAA = getAttr<SelectAnyAttr>()) |
| 2092 | if (!SAA->isInherited()) |
| 2093 | return Definition; |
| 2094 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2095 | // A variable template specialization (other than a static data member |
| 2096 | // template or an explicit specialization) is a declaration until we |
| 2097 | // instantiate its initializer. |
| Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 2098 | if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2099 | if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && |
| 2100 | !isa<VarTemplatePartialSpecializationDecl>(VTSD) && |
| 2101 | !VTSD->IsCompleteDefinition) |
| 2102 | return DeclarationOnly; |
| 2103 | } |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2104 | |
| Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 2105 | if (hasExternalStorage()) |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2106 | return DeclarationOnly; |
| Rafael Espindola | 8f326a5 | 2013-03-07 01:42:44 +0000 | [diff] [blame] | 2107 | |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2108 | // [dcl.link] p7: |
| 2109 | // A declaration directly contained in a linkage-specification is treated |
| 2110 | // as if it contains the extern specifier for the purpose of determining |
| 2111 | // the linkage of the declared name and whether it is a definition. |
| Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 2112 | if (isSingleLineLanguageLinkage(*this)) |
| Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 2113 | return DeclarationOnly; |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2114 | |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2115 | // C99 6.9.2p2: |
| 2116 | // A declaration of an object that has file scope without an initializer, |
| 2117 | // and without a storage class specifier or the scs 'static', constitutes |
| 2118 | // a tentative definition. |
| 2119 | // No such thing in C++. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2120 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2121 | return TentativeDefinition; |
| 2122 | |
| 2123 | // What's left is (in C, block-scope) declarations without initializers or |
| 2124 | // external storage. These are definitions. |
| 2125 | return Definition; |
| 2126 | } |
| 2127 | |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2128 | VarDecl *VarDecl::getActingDefinition() { |
| 2129 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 2130 | if (Kind != TentativeDefinition) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2131 | return nullptr; |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2132 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2133 | VarDecl *LastTentative = nullptr; |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2134 | VarDecl *First = getFirstDecl(); |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2135 | for (auto I : First->redecls()) { |
| 2136 | Kind = I->isThisDeclarationADefinition(); |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2137 | if (Kind == Definition) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2138 | return nullptr; |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2139 | else if (Kind == TentativeDefinition) |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2140 | LastTentative = I; |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2141 | } |
| 2142 | return LastTentative; |
| 2143 | } |
| 2144 | |
| Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2145 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2146 | VarDecl *First = getFirstDecl(); |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2147 | for (auto I : First->redecls()) { |
| 2148 | if (I->isThisDeclarationADefinition(C) == Definition) |
| 2149 | return I; |
| Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2150 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2151 | return nullptr; |
| Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
| Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2154 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 2155 | DefinitionKind Kind = DeclarationOnly; |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2156 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2157 | const VarDecl *First = getFirstDecl(); |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2158 | for (auto I : First->redecls()) { |
| 2159 | Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); |
| Daniel Dunbar | 082c62d | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 2160 | if (Kind == Definition) |
| 2161 | break; |
| 2162 | } |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 2163 | |
| 2164 | return Kind; |
| 2165 | } |
| 2166 | |
| Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2167 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2168 | for (auto I : redecls()) { |
| 2169 | if (auto Expr = I->getInit()) { |
| 2170 | D = I; |
| 2171 | return Expr; |
| 2172 | } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2173 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2174 | return nullptr; |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2177 | bool VarDecl::hasInit() const { |
| 2178 | if (auto *P = dyn_cast<ParmVarDecl>(this)) |
| 2179 | if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg()) |
| 2180 | return false; |
| 2181 | |
| 2182 | return !Init.isNull(); |
| 2183 | } |
| 2184 | |
| 2185 | Expr *VarDecl::getInit() { |
| 2186 | if (!hasInit()) |
| 2187 | return nullptr; |
| 2188 | |
| 2189 | if (auto *S = Init.dyn_cast<Stmt *>()) |
| 2190 | return cast<Expr>(S); |
| 2191 | |
| 2192 | return cast_or_null<Expr>(Init.get<EvaluatedStmt *>()->Value); |
| 2193 | } |
| 2194 | |
| 2195 | Stmt **VarDecl::getInitAddress() { |
| 2196 | if (auto *ES = Init.dyn_cast<EvaluatedStmt *>()) |
| 2197 | return &ES->Value; |
| 2198 | |
| 2199 | return Init.getAddrOfPtr1(); |
| 2200 | } |
| 2201 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2202 | bool VarDecl::isOutOfLine() const { |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2203 | if (Decl::isOutOfLine()) |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2204 | return true; |
| Chandler Carruth | f50ef6e | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 2205 | |
| 2206 | if (!isStaticDataMember()) |
| 2207 | return false; |
| 2208 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2209 | // If this static data member was instantiated from a static data member of |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2210 | // a class template, check whether that static data member was defined |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2211 | // out-of-line. |
| 2212 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 2213 | return VD->isOutOfLine(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2214 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2215 | return false; |
| 2216 | } |
| 2217 | |
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2218 | void VarDecl::setInit(Expr *I) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2219 | if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2220 | Eval->~EvaluatedStmt(); |
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2221 | getASTContext().Deallocate(Eval); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | Init = I; |
| 2225 | } |
| 2226 | |
| Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2227 | bool VarDecl::isUsableInConstantExpressions(ASTContext &C) const { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2228 | const LangOptions &Lang = C.getLangOpts(); |
| Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2229 | |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2230 | if (!Lang.CPlusPlus) |
| 2231 | return false; |
| 2232 | |
| 2233 | // In C++11, any variable of reference type can be used in a constant |
| 2234 | // expression if it is initialized by a constant expression. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2235 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2236 | return true; |
| 2237 | |
| 2238 | // 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] | 2239 | // not require the variable to be non-volatile, but we consider this to be a |
| 2240 | // defect. |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2241 | if (!getType().isConstQualified() || getType().isVolatileQualified()) |
| Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2242 | return false; |
| 2243 | |
| 2244 | // In C++, const, non-volatile variables of integral or enumeration types |
| 2245 | // can be used in constant expressions. |
| 2246 | if (getType()->isIntegralOrEnumerationType()) |
| 2247 | return true; |
| 2248 | |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2249 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 2250 | // constant expressions. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2251 | return Lang.CPlusPlus11 && isConstexpr(); |
| Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2254 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 2255 | /// form, which contains extra information on the evaluated value of the |
| 2256 | /// initializer. |
| 2257 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2258 | auto *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2259 | if (!Eval) { |
| Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2260 | // Note: EvaluatedStmt contains an APValue, which usually holds |
| 2261 | // resources not allocated from the ASTContext. We need to do some |
| 2262 | // work to avoid leaking those, but we do so in VarDecl::evaluateValue |
| 2263 | // 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] | 2264 | Eval = new (getASTContext()) EvaluatedStmt; |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2265 | Eval->Value = Init.get<Stmt *>(); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2266 | Init = Eval; |
| 2267 | } |
| 2268 | return Eval; |
| 2269 | } |
| 2270 | |
| Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2271 | APValue *VarDecl::evaluateValue() const { |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2272 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2273 | return evaluateValue(Notes); |
| 2274 | } |
| 2275 | |
| 2276 | APValue *VarDecl::evaluateValue( |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2277 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2278 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2279 | |
| 2280 | // We only produce notes indicating why an initializer is non-constant the |
| 2281 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 2282 | // first time we try evaluation, so might not be produced at all. |
| 2283 | if (Eval->WasEvaluated) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2284 | return Eval->Evaluated.isUninit() ? nullptr : &Eval->Evaluated; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2285 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2286 | const auto *Init = cast<Expr>(Eval->Value); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2287 | assert(!Init->isValueDependent()); |
| 2288 | |
| 2289 | if (Eval->IsEvaluating) { |
| 2290 | // FIXME: Produce a diagnostic for self-initialization. |
| 2291 | Eval->CheckedICE = true; |
| 2292 | Eval->IsICE = false; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2293 | return nullptr; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | Eval->IsEvaluating = true; |
| 2297 | |
| 2298 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), |
| 2299 | this, Notes); |
| 2300 | |
| Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2301 | // Ensure the computed APValue is cleaned up later if evaluation succeeded, |
| 2302 | // or that it's empty (so that there's nothing to clean up) if evaluation |
| 2303 | // failed. |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2304 | if (!Result) |
| 2305 | Eval->Evaluated = APValue(); |
| Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2306 | else if (Eval->Evaluated.needsCleanup()) |
| George Burgess IV | b61bfbd | 2017-02-14 05:37:36 +0000 | [diff] [blame] | 2307 | getASTContext().addDestruction(&Eval->Evaluated); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2308 | |
| 2309 | Eval->IsEvaluating = false; |
| 2310 | Eval->WasEvaluated = true; |
| 2311 | |
| 2312 | // In C++11, we have determined whether the initializer was a constant |
| 2313 | // expression as a side-effect. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2314 | if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) { |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2315 | Eval->CheckedICE = true; |
| Eli Friedman | 8f66cdf | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 2316 | Eval->IsICE = Result && Notes.empty(); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2319 | return Result ? &Eval->Evaluated : nullptr; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2322 | APValue *VarDecl::getEvaluatedValue() const { |
| 2323 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) |
| 2324 | if (Eval->WasEvaluated) |
| 2325 | return &Eval->Evaluated; |
| 2326 | |
| 2327 | return nullptr; |
| 2328 | } |
| 2329 | |
| 2330 | bool VarDecl::isInitKnownICE() const { |
| 2331 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) |
| 2332 | return Eval->CheckedICE; |
| 2333 | |
| 2334 | return false; |
| 2335 | } |
| 2336 | |
| 2337 | bool VarDecl::isInitICE() const { |
| 2338 | assert(isInitKnownICE() && |
| 2339 | "Check whether we already know that the initializer is an ICE"); |
| 2340 | return Init.get<EvaluatedStmt *>()->IsICE; |
| 2341 | } |
| 2342 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2343 | bool VarDecl::checkInitIsICE() const { |
| John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2344 | // Initializers of weak variables are never ICEs. |
| 2345 | if (isWeak()) |
| 2346 | return false; |
| 2347 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2348 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2349 | if (Eval->CheckedICE) |
| 2350 | // We have already checked whether this subexpression is an |
| 2351 | // integral constant expression. |
| 2352 | return Eval->IsICE; |
| 2353 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2354 | const auto *Init = cast<Expr>(Eval->Value); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2355 | assert(!Init->isValueDependent()); |
| 2356 | |
| 2357 | // In C++11, evaluate the initializer to check whether it's a constant |
| 2358 | // expression. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2359 | if (getASTContext().getLangOpts().CPlusPlus11) { |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2360 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2361 | evaluateValue(Notes); |
| 2362 | return Eval->IsICE; |
| 2363 | } |
| 2364 | |
| 2365 | // It's an ICE whether or not the definition we found is |
| 2366 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 2367 | // 6206 for details. |
| 2368 | |
| 2369 | if (Eval->CheckingICE) |
| 2370 | return false; |
| 2371 | Eval->CheckingICE = true; |
| 2372 | |
| 2373 | Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); |
| 2374 | Eval->CheckingICE = false; |
| 2375 | Eval->CheckedICE = true; |
| 2376 | return Eval->IsICE; |
| 2377 | } |
| 2378 | |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2379 | template<typename DeclT> |
| 2380 | static DeclT *getDefinitionOrSelf(DeclT *D) { |
| 2381 | assert(D); |
| 2382 | if (auto *Def = D->getDefinition()) |
| 2383 | return Def; |
| 2384 | return D; |
| 2385 | } |
| 2386 | |
| Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 2387 | bool VarDecl::isEscapingByref() const { |
| 2388 | return hasAttr<BlocksAttr>() && NonParmVarDeclBits.EscapingByref; |
| 2389 | } |
| 2390 | |
| 2391 | bool VarDecl::isNonEscapingByref() const { |
| 2392 | return hasAttr<BlocksAttr>() && !NonParmVarDeclBits.EscapingByref; |
| 2393 | } |
| 2394 | |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2395 | VarDecl *VarDecl::getTemplateInstantiationPattern() const { |
| 2396 | // If it's a variable template specialization, find the template or partial |
| 2397 | // specialization from which it was instantiated. |
| 2398 | if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2399 | auto From = VDTemplSpec->getInstantiatedFrom(); |
| 2400 | if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) { |
| 2401 | while (auto *NewVTD = VTD->getInstantiatedFromMemberTemplate()) { |
| 2402 | if (NewVTD->isMemberSpecialization()) |
| 2403 | break; |
| 2404 | VTD = NewVTD; |
| 2405 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2406 | return getDefinitionOrSelf(VTD->getTemplatedDecl()); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2407 | } |
| 2408 | if (auto *VTPSD = |
| 2409 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 2410 | while (auto *NewVTPSD = VTPSD->getInstantiatedFromMember()) { |
| 2411 | if (NewVTPSD->isMemberSpecialization()) |
| 2412 | break; |
| 2413 | VTPSD = NewVTPSD; |
| 2414 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2415 | return getDefinitionOrSelf<VarDecl>(VTPSD); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
| 2420 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
| 2421 | VarDecl *VD = getInstantiatedFromStaticDataMember(); |
| 2422 | while (auto *NewVD = VD->getInstantiatedFromStaticDataMember()) |
| 2423 | VD = NewVD; |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2424 | return getDefinitionOrSelf(VD); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | if (VarTemplateDecl *VarTemplate = getDescribedVarTemplate()) { |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2429 | while (VarTemplate->getInstantiatedFromMemberTemplate()) { |
| 2430 | if (VarTemplate->isMemberSpecialization()) |
| 2431 | break; |
| 2432 | VarTemplate = VarTemplate->getInstantiatedFromMemberTemplate(); |
| 2433 | } |
| 2434 | |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2435 | return getDefinitionOrSelf(VarTemplate->getTemplatedDecl()); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2436 | } |
| 2437 | return nullptr; |
| 2438 | } |
| 2439 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2440 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2441 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2442 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2443 | |
| 2444 | return nullptr; |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
| Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 2447 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2448 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2449 | return Spec->getSpecializationKind(); |
| 2450 | |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2451 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2452 | return MSI->getTemplateSpecializationKind(); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2453 | |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2454 | return TSK_Undeclared; |
| 2455 | } |
| 2456 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2457 | SourceLocation VarDecl::getPointOfInstantiation() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2458 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2459 | return Spec->getPointOfInstantiation(); |
| 2460 | |
| 2461 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2462 | return MSI->getPointOfInstantiation(); |
| 2463 | |
| 2464 | return SourceLocation(); |
| 2465 | } |
| 2466 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2467 | VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { |
| 2468 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2469 | .dyn_cast<VarTemplateDecl *>(); |
| 2470 | } |
| 2471 | |
| 2472 | void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { |
| 2473 | getASTContext().setTemplateOrSpecializationInfo(this, Template); |
| 2474 | } |
| 2475 | |
| Justin Lebar | 5489f85 | 2018-05-17 16:15:07 +0000 | [diff] [blame] | 2476 | bool VarDecl::isKnownToBeDefined() const { |
| 2477 | const auto &LangOpts = getASTContext().getLangOpts(); |
| 2478 | // In CUDA mode without relocatable device code, variables of form 'extern |
| 2479 | // __shared__ Foo foo[]' are pointers to the base of the GPU core's shared |
| 2480 | // memory pool. These are never undefined variables, even if they appear |
| 2481 | // inside of an anon namespace or static function. |
| 2482 | // |
| 2483 | // With CUDA relocatable device code enabled, these variables don't get |
| 2484 | // special handling; they're treated like regular extern variables. |
| Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2485 | if (LangOpts.CUDA && !LangOpts.GPURelocatableDeviceCode && |
| Justin Lebar | 5489f85 | 2018-05-17 16:15:07 +0000 | [diff] [blame] | 2486 | hasExternalStorage() && hasAttr<CUDASharedAttr>() && |
| 2487 | isa<IncompleteArrayType>(getType())) |
| 2488 | return true; |
| 2489 | |
| 2490 | return hasDefinition(); |
| 2491 | } |
| 2492 | |
| Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 2493 | bool VarDecl::isNoDestroy(const ASTContext &Ctx) const { |
| 2494 | return hasGlobalStorage() && (hasAttr<NoDestroyAttr>() || |
| 2495 | (!Ctx.getLangOpts().RegisterStaticDestructors && |
| 2496 | !hasAttr<AlwaysDestroyAttr>())); |
| 2497 | } |
| 2498 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2499 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
| Richard Smith | b71782b | 2013-08-01 04:12:04 +0000 | [diff] [blame] | 2500 | if (isStaticDataMember()) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2501 | // FIXME: Remove ? |
| 2502 | // return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 2503 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2504 | .dyn_cast<MemberSpecializationInfo *>(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2505 | return nullptr; |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2508 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2509 | SourceLocation PointOfInstantiation) { |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2510 | assert((isa<VarTemplateSpecializationDecl>(this) || |
| 2511 | getMemberSpecializationInfo()) && |
| 2512 | "not a variable or static data member template specialization"); |
| 2513 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2514 | if (VarTemplateSpecializationDecl *Spec = |
| 2515 | dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2516 | Spec->setSpecializationKind(TSK); |
| 2517 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2518 | Spec->getPointOfInstantiation().isInvalid()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2519 | Spec->setPointOfInstantiation(PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2520 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 2521 | L->InstantiationRequested(this); |
| 2522 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { |
| 2526 | MSI->setTemplateSpecializationKind(TSK); |
| 2527 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2528 | MSI->getPointOfInstantiation().isInvalid()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2529 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2530 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 2531 | L->InstantiationRequested(this); |
| 2532 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2533 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | void |
| 2537 | VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, |
| 2538 | TemplateSpecializationKind TSK) { |
| 2539 | assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && |
| 2540 | "Previous template or instantiation?"); |
| 2541 | getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2544 | //===----------------------------------------------------------------------===// |
| 2545 | // ParmVarDecl Implementation |
| 2546 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2547 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2548 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2549 | SourceLocation StartLoc, |
| 2550 | SourceLocation IdLoc, IdentifierInfo *Id, |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2551 | QualType T, TypeSourceInfo *TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2552 | StorageClass S, Expr *DefArg) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2553 | 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] | 2554 | S, DefArg); |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
| Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2557 | QualType ParmVarDecl::getOriginalType() const { |
| 2558 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 2559 | QualType T = TSI ? TSI->getType() : getType(); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2560 | if (const auto *DT = dyn_cast<DecayedType>(T)) |
| Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2561 | return DT->getOriginalType(); |
| 2562 | return T; |
| 2563 | } |
| 2564 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2565 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2566 | return new (C, ID) |
| 2567 | ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), |
| 2568 | nullptr, QualType(), nullptr, SC_None, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
| Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2571 | SourceRange ParmVarDecl::getSourceRange() const { |
| 2572 | if (!hasInheritedDefaultArg()) { |
| 2573 | SourceRange ArgRange = getDefaultArgRange(); |
| 2574 | if (ArgRange.isValid()) |
| 2575 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 2576 | } |
| 2577 | |
| Argyrios Kyrtzidis | a077279 | 2013-04-17 01:56:48 +0000 | [diff] [blame] | 2578 | // DeclaratorDecl considers the range of postfix types as overlapping with the |
| 2579 | // declaration name, but this is not the case with parameters in ObjC methods. |
| 2580 | if (isa<ObjCMethodDecl>(getDeclContext())) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2581 | return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation()); |
| Argyrios Kyrtzidis | a077279 | 2013-04-17 01:56:48 +0000 | [diff] [blame] | 2582 | |
| Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2583 | return DeclaratorDecl::getSourceRange(); |
| 2584 | } |
| 2585 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2586 | Expr *ParmVarDecl::getDefaultArg() { |
| 2587 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 2588 | assert(!hasUninstantiatedDefaultArg() && |
| 2589 | "Default argument is not yet instantiated!"); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2590 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2591 | Expr *Arg = getInit(); |
| Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 2592 | if (auto *E = dyn_cast_or_null<FullExpr>(Arg)) |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2593 | return E->getSubExpr(); |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2594 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2595 | return Arg; |
| 2596 | } |
| 2597 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2598 | void ParmVarDecl::setDefaultArg(Expr *defarg) { |
| 2599 | ParmVarDeclBits.DefaultArgKind = DAK_Normal; |
| 2600 | Init = defarg; |
| 2601 | } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2602 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2603 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 2604 | switch (ParmVarDeclBits.DefaultArgKind) { |
| 2605 | case DAK_None: |
| 2606 | case DAK_Unparsed: |
| 2607 | // Nothing we can do here. |
| 2608 | return SourceRange(); |
| 2609 | |
| 2610 | case DAK_Uninstantiated: |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2611 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 2612 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2613 | case DAK_Normal: |
| 2614 | if (const Expr *E = getInit()) |
| 2615 | return E->getSourceRange(); |
| 2616 | |
| 2617 | // Missing an actual expression, may be invalid. |
| 2618 | return SourceRange(); |
| 2619 | } |
| 2620 | llvm_unreachable("Invalid default argument kind."); |
| 2621 | } |
| 2622 | |
| 2623 | void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) { |
| 2624 | ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated; |
| 2625 | Init = arg; |
| 2626 | } |
| 2627 | |
| 2628 | Expr *ParmVarDecl::getUninstantiatedDefaultArg() { |
| 2629 | assert(hasUninstantiatedDefaultArg() && |
| 2630 | "Wrong kind of initialization expression!"); |
| 2631 | return cast_or_null<Expr>(Init.get<Stmt *>()); |
| 2632 | } |
| 2633 | |
| 2634 | bool ParmVarDecl::hasDefaultArg() const { |
| 2635 | // FIXME: We should just return false for DAK_None here once callers are |
| 2636 | // prepared for the case that we encountered an invalid default argument and |
| 2637 | // were unable to even build an invalid expression. |
| 2638 | return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() || |
| 2639 | !Init.isNull(); |
| Argyrios Kyrtzidis | 02dd4f9 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 2640 | } |
| 2641 | |
| Douglas Gregor | 3c6bd2a | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 2642 | bool ParmVarDecl::isParameterPack() const { |
| 2643 | return isa<PackExpansionType>(getType()); |
| 2644 | } |
| 2645 | |
| Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 2646 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 2647 | getASTContext().setParameterIndex(this, parameterIndex); |
| 2648 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 2649 | } |
| 2650 | |
| 2651 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 2652 | return getASTContext().getParameterIndex(this); |
| 2653 | } |
| 2654 | |
| Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 2655 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2656 | // FunctionDecl Implementation |
| 2657 | //===----------------------------------------------------------------------===// |
| 2658 | |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2659 | FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 2660 | SourceLocation StartLoc, |
| 2661 | const DeclarationNameInfo &NameInfo, QualType T, |
| 2662 | TypeSourceInfo *TInfo, StorageClass S, |
| 2663 | bool isInlineSpecified, bool isConstexprSpecified) |
| 2664 | : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, |
| 2665 | StartLoc), |
| 2666 | DeclContext(DK), redeclarable_base(C), ODRHash(0), |
| 2667 | EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) { |
| Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 2668 | assert(T.isNull() || T->isFunctionType()); |
| Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2669 | FunctionDeclBits.SClass = S; |
| 2670 | FunctionDeclBits.IsInline = isInlineSpecified; |
| 2671 | FunctionDeclBits.IsInlineSpecified = isInlineSpecified; |
| 2672 | FunctionDeclBits.IsExplicitSpecified = false; |
| 2673 | FunctionDeclBits.IsVirtualAsWritten = false; |
| 2674 | FunctionDeclBits.IsPure = false; |
| 2675 | FunctionDeclBits.HasInheritedPrototype = false; |
| 2676 | FunctionDeclBits.HasWrittenPrototype = true; |
| 2677 | FunctionDeclBits.IsDeleted = false; |
| 2678 | FunctionDeclBits.IsTrivial = false; |
| 2679 | FunctionDeclBits.IsTrivialForCall = false; |
| 2680 | FunctionDeclBits.IsDefaulted = false; |
| 2681 | FunctionDeclBits.IsExplicitlyDefaulted = false; |
| 2682 | FunctionDeclBits.HasImplicitReturnZero = false; |
| 2683 | FunctionDeclBits.IsLateTemplateParsed = false; |
| 2684 | FunctionDeclBits.IsConstexpr = isConstexprSpecified; |
| 2685 | FunctionDeclBits.InstantiationIsPending = false; |
| 2686 | FunctionDeclBits.UsesSEHTry = false; |
| 2687 | FunctionDeclBits.HasSkippedBody = false; |
| 2688 | FunctionDeclBits.WillHaveBody = false; |
| 2689 | FunctionDeclBits.IsMultiVersion = false; |
| 2690 | FunctionDeclBits.IsCopyDeductionCandidate = false; |
| 2691 | FunctionDeclBits.HasODRHash = false; |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
| Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 2694 | void FunctionDecl::getNameForDiagnostic( |
| 2695 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 2696 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2697 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 2698 | if (TemplateArgs) |
| Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 2699 | printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy); |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
| Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2702 | bool FunctionDecl::isVariadic() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2703 | if (const auto *FT = getType()->getAs<FunctionProtoType>()) |
| Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2704 | return FT->isVariadic(); |
| 2705 | return false; |
| 2706 | } |
| 2707 | |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2708 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2709 | for (auto I : redecls()) { |
| Serge Pavlov | 9a618e1 | 2017-02-15 12:30:35 +0000 | [diff] [blame] | 2710 | if (I->doesThisDeclarationHaveABody()) { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2711 | Definition = I; |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2712 | return true; |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | return false; |
| 2717 | } |
| 2718 | |
| Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 2719 | bool FunctionDecl::hasTrivialBody() const |
| 2720 | { |
| 2721 | Stmt *S = getBody(); |
| 2722 | if (!S) { |
| 2723 | // Since we don't have a body for this function, we don't know if it's |
| 2724 | // trivial or not. |
| 2725 | return false; |
| 2726 | } |
| 2727 | |
| 2728 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 2729 | return true; |
| 2730 | return false; |
| 2731 | } |
| 2732 | |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2733 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2734 | for (auto I : redecls()) { |
| Serge Pavlov | 10673c9 | 2017-06-04 12:53:12 +0000 | [diff] [blame] | 2735 | if (I->isThisDeclarationADefinition()) { |
| 2736 | Definition = I; |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2737 | return true; |
| 2738 | } |
| 2739 | } |
| 2740 | |
| 2741 | return false; |
| 2742 | } |
| 2743 | |
| Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2744 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2745 | if (!hasBody(Definition)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2746 | return nullptr; |
| Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2747 | |
| 2748 | if (Definition->Body) |
| 2749 | return Definition->Body.get(getASTContext().getExternalSource()); |
| Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2750 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2751 | return nullptr; |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2754 | void FunctionDecl::setBody(Stmt *B) { |
| 2755 | Body = B; |
| Douglas Gregor | 027ba50 | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 2756 | if (B) |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2757 | EndRangeLoc = B->getEndLoc(); |
| Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2760 | void FunctionDecl::setPure(bool P) { |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2761 | FunctionDeclBits.IsPure = P; |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2762 | if (P) |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2763 | if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2764 | Parent->markedVirtualFunctionPure(); |
| 2765 | } |
| 2766 | |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2767 | template<std::size_t Len> |
| 2768 | static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { |
| 2769 | IdentifierInfo *II = ND->getIdentifier(); |
| 2770 | return II && II->isStr(Str); |
| 2771 | } |
| 2772 | |
| Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 2773 | bool FunctionDecl::isMain() const { |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2774 | const TranslationUnitDecl *tunit = |
| 2775 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2776 | return tunit && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2777 | !tunit->getASTContext().getLangOpts().Freestanding && |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2778 | isNamed(this, "main"); |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 2781 | bool FunctionDecl::isMSVCRTEntryPoint() const { |
| 2782 | const TranslationUnitDecl *TUnit = |
| 2783 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2784 | if (!TUnit) |
| 2785 | return false; |
| 2786 | |
| 2787 | // Even though we aren't really targeting MSVCRT if we are freestanding, |
| 2788 | // semantic analysis for these functions remains the same. |
| 2789 | |
| 2790 | // MSVCRT entry points only exist on MSVCRT targets. |
| 2791 | if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) |
| 2792 | return false; |
| 2793 | |
| 2794 | // Nameless functions like constructors cannot be entry points. |
| 2795 | if (!getIdentifier()) |
| 2796 | return false; |
| 2797 | |
| 2798 | return llvm::StringSwitch<bool>(getName()) |
| 2799 | .Cases("main", // an ANSI console app |
| 2800 | "wmain", // a Unicode console App |
| 2801 | "WinMain", // an ANSI GUI app |
| 2802 | "wWinMain", // a Unicode GUI app |
| 2803 | "DllMain", // a DLL |
| 2804 | true) |
| 2805 | .Default(false); |
| 2806 | } |
| 2807 | |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2808 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 2809 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 2810 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 2811 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 2812 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 2813 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 2814 | |
| Richard Smith | f600441 | 2014-01-19 23:25:37 +0000 | [diff] [blame] | 2815 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2816 | return false; |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2817 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2818 | const auto *proto = getType()->castAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2819 | if (proto->getNumParams() != 2 || proto->isVariadic()) |
| 2820 | return false; |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2821 | |
| 2822 | ASTContext &Context = |
| 2823 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 2824 | ->getASTContext(); |
| 2825 | |
| 2826 | // The result type and first argument type are constant across all |
| 2827 | // these operators. The second argument must be exactly void*. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2828 | return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
| Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2831 | bool FunctionDecl::isReplaceableGlobalAllocationFunction(bool *IsAligned) const { |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2832 | if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) |
| 2833 | return false; |
| 2834 | if (getDeclName().getCXXOverloadedOperator() != OO_New && |
| 2835 | getDeclName().getCXXOverloadedOperator() != OO_Delete && |
| 2836 | getDeclName().getCXXOverloadedOperator() != OO_Array_New && |
| 2837 | getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
| 2838 | return false; |
| 2839 | |
| 2840 | if (isa<CXXRecordDecl>(getDeclContext())) |
| 2841 | return false; |
| Richard Smith | f600441 | 2014-01-19 23:25:37 +0000 | [diff] [blame] | 2842 | |
| 2843 | // This can only fail for an invalid 'operator new' declaration. |
| 2844 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2845 | return false; |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2846 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2847 | const auto *FPT = getType()->castAs<FunctionProtoType>(); |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2848 | if (FPT->getNumParams() == 0 || FPT->getNumParams() > 3 || FPT->isVariadic()) |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2849 | return false; |
| 2850 | |
| 2851 | // If this is a single-parameter function, it must be a replaceable global |
| 2852 | // allocation or deallocation function. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2853 | if (FPT->getNumParams() == 1) |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2854 | return true; |
| 2855 | |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2856 | unsigned Params = 1; |
| 2857 | QualType Ty = FPT->getParamType(Params); |
| Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2858 | ASTContext &Ctx = getASTContext(); |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2859 | |
| 2860 | auto Consume = [&] { |
| 2861 | ++Params; |
| 2862 | Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType(); |
| 2863 | }; |
| 2864 | |
| 2865 | // In C++14, the next parameter can be a 'std::size_t' for sized delete. |
| 2866 | bool IsSizedDelete = false; |
| Richard Smith | b47c36f | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 2867 | if (Ctx.getLangOpts().SizedDeallocation && |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2868 | (getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 2869 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) && |
| 2870 | Ctx.hasSameType(Ty, Ctx.getSizeType())) { |
| 2871 | IsSizedDelete = true; |
| 2872 | Consume(); |
| 2873 | } |
| 2874 | |
| 2875 | // In C++17, the next parameter can be a 'std::align_val_t' for aligned |
| 2876 | // new/delete. |
| Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2877 | if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) { |
| 2878 | if (IsAligned) |
| 2879 | *IsAligned = true; |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2880 | Consume(); |
| Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2881 | } |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2882 | |
| 2883 | // Finally, if this is not a sized delete, the final parameter can |
| 2884 | // be a 'const std::nothrow_t&'. |
| 2885 | if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) { |
| 2886 | Ty = Ty->getPointeeType(); |
| 2887 | if (Ty.getCVRQualifiers() != Qualifiers::Const) |
| 2888 | return false; |
| 2889 | const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 2890 | if (RD && isNamed(RD, "nothrow_t") && RD->isInStdNamespace()) |
| 2891 | Consume(); |
| 2892 | } |
| 2893 | |
| 2894 | return Params == FPT->getNumParams(); |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
| Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 2897 | bool FunctionDecl::isDestroyingOperatorDelete() const { |
| 2898 | // C++ P0722: |
| 2899 | // Within a class C, a single object deallocation function with signature |
| 2900 | // (T, std::destroying_delete_t, <more params>) |
| 2901 | // is a destroying operator delete. |
| 2902 | if (!isa<CXXMethodDecl>(this) || getOverloadedOperator() != OO_Delete || |
| 2903 | getNumParams() < 2) |
| 2904 | return false; |
| 2905 | |
| 2906 | auto *RD = getParamDecl(1)->getType()->getAsCXXRecordDecl(); |
| 2907 | return RD && RD->isInStdNamespace() && RD->getIdentifier() && |
| 2908 | RD->getIdentifier()->isStr("destroying_delete_t"); |
| 2909 | } |
| 2910 | |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2911 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2912 | return getDeclLanguageLinkage(*this); |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2915 | bool FunctionDecl::isExternC() const { |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2916 | return isDeclExternC(*this); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2919 | bool FunctionDecl::isInExternCContext() const { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2920 | return getLexicalDeclContext()->isExternCContext(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | bool FunctionDecl::isInExternCXXContext() const { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2924 | return getLexicalDeclContext()->isExternCXXContext(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2927 | bool FunctionDecl::isGlobal() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2928 | if (const auto *Method = dyn_cast<CXXMethodDecl>(this)) |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2929 | return Method->isStatic(); |
| 2930 | |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2931 | if (getCanonicalDecl()->getStorageClass() == SC_Static) |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2932 | return false; |
| 2933 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2934 | for (const DeclContext *DC = getDeclContext(); |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2935 | DC->isNamespace(); |
| 2936 | DC = DC->getParent()) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2937 | if (const auto *Namespace = cast<NamespaceDecl>(DC)) { |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2938 | if (!Namespace->getDeclName()) |
| 2939 | return false; |
| 2940 | break; |
| 2941 | } |
| 2942 | } |
| 2943 | |
| 2944 | return true; |
| 2945 | } |
| 2946 | |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2947 | bool FunctionDecl::isNoReturn() const { |
| Adrian Prantl | 721c7cb | 2016-08-17 16:42:15 +0000 | [diff] [blame] | 2948 | if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
| 2949 | hasAttr<C11NoReturnAttr>()) |
| 2950 | return true; |
| 2951 | |
| 2952 | if (auto *FnTy = getType()->getAs<FunctionType>()) |
| 2953 | return FnTy->getNoReturnAttr(); |
| 2954 | |
| 2955 | return false; |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
| Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 2958 | |
| 2959 | MultiVersionKind FunctionDecl::getMultiVersionKind() const { |
| 2960 | if (hasAttr<TargetAttr>()) |
| 2961 | return MultiVersionKind::Target; |
| 2962 | if (hasAttr<CPUDispatchAttr>()) |
| 2963 | return MultiVersionKind::CPUDispatch; |
| 2964 | if (hasAttr<CPUSpecificAttr>()) |
| 2965 | return MultiVersionKind::CPUSpecific; |
| 2966 | return MultiVersionKind::None; |
| 2967 | } |
| 2968 | |
| Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 2969 | bool FunctionDecl::isCPUDispatchMultiVersion() const { |
| 2970 | return isMultiVersion() && hasAttr<CPUDispatchAttr>(); |
| 2971 | } |
| 2972 | |
| 2973 | bool FunctionDecl::isCPUSpecificMultiVersion() const { |
| 2974 | return isMultiVersion() && hasAttr<CPUSpecificAttr>(); |
| 2975 | } |
| 2976 | |
| Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 2977 | bool FunctionDecl::isTargetMultiVersion() const { |
| 2978 | return isMultiVersion() && hasAttr<TargetAttr>(); |
| 2979 | } |
| 2980 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2981 | void |
| 2982 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2983 | redeclarable_base::setPreviousDecl(PrevDecl); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2984 | |
| 2985 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 2986 | FunctionTemplateDecl *PrevFunTmpl |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2987 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2988 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2989 | FunTmpl->setPreviousDecl(PrevFunTmpl); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2990 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2991 | |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2992 | if (PrevDecl && PrevDecl->isInlined()) |
| 2993 | setImplicitlyInline(true); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2996 | FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2997 | |
| Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 2998 | /// Returns a value indicating whether this function corresponds to a builtin |
| 2999 | /// function. |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 3000 | /// |
| Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3001 | /// The function corresponds to a built-in function if it is declared at |
| 3002 | /// translation scope or within an extern "C" block and its name matches with |
| 3003 | /// the name of a builtin. The returned value will be 0 for functions that do |
| 3004 | /// not correspond to a builtin, a value of type \c Builtin::ID if in the |
| 3005 | /// target-independent range \c [1,Builtin::First), or a target-specific builtin |
| 3006 | /// value. |
| 3007 | /// |
| 3008 | /// \param ConsiderWrapperFunctions If true, we should consider wrapper |
| 3009 | /// functions as their wrapped builtins. This shouldn't be done in general, but |
| 3010 | /// it's useful in Sema to diagnose calls to wrappers based on their semantics. |
| 3011 | unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { |
| Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 3012 | if (!getIdentifier()) |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 3013 | return 0; |
| 3014 | |
| 3015 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
| Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 3016 | if (!BuiltinID) |
| 3017 | return 0; |
| 3018 | |
| 3019 | ASTContext &Context = getASTContext(); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3020 | if (Context.getLangOpts().CPlusPlus) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3021 | const auto *LinkageDecl = |
| 3022 | dyn_cast<LinkageSpecDecl>(getFirstDecl()->getDeclContext()); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3023 | // In C++, the first declaration of a builtin is always inside an implicit |
| 3024 | // extern "C". |
| 3025 | // FIXME: A recognised library function may not be directly in an extern "C" |
| 3026 | // declaration, for instance "extern "C" { namespace std { decl } }". |
| David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 3027 | if (!LinkageDecl) { |
| 3028 | if (BuiltinID == Builtin::BI__GetExceptionInfo && |
| David Majnemer | 145abde | 2016-01-26 01:12:17 +0000 | [diff] [blame] | 3029 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 3030 | return Builtin::BI__GetExceptionInfo; |
| 3031 | return 0; |
| 3032 | } |
| 3033 | if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c) |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3034 | return 0; |
| 3035 | } |
| 3036 | |
| 3037 | // If the function is marked "overloadable", it has a different mangled name |
| 3038 | // and is not the C library function. |
| Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3039 | if (!ConsiderWrapperFunctions && hasAttr<OverloadableAttr>()) |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3040 | return 0; |
| 3041 | |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 3042 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 3043 | return BuiltinID; |
| 3044 | |
| 3045 | // This function has the name of a known C library |
| 3046 | // function. Determine whether it actually refers to the C library |
| 3047 | // function or whether it just has the same name. |
| 3048 | |
| Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 3049 | // If this is a static function, it's not a builtin. |
| Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3050 | if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) |
| Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 3051 | return 0; |
| 3052 | |
| Anastasia Stulova | 1202de3 | 2016-02-12 12:07:04 +0000 | [diff] [blame] | 3053 | // OpenCL v1.2 s6.9.f - The library functions defined in |
| 3054 | // the C99 standard headers are not available. |
| 3055 | if (Context.getLangOpts().OpenCL && |
| 3056 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 3057 | return 0; |
| 3058 | |
| Artem Belevich | 5ecdb94 | 2018-01-23 19:08:18 +0000 | [diff] [blame] | 3059 | // CUDA does not have device-side standard library. printf and malloc are the |
| 3060 | // only special cases that are supported by device-side runtime. |
| 3061 | if (Context.getLangOpts().CUDA && hasAttr<CUDADeviceAttr>() && |
| 3062 | !hasAttr<CUDAHostAttr>() && |
| 3063 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
| 3064 | return 0; |
| 3065 | |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3066 | return BuiltinID; |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 3069 | /// getNumParams - Return the number of parameters this function must have |
| Bob Wilson | b39017a | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 3070 | /// based on its FunctionType. This is the length of the ParamInfo array |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 3071 | /// after it has been created. |
| 3072 | unsigned FunctionDecl::getNumParams() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3073 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3074 | return FPT ? FPT->getNumParams() : 0; |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
| Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3077 | void FunctionDecl::setParams(ASTContext &C, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3078 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3079 | assert(!ParamInfo && "Already has param info!"); |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3080 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3081 | |
| Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 3082 | // Zero params -> null pointer. |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3083 | if (!NewParamInfo.empty()) { |
| 3084 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 3085 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
| Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 3086 | } |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 3087 | } |
| Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 3088 | |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3089 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 3090 | /// needed to call this function. This may be fewer than the number of |
| 3091 | /// function parameters, if some of the parameters have default |
| Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 3092 | /// arguments (in C++) or are parameter packs (C++11). |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3093 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3094 | if (!getASTContext().getLangOpts().CPlusPlus) |
| Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3095 | return getNumParams(); |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3096 | |
| Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 3097 | unsigned NumRequiredArgs = 0; |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3098 | for (auto *Param : parameters()) |
| Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 3099 | if (!Param->isParameterPack() && !Param->hasDefaultArg()) |
| 3100 | ++NumRequiredArgs; |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3101 | return NumRequiredArgs; |
| 3102 | } |
| 3103 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3104 | /// The combination of the extern and inline keywords under MSVC forces |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3105 | /// the function to be required. |
| 3106 | /// |
| 3107 | /// Note: This function assumes that we will only get called when isInlined() |
| 3108 | /// would return true for this FunctionDecl. |
| 3109 | bool FunctionDecl::isMSExternInline() const { |
| 3110 | assert(isInlined() && "expected to get called on an inlined function!"); |
| 3111 | |
| 3112 | const ASTContext &Context = getASTContext(); |
| David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 3113 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 3114 | !hasAttr<DLLExportAttr>()) |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3115 | return false; |
| 3116 | |
| David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 3117 | for (const FunctionDecl *FD = getMostRecentDecl(); FD; |
| 3118 | FD = FD->getPreviousDecl()) |
| David Majnemer | c0c42f3 | 2015-07-10 20:55:38 +0000 | [diff] [blame] | 3119 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3120 | return true; |
| 3121 | |
| 3122 | return false; |
| 3123 | } |
| 3124 | |
| 3125 | static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { |
| 3126 | if (Redecl->getStorageClass() != SC_Extern) |
| 3127 | return false; |
| 3128 | |
| 3129 | for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; |
| 3130 | FD = FD->getPreviousDecl()) |
| David Majnemer | c0c42f3 | 2015-07-10 20:55:38 +0000 | [diff] [blame] | 3131 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3132 | return false; |
| 3133 | |
| 3134 | return true; |
| 3135 | } |
| 3136 | |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3137 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 3138 | // Only consider file-scope declarations in this test. |
| 3139 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 3140 | return false; |
| 3141 | |
| 3142 | // Only consider explicit declarations; the presence of a builtin for a |
| 3143 | // libcall shouldn't affect whether a definition is externally visible. |
| 3144 | if (Redecl->isImplicit()) |
| 3145 | return false; |
| 3146 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3147 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3148 | return true; // Not an inline definition |
| 3149 | |
| 3150 | return false; |
| 3151 | } |
| 3152 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3153 | /// For a function declaration in C or C++, determine whether this |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3154 | /// declaration causes the definition to be externally visible. |
| 3155 | /// |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3156 | /// For instance, this determines if adding the current declaration to the set |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3157 | /// of redeclarations of the given functions causes |
| 3158 | /// isInlineDefinitionExternallyVisible to change from false to true. |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3159 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| Justin Lebar | 0241c96 | 2016-10-28 16:46:39 +0000 | [diff] [blame] | 3160 | assert(!doesThisDeclarationHaveABody() && |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3161 | "Must have a declaration without a body."); |
| 3162 | |
| 3163 | ASTContext &Context = getASTContext(); |
| 3164 | |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3165 | if (Context.getLangOpts().MSVCCompat) { |
| 3166 | const FunctionDecl *Definition; |
| 3167 | if (hasBody(Definition) && Definition->isInlined() && |
| 3168 | redeclForcesDefMSVC(this)) |
| 3169 | return true; |
| 3170 | } |
| 3171 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3172 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3173 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 3174 | // an externally visible definition. |
| 3175 | // |
| 3176 | // FIXME: What happens if gnu_inline gets added on after the first |
| 3177 | // declaration? |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3178 | if (!isInlineSpecified() || getStorageClass() == SC_Extern) |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3179 | return false; |
| 3180 | |
| 3181 | const FunctionDecl *Prev = this; |
| 3182 | bool FoundBody = false; |
| 3183 | while ((Prev = Prev->getPreviousDecl())) { |
| David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3184 | FoundBody |= Prev->Body.isValid(); |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3185 | |
| 3186 | if (Prev->Body) { |
| 3187 | // If it's not the case that both 'inline' and 'extern' are |
| 3188 | // specified on the definition, then it is always externally visible. |
| 3189 | if (!Prev->isInlineSpecified() || |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3190 | Prev->getStorageClass() != SC_Extern) |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3191 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3192 | } else if (Prev->isInlineSpecified() && |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3193 | Prev->getStorageClass() != SC_Extern) { |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3194 | return false; |
| 3195 | } |
| 3196 | } |
| 3197 | return FoundBody; |
| 3198 | } |
| 3199 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3200 | if (Context.getLangOpts().CPlusPlus) |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3201 | return false; |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3202 | |
| 3203 | // C99 6.7.4p6: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3204 | // [...] If all of the file scope declarations for a function in a |
| 3205 | // translation unit include the inline function specifier without extern, |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3206 | // then the definition in that translation unit is an inline definition. |
| 3207 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3208 | return false; |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3209 | const FunctionDecl *Prev = this; |
| 3210 | bool FoundBody = false; |
| 3211 | while ((Prev = Prev->getPreviousDecl())) { |
| David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3212 | FoundBody |= Prev->Body.isValid(); |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3213 | if (RedeclForcesDefC99(Prev)) |
| 3214 | return false; |
| 3215 | } |
| 3216 | return FoundBody; |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3219 | SourceRange FunctionDecl::getReturnTypeSourceRange() const { |
| 3220 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 3221 | if (!TSI) |
| 3222 | return SourceRange(); |
| Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3223 | FunctionTypeLoc FTL = |
| 3224 | TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>(); |
| 3225 | if (!FTL) |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3226 | return SourceRange(); |
| 3227 | |
| Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3228 | // Skip self-referential return types. |
| 3229 | const SourceManager &SM = getASTContext().getSourceManager(); |
| 3230 | SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3231 | SourceLocation Boundary = getNameInfo().getBeginLoc(); |
| Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3232 | if (RTRange.isInvalid() || Boundary.isInvalid() || |
| 3233 | !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) |
| 3234 | return SourceRange(); |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3235 | |
| Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3236 | return RTRange; |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| Malcolm Parsons | a3220ce | 2017-01-12 16:11:28 +0000 | [diff] [blame] | 3239 | SourceRange FunctionDecl::getExceptionSpecSourceRange() const { |
| 3240 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 3241 | if (!TSI) |
| 3242 | return SourceRange(); |
| 3243 | FunctionTypeLoc FTL = |
| 3244 | TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>(); |
| 3245 | if (!FTL) |
| 3246 | return SourceRange(); |
| 3247 | |
| 3248 | return FTL.getExceptionSpecRange(); |
| 3249 | } |
| 3250 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3251 | /// For an inline function definition in C, or for a gnu_inline function |
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3252 | /// in C++, determine whether the definition will be externally visible. |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3253 | /// |
| 3254 | /// Inline function definitions are always available for inlining optimizations. |
| 3255 | /// However, depending on the language dialect, declaration specifiers, and |
| 3256 | /// attributes, the definition of an inline function may or may not be |
| 3257 | /// "externally" visible to other translation units in the program. |
| 3258 | /// |
| 3259 | /// In C99, inline definitions are not externally visible by default. However, |
| Mike Stump | 13c6670 | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 3260 | /// 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] | 3261 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 3262 | /// |
| 3263 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3264 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 3265 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 3266 | /// an externally visible symbol, but "extern inline" will not create an |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3267 | /// externally visible symbol. |
| 3268 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| Manuel Klimek | da7456a | 2016-11-01 10:30:50 +0000 | [diff] [blame] | 3269 | assert((doesThisDeclarationHaveABody() || willHaveBody()) && |
| 3270 | "Must be a function definition"); |
| Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 3271 | assert(isInlined() && "Function must be inline"); |
| Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 3272 | ASTContext &Context = getASTContext(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3273 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3274 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3275 | // Note: If you change the logic here, please change |
| 3276 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 3277 | // |
| Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3278 | // If it's not the case that both 'inline' and 'extern' are |
| 3279 | // specified on the definition, then this inline definition is |
| 3280 | // externally visible. |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3281 | if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) |
| Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3282 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3283 | |
| Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3284 | // If any declaration is 'inline' but not 'extern', then this definition |
| 3285 | // is externally visible. |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3286 | for (auto Redecl : redecls()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3287 | if (Redecl->isInlineSpecified() && |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3288 | Redecl->getStorageClass() != SC_Extern) |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3289 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 3292 | return false; |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3293 | } |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3294 | |
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3295 | // The rest of this function is C-only. |
| 3296 | assert(!Context.getLangOpts().CPlusPlus && |
| 3297 | "should not use C inline rules in C++"); |
| 3298 | |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3299 | // C99 6.7.4p6: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3300 | // [...] If all of the file scope declarations for a function in a |
| 3301 | // translation unit include the inline function specifier without extern, |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3302 | // then the definition in that translation unit is an inline definition. |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3303 | for (auto Redecl : redecls()) { |
| 3304 | if (RedeclForcesDefC99(Redecl)) |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3305 | return true; |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3306 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3307 | |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3308 | // C99 6.7.4p6: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3309 | // An inline definition does not provide an external definition for the |
| 3310 | // function, and does not forbid an external definition in another |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3311 | // translation unit. |
| Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 3312 | return false; |
| 3313 | } |
| 3314 | |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3315 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 3316 | /// function represents, if any. |
| 3317 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
| Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 3318 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 3319 | return getDeclName().getCXXOverloadedOperator(); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3320 | else |
| 3321 | return OO_None; |
| 3322 | } |
| 3323 | |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 3324 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 3325 | /// represents, if any. |
| 3326 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 3327 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 3328 | return getDeclName().getCXXLiteralIdentifier(); |
| 3329 | else |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3330 | return nullptr; |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 3333 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 3334 | if (TemplateOrSpecialization.isNull()) |
| 3335 | return TK_NonTemplate; |
| 3336 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 3337 | return TK_FunctionTemplate; |
| 3338 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 3339 | return TK_MemberSpecialization; |
| 3340 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 3341 | return TK_FunctionTemplateSpecialization; |
| 3342 | if (TemplateOrSpecialization.is |
| 3343 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 3344 | return TK_DependentFunctionTemplateSpecialization; |
| 3345 | |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3346 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
| Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3349 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3350 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3351 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3352 | |
| 3353 | return nullptr; |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3356 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 3357 | return TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>(); |
| 3358 | } |
| 3359 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3360 | void |
| Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3361 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 3362 | FunctionDecl *FD, |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3363 | TemplateSpecializationKind TSK) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3364 | assert(TemplateOrSpecialization.isNull() && |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3365 | "Member function is already a specialization"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3366 | MemberSpecializationInfo *Info |
| Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3367 | = new (C) MemberSpecializationInfo(FD, TSK); |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3368 | TemplateOrSpecialization = Info; |
| 3369 | } |
| 3370 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3371 | FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const { |
| 3372 | return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl *>(); |
| 3373 | } |
| 3374 | |
| 3375 | void FunctionDecl::setDescribedFunctionTemplate(FunctionTemplateDecl *Template) { |
| 3376 | TemplateOrSpecialization = Template; |
| 3377 | } |
| 3378 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3379 | bool FunctionDecl::isImplicitlyInstantiable() const { |
| Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 3380 | // If the function is invalid, it can't be implicitly instantiated. |
| 3381 | if (isInvalidDecl()) |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3382 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3383 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3384 | switch (getTemplateSpecializationKind()) { |
| 3385 | case TSK_Undeclared: |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3386 | case TSK_ExplicitInstantiationDefinition: |
| 3387 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3388 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3389 | case TSK_ImplicitInstantiation: |
| 3390 | return true; |
| 3391 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3392 | // It is possible to instantiate TSK_ExplicitSpecialization kind |
| 3393 | // if the FunctionDecl has a class scope specialization pattern. |
| 3394 | case TSK_ExplicitSpecialization: |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3395 | return getClassScopeSpecializationPattern() != nullptr; |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3396 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3397 | case TSK_ExplicitInstantiationDeclaration: |
| 3398 | // Handled below. |
| 3399 | break; |
| 3400 | } |
| 3401 | |
| 3402 | // Find the actual template from which we will instantiate. |
| 3403 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3404 | bool HasPattern = false; |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3405 | if (PatternDecl) |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3406 | HasPattern = PatternDecl->hasBody(PatternDecl); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3407 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3408 | // C++0x [temp.explicit]p9: |
| 3409 | // Except for inline functions, other explicit instantiation declarations |
| 3410 | // have the effect of suppressing the implicit instantiation of the entity |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3411 | // to which they refer. |
| 3412 | if (!HasPattern || !PatternDecl) |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3413 | return true; |
| 3414 | |
| Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 3415 | return PatternDecl->isInlined(); |
| Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | bool FunctionDecl::isTemplateInstantiation() const { |
| 3419 | switch (getTemplateSpecializationKind()) { |
| 3420 | case TSK_Undeclared: |
| 3421 | case TSK_ExplicitSpecialization: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3422 | return false; |
| Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 3423 | case TSK_ImplicitInstantiation: |
| 3424 | case TSK_ExplicitInstantiationDeclaration: |
| 3425 | case TSK_ExplicitInstantiationDefinition: |
| 3426 | return true; |
| 3427 | } |
| 3428 | llvm_unreachable("All TSK values handled."); |
| 3429 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3430 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3431 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3432 | // Handle class scope explicit specialization special case. |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3433 | if (getTemplateSpecializationKind() == TSK_ExplicitSpecialization) { |
| 3434 | if (auto *Spec = getClassScopeSpecializationPattern()) |
| 3435 | return getDefinitionOrSelf(Spec); |
| 3436 | return nullptr; |
| 3437 | } |
| 3438 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3439 | // If this is a generic lambda call operator specialization, its |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3440 | // instantiation pattern is always its primary template's pattern |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3441 | // even if its primary template was instantiated from another |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3442 | // member template (which happens with nested generic lambdas). |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3443 | // Since a lambda's call operator's body is transformed eagerly, |
| 3444 | // we don't have to go hunting for a prototype definition template |
| 3445 | // (i.e. instantiated-from-member-template) to use as an instantiation |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3446 | // pattern. |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3447 | |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3448 | if (isGenericLambdaCallOperatorSpecialization( |
| 3449 | dyn_cast<CXXMethodDecl>(this))) { |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3450 | assert(getPrimaryTemplate() && "not a generic lambda call operator?"); |
| 3451 | return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl()); |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3452 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3453 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3454 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 3455 | while (Primary->getInstantiatedFromMemberTemplate()) { |
| 3456 | // If we have hit a point where the user provided a specialization of |
| 3457 | // this template, we're done looking. |
| 3458 | if (Primary->isMemberSpecialization()) |
| 3459 | break; |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3460 | Primary = Primary->getInstantiatedFromMemberTemplate(); |
| 3461 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3462 | |
| 3463 | return getDefinitionOrSelf(Primary->getTemplatedDecl()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3464 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3465 | |
| 3466 | if (auto *MFD = getInstantiatedFromMemberFunction()) |
| 3467 | return getDefinitionOrSelf(MFD); |
| 3468 | |
| 3469 | return nullptr; |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3472 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | if (FunctionTemplateSpecializationInfo *Info |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3474 | = TemplateOrSpecialization |
| 3475 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3476 | return Info->Template.getPointer(); |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3477 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3478 | return nullptr; |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3481 | FunctionDecl *FunctionDecl::getClassScopeSpecializationPattern() const { |
| 3482 | return getASTContext().getClassScopeSpecializationPattern(this); |
| 3483 | } |
| 3484 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3485 | FunctionTemplateSpecializationInfo * |
| 3486 | FunctionDecl::getTemplateSpecializationInfo() const { |
| 3487 | return TemplateOrSpecialization |
| 3488 | .dyn_cast<FunctionTemplateSpecializationInfo *>(); |
| 3489 | } |
| 3490 | |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3491 | const TemplateArgumentList * |
| 3492 | FunctionDecl::getTemplateSpecializationArgs() const { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | if (FunctionTemplateSpecializationInfo *Info |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 3494 | = TemplateOrSpecialization |
| 3495 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3496 | return Info->TemplateArguments; |
| 3497 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3498 | return nullptr; |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
| Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 3501 | const ASTTemplateArgumentListInfo * |
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3502 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 3503 | if (FunctionTemplateSpecializationInfo *Info |
| 3504 | = TemplateOrSpecialization |
| 3505 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 3506 | return Info->TemplateArgumentsAsWritten; |
| 3507 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3508 | return nullptr; |
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3511 | void |
| Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3512 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 3513 | FunctionTemplateDecl *Template, |
| Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3514 | const TemplateArgumentList *TemplateArgs, |
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3515 | void *InsertPos, |
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3516 | TemplateSpecializationKind TSK, |
| Argyrios Kyrtzidis | 927d8e0 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 3517 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 3518 | SourceLocation PointOfInstantiation) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3519 | assert(TSK != TSK_Undeclared && |
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3520 | "Must specify the type of function template specialization"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | FunctionTemplateSpecializationInfo *Info |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3522 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3523 | if (!Info) |
| Argyrios Kyrtzidis | e262a95 | 2010-09-09 11:28:23 +0000 | [diff] [blame] | 3524 | Info = FunctionTemplateSpecializationInfo::Create(C, this, Template, TSK, |
| 3525 | TemplateArgs, |
| 3526 | TemplateArgsAsWritten, |
| 3527 | PointOfInstantiation); |
| Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3528 | TemplateOrSpecialization = Info; |
| Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 3529 | Template->addSpecialization(Info, InsertPos); |
| Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3532 | void |
| 3533 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 3534 | const UnresolvedSetImpl &Templates, |
| 3535 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3536 | assert(TemplateOrSpecialization.isNull()); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3537 | DependentFunctionTemplateSpecializationInfo *Info = |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3538 | DependentFunctionTemplateSpecializationInfo::Create(Context, Templates, |
| 3539 | TemplateArgs); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3540 | TemplateOrSpecialization = Info; |
| 3541 | } |
| 3542 | |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3543 | DependentFunctionTemplateSpecializationInfo * |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3544 | FunctionDecl::getDependentSpecializationInfo() const { |
| 3545 | return TemplateOrSpecialization |
| 3546 | .dyn_cast<DependentFunctionTemplateSpecializationInfo *>(); |
| 3547 | } |
| 3548 | |
| 3549 | DependentFunctionTemplateSpecializationInfo * |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3550 | DependentFunctionTemplateSpecializationInfo::Create( |
| 3551 | ASTContext &Context, const UnresolvedSetImpl &Ts, |
| 3552 | const TemplateArgumentListInfo &TArgs) { |
| 3553 | void *Buffer = Context.Allocate( |
| 3554 | totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>( |
| 3555 | TArgs.size(), Ts.size())); |
| 3556 | return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs); |
| 3557 | } |
| 3558 | |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3559 | DependentFunctionTemplateSpecializationInfo:: |
| 3560 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 3561 | const TemplateArgumentListInfo &TArgs) |
| Benjamin Kramer | 3ebba52 | 2015-04-02 12:43:31 +0000 | [diff] [blame] | 3562 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| James Y Knight | 53c7616 | 2015-07-17 18:21:37 +0000 | [diff] [blame] | 3563 | NumTemplates = Ts.size(); |
| 3564 | NumArgs = TArgs.size(); |
| Benjamin Kramer | 3ebba52 | 2015-04-02 12:43:31 +0000 | [diff] [blame] | 3565 | |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3566 | FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>(); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3567 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 3568 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 3569 | |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3570 | TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>(); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3571 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 3572 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 3573 | } |
| 3574 | |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3575 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | // For a function template specialization, query the specialization |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3577 | // information object. |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3578 | FunctionTemplateSpecializationInfo *FTSInfo |
| Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3579 | = TemplateOrSpecialization.dyn_cast<FunctionTemplateSpecializationInfo*>(); |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3580 | if (FTSInfo) |
| 3581 | return FTSInfo->getTemplateSpecializationKind(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3582 | |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3583 | MemberSpecializationInfo *MSInfo |
| 3584 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>(); |
| 3585 | if (MSInfo) |
| 3586 | return MSInfo->getTemplateSpecializationKind(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3587 | |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3588 | return TSK_Undeclared; |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3591 | void |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3592 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 3593 | SourceLocation PointOfInstantiation) { |
| 3594 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3595 | = TemplateOrSpecialization.dyn_cast< |
| 3596 | FunctionTemplateSpecializationInfo*>()) { |
| 3597 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 3598 | if (TSK != TSK_ExplicitSpecialization && |
| 3599 | PointOfInstantiation.isValid() && |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3600 | FTSInfo->getPointOfInstantiation().isInvalid()) { |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3601 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3602 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 3603 | L->InstantiationRequested(this); |
| 3604 | } |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3605 | } else if (MemberSpecializationInfo *MSInfo |
| 3606 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 3607 | MSInfo->setTemplateSpecializationKind(TSK); |
| 3608 | if (TSK != TSK_ExplicitSpecialization && |
| 3609 | PointOfInstantiation.isValid() && |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3610 | MSInfo->getPointOfInstantiation().isInvalid()) { |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3611 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3612 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 3613 | L->InstantiationRequested(this); |
| 3614 | } |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3615 | } else |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3616 | llvm_unreachable("Function cannot have a template specialization kind"); |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3620 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3621 | = TemplateOrSpecialization.dyn_cast< |
| 3622 | FunctionTemplateSpecializationInfo*>()) |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3623 | return FTSInfo->getPointOfInstantiation(); |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3624 | else if (MemberSpecializationInfo *MSInfo |
| 3625 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3626 | return MSInfo->getPointOfInstantiation(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3627 | |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3628 | return SourceLocation(); |
| Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3631 | bool FunctionDecl::isOutOfLine() const { |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3632 | if (Decl::isOutOfLine()) |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3633 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3634 | |
| 3635 | // If this function was instantiated from a member function of a |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3636 | // class template, check whether that member function was defined out-of-line. |
| 3637 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 3638 | const FunctionDecl *Definition; |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3639 | if (FD->hasBody(Definition)) |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3640 | return Definition->isOutOfLine(); |
| 3641 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3642 | |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3643 | // If this function was instantiated from a function template, |
| 3644 | // check whether that function template was defined out-of-line. |
| 3645 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 3646 | const FunctionDecl *Definition; |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3647 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3648 | return Definition->isOutOfLine(); |
| 3649 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3650 | |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3651 | return false; |
| 3652 | } |
| 3653 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3654 | SourceRange FunctionDecl::getSourceRange() const { |
| 3655 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 3656 | } |
| 3657 | |
| Anna Zaks | 28db7ce | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 3658 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3659 | IdentifierInfo *FnInfo = getIdentifier(); |
| 3660 | |
| 3661 | if (!FnInfo) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3662 | return 0; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3663 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3664 | // Builtin handling. |
| 3665 | switch (getBuiltinID()) { |
| 3666 | case Builtin::BI__builtin_memset: |
| 3667 | case Builtin::BI__builtin___memset_chk: |
| 3668 | case Builtin::BImemset: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3669 | return Builtin::BImemset; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3670 | |
| 3671 | case Builtin::BI__builtin_memcpy: |
| 3672 | case Builtin::BI__builtin___memcpy_chk: |
| 3673 | case Builtin::BImemcpy: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3674 | return Builtin::BImemcpy; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3675 | |
| 3676 | case Builtin::BI__builtin_memmove: |
| 3677 | case Builtin::BI__builtin___memmove_chk: |
| 3678 | case Builtin::BImemmove: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3679 | return Builtin::BImemmove; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3680 | |
| 3681 | case Builtin::BIstrlcpy: |
| Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3682 | case Builtin::BI__builtin___strlcpy_chk: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3683 | return Builtin::BIstrlcpy; |
| Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3684 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3685 | case Builtin::BIstrlcat: |
| Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3686 | case Builtin::BI__builtin___strlcat_chk: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3687 | return Builtin::BIstrlcat; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3688 | |
| 3689 | case Builtin::BI__builtin_memcmp: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3690 | case Builtin::BImemcmp: |
| 3691 | return Builtin::BImemcmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3692 | |
| Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 3693 | case Builtin::BI__builtin_bcmp: |
| 3694 | case Builtin::BIbcmp: |
| 3695 | return Builtin::BIbcmp; |
| 3696 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3697 | case Builtin::BI__builtin_strncpy: |
| 3698 | case Builtin::BI__builtin___strncpy_chk: |
| 3699 | case Builtin::BIstrncpy: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3700 | return Builtin::BIstrncpy; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3701 | |
| 3702 | case Builtin::BI__builtin_strncmp: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3703 | case Builtin::BIstrncmp: |
| 3704 | return Builtin::BIstrncmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3705 | |
| 3706 | case Builtin::BI__builtin_strncasecmp: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3707 | case Builtin::BIstrncasecmp: |
| 3708 | return Builtin::BIstrncasecmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3709 | |
| 3710 | case Builtin::BI__builtin_strncat: |
| Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3711 | case Builtin::BI__builtin___strncat_chk: |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3712 | case Builtin::BIstrncat: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3713 | return Builtin::BIstrncat; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3714 | |
| 3715 | case Builtin::BI__builtin_strndup: |
| 3716 | case Builtin::BIstrndup: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3717 | return Builtin::BIstrndup; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3718 | |
| Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3719 | case Builtin::BI__builtin_strlen: |
| 3720 | case Builtin::BIstrlen: |
| 3721 | return Builtin::BIstrlen; |
| 3722 | |
| Bruno Cardoso Lopes | 7ea9fd2 | 2016-08-10 18:34:47 +0000 | [diff] [blame] | 3723 | case Builtin::BI__builtin_bzero: |
| 3724 | case Builtin::BIbzero: |
| 3725 | return Builtin::BIbzero; |
| 3726 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3727 | default: |
| Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 3728 | if (isExternC()) { |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3729 | if (FnInfo->isStr("memset")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3730 | return Builtin::BImemset; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3731 | else if (FnInfo->isStr("memcpy")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3732 | return Builtin::BImemcpy; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3733 | else if (FnInfo->isStr("memmove")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3734 | return Builtin::BImemmove; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3735 | else if (FnInfo->isStr("memcmp")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3736 | return Builtin::BImemcmp; |
| Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 3737 | else if (FnInfo->isStr("bcmp")) |
| 3738 | return Builtin::BIbcmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3739 | else if (FnInfo->isStr("strncpy")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3740 | return Builtin::BIstrncpy; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3741 | else if (FnInfo->isStr("strncmp")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3742 | return Builtin::BIstrncmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3743 | else if (FnInfo->isStr("strncasecmp")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3744 | return Builtin::BIstrncasecmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3745 | else if (FnInfo->isStr("strncat")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3746 | return Builtin::BIstrncat; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3747 | else if (FnInfo->isStr("strndup")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3748 | return Builtin::BIstrndup; |
| Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3749 | else if (FnInfo->isStr("strlen")) |
| 3750 | return Builtin::BIstrlen; |
| Bruno Cardoso Lopes | 7ea9fd2 | 2016-08-10 18:34:47 +0000 | [diff] [blame] | 3751 | else if (FnInfo->isStr("bzero")) |
| 3752 | return Builtin::BIbzero; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3753 | } |
| 3754 | break; |
| 3755 | } |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3756 | return 0; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
| Richard Trieu | 27c1b1a | 2018-07-10 01:40:50 +0000 | [diff] [blame] | 3759 | unsigned FunctionDecl::getODRHash() const { |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3760 | assert(hasODRHash()); |
| Richard Trieu | 27c1b1a | 2018-07-10 01:40:50 +0000 | [diff] [blame] | 3761 | return ODRHash; |
| 3762 | } |
| 3763 | |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3764 | unsigned FunctionDecl::getODRHash() { |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3765 | if (hasODRHash()) |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3766 | return ODRHash; |
| 3767 | |
| Richard Trieu | 0ac2eb7 | 2018-02-22 05:50:29 +0000 | [diff] [blame] | 3768 | if (auto *FT = getInstantiatedFromMemberFunction()) { |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3769 | setHasODRHash(true); |
| Richard Trieu | 0ac2eb7 | 2018-02-22 05:50:29 +0000 | [diff] [blame] | 3770 | ODRHash = FT->getODRHash(); |
| 3771 | return ODRHash; |
| 3772 | } |
| 3773 | |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3774 | class ODRHash Hash; |
| 3775 | Hash.AddFunctionDecl(this); |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3776 | setHasODRHash(true); |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3777 | ODRHash = Hash.CalculateHash(); |
| 3778 | return ODRHash; |
| 3779 | } |
| 3780 | |
| Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 3781 | //===----------------------------------------------------------------------===// |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3782 | // FieldDecl Implementation |
| 3783 | //===----------------------------------------------------------------------===// |
| 3784 | |
| Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3785 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3786 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3787 | IdentifierInfo *Id, QualType T, |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3788 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3789 | InClassInitStyle InitStyle) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3790 | return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
| 3791 | BW, Mutable, InitStyle); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3794 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3795 | return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), |
| 3796 | SourceLocation(), nullptr, QualType(), nullptr, |
| 3797 | nullptr, false, ICIS_NoInit); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3800 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 3801 | if (!isImplicit() || getDeclName()) |
| 3802 | return false; |
| 3803 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3804 | if (const auto *Record = getType()->getAs<RecordType>()) |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3805 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 3806 | |
| 3807 | return false; |
| 3808 | } |
| 3809 | |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3810 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 3811 | assert(isBitField() && "not a bitfield"); |
| Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3812 | return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue(); |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
| Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 3815 | bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const { |
| 3816 | return isUnnamedBitfield() && !getBitWidth()->isValueDependent() && |
| 3817 | getBitWidthValue(Ctx) == 0; |
| 3818 | } |
| 3819 | |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3820 | unsigned FieldDecl::getFieldIndex() const { |
| Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 3821 | const FieldDecl *Canonical = getCanonicalDecl(); |
| 3822 | if (Canonical != this) |
| 3823 | return Canonical->getFieldIndex(); |
| 3824 | |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3825 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 3826 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3827 | unsigned Index = 0; |
| Richard Smith | 85567dd | 2017-11-15 01:33:46 +0000 | [diff] [blame] | 3828 | const RecordDecl *RD = getParent()->getDefinition(); |
| 3829 | assert(RD && "requested index for field of struct with no definition"); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3830 | |
| Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 3831 | for (auto *Field : RD->fields()) { |
| 3832 | Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; |
| 3833 | ++Index; |
| 3834 | } |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3835 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3836 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 3837 | return CachedFieldIndex - 1; |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
| Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 3840 | SourceRange FieldDecl::getSourceRange() const { |
| Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3841 | const Expr *FinalExpr = getInClassInitializer(); |
| 3842 | if (!FinalExpr) |
| 3843 | FinalExpr = getBitWidth(); |
| 3844 | if (FinalExpr) |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3845 | return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc()); |
| Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3846 | return DeclaratorDecl::getSourceRange(); |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
| 3849 | void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { |
| David Blaikie | 11ab078 | 2014-10-31 17:18:09 +0000 | [diff] [blame] | 3850 | assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && |
| Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 3851 | "capturing type in non-lambda or captured record."); |
| Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3852 | assert(InitStorage.getInt() == ISK_NoInit && |
| John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3853 | InitStorage.getPointer() == nullptr && |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3854 | "bit width, initializer or captured type already set"); |
| John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3855 | InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType), |
| 3856 | ISK_CapturedVLAType); |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3859 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3860 | // TagDecl Implementation |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3861 | //===----------------------------------------------------------------------===// |
| 3862 | |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3863 | TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
| 3864 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
| 3865 | SourceLocation StartL) |
| 3866 | : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C), |
| 3867 | TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) { |
| 3868 | assert((DK != Enum || TK == TTK_Enum) && |
| 3869 | "EnumDecl not matched with TTK_Enum"); |
| 3870 | setPreviousDecl(PrevDecl); |
| 3871 | setTagKind(TK); |
| 3872 | setCompleteDefinition(false); |
| 3873 | setBeingDefined(false); |
| 3874 | setEmbeddedInDeclarator(false); |
| 3875 | setFreeStanding(false); |
| 3876 | setCompleteDefinitionRequired(false); |
| 3877 | } |
| 3878 | |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3879 | SourceLocation TagDecl::getOuterLocStart() const { |
| 3880 | return getTemplateOrInnerLocStart(this); |
| 3881 | } |
| 3882 | |
| Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3883 | SourceRange TagDecl::getSourceRange() const { |
| Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 3884 | SourceLocation RBraceLoc = BraceRange.getEnd(); |
| Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3885 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3886 | return SourceRange(getOuterLocStart(), E); |
| Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3889 | TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } |
| Argyrios Kyrtzidis | 5614aef | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 3890 | |
| Rafael Espindola | bf5c33b | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 3891 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3892 | TypedefNameDeclOrQualifier = TDD; |
| Reid Kleckner | cae82a2 | 2014-04-23 22:03:04 +0000 | [diff] [blame] | 3893 | if (const Type *T = getTypeForDecl()) { |
| 3894 | (void)T; |
| Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 3895 | assert(T->isLinkageValid()); |
| Reid Kleckner | cae82a2 | 2014-04-23 22:03:04 +0000 | [diff] [blame] | 3896 | } |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 3897 | assert(isLinkageValid()); |
| Douglas Gregor | a72a4e3 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3900 | void TagDecl::startDefinition() { |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3901 | setBeingDefined(true); |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3902 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3903 | if (auto *D = dyn_cast<CXXRecordDecl>(this)) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 3904 | struct CXXRecordDecl::DefinitionData *Data = |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3905 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3906 | for (auto I : redecls()) |
| Richard Smith | 64c0630 | 2014-05-22 23:19:02 +0000 | [diff] [blame] | 3907 | cast<CXXRecordDecl>(I)->DefinitionData = Data; |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3908 | } |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | void TagDecl::completeDefinition() { |
| John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 3912 | assert((!isa<CXXRecordDecl>(this) || |
| 3913 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 3914 | "definition completed but not started"); |
| 3915 | |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3916 | setCompleteDefinition(true); |
| 3917 | setBeingDefined(false); |
| Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 3918 | |
| 3919 | if (ASTMutationListener *L = getASTMutationListener()) |
| 3920 | L->CompletedTagDefinition(this); |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3921 | } |
| 3922 | |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3923 | TagDecl *TagDecl::getDefinition() const { |
| 3924 | if (isCompleteDefinition()) |
| Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 3925 | return const_cast<TagDecl *>(this); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3926 | |
| 3927 | // If it's possible for us to have an out-of-date definition, check now. |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3928 | if (mayHaveOutOfDateDef()) { |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 3929 | if (IdentifierInfo *II = getIdentifier()) { |
| 3930 | if (II->isOutOfDate()) { |
| 3931 | updateOutOfDate(*II); |
| 3932 | } |
| 3933 | } |
| 3934 | } |
| 3935 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3936 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| Andrew Trick | ba266ee | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 3937 | return CXXRD->getDefinition(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3938 | |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3939 | for (auto R : redecls()) |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3940 | if (R->isCompleteDefinition()) |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3941 | return R; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3942 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3943 | return nullptr; |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3946 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 3947 | if (QualifierLoc) { |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3948 | // Make sure the extended qualifier info is allocated. |
| 3949 | if (!hasExtInfo()) |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3950 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3951 | // Set qualifier info. |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3952 | getExtInfo()->QualifierLoc = QualifierLoc; |
| Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 3953 | } else { |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3954 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3955 | if (hasExtInfo()) { |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3956 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 3957 | getASTContext().Deallocate(getExtInfo()); |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3958 | TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3959 | } |
| 3960 | else |
| 3961 | getExtInfo()->QualifierLoc = QualifierLoc; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3962 | } |
| 3963 | } |
| 3964 | } |
| 3965 | |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 3966 | void TagDecl::setTemplateParameterListsInfo( |
| 3967 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 3968 | assert(!TPLists.empty()); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3969 | // Make sure the extended decl info is allocated. |
| 3970 | if (!hasExtInfo()) |
| 3971 | // Allocate external info struct. |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3972 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3973 | // Set the template parameter lists info. |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 3974 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3977 | //===----------------------------------------------------------------------===// |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3978 | // EnumDecl Implementation |
| 3979 | //===----------------------------------------------------------------------===// |
| 3980 | |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3981 | EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 3982 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
| 3983 | bool Scoped, bool ScopedUsingClassTag, bool Fixed) |
| 3984 | : TagDecl(Enum, TTK_Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
| 3985 | assert(Scoped || !ScopedUsingClassTag); |
| 3986 | IntegerType = nullptr; |
| 3987 | setNumPositiveBits(0); |
| 3988 | setNumNegativeBits(0); |
| 3989 | setScoped(Scoped); |
| 3990 | setScopedUsingClassTag(ScopedUsingClassTag); |
| 3991 | setFixed(Fixed); |
| 3992 | setHasODRHash(false); |
| 3993 | ODRHash = 0; |
| 3994 | } |
| 3995 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 3996 | void EnumDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 3997 | |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 3998 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 3999 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4000 | IdentifierInfo *Id, |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4001 | EnumDecl *PrevDecl, bool IsScoped, |
| 4002 | bool IsScopedUsingClassTag, bool IsFixed) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4003 | auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, |
| 4004 | IsScoped, IsScopedUsingClassTag, IsFixed); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4005 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4006 | C.getTypeDeclType(Enum, PrevDecl); |
| 4007 | return Enum; |
| 4008 | } |
| 4009 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4010 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4011 | EnumDecl *Enum = |
| 4012 | new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 4013 | nullptr, nullptr, false, false, false); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4014 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4015 | return Enum; |
| Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
| Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 4018 | SourceRange EnumDecl::getIntegerTypeRange() const { |
| 4019 | if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) |
| 4020 | return TI->getTypeLoc().getSourceRange(); |
| 4021 | return SourceRange(); |
| 4022 | } |
| 4023 | |
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4024 | void EnumDecl::completeDefinition(QualType NewType, |
| John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4025 | QualType NewPromotionType, |
| 4026 | unsigned NumPositiveBits, |
| 4027 | unsigned NumNegativeBits) { |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4028 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4029 | if (!IntegerType) |
| 4030 | IntegerType = NewType.getTypePtr(); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4031 | PromotionType = NewPromotionType; |
| John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4032 | setNumPositiveBits(NumPositiveBits); |
| 4033 | setNumNegativeBits(NumNegativeBits); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4034 | TagDecl::completeDefinition(); |
| 4035 | } |
| 4036 | |
| Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 4037 | bool EnumDecl::isClosed() const { |
| 4038 | if (const auto *A = getAttr<EnumExtensibilityAttr>()) |
| 4039 | return A->getExtensibility() == EnumExtensibilityAttr::Closed; |
| 4040 | return true; |
| 4041 | } |
| 4042 | |
| 4043 | bool EnumDecl::isClosedFlag() const { |
| 4044 | return isClosed() && hasAttr<FlagEnumAttr>(); |
| 4045 | } |
| 4046 | |
| 4047 | bool EnumDecl::isClosedNonFlag() const { |
| 4048 | return isClosed() && !hasAttr<FlagEnumAttr>(); |
| 4049 | } |
| 4050 | |
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4051 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 4052 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 4053 | return MSI->getTemplateSpecializationKind(); |
| 4054 | |
| 4055 | return TSK_Undeclared; |
| 4056 | } |
| 4057 | |
| 4058 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 4059 | SourceLocation PointOfInstantiation) { |
| 4060 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 4061 | assert(MSI && "Not an instantiated member enumeration?"); |
| 4062 | MSI->setTemplateSpecializationKind(TSK); |
| 4063 | if (TSK != TSK_ExplicitSpecialization && |
| 4064 | PointOfInstantiation.isValid() && |
| 4065 | MSI->getPointOfInstantiation().isInvalid()) |
| 4066 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 4067 | } |
| 4068 | |
| Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4069 | EnumDecl *EnumDecl::getTemplateInstantiationPattern() const { |
| 4070 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
| 4071 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
| 4072 | EnumDecl *ED = getInstantiatedFromMemberEnum(); |
| 4073 | while (auto *NewED = ED->getInstantiatedFromMemberEnum()) |
| 4074 | ED = NewED; |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 4075 | return getDefinitionOrSelf(ED); |
| Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4076 | } |
| 4077 | } |
| 4078 | |
| 4079 | assert(!isTemplateInstantiation(getTemplateSpecializationKind()) && |
| 4080 | "couldn't find pattern for enum instantiation"); |
| 4081 | return nullptr; |
| 4082 | } |
| 4083 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4084 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 4085 | if (SpecializationInfo) |
| 4086 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 4087 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4088 | return nullptr; |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 4092 | TemplateSpecializationKind TSK) { |
| 4093 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 4094 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 4095 | } |
| 4096 | |
| Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4097 | unsigned EnumDecl::getODRHash() { |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4098 | if (hasODRHash()) |
| Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4099 | return ODRHash; |
| 4100 | |
| 4101 | class ODRHash Hash; |
| 4102 | Hash.AddEnumDecl(this); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4103 | setHasODRHash(true); |
| Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4104 | ODRHash = Hash.CalculateHash(); |
| 4105 | return ODRHash; |
| 4106 | } |
| 4107 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4108 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 4109 | // RecordDecl Implementation |
| 4110 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 4111 | |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4112 | RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, |
| 4113 | DeclContext *DC, SourceLocation StartLoc, |
| 4114 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 4115 | RecordDecl *PrevDecl) |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4116 | : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
| 4117 | assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!"); |
| 4118 | setHasFlexibleArrayMember(false); |
| 4119 | setAnonymousStructOrUnion(false); |
| 4120 | setHasObjectMember(false); |
| 4121 | setHasVolatileMember(false); |
| 4122 | setHasLoadedFieldsFromExternalStorage(false); |
| 4123 | setNonTrivialToPrimitiveDefaultInitialize(false); |
| 4124 | setNonTrivialToPrimitiveCopy(false); |
| 4125 | setNonTrivialToPrimitiveDestroy(false); |
| 4126 | setParamDestroyedInCallee(false); |
| 4127 | setArgPassingRestrictions(APK_CanPassInRegs); |
| Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
| Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4130 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4131 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4132 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4133 | RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, |
| 4134 | StartLoc, IdLoc, Id, PrevDecl); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4135 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4136 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4137 | C.getTypeDeclType(R, PrevDecl); |
| 4138 | return R; |
| Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4141 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4142 | RecordDecl *R = |
| 4143 | new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(), |
| 4144 | SourceLocation(), nullptr, nullptr); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4145 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4146 | return R; |
| Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
| Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 4149 | bool RecordDecl::isInjectedClassName() const { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4150 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
| Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 4151 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 4152 | } |
| 4153 | |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 4154 | bool RecordDecl::isLambda() const { |
| 4155 | if (auto RD = dyn_cast<CXXRecordDecl>(this)) |
| 4156 | return RD->isLambda(); |
| 4157 | return false; |
| 4158 | } |
| 4159 | |
| Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 4160 | bool RecordDecl::isCapturedRecord() const { |
| 4161 | return hasAttr<CapturedRecordAttr>(); |
| 4162 | } |
| 4163 | |
| 4164 | void RecordDecl::setCapturedRecord() { |
| 4165 | addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); |
| 4166 | } |
| 4167 | |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4168 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4169 | if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage()) |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4170 | LoadFieldsFromExternalStorage(); |
| 4171 | |
| 4172 | return field_iterator(decl_iterator(FirstDecl)); |
| 4173 | } |
| 4174 | |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 4175 | /// completeDefinition - Notes that the definition of this type is now |
| 4176 | /// complete. |
| 4177 | void RecordDecl::completeDefinition() { |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4178 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 4179 | TagDecl::completeDefinition(); |
| 4180 | } |
| 4181 | |
| Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 4182 | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
| 4183 | /// This which can be turned on with an attribute, pragma, or the |
| 4184 | /// -mms-bitfields command-line option. |
| 4185 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
| David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 4186 | return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; |
| Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 4187 | } |
| 4188 | |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4189 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 4190 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 4191 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 4192 | |
| 4193 | // Notify that we have a RecordDecl doing some initialization. |
| 4194 | ExternalASTSource::Deserializing TheFields(Source); |
| 4195 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4196 | SmallVector<Decl*, 64> Decls; |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4197 | setHasLoadedFieldsFromExternalStorage(true); |
| Richard Smith | 3cb1572 | 2015-08-05 22:41:45 +0000 | [diff] [blame] | 4198 | Source->FindExternalLexicalDecls(this, [](Decl::Kind K) { |
| 4199 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
| 4200 | }, Decls); |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4201 | |
| 4202 | #ifndef NDEBUG |
| 4203 | // Check that all decls we got were FieldDecls. |
| 4204 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
| Argyrios Kyrtzidis | f89a927 | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 4205 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4206 | #endif |
| 4207 | |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4208 | if (Decls.empty()) |
| 4209 | return; |
| 4210 | |
| Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 4211 | std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
| Argyrios Kyrtzidis | 094da73 | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 4212 | /*FieldsAlreadyLoaded=*/false); |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4215 | bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { |
| 4216 | ASTContext &Context = getASTContext(); |
| Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4217 | const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask & |
| 4218 | (SanitizerKind::Address | SanitizerKind::KernelAddress); |
| 4219 | if (!EnabledAsanMask || !Context.getLangOpts().SanitizeAddressFieldPadding) |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4220 | return false; |
| Alexey Samsonov | 33e00e2 | 2014-10-16 23:50:26 +0000 | [diff] [blame] | 4221 | const auto &Blacklist = Context.getSanitizerBlacklist(); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4222 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(this); |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4223 | // We may be able to relax some of these requirements. |
| 4224 | int ReasonToReject = -1; |
| 4225 | if (!CXXRD || CXXRD->isExternCContext()) |
| 4226 | ReasonToReject = 0; // is not C++. |
| 4227 | else if (CXXRD->hasAttr<PackedAttr>()) |
| 4228 | ReasonToReject = 1; // is packed. |
| 4229 | else if (CXXRD->isUnion()) |
| 4230 | ReasonToReject = 2; // is a union. |
| 4231 | else if (CXXRD->isTriviallyCopyable()) |
| 4232 | ReasonToReject = 3; // is trivially copyable. |
| 4233 | else if (CXXRD->hasTrivialDestructor()) |
| 4234 | ReasonToReject = 4; // has trivial destructor. |
| 4235 | else if (CXXRD->isStandardLayout()) |
| 4236 | ReasonToReject = 5; // is standard layout. |
| Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4237 | else if (Blacklist.isBlacklistedLocation(EnabledAsanMask, getLocation(), |
| 4238 | "field-padding")) |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4239 | ReasonToReject = 6; // is in a blacklisted file. |
| Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4240 | else if (Blacklist.isBlacklistedType(EnabledAsanMask, |
| 4241 | getQualifiedNameAsString(), |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4242 | "field-padding")) |
| 4243 | ReasonToReject = 7; // is blacklisted. |
| 4244 | |
| 4245 | if (EmitRemark) { |
| 4246 | if (ReasonToReject >= 0) |
| 4247 | Context.getDiagnostics().Report( |
| 4248 | getLocation(), |
| 4249 | diag::remark_sanitize_address_insert_extra_padding_rejected) |
| 4250 | << getQualifiedNameAsString() << ReasonToReject; |
| 4251 | else |
| 4252 | Context.getDiagnostics().Report( |
| 4253 | getLocation(), |
| 4254 | diag::remark_sanitize_address_insert_extra_padding_accepted) |
| 4255 | << getQualifiedNameAsString(); |
| 4256 | } |
| 4257 | return ReasonToReject < 0; |
| 4258 | } |
| 4259 | |
| Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4260 | const FieldDecl *RecordDecl::findFirstNamedDataMember() const { |
| 4261 | for (const auto *I : fields()) { |
| 4262 | if (I->getIdentifier()) |
| 4263 | return I; |
| 4264 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4265 | if (const auto *RT = I->getType()->getAs<RecordType>()) |
| Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4266 | if (const FieldDecl *NamedDataMember = |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4267 | RT->getDecl()->findFirstNamedDataMember()) |
| Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4268 | return NamedDataMember; |
| 4269 | } |
| 4270 | |
| 4271 | // We didn't find a named data member. |
| 4272 | return nullptr; |
| 4273 | } |
| 4274 | |
| Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4275 | //===----------------------------------------------------------------------===// |
| 4276 | // BlockDecl Implementation |
| 4277 | //===----------------------------------------------------------------------===// |
| 4278 | |
| Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4279 | BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc) |
| 4280 | : Decl(Block, DC, CaretLoc), DeclContext(Block) { |
| 4281 | setIsVariadic(false); |
| 4282 | setCapturesCXXThis(false); |
| 4283 | setBlockMissingReturnType(true); |
| 4284 | setIsConversionFromLambda(false); |
| 4285 | setDoesNotEscape(false); |
| Akira Hatanaka | c5792aa | 2019-02-27 18:17:16 +0000 | [diff] [blame] | 4286 | setCanAvoidCopyToHeap(false); |
| Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4289 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4290 | assert(!ParamInfo && "Already has param info!"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4291 | |
| Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4292 | // Zero params -> null pointer. |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4293 | if (!NewParamInfo.empty()) { |
| 4294 | NumParams = NewParamInfo.size(); |
| 4295 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 4296 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
| Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4297 | } |
| 4298 | } |
| 4299 | |
| Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4300 | void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
| 4301 | bool CapturesCXXThis) { |
| Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4302 | this->setCapturesCXXThis(CapturesCXXThis); |
| Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4303 | this->NumCaptures = Captures.size(); |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4304 | |
| Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4305 | if (Captures.empty()) { |
| 4306 | this->Captures = nullptr; |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4307 | return; |
| 4308 | } |
| 4309 | |
| Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4310 | this->Captures = Captures.copy(Context).data(); |
| Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4311 | } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4312 | |
| John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4313 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
| Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4314 | for (const auto &I : captures()) |
| John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4315 | // Only auto vars can be captured, so no redeclaration worries. |
| Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4316 | if (I.getVariable() == variable) |
| John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4317 | return true; |
| 4318 | |
| 4319 | return false; |
| 4320 | } |
| 4321 | |
| Douglas Gregor | 70226da | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 4322 | SourceRange BlockDecl::getSourceRange() const { |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4323 | return SourceRange(getLocation(), Body ? Body->getEndLoc() : getLocation()); |
| Douglas Gregor | 70226da | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 4324 | } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4325 | |
| 4326 | //===----------------------------------------------------------------------===// |
| 4327 | // Other Decl Allocation/Deallocation Method Implementations |
| 4328 | //===----------------------------------------------------------------------===// |
| 4329 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4330 | void TranslationUnitDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4331 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4332 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4333 | return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4336 | void PragmaCommentDecl::anchor() {} |
| Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4337 | |
| 4338 | PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C, |
| 4339 | TranslationUnitDecl *DC, |
| 4340 | SourceLocation CommentLoc, |
| 4341 | PragmaMSCommentKind CommentKind, |
| 4342 | StringRef Arg) { |
| 4343 | PragmaCommentDecl *PCD = |
| 4344 | new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1)) |
| 4345 | PragmaCommentDecl(DC, CommentLoc, CommentKind); |
| 4346 | memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size()); |
| 4347 | PCD->getTrailingObjects<char>()[Arg.size()] = '\0'; |
| 4348 | return PCD; |
| 4349 | } |
| 4350 | |
| 4351 | PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C, |
| 4352 | unsigned ID, |
| 4353 | unsigned ArgSize) { |
| 4354 | return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1)) |
| 4355 | PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); |
| 4356 | } |
| 4357 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4358 | void PragmaDetectMismatchDecl::anchor() {} |
| Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 4359 | |
| 4360 | PragmaDetectMismatchDecl * |
| 4361 | PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, |
| 4362 | SourceLocation Loc, StringRef Name, |
| 4363 | StringRef Value) { |
| 4364 | size_t ValueStart = Name.size() + 1; |
| 4365 | PragmaDetectMismatchDecl *PDMD = |
| 4366 | new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1)) |
| 4367 | PragmaDetectMismatchDecl(DC, Loc, ValueStart); |
| 4368 | memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size()); |
| 4369 | PDMD->getTrailingObjects<char>()[Name.size()] = '\0'; |
| 4370 | memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(), |
| 4371 | Value.size()); |
| 4372 | PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0'; |
| 4373 | return PDMD; |
| 4374 | } |
| 4375 | |
| 4376 | PragmaDetectMismatchDecl * |
| 4377 | PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 4378 | unsigned NameValueSize) { |
| 4379 | return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1)) |
| 4380 | PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); |
| 4381 | } |
| Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4382 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4383 | void ExternCContextDecl::anchor() {} |
| Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 4384 | |
| 4385 | ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, |
| 4386 | TranslationUnitDecl *DC) { |
| 4387 | return new (C, DC) ExternCContextDecl(DC); |
| 4388 | } |
| 4389 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4390 | void LabelDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4391 | |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4392 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 4393 | SourceLocation IdentL, IdentifierInfo *II) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4394 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); |
| Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 4398 | SourceLocation IdentL, IdentifierInfo *II, |
| 4399 | SourceLocation GnuLabelL) { |
| 4400 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4401 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4404 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4405 | return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, |
| 4406 | SourceLocation()); |
| Douglas Gregor | 417e87c | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 4409 | void LabelDecl::setMSAsmLabel(StringRef Name) { |
| 4410 | char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; |
| 4411 | memcpy(Buffer, Name.data(), Name.size()); |
| 4412 | Buffer[Name.size()] = '\0'; |
| 4413 | MSAsmName = Buffer; |
| 4414 | } |
| 4415 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4416 | void ValueDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4417 | |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 4418 | bool ValueDecl::isWeak() const { |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 4419 | for (const auto *I : attrs()) |
| 4420 | if (isa<WeakAttr>(I) || isa<WeakRefAttr>(I)) |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 4421 | return true; |
| 4422 | |
| 4423 | return isWeakImported(); |
| 4424 | } |
| 4425 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4426 | void ImplicitParamDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4427 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4428 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4429 | SourceLocation IdLoc, |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4430 | IdentifierInfo *Id, QualType Type, |
| 4431 | ImplicitParamKind ParamKind) { |
| 4432 | return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind); |
| 4433 | } |
| 4434 | |
| 4435 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type, |
| 4436 | ImplicitParamKind ParamKind) { |
| 4437 | return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4440 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4441 | unsigned ID) { |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4442 | return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4445 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4446 | SourceLocation StartLoc, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4447 | const DeclarationNameInfo &NameInfo, |
| 4448 | QualType T, TypeSourceInfo *TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4449 | StorageClass SC, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4450 | bool isInlineSpecified, |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 4451 | bool hasWrittenPrototype, |
| 4452 | bool isConstexprSpecified) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4453 | FunctionDecl *New = |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4454 | new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo, |
| 4455 | SC, isInlineSpecified, isConstexprSpecified); |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 4456 | New->setHasWrittenPrototype(hasWrittenPrototype); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4457 | return New; |
| 4458 | } |
| 4459 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4460 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4461 | return new (C, ID) FunctionDecl(Function, C, nullptr, SourceLocation(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4462 | DeclarationNameInfo(), QualType(), nullptr, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4463 | SC_None, false, false); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4466 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4467 | return new (C, DC) BlockDecl(DC, L); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4468 | } |
| 4469 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4470 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4471 | return new (C, ID) BlockDecl(nullptr, SourceLocation()); |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4472 | } |
| 4473 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4474 | CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams) |
| 4475 | : Decl(Captured, DC, SourceLocation()), DeclContext(Captured), |
| 4476 | NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {} |
| 4477 | |
| Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4478 | CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, |
| 4479 | unsigned NumParams) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4480 | return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4481 | CapturedDecl(DC, NumParams); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4482 | } |
| 4483 | |
| Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 4484 | CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4485 | unsigned NumParams) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4486 | return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4487 | CapturedDecl(nullptr, NumParams); |
| Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 4488 | } |
| 4489 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4490 | Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); } |
| 4491 | void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } |
| 4492 | |
| 4493 | bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); } |
| 4494 | void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); } |
| 4495 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4496 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 4497 | SourceLocation L, |
| 4498 | IdentifierInfo *Id, QualType T, |
| 4499 | Expr *E, const llvm::APSInt &V) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4500 | return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4503 | EnumConstantDecl * |
| 4504 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4505 | return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr, |
| 4506 | QualType(), nullptr, llvm::APSInt()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4509 | void IndirectFieldDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4510 | |
| Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4511 | IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC, |
| 4512 | SourceLocation L, DeclarationName N, |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4513 | QualType T, |
| 4514 | MutableArrayRef<NamedDecl *> CH) |
| 4515 | : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()), |
| 4516 | ChainingSize(CH.size()) { |
| Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4517 | // In C++, indirect field declarations conflict with tag declarations in the |
| 4518 | // same scope, so add them to IDNS_Tag so that tag redeclaration finds them. |
| 4519 | if (C.getLangOpts().CPlusPlus) |
| 4520 | IdentifierNamespace |= IDNS_Tag; |
| 4521 | } |
| 4522 | |
| Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4523 | IndirectFieldDecl * |
| 4524 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4525 | IdentifierInfo *Id, QualType T, |
| 4526 | llvm::MutableArrayRef<NamedDecl *> CH) { |
| 4527 | return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4530 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 4531 | unsigned ID) { |
| Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4532 | return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(), |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4533 | DeclarationName(), QualType(), None); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 4536 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 4537 | SourceLocation End = getLocation(); |
| 4538 | if (Init) |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4539 | End = Init->getEndLoc(); |
| Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 4540 | return SourceRange(getLocation(), End); |
| 4541 | } |
| 4542 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4543 | void TypeDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4544 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4545 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 4546 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4547 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4548 | return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4551 | void TypedefNameDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4552 | |
| Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4553 | TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { |
| 4554 | if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { |
| 4555 | auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); |
| 4556 | auto *ThisTypedef = this; |
| 4557 | if (AnyRedecl && OwningTypedef) { |
| 4558 | OwningTypedef = OwningTypedef->getCanonicalDecl(); |
| 4559 | ThisTypedef = ThisTypedef->getCanonicalDecl(); |
| 4560 | } |
| 4561 | if (OwningTypedef == ThisTypedef) |
| Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 4562 | return TT->getDecl(); |
| Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4563 | } |
| Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 4564 | |
| 4565 | return nullptr; |
| 4566 | } |
| 4567 | |
| Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 4568 | bool TypedefNameDecl::isTransparentTagSlow() const { |
| 4569 | auto determineIsTransparent = [&]() { |
| 4570 | if (auto *TT = getUnderlyingType()->getAs<TagType>()) { |
| 4571 | if (auto *TD = TT->getDecl()) { |
| 4572 | if (TD->getName() != getName()) |
| 4573 | return false; |
| 4574 | SourceLocation TTLoc = getLocation(); |
| 4575 | SourceLocation TDLoc = TD->getLocation(); |
| 4576 | if (!TTLoc.isMacroID() || !TDLoc.isMacroID()) |
| 4577 | return false; |
| 4578 | SourceManager &SM = getASTContext().getSourceManager(); |
| 4579 | return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc); |
| 4580 | } |
| 4581 | } |
| 4582 | return false; |
| 4583 | }; |
| 4584 | |
| 4585 | bool isTransparent = determineIsTransparent(); |
| Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 4586 | MaybeModedTInfo.setInt((isTransparent << 1) | 1); |
| Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 4587 | return isTransparent; |
| 4588 | } |
| 4589 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4590 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4591 | return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4592 | nullptr, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4593 | } |
| 4594 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4595 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 4596 | SourceLocation StartLoc, |
| 4597 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 4598 | TypeSourceInfo *TInfo) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4599 | return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4600 | } |
| 4601 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4602 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4603 | return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), |
| 4604 | SourceLocation(), nullptr, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 4607 | SourceRange TypedefDecl::getSourceRange() const { |
| 4608 | SourceLocation RangeEnd = getLocation(); |
| 4609 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 4610 | if (typeIsPostfix(TInfo->getType())) |
| 4611 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 4612 | } |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4613 | return SourceRange(getBeginLoc(), RangeEnd); |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4616 | SourceRange TypeAliasDecl::getSourceRange() const { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4617 | SourceLocation RangeEnd = getBeginLoc(); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4618 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 4619 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4620 | return SourceRange(getBeginLoc(), RangeEnd); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4623 | void FileScopeAsmDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4624 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4625 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 4626 | StringLiteral *Str, |
| 4627 | SourceLocation AsmLoc, |
| 4628 | SourceLocation RParenLoc) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4629 | return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4630 | } |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4631 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4632 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4633 | unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4634 | return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), |
| 4635 | SourceLocation()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4638 | void EmptyDecl::anchor() {} |
| 4639 | |
| 4640 | EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4641 | return new (C, DC) EmptyDecl(DC, L); |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
| 4644 | EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4645 | return new (C, ID) EmptyDecl(nullptr, SourceLocation()); |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4648 | //===----------------------------------------------------------------------===// |
| 4649 | // ImportDecl Implementation |
| 4650 | //===----------------------------------------------------------------------===// |
| 4651 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4652 | /// Retrieve the number of module identifiers needed to name the given |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4653 | /// module. |
| 4654 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 4655 | unsigned Result = 1; |
| 4656 | while (Mod->Parent) { |
| 4657 | Mod = Mod->Parent; |
| 4658 | ++Result; |
| 4659 | } |
| 4660 | return Result; |
| 4661 | } |
| 4662 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4663 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4664 | Module *Imported, |
| 4665 | ArrayRef<SourceLocation> IdentifierLocs) |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4666 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true) { |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4667 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4668 | auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4669 | std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(), |
| 4670 | StoredLocs); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4673 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4674 | Module *Imported, SourceLocation EndLoc) |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4675 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4676 | *getTrailingObjects<SourceLocation>() = EndLoc; |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4679 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
| Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4680 | SourceLocation StartLoc, Module *Imported, |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4681 | ArrayRef<SourceLocation> IdentifierLocs) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4682 | return new (C, DC, |
| 4683 | additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size())) |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4684 | ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4687 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
| Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4688 | SourceLocation StartLoc, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4689 | Module *Imported, |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4690 | SourceLocation EndLoc) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4691 | ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1)) |
| 4692 | ImportDecl(DC, StartLoc, Imported, EndLoc); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4693 | Import->setImplicit(); |
| 4694 | return Import; |
| 4695 | } |
| 4696 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4697 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 4698 | unsigned NumLocations) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4699 | return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations)) |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4700 | ImportDecl(EmptyShell()); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4701 | } |
| 4702 | |
| 4703 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 4704 | if (!ImportedAndComplete.getInt()) |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4705 | return None; |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4706 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4707 | const auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
| Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4708 | return llvm::makeArrayRef(StoredLocs, |
| 4709 | getNumModuleIdentifiers(getImportedModule())); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | SourceRange ImportDecl::getSourceRange() const { |
| 4713 | if (!ImportedAndComplete.getInt()) |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4714 | return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>()); |
| 4715 | |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4716 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 4717 | } |
| Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4718 | |
| 4719 | //===----------------------------------------------------------------------===// |
| 4720 | // ExportDecl Implementation |
| 4721 | //===----------------------------------------------------------------------===// |
| 4722 | |
| 4723 | void ExportDecl::anchor() {} |
| 4724 | |
| 4725 | ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC, |
| 4726 | SourceLocation ExportLoc) { |
| 4727 | return new (C, DC) ExportDecl(DC, ExportLoc); |
| 4728 | } |
| 4729 | |
| 4730 | ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 4731 | return new (C, ID) ExportDecl(nullptr, SourceLocation()); |
| 4732 | } |