| 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" |
| Reid Kleckner | 60573ae | 2019-11-15 17:31:55 -0800 | [diff] [blame] | 19 | #include "clang/AST/Attr.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 20 | #include "clang/AST/CanonicalType.h" |
| 21 | #include "clang/AST/DeclBase.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclCXX.h" |
| 23 | #include "clang/AST/DeclObjC.h" |
| Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclOpenMP.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclTemplate.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 26 | #include "clang/AST/DeclarationName.h" |
| Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 27 | #include "clang/AST/Expr.h" |
| Anders Carlsson | 714d096 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprCXX.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 29 | #include "clang/AST/ExternalASTSource.h" |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 30 | #include "clang/AST/ODRHash.h" |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 31 | #include "clang/AST/PrettyDeclStackTrace.h" |
| Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 32 | #include "clang/AST/PrettyPrinter.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 33 | #include "clang/AST/Redeclarable.h" |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 34 | #include "clang/AST/Stmt.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 35 | #include "clang/AST/TemplateBase.h" |
| 36 | #include "clang/AST/Type.h" |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 37 | #include "clang/AST/TypeLoc.h" |
| Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 38 | #include "clang/Basic/Builtins.h" |
| Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 39 | #include "clang/Basic/IdentifierTable.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 40 | #include "clang/Basic/LLVM.h" |
| 41 | #include "clang/Basic/LangOptions.h" |
| 42 | #include "clang/Basic/Linkage.h" |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 43 | #include "clang/Basic/Module.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 44 | #include "clang/Basic/PartialDiagnostic.h" |
| 45 | #include "clang/Basic/SanitizerBlacklist.h" |
| 46 | #include "clang/Basic/Sanitizers.h" |
| 47 | #include "clang/Basic/SourceLocation.h" |
| 48 | #include "clang/Basic/SourceManager.h" |
| Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 49 | #include "clang/Basic/Specifiers.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 50 | #include "clang/Basic/TargetCXXABI.h" |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 51 | #include "clang/Basic/TargetInfo.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 52 | #include "clang/Basic/Visibility.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/APSInt.h" |
| 54 | #include "llvm/ADT/ArrayRef.h" |
| 55 | #include "llvm/ADT/None.h" |
| 56 | #include "llvm/ADT/Optional.h" |
| 57 | #include "llvm/ADT/STLExtras.h" |
| 58 | #include "llvm/ADT/SmallVector.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/StringRef.h" |
| Reid Kleckner | 60573ae | 2019-11-15 17:31:55 -0800 | [diff] [blame] | 60 | #include "llvm/ADT/StringSwitch.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/Triple.h" |
| 62 | #include "llvm/Support/Casting.h" |
| John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 63 | #include "llvm/Support/ErrorHandling.h" |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 64 | #include "llvm/Support/raw_ostream.h" |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 65 | #include <algorithm> |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 66 | #include <cassert> |
| 67 | #include <cstddef> |
| 68 | #include <cstring> |
| 69 | #include <memory> |
| 70 | #include <string> |
| 71 | #include <tuple> |
| 72 | #include <type_traits> |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 73 | |
| Chris Lattner | 6d9a685 | 2006-10-25 05:11:20 +0000 | [diff] [blame] | 74 | using namespace clang; |
| Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 75 | |
| Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 76 | Decl *clang::getPrimaryMergedDecl(Decl *D) { |
| 77 | return D->getASTContext().getPrimaryMergedDecl(D); |
| 78 | } |
| 79 | |
| Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 80 | void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { |
| 81 | SourceLocation Loc = this->Loc; |
| 82 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
| 83 | if (Loc.isValid()) { |
| 84 | Loc.print(OS, Context.getSourceManager()); |
| 85 | OS << ": "; |
| 86 | } |
| 87 | OS << Message; |
| 88 | |
| 89 | if (auto *ND = dyn_cast_or_null<NamedDecl>(TheDecl)) { |
| 90 | OS << " '"; |
| 91 | ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true); |
| 92 | OS << "'"; |
| 93 | } |
| 94 | |
| 95 | OS << '\n'; |
| 96 | } |
| 97 | |
| Richard Smith | 73b21d8 | 2014-09-03 02:33:22 +0000 | [diff] [blame] | 98 | // Defined here so that it can be inlined into its direct callers. |
| 99 | bool Decl::isOutOfLine() const { |
| 100 | return !getLexicalDeclContext()->Equals(getDeclContext()); |
| 101 | } |
| 102 | |
| Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 103 | TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx) |
| 104 | : Decl(TranslationUnit, nullptr, SourceLocation()), |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 105 | DeclContext(TranslationUnit), Ctx(ctx) {} |
| Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 106 | |
| Chris Lattner | 88f70d6 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 107 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 108 | // NamedDecl Implementation |
| Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 109 | //===----------------------------------------------------------------------===// |
| 110 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 111 | // Visibility rules aren't rigorously externally specified, but here |
| 112 | // are the basic principles behind what we implement: |
| 113 | // |
| 114 | // 1. An explicit visibility attribute is generally a direct expression |
| 115 | // of the user's intent and should be honored. Only the innermost |
| 116 | // visibility attribute applies. If no visibility attribute applies, |
| 117 | // global visibility settings are considered. |
| 118 | // |
| 119 | // 2. There is one caveat to the above: on or in a template pattern, |
| 120 | // an explicit visibility attribute is just a default rule, and |
| 121 | // visibility can be decreased by the visibility of template |
| 122 | // arguments. But this, too, has an exception: an attribute on an |
| 123 | // explicit specialization or instantiation causes all the visibility |
| 124 | // restrictions of the template arguments to be ignored. |
| 125 | // |
| 126 | // 3. A variable that does not otherwise have explicit visibility can |
| 127 | // be restricted by the visibility of its type. |
| 128 | // |
| 129 | // 4. A visibility restriction is explicit if it comes from an |
| 130 | // attribute (or something like it), not a global visibility setting. |
| 131 | // When emitting a reference to an external symbol, visibility |
| 132 | // restrictions are ignored unless they are explicit. |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 133 | // |
| 134 | // 5. When computing the visibility of a non-type, including a |
| 135 | // non-type member of a class, only non-type visibility restrictions |
| 136 | // are considered: the 'visibility' attribute, global value-visibility |
| 137 | // settings, and a few special cases like __private_extern. |
| 138 | // |
| 139 | // 6. When computing the visibility of a type, including a type member |
| 140 | // of a class, only type visibility restrictions are considered: |
| 141 | // the 'type_visibility' attribute and global type-visibility settings. |
| 142 | // However, a 'visibility' attribute counts as a 'type_visibility' |
| 143 | // attribute on any declaration that only has the former. |
| 144 | // |
| 145 | // The visibility of a "secondary" entity, like a template argument, |
| 146 | // is computed using the kind of that entity, not the kind of the |
| 147 | // primary entity for which we are computing visibility. For example, |
| 148 | // the visibility of a specialization of either of these templates: |
| 149 | // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); |
| 150 | // template <class T, bool (&compare)(T, X)> class matcher; |
| 151 | // is restricted according to the type visibility of the argument 'T', |
| 152 | // the type visibility of 'bool(&)(T,X)', and the value visibility of |
| 153 | // the argument function 'compare'. That 'has_match' is a value |
| 154 | // and 'matcher' is a type only matters when looking for attributes |
| 155 | // and settings from the immediate context. |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 156 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 157 | /// Does this computation kind permit us to consider additional |
| 158 | /// visibility settings from attributes and the like? |
| 159 | static bool hasExplicitVisibilityAlready(LVComputationKind computation) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 160 | return computation.IgnoreExplicitVisibility; |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /// Given an LVComputationKind, return one of the same type/value sort |
| 164 | /// that records that it already has explicit visibility. |
| 165 | static LVComputationKind |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 166 | withExplicitVisibilityAlready(LVComputationKind Kind) { |
| 167 | Kind.IgnoreExplicitVisibility = true; |
| 168 | return Kind; |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 171 | static Optional<Visibility> getExplicitVisibility(const NamedDecl *D, |
| 172 | LVComputationKind kind) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 173 | assert(!kind.IgnoreExplicitVisibility && |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 174 | "asking for explicit visibility when we shouldn't be"); |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 175 | return D->getExplicitVisibility(kind.getExplicitVisibilityKind()); |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 178 | /// Is the given declaration a "type" or a "value" for the purposes of |
| 179 | /// visibility computation? |
| 180 | static bool usesTypeVisibility(const NamedDecl *D) { |
| John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 181 | return isa<TypeDecl>(D) || |
| 182 | isa<ClassTemplateDecl>(D) || |
| 183 | isa<ObjCInterfaceDecl>(D); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 186 | /// Does the given declaration have member specialization information, |
| 187 | /// and if so, is it an explicit specialization? |
| 188 | template <class T> static typename |
| Benjamin Kramer | ed2f476 | 2014-03-07 14:30:23 +0000 | [diff] [blame] | 189 | std::enable_if<!std::is_base_of<RedeclarableTemplateDecl, T>::value, bool>::type |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 190 | isExplicitMemberSpecialization(const T *D) { |
| 191 | if (const MemberSpecializationInfo *member = |
| 192 | D->getMemberSpecializationInfo()) { |
| 193 | return member->isExplicitSpecialization(); |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | /// For templates, this question is easier: a member template can't be |
| 199 | /// explicitly instantiated, so there's a single bit indicating whether |
| 200 | /// or not this is an explicit member specialization. |
| 201 | static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { |
| 202 | return D->isMemberSpecialization(); |
| 203 | } |
| 204 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 205 | /// Given a visibility attribute, return the explicit visibility |
| 206 | /// associated with it. |
| 207 | template <class T> |
| 208 | static Visibility getVisibilityFromAttr(const T *attr) { |
| 209 | switch (attr->getVisibility()) { |
| 210 | case T::Default: |
| 211 | return DefaultVisibility; |
| 212 | case T::Hidden: |
| 213 | return HiddenVisibility; |
| 214 | case T::Protected: |
| 215 | return ProtectedVisibility; |
| 216 | } |
| 217 | llvm_unreachable("bad visibility kind"); |
| 218 | } |
| 219 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 220 | /// Return the explicit visibility of the given declaration. |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 221 | static Optional<Visibility> getVisibilityOf(const NamedDecl *D, |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 222 | NamedDecl::ExplicitVisibilityKind kind) { |
| 223 | // If we're ultimately computing the visibility of a type, look for |
| 224 | // a 'type_visibility' attribute before looking for 'visibility'. |
| 225 | if (kind == NamedDecl::VisibilityForType) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 226 | if (const auto *A = D->getAttr<TypeVisibilityAttr>()) { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 227 | return getVisibilityFromAttr(A); |
| 228 | } |
| 229 | } |
| 230 | |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 231 | // If this declaration has an explicit visibility attribute, use it. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 232 | if (const auto *A = D->getAttr<VisibilityAttr>()) { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 233 | return getVisibilityFromAttr(A); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 234 | } |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 235 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 236 | return None; |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 239 | LinkageInfo LinkageComputer::getLVForType(const Type &T, |
| 240 | LVComputationKind computation) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 241 | if (computation.IgnoreAllVisibility) |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 242 | return LinkageInfo(T.getLinkage(), DefaultVisibility, true); |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 243 | return getTypeLinkageAndVisibility(&T); |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 246 | /// Get the most restrictive linkage for the types in the given |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 247 | /// template parameter list. For visibility purposes, template |
| 248 | /// parameters are part of the signature of a template. |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 249 | LinkageInfo LinkageComputer::getLVForTemplateParameterList( |
| 250 | const TemplateParameterList *Params, LVComputationKind computation) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 251 | LinkageInfo LV; |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 252 | for (const NamedDecl *P : *Params) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 253 | // Template type parameters are the most common and never |
| 254 | // contribute to visibility, pack or not. |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 255 | if (isa<TemplateTypeParmDecl>(P)) |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 256 | continue; |
| 257 | |
| 258 | // Non-type template parameters can be restricted by the value type, e.g. |
| 259 | // template <enum X> class A { ... }; |
| 260 | // We have to be careful here, though, because we can be dealing with |
| 261 | // dependent types. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 262 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 263 | // Handle the non-pack case first. |
| 264 | if (!NTTP->isExpandedParameterPack()) { |
| 265 | if (!NTTP->getType()->isDependentType()) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 266 | LV.merge(getLVForType(*NTTP->getType(), computation)); |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 267 | } |
| 268 | continue; |
| 269 | } |
| Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 270 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 271 | // Look at all the types in an expanded pack. |
| 272 | for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { |
| 273 | QualType type = NTTP->getExpansionType(i); |
| 274 | if (!type->isDependentType()) |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 275 | LV.merge(getTypeLinkageAndVisibility(type)); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 276 | } |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 277 | continue; |
| Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 278 | } |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 279 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 280 | // Template template parameters can be restricted by their |
| 281 | // template parameters, recursively. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 282 | const auto *TTP = cast<TemplateTemplateParmDecl>(P); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 283 | |
| 284 | // Handle the non-pack case first. |
| 285 | if (!TTP->isExpandedParameterPack()) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 286 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(), |
| 287 | computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 288 | continue; |
| 289 | } |
| 290 | |
| 291 | // Look at all expansions in an expanded pack. |
| 292 | for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); |
| 293 | i != n; ++i) { |
| 294 | LV.merge(getLVForTemplateParameterList( |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 295 | TTP->getExpansionTemplateParameters(i), computation)); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 299 | return LV; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 302 | static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 303 | const Decl *Ret = nullptr; |
| Rafael Espindola | c1b38a2 | 2013-05-16 04:30:21 +0000 | [diff] [blame] | 304 | const DeclContext *DC = D->getDeclContext(); |
| 305 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 306 | if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC)) |
| 307 | Ret = cast<Decl>(DC); |
| Rafael Espindola | c1b38a2 | 2013-05-16 04:30:21 +0000 | [diff] [blame] | 308 | DC = DC->getParent(); |
| 309 | } |
| 310 | return Ret; |
| 311 | } |
| 312 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 313 | /// Get the most restrictive linkage for the types and |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 314 | /// declarations in the given template argument list. |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 315 | /// |
| 316 | /// Note that we don't take an LVComputationKind because we always |
| 317 | /// 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] | 318 | LinkageInfo |
| 319 | LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, |
| 320 | LVComputationKind computation) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 321 | LinkageInfo LV; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 322 | |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 323 | for (const TemplateArgument &Arg : Args) { |
| 324 | switch (Arg.getKind()) { |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 325 | case TemplateArgument::Null: |
| 326 | case TemplateArgument::Integral: |
| 327 | case TemplateArgument::Expression: |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 328 | continue; |
| Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 329 | |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 330 | case TemplateArgument::Type: |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 331 | LV.merge(getLVForType(*Arg.getAsType(), computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 332 | continue; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 333 | |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 334 | case TemplateArgument::Declaration: { |
| 335 | const NamedDecl *ND = Arg.getAsDecl(); |
| 336 | assert(!usesTypeVisibility(ND)); |
| 337 | LV.merge(getLVForDecl(ND, computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 338 | continue; |
| George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 339 | } |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 340 | |
| 341 | case TemplateArgument::NullPtr: |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 342 | LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType())); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 343 | continue; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 344 | |
| 345 | case TemplateArgument::Template: |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 346 | case TemplateArgument::TemplateExpansion: |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 347 | if (TemplateDecl *Template = |
| 348 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 349 | LV.merge(getLVForDecl(Template, computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 350 | continue; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 351 | |
| 352 | case TemplateArgument::Pack: |
| David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 353 | LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 354 | continue; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 355 | } |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 356 | llvm_unreachable("bad template argument kind"); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 359 | return LV; |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 362 | LinkageInfo |
| 363 | LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
| 364 | LVComputationKind computation) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 365 | return getLVForTemplateArgumentList(TArgs.asArray(), computation); |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 368 | static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, |
| 369 | const FunctionTemplateSpecializationInfo *specInfo) { |
| 370 | // Include visibility from the template parameters and arguments |
| 371 | // only if this is not an explicit instantiation or specialization |
| 372 | // with direct explicit visibility. (Implicit instantiations won't |
| 373 | // have a direct attribute.) |
| 374 | if (!specInfo->isExplicitInstantiationOrSpecialization()) |
| 375 | return true; |
| 376 | |
| 377 | return !fn->hasAttr<VisibilityAttr>(); |
| 378 | } |
| 379 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 380 | /// Merge in template-related linkage and visibility for the given |
| 381 | /// function template specialization. |
| 382 | /// |
| 383 | /// We don't need a computation kind here because we can assume |
| 384 | /// LVForValue. |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 385 | /// |
| NAKAMURA Takumi | 62eae08 | 2013-02-22 04:06:28 +0000 | [diff] [blame] | 386 | /// \param[out] LV the computation to use for the parent |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 387 | void LinkageComputer::mergeTemplateLV( |
| 388 | LinkageInfo &LV, const FunctionDecl *fn, |
| 389 | const FunctionTemplateSpecializationInfo *specInfo, |
| 390 | LVComputationKind computation) { |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 391 | bool considerVisibility = |
| 392 | shouldConsiderTemplateVisibility(fn, specInfo); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 393 | |
| 394 | // Merge information from the template parameters. |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 395 | FunctionTemplateDecl *temp = specInfo->getTemplate(); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 396 | LinkageInfo tempLV = |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 397 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 398 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 399 | |
| 400 | // Merge information from the template arguments. |
| 401 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 402 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 403 | LV.mergeMaybeWithVisibility(argsLV, considerVisibility); |
| John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 406 | /// Does the given declaration have a direct visibility attribute |
| 407 | /// that would match the given rules? |
| 408 | static bool hasDirectVisibilityAttribute(const NamedDecl *D, |
| 409 | LVComputationKind computation) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 410 | if (computation.IgnoreAllVisibility) |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 411 | return false; |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 412 | |
| 413 | return (computation.isTypeVisibility() && D->hasAttr<TypeVisibilityAttr>()) || |
| 414 | D->hasAttr<VisibilityAttr>(); |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 417 | /// Should we consider visibility associated with the template |
| 418 | /// arguments and parameters of the given class template specialization? |
| 419 | static bool shouldConsiderTemplateVisibility( |
| 420 | const ClassTemplateSpecializationDecl *spec, |
| 421 | LVComputationKind computation) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 422 | // Include visibility from the template parameters and arguments |
| 423 | // only if this is not an explicit instantiation or specialization |
| 424 | // with direct explicit visibility (and note that implicit |
| 425 | // instantiations won't have a direct attribute). |
| 426 | // |
| 427 | // Furthermore, we want to ignore template parameters and arguments |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 428 | // for an explicit specialization when computing the visibility of a |
| 429 | // member thereof with explicit visibility. |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 430 | // |
| 431 | // This is a bit complex; let's unpack it. |
| 432 | // |
| 433 | // An explicit class specialization is an independent, top-level |
| 434 | // declaration. As such, if it or any of its members has an |
| 435 | // explicit visibility attribute, that must directly express the |
| 436 | // user's intent, and we should honor it. The same logic applies to |
| 437 | // an explicit instantiation of a member of such a thing. |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 438 | |
| 439 | // Fast path: if this is not an explicit instantiation or |
| 440 | // specialization, we always want to consider template-related |
| 441 | // visibility restrictions. |
| 442 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 443 | return true; |
| 444 | |
| 445 | // This is the 'member thereof' check. |
| 446 | if (spec->isExplicitSpecialization() && |
| 447 | hasExplicitVisibilityAlready(computation)) |
| 448 | return false; |
| 449 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 450 | return !hasDirectVisibilityAttribute(spec, computation); |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | /// Merge in template-related linkage and visibility for the given |
| 454 | /// class template specialization. |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 455 | void LinkageComputer::mergeTemplateLV( |
| 456 | LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec, |
| 457 | LVComputationKind computation) { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 458 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 459 | |
| 460 | // Merge information from the template parameters, but ignore |
| 461 | // visibility if we're only considering template arguments. |
| 462 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 463 | ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 464 | LinkageInfo tempLV = |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 465 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 466 | LV.mergeMaybeWithVisibility(tempLV, |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 467 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 468 | |
| 469 | // Merge information from the template arguments. We ignore |
| 470 | // template-argument visibility if we've got an explicit |
| 471 | // instantiation with a visibility attribute. |
| 472 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 473 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
| Rafael Espindola | 503276b | 2013-05-30 21:23:15 +0000 | [diff] [blame] | 474 | if (considerVisibility) |
| 475 | LV.mergeVisibility(argsLV); |
| 476 | LV.mergeExternalVisibility(argsLV); |
| John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 479 | /// Should we consider visibility associated with the template |
| 480 | /// arguments and parameters of the given variable template |
| 481 | /// specialization? As usual, follow class template specialization |
| 482 | /// logic up to initialization. |
| 483 | static bool shouldConsiderTemplateVisibility( |
| 484 | const VarTemplateSpecializationDecl *spec, |
| 485 | LVComputationKind computation) { |
| 486 | // Include visibility from the template parameters and arguments |
| 487 | // only if this is not an explicit instantiation or specialization |
| 488 | // with direct explicit visibility (and note that implicit |
| 489 | // instantiations won't have a direct attribute). |
| 490 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 491 | return true; |
| 492 | |
| 493 | // An explicit variable specialization is an independent, top-level |
| 494 | // declaration. As such, if it has an explicit visibility attribute, |
| 495 | // that must directly express the user's intent, and we should honor |
| 496 | // it. |
| 497 | if (spec->isExplicitSpecialization() && |
| 498 | hasExplicitVisibilityAlready(computation)) |
| 499 | return false; |
| 500 | |
| 501 | return !hasDirectVisibilityAttribute(spec, computation); |
| 502 | } |
| 503 | |
| 504 | /// Merge in template-related linkage and visibility for the given |
| 505 | /// variable template specialization. As usual, follow class template |
| 506 | /// specialization logic up to initialization. |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 507 | void LinkageComputer::mergeTemplateLV(LinkageInfo &LV, |
| 508 | const VarTemplateSpecializationDecl *spec, |
| 509 | LVComputationKind computation) { |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 510 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
| 511 | |
| 512 | // Merge information from the template parameters, but ignore |
| 513 | // visibility if we're only considering template arguments. |
| 514 | |
| 515 | VarTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 516 | LinkageInfo tempLV = |
| 517 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| 518 | LV.mergeMaybeWithVisibility(tempLV, |
| 519 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
| 520 | |
| 521 | // Merge information from the template arguments. We ignore |
| 522 | // template-argument visibility if we've got an explicit |
| 523 | // instantiation with a visibility attribute. |
| 524 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| 525 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
| 526 | if (considerVisibility) |
| 527 | LV.mergeVisibility(argsLV); |
| 528 | LV.mergeExternalVisibility(argsLV); |
| 529 | } |
| 530 | |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 531 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
| 532 | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
| Rafael Espindola | 5cc7890 | 2012-07-13 23:26:43 +0000 | [diff] [blame] | 533 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 534 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 535 | return false; |
| 536 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 537 | const auto *FD = dyn_cast<FunctionDecl>(D); |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 538 | if (!FD) |
| 539 | return false; |
| 540 | |
| 541 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 542 | if (FunctionTemplateSpecializationInfo *spec |
| 543 | = FD->getTemplateSpecializationInfo()) { |
| 544 | TSK = spec->getTemplateSpecializationKind(); |
| 545 | } else if (MemberSpecializationInfo *MSI = |
| 546 | FD->getMemberSpecializationInfo()) { |
| 547 | TSK = MSI->getTemplateSpecializationKind(); |
| 548 | } |
| 549 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 550 | const FunctionDecl *Def = nullptr; |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 551 | // InlineVisibilityHidden only applies to definitions, and |
| 552 | // isInlined() only gives meaningful answers on definitions |
| 553 | // anyway. |
| 554 | return TSK != TSK_ExplicitInstantiationDeclaration && |
| 555 | TSK != TSK_ExplicitInstantiationDefinition && |
| Rafael Espindola | fb9d4b4 | 2012-10-11 16:32:25 +0000 | [diff] [blame] | 556 | FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 559 | template <typename T> static bool isFirstInExternCContext(T *D) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 560 | const T *First = D->getFirstDecl(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 561 | return First->isInExternCContext(); |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 564 | static bool isSingleLineLanguageLinkage(const Decl &D) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 565 | if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext())) |
| Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 566 | if (!SD->hasBraces()) |
| Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 567 | return true; |
| 568 | return false; |
| 569 | } |
| 570 | |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 571 | /// Determine whether D is declared in the purview of a named module. |
| 572 | static bool isInModulePurview(const NamedDecl *D) { |
| 573 | if (auto *M = D->getOwningModule()) |
| 574 | return M->isModulePurview(); |
| 575 | return false; |
| 576 | } |
| 577 | |
| Ali Tamur | b6a8a6c | 2019-04-19 02:15:57 +0000 | [diff] [blame] | 578 | static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 579 | // FIXME: Handle isModulePrivate. |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 580 | switch (D->getModuleOwnershipKind()) { |
| 581 | case Decl::ModuleOwnershipKind::Unowned: |
| 582 | case Decl::ModuleOwnershipKind::ModulePrivate: |
| 583 | return false; |
| 584 | case Decl::ModuleOwnershipKind::Visible: |
| 585 | case Decl::ModuleOwnershipKind::VisibleWhenImported: |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 586 | return isInModulePurview(D); |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 587 | } |
| 588 | llvm_unreachable("unexpected module ownership kind"); |
| 589 | } |
| 590 | |
| 591 | static LinkageInfo getInternalLinkageFor(const NamedDecl *D) { |
| 592 | // Internal linkage declarations within a module interface unit are modeled |
| 593 | // as "module-internal linkage", which means that they have internal linkage |
| 594 | // formally but can be indirectly accessed from outside the module via inline |
| 595 | // functions and templates defined within the module. |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 596 | if (isInModulePurview(D)) |
| 597 | return LinkageInfo(ModuleInternalLinkage, DefaultVisibility, false); |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 598 | |
| 599 | return LinkageInfo::internal(); |
| 600 | } |
| 601 | |
| 602 | static LinkageInfo getExternalLinkageFor(const NamedDecl *D) { |
| 603 | // C++ Modules TS [basic.link]/6.8: |
| 604 | // - A name declared at namespace scope that does not have internal linkage |
| 605 | // by the previous rules and that is introduced by a non-exported |
| 606 | // declaration has module linkage. |
| Richard Smith | 96451e3 | 2019-04-19 02:46:50 +0000 | [diff] [blame] | 607 | if (isInModulePurview(D) && !isExportedFromModuleInterfaceUnit( |
| 608 | cast<NamedDecl>(D->getCanonicalDecl()))) |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 609 | return LinkageInfo(ModuleLinkage, DefaultVisibility, false); |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 610 | |
| 611 | return LinkageInfo::external(); |
| 612 | } |
| 613 | |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 614 | static StorageClass getStorageClass(const Decl *D) { |
| 615 | if (auto *TD = dyn_cast<TemplateDecl>(D)) |
| 616 | D = TD->getTemplatedDecl(); |
| Hans Wennborg | b0dbc961 | 2019-05-14 10:11:33 +0000 | [diff] [blame] | 617 | if (D) { |
| 618 | if (auto *VD = dyn_cast<VarDecl>(D)) |
| 619 | return VD->getStorageClass(); |
| 620 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 621 | return FD->getStorageClass(); |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 622 | } |
| 623 | return SC_None; |
| 624 | } |
| 625 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 626 | LinkageInfo |
| 627 | LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 628 | LVComputationKind computation, |
| 629 | bool IgnoreVarTypeLinkage) { |
| Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 630 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 631 | "Not a name having namespace scope"); |
| 632 | ASTContext &Context = D->getASTContext(); |
| 633 | |
| 634 | // C++ [basic.link]p3: |
| 635 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 636 | // is the name of |
| Ilya Biryukov | b8292c9 | 2019-04-24 08:50:24 +0000 | [diff] [blame] | 637 | |
| Hans Wennborg | b0dbc961 | 2019-05-14 10:11:33 +0000 | [diff] [blame] | 638 | if (getStorageClass(D->getCanonicalDecl()) == SC_Static) { |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 639 | // - a variable, variable template, function, or function template |
| 640 | // that is explicitly declared static; or |
| 641 | // (This bullet corresponds to C99 6.2.2p3.) |
| 642 | return getInternalLinkageFor(D); |
| 643 | } |
| 644 | |
| 645 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
| 646 | // - a non-template variable of non-volatile const-qualified type, unless |
| 647 | // - it is explicitly declared extern, or |
| 648 | // - it is inline or exported, or |
| 649 | // - it was previously declared and the prior declaration did not have |
| 650 | // internal linkage |
| 651 | // (There is no equivalent in C99.) |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 652 | if (Context.getLangOpts().CPlusPlus && |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 653 | Var->getType().isConstQualified() && |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 654 | !Var->getType().isVolatileQualified() && |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 655 | !Var->isInline() && |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 656 | !isExportedFromModuleInterfaceUnit(Var) && |
| 657 | !isa<VarTemplateSpecializationDecl>(Var) && |
| 658 | !Var->getDescribedVarTemplate()) { |
| Rafael Espindola | 985a3ab | 2013-04-03 19:22:20 +0000 | [diff] [blame] | 659 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
| Rafael Espindola | 985a3ab | 2013-04-03 19:22:20 +0000 | [diff] [blame] | 660 | if (PrevVar) |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 661 | return getLVForDecl(PrevVar, computation); |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 662 | |
| 663 | if (Var->getStorageClass() != SC_Extern && |
| Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 664 | Var->getStorageClass() != SC_PrivateExtern && |
| Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 665 | !isSingleLineLanguageLinkage(*Var)) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 666 | return getInternalLinkageFor(Var); |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; |
| 670 | PrevVar = PrevVar->getPreviousDecl()) { |
| 671 | if (PrevVar->getStorageClass() == SC_PrivateExtern && |
| 672 | Var->getStorageClass() == SC_None) |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 673 | return getDeclLinkageAndVisibility(PrevVar); |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 674 | // Explicitly declared static. |
| 675 | if (PrevVar->getStorageClass() == SC_Static) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 676 | return getInternalLinkageFor(Var); |
| Fariborz Jahanian | 8feee2d | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 677 | } |
| David Majnemer | 18fac3b | 2014-12-10 22:58:14 +0000 | [diff] [blame] | 678 | } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) { |
| 679 | // - a data member of an anonymous union. |
| 680 | const VarDecl *VD = IFD->getVarDecl(); |
| 681 | assert(VD && "Expected a VarDecl in this IndirectFieldDecl!"); |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 682 | return getLVForNamespaceScopeDecl(VD, computation, IgnoreVarTypeLinkage); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 683 | } |
| David Majnemer | 0eb8bbd | 2013-10-23 20:52:43 +0000 | [diff] [blame] | 684 | assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!"); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 685 | |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 686 | // FIXME: This gives internal linkage to names that should have no linkage |
| 687 | // (those not covered by [basic.link]p6). |
| Chandler Carruth | 9682a2fd | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 688 | if (D->isInAnonymousNamespace()) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 689 | const auto *Var = dyn_cast<VarDecl>(D); |
| 690 | const auto *Func = dyn_cast<FunctionDecl>(D); |
| Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 691 | // FIXME: The check for extern "C" here is not justified by the standard |
| 692 | // wording, but we retain it from the pre-DR1113 model to avoid breaking |
| 693 | // code. |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 694 | // |
| 695 | // C++11 [basic.link]p4: |
| 696 | // An unnamed namespace or a namespace declared directly or indirectly |
| 697 | // within an unnamed namespace has internal linkage. |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 698 | if ((!Var || !isFirstInExternCContext(Var)) && |
| 699 | (!Func || !isFirstInExternCContext(Func))) |
| Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 700 | return getInternalLinkageFor(D); |
| Chandler Carruth | 9682a2fd | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 701 | } |
| John McCall | b7139c4 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 702 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 703 | // Set up the defaults. |
| 704 | |
| 705 | // C99 6.2.2p5: |
| 706 | // If the declaration of an identifier for an object has file |
| 707 | // scope and no storage-class specifier, its linkage is |
| 708 | // external. |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 709 | LinkageInfo LV = getExternalLinkageFor(D); |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 710 | |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 711 | if (!hasExplicitVisibilityAlready(computation)) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 712 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) { |
| Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 713 | LV.mergeVisibility(*Vis, true); |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 714 | } else { |
| 715 | // If we're declared in a namespace with a visibility attribute, |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 716 | // use that namespace's visibility, and it still counts as explicit. |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 717 | for (const DeclContext *DC = D->getDeclContext(); |
| 718 | !isa<TranslationUnitDecl>(DC); |
| 719 | DC = DC->getParent()) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 720 | const auto *ND = dyn_cast<NamespaceDecl>(DC); |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 721 | if (!ND) continue; |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 722 | if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) { |
| Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 723 | LV.mergeVisibility(*Vis, true); |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 724 | break; |
| 725 | } |
| 726 | } |
| 727 | } |
| Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 728 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 729 | // 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] | 730 | if (!LV.isVisibilityExplicit()) { |
| John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 731 | // Use global type/value visibility as appropriate. |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 732 | Visibility globalVisibility = |
| 733 | computation.isValueVisibility() |
| 734 | ? Context.getLangOpts().getValueVisibilityMode() |
| 735 | : Context.getLangOpts().getTypeVisibilityMode(); |
| John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 736 | LV.mergeVisibility(globalVisibility, /*explicit*/ false); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 737 | |
| 738 | // If we're paying attention to global visibility, apply |
| 739 | // -finline-visibility-hidden if this is an inline method. |
| 740 | if (useInlineVisibilityHidden(D)) |
| Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 741 | LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 742 | } |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 743 | } |
| Rafael Espindola | af690f5 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 744 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 745 | // C++ [basic.link]p4: |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 746 | |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 747 | // A name having namespace scope that has not been given internal linkage |
| 748 | // above and that is the name of |
| 749 | // [...bullets...] |
| 750 | // has its linkage determined as follows: |
| 751 | // - if the enclosing namespace has internal linkage, the name has |
| 752 | // internal linkage; [handled above] |
| 753 | // - otherwise, if the declaration of the name is attached to a named |
| 754 | // module and is not exported, the name has module linkage; |
| 755 | // - otherwise, the name has external linkage. |
| 756 | // LV is currently set up to handle the last two bullets. |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 757 | // |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 758 | // The bullets are: |
| 759 | |
| 760 | // - a variable; or |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 761 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 762 | // GCC applies the following optimization to variables and static |
| 763 | // data members, but not to functions: |
| 764 | // |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 765 | // Modify the variable's LV by the LV of its type unless this is |
| 766 | // C or extern "C". This follows from [basic.link]p9: |
| 767 | // A type without linkage shall not be used as the type of a |
| 768 | // variable or function with external linkage unless |
| 769 | // - the entity has C language linkage, or |
| 770 | // - the entity is declared within an unnamed namespace, or |
| 771 | // - the entity is not used or is defined in the same |
| 772 | // translation unit. |
| 773 | // and [basic.link]p10: |
| 774 | // ...the types specified by all declarations referring to a |
| 775 | // given variable or function shall be identical... |
| 776 | // C does not have an equivalent rule. |
| 777 | // |
| John McCall | 5fe8412 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 778 | // Ignore this if we've got an explicit attribute; the user |
| 779 | // probably knows what they're doing. |
| 780 | // |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 781 | // Note that we don't want to make the variable non-external |
| 782 | // because of this, but unique-external linkage suits us. |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 783 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var) && |
| 784 | !IgnoreVarTypeLinkage) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 785 | LinkageInfo TypeLV = getLVForType(*Var->getType(), computation); |
| Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 786 | if (!isExternallyVisible(TypeLV.getLinkage())) |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 787 | return LinkageInfo::uniqueExternal(); |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 788 | if (!LV.isVisibilityExplicit()) |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 789 | LV.mergeVisibility(TypeLV); |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 792 | if (Var->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 | d5ed033 | 2012-11-12 04:10:23 +0000 | [diff] [blame] | 795 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 796 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't have |
| 797 | // to do it here. |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 798 | |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 799 | // As per function and class template specializations (below), |
| 800 | // consider LV for the template and template arguments. We're at file |
| 801 | // scope, so we do not need to worry about nested specializations. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 802 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) { |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 803 | mergeTemplateLV(LV, spec, computation); |
| 804 | } |
| 805 | |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 806 | // - a function; or |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 807 | } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
| John McCall | 2efaf11 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 808 | // In theory, we can modify the function's LV by the LV of its |
| 809 | // type unless it has C linkage (see comment above about variables |
| 810 | // for justification). In practice, GCC doesn't do this, so it's |
| 811 | // just too painful to make work. |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 812 | |
| John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 813 | if (Function->getStorageClass() == SC_PrivateExtern) |
| Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 814 | LV.mergeVisibility(HiddenVisibility, true); |
| John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 815 | |
| Rafael Espindola | a508c5d | 2012-11-21 02:47:19 +0000 | [diff] [blame] | 816 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 817 | // merging storage classes and visibility attributes, so we don't have to |
| 818 | // look at previous decls in here. |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 819 | |
| John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 820 | // In C++, then if the type of the function uses a type with |
| 821 | // unique-external linkage, it's not legally usable from outside |
| 822 | // this translation unit. However, we should use the C linkage |
| 823 | // rules instead for extern "C" declarations. |
| Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 824 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Function)) { |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 825 | // Only look at the type-as-written. Otherwise, deducing the return type |
| 826 | // of a function could change its linkage. |
| Richard Smith | 50f4afc | 2013-05-12 23:17:59 +0000 | [diff] [blame] | 827 | QualType TypeAsWritten = Function->getType(); |
| 828 | if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) |
| 829 | TypeAsWritten = TSI->getType(); |
| Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 830 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
| Richard Smith | 50f4afc | 2013-05-12 23:17:59 +0000 | [diff] [blame] | 831 | return LinkageInfo::uniqueExternal(); |
| 832 | } |
| John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 833 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 834 | // Consider LV from the template and the template arguments. |
| 835 | // We're at file scope, so we do not need to worry about nested |
| 836 | // specializations. |
| John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 837 | if (FunctionTemplateSpecializationInfo *specInfo |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 838 | = Function->getTemplateSpecializationInfo()) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 839 | mergeTemplateLV(LV, Function, specInfo, computation); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 842 | // - a named class (Clause 9), or an unnamed class defined in a |
| 843 | // typedef declaration in which the class has the typedef name |
| 844 | // for linkage purposes (7.1.3); or |
| 845 | // - a named enumeration (7.2), or an unnamed enumeration |
| 846 | // defined in a typedef declaration in which the enumeration |
| 847 | // has the typedef name for linkage purposes (7.1.3); or |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 848 | } else if (const auto *Tag = dyn_cast<TagDecl>(D)) { |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 849 | // Unnamed tags have no linkage. |
| John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 850 | if (!Tag->hasNameForLinkage()) |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 851 | return LinkageInfo::none(); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 852 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 853 | // If this is a class template specialization, consider the |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 854 | // linkage of the template and template arguments. We're at file |
| 855 | // scope, so we do not need to worry about nested specializations. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 856 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 857 | mergeTemplateLV(LV, spec, computation); |
| Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 858 | } |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 859 | |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 860 | // FIXME: This is not part of the C++ standard any more. |
| 861 | // - an enumerator belonging to an enumeration with external linkage; or |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 862 | } else if (isa<EnumConstantDecl>(D)) { |
| Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 863 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 864 | computation); |
| Rafael Espindola | b97e896 | 2013-05-27 14:14:42 +0000 | [diff] [blame] | 865 | if (!isExternalFormalLinkage(EnumLV.getLinkage())) |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 866 | return LinkageInfo::none(); |
| 867 | LV.merge(EnumLV); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 868 | |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 869 | // - a template |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 870 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 871 | bool considerVisibility = !hasExplicitVisibilityAlready(computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 872 | LinkageInfo tempLV = |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 873 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 874 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 875 | |
| Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 876 | // An unnamed namespace or a namespace declared directly or indirectly |
| 877 | // within an unnamed namespace has internal linkage. All other namespaces |
| 878 | // have external linkage. |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 879 | // |
| 880 | // We handled names in anonymous namespaces above. |
| 881 | } else if (isa<NamespaceDecl>(D)) { |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 882 | return LV; |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 883 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 884 | // By extension, we assign external linkage to Objective-C |
| 885 | // interfaces. |
| 886 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 887 | // fallout |
| 888 | |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 889 | } else if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 890 | // A typedef declaration has linkage if it gives a type a name for |
| 891 | // linkage purposes. |
| 892 | if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
| 893 | return LinkageInfo::none(); |
| 894 | |
| Richard Smith | bab6df8 | 2020-04-11 22:15:29 -0700 | [diff] [blame] | 895 | } else if (isa<MSGuidDecl>(D)) { |
| 896 | // A GUID behaves like an inline variable with external linkage. Fall |
| 897 | // through. |
| 898 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 899 | // Everything not covered here has no linkage. |
| 900 | } else { |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 901 | return LinkageInfo::none(); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 902 | } |
| 903 | |
| Richard Smith | c445d5d | 2017-10-03 01:58:15 +0000 | [diff] [blame] | 904 | // If we ended up with non-externally-visible linkage, visibility should |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 905 | // always be default. |
| Richard Smith | c445d5d | 2017-10-03 01:58:15 +0000 | [diff] [blame] | 906 | if (!isExternallyVisible(LV.getLinkage())) |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 907 | return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 908 | |
| Alexey Bataev | bbc328c | 2019-11-21 11:30:43 -0500 | [diff] [blame] | 909 | // Mark the symbols as hidden when compiling for the device. |
| 910 | if (Context.getLangOpts().OpenMP && Context.getLangOpts().OpenMPIsDevice) |
| 911 | LV.mergeVisibility(HiddenVisibility, /*newExplicit=*/false); |
| 912 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 913 | return LV; |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 916 | LinkageInfo |
| 917 | LinkageComputer::getLVForClassMember(const NamedDecl *D, |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 918 | LVComputationKind computation, |
| 919 | bool IgnoreVarTypeLinkage) { |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 920 | // Only certain class members have linkage. Note that fields don't |
| 921 | // really have linkage, but it's convenient to say they do for the |
| 922 | // purposes of calculating linkage of pointer-to-data-member |
| 923 | // template arguments. |
| Richard Smith | 26d11be | 2014-01-08 01:51:59 +0000 | [diff] [blame] | 924 | // |
| 925 | // Templates also don't officially have linkage, but since we ignore |
| 926 | // the C++ standard and look at template arguments when determining |
| 927 | // linkage and visibility of a template specialization, we might hit |
| 928 | // a template template argument that way. If we do, we need to |
| 929 | // consider its linkage. |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 930 | if (!(isa<CXXMethodDecl>(D) || |
| 931 | isa<VarDecl>(D) || |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 932 | isa<FieldDecl>(D) || |
| David Majnemer | 0eb8bbd | 2013-10-23 20:52:43 +0000 | [diff] [blame] | 933 | isa<IndirectFieldDecl>(D) || |
| Richard Smith | 26d11be | 2014-01-08 01:51:59 +0000 | [diff] [blame] | 934 | isa<TagDecl>(D) || |
| 935 | isa<TemplateDecl>(D))) |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 936 | return LinkageInfo::none(); |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 937 | |
| John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 938 | LinkageInfo LV; |
| 939 | |
| John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 940 | // If we have an explicit visibility attribute, merge that in. |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 941 | if (!hasExplicitVisibilityAlready(computation)) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 942 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 943 | LV.mergeVisibility(*Vis, true); |
| Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 944 | // If we're paying attention to global visibility, apply |
| 945 | // -finline-visibility-hidden if this is an inline method. |
| 946 | // |
| 947 | // Note that we do this before merging information about |
| 948 | // the class visibility. |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 949 | if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) |
| Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 950 | LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); |
| John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 951 | } |
| Rafael Espindola | 53cf219 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 952 | |
| 953 | // If this class member has an explicit visibility attribute, the only |
| 954 | // thing that can change its visibility is the template arguments, so |
| Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 955 | // only look for them when processing the class. |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 956 | LVComputationKind classComputation = computation; |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 957 | if (LV.isVisibilityExplicit()) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 958 | classComputation = withExplicitVisibilityAlready(computation); |
| Rafael Espindola | 505a7c8 | 2012-04-16 18:25:01 +0000 | [diff] [blame] | 959 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 960 | LinkageInfo classLV = |
| 961 | getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation); |
| Richard Smith | cdb06f2 | 2017-09-23 04:02:17 +0000 | [diff] [blame] | 962 | // The member has the same linkage as the class. If that's not externally |
| 963 | // visible, we don't need to compute anything about the linkage. |
| 964 | // FIXME: If we're only computing linkage, can we bail out here? |
| Rafael Espindola | e6190db | 2013-05-28 02:22:10 +0000 | [diff] [blame] | 965 | if (!isExternallyVisible(classLV.getLinkage())) |
| Richard Smith | cdb06f2 | 2017-09-23 04:02:17 +0000 | [diff] [blame] | 966 | return classLV; |
| Rafael Espindola | e6190db | 2013-05-28 02:22:10 +0000 | [diff] [blame] | 967 | |
| 968 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 969 | // Otherwise, don't merge in classLV yet, because in certain cases |
| 970 | // we need to completely ignore the visibility from it. |
| 971 | |
| 972 | // Specifically, if this decl exists and has an explicit attribute. |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 973 | const NamedDecl *explicitSpecSuppressor = nullptr; |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 974 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 975 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 976 | // Only look at the type-as-written. Otherwise, deducing the return type |
| 977 | // of a function could change its linkage. |
| 978 | QualType TypeAsWritten = MD->getType(); |
| 979 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 980 | TypeAsWritten = TSI->getType(); |
| 981 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
| 982 | return LinkageInfo::uniqueExternal(); |
| 983 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 984 | // If this is a method template specialization, use the linkage for |
| 985 | // the template parameters and arguments. |
| John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 986 | if (FunctionTemplateSpecializationInfo *spec |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 987 | = MD->getTemplateSpecializationInfo()) { |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 988 | mergeTemplateLV(LV, MD, spec, computation); |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 989 | if (spec->isExplicitSpecialization()) { |
| 990 | explicitSpecSuppressor = MD; |
| 991 | } else if (isExplicitMemberSpecialization(spec->getTemplate())) { |
| 992 | explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); |
| 993 | } |
| 994 | } else if (isExplicitMemberSpecialization(MD)) { |
| 995 | explicitSpecSuppressor = MD; |
| John McCall | e6e622e | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 996 | } |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 997 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 998 | } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 999 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1000 | mergeTemplateLV(LV, spec, computation); |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1001 | if (spec->isExplicitSpecialization()) { |
| 1002 | explicitSpecSuppressor = spec; |
| 1003 | } else { |
| 1004 | const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 1005 | if (isExplicitMemberSpecialization(temp)) { |
| 1006 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 1007 | } |
| 1008 | } |
| 1009 | } else if (isExplicitMemberSpecialization(RD)) { |
| 1010 | explicitSpecSuppressor = RD; |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1013 | // Static data members. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1014 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 1015 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD)) |
| Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 1016 | mergeTemplateLV(LV, spec, computation); |
| 1017 | |
| John McCall | 36cd5cc | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 1018 | // Modify the variable's linkage by its type, but ignore the |
| 1019 | // type's visibility unless it's a definition. |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1020 | if (!IgnoreVarTypeLinkage) { |
| 1021 | LinkageInfo typeLV = getLVForType(*VD->getType(), computation); |
| 1022 | // FIXME: If the type's linkage is not externally visible, we can |
| 1023 | // give this static data member UniqueExternalLinkage. |
| 1024 | if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) |
| 1025 | LV.mergeVisibility(typeLV); |
| 1026 | LV.mergeExternalVisibility(typeLV); |
| 1027 | } |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1028 | |
| 1029 | if (isExplicitMemberSpecialization(VD)) { |
| 1030 | explicitSpecSuppressor = VD; |
| 1031 | } |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Template members. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1034 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1035 | bool considerVisibility = |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 1036 | (!LV.isVisibilityExplicit() && |
| 1037 | !classLV.isVisibilityExplicit() && |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1038 | !hasExplicitVisibilityAlready(computation)); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1039 | LinkageInfo tempLV = |
| Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 1040 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1041 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1042 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1043 | if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) { |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1044 | if (isExplicitMemberSpecialization(redeclTemp)) { |
| 1045 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 1046 | } |
| 1047 | } |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1050 | // We should never be looking for an attribute directly on a template. |
| 1051 | assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); |
| 1052 | |
| 1053 | // If this member is an explicit member specialization, and it has |
| 1054 | // an explicit attribute, ignore visibility from the parent. |
| 1055 | bool considerClassVisibility = true; |
| 1056 | if (explicitSpecSuppressor && |
| Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 1057 | // optimization: hasDVA() is true only with explicit visibility. |
| 1058 | LV.isVisibilityExplicit() && |
| 1059 | classLV.getVisibility() != DefaultVisibility && |
| John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1060 | hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) { |
| 1061 | considerClassVisibility = false; |
| 1062 | } |
| 1063 | |
| 1064 | // Finally, merge in information from the class. |
| 1065 | LV.mergeMaybeWithVisibility(classLV, considerClassVisibility); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1066 | return LV; |
| John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1069 | void NamedDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1070 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1071 | bool NamedDecl::isLinkageValid() const { |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1072 | if (!hasCachedLinkage()) |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1073 | return true; |
| John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 1074 | |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1075 | Linkage L = LinkageComputer{} |
| 1076 | .computeLVForDecl(this, LVComputationKind::forLinkageOnly()) |
| 1077 | .getLinkage(); |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1078 | return L == getCachedLinkage(); |
| John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1081 | ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { |
| 1082 | StringRef name = getName(); |
| 1083 | if (name.empty()) return SFF_None; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1084 | |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1085 | if (name.front() == 'C') |
| 1086 | if (name == "CFStringCreateWithFormat" || |
| 1087 | name == "CFStringCreateWithFormatAndArguments" || |
| 1088 | name == "CFStringAppendFormat" || |
| 1089 | name == "CFStringAppendFormatAndArguments") |
| 1090 | return SFF_CFString; |
| 1091 | return SFF_None; |
| 1092 | } |
| 1093 | |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1094 | Linkage NamedDecl::getLinkageInternal() const { |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1095 | // We don't care about visibility here, so ask for the cheapest |
| 1096 | // possible visibility analysis. |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1097 | return LinkageComputer{} |
| 1098 | .getLVForDecl(this, LVComputationKind::forLinkageOnly()) |
| 1099 | .getLinkage(); |
| Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1102 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1103 | return LinkageComputer{}.getDeclLinkageAndVisibility(this); |
| John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1104 | } |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1105 | |
| Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1106 | static Optional<Visibility> |
| 1107 | getExplicitVisibilityAux(const NamedDecl *ND, |
| 1108 | NamedDecl::ExplicitVisibilityKind kind, |
| 1109 | bool IsMostRecent) { |
| 1110 | assert(!IsMostRecent || ND == ND->getMostRecentDecl()); |
| 1111 | |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1112 | // Check the declaration itself first. |
| Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1113 | if (Optional<Visibility> V = getVisibilityOf(ND, kind)) |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1114 | return V; |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1115 | |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1116 | // If this is a member class of a specialization of a class template |
| 1117 | // and the corresponding decl has explicit visibility, use that. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1118 | if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) { |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1119 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 1120 | if (InstantiatedFrom) |
| 1121 | return getVisibilityOf(InstantiatedFrom, kind); |
| 1122 | } |
| 1123 | |
| 1124 | // If there wasn't explicit visibility there, and this is a |
| 1125 | // specialization of a class template, check for visibility |
| 1126 | // on the pattern. |
| Steven Wu | 79cbd11 | 2018-04-19 15:46:43 +0000 | [diff] [blame] | 1127 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 1128 | // Walk all the template decl till this point to see if there are |
| 1129 | // explicit visibility attributes. |
| 1130 | const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl(); |
| 1131 | while (TD != nullptr) { |
| 1132 | auto Vis = getVisibilityOf(TD, kind); |
| 1133 | if (Vis != None) |
| 1134 | return Vis; |
| 1135 | TD = TD->getPreviousDecl(); |
| 1136 | } |
| 1137 | return None; |
| 1138 | } |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1139 | |
| 1140 | // Use the most recent declaration. |
| Rafael Espindola | 7ab1ce0 | 2013-12-08 01:13:22 +0000 | [diff] [blame] | 1141 | if (!IsMostRecent && !isa<NamespaceDecl>(ND)) { |
| Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1142 | const NamedDecl *MostRecent = ND->getMostRecentDecl(); |
| 1143 | if (MostRecent != ND) |
| 1144 | return getExplicitVisibilityAux(MostRecent, kind, true); |
| 1145 | } |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1146 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1147 | if (const auto *Var = dyn_cast<VarDecl>(ND)) { |
| Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1148 | if (Var->isStaticDataMember()) { |
| 1149 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
| 1150 | if (InstantiatedFrom) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1151 | return getVisibilityOf(InstantiatedFrom, kind); |
| Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| David Majnemer | 35b02bc | 2014-04-29 07:32:26 +0000 | [diff] [blame] | 1154 | if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var)) |
| 1155 | return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(), |
| 1156 | kind); |
| 1157 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1158 | return None; |
| Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1159 | } |
| Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1160 | // Also handle function template specializations. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1161 | if (const auto *fn = dyn_cast<FunctionDecl>(ND)) { |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1162 | // If the function is a specialization of a template with an |
| 1163 | // explicit visibility attribute, use that. |
| 1164 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 1165 | = fn->getTemplateSpecializationInfo()) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1166 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), |
| 1167 | kind); |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1168 | |
| Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 1169 | // If the function is a member of a specialization of a class template |
| 1170 | // and the corresponding decl has explicit visibility, use that. |
| 1171 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 1172 | if (InstantiatedFrom) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1173 | return getVisibilityOf(InstantiatedFrom, kind); |
| Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 1174 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1175 | return None; |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| Rafael Espindola | fb4263f | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 1178 | // The visibility of a template is stored in the templated decl. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1179 | if (const auto *TD = dyn_cast<TemplateDecl>(ND)) |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1180 | return getVisibilityOf(TD->getTemplatedDecl(), kind); |
| Rafael Espindola | fb4263f | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 1181 | |
| David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1182 | return None; |
| Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
| Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1185 | Optional<Visibility> |
| 1186 | NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { |
| 1187 | return getExplicitVisibilityAux(this, kind, false); |
| 1188 | } |
| 1189 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1190 | LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC, |
| 1191 | Decl *ContextDecl, |
| 1192 | LVComputationKind computation) { |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1193 | // This lambda has its linkage/visibility determined by its owner. |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1194 | const NamedDecl *Owner; |
| 1195 | if (!ContextDecl) |
| 1196 | Owner = dyn_cast<NamedDecl>(DC); |
| 1197 | else if (isa<ParmVarDecl>(ContextDecl)) |
| 1198 | Owner = |
| 1199 | dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext()); |
| 1200 | else |
| 1201 | Owner = cast<NamedDecl>(ContextDecl); |
| 1202 | |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1203 | if (!Owner) |
| Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1204 | return LinkageInfo::none(); |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1205 | |
| 1206 | // If the owner has a deduced type, we need to skip querying the linkage and |
| 1207 | // visibility of that type, because it might involve this closure type. The |
| 1208 | // only effect of this is that we might give a lambda VisibleNoLinkage rather |
| 1209 | // than NoLinkage when we don't strictly need to, which is benign. |
| 1210 | auto *VD = dyn_cast<VarDecl>(Owner); |
| Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1211 | LinkageInfo OwnerLV = |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1212 | VD && VD->getType()->getContainedDeducedType() |
| 1213 | ? computeLVForDecl(Owner, computation, /*IgnoreVarTypeLinkage*/true) |
| 1214 | : getLVForDecl(Owner, computation); |
| 1215 | |
| Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1216 | // A lambda never formally has linkage. But if the owner is externally |
| 1217 | // visible, then the lambda is too. We apply the same rules to blocks. |
| 1218 | if (!isExternallyVisible(OwnerLV.getLinkage())) |
| 1219 | return LinkageInfo::none(); |
| 1220 | return LinkageInfo(VisibleNoLinkage, OwnerLV.getVisibility(), |
| 1221 | OwnerLV.isVisibilityExplicit()); |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1224 | LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D, |
| 1225 | LVComputationKind computation) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1226 | if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1227 | if (Function->isInAnonymousNamespace() && |
| Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 1228 | !isFirstInExternCContext(Function)) |
| Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 1229 | return getInternalLinkageFor(Function); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1230 | |
| 1231 | // This is a "void f();" which got merged with a file static. |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1232 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1233 | return getInternalLinkageFor(Function); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1234 | |
| 1235 | LinkageInfo LV; |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1236 | if (!hasExplicitVisibilityAlready(computation)) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1237 | if (Optional<Visibility> Vis = |
| 1238 | getExplicitVisibility(Function, computation)) |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1239 | LV.mergeVisibility(*Vis, true); |
| 1240 | } |
| 1241 | |
| 1242 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 1243 | // merging storage classes and visibility attributes, so we don't have to |
| 1244 | // look at previous decls in here. |
| 1245 | |
| 1246 | return LV; |
| 1247 | } |
| 1248 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1249 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1250 | if (Var->hasExternalStorage()) { |
| Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 1251 | if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(Var)) |
| Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 1252 | return getInternalLinkageFor(Var); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1253 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1254 | LinkageInfo LV; |
| 1255 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 1256 | LV.mergeVisibility(HiddenVisibility, true); |
| John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1257 | else if (!hasExplicitVisibilityAlready(computation)) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1258 | if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation)) |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1259 | LV.mergeVisibility(*Vis, true); |
| 1260 | } |
| 1261 | |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1262 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
| 1263 | LinkageInfo PrevLV = getLVForDecl(Prev, computation); |
| 1264 | if (PrevLV.getLinkage()) |
| 1265 | LV.setLinkage(PrevLV.getLinkage()); |
| 1266 | LV.mergeVisibility(PrevLV); |
| 1267 | } |
| 1268 | |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1269 | return LV; |
| 1270 | } |
| Rafael Espindola | a418418 | 2013-06-17 20:04:51 +0000 | [diff] [blame] | 1271 | |
| 1272 | if (!Var->isStaticLocal()) |
| 1273 | return LinkageInfo::none(); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| Rafael Espindola | a418418 | 2013-06-17 20:04:51 +0000 | [diff] [blame] | 1276 | ASTContext &Context = D->getASTContext(); |
| 1277 | if (!Context.getLangOpts().CPlusPlus) |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1278 | return LinkageInfo::none(); |
| 1279 | |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1280 | const Decl *OuterD = getOutermostFuncOrBlockContext(D); |
| Alexey Bataev | 0a47e65 | 2016-01-12 09:01:25 +0000 | [diff] [blame] | 1281 | if (!OuterD || OuterD->isInvalidDecl()) |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1282 | return LinkageInfo::none(); |
| Rafael Espindola | 5218936 | 2013-06-04 13:43:35 +0000 | [diff] [blame] | 1283 | |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1284 | LinkageInfo LV; |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1285 | if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) { |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1286 | if (!BD->getBlockManglingNumber()) |
| 1287 | return LinkageInfo::none(); |
| Rafael Espindola | 5218936 | 2013-06-04 13:43:35 +0000 | [diff] [blame] | 1288 | |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1289 | LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(), |
| 1290 | BD->getBlockManglingContextDecl(), computation); |
| 1291 | } else { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1292 | const auto *FD = cast<FunctionDecl>(OuterD); |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1293 | if (!FD->isInlined() && |
| David Majnemer | 9963ade | 2014-12-16 04:52:14 +0000 | [diff] [blame] | 1294 | !isTemplateInstantiation(FD->getTemplateSpecializationKind())) |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1295 | return LinkageInfo::none(); |
| 1296 | |
| Hiroshi Inoue | 03ff37d | 2018-08-22 05:43:27 +0000 | [diff] [blame] | 1297 | // If a function is hidden by -fvisibility-inlines-hidden option and |
| 1298 | // is not explicitly attributed as a hidden function, |
| 1299 | // we should not make static local variables in the function hidden. |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1300 | LV = getLVForDecl(FD, computation); |
| Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 1301 | if (isa<VarDecl>(D) && useInlineVisibilityHidden(FD) && |
| 1302 | !LV.isVisibilityExplicit()) { |
| 1303 | assert(cast<VarDecl>(D)->isStaticLocal()); |
| 1304 | // If this was an implicitly hidden inline method, check again for |
| 1305 | // explicit visibility on the parent class, and use that for static locals |
| 1306 | // if present. |
| 1307 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1308 | LV = getLVForDecl(MD->getParent(), computation); |
| 1309 | if (!LV.isVisibilityExplicit()) { |
| 1310 | Visibility globalVisibility = |
| 1311 | computation.isValueVisibility() |
| 1312 | ? Context.getLangOpts().getValueVisibilityMode() |
| 1313 | : Context.getLangOpts().getTypeVisibilityMode(); |
| 1314 | return LinkageInfo(VisibleNoLinkage, globalVisibility, |
| 1315 | /*visibilityExplicit=*/false); |
| 1316 | } |
| 1317 | } |
| Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1318 | } |
| Rafael Espindola | 111bb2e | 2013-05-27 14:50:21 +0000 | [diff] [blame] | 1319 | if (!isExternallyVisible(LV.getLinkage())) |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1320 | return LinkageInfo::none(); |
| 1321 | return LinkageInfo(VisibleNoLinkage, LV.getVisibility(), |
| 1322 | LV.isVisibilityExplicit()); |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1325 | LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D, |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1326 | LVComputationKind computation, |
| 1327 | bool IgnoreVarTypeLinkage) { |
| Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1328 | // Internal_linkage attribute overrides other considerations. |
| 1329 | if (D->hasAttr<InternalLinkageAttr>()) |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1330 | return getInternalLinkageFor(D); |
| Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1331 | |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1332 | // Objective-C: treat all Objective-C declarations as having external |
| 1333 | // linkage. |
| John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1334 | switch (D->getKind()) { |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1335 | default: |
| 1336 | break; |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Per C++ [basic.link]p2, only the names of objects, references, |
| 1339 | // functions, types, templates, namespaces, and values ever have linkage. |
| 1340 | // |
| 1341 | // Note that the name of a typedef, namespace alias, using declaration, |
| 1342 | // and so on are not the name of the corresponding type, namespace, or |
| 1343 | // declaration, so they do *not* have linkage. |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1344 | case Decl::ImplicitParam: |
| 1345 | case Decl::Label: |
| 1346 | case Decl::NamespaceAlias: |
| Argyrios Kyrtzidis | 79d0428 | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 1347 | case Decl::ParmVar: |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1348 | case Decl::Using: |
| 1349 | case Decl::UsingShadow: |
| 1350 | case Decl::UsingDirective: |
| Argyrios Kyrtzidis | 79d0428 | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 1351 | return LinkageInfo::none(); |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1352 | |
| Richard Smith | 26210db | 2015-11-13 03:52:13 +0000 | [diff] [blame] | 1353 | case Decl::EnumConstant: |
| 1354 | // 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] | 1355 | if (D->getASTContext().getLangOpts().CPlusPlus) |
| 1356 | return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation); |
| 1357 | return LinkageInfo::visible_none(); |
| Richard Smith | 26210db | 2015-11-13 03:52:13 +0000 | [diff] [blame] | 1358 | |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1359 | case Decl::Typedef: |
| 1360 | case Decl::TypeAlias: |
| 1361 | // A typedef declaration has linkage if it gives a type a name for |
| 1362 | // linkage purposes. |
| Vassil Vassilev | b00ea08 | 2017-07-01 20:44:49 +0000 | [diff] [blame] | 1363 | if (!cast<TypedefNameDecl>(D) |
| Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1364 | ->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
| 1365 | return LinkageInfo::none(); |
| 1366 | break; |
| 1367 | |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1368 | case Decl::TemplateTemplateParm: // count these as external |
| 1369 | case Decl::NonTypeTemplateParm: |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1370 | case Decl::ObjCAtDefsField: |
| 1371 | case Decl::ObjCCategory: |
| 1372 | case Decl::ObjCCategoryImpl: |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1373 | case Decl::ObjCCompatibleAlias: |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1374 | case Decl::ObjCImplementation: |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1375 | case Decl::ObjCMethod: |
| 1376 | case Decl::ObjCProperty: |
| 1377 | case Decl::ObjCPropertyImpl: |
| 1378 | case Decl::ObjCProtocol: |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1379 | return getExternalLinkageFor(D); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1380 | |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1381 | case Decl::CXXRecord: { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1382 | const auto *Record = cast<CXXRecordDecl>(D); |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1383 | if (Record->isLambda()) { |
| Michael Liao | 243ebfb | 2019-10-19 00:15:19 +0000 | [diff] [blame] | 1384 | if (Record->hasKnownLambdaInternalLinkage() || |
| 1385 | !Record->getLambdaManglingNumber()) { |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1386 | // This lambda has no mangling number, so it's internal. |
| Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1387 | return getInternalLinkageFor(D); |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1388 | } |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1389 | |
| Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1390 | return getLVForClosure( |
| Michael Liao | 2926917 | 2020-02-07 12:58:17 -0500 | [diff] [blame] | 1391 | Record->getDeclContext()->getRedeclContext(), |
| 1392 | Record->getLambdaContextDecl(), computation); |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1393 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1394 | |
| Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1395 | break; |
| 1396 | } |
| Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1399 | // Handle linkage for namespace-scope names. |
| John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1400 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1401 | return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1402 | |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1403 | // C++ [basic.link]p5: |
| 1404 | // In addition, a member function, static data member, a named |
| 1405 | // class or enumeration of class scope, or an unnamed class or |
| 1406 | // enumeration defined in a class-scope typedef declaration such |
| 1407 | // that the class or enumeration has the typedef name for linkage |
| 1408 | // purposes (7.1.3), has external linkage if the name of the class |
| 1409 | // has external linkage. |
| John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1410 | if (D->getDeclContext()->isRecord()) |
| Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1411 | return getLVForClassMember(D, computation, IgnoreVarTypeLinkage); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1412 | |
| 1413 | // C++ [basic.link]p6: |
| 1414 | // The name of a function declared in block scope and the name of |
| 1415 | // an object declared by a block scope extern declaration have |
| 1416 | // linkage. If there is a visible declaration of an entity with |
| 1417 | // linkage having the same name and type, ignoring entities |
| 1418 | // declared outside the innermost enclosing namespace scope, the |
| 1419 | // block scope declaration declares that same entity and receives |
| 1420 | // the linkage of the previous declaration. If there is more than |
| 1421 | // one such matching entity, the program is ill-formed. Otherwise, |
| 1422 | // if no matching entity is found, the block scope entity receives |
| 1423 | // external linkage. |
| John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1424 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 1425 | return getLVForLocalDecl(D, computation); |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1426 | |
| 1427 | // C++ [basic.link]p6: |
| 1428 | // Names not covered by these rules have no linkage. |
| John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1429 | return LinkageInfo::none(); |
| John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1430 | } |
| Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1431 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1432 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
| 1433 | LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D, |
| 1434 | LVComputationKind computation) { |
| 1435 | // Internal_linkage attribute overrides other considerations. |
| 1436 | if (D->hasAttr<InternalLinkageAttr>()) |
| 1437 | return getInternalLinkageFor(D); |
| Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1438 | |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1439 | if (computation.IgnoreAllVisibility && D->hasCachedLinkage()) |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1440 | return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1441 | |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 1442 | if (llvm::Optional<LinkageInfo> LI = lookup(D, computation)) |
| 1443 | return *LI; |
| 1444 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1445 | LinkageInfo LV = computeLVForDecl(D, computation); |
| 1446 | if (D->hasCachedLinkage()) |
| 1447 | assert(D->getCachedLinkage() == LV.getLinkage()); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1448 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1449 | D->setCachedLinkage(LV.getLinkage()); |
| George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 1450 | cache(D, computation, LV); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1451 | |
| 1452 | #ifndef NDEBUG |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1453 | // In C (because of gnu inline) and in c++ with microsoft extensions an |
| 1454 | // static can follow an extern, so we can have two decls with different |
| 1455 | // linkages. |
| 1456 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 1457 | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
| 1458 | return LV; |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1459 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1460 | // We have just computed the linkage for this decl. By induction we know |
| 1461 | // that all other computed linkages match, check that the one we just |
| 1462 | // computed also does. |
| 1463 | NamedDecl *Old = nullptr; |
| 1464 | for (auto I : D->redecls()) { |
| 1465 | auto *T = cast<NamedDecl>(I); |
| 1466 | if (T == D) |
| 1467 | continue; |
| 1468 | if (!T->isInvalidDecl() && T->hasCachedLinkage()) { |
| 1469 | Old = T; |
| 1470 | break; |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1471 | } |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1472 | } |
| 1473 | assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1474 | #endif |
| 1475 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1476 | return LV; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1477 | } |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1478 | |
| George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1479 | LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { |
| Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1480 | return getLVForDecl(D, |
| 1481 | LVComputationKind(usesTypeVisibility(D) |
| 1482 | ? NamedDecl::VisibilityForType |
| 1483 | : NamedDecl::VisibilityForValue)); |
| Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1486 | Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1487 | Module *M = getOwningModule(); |
| 1488 | if (!M) |
| 1489 | return nullptr; |
| 1490 | |
| 1491 | switch (M->Kind) { |
| 1492 | case Module::ModuleMapModule: |
| 1493 | // Module map modules have no special linkage semantics. |
| 1494 | return nullptr; |
| 1495 | |
| 1496 | case Module::ModuleInterfaceUnit: |
| 1497 | return M; |
| 1498 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1499 | case Module::GlobalModuleFragment: { |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1500 | // External linkage declarations in the global module have no owning module |
| 1501 | // for linkage purposes. But internal linkage declarations in the global |
| 1502 | // module fragment of a particular module are owned by that module for |
| 1503 | // linkage purposes. |
| Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1504 | if (IgnoreLinkage) |
| 1505 | return nullptr; |
| 1506 | bool InternalLinkage; |
| 1507 | if (auto *ND = dyn_cast<NamedDecl>(this)) |
| 1508 | InternalLinkage = !ND->hasExternalFormalLinkage(); |
| 1509 | else { |
| 1510 | auto *NSD = dyn_cast<NamespaceDecl>(this); |
| 1511 | InternalLinkage = (NSD && NSD->isAnonymousNamespace()) || |
| 1512 | isInAnonymousNamespace(); |
| 1513 | } |
| 1514 | return InternalLinkage ? M->Parent : nullptr; |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1515 | } |
| Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 1516 | |
| 1517 | case Module::PrivateModuleFragment: |
| 1518 | // The private module fragment is part of its containing module for linkage |
| 1519 | // purposes. |
| 1520 | return M->Parent; |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1521 | } |
| Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1522 | |
| 1523 | llvm_unreachable("unknown module kind"); |
| 1524 | } |
| 1525 | |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1526 | void NamedDecl::printName(raw_ostream &os) const { |
| 1527 | os << Name; |
| 1528 | } |
| 1529 | |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1530 | std::string NamedDecl::getQualifiedNameAsString() const { |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1531 | std::string QualName; |
| 1532 | llvm::raw_string_ostream OS(QualName); |
| Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 1533 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1534 | return OS.str(); |
| 1535 | } |
| 1536 | |
| 1537 | void NamedDecl::printQualifiedName(raw_ostream &OS) const { |
| 1538 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
| 1539 | } |
| 1540 | |
| 1541 | void NamedDecl::printQualifiedName(raw_ostream &OS, |
| 1542 | const PrintingPolicy &P) const { |
| Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 1543 | if (getDeclContext()->isFunctionOrMethod()) { |
| 1544 | // We do not print '(anonymous)' for function parameters without name. |
| 1545 | printName(OS); |
| 1546 | return; |
| 1547 | } |
| 1548 | printNestedNameSpecifier(OS, P); |
| Richard Smith | 9bd6b77 | 2020-04-14 23:35:35 -0700 | [diff] [blame] | 1549 | if (getDeclName()) |
| Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 1550 | OS << *this; |
| Richard Smith | 9bd6b77 | 2020-04-14 23:35:35 -0700 | [diff] [blame] | 1551 | else { |
| 1552 | // Give the printName override a chance to pick a different name before we |
| 1553 | // fall back to "(anonymous)". |
| 1554 | SmallString<64> NameBuffer; |
| 1555 | llvm::raw_svector_ostream NameOS(NameBuffer); |
| 1556 | printName(NameOS); |
| 1557 | if (NameBuffer.empty()) |
| 1558 | OS << "(anonymous)"; |
| 1559 | else |
| 1560 | OS << NameBuffer; |
| 1561 | } |
| Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | void NamedDecl::printNestedNameSpecifier(raw_ostream &OS) const { |
| 1565 | printNestedNameSpecifier(OS, getASTContext().getPrintingPolicy()); |
| 1566 | } |
| 1567 | |
| 1568 | void NamedDecl::printNestedNameSpecifier(raw_ostream &OS, |
| 1569 | const PrintingPolicy &P) const { |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1570 | const DeclContext *Ctx = getDeclContext(); |
| 1571 | |
| David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 1572 | // For ObjC methods and properties, look through categories and use the |
| 1573 | // interface as context. |
| Haojian Wu | 6f56599 | 2020-05-19 15:10:10 +0200 | [diff] [blame] | 1574 | if (auto *MD = dyn_cast<ObjCMethodDecl>(this)) { |
| Argyrios Kyrtzidis | a166a2b | 2017-03-07 09:26:07 +0000 | [diff] [blame] | 1575 | if (auto *ID = MD->getClassInterface()) |
| 1576 | Ctx = ID; |
| Haojian Wu | 6f56599 | 2020-05-19 15:10:10 +0200 | [diff] [blame] | 1577 | } else if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) { |
| David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 1578 | if (auto *MD = PD->getGetterMethodDecl()) |
| 1579 | if (auto *ID = MD->getClassInterface()) |
| 1580 | Ctx = ID; |
| Haojian Wu | 6f56599 | 2020-05-19 15:10:10 +0200 | [diff] [blame] | 1581 | } else if (auto *ID = dyn_cast<ObjCIvarDecl>(this)) { |
| 1582 | if (auto *CI = ID->getContainingInterface()) |
| 1583 | Ctx = CI; |
| David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 1584 | } |
| Argyrios Kyrtzidis | a166a2b | 2017-03-07 09:26:07 +0000 | [diff] [blame] | 1585 | |
| Ilya Biryukov | 6648223 | 2019-09-25 15:46:04 +0000 | [diff] [blame] | 1586 | if (Ctx->isFunctionOrMethod()) |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1587 | return; |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1588 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1589 | using ContextsTy = SmallVector<const DeclContext *, 8>; |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1590 | ContextsTy Contexts; |
| 1591 | |
| Sam McCall | 34f9d3f | 2018-02-02 13:34:47 +0000 | [diff] [blame] | 1592 | // Collect named contexts. |
| 1593 | while (Ctx) { |
| 1594 | if (isa<NamedDecl>(Ctx)) |
| 1595 | Contexts.push_back(Ctx); |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1596 | Ctx = Ctx->getParent(); |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1597 | } |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1598 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1599 | for (const DeclContext *DC : llvm::reverse(Contexts)) { |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1600 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
| Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 1601 | OS << Spec->getName(); |
| Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1602 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 1603 | printTemplateArgumentList(OS, TemplateArgs.asArray(), P); |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1604 | } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { |
| Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 1605 | if (P.SuppressUnwrittenScope && |
| 1606 | (ND->isAnonymousNamespace() || ND->isInline())) |
| 1607 | continue; |
| Reid Kleckner | 6010338 | 2015-12-16 02:04:40 +0000 | [diff] [blame] | 1608 | if (ND->isAnonymousNamespace()) { |
| 1609 | OS << (P.MSVCFormatting ? "`anonymous namespace\'" |
| 1610 | : "(anonymous namespace)"); |
| 1611 | } |
| Sam Weinig | 07d211e | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 1612 | else |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1613 | OS << *ND; |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1614 | } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) { |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1615 | if (!RD->getIdentifier()) |
| David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 1616 | OS << "(anonymous " << RD->getKindName() << ')'; |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1617 | else |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1618 | OS << *RD; |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1619 | } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1620 | const FunctionProtoType *FT = nullptr; |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1621 | if (FD->hasWrittenPrototype()) |
| Eli Friedman | 5c27c4c | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 1622 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1623 | |
| Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1624 | OS << *FD << '('; |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1625 | if (FT) { |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1626 | unsigned NumParams = FD->getNumParams(); |
| 1627 | for (unsigned i = 0; i < NumParams; ++i) { |
| 1628 | if (i) |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1629 | OS << ", "; |
| Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 1630 | OS << FD->getParamDecl(i)->getType().stream(P); |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | if (FT->isVariadic()) { |
| 1634 | if (NumParams > 0) |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1635 | OS << ", "; |
| 1636 | OS << "..."; |
| Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1637 | } |
| 1638 | } |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1639 | OS << ')'; |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1640 | } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) { |
| Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 1641 | // C++ [dcl.enum]p10: Each enum-name and each unscoped |
| 1642 | // enumerator is declared in the scope that immediately contains |
| 1643 | // the enum-specifier. Each scoped enumerator is declared in the |
| 1644 | // scope of the enumeration. |
| Paul Robinson | f183848 | 2017-12-21 21:47:22 +0000 | [diff] [blame] | 1645 | // For the case of unscoped enumerator, do not include in the qualified |
| Paul Robinson | d1c3dd8 | 2017-12-26 18:01:19 +0000 | [diff] [blame] | 1646 | // name any information about its enum enclosing scope, as its visibility |
| Paul Robinson | f183848 | 2017-12-21 21:47:22 +0000 | [diff] [blame] | 1647 | // is global. |
| 1648 | if (ED->isScoped()) |
| Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 1649 | OS << *ED; |
| 1650 | else |
| 1651 | continue; |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1652 | } else { |
| David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1653 | OS << *cast<NamedDecl>(DC); |
| Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1654 | } |
| 1655 | OS << "::"; |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1656 | } |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1657 | } |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1658 | |
| Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1659 | void NamedDecl::getNameForDiagnostic(raw_ostream &OS, |
| 1660 | const PrintingPolicy &Policy, |
| 1661 | bool Qualified) const { |
| 1662 | if (Qualified) |
| 1663 | printQualifiedName(OS, Policy); |
| 1664 | else |
| 1665 | printName(OS); |
| Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1668 | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { |
| 1669 | return true; |
| 1670 | } |
| 1671 | static bool isRedeclarableImpl(...) { return false; } |
| 1672 | static bool isRedeclarable(Decl::Kind K) { |
| 1673 | switch (K) { |
| 1674 | #define DECL(Type, Base) \ |
| 1675 | case Decl::Type: \ |
| 1676 | return isRedeclarableImpl((Type##Decl *)nullptr); |
| 1677 | #define ABSTRACT_DECL(DECL) |
| 1678 | #include "clang/AST/DeclNodes.inc" |
| 1679 | } |
| 1680 | llvm_unreachable("unknown decl kind"); |
| 1681 | } |
| 1682 | |
| 1683 | bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const { |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1684 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 1685 | |
| Richard Smith | fe620d2 | 2015-03-05 23:24:12 +0000 | [diff] [blame] | 1686 | // Never replace one imported declaration with another; we need both results |
| 1687 | // when re-exporting. |
| 1688 | if (OldD->isFromASTFile() && isFromASTFile()) |
| 1689 | return false; |
| 1690 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1691 | // A kind mismatch implies that the declaration is not replaced. |
| 1692 | if (OldD->getKind() != getKind()) |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1693 | return false; |
| 1694 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1695 | // For method declarations, we never replace. (Why?) |
| 1696 | if (isa<ObjCMethodDecl>(this)) |
| 1697 | return false; |
| 1698 | |
| 1699 | // For parameters, pick the newer one. This is either an error or (in |
| 1700 | // Objective-C) permitted as an extension. |
| 1701 | if (isa<ParmVarDecl>(this)) |
| 1702 | return true; |
| 1703 | |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1704 | // Inline namespaces can give us two declarations with the same |
| 1705 | // name and kind in the same scope but different contexts; we should |
| 1706 | // keep both declarations in this case. |
| 1707 | if (!this->getDeclContext()->getRedeclContext()->Equals( |
| 1708 | OldD->getDeclContext()->getRedeclContext())) |
| 1709 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1711 | // Using declarations can be replaced if they import the same name from the |
| 1712 | // same context. |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1713 | if (auto *UD = dyn_cast<UsingDecl>(this)) { |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1714 | ASTContext &Context = getASTContext(); |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1715 | return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1716 | Context.getCanonicalNestedNameSpecifier( |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1717 | cast<UsingDecl>(OldD)->getQualifier()); |
| Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1718 | } |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1719 | if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { |
| Eli Friedman | 0eaf10b | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1720 | ASTContext &Context = getASTContext(); |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1721 | return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == |
| Eli Friedman | 0eaf10b | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1722 | Context.getCanonicalNestedNameSpecifier( |
| 1723 | cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); |
| 1724 | } |
| 1725 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1726 | if (isRedeclarable(getKind())) { |
| 1727 | if (getCanonicalDecl() != OldD->getCanonicalDecl()) |
| 1728 | return false; |
| 1729 | |
| 1730 | if (IsKnownNewer) |
| 1731 | return true; |
| 1732 | |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1733 | // Check whether this is actually newer than OldD. We want to keep the |
| 1734 | // newer declaration. This loop will usually only iterate once, because |
| 1735 | // OldD is usually the previous declaration. |
| 1736 | for (auto D : redecls()) { |
| 1737 | if (D == OldD) |
| 1738 | break; |
| 1739 | |
| 1740 | // If we reach the canonical declaration, then OldD is not actually older |
| 1741 | // than this one. |
| 1742 | // |
| 1743 | // FIXME: In this case, we should not add this decl to the lookup table. |
| 1744 | if (D->isCanonicalDecl()) |
| 1745 | return false; |
| 1746 | } |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1747 | |
| 1748 | // It's a newer declaration of the same kind of declaration in the same |
| 1749 | // scope: we want this decl instead of the existing one. |
| 1750 | return true; |
| Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1753 | // In all other cases, we need to keep both declarations in case they have |
| 1754 | // different visibility. Any attempt to use the name will result in an |
| 1755 | // ambiguity if more than one is visible. |
| 1756 | return false; |
| Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1759 | bool NamedDecl::hasLinkage() const { |
| Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1760 | return getFormalLinkage() != NoLinkage; |
| Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1761 | } |
| Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1762 | |
| Daniel Dunbar | 166ea9ad | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 1763 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
| Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1764 | NamedDecl *ND = this; |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1765 | while (auto *UD = dyn_cast<UsingShadowDecl>(ND)) |
| Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1766 | ND = UD->getTargetDecl(); |
| 1767 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1768 | if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
| Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1769 | return AD->getClassInterface(); |
| 1770 | |
| Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1771 | if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND)) |
| 1772 | return AD->getNamespace(); |
| 1773 | |
| Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1774 | return ND; |
| Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1777 | bool NamedDecl::isCXXInstanceMember() const { |
| Douglas Gregor | 3f28ec2 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 1778 | if (!isCXXClassMember()) |
| 1779 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1780 | |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1781 | const NamedDecl *D = this; |
| 1782 | if (isa<UsingShadowDecl>(D)) |
| 1783 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1784 | |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1785 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1786 | return true; |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1787 | if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction())) |
| Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1788 | return MD->isInstance(); |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1789 | return false; |
| 1790 | } |
| 1791 | |
| Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1792 | //===----------------------------------------------------------------------===// |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1793 | // DeclaratorDecl Implementation |
| 1794 | //===----------------------------------------------------------------------===// |
| 1795 | |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1796 | template <typename DeclT> |
| 1797 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1798 | if (decl->getNumTemplateParameterLists() > 0) |
| 1799 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1800 | else |
| 1801 | return decl->getInnerLocStart(); |
| 1802 | } |
| 1803 | |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1804 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
| John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1805 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1806 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1807 | return SourceLocation(); |
| 1808 | } |
| 1809 | |
| Mark de Wever | f022a5a | 2020-01-01 17:23:19 +0100 | [diff] [blame] | 1810 | SourceLocation DeclaratorDecl::getTypeSpecEndLoc() const { |
| 1811 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1812 | if (TSI) return TSI->getTypeLoc().getEndLoc(); |
| 1813 | return SourceLocation(); |
| 1814 | } |
| 1815 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1816 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1817 | if (QualifierLoc) { |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1818 | // Make sure the extended decl info is allocated. |
| 1819 | if (!hasExtInfo()) { |
| 1820 | // Save (non-extended) type source info pointer. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1821 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1822 | // Allocate external info struct. |
| 1823 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1824 | // Restore savedTInfo into (extended) decl info. |
| 1825 | getExtInfo()->TInfo = savedTInfo; |
| 1826 | } |
| 1827 | // Set qualifier info. |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1828 | getExtInfo()->QualifierLoc = QualifierLoc; |
| Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 1829 | } else if (hasExtInfo()) { |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1830 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 1831 | getExtInfo()->QualifierLoc = QualifierLoc; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1832 | } |
| 1833 | } |
| 1834 | |
| Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 1835 | void DeclaratorDecl::setTrailingRequiresClause(Expr *TrailingRequiresClause) { |
| 1836 | assert(TrailingRequiresClause); |
| 1837 | // Make sure the extended decl info is allocated. |
| 1838 | if (!hasExtInfo()) { |
| 1839 | // Save (non-extended) type source info pointer. |
| 1840 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| 1841 | // Allocate external info struct. |
| 1842 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1843 | // Restore savedTInfo into (extended) decl info. |
| 1844 | getExtInfo()->TInfo = savedTInfo; |
| 1845 | } |
| 1846 | // Set requires clause info. |
| 1847 | getExtInfo()->TrailingRequiresClause = TrailingRequiresClause; |
| 1848 | } |
| 1849 | |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1850 | void DeclaratorDecl::setTemplateParameterListsInfo( |
| 1851 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 1852 | assert(!TPLists.empty()); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1853 | // Make sure the extended decl info is allocated. |
| 1854 | if (!hasExtInfo()) { |
| 1855 | // Save (non-extended) type source info pointer. |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1856 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1857 | // Allocate external info struct. |
| 1858 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1859 | // Restore savedTInfo into (extended) decl info. |
| 1860 | getExtInfo()->TInfo = savedTInfo; |
| 1861 | } |
| 1862 | // Set the template parameter lists info. |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1863 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1866 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1867 | return getTemplateOrInnerLocStart(this); |
| 1868 | } |
| 1869 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1870 | // Helper function: returns true if QT is or contains a type |
| 1871 | // having a postfix component. |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1872 | static bool typeIsPostfix(QualType QT) { |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1873 | while (true) { |
| 1874 | const Type* T = QT.getTypePtr(); |
| 1875 | switch (T->getTypeClass()) { |
| 1876 | default: |
| 1877 | return false; |
| 1878 | case Type::Pointer: |
| 1879 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1880 | break; |
| 1881 | case Type::BlockPointer: |
| 1882 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1883 | break; |
| 1884 | case Type::MemberPointer: |
| 1885 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1886 | break; |
| 1887 | case Type::LValueReference: |
| 1888 | case Type::RValueReference: |
| 1889 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1890 | break; |
| 1891 | case Type::PackExpansion: |
| 1892 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1893 | break; |
| 1894 | case Type::Paren: |
| 1895 | case Type::ConstantArray: |
| 1896 | case Type::DependentSizedArray: |
| 1897 | case Type::IncompleteArray: |
| 1898 | case Type::VariableArray: |
| 1899 | case Type::FunctionProto: |
| 1900 | case Type::FunctionNoProto: |
| 1901 | return true; |
| 1902 | } |
| 1903 | } |
| 1904 | } |
| 1905 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1906 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1907 | SourceLocation RangeEnd = getLocation(); |
| 1908 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| Benjamin Kramer | 3a7cc81 | 2014-02-02 15:28:46 +0000 | [diff] [blame] | 1909 | // If the declaration has no name or the type extends past the name take the |
| 1910 | // end location of the type. |
| 1911 | if (!getDeclName() || typeIsPostfix(TInfo->getType())) |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1912 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1913 | } |
| 1914 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1915 | } |
| 1916 | |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1917 | void QualifierInfo::setTemplateParameterListsInfo( |
| 1918 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1919 | // Free previous template parameters (if any). |
| 1920 | if (NumTemplParamLists > 0) { |
| Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1921 | Context.Deallocate(TemplParamLists); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1922 | TemplParamLists = nullptr; |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1923 | NumTemplParamLists = 0; |
| 1924 | } |
| 1925 | // Set info on matched template parameter lists (if any). |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1926 | if (!TPLists.empty()) { |
| 1927 | TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; |
| 1928 | NumTemplParamLists = TPLists.size(); |
| 1929 | std::copy(TPLists.begin(), TPLists.end(), TemplParamLists); |
| Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
| Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1933 | //===----------------------------------------------------------------------===// |
| Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1934 | // VarDecl Implementation |
| 1935 | //===----------------------------------------------------------------------===// |
| 1936 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1937 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1938 | switch (SC) { |
| Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1939 | case SC_None: break; |
| Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1940 | case SC_Auto: return "auto"; |
| 1941 | case SC_Extern: return "extern"; |
| Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1942 | case SC_PrivateExtern: return "__private_extern__"; |
| 1943 | case SC_Register: return "register"; |
| 1944 | case SC_Static: return "static"; |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1947 | llvm_unreachable("Invalid storage class"); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1950 | VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 1951 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 1952 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| 1953 | StorageClass SC) |
| 1954 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1955 | redeclarable_base(C) { |
| Benjamin Kramer | a939c23 | 2014-03-15 18:54:13 +0000 | [diff] [blame] | 1956 | static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), |
| 1957 | "VarDeclBitfields too large!"); |
| 1958 | static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), |
| 1959 | "ParmVarDeclBitfields too large!"); |
| David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1960 | static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), |
| 1961 | "NonParmVarDeclBitfields too large!"); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1962 | AllBits = 0; |
| 1963 | VarDeclBits.SClass = SC; |
| 1964 | // Everything else is implicitly initialized to false. |
| 1965 | } |
| 1966 | |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1967 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1968 | SourceLocation StartL, SourceLocation IdL, |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1969 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1970 | StorageClass S) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1971 | 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] | 1972 | } |
| 1973 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1974 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1975 | return new (C, ID) |
| 1976 | VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
| 1977 | QualType(), nullptr, SC_None); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1980 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1981 | assert(isLegalForVariable(SC)); |
| John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1982 | VarDeclBits.SClass = SC; |
| Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
| Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1985 | VarDecl::TLSKind VarDecl::getTLSKind() const { |
| 1986 | switch (VarDeclBits.TSCSpec) { |
| 1987 | case TSCS_unspecified: |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1988 | if (!hasAttr<ThreadAttr>() && |
| 1989 | !(getASTContext().getLangOpts().OpenMPUseTLS && |
| 1990 | getASTContext().getTargetInfo().isTLSSupported() && |
| 1991 | hasAttr<OMPThreadPrivateDeclAttr>())) |
| David Majnemer | 1b5baff | 2015-05-14 05:19:23 +0000 | [diff] [blame] | 1992 | return TLS_None; |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1993 | return ((getASTContext().getLangOpts().isCompatibleWithMSVC( |
| 1994 | LangOptions::MSVC2015)) || |
| 1995 | hasAttr<OMPThreadPrivateDeclAttr>()) |
| David Majnemer | 1b5baff | 2015-05-14 05:19:23 +0000 | [diff] [blame] | 1996 | ? TLS_Dynamic |
| 1997 | : TLS_Static; |
| Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1998 | case TSCS___thread: // Fall through. |
| 1999 | case TSCS__Thread_local: |
| Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 2000 | return TLS_Static; |
| Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 2001 | case TSCS_thread_local: |
| 2002 | return TLS_Dynamic; |
| 2003 | } |
| 2004 | llvm_unreachable("Unknown thread storage class specifier!"); |
| 2005 | } |
| 2006 | |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 2007 | SourceRange VarDecl::getSourceRange() const { |
| Argyrios Kyrtzidis | e0d6497 | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 2008 | if (const Expr *Init = getInit()) { |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2009 | SourceLocation InitEnd = Init->getEndLoc(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2010 | // If Init is implicit, ignore its source range and fallback on |
| Nico Weber | bbe1394 | 2013-01-22 17:00:09 +0000 | [diff] [blame] | 2011 | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
| 2012 | if (InitEnd.isValid() && InitEnd != getLocation()) |
| Argyrios Kyrtzidis | e0d6497 | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 2013 | return SourceRange(getOuterLocStart(), InitEnd); |
| 2014 | } |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2015 | return DeclaratorDecl::getSourceRange(); |
| Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
| Rafael Espindola | 8851067 | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 2018 | template<typename T> |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2019 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { |
| Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2020 | // C++ [dcl.link]p1: All function types, function names with external linkage, |
| 2021 | // and variable names with external linkage have a language linkage. |
| Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2022 | if (!D.hasExternalFormalLinkage()) |
| Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2023 | return NoLanguageLinkage; |
| 2024 | |
| 2025 | // 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] | 2026 | // C language linkage fits the implementation nicely. |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2027 | ASTContext &Context = D.getASTContext(); |
| 2028 | if (!Context.getLangOpts().CPlusPlus) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2029 | return CLanguageLinkage; |
| 2030 | |
| Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2031 | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
| 2032 | // language linkage of the names of class members and the function type of |
| 2033 | // class member functions. |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2034 | const DeclContext *DC = D.getDeclContext(); |
| 2035 | if (DC->isRecord()) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2036 | return CXXLanguageLinkage; |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2037 | |
| 2038 | // If the first decl is in an extern "C" context, any other redeclaration |
| 2039 | // will have C language linkage. If the first one is not in an extern "C" |
| 2040 | // 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] | 2041 | if (isFirstInExternCContext(&D)) |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2042 | return CLanguageLinkage; |
| 2043 | return CXXLanguageLinkage; |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2046 | template<typename T> |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2047 | static bool isDeclExternC(const T &D) { |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2048 | // Since the context is ignored for class members, they can only have C++ |
| 2049 | // language linkage or no language linkage. |
| 2050 | const DeclContext *DC = D.getDeclContext(); |
| 2051 | if (DC->isRecord()) { |
| 2052 | assert(D.getASTContext().getLangOpts().CPlusPlus); |
| 2053 | return false; |
| 2054 | } |
| 2055 | |
| 2056 | return D.getLanguageLinkage() == CLanguageLinkage; |
| 2057 | } |
| 2058 | |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2059 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2060 | return getDeclLanguageLinkage(*this); |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2063 | bool VarDecl::isExternC() const { |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2064 | return isDeclExternC(*this); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2067 | bool VarDecl::isInExternCContext() const { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2068 | return getLexicalDeclContext()->isExternCContext(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | bool VarDecl::isInExternCXXContext() const { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2072 | return getLexicalDeclContext()->isExternCXXContext(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2075 | VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2076 | |
| Nico Weber | 7f5015a | 2015-04-15 21:53:00 +0000 | [diff] [blame] | 2077 | VarDecl::DefinitionKind |
| 2078 | VarDecl::isThisDeclarationADefinition(ASTContext &C) const { |
| Richard Smith | a22d9a5 | 2017-12-02 00:55:48 +0000 | [diff] [blame] | 2079 | if (isThisDeclarationADemotedDefinition()) |
| 2080 | return DeclarationOnly; |
| 2081 | |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2082 | // C++ [basic.def]p2: |
| 2083 | // A declaration is a definition unless [...] it contains the 'extern' |
| 2084 | // specifier or a linkage-specification and neither an initializer [...], |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2085 | // it declares a non-inline static data member in a class declaration [...], |
| 2086 | // it declares a static data member outside a class definition and the variable |
| 2087 | // was defined within the class with the constexpr specifier [...], |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2088 | // C++1y [temp.expl.spec]p15: |
| 2089 | // An explicit specialization of a static data member or an explicit |
| 2090 | // specialization of a static data member template is a definition if the |
| 2091 | // declaration includes an initializer; otherwise, it is a declaration. |
| 2092 | // |
| 2093 | // FIXME: How do you declare (but not define) a partial specialization of |
| 2094 | // a static data member template outside the containing class? |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2095 | if (isStaticDataMember()) { |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2096 | if (isOutOfLine() && |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2097 | !(getCanonicalDecl()->isInline() && |
| 2098 | getCanonicalDecl()->isConstexpr()) && |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2099 | (hasInit() || |
| 2100 | // If the first declaration is out-of-line, this may be an |
| 2101 | // instantiation of an out-of-line partial specialization of a variable |
| 2102 | // template for which we have not yet instantiated the initializer. |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2103 | (getFirstDecl()->isOutOfLine() |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2104 | ? getTemplateSpecializationKind() == TSK_Undeclared |
| 2105 | : getTemplateSpecializationKind() != |
| 2106 | TSK_ExplicitSpecialization) || |
| 2107 | isa<VarTemplatePartialSpecializationDecl>(this))) |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2108 | return Definition; |
| Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2109 | else if (!isOutOfLine() && isInline()) |
| 2110 | return Definition; |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2111 | else |
| 2112 | return DeclarationOnly; |
| 2113 | } |
| 2114 | // C99 6.7p5: |
| 2115 | // A definition of an identifier is a declaration for that identifier that |
| 2116 | // [...] causes storage to be reserved for that object. |
| 2117 | // Note: that applies for all non-file-scope objects. |
| 2118 | // C99 6.9.2p1: |
| 2119 | // If the declaration of an identifier for an object has file scope and an |
| 2120 | // initializer, the declaration is an external definition for the identifier |
| 2121 | if (hasInit()) |
| 2122 | return Definition; |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2123 | |
| Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 2124 | if (hasDefiningAttr()) |
| Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2125 | return Definition; |
| 2126 | |
| David Majnemer | dd0bed1 | 2015-05-14 05:19:20 +0000 | [diff] [blame] | 2127 | if (const auto *SAA = getAttr<SelectAnyAttr>()) |
| 2128 | if (!SAA->isInherited()) |
| 2129 | return Definition; |
| 2130 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2131 | // A variable template specialization (other than a static data member |
| 2132 | // template or an explicit specialization) is a declaration until we |
| 2133 | // instantiate its initializer. |
| Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 2134 | if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2135 | if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && |
| 2136 | !isa<VarTemplatePartialSpecializationDecl>(VTSD) && |
| 2137 | !VTSD->IsCompleteDefinition) |
| 2138 | return DeclarationOnly; |
| 2139 | } |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2140 | |
| Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 2141 | if (hasExternalStorage()) |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2142 | return DeclarationOnly; |
| Rafael Espindola | 8f326a5 | 2013-03-07 01:42:44 +0000 | [diff] [blame] | 2143 | |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2144 | // [dcl.link] p7: |
| 2145 | // A declaration directly contained in a linkage-specification is treated |
| 2146 | // as if it contains the extern specifier for the purpose of determining |
| 2147 | // the linkage of the declared name and whether it is a definition. |
| Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 2148 | if (isSingleLineLanguageLinkage(*this)) |
| Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 2149 | return DeclarationOnly; |
| Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2150 | |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2151 | // C99 6.9.2p2: |
| 2152 | // A declaration of an object that has file scope without an initializer, |
| 2153 | // and without a storage class specifier or the scs 'static', constitutes |
| 2154 | // a tentative definition. |
| 2155 | // No such thing in C++. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2156 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2157 | return TentativeDefinition; |
| 2158 | |
| 2159 | // What's left is (in C, block-scope) declarations without initializers or |
| 2160 | // external storage. These are definitions. |
| 2161 | return Definition; |
| 2162 | } |
| 2163 | |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2164 | VarDecl *VarDecl::getActingDefinition() { |
| 2165 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 2166 | if (Kind != TentativeDefinition) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2167 | return nullptr; |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2168 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2169 | VarDecl *LastTentative = nullptr; |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2170 | VarDecl *First = getFirstDecl(); |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2171 | for (auto I : First->redecls()) { |
| 2172 | Kind = I->isThisDeclarationADefinition(); |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2173 | if (Kind == Definition) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2174 | return nullptr; |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2175 | else if (Kind == TentativeDefinition) |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2176 | LastTentative = I; |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2177 | } |
| 2178 | return LastTentative; |
| 2179 | } |
| 2180 | |
| Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2181 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2182 | VarDecl *First = getFirstDecl(); |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2183 | for (auto I : First->redecls()) { |
| 2184 | if (I->isThisDeclarationADefinition(C) == Definition) |
| 2185 | return I; |
| Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2186 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2187 | return nullptr; |
| Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2190 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 2191 | DefinitionKind Kind = DeclarationOnly; |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2192 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2193 | const VarDecl *First = getFirstDecl(); |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2194 | for (auto I : First->redecls()) { |
| 2195 | Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); |
| Daniel Dunbar | 082c62d | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 2196 | if (Kind == Definition) |
| 2197 | break; |
| 2198 | } |
| John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 2199 | |
| 2200 | return Kind; |
| 2201 | } |
| 2202 | |
| Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2203 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2204 | for (auto I : redecls()) { |
| 2205 | if (auto Expr = I->getInit()) { |
| 2206 | D = I; |
| 2207 | return Expr; |
| 2208 | } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2209 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2210 | return nullptr; |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2213 | bool VarDecl::hasInit() const { |
| 2214 | if (auto *P = dyn_cast<ParmVarDecl>(this)) |
| 2215 | if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg()) |
| 2216 | return false; |
| 2217 | |
| 2218 | return !Init.isNull(); |
| 2219 | } |
| 2220 | |
| 2221 | Expr *VarDecl::getInit() { |
| 2222 | if (!hasInit()) |
| 2223 | return nullptr; |
| 2224 | |
| 2225 | if (auto *S = Init.dyn_cast<Stmt *>()) |
| 2226 | return cast<Expr>(S); |
| 2227 | |
| 2228 | return cast_or_null<Expr>(Init.get<EvaluatedStmt *>()->Value); |
| 2229 | } |
| 2230 | |
| 2231 | Stmt **VarDecl::getInitAddress() { |
| 2232 | if (auto *ES = Init.dyn_cast<EvaluatedStmt *>()) |
| 2233 | return &ES->Value; |
| 2234 | |
| 2235 | return Init.getAddrOfPtr1(); |
| 2236 | } |
| 2237 | |
| Richard Smith | a6e8b68 | 2019-09-04 20:30:37 +0000 | [diff] [blame] | 2238 | VarDecl *VarDecl::getInitializingDeclaration() { |
| 2239 | VarDecl *Def = nullptr; |
| 2240 | for (auto I : redecls()) { |
| 2241 | if (I->hasInit()) |
| 2242 | return I; |
| 2243 | |
| 2244 | if (I->isThisDeclarationADefinition()) { |
| 2245 | if (isStaticDataMember()) |
| 2246 | return I; |
| 2247 | else |
| 2248 | Def = I; |
| 2249 | } |
| 2250 | } |
| 2251 | return Def; |
| 2252 | } |
| 2253 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2254 | bool VarDecl::isOutOfLine() const { |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2255 | if (Decl::isOutOfLine()) |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2256 | return true; |
| Chandler Carruth | f50ef6e | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 2257 | |
| 2258 | if (!isStaticDataMember()) |
| 2259 | return false; |
| 2260 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2261 | // 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] | 2262 | // a class template, check whether that static data member was defined |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2263 | // out-of-line. |
| 2264 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 2265 | return VD->isOutOfLine(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2266 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2267 | return false; |
| 2268 | } |
| 2269 | |
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2270 | void VarDecl::setInit(Expr *I) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2271 | if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2272 | Eval->~EvaluatedStmt(); |
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2273 | getASTContext().Deallocate(Eval); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | Init = I; |
| 2277 | } |
| 2278 | |
| Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 2279 | bool VarDecl::mightBeUsableInConstantExpressions(ASTContext &C) const { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2280 | const LangOptions &Lang = C.getLangOpts(); |
| Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2281 | |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2282 | if (!Lang.CPlusPlus) |
| 2283 | return false; |
| 2284 | |
| Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 2285 | // Function parameters are never usable in constant expressions. |
| 2286 | if (isa<ParmVarDecl>(this)) |
| 2287 | return false; |
| 2288 | |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2289 | // In C++11, any variable of reference type can be used in a constant |
| 2290 | // expression if it is initialized by a constant expression. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2291 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2292 | return true; |
| 2293 | |
| 2294 | // 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] | 2295 | // not require the variable to be non-volatile, but we consider this to be a |
| 2296 | // defect. |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2297 | if (!getType().isConstQualified() || getType().isVolatileQualified()) |
| Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2298 | return false; |
| 2299 | |
| 2300 | // In C++, const, non-volatile variables of integral or enumeration types |
| 2301 | // can be used in constant expressions. |
| 2302 | if (getType()->isIntegralOrEnumerationType()) |
| 2303 | return true; |
| 2304 | |
| Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2305 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 2306 | // constant expressions. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2307 | return Lang.CPlusPlus11 && isConstexpr(); |
| Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
| Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 2310 | bool VarDecl::isUsableInConstantExpressions(ASTContext &Context) const { |
| 2311 | // C++2a [expr.const]p3: |
| 2312 | // A variable is usable in constant expressions after its initializing |
| 2313 | // declaration is encountered... |
| 2314 | const VarDecl *DefVD = nullptr; |
| 2315 | const Expr *Init = getAnyInitializer(DefVD); |
| Richard Smith | 94bc88e | 2019-06-17 21:46:17 +0000 | [diff] [blame] | 2316 | if (!Init || Init->isValueDependent() || getType()->isDependentType()) |
| Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 2317 | return false; |
| 2318 | // ... if it is a constexpr variable, or it is of reference type or of |
| 2319 | // const-qualified integral or enumeration type, ... |
| 2320 | if (!DefVD->mightBeUsableInConstantExpressions(Context)) |
| 2321 | return false; |
| 2322 | // ... and its initializer is a constant initializer. |
| 2323 | return DefVD->checkInitIsICE(); |
| 2324 | } |
| 2325 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2326 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 2327 | /// form, which contains extra information on the evaluated value of the |
| 2328 | /// initializer. |
| 2329 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2330 | auto *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2331 | if (!Eval) { |
| Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2332 | // Note: EvaluatedStmt contains an APValue, which usually holds |
| 2333 | // resources not allocated from the ASTContext. We need to do some |
| 2334 | // work to avoid leaking those, but we do so in VarDecl::evaluateValue |
| 2335 | // 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] | 2336 | Eval = new (getASTContext()) EvaluatedStmt; |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2337 | Eval->Value = Init.get<Stmt *>(); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2338 | Init = Eval; |
| 2339 | } |
| 2340 | return Eval; |
| 2341 | } |
| 2342 | |
| Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2343 | APValue *VarDecl::evaluateValue() const { |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2344 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2345 | return evaluateValue(Notes); |
| 2346 | } |
| 2347 | |
| 2348 | APValue *VarDecl::evaluateValue( |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2349 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2350 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2351 | |
| 2352 | // We only produce notes indicating why an initializer is non-constant the |
| 2353 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 2354 | // first time we try evaluation, so might not be produced at all. |
| 2355 | if (Eval->WasEvaluated) |
| Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 2356 | return Eval->Evaluated.isAbsent() ? nullptr : &Eval->Evaluated; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2357 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2358 | const auto *Init = cast<Expr>(Eval->Value); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2359 | assert(!Init->isValueDependent()); |
| 2360 | |
| 2361 | if (Eval->IsEvaluating) { |
| 2362 | // FIXME: Produce a diagnostic for self-initialization. |
| 2363 | Eval->CheckedICE = true; |
| 2364 | Eval->IsICE = false; |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2365 | return nullptr; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
| 2368 | Eval->IsEvaluating = true; |
| 2369 | |
| 2370 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), |
| 2371 | this, Notes); |
| 2372 | |
| Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2373 | // Ensure the computed APValue is cleaned up later if evaluation succeeded, |
| 2374 | // or that it's empty (so that there's nothing to clean up) if evaluation |
| 2375 | // failed. |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2376 | if (!Result) |
| 2377 | Eval->Evaluated = APValue(); |
| Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2378 | else if (Eval->Evaluated.needsCleanup()) |
| George Burgess IV | b61bfbd | 2017-02-14 05:37:36 +0000 | [diff] [blame] | 2379 | getASTContext().addDestruction(&Eval->Evaluated); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2380 | |
| 2381 | Eval->IsEvaluating = false; |
| 2382 | Eval->WasEvaluated = true; |
| 2383 | |
| 2384 | // In C++11, we have determined whether the initializer was a constant |
| 2385 | // expression as a side-effect. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2386 | if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) { |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2387 | Eval->CheckedICE = true; |
| Eli Friedman | 8f66cdf | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 2388 | Eval->IsICE = Result && Notes.empty(); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2391 | return Result ? &Eval->Evaluated : nullptr; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2394 | APValue *VarDecl::getEvaluatedValue() const { |
| 2395 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) |
| 2396 | if (Eval->WasEvaluated) |
| 2397 | return &Eval->Evaluated; |
| 2398 | |
| 2399 | return nullptr; |
| 2400 | } |
| 2401 | |
| 2402 | bool VarDecl::isInitKnownICE() const { |
| 2403 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) |
| 2404 | return Eval->CheckedICE; |
| 2405 | |
| 2406 | return false; |
| 2407 | } |
| 2408 | |
| 2409 | bool VarDecl::isInitICE() const { |
| 2410 | assert(isInitKnownICE() && |
| 2411 | "Check whether we already know that the initializer is an ICE"); |
| 2412 | return Init.get<EvaluatedStmt *>()->IsICE; |
| 2413 | } |
| 2414 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2415 | bool VarDecl::checkInitIsICE() const { |
| John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2416 | // Initializers of weak variables are never ICEs. |
| 2417 | if (isWeak()) |
| 2418 | return false; |
| 2419 | |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2420 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2421 | if (Eval->CheckedICE) |
| 2422 | // We have already checked whether this subexpression is an |
| 2423 | // integral constant expression. |
| 2424 | return Eval->IsICE; |
| 2425 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2426 | const auto *Init = cast<Expr>(Eval->Value); |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2427 | assert(!Init->isValueDependent()); |
| 2428 | |
| 2429 | // In C++11, evaluate the initializer to check whether it's a constant |
| 2430 | // expression. |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2431 | if (getASTContext().getLangOpts().CPlusPlus11) { |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2432 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2433 | evaluateValue(Notes); |
| 2434 | return Eval->IsICE; |
| 2435 | } |
| 2436 | |
| 2437 | // It's an ICE whether or not the definition we found is |
| 2438 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 2439 | // 6206 for details. |
| 2440 | |
| 2441 | if (Eval->CheckingICE) |
| 2442 | return false; |
| 2443 | Eval->CheckingICE = true; |
| 2444 | |
| 2445 | Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); |
| 2446 | Eval->CheckingICE = false; |
| 2447 | Eval->CheckedICE = true; |
| 2448 | return Eval->IsICE; |
| 2449 | } |
| 2450 | |
| Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 2451 | bool VarDecl::isParameterPack() const { |
| 2452 | return isa<PackExpansionType>(getType()); |
| 2453 | } |
| 2454 | |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2455 | template<typename DeclT> |
| 2456 | static DeclT *getDefinitionOrSelf(DeclT *D) { |
| 2457 | assert(D); |
| 2458 | if (auto *Def = D->getDefinition()) |
| 2459 | return Def; |
| 2460 | return D; |
| 2461 | } |
| 2462 | |
| Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 2463 | bool VarDecl::isEscapingByref() const { |
| 2464 | return hasAttr<BlocksAttr>() && NonParmVarDeclBits.EscapingByref; |
| 2465 | } |
| 2466 | |
| 2467 | bool VarDecl::isNonEscapingByref() const { |
| 2468 | return hasAttr<BlocksAttr>() && !NonParmVarDeclBits.EscapingByref; |
| 2469 | } |
| 2470 | |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2471 | VarDecl *VarDecl::getTemplateInstantiationPattern() const { |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2472 | const VarDecl *VD = this; |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2473 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2474 | // If this is an instantiated member, walk back to the template from which |
| 2475 | // it was instantiated. |
| 2476 | if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) { |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2477 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2478 | VD = VD->getInstantiatedFromStaticDataMember(); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2479 | while (auto *NewVD = VD->getInstantiatedFromStaticDataMember()) |
| 2480 | VD = NewVD; |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2484 | // If it's an instantiated variable template specialization, find the |
| 2485 | // template or partial specialization from which it was instantiated. |
| 2486 | if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
| 2487 | if (isTemplateInstantiation(VDTemplSpec->getTemplateSpecializationKind())) { |
| 2488 | auto From = VDTemplSpec->getInstantiatedFrom(); |
| 2489 | if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) { |
| 2490 | while (!VTD->isMemberSpecialization()) { |
| 2491 | auto *NewVTD = VTD->getInstantiatedFromMemberTemplate(); |
| 2492 | if (!NewVTD) |
| 2493 | break; |
| 2494 | VTD = NewVTD; |
| 2495 | } |
| 2496 | return getDefinitionOrSelf(VTD->getTemplatedDecl()); |
| 2497 | } |
| 2498 | if (auto *VTPSD = |
| 2499 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 2500 | while (!VTPSD->isMemberSpecialization()) { |
| 2501 | auto *NewVTPSD = VTPSD->getInstantiatedFromMember(); |
| 2502 | if (!NewVTPSD) |
| 2503 | break; |
| 2504 | VTPSD = NewVTPSD; |
| 2505 | } |
| 2506 | return getDefinitionOrSelf<VarDecl>(VTPSD); |
| 2507 | } |
| 2508 | } |
| 2509 | } |
| 2510 | |
| 2511 | // If this is the pattern of a variable template, find where it was |
| 2512 | // instantiated from. FIXME: Is this necessary? |
| 2513 | if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) { |
| 2514 | while (!VarTemplate->isMemberSpecialization()) { |
| 2515 | auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate(); |
| 2516 | if (!NewVT) |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2517 | break; |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2518 | VarTemplate = NewVT; |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2521 | return getDefinitionOrSelf(VarTemplate->getTemplatedDecl()); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2522 | } |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2523 | |
| 2524 | if (VD == this) |
| 2525 | return nullptr; |
| 2526 | return getDefinitionOrSelf(const_cast<VarDecl*>(VD)); |
| Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2529 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2530 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2531 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2532 | |
| 2533 | return nullptr; |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 2536 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2537 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2538 | return Spec->getSpecializationKind(); |
| 2539 | |
| Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2540 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2541 | return MSI->getTemplateSpecializationKind(); |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2542 | |
| Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2543 | return TSK_Undeclared; |
| 2544 | } |
| 2545 | |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2546 | TemplateSpecializationKind |
| 2547 | VarDecl::getTemplateSpecializationKindForInstantiation() const { |
| 2548 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2549 | return MSI->getTemplateSpecializationKind(); |
| 2550 | |
| 2551 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
| 2552 | return Spec->getSpecializationKind(); |
| 2553 | |
| 2554 | return TSK_Undeclared; |
| 2555 | } |
| 2556 | |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2557 | SourceLocation VarDecl::getPointOfInstantiation() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2558 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2559 | return Spec->getPointOfInstantiation(); |
| 2560 | |
| 2561 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2562 | return MSI->getPointOfInstantiation(); |
| 2563 | |
| 2564 | return SourceLocation(); |
| 2565 | } |
| 2566 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2567 | VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { |
| 2568 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2569 | .dyn_cast<VarTemplateDecl *>(); |
| 2570 | } |
| 2571 | |
| 2572 | void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { |
| 2573 | getASTContext().setTemplateOrSpecializationInfo(this, Template); |
| 2574 | } |
| 2575 | |
| Justin Lebar | 5489f85 | 2018-05-17 16:15:07 +0000 | [diff] [blame] | 2576 | bool VarDecl::isKnownToBeDefined() const { |
| 2577 | const auto &LangOpts = getASTContext().getLangOpts(); |
| 2578 | // In CUDA mode without relocatable device code, variables of form 'extern |
| 2579 | // __shared__ Foo foo[]' are pointers to the base of the GPU core's shared |
| 2580 | // memory pool. These are never undefined variables, even if they appear |
| 2581 | // inside of an anon namespace or static function. |
| 2582 | // |
| 2583 | // With CUDA relocatable device code enabled, these variables don't get |
| 2584 | // special handling; they're treated like regular extern variables. |
| Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2585 | if (LangOpts.CUDA && !LangOpts.GPURelocatableDeviceCode && |
| Justin Lebar | 5489f85 | 2018-05-17 16:15:07 +0000 | [diff] [blame] | 2586 | hasExternalStorage() && hasAttr<CUDASharedAttr>() && |
| 2587 | isa<IncompleteArrayType>(getType())) |
| 2588 | return true; |
| 2589 | |
| 2590 | return hasDefinition(); |
| 2591 | } |
| 2592 | |
| Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 2593 | bool VarDecl::isNoDestroy(const ASTContext &Ctx) const { |
| 2594 | return hasGlobalStorage() && (hasAttr<NoDestroyAttr>() || |
| 2595 | (!Ctx.getLangOpts().RegisterStaticDestructors && |
| 2596 | !hasAttr<AlwaysDestroyAttr>())); |
| 2597 | } |
| 2598 | |
| Richard Smith | 2b4fa53 | 2019-09-29 05:08:46 +0000 | [diff] [blame] | 2599 | QualType::DestructionKind |
| 2600 | VarDecl::needsDestruction(const ASTContext &Ctx) const { |
| 2601 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) |
| 2602 | if (Eval->HasConstantDestruction) |
| 2603 | return QualType::DK_none; |
| 2604 | |
| 2605 | if (isNoDestroy(Ctx)) |
| 2606 | return QualType::DK_none; |
| 2607 | |
| 2608 | return getType().isDestructedType(); |
| 2609 | } |
| 2610 | |
| Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2611 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
| Richard Smith | b71782b | 2013-08-01 04:12:04 +0000 | [diff] [blame] | 2612 | if (isStaticDataMember()) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2613 | // FIXME: Remove ? |
| 2614 | // return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 2615 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2616 | .dyn_cast<MemberSpecializationInfo *>(); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2617 | return nullptr; |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2620 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2621 | SourceLocation PointOfInstantiation) { |
| Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2622 | assert((isa<VarTemplateSpecializationDecl>(this) || |
| 2623 | getMemberSpecializationInfo()) && |
| 2624 | "not a variable or static data member template specialization"); |
| 2625 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2626 | if (VarTemplateSpecializationDecl *Spec = |
| 2627 | dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2628 | Spec->setSpecializationKind(TSK); |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2629 | if (TSK != TSK_ExplicitSpecialization && |
| 2630 | PointOfInstantiation.isValid() && |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2631 | Spec->getPointOfInstantiation().isInvalid()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2632 | Spec->setPointOfInstantiation(PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2633 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 2634 | L->InstantiationRequested(this); |
| 2635 | } |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2636 | } else if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2637 | MSI->setTemplateSpecializationKind(TSK); |
| 2638 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2639 | MSI->getPointOfInstantiation().isInvalid()) { |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2640 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2641 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 2642 | L->InstantiationRequested(this); |
| 2643 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2644 | } |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | void |
| 2648 | VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, |
| 2649 | TemplateSpecializationKind TSK) { |
| 2650 | assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && |
| 2651 | "Previous template or instantiation?"); |
| 2652 | getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2655 | //===----------------------------------------------------------------------===// |
| 2656 | // ParmVarDecl Implementation |
| 2657 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2658 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2659 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2660 | SourceLocation StartLoc, |
| 2661 | SourceLocation IdLoc, IdentifierInfo *Id, |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2662 | QualType T, TypeSourceInfo *TInfo, |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2663 | StorageClass S, Expr *DefArg) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2664 | 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] | 2665 | S, DefArg); |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
| Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2668 | QualType ParmVarDecl::getOriginalType() const { |
| 2669 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 2670 | QualType T = TSI ? TSI->getType() : getType(); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2671 | if (const auto *DT = dyn_cast<DecayedType>(T)) |
| Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2672 | return DT->getOriginalType(); |
| 2673 | return T; |
| 2674 | } |
| 2675 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2676 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2677 | return new (C, ID) |
| 2678 | ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), |
| 2679 | nullptr, QualType(), nullptr, SC_None, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
| Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2682 | SourceRange ParmVarDecl::getSourceRange() const { |
| 2683 | if (!hasInheritedDefaultArg()) { |
| 2684 | SourceRange ArgRange = getDefaultArgRange(); |
| 2685 | if (ArgRange.isValid()) |
| 2686 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 2687 | } |
| 2688 | |
| Argyrios Kyrtzidis | a077279 | 2013-04-17 01:56:48 +0000 | [diff] [blame] | 2689 | // DeclaratorDecl considers the range of postfix types as overlapping with the |
| 2690 | // declaration name, but this is not the case with parameters in ObjC methods. |
| 2691 | if (isa<ObjCMethodDecl>(getDeclContext())) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2692 | return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation()); |
| Argyrios Kyrtzidis | a077279 | 2013-04-17 01:56:48 +0000 | [diff] [blame] | 2693 | |
| Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2694 | return DeclaratorDecl::getSourceRange(); |
| 2695 | } |
| 2696 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2697 | Expr *ParmVarDecl::getDefaultArg() { |
| 2698 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 2699 | assert(!hasUninstantiatedDefaultArg() && |
| 2700 | "Default argument is not yet instantiated!"); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2701 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2702 | Expr *Arg = getInit(); |
| Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 2703 | if (auto *E = dyn_cast_or_null<FullExpr>(Arg)) |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2704 | return E->getSubExpr(); |
| Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2705 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2706 | return Arg; |
| 2707 | } |
| 2708 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2709 | void ParmVarDecl::setDefaultArg(Expr *defarg) { |
| 2710 | ParmVarDeclBits.DefaultArgKind = DAK_Normal; |
| 2711 | Init = defarg; |
| 2712 | } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2713 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2714 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 2715 | switch (ParmVarDeclBits.DefaultArgKind) { |
| 2716 | case DAK_None: |
| 2717 | case DAK_Unparsed: |
| 2718 | // Nothing we can do here. |
| 2719 | return SourceRange(); |
| 2720 | |
| 2721 | case DAK_Uninstantiated: |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2722 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 2723 | |
| Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2724 | case DAK_Normal: |
| 2725 | if (const Expr *E = getInit()) |
| 2726 | return E->getSourceRange(); |
| 2727 | |
| 2728 | // Missing an actual expression, may be invalid. |
| 2729 | return SourceRange(); |
| 2730 | } |
| 2731 | llvm_unreachable("Invalid default argument kind."); |
| 2732 | } |
| 2733 | |
| 2734 | void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) { |
| 2735 | ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated; |
| 2736 | Init = arg; |
| 2737 | } |
| 2738 | |
| 2739 | Expr *ParmVarDecl::getUninstantiatedDefaultArg() { |
| 2740 | assert(hasUninstantiatedDefaultArg() && |
| 2741 | "Wrong kind of initialization expression!"); |
| 2742 | return cast_or_null<Expr>(Init.get<Stmt *>()); |
| 2743 | } |
| 2744 | |
| 2745 | bool ParmVarDecl::hasDefaultArg() const { |
| 2746 | // FIXME: We should just return false for DAK_None here once callers are |
| 2747 | // prepared for the case that we encountered an invalid default argument and |
| 2748 | // were unable to even build an invalid expression. |
| 2749 | return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() || |
| 2750 | !Init.isNull(); |
| Argyrios Kyrtzidis | 02dd4f9 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
| Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 2753 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 2754 | getASTContext().setParameterIndex(this, parameterIndex); |
| 2755 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 2756 | } |
| 2757 | |
| 2758 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 2759 | return getASTContext().getParameterIndex(this); |
| 2760 | } |
| 2761 | |
| Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 2762 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2763 | // FunctionDecl Implementation |
| 2764 | //===----------------------------------------------------------------------===// |
| 2765 | |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2766 | FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 2767 | SourceLocation StartLoc, |
| 2768 | const DeclarationNameInfo &NameInfo, QualType T, |
| 2769 | TypeSourceInfo *TInfo, StorageClass S, |
| Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2770 | bool isInlineSpecified, |
| Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2771 | ConstexprSpecKind ConstexprKind, |
| 2772 | Expr *TrailingRequiresClause) |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2773 | : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, |
| 2774 | StartLoc), |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2775 | DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0), |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2776 | EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) { |
| Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 2777 | assert(T.isNull() || T->isFunctionType()); |
| Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2778 | FunctionDeclBits.SClass = S; |
| 2779 | FunctionDeclBits.IsInline = isInlineSpecified; |
| 2780 | FunctionDeclBits.IsInlineSpecified = isInlineSpecified; |
| Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2781 | FunctionDeclBits.IsVirtualAsWritten = false; |
| 2782 | FunctionDeclBits.IsPure = false; |
| 2783 | FunctionDeclBits.HasInheritedPrototype = false; |
| 2784 | FunctionDeclBits.HasWrittenPrototype = true; |
| 2785 | FunctionDeclBits.IsDeleted = false; |
| 2786 | FunctionDeclBits.IsTrivial = false; |
| 2787 | FunctionDeclBits.IsTrivialForCall = false; |
| 2788 | FunctionDeclBits.IsDefaulted = false; |
| 2789 | FunctionDeclBits.IsExplicitlyDefaulted = false; |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2790 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
| Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2791 | FunctionDeclBits.HasImplicitReturnZero = false; |
| 2792 | FunctionDeclBits.IsLateTemplateParsed = false; |
| Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 2793 | FunctionDeclBits.ConstexprKind = ConstexprKind; |
| Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2794 | FunctionDeclBits.InstantiationIsPending = false; |
| 2795 | FunctionDeclBits.UsesSEHTry = false; |
| Melanie Blower | 7f9b513 | 2019-12-04 12:23:46 -0800 | [diff] [blame] | 2796 | FunctionDeclBits.UsesFPIntrin = false; |
| Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2797 | FunctionDeclBits.HasSkippedBody = false; |
| 2798 | FunctionDeclBits.WillHaveBody = false; |
| 2799 | FunctionDeclBits.IsMultiVersion = false; |
| 2800 | FunctionDeclBits.IsCopyDeductionCandidate = false; |
| 2801 | FunctionDeclBits.HasODRHash = false; |
| Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 2802 | if (TrailingRequiresClause) |
| 2803 | setTrailingRequiresClause(TrailingRequiresClause); |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2804 | } |
| 2805 | |
| Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 2806 | void FunctionDecl::getNameForDiagnostic( |
| 2807 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 2808 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2809 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 2810 | if (TemplateArgs) |
| Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 2811 | printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy); |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
| Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2814 | bool FunctionDecl::isVariadic() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2815 | if (const auto *FT = getType()->getAs<FunctionProtoType>()) |
| Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2816 | return FT->isVariadic(); |
| 2817 | return false; |
| 2818 | } |
| 2819 | |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2820 | FunctionDecl::DefaultedFunctionInfo * |
| 2821 | FunctionDecl::DefaultedFunctionInfo::Create(ASTContext &Context, |
| 2822 | ArrayRef<DeclAccessPair> Lookups) { |
| 2823 | DefaultedFunctionInfo *Info = new (Context.Allocate( |
| 2824 | totalSizeToAlloc<DeclAccessPair>(Lookups.size()), |
| 2825 | std::max(alignof(DefaultedFunctionInfo), alignof(DeclAccessPair)))) |
| 2826 | DefaultedFunctionInfo; |
| 2827 | Info->NumLookups = Lookups.size(); |
| 2828 | std::uninitialized_copy(Lookups.begin(), Lookups.end(), |
| 2829 | Info->getTrailingObjects<DeclAccessPair>()); |
| 2830 | return Info; |
| 2831 | } |
| 2832 | |
| 2833 | void FunctionDecl::setDefaultedFunctionInfo(DefaultedFunctionInfo *Info) { |
| 2834 | assert(!FunctionDeclBits.HasDefaultedFunctionInfo && "already have this"); |
| 2835 | assert(!Body && "can't replace function body with defaulted function info"); |
| 2836 | |
| 2837 | FunctionDeclBits.HasDefaultedFunctionInfo = true; |
| 2838 | DefaultedInfo = Info; |
| 2839 | } |
| 2840 | |
| 2841 | FunctionDecl::DefaultedFunctionInfo * |
| 2842 | FunctionDecl::getDefaultedFunctionInfo() const { |
| 2843 | return FunctionDeclBits.HasDefaultedFunctionInfo ? DefaultedInfo : nullptr; |
| 2844 | } |
| 2845 | |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2846 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2847 | for (auto I : redecls()) { |
| Serge Pavlov | 9a618e1 | 2017-02-15 12:30:35 +0000 | [diff] [blame] | 2848 | if (I->doesThisDeclarationHaveABody()) { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2849 | Definition = I; |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2850 | return true; |
| 2851 | } |
| 2852 | } |
| 2853 | |
| 2854 | return false; |
| 2855 | } |
| 2856 | |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2857 | bool FunctionDecl::hasTrivialBody() const { |
| Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 2858 | Stmt *S = getBody(); |
| 2859 | if (!S) { |
| 2860 | // Since we don't have a body for this function, we don't know if it's |
| 2861 | // trivial or not. |
| 2862 | return false; |
| 2863 | } |
| 2864 | |
| 2865 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 2866 | return true; |
| 2867 | return false; |
| 2868 | } |
| 2869 | |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2870 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2871 | for (auto I : redecls()) { |
| Serge Pavlov | 10673c9 | 2017-06-04 12:53:12 +0000 | [diff] [blame] | 2872 | if (I->isThisDeclarationADefinition()) { |
| 2873 | Definition = I; |
| Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2874 | return true; |
| 2875 | } |
| 2876 | } |
| 2877 | |
| 2878 | return false; |
| 2879 | } |
| 2880 | |
| Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2881 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2882 | if (!hasBody(Definition)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2883 | return nullptr; |
| Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2884 | |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2885 | assert(!Definition->FunctionDeclBits.HasDefaultedFunctionInfo && |
| 2886 | "definition should not have a body"); |
| Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2887 | if (Definition->Body) |
| 2888 | return Definition->Body.get(getASTContext().getExternalSource()); |
| Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2889 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2890 | return nullptr; |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 2891 | } |
| 2892 | |
| Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2893 | void FunctionDecl::setBody(Stmt *B) { |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 2894 | FunctionDeclBits.HasDefaultedFunctionInfo = false; |
| 2895 | Body = LazyDeclStmtPtr(B); |
| Douglas Gregor | 027ba50 | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 2896 | if (B) |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2897 | EndRangeLoc = B->getEndLoc(); |
| Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2900 | void FunctionDecl::setPure(bool P) { |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2901 | FunctionDeclBits.IsPure = P; |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2902 | if (P) |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2903 | if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
| Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2904 | Parent->markedVirtualFunctionPure(); |
| 2905 | } |
| 2906 | |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2907 | template<std::size_t Len> |
| 2908 | static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { |
| 2909 | IdentifierInfo *II = ND->getIdentifier(); |
| 2910 | return II && II->isStr(Str); |
| 2911 | } |
| 2912 | |
| Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 2913 | bool FunctionDecl::isMain() const { |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2914 | const TranslationUnitDecl *tunit = |
| 2915 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2916 | return tunit && |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2917 | !tunit->getASTContext().getLangOpts().Freestanding && |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2918 | isNamed(this, "main"); |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
| David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 2921 | bool FunctionDecl::isMSVCRTEntryPoint() const { |
| 2922 | const TranslationUnitDecl *TUnit = |
| 2923 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2924 | if (!TUnit) |
| 2925 | return false; |
| 2926 | |
| 2927 | // Even though we aren't really targeting MSVCRT if we are freestanding, |
| 2928 | // semantic analysis for these functions remains the same. |
| 2929 | |
| 2930 | // MSVCRT entry points only exist on MSVCRT targets. |
| 2931 | if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) |
| 2932 | return false; |
| 2933 | |
| 2934 | // Nameless functions like constructors cannot be entry points. |
| 2935 | if (!getIdentifier()) |
| 2936 | return false; |
| 2937 | |
| 2938 | return llvm::StringSwitch<bool>(getName()) |
| 2939 | .Cases("main", // an ANSI console app |
| 2940 | "wmain", // a Unicode console App |
| 2941 | "WinMain", // an ANSI GUI app |
| 2942 | "wWinMain", // a Unicode GUI app |
| 2943 | "DllMain", // a DLL |
| 2944 | true) |
| 2945 | .Default(false); |
| 2946 | } |
| 2947 | |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2948 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 2949 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 2950 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 2951 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 2952 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 2953 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 2954 | |
| Richard Smith | f600441 | 2014-01-19 23:25:37 +0000 | [diff] [blame] | 2955 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2956 | return false; |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2957 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2958 | const auto *proto = getType()->castAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2959 | if (proto->getNumParams() != 2 || proto->isVariadic()) |
| 2960 | return false; |
| John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2961 | |
| 2962 | ASTContext &Context = |
| 2963 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 2964 | ->getASTContext(); |
| 2965 | |
| 2966 | // The result type and first argument type are constant across all |
| 2967 | // these operators. The second argument must be exactly void*. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2968 | return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); |
| Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| Roman Lebedev | 3dd5a29 | 2020-02-26 01:37:17 +0300 | [diff] [blame] | 2971 | bool FunctionDecl::isReplaceableGlobalAllocationFunction( |
| 2972 | Optional<unsigned> *AlignmentParam, bool *IsNothrow) const { |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2973 | if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) |
| 2974 | return false; |
| 2975 | if (getDeclName().getCXXOverloadedOperator() != OO_New && |
| 2976 | getDeclName().getCXXOverloadedOperator() != OO_Delete && |
| 2977 | getDeclName().getCXXOverloadedOperator() != OO_Array_New && |
| 2978 | getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
| 2979 | return false; |
| 2980 | |
| 2981 | if (isa<CXXRecordDecl>(getDeclContext())) |
| 2982 | return false; |
| Richard Smith | f600441 | 2014-01-19 23:25:37 +0000 | [diff] [blame] | 2983 | |
| 2984 | // This can only fail for an invalid 'operator new' declaration. |
| 2985 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2986 | return false; |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2987 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2988 | const auto *FPT = getType()->castAs<FunctionProtoType>(); |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2989 | if (FPT->getNumParams() == 0 || FPT->getNumParams() > 3 || FPT->isVariadic()) |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2990 | return false; |
| 2991 | |
| 2992 | // If this is a single-parameter function, it must be a replaceable global |
| 2993 | // allocation or deallocation function. |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2994 | if (FPT->getNumParams() == 1) |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2995 | return true; |
| 2996 | |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2997 | unsigned Params = 1; |
| 2998 | QualType Ty = FPT->getParamType(Params); |
| Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2999 | ASTContext &Ctx = getASTContext(); |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3000 | |
| 3001 | auto Consume = [&] { |
| 3002 | ++Params; |
| 3003 | Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType(); |
| 3004 | }; |
| 3005 | |
| 3006 | // In C++14, the next parameter can be a 'std::size_t' for sized delete. |
| 3007 | bool IsSizedDelete = false; |
| Richard Smith | b47c36f | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 3008 | if (Ctx.getLangOpts().SizedDeallocation && |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3009 | (getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 3010 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) && |
| 3011 | Ctx.hasSameType(Ty, Ctx.getSizeType())) { |
| 3012 | IsSizedDelete = true; |
| 3013 | Consume(); |
| 3014 | } |
| 3015 | |
| 3016 | // In C++17, the next parameter can be a 'std::align_val_t' for aligned |
| 3017 | // new/delete. |
| Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 3018 | if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) { |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3019 | Consume(); |
| Roman Lebedev | 3dd5a29 | 2020-02-26 01:37:17 +0300 | [diff] [blame] | 3020 | if (AlignmentParam) |
| 3021 | *AlignmentParam = Params; |
| Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 3022 | } |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3023 | |
| 3024 | // Finally, if this is not a sized delete, the final parameter can |
| 3025 | // be a 'const std::nothrow_t&'. |
| 3026 | if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) { |
| 3027 | Ty = Ty->getPointeeType(); |
| 3028 | if (Ty.getCVRQualifiers() != Qualifiers::Const) |
| 3029 | return false; |
| Roman Lebedev | 3dd5a29 | 2020-02-26 01:37:17 +0300 | [diff] [blame] | 3030 | if (Ty->isNothrowT()) { |
| 3031 | if (IsNothrow) |
| 3032 | *IsNothrow = true; |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3033 | Consume(); |
| Roman Lebedev | 3dd5a29 | 2020-02-26 01:37:17 +0300 | [diff] [blame] | 3034 | } |
| Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | return Params == FPT->getNumParams(); |
| Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
| serge-sans-paille | d437fba | 2020-01-16 19:54:38 +0100 | [diff] [blame] | 3040 | bool FunctionDecl::isInlineBuiltinDeclaration() const { |
| 3041 | if (!getBuiltinID()) |
| 3042 | return false; |
| 3043 | |
| 3044 | const FunctionDecl *Definition; |
| 3045 | return hasBody(Definition) && Definition->isInlineSpecified(); |
| 3046 | } |
| 3047 | |
| Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 3048 | bool FunctionDecl::isDestroyingOperatorDelete() const { |
| 3049 | // C++ P0722: |
| 3050 | // Within a class C, a single object deallocation function with signature |
| 3051 | // (T, std::destroying_delete_t, <more params>) |
| 3052 | // is a destroying operator delete. |
| 3053 | if (!isa<CXXMethodDecl>(this) || getOverloadedOperator() != OO_Delete || |
| 3054 | getNumParams() < 2) |
| 3055 | return false; |
| 3056 | |
| 3057 | auto *RD = getParamDecl(1)->getType()->getAsCXXRecordDecl(); |
| 3058 | return RD && RD->isInStdNamespace() && RD->getIdentifier() && |
| 3059 | RD->getIdentifier()->isStr("destroying_delete_t"); |
| 3060 | } |
| 3061 | |
| Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 3062 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 3063 | return getDeclLanguageLinkage(*this); |
| Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 3066 | bool FunctionDecl::isExternC() const { |
| Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 3067 | return isDeclExternC(*this); |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 3070 | bool FunctionDecl::isInExternCContext() const { |
| Anastasia Stulova | d6865b7 | 2019-05-07 14:22:34 +0000 | [diff] [blame] | 3071 | if (hasAttr<OpenCLKernelAttr>()) |
| 3072 | return true; |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 3073 | return getLexicalDeclContext()->isExternCContext(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 3074 | } |
| 3075 | |
| 3076 | bool FunctionDecl::isInExternCXXContext() const { |
| Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 3077 | return getLexicalDeclContext()->isExternCXXContext(); |
| Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 3080 | bool FunctionDecl::isGlobal() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3081 | if (const auto *Method = dyn_cast<CXXMethodDecl>(this)) |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 3082 | return Method->isStatic(); |
| 3083 | |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3084 | if (getCanonicalDecl()->getStorageClass() == SC_Static) |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 3085 | return false; |
| 3086 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3087 | for (const DeclContext *DC = getDeclContext(); |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 3088 | DC->isNamespace(); |
| 3089 | DC = DC->getParent()) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3090 | if (const auto *Namespace = cast<NamespaceDecl>(DC)) { |
| Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 3091 | if (!Namespace->getDeclName()) |
| 3092 | return false; |
| 3093 | break; |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | return true; |
| 3098 | } |
| 3099 | |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3100 | bool FunctionDecl::isNoReturn() const { |
| Adrian Prantl | 721c7cb | 2016-08-17 16:42:15 +0000 | [diff] [blame] | 3101 | if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
| 3102 | hasAttr<C11NoReturnAttr>()) |
| 3103 | return true; |
| 3104 | |
| 3105 | if (auto *FnTy = getType()->getAs<FunctionType>()) |
| 3106 | return FnTy->getNoReturnAttr(); |
| 3107 | |
| 3108 | return false; |
| Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3109 | } |
| 3110 | |
| Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 3111 | |
| 3112 | MultiVersionKind FunctionDecl::getMultiVersionKind() const { |
| 3113 | if (hasAttr<TargetAttr>()) |
| 3114 | return MultiVersionKind::Target; |
| 3115 | if (hasAttr<CPUDispatchAttr>()) |
| 3116 | return MultiVersionKind::CPUDispatch; |
| 3117 | if (hasAttr<CPUSpecificAttr>()) |
| 3118 | return MultiVersionKind::CPUSpecific; |
| 3119 | return MultiVersionKind::None; |
| 3120 | } |
| 3121 | |
| Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 3122 | bool FunctionDecl::isCPUDispatchMultiVersion() const { |
| 3123 | return isMultiVersion() && hasAttr<CPUDispatchAttr>(); |
| 3124 | } |
| 3125 | |
| 3126 | bool FunctionDecl::isCPUSpecificMultiVersion() const { |
| 3127 | return isMultiVersion() && hasAttr<CPUSpecificAttr>(); |
| 3128 | } |
| 3129 | |
| Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 3130 | bool FunctionDecl::isTargetMultiVersion() const { |
| 3131 | return isMultiVersion() && hasAttr<TargetAttr>(); |
| 3132 | } |
| 3133 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3134 | void |
| 3135 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3136 | redeclarable_base::setPreviousDecl(PrevDecl); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3137 | |
| 3138 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 3139 | FunctionTemplateDecl *PrevFunTmpl |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3140 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3141 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3142 | FunTmpl->setPreviousDecl(PrevFunTmpl); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3143 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3144 | |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3145 | if (PrevDecl && PrevDecl->isInlined()) |
| 3146 | setImplicitlyInline(true); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3149 | FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3150 | |
| Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3151 | /// Returns a value indicating whether this function corresponds to a builtin |
| 3152 | /// function. |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 3153 | /// |
| Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3154 | /// The function corresponds to a built-in function if it is declared at |
| 3155 | /// translation scope or within an extern "C" block and its name matches with |
| 3156 | /// the name of a builtin. The returned value will be 0 for functions that do |
| 3157 | /// not correspond to a builtin, a value of type \c Builtin::ID if in the |
| 3158 | /// target-independent range \c [1,Builtin::First), or a target-specific builtin |
| 3159 | /// value. |
| 3160 | /// |
| 3161 | /// \param ConsiderWrapperFunctions If true, we should consider wrapper |
| 3162 | /// functions as their wrapped builtins. This shouldn't be done in general, but |
| 3163 | /// it's useful in Sema to diagnose calls to wrappers based on their semantics. |
| 3164 | unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { |
| Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 3165 | unsigned BuiltinID; |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 3166 | |
| Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 3167 | if (const auto *ABAA = getAttr<ArmBuiltinAliasAttr>()) { |
| 3168 | BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); |
| Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 3169 | } else { |
| 3170 | if (!getIdentifier()) |
| 3171 | return 0; |
| 3172 | |
| 3173 | BuiltinID = getIdentifier()->getBuiltinID(); |
| 3174 | } |
| 3175 | |
| Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 3176 | if (!BuiltinID) |
| 3177 | return 0; |
| 3178 | |
| 3179 | ASTContext &Context = getASTContext(); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3180 | if (Context.getLangOpts().CPlusPlus) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3181 | const auto *LinkageDecl = |
| 3182 | dyn_cast<LinkageSpecDecl>(getFirstDecl()->getDeclContext()); |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3183 | // In C++, the first declaration of a builtin is always inside an implicit |
| 3184 | // extern "C". |
| 3185 | // FIXME: A recognised library function may not be directly in an extern "C" |
| 3186 | // declaration, for instance "extern "C" { namespace std { decl } }". |
| David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 3187 | if (!LinkageDecl) { |
| 3188 | if (BuiltinID == Builtin::BI__GetExceptionInfo && |
| David Majnemer | 145abde | 2016-01-26 01:12:17 +0000 | [diff] [blame] | 3189 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
| David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 3190 | return Builtin::BI__GetExceptionInfo; |
| 3191 | return 0; |
| 3192 | } |
| 3193 | if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c) |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3194 | return 0; |
| 3195 | } |
| 3196 | |
| 3197 | // If the function is marked "overloadable", it has a different mangled name |
| 3198 | // and is not the C library function. |
| Simon Tatham | 7c11da0 | 2019-09-02 15:35:09 +0100 | [diff] [blame] | 3199 | if (!ConsiderWrapperFunctions && hasAttr<OverloadableAttr>() && |
| Mikhail Maltsev | 47edf5ba | 2020-03-10 14:01:42 +0000 | [diff] [blame] | 3200 | !hasAttr<ArmBuiltinAliasAttr>()) |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3201 | return 0; |
| 3202 | |
| Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 3203 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 3204 | return BuiltinID; |
| 3205 | |
| 3206 | // This function has the name of a known C library |
| 3207 | // function. Determine whether it actually refers to the C library |
| 3208 | // function or whether it just has the same name. |
| 3209 | |
| Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 3210 | // If this is a static function, it's not a builtin. |
| Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3211 | if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) |
| Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 3212 | return 0; |
| 3213 | |
| Anastasia Stulova | 1202de3 | 2016-02-12 12:07:04 +0000 | [diff] [blame] | 3214 | // OpenCL v1.2 s6.9.f - The library functions defined in |
| 3215 | // the C99 standard headers are not available. |
| 3216 | if (Context.getLangOpts().OpenCL && |
| 3217 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 3218 | return 0; |
| 3219 | |
| Artem Belevich | 5ecdb94 | 2018-01-23 19:08:18 +0000 | [diff] [blame] | 3220 | // CUDA does not have device-side standard library. printf and malloc are the |
| 3221 | // only special cases that are supported by device-side runtime. |
| 3222 | if (Context.getLangOpts().CUDA && hasAttr<CUDADeviceAttr>() && |
| 3223 | !hasAttr<CUDAHostAttr>() && |
| 3224 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
| 3225 | return 0; |
| 3226 | |
| Saiyedul Islam | 602d9b0 | 2020-05-14 06:09:04 +0000 | [diff] [blame] | 3227 | // As AMDGCN implementation of OpenMP does not have a device-side standard |
| 3228 | // library, none of the predefined library functions except printf and malloc |
| 3229 | // should be treated as a builtin i.e. 0 should be returned for them. |
| 3230 | if (Context.getTargetInfo().getTriple().isAMDGCN() && |
| 3231 | Context.getLangOpts().OpenMPIsDevice && |
| 3232 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID) && |
| 3233 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
| 3234 | return 0; |
| 3235 | |
| Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3236 | return BuiltinID; |
| Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 3239 | /// getNumParams - Return the number of parameters this function must have |
| Bob Wilson | b39017a | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 3240 | /// based on its FunctionType. This is the length of the ParamInfo array |
| Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 3241 | /// after it has been created. |
| 3242 | unsigned FunctionDecl::getNumParams() const { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3243 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3244 | return FPT ? FPT->getNumParams() : 0; |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3247 | void FunctionDecl::setParams(ASTContext &C, |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3248 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3249 | assert(!ParamInfo && "Already has param info!"); |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3250 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3251 | |
| Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 3252 | // Zero params -> null pointer. |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3253 | if (!NewParamInfo.empty()) { |
| 3254 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 3255 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
| Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 3256 | } |
| Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 3257 | } |
| Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 3258 | |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3259 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 3260 | /// needed to call this function. This may be fewer than the number of |
| 3261 | /// function parameters, if some of the parameters have default |
| Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 3262 | /// arguments (in C++) or are parameter packs (C++11). |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3263 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3264 | if (!getASTContext().getLangOpts().CPlusPlus) |
| Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3265 | return getNumParams(); |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3266 | |
| Richard Smith | b5f2c4e | 2020-06-02 10:42:36 -0700 | [diff] [blame] | 3267 | // Note that it is possible for a parameter with no default argument to |
| 3268 | // follow a parameter with a default argument. |
| Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 3269 | unsigned NumRequiredArgs = 0; |
| Richard Smith | b5f2c4e | 2020-06-02 10:42:36 -0700 | [diff] [blame] | 3270 | unsigned MinParamsSoFar = 0; |
| 3271 | for (auto *Param : parameters()) { |
| 3272 | if (!Param->isParameterPack()) { |
| 3273 | ++MinParamsSoFar; |
| 3274 | if (!Param->hasDefaultArg()) |
| 3275 | NumRequiredArgs = MinParamsSoFar; |
| 3276 | } |
| 3277 | } |
| Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3278 | return NumRequiredArgs; |
| 3279 | } |
| 3280 | |
| Richard Smith | b5f2c4e | 2020-06-02 10:42:36 -0700 | [diff] [blame] | 3281 | bool FunctionDecl::hasOneParamOrDefaultArgs() const { |
| 3282 | return getNumParams() == 1 || |
| 3283 | (getNumParams() > 1 && |
| 3284 | std::all_of(param_begin() + 1, param_end(), |
| 3285 | [](ParmVarDecl *P) { return P->hasDefaultArg(); })); |
| 3286 | } |
| 3287 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3288 | /// The combination of the extern and inline keywords under MSVC forces |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3289 | /// the function to be required. |
| 3290 | /// |
| 3291 | /// Note: This function assumes that we will only get called when isInlined() |
| 3292 | /// would return true for this FunctionDecl. |
| 3293 | bool FunctionDecl::isMSExternInline() const { |
| 3294 | assert(isInlined() && "expected to get called on an inlined function!"); |
| 3295 | |
| 3296 | const ASTContext &Context = getASTContext(); |
| David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 3297 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 3298 | !hasAttr<DLLExportAttr>()) |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3299 | return false; |
| 3300 | |
| David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 3301 | for (const FunctionDecl *FD = getMostRecentDecl(); FD; |
| 3302 | FD = FD->getPreviousDecl()) |
| David Majnemer | c0c42f3 | 2015-07-10 20:55:38 +0000 | [diff] [blame] | 3303 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3304 | return true; |
| 3305 | |
| 3306 | return false; |
| 3307 | } |
| 3308 | |
| 3309 | static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { |
| 3310 | if (Redecl->getStorageClass() != SC_Extern) |
| 3311 | return false; |
| 3312 | |
| 3313 | for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; |
| 3314 | FD = FD->getPreviousDecl()) |
| David Majnemer | c0c42f3 | 2015-07-10 20:55:38 +0000 | [diff] [blame] | 3315 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3316 | return false; |
| 3317 | |
| 3318 | return true; |
| 3319 | } |
| 3320 | |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3321 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 3322 | // Only consider file-scope declarations in this test. |
| 3323 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 3324 | return false; |
| 3325 | |
| 3326 | // Only consider explicit declarations; the presence of a builtin for a |
| 3327 | // libcall shouldn't affect whether a definition is externally visible. |
| 3328 | if (Redecl->isImplicit()) |
| 3329 | return false; |
| 3330 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3331 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3332 | return true; // Not an inline definition |
| 3333 | |
| 3334 | return false; |
| 3335 | } |
| 3336 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3337 | /// For a function declaration in C or C++, determine whether this |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3338 | /// declaration causes the definition to be externally visible. |
| 3339 | /// |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3340 | /// For instance, this determines if adding the current declaration to the set |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3341 | /// of redeclarations of the given functions causes |
| 3342 | /// isInlineDefinitionExternallyVisible to change from false to true. |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3343 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| Justin Lebar | 0241c96 | 2016-10-28 16:46:39 +0000 | [diff] [blame] | 3344 | assert(!doesThisDeclarationHaveABody() && |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3345 | "Must have a declaration without a body."); |
| 3346 | |
| 3347 | ASTContext &Context = getASTContext(); |
| 3348 | |
| David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3349 | if (Context.getLangOpts().MSVCCompat) { |
| 3350 | const FunctionDecl *Definition; |
| 3351 | if (hasBody(Definition) && Definition->isInlined() && |
| 3352 | redeclForcesDefMSVC(this)) |
| 3353 | return true; |
| 3354 | } |
| 3355 | |
| Martin Storsjo | 71decf8 | 2019-09-27 12:25:19 +0000 | [diff] [blame] | 3356 | if (Context.getLangOpts().CPlusPlus) |
| 3357 | return false; |
| 3358 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3359 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3360 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 3361 | // an externally visible definition. |
| 3362 | // |
| 3363 | // FIXME: What happens if gnu_inline gets added on after the first |
| 3364 | // declaration? |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3365 | if (!isInlineSpecified() || getStorageClass() == SC_Extern) |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3366 | return false; |
| 3367 | |
| 3368 | const FunctionDecl *Prev = this; |
| 3369 | bool FoundBody = false; |
| 3370 | while ((Prev = Prev->getPreviousDecl())) { |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 3371 | FoundBody |= Prev->doesThisDeclarationHaveABody(); |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3372 | |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 3373 | if (Prev->doesThisDeclarationHaveABody()) { |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3374 | // If it's not the case that both 'inline' and 'extern' are |
| 3375 | // specified on the definition, then it is always externally visible. |
| 3376 | if (!Prev->isInlineSpecified() || |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3377 | Prev->getStorageClass() != SC_Extern) |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3378 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3379 | } else if (Prev->isInlineSpecified() && |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3380 | Prev->getStorageClass() != SC_Extern) { |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3381 | return false; |
| 3382 | } |
| 3383 | } |
| 3384 | return FoundBody; |
| 3385 | } |
| 3386 | |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3387 | // C99 6.7.4p6: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3388 | // [...] If all of the file scope declarations for a function in a |
| 3389 | // translation unit include the inline function specifier without extern, |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3390 | // then the definition in that translation unit is an inline definition. |
| 3391 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3392 | return false; |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3393 | const FunctionDecl *Prev = this; |
| 3394 | bool FoundBody = false; |
| 3395 | while ((Prev = Prev->getPreviousDecl())) { |
| Richard Smith | 848934c | 2019-12-05 13:37:35 -0800 | [diff] [blame] | 3396 | FoundBody |= Prev->doesThisDeclarationHaveABody(); |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3397 | if (RedeclForcesDefC99(Prev)) |
| 3398 | return false; |
| 3399 | } |
| 3400 | return FoundBody; |
| Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
| Richard Smith | d052a578 | 2019-10-22 17:44:08 -0700 | [diff] [blame] | 3403 | FunctionTypeLoc FunctionDecl::getFunctionTypeLoc() const { |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3404 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
| Richard Smith | d052a578 | 2019-10-22 17:44:08 -0700 | [diff] [blame] | 3405 | return TSI ? TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>() |
| 3406 | : FunctionTypeLoc(); |
| 3407 | } |
| 3408 | |
| 3409 | SourceRange FunctionDecl::getReturnTypeSourceRange() const { |
| 3410 | FunctionTypeLoc FTL = getFunctionTypeLoc(); |
| Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3411 | if (!FTL) |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3412 | return SourceRange(); |
| 3413 | |
| Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3414 | // Skip self-referential return types. |
| 3415 | const SourceManager &SM = getASTContext().getSourceManager(); |
| 3416 | SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3417 | SourceLocation Boundary = getNameInfo().getBeginLoc(); |
| Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3418 | if (RTRange.isInvalid() || Boundary.isInvalid() || |
| 3419 | !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) |
| 3420 | return SourceRange(); |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3421 | |
| Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3422 | return RTRange; |
| Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3423 | } |
| 3424 | |
| Nicolas Manichon | cc3c935 | 2019-12-03 08:21:55 -0500 | [diff] [blame] | 3425 | SourceRange FunctionDecl::getParametersSourceRange() const { |
| 3426 | unsigned NP = getNumParams(); |
| 3427 | SourceLocation EllipsisLoc = getEllipsisLoc(); |
| 3428 | |
| 3429 | if (NP == 0 && EllipsisLoc.isInvalid()) |
| 3430 | return SourceRange(); |
| 3431 | |
| 3432 | SourceLocation Begin = |
| 3433 | NP > 0 ? ParamInfo[0]->getSourceRange().getBegin() : EllipsisLoc; |
| 3434 | SourceLocation End = EllipsisLoc.isValid() |
| 3435 | ? EllipsisLoc |
| 3436 | : ParamInfo[NP - 1]->getSourceRange().getEnd(); |
| 3437 | |
| 3438 | return SourceRange(Begin, End); |
| 3439 | } |
| 3440 | |
| Malcolm Parsons | a3220ce | 2017-01-12 16:11:28 +0000 | [diff] [blame] | 3441 | SourceRange FunctionDecl::getExceptionSpecSourceRange() const { |
| Richard Smith | d052a578 | 2019-10-22 17:44:08 -0700 | [diff] [blame] | 3442 | FunctionTypeLoc FTL = getFunctionTypeLoc(); |
| 3443 | return FTL ? FTL.getExceptionSpecRange() : SourceRange(); |
| Malcolm Parsons | a3220ce | 2017-01-12 16:11:28 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3446 | /// 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] | 3447 | /// in C++, determine whether the definition will be externally visible. |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3448 | /// |
| 3449 | /// Inline function definitions are always available for inlining optimizations. |
| 3450 | /// However, depending on the language dialect, declaration specifiers, and |
| 3451 | /// attributes, the definition of an inline function may or may not be |
| 3452 | /// "externally" visible to other translation units in the program. |
| 3453 | /// |
| 3454 | /// In C99, inline definitions are not externally visible by default. However, |
| Mike Stump | 13c6670 | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 3455 | /// 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] | 3456 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 3457 | /// |
| 3458 | /// 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] | 3459 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 3460 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 3461 | /// an externally visible symbol, but "extern inline" will not create an |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3462 | /// externally visible symbol. |
| 3463 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| Nick Desaulniers | 5806022 | 2019-09-12 19:53:35 +0000 | [diff] [blame] | 3464 | assert((doesThisDeclarationHaveABody() || willHaveBody() || |
| 3465 | hasAttr<AliasAttr>()) && |
| Manuel Klimek | da7456a | 2016-11-01 10:30:50 +0000 | [diff] [blame] | 3466 | "Must be a function definition"); |
| Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 3467 | assert(isInlined() && "Function must be inline"); |
| Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 3468 | ASTContext &Context = getASTContext(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3469 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3470 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3471 | // Note: If you change the logic here, please change |
| 3472 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 3473 | // |
| Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3474 | // If it's not the case that both 'inline' and 'extern' are |
| 3475 | // specified on the definition, then this inline definition is |
| 3476 | // externally visible. |
| Martin Storsjo | 71decf8 | 2019-09-27 12:25:19 +0000 | [diff] [blame] | 3477 | if (Context.getLangOpts().CPlusPlus) |
| 3478 | return false; |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3479 | if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) |
| Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3480 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3481 | |
| Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3482 | // If any declaration is 'inline' but not 'extern', then this definition |
| 3483 | // is externally visible. |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3484 | for (auto Redecl : redecls()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3485 | if (Redecl->isInlineSpecified() && |
| Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3486 | Redecl->getStorageClass() != SC_Extern) |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3487 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 3490 | return false; |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3491 | } |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3492 | |
| Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3493 | // The rest of this function is C-only. |
| 3494 | assert(!Context.getLangOpts().CPlusPlus && |
| 3495 | "should not use C inline rules in C++"); |
| 3496 | |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3497 | // C99 6.7.4p6: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3498 | // [...] If all of the file scope declarations for a function in a |
| 3499 | // translation unit include the inline function specifier without extern, |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3500 | // then the definition in that translation unit is an inline definition. |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3501 | for (auto Redecl : redecls()) { |
| 3502 | if (RedeclForcesDefC99(Redecl)) |
| Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3503 | return true; |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3504 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3505 | |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3506 | // C99 6.7.4p6: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3507 | // An inline definition does not provide an external definition for the |
| 3508 | // function, and does not forbid an external definition in another |
| Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3509 | // translation unit. |
| Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 3510 | return false; |
| 3511 | } |
| 3512 | |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3513 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 3514 | /// function represents, if any. |
| 3515 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
| Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 3516 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 3517 | return getDeclName().getCXXOverloadedOperator(); |
| Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3518 | else |
| 3519 | return OO_None; |
| 3520 | } |
| 3521 | |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 3522 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 3523 | /// represents, if any. |
| 3524 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 3525 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 3526 | return getDeclName().getCXXLiteralIdentifier(); |
| 3527 | else |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3528 | return nullptr; |
| Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
| Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 3531 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 3532 | if (TemplateOrSpecialization.isNull()) |
| 3533 | return TK_NonTemplate; |
| 3534 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 3535 | return TK_FunctionTemplate; |
| 3536 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 3537 | return TK_MemberSpecialization; |
| 3538 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 3539 | return TK_FunctionTemplateSpecialization; |
| 3540 | if (TemplateOrSpecialization.is |
| 3541 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 3542 | return TK_DependentFunctionTemplateSpecialization; |
| 3543 | |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3544 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
| Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3547 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3548 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3549 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3550 | |
| 3551 | return nullptr; |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3552 | } |
| 3553 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3554 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3555 | if (auto *MSI = |
| 3556 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
| 3557 | return MSI; |
| 3558 | if (auto *FTSI = TemplateOrSpecialization |
| 3559 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) |
| 3560 | return FTSI->getMemberSpecializationInfo(); |
| 3561 | return nullptr; |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3564 | void |
| Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3565 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 3566 | FunctionDecl *FD, |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3567 | TemplateSpecializationKind TSK) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3568 | assert(TemplateOrSpecialization.isNull() && |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3569 | "Member function is already a specialization"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3570 | MemberSpecializationInfo *Info |
| Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3571 | = new (C) MemberSpecializationInfo(FD, TSK); |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3572 | TemplateOrSpecialization = Info; |
| 3573 | } |
| 3574 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3575 | FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const { |
| 3576 | return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl *>(); |
| 3577 | } |
| 3578 | |
| 3579 | void FunctionDecl::setDescribedFunctionTemplate(FunctionTemplateDecl *Template) { |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3580 | assert(TemplateOrSpecialization.isNull() && |
| 3581 | "Member function is already a specialization"); |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3582 | TemplateOrSpecialization = Template; |
| 3583 | } |
| 3584 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3585 | bool FunctionDecl::isImplicitlyInstantiable() const { |
| Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 3586 | // If the function is invalid, it can't be implicitly instantiated. |
| 3587 | if (isInvalidDecl()) |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3588 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3589 | |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3590 | switch (getTemplateSpecializationKindForInstantiation()) { |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3591 | case TSK_Undeclared: |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3592 | case TSK_ExplicitInstantiationDefinition: |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3593 | case TSK_ExplicitSpecialization: |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3594 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3595 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3596 | case TSK_ImplicitInstantiation: |
| 3597 | return true; |
| 3598 | |
| 3599 | case TSK_ExplicitInstantiationDeclaration: |
| 3600 | // Handled below. |
| 3601 | break; |
| 3602 | } |
| 3603 | |
| 3604 | // Find the actual template from which we will instantiate. |
| 3605 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3606 | bool HasPattern = false; |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3607 | if (PatternDecl) |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3608 | HasPattern = PatternDecl->hasBody(PatternDecl); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3609 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3610 | // C++0x [temp.explicit]p9: |
| 3611 | // Except for inline functions, other explicit instantiation declarations |
| 3612 | // have the effect of suppressing the implicit instantiation of the entity |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3613 | // to which they refer. |
| 3614 | if (!HasPattern || !PatternDecl) |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3615 | return true; |
| 3616 | |
| Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 3617 | return PatternDecl->isInlined(); |
| Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | bool FunctionDecl::isTemplateInstantiation() const { |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3621 | // FIXME: Remove this, it's not clear what it means. (Which template |
| 3622 | // specialization kind?) |
| 3623 | return clang::isTemplateInstantiation(getTemplateSpecializationKind()); |
| Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 3624 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3625 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3626 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3627 | // If this is a generic lambda call operator specialization, its |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3628 | // instantiation pattern is always its primary template's pattern |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3629 | // even if its primary template was instantiated from another |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3630 | // member template (which happens with nested generic lambdas). |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3631 | // Since a lambda's call operator's body is transformed eagerly, |
| 3632 | // we don't have to go hunting for a prototype definition template |
| 3633 | // (i.e. instantiated-from-member-template) to use as an instantiation |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3634 | // pattern. |
| Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3635 | |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3636 | if (isGenericLambdaCallOperatorSpecialization( |
| 3637 | dyn_cast<CXXMethodDecl>(this))) { |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3638 | assert(getPrimaryTemplate() && "not a generic lambda call operator?"); |
| 3639 | return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl()); |
| Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3640 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3641 | |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3642 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) { |
| 3643 | if (!clang::isTemplateInstantiation(Info->getTemplateSpecializationKind())) |
| 3644 | return nullptr; |
| 3645 | return getDefinitionOrSelf(cast<FunctionDecl>(Info->getInstantiatedFrom())); |
| 3646 | } |
| 3647 | |
| 3648 | if (!clang::isTemplateInstantiation(getTemplateSpecializationKind())) |
| 3649 | return nullptr; |
| 3650 | |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3651 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3652 | // If we hit a point where the user provided a specialization of this |
| 3653 | // template, we're done looking. |
| 3654 | while (!Primary->isMemberSpecialization()) { |
| 3655 | auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate(); |
| 3656 | if (!NewPrimary) |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3657 | break; |
| Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3658 | Primary = NewPrimary; |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3659 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3660 | |
| 3661 | return getDefinitionOrSelf(Primary->getTemplatedDecl()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3662 | } |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3663 | |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3664 | return nullptr; |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3667 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3668 | if (FunctionTemplateSpecializationInfo *Info |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3669 | = TemplateOrSpecialization |
| 3670 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3671 | return Info->getTemplate(); |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3672 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3673 | return nullptr; |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3676 | FunctionTemplateSpecializationInfo * |
| 3677 | FunctionDecl::getTemplateSpecializationInfo() const { |
| 3678 | return TemplateOrSpecialization |
| 3679 | .dyn_cast<FunctionTemplateSpecializationInfo *>(); |
| 3680 | } |
| 3681 | |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3682 | const TemplateArgumentList * |
| 3683 | FunctionDecl::getTemplateSpecializationArgs() const { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3684 | if (FunctionTemplateSpecializationInfo *Info |
| Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 3685 | = TemplateOrSpecialization |
| 3686 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3687 | return Info->TemplateArguments; |
| 3688 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3689 | return nullptr; |
| Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3690 | } |
| 3691 | |
| Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 3692 | const ASTTemplateArgumentListInfo * |
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3693 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 3694 | if (FunctionTemplateSpecializationInfo *Info |
| 3695 | = TemplateOrSpecialization |
| 3696 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 3697 | return Info->TemplateArgumentsAsWritten; |
| 3698 | } |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3699 | return nullptr; |
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3702 | void |
| Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3703 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 3704 | FunctionTemplateDecl *Template, |
| Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3705 | const TemplateArgumentList *TemplateArgs, |
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3706 | void *InsertPos, |
| Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3707 | TemplateSpecializationKind TSK, |
| Argyrios Kyrtzidis | 927d8e0 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 3708 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 3709 | SourceLocation PointOfInstantiation) { |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3710 | assert((TemplateOrSpecialization.isNull() || |
| 3711 | TemplateOrSpecialization.is<MemberSpecializationInfo *>()) && |
| 3712 | "Member function is already a specialization"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3713 | assert(TSK != TSK_Undeclared && |
| Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3714 | "Must specify the type of function template specialization"); |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3715 | assert((TemplateOrSpecialization.isNull() || |
| 3716 | TSK == TSK_ExplicitSpecialization) && |
| 3717 | "Member specialization must be an explicit specialization"); |
| 3718 | FunctionTemplateSpecializationInfo *Info = |
| 3719 | FunctionTemplateSpecializationInfo::Create( |
| 3720 | C, this, Template, TSK, TemplateArgs, TemplateArgsAsWritten, |
| 3721 | PointOfInstantiation, |
| 3722 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()); |
| Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3723 | TemplateOrSpecialization = Info; |
| Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 3724 | Template->addSpecialization(Info, InsertPos); |
| Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3727 | void |
| 3728 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 3729 | const UnresolvedSetImpl &Templates, |
| 3730 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3731 | assert(TemplateOrSpecialization.isNull()); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3732 | DependentFunctionTemplateSpecializationInfo *Info = |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3733 | DependentFunctionTemplateSpecializationInfo::Create(Context, Templates, |
| 3734 | TemplateArgs); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3735 | TemplateOrSpecialization = Info; |
| 3736 | } |
| 3737 | |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3738 | DependentFunctionTemplateSpecializationInfo * |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3739 | FunctionDecl::getDependentSpecializationInfo() const { |
| 3740 | return TemplateOrSpecialization |
| 3741 | .dyn_cast<DependentFunctionTemplateSpecializationInfo *>(); |
| 3742 | } |
| 3743 | |
| 3744 | DependentFunctionTemplateSpecializationInfo * |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3745 | DependentFunctionTemplateSpecializationInfo::Create( |
| 3746 | ASTContext &Context, const UnresolvedSetImpl &Ts, |
| 3747 | const TemplateArgumentListInfo &TArgs) { |
| 3748 | void *Buffer = Context.Allocate( |
| 3749 | totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>( |
| 3750 | TArgs.size(), Ts.size())); |
| 3751 | return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs); |
| 3752 | } |
| 3753 | |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3754 | DependentFunctionTemplateSpecializationInfo:: |
| 3755 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 3756 | const TemplateArgumentListInfo &TArgs) |
| Benjamin Kramer | 3ebba52 | 2015-04-02 12:43:31 +0000 | [diff] [blame] | 3757 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
| James Y Knight | 53c7616 | 2015-07-17 18:21:37 +0000 | [diff] [blame] | 3758 | NumTemplates = Ts.size(); |
| 3759 | NumArgs = TArgs.size(); |
| Benjamin Kramer | 3ebba52 | 2015-04-02 12:43:31 +0000 | [diff] [blame] | 3760 | |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3761 | FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>(); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3762 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 3763 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 3764 | |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3765 | TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>(); |
| John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3766 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 3767 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 3768 | } |
| 3769 | |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3770 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3771 | // For a function template specialization, query the specialization |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3772 | // information object. |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3773 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
| 3774 | TemplateOrSpecialization |
| 3775 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3776 | return FTSInfo->getTemplateSpecializationKind(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | |
| Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3778 | if (MemberSpecializationInfo *MSInfo = |
| 3779 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
| 3780 | return MSInfo->getTemplateSpecializationKind(); |
| 3781 | |
| 3782 | return TSK_Undeclared; |
| 3783 | } |
| 3784 | |
| 3785 | TemplateSpecializationKind |
| 3786 | FunctionDecl::getTemplateSpecializationKindForInstantiation() const { |
| 3787 | // This is the same as getTemplateSpecializationKind(), except that for a |
| 3788 | // function that is both a function template specialization and a member |
| 3789 | // specialization, we prefer the member specialization information. Eg: |
| 3790 | // |
| 3791 | // template<typename T> struct A { |
| 3792 | // template<typename U> void f() {} |
| 3793 | // template<> void f<int>() {} |
| 3794 | // }; |
| 3795 | // |
| 3796 | // For A<int>::f<int>(): |
| 3797 | // * getTemplateSpecializationKind() will return TSK_ExplicitSpecialization |
| 3798 | // * getTemplateSpecializationKindForInstantiation() will return |
| 3799 | // TSK_ImplicitInstantiation |
| 3800 | // |
| 3801 | // This reflects the facts that A<int>::f<int> is an explicit specialization |
| 3802 | // of A<int>::f, and that A<int>::f<int> should be implicitly instantiated |
| 3803 | // from A::f<int> if a definition is needed. |
| 3804 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
| 3805 | TemplateOrSpecialization |
| 3806 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) { |
| 3807 | if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo()) |
| 3808 | return MSInfo->getTemplateSpecializationKind(); |
| 3809 | return FTSInfo->getTemplateSpecializationKind(); |
| 3810 | } |
| 3811 | |
| 3812 | if (MemberSpecializationInfo *MSInfo = |
| 3813 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3814 | return MSInfo->getTemplateSpecializationKind(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3815 | |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3816 | return TSK_Undeclared; |
| Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3817 | } |
| 3818 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3819 | void |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3820 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 3821 | SourceLocation PointOfInstantiation) { |
| 3822 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3823 | = TemplateOrSpecialization.dyn_cast< |
| 3824 | FunctionTemplateSpecializationInfo*>()) { |
| 3825 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 3826 | if (TSK != TSK_ExplicitSpecialization && |
| 3827 | PointOfInstantiation.isValid() && |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3828 | FTSInfo->getPointOfInstantiation().isInvalid()) { |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3829 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3830 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 3831 | L->InstantiationRequested(this); |
| 3832 | } |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3833 | } else if (MemberSpecializationInfo *MSInfo |
| 3834 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 3835 | MSInfo->setTemplateSpecializationKind(TSK); |
| 3836 | if (TSK != TSK_ExplicitSpecialization && |
| 3837 | PointOfInstantiation.isValid() && |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3838 | MSInfo->getPointOfInstantiation().isInvalid()) { |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3839 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3840 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 3841 | L->InstantiationRequested(this); |
| 3842 | } |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3843 | } else |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3844 | llvm_unreachable("Function cannot have a template specialization kind"); |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3845 | } |
| 3846 | |
| 3847 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3848 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3849 | = TemplateOrSpecialization.dyn_cast< |
| 3850 | FunctionTemplateSpecializationInfo*>()) |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3851 | return FTSInfo->getPointOfInstantiation(); |
| Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3852 | else if (MemberSpecializationInfo *MSInfo |
| 3853 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3854 | return MSInfo->getPointOfInstantiation(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3855 | |
| Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3856 | return SourceLocation(); |
| Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3857 | } |
| 3858 | |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3859 | bool FunctionDecl::isOutOfLine() const { |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3860 | if (Decl::isOutOfLine()) |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3861 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3862 | |
| 3863 | // If this function was instantiated from a member function of a |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3864 | // class template, check whether that member function was defined out-of-line. |
| 3865 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 3866 | const FunctionDecl *Definition; |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3867 | if (FD->hasBody(Definition)) |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3868 | return Definition->isOutOfLine(); |
| 3869 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3870 | |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3871 | // If this function was instantiated from a function template, |
| 3872 | // check whether that function template was defined out-of-line. |
| 3873 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 3874 | const FunctionDecl *Definition; |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3875 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3876 | return Definition->isOutOfLine(); |
| 3877 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3878 | |
| Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3879 | return false; |
| 3880 | } |
| 3881 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3882 | SourceRange FunctionDecl::getSourceRange() const { |
| 3883 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 3884 | } |
| 3885 | |
| Anna Zaks | 28db7ce | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 3886 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3887 | IdentifierInfo *FnInfo = getIdentifier(); |
| 3888 | |
| 3889 | if (!FnInfo) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3890 | return 0; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3891 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3892 | // Builtin handling. |
| 3893 | switch (getBuiltinID()) { |
| 3894 | case Builtin::BI__builtin_memset: |
| 3895 | case Builtin::BI__builtin___memset_chk: |
| 3896 | case Builtin::BImemset: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3897 | return Builtin::BImemset; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3898 | |
| 3899 | case Builtin::BI__builtin_memcpy: |
| 3900 | case Builtin::BI__builtin___memcpy_chk: |
| 3901 | case Builtin::BImemcpy: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3902 | return Builtin::BImemcpy; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3903 | |
| serge-sans-paille | cee4a1c | 2019-12-11 21:30:10 +0100 | [diff] [blame] | 3904 | case Builtin::BI__builtin_mempcpy: |
| 3905 | case Builtin::BI__builtin___mempcpy_chk: |
| 3906 | case Builtin::BImempcpy: |
| 3907 | return Builtin::BImempcpy; |
| 3908 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3909 | case Builtin::BI__builtin_memmove: |
| 3910 | case Builtin::BI__builtin___memmove_chk: |
| 3911 | case Builtin::BImemmove: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3912 | return Builtin::BImemmove; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3913 | |
| 3914 | case Builtin::BIstrlcpy: |
| Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3915 | case Builtin::BI__builtin___strlcpy_chk: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3916 | return Builtin::BIstrlcpy; |
| Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3917 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3918 | case Builtin::BIstrlcat: |
| Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3919 | case Builtin::BI__builtin___strlcat_chk: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3920 | return Builtin::BIstrlcat; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3921 | |
| 3922 | case Builtin::BI__builtin_memcmp: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3923 | case Builtin::BImemcmp: |
| 3924 | return Builtin::BImemcmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3925 | |
| Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 3926 | case Builtin::BI__builtin_bcmp: |
| 3927 | case Builtin::BIbcmp: |
| 3928 | return Builtin::BIbcmp; |
| 3929 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3930 | case Builtin::BI__builtin_strncpy: |
| 3931 | case Builtin::BI__builtin___strncpy_chk: |
| 3932 | case Builtin::BIstrncpy: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3933 | return Builtin::BIstrncpy; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3934 | |
| 3935 | case Builtin::BI__builtin_strncmp: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3936 | case Builtin::BIstrncmp: |
| 3937 | return Builtin::BIstrncmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3938 | |
| 3939 | case Builtin::BI__builtin_strncasecmp: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3940 | case Builtin::BIstrncasecmp: |
| 3941 | return Builtin::BIstrncasecmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3942 | |
| 3943 | case Builtin::BI__builtin_strncat: |
| Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3944 | case Builtin::BI__builtin___strncat_chk: |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3945 | case Builtin::BIstrncat: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3946 | return Builtin::BIstrncat; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3947 | |
| 3948 | case Builtin::BI__builtin_strndup: |
| 3949 | case Builtin::BIstrndup: |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3950 | return Builtin::BIstrndup; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3951 | |
| Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3952 | case Builtin::BI__builtin_strlen: |
| 3953 | case Builtin::BIstrlen: |
| 3954 | return Builtin::BIstrlen; |
| 3955 | |
| Bruno Cardoso Lopes | 7ea9fd2 | 2016-08-10 18:34:47 +0000 | [diff] [blame] | 3956 | case Builtin::BI__builtin_bzero: |
| 3957 | case Builtin::BIbzero: |
| 3958 | return Builtin::BIbzero; |
| 3959 | |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3960 | default: |
| Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 3961 | if (isExternC()) { |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3962 | if (FnInfo->isStr("memset")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3963 | return Builtin::BImemset; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3964 | else if (FnInfo->isStr("memcpy")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3965 | return Builtin::BImemcpy; |
| serge-sans-paille | cee4a1c | 2019-12-11 21:30:10 +0100 | [diff] [blame] | 3966 | else if (FnInfo->isStr("mempcpy")) |
| 3967 | return Builtin::BImempcpy; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3968 | else if (FnInfo->isStr("memmove")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3969 | return Builtin::BImemmove; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3970 | else if (FnInfo->isStr("memcmp")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3971 | return Builtin::BImemcmp; |
| Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 3972 | else if (FnInfo->isStr("bcmp")) |
| 3973 | return Builtin::BIbcmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3974 | else if (FnInfo->isStr("strncpy")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3975 | return Builtin::BIstrncpy; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3976 | else if (FnInfo->isStr("strncmp")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3977 | return Builtin::BIstrncmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3978 | else if (FnInfo->isStr("strncasecmp")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3979 | return Builtin::BIstrncasecmp; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3980 | else if (FnInfo->isStr("strncat")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3981 | return Builtin::BIstrncat; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3982 | else if (FnInfo->isStr("strndup")) |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3983 | return Builtin::BIstrndup; |
| Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3984 | else if (FnInfo->isStr("strlen")) |
| 3985 | return Builtin::BIstrlen; |
| Bruno Cardoso Lopes | 7ea9fd2 | 2016-08-10 18:34:47 +0000 | [diff] [blame] | 3986 | else if (FnInfo->isStr("bzero")) |
| 3987 | return Builtin::BIbzero; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3988 | } |
| 3989 | break; |
| 3990 | } |
| Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3991 | return 0; |
| Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
| Richard Trieu | 27c1b1a | 2018-07-10 01:40:50 +0000 | [diff] [blame] | 3994 | unsigned FunctionDecl::getODRHash() const { |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3995 | assert(hasODRHash()); |
| Richard Trieu | 27c1b1a | 2018-07-10 01:40:50 +0000 | [diff] [blame] | 3996 | return ODRHash; |
| 3997 | } |
| 3998 | |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3999 | unsigned FunctionDecl::getODRHash() { |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 4000 | if (hasODRHash()) |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 4001 | return ODRHash; |
| 4002 | |
| Richard Trieu | 0ac2eb7 | 2018-02-22 05:50:29 +0000 | [diff] [blame] | 4003 | if (auto *FT = getInstantiatedFromMemberFunction()) { |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 4004 | setHasODRHash(true); |
| Richard Trieu | 0ac2eb7 | 2018-02-22 05:50:29 +0000 | [diff] [blame] | 4005 | ODRHash = FT->getODRHash(); |
| 4006 | return ODRHash; |
| 4007 | } |
| 4008 | |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 4009 | class ODRHash Hash; |
| 4010 | Hash.AddFunctionDecl(this); |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 4011 | setHasODRHash(true); |
| Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 4012 | ODRHash = Hash.CalculateHash(); |
| 4013 | return ODRHash; |
| 4014 | } |
| 4015 | |
| Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 4016 | //===----------------------------------------------------------------------===// |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4017 | // FieldDecl Implementation |
| 4018 | //===----------------------------------------------------------------------===// |
| 4019 | |
| Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4020 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4021 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4022 | IdentifierInfo *Id, QualType T, |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 4023 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
| Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 4024 | InClassInitStyle InitStyle) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4025 | return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
| 4026 | BW, Mutable, InitStyle); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4027 | } |
| 4028 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4029 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4030 | return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), |
| 4031 | SourceLocation(), nullptr, QualType(), nullptr, |
| 4032 | nullptr, false, ICIS_NoInit); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4035 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 4036 | if (!isImplicit() || getDeclName()) |
| 4037 | return false; |
| 4038 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4039 | if (const auto *Record = getType()->getAs<RecordType>()) |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4040 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 4041 | |
| 4042 | return false; |
| 4043 | } |
| 4044 | |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4045 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 4046 | assert(isBitField() && "not a bitfield"); |
| Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 4047 | return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue(); |
| Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 4050 | bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const { |
| 4051 | return isUnnamedBitfield() && !getBitWidth()->isValueDependent() && |
| 4052 | getBitWidthValue(Ctx) == 0; |
| 4053 | } |
| 4054 | |
| Richard Smith | 78b239e | 2019-06-20 20:44:45 +0000 | [diff] [blame] | 4055 | bool FieldDecl::isZeroSize(const ASTContext &Ctx) const { |
| 4056 | if (isZeroLengthBitField(Ctx)) |
| 4057 | return true; |
| 4058 | |
| 4059 | // C++2a [intro.object]p7: |
| 4060 | // An object has nonzero size if it |
| 4061 | // -- is not a potentially-overlapping subobject, or |
| 4062 | if (!hasAttr<NoUniqueAddressAttr>()) |
| 4063 | return false; |
| 4064 | |
| 4065 | // -- is not of class type, or |
| 4066 | const auto *RT = getType()->getAs<RecordType>(); |
| 4067 | if (!RT) |
| 4068 | return false; |
| 4069 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 4070 | if (!RD) { |
| 4071 | assert(isInvalidDecl() && "valid field has incomplete type"); |
| 4072 | return false; |
| 4073 | } |
| 4074 | |
| 4075 | // -- [has] virtual member functions or virtual base classes, or |
| 4076 | // -- has subobjects of nonzero size or bit-fields of nonzero length |
| 4077 | const auto *CXXRD = cast<CXXRecordDecl>(RD); |
| 4078 | if (!CXXRD->isEmpty()) |
| 4079 | return false; |
| 4080 | |
| 4081 | // Otherwise, [...] the circumstances under which the object has zero size |
| 4082 | // are implementation-defined. |
| 4083 | // FIXME: This might be Itanium ABI specific; we don't yet know what the MS |
| 4084 | // ABI will do. |
| 4085 | return true; |
| 4086 | } |
| 4087 | |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 4088 | unsigned FieldDecl::getFieldIndex() const { |
| Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 4089 | const FieldDecl *Canonical = getCanonicalDecl(); |
| 4090 | if (Canonical != this) |
| 4091 | return Canonical->getFieldIndex(); |
| 4092 | |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 4093 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 4094 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4095 | unsigned Index = 0; |
| Richard Smith | 85567dd | 2017-11-15 01:33:46 +0000 | [diff] [blame] | 4096 | const RecordDecl *RD = getParent()->getDefinition(); |
| 4097 | assert(RD && "requested index for field of struct with no definition"); |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4098 | |
| Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 4099 | for (auto *Field : RD->fields()) { |
| 4100 | Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; |
| 4101 | ++Index; |
| 4102 | } |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 4103 | |
| Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 4104 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 4105 | return CachedFieldIndex - 1; |
| John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 4106 | } |
| 4107 | |
| Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 4108 | SourceRange FieldDecl::getSourceRange() const { |
| Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 4109 | const Expr *FinalExpr = getInClassInitializer(); |
| 4110 | if (!FinalExpr) |
| 4111 | FinalExpr = getBitWidth(); |
| 4112 | if (FinalExpr) |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4113 | return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc()); |
| Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 4114 | return DeclaratorDecl::getSourceRange(); |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { |
| David Blaikie | 11ab078 | 2014-10-31 17:18:09 +0000 | [diff] [blame] | 4118 | assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && |
| Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 4119 | "capturing type in non-lambda or captured record."); |
| Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 4120 | assert(InitStorage.getInt() == ISK_NoInit && |
| John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 4121 | InitStorage.getPointer() == nullptr && |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 4122 | "bit width, initializer or captured type already set"); |
| John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 4123 | InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType), |
| 4124 | ISK_CapturedVLAType); |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4127 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 4128 | // TagDecl Implementation |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4129 | //===----------------------------------------------------------------------===// |
| 4130 | |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4131 | TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
| 4132 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
| 4133 | SourceLocation StartL) |
| 4134 | : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C), |
| 4135 | TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) { |
| 4136 | assert((DK != Enum || TK == TTK_Enum) && |
| 4137 | "EnumDecl not matched with TTK_Enum"); |
| 4138 | setPreviousDecl(PrevDecl); |
| 4139 | setTagKind(TK); |
| 4140 | setCompleteDefinition(false); |
| 4141 | setBeingDefined(false); |
| 4142 | setEmbeddedInDeclarator(false); |
| 4143 | setFreeStanding(false); |
| 4144 | setCompleteDefinitionRequired(false); |
| 4145 | } |
| 4146 | |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 4147 | SourceLocation TagDecl::getOuterLocStart() const { |
| 4148 | return getTemplateOrInnerLocStart(this); |
| 4149 | } |
| 4150 | |
| Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 4151 | SourceRange TagDecl::getSourceRange() const { |
| Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 4152 | SourceLocation RBraceLoc = BraceRange.getEnd(); |
| Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 4153 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
| Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 4154 | return SourceRange(getOuterLocStart(), E); |
| Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
| Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 4157 | TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } |
| Argyrios Kyrtzidis | 5614aef | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 4158 | |
| Rafael Espindola | bf5c33b | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 4159 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 4160 | TypedefNameDeclOrQualifier = TDD; |
| Reid Kleckner | cae82a2 | 2014-04-23 22:03:04 +0000 | [diff] [blame] | 4161 | if (const Type *T = getTypeForDecl()) { |
| 4162 | (void)T; |
| Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 4163 | assert(T->isLinkageValid()); |
| Reid Kleckner | cae82a2 | 2014-04-23 22:03:04 +0000 | [diff] [blame] | 4164 | } |
| Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 4165 | assert(isLinkageValid()); |
| Douglas Gregor | a72a4e3 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 4168 | void TagDecl::startDefinition() { |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4169 | setBeingDefined(true); |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4170 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4171 | if (auto *D = dyn_cast<CXXRecordDecl>(this)) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4172 | struct CXXRecordDecl::DefinitionData *Data = |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4173 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 4174 | for (auto I : redecls()) |
| Richard Smith | 64c0630 | 2014-05-22 23:19:02 +0000 | [diff] [blame] | 4175 | cast<CXXRecordDecl>(I)->DefinitionData = Data; |
| John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4176 | } |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | void TagDecl::completeDefinition() { |
| John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4180 | assert((!isa<CXXRecordDecl>(this) || |
| 4181 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 4182 | "definition completed but not started"); |
| 4183 | |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4184 | setCompleteDefinition(true); |
| 4185 | setBeingDefined(false); |
| Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 4186 | |
| 4187 | if (ASTMutationListener *L = getASTMutationListener()) |
| 4188 | L->CompletedTagDefinition(this); |
| Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4191 | TagDecl *TagDecl::getDefinition() const { |
| 4192 | if (isCompleteDefinition()) |
| Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 4193 | return const_cast<TagDecl *>(this); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4194 | |
| 4195 | // 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] | 4196 | if (mayHaveOutOfDateDef()) { |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4197 | if (IdentifierInfo *II = getIdentifier()) { |
| 4198 | if (II->isOutOfDate()) { |
| 4199 | updateOutOfDate(*II); |
| 4200 | } |
| 4201 | } |
| 4202 | } |
| 4203 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4204 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
| Andrew Trick | ba266ee | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 4205 | return CXXRD->getDefinition(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4206 | |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 4207 | for (auto R : redecls()) |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4208 | if (R->isCompleteDefinition()) |
| Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 4209 | return R; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4211 | return nullptr; |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4214 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 4215 | if (QualifierLoc) { |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4216 | // Make sure the extended qualifier info is allocated. |
| 4217 | if (!hasExtInfo()) |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 4218 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4219 | // Set qualifier info. |
| Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4220 | getExtInfo()->QualifierLoc = QualifierLoc; |
| Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 4221 | } else { |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4222 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4223 | if (hasExtInfo()) { |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4224 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 4225 | getASTContext().Deallocate(getExtInfo()); |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 4226 | TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4227 | } |
| 4228 | else |
| 4229 | getExtInfo()->QualifierLoc = QualifierLoc; |
| John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4230 | } |
| 4231 | } |
| 4232 | } |
| 4233 | |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 4234 | void TagDecl::setTemplateParameterListsInfo( |
| 4235 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 4236 | assert(!TPLists.empty()); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4237 | // Make sure the extended decl info is allocated. |
| 4238 | if (!hasExtInfo()) |
| 4239 | // Allocate external info struct. |
| David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 4240 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4241 | // Set the template parameter lists info. |
| Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 4242 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
| Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4245 | //===----------------------------------------------------------------------===// |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4246 | // EnumDecl Implementation |
| 4247 | //===----------------------------------------------------------------------===// |
| 4248 | |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4249 | EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 4250 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
| 4251 | bool Scoped, bool ScopedUsingClassTag, bool Fixed) |
| 4252 | : TagDecl(Enum, TTK_Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
| 4253 | assert(Scoped || !ScopedUsingClassTag); |
| 4254 | IntegerType = nullptr; |
| 4255 | setNumPositiveBits(0); |
| 4256 | setNumNegativeBits(0); |
| 4257 | setScoped(Scoped); |
| 4258 | setScopedUsingClassTag(ScopedUsingClassTag); |
| 4259 | setFixed(Fixed); |
| 4260 | setHasODRHash(false); |
| 4261 | ODRHash = 0; |
| 4262 | } |
| 4263 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4264 | void EnumDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4265 | |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4266 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 4267 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4268 | IdentifierInfo *Id, |
| Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4269 | EnumDecl *PrevDecl, bool IsScoped, |
| 4270 | bool IsScopedUsingClassTag, bool IsFixed) { |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4271 | auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, |
| 4272 | IsScoped, IsScopedUsingClassTag, IsFixed); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4273 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4274 | C.getTypeDeclType(Enum, PrevDecl); |
| 4275 | return Enum; |
| 4276 | } |
| 4277 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4278 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4279 | EnumDecl *Enum = |
| 4280 | new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 4281 | nullptr, nullptr, false, false, false); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4282 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4283 | return Enum; |
| Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 4284 | } |
| 4285 | |
| Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 4286 | SourceRange EnumDecl::getIntegerTypeRange() const { |
| 4287 | if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) |
| 4288 | return TI->getTypeLoc().getSourceRange(); |
| 4289 | return SourceRange(); |
| 4290 | } |
| 4291 | |
| Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4292 | void EnumDecl::completeDefinition(QualType NewType, |
| John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4293 | QualType NewPromotionType, |
| 4294 | unsigned NumPositiveBits, |
| 4295 | unsigned NumNegativeBits) { |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4296 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4297 | if (!IntegerType) |
| 4298 | IntegerType = NewType.getTypePtr(); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4299 | PromotionType = NewPromotionType; |
| John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4300 | setNumPositiveBits(NumPositiveBits); |
| 4301 | setNumNegativeBits(NumNegativeBits); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4302 | TagDecl::completeDefinition(); |
| 4303 | } |
| 4304 | |
| Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 4305 | bool EnumDecl::isClosed() const { |
| 4306 | if (const auto *A = getAttr<EnumExtensibilityAttr>()) |
| 4307 | return A->getExtensibility() == EnumExtensibilityAttr::Closed; |
| 4308 | return true; |
| 4309 | } |
| 4310 | |
| 4311 | bool EnumDecl::isClosedFlag() const { |
| 4312 | return isClosed() && hasAttr<FlagEnumAttr>(); |
| 4313 | } |
| 4314 | |
| 4315 | bool EnumDecl::isClosedNonFlag() const { |
| 4316 | return isClosed() && !hasAttr<FlagEnumAttr>(); |
| 4317 | } |
| 4318 | |
| Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4319 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 4320 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 4321 | return MSI->getTemplateSpecializationKind(); |
| 4322 | |
| 4323 | return TSK_Undeclared; |
| 4324 | } |
| 4325 | |
| 4326 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 4327 | SourceLocation PointOfInstantiation) { |
| 4328 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 4329 | assert(MSI && "Not an instantiated member enumeration?"); |
| 4330 | MSI->setTemplateSpecializationKind(TSK); |
| 4331 | if (TSK != TSK_ExplicitSpecialization && |
| 4332 | PointOfInstantiation.isValid() && |
| 4333 | MSI->getPointOfInstantiation().isInvalid()) |
| 4334 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 4335 | } |
| 4336 | |
| Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4337 | EnumDecl *EnumDecl::getTemplateInstantiationPattern() const { |
| 4338 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
| 4339 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
| 4340 | EnumDecl *ED = getInstantiatedFromMemberEnum(); |
| 4341 | while (auto *NewED = ED->getInstantiatedFromMemberEnum()) |
| 4342 | ED = NewED; |
| Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 4343 | return getDefinitionOrSelf(ED); |
| Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | |
| 4347 | assert(!isTemplateInstantiation(getTemplateSpecializationKind()) && |
| 4348 | "couldn't find pattern for enum instantiation"); |
| 4349 | return nullptr; |
| 4350 | } |
| 4351 | |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4352 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 4353 | if (SpecializationInfo) |
| 4354 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 4355 | |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4356 | return nullptr; |
| Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 4360 | TemplateSpecializationKind TSK) { |
| 4361 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 4362 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 4363 | } |
| 4364 | |
| Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4365 | unsigned EnumDecl::getODRHash() { |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4366 | if (hasODRHash()) |
| Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4367 | return ODRHash; |
| 4368 | |
| 4369 | class ODRHash Hash; |
| 4370 | Hash.AddEnumDecl(this); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4371 | setHasODRHash(true); |
| Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4372 | ODRHash = Hash.CalculateHash(); |
| 4373 | return ODRHash; |
| 4374 | } |
| 4375 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4376 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 4377 | // RecordDecl Implementation |
| 4378 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 4379 | |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4380 | RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, |
| 4381 | DeclContext *DC, SourceLocation StartLoc, |
| 4382 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 4383 | RecordDecl *PrevDecl) |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4384 | : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
| 4385 | assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!"); |
| 4386 | setHasFlexibleArrayMember(false); |
| 4387 | setAnonymousStructOrUnion(false); |
| 4388 | setHasObjectMember(false); |
| 4389 | setHasVolatileMember(false); |
| 4390 | setHasLoadedFieldsFromExternalStorage(false); |
| 4391 | setNonTrivialToPrimitiveDefaultInitialize(false); |
| 4392 | setNonTrivialToPrimitiveCopy(false); |
| 4393 | setNonTrivialToPrimitiveDestroy(false); |
| Akira Hatanaka | 0905106 | 2019-09-07 00:34:43 +0000 | [diff] [blame] | 4394 | setHasNonTrivialToPrimitiveDefaultInitializeCUnion(false); |
| 4395 | setHasNonTrivialToPrimitiveDestructCUnion(false); |
| 4396 | setHasNonTrivialToPrimitiveCopyCUnion(false); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4397 | setParamDestroyedInCallee(false); |
| 4398 | setArgPassingRestrictions(APK_CanPassInRegs); |
| Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4401 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
| Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4402 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4403 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4404 | RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, |
| 4405 | StartLoc, IdLoc, Id, PrevDecl); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4406 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4407 | |
| Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4408 | C.getTypeDeclType(R, PrevDecl); |
| 4409 | return R; |
| Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4412 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4413 | RecordDecl *R = |
| 4414 | new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(), |
| 4415 | SourceLocation(), nullptr, nullptr); |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4416 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4417 | return R; |
| Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
| Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 4420 | bool RecordDecl::isInjectedClassName() const { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4421 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
| Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 4422 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 4423 | } |
| 4424 | |
| Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 4425 | bool RecordDecl::isLambda() const { |
| 4426 | if (auto RD = dyn_cast<CXXRecordDecl>(this)) |
| 4427 | return RD->isLambda(); |
| 4428 | return false; |
| 4429 | } |
| 4430 | |
| Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 4431 | bool RecordDecl::isCapturedRecord() const { |
| 4432 | return hasAttr<CapturedRecordAttr>(); |
| 4433 | } |
| 4434 | |
| 4435 | void RecordDecl::setCapturedRecord() { |
| 4436 | addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); |
| 4437 | } |
| 4438 | |
| Momchil Velikov | 102b410 | 2020-04-28 16:27:52 +0100 | [diff] [blame] | 4439 | bool RecordDecl::isOrContainsUnion() const { |
| 4440 | if (isUnion()) |
| 4441 | return true; |
| 4442 | |
| 4443 | if (const RecordDecl *Def = getDefinition()) { |
| 4444 | for (const FieldDecl *FD : Def->fields()) { |
| 4445 | const RecordType *RT = FD->getType()->getAs<RecordType>(); |
| 4446 | if (RT && RT->getDecl()->isOrContainsUnion()) |
| 4447 | return true; |
| 4448 | } |
| 4449 | } |
| 4450 | |
| 4451 | return false; |
| 4452 | } |
| 4453 | |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4454 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4455 | if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage()) |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4456 | LoadFieldsFromExternalStorage(); |
| 4457 | |
| 4458 | return field_iterator(decl_iterator(FirstDecl)); |
| 4459 | } |
| 4460 | |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 4461 | /// completeDefinition - Notes that the definition of this type is now |
| 4462 | /// complete. |
| 4463 | void RecordDecl::completeDefinition() { |
| John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4464 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
| Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 4465 | TagDecl::completeDefinition(); |
| 4466 | } |
| 4467 | |
| Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 4468 | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
| 4469 | /// This which can be turned on with an attribute, pragma, or the |
| 4470 | /// -mms-bitfields command-line option. |
| 4471 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
| David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 4472 | return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; |
| Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4475 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 4476 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 4477 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 4478 | |
| 4479 | // Notify that we have a RecordDecl doing some initialization. |
| 4480 | ExternalASTSource::Deserializing TheFields(Source); |
| 4481 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4482 | SmallVector<Decl*, 64> Decls; |
| Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4483 | setHasLoadedFieldsFromExternalStorage(true); |
| Richard Smith | 3cb1572 | 2015-08-05 22:41:45 +0000 | [diff] [blame] | 4484 | Source->FindExternalLexicalDecls(this, [](Decl::Kind K) { |
| 4485 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
| 4486 | }, Decls); |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4487 | |
| 4488 | #ifndef NDEBUG |
| 4489 | // Check that all decls we got were FieldDecls. |
| 4490 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
| Argyrios Kyrtzidis | f89a927 | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 4491 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4492 | #endif |
| 4493 | |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4494 | if (Decls.empty()) |
| 4495 | return; |
| 4496 | |
| Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 4497 | std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
| Argyrios Kyrtzidis | 094da73 | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 4498 | /*FieldsAlreadyLoaded=*/false); |
| Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4499 | } |
| 4500 | |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4501 | bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { |
| 4502 | ASTContext &Context = getASTContext(); |
| Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4503 | const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask & |
| 4504 | (SanitizerKind::Address | SanitizerKind::KernelAddress); |
| 4505 | if (!EnabledAsanMask || !Context.getLangOpts().SanitizeAddressFieldPadding) |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4506 | return false; |
| Alexey Samsonov | 33e00e2 | 2014-10-16 23:50:26 +0000 | [diff] [blame] | 4507 | const auto &Blacklist = Context.getSanitizerBlacklist(); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4508 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(this); |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4509 | // We may be able to relax some of these requirements. |
| 4510 | int ReasonToReject = -1; |
| 4511 | if (!CXXRD || CXXRD->isExternCContext()) |
| 4512 | ReasonToReject = 0; // is not C++. |
| 4513 | else if (CXXRD->hasAttr<PackedAttr>()) |
| 4514 | ReasonToReject = 1; // is packed. |
| 4515 | else if (CXXRD->isUnion()) |
| 4516 | ReasonToReject = 2; // is a union. |
| 4517 | else if (CXXRD->isTriviallyCopyable()) |
| 4518 | ReasonToReject = 3; // is trivially copyable. |
| 4519 | else if (CXXRD->hasTrivialDestructor()) |
| 4520 | ReasonToReject = 4; // has trivial destructor. |
| 4521 | else if (CXXRD->isStandardLayout()) |
| 4522 | ReasonToReject = 5; // is standard layout. |
| Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4523 | else if (Blacklist.isBlacklistedLocation(EnabledAsanMask, getLocation(), |
| 4524 | "field-padding")) |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4525 | ReasonToReject = 6; // is in a blacklisted file. |
| Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4526 | else if (Blacklist.isBlacklistedType(EnabledAsanMask, |
| 4527 | getQualifiedNameAsString(), |
| Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4528 | "field-padding")) |
| 4529 | ReasonToReject = 7; // is blacklisted. |
| 4530 | |
| 4531 | if (EmitRemark) { |
| 4532 | if (ReasonToReject >= 0) |
| 4533 | Context.getDiagnostics().Report( |
| 4534 | getLocation(), |
| 4535 | diag::remark_sanitize_address_insert_extra_padding_rejected) |
| 4536 | << getQualifiedNameAsString() << ReasonToReject; |
| 4537 | else |
| 4538 | Context.getDiagnostics().Report( |
| 4539 | getLocation(), |
| 4540 | diag::remark_sanitize_address_insert_extra_padding_accepted) |
| 4541 | << getQualifiedNameAsString(); |
| 4542 | } |
| 4543 | return ReasonToReject < 0; |
| 4544 | } |
| 4545 | |
| Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4546 | const FieldDecl *RecordDecl::findFirstNamedDataMember() const { |
| 4547 | for (const auto *I : fields()) { |
| 4548 | if (I->getIdentifier()) |
| 4549 | return I; |
| 4550 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4551 | if (const auto *RT = I->getType()->getAs<RecordType>()) |
| Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4552 | if (const FieldDecl *NamedDataMember = |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4553 | RT->getDecl()->findFirstNamedDataMember()) |
| Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4554 | return NamedDataMember; |
| 4555 | } |
| 4556 | |
| 4557 | // We didn't find a named data member. |
| 4558 | return nullptr; |
| 4559 | } |
| 4560 | |
| Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4561 | //===----------------------------------------------------------------------===// |
| 4562 | // BlockDecl Implementation |
| 4563 | //===----------------------------------------------------------------------===// |
| 4564 | |
| Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4565 | BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc) |
| 4566 | : Decl(Block, DC, CaretLoc), DeclContext(Block) { |
| 4567 | setIsVariadic(false); |
| 4568 | setCapturesCXXThis(false); |
| 4569 | setBlockMissingReturnType(true); |
| 4570 | setIsConversionFromLambda(false); |
| 4571 | setDoesNotEscape(false); |
| Akira Hatanaka | c5792aa | 2019-02-27 18:17:16 +0000 | [diff] [blame] | 4572 | setCanAvoidCopyToHeap(false); |
| Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
| Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4575 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4576 | assert(!ParamInfo && "Already has param info!"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4577 | |
| Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4578 | // Zero params -> null pointer. |
| David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4579 | if (!NewParamInfo.empty()) { |
| 4580 | NumParams = NewParamInfo.size(); |
| 4581 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 4582 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
| Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4583 | } |
| 4584 | } |
| 4585 | |
| Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4586 | void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
| 4587 | bool CapturesCXXThis) { |
| Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4588 | this->setCapturesCXXThis(CapturesCXXThis); |
| Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4589 | this->NumCaptures = Captures.size(); |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4590 | |
| Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4591 | if (Captures.empty()) { |
| 4592 | this->Captures = nullptr; |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4593 | return; |
| 4594 | } |
| 4595 | |
| Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4596 | this->Captures = Captures.copy(Context).data(); |
| Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4597 | } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4598 | |
| John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4599 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
| Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4600 | for (const auto &I : captures()) |
| John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4601 | // Only auto vars can be captured, so no redeclaration worries. |
| Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4602 | if (I.getVariable() == variable) |
| John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4603 | return true; |
| 4604 | |
| 4605 | return false; |
| 4606 | } |
| 4607 | |
| Douglas Gregor | 70226da | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 4608 | SourceRange BlockDecl::getSourceRange() const { |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4609 | return SourceRange(getLocation(), Body ? Body->getEndLoc() : getLocation()); |
| Douglas Gregor | 70226da | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 4610 | } |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4611 | |
| 4612 | //===----------------------------------------------------------------------===// |
| 4613 | // Other Decl Allocation/Deallocation Method Implementations |
| 4614 | //===----------------------------------------------------------------------===// |
| 4615 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4616 | void TranslationUnitDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4617 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4618 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4619 | return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4622 | void PragmaCommentDecl::anchor() {} |
| Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4623 | |
| 4624 | PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C, |
| 4625 | TranslationUnitDecl *DC, |
| 4626 | SourceLocation CommentLoc, |
| 4627 | PragmaMSCommentKind CommentKind, |
| 4628 | StringRef Arg) { |
| 4629 | PragmaCommentDecl *PCD = |
| 4630 | new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1)) |
| 4631 | PragmaCommentDecl(DC, CommentLoc, CommentKind); |
| 4632 | memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size()); |
| 4633 | PCD->getTrailingObjects<char>()[Arg.size()] = '\0'; |
| 4634 | return PCD; |
| 4635 | } |
| 4636 | |
| 4637 | PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C, |
| 4638 | unsigned ID, |
| 4639 | unsigned ArgSize) { |
| 4640 | return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1)) |
| 4641 | PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); |
| 4642 | } |
| 4643 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4644 | void PragmaDetectMismatchDecl::anchor() {} |
| Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 4645 | |
| 4646 | PragmaDetectMismatchDecl * |
| 4647 | PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, |
| 4648 | SourceLocation Loc, StringRef Name, |
| 4649 | StringRef Value) { |
| 4650 | size_t ValueStart = Name.size() + 1; |
| 4651 | PragmaDetectMismatchDecl *PDMD = |
| 4652 | new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1)) |
| 4653 | PragmaDetectMismatchDecl(DC, Loc, ValueStart); |
| 4654 | memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size()); |
| 4655 | PDMD->getTrailingObjects<char>()[Name.size()] = '\0'; |
| 4656 | memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(), |
| 4657 | Value.size()); |
| 4658 | PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0'; |
| 4659 | return PDMD; |
| 4660 | } |
| 4661 | |
| 4662 | PragmaDetectMismatchDecl * |
| 4663 | PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 4664 | unsigned NameValueSize) { |
| 4665 | return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1)) |
| 4666 | PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); |
| 4667 | } |
| Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4668 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4669 | void ExternCContextDecl::anchor() {} |
| Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 4670 | |
| 4671 | ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, |
| 4672 | TranslationUnitDecl *DC) { |
| 4673 | return new (C, DC) ExternCContextDecl(DC); |
| 4674 | } |
| 4675 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4676 | void LabelDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4677 | |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4678 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 4679 | SourceLocation IdentL, IdentifierInfo *II) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4680 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); |
| Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
| 4683 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 4684 | SourceLocation IdentL, IdentifierInfo *II, |
| 4685 | SourceLocation GnuLabelL) { |
| 4686 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4687 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4690 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4691 | return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, |
| 4692 | SourceLocation()); |
| Douglas Gregor | 417e87c | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 4693 | } |
| 4694 | |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 4695 | void LabelDecl::setMSAsmLabel(StringRef Name) { |
| Saar Raz | fdf80e8 | 2019-12-06 01:30:21 +0200 | [diff] [blame] | 4696 | char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; |
| Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 4697 | memcpy(Buffer, Name.data(), Name.size()); |
| 4698 | Buffer[Name.size()] = '\0'; |
| 4699 | MSAsmName = Buffer; |
| 4700 | } |
| 4701 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4702 | void ValueDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4703 | |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 4704 | bool ValueDecl::isWeak() const { |
| Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 4705 | for (const auto *I : attrs()) |
| 4706 | if (isa<WeakAttr>(I) || isa<WeakRefAttr>(I)) |
| Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 4707 | return true; |
| 4708 | |
| 4709 | return isWeakImported(); |
| 4710 | } |
| 4711 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4712 | void ImplicitParamDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4713 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4714 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4715 | SourceLocation IdLoc, |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4716 | IdentifierInfo *Id, QualType Type, |
| 4717 | ImplicitParamKind ParamKind) { |
| 4718 | return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind); |
| 4719 | } |
| 4720 | |
| 4721 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type, |
| 4722 | ImplicitParamKind ParamKind) { |
| 4723 | return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4726 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4727 | unsigned ID) { |
| Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4728 | return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4731 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4732 | SourceLocation StartLoc, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4733 | const DeclarationNameInfo &NameInfo, |
| 4734 | QualType T, TypeSourceInfo *TInfo, |
| Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame] | 4735 | StorageClass SC, bool isInlineSpecified, |
| Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 4736 | bool hasWrittenPrototype, |
| Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 4737 | ConstexprSpecKind ConstexprKind, |
| 4738 | Expr *TrailingRequiresClause) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4739 | FunctionDecl *New = |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4740 | new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo, |
| Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 4741 | SC, isInlineSpecified, ConstexprKind, |
| 4742 | TrailingRequiresClause); |
| Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 4743 | New->setHasWrittenPrototype(hasWrittenPrototype); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4744 | return New; |
| 4745 | } |
| 4746 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4747 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4748 | return new (C, ID) FunctionDecl(Function, C, nullptr, SourceLocation(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4749 | DeclarationNameInfo(), QualType(), nullptr, |
| Saar Raz | b65b1f3 | 2020-01-09 15:07:51 +0200 | [diff] [blame] | 4750 | SC_None, false, CSK_unspecified, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4753 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4754 | return new (C, DC) BlockDecl(DC, L); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4757 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4758 | return new (C, ID) BlockDecl(nullptr, SourceLocation()); |
| John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4759 | } |
| 4760 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4761 | CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams) |
| 4762 | : Decl(Captured, DC, SourceLocation()), DeclContext(Captured), |
| 4763 | NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {} |
| 4764 | |
| Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4765 | CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, |
| 4766 | unsigned NumParams) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4767 | return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4768 | CapturedDecl(DC, NumParams); |
| Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
| Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 4771 | CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4772 | unsigned NumParams) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4773 | return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4774 | CapturedDecl(nullptr, NumParams); |
| Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
| Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4777 | Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); } |
| 4778 | void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } |
| 4779 | |
| 4780 | bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); } |
| 4781 | void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); } |
| 4782 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4783 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 4784 | SourceLocation L, |
| 4785 | IdentifierInfo *Id, QualType T, |
| 4786 | Expr *E, const llvm::APSInt &V) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4787 | return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4790 | EnumConstantDecl * |
| 4791 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4792 | return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr, |
| 4793 | QualType(), nullptr, llvm::APSInt()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4796 | void IndirectFieldDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4797 | |
| Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4798 | IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC, |
| 4799 | SourceLocation L, DeclarationName N, |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4800 | QualType T, |
| 4801 | MutableArrayRef<NamedDecl *> CH) |
| 4802 | : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()), |
| 4803 | ChainingSize(CH.size()) { |
| Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4804 | // In C++, indirect field declarations conflict with tag declarations in the |
| 4805 | // same scope, so add them to IDNS_Tag so that tag redeclaration finds them. |
| 4806 | if (C.getLangOpts().CPlusPlus) |
| 4807 | IdentifierNamespace |= IDNS_Tag; |
| 4808 | } |
| 4809 | |
| Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4810 | IndirectFieldDecl * |
| 4811 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4812 | IdentifierInfo *Id, QualType T, |
| 4813 | llvm::MutableArrayRef<NamedDecl *> CH) { |
| 4814 | return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4815 | } |
| 4816 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4817 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 4818 | unsigned ID) { |
| Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4819 | return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(), |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4820 | DeclarationName(), QualType(), None); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
| Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 4823 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 4824 | SourceLocation End = getLocation(); |
| 4825 | if (Init) |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4826 | End = Init->getEndLoc(); |
| Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 4827 | return SourceRange(getLocation(), End); |
| 4828 | } |
| 4829 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4830 | void TypeDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4831 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4832 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 4833 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4834 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4835 | return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4838 | void TypedefNameDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4839 | |
| Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4840 | TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { |
| 4841 | if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { |
| 4842 | auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); |
| 4843 | auto *ThisTypedef = this; |
| 4844 | if (AnyRedecl && OwningTypedef) { |
| 4845 | OwningTypedef = OwningTypedef->getCanonicalDecl(); |
| 4846 | ThisTypedef = ThisTypedef->getCanonicalDecl(); |
| 4847 | } |
| 4848 | if (OwningTypedef == ThisTypedef) |
| Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 4849 | return TT->getDecl(); |
| Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4850 | } |
| Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 4851 | |
| 4852 | return nullptr; |
| 4853 | } |
| 4854 | |
| Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 4855 | bool TypedefNameDecl::isTransparentTagSlow() const { |
| 4856 | auto determineIsTransparent = [&]() { |
| 4857 | if (auto *TT = getUnderlyingType()->getAs<TagType>()) { |
| 4858 | if (auto *TD = TT->getDecl()) { |
| 4859 | if (TD->getName() != getName()) |
| 4860 | return false; |
| 4861 | SourceLocation TTLoc = getLocation(); |
| 4862 | SourceLocation TDLoc = TD->getLocation(); |
| 4863 | if (!TTLoc.isMacroID() || !TDLoc.isMacroID()) |
| 4864 | return false; |
| 4865 | SourceManager &SM = getASTContext().getSourceManager(); |
| 4866 | return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc); |
| 4867 | } |
| 4868 | } |
| 4869 | return false; |
| 4870 | }; |
| 4871 | |
| 4872 | bool isTransparent = determineIsTransparent(); |
| Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 4873 | MaybeModedTInfo.setInt((isTransparent << 1) | 1); |
| Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 4874 | return isTransparent; |
| 4875 | } |
| 4876 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4877 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4878 | return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4879 | nullptr, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4882 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 4883 | SourceLocation StartLoc, |
| 4884 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 4885 | TypeSourceInfo *TInfo) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4886 | return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4889 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4890 | return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), |
| 4891 | SourceLocation(), nullptr, nullptr); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 4894 | SourceRange TypedefDecl::getSourceRange() const { |
| 4895 | SourceLocation RangeEnd = getLocation(); |
| 4896 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 4897 | if (typeIsPostfix(TInfo->getType())) |
| 4898 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 4899 | } |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4900 | return SourceRange(getBeginLoc(), RangeEnd); |
| Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4903 | SourceRange TypeAliasDecl::getSourceRange() const { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4904 | SourceLocation RangeEnd = getBeginLoc(); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4905 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 4906 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4907 | return SourceRange(getBeginLoc(), RangeEnd); |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4910 | void FileScopeAsmDecl::anchor() {} |
| David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4911 | |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4912 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
| Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 4913 | StringLiteral *Str, |
| 4914 | SourceLocation AsmLoc, |
| 4915 | SourceLocation RParenLoc) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4916 | return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
| Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4917 | } |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4918 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4919 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4920 | unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4921 | return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), |
| 4922 | SourceLocation()); |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4925 | void EmptyDecl::anchor() {} |
| 4926 | |
| 4927 | EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4928 | return new (C, DC) EmptyDecl(DC, L); |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4932 | return new (C, ID) EmptyDecl(nullptr, SourceLocation()); |
| Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4935 | //===----------------------------------------------------------------------===// |
| 4936 | // ImportDecl Implementation |
| 4937 | //===----------------------------------------------------------------------===// |
| 4938 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4939 | /// Retrieve the number of module identifiers needed to name the given |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4940 | /// module. |
| 4941 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 4942 | unsigned Result = 1; |
| 4943 | while (Mod->Parent) { |
| 4944 | Mod = Mod->Parent; |
| 4945 | ++Result; |
| 4946 | } |
| 4947 | return Result; |
| 4948 | } |
| 4949 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4950 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4951 | Module *Imported, |
| 4952 | ArrayRef<SourceLocation> IdentifierLocs) |
| Reid Kleckner | c915cb9 | 2020-02-27 18:13:54 -0800 | [diff] [blame] | 4953 | : Decl(Import, DC, StartLoc), ImportedModule(Imported), |
| 4954 | NextLocalImportAndComplete(nullptr, true) { |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4955 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4956 | auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4957 | std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(), |
| 4958 | StoredLocs); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4961 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4962 | Module *Imported, SourceLocation EndLoc) |
| Reid Kleckner | c915cb9 | 2020-02-27 18:13:54 -0800 | [diff] [blame] | 4963 | : Decl(Import, DC, StartLoc), ImportedModule(Imported), |
| 4964 | NextLocalImportAndComplete(nullptr, false) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4965 | *getTrailingObjects<SourceLocation>() = EndLoc; |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4968 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
| Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4969 | SourceLocation StartLoc, Module *Imported, |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4970 | ArrayRef<SourceLocation> IdentifierLocs) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4971 | return new (C, DC, |
| 4972 | additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size())) |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4973 | ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4976 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
| Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4977 | SourceLocation StartLoc, |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4978 | Module *Imported, |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4979 | SourceLocation EndLoc) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4980 | ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1)) |
| 4981 | ImportDecl(DC, StartLoc, Imported, EndLoc); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4982 | Import->setImplicit(); |
| 4983 | return Import; |
| 4984 | } |
| 4985 | |
| Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4986 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 4987 | unsigned NumLocations) { |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4988 | return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations)) |
| Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4989 | ImportDecl(EmptyShell()); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
| 4992 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| Reid Kleckner | c915cb9 | 2020-02-27 18:13:54 -0800 | [diff] [blame] | 4993 | if (!isImportComplete()) |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4994 | return None; |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4995 | |
| Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4996 | const auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
| Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4997 | return llvm::makeArrayRef(StoredLocs, |
| 4998 | getNumModuleIdentifiers(getImportedModule())); |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4999 | } |
| 5000 | |
| 5001 | SourceRange ImportDecl::getSourceRange() const { |
| Reid Kleckner | c915cb9 | 2020-02-27 18:13:54 -0800 | [diff] [blame] | 5002 | if (!isImportComplete()) |
| James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 5003 | return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>()); |
| 5004 | |
| Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 5005 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 5006 | } |
| Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 5007 | |
| 5008 | //===----------------------------------------------------------------------===// |
| 5009 | // ExportDecl Implementation |
| 5010 | //===----------------------------------------------------------------------===// |
| 5011 | |
| 5012 | void ExportDecl::anchor() {} |
| 5013 | |
| 5014 | ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC, |
| 5015 | SourceLocation ExportLoc) { |
| 5016 | return new (C, DC) ExportDecl(DC, ExportLoc); |
| 5017 | } |
| 5018 | |
| 5019 | ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 5020 | return new (C, ID) ExportDecl(nullptr, SourceLocation()); |
| 5021 | } |