Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1 | //===- Decl.cpp - Declaration AST Node Implementation ---------------------===// |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Argyrios Kyrtzidis | 6301884 | 2008-06-04 13:04:04 +0000 | [diff] [blame] | 9 | // This file implements the Decl subclasses. |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/Decl.h" |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 14 | #include "Linkage.h" |
Chris Lattner | a7b3287 | 2008-03-15 06:12:44 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Richard Trieu | 0f25c74 | 2018-12-14 03:35:10 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTDiagnostic.h" |
Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTLambda.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTMutationListener.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 19 | #include "clang/AST/CanonicalType.h" |
| 20 | #include "clang/AST/DeclBase.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
| 22 | #include "clang/AST/DeclObjC.h" |
Alexey Bataev | 94a4f0c | 2016-03-03 05:21:39 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclOpenMP.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclTemplate.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclarationName.h" |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 26 | #include "clang/AST/Expr.h" |
Anders Carlsson | 714d096 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 27 | #include "clang/AST/ExprCXX.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExternalASTSource.h" |
Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 29 | #include "clang/AST/ODRHash.h" |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 30 | #include "clang/AST/PrettyDeclStackTrace.h" |
Douglas Gregor | 7de5966 | 2009-05-29 20:38:28 +0000 | [diff] [blame] | 31 | #include "clang/AST/PrettyPrinter.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 32 | #include "clang/AST/Redeclarable.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 33 | #include "clang/AST/Stmt.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 34 | #include "clang/AST/TemplateBase.h" |
| 35 | #include "clang/AST/Type.h" |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 36 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 15ba949 | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 37 | #include "clang/Basic/Builtins.h" |
Daniel Dunbar | 221fa94 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 38 | #include "clang/Basic/IdentifierTable.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 39 | #include "clang/Basic/LLVM.h" |
| 40 | #include "clang/Basic/LangOptions.h" |
| 41 | #include "clang/Basic/Linkage.h" |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 42 | #include "clang/Basic/Module.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 43 | #include "clang/Basic/PartialDiagnostic.h" |
| 44 | #include "clang/Basic/SanitizerBlacklist.h" |
| 45 | #include "clang/Basic/Sanitizers.h" |
| 46 | #include "clang/Basic/SourceLocation.h" |
| 47 | #include "clang/Basic/SourceManager.h" |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 48 | #include "clang/Basic/Specifiers.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 49 | #include "clang/Basic/TargetCXXABI.h" |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 50 | #include "clang/Basic/TargetInfo.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 51 | #include "clang/Basic/Visibility.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/APSInt.h" |
| 53 | #include "llvm/ADT/ArrayRef.h" |
| 54 | #include "llvm/ADT/None.h" |
| 55 | #include "llvm/ADT/Optional.h" |
| 56 | #include "llvm/ADT/STLExtras.h" |
| 57 | #include "llvm/ADT/SmallVector.h" |
| 58 | #include "llvm/ADT/StringSwitch.h" |
| 59 | #include "llvm/ADT/StringRef.h" |
| 60 | #include "llvm/ADT/Triple.h" |
| 61 | #include "llvm/Support/Casting.h" |
John McCall | 06f6fe8d | 2009-09-04 01:14:41 +0000 | [diff] [blame] | 62 | #include "llvm/Support/ErrorHandling.h" |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 63 | #include "llvm/Support/raw_ostream.h" |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 64 | #include <algorithm> |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 65 | #include <cassert> |
| 66 | #include <cstddef> |
| 67 | #include <cstring> |
| 68 | #include <memory> |
| 69 | #include <string> |
| 70 | #include <tuple> |
| 71 | #include <type_traits> |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 6d9a685 | 2006-10-25 05:11:20 +0000 | [diff] [blame] | 73 | using namespace clang; |
Chris Lattner | a11999d | 2006-10-15 22:34:45 +0000 | [diff] [blame] | 74 | |
Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 75 | Decl *clang::getPrimaryMergedDecl(Decl *D) { |
| 76 | return D->getASTContext().getPrimaryMergedDecl(D); |
| 77 | } |
| 78 | |
Jordan Rose | 1e879d8 | 2018-03-23 00:07:18 +0000 | [diff] [blame] | 79 | void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { |
| 80 | SourceLocation Loc = this->Loc; |
| 81 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
| 82 | if (Loc.isValid()) { |
| 83 | Loc.print(OS, Context.getSourceManager()); |
| 84 | OS << ": "; |
| 85 | } |
| 86 | OS << Message; |
| 87 | |
| 88 | if (auto *ND = dyn_cast_or_null<NamedDecl>(TheDecl)) { |
| 89 | OS << " '"; |
| 90 | ND->getNameForDiagnostic(OS, Context.getPrintingPolicy(), true); |
| 91 | OS << "'"; |
| 92 | } |
| 93 | |
| 94 | OS << '\n'; |
| 95 | } |
| 96 | |
Richard Smith | 73b21d8 | 2014-09-03 02:33:22 +0000 | [diff] [blame] | 97 | // Defined here so that it can be inlined into its direct callers. |
| 98 | bool Decl::isOutOfLine() const { |
| 99 | return !getLexicalDeclContext()->Equals(getDeclContext()); |
| 100 | } |
| 101 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 102 | TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx) |
| 103 | : Decl(TranslationUnit, nullptr, SourceLocation()), |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 104 | DeclContext(TranslationUnit), Ctx(ctx) {} |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 88f70d6 | 2008-03-15 05:43:15 +0000 | [diff] [blame] | 106 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 107 | // NamedDecl Implementation |
Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 108 | //===----------------------------------------------------------------------===// |
| 109 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 110 | // Visibility rules aren't rigorously externally specified, but here |
| 111 | // are the basic principles behind what we implement: |
| 112 | // |
| 113 | // 1. An explicit visibility attribute is generally a direct expression |
| 114 | // of the user's intent and should be honored. Only the innermost |
| 115 | // visibility attribute applies. If no visibility attribute applies, |
| 116 | // global visibility settings are considered. |
| 117 | // |
| 118 | // 2. There is one caveat to the above: on or in a template pattern, |
| 119 | // an explicit visibility attribute is just a default rule, and |
| 120 | // visibility can be decreased by the visibility of template |
| 121 | // arguments. But this, too, has an exception: an attribute on an |
| 122 | // explicit specialization or instantiation causes all the visibility |
| 123 | // restrictions of the template arguments to be ignored. |
| 124 | // |
| 125 | // 3. A variable that does not otherwise have explicit visibility can |
| 126 | // be restricted by the visibility of its type. |
| 127 | // |
| 128 | // 4. A visibility restriction is explicit if it comes from an |
| 129 | // attribute (or something like it), not a global visibility setting. |
| 130 | // When emitting a reference to an external symbol, visibility |
| 131 | // restrictions are ignored unless they are explicit. |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 132 | // |
| 133 | // 5. When computing the visibility of a non-type, including a |
| 134 | // non-type member of a class, only non-type visibility restrictions |
| 135 | // are considered: the 'visibility' attribute, global value-visibility |
| 136 | // settings, and a few special cases like __private_extern. |
| 137 | // |
| 138 | // 6. When computing the visibility of a type, including a type member |
| 139 | // of a class, only type visibility restrictions are considered: |
| 140 | // the 'type_visibility' attribute and global type-visibility settings. |
| 141 | // However, a 'visibility' attribute counts as a 'type_visibility' |
| 142 | // attribute on any declaration that only has the former. |
| 143 | // |
| 144 | // The visibility of a "secondary" entity, like a template argument, |
| 145 | // is computed using the kind of that entity, not the kind of the |
| 146 | // primary entity for which we are computing visibility. For example, |
| 147 | // the visibility of a specialization of either of these templates: |
| 148 | // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); |
| 149 | // template <class T, bool (&compare)(T, X)> class matcher; |
| 150 | // is restricted according to the type visibility of the argument 'T', |
| 151 | // the type visibility of 'bool(&)(T,X)', and the value visibility of |
| 152 | // the argument function 'compare'. That 'has_match' is a value |
| 153 | // and 'matcher' is a type only matters when looking for attributes |
| 154 | // and settings from the immediate context. |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 155 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 156 | /// Does this computation kind permit us to consider additional |
| 157 | /// visibility settings from attributes and the like? |
| 158 | static bool hasExplicitVisibilityAlready(LVComputationKind computation) { |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 159 | return computation.IgnoreExplicitVisibility; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /// Given an LVComputationKind, return one of the same type/value sort |
| 163 | /// that records that it already has explicit visibility. |
| 164 | static LVComputationKind |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 165 | withExplicitVisibilityAlready(LVComputationKind Kind) { |
| 166 | Kind.IgnoreExplicitVisibility = true; |
| 167 | return Kind; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 168 | } |
| 169 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 170 | static Optional<Visibility> getExplicitVisibility(const NamedDecl *D, |
| 171 | LVComputationKind kind) { |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 172 | assert(!kind.IgnoreExplicitVisibility && |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 173 | "asking for explicit visibility when we shouldn't be"); |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 174 | return D->getExplicitVisibility(kind.getExplicitVisibilityKind()); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 175 | } |
| 176 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 177 | /// Is the given declaration a "type" or a "value" for the purposes of |
| 178 | /// visibility computation? |
| 179 | static bool usesTypeVisibility(const NamedDecl *D) { |
John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 180 | return isa<TypeDecl>(D) || |
| 181 | isa<ClassTemplateDecl>(D) || |
| 182 | isa<ObjCInterfaceDecl>(D); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 183 | } |
| 184 | |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 185 | /// Does the given declaration have member specialization information, |
| 186 | /// and if so, is it an explicit specialization? |
| 187 | template <class T> static typename |
Benjamin Kramer | ed2f476 | 2014-03-07 14:30:23 +0000 | [diff] [blame] | 188 | std::enable_if<!std::is_base_of<RedeclarableTemplateDecl, T>::value, bool>::type |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 189 | isExplicitMemberSpecialization(const T *D) { |
| 190 | if (const MemberSpecializationInfo *member = |
| 191 | D->getMemberSpecializationInfo()) { |
| 192 | return member->isExplicitSpecialization(); |
| 193 | } |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | /// For templates, this question is easier: a member template can't be |
| 198 | /// explicitly instantiated, so there's a single bit indicating whether |
| 199 | /// or not this is an explicit member specialization. |
| 200 | static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { |
| 201 | return D->isMemberSpecialization(); |
| 202 | } |
| 203 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 204 | /// Given a visibility attribute, return the explicit visibility |
| 205 | /// associated with it. |
| 206 | template <class T> |
| 207 | static Visibility getVisibilityFromAttr(const T *attr) { |
| 208 | switch (attr->getVisibility()) { |
| 209 | case T::Default: |
| 210 | return DefaultVisibility; |
| 211 | case T::Hidden: |
| 212 | return HiddenVisibility; |
| 213 | case T::Protected: |
| 214 | return ProtectedVisibility; |
| 215 | } |
| 216 | llvm_unreachable("bad visibility kind"); |
| 217 | } |
| 218 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 219 | /// Return the explicit visibility of the given declaration. |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 220 | static Optional<Visibility> getVisibilityOf(const NamedDecl *D, |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 221 | NamedDecl::ExplicitVisibilityKind kind) { |
| 222 | // If we're ultimately computing the visibility of a type, look for |
| 223 | // a 'type_visibility' attribute before looking for 'visibility'. |
| 224 | if (kind == NamedDecl::VisibilityForType) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 225 | if (const auto *A = D->getAttr<TypeVisibilityAttr>()) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 226 | return getVisibilityFromAttr(A); |
| 227 | } |
| 228 | } |
| 229 | |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 230 | // If this declaration has an explicit visibility attribute, use it. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 231 | if (const auto *A = D->getAttr<VisibilityAttr>()) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 232 | return getVisibilityFromAttr(A); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 233 | } |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 234 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 235 | return None; |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 236 | } |
| 237 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 238 | LinkageInfo LinkageComputer::getLVForType(const Type &T, |
| 239 | LVComputationKind computation) { |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 240 | if (computation.IgnoreAllVisibility) |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 241 | return LinkageInfo(T.getLinkage(), DefaultVisibility, true); |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 242 | return getTypeLinkageAndVisibility(&T); |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 245 | /// Get the most restrictive linkage for the types in the given |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 246 | /// template parameter list. For visibility purposes, template |
| 247 | /// parameters are part of the signature of a template. |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 248 | LinkageInfo LinkageComputer::getLVForTemplateParameterList( |
| 249 | const TemplateParameterList *Params, LVComputationKind computation) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 250 | LinkageInfo LV; |
David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 251 | for (const NamedDecl *P : *Params) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 252 | // Template type parameters are the most common and never |
| 253 | // contribute to visibility, pack or not. |
David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 254 | if (isa<TemplateTypeParmDecl>(P)) |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 255 | continue; |
| 256 | |
| 257 | // Non-type template parameters can be restricted by the value type, e.g. |
| 258 | // template <enum X> class A { ... }; |
| 259 | // We have to be careful here, though, because we can be dealing with |
| 260 | // dependent types. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 261 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(P)) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 262 | // Handle the non-pack case first. |
| 263 | if (!NTTP->isExpandedParameterPack()) { |
| 264 | if (!NTTP->getType()->isDependentType()) { |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 265 | LV.merge(getLVForType(*NTTP->getType(), computation)); |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 266 | } |
| 267 | continue; |
| 268 | } |
Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 269 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 270 | // Look at all the types in an expanded pack. |
| 271 | for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { |
| 272 | QualType type = NTTP->getExpansionType(i); |
| 273 | if (!type->isDependentType()) |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 274 | LV.merge(getTypeLinkageAndVisibility(type)); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 275 | } |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 276 | continue; |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 277 | } |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 278 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 279 | // Template template parameters can be restricted by their |
| 280 | // template parameters, recursively. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 281 | const auto *TTP = cast<TemplateTemplateParmDecl>(P); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 282 | |
| 283 | // Handle the non-pack case first. |
| 284 | if (!TTP->isExpandedParameterPack()) { |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 285 | LV.merge(getLVForTemplateParameterList(TTP->getTemplateParameters(), |
| 286 | computation)); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | |
| 290 | // Look at all expansions in an expanded pack. |
| 291 | for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); |
| 292 | i != n; ++i) { |
| 293 | LV.merge(getLVForTemplateParameterList( |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 294 | TTP->getExpansionTemplateParameters(i), computation)); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 298 | return LV; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 301 | static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 302 | const Decl *Ret = nullptr; |
Rafael Espindola | c1b38a2 | 2013-05-16 04:30:21 +0000 | [diff] [blame] | 303 | const DeclContext *DC = D->getDeclContext(); |
| 304 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 305 | if (isa<FunctionDecl>(DC) || isa<BlockDecl>(DC)) |
| 306 | Ret = cast<Decl>(DC); |
Rafael Espindola | c1b38a2 | 2013-05-16 04:30:21 +0000 | [diff] [blame] | 307 | DC = DC->getParent(); |
| 308 | } |
| 309 | return Ret; |
| 310 | } |
| 311 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 312 | /// Get the most restrictive linkage for the types and |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 313 | /// declarations in the given template argument list. |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 314 | /// |
| 315 | /// Note that we don't take an LVComputationKind because we always |
| 316 | /// want to honor the visibility of template arguments in the same way. |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 317 | LinkageInfo |
| 318 | LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, |
| 319 | LVComputationKind computation) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 320 | LinkageInfo LV; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 321 | |
David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 322 | for (const TemplateArgument &Arg : Args) { |
| 323 | switch (Arg.getKind()) { |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 324 | case TemplateArgument::Null: |
| 325 | case TemplateArgument::Integral: |
| 326 | case TemplateArgument::Expression: |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 327 | continue; |
Rafael Espindola | eeb9d9f | 2012-01-02 06:26:22 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 329 | case TemplateArgument::Type: |
David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 330 | LV.merge(getLVForType(*Arg.getAsType(), computation)); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 331 | continue; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 332 | |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 333 | case TemplateArgument::Declaration: { |
| 334 | const NamedDecl *ND = Arg.getAsDecl(); |
| 335 | assert(!usesTypeVisibility(ND)); |
| 336 | LV.merge(getLVForDecl(ND, computation)); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 337 | continue; |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 338 | } |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 339 | |
| 340 | case TemplateArgument::NullPtr: |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 341 | LV.merge(getTypeLinkageAndVisibility(Arg.getNullPtrType())); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 342 | continue; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 343 | |
| 344 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 345 | case TemplateArgument::TemplateExpansion: |
David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 346 | if (TemplateDecl *Template = |
| 347 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl()) |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 348 | LV.merge(getLVForDecl(Template, computation)); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 349 | continue; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 350 | |
| 351 | case TemplateArgument::Pack: |
David Majnemer | c7b85c4 | 2014-04-23 05:16:48 +0000 | [diff] [blame] | 352 | LV.merge(getLVForTemplateArgumentList(Arg.getPackAsArray(), computation)); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 353 | continue; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 354 | } |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 355 | llvm_unreachable("bad template argument kind"); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 356 | } |
| 357 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 358 | return LV; |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 359 | } |
| 360 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 361 | LinkageInfo |
| 362 | LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
| 363 | LVComputationKind computation) { |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 364 | return getLVForTemplateArgumentList(TArgs.asArray(), computation); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 365 | } |
| 366 | |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 367 | static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, |
| 368 | const FunctionTemplateSpecializationInfo *specInfo) { |
| 369 | // Include visibility from the template parameters and arguments |
| 370 | // only if this is not an explicit instantiation or specialization |
| 371 | // with direct explicit visibility. (Implicit instantiations won't |
| 372 | // have a direct attribute.) |
| 373 | if (!specInfo->isExplicitInstantiationOrSpecialization()) |
| 374 | return true; |
| 375 | |
| 376 | return !fn->hasAttr<VisibilityAttr>(); |
| 377 | } |
| 378 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 379 | /// Merge in template-related linkage and visibility for the given |
| 380 | /// function template specialization. |
| 381 | /// |
| 382 | /// We don't need a computation kind here because we can assume |
| 383 | /// LVForValue. |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 384 | /// |
NAKAMURA Takumi | 62eae08 | 2013-02-22 04:06:28 +0000 | [diff] [blame] | 385 | /// \param[out] LV the computation to use for the parent |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 386 | void LinkageComputer::mergeTemplateLV( |
| 387 | LinkageInfo &LV, const FunctionDecl *fn, |
| 388 | const FunctionTemplateSpecializationInfo *specInfo, |
| 389 | LVComputationKind computation) { |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 390 | bool considerVisibility = |
| 391 | shouldConsiderTemplateVisibility(fn, specInfo); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 392 | |
| 393 | // Merge information from the template parameters. |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 394 | FunctionTemplateDecl *temp = specInfo->getTemplate(); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 395 | LinkageInfo tempLV = |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 396 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 397 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 398 | |
| 399 | // Merge information from the template arguments. |
| 400 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 401 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 402 | LV.mergeMaybeWithVisibility(argsLV, considerVisibility); |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 403 | } |
| 404 | |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 405 | /// Does the given declaration have a direct visibility attribute |
| 406 | /// that would match the given rules? |
| 407 | static bool hasDirectVisibilityAttribute(const NamedDecl *D, |
| 408 | LVComputationKind computation) { |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 409 | if (computation.IgnoreAllVisibility) |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 410 | return false; |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 411 | |
| 412 | return (computation.isTypeVisibility() && D->hasAttr<TypeVisibilityAttr>()) || |
| 413 | D->hasAttr<VisibilityAttr>(); |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 414 | } |
| 415 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 416 | /// Should we consider visibility associated with the template |
| 417 | /// arguments and parameters of the given class template specialization? |
| 418 | static bool shouldConsiderTemplateVisibility( |
| 419 | const ClassTemplateSpecializationDecl *spec, |
| 420 | LVComputationKind computation) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 421 | // Include visibility from the template parameters and arguments |
| 422 | // only if this is not an explicit instantiation or specialization |
| 423 | // with direct explicit visibility (and note that implicit |
| 424 | // instantiations won't have a direct attribute). |
| 425 | // |
| 426 | // Furthermore, we want to ignore template parameters and arguments |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 427 | // for an explicit specialization when computing the visibility of a |
| 428 | // member thereof with explicit visibility. |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 429 | // |
| 430 | // This is a bit complex; let's unpack it. |
| 431 | // |
| 432 | // An explicit class specialization is an independent, top-level |
| 433 | // declaration. As such, if it or any of its members has an |
| 434 | // explicit visibility attribute, that must directly express the |
| 435 | // user's intent, and we should honor it. The same logic applies to |
| 436 | // an explicit instantiation of a member of such a thing. |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 437 | |
| 438 | // Fast path: if this is not an explicit instantiation or |
| 439 | // specialization, we always want to consider template-related |
| 440 | // visibility restrictions. |
| 441 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 442 | return true; |
| 443 | |
| 444 | // This is the 'member thereof' check. |
| 445 | if (spec->isExplicitSpecialization() && |
| 446 | hasExplicitVisibilityAlready(computation)) |
| 447 | return false; |
| 448 | |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 449 | return !hasDirectVisibilityAttribute(spec, computation); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /// Merge in template-related linkage and visibility for the given |
| 453 | /// class template specialization. |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 454 | void LinkageComputer::mergeTemplateLV( |
| 455 | LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec, |
| 456 | LVComputationKind computation) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 457 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 458 | |
| 459 | // Merge information from the template parameters, but ignore |
| 460 | // visibility if we're only considering template arguments. |
| 461 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 462 | ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 463 | LinkageInfo tempLV = |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 464 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 465 | LV.mergeMaybeWithVisibility(tempLV, |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 466 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 467 | |
| 468 | // Merge information from the template arguments. We ignore |
| 469 | // template-argument visibility if we've got an explicit |
| 470 | // instantiation with a visibility attribute. |
| 471 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 472 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
Rafael Espindola | 503276b | 2013-05-30 21:23:15 +0000 | [diff] [blame] | 473 | if (considerVisibility) |
| 474 | LV.mergeVisibility(argsLV); |
| 475 | LV.mergeExternalVisibility(argsLV); |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 478 | /// Should we consider visibility associated with the template |
| 479 | /// arguments and parameters of the given variable template |
| 480 | /// specialization? As usual, follow class template specialization |
| 481 | /// logic up to initialization. |
| 482 | static bool shouldConsiderTemplateVisibility( |
| 483 | const VarTemplateSpecializationDecl *spec, |
| 484 | LVComputationKind computation) { |
| 485 | // Include visibility from the template parameters and arguments |
| 486 | // only if this is not an explicit instantiation or specialization |
| 487 | // with direct explicit visibility (and note that implicit |
| 488 | // instantiations won't have a direct attribute). |
| 489 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 490 | return true; |
| 491 | |
| 492 | // An explicit variable specialization is an independent, top-level |
| 493 | // declaration. As such, if it has an explicit visibility attribute, |
| 494 | // that must directly express the user's intent, and we should honor |
| 495 | // it. |
| 496 | if (spec->isExplicitSpecialization() && |
| 497 | hasExplicitVisibilityAlready(computation)) |
| 498 | return false; |
| 499 | |
| 500 | return !hasDirectVisibilityAttribute(spec, computation); |
| 501 | } |
| 502 | |
| 503 | /// Merge in template-related linkage and visibility for the given |
| 504 | /// variable template specialization. As usual, follow class template |
| 505 | /// specialization logic up to initialization. |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 506 | void LinkageComputer::mergeTemplateLV(LinkageInfo &LV, |
| 507 | const VarTemplateSpecializationDecl *spec, |
| 508 | LVComputationKind computation) { |
Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 509 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
| 510 | |
| 511 | // Merge information from the template parameters, but ignore |
| 512 | // visibility if we're only considering template arguments. |
| 513 | |
| 514 | VarTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 515 | LinkageInfo tempLV = |
| 516 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
| 517 | LV.mergeMaybeWithVisibility(tempLV, |
| 518 | considerVisibility && !hasExplicitVisibilityAlready(computation)); |
| 519 | |
| 520 | // Merge information from the template arguments. We ignore |
| 521 | // template-argument visibility if we've got an explicit |
| 522 | // instantiation with a visibility attribute. |
| 523 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| 524 | LinkageInfo argsLV = getLVForTemplateArgumentList(templateArgs, computation); |
| 525 | if (considerVisibility) |
| 526 | LV.mergeVisibility(argsLV); |
| 527 | LV.mergeExternalVisibility(argsLV); |
| 528 | } |
| 529 | |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 530 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
| 531 | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
Rafael Espindola | 5cc7890 | 2012-07-13 23:26:43 +0000 | [diff] [blame] | 532 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 533 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 534 | return false; |
| 535 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 536 | const auto *FD = dyn_cast<FunctionDecl>(D); |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 537 | if (!FD) |
| 538 | return false; |
| 539 | |
| 540 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 541 | if (FunctionTemplateSpecializationInfo *spec |
| 542 | = FD->getTemplateSpecializationInfo()) { |
| 543 | TSK = spec->getTemplateSpecializationKind(); |
| 544 | } else if (MemberSpecializationInfo *MSI = |
| 545 | FD->getMemberSpecializationInfo()) { |
| 546 | TSK = MSI->getTemplateSpecializationKind(); |
| 547 | } |
| 548 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 549 | const FunctionDecl *Def = nullptr; |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 550 | // InlineVisibilityHidden only applies to definitions, and |
| 551 | // isInlined() only gives meaningful answers on definitions |
| 552 | // anyway. |
| 553 | return TSK != TSK_ExplicitInstantiationDeclaration && |
| 554 | TSK != TSK_ExplicitInstantiationDefinition && |
Rafael Espindola | fb9d4b4 | 2012-10-11 16:32:25 +0000 | [diff] [blame] | 555 | FD->hasBody(Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 558 | template <typename T> static bool isFirstInExternCContext(T *D) { |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 559 | const T *First = D->getFirstDecl(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 560 | return First->isInExternCContext(); |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 563 | static bool isSingleLineLanguageLinkage(const Decl &D) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 564 | if (const auto *SD = dyn_cast<LinkageSpecDecl>(D.getDeclContext())) |
Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 565 | if (!SD->hasBraces()) |
Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 566 | return true; |
| 567 | return false; |
| 568 | } |
| 569 | |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 570 | /// Determine whether D is declared in the purview of a named module. |
| 571 | static bool isInModulePurview(const NamedDecl *D) { |
| 572 | if (auto *M = D->getOwningModule()) |
| 573 | return M->isModulePurview(); |
| 574 | return false; |
| 575 | } |
| 576 | |
Ali Tamur | b6a8a6c | 2019-04-19 02:15:57 +0000 | [diff] [blame] | 577 | static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 578 | // FIXME: Handle isModulePrivate. |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 579 | switch (D->getModuleOwnershipKind()) { |
| 580 | case Decl::ModuleOwnershipKind::Unowned: |
| 581 | case Decl::ModuleOwnershipKind::ModulePrivate: |
| 582 | return false; |
| 583 | case Decl::ModuleOwnershipKind::Visible: |
| 584 | case Decl::ModuleOwnershipKind::VisibleWhenImported: |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 585 | return isInModulePurview(D); |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 586 | } |
| 587 | llvm_unreachable("unexpected module ownership kind"); |
| 588 | } |
| 589 | |
| 590 | static LinkageInfo getInternalLinkageFor(const NamedDecl *D) { |
| 591 | // Internal linkage declarations within a module interface unit are modeled |
| 592 | // as "module-internal linkage", which means that they have internal linkage |
| 593 | // formally but can be indirectly accessed from outside the module via inline |
| 594 | // functions and templates defined within the module. |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 595 | if (isInModulePurview(D)) |
| 596 | return LinkageInfo(ModuleInternalLinkage, DefaultVisibility, false); |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 597 | |
| 598 | return LinkageInfo::internal(); |
| 599 | } |
| 600 | |
| 601 | static LinkageInfo getExternalLinkageFor(const NamedDecl *D) { |
| 602 | // C++ Modules TS [basic.link]/6.8: |
| 603 | // - A name declared at namespace scope that does not have internal linkage |
| 604 | // by the previous rules and that is introduced by a non-exported |
| 605 | // declaration has module linkage. |
Richard Smith | 96451e3 | 2019-04-19 02:46:50 +0000 | [diff] [blame] | 606 | if (isInModulePurview(D) && !isExportedFromModuleInterfaceUnit( |
| 607 | cast<NamedDecl>(D->getCanonicalDecl()))) |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 608 | return LinkageInfo(ModuleLinkage, DefaultVisibility, false); |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 609 | |
| 610 | return LinkageInfo::external(); |
| 611 | } |
| 612 | |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 613 | static StorageClass getStorageClass(const Decl *D) { |
| 614 | if (auto *TD = dyn_cast<TemplateDecl>(D)) |
| 615 | D = TD->getTemplatedDecl(); |
Hans Wennborg | b0dbc961 | 2019-05-14 10:11:33 +0000 | [diff] [blame] | 616 | if (D) { |
| 617 | if (auto *VD = dyn_cast<VarDecl>(D)) |
| 618 | return VD->getStorageClass(); |
| 619 | if (auto *FD = dyn_cast<FunctionDecl>(D)) |
| 620 | return FD->getStorageClass(); |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 621 | } |
| 622 | return SC_None; |
| 623 | } |
| 624 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 625 | LinkageInfo |
| 626 | LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 627 | LVComputationKind computation, |
| 628 | bool IgnoreVarTypeLinkage) { |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 629 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 630 | "Not a name having namespace scope"); |
| 631 | ASTContext &Context = D->getASTContext(); |
| 632 | |
| 633 | // C++ [basic.link]p3: |
| 634 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 635 | // is the name of |
Ilya Biryukov | b8292c9 | 2019-04-24 08:50:24 +0000 | [diff] [blame] | 636 | |
Hans Wennborg | b0dbc961 | 2019-05-14 10:11:33 +0000 | [diff] [blame] | 637 | if (getStorageClass(D->getCanonicalDecl()) == SC_Static) { |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 638 | // - a variable, variable template, function, or function template |
| 639 | // that is explicitly declared static; or |
| 640 | // (This bullet corresponds to C99 6.2.2p3.) |
| 641 | return getInternalLinkageFor(D); |
| 642 | } |
| 643 | |
| 644 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
| 645 | // - a non-template variable of non-volatile const-qualified type, unless |
| 646 | // - it is explicitly declared extern, or |
| 647 | // - it is inline or exported, or |
| 648 | // - it was previously declared and the prior declaration did not have |
| 649 | // internal linkage |
| 650 | // (There is no equivalent in C99.) |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 651 | if (Context.getLangOpts().CPlusPlus && |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 652 | Var->getType().isConstQualified() && |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 653 | !Var->getType().isVolatileQualified() && |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 654 | !Var->isInline() && |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 655 | !isExportedFromModuleInterfaceUnit(Var) && |
| 656 | !isa<VarTemplateSpecializationDecl>(Var) && |
| 657 | !Var->getDescribedVarTemplate()) { |
Rafael Espindola | 985a3ab | 2013-04-03 19:22:20 +0000 | [diff] [blame] | 658 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
Rafael Espindola | 985a3ab | 2013-04-03 19:22:20 +0000 | [diff] [blame] | 659 | if (PrevVar) |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 660 | return getLVForDecl(PrevVar, computation); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 661 | |
| 662 | if (Var->getStorageClass() != SC_Extern && |
Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 663 | Var->getStorageClass() != SC_PrivateExtern && |
Richard Smith | 03c0503 | 2014-02-17 23:34:47 +0000 | [diff] [blame] | 664 | !isSingleLineLanguageLinkage(*Var)) |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 665 | return getInternalLinkageFor(Var); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; |
| 669 | PrevVar = PrevVar->getPreviousDecl()) { |
| 670 | if (PrevVar->getStorageClass() == SC_PrivateExtern && |
| 671 | Var->getStorageClass() == SC_None) |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 672 | return getDeclLinkageAndVisibility(PrevVar); |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 673 | // Explicitly declared static. |
| 674 | if (PrevVar->getStorageClass() == SC_Static) |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 675 | return getInternalLinkageFor(Var); |
Fariborz Jahanian | 8feee2d | 2011-06-16 20:14:50 +0000 | [diff] [blame] | 676 | } |
David Majnemer | 18fac3b | 2014-12-10 22:58:14 +0000 | [diff] [blame] | 677 | } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(D)) { |
| 678 | // - a data member of an anonymous union. |
| 679 | const VarDecl *VD = IFD->getVarDecl(); |
| 680 | assert(VD && "Expected a VarDecl in this IndirectFieldDecl!"); |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 681 | return getLVForNamespaceScopeDecl(VD, computation, IgnoreVarTypeLinkage); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 682 | } |
David Majnemer | 0eb8bbd | 2013-10-23 20:52:43 +0000 | [diff] [blame] | 683 | assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!"); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 684 | |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 685 | // FIXME: This gives internal linkage to names that should have no linkage |
| 686 | // (those not covered by [basic.link]p6). |
Chandler Carruth | 9682a2fd | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 687 | if (D->isInAnonymousNamespace()) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 688 | const auto *Var = dyn_cast<VarDecl>(D); |
| 689 | const auto *Func = dyn_cast<FunctionDecl>(D); |
Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 690 | // FIXME: The check for extern "C" here is not justified by the standard |
| 691 | // wording, but we retain it from the pre-DR1113 model to avoid breaking |
| 692 | // code. |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 693 | // |
| 694 | // C++11 [basic.link]p4: |
| 695 | // An unnamed namespace or a namespace declared directly or indirectly |
| 696 | // within an unnamed namespace has internal linkage. |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 697 | if ((!Var || !isFirstInExternCContext(Var)) && |
| 698 | (!Func || !isFirstInExternCContext(Func))) |
Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 699 | return getInternalLinkageFor(D); |
Chandler Carruth | 9682a2fd | 2011-02-24 19:03:39 +0000 | [diff] [blame] | 700 | } |
John McCall | b7139c4 | 2010-10-28 04:18:25 +0000 | [diff] [blame] | 701 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 702 | // Set up the defaults. |
| 703 | |
| 704 | // C99 6.2.2p5: |
| 705 | // If the declaration of an identifier for an object has file |
| 706 | // scope and no storage-class specifier, its linkage is |
| 707 | // external. |
Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 708 | LinkageInfo LV = getExternalLinkageFor(D); |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 709 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 710 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 711 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) { |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 712 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 713 | } else { |
| 714 | // If we're declared in a namespace with a visibility attribute, |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 715 | // use that namespace's visibility, and it still counts as explicit. |
Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 716 | for (const DeclContext *DC = D->getDeclContext(); |
| 717 | !isa<TranslationUnitDecl>(DC); |
| 718 | DC = DC->getParent()) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 719 | const auto *ND = dyn_cast<NamespaceDecl>(DC); |
Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 720 | if (!ND) continue; |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 721 | if (Optional<Visibility> Vis = getExplicitVisibility(ND, computation)) { |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 722 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | } |
| 726 | } |
Rafael Espindola | 78158af | 2012-04-16 18:46:26 +0000 | [diff] [blame] | 727 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 728 | // 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] | 729 | if (!LV.isVisibilityExplicit()) { |
John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 730 | // Use global type/value visibility as appropriate. |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 731 | Visibility globalVisibility = |
| 732 | computation.isValueVisibility() |
| 733 | ? Context.getLangOpts().getValueVisibilityMode() |
| 734 | : Context.getLangOpts().getTypeVisibilityMode(); |
John McCall | b4a99d3 | 2013-02-19 01:57:35 +0000 | [diff] [blame] | 735 | LV.mergeVisibility(globalVisibility, /*explicit*/ false); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 736 | |
| 737 | // If we're paying attention to global visibility, apply |
| 738 | // -finline-visibility-hidden if this is an inline method. |
| 739 | if (useInlineVisibilityHidden(D)) |
Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 740 | LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 741 | } |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 742 | } |
Rafael Espindola | af690f5 | 2012-04-19 02:55:01 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 744 | // C++ [basic.link]p4: |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 745 | |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 746 | // A name having namespace scope that has not been given internal linkage |
| 747 | // above and that is the name of |
| 748 | // [...bullets...] |
| 749 | // has its linkage determined as follows: |
| 750 | // - if the enclosing namespace has internal linkage, the name has |
| 751 | // internal linkage; [handled above] |
| 752 | // - otherwise, if the declaration of the name is attached to a named |
| 753 | // module and is not exported, the name has module linkage; |
| 754 | // - otherwise, the name has external linkage. |
| 755 | // LV is currently set up to handle the last two bullets. |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 756 | // |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 757 | // The bullets are: |
| 758 | |
| 759 | // - a variable; or |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 760 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 761 | // GCC applies the following optimization to variables and static |
| 762 | // data members, but not to functions: |
| 763 | // |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 764 | // Modify the variable's LV by the LV of its type unless this is |
| 765 | // C or extern "C". This follows from [basic.link]p9: |
| 766 | // A type without linkage shall not be used as the type of a |
| 767 | // variable or function with external linkage unless |
| 768 | // - the entity has C language linkage, or |
| 769 | // - the entity is declared within an unnamed namespace, or |
| 770 | // - the entity is not used or is defined in the same |
| 771 | // translation unit. |
| 772 | // and [basic.link]p10: |
| 773 | // ...the types specified by all declarations referring to a |
| 774 | // given variable or function shall be identical... |
| 775 | // C does not have an equivalent rule. |
| 776 | // |
John McCall | 5fe8412 | 2010-10-26 04:59:26 +0000 | [diff] [blame] | 777 | // Ignore this if we've got an explicit attribute; the user |
| 778 | // probably knows what they're doing. |
| 779 | // |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 780 | // Note that we don't want to make the variable non-external |
| 781 | // because of this, but unique-external linkage suits us. |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 782 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Var) && |
| 783 | !IgnoreVarTypeLinkage) { |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 784 | LinkageInfo TypeLV = getLVForType(*Var->getType(), computation); |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 785 | if (!isExternallyVisible(TypeLV.getLinkage())) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 786 | return LinkageInfo::uniqueExternal(); |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 787 | if (!LV.isVisibilityExplicit()) |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 788 | LV.mergeVisibility(TypeLV); |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 789 | } |
| 790 | |
John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 791 | if (Var->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 792 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 793 | |
Rafael Espindola | d5ed033 | 2012-11-12 04:10:23 +0000 | [diff] [blame] | 794 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 795 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't have |
| 796 | // to do it here. |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 797 | |
Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 798 | // As per function and class template specializations (below), |
| 799 | // consider LV for the template and template arguments. We're at file |
| 800 | // scope, so we do not need to worry about nested specializations. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 801 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Var)) { |
Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 802 | mergeTemplateLV(LV, spec, computation); |
| 803 | } |
| 804 | |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 805 | // - a function; or |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 806 | } else if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 2efaf11 | 2010-10-28 07:07:52 +0000 | [diff] [blame] | 807 | // In theory, we can modify the function's LV by the LV of its |
| 808 | // type unless it has C linkage (see comment above about variables |
| 809 | // for justification). In practice, GCC doesn't do this, so it's |
| 810 | // just too painful to make work. |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 811 | |
John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 812 | if (Function->getStorageClass() == SC_PrivateExtern) |
Rafael Espindola | 7a5543d | 2012-04-19 02:22:07 +0000 | [diff] [blame] | 813 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | 2303265 | 2010-11-02 18:38:13 +0000 | [diff] [blame] | 814 | |
Rafael Espindola | a508c5d | 2012-11-21 02:47:19 +0000 | [diff] [blame] | 815 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 816 | // merging storage classes and visibility attributes, so we don't have to |
| 817 | // look at previous decls in here. |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 818 | |
John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 819 | // In C++, then if the type of the function uses a type with |
| 820 | // unique-external linkage, it's not legally usable from outside |
| 821 | // this translation unit. However, we should use the C linkage |
| 822 | // rules instead for extern "C" declarations. |
Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 823 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(Function)) { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 824 | // Only look at the type-as-written. Otherwise, deducing the return type |
| 825 | // of a function could change its linkage. |
Richard Smith | 50f4afc | 2013-05-12 23:17:59 +0000 | [diff] [blame] | 826 | QualType TypeAsWritten = Function->getType(); |
| 827 | if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) |
| 828 | TypeAsWritten = TSI->getType(); |
Richard Smith | 405e2db | 2017-09-20 07:22:00 +0000 | [diff] [blame] | 829 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
Richard Smith | 50f4afc | 2013-05-12 23:17:59 +0000 | [diff] [blame] | 830 | return LinkageInfo::uniqueExternal(); |
| 831 | } |
John McCall | f768aa7 | 2011-02-10 06:50:24 +0000 | [diff] [blame] | 832 | |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 833 | // Consider LV from the template and the template arguments. |
| 834 | // We're at file scope, so we do not need to worry about nested |
| 835 | // specializations. |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 836 | if (FunctionTemplateSpecializationInfo *specInfo |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 837 | = Function->getTemplateSpecializationInfo()) { |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 838 | mergeTemplateLV(LV, Function, specInfo, computation); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 841 | // - a named class (Clause 9), or an unnamed class defined in a |
| 842 | // typedef declaration in which the class has the typedef name |
| 843 | // for linkage purposes (7.1.3); or |
| 844 | // - a named enumeration (7.2), or an unnamed enumeration |
| 845 | // defined in a typedef declaration in which the enumeration |
| 846 | // has the typedef name for linkage purposes (7.1.3); or |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 847 | } else if (const auto *Tag = dyn_cast<TagDecl>(D)) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 848 | // Unnamed tags have no linkage. |
John McCall | 5ea9577 | 2013-03-09 00:54:27 +0000 | [diff] [blame] | 849 | if (!Tag->hasNameForLinkage()) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 850 | return LinkageInfo::none(); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 851 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 852 | // If this is a class template specialization, consider the |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 853 | // linkage of the template and template arguments. We're at file |
| 854 | // scope, so we do not need to worry about nested specializations. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 855 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Tag)) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 856 | mergeTemplateLV(LV, spec, computation); |
Douglas Gregor | 7dc5c17 | 2010-02-03 09:33:45 +0000 | [diff] [blame] | 857 | } |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 858 | |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 859 | // FIXME: This is not part of the C++ standard any more. |
| 860 | // - an enumerator belonging to an enumeration with external linkage; or |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 861 | } else if (isa<EnumConstantDecl>(D)) { |
Rafael Espindola | 46cb6f1 | 2012-04-21 23:28:21 +0000 | [diff] [blame] | 862 | LinkageInfo EnumLV = getLVForDecl(cast<NamedDecl>(D->getDeclContext()), |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 863 | computation); |
Rafael Espindola | b97e896 | 2013-05-27 14:14:42 +0000 | [diff] [blame] | 864 | if (!isExternalFormalLinkage(EnumLV.getLinkage())) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 865 | return LinkageInfo::none(); |
| 866 | LV.merge(EnumLV); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 867 | |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 868 | // - a template |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 869 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 870 | bool considerVisibility = !hasExplicitVisibilityAlready(computation); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 871 | LinkageInfo tempLV = |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 872 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 873 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
| 874 | |
Richard Smith | bb75068 | 2019-04-26 01:51:08 +0000 | [diff] [blame] | 875 | // An unnamed namespace or a namespace declared directly or indirectly |
| 876 | // within an unnamed namespace has internal linkage. All other namespaces |
| 877 | // have external linkage. |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 878 | // |
| 879 | // We handled names in anonymous namespaces above. |
| 880 | } else if (isa<NamespaceDecl>(D)) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 881 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 882 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 883 | // By extension, we assign external linkage to Objective-C |
| 884 | // interfaces. |
| 885 | } else if (isa<ObjCInterfaceDecl>(D)) { |
| 886 | // fallout |
| 887 | |
Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 888 | } else if (auto *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 889 | // A typedef declaration has linkage if it gives a type a name for |
| 890 | // linkage purposes. |
| 891 | if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
| 892 | return LinkageInfo::none(); |
| 893 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 894 | // Everything not covered here has no linkage. |
| 895 | } else { |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 896 | return LinkageInfo::none(); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Richard Smith | c445d5d | 2017-10-03 01:58:15 +0000 | [diff] [blame] | 899 | // If we ended up with non-externally-visible linkage, visibility should |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 900 | // always be default. |
Richard Smith | c445d5d | 2017-10-03 01:58:15 +0000 | [diff] [blame] | 901 | if (!isExternallyVisible(LV.getLinkage())) |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 902 | return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 903 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 904 | return LV; |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 905 | } |
| 906 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 907 | LinkageInfo |
| 908 | LinkageComputer::getLVForClassMember(const NamedDecl *D, |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 909 | LVComputationKind computation, |
| 910 | bool IgnoreVarTypeLinkage) { |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 911 | // Only certain class members have linkage. Note that fields don't |
| 912 | // really have linkage, but it's convenient to say they do for the |
| 913 | // purposes of calculating linkage of pointer-to-data-member |
| 914 | // template arguments. |
Richard Smith | 26d11be | 2014-01-08 01:51:59 +0000 | [diff] [blame] | 915 | // |
| 916 | // Templates also don't officially have linkage, but since we ignore |
| 917 | // the C++ standard and look at template arguments when determining |
| 918 | // linkage and visibility of a template specialization, we might hit |
| 919 | // a template template argument that way. If we do, we need to |
| 920 | // consider its linkage. |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 921 | if (!(isa<CXXMethodDecl>(D) || |
| 922 | isa<VarDecl>(D) || |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 923 | isa<FieldDecl>(D) || |
David Majnemer | 0eb8bbd | 2013-10-23 20:52:43 +0000 | [diff] [blame] | 924 | isa<IndirectFieldDecl>(D) || |
Richard Smith | 26d11be | 2014-01-08 01:51:59 +0000 | [diff] [blame] | 925 | isa<TagDecl>(D) || |
| 926 | isa<TemplateDecl>(D))) |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 927 | return LinkageInfo::none(); |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 928 | |
John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 929 | LinkageInfo LV; |
| 930 | |
John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 931 | // If we have an explicit visibility attribute, merge that in. |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 932 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 933 | if (Optional<Visibility> Vis = getExplicitVisibility(D, computation)) |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 934 | LV.mergeVisibility(*Vis, true); |
Rafael Espindola | c7c7ad5 | 2012-07-13 14:25:36 +0000 | [diff] [blame] | 935 | // If we're paying attention to global visibility, apply |
| 936 | // -finline-visibility-hidden if this is an inline method. |
| 937 | // |
| 938 | // Note that we do this before merging information about |
| 939 | // the class visibility. |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 940 | if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) |
Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 941 | LV.mergeVisibility(HiddenVisibility, /*visibilityExplicit=*/false); |
John McCall | 0707266 | 2010-11-02 01:45:15 +0000 | [diff] [blame] | 942 | } |
Rafael Espindola | 53cf219 | 2012-04-19 05:50:08 +0000 | [diff] [blame] | 943 | |
| 944 | // If this class member has an explicit visibility attribute, the only |
| 945 | // thing that can change its visibility is the template arguments, so |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 946 | // only look for them when processing the class. |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 947 | LVComputationKind classComputation = computation; |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 948 | if (LV.isVisibilityExplicit()) |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 949 | classComputation = withExplicitVisibilityAlready(computation); |
Rafael Espindola | 505a7c8 | 2012-04-16 18:25:01 +0000 | [diff] [blame] | 950 | |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 951 | LinkageInfo classLV = |
| 952 | getLVForDecl(cast<RecordDecl>(D->getDeclContext()), classComputation); |
Richard Smith | cdb06f2 | 2017-09-23 04:02:17 +0000 | [diff] [blame] | 953 | // The member has the same linkage as the class. If that's not externally |
| 954 | // visible, we don't need to compute anything about the linkage. |
| 955 | // FIXME: If we're only computing linkage, can we bail out here? |
Rafael Espindola | e6190db | 2013-05-28 02:22:10 +0000 | [diff] [blame] | 956 | if (!isExternallyVisible(classLV.getLinkage())) |
Richard Smith | cdb06f2 | 2017-09-23 04:02:17 +0000 | [diff] [blame] | 957 | return classLV; |
Rafael Espindola | e6190db | 2013-05-28 02:22:10 +0000 | [diff] [blame] | 958 | |
| 959 | |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 960 | // Otherwise, don't merge in classLV yet, because in certain cases |
| 961 | // we need to completely ignore the visibility from it. |
| 962 | |
| 963 | // Specifically, if this decl exists and has an explicit attribute. |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 964 | const NamedDecl *explicitSpecSuppressor = nullptr; |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 965 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 966 | if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 967 | // Only look at the type-as-written. Otherwise, deducing the return type |
| 968 | // of a function could change its linkage. |
| 969 | QualType TypeAsWritten = MD->getType(); |
| 970 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 971 | TypeAsWritten = TSI->getType(); |
| 972 | if (!isExternallyVisible(TypeAsWritten->getLinkage())) |
| 973 | return LinkageInfo::uniqueExternal(); |
| 974 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 975 | // If this is a method template specialization, use the linkage for |
| 976 | // the template parameters and arguments. |
John McCall | b8c604a | 2011-06-27 23:06:04 +0000 | [diff] [blame] | 977 | if (FunctionTemplateSpecializationInfo *spec |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 978 | = MD->getTemplateSpecializationInfo()) { |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 979 | mergeTemplateLV(LV, MD, spec, computation); |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 980 | if (spec->isExplicitSpecialization()) { |
| 981 | explicitSpecSuppressor = MD; |
| 982 | } else if (isExplicitMemberSpecialization(spec->getTemplate())) { |
| 983 | explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); |
| 984 | } |
| 985 | } else if (isExplicitMemberSpecialization(MD)) { |
| 986 | explicitSpecSuppressor = MD; |
John McCall | e6e622e | 2010-11-01 01:29:57 +0000 | [diff] [blame] | 987 | } |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 988 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 989 | } else if (const auto *RD = dyn_cast<CXXRecordDecl>(D)) { |
| 990 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(RD)) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 991 | mergeTemplateLV(LV, spec, computation); |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 992 | if (spec->isExplicitSpecialization()) { |
| 993 | explicitSpecSuppressor = spec; |
| 994 | } else { |
| 995 | const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 996 | if (isExplicitMemberSpecialization(temp)) { |
| 997 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 998 | } |
| 999 | } |
| 1000 | } else if (isExplicitMemberSpecialization(RD)) { |
| 1001 | explicitSpecSuppressor = RD; |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1004 | // Static data members. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1005 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 1006 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(VD)) |
Larisse Voufo | 5899e19 | 2014-07-02 23:08:34 +0000 | [diff] [blame] | 1007 | mergeTemplateLV(LV, spec, computation); |
| 1008 | |
John McCall | 36cd5cc | 2010-10-30 09:18:49 +0000 | [diff] [blame] | 1009 | // Modify the variable's linkage by its type, but ignore the |
| 1010 | // type's visibility unless it's a definition. |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1011 | if (!IgnoreVarTypeLinkage) { |
| 1012 | LinkageInfo typeLV = getLVForType(*VD->getType(), computation); |
| 1013 | // FIXME: If the type's linkage is not externally visible, we can |
| 1014 | // give this static data member UniqueExternalLinkage. |
| 1015 | if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) |
| 1016 | LV.mergeVisibility(typeLV); |
| 1017 | LV.mergeExternalVisibility(typeLV); |
| 1018 | } |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1019 | |
| 1020 | if (isExplicitMemberSpecialization(VD)) { |
| 1021 | explicitSpecSuppressor = VD; |
| 1022 | } |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1023 | |
| 1024 | // Template members. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1025 | } else if (const auto *temp = dyn_cast<TemplateDecl>(D)) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1026 | bool considerVisibility = |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 1027 | (!LV.isVisibilityExplicit() && |
| 1028 | !classLV.isVisibilityExplicit() && |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1029 | !hasExplicitVisibilityAlready(computation)); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1030 | LinkageInfo tempLV = |
Rafael Espindola | ecf63c6 | 2013-05-29 04:55:30 +0000 | [diff] [blame] | 1031 | getLVForTemplateParameterList(temp->getTemplateParameters(), computation); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1032 | LV.mergeMaybeWithVisibility(tempLV, considerVisibility); |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1033 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1034 | if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(temp)) { |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1035 | if (isExplicitMemberSpecialization(redeclTemp)) { |
| 1036 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 1037 | } |
| 1038 | } |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1041 | // We should never be looking for an attribute directly on a template. |
| 1042 | assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); |
| 1043 | |
| 1044 | // If this member is an explicit member specialization, and it has |
| 1045 | // an explicit attribute, ignore visibility from the parent. |
| 1046 | bool considerClassVisibility = true; |
| 1047 | if (explicitSpecSuppressor && |
Rafael Espindola | 4a5da44 | 2013-02-27 02:56:45 +0000 | [diff] [blame] | 1048 | // optimization: hasDVA() is true only with explicit visibility. |
| 1049 | LV.isVisibilityExplicit() && |
| 1050 | classLV.getVisibility() != DefaultVisibility && |
John McCall | 5f46c48 | 2013-02-21 23:42:58 +0000 | [diff] [blame] | 1051 | hasDirectVisibilityAttribute(explicitSpecSuppressor, computation)) { |
| 1052 | considerClassVisibility = false; |
| 1053 | } |
| 1054 | |
| 1055 | // Finally, merge in information from the class. |
| 1056 | LV.mergeMaybeWithVisibility(classLV, considerClassVisibility); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1057 | return LV; |
John McCall | 8823c65 | 2010-08-13 08:35:10 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1060 | void NamedDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 1061 | |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1062 | bool NamedDecl::isLinkageValid() const { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1063 | if (!hasCachedLinkage()) |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 1064 | return true; |
John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 1065 | |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1066 | Linkage L = LinkageComputer{} |
| 1067 | .computeLVForDecl(this, LVComputationKind::forLinkageOnly()) |
| 1068 | .getLinkage(); |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1069 | return L == getCachedLinkage(); |
John McCall | d396b97 | 2011-02-08 19:01:05 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1072 | ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { |
| 1073 | StringRef name = getName(); |
| 1074 | if (name.empty()) return SFF_None; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1075 | |
Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 1076 | if (name.front() == 'C') |
| 1077 | if (name == "CFStringCreateWithFormat" || |
| 1078 | name == "CFStringCreateWithFormatAndArguments" || |
| 1079 | name == "CFStringAppendFormat" || |
| 1080 | name == "CFStringAppendFormatAndArguments") |
| 1081 | return SFF_CFString; |
| 1082 | return SFF_None; |
| 1083 | } |
| 1084 | |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 1085 | Linkage NamedDecl::getLinkageInternal() const { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1086 | // We don't care about visibility here, so ask for the cheapest |
| 1087 | // possible visibility analysis. |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1088 | return LinkageComputer{} |
| 1089 | .getLVForDecl(this, LVComputationKind::forLinkageOnly()) |
| 1090 | .getLinkage(); |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1093 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1094 | return LinkageComputer{}.getDeclLinkageAndVisibility(this); |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1095 | } |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1096 | |
Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1097 | static Optional<Visibility> |
| 1098 | getExplicitVisibilityAux(const NamedDecl *ND, |
| 1099 | NamedDecl::ExplicitVisibilityKind kind, |
| 1100 | bool IsMostRecent) { |
| 1101 | assert(!IsMostRecent || ND == ND->getMostRecentDecl()); |
| 1102 | |
Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1103 | // Check the declaration itself first. |
Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1104 | if (Optional<Visibility> V = getVisibilityOf(ND, kind)) |
Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1105 | return V; |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1106 | |
Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1107 | // If this is a member class of a specialization of a class template |
| 1108 | // and the corresponding decl has explicit visibility, use that. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1109 | if (const auto *RD = dyn_cast<CXXRecordDecl>(ND)) { |
Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1110 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 1111 | if (InstantiatedFrom) |
| 1112 | return getVisibilityOf(InstantiatedFrom, kind); |
| 1113 | } |
| 1114 | |
| 1115 | // If there wasn't explicit visibility there, and this is a |
| 1116 | // specialization of a class template, check for visibility |
| 1117 | // on the pattern. |
Steven Wu | 79cbd11 | 2018-04-19 15:46:43 +0000 | [diff] [blame] | 1118 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(ND)) { |
| 1119 | // Walk all the template decl till this point to see if there are |
| 1120 | // explicit visibility attributes. |
| 1121 | const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl(); |
| 1122 | while (TD != nullptr) { |
| 1123 | auto Vis = getVisibilityOf(TD, kind); |
| 1124 | if (Vis != None) |
| 1125 | return Vis; |
| 1126 | TD = TD->getPreviousDecl(); |
| 1127 | } |
| 1128 | return None; |
| 1129 | } |
Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Use the most recent declaration. |
Rafael Espindola | 7ab1ce0 | 2013-12-08 01:13:22 +0000 | [diff] [blame] | 1132 | if (!IsMostRecent && !isa<NamespaceDecl>(ND)) { |
Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1133 | const NamedDecl *MostRecent = ND->getMostRecentDecl(); |
| 1134 | if (MostRecent != ND) |
| 1135 | return getExplicitVisibilityAux(MostRecent, kind, true); |
| 1136 | } |
Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1137 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1138 | if (const auto *Var = dyn_cast<VarDecl>(ND)) { |
Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1139 | if (Var->isStaticDataMember()) { |
| 1140 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
| 1141 | if (InstantiatedFrom) |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1142 | return getVisibilityOf(InstantiatedFrom, kind); |
Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
David Majnemer | 35b02bc | 2014-04-29 07:32:26 +0000 | [diff] [blame] | 1145 | if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Var)) |
| 1146 | return getVisibilityOf(VTSD->getSpecializedTemplate()->getTemplatedDecl(), |
| 1147 | kind); |
| 1148 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1149 | return None; |
Rafael Espindola | 96e6824 | 2012-05-16 02:10:38 +0000 | [diff] [blame] | 1150 | } |
Rafael Espindola | 3a52c44 | 2013-02-26 19:33:14 +0000 | [diff] [blame] | 1151 | // Also handle function template specializations. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1152 | if (const auto *fn = dyn_cast<FunctionDecl>(ND)) { |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1153 | // If the function is a specialization of a template with an |
| 1154 | // explicit visibility attribute, use that. |
| 1155 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 1156 | = fn->getTemplateSpecializationInfo()) |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1157 | return getVisibilityOf(templateInfo->getTemplate()->getTemplatedDecl(), |
| 1158 | kind); |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1159 | |
Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 1160 | // If the function is a member of a specialization of a class template |
| 1161 | // and the corresponding decl has explicit visibility, use that. |
| 1162 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 1163 | if (InstantiatedFrom) |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1164 | return getVisibilityOf(InstantiatedFrom, kind); |
Rafael Espindola | 8093fdf | 2012-02-23 04:17:32 +0000 | [diff] [blame] | 1165 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1166 | return None; |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Rafael Espindola | fb4263f | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 1169 | // The visibility of a template is stored in the templated decl. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1170 | if (const auto *TD = dyn_cast<TemplateDecl>(ND)) |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1171 | return getVisibilityOf(TD->getTemplatedDecl(), kind); |
Rafael Espindola | fb4263f | 2012-07-31 19:02:02 +0000 | [diff] [blame] | 1172 | |
David Blaikie | 7a30dc5 | 2013-02-21 01:47:18 +0000 | [diff] [blame] | 1173 | return None; |
Douglas Gregor | 1baf38f | 2011-03-26 12:10:19 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Rafael Espindola | 9ad6112 | 2013-11-26 16:09:08 +0000 | [diff] [blame] | 1176 | Optional<Visibility> |
| 1177 | NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { |
| 1178 | return getExplicitVisibilityAux(this, kind, false); |
| 1179 | } |
| 1180 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1181 | LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC, |
| 1182 | Decl *ContextDecl, |
| 1183 | LVComputationKind computation) { |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1184 | // This lambda has its linkage/visibility determined by its owner. |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1185 | const NamedDecl *Owner; |
| 1186 | if (!ContextDecl) |
| 1187 | Owner = dyn_cast<NamedDecl>(DC); |
| 1188 | else if (isa<ParmVarDecl>(ContextDecl)) |
| 1189 | Owner = |
| 1190 | dyn_cast<NamedDecl>(ContextDecl->getDeclContext()->getRedeclContext()); |
| 1191 | else |
| 1192 | Owner = cast<NamedDecl>(ContextDecl); |
| 1193 | |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1194 | if (!Owner) |
Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1195 | return LinkageInfo::none(); |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1196 | |
| 1197 | // If the owner has a deduced type, we need to skip querying the linkage and |
| 1198 | // visibility of that type, because it might involve this closure type. The |
| 1199 | // only effect of this is that we might give a lambda VisibleNoLinkage rather |
| 1200 | // than NoLinkage when we don't strictly need to, which is benign. |
| 1201 | auto *VD = dyn_cast<VarDecl>(Owner); |
Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1202 | LinkageInfo OwnerLV = |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1203 | VD && VD->getType()->getContainedDeducedType() |
| 1204 | ? computeLVForDecl(Owner, computation, /*IgnoreVarTypeLinkage*/true) |
| 1205 | : getLVForDecl(Owner, computation); |
| 1206 | |
Richard Smith | 7ff8304 | 2017-09-22 04:33:20 +0000 | [diff] [blame] | 1207 | // A lambda never formally has linkage. But if the owner is externally |
| 1208 | // visible, then the lambda is too. We apply the same rules to blocks. |
| 1209 | if (!isExternallyVisible(OwnerLV.getLinkage())) |
| 1210 | return LinkageInfo::none(); |
| 1211 | return LinkageInfo(VisibleNoLinkage, OwnerLV.getVisibility(), |
| 1212 | OwnerLV.isVisibilityExplicit()); |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1215 | LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D, |
| 1216 | LVComputationKind computation) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1217 | if (const auto *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1218 | if (Function->isInAnonymousNamespace() && |
Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 1219 | !isFirstInExternCContext(Function)) |
Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 1220 | return getInternalLinkageFor(Function); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1221 | |
| 1222 | // This is a "void f();" which got merged with a file static. |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1223 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1224 | return getInternalLinkageFor(Function); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1225 | |
| 1226 | LinkageInfo LV; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1227 | if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1228 | if (Optional<Visibility> Vis = |
| 1229 | getExplicitVisibility(Function, computation)) |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1230 | LV.mergeVisibility(*Vis, true); |
| 1231 | } |
| 1232 | |
| 1233 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 1234 | // merging storage classes and visibility attributes, so we don't have to |
| 1235 | // look at previous decls in here. |
| 1236 | |
| 1237 | return LV; |
| 1238 | } |
| 1239 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1240 | if (const auto *Var = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1241 | if (Var->hasExternalStorage()) { |
Richard Smith | 99f5479 | 2018-01-03 02:34:35 +0000 | [diff] [blame] | 1242 | if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(Var)) |
Richard Smith | df963a3 | 2017-09-22 22:21:44 +0000 | [diff] [blame] | 1243 | return getInternalLinkageFor(Var); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1244 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1245 | LinkageInfo LV; |
| 1246 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 1247 | LV.mergeVisibility(HiddenVisibility, true); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1248 | else if (!hasExplicitVisibilityAlready(computation)) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 1249 | if (Optional<Visibility> Vis = getExplicitVisibility(Var, computation)) |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1250 | LV.mergeVisibility(*Vis, true); |
| 1251 | } |
| 1252 | |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1253 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
| 1254 | LinkageInfo PrevLV = getLVForDecl(Prev, computation); |
| 1255 | if (PrevLV.getLinkage()) |
| 1256 | LV.setLinkage(PrevLV.getLinkage()); |
| 1257 | LV.mergeVisibility(PrevLV); |
| 1258 | } |
| 1259 | |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1260 | return LV; |
| 1261 | } |
Rafael Espindola | a418418 | 2013-06-17 20:04:51 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (!Var->isStaticLocal()) |
| 1264 | return LinkageInfo::none(); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Rafael Espindola | a418418 | 2013-06-17 20:04:51 +0000 | [diff] [blame] | 1267 | ASTContext &Context = D->getASTContext(); |
| 1268 | if (!Context.getLangOpts().CPlusPlus) |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1269 | return LinkageInfo::none(); |
| 1270 | |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1271 | const Decl *OuterD = getOutermostFuncOrBlockContext(D); |
Alexey Bataev | 0a47e65 | 2016-01-12 09:01:25 +0000 | [diff] [blame] | 1272 | if (!OuterD || OuterD->isInvalidDecl()) |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1273 | return LinkageInfo::none(); |
Rafael Espindola | 5218936 | 2013-06-04 13:43:35 +0000 | [diff] [blame] | 1274 | |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1275 | LinkageInfo LV; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1276 | if (const auto *BD = dyn_cast<BlockDecl>(OuterD)) { |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1277 | if (!BD->getBlockManglingNumber()) |
| 1278 | return LinkageInfo::none(); |
Rafael Espindola | 5218936 | 2013-06-04 13:43:35 +0000 | [diff] [blame] | 1279 | |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1280 | LV = getLVForClosure(BD->getDeclContext()->getRedeclContext(), |
| 1281 | BD->getBlockManglingContextDecl(), computation); |
| 1282 | } else { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1283 | const auto *FD = cast<FunctionDecl>(OuterD); |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1284 | if (!FD->isInlined() && |
David Majnemer | 9963ade | 2014-12-16 04:52:14 +0000 | [diff] [blame] | 1285 | !isTemplateInstantiation(FD->getTemplateSpecializationKind())) |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1286 | return LinkageInfo::none(); |
| 1287 | |
Hiroshi Inoue | 03ff37d | 2018-08-22 05:43:27 +0000 | [diff] [blame] | 1288 | // If a function is hidden by -fvisibility-inlines-hidden option and |
| 1289 | // is not explicitly attributed as a hidden function, |
| 1290 | // we should not make static local variables in the function hidden. |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1291 | LV = getLVForDecl(FD, computation); |
Reid Kleckner | a52d151 | 2018-10-10 21:59:56 +0000 | [diff] [blame] | 1292 | if (isa<VarDecl>(D) && useInlineVisibilityHidden(FD) && |
| 1293 | !LV.isVisibilityExplicit()) { |
| 1294 | assert(cast<VarDecl>(D)->isStaticLocal()); |
| 1295 | // If this was an implicitly hidden inline method, check again for |
| 1296 | // explicit visibility on the parent class, and use that for static locals |
| 1297 | // if present. |
| 1298 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 1299 | LV = getLVForDecl(MD->getParent(), computation); |
| 1300 | if (!LV.isVisibilityExplicit()) { |
| 1301 | Visibility globalVisibility = |
| 1302 | computation.isValueVisibility() |
| 1303 | ? Context.getLangOpts().getValueVisibilityMode() |
| 1304 | : Context.getLangOpts().getTypeVisibilityMode(); |
| 1305 | return LinkageInfo(VisibleNoLinkage, globalVisibility, |
| 1306 | /*visibilityExplicit=*/false); |
| 1307 | } |
| 1308 | } |
Eli Friedman | 7e346a8 | 2013-07-01 20:22:57 +0000 | [diff] [blame] | 1309 | } |
Rafael Espindola | 111bb2e | 2013-05-27 14:50:21 +0000 | [diff] [blame] | 1310 | if (!isExternallyVisible(LV.getLinkage())) |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1311 | return LinkageInfo::none(); |
| 1312 | return LinkageInfo(VisibleNoLinkage, LV.getVisibility(), |
| 1313 | LV.isVisibilityExplicit()); |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1316 | static inline const CXXRecordDecl* |
| 1317 | getOutermostEnclosingLambda(const CXXRecordDecl *Record) { |
| 1318 | const CXXRecordDecl *Ret = Record; |
| 1319 | while (Record && Record->isLambda()) { |
| 1320 | Ret = Record; |
| 1321 | if (!Record->getParent()) break; |
| 1322 | // Get the Containing Class of this Lambda Class |
| 1323 | Record = dyn_cast_or_null<CXXRecordDecl>( |
| 1324 | Record->getParent()->getParent()); |
| 1325 | } |
| 1326 | return Ret; |
| 1327 | } |
| 1328 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1329 | LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D, |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1330 | LVComputationKind computation, |
| 1331 | bool IgnoreVarTypeLinkage) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1332 | // Internal_linkage attribute overrides other considerations. |
| 1333 | if (D->hasAttr<InternalLinkageAttr>()) |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1334 | return getInternalLinkageFor(D); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1335 | |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1336 | // Objective-C: treat all Objective-C declarations as having external |
| 1337 | // linkage. |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1338 | switch (D->getKind()) { |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1339 | default: |
| 1340 | break; |
Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1341 | |
| 1342 | // Per C++ [basic.link]p2, only the names of objects, references, |
| 1343 | // functions, types, templates, namespaces, and values ever have linkage. |
| 1344 | // |
| 1345 | // Note that the name of a typedef, namespace alias, using declaration, |
| 1346 | // and so on are not the name of the corresponding type, namespace, or |
| 1347 | // declaration, so they do *not* have linkage. |
Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1348 | case Decl::ImplicitParam: |
| 1349 | case Decl::Label: |
| 1350 | case Decl::NamespaceAlias: |
Argyrios Kyrtzidis | 79d0428 | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 1351 | case Decl::ParmVar: |
Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1352 | case Decl::Using: |
| 1353 | case Decl::UsingShadow: |
| 1354 | case Decl::UsingDirective: |
Argyrios Kyrtzidis | 79d0428 | 2011-12-01 01:28:21 +0000 | [diff] [blame] | 1355 | return LinkageInfo::none(); |
Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1356 | |
Richard Smith | 26210db | 2015-11-13 03:52:13 +0000 | [diff] [blame] | 1357 | case Decl::EnumConstant: |
| 1358 | // 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] | 1359 | if (D->getASTContext().getLangOpts().CPlusPlus) |
| 1360 | return getLVForDecl(cast<EnumDecl>(D->getDeclContext()), computation); |
| 1361 | return LinkageInfo::visible_none(); |
Richard Smith | 26210db | 2015-11-13 03:52:13 +0000 | [diff] [blame] | 1362 | |
Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1363 | case Decl::Typedef: |
| 1364 | case Decl::TypeAlias: |
| 1365 | // A typedef declaration has linkage if it gives a type a name for |
| 1366 | // linkage purposes. |
Vassil Vassilev | b00ea08 | 2017-07-01 20:44:49 +0000 | [diff] [blame] | 1367 | if (!cast<TypedefNameDecl>(D) |
Richard Smith | 97135cc | 2015-11-12 22:19:45 +0000 | [diff] [blame] | 1368 | ->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
| 1369 | return LinkageInfo::none(); |
| 1370 | break; |
| 1371 | |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1372 | case Decl::TemplateTemplateParm: // count these as external |
| 1373 | case Decl::NonTypeTemplateParm: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1374 | case Decl::ObjCAtDefsField: |
| 1375 | case Decl::ObjCCategory: |
| 1376 | case Decl::ObjCCategoryImpl: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1377 | case Decl::ObjCCompatibleAlias: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1378 | case Decl::ObjCImplementation: |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1379 | case Decl::ObjCMethod: |
| 1380 | case Decl::ObjCProperty: |
| 1381 | case Decl::ObjCPropertyImpl: |
| 1382 | case Decl::ObjCProtocol: |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1383 | return getExternalLinkageFor(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1384 | |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1385 | case Decl::CXXRecord: { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1386 | const auto *Record = cast<CXXRecordDecl>(D); |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1387 | if (Record->isLambda()) { |
| 1388 | if (!Record->getLambdaManglingNumber()) { |
| 1389 | // This lambda has no mangling number, so it's internal. |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1390 | return getInternalLinkageFor(D); |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1391 | } |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1392 | |
Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1393 | // This lambda has its linkage/visibility determined: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1394 | // - either by the outermost lambda if that lambda has no mangling |
| 1395 | // number. |
Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1396 | // - or by the parent of the outer most lambda |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1397 | // This prevents infinite recursion in settings such as nested lambdas |
| 1398 | // used in NSDMI's, for e.g. |
Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1399 | // struct L { |
| 1400 | // int t{}; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1401 | // int t2 = ([](int a) { return [](int b) { return b; };})(t)(t); |
Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1402 | // }; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1403 | const CXXRecordDecl *OuterMostLambda = |
Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1404 | getOutermostEnclosingLambda(Record); |
| 1405 | if (!OuterMostLambda->getLambdaManglingNumber()) |
Richard Smith | 1283e98 | 2017-07-07 20:04:28 +0000 | [diff] [blame] | 1406 | return getInternalLinkageFor(D); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1407 | |
Faisal Vali | 49b4c1f | 2013-10-01 02:51:53 +0000 | [diff] [blame] | 1408 | return getLVForClosure( |
| 1409 | OuterMostLambda->getDeclContext()->getRedeclContext(), |
| 1410 | OuterMostLambda->getLambdaContextDecl(), computation); |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1411 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1412 | |
Douglas Gregor | 6f88e5e | 2012-02-21 04:17:39 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | } |
Ted Kremenek | 926d860 | 2010-04-20 23:15:35 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1417 | // Handle linkage for namespace-scope names. |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1418 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1419 | return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1420 | |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1421 | // C++ [basic.link]p5: |
| 1422 | // In addition, a member function, static data member, a named |
| 1423 | // class or enumeration of class scope, or an unnamed class or |
| 1424 | // enumeration defined in a class-scope typedef declaration such |
| 1425 | // that the class or enumeration has the typedef name for linkage |
| 1426 | // purposes (7.1.3), has external linkage if the name of the class |
| 1427 | // has external linkage. |
John McCall | 033caa5 | 2010-10-29 00:29:13 +0000 | [diff] [blame] | 1428 | if (D->getDeclContext()->isRecord()) |
Richard Smith | c95d2c5 | 2017-09-22 04:25:05 +0000 | [diff] [blame] | 1429 | return getLVForClassMember(D, computation, IgnoreVarTypeLinkage); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1430 | |
| 1431 | // C++ [basic.link]p6: |
| 1432 | // The name of a function declared in block scope and the name of |
| 1433 | // an object declared by a block scope extern declaration have |
| 1434 | // linkage. If there is a visible declaration of an entity with |
| 1435 | // linkage having the same name and type, ignoring entities |
| 1436 | // declared outside the innermost enclosing namespace scope, the |
| 1437 | // block scope declaration declares that same entity and receives |
| 1438 | // the linkage of the previous declaration. If there is more than |
| 1439 | // one such matching entity, the program is ill-formed. Otherwise, |
| 1440 | // if no matching entity is found, the block scope entity receives |
| 1441 | // external linkage. |
John McCall | df25c43 | 2013-02-16 00:17:33 +0000 | [diff] [blame] | 1442 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 1443 | return getLVForLocalDecl(D, computation); |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1444 | |
| 1445 | // C++ [basic.link]p6: |
| 1446 | // Names not covered by these rules have no linkage. |
John McCall | c273f24 | 2010-10-30 11:50:40 +0000 | [diff] [blame] | 1447 | return LinkageInfo::none(); |
John McCall | 457a04e | 2010-10-22 21:05:15 +0000 | [diff] [blame] | 1448 | } |
Douglas Gregor | f73b282 | 2009-11-25 22:24:25 +0000 | [diff] [blame] | 1449 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1450 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
| 1451 | LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D, |
| 1452 | LVComputationKind computation) { |
| 1453 | // Internal_linkage attribute overrides other considerations. |
| 1454 | if (D->hasAttr<InternalLinkageAttr>()) |
| 1455 | return getInternalLinkageFor(D); |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1456 | |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1457 | if (computation.IgnoreAllVisibility && D->hasCachedLinkage()) |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1458 | return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); |
Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1459 | |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 1460 | if (llvm::Optional<LinkageInfo> LI = lookup(D, computation)) |
| 1461 | return *LI; |
| 1462 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1463 | LinkageInfo LV = computeLVForDecl(D, computation); |
| 1464 | if (D->hasCachedLinkage()) |
| 1465 | assert(D->getCachedLinkage() == LV.getLinkage()); |
Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1466 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1467 | D->setCachedLinkage(LV.getLinkage()); |
George Burgess IV | 35cb4f8 | 2017-08-09 04:12:17 +0000 | [diff] [blame] | 1468 | cache(D, computation, LV); |
Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1469 | |
| 1470 | #ifndef NDEBUG |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1471 | // In C (because of gnu inline) and in c++ with microsoft extensions an |
| 1472 | // static can follow an extern, so we can have two decls with different |
| 1473 | // linkages. |
| 1474 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 1475 | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
| 1476 | return LV; |
Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1477 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1478 | // We have just computed the linkage for this decl. By induction we know |
| 1479 | // that all other computed linkages match, check that the one we just |
| 1480 | // computed also does. |
| 1481 | NamedDecl *Old = nullptr; |
| 1482 | for (auto I : D->redecls()) { |
| 1483 | auto *T = cast<NamedDecl>(I); |
| 1484 | if (T == D) |
| 1485 | continue; |
| 1486 | if (!T->isInvalidDecl() && T->hasCachedLinkage()) { |
| 1487 | Old = T; |
| 1488 | break; |
Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1489 | } |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1490 | } |
| 1491 | assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); |
Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1492 | #endif |
| 1493 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1494 | return LV; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 1495 | } |
Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1496 | |
George Burgess IV | 99db3ea | 2017-08-09 04:02:49 +0000 | [diff] [blame] | 1497 | LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { |
Richard Smith | fbe7b46 | 2017-09-22 02:22:32 +0000 | [diff] [blame] | 1498 | return getLVForDecl(D, |
| 1499 | LVComputationKind(usesTypeVisibility(D) |
| 1500 | ? NamedDecl::VisibilityForType |
| 1501 | : NamedDecl::VisibilityForValue)); |
Rafael Espindola | 9551d3b | 2013-05-28 19:43:11 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1504 | Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { |
Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1505 | Module *M = getOwningModule(); |
| 1506 | if (!M) |
| 1507 | return nullptr; |
| 1508 | |
| 1509 | switch (M->Kind) { |
| 1510 | case Module::ModuleMapModule: |
| 1511 | // Module map modules have no special linkage semantics. |
| 1512 | return nullptr; |
| 1513 | |
| 1514 | case Module::ModuleInterfaceUnit: |
| 1515 | return M; |
| 1516 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1517 | case Module::GlobalModuleFragment: { |
Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1518 | // External linkage declarations in the global module have no owning module |
| 1519 | // for linkage purposes. But internal linkage declarations in the global |
| 1520 | // module fragment of a particular module are owned by that module for |
| 1521 | // linkage purposes. |
Richard Smith | becb92d | 2017-10-10 22:33:17 +0000 | [diff] [blame] | 1522 | if (IgnoreLinkage) |
| 1523 | return nullptr; |
| 1524 | bool InternalLinkage; |
| 1525 | if (auto *ND = dyn_cast<NamedDecl>(this)) |
| 1526 | InternalLinkage = !ND->hasExternalFormalLinkage(); |
| 1527 | else { |
| 1528 | auto *NSD = dyn_cast<NamespaceDecl>(this); |
| 1529 | InternalLinkage = (NSD && NSD->isAnonymousNamespace()) || |
| 1530 | isInAnonymousNamespace(); |
| 1531 | } |
| 1532 | return InternalLinkage ? M->Parent : nullptr; |
Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1533 | } |
Richard Smith | a5bbbfe | 2019-04-18 21:12:54 +0000 | [diff] [blame] | 1534 | |
| 1535 | case Module::PrivateModuleFragment: |
| 1536 | // The private module fragment is part of its containing module for linkage |
| 1537 | // purposes. |
| 1538 | return M->Parent; |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1539 | } |
Richard Smith | dd8b533 | 2017-09-04 05:37:53 +0000 | [diff] [blame] | 1540 | |
| 1541 | llvm_unreachable("unknown module kind"); |
| 1542 | } |
| 1543 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1544 | void NamedDecl::printName(raw_ostream &os) const { |
| 1545 | os << Name; |
| 1546 | } |
| 1547 | |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1548 | std::string NamedDecl::getQualifiedNameAsString() const { |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1549 | std::string QualName; |
| 1550 | llvm::raw_string_ostream OS(QualName); |
Aaron Ballman | 75ee4cc | 2014-01-03 18:42:48 +0000 | [diff] [blame] | 1551 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1552 | return OS.str(); |
| 1553 | } |
| 1554 | |
| 1555 | void NamedDecl::printQualifiedName(raw_ostream &OS) const { |
| 1556 | printQualifiedName(OS, getASTContext().getPrintingPolicy()); |
| 1557 | } |
| 1558 | |
| 1559 | void NamedDecl::printQualifiedName(raw_ostream &OS, |
| 1560 | const PrintingPolicy &P) const { |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1561 | const DeclContext *Ctx = getDeclContext(); |
| 1562 | |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 1563 | // For ObjC methods and properties, look through categories and use the |
| 1564 | // interface as context. |
Argyrios Kyrtzidis | a166a2b | 2017-03-07 09:26:07 +0000 | [diff] [blame] | 1565 | if (auto *MD = dyn_cast<ObjCMethodDecl>(this)) |
| 1566 | if (auto *ID = MD->getClassInterface()) |
| 1567 | Ctx = ID; |
David Goldman | 19d2185 | 2019-04-04 20:13:22 +0000 | [diff] [blame] | 1568 | if (auto *PD = dyn_cast<ObjCPropertyDecl>(this)) { |
| 1569 | if (auto *MD = PD->getGetterMethodDecl()) |
| 1570 | if (auto *ID = MD->getClassInterface()) |
| 1571 | Ctx = ID; |
| 1572 | } |
Argyrios Kyrtzidis | a166a2b | 2017-03-07 09:26:07 +0000 | [diff] [blame] | 1573 | |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1574 | if (Ctx->isFunctionOrMethod()) { |
| 1575 | printName(OS); |
| 1576 | return; |
| 1577 | } |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1578 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1579 | using ContextsTy = SmallVector<const DeclContext *, 8>; |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1580 | ContextsTy Contexts; |
| 1581 | |
Sam McCall | 34f9d3f | 2018-02-02 13:34:47 +0000 | [diff] [blame] | 1582 | // Collect named contexts. |
| 1583 | while (Ctx) { |
| 1584 | if (isa<NamedDecl>(Ctx)) |
| 1585 | Contexts.push_back(Ctx); |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1586 | Ctx = Ctx->getParent(); |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1587 | } |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1588 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1589 | for (const DeclContext *DC : llvm::reverse(Contexts)) { |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1590 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(DC)) { |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 1591 | OS << Spec->getName(); |
Douglas Gregor | 8567358 | 2009-05-18 17:01:57 +0000 | [diff] [blame] | 1592 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 1593 | printTemplateArgumentList(OS, TemplateArgs.asArray(), P); |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1594 | } else if (const auto *ND = dyn_cast<NamespaceDecl>(DC)) { |
Richard Smith | 46dd5bb | 2014-05-30 22:16:51 +0000 | [diff] [blame] | 1595 | if (P.SuppressUnwrittenScope && |
| 1596 | (ND->isAnonymousNamespace() || ND->isInline())) |
| 1597 | continue; |
Reid Kleckner | 6010338 | 2015-12-16 02:04:40 +0000 | [diff] [blame] | 1598 | if (ND->isAnonymousNamespace()) { |
| 1599 | OS << (P.MSVCFormatting ? "`anonymous namespace\'" |
| 1600 | : "(anonymous namespace)"); |
| 1601 | } |
Sam Weinig | 07d211e | 2009-12-24 23:15:03 +0000 | [diff] [blame] | 1602 | else |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1603 | OS << *ND; |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1604 | } else if (const auto *RD = dyn_cast<RecordDecl>(DC)) { |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1605 | if (!RD->getIdentifier()) |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 1606 | OS << "(anonymous " << RD->getKindName() << ')'; |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1607 | else |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1608 | OS << *RD; |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1609 | } else if (const auto *FD = dyn_cast<FunctionDecl>(DC)) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1610 | const FunctionProtoType *FT = nullptr; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1611 | if (FD->hasWrittenPrototype()) |
Eli Friedman | 5c27c4c | 2012-08-30 22:22:09 +0000 | [diff] [blame] | 1612 | FT = dyn_cast<FunctionProtoType>(FD->getType()->castAs<FunctionType>()); |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1613 | |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1614 | OS << *FD << '('; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1615 | if (FT) { |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1616 | unsigned NumParams = FD->getNumParams(); |
| 1617 | for (unsigned i = 0; i < NumParams; ++i) { |
| 1618 | if (i) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1619 | OS << ", "; |
Argyrios Kyrtzidis | a18347e | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 1620 | OS << FD->getParamDecl(i)->getType().stream(P); |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | if (FT->isVariadic()) { |
| 1624 | if (NumParams > 0) |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1625 | OS << ", "; |
| 1626 | OS << "..."; |
Sam Weinig | b999f68 | 2009-12-28 03:19:38 +0000 | [diff] [blame] | 1627 | } |
| 1628 | } |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1629 | OS << ')'; |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1630 | } else if (const auto *ED = dyn_cast<EnumDecl>(DC)) { |
Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 1631 | // C++ [dcl.enum]p10: Each enum-name and each unscoped |
| 1632 | // enumerator is declared in the scope that immediately contains |
| 1633 | // the enum-specifier. Each scoped enumerator is declared in the |
| 1634 | // scope of the enumeration. |
Paul Robinson | f183848 | 2017-12-21 21:47:22 +0000 | [diff] [blame] | 1635 | // For the case of unscoped enumerator, do not include in the qualified |
Paul Robinson | d1c3dd8 | 2017-12-26 18:01:19 +0000 | [diff] [blame] | 1636 | // name any information about its enum enclosing scope, as its visibility |
Paul Robinson | f183848 | 2017-12-21 21:47:22 +0000 | [diff] [blame] | 1637 | // is global. |
| 1638 | if (ED->isScoped()) |
Alexander Kornienko | 4f35532 | 2015-11-09 16:45:17 +0000 | [diff] [blame] | 1639 | OS << *ED; |
| 1640 | else |
| 1641 | continue; |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1642 | } else { |
David Majnemer | f7e3609 | 2016-06-23 00:15:04 +0000 | [diff] [blame] | 1643 | OS << *cast<NamedDecl>(DC); |
Benjamin Kramer | d76b698 | 2010-04-28 14:33:51 +0000 | [diff] [blame] | 1644 | } |
| 1645 | OS << "::"; |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 1648 | if (getDeclName() || isa<DecompositionDecl>(this)) |
Benjamin Kramer | b89514a | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 1649 | OS << *this; |
John McCall | a2a3f7d | 2010-03-16 21:48:18 +0000 | [diff] [blame] | 1650 | else |
David Blaikie | abe1a39 | 2014-04-02 05:58:29 +0000 | [diff] [blame] | 1651 | OS << "(anonymous)"; |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1652 | } |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1653 | |
Benjamin Kramer | 24ebf7c | 2013-02-23 13:53:57 +0000 | [diff] [blame] | 1654 | void NamedDecl::getNameForDiagnostic(raw_ostream &OS, |
| 1655 | const PrintingPolicy &Policy, |
| 1656 | bool Qualified) const { |
| 1657 | if (Qualified) |
| 1658 | printQualifiedName(OS, Policy); |
| 1659 | else |
| 1660 | printName(OS); |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1663 | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { |
| 1664 | return true; |
| 1665 | } |
| 1666 | static bool isRedeclarableImpl(...) { return false; } |
| 1667 | static bool isRedeclarable(Decl::Kind K) { |
| 1668 | switch (K) { |
| 1669 | #define DECL(Type, Base) \ |
| 1670 | case Decl::Type: \ |
| 1671 | return isRedeclarableImpl((Type##Decl *)nullptr); |
| 1672 | #define ABSTRACT_DECL(DECL) |
| 1673 | #include "clang/AST/DeclNodes.inc" |
| 1674 | } |
| 1675 | llvm_unreachable("unknown decl kind"); |
| 1676 | } |
| 1677 | |
| 1678 | bool NamedDecl::declarationReplaces(NamedDecl *OldD, bool IsKnownNewer) const { |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1679 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch"); |
| 1680 | |
Richard Smith | fe620d2 | 2015-03-05 23:24:12 +0000 | [diff] [blame] | 1681 | // Never replace one imported declaration with another; we need both results |
| 1682 | // when re-exporting. |
| 1683 | if (OldD->isFromASTFile() && isFromASTFile()) |
| 1684 | return false; |
| 1685 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1686 | // A kind mismatch implies that the declaration is not replaced. |
| 1687 | if (OldD->getKind() != getKind()) |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1688 | return false; |
| 1689 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1690 | // For method declarations, we never replace. (Why?) |
| 1691 | if (isa<ObjCMethodDecl>(this)) |
| 1692 | return false; |
| 1693 | |
| 1694 | // For parameters, pick the newer one. This is either an error or (in |
| 1695 | // Objective-C) permitted as an extension. |
| 1696 | if (isa<ParmVarDecl>(this)) |
| 1697 | return true; |
| 1698 | |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1699 | // Inline namespaces can give us two declarations with the same |
| 1700 | // name and kind in the same scope but different contexts; we should |
| 1701 | // keep both declarations in this case. |
| 1702 | if (!this->getDeclContext()->getRedeclContext()->Equals( |
| 1703 | OldD->getDeclContext()->getRedeclContext())) |
| 1704 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1705 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1706 | // Using declarations can be replaced if they import the same name from the |
| 1707 | // same context. |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1708 | if (auto *UD = dyn_cast<UsingDecl>(this)) { |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1709 | ASTContext &Context = getASTContext(); |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1710 | return Context.getCanonicalNestedNameSpecifier(UD->getQualifier()) == |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1711 | Context.getCanonicalNestedNameSpecifier( |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1712 | cast<UsingDecl>(OldD)->getQualifier()); |
Douglas Gregor | a9d87bc | 2011-02-25 00:36:19 +0000 | [diff] [blame] | 1713 | } |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1714 | if (auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(this)) { |
Eli Friedman | 0eaf10b | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1715 | ASTContext &Context = getASTContext(); |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1716 | return Context.getCanonicalNestedNameSpecifier(UUVD->getQualifier()) == |
Eli Friedman | 0eaf10b | 2013-08-20 00:39:40 +0000 | [diff] [blame] | 1717 | Context.getCanonicalNestedNameSpecifier( |
| 1718 | cast<UnresolvedUsingValueDecl>(OldD)->getQualifier()); |
| 1719 | } |
| 1720 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1721 | if (isRedeclarable(getKind())) { |
| 1722 | if (getCanonicalDecl() != OldD->getCanonicalDecl()) |
| 1723 | return false; |
| 1724 | |
| 1725 | if (IsKnownNewer) |
| 1726 | return true; |
| 1727 | |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1728 | // Check whether this is actually newer than OldD. We want to keep the |
| 1729 | // newer declaration. This loop will usually only iterate once, because |
| 1730 | // OldD is usually the previous declaration. |
| 1731 | for (auto D : redecls()) { |
| 1732 | if (D == OldD) |
| 1733 | break; |
| 1734 | |
| 1735 | // If we reach the canonical declaration, then OldD is not actually older |
| 1736 | // than this one. |
| 1737 | // |
| 1738 | // FIXME: In this case, we should not add this decl to the lookup table. |
| 1739 | if (D->isCanonicalDecl()) |
| 1740 | return false; |
| 1741 | } |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1742 | |
| 1743 | // It's a newer declaration of the same kind of declaration in the same |
| 1744 | // scope: we want this decl instead of the existing one. |
| 1745 | return true; |
Richard Smith | e8292b1 | 2015-02-10 03:28:10 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Richard Smith | 5cd86f8 | 2015-11-03 03:13:11 +0000 | [diff] [blame] | 1748 | // In all other cases, we need to keep both declarations in case they have |
| 1749 | // different visibility. Any attempt to use the name will result in an |
| 1750 | // ambiguity if more than one is visible. |
| 1751 | return false; |
Douglas Gregor | 8b9ccca | 2008-12-23 21:05:05 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1754 | bool NamedDecl::hasLinkage() const { |
Rafael Espindola | 50df3a0 | 2013-05-25 17:16:20 +0000 | [diff] [blame] | 1755 | return getFormalLinkage() != NoLinkage; |
Douglas Gregor | eddf433 | 2009-02-24 20:03:32 +0000 | [diff] [blame] | 1756 | } |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 1757 | |
Daniel Dunbar | 166ea9ad | 2012-03-08 18:20:41 +0000 | [diff] [blame] | 1758 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1759 | NamedDecl *ND = this; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1760 | while (auto *UD = dyn_cast<UsingShadowDecl>(ND)) |
Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1761 | ND = UD->getTargetDecl(); |
| 1762 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1763 | if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(ND)) |
Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1764 | return AD->getClassInterface(); |
| 1765 | |
Richard Smith | f2005d3 | 2015-12-29 23:34:32 +0000 | [diff] [blame] | 1766 | if (auto *AD = dyn_cast<NamespaceAliasDecl>(ND)) |
| 1767 | return AD->getNamespace(); |
| 1768 | |
Benjamin Kramer | ba0495a | 2012-03-08 21:00:45 +0000 | [diff] [blame] | 1769 | return ND; |
Anders Carlsson | 6915bf6 | 2009-06-26 06:29:23 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1772 | bool NamedDecl::isCXXInstanceMember() const { |
Douglas Gregor | 3f28ec2 | 2012-03-08 02:08:05 +0000 | [diff] [blame] | 1773 | if (!isCXXClassMember()) |
| 1774 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1775 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1776 | const NamedDecl *D = this; |
| 1777 | if (isa<UsingShadowDecl>(D)) |
| 1778 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1779 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 1780 | if (isa<FieldDecl>(D) || isa<IndirectFieldDecl>(D) || isa<MSPropertyDecl>(D)) |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1781 | return true; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1782 | if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(D->getAsFunction())) |
Alp Toker | a2794f9 | 2014-01-22 07:29:52 +0000 | [diff] [blame] | 1783 | return MD->isInstance(); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1784 | return false; |
| 1785 | } |
| 1786 | |
Argyrios Kyrtzidis | 9e59b57 | 2008-11-09 23:41:00 +0000 | [diff] [blame] | 1787 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1788 | // DeclaratorDecl Implementation |
| 1789 | //===----------------------------------------------------------------------===// |
| 1790 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1791 | template <typename DeclT> |
| 1792 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1793 | if (decl->getNumTemplateParameterLists() > 0) |
| 1794 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1795 | else |
| 1796 | return decl->getInnerLocStart(); |
| 1797 | } |
| 1798 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1799 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
John McCall | f7bcc81 | 2010-05-28 23:32:21 +0000 | [diff] [blame] | 1800 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1801 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1802 | return SourceLocation(); |
| 1803 | } |
| 1804 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1805 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 1806 | if (QualifierLoc) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1807 | // Make sure the extended decl info is allocated. |
| 1808 | if (!hasExtInfo()) { |
| 1809 | // Save (non-extended) type source info pointer. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1810 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1811 | // Allocate external info struct. |
| 1812 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1813 | // Restore savedTInfo into (extended) decl info. |
| 1814 | getExtInfo()->TInfo = savedTInfo; |
| 1815 | } |
| 1816 | // Set qualifier info. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 1817 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 1818 | } else { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1819 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1820 | if (hasExtInfo()) { |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1821 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 1822 | // Save type source info pointer. |
| 1823 | TypeSourceInfo *savedTInfo = getExtInfo()->TInfo; |
| 1824 | // Deallocate the extended decl info. |
| 1825 | getASTContext().Deallocate(getExtInfo()); |
| 1826 | // Restore savedTInfo into (non-extended) decl info. |
| 1827 | DeclInfo = savedTInfo; |
| 1828 | } |
| 1829 | else |
| 1830 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1835 | void DeclaratorDecl::setTemplateParameterListsInfo( |
| 1836 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 1837 | assert(!TPLists.empty()); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1838 | // Make sure the extended decl info is allocated. |
| 1839 | if (!hasExtInfo()) { |
| 1840 | // Save (non-extended) type source info pointer. |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 1841 | auto *savedTInfo = DeclInfo.get<TypeSourceInfo*>(); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1842 | // Allocate external info struct. |
| 1843 | DeclInfo = new (getASTContext()) ExtInfo; |
| 1844 | // Restore savedTInfo into (extended) decl info. |
| 1845 | getExtInfo()->TInfo = savedTInfo; |
| 1846 | } |
| 1847 | // Set the template parameter lists info. |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1848 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1851 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 1852 | return getTemplateOrInnerLocStart(this); |
| 1853 | } |
| 1854 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1855 | // Helper function: returns true if QT is or contains a type |
| 1856 | // having a postfix component. |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1857 | static bool typeIsPostfix(QualType QT) { |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1858 | while (true) { |
| 1859 | const Type* T = QT.getTypePtr(); |
| 1860 | switch (T->getTypeClass()) { |
| 1861 | default: |
| 1862 | return false; |
| 1863 | case Type::Pointer: |
| 1864 | QT = cast<PointerType>(T)->getPointeeType(); |
| 1865 | break; |
| 1866 | case Type::BlockPointer: |
| 1867 | QT = cast<BlockPointerType>(T)->getPointeeType(); |
| 1868 | break; |
| 1869 | case Type::MemberPointer: |
| 1870 | QT = cast<MemberPointerType>(T)->getPointeeType(); |
| 1871 | break; |
| 1872 | case Type::LValueReference: |
| 1873 | case Type::RValueReference: |
| 1874 | QT = cast<ReferenceType>(T)->getPointeeType(); |
| 1875 | break; |
| 1876 | case Type::PackExpansion: |
| 1877 | QT = cast<PackExpansionType>(T)->getPattern(); |
| 1878 | break; |
| 1879 | case Type::Paren: |
| 1880 | case Type::ConstantArray: |
| 1881 | case Type::DependentSizedArray: |
| 1882 | case Type::IncompleteArray: |
| 1883 | case Type::VariableArray: |
| 1884 | case Type::FunctionProto: |
| 1885 | case Type::FunctionNoProto: |
| 1886 | return true; |
| 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1891 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 1892 | SourceLocation RangeEnd = getLocation(); |
| 1893 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
Benjamin Kramer | 3a7cc81 | 2014-02-02 15:28:46 +0000 | [diff] [blame] | 1894 | // If the declaration has no name or the type extends past the name take the |
| 1895 | // end location of the type. |
| 1896 | if (!getDeclName() || typeIsPostfix(TInfo->getType())) |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 1897 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 1898 | } |
| 1899 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 1900 | } |
| 1901 | |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1902 | void QualifierInfo::setTemplateParameterListsInfo( |
| 1903 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1904 | // Free previous template parameters (if any). |
| 1905 | if (NumTemplParamLists > 0) { |
Douglas Gregor | 20527e2 | 2010-06-15 17:44:38 +0000 | [diff] [blame] | 1906 | Context.Deallocate(TemplParamLists); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1907 | TemplParamLists = nullptr; |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1908 | NumTemplParamLists = 0; |
| 1909 | } |
| 1910 | // Set info on matched template parameter lists (if any). |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 1911 | if (!TPLists.empty()) { |
| 1912 | TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; |
| 1913 | NumTemplParamLists = TPLists.size(); |
| 1914 | std::copy(TPLists.begin(), TPLists.end(), TemplParamLists); |
Abramo Bagnara | da41d0c | 2010-06-12 08:15:14 +0000 | [diff] [blame] | 1915 | } |
| 1916 | } |
| 1917 | |
Argyrios Kyrtzidis | 6032ef1 | 2009-08-21 00:31:54 +0000 | [diff] [blame] | 1918 | //===----------------------------------------------------------------------===// |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 1919 | // VarDecl Implementation |
| 1920 | //===----------------------------------------------------------------------===// |
| 1921 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1922 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 1923 | switch (SC) { |
Peter Collingbourne | 2dbb708 | 2011-09-19 21:14:35 +0000 | [diff] [blame] | 1924 | case SC_None: break; |
Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1925 | case SC_Auto: return "auto"; |
| 1926 | case SC_Extern: return "extern"; |
Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1927 | case SC_PrivateExtern: return "__private_extern__"; |
| 1928 | case SC_Register: return "register"; |
| 1929 | case SC_Static: return "static"; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
Peter Collingbourne | 9a8f153 | 2011-09-20 12:40:26 +0000 | [diff] [blame] | 1932 | llvm_unreachable("Invalid storage class"); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1935 | VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 1936 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 1937 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| 1938 | StorageClass SC) |
| 1939 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 1940 | redeclarable_base(C) { |
Benjamin Kramer | a939c23 | 2014-03-15 18:54:13 +0000 | [diff] [blame] | 1941 | static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), |
| 1942 | "VarDeclBitfields too large!"); |
| 1943 | static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), |
| 1944 | "ParmVarDeclBitfields too large!"); |
David Majnemer | fa7bc78 | 2015-05-19 00:57:16 +0000 | [diff] [blame] | 1945 | static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), |
| 1946 | "NonParmVarDeclBitfields too large!"); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1947 | AllBits = 0; |
| 1948 | VarDeclBits.SClass = SC; |
| 1949 | // Everything else is implicitly initialized to false. |
| 1950 | } |
| 1951 | |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1952 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, |
| 1953 | SourceLocation StartL, SourceLocation IdL, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1954 | IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 1955 | StorageClass S) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1956 | 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] | 1957 | } |
| 1958 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1959 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 1960 | return new (C, ID) |
| 1961 | VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
| 1962 | QualType(), nullptr, SC_None); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1965 | void VarDecl::setStorageClass(StorageClass SC) { |
| 1966 | assert(isLegalForVariable(SC)); |
John McCall | beaa11c | 2011-05-01 02:13:58 +0000 | [diff] [blame] | 1967 | VarDeclBits.SClass = SC; |
Douglas Gregor | bf62d64 | 2010-12-06 18:36:25 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1970 | VarDecl::TLSKind VarDecl::getTLSKind() const { |
| 1971 | switch (VarDeclBits.TSCSpec) { |
| 1972 | case TSCS_unspecified: |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1973 | if (!hasAttr<ThreadAttr>() && |
| 1974 | !(getASTContext().getLangOpts().OpenMPUseTLS && |
| 1975 | getASTContext().getTargetInfo().isTLSSupported() && |
| 1976 | hasAttr<OMPThreadPrivateDeclAttr>())) |
David Majnemer | 1b5baff | 2015-05-14 05:19:23 +0000 | [diff] [blame] | 1977 | return TLS_None; |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1978 | return ((getASTContext().getLangOpts().isCompatibleWithMSVC( |
| 1979 | LangOptions::MSVC2015)) || |
| 1980 | hasAttr<OMPThreadPrivateDeclAttr>()) |
David Majnemer | 1b5baff | 2015-05-14 05:19:23 +0000 | [diff] [blame] | 1981 | ? TLS_Dynamic |
| 1982 | : TLS_Static; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1983 | case TSCS___thread: // Fall through. |
| 1984 | case TSCS__Thread_local: |
Samuel Antao | f8b5012 | 2015-07-13 22:54:53 +0000 | [diff] [blame] | 1985 | return TLS_Static; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 1986 | case TSCS_thread_local: |
| 1987 | return TLS_Dynamic; |
| 1988 | } |
| 1989 | llvm_unreachable("Unknown thread storage class specifier!"); |
| 1990 | } |
| 1991 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 1992 | SourceRange VarDecl::getSourceRange() const { |
Argyrios Kyrtzidis | e0d6497 | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1993 | if (const Expr *Init = getInit()) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1994 | SourceLocation InitEnd = Init->getEndLoc(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1995 | // If Init is implicit, ignore its source range and fallback on |
Nico Weber | bbe1394 | 2013-01-22 17:00:09 +0000 | [diff] [blame] | 1996 | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
| 1997 | if (InitEnd.isValid() && InitEnd != getLocation()) |
Argyrios Kyrtzidis | e0d6497 | 2012-10-08 23:08:41 +0000 | [diff] [blame] | 1998 | return SourceRange(getOuterLocStart(), InitEnd); |
| 1999 | } |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 2000 | return DeclaratorDecl::getSourceRange(); |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Rafael Espindola | 8851067 | 2013-01-04 21:18:45 +0000 | [diff] [blame] | 2003 | template<typename T> |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2004 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2005 | // C++ [dcl.link]p1: All function types, function names with external linkage, |
| 2006 | // and variable names with external linkage have a language linkage. |
Rafael Espindola | 3ae0005 | 2013-05-13 00:12:11 +0000 | [diff] [blame] | 2007 | if (!D.hasExternalFormalLinkage()) |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2008 | return NoLanguageLinkage; |
| 2009 | |
| 2010 | // 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] | 2011 | // C language linkage fits the implementation nicely. |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2012 | ASTContext &Context = D.getASTContext(); |
| 2013 | if (!Context.getLangOpts().CPlusPlus) |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2014 | return CLanguageLinkage; |
| 2015 | |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 2016 | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
| 2017 | // language linkage of the names of class members and the function type of |
| 2018 | // class member functions. |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2019 | const DeclContext *DC = D.getDeclContext(); |
| 2020 | if (DC->isRecord()) |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2021 | return CXXLanguageLinkage; |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2022 | |
| 2023 | // If the first decl is in an extern "C" context, any other redeclaration |
| 2024 | // will have C language linkage. If the first one is not in an extern "C" |
| 2025 | // 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] | 2026 | if (isFirstInExternCContext(&D)) |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2027 | return CLanguageLinkage; |
| 2028 | return CXXLanguageLinkage; |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2031 | template<typename T> |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2032 | static bool isDeclExternC(const T &D) { |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2033 | // Since the context is ignored for class members, they can only have C++ |
| 2034 | // language linkage or no language linkage. |
| 2035 | const DeclContext *DC = D.getDeclContext(); |
| 2036 | if (DC->isRecord()) { |
| 2037 | assert(D.getASTContext().getLangOpts().CPlusPlus); |
| 2038 | return false; |
| 2039 | } |
| 2040 | |
| 2041 | return D.getLanguageLinkage() == CLanguageLinkage; |
| 2042 | } |
| 2043 | |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2044 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2045 | return getDeclLanguageLinkage(*this); |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2048 | bool VarDecl::isExternC() const { |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2049 | return isDeclExternC(*this); |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2052 | bool VarDecl::isInExternCContext() const { |
Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2053 | return getLexicalDeclContext()->isExternCContext(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | bool VarDecl::isInExternCXXContext() const { |
Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2057 | return getLexicalDeclContext()->isExternCXXContext(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2060 | VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2061 | |
Nico Weber | 7f5015a | 2015-04-15 21:53:00 +0000 | [diff] [blame] | 2062 | VarDecl::DefinitionKind |
| 2063 | VarDecl::isThisDeclarationADefinition(ASTContext &C) const { |
Richard Smith | a22d9a5 | 2017-12-02 00:55:48 +0000 | [diff] [blame] | 2064 | if (isThisDeclarationADemotedDefinition()) |
| 2065 | return DeclarationOnly; |
| 2066 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2067 | // C++ [basic.def]p2: |
| 2068 | // A declaration is a definition unless [...] it contains the 'extern' |
| 2069 | // specifier or a linkage-specification and neither an initializer [...], |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2070 | // it declares a non-inline static data member in a class declaration [...], |
| 2071 | // it declares a static data member outside a class definition and the variable |
| 2072 | // was defined within the class with the constexpr specifier [...], |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2073 | // C++1y [temp.expl.spec]p15: |
| 2074 | // An explicit specialization of a static data member or an explicit |
| 2075 | // specialization of a static data member template is a definition if the |
| 2076 | // declaration includes an initializer; otherwise, it is a declaration. |
| 2077 | // |
| 2078 | // FIXME: How do you declare (but not define) a partial specialization of |
| 2079 | // a static data member template outside the containing class? |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2080 | if (isStaticDataMember()) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2081 | if (isOutOfLine() && |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2082 | !(getCanonicalDecl()->isInline() && |
| 2083 | getCanonicalDecl()->isConstexpr()) && |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2084 | (hasInit() || |
| 2085 | // If the first declaration is out-of-line, this may be an |
| 2086 | // instantiation of an out-of-line partial specialization of a variable |
| 2087 | // template for which we have not yet instantiated the initializer. |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2088 | (getFirstDecl()->isOutOfLine() |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2089 | ? getTemplateSpecializationKind() == TSK_Undeclared |
| 2090 | : getTemplateSpecializationKind() != |
| 2091 | TSK_ExplicitSpecialization) || |
| 2092 | isa<VarTemplatePartialSpecializationDecl>(this))) |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2093 | return Definition; |
Richard Smith | 62f19e7 | 2016-06-25 00:15:56 +0000 | [diff] [blame] | 2094 | else if (!isOutOfLine() && isInline()) |
| 2095 | return Definition; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2096 | else |
| 2097 | return DeclarationOnly; |
| 2098 | } |
| 2099 | // C99 6.7p5: |
| 2100 | // A definition of an identifier is a declaration for that identifier that |
| 2101 | // [...] causes storage to be reserved for that object. |
| 2102 | // Note: that applies for all non-file-scope objects. |
| 2103 | // C99 6.9.2p1: |
| 2104 | // If the declaration of an identifier for an object has file scope and an |
| 2105 | // initializer, the declaration is an external definition for the identifier |
| 2106 | if (hasInit()) |
| 2107 | return Definition; |
Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2108 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 2109 | if (hasDefiningAttr()) |
Rafael Espindola | d53ffa0 | 2013-10-22 21:39:03 +0000 | [diff] [blame] | 2110 | return Definition; |
| 2111 | |
David Majnemer | dd0bed1 | 2015-05-14 05:19:20 +0000 | [diff] [blame] | 2112 | if (const auto *SAA = getAttr<SelectAnyAttr>()) |
| 2113 | if (!SAA->isInherited()) |
| 2114 | return Definition; |
| 2115 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2116 | // A variable template specialization (other than a static data member |
| 2117 | // template or an explicit specialization) is a declaration until we |
| 2118 | // instantiate its initializer. |
Richard Smith | 435e647 | 2017-12-02 02:48:42 +0000 | [diff] [blame] | 2119 | if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2120 | if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && |
| 2121 | !isa<VarTemplatePartialSpecializationDecl>(VTSD) && |
| 2122 | !VTSD->IsCompleteDefinition) |
| 2123 | return DeclarationOnly; |
| 2124 | } |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2125 | |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 2126 | if (hasExternalStorage()) |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2127 | return DeclarationOnly; |
Rafael Espindola | 8f326a5 | 2013-03-07 01:42:44 +0000 | [diff] [blame] | 2128 | |
Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2129 | // [dcl.link] p7: |
| 2130 | // A declaration directly contained in a linkage-specification is treated |
| 2131 | // as if it contains the extern specifier for the purpose of determining |
| 2132 | // the linkage of the declared name and whether it is a definition. |
Nico Weber | 608e768 | 2015-04-15 23:04:24 +0000 | [diff] [blame] | 2133 | if (isSingleLineLanguageLinkage(*this)) |
Rafael Espindola | 327be3c | 2013-04-26 01:30:23 +0000 | [diff] [blame] | 2134 | return DeclarationOnly; |
Rafael Espindola | bff5956 | 2013-04-25 12:11:36 +0000 | [diff] [blame] | 2135 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2136 | // C99 6.9.2p2: |
| 2137 | // A declaration of an object that has file scope without an initializer, |
| 2138 | // and without a storage class specifier or the scs 'static', constitutes |
| 2139 | // a tentative definition. |
| 2140 | // No such thing in C++. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2141 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2142 | return TentativeDefinition; |
| 2143 | |
| 2144 | // What's left is (in C, block-scope) declarations without initializers or |
| 2145 | // external storage. These are definitions. |
| 2146 | return Definition; |
| 2147 | } |
| 2148 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2149 | VarDecl *VarDecl::getActingDefinition() { |
| 2150 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 2151 | if (Kind != TentativeDefinition) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2152 | return nullptr; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2153 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2154 | VarDecl *LastTentative = nullptr; |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2155 | VarDecl *First = getFirstDecl(); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2156 | for (auto I : First->redecls()) { |
| 2157 | Kind = I->isThisDeclarationADefinition(); |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2158 | if (Kind == Definition) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2159 | return nullptr; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2160 | else if (Kind == TentativeDefinition) |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2161 | LastTentative = I; |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2162 | } |
| 2163 | return LastTentative; |
| 2164 | } |
| 2165 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2166 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2167 | VarDecl *First = getFirstDecl(); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2168 | for (auto I : First->redecls()) { |
| 2169 | if (I->isThisDeclarationADefinition(C) == Definition) |
| 2170 | return I; |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2171 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2172 | return nullptr; |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Daniel Dunbar | 9d35581 | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 2175 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 2176 | DefinitionKind Kind = DeclarationOnly; |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2177 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 2178 | const VarDecl *First = getFirstDecl(); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2179 | for (auto I : First->redecls()) { |
| 2180 | Kind = std::max(Kind, I->isThisDeclarationADefinition(C)); |
Daniel Dunbar | 082c62d | 2012-03-06 23:52:46 +0000 | [diff] [blame] | 2181 | if (Kind == Definition) |
| 2182 | break; |
| 2183 | } |
John McCall | 37bb6c9 | 2010-10-29 22:22:43 +0000 | [diff] [blame] | 2184 | |
| 2185 | return Kind; |
| 2186 | } |
| 2187 | |
Sebastian Redl | 5ca7984 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 2188 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2189 | for (auto I : redecls()) { |
| 2190 | if (auto Expr = I->getInit()) { |
| 2191 | D = I; |
| 2192 | return Expr; |
| 2193 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2194 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2195 | return nullptr; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2198 | bool VarDecl::hasInit() const { |
| 2199 | if (auto *P = dyn_cast<ParmVarDecl>(this)) |
| 2200 | if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg()) |
| 2201 | return false; |
| 2202 | |
| 2203 | return !Init.isNull(); |
| 2204 | } |
| 2205 | |
| 2206 | Expr *VarDecl::getInit() { |
| 2207 | if (!hasInit()) |
| 2208 | return nullptr; |
| 2209 | |
| 2210 | if (auto *S = Init.dyn_cast<Stmt *>()) |
| 2211 | return cast<Expr>(S); |
| 2212 | |
| 2213 | return cast_or_null<Expr>(Init.get<EvaluatedStmt *>()->Value); |
| 2214 | } |
| 2215 | |
| 2216 | Stmt **VarDecl::getInitAddress() { |
| 2217 | if (auto *ES = Init.dyn_cast<EvaluatedStmt *>()) |
| 2218 | return &ES->Value; |
| 2219 | |
| 2220 | return Init.getAddrOfPtr1(); |
| 2221 | } |
| 2222 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2223 | bool VarDecl::isOutOfLine() const { |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2224 | if (Decl::isOutOfLine()) |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2225 | return true; |
Chandler Carruth | f50ef6e | 2010-02-21 07:08:09 +0000 | [diff] [blame] | 2226 | |
| 2227 | if (!isStaticDataMember()) |
| 2228 | return false; |
| 2229 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2230 | // 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] | 2231 | // a class template, check whether that static data member was defined |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2232 | // out-of-line. |
| 2233 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 2234 | return VD->isOutOfLine(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2235 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2236 | return false; |
| 2237 | } |
| 2238 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2239 | void VarDecl::setInit(Expr *I) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2240 | if (auto *Eval = Init.dyn_cast<EvaluatedStmt *>()) { |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2241 | Eval->~EvaluatedStmt(); |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 2242 | getASTContext().Deallocate(Eval); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
| 2245 | Init = I; |
| 2246 | } |
| 2247 | |
Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 2248 | bool VarDecl::mightBeUsableInConstantExpressions(ASTContext &C) const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2249 | const LangOptions &Lang = C.getLangOpts(); |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2250 | |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2251 | if (!Lang.CPlusPlus) |
| 2252 | return false; |
| 2253 | |
Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 2254 | // Function parameters are never usable in constant expressions. |
| 2255 | if (isa<ParmVarDecl>(this)) |
| 2256 | return false; |
| 2257 | |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2258 | // In C++11, any variable of reference type can be used in a constant |
| 2259 | // expression if it is initialized by a constant expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2260 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2261 | return true; |
| 2262 | |
| 2263 | // 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] | 2264 | // not require the variable to be non-volatile, but we consider this to be a |
| 2265 | // defect. |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2266 | if (!getType().isConstQualified() || getType().isVolatileQualified()) |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2267 | return false; |
| 2268 | |
| 2269 | // In C++, const, non-volatile variables of integral or enumeration types |
| 2270 | // can be used in constant expressions. |
| 2271 | if (getType()->isIntegralOrEnumerationType()) |
| 2272 | return true; |
| 2273 | |
Richard Smith | 35ecb36 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 2274 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 2275 | // constant expressions. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2276 | return Lang.CPlusPlus11 && isConstexpr(); |
Richard Smith | 242ad89 | 2011-12-21 02:55:12 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Richard Smith | 715f7a1 | 2019-06-11 17:50:32 +0000 | [diff] [blame] | 2279 | bool VarDecl::isUsableInConstantExpressions(ASTContext &Context) const { |
| 2280 | // C++2a [expr.const]p3: |
| 2281 | // A variable is usable in constant expressions after its initializing |
| 2282 | // declaration is encountered... |
| 2283 | const VarDecl *DefVD = nullptr; |
| 2284 | const Expr *Init = getAnyInitializer(DefVD); |
| 2285 | if (!Init || Init->isValueDependent()) |
| 2286 | return false; |
| 2287 | // ... if it is a constexpr variable, or it is of reference type or of |
| 2288 | // const-qualified integral or enumeration type, ... |
| 2289 | if (!DefVD->mightBeUsableInConstantExpressions(Context)) |
| 2290 | return false; |
| 2291 | // ... and its initializer is a constant initializer. |
| 2292 | return DefVD->checkInitIsICE(); |
| 2293 | } |
| 2294 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2295 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 2296 | /// form, which contains extra information on the evaluated value of the |
| 2297 | /// initializer. |
| 2298 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2299 | auto *Eval = Init.dyn_cast<EvaluatedStmt *>(); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2300 | if (!Eval) { |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2301 | // Note: EvaluatedStmt contains an APValue, which usually holds |
| 2302 | // resources not allocated from the ASTContext. We need to do some |
| 2303 | // work to avoid leaking those, but we do so in VarDecl::evaluateValue |
| 2304 | // 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] | 2305 | Eval = new (getASTContext()) EvaluatedStmt; |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2306 | Eval->Value = Init.get<Stmt *>(); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2307 | Init = Eval; |
| 2308 | } |
| 2309 | return Eval; |
| 2310 | } |
| 2311 | |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2312 | APValue *VarDecl::evaluateValue() const { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2313 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | dafff94 | 2012-01-14 04:30:29 +0000 | [diff] [blame] | 2314 | return evaluateValue(Notes); |
| 2315 | } |
| 2316 | |
| 2317 | APValue *VarDecl::evaluateValue( |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2318 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2319 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2320 | |
| 2321 | // We only produce notes indicating why an initializer is non-constant the |
| 2322 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 2323 | // first time we try evaluation, so might not be produced at all. |
| 2324 | if (Eval->WasEvaluated) |
Richard Smith | e637cbe | 2019-05-21 23:15:18 +0000 | [diff] [blame] | 2325 | return Eval->Evaluated.isAbsent() ? nullptr : &Eval->Evaluated; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2326 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2327 | const auto *Init = cast<Expr>(Eval->Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2328 | assert(!Init->isValueDependent()); |
| 2329 | |
| 2330 | if (Eval->IsEvaluating) { |
| 2331 | // FIXME: Produce a diagnostic for self-initialization. |
| 2332 | Eval->CheckedICE = true; |
| 2333 | Eval->IsICE = false; |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2334 | return nullptr; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | Eval->IsEvaluating = true; |
| 2338 | |
| 2339 | bool Result = Init->EvaluateAsInitializer(Eval->Evaluated, getASTContext(), |
| 2340 | this, Notes); |
| 2341 | |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2342 | // Ensure the computed APValue is cleaned up later if evaluation succeeded, |
| 2343 | // or that it's empty (so that there's nothing to clean up) if evaluation |
| 2344 | // failed. |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2345 | if (!Result) |
| 2346 | Eval->Evaluated = APValue(); |
Manuel Klimek | a732899 | 2013-06-03 13:51:33 +0000 | [diff] [blame] | 2347 | else if (Eval->Evaluated.needsCleanup()) |
George Burgess IV | b61bfbd | 2017-02-14 05:37:36 +0000 | [diff] [blame] | 2348 | getASTContext().addDestruction(&Eval->Evaluated); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2349 | |
| 2350 | Eval->IsEvaluating = false; |
| 2351 | Eval->WasEvaluated = true; |
| 2352 | |
| 2353 | // In C++11, we have determined whether the initializer was a constant |
| 2354 | // expression as a side-effect. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2355 | if (getASTContext().getLangOpts().CPlusPlus11 && !Eval->CheckedICE) { |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2356 | Eval->CheckedICE = true; |
Eli Friedman | 8f66cdf | 2012-02-06 21:50:18 +0000 | [diff] [blame] | 2357 | Eval->IsICE = Result && Notes.empty(); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2360 | return Result ? &Eval->Evaluated : nullptr; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2363 | APValue *VarDecl::getEvaluatedValue() const { |
| 2364 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) |
| 2365 | if (Eval->WasEvaluated) |
| 2366 | return &Eval->Evaluated; |
| 2367 | |
| 2368 | return nullptr; |
| 2369 | } |
| 2370 | |
| 2371 | bool VarDecl::isInitKnownICE() const { |
| 2372 | if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) |
| 2373 | return Eval->CheckedICE; |
| 2374 | |
| 2375 | return false; |
| 2376 | } |
| 2377 | |
| 2378 | bool VarDecl::isInitICE() const { |
| 2379 | assert(isInitKnownICE() && |
| 2380 | "Check whether we already know that the initializer is an ICE"); |
| 2381 | return Init.get<EvaluatedStmt *>()->IsICE; |
| 2382 | } |
| 2383 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2384 | bool VarDecl::checkInitIsICE() const { |
John McCall | a59dc2f | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2385 | // Initializers of weak variables are never ICEs. |
| 2386 | if (isWeak()) |
| 2387 | return false; |
| 2388 | |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2389 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2390 | if (Eval->CheckedICE) |
| 2391 | // We have already checked whether this subexpression is an |
| 2392 | // integral constant expression. |
| 2393 | return Eval->IsICE; |
| 2394 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2395 | const auto *Init = cast<Expr>(Eval->Value); |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2396 | assert(!Init->isValueDependent()); |
| 2397 | |
| 2398 | // In C++11, evaluate the initializer to check whether it's a constant |
| 2399 | // expression. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2400 | if (getASTContext().getLangOpts().CPlusPlus11) { |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2401 | SmallVector<PartialDiagnosticAt, 8> Notes; |
Richard Smith | d0b4dd6 | 2011-12-19 06:19:21 +0000 | [diff] [blame] | 2402 | evaluateValue(Notes); |
| 2403 | return Eval->IsICE; |
| 2404 | } |
| 2405 | |
| 2406 | // It's an ICE whether or not the definition we found is |
| 2407 | // out-of-line. See DR 721 and the discussion in Clang PR |
| 2408 | // 6206 for details. |
| 2409 | |
| 2410 | if (Eval->CheckingICE) |
| 2411 | return false; |
| 2412 | Eval->CheckingICE = true; |
| 2413 | |
| 2414 | Eval->IsICE = Init->isIntegerConstantExpr(getASTContext()); |
| 2415 | Eval->CheckingICE = false; |
| 2416 | Eval->CheckedICE = true; |
| 2417 | return Eval->IsICE; |
| 2418 | } |
| 2419 | |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 2420 | bool VarDecl::isParameterPack() const { |
| 2421 | return isa<PackExpansionType>(getType()); |
| 2422 | } |
| 2423 | |
Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2424 | template<typename DeclT> |
| 2425 | static DeclT *getDefinitionOrSelf(DeclT *D) { |
| 2426 | assert(D); |
| 2427 | if (auto *Def = D->getDefinition()) |
| 2428 | return Def; |
| 2429 | return D; |
| 2430 | } |
| 2431 | |
Akira Hatanaka | 8e57b07 | 2018-10-01 21:51:28 +0000 | [diff] [blame] | 2432 | bool VarDecl::isEscapingByref() const { |
| 2433 | return hasAttr<BlocksAttr>() && NonParmVarDeclBits.EscapingByref; |
| 2434 | } |
| 2435 | |
| 2436 | bool VarDecl::isNonEscapingByref() const { |
| 2437 | return hasAttr<BlocksAttr>() && !NonParmVarDeclBits.EscapingByref; |
| 2438 | } |
| 2439 | |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2440 | VarDecl *VarDecl::getTemplateInstantiationPattern() const { |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2441 | const VarDecl *VD = this; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2442 | |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2443 | // If this is an instantiated member, walk back to the template from which |
| 2444 | // it was instantiated. |
| 2445 | if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) { |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2446 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2447 | VD = VD->getInstantiatedFromStaticDataMember(); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2448 | while (auto *NewVD = VD->getInstantiatedFromStaticDataMember()) |
| 2449 | VD = NewVD; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2450 | } |
| 2451 | } |
| 2452 | |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2453 | // If it's an instantiated variable template specialization, find the |
| 2454 | // template or partial specialization from which it was instantiated. |
| 2455 | if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(VD)) { |
| 2456 | if (isTemplateInstantiation(VDTemplSpec->getTemplateSpecializationKind())) { |
| 2457 | auto From = VDTemplSpec->getInstantiatedFrom(); |
| 2458 | if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) { |
| 2459 | while (!VTD->isMemberSpecialization()) { |
| 2460 | auto *NewVTD = VTD->getInstantiatedFromMemberTemplate(); |
| 2461 | if (!NewVTD) |
| 2462 | break; |
| 2463 | VTD = NewVTD; |
| 2464 | } |
| 2465 | return getDefinitionOrSelf(VTD->getTemplatedDecl()); |
| 2466 | } |
| 2467 | if (auto *VTPSD = |
| 2468 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 2469 | while (!VTPSD->isMemberSpecialization()) { |
| 2470 | auto *NewVTPSD = VTPSD->getInstantiatedFromMember(); |
| 2471 | if (!NewVTPSD) |
| 2472 | break; |
| 2473 | VTPSD = NewVTPSD; |
| 2474 | } |
| 2475 | return getDefinitionOrSelf<VarDecl>(VTPSD); |
| 2476 | } |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | // If this is the pattern of a variable template, find where it was |
| 2481 | // instantiated from. FIXME: Is this necessary? |
| 2482 | if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) { |
| 2483 | while (!VarTemplate->isMemberSpecialization()) { |
| 2484 | auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate(); |
| 2485 | if (!NewVT) |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2486 | break; |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2487 | VarTemplate = NewVT; |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 2490 | return getDefinitionOrSelf(VarTemplate->getTemplatedDecl()); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2491 | } |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2492 | |
| 2493 | if (VD == this) |
| 2494 | return nullptr; |
| 2495 | return getDefinitionOrSelf(const_cast<VarDecl*>(VD)); |
Richard Smith | edbc6e9 | 2016-10-14 21:41:24 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2498 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2499 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2500 | return cast<VarDecl>(MSI->getInstantiatedFrom()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2501 | |
| 2502 | return nullptr; |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Douglas Gregor | 3c74d41 | 2009-10-14 20:14:33 +0000 | [diff] [blame] | 2505 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2506 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2507 | return Spec->getSpecializationKind(); |
| 2508 | |
Sebastian Redl | 35351a9 | 2010-01-31 22:27:38 +0000 | [diff] [blame] | 2509 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2510 | return MSI->getTemplateSpecializationKind(); |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2511 | |
Douglas Gregor | 86d142a | 2009-10-08 07:24:58 +0000 | [diff] [blame] | 2512 | return TSK_Undeclared; |
| 2513 | } |
| 2514 | |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2515 | TemplateSpecializationKind |
| 2516 | VarDecl::getTemplateSpecializationKindForInstantiation() const { |
| 2517 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2518 | return MSI->getTemplateSpecializationKind(); |
| 2519 | |
| 2520 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
| 2521 | return Spec->getSpecializationKind(); |
| 2522 | |
| 2523 | return TSK_Undeclared; |
| 2524 | } |
| 2525 | |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2526 | SourceLocation VarDecl::getPointOfInstantiation() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2527 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(this)) |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2528 | return Spec->getPointOfInstantiation(); |
| 2529 | |
| 2530 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2531 | return MSI->getPointOfInstantiation(); |
| 2532 | |
| 2533 | return SourceLocation(); |
| 2534 | } |
| 2535 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2536 | VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { |
| 2537 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2538 | .dyn_cast<VarTemplateDecl *>(); |
| 2539 | } |
| 2540 | |
| 2541 | void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { |
| 2542 | getASTContext().setTemplateOrSpecializationInfo(this, Template); |
| 2543 | } |
| 2544 | |
Justin Lebar | 5489f85 | 2018-05-17 16:15:07 +0000 | [diff] [blame] | 2545 | bool VarDecl::isKnownToBeDefined() const { |
| 2546 | const auto &LangOpts = getASTContext().getLangOpts(); |
| 2547 | // In CUDA mode without relocatable device code, variables of form 'extern |
| 2548 | // __shared__ Foo foo[]' are pointers to the base of the GPU core's shared |
| 2549 | // memory pool. These are never undefined variables, even if they appear |
| 2550 | // inside of an anon namespace or static function. |
| 2551 | // |
| 2552 | // With CUDA relocatable device code enabled, these variables don't get |
| 2553 | // special handling; they're treated like regular extern variables. |
Yaxun Liu | 9767089 | 2018-10-02 17:48:54 +0000 | [diff] [blame] | 2554 | if (LangOpts.CUDA && !LangOpts.GPURelocatableDeviceCode && |
Justin Lebar | 5489f85 | 2018-05-17 16:15:07 +0000 | [diff] [blame] | 2555 | hasExternalStorage() && hasAttr<CUDASharedAttr>() && |
| 2556 | isa<IncompleteArrayType>(getType())) |
| 2557 | return true; |
| 2558 | |
| 2559 | return hasDefinition(); |
| 2560 | } |
| 2561 | |
Erik Pilkington | 5a559e6 | 2018-08-21 17:24:06 +0000 | [diff] [blame] | 2562 | bool VarDecl::isNoDestroy(const ASTContext &Ctx) const { |
| 2563 | return hasGlobalStorage() && (hasAttr<NoDestroyAttr>() || |
| 2564 | (!Ctx.getLangOpts().RegisterStaticDestructors && |
| 2565 | !hasAttr<AlwaysDestroyAttr>())); |
| 2566 | } |
| 2567 | |
Douglas Gregor | 3cc3cde | 2009-10-14 21:29:40 +0000 | [diff] [blame] | 2568 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
Richard Smith | b71782b | 2013-08-01 04:12:04 +0000 | [diff] [blame] | 2569 | if (isStaticDataMember()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2570 | // FIXME: Remove ? |
| 2571 | // return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 2572 | return getASTContext().getTemplateOrSpecializationInfo(this) |
| 2573 | .dyn_cast<MemberSpecializationInfo *>(); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2574 | return nullptr; |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 2577 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2578 | SourceLocation PointOfInstantiation) { |
Richard Smith | 8809a0c | 2013-09-27 20:14:12 +0000 | [diff] [blame] | 2579 | assert((isa<VarTemplateSpecializationDecl>(this) || |
| 2580 | getMemberSpecializationInfo()) && |
| 2581 | "not a variable or static data member template specialization"); |
| 2582 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2583 | if (VarTemplateSpecializationDecl *Spec = |
| 2584 | dyn_cast<VarTemplateSpecializationDecl>(this)) { |
| 2585 | Spec->setSpecializationKind(TSK); |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2586 | if (TSK != TSK_ExplicitSpecialization && |
| 2587 | PointOfInstantiation.isValid() && |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2588 | Spec->getPointOfInstantiation().isInvalid()) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2589 | Spec->setPointOfInstantiation(PointOfInstantiation); |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2590 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 2591 | L->InstantiationRequested(this); |
| 2592 | } |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 2593 | } else if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2594 | MSI->setTemplateSpecializationKind(TSK); |
| 2595 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2596 | MSI->getPointOfInstantiation().isInvalid()) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2597 | MSI->setPointOfInstantiation(PointOfInstantiation); |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 2598 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 2599 | L->InstantiationRequested(this); |
| 2600 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2601 | } |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
| 2604 | void |
| 2605 | VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, |
| 2606 | TemplateSpecializationKind TSK) { |
| 2607 | assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && |
| 2608 | "Previous template or instantiation?"); |
| 2609 | getASTContext().setInstantiatedFromStaticDataMember(this, VD, TSK); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2612 | //===----------------------------------------------------------------------===// |
| 2613 | // ParmVarDecl Implementation |
| 2614 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2615 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2616 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2617 | SourceLocation StartLoc, |
| 2618 | SourceLocation IdLoc, IdentifierInfo *Id, |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2619 | QualType T, TypeSourceInfo *TInfo, |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2620 | StorageClass S, Expr *DefArg) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2621 | 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] | 2622 | S, DefArg); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2623 | } |
| 2624 | |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2625 | QualType ParmVarDecl::getOriginalType() const { |
| 2626 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 2627 | QualType T = TSI ? TSI->getType() : getType(); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2628 | if (const auto *DT = dyn_cast<DecayedType>(T)) |
Reid Kleckner | 8a36502 | 2013-06-24 17:51:48 +0000 | [diff] [blame] | 2629 | return DT->getOriginalType(); |
| 2630 | return T; |
| 2631 | } |
| 2632 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2633 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 2634 | return new (C, ID) |
| 2635 | ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), |
| 2636 | nullptr, QualType(), nullptr, SC_None, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2639 | SourceRange ParmVarDecl::getSourceRange() const { |
| 2640 | if (!hasInheritedDefaultArg()) { |
| 2641 | SourceRange ArgRange = getDefaultArgRange(); |
| 2642 | if (ArgRange.isValid()) |
| 2643 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 2644 | } |
| 2645 | |
Argyrios Kyrtzidis | a077279 | 2013-04-17 01:56:48 +0000 | [diff] [blame] | 2646 | // DeclaratorDecl considers the range of postfix types as overlapping with the |
| 2647 | // declaration name, but this is not the case with parameters in ObjC methods. |
| 2648 | if (isa<ObjCMethodDecl>(getDeclContext())) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2649 | return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation()); |
Argyrios Kyrtzidis | a077279 | 2013-04-17 01:56:48 +0000 | [diff] [blame] | 2650 | |
Argyrios Kyrtzidis | 4c6efa62 | 2011-07-30 17:23:26 +0000 | [diff] [blame] | 2651 | return DeclaratorDecl::getSourceRange(); |
| 2652 | } |
| 2653 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2654 | Expr *ParmVarDecl::getDefaultArg() { |
| 2655 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!"); |
| 2656 | assert(!hasUninstantiatedDefaultArg() && |
| 2657 | "Default argument is not yet instantiated!"); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2658 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2659 | Expr *Arg = getInit(); |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 2660 | if (auto *E = dyn_cast_or_null<FullExpr>(Arg)) |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2661 | return E->getSubExpr(); |
Douglas Gregor | 0760fa1 | 2009-03-10 23:43:53 +0000 | [diff] [blame] | 2662 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2663 | return Arg; |
| 2664 | } |
| 2665 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2666 | void ParmVarDecl::setDefaultArg(Expr *defarg) { |
| 2667 | ParmVarDeclBits.DefaultArgKind = DAK_Normal; |
| 2668 | Init = defarg; |
| 2669 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2670 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2671 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 2672 | switch (ParmVarDeclBits.DefaultArgKind) { |
| 2673 | case DAK_None: |
| 2674 | case DAK_Unparsed: |
| 2675 | // Nothing we can do here. |
| 2676 | return SourceRange(); |
| 2677 | |
| 2678 | case DAK_Uninstantiated: |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 2679 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 2680 | |
Chandler Carruth | 813faed | 2015-12-30 02:51:00 +0000 | [diff] [blame] | 2681 | case DAK_Normal: |
| 2682 | if (const Expr *E = getInit()) |
| 2683 | return E->getSourceRange(); |
| 2684 | |
| 2685 | // Missing an actual expression, may be invalid. |
| 2686 | return SourceRange(); |
| 2687 | } |
| 2688 | llvm_unreachable("Invalid default argument kind."); |
| 2689 | } |
| 2690 | |
| 2691 | void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) { |
| 2692 | ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated; |
| 2693 | Init = arg; |
| 2694 | } |
| 2695 | |
| 2696 | Expr *ParmVarDecl::getUninstantiatedDefaultArg() { |
| 2697 | assert(hasUninstantiatedDefaultArg() && |
| 2698 | "Wrong kind of initialization expression!"); |
| 2699 | return cast_or_null<Expr>(Init.get<Stmt *>()); |
| 2700 | } |
| 2701 | |
| 2702 | bool ParmVarDecl::hasDefaultArg() const { |
| 2703 | // FIXME: We should just return false for DAK_None here once callers are |
| 2704 | // prepared for the case that we encountered an invalid default argument and |
| 2705 | // were unable to even build an invalid expression. |
| 2706 | return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() || |
| 2707 | !Init.isNull(); |
Argyrios Kyrtzidis | 02dd4f9 | 2009-07-05 22:21:56 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Ted Kremenek | 540017e | 2011-10-06 05:00:56 +0000 | [diff] [blame] | 2710 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 2711 | getASTContext().setParameterIndex(this, parameterIndex); |
| 2712 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 2713 | } |
| 2714 | |
| 2715 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 2716 | return getASTContext().getParameterIndex(this); |
| 2717 | } |
| 2718 | |
Nuno Lopes | 394ec98 | 2008-12-17 23:39:55 +0000 | [diff] [blame] | 2719 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 2720 | // FunctionDecl Implementation |
| 2721 | //===----------------------------------------------------------------------===// |
| 2722 | |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2723 | FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 2724 | SourceLocation StartLoc, |
| 2725 | const DeclarationNameInfo &NameInfo, QualType T, |
| 2726 | TypeSourceInfo *TInfo, StorageClass S, |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame^] | 2727 | bool isInlineSpecified, |
| 2728 | ConstexprSpecKind ConstexprKind) |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2729 | : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, |
| 2730 | StartLoc), |
| 2731 | DeclContext(DK), redeclarable_base(C), ODRHash(0), |
| 2732 | EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) { |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 2733 | assert(T.isNull() || T->isFunctionType()); |
Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2734 | FunctionDeclBits.SClass = S; |
| 2735 | FunctionDeclBits.IsInline = isInlineSpecified; |
| 2736 | FunctionDeclBits.IsInlineSpecified = isInlineSpecified; |
Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2737 | FunctionDeclBits.IsVirtualAsWritten = false; |
| 2738 | FunctionDeclBits.IsPure = false; |
| 2739 | FunctionDeclBits.HasInheritedPrototype = false; |
| 2740 | FunctionDeclBits.HasWrittenPrototype = true; |
| 2741 | FunctionDeclBits.IsDeleted = false; |
| 2742 | FunctionDeclBits.IsTrivial = false; |
| 2743 | FunctionDeclBits.IsTrivialForCall = false; |
| 2744 | FunctionDeclBits.IsDefaulted = false; |
| 2745 | FunctionDeclBits.IsExplicitlyDefaulted = false; |
| 2746 | FunctionDeclBits.HasImplicitReturnZero = false; |
| 2747 | FunctionDeclBits.IsLateTemplateParsed = false; |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame^] | 2748 | FunctionDeclBits.ConstexprKind = ConstexprKind; |
Bruno Ricci | 0b639da | 2018-12-03 13:04:10 +0000 | [diff] [blame] | 2749 | FunctionDeclBits.InstantiationIsPending = false; |
| 2750 | FunctionDeclBits.UsesSEHTry = false; |
| 2751 | FunctionDeclBits.HasSkippedBody = false; |
| 2752 | FunctionDeclBits.WillHaveBody = false; |
| 2753 | FunctionDeclBits.IsMultiVersion = false; |
| 2754 | FunctionDeclBits.IsCopyDeductionCandidate = false; |
| 2755 | FunctionDeclBits.HasODRHash = false; |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
Benjamin Kramer | 9170e91 | 2013-02-22 15:46:01 +0000 | [diff] [blame] | 2758 | void FunctionDecl::getNameForDiagnostic( |
| 2759 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 2760 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2761 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 2762 | if (TemplateArgs) |
Serge Pavlov | 03e672c | 2017-11-28 16:14:14 +0000 | [diff] [blame] | 2763 | printTemplateArgumentList(OS, TemplateArgs->asArray(), Policy); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2766 | bool FunctionDecl::isVariadic() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2767 | if (const auto *FT = getType()->getAs<FunctionProtoType>()) |
Ted Kremenek | 186a074 | 2010-04-29 16:49:01 +0000 | [diff] [blame] | 2768 | return FT->isVariadic(); |
| 2769 | return false; |
| 2770 | } |
| 2771 | |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2772 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2773 | for (auto I : redecls()) { |
Serge Pavlov | 9a618e1 | 2017-02-15 12:30:35 +0000 | [diff] [blame] | 2774 | if (I->doesThisDeclarationHaveABody()) { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2775 | Definition = I; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 2776 | return true; |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | return false; |
| 2781 | } |
| 2782 | |
Anders Carlsson | 9bd7d16 | 2011-05-14 23:26:09 +0000 | [diff] [blame] | 2783 | bool FunctionDecl::hasTrivialBody() const |
| 2784 | { |
| 2785 | Stmt *S = getBody(); |
| 2786 | if (!S) { |
| 2787 | // Since we don't have a body for this function, we don't know if it's |
| 2788 | // trivial or not. |
| 2789 | return false; |
| 2790 | } |
| 2791 | |
| 2792 | if (isa<CompoundStmt>(S) && cast<CompoundStmt>(S)->body_empty()) |
| 2793 | return true; |
| 2794 | return false; |
| 2795 | } |
| 2796 | |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2797 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition) const { |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 2798 | for (auto I : redecls()) { |
Serge Pavlov | 10673c9 | 2017-06-04 12:53:12 +0000 | [diff] [blame] | 2799 | if (I->isThisDeclarationADefinition()) { |
| 2800 | Definition = I; |
Alexis Hunt | 4a8ea10 | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 2801 | return true; |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | return false; |
| 2806 | } |
| 2807 | |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2808 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2809 | if (!hasBody(Definition)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2810 | return nullptr; |
Rafael Espindola | 4450301 | 2013-10-19 01:37:17 +0000 | [diff] [blame] | 2811 | |
| 2812 | if (Definition->Body) |
| 2813 | return Definition->Body.get(getASTContext().getExternalSource()); |
Douglas Gregor | 89f238c | 2008-04-21 02:02:58 +0000 | [diff] [blame] | 2814 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 2815 | return nullptr; |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2818 | void FunctionDecl::setBody(Stmt *B) { |
| 2819 | Body = B; |
Douglas Gregor | 027ba50 | 2010-12-06 17:49:01 +0000 | [diff] [blame] | 2820 | if (B) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2821 | EndRangeLoc = B->getEndLoc(); |
Argyrios Kyrtzidis | a3aeb5a | 2009-06-20 08:09:14 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2824 | void FunctionDecl::setPure(bool P) { |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 2825 | FunctionDeclBits.IsPure = P; |
Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2826 | if (P) |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2827 | if (auto *Parent = dyn_cast<CXXRecordDecl>(getDeclContext())) |
Douglas Gregor | 7d9120c | 2010-09-28 21:55:22 +0000 | [diff] [blame] | 2828 | Parent->markedVirtualFunctionPure(); |
| 2829 | } |
| 2830 | |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2831 | template<std::size_t Len> |
| 2832 | static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { |
| 2833 | IdentifierInfo *II = ND->getIdentifier(); |
| 2834 | return II && II->isStr(Str); |
| 2835 | } |
| 2836 | |
Douglas Gregor | 16618f2 | 2009-09-12 00:17:51 +0000 | [diff] [blame] | 2837 | bool FunctionDecl::isMain() const { |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2838 | const TranslationUnitDecl *tunit = |
| 2839 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2840 | return tunit && |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2841 | !tunit->getASTContext().getLangOpts().Freestanding && |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2842 | isNamed(this, "main"); |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
David Majnemer | c729b0b | 2013-09-16 22:44:20 +0000 | [diff] [blame] | 2845 | bool FunctionDecl::isMSVCRTEntryPoint() const { |
| 2846 | const TranslationUnitDecl *TUnit = |
| 2847 | dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()); |
| 2848 | if (!TUnit) |
| 2849 | return false; |
| 2850 | |
| 2851 | // Even though we aren't really targeting MSVCRT if we are freestanding, |
| 2852 | // semantic analysis for these functions remains the same. |
| 2853 | |
| 2854 | // MSVCRT entry points only exist on MSVCRT targets. |
| 2855 | if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT()) |
| 2856 | return false; |
| 2857 | |
| 2858 | // Nameless functions like constructors cannot be entry points. |
| 2859 | if (!getIdentifier()) |
| 2860 | return false; |
| 2861 | |
| 2862 | return llvm::StringSwitch<bool>(getName()) |
| 2863 | .Cases("main", // an ANSI console app |
| 2864 | "wmain", // a Unicode console App |
| 2865 | "WinMain", // an ANSI GUI app |
| 2866 | "wWinMain", // a Unicode GUI app |
| 2867 | "DllMain", // a DLL |
| 2868 | true) |
| 2869 | .Default(false); |
| 2870 | } |
| 2871 | |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2872 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 2873 | assert(getDeclName().getNameKind() == DeclarationName::CXXOperatorName); |
| 2874 | assert(getDeclName().getCXXOverloadedOperator() == OO_New || |
| 2875 | getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 2876 | getDeclName().getCXXOverloadedOperator() == OO_Array_New || |
| 2877 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete); |
| 2878 | |
Richard Smith | f600441 | 2014-01-19 23:25:37 +0000 | [diff] [blame] | 2879 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2880 | return false; |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2881 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2882 | const auto *proto = getType()->castAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2883 | if (proto->getNumParams() != 2 || proto->isVariadic()) |
| 2884 | return false; |
John McCall | 53ffd37 | 2011-05-15 17:49:20 +0000 | [diff] [blame] | 2885 | |
| 2886 | ASTContext &Context = |
| 2887 | cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext()) |
| 2888 | ->getASTContext(); |
| 2889 | |
| 2890 | // The result type and first argument type are constant across all |
| 2891 | // these operators. The second argument must be exactly void*. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2892 | return (proto->getParamType(1).getCanonicalType() == Context.VoidPtrTy); |
Douglas Gregor | e62c0a4 | 2009-02-24 01:23:02 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2895 | bool FunctionDecl::isReplaceableGlobalAllocationFunction(bool *IsAligned) const { |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2896 | if (getDeclName().getNameKind() != DeclarationName::CXXOperatorName) |
| 2897 | return false; |
| 2898 | if (getDeclName().getCXXOverloadedOperator() != OO_New && |
| 2899 | getDeclName().getCXXOverloadedOperator() != OO_Delete && |
| 2900 | getDeclName().getCXXOverloadedOperator() != OO_Array_New && |
| 2901 | getDeclName().getCXXOverloadedOperator() != OO_Array_Delete) |
| 2902 | return false; |
| 2903 | |
| 2904 | if (isa<CXXRecordDecl>(getDeclContext())) |
| 2905 | return false; |
Richard Smith | f600441 | 2014-01-19 23:25:37 +0000 | [diff] [blame] | 2906 | |
| 2907 | // This can only fail for an invalid 'operator new' declaration. |
| 2908 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 2909 | return false; |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2910 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2911 | const auto *FPT = getType()->castAs<FunctionProtoType>(); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2912 | if (FPT->getNumParams() == 0 || FPT->getNumParams() > 3 || FPT->isVariadic()) |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2913 | return false; |
| 2914 | |
| 2915 | // If this is a single-parameter function, it must be a replaceable global |
| 2916 | // allocation or deallocation function. |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 2917 | if (FPT->getNumParams() == 1) |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2918 | return true; |
| 2919 | |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2920 | unsigned Params = 1; |
| 2921 | QualType Ty = FPT->getParamType(Params); |
Richard Smith | 1cdec01 | 2013-09-29 04:40:38 +0000 | [diff] [blame] | 2922 | ASTContext &Ctx = getASTContext(); |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2923 | |
| 2924 | auto Consume = [&] { |
| 2925 | ++Params; |
| 2926 | Ty = Params < FPT->getNumParams() ? FPT->getParamType(Params) : QualType(); |
| 2927 | }; |
| 2928 | |
| 2929 | // In C++14, the next parameter can be a 'std::size_t' for sized delete. |
| 2930 | bool IsSizedDelete = false; |
Richard Smith | b47c36f | 2013-11-05 09:12:18 +0000 | [diff] [blame] | 2931 | if (Ctx.getLangOpts().SizedDeallocation && |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2932 | (getDeclName().getCXXOverloadedOperator() == OO_Delete || |
| 2933 | getDeclName().getCXXOverloadedOperator() == OO_Array_Delete) && |
| 2934 | Ctx.hasSameType(Ty, Ctx.getSizeType())) { |
| 2935 | IsSizedDelete = true; |
| 2936 | Consume(); |
| 2937 | } |
| 2938 | |
| 2939 | // In C++17, the next parameter can be a 'std::align_val_t' for aligned |
| 2940 | // new/delete. |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2941 | if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) { |
| 2942 | if (IsAligned) |
| 2943 | *IsAligned = true; |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2944 | Consume(); |
Akira Hatanaka | cae83f7 | 2017-06-29 18:48:40 +0000 | [diff] [blame] | 2945 | } |
Richard Smith | b2f0f05 | 2016-10-10 18:54:32 +0000 | [diff] [blame] | 2946 | |
| 2947 | // Finally, if this is not a sized delete, the final parameter can |
| 2948 | // be a 'const std::nothrow_t&'. |
| 2949 | if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) { |
| 2950 | Ty = Ty->getPointeeType(); |
| 2951 | if (Ty.getCVRQualifiers() != Qualifiers::Const) |
| 2952 | return false; |
| 2953 | const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl(); |
| 2954 | if (RD && isNamed(RD, "nothrow_t") && RD->isInStdNamespace()) |
| 2955 | Consume(); |
| 2956 | } |
| 2957 | |
| 2958 | return Params == FPT->getNumParams(); |
Richard Smith | 8d0dc31 | 2013-07-21 23:12:18 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Richard Smith | 5b34958 | 2017-10-13 01:55:36 +0000 | [diff] [blame] | 2961 | bool FunctionDecl::isDestroyingOperatorDelete() const { |
| 2962 | // C++ P0722: |
| 2963 | // Within a class C, a single object deallocation function with signature |
| 2964 | // (T, std::destroying_delete_t, <more params>) |
| 2965 | // is a destroying operator delete. |
| 2966 | if (!isa<CXXMethodDecl>(this) || getOverloadedOperator() != OO_Delete || |
| 2967 | getNumParams() < 2) |
| 2968 | return false; |
| 2969 | |
| 2970 | auto *RD = getParamDecl(1)->getType()->getAsCXXRecordDecl(); |
| 2971 | return RD && RD->isInStdNamespace() && RD->getIdentifier() && |
| 2972 | RD->getIdentifier()->isStr("destroying_delete_t"); |
| 2973 | } |
| 2974 | |
Rafael Espindola | f418765 | 2013-02-14 01:18:37 +0000 | [diff] [blame] | 2975 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2976 | return getDeclLanguageLinkage(*this); |
Rafael Espindola | 576127d | 2012-12-28 14:21:58 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2979 | bool FunctionDecl::isExternC() const { |
Alp Toker | 84212df | 2014-05-31 06:11:02 +0000 | [diff] [blame] | 2980 | return isDeclExternC(*this); |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2983 | bool FunctionDecl::isInExternCContext() const { |
Anastasia Stulova | d6865b7 | 2019-05-07 14:22:34 +0000 | [diff] [blame] | 2984 | if (hasAttr<OpenCLKernelAttr>()) |
| 2985 | return true; |
Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2986 | return getLexicalDeclContext()->isExternCContext(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | bool FunctionDecl::isInExternCXXContext() const { |
Serge Pavlov | 3cb8022 | 2013-11-14 02:13:03 +0000 | [diff] [blame] | 2990 | return getLexicalDeclContext()->isExternCXXContext(); |
Rafael Espindola | 593537a | 2013-05-05 20:15:21 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2993 | bool FunctionDecl::isGlobal() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 2994 | if (const auto *Method = dyn_cast<CXXMethodDecl>(this)) |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2995 | return Method->isStatic(); |
| 2996 | |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 2997 | if (getCanonicalDecl()->getStorageClass() == SC_Static) |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 2998 | return false; |
| 2999 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | for (const DeclContext *DC = getDeclContext(); |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 3001 | DC->isNamespace(); |
| 3002 | DC = DC->getParent()) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3003 | if (const auto *Namespace = cast<NamespaceDecl>(DC)) { |
Douglas Gregor | f1b876d | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 3004 | if (!Namespace->getDeclName()) |
| 3005 | return false; |
| 3006 | break; |
| 3007 | } |
| 3008 | } |
| 3009 | |
| 3010 | return true; |
| 3011 | } |
| 3012 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3013 | bool FunctionDecl::isNoReturn() const { |
Adrian Prantl | 721c7cb | 2016-08-17 16:42:15 +0000 | [diff] [blame] | 3014 | if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
| 3015 | hasAttr<C11NoReturnAttr>()) |
| 3016 | return true; |
| 3017 | |
| 3018 | if (auto *FnTy = getType()->getAs<FunctionType>()) |
| 3019 | return FnTy->getNoReturnAttr(); |
| 3020 | |
| 3021 | return false; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Erich Keane | 5c0d192 | 2018-11-28 18:34:14 +0000 | [diff] [blame] | 3024 | |
| 3025 | MultiVersionKind FunctionDecl::getMultiVersionKind() const { |
| 3026 | if (hasAttr<TargetAttr>()) |
| 3027 | return MultiVersionKind::Target; |
| 3028 | if (hasAttr<CPUDispatchAttr>()) |
| 3029 | return MultiVersionKind::CPUDispatch; |
| 3030 | if (hasAttr<CPUSpecificAttr>()) |
| 3031 | return MultiVersionKind::CPUSpecific; |
| 3032 | return MultiVersionKind::None; |
| 3033 | } |
| 3034 | |
Erich Keane | 3efe002 | 2018-07-20 14:13:28 +0000 | [diff] [blame] | 3035 | bool FunctionDecl::isCPUDispatchMultiVersion() const { |
| 3036 | return isMultiVersion() && hasAttr<CPUDispatchAttr>(); |
| 3037 | } |
| 3038 | |
| 3039 | bool FunctionDecl::isCPUSpecificMultiVersion() const { |
| 3040 | return isMultiVersion() && hasAttr<CPUSpecificAttr>(); |
| 3041 | } |
| 3042 | |
Erich Keane | 19a8adc | 2018-10-25 18:57:19 +0000 | [diff] [blame] | 3043 | bool FunctionDecl::isTargetMultiVersion() const { |
| 3044 | return isMultiVersion() && hasAttr<TargetAttr>(); |
| 3045 | } |
| 3046 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3047 | void |
| 3048 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3049 | redeclarable_base::setPreviousDecl(PrevDecl); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3050 | |
| 3051 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 3052 | FunctionTemplateDecl *PrevFunTmpl |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3053 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3054 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch"); |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3055 | FunTmpl->setPreviousDecl(PrevFunTmpl); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3056 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3057 | |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3058 | if (PrevDecl && PrevDecl->isInlined()) |
| 3059 | setImplicitlyInline(true); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3062 | FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3063 | |
Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3064 | /// Returns a value indicating whether this function corresponds to a builtin |
| 3065 | /// function. |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 3066 | /// |
Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3067 | /// The function corresponds to a built-in function if it is declared at |
| 3068 | /// translation scope or within an extern "C" block and its name matches with |
| 3069 | /// the name of a builtin. The returned value will be 0 for functions that do |
| 3070 | /// not correspond to a builtin, a value of type \c Builtin::ID if in the |
| 3071 | /// target-independent range \c [1,Builtin::First), or a target-specific builtin |
| 3072 | /// value. |
| 3073 | /// |
| 3074 | /// \param ConsiderWrapperFunctions If true, we should consider wrapper |
| 3075 | /// functions as their wrapped builtins. This shouldn't be done in general, but |
| 3076 | /// it's useful in Sema to diagnose calls to wrappers based on their semantics. |
| 3077 | unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { |
Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 3078 | if (!getIdentifier()) |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 3079 | return 0; |
| 3080 | |
| 3081 | unsigned BuiltinID = getIdentifier()->getBuiltinID(); |
Daniel Dunbar | 304314d | 2012-03-06 23:52:37 +0000 | [diff] [blame] | 3082 | if (!BuiltinID) |
| 3083 | return 0; |
| 3084 | |
| 3085 | ASTContext &Context = getASTContext(); |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3086 | if (Context.getLangOpts().CPlusPlus) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3087 | const auto *LinkageDecl = |
| 3088 | dyn_cast<LinkageSpecDecl>(getFirstDecl()->getDeclContext()); |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3089 | // In C++, the first declaration of a builtin is always inside an implicit |
| 3090 | // extern "C". |
| 3091 | // FIXME: A recognised library function may not be directly in an extern "C" |
| 3092 | // declaration, for instance "extern "C" { namespace std { decl } }". |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 3093 | if (!LinkageDecl) { |
| 3094 | if (BuiltinID == Builtin::BI__GetExceptionInfo && |
David Majnemer | 145abde | 2016-01-26 01:12:17 +0000 | [diff] [blame] | 3095 | Context.getTargetInfo().getCXXABI().isMicrosoft()) |
David Majnemer | ba3e5ec | 2015-03-13 18:26:17 +0000 | [diff] [blame] | 3096 | return Builtin::BI__GetExceptionInfo; |
| 3097 | return 0; |
| 3098 | } |
| 3099 | if (LinkageDecl->getLanguage() != LinkageSpecDecl::lang_c) |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3100 | return 0; |
| 3101 | } |
| 3102 | |
| 3103 | // If the function is marked "overloadable", it has a different mangled name |
| 3104 | // and is not the C library function. |
Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3105 | if (!ConsiderWrapperFunctions && hasAttr<OverloadableAttr>()) |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3106 | return 0; |
| 3107 | |
Douglas Gregor | e711f70 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 3108 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 3109 | return BuiltinID; |
| 3110 | |
| 3111 | // This function has the name of a known C library |
| 3112 | // function. Determine whether it actually refers to the C library |
| 3113 | // function or whether it just has the same name. |
| 3114 | |
Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 3115 | // If this is a static function, it's not a builtin. |
Erik Pilkington | b6e16ea | 2019-03-18 19:23:45 +0000 | [diff] [blame] | 3116 | if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) |
Douglas Gregor | a908e7f | 2009-02-17 03:23:10 +0000 | [diff] [blame] | 3117 | return 0; |
| 3118 | |
Anastasia Stulova | 1202de3 | 2016-02-12 12:07:04 +0000 | [diff] [blame] | 3119 | // OpenCL v1.2 s6.9.f - The library functions defined in |
| 3120 | // the C99 standard headers are not available. |
| 3121 | if (Context.getLangOpts().OpenCL && |
| 3122 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) |
| 3123 | return 0; |
| 3124 | |
Artem Belevich | 5ecdb94 | 2018-01-23 19:08:18 +0000 | [diff] [blame] | 3125 | // CUDA does not have device-side standard library. printf and malloc are the |
| 3126 | // only special cases that are supported by device-side runtime. |
| 3127 | if (Context.getLangOpts().CUDA && hasAttr<CUDADeviceAttr>() && |
| 3128 | !hasAttr<CUDAHostAttr>() && |
| 3129 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
| 3130 | return 0; |
| 3131 | |
Warren Hunt | 445d83e | 2013-11-01 23:46:51 +0000 | [diff] [blame] | 3132 | return BuiltinID; |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 3135 | /// getNumParams - Return the number of parameters this function must have |
Bob Wilson | b39017a | 2011-01-10 18:23:55 +0000 | [diff] [blame] | 3136 | /// based on its FunctionType. This is the length of the ParamInfo array |
Chris Lattner | 47c0d00 | 2009-04-25 06:03:53 +0000 | [diff] [blame] | 3137 | /// after it has been created. |
| 3138 | unsigned FunctionDecl::getNumParams() const { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3139 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 3140 | return FPT ? FPT->getNumParams() : 0; |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3143 | void FunctionDecl::setParams(ASTContext &C, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 3144 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3145 | assert(!ParamInfo && "Already has param info!"); |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3146 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | |
Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 3148 | // Zero params -> null pointer. |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 3149 | if (!NewParamInfo.empty()) { |
| 3150 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 3151 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Chris Lattner | 8f5bf2f | 2007-01-21 19:04:10 +0000 | [diff] [blame] | 3152 | } |
Chris Lattner | c5cdf4d | 2007-01-21 07:42:07 +0000 | [diff] [blame] | 3153 | } |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 3154 | |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3155 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 3156 | /// needed to call this function. This may be fewer than the number of |
| 3157 | /// function parameters, if some of the parameters have default |
Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 3158 | /// arguments (in C++) or are parameter packs (C++11). |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3159 | unsigned FunctionDecl::getMinRequiredArguments() const { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3160 | if (!getASTContext().getLangOpts().CPlusPlus) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3161 | return getNumParams(); |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3162 | |
Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 3163 | unsigned NumRequiredArgs = 0; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3164 | for (auto *Param : parameters()) |
Richard Smith | 9115178 | 2014-04-01 19:18:16 +0000 | [diff] [blame] | 3165 | if (!Param->isParameterPack() && !Param->hasDefaultArg()) |
| 3166 | ++NumRequiredArgs; |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3167 | return NumRequiredArgs; |
| 3168 | } |
| 3169 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3170 | /// The combination of the extern and inline keywords under MSVC forces |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3171 | /// the function to be required. |
| 3172 | /// |
| 3173 | /// Note: This function assumes that we will only get called when isInlined() |
| 3174 | /// would return true for this FunctionDecl. |
| 3175 | bool FunctionDecl::isMSExternInline() const { |
| 3176 | assert(isInlined() && "expected to get called on an inlined function!"); |
| 3177 | |
| 3178 | const ASTContext &Context = getASTContext(); |
David Majnemer | 3f02150 | 2015-10-08 04:53:31 +0000 | [diff] [blame] | 3179 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 3180 | !hasAttr<DLLExportAttr>()) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3181 | return false; |
| 3182 | |
David Majnemer | 7376870 | 2015-03-20 00:02:27 +0000 | [diff] [blame] | 3183 | for (const FunctionDecl *FD = getMostRecentDecl(); FD; |
| 3184 | FD = FD->getPreviousDecl()) |
David Majnemer | c0c42f3 | 2015-07-10 20:55:38 +0000 | [diff] [blame] | 3185 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3186 | return true; |
| 3187 | |
| 3188 | return false; |
| 3189 | } |
| 3190 | |
| 3191 | static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { |
| 3192 | if (Redecl->getStorageClass() != SC_Extern) |
| 3193 | return false; |
| 3194 | |
| 3195 | for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; |
| 3196 | FD = FD->getPreviousDecl()) |
David Majnemer | c0c42f3 | 2015-07-10 20:55:38 +0000 | [diff] [blame] | 3197 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3198 | return false; |
| 3199 | |
| 3200 | return true; |
| 3201 | } |
| 3202 | |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3203 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 3204 | // Only consider file-scope declarations in this test. |
| 3205 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 3206 | return false; |
| 3207 | |
| 3208 | // Only consider explicit declarations; the presence of a builtin for a |
| 3209 | // libcall shouldn't affect whether a definition is externally visible. |
| 3210 | if (Redecl->isImplicit()) |
| 3211 | return false; |
| 3212 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3213 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3214 | return true; // Not an inline definition |
| 3215 | |
| 3216 | return false; |
| 3217 | } |
| 3218 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3219 | /// For a function declaration in C or C++, determine whether this |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3220 | /// declaration causes the definition to be externally visible. |
| 3221 | /// |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3222 | /// For instance, this determines if adding the current declaration to the set |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3223 | /// of redeclarations of the given functions causes |
| 3224 | /// isInlineDefinitionExternallyVisible to change from false to true. |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3225 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
Justin Lebar | 0241c96 | 2016-10-28 16:46:39 +0000 | [diff] [blame] | 3226 | assert(!doesThisDeclarationHaveABody() && |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3227 | "Must have a declaration without a body."); |
| 3228 | |
| 3229 | ASTContext &Context = getASTContext(); |
| 3230 | |
David Majnemer | 54e3ba5 | 2014-04-02 23:17:29 +0000 | [diff] [blame] | 3231 | if (Context.getLangOpts().MSVCCompat) { |
| 3232 | const FunctionDecl *Definition; |
| 3233 | if (hasBody(Definition) && Definition->isInlined() && |
| 3234 | redeclForcesDefMSVC(this)) |
| 3235 | return true; |
| 3236 | } |
| 3237 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3238 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3239 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 3240 | // an externally visible definition. |
| 3241 | // |
| 3242 | // FIXME: What happens if gnu_inline gets added on after the first |
| 3243 | // declaration? |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3244 | if (!isInlineSpecified() || getStorageClass() == SC_Extern) |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3245 | return false; |
| 3246 | |
| 3247 | const FunctionDecl *Prev = this; |
| 3248 | bool FoundBody = false; |
| 3249 | while ((Prev = Prev->getPreviousDecl())) { |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3250 | FoundBody |= Prev->Body.isValid(); |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3251 | |
| 3252 | if (Prev->Body) { |
| 3253 | // If it's not the case that both 'inline' and 'extern' are |
| 3254 | // specified on the definition, then it is always externally visible. |
| 3255 | if (!Prev->isInlineSpecified() || |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3256 | Prev->getStorageClass() != SC_Extern) |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3257 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3258 | } else if (Prev->isInlineSpecified() && |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3259 | Prev->getStorageClass() != SC_Extern) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3260 | return false; |
| 3261 | } |
| 3262 | } |
| 3263 | return FoundBody; |
| 3264 | } |
| 3265 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3266 | if (Context.getLangOpts().CPlusPlus) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3267 | return false; |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3268 | |
| 3269 | // C99 6.7.4p6: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3270 | // [...] If all of the file scope declarations for a function in a |
| 3271 | // translation unit include the inline function specifier without extern, |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3272 | // then the definition in that translation unit is an inline definition. |
| 3273 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3274 | return false; |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3275 | const FunctionDecl *Prev = this; |
| 3276 | bool FoundBody = false; |
| 3277 | while ((Prev = Prev->getPreviousDecl())) { |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 3278 | FoundBody |= Prev->Body.isValid(); |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3279 | if (RedeclForcesDefC99(Prev)) |
| 3280 | return false; |
| 3281 | } |
| 3282 | return FoundBody; |
Nick Lewycky | 26da4dd | 2011-07-18 05:26:13 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3285 | SourceRange FunctionDecl::getReturnTypeSourceRange() const { |
| 3286 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 3287 | if (!TSI) |
| 3288 | return SourceRange(); |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3289 | FunctionTypeLoc FTL = |
| 3290 | TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>(); |
| 3291 | if (!FTL) |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3292 | return SourceRange(); |
| 3293 | |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3294 | // Skip self-referential return types. |
| 3295 | const SourceManager &SM = getASTContext().getSourceManager(); |
| 3296 | SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3297 | SourceLocation Boundary = getNameInfo().getBeginLoc(); |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3298 | if (RTRange.isInvalid() || Boundary.isInvalid() || |
| 3299 | !SM.isBeforeInTranslationUnit(RTRange.getEnd(), Boundary)) |
| 3300 | return SourceRange(); |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3301 | |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3302 | return RTRange; |
Alp Toker | d0787eb | 2014-07-02 01:47:15 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
Malcolm Parsons | a3220ce | 2017-01-12 16:11:28 +0000 | [diff] [blame] | 3305 | SourceRange FunctionDecl::getExceptionSpecSourceRange() const { |
| 3306 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 3307 | if (!TSI) |
| 3308 | return SourceRange(); |
| 3309 | FunctionTypeLoc FTL = |
| 3310 | TSI->getTypeLoc().IgnoreParens().getAs<FunctionTypeLoc>(); |
| 3311 | if (!FTL) |
| 3312 | return SourceRange(); |
| 3313 | |
| 3314 | return FTL.getExceptionSpecRange(); |
| 3315 | } |
| 3316 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3317 | /// 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] | 3318 | /// in C++, determine whether the definition will be externally visible. |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3319 | /// |
| 3320 | /// Inline function definitions are always available for inlining optimizations. |
| 3321 | /// However, depending on the language dialect, declaration specifiers, and |
| 3322 | /// attributes, the definition of an inline function may or may not be |
| 3323 | /// "externally" visible to other translation units in the program. |
| 3324 | /// |
| 3325 | /// In C99, inline definitions are not externally visible by default. However, |
Mike Stump | 13c6670 | 2010-01-06 02:05:39 +0000 | [diff] [blame] | 3326 | /// 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] | 3327 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 3328 | /// |
| 3329 | /// 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] | 3330 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 3331 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 3332 | /// an externally visible symbol, but "extern inline" will not create an |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3333 | /// externally visible symbol. |
| 3334 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
Manuel Klimek | da7456a | 2016-11-01 10:30:50 +0000 | [diff] [blame] | 3335 | assert((doesThisDeclarationHaveABody() || willHaveBody()) && |
| 3336 | "Must be a function definition"); |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 3337 | assert(isInlined() && "Function must be inline"); |
Douglas Gregor | b7e5c84 | 2009-10-27 23:26:40 +0000 | [diff] [blame] | 3338 | ASTContext &Context = getASTContext(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3339 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3340 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3341 | // Note: If you change the logic here, please change |
| 3342 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 3343 | // |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3344 | // If it's not the case that both 'inline' and 'extern' are |
| 3345 | // specified on the definition, then this inline definition is |
| 3346 | // externally visible. |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3347 | if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3348 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3349 | |
Douglas Gregor | ff76cb9 | 2010-12-09 16:59:22 +0000 | [diff] [blame] | 3350 | // If any declaration is 'inline' but not 'extern', then this definition |
| 3351 | // is externally visible. |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3352 | for (auto Redecl : redecls()) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3353 | if (Redecl->isInlineSpecified() && |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 3354 | Redecl->getStorageClass() != SC_Extern) |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3355 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 3358 | return false; |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3359 | } |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3360 | |
Richard Smith | f3814ad | 2013-01-25 00:08:28 +0000 | [diff] [blame] | 3361 | // The rest of this function is C-only. |
| 3362 | assert(!Context.getLangOpts().CPlusPlus && |
| 3363 | "should not use C inline rules in C++"); |
| 3364 | |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3365 | // C99 6.7.4p6: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3366 | // [...] If all of the file scope declarations for a function in a |
| 3367 | // translation unit include the inline function specifier without extern, |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3368 | // then the definition in that translation unit is an inline definition. |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 3369 | for (auto Redecl : redecls()) { |
| 3370 | if (RedeclForcesDefC99(Redecl)) |
Eli Friedman | 1b125c3 | 2012-02-07 03:50:18 +0000 | [diff] [blame] | 3371 | return true; |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3372 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3373 | |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3374 | // C99 6.7.4p6: |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3375 | // An inline definition does not provide an external definition for the |
| 3376 | // function, and does not forbid an external definition in another |
Douglas Gregor | 299d76e | 2009-09-13 07:46:26 +0000 | [diff] [blame] | 3377 | // translation unit. |
Douglas Gregor | 76fe50c | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 3378 | return false; |
| 3379 | } |
| 3380 | |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3381 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 3382 | /// function represents, if any. |
| 3383 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
Douglas Gregor | 163c585 | 2008-11-18 14:39:36 +0000 | [diff] [blame] | 3384 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 3385 | return getDeclName().getCXXOverloadedOperator(); |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3386 | else |
| 3387 | return OO_None; |
| 3388 | } |
| 3389 | |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 3390 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 3391 | /// represents, if any. |
| 3392 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 3393 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 3394 | return getDeclName().getCXXLiteralIdentifier(); |
| 3395 | else |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3396 | return nullptr; |
Alexis Hunt | c88db06 | 2010-01-13 09:01:02 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 3399 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 3400 | if (TemplateOrSpecialization.isNull()) |
| 3401 | return TK_NonTemplate; |
| 3402 | if (TemplateOrSpecialization.is<FunctionTemplateDecl *>()) |
| 3403 | return TK_FunctionTemplate; |
| 3404 | if (TemplateOrSpecialization.is<MemberSpecializationInfo *>()) |
| 3405 | return TK_MemberSpecialization; |
| 3406 | if (TemplateOrSpecialization.is<FunctionTemplateSpecializationInfo *>()) |
| 3407 | return TK_FunctionTemplateSpecialization; |
| 3408 | if (TemplateOrSpecialization.is |
| 3409 | <DependentFunctionTemplateSpecializationInfo*>()) |
| 3410 | return TK_DependentFunctionTemplateSpecialization; |
| 3411 | |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3412 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?"); |
Argyrios Kyrtzidis | cb6f346 | 2010-06-22 09:54:51 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3415 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 3416 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3417 | return cast<FunctionDecl>(Info->getInstantiatedFrom()); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3418 | |
| 3419 | return nullptr; |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3420 | } |
| 3421 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3422 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3423 | if (auto *MSI = |
| 3424 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
| 3425 | return MSI; |
| 3426 | if (auto *FTSI = TemplateOrSpecialization |
| 3427 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) |
| 3428 | return FTSI->getMemberSpecializationInfo(); |
| 3429 | return nullptr; |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3432 | void |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3433 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 3434 | FunctionDecl *FD, |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3435 | TemplateSpecializationKind TSK) { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3436 | assert(TemplateOrSpecialization.isNull() && |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3437 | "Member function is already a specialization"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3438 | MemberSpecializationInfo *Info |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3439 | = new (C) MemberSpecializationInfo(FD, TSK); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3440 | TemplateOrSpecialization = Info; |
| 3441 | } |
| 3442 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3443 | FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const { |
| 3444 | return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl *>(); |
| 3445 | } |
| 3446 | |
| 3447 | void FunctionDecl::setDescribedFunctionTemplate(FunctionTemplateDecl *Template) { |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3448 | assert(TemplateOrSpecialization.isNull() && |
| 3449 | "Member function is already a specialization"); |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3450 | TemplateOrSpecialization = Template; |
| 3451 | } |
| 3452 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3453 | bool FunctionDecl::isImplicitlyInstantiable() const { |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 3454 | // If the function is invalid, it can't be implicitly instantiated. |
| 3455 | if (isInvalidDecl()) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3456 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3457 | |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3458 | switch (getTemplateSpecializationKindForInstantiation()) { |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3459 | case TSK_Undeclared: |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3460 | case TSK_ExplicitInstantiationDefinition: |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3461 | case TSK_ExplicitSpecialization: |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3462 | return false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3463 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3464 | case TSK_ImplicitInstantiation: |
| 3465 | return true; |
| 3466 | |
| 3467 | case TSK_ExplicitInstantiationDeclaration: |
| 3468 | // Handled below. |
| 3469 | break; |
| 3470 | } |
| 3471 | |
| 3472 | // Find the actual template from which we will instantiate. |
| 3473 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3474 | bool HasPattern = false; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3475 | if (PatternDecl) |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3476 | HasPattern = PatternDecl->hasBody(PatternDecl); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3477 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3478 | // C++0x [temp.explicit]p9: |
| 3479 | // Except for inline functions, other explicit instantiation declarations |
| 3480 | // have the effect of suppressing the implicit instantiation of the entity |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3481 | // to which they refer. |
| 3482 | if (!HasPattern || !PatternDecl) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3483 | return true; |
| 3484 | |
Douglas Gregor | 583dcaf | 2009-10-27 21:11:48 +0000 | [diff] [blame] | 3485 | return PatternDecl->isInlined(); |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | bool FunctionDecl::isTemplateInstantiation() const { |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3489 | // FIXME: Remove this, it's not clear what it means. (Which template |
| 3490 | // specialization kind?) |
| 3491 | return clang::isTemplateInstantiation(getTemplateSpecializationKind()); |
Ted Kremenek | 85825ae | 2011-12-01 00:59:17 +0000 | [diff] [blame] | 3492 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3493 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3494 | FunctionDecl *FunctionDecl::getTemplateInstantiationPattern() const { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3495 | // If this is a generic lambda call operator specialization, its |
Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3496 | // instantiation pattern is always its primary template's pattern |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3497 | // even if its primary template was instantiated from another |
Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3498 | // member template (which happens with nested generic lambdas). |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3499 | // Since a lambda's call operator's body is transformed eagerly, |
| 3500 | // we don't have to go hunting for a prototype definition template |
| 3501 | // (i.e. instantiated-from-member-template) to use as an instantiation |
Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3502 | // pattern. |
Francois Pichet | 00c7e6c | 2011-08-14 03:52:19 +0000 | [diff] [blame] | 3503 | |
Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3504 | if (isGenericLambdaCallOperatorSpecialization( |
| 3505 | dyn_cast<CXXMethodDecl>(this))) { |
Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3506 | assert(getPrimaryTemplate() && "not a generic lambda call operator?"); |
| 3507 | return getDefinitionOrSelf(getPrimaryTemplate()->getTemplatedDecl()); |
Faisal Vali | b90b211 | 2014-04-03 16:32:21 +0000 | [diff] [blame] | 3508 | } |
Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3509 | |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3510 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) { |
| 3511 | if (!clang::isTemplateInstantiation(Info->getTemplateSpecializationKind())) |
| 3512 | return nullptr; |
| 3513 | return getDefinitionOrSelf(cast<FunctionDecl>(Info->getInstantiatedFrom())); |
| 3514 | } |
| 3515 | |
| 3516 | if (!clang::isTemplateInstantiation(getTemplateSpecializationKind())) |
| 3517 | return nullptr; |
| 3518 | |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3519 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3520 | // If we hit a point where the user provided a specialization of this |
| 3521 | // template, we're done looking. |
| 3522 | while (!Primary->isMemberSpecialization()) { |
| 3523 | auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate(); |
| 3524 | if (!NewPrimary) |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3525 | break; |
Richard Smith | a6b41d7 | 2019-05-03 23:51:38 +0000 | [diff] [blame] | 3526 | Primary = NewPrimary; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3527 | } |
Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3528 | |
| 3529 | return getDefinitionOrSelf(Primary->getTemplatedDecl()); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3530 | } |
Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3531 | |
Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 3532 | return nullptr; |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3535 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3537 | = TemplateOrSpecialization |
| 3538 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3539 | return Info->getTemplate(); |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3540 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3541 | return nullptr; |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3544 | FunctionTemplateSpecializationInfo * |
| 3545 | FunctionDecl::getTemplateSpecializationInfo() const { |
| 3546 | return TemplateOrSpecialization |
| 3547 | .dyn_cast<FunctionTemplateSpecializationInfo *>(); |
| 3548 | } |
| 3549 | |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3550 | const TemplateArgumentList * |
| 3551 | FunctionDecl::getTemplateSpecializationArgs() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | if (FunctionTemplateSpecializationInfo *Info |
Douglas Gregor | cf91555 | 2009-10-13 16:30:37 +0000 | [diff] [blame] | 3553 | = TemplateOrSpecialization |
| 3554 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3555 | return Info->TemplateArguments; |
| 3556 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3557 | return nullptr; |
Douglas Gregor | 70d83e2 | 2009-06-29 17:30:29 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Argyrios Kyrtzidis | e9a2443 | 2011-09-22 20:07:09 +0000 | [diff] [blame] | 3560 | const ASTTemplateArgumentListInfo * |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3561 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 3562 | if (FunctionTemplateSpecializationInfo *Info |
| 3563 | = TemplateOrSpecialization |
| 3564 | .dyn_cast<FunctionTemplateSpecializationInfo*>()) { |
| 3565 | return Info->TemplateArgumentsAsWritten; |
| 3566 | } |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3567 | return nullptr; |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3570 | void |
Argyrios Kyrtzidis | f4bc0d8 | 2010-09-08 19:31:22 +0000 | [diff] [blame] | 3571 | FunctionDecl::setFunctionTemplateSpecialization(ASTContext &C, |
| 3572 | FunctionTemplateDecl *Template, |
Douglas Gregor | 8f5d442 | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 3573 | const TemplateArgumentList *TemplateArgs, |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3574 | void *InsertPos, |
Abramo Bagnara | 02ccd28 | 2010-05-20 15:32:11 +0000 | [diff] [blame] | 3575 | TemplateSpecializationKind TSK, |
Argyrios Kyrtzidis | 927d8e0 | 2010-07-05 10:37:55 +0000 | [diff] [blame] | 3576 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 3577 | SourceLocation PointOfInstantiation) { |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3578 | assert((TemplateOrSpecialization.isNull() || |
| 3579 | TemplateOrSpecialization.is<MemberSpecializationInfo *>()) && |
| 3580 | "Member function is already a specialization"); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3581 | assert(TSK != TSK_Undeclared && |
Douglas Gregor | 3a923c2d | 2009-09-24 23:14:47 +0000 | [diff] [blame] | 3582 | "Must specify the type of function template specialization"); |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3583 | assert((TemplateOrSpecialization.isNull() || |
| 3584 | TSK == TSK_ExplicitSpecialization) && |
| 3585 | "Member specialization must be an explicit specialization"); |
| 3586 | FunctionTemplateSpecializationInfo *Info = |
| 3587 | FunctionTemplateSpecializationInfo::Create( |
| 3588 | C, this, Template, TSK, TemplateArgs, TemplateArgsAsWritten, |
| 3589 | PointOfInstantiation, |
| 3590 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3591 | TemplateOrSpecialization = Info; |
Douglas Gregor | ce9978f | 2012-03-28 14:34:23 +0000 | [diff] [blame] | 3592 | Template->addSpecialization(Info, InsertPos); |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3595 | void |
| 3596 | FunctionDecl::setDependentTemplateSpecialization(ASTContext &Context, |
| 3597 | const UnresolvedSetImpl &Templates, |
| 3598 | const TemplateArgumentListInfo &TemplateArgs) { |
| 3599 | assert(TemplateOrSpecialization.isNull()); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3600 | DependentFunctionTemplateSpecializationInfo *Info = |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3601 | DependentFunctionTemplateSpecializationInfo::Create(Context, Templates, |
| 3602 | TemplateArgs); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3603 | TemplateOrSpecialization = Info; |
| 3604 | } |
| 3605 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3606 | DependentFunctionTemplateSpecializationInfo * |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 3607 | FunctionDecl::getDependentSpecializationInfo() const { |
| 3608 | return TemplateOrSpecialization |
| 3609 | .dyn_cast<DependentFunctionTemplateSpecializationInfo *>(); |
| 3610 | } |
| 3611 | |
| 3612 | DependentFunctionTemplateSpecializationInfo * |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3613 | DependentFunctionTemplateSpecializationInfo::Create( |
| 3614 | ASTContext &Context, const UnresolvedSetImpl &Ts, |
| 3615 | const TemplateArgumentListInfo &TArgs) { |
| 3616 | void *Buffer = Context.Allocate( |
| 3617 | totalSizeToAlloc<TemplateArgumentLoc, FunctionTemplateDecl *>( |
| 3618 | TArgs.size(), Ts.size())); |
| 3619 | return new (Buffer) DependentFunctionTemplateSpecializationInfo(Ts, TArgs); |
| 3620 | } |
| 3621 | |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3622 | DependentFunctionTemplateSpecializationInfo:: |
| 3623 | DependentFunctionTemplateSpecializationInfo(const UnresolvedSetImpl &Ts, |
| 3624 | const TemplateArgumentListInfo &TArgs) |
Benjamin Kramer | 3ebba52 | 2015-04-02 12:43:31 +0000 | [diff] [blame] | 3625 | : AngleLocs(TArgs.getLAngleLoc(), TArgs.getRAngleLoc()) { |
James Y Knight | 53c7616 | 2015-07-17 18:21:37 +0000 | [diff] [blame] | 3626 | NumTemplates = Ts.size(); |
| 3627 | NumArgs = TArgs.size(); |
Benjamin Kramer | 3ebba52 | 2015-04-02 12:43:31 +0000 | [diff] [blame] | 3628 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3629 | FunctionTemplateDecl **TsArray = getTrailingObjects<FunctionTemplateDecl *>(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3630 | for (unsigned I = 0, E = Ts.size(); I != E; ++I) |
| 3631 | TsArray[I] = cast<FunctionTemplateDecl>(Ts[I]->getUnderlyingDecl()); |
| 3632 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 3633 | TemplateArgumentLoc *ArgsArray = getTrailingObjects<TemplateArgumentLoc>(); |
John McCall | b9c7848 | 2010-04-08 09:05:18 +0000 | [diff] [blame] | 3634 | for (unsigned I = 0, E = TArgs.size(); I != E; ++I) |
| 3635 | new (&ArgsArray[I]) TemplateArgumentLoc(TArgs[I]); |
| 3636 | } |
| 3637 | |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3638 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3639 | // For a function template specialization, query the specialization |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3640 | // information object. |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3641 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
| 3642 | TemplateOrSpecialization |
| 3643 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3644 | return FTSInfo->getTemplateSpecializationKind(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3645 | |
Richard Smith | f19a8b0 | 2019-05-02 00:49:14 +0000 | [diff] [blame] | 3646 | if (MemberSpecializationInfo *MSInfo = |
| 3647 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
| 3648 | return MSInfo->getTemplateSpecializationKind(); |
| 3649 | |
| 3650 | return TSK_Undeclared; |
| 3651 | } |
| 3652 | |
| 3653 | TemplateSpecializationKind |
| 3654 | FunctionDecl::getTemplateSpecializationKindForInstantiation() const { |
| 3655 | // This is the same as getTemplateSpecializationKind(), except that for a |
| 3656 | // function that is both a function template specialization and a member |
| 3657 | // specialization, we prefer the member specialization information. Eg: |
| 3658 | // |
| 3659 | // template<typename T> struct A { |
| 3660 | // template<typename U> void f() {} |
| 3661 | // template<> void f<int>() {} |
| 3662 | // }; |
| 3663 | // |
| 3664 | // For A<int>::f<int>(): |
| 3665 | // * getTemplateSpecializationKind() will return TSK_ExplicitSpecialization |
| 3666 | // * getTemplateSpecializationKindForInstantiation() will return |
| 3667 | // TSK_ImplicitInstantiation |
| 3668 | // |
| 3669 | // This reflects the facts that A<int>::f<int> is an explicit specialization |
| 3670 | // of A<int>::f, and that A<int>::f<int> should be implicitly instantiated |
| 3671 | // from A::f<int> if a definition is needed. |
| 3672 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
| 3673 | TemplateOrSpecialization |
| 3674 | .dyn_cast<FunctionTemplateSpecializationInfo *>()) { |
| 3675 | if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo()) |
| 3676 | return MSInfo->getTemplateSpecializationKind(); |
| 3677 | return FTSInfo->getTemplateSpecializationKind(); |
| 3678 | } |
| 3679 | |
| 3680 | if (MemberSpecializationInfo *MSInfo = |
| 3681 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3682 | return MSInfo->getTemplateSpecializationKind(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3684 | return TSK_Undeclared; |
Douglas Gregor | 34ec2ef | 2009-09-04 22:48:11 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | void |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3688 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 3689 | SourceLocation PointOfInstantiation) { |
| 3690 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3691 | = TemplateOrSpecialization.dyn_cast< |
| 3692 | FunctionTemplateSpecializationInfo*>()) { |
| 3693 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 3694 | if (TSK != TSK_ExplicitSpecialization && |
| 3695 | PointOfInstantiation.isValid() && |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3696 | FTSInfo->getPointOfInstantiation().isInvalid()) { |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3697 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3698 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 3699 | L->InstantiationRequested(this); |
| 3700 | } |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3701 | } else if (MemberSpecializationInfo *MSInfo |
| 3702 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) { |
| 3703 | MSInfo->setTemplateSpecializationKind(TSK); |
| 3704 | if (TSK != TSK_ExplicitSpecialization && |
| 3705 | PointOfInstantiation.isValid() && |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3706 | MSInfo->getPointOfInstantiation().isInvalid()) { |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3707 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
Richard Smith | 891fc7f | 2017-12-05 01:31:47 +0000 | [diff] [blame] | 3708 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 3709 | L->InstantiationRequested(this); |
| 3710 | } |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3711 | } else |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3712 | llvm_unreachable("Function cannot have a template specialization kind"); |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3713 | } |
| 3714 | |
| 3715 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3716 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 3717 | = TemplateOrSpecialization.dyn_cast< |
| 3718 | FunctionTemplateSpecializationInfo*>()) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3719 | return FTSInfo->getPointOfInstantiation(); |
Douglas Gregor | d801b06 | 2009-10-07 23:56:10 +0000 | [diff] [blame] | 3720 | else if (MemberSpecializationInfo *MSInfo |
| 3721 | = TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo*>()) |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3722 | return MSInfo->getPointOfInstantiation(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3723 | |
Douglas Gregor | 3d7e69f | 2009-10-15 17:21:20 +0000 | [diff] [blame] | 3724 | return SourceLocation(); |
Douglas Gregor | e8925db | 2009-06-29 22:39:32 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3727 | bool FunctionDecl::isOutOfLine() const { |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 3728 | if (Decl::isOutOfLine()) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3729 | return true; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3730 | |
| 3731 | // If this function was instantiated from a member function of a |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3732 | // class template, check whether that member function was defined out-of-line. |
| 3733 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 3734 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3735 | if (FD->hasBody(Definition)) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3736 | return Definition->isOutOfLine(); |
| 3737 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3738 | |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3739 | // If this function was instantiated from a function template, |
| 3740 | // check whether that function template was defined out-of-line. |
| 3741 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 3742 | const FunctionDecl *Definition; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3743 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3744 | return Definition->isOutOfLine(); |
| 3745 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3746 | |
Douglas Gregor | 6411b92 | 2009-09-11 20:15:17 +0000 | [diff] [blame] | 3747 | return false; |
| 3748 | } |
| 3749 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 3750 | SourceRange FunctionDecl::getSourceRange() const { |
| 3751 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 3752 | } |
| 3753 | |
Anna Zaks | 28db7ce | 2012-01-18 02:45:01 +0000 | [diff] [blame] | 3754 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3755 | IdentifierInfo *FnInfo = getIdentifier(); |
| 3756 | |
| 3757 | if (!FnInfo) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3758 | return 0; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3759 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3760 | // Builtin handling. |
| 3761 | switch (getBuiltinID()) { |
| 3762 | case Builtin::BI__builtin_memset: |
| 3763 | case Builtin::BI__builtin___memset_chk: |
| 3764 | case Builtin::BImemset: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3765 | return Builtin::BImemset; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3766 | |
| 3767 | case Builtin::BI__builtin_memcpy: |
| 3768 | case Builtin::BI__builtin___memcpy_chk: |
| 3769 | case Builtin::BImemcpy: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3770 | return Builtin::BImemcpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3771 | |
| 3772 | case Builtin::BI__builtin_memmove: |
| 3773 | case Builtin::BI__builtin___memmove_chk: |
| 3774 | case Builtin::BImemmove: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3775 | return Builtin::BImemmove; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3776 | |
| 3777 | case Builtin::BIstrlcpy: |
Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3778 | case Builtin::BI__builtin___strlcpy_chk: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3779 | return Builtin::BIstrlcpy; |
Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3780 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3781 | case Builtin::BIstrlcat: |
Fariborz Jahanian | ab4fe98 | 2014-09-12 18:44:36 +0000 | [diff] [blame] | 3782 | case Builtin::BI__builtin___strlcat_chk: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3783 | return Builtin::BIstrlcat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3784 | |
| 3785 | case Builtin::BI__builtin_memcmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3786 | case Builtin::BImemcmp: |
| 3787 | return Builtin::BImemcmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3788 | |
Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 3789 | case Builtin::BI__builtin_bcmp: |
| 3790 | case Builtin::BIbcmp: |
| 3791 | return Builtin::BIbcmp; |
| 3792 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3793 | case Builtin::BI__builtin_strncpy: |
| 3794 | case Builtin::BI__builtin___strncpy_chk: |
| 3795 | case Builtin::BIstrncpy: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3796 | return Builtin::BIstrncpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3797 | |
| 3798 | case Builtin::BI__builtin_strncmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3799 | case Builtin::BIstrncmp: |
| 3800 | return Builtin::BIstrncmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3801 | |
| 3802 | case Builtin::BI__builtin_strncasecmp: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3803 | case Builtin::BIstrncasecmp: |
| 3804 | return Builtin::BIstrncasecmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3805 | |
| 3806 | case Builtin::BI__builtin_strncat: |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3807 | case Builtin::BI__builtin___strncat_chk: |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3808 | case Builtin::BIstrncat: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3809 | return Builtin::BIstrncat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3810 | |
| 3811 | case Builtin::BI__builtin_strndup: |
| 3812 | case Builtin::BIstrndup: |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3813 | return Builtin::BIstrndup; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3814 | |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3815 | case Builtin::BI__builtin_strlen: |
| 3816 | case Builtin::BIstrlen: |
| 3817 | return Builtin::BIstrlen; |
| 3818 | |
Bruno Cardoso Lopes | 7ea9fd2 | 2016-08-10 18:34:47 +0000 | [diff] [blame] | 3819 | case Builtin::BI__builtin_bzero: |
| 3820 | case Builtin::BIbzero: |
| 3821 | return Builtin::BIbzero; |
| 3822 | |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3823 | default: |
Rafael Espindola | 5bda63f | 2013-02-14 01:47:04 +0000 | [diff] [blame] | 3824 | if (isExternC()) { |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3825 | if (FnInfo->isStr("memset")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3826 | return Builtin::BImemset; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3827 | else if (FnInfo->isStr("memcpy")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3828 | return Builtin::BImemcpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3829 | else if (FnInfo->isStr("memmove")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3830 | return Builtin::BImemmove; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3831 | else if (FnInfo->isStr("memcmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3832 | return Builtin::BImemcmp; |
Clement Courbet | 8c3343d | 2019-02-14 12:00:34 +0000 | [diff] [blame] | 3833 | else if (FnInfo->isStr("bcmp")) |
| 3834 | return Builtin::BIbcmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3835 | else if (FnInfo->isStr("strncpy")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3836 | return Builtin::BIstrncpy; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3837 | else if (FnInfo->isStr("strncmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3838 | return Builtin::BIstrncmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3839 | else if (FnInfo->isStr("strncasecmp")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3840 | return Builtin::BIstrncasecmp; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3841 | else if (FnInfo->isStr("strncat")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3842 | return Builtin::BIstrncat; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3843 | else if (FnInfo->isStr("strndup")) |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3844 | return Builtin::BIstrndup; |
Anna Zaks | 314cd09 | 2012-02-01 19:08:57 +0000 | [diff] [blame] | 3845 | else if (FnInfo->isStr("strlen")) |
| 3846 | return Builtin::BIstrlen; |
Bruno Cardoso Lopes | 7ea9fd2 | 2016-08-10 18:34:47 +0000 | [diff] [blame] | 3847 | else if (FnInfo->isStr("bzero")) |
| 3848 | return Builtin::BIbzero; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3849 | } |
| 3850 | break; |
| 3851 | } |
Anna Zaks | 2212270 | 2012-01-17 00:37:07 +0000 | [diff] [blame] | 3852 | return 0; |
Anna Zaks | 201d489 | 2012-01-13 21:52:01 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
Richard Trieu | 27c1b1a | 2018-07-10 01:40:50 +0000 | [diff] [blame] | 3855 | unsigned FunctionDecl::getODRHash() const { |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3856 | assert(hasODRHash()); |
Richard Trieu | 27c1b1a | 2018-07-10 01:40:50 +0000 | [diff] [blame] | 3857 | return ODRHash; |
| 3858 | } |
| 3859 | |
Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3860 | unsigned FunctionDecl::getODRHash() { |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3861 | if (hasODRHash()) |
Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3862 | return ODRHash; |
| 3863 | |
Richard Trieu | 0ac2eb7 | 2018-02-22 05:50:29 +0000 | [diff] [blame] | 3864 | if (auto *FT = getInstantiatedFromMemberFunction()) { |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3865 | setHasODRHash(true); |
Richard Trieu | 0ac2eb7 | 2018-02-22 05:50:29 +0000 | [diff] [blame] | 3866 | ODRHash = FT->getODRHash(); |
| 3867 | return ODRHash; |
| 3868 | } |
| 3869 | |
Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3870 | class ODRHash Hash; |
| 3871 | Hash.AddFunctionDecl(this); |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 3872 | setHasODRHash(true); |
Richard Trieu | e6caa26 | 2017-12-23 00:41:01 +0000 | [diff] [blame] | 3873 | ODRHash = Hash.CalculateHash(); |
| 3874 | return ODRHash; |
| 3875 | } |
| 3876 | |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 3877 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3878 | // FieldDecl Implementation |
| 3879 | //===----------------------------------------------------------------------===// |
| 3880 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3881 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3882 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 3883 | IdentifierInfo *Id, QualType T, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 3884 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
Richard Smith | 2b01318 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 3885 | InClassInitStyle InitStyle) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 3886 | return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
| 3887 | BW, Mutable, InitStyle); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3890 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3891 | return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), |
| 3892 | SourceLocation(), nullptr, QualType(), nullptr, |
| 3893 | nullptr, false, ICIS_NoInit); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 3894 | } |
| 3895 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3896 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 3897 | if (!isImplicit() || getDeclName()) |
| 3898 | return false; |
| 3899 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3900 | if (const auto *Record = getType()->getAs<RecordType>()) |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3901 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 3902 | |
| 3903 | return false; |
| 3904 | } |
| 3905 | |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3906 | unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const { |
| 3907 | assert(isBitField() && "not a bitfield"); |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3908 | return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue(); |
Richard Smith | caf3390 | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Richard Smith | 866dee4 | 2018-04-02 18:29:43 +0000 | [diff] [blame] | 3911 | bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const { |
| 3912 | return isUnnamedBitfield() && !getBitWidth()->isValueDependent() && |
| 3913 | getBitWidthValue(Ctx) == 0; |
| 3914 | } |
| 3915 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3916 | unsigned FieldDecl::getFieldIndex() const { |
Richard Smith | 0b87e07 | 2013-10-07 08:02:11 +0000 | [diff] [blame] | 3917 | const FieldDecl *Canonical = getCanonicalDecl(); |
| 3918 | if (Canonical != this) |
| 3919 | return Canonical->getFieldIndex(); |
| 3920 | |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3921 | if (CachedFieldIndex) return CachedFieldIndex - 1; |
| 3922 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3923 | unsigned Index = 0; |
Richard Smith | 85567dd | 2017-11-15 01:33:46 +0000 | [diff] [blame] | 3924 | const RecordDecl *RD = getParent()->getDefinition(); |
| 3925 | assert(RD && "requested index for field of struct with no definition"); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3926 | |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 3927 | for (auto *Field : RD->fields()) { |
| 3928 | Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; |
| 3929 | ++Index; |
| 3930 | } |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3931 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 3932 | assert(CachedFieldIndex && "failed to find field in parent"); |
| 3933 | return CachedFieldIndex - 1; |
John McCall | 4e81961 | 2011-01-20 07:57:12 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Abramo Bagnara | 20c9e24 | 2011-03-08 11:07:11 +0000 | [diff] [blame] | 3936 | SourceRange FieldDecl::getSourceRange() const { |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3937 | const Expr *FinalExpr = getInClassInitializer(); |
| 3938 | if (!FinalExpr) |
| 3939 | FinalExpr = getBitWidth(); |
| 3940 | if (FinalExpr) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3941 | return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc()); |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3942 | return DeclaratorDecl::getSourceRange(); |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { |
David Blaikie | 11ab078 | 2014-10-31 17:18:09 +0000 | [diff] [blame] | 3946 | assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 3947 | "capturing type in non-lambda or captured record."); |
Richard Smith | 6b8e3c0 | 2017-08-28 00:28:14 +0000 | [diff] [blame] | 3948 | assert(InitStorage.getInt() == ISK_NoInit && |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3949 | InitStorage.getPointer() == nullptr && |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3950 | "bit width, initializer or captured type already set"); |
John McCall | c90c149 | 2014-10-10 18:44:34 +0000 | [diff] [blame] | 3951 | InitStorage.setPointerAndInt(const_cast<VariableArrayType *>(VLAType), |
| 3952 | ISK_CapturedVLAType); |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 3955 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 3956 | // TagDecl Implementation |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 3957 | //===----------------------------------------------------------------------===// |
| 3958 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3959 | TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
| 3960 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
| 3961 | SourceLocation StartL) |
| 3962 | : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C), |
| 3963 | TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) { |
| 3964 | assert((DK != Enum || TK == TTK_Enum) && |
| 3965 | "EnumDecl not matched with TTK_Enum"); |
| 3966 | setPreviousDecl(PrevDecl); |
| 3967 | setTagKind(TK); |
| 3968 | setCompleteDefinition(false); |
| 3969 | setBeingDefined(false); |
| 3970 | setEmbeddedInDeclarator(false); |
| 3971 | setFreeStanding(false); |
| 3972 | setCompleteDefinitionRequired(false); |
| 3973 | } |
| 3974 | |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3975 | SourceLocation TagDecl::getOuterLocStart() const { |
| 3976 | return getTemplateOrInnerLocStart(this); |
| 3977 | } |
| 3978 | |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3979 | SourceRange TagDecl::getSourceRange() const { |
Argyrios Kyrtzidis | d798c05 | 2016-07-15 18:11:33 +0000 | [diff] [blame] | 3980 | SourceLocation RBraceLoc = BraceRange.getEnd(); |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3981 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
Douglas Gregor | ec9c6ae | 2010-07-06 18:42:40 +0000 | [diff] [blame] | 3982 | return SourceRange(getOuterLocStart(), E); |
Argyrios Kyrtzidis | 575fa05 | 2009-07-14 03:17:17 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Rafael Espindola | 8db352d | 2013-10-17 15:37:26 +0000 | [diff] [blame] | 3985 | TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } |
Argyrios Kyrtzidis | 5614aef | 2009-07-18 00:34:07 +0000 | [diff] [blame] | 3986 | |
Rafael Espindola | bf5c33b | 2013-03-12 21:06:00 +0000 | [diff] [blame] | 3987 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 3988 | TypedefNameDeclOrQualifier = TDD; |
Reid Kleckner | cae82a2 | 2014-04-23 22:03:04 +0000 | [diff] [blame] | 3989 | if (const Type *T = getTypeForDecl()) { |
| 3990 | (void)T; |
Richard Smith | 5b21db8 | 2014-04-23 18:20:42 +0000 | [diff] [blame] | 3991 | assert(T->isLinkageValid()); |
Reid Kleckner | cae82a2 | 2014-04-23 22:03:04 +0000 | [diff] [blame] | 3992 | } |
Rafael Espindola | 0e0d009 | 2013-03-14 03:07:35 +0000 | [diff] [blame] | 3993 | assert(isLinkageValid()); |
Douglas Gregor | a72a4e3 | 2010-05-19 18:39:18 +0000 | [diff] [blame] | 3994 | } |
| 3995 | |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 3996 | void TagDecl::startDefinition() { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 3997 | setBeingDefined(true); |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 3998 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 3999 | if (auto *D = dyn_cast<CXXRecordDecl>(this)) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4000 | struct CXXRecordDecl::DefinitionData *Data = |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4001 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 4002 | for (auto I : redecls()) |
Richard Smith | 64c0630 | 2014-05-22 23:19:02 +0000 | [diff] [blame] | 4003 | cast<CXXRecordDecl>(I)->DefinitionData = Data; |
John McCall | 67da35c | 2010-02-04 22:26:26 +0000 | [diff] [blame] | 4004 | } |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | void TagDecl::completeDefinition() { |
John McCall | ae580fe | 2010-02-05 01:33:36 +0000 | [diff] [blame] | 4008 | assert((!isa<CXXRecordDecl>(this) || |
| 4009 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 4010 | "definition completed but not started"); |
| 4011 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4012 | setCompleteDefinition(true); |
| 4013 | setBeingDefined(false); |
Argyrios Kyrtzidis | d170d84 | 2010-10-24 17:26:50 +0000 | [diff] [blame] | 4014 | |
| 4015 | if (ASTMutationListener *L = getASTMutationListener()) |
| 4016 | L->CompletedTagDefinition(this); |
Douglas Gregor | dee1be8 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 4017 | } |
| 4018 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4019 | TagDecl *TagDecl::getDefinition() const { |
| 4020 | if (isCompleteDefinition()) |
Douglas Gregor | b6b8f9e | 2009-07-29 23:36:44 +0000 | [diff] [blame] | 4021 | return const_cast<TagDecl *>(this); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4022 | |
| 4023 | // 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] | 4024 | if (mayHaveOutOfDateDef()) { |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4025 | if (IdentifierInfo *II = getIdentifier()) { |
| 4026 | if (II->isOutOfDate()) { |
| 4027 | updateOutOfDate(*II); |
| 4028 | } |
| 4029 | } |
| 4030 | } |
| 4031 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4032 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(this)) |
Andrew Trick | ba266ee | 2010-10-19 21:54:32 +0000 | [diff] [blame] | 4033 | return CXXRD->getDefinition(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 4035 | for (auto R : redecls()) |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4036 | if (R->isCompleteDefinition()) |
Aaron Ballman | 86c9390 | 2014-03-06 23:45:36 +0000 | [diff] [blame] | 4037 | return R; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4038 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4039 | return nullptr; |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4042 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 4043 | if (QualifierLoc) { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4044 | // Make sure the extended qualifier info is allocated. |
| 4045 | if (!hasExtInfo()) |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 4046 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4047 | // Set qualifier info. |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4048 | getExtInfo()->QualifierLoc = QualifierLoc; |
Chad Rosier | 6fdf38b | 2011-08-17 23:08:45 +0000 | [diff] [blame] | 4049 | } else { |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4050 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4051 | if (hasExtInfo()) { |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4052 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 4053 | getASTContext().Deallocate(getExtInfo()); |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 4054 | TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4055 | } |
| 4056 | else |
| 4057 | getExtInfo()->QualifierLoc = QualifierLoc; |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4058 | } |
| 4059 | } |
| 4060 | } |
| 4061 | |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 4062 | void TagDecl::setTemplateParameterListsInfo( |
| 4063 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 4064 | assert(!TPLists.empty()); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4065 | // Make sure the extended decl info is allocated. |
| 4066 | if (!hasExtInfo()) |
| 4067 | // Allocate external info struct. |
David Majnemer | 0035052 | 2015-08-31 18:48:39 +0000 | [diff] [blame] | 4068 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4069 | // Set the template parameter lists info. |
Benjamin Kramer | 9cc21065 | 2015-08-05 09:40:49 +0000 | [diff] [blame] | 4070 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
Abramo Bagnara | 60804e1 | 2011-03-18 15:16:37 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4073 | //===----------------------------------------------------------------------===// |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4074 | // EnumDecl Implementation |
| 4075 | //===----------------------------------------------------------------------===// |
| 4076 | |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4077 | EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 4078 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
| 4079 | bool Scoped, bool ScopedUsingClassTag, bool Fixed) |
| 4080 | : TagDecl(Enum, TTK_Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
| 4081 | assert(Scoped || !ScopedUsingClassTag); |
| 4082 | IntegerType = nullptr; |
| 4083 | setNumPositiveBits(0); |
| 4084 | setNumNegativeBits(0); |
| 4085 | setScoped(Scoped); |
| 4086 | setScopedUsingClassTag(ScopedUsingClassTag); |
| 4087 | setFixed(Fixed); |
| 4088 | setHasODRHash(false); |
| 4089 | ODRHash = 0; |
| 4090 | } |
| 4091 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4092 | void EnumDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4093 | |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4094 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 4095 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4096 | IdentifierInfo *Id, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 4097 | EnumDecl *PrevDecl, bool IsScoped, |
| 4098 | bool IsScopedUsingClassTag, bool IsFixed) { |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4099 | auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, |
| 4100 | IsScoped, IsScopedUsingClassTag, IsFixed); |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4101 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4102 | C.getTypeDeclType(Enum, PrevDecl); |
| 4103 | return Enum; |
| 4104 | } |
| 4105 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4106 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4107 | EnumDecl *Enum = |
| 4108 | new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 4109 | nullptr, nullptr, false, false, false); |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4110 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4111 | return Enum; |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Alp Toker | b9fa512 | 2014-01-06 11:31:18 +0000 | [diff] [blame] | 4114 | SourceRange EnumDecl::getIntegerTypeRange() const { |
| 4115 | if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) |
| 4116 | return TI->getTypeLoc().getSourceRange(); |
| 4117 | return SourceRange(); |
| 4118 | } |
| 4119 | |
Douglas Gregor | d505812 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 4120 | void EnumDecl::completeDefinition(QualType NewType, |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4121 | QualType NewPromotionType, |
| 4122 | unsigned NumPositiveBits, |
| 4123 | unsigned NumNegativeBits) { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4124 | assert(!isCompleteDefinition() && "Cannot redefine enums!"); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 4125 | if (!IntegerType) |
| 4126 | IntegerType = NewType.getTypePtr(); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4127 | PromotionType = NewPromotionType; |
John McCall | 9aa35be | 2010-05-06 08:49:23 +0000 | [diff] [blame] | 4128 | setNumPositiveBits(NumPositiveBits); |
| 4129 | setNumNegativeBits(NumNegativeBits); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4130 | TagDecl::completeDefinition(); |
| 4131 | } |
| 4132 | |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 4133 | bool EnumDecl::isClosed() const { |
| 4134 | if (const auto *A = getAttr<EnumExtensibilityAttr>()) |
| 4135 | return A->getExtensibility() == EnumExtensibilityAttr::Closed; |
| 4136 | return true; |
| 4137 | } |
| 4138 | |
| 4139 | bool EnumDecl::isClosedFlag() const { |
| 4140 | return isClosed() && hasAttr<FlagEnumAttr>(); |
| 4141 | } |
| 4142 | |
| 4143 | bool EnumDecl::isClosedNonFlag() const { |
| 4144 | return isClosed() && !hasAttr<FlagEnumAttr>(); |
| 4145 | } |
| 4146 | |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 4147 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 4148 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 4149 | return MSI->getTemplateSpecializationKind(); |
| 4150 | |
| 4151 | return TSK_Undeclared; |
| 4152 | } |
| 4153 | |
| 4154 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 4155 | SourceLocation PointOfInstantiation) { |
| 4156 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 4157 | assert(MSI && "Not an instantiated member enumeration?"); |
| 4158 | MSI->setTemplateSpecializationKind(TSK); |
| 4159 | if (TSK != TSK_ExplicitSpecialization && |
| 4160 | PointOfInstantiation.isValid() && |
| 4161 | MSI->getPointOfInstantiation().isInvalid()) |
| 4162 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 4163 | } |
| 4164 | |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4165 | EnumDecl *EnumDecl::getTemplateInstantiationPattern() const { |
| 4166 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
| 4167 | if (isTemplateInstantiation(MSInfo->getTemplateSpecializationKind())) { |
| 4168 | EnumDecl *ED = getInstantiatedFromMemberEnum(); |
| 4169 | while (auto *NewED = ED->getInstantiatedFromMemberEnum()) |
| 4170 | ED = NewED; |
Richard Smith | 2195ec9 | 2017-04-21 01:15:13 +0000 | [diff] [blame] | 4171 | return getDefinitionOrSelf(ED); |
Richard Smith | 6739a10 | 2016-05-05 00:56:12 +0000 | [diff] [blame] | 4172 | } |
| 4173 | } |
| 4174 | |
| 4175 | assert(!isTemplateInstantiation(getTemplateSpecializationKind()) && |
| 4176 | "couldn't find pattern for enum instantiation"); |
| 4177 | return nullptr; |
| 4178 | } |
| 4179 | |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4180 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 4181 | if (SpecializationInfo) |
| 4182 | return cast<EnumDecl>(SpecializationInfo->getInstantiatedFrom()); |
| 4183 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4184 | return nullptr; |
Richard Smith | 4b38ded | 2012-03-14 23:13:10 +0000 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 4188 | TemplateSpecializationKind TSK) { |
| 4189 | assert(!SpecializationInfo && "Member enum is already a specialization"); |
| 4190 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 4191 | } |
| 4192 | |
Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4193 | unsigned EnumDecl::getODRHash() { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4194 | if (hasODRHash()) |
Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4195 | return ODRHash; |
| 4196 | |
| 4197 | class ODRHash Hash; |
| 4198 | Hash.AddEnumDecl(this); |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4199 | setHasODRHash(true); |
Richard Trieu | ab4d730 | 2018-07-25 22:52:05 +0000 | [diff] [blame] | 4200 | ODRHash = Hash.CalculateHash(); |
| 4201 | return ODRHash; |
| 4202 | } |
| 4203 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4204 | //===----------------------------------------------------------------------===// |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 4205 | // RecordDecl Implementation |
| 4206 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4194315 | 2007-01-25 04:52:46 +0000 | [diff] [blame] | 4207 | |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4208 | RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, |
| 4209 | DeclContext *DC, SourceLocation StartLoc, |
| 4210 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 4211 | RecordDecl *PrevDecl) |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4212 | : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
| 4213 | assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!"); |
| 4214 | setHasFlexibleArrayMember(false); |
| 4215 | setAnonymousStructOrUnion(false); |
| 4216 | setHasObjectMember(false); |
| 4217 | setHasVolatileMember(false); |
| 4218 | setHasLoadedFieldsFromExternalStorage(false); |
| 4219 | setNonTrivialToPrimitiveDefaultInitialize(false); |
| 4220 | setNonTrivialToPrimitiveCopy(false); |
| 4221 | setNonTrivialToPrimitiveDestroy(false); |
| 4222 | setParamDestroyedInCallee(false); |
| 4223 | setArgPassingRestrictions(APK_CanPassInRegs); |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Jay Foad | 39c7980 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 4226 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
Abramo Bagnara | 29c2d46 | 2011-03-09 14:09:51 +0000 | [diff] [blame] | 4227 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4228 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4229 | RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, |
| 4230 | StartLoc, IdLoc, Id, PrevDecl); |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4231 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4232 | |
Ted Kremenek | 2147570 | 2008-09-05 17:16:31 +0000 | [diff] [blame] | 4233 | C.getTypeDeclType(R, PrevDecl); |
| 4234 | return R; |
Ted Kremenek | 52baf50 | 2008-09-02 21:12:32 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4237 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4238 | RecordDecl *R = |
| 4239 | new (C, ID) RecordDecl(Record, TTK_Struct, C, nullptr, SourceLocation(), |
| 4240 | SourceLocation(), nullptr, nullptr); |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4241 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
Douglas Gregor | 7dab26b | 2013-02-09 01:35:03 +0000 | [diff] [blame] | 4242 | return R; |
Argyrios Kyrtzidis | 39f0e30 | 2010-07-02 11:54:55 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 4245 | bool RecordDecl::isInjectedClassName() const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4246 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
Douglas Gregor | dfcad11 | 2009-03-25 15:59:44 +0000 | [diff] [blame] | 4247 | cast<RecordDecl>(getDeclContext())->getDeclName() == getDeclName(); |
| 4248 | } |
| 4249 | |
Alexey Bataev | 39c81e2 | 2014-08-28 04:28:19 +0000 | [diff] [blame] | 4250 | bool RecordDecl::isLambda() const { |
| 4251 | if (auto RD = dyn_cast<CXXRecordDecl>(this)) |
| 4252 | return RD->isLambda(); |
| 4253 | return false; |
| 4254 | } |
| 4255 | |
Alexey Bataev | 330de03 | 2014-10-29 12:21:55 +0000 | [diff] [blame] | 4256 | bool RecordDecl::isCapturedRecord() const { |
| 4257 | return hasAttr<CapturedRecordAttr>(); |
| 4258 | } |
| 4259 | |
| 4260 | void RecordDecl::setCapturedRecord() { |
| 4261 | addAttr(CapturedRecordAttr::CreateImplicit(getASTContext())); |
| 4262 | } |
| 4263 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4264 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4265 | if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage()) |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4266 | LoadFieldsFromExternalStorage(); |
| 4267 | |
| 4268 | return field_iterator(decl_iterator(FirstDecl)); |
| 4269 | } |
| 4270 | |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 4271 | /// completeDefinition - Notes that the definition of this type is now |
| 4272 | /// complete. |
| 4273 | void RecordDecl::completeDefinition() { |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 4274 | assert(!isCompleteDefinition() && "Cannot redefine record!"); |
Douglas Gregor | b11aad8 | 2011-02-19 18:51:44 +0000 | [diff] [blame] | 4275 | TagDecl::completeDefinition(); |
| 4276 | } |
| 4277 | |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 4278 | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
| 4279 | /// This which can be turned on with an attribute, pragma, or the |
| 4280 | /// -mms-bitfields command-line option. |
| 4281 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 4282 | return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 4283 | } |
| 4284 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4285 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 4286 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 4287 | assert(hasExternalLexicalStorage() && Source && "No external storage?"); |
| 4288 | |
| 4289 | // Notify that we have a RecordDecl doing some initialization. |
| 4290 | ExternalASTSource::Deserializing TheFields(Source); |
| 4291 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4292 | SmallVector<Decl*, 64> Decls; |
Erich Keane | f92f31c | 2018-08-01 20:48:16 +0000 | [diff] [blame] | 4293 | setHasLoadedFieldsFromExternalStorage(true); |
Richard Smith | 3cb1572 | 2015-08-05 22:41:45 +0000 | [diff] [blame] | 4294 | Source->FindExternalLexicalDecls(this, [](Decl::Kind K) { |
| 4295 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
| 4296 | }, Decls); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4297 | |
| 4298 | #ifndef NDEBUG |
| 4299 | // Check that all decls we got were FieldDecls. |
| 4300 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
Argyrios Kyrtzidis | f89a927 | 2012-09-10 22:04:22 +0000 | [diff] [blame] | 4301 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4302 | #endif |
| 4303 | |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4304 | if (Decls.empty()) |
| 4305 | return; |
| 4306 | |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 4307 | std::tie(FirstDecl, LastDecl) = BuildDeclChain(Decls, |
Argyrios Kyrtzidis | 094da73 | 2011-10-07 21:55:43 +0000 | [diff] [blame] | 4308 | /*FieldsAlreadyLoaded=*/false); |
Argyrios Kyrtzidis | 0e88a56 | 2010-10-14 20:14:34 +0000 | [diff] [blame] | 4309 | } |
| 4310 | |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4311 | bool RecordDecl::mayInsertExtraPadding(bool EmitRemark) const { |
| 4312 | ASTContext &Context = getASTContext(); |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4313 | const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask & |
| 4314 | (SanitizerKind::Address | SanitizerKind::KernelAddress); |
| 4315 | if (!EnabledAsanMask || !Context.getLangOpts().SanitizeAddressFieldPadding) |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4316 | return false; |
Alexey Samsonov | 33e00e2 | 2014-10-16 23:50:26 +0000 | [diff] [blame] | 4317 | const auto &Blacklist = Context.getSanitizerBlacklist(); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4318 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(this); |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4319 | // We may be able to relax some of these requirements. |
| 4320 | int ReasonToReject = -1; |
| 4321 | if (!CXXRD || CXXRD->isExternCContext()) |
| 4322 | ReasonToReject = 0; // is not C++. |
| 4323 | else if (CXXRD->hasAttr<PackedAttr>()) |
| 4324 | ReasonToReject = 1; // is packed. |
| 4325 | else if (CXXRD->isUnion()) |
| 4326 | ReasonToReject = 2; // is a union. |
| 4327 | else if (CXXRD->isTriviallyCopyable()) |
| 4328 | ReasonToReject = 3; // is trivially copyable. |
| 4329 | else if (CXXRD->hasTrivialDestructor()) |
| 4330 | ReasonToReject = 4; // has trivial destructor. |
| 4331 | else if (CXXRD->isStandardLayout()) |
| 4332 | ReasonToReject = 5; // is standard layout. |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4333 | else if (Blacklist.isBlacklistedLocation(EnabledAsanMask, getLocation(), |
| 4334 | "field-padding")) |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4335 | ReasonToReject = 6; // is in a blacklisted file. |
Vlad Tsyrklevich | 2eccdab | 2017-09-25 22:11:12 +0000 | [diff] [blame] | 4336 | else if (Blacklist.isBlacklistedType(EnabledAsanMask, |
| 4337 | getQualifiedNameAsString(), |
Kostya Serebryany | 293dc9b | 2014-10-16 20:54:52 +0000 | [diff] [blame] | 4338 | "field-padding")) |
| 4339 | ReasonToReject = 7; // is blacklisted. |
| 4340 | |
| 4341 | if (EmitRemark) { |
| 4342 | if (ReasonToReject >= 0) |
| 4343 | Context.getDiagnostics().Report( |
| 4344 | getLocation(), |
| 4345 | diag::remark_sanitize_address_insert_extra_padding_rejected) |
| 4346 | << getQualifiedNameAsString() << ReasonToReject; |
| 4347 | else |
| 4348 | Context.getDiagnostics().Report( |
| 4349 | getLocation(), |
| 4350 | diag::remark_sanitize_address_insert_extra_padding_accepted) |
| 4351 | << getQualifiedNameAsString(); |
| 4352 | } |
| 4353 | return ReasonToReject < 0; |
| 4354 | } |
| 4355 | |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4356 | const FieldDecl *RecordDecl::findFirstNamedDataMember() const { |
| 4357 | for (const auto *I : fields()) { |
| 4358 | if (I->getIdentifier()) |
| 4359 | return I; |
| 4360 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4361 | if (const auto *RT = I->getType()->getAs<RecordType>()) |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4362 | if (const FieldDecl *NamedDataMember = |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4363 | RT->getDecl()->findFirstNamedDataMember()) |
Evgeny Astigeevich | 665027d | 2014-12-12 16:17:46 +0000 | [diff] [blame] | 4364 | return NamedDataMember; |
| 4365 | } |
| 4366 | |
| 4367 | // We didn't find a named data member. |
| 4368 | return nullptr; |
| 4369 | } |
| 4370 | |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 4371 | //===----------------------------------------------------------------------===// |
| 4372 | // BlockDecl Implementation |
| 4373 | //===----------------------------------------------------------------------===// |
| 4374 | |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4375 | BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc) |
| 4376 | : Decl(Block, DC, CaretLoc), DeclContext(Block) { |
| 4377 | setIsVariadic(false); |
| 4378 | setCapturesCXXThis(false); |
| 4379 | setBlockMissingReturnType(true); |
| 4380 | setIsConversionFromLambda(false); |
| 4381 | setDoesNotEscape(false); |
Akira Hatanaka | c5792aa | 2019-02-27 18:17:16 +0000 | [diff] [blame] | 4382 | setCanAvoidCopyToHeap(false); |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 4385 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4386 | assert(!ParamInfo && "Already has param info!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4388 | // Zero params -> null pointer. |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4389 | if (!NewParamInfo.empty()) { |
| 4390 | NumParams = NewParamInfo.size(); |
| 4391 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 4392 | std::copy(NewParamInfo.begin(), NewParamInfo.end(), ParamInfo); |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4393 | } |
| 4394 | } |
| 4395 | |
Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4396 | void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
| 4397 | bool CapturesCXXThis) { |
Erich Keane | c9d2990 | 2018-08-01 21:16:54 +0000 | [diff] [blame] | 4398 | this->setCapturesCXXThis(CapturesCXXThis); |
Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4399 | this->NumCaptures = Captures.size(); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4400 | |
Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4401 | if (Captures.empty()) { |
| 4402 | this->Captures = nullptr; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 4403 | return; |
| 4404 | } |
| 4405 | |
Benjamin Kramer | b40e4af | 2015-08-05 09:40:35 +0000 | [diff] [blame] | 4406 | this->Captures = Captures.copy(Context).data(); |
Steve Naroff | c4b30e5 | 2009-03-13 16:56:44 +0000 | [diff] [blame] | 4407 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4408 | |
John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4409 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4410 | for (const auto &I : captures()) |
John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4411 | // Only auto vars can be captured, so no redeclaration worries. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 4412 | if (I.getVariable() == variable) |
John McCall | ce45f88 | 2011-06-15 22:51:16 +0000 | [diff] [blame] | 4413 | return true; |
| 4414 | |
| 4415 | return false; |
| 4416 | } |
| 4417 | |
Douglas Gregor | 70226da | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 4418 | SourceRange BlockDecl::getSourceRange() const { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4419 | return SourceRange(getLocation(), Body ? Body->getEndLoc() : getLocation()); |
Douglas Gregor | 70226da | 2010-12-21 16:27:07 +0000 | [diff] [blame] | 4420 | } |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4421 | |
| 4422 | //===----------------------------------------------------------------------===// |
| 4423 | // Other Decl Allocation/Deallocation Method Implementations |
| 4424 | //===----------------------------------------------------------------------===// |
| 4425 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4426 | void TranslationUnitDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4427 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4428 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4429 | return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4432 | void PragmaCommentDecl::anchor() {} |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4433 | |
| 4434 | PragmaCommentDecl *PragmaCommentDecl::Create(const ASTContext &C, |
| 4435 | TranslationUnitDecl *DC, |
| 4436 | SourceLocation CommentLoc, |
| 4437 | PragmaMSCommentKind CommentKind, |
| 4438 | StringRef Arg) { |
| 4439 | PragmaCommentDecl *PCD = |
| 4440 | new (C, DC, additionalSizeToAlloc<char>(Arg.size() + 1)) |
| 4441 | PragmaCommentDecl(DC, CommentLoc, CommentKind); |
| 4442 | memcpy(PCD->getTrailingObjects<char>(), Arg.data(), Arg.size()); |
| 4443 | PCD->getTrailingObjects<char>()[Arg.size()] = '\0'; |
| 4444 | return PCD; |
| 4445 | } |
| 4446 | |
| 4447 | PragmaCommentDecl *PragmaCommentDecl::CreateDeserialized(ASTContext &C, |
| 4448 | unsigned ID, |
| 4449 | unsigned ArgSize) { |
| 4450 | return new (C, ID, additionalSizeToAlloc<char>(ArgSize + 1)) |
| 4451 | PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); |
| 4452 | } |
| 4453 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4454 | void PragmaDetectMismatchDecl::anchor() {} |
Nico Weber | cbbaeb1 | 2016-03-02 19:28:54 +0000 | [diff] [blame] | 4455 | |
| 4456 | PragmaDetectMismatchDecl * |
| 4457 | PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, |
| 4458 | SourceLocation Loc, StringRef Name, |
| 4459 | StringRef Value) { |
| 4460 | size_t ValueStart = Name.size() + 1; |
| 4461 | PragmaDetectMismatchDecl *PDMD = |
| 4462 | new (C, DC, additionalSizeToAlloc<char>(ValueStart + Value.size() + 1)) |
| 4463 | PragmaDetectMismatchDecl(DC, Loc, ValueStart); |
| 4464 | memcpy(PDMD->getTrailingObjects<char>(), Name.data(), Name.size()); |
| 4465 | PDMD->getTrailingObjects<char>()[Name.size()] = '\0'; |
| 4466 | memcpy(PDMD->getTrailingObjects<char>() + ValueStart, Value.data(), |
| 4467 | Value.size()); |
| 4468 | PDMD->getTrailingObjects<char>()[ValueStart + Value.size()] = '\0'; |
| 4469 | return PDMD; |
| 4470 | } |
| 4471 | |
| 4472 | PragmaDetectMismatchDecl * |
| 4473 | PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 4474 | unsigned NameValueSize) { |
| 4475 | return new (C, ID, additionalSizeToAlloc<char>(NameValueSize + 1)) |
| 4476 | PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); |
| 4477 | } |
Nico Weber | 6622029 | 2016-03-02 17:28:48 +0000 | [diff] [blame] | 4478 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4479 | void ExternCContextDecl::anchor() {} |
Richard Smith | f19e127 | 2015-03-07 00:04:49 +0000 | [diff] [blame] | 4480 | |
| 4481 | ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, |
| 4482 | TranslationUnitDecl *DC) { |
| 4483 | return new (C, DC) ExternCContextDecl(DC); |
| 4484 | } |
| 4485 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4486 | void LabelDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4487 | |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4488 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 4489 | SourceLocation IdentL, IdentifierInfo *II) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4490 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); |
Abramo Bagnara | 1c3af96 | 2011-03-05 18:21:20 +0000 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 4494 | SourceLocation IdentL, IdentifierInfo *II, |
| 4495 | SourceLocation GnuLabelL) { |
| 4496 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels"); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4497 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4500 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4501 | return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, |
| 4502 | SourceLocation()); |
Douglas Gregor | 417e87c | 2010-10-27 19:49:05 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
Ehsan Akhgari | 3109758 | 2014-09-22 02:21:54 +0000 | [diff] [blame] | 4505 | void LabelDecl::setMSAsmLabel(StringRef Name) { |
| 4506 | char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; |
| 4507 | memcpy(Buffer, Name.data(), Name.size()); |
| 4508 | Buffer[Name.size()] = '\0'; |
| 4509 | MSAsmName = Buffer; |
| 4510 | } |
| 4511 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4512 | void ValueDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4513 | |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 4514 | bool ValueDecl::isWeak() const { |
Aaron Ballman | b97112e | 2014-03-08 22:19:01 +0000 | [diff] [blame] | 4515 | for (const auto *I : attrs()) |
| 4516 | if (isa<WeakAttr>(I) || isa<WeakRefAttr>(I)) |
Benjamin Kramer | ea70eb3 | 2012-12-01 15:09:41 +0000 | [diff] [blame] | 4517 | return true; |
| 4518 | |
| 4519 | return isWeakImported(); |
| 4520 | } |
| 4521 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4522 | void ImplicitParamDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4523 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4524 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4525 | SourceLocation IdLoc, |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4526 | IdentifierInfo *Id, QualType Type, |
| 4527 | ImplicitParamKind ParamKind) { |
| 4528 | return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind); |
| 4529 | } |
| 4530 | |
| 4531 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type, |
| 4532 | ImplicitParamKind ParamKind) { |
| 4533 | return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4536 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4537 | unsigned ID) { |
Alexey Bataev | 5622323 | 2017-06-09 13:40:18 +0000 | [diff] [blame] | 4538 | return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4539 | } |
| 4540 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4541 | FunctionDecl *FunctionDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4542 | SourceLocation StartLoc, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4543 | const DeclarationNameInfo &NameInfo, |
| 4544 | QualType T, TypeSourceInfo *TInfo, |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame^] | 4545 | StorageClass SC, bool isInlineSpecified, |
Richard Smith | a77a0a6 | 2011-08-15 21:04:07 +0000 | [diff] [blame] | 4546 | bool hasWrittenPrototype, |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame^] | 4547 | ConstexprSpecKind ConstexprKind) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4548 | FunctionDecl *New = |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4549 | new (C, DC) FunctionDecl(Function, C, DC, StartLoc, NameInfo, T, TInfo, |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame^] | 4550 | SC, isInlineSpecified, ConstexprKind); |
Erich Keane | 9c66506 | 2018-08-01 21:02:40 +0000 | [diff] [blame] | 4551 | New->setHasWrittenPrototype(hasWrittenPrototype); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4552 | return New; |
| 4553 | } |
| 4554 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4555 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4556 | return new (C, ID) FunctionDecl(Function, C, nullptr, SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4557 | DeclarationNameInfo(), QualType(), nullptr, |
Gauthier Harnisch | 796ed03 | 2019-06-14 08:56:20 +0000 | [diff] [blame^] | 4558 | SC_None, false, CSK_unspecified); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4559 | } |
| 4560 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4561 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4562 | return new (C, DC) BlockDecl(DC, L); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4563 | } |
| 4564 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4565 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4566 | return new (C, ID) BlockDecl(nullptr, SourceLocation()); |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4567 | } |
| 4568 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4569 | CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams) |
| 4570 | : Decl(Captured, DC, SourceLocation()), DeclContext(Captured), |
| 4571 | NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {} |
| 4572 | |
Ben Langmuir | 37943a7 | 2013-05-03 19:00:33 +0000 | [diff] [blame] | 4573 | CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, |
| 4574 | unsigned NumParams) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4575 | return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4576 | CapturedDecl(DC, NumParams); |
Tareq A. Siraj | 6dfa25a | 2013-04-16 19:37:38 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 4579 | CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4580 | unsigned NumParams) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4581 | return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(NumParams)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4582 | CapturedDecl(nullptr, NumParams); |
Ben Langmuir | ce914fc | 2013-05-03 19:20:19 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
Chandler Carruth | 21c9060 | 2015-12-30 03:24:14 +0000 | [diff] [blame] | 4585 | Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); } |
| 4586 | void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } |
| 4587 | |
| 4588 | bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); } |
| 4589 | void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); } |
| 4590 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4591 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 4592 | SourceLocation L, |
| 4593 | IdentifierInfo *Id, QualType T, |
| 4594 | Expr *E, const llvm::APSInt &V) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4595 | return new (C, CD) EnumConstantDecl(CD, L, Id, T, E, V); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4598 | EnumConstantDecl * |
| 4599 | EnumConstantDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4600 | return new (C, ID) EnumConstantDecl(nullptr, SourceLocation(), nullptr, |
| 4601 | QualType(), nullptr, llvm::APSInt()); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4604 | void IndirectFieldDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4605 | |
Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4606 | IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC, |
| 4607 | SourceLocation L, DeclarationName N, |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4608 | QualType T, |
| 4609 | MutableArrayRef<NamedDecl *> CH) |
| 4610 | : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()), |
| 4611 | ChainingSize(CH.size()) { |
Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4612 | // In C++, indirect field declarations conflict with tag declarations in the |
| 4613 | // same scope, so add them to IDNS_Tag so that tag redeclaration finds them. |
| 4614 | if (C.getLangOpts().CPlusPlus) |
| 4615 | IdentifierNamespace |= IDNS_Tag; |
| 4616 | } |
| 4617 | |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 4618 | IndirectFieldDecl * |
| 4619 | IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4620 | IdentifierInfo *Id, QualType T, |
| 4621 | llvm::MutableArrayRef<NamedDecl *> CH) { |
| 4622 | return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4625 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 4626 | unsigned ID) { |
Richard Smith | 9f95182 | 2016-01-06 22:49:11 +0000 | [diff] [blame] | 4627 | return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(), |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 4628 | DeclarationName(), QualType(), None); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 4631 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 4632 | SourceLocation End = getLocation(); |
| 4633 | if (Init) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4634 | End = Init->getEndLoc(); |
Douglas Gregor | be99693 | 2010-09-01 20:41:53 +0000 | [diff] [blame] | 4635 | return SourceRange(getLocation(), End); |
| 4636 | } |
| 4637 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4638 | void TypeDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4639 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4640 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | b3185b0 | 2011-03-06 15:48:19 +0000 | [diff] [blame] | 4641 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4642 | IdentifierInfo *Id, TypeSourceInfo *TInfo) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4643 | return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4646 | void TypedefNameDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4647 | |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4648 | TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { |
| 4649 | if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { |
| 4650 | auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); |
| 4651 | auto *ThisTypedef = this; |
| 4652 | if (AnyRedecl && OwningTypedef) { |
| 4653 | OwningTypedef = OwningTypedef->getCanonicalDecl(); |
| 4654 | ThisTypedef = ThisTypedef->getCanonicalDecl(); |
| 4655 | } |
| 4656 | if (OwningTypedef == ThisTypedef) |
Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 4657 | return TT->getDecl(); |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 4658 | } |
Richard Smith | a523022 | 2015-03-27 01:37:43 +0000 | [diff] [blame] | 4659 | |
| 4660 | return nullptr; |
| 4661 | } |
| 4662 | |
Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 4663 | bool TypedefNameDecl::isTransparentTagSlow() const { |
| 4664 | auto determineIsTransparent = [&]() { |
| 4665 | if (auto *TT = getUnderlyingType()->getAs<TagType>()) { |
| 4666 | if (auto *TD = TT->getDecl()) { |
| 4667 | if (TD->getName() != getName()) |
| 4668 | return false; |
| 4669 | SourceLocation TTLoc = getLocation(); |
| 4670 | SourceLocation TDLoc = TD->getLocation(); |
| 4671 | if (!TTLoc.isMacroID() || !TDLoc.isMacroID()) |
| 4672 | return false; |
| 4673 | SourceManager &SM = getASTContext().getSourceManager(); |
| 4674 | return SM.getSpellingLoc(TTLoc) == SM.getSpellingLoc(TDLoc); |
| 4675 | } |
| 4676 | } |
| 4677 | return false; |
| 4678 | }; |
| 4679 | |
| 4680 | bool isTransparent = determineIsTransparent(); |
Benjamin Kramer | dfb730a | 2018-01-26 14:14:11 +0000 | [diff] [blame] | 4681 | MaybeModedTInfo.setInt((isTransparent << 1) | 1); |
Argyrios Kyrtzidis | 3b25c91 | 2017-03-21 16:56:02 +0000 | [diff] [blame] | 4682 | return isTransparent; |
| 4683 | } |
| 4684 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4685 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4686 | return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4687 | nullptr, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4688 | } |
| 4689 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4690 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 4691 | SourceLocation StartLoc, |
| 4692 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 4693 | TypeSourceInfo *TInfo) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4694 | return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4697 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Richard Smith | 053f6c6 | 2014-05-16 23:01:30 +0000 | [diff] [blame] | 4698 | return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), |
| 4699 | SourceLocation(), nullptr, nullptr); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4700 | } |
| 4701 | |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 4702 | SourceRange TypedefDecl::getSourceRange() const { |
| 4703 | SourceLocation RangeEnd = getLocation(); |
| 4704 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 4705 | if (typeIsPostfix(TInfo->getType())) |
| 4706 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 4707 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4708 | return SourceRange(getBeginLoc(), RangeEnd); |
Abramo Bagnara | ea94788 | 2011-03-08 16:41:52 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4711 | SourceRange TypeAliasDecl::getSourceRange() const { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4712 | SourceLocation RangeEnd = getBeginLoc(); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4713 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 4714 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4715 | return SourceRange(getBeginLoc(), RangeEnd); |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4718 | void FileScopeAsmDecl::anchor() {} |
David Blaikie | 68e081d | 2011-12-20 02:48:34 +0000 | [diff] [blame] | 4719 | |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4720 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
Abramo Bagnara | 348823a | 2011-03-03 14:20:18 +0000 | [diff] [blame] | 4721 | StringLiteral *Str, |
| 4722 | SourceLocation AsmLoc, |
| 4723 | SourceLocation RParenLoc) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4724 | return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
Sebastian Redl | 833ef45 | 2010-01-26 22:01:41 +0000 | [diff] [blame] | 4725 | } |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4726 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4727 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4728 | unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4729 | return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), |
| 4730 | SourceLocation()); |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4731 | } |
| 4732 | |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4733 | void EmptyDecl::anchor() {} |
| 4734 | |
| 4735 | EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4736 | return new (C, DC) EmptyDecl(DC, L); |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 4740 | return new (C, ID) EmptyDecl(nullptr, SourceLocation()); |
Michael Han | 8432435 | 2013-02-22 17:15:32 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4743 | //===----------------------------------------------------------------------===// |
| 4744 | // ImportDecl Implementation |
| 4745 | //===----------------------------------------------------------------------===// |
| 4746 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4747 | /// Retrieve the number of module identifiers needed to name the given |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4748 | /// module. |
| 4749 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 4750 | unsigned Result = 1; |
| 4751 | while (Mod->Parent) { |
| 4752 | Mod = Mod->Parent; |
| 4753 | ++Result; |
| 4754 | } |
| 4755 | return Result; |
| 4756 | } |
| 4757 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4758 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4759 | Module *Imported, |
| 4760 | ArrayRef<SourceLocation> IdentifierLocs) |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4761 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, true) { |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4762 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4763 | auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4764 | std::uninitialized_copy(IdentifierLocs.begin(), IdentifierLocs.end(), |
| 4765 | StoredLocs); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4768 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4769 | Module *Imported, SourceLocation EndLoc) |
Eugene Zelenko | de0215c | 2017-11-13 23:01:27 +0000 | [diff] [blame] | 4770 | : Decl(Import, DC, StartLoc), ImportedAndComplete(Imported, false) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4771 | *getTrailingObjects<SourceLocation>() = EndLoc; |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4774 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4775 | SourceLocation StartLoc, Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4776 | ArrayRef<SourceLocation> IdentifierLocs) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4777 | return new (C, DC, |
| 4778 | additionalSizeToAlloc<SourceLocation>(IdentifierLocs.size())) |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4779 | ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4782 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 4783 | SourceLocation StartLoc, |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4784 | Module *Imported, |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4785 | SourceLocation EndLoc) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4786 | ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(1)) |
| 4787 | ImportDecl(DC, StartLoc, Imported, EndLoc); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4788 | Import->setImplicit(); |
| 4789 | return Import; |
| 4790 | } |
| 4791 | |
Douglas Gregor | 72172e9 | 2012-01-05 21:55:30 +0000 | [diff] [blame] | 4792 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, unsigned ID, |
| 4793 | unsigned NumLocations) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4794 | return new (C, ID, additionalSizeToAlloc<SourceLocation>(NumLocations)) |
Richard Smith | f798172 | 2013-11-22 09:01:48 +0000 | [diff] [blame] | 4795 | ImportDecl(EmptyShell()); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 4799 | if (!ImportedAndComplete.getInt()) |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 4800 | return None; |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4801 | |
Alexander Kornienko | fd6ce04 | 2015-11-09 17:53:06 +0000 | [diff] [blame] | 4802 | const auto *StoredLocs = getTrailingObjects<SourceLocation>(); |
Craig Topper | 5fc8fc2 | 2014-08-27 06:28:36 +0000 | [diff] [blame] | 4803 | return llvm::makeArrayRef(StoredLocs, |
| 4804 | getNumModuleIdentifiers(getImportedModule())); |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | SourceRange ImportDecl::getSourceRange() const { |
| 4808 | if (!ImportedAndComplete.getInt()) |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4809 | return SourceRange(getLocation(), *getTrailingObjects<SourceLocation>()); |
| 4810 | |
Douglas Gregor | ba34552 | 2011-12-02 23:23:56 +0000 | [diff] [blame] | 4811 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 4812 | } |
Richard Smith | 8df390f | 2016-09-08 23:14:54 +0000 | [diff] [blame] | 4813 | |
| 4814 | //===----------------------------------------------------------------------===// |
| 4815 | // ExportDecl Implementation |
| 4816 | //===----------------------------------------------------------------------===// |
| 4817 | |
| 4818 | void ExportDecl::anchor() {} |
| 4819 | |
| 4820 | ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC, |
| 4821 | SourceLocation ExportLoc) { |
| 4822 | return new (C, DC) ExportDecl(DC, ExportLoc); |
| 4823 | } |
| 4824 | |
| 4825 | ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, unsigned ID) { |
| 4826 | return new (C, ID) ExportDecl(nullptr, SourceLocation()); |
| 4827 | } |