| 1 | //===- Decl.cpp - Declaration AST Node Implementation ---------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the Decl subclasses. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "clang/AST/Decl.h" |
| 14 | #include "Linkage.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/ASTDiagnostic.h" |
| 17 | #include "clang/AST/ASTLambda.h" |
| 18 | #include "clang/AST/ASTMutationListener.h" |
| 19 | #include "clang/AST/Attr.h" |
| 20 | #include "clang/AST/CanonicalType.h" |
| 21 | #include "clang/AST/DeclBase.h" |
| 22 | #include "clang/AST/DeclCXX.h" |
| 23 | #include "clang/AST/DeclObjC.h" |
| 24 | #include "clang/AST/DeclTemplate.h" |
| 25 | #include "clang/AST/DeclarationName.h" |
| 26 | #include "clang/AST/Expr.h" |
| 27 | #include "clang/AST/ExprCXX.h" |
| 28 | #include "clang/AST/ExternalASTSource.h" |
| 29 | #include "clang/AST/ODRHash.h" |
| 30 | #include "clang/AST/PrettyDeclStackTrace.h" |
| 31 | #include "clang/AST/PrettyPrinter.h" |
| 32 | #include "clang/AST/Randstruct.h" |
| 33 | #include "clang/AST/RecordLayout.h" |
| 34 | #include "clang/AST/Redeclarable.h" |
| 35 | #include "clang/AST/Stmt.h" |
| 36 | #include "clang/AST/TemplateBase.h" |
| 37 | #include "clang/AST/Type.h" |
| 38 | #include "clang/AST/TypeLoc.h" |
| 39 | #include "clang/Basic/Builtins.h" |
| 40 | #include "clang/Basic/IdentifierTable.h" |
| 41 | #include "clang/Basic/LLVM.h" |
| 42 | #include "clang/Basic/LangOptions.h" |
| 43 | #include "clang/Basic/Linkage.h" |
| 44 | #include "clang/Basic/Module.h" |
| 45 | #include "clang/Basic/NoSanitizeList.h" |
| 46 | #include "clang/Basic/PartialDiagnostic.h" |
| 47 | #include "clang/Basic/Sanitizers.h" |
| 48 | #include "clang/Basic/SourceLocation.h" |
| 49 | #include "clang/Basic/SourceManager.h" |
| 50 | #include "clang/Basic/Specifiers.h" |
| 51 | #include "clang/Basic/TargetCXXABI.h" |
| 52 | #include "clang/Basic/TargetInfo.h" |
| 53 | #include "clang/Basic/Visibility.h" |
| 54 | #include "llvm/ADT/APSInt.h" |
| 55 | #include "llvm/ADT/ArrayRef.h" |
| 56 | #include "llvm/ADT/STLExtras.h" |
| 57 | #include "llvm/ADT/SmallVector.h" |
| 58 | #include "llvm/ADT/StringRef.h" |
| 59 | #include "llvm/ADT/StringSwitch.h" |
| 60 | #include "llvm/ADT/iterator_range.h" |
| 61 | #include "llvm/Support/Casting.h" |
| 62 | #include "llvm/Support/ErrorHandling.h" |
| 63 | #include "llvm/Support/raw_ostream.h" |
| 64 | #include "llvm/TargetParser/Triple.h" |
| 65 | #include <algorithm> |
| 66 | #include <cassert> |
| 67 | #include <cstddef> |
| 68 | #include <cstring> |
| 69 | #include <optional> |
| 70 | #include <string> |
| 71 | #include <tuple> |
| 72 | #include <type_traits> |
| 73 | |
| 74 | using namespace clang; |
| 75 | |
| 76 | Decl *clang::getPrimaryMergedDecl(Decl *D) { |
| 77 | return D->getASTContext().getPrimaryMergedDecl(D); |
| 78 | } |
| 79 | |
| 80 | void PrettyDeclStackTraceEntry::print(raw_ostream &OS) const { |
| 81 | SourceLocation Loc = this->Loc; |
| 82 | if (!Loc.isValid() && TheDecl) Loc = TheDecl->getLocation(); |
| 83 | if (Loc.isValid()) { |
| 84 | Loc.print(OS, SM: Context.getSourceManager()); |
| 85 | OS << ": " ; |
| 86 | } |
| 87 | OS << Message; |
| 88 | |
| 89 | if (auto *ND = dyn_cast_if_present<NamedDecl>(Val: TheDecl)) { |
| 90 | OS << " '" ; |
| 91 | ND->getNameForDiagnostic(OS, Policy: Context.getPrintingPolicy(), Qualified: true); |
| 92 | OS << "'" ; |
| 93 | } |
| 94 | |
| 95 | OS << '\n'; |
| 96 | } |
| 97 | |
| 98 | // Defined here so that it can be inlined into its direct callers. |
| 99 | bool Decl::isOutOfLine() const { |
| 100 | return !getLexicalDeclContext()->Equals(DC: getDeclContext()); |
| 101 | } |
| 102 | |
| 103 | TranslationUnitDecl::TranslationUnitDecl(ASTContext &ctx) |
| 104 | : Decl(TranslationUnit, nullptr, SourceLocation()), |
| 105 | DeclContext(TranslationUnit), redeclarable_base(ctx), Ctx(ctx) {} |
| 106 | |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | // NamedDecl Implementation |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | |
| 111 | // Visibility rules aren't rigorously externally specified, but here |
| 112 | // are the basic principles behind what we implement: |
| 113 | // |
| 114 | // 1. An explicit visibility attribute is generally a direct expression |
| 115 | // of the user's intent and should be honored. Only the innermost |
| 116 | // visibility attribute applies. If no visibility attribute applies, |
| 117 | // global visibility settings are considered. |
| 118 | // |
| 119 | // 2. There is one caveat to the above: on or in a template pattern, |
| 120 | // an explicit visibility attribute is just a default rule, and |
| 121 | // visibility can be decreased by the visibility of template |
| 122 | // arguments. But this, too, has an exception: an attribute on an |
| 123 | // explicit specialization or instantiation causes all the visibility |
| 124 | // restrictions of the template arguments to be ignored. |
| 125 | // |
| 126 | // 3. A variable that does not otherwise have explicit visibility can |
| 127 | // be restricted by the visibility of its type. |
| 128 | // |
| 129 | // 4. A visibility restriction is explicit if it comes from an |
| 130 | // attribute (or something like it), not a global visibility setting. |
| 131 | // When emitting a reference to an external symbol, visibility |
| 132 | // restrictions are ignored unless they are explicit. |
| 133 | // |
| 134 | // 5. When computing the visibility of a non-type, including a |
| 135 | // non-type member of a class, only non-type visibility restrictions |
| 136 | // are considered: the 'visibility' attribute, global value-visibility |
| 137 | // settings, and a few special cases like __private_extern. |
| 138 | // |
| 139 | // 6. When computing the visibility of a type, including a type member |
| 140 | // of a class, only type visibility restrictions are considered: |
| 141 | // the 'type_visibility' attribute and global type-visibility settings. |
| 142 | // However, a 'visibility' attribute counts as a 'type_visibility' |
| 143 | // attribute on any declaration that only has the former. |
| 144 | // |
| 145 | // The visibility of a "secondary" entity, like a template argument, |
| 146 | // is computed using the kind of that entity, not the kind of the |
| 147 | // primary entity for which we are computing visibility. For example, |
| 148 | // the visibility of a specialization of either of these templates: |
| 149 | // template <class T, bool (&compare)(T, X)> bool has_match(list<T>, X); |
| 150 | // template <class T, bool (&compare)(T, X)> class matcher; |
| 151 | // is restricted according to the type visibility of the argument 'T', |
| 152 | // the type visibility of 'bool(&)(T,X)', and the value visibility of |
| 153 | // the argument function 'compare'. That 'has_match' is a value |
| 154 | // and 'matcher' is a type only matters when looking for attributes |
| 155 | // and settings from the immediate context. |
| 156 | |
| 157 | /// Does this computation kind permit us to consider additional |
| 158 | /// visibility settings from attributes and the like? |
| 159 | static bool hasExplicitVisibilityAlready(LVComputationKind computation) { |
| 160 | return computation.IgnoreExplicitVisibility; |
| 161 | } |
| 162 | |
| 163 | /// Given an LVComputationKind, return one of the same type/value sort |
| 164 | /// that records that it already has explicit visibility. |
| 165 | static LVComputationKind |
| 166 | withExplicitVisibilityAlready(LVComputationKind Kind) { |
| 167 | Kind.IgnoreExplicitVisibility = true; |
| 168 | return Kind; |
| 169 | } |
| 170 | |
| 171 | static std::optional<Visibility> getExplicitVisibility(const NamedDecl *D, |
| 172 | LVComputationKind kind) { |
| 173 | assert(!kind.IgnoreExplicitVisibility && |
| 174 | "asking for explicit visibility when we shouldn't be" ); |
| 175 | return D->getExplicitVisibility(kind: kind.getExplicitVisibilityKind()); |
| 176 | } |
| 177 | |
| 178 | /// Is the given declaration a "type" or a "value" for the purposes of |
| 179 | /// visibility computation? |
| 180 | static bool usesTypeVisibility(const NamedDecl *D) { |
| 181 | return isa<TypeDecl>(Val: D) || |
| 182 | isa<ClassTemplateDecl>(Val: D) || |
| 183 | isa<ObjCInterfaceDecl>(Val: D); |
| 184 | } |
| 185 | |
| 186 | /// Does the given declaration have member specialization information, |
| 187 | /// and if so, is it an explicit specialization? |
| 188 | template <class T> |
| 189 | static std::enable_if_t<!std::is_base_of_v<RedeclarableTemplateDecl, T>, bool> |
| 190 | isExplicitMemberSpecialization(const T *D) { |
| 191 | if (const MemberSpecializationInfo *member = |
| 192 | D->getMemberSpecializationInfo()) { |
| 193 | return member->isExplicitSpecialization(); |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | /// For templates, this question is easier: a member template can't be |
| 199 | /// explicitly instantiated, so there's a single bit indicating whether |
| 200 | /// or not this is an explicit member specialization. |
| 201 | static bool isExplicitMemberSpecialization(const RedeclarableTemplateDecl *D) { |
| 202 | return D->isMemberSpecialization(); |
| 203 | } |
| 204 | |
| 205 | /// Given a visibility attribute, return the explicit visibility |
| 206 | /// associated with it. |
| 207 | template <class T> |
| 208 | static Visibility getVisibilityFromAttr(const T *attr) { |
| 209 | switch (attr->getVisibility()) { |
| 210 | case T::Default: |
| 211 | return DefaultVisibility; |
| 212 | case T::Hidden: |
| 213 | return HiddenVisibility; |
| 214 | case T::Protected: |
| 215 | return ProtectedVisibility; |
| 216 | } |
| 217 | llvm_unreachable("bad visibility kind" ); |
| 218 | } |
| 219 | |
| 220 | /// Return the explicit visibility of the given declaration. |
| 221 | static std::optional<Visibility> |
| 222 | getVisibilityOf(const NamedDecl *D, NamedDecl::ExplicitVisibilityKind kind) { |
| 223 | // If we're ultimately computing the visibility of a type, look for |
| 224 | // a 'type_visibility' attribute before looking for 'visibility'. |
| 225 | if (kind == NamedDecl::VisibilityForType) { |
| 226 | if (const auto *A = D->getAttr<TypeVisibilityAttr>()) { |
| 227 | return getVisibilityFromAttr(attr: A); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // If this declaration has an explicit visibility attribute, use it. |
| 232 | if (const auto *A = D->getAttr<VisibilityAttr>()) { |
| 233 | return getVisibilityFromAttr(attr: A); |
| 234 | } |
| 235 | |
| 236 | return std::nullopt; |
| 237 | } |
| 238 | |
| 239 | LinkageInfo LinkageComputer::getLVForType(const Type &T, |
| 240 | LVComputationKind computation) { |
| 241 | if (computation.IgnoreAllVisibility) |
| 242 | return LinkageInfo(T.getLinkage(), DefaultVisibility, true); |
| 243 | return getTypeLinkageAndVisibility(T: &T); |
| 244 | } |
| 245 | |
| 246 | /// Get the most restrictive linkage for the types in the given |
| 247 | /// template parameter list. For visibility purposes, template |
| 248 | /// parameters are part of the signature of a template. |
| 249 | LinkageInfo LinkageComputer::getLVForTemplateParameterList( |
| 250 | const TemplateParameterList *Params, LVComputationKind computation) { |
| 251 | LinkageInfo LV; |
| 252 | for (const NamedDecl *P : *Params) { |
| 253 | // Template type parameters are the most common and never |
| 254 | // contribute to visibility, pack or not. |
| 255 | if (isa<TemplateTypeParmDecl>(Val: P)) |
| 256 | continue; |
| 257 | |
| 258 | // Non-type template parameters can be restricted by the value type, e.g. |
| 259 | // template <enum X> class A { ... }; |
| 260 | // We have to be careful here, though, because we can be dealing with |
| 261 | // dependent types. |
| 262 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: P)) { |
| 263 | // Handle the non-pack case first. |
| 264 | if (!NTTP->isExpandedParameterPack()) { |
| 265 | if (!NTTP->getType()->isDependentType()) { |
| 266 | LV.merge(other: getLVForType(T: *NTTP->getType(), computation)); |
| 267 | } |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | // Look at all the types in an expanded pack. |
| 272 | for (unsigned i = 0, n = NTTP->getNumExpansionTypes(); i != n; ++i) { |
| 273 | QualType type = NTTP->getExpansionType(I: i); |
| 274 | if (!type->isDependentType()) |
| 275 | LV.merge(other: getTypeLinkageAndVisibility(T: type)); |
| 276 | } |
| 277 | continue; |
| 278 | } |
| 279 | |
| 280 | // Template template parameters can be restricted by their |
| 281 | // template parameters, recursively. |
| 282 | const auto *TTP = cast<TemplateTemplateParmDecl>(Val: P); |
| 283 | |
| 284 | // Handle the non-pack case first. |
| 285 | if (!TTP->isExpandedParameterPack()) { |
| 286 | LV.merge(other: getLVForTemplateParameterList(Params: TTP->getTemplateParameters(), |
| 287 | computation)); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | // Look at all expansions in an expanded pack. |
| 292 | for (unsigned i = 0, n = TTP->getNumExpansionTemplateParameters(); |
| 293 | i != n; ++i) { |
| 294 | LV.merge(other: getLVForTemplateParameterList( |
| 295 | Params: TTP->getExpansionTemplateParameters(I: i), computation)); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return LV; |
| 300 | } |
| 301 | |
| 302 | static const Decl *getOutermostFuncOrBlockContext(const Decl *D) { |
| 303 | const Decl *Ret = nullptr; |
| 304 | const DeclContext *DC = D->getDeclContext(); |
| 305 | while (DC->getDeclKind() != Decl::TranslationUnit) { |
| 306 | if (isa<FunctionDecl>(Val: DC) || isa<BlockDecl>(Val: DC)) |
| 307 | Ret = cast<Decl>(Val: DC); |
| 308 | DC = DC->getParent(); |
| 309 | } |
| 310 | return Ret; |
| 311 | } |
| 312 | |
| 313 | /// Get the most restrictive linkage for the types and |
| 314 | /// declarations in the given template argument list. |
| 315 | /// |
| 316 | /// Note that we don't take an LVComputationKind because we always |
| 317 | /// want to honor the visibility of template arguments in the same way. |
| 318 | LinkageInfo |
| 319 | LinkageComputer::getLVForTemplateArgumentList(ArrayRef<TemplateArgument> Args, |
| 320 | LVComputationKind computation) { |
| 321 | LinkageInfo LV; |
| 322 | |
| 323 | for (const TemplateArgument &Arg : Args) { |
| 324 | switch (Arg.getKind()) { |
| 325 | case TemplateArgument::Null: |
| 326 | case TemplateArgument::Integral: |
| 327 | case TemplateArgument::Expression: |
| 328 | continue; |
| 329 | |
| 330 | case TemplateArgument::Type: |
| 331 | LV.merge(other: getLVForType(T: *Arg.getAsType(), computation)); |
| 332 | continue; |
| 333 | |
| 334 | case TemplateArgument::Declaration: { |
| 335 | const NamedDecl *ND = Arg.getAsDecl(); |
| 336 | assert(!usesTypeVisibility(ND)); |
| 337 | LV.merge(other: getLVForDecl(D: ND, computation)); |
| 338 | continue; |
| 339 | } |
| 340 | |
| 341 | case TemplateArgument::NullPtr: |
| 342 | LV.merge(other: getTypeLinkageAndVisibility(T: Arg.getNullPtrType())); |
| 343 | continue; |
| 344 | |
| 345 | case TemplateArgument::StructuralValue: |
| 346 | LV.merge(other: getLVForValue(V: Arg.getAsStructuralValue(), computation)); |
| 347 | continue; |
| 348 | |
| 349 | case TemplateArgument::Template: |
| 350 | case TemplateArgument::TemplateExpansion: |
| 351 | if (TemplateDecl *Template = |
| 352 | Arg.getAsTemplateOrTemplatePattern().getAsTemplateDecl( |
| 353 | /*IgnoreDeduced=*/true)) |
| 354 | LV.merge(other: getLVForDecl(D: Template, computation)); |
| 355 | continue; |
| 356 | |
| 357 | case TemplateArgument::Pack: |
| 358 | LV.merge(other: getLVForTemplateArgumentList(Args: Arg.getPackAsArray(), computation)); |
| 359 | continue; |
| 360 | } |
| 361 | llvm_unreachable("bad template argument kind" ); |
| 362 | } |
| 363 | |
| 364 | return LV; |
| 365 | } |
| 366 | |
| 367 | LinkageInfo |
| 368 | LinkageComputer::getLVForTemplateArgumentList(const TemplateArgumentList &TArgs, |
| 369 | LVComputationKind computation) { |
| 370 | return getLVForTemplateArgumentList(Args: TArgs.asArray(), computation); |
| 371 | } |
| 372 | |
| 373 | static bool shouldConsiderTemplateVisibility(const FunctionDecl *fn, |
| 374 | const FunctionTemplateSpecializationInfo *specInfo) { |
| 375 | // Include visibility from the template parameters and arguments |
| 376 | // only if this is not an explicit instantiation or specialization |
| 377 | // with direct explicit visibility. (Implicit instantiations won't |
| 378 | // have a direct attribute.) |
| 379 | if (!specInfo->isExplicitInstantiationOrSpecialization()) |
| 380 | return true; |
| 381 | |
| 382 | return !fn->hasAttr<VisibilityAttr>(); |
| 383 | } |
| 384 | |
| 385 | /// Merge in template-related linkage and visibility for the given |
| 386 | /// function template specialization. |
| 387 | /// |
| 388 | /// We don't need a computation kind here because we can assume |
| 389 | /// LVForValue. |
| 390 | /// |
| 391 | /// \param[out] LV the computation to use for the parent |
| 392 | void LinkageComputer::mergeTemplateLV( |
| 393 | LinkageInfo &LV, const FunctionDecl *fn, |
| 394 | const FunctionTemplateSpecializationInfo *specInfo, |
| 395 | LVComputationKind computation) { |
| 396 | bool considerVisibility = |
| 397 | shouldConsiderTemplateVisibility(fn, specInfo); |
| 398 | |
| 399 | FunctionTemplateDecl *temp = specInfo->getTemplate(); |
| 400 | // Merge information from the template declaration. |
| 401 | LinkageInfo tempLV = getLVForDecl(D: temp, computation); |
| 402 | // The linkage and visibility of the specialization should be |
| 403 | // consistent with the template declaration. |
| 404 | LV.mergeMaybeWithVisibility(other: tempLV, withVis: considerVisibility); |
| 405 | |
| 406 | // Merge information from the template parameters. |
| 407 | LinkageInfo paramsLV = |
| 408 | getLVForTemplateParameterList(Params: temp->getTemplateParameters(), computation); |
| 409 | LV.mergeMaybeWithVisibility(other: paramsLV, withVis: considerVisibility); |
| 410 | |
| 411 | // Merge information from the template arguments. |
| 412 | const TemplateArgumentList &templateArgs = *specInfo->TemplateArguments; |
| 413 | LinkageInfo argsLV = getLVForTemplateArgumentList(TArgs: templateArgs, computation); |
| 414 | LV.mergeMaybeWithVisibility(other: argsLV, withVis: considerVisibility); |
| 415 | } |
| 416 | |
| 417 | /// Does the given declaration have a direct visibility attribute |
| 418 | /// that would match the given rules? |
| 419 | static bool hasDirectVisibilityAttribute(const NamedDecl *D, |
| 420 | LVComputationKind computation) { |
| 421 | if (computation.IgnoreAllVisibility) |
| 422 | return false; |
| 423 | |
| 424 | return (computation.isTypeVisibility() && D->hasAttr<TypeVisibilityAttr>()) || |
| 425 | D->hasAttr<VisibilityAttr>(); |
| 426 | } |
| 427 | |
| 428 | /// Should we consider visibility associated with the template |
| 429 | /// arguments and parameters of the given class template specialization? |
| 430 | static bool shouldConsiderTemplateVisibility( |
| 431 | const ClassTemplateSpecializationDecl *spec, |
| 432 | LVComputationKind computation) { |
| 433 | // Include visibility from the template parameters and arguments |
| 434 | // only if this is not an explicit instantiation or specialization |
| 435 | // with direct explicit visibility (and note that implicit |
| 436 | // instantiations won't have a direct attribute). |
| 437 | // |
| 438 | // Furthermore, we want to ignore template parameters and arguments |
| 439 | // for an explicit specialization when computing the visibility of a |
| 440 | // member thereof with explicit visibility. |
| 441 | // |
| 442 | // This is a bit complex; let's unpack it. |
| 443 | // |
| 444 | // An explicit class specialization is an independent, top-level |
| 445 | // declaration. As such, if it or any of its members has an |
| 446 | // explicit visibility attribute, that must directly express the |
| 447 | // user's intent, and we should honor it. The same logic applies to |
| 448 | // an explicit instantiation of a member of such a thing. |
| 449 | |
| 450 | // Fast path: if this is not an explicit instantiation or |
| 451 | // specialization, we always want to consider template-related |
| 452 | // visibility restrictions. |
| 453 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 454 | return true; |
| 455 | |
| 456 | // This is the 'member thereof' check. |
| 457 | if (spec->isExplicitSpecialization() && |
| 458 | hasExplicitVisibilityAlready(computation)) |
| 459 | return false; |
| 460 | |
| 461 | return !hasDirectVisibilityAttribute(D: spec, computation); |
| 462 | } |
| 463 | |
| 464 | /// Merge in template-related linkage and visibility for the given |
| 465 | /// class template specialization. |
| 466 | void LinkageComputer::mergeTemplateLV( |
| 467 | LinkageInfo &LV, const ClassTemplateSpecializationDecl *spec, |
| 468 | LVComputationKind computation) { |
| 469 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
| 470 | |
| 471 | // Merge information from the template parameters, but ignore |
| 472 | // visibility if we're only considering template arguments. |
| 473 | ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 474 | // Merge information from the template declaration. |
| 475 | LinkageInfo tempLV = getLVForDecl(D: temp, computation); |
| 476 | // The linkage of the specialization should be consistent with the |
| 477 | // template declaration. |
| 478 | LV.setLinkage(tempLV.getLinkage()); |
| 479 | |
| 480 | LinkageInfo paramsLV = |
| 481 | getLVForTemplateParameterList(Params: temp->getTemplateParameters(), computation); |
| 482 | LV.mergeMaybeWithVisibility(other: paramsLV, |
| 483 | withVis: considerVisibility && !hasExplicitVisibilityAlready(computation)); |
| 484 | |
| 485 | // Merge information from the template arguments. We ignore |
| 486 | // template-argument visibility if we've got an explicit |
| 487 | // instantiation with a visibility attribute. |
| 488 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| 489 | LinkageInfo argsLV = getLVForTemplateArgumentList(TArgs: templateArgs, computation); |
| 490 | if (considerVisibility) |
| 491 | LV.mergeVisibility(other: argsLV); |
| 492 | LV.mergeExternalVisibility(Other: argsLV); |
| 493 | } |
| 494 | |
| 495 | /// Should we consider visibility associated with the template |
| 496 | /// arguments and parameters of the given variable template |
| 497 | /// specialization? As usual, follow class template specialization |
| 498 | /// logic up to initialization. |
| 499 | static bool shouldConsiderTemplateVisibility( |
| 500 | const VarTemplateSpecializationDecl *spec, |
| 501 | LVComputationKind computation) { |
| 502 | // Include visibility from the template parameters and arguments |
| 503 | // only if this is not an explicit instantiation or specialization |
| 504 | // with direct explicit visibility (and note that implicit |
| 505 | // instantiations won't have a direct attribute). |
| 506 | if (!spec->isExplicitInstantiationOrSpecialization()) |
| 507 | return true; |
| 508 | |
| 509 | // An explicit variable specialization is an independent, top-level |
| 510 | // declaration. As such, if it has an explicit visibility attribute, |
| 511 | // that must directly express the user's intent, and we should honor |
| 512 | // it. |
| 513 | if (spec->isExplicitSpecialization() && |
| 514 | hasExplicitVisibilityAlready(computation)) |
| 515 | return false; |
| 516 | |
| 517 | return !hasDirectVisibilityAttribute(D: spec, computation); |
| 518 | } |
| 519 | |
| 520 | /// Merge in template-related linkage and visibility for the given |
| 521 | /// variable template specialization. As usual, follow class template |
| 522 | /// specialization logic up to initialization. |
| 523 | void LinkageComputer::mergeTemplateLV(LinkageInfo &LV, |
| 524 | const VarTemplateSpecializationDecl *spec, |
| 525 | LVComputationKind computation) { |
| 526 | bool considerVisibility = shouldConsiderTemplateVisibility(spec, computation); |
| 527 | |
| 528 | // Merge information from the template parameters, but ignore |
| 529 | // visibility if we're only considering template arguments. |
| 530 | VarTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 531 | LinkageInfo tempLV = |
| 532 | getLVForTemplateParameterList(Params: temp->getTemplateParameters(), computation); |
| 533 | LV.mergeMaybeWithVisibility(other: tempLV, |
| 534 | withVis: considerVisibility && !hasExplicitVisibilityAlready(computation)); |
| 535 | |
| 536 | // Merge information from the template arguments. We ignore |
| 537 | // template-argument visibility if we've got an explicit |
| 538 | // instantiation with a visibility attribute. |
| 539 | const TemplateArgumentList &templateArgs = spec->getTemplateArgs(); |
| 540 | LinkageInfo argsLV = getLVForTemplateArgumentList(TArgs: templateArgs, computation); |
| 541 | if (considerVisibility) |
| 542 | LV.mergeVisibility(other: argsLV); |
| 543 | LV.mergeExternalVisibility(Other: argsLV); |
| 544 | } |
| 545 | |
| 546 | static bool useInlineVisibilityHidden(const NamedDecl *D) { |
| 547 | // FIXME: we should warn if -fvisibility-inlines-hidden is used with c. |
| 548 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 549 | if (!Opts.CPlusPlus || !Opts.InlineVisibilityHidden) |
| 550 | return false; |
| 551 | |
| 552 | const auto *FD = dyn_cast<FunctionDecl>(Val: D); |
| 553 | if (!FD) |
| 554 | return false; |
| 555 | |
| 556 | TemplateSpecializationKind TSK = TSK_Undeclared; |
| 557 | if (FunctionTemplateSpecializationInfo *spec |
| 558 | = FD->getTemplateSpecializationInfo()) { |
| 559 | TSK = spec->getTemplateSpecializationKind(); |
| 560 | } else if (MemberSpecializationInfo *MSI = |
| 561 | FD->getMemberSpecializationInfo()) { |
| 562 | TSK = MSI->getTemplateSpecializationKind(); |
| 563 | } |
| 564 | |
| 565 | const FunctionDecl *Def = nullptr; |
| 566 | // InlineVisibilityHidden only applies to definitions, and |
| 567 | // isInlined() only gives meaningful answers on definitions |
| 568 | // anyway. |
| 569 | return TSK != TSK_ExplicitInstantiationDeclaration && |
| 570 | TSK != TSK_ExplicitInstantiationDefinition && |
| 571 | FD->hasBody(Definition&: Def) && Def->isInlined() && !Def->hasAttr<GNUInlineAttr>(); |
| 572 | } |
| 573 | |
| 574 | template <typename T> static bool isFirstInExternCContext(T *D) { |
| 575 | const T *First = D->getFirstDecl(); |
| 576 | return First->isInExternCContext(); |
| 577 | } |
| 578 | |
| 579 | static bool isSingleLineLanguageLinkage(const Decl &D) { |
| 580 | if (const auto *SD = dyn_cast<LinkageSpecDecl>(Val: D.getDeclContext())) |
| 581 | if (!SD->hasBraces()) |
| 582 | return true; |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | static LinkageInfo getExternalLinkageFor(const NamedDecl *D) { |
| 587 | return LinkageInfo::external(); |
| 588 | } |
| 589 | |
| 590 | static StorageClass getStorageClass(const Decl *D) { |
| 591 | if (auto *TD = dyn_cast<TemplateDecl>(Val: D)) |
| 592 | D = TD->getTemplatedDecl(); |
| 593 | if (D) { |
| 594 | if (auto *VD = dyn_cast<VarDecl>(Val: D)) |
| 595 | return VD->getStorageClass(); |
| 596 | if (auto *FD = dyn_cast<FunctionDecl>(Val: D)) |
| 597 | return FD->getStorageClass(); |
| 598 | } |
| 599 | return SC_None; |
| 600 | } |
| 601 | |
| 602 | LinkageInfo |
| 603 | LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, |
| 604 | LVComputationKind computation, |
| 605 | bool IgnoreVarTypeLinkage) { |
| 606 | assert(D->getDeclContext()->getRedeclContext()->isFileContext() && |
| 607 | "Not a name having namespace scope" ); |
| 608 | ASTContext &Context = D->getASTContext(); |
| 609 | const auto *Var = dyn_cast<VarDecl>(Val: D); |
| 610 | |
| 611 | // C++ [basic.link]p3: |
| 612 | // A name having namespace scope (3.3.6) has internal linkage if it |
| 613 | // is the name of |
| 614 | |
| 615 | if ((getStorageClass(D: D->getCanonicalDecl()) == SC_Static) || |
| 616 | (Context.getLangOpts().C23 && Var && Var->isConstexpr())) { |
| 617 | // - a variable, variable template, function, or function template |
| 618 | // that is explicitly declared static; or |
| 619 | // (This bullet corresponds to C99 6.2.2p3.) |
| 620 | |
| 621 | // C23 6.2.2p3 |
| 622 | // If the declaration of a file scope identifier for |
| 623 | // an object contains any of the storage-class specifiers static or |
| 624 | // constexpr then the identifier has internal linkage. |
| 625 | return LinkageInfo::internal(); |
| 626 | } |
| 627 | |
| 628 | if (Var) { |
| 629 | // - a non-template variable of non-volatile const-qualified type, unless |
| 630 | // - it is explicitly declared extern, or |
| 631 | // - it is declared in the purview of a module interface unit |
| 632 | // (outside the private-module-fragment, if any) or module partition, or |
| 633 | // - it is inline, or |
| 634 | // - it was previously declared and the prior declaration did not have |
| 635 | // internal linkage |
| 636 | // (There is no equivalent in C99.) |
| 637 | if (Context.getLangOpts().CPlusPlus && Var->getType().isConstQualified() && |
| 638 | !Var->getType().isVolatileQualified() && !Var->isInline() && |
| 639 | ![Var]() { |
| 640 | // Check if it is module purview except private module fragment |
| 641 | // and implementation unit. |
| 642 | if (auto *M = Var->getOwningModule()) |
| 643 | return M->isInterfaceOrPartition() || M->isImplicitGlobalModule(); |
| 644 | return false; |
| 645 | }() && |
| 646 | !isa<VarTemplateSpecializationDecl>(Val: Var) && |
| 647 | !Var->getDescribedVarTemplate()) { |
| 648 | const VarDecl *PrevVar = Var->getPreviousDecl(); |
| 649 | if (PrevVar) |
| 650 | return getLVForDecl(D: PrevVar, computation); |
| 651 | |
| 652 | if (Var->getStorageClass() != SC_Extern && |
| 653 | Var->getStorageClass() != SC_PrivateExtern && |
| 654 | !isSingleLineLanguageLinkage(D: *Var)) |
| 655 | return LinkageInfo::internal(); |
| 656 | } |
| 657 | |
| 658 | for (const VarDecl *PrevVar = Var->getPreviousDecl(); PrevVar; |
| 659 | PrevVar = PrevVar->getPreviousDecl()) { |
| 660 | if (PrevVar->getStorageClass() == SC_PrivateExtern && |
| 661 | Var->getStorageClass() == SC_None) |
| 662 | return getDeclLinkageAndVisibility(D: PrevVar); |
| 663 | // Explicitly declared static. |
| 664 | if (PrevVar->getStorageClass() == SC_Static) |
| 665 | return LinkageInfo::internal(); |
| 666 | } |
| 667 | } else if (const auto *IFD = dyn_cast<IndirectFieldDecl>(Val: D)) { |
| 668 | // - a data member of an anonymous union. |
| 669 | const VarDecl *VD = IFD->getVarDecl(); |
| 670 | assert(VD && "Expected a VarDecl in this IndirectFieldDecl!" ); |
| 671 | return getLVForNamespaceScopeDecl(D: VD, computation, IgnoreVarTypeLinkage); |
| 672 | } |
| 673 | assert(!isa<FieldDecl>(D) && "Didn't expect a FieldDecl!" ); |
| 674 | |
| 675 | // FIXME: This gives internal linkage to names that should have no linkage |
| 676 | // (those not covered by [basic.link]p6). |
| 677 | if (D->isInAnonymousNamespace()) { |
| 678 | const auto *Var = dyn_cast<VarDecl>(Val: D); |
| 679 | const auto *Func = dyn_cast<FunctionDecl>(Val: D); |
| 680 | // FIXME: The check for extern "C" here is not justified by the standard |
| 681 | // wording, but we retain it from the pre-DR1113 model to avoid breaking |
| 682 | // code. |
| 683 | // |
| 684 | // C++11 [basic.link]p4: |
| 685 | // An unnamed namespace or a namespace declared directly or indirectly |
| 686 | // within an unnamed namespace has internal linkage. |
| 687 | if ((!Var || !isFirstInExternCContext(D: Var)) && |
| 688 | (!Func || !isFirstInExternCContext(D: Func))) |
| 689 | return LinkageInfo::internal(); |
| 690 | } |
| 691 | |
| 692 | // Set up the defaults. |
| 693 | |
| 694 | // C99 6.2.2p5: |
| 695 | // If the declaration of an identifier for an object has file |
| 696 | // scope and no storage-class specifier, its linkage is |
| 697 | // external. |
| 698 | LinkageInfo LV = getExternalLinkageFor(D); |
| 699 | |
| 700 | if (!hasExplicitVisibilityAlready(computation)) { |
| 701 | if (std::optional<Visibility> Vis = getExplicitVisibility(D, kind: computation)) { |
| 702 | LV.mergeVisibility(newVis: *Vis, newExplicit: true); |
| 703 | } else { |
| 704 | // If we're declared in a namespace with a visibility attribute, |
| 705 | // use that namespace's visibility, and it still counts as explicit. |
| 706 | for (const DeclContext *DC = D->getDeclContext(); |
| 707 | !isa<TranslationUnitDecl>(Val: DC); |
| 708 | DC = DC->getParent()) { |
| 709 | const auto *ND = dyn_cast<NamespaceDecl>(Val: DC); |
| 710 | if (!ND) continue; |
| 711 | if (std::optional<Visibility> Vis = |
| 712 | getExplicitVisibility(D: ND, kind: computation)) { |
| 713 | LV.mergeVisibility(newVis: *Vis, newExplicit: true); |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | // Add in global settings if the above didn't give us direct visibility. |
| 720 | if (!LV.isVisibilityExplicit()) { |
| 721 | // Use global type/value visibility as appropriate. |
| 722 | Visibility globalVisibility = |
| 723 | computation.isValueVisibility() |
| 724 | ? Context.getLangOpts().getValueVisibilityMode() |
| 725 | : Context.getLangOpts().getTypeVisibilityMode(); |
| 726 | LV.mergeVisibility(newVis: globalVisibility, /*explicit*/ newExplicit: false); |
| 727 | |
| 728 | // If we're paying attention to global visibility, apply |
| 729 | // -finline-visibility-hidden if this is an inline method. |
| 730 | if (useInlineVisibilityHidden(D)) |
| 731 | LV.mergeVisibility(newVis: HiddenVisibility, /*visibilityExplicit=*/newExplicit: false); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | // C++ [basic.link]p4: |
| 736 | |
| 737 | // A name having namespace scope that has not been given internal linkage |
| 738 | // above and that is the name of |
| 739 | // [...bullets...] |
| 740 | // has its linkage determined as follows: |
| 741 | // - if the enclosing namespace has internal linkage, the name has |
| 742 | // internal linkage; [handled above] |
| 743 | // - otherwise, if the declaration of the name is attached to a named |
| 744 | // module and is not exported, the name has module linkage; |
| 745 | // - otherwise, the name has external linkage. |
| 746 | // LV is currently set up to handle the last two bullets. |
| 747 | // |
| 748 | // The bullets are: |
| 749 | |
| 750 | // - a variable; or |
| 751 | if (const auto *Var = dyn_cast<VarDecl>(Val: D)) { |
| 752 | // GCC applies the following optimization to variables and static |
| 753 | // data members, but not to functions: |
| 754 | // |
| 755 | // Modify the variable's LV by the LV of its type unless this is |
| 756 | // C or extern "C". This follows from [basic.link]p9: |
| 757 | // A type without linkage shall not be used as the type of a |
| 758 | // variable or function with external linkage unless |
| 759 | // - the entity has C language linkage, or |
| 760 | // - the entity is declared within an unnamed namespace, or |
| 761 | // - the entity is not used or is defined in the same |
| 762 | // translation unit. |
| 763 | // and [basic.link]p10: |
| 764 | // ...the types specified by all declarations referring to a |
| 765 | // given variable or function shall be identical... |
| 766 | // C does not have an equivalent rule. |
| 767 | // |
| 768 | // Ignore this if we've got an explicit attribute; the user |
| 769 | // probably knows what they're doing. |
| 770 | // |
| 771 | // Note that we don't want to make the variable non-external |
| 772 | // because of this, but unique-external linkage suits us. |
| 773 | |
| 774 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(D: Var) && |
| 775 | !IgnoreVarTypeLinkage) { |
| 776 | LinkageInfo TypeLV = getLVForType(T: *Var->getType(), computation); |
| 777 | if (!isExternallyVisible(L: TypeLV.getLinkage())) |
| 778 | return LinkageInfo::uniqueExternal(); |
| 779 | if (!LV.isVisibilityExplicit()) |
| 780 | LV.mergeVisibility(other: TypeLV); |
| 781 | } |
| 782 | |
| 783 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 784 | LV.mergeVisibility(newVis: HiddenVisibility, newExplicit: true); |
| 785 | |
| 786 | // Note that Sema::MergeVarDecl already takes care of implementing |
| 787 | // C99 6.2.2p4 and propagating the visibility attribute, so we don't have |
| 788 | // to do it here. |
| 789 | |
| 790 | // As per function and class template specializations (below), |
| 791 | // consider LV for the template and template arguments. We're at file |
| 792 | // scope, so we do not need to worry about nested specializations. |
| 793 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Val: Var)) { |
| 794 | mergeTemplateLV(LV, spec, computation); |
| 795 | } |
| 796 | |
| 797 | // - a function; or |
| 798 | } else if (const auto *Function = dyn_cast<FunctionDecl>(Val: D)) { |
| 799 | // In theory, we can modify the function's LV by the LV of its |
| 800 | // type unless it has C linkage (see comment above about variables |
| 801 | // for justification). In practice, GCC doesn't do this, so it's |
| 802 | // just too painful to make work. |
| 803 | |
| 804 | if (Function->getStorageClass() == SC_PrivateExtern) |
| 805 | LV.mergeVisibility(newVis: HiddenVisibility, newExplicit: true); |
| 806 | |
| 807 | // OpenMP target declare device functions are not callable from the host so |
| 808 | // they should not be exported from the device image. This applies to all |
| 809 | // functions as the host-callable kernel functions are emitted at codegen. |
| 810 | if (Context.getLangOpts().OpenMP && |
| 811 | Context.getLangOpts().OpenMPIsTargetDevice && |
| 812 | (Context.getTargetInfo().getTriple().isGPU() || |
| 813 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: Function))) |
| 814 | LV.mergeVisibility(newVis: HiddenVisibility, /*newExplicit=*/false); |
| 815 | |
| 816 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 817 | // merging storage classes and visibility attributes, so we don't have to |
| 818 | // look at previous decls in here. |
| 819 | |
| 820 | // In C++, then if the type of the function uses a type with |
| 821 | // unique-external linkage, it's not legally usable from outside |
| 822 | // this translation unit. However, we should use the C linkage |
| 823 | // rules instead for extern "C" declarations. |
| 824 | if (Context.getLangOpts().CPlusPlus && !isFirstInExternCContext(D: Function)) { |
| 825 | // Only look at the type-as-written. Otherwise, deducing the return type |
| 826 | // of a function could change its linkage. |
| 827 | QualType TypeAsWritten = Function->getType(); |
| 828 | if (TypeSourceInfo *TSI = Function->getTypeSourceInfo()) |
| 829 | TypeAsWritten = TSI->getType(); |
| 830 | if (!isExternallyVisible(L: TypeAsWritten->getLinkage())) |
| 831 | return LinkageInfo::uniqueExternal(); |
| 832 | } |
| 833 | |
| 834 | // Consider LV from the template and the template arguments. |
| 835 | // We're at file scope, so we do not need to worry about nested |
| 836 | // specializations. |
| 837 | if (FunctionTemplateSpecializationInfo *specInfo |
| 838 | = Function->getTemplateSpecializationInfo()) { |
| 839 | mergeTemplateLV(LV, fn: Function, specInfo, computation); |
| 840 | } |
| 841 | |
| 842 | // - a named class (Clause 9), or an unnamed class defined in a |
| 843 | // typedef declaration in which the class has the typedef name |
| 844 | // for linkage purposes (7.1.3); or |
| 845 | // - a named enumeration (7.2), or an unnamed enumeration |
| 846 | // defined in a typedef declaration in which the enumeration |
| 847 | // has the typedef name for linkage purposes (7.1.3); or |
| 848 | } else if (const auto *Tag = dyn_cast<TagDecl>(Val: D)) { |
| 849 | // Unnamed tags have no linkage. |
| 850 | if (!Tag->hasNameForLinkage()) |
| 851 | return LinkageInfo::none(); |
| 852 | |
| 853 | // If this is a class template specialization, consider the |
| 854 | // linkage of the template and template arguments. We're at file |
| 855 | // scope, so we do not need to worry about nested specializations. |
| 856 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: Tag)) { |
| 857 | mergeTemplateLV(LV, spec, computation); |
| 858 | } |
| 859 | |
| 860 | // FIXME: This is not part of the C++ standard any more. |
| 861 | // - an enumerator belonging to an enumeration with external linkage; or |
| 862 | } else if (isa<EnumConstantDecl>(Val: D)) { |
| 863 | LinkageInfo EnumLV = getLVForDecl(D: cast<NamedDecl>(Val: D->getDeclContext()), |
| 864 | computation); |
| 865 | if (!isExternalFormalLinkage(L: EnumLV.getLinkage())) |
| 866 | return LinkageInfo::none(); |
| 867 | LV.merge(other: EnumLV); |
| 868 | |
| 869 | // - a template |
| 870 | } else if (const auto *temp = dyn_cast<TemplateDecl>(Val: D)) { |
| 871 | bool considerVisibility = !hasExplicitVisibilityAlready(computation); |
| 872 | LinkageInfo tempLV = |
| 873 | getLVForTemplateParameterList(Params: temp->getTemplateParameters(), computation); |
| 874 | LV.mergeMaybeWithVisibility(other: tempLV, withVis: considerVisibility); |
| 875 | |
| 876 | // An unnamed namespace or a namespace declared directly or indirectly |
| 877 | // within an unnamed namespace has internal linkage. All other namespaces |
| 878 | // have external linkage. |
| 879 | // |
| 880 | // We handled names in anonymous namespaces above. |
| 881 | } else if (isa<NamespaceDecl>(Val: D)) { |
| 882 | return LV; |
| 883 | |
| 884 | // By extension, we assign external linkage to Objective-C |
| 885 | // interfaces. |
| 886 | } else if (isa<ObjCInterfaceDecl>(Val: D)) { |
| 887 | // fallout |
| 888 | |
| 889 | } else if (auto *TD = dyn_cast<TypedefNameDecl>(Val: D)) { |
| 890 | // A typedef declaration has linkage if it gives a type a name for |
| 891 | // linkage purposes. |
| 892 | if (!TD->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
| 893 | return LinkageInfo::none(); |
| 894 | |
| 895 | } else if (isa<MSGuidDecl>(Val: D)) { |
| 896 | // A GUID behaves like an inline variable with external linkage. Fall |
| 897 | // through. |
| 898 | |
| 899 | // Everything not covered here has no linkage. |
| 900 | } else { |
| 901 | return LinkageInfo::none(); |
| 902 | } |
| 903 | |
| 904 | // If we ended up with non-externally-visible linkage, visibility should |
| 905 | // always be default. |
| 906 | if (!isExternallyVisible(L: LV.getLinkage())) |
| 907 | return LinkageInfo(LV.getLinkage(), DefaultVisibility, false); |
| 908 | |
| 909 | return LV; |
| 910 | } |
| 911 | |
| 912 | LinkageInfo |
| 913 | LinkageComputer::getLVForClassMember(const NamedDecl *D, |
| 914 | LVComputationKind computation, |
| 915 | bool IgnoreVarTypeLinkage) { |
| 916 | // Only certain class members have linkage. Note that fields don't |
| 917 | // really have linkage, but it's convenient to say they do for the |
| 918 | // purposes of calculating linkage of pointer-to-data-member |
| 919 | // template arguments. |
| 920 | // |
| 921 | // Templates also don't officially have linkage, but since we ignore |
| 922 | // the C++ standard and look at template arguments when determining |
| 923 | // linkage and visibility of a template specialization, we might hit |
| 924 | // a template template argument that way. If we do, we need to |
| 925 | // consider its linkage. |
| 926 | if (!(isa<CXXMethodDecl>(Val: D) || |
| 927 | isa<VarDecl>(Val: D) || |
| 928 | isa<FieldDecl>(Val: D) || |
| 929 | isa<IndirectFieldDecl>(Val: D) || |
| 930 | isa<TagDecl>(Val: D) || |
| 931 | isa<TemplateDecl>(Val: D))) |
| 932 | return LinkageInfo::none(); |
| 933 | |
| 934 | LinkageInfo LV; |
| 935 | |
| 936 | // If we have an explicit visibility attribute, merge that in. |
| 937 | if (!hasExplicitVisibilityAlready(computation)) { |
| 938 | if (std::optional<Visibility> Vis = getExplicitVisibility(D, kind: computation)) |
| 939 | LV.mergeVisibility(newVis: *Vis, newExplicit: true); |
| 940 | // If we're paying attention to global visibility, apply |
| 941 | // -finline-visibility-hidden if this is an inline method. |
| 942 | // |
| 943 | // Note that we do this before merging information about |
| 944 | // the class visibility. |
| 945 | if (!LV.isVisibilityExplicit() && useInlineVisibilityHidden(D)) |
| 946 | LV.mergeVisibility(newVis: HiddenVisibility, /*visibilityExplicit=*/newExplicit: false); |
| 947 | } |
| 948 | |
| 949 | // If this class member has an explicit visibility attribute, the only |
| 950 | // thing that can change its visibility is the template arguments, so |
| 951 | // only look for them when processing the class. |
| 952 | LVComputationKind classComputation = computation; |
| 953 | if (LV.isVisibilityExplicit()) |
| 954 | classComputation = withExplicitVisibilityAlready(Kind: computation); |
| 955 | |
| 956 | LinkageInfo classLV = |
| 957 | getLVForDecl(D: cast<RecordDecl>(Val: D->getDeclContext()), computation: classComputation); |
| 958 | // The member has the same linkage as the class. If that's not externally |
| 959 | // visible, we don't need to compute anything about the linkage. |
| 960 | // FIXME: If we're only computing linkage, can we bail out here? |
| 961 | if (!isExternallyVisible(L: classLV.getLinkage())) |
| 962 | return classLV; |
| 963 | |
| 964 | |
| 965 | // Otherwise, don't merge in classLV yet, because in certain cases |
| 966 | // we need to completely ignore the visibility from it. |
| 967 | |
| 968 | // Specifically, if this decl exists and has an explicit attribute. |
| 969 | const NamedDecl *explicitSpecSuppressor = nullptr; |
| 970 | |
| 971 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: D)) { |
| 972 | // Only look at the type-as-written. Otherwise, deducing the return type |
| 973 | // of a function could change its linkage. |
| 974 | QualType TypeAsWritten = MD->getType(); |
| 975 | if (TypeSourceInfo *TSI = MD->getTypeSourceInfo()) |
| 976 | TypeAsWritten = TSI->getType(); |
| 977 | if (!isExternallyVisible(L: TypeAsWritten->getLinkage())) |
| 978 | return LinkageInfo::uniqueExternal(); |
| 979 | |
| 980 | // If this is a method template specialization, use the linkage for |
| 981 | // the template parameters and arguments. |
| 982 | if (FunctionTemplateSpecializationInfo *spec |
| 983 | = MD->getTemplateSpecializationInfo()) { |
| 984 | mergeTemplateLV(LV, fn: MD, specInfo: spec, computation); |
| 985 | if (spec->isExplicitSpecialization()) { |
| 986 | explicitSpecSuppressor = MD; |
| 987 | } else if (isExplicitMemberSpecialization(D: spec->getTemplate())) { |
| 988 | explicitSpecSuppressor = spec->getTemplate()->getTemplatedDecl(); |
| 989 | } |
| 990 | } else if (isExplicitMemberSpecialization(D: MD)) { |
| 991 | explicitSpecSuppressor = MD; |
| 992 | } |
| 993 | |
| 994 | // OpenMP target declare device functions are not callable from the host so |
| 995 | // they should not be exported from the device image. This applies to all |
| 996 | // functions as the host-callable kernel functions are emitted at codegen. |
| 997 | ASTContext &Context = D->getASTContext(); |
| 998 | if (Context.getLangOpts().OpenMP && |
| 999 | Context.getLangOpts().OpenMPIsTargetDevice && |
| 1000 | ((Context.getTargetInfo().getTriple().isAMDGPU() || |
| 1001 | Context.getTargetInfo().getTriple().isNVPTX()) || |
| 1002 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD: MD))) |
| 1003 | LV.mergeVisibility(newVis: HiddenVisibility, /*newExplicit=*/false); |
| 1004 | |
| 1005 | } else if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: D)) { |
| 1006 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: RD)) { |
| 1007 | mergeTemplateLV(LV, spec, computation); |
| 1008 | if (spec->isExplicitSpecialization()) { |
| 1009 | explicitSpecSuppressor = spec; |
| 1010 | } else { |
| 1011 | const ClassTemplateDecl *temp = spec->getSpecializedTemplate(); |
| 1012 | if (isExplicitMemberSpecialization(D: temp)) { |
| 1013 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 1014 | } |
| 1015 | } |
| 1016 | } else if (isExplicitMemberSpecialization(D: RD)) { |
| 1017 | explicitSpecSuppressor = RD; |
| 1018 | } |
| 1019 | |
| 1020 | // Static data members. |
| 1021 | } else if (const auto *VD = dyn_cast<VarDecl>(Val: D)) { |
| 1022 | if (const auto *spec = dyn_cast<VarTemplateSpecializationDecl>(Val: VD)) |
| 1023 | mergeTemplateLV(LV, spec, computation); |
| 1024 | |
| 1025 | // Modify the variable's linkage by its type, but ignore the |
| 1026 | // type's visibility unless it's a definition. |
| 1027 | if (!IgnoreVarTypeLinkage) { |
| 1028 | LinkageInfo typeLV = getLVForType(T: *VD->getType(), computation); |
| 1029 | // FIXME: If the type's linkage is not externally visible, we can |
| 1030 | // give this static data member UniqueExternalLinkage. |
| 1031 | if (!LV.isVisibilityExplicit() && !classLV.isVisibilityExplicit()) |
| 1032 | LV.mergeVisibility(other: typeLV); |
| 1033 | LV.mergeExternalVisibility(Other: typeLV); |
| 1034 | } |
| 1035 | |
| 1036 | if (isExplicitMemberSpecialization(D: VD)) { |
| 1037 | explicitSpecSuppressor = VD; |
| 1038 | } |
| 1039 | |
| 1040 | // Template members. |
| 1041 | } else if (const auto *temp = dyn_cast<TemplateDecl>(Val: D)) { |
| 1042 | bool considerVisibility = |
| 1043 | (!LV.isVisibilityExplicit() && |
| 1044 | !classLV.isVisibilityExplicit() && |
| 1045 | !hasExplicitVisibilityAlready(computation)); |
| 1046 | LinkageInfo tempLV = |
| 1047 | getLVForTemplateParameterList(Params: temp->getTemplateParameters(), computation); |
| 1048 | LV.mergeMaybeWithVisibility(other: tempLV, withVis: considerVisibility); |
| 1049 | |
| 1050 | if (const auto *redeclTemp = dyn_cast<RedeclarableTemplateDecl>(Val: temp)) { |
| 1051 | if (isExplicitMemberSpecialization(D: redeclTemp)) { |
| 1052 | explicitSpecSuppressor = temp->getTemplatedDecl(); |
| 1053 | } else if (const RedeclarableTemplateDecl *from = |
| 1054 | redeclTemp->getInstantiatedFromMemberTemplate()) { |
| 1055 | // If no explicit visibility is specified yet, and this is an |
| 1056 | // instantiated member of a template, look up visibility there |
| 1057 | // as well. |
| 1058 | LinkageInfo fromLV = from->getLinkageAndVisibility(); |
| 1059 | LV.mergeMaybeWithVisibility(other: fromLV, withVis: considerVisibility); |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | // We should never be looking for an attribute directly on a template. |
| 1065 | assert(!explicitSpecSuppressor || !isa<TemplateDecl>(explicitSpecSuppressor)); |
| 1066 | |
| 1067 | // If this member is an explicit member specialization, and it has |
| 1068 | // an explicit attribute, ignore visibility from the parent. |
| 1069 | bool considerClassVisibility = true; |
| 1070 | if (explicitSpecSuppressor && |
| 1071 | // optimization: hasDVA() is true only with explicit visibility. |
| 1072 | LV.isVisibilityExplicit() && |
| 1073 | classLV.getVisibility() != DefaultVisibility && |
| 1074 | hasDirectVisibilityAttribute(D: explicitSpecSuppressor, computation)) { |
| 1075 | considerClassVisibility = false; |
| 1076 | } |
| 1077 | |
| 1078 | // Finally, merge in information from the class. |
| 1079 | LV.mergeMaybeWithVisibility(other: classLV, withVis: considerClassVisibility); |
| 1080 | return LV; |
| 1081 | } |
| 1082 | |
| 1083 | void NamedDecl::anchor() {} |
| 1084 | |
| 1085 | bool NamedDecl::isLinkageValid() const { |
| 1086 | if (!hasCachedLinkage()) |
| 1087 | return true; |
| 1088 | |
| 1089 | Linkage L = LinkageComputer{} |
| 1090 | .computeLVForDecl(D: this, computation: LVComputationKind::forLinkageOnly()) |
| 1091 | .getLinkage(); |
| 1092 | return L == getCachedLinkage(); |
| 1093 | } |
| 1094 | |
| 1095 | bool NamedDecl::isPlaceholderVar(const LangOptions &LangOpts) const { |
| 1096 | // [C++2c] [basic.scope.scope]/p5 |
| 1097 | // A declaration is name-independent if its name is _ and it declares |
| 1098 | // - a variable with automatic storage duration, |
| 1099 | // - a structured binding not inhabiting a namespace scope, |
| 1100 | // - the variable introduced by an init-capture |
| 1101 | // - or a non-static data member. |
| 1102 | |
| 1103 | if (!LangOpts.CPlusPlus || !getIdentifier() || |
| 1104 | !getIdentifier()->isPlaceholder()) |
| 1105 | return false; |
| 1106 | if (isa<FieldDecl>(Val: this)) |
| 1107 | return true; |
| 1108 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(Val: this)) { |
| 1109 | if (!getDeclContext()->isFunctionOrMethod() && |
| 1110 | !getDeclContext()->isRecord()) |
| 1111 | return false; |
| 1112 | const VarDecl *VD = IFD->getVarDecl(); |
| 1113 | return !VD || VD->getStorageDuration() == SD_Automatic; |
| 1114 | } |
| 1115 | // and it declares a variable with automatic storage duration |
| 1116 | if (const auto *VD = dyn_cast<VarDecl>(Val: this)) { |
| 1117 | if (isa<ParmVarDecl>(Val: VD)) |
| 1118 | return false; |
| 1119 | if (VD->isInitCapture()) |
| 1120 | return true; |
| 1121 | return VD->getStorageDuration() == StorageDuration::SD_Automatic; |
| 1122 | } |
| 1123 | if (const auto *BD = dyn_cast<BindingDecl>(Val: this); |
| 1124 | BD && getDeclContext()->isFunctionOrMethod()) { |
| 1125 | const VarDecl *VD = BD->getHoldingVar(); |
| 1126 | return !VD || VD->getStorageDuration() == StorageDuration::SD_Automatic; |
| 1127 | } |
| 1128 | return false; |
| 1129 | } |
| 1130 | |
| 1131 | ReservedIdentifierStatus |
| 1132 | NamedDecl::isReserved(const LangOptions &LangOpts) const { |
| 1133 | const IdentifierInfo *II = getIdentifier(); |
| 1134 | |
| 1135 | // This triggers at least for CXXLiteralIdentifiers, which we already checked |
| 1136 | // at lexing time. |
| 1137 | if (!II) |
| 1138 | return ReservedIdentifierStatus::NotReserved; |
| 1139 | |
| 1140 | ReservedIdentifierStatus Status = II->isReserved(LangOpts); |
| 1141 | if (isReservedAtGlobalScope(Status) && !isReservedInAllContexts(Status)) { |
| 1142 | // This name is only reserved at global scope. Check if this declaration |
| 1143 | // conflicts with a global scope declaration. |
| 1144 | if (isa<ParmVarDecl>(Val: this) || isTemplateParameter()) |
| 1145 | return ReservedIdentifierStatus::NotReserved; |
| 1146 | |
| 1147 | // C++ [dcl.link]/7: |
| 1148 | // Two declarations [conflict] if [...] one declares a function or |
| 1149 | // variable with C language linkage, and the other declares [...] a |
| 1150 | // variable that belongs to the global scope. |
| 1151 | // |
| 1152 | // Therefore names that are reserved at global scope are also reserved as |
| 1153 | // names of variables and functions with C language linkage. |
| 1154 | const DeclContext *DC = getDeclContext()->getRedeclContext(); |
| 1155 | if (DC->isTranslationUnit()) |
| 1156 | return Status; |
| 1157 | if (auto *VD = dyn_cast<VarDecl>(Val: this)) |
| 1158 | if (VD->isExternC()) |
| 1159 | return ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC; |
| 1160 | if (auto *FD = dyn_cast<FunctionDecl>(Val: this)) |
| 1161 | if (FD->isExternC()) |
| 1162 | return ReservedIdentifierStatus::StartsWithUnderscoreAndIsExternC; |
| 1163 | return ReservedIdentifierStatus::NotReserved; |
| 1164 | } |
| 1165 | |
| 1166 | return Status; |
| 1167 | } |
| 1168 | |
| 1169 | ObjCStringFormatFamily NamedDecl::getObjCFStringFormattingFamily() const { |
| 1170 | StringRef name = getName(); |
| 1171 | if (name.empty()) return SFF_None; |
| 1172 | |
| 1173 | if (name.front() == 'C') |
| 1174 | if (name == "CFStringCreateWithFormat" || |
| 1175 | name == "CFStringCreateWithFormatAndArguments" || |
| 1176 | name == "CFStringAppendFormat" || |
| 1177 | name == "CFStringAppendFormatAndArguments" ) |
| 1178 | return SFF_CFString; |
| 1179 | return SFF_None; |
| 1180 | } |
| 1181 | |
| 1182 | Linkage NamedDecl::getLinkageInternal() const { |
| 1183 | // We don't care about visibility here, so ask for the cheapest |
| 1184 | // possible visibility analysis. |
| 1185 | return LinkageComputer{} |
| 1186 | .getLVForDecl(D: this, computation: LVComputationKind::forLinkageOnly()) |
| 1187 | .getLinkage(); |
| 1188 | } |
| 1189 | |
| 1190 | static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) { |
| 1191 | // FIXME: Handle isModulePrivate. |
| 1192 | switch (D->getModuleOwnershipKind()) { |
| 1193 | case Decl::ModuleOwnershipKind::Unowned: |
| 1194 | case Decl::ModuleOwnershipKind::ReachableWhenImported: |
| 1195 | case Decl::ModuleOwnershipKind::ModulePrivate: |
| 1196 | return false; |
| 1197 | case Decl::ModuleOwnershipKind::Visible: |
| 1198 | case Decl::ModuleOwnershipKind::VisibleWhenImported: |
| 1199 | return D->isInNamedModule(); |
| 1200 | } |
| 1201 | llvm_unreachable("unexpected module ownership kind" ); |
| 1202 | } |
| 1203 | |
| 1204 | /// Get the linkage from a semantic point of view. Entities in |
| 1205 | /// anonymous namespaces are external (in c++98). |
| 1206 | Linkage NamedDecl::getFormalLinkage() const { |
| 1207 | Linkage InternalLinkage = getLinkageInternal(); |
| 1208 | |
| 1209 | // C++ [basic.link]p4.8: |
| 1210 | // - if the declaration of the name is attached to a named module and is not |
| 1211 | // exported |
| 1212 | // the name has module linkage; |
| 1213 | // |
| 1214 | // [basic.namespace.general]/p2 |
| 1215 | // A namespace is never attached to a named module and never has a name with |
| 1216 | // module linkage. |
| 1217 | if (isInNamedModule() && InternalLinkage == Linkage::External && |
| 1218 | !isExportedFromModuleInterfaceUnit( |
| 1219 | D: cast<NamedDecl>(Val: this->getCanonicalDecl())) && |
| 1220 | !isa<NamespaceDecl>(Val: this)) |
| 1221 | InternalLinkage = Linkage::Module; |
| 1222 | |
| 1223 | return clang::getFormalLinkage(L: InternalLinkage); |
| 1224 | } |
| 1225 | |
| 1226 | LinkageInfo NamedDecl::getLinkageAndVisibility() const { |
| 1227 | return LinkageComputer{}.getDeclLinkageAndVisibility(D: this); |
| 1228 | } |
| 1229 | |
| 1230 | static std::optional<Visibility> |
| 1231 | getExplicitVisibilityAux(const NamedDecl *ND, |
| 1232 | NamedDecl::ExplicitVisibilityKind kind, |
| 1233 | bool IsMostRecent) { |
| 1234 | assert(!IsMostRecent || ND == ND->getMostRecentDecl()); |
| 1235 | |
| 1236 | // Check the declaration itself first. |
| 1237 | if (std::optional<Visibility> V = getVisibilityOf(D: ND, kind)) |
| 1238 | return V; |
| 1239 | |
| 1240 | // If this is a member class of a specialization of a class template |
| 1241 | // and the corresponding decl has explicit visibility, use that. |
| 1242 | if (const auto *RD = dyn_cast<CXXRecordDecl>(Val: ND)) { |
| 1243 | CXXRecordDecl *InstantiatedFrom = RD->getInstantiatedFromMemberClass(); |
| 1244 | if (InstantiatedFrom) |
| 1245 | return getVisibilityOf(D: InstantiatedFrom, kind); |
| 1246 | } |
| 1247 | |
| 1248 | // If there wasn't explicit visibility there, and this is a |
| 1249 | // specialization of a class template, check for visibility |
| 1250 | // on the pattern. |
| 1251 | if (const auto *spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: ND)) { |
| 1252 | // Walk all the template decl till this point to see if there are |
| 1253 | // explicit visibility attributes. |
| 1254 | const auto *TD = spec->getSpecializedTemplate()->getTemplatedDecl(); |
| 1255 | while (TD != nullptr) { |
| 1256 | auto Vis = getVisibilityOf(D: TD, kind); |
| 1257 | if (Vis != std::nullopt) |
| 1258 | return Vis; |
| 1259 | TD = TD->getPreviousDecl(); |
| 1260 | } |
| 1261 | return std::nullopt; |
| 1262 | } |
| 1263 | |
| 1264 | // Use the most recent declaration. |
| 1265 | if (!IsMostRecent && !isa<NamespaceDecl>(Val: ND)) { |
| 1266 | const NamedDecl *MostRecent = ND->getMostRecentDecl(); |
| 1267 | if (MostRecent != ND) |
| 1268 | return getExplicitVisibilityAux(ND: MostRecent, kind, IsMostRecent: true); |
| 1269 | } |
| 1270 | |
| 1271 | if (const auto *Var = dyn_cast<VarDecl>(Val: ND)) { |
| 1272 | if (Var->isStaticDataMember()) { |
| 1273 | VarDecl *InstantiatedFrom = Var->getInstantiatedFromStaticDataMember(); |
| 1274 | if (InstantiatedFrom) |
| 1275 | return getVisibilityOf(D: InstantiatedFrom, kind); |
| 1276 | } |
| 1277 | |
| 1278 | if (const auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: Var)) |
| 1279 | return getVisibilityOf(D: VTSD->getSpecializedTemplate()->getTemplatedDecl(), |
| 1280 | kind); |
| 1281 | |
| 1282 | return std::nullopt; |
| 1283 | } |
| 1284 | // Also handle function template specializations. |
| 1285 | if (const auto *fn = dyn_cast<FunctionDecl>(Val: ND)) { |
| 1286 | // If the function is a specialization of a template with an |
| 1287 | // explicit visibility attribute, use that. |
| 1288 | if (FunctionTemplateSpecializationInfo *templateInfo |
| 1289 | = fn->getTemplateSpecializationInfo()) |
| 1290 | return getVisibilityOf(D: templateInfo->getTemplate()->getTemplatedDecl(), |
| 1291 | kind); |
| 1292 | |
| 1293 | // If the function is a member of a specialization of a class template |
| 1294 | // and the corresponding decl has explicit visibility, use that. |
| 1295 | FunctionDecl *InstantiatedFrom = fn->getInstantiatedFromMemberFunction(); |
| 1296 | if (InstantiatedFrom) |
| 1297 | return getVisibilityOf(D: InstantiatedFrom, kind); |
| 1298 | |
| 1299 | return std::nullopt; |
| 1300 | } |
| 1301 | |
| 1302 | // The visibility of a template is stored in the templated decl. |
| 1303 | if (const auto *TD = dyn_cast<TemplateDecl>(Val: ND)) |
| 1304 | return getVisibilityOf(D: TD->getTemplatedDecl(), kind); |
| 1305 | |
| 1306 | return std::nullopt; |
| 1307 | } |
| 1308 | |
| 1309 | std::optional<Visibility> |
| 1310 | NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { |
| 1311 | return getExplicitVisibilityAux(ND: this, kind, IsMostRecent: false); |
| 1312 | } |
| 1313 | |
| 1314 | LinkageInfo LinkageComputer::getLVForClosure(const DeclContext *DC, |
| 1315 | Decl *ContextDecl, |
| 1316 | LVComputationKind computation) { |
| 1317 | // This lambda has its linkage/visibility determined by its owner. |
| 1318 | const NamedDecl *Owner; |
| 1319 | if (!ContextDecl) |
| 1320 | Owner = dyn_cast<NamedDecl>(Val: DC); |
| 1321 | else if (isa<ParmVarDecl>(Val: ContextDecl)) |
| 1322 | Owner = |
| 1323 | dyn_cast<NamedDecl>(Val: ContextDecl->getDeclContext()->getRedeclContext()); |
| 1324 | else if (isa<ImplicitConceptSpecializationDecl>(Val: ContextDecl)) { |
| 1325 | // Replace with the concept's owning decl, which is either a namespace or a |
| 1326 | // TU, so this needs a dyn_cast. |
| 1327 | Owner = dyn_cast<NamedDecl>(Val: ContextDecl->getDeclContext()); |
| 1328 | } else { |
| 1329 | Owner = cast<NamedDecl>(Val: ContextDecl); |
| 1330 | } |
| 1331 | |
| 1332 | if (!Owner) |
| 1333 | return LinkageInfo::none(); |
| 1334 | |
| 1335 | // If the owner has a deduced type, we need to skip querying the linkage and |
| 1336 | // visibility of that type, because it might involve this closure type. The |
| 1337 | // only effect of this is that we might give a lambda VisibleNoLinkage rather |
| 1338 | // than NoLinkage when we don't strictly need to, which is benign. |
| 1339 | auto *VD = dyn_cast<VarDecl>(Val: Owner); |
| 1340 | LinkageInfo OwnerLV = |
| 1341 | VD && VD->getType()->getContainedDeducedType() |
| 1342 | ? computeLVForDecl(D: Owner, computation, /*IgnoreVarTypeLinkage*/true) |
| 1343 | : getLVForDecl(D: Owner, computation); |
| 1344 | |
| 1345 | // A lambda never formally has linkage. But if the owner is externally |
| 1346 | // visible, then the lambda is too. We apply the same rules to blocks. |
| 1347 | if (!isExternallyVisible(L: OwnerLV.getLinkage())) |
| 1348 | return LinkageInfo::none(); |
| 1349 | return LinkageInfo(Linkage::VisibleNone, OwnerLV.getVisibility(), |
| 1350 | OwnerLV.isVisibilityExplicit()); |
| 1351 | } |
| 1352 | |
| 1353 | LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D, |
| 1354 | LVComputationKind computation) { |
| 1355 | if (const auto *Function = dyn_cast<FunctionDecl>(Val: D)) { |
| 1356 | if (Function->isInAnonymousNamespace() && |
| 1357 | !isFirstInExternCContext(D: Function)) |
| 1358 | return LinkageInfo::internal(); |
| 1359 | |
| 1360 | // This is a "void f();" which got merged with a file static. |
| 1361 | if (Function->getCanonicalDecl()->getStorageClass() == SC_Static) |
| 1362 | return LinkageInfo::internal(); |
| 1363 | |
| 1364 | LinkageInfo LV; |
| 1365 | if (!hasExplicitVisibilityAlready(computation)) { |
| 1366 | if (std::optional<Visibility> Vis = |
| 1367 | getExplicitVisibility(D: Function, kind: computation)) |
| 1368 | LV.mergeVisibility(newVis: *Vis, newExplicit: true); |
| 1369 | } |
| 1370 | |
| 1371 | // Note that Sema::MergeCompatibleFunctionDecls already takes care of |
| 1372 | // merging storage classes and visibility attributes, so we don't have to |
| 1373 | // look at previous decls in here. |
| 1374 | |
| 1375 | return LV; |
| 1376 | } |
| 1377 | |
| 1378 | if (const auto *Var = dyn_cast<VarDecl>(Val: D)) { |
| 1379 | if (Var->hasExternalStorage()) { |
| 1380 | if (Var->isInAnonymousNamespace() && !isFirstInExternCContext(D: Var)) |
| 1381 | return LinkageInfo::internal(); |
| 1382 | |
| 1383 | LinkageInfo LV; |
| 1384 | if (Var->getStorageClass() == SC_PrivateExtern) |
| 1385 | LV.mergeVisibility(newVis: HiddenVisibility, newExplicit: true); |
| 1386 | else if (!hasExplicitVisibilityAlready(computation)) { |
| 1387 | if (std::optional<Visibility> Vis = |
| 1388 | getExplicitVisibility(D: Var, kind: computation)) |
| 1389 | LV.mergeVisibility(newVis: *Vis, newExplicit: true); |
| 1390 | } |
| 1391 | |
| 1392 | if (const VarDecl *Prev = Var->getPreviousDecl()) { |
| 1393 | LinkageInfo PrevLV = getLVForDecl(D: Prev, computation); |
| 1394 | if (PrevLV.getLinkage() != Linkage::Invalid) |
| 1395 | LV.setLinkage(PrevLV.getLinkage()); |
| 1396 | LV.mergeVisibility(other: PrevLV); |
| 1397 | } |
| 1398 | |
| 1399 | return LV; |
| 1400 | } |
| 1401 | |
| 1402 | if (!Var->isStaticLocal()) |
| 1403 | return LinkageInfo::none(); |
| 1404 | } |
| 1405 | |
| 1406 | ASTContext &Context = D->getASTContext(); |
| 1407 | if (!Context.getLangOpts().CPlusPlus) |
| 1408 | return LinkageInfo::none(); |
| 1409 | |
| 1410 | const Decl *OuterD = getOutermostFuncOrBlockContext(D); |
| 1411 | if (!OuterD || OuterD->isInvalidDecl()) |
| 1412 | return LinkageInfo::none(); |
| 1413 | |
| 1414 | LinkageInfo LV; |
| 1415 | if (const auto *BD = dyn_cast<BlockDecl>(Val: OuterD)) { |
| 1416 | if (!BD->getBlockManglingNumber()) |
| 1417 | return LinkageInfo::none(); |
| 1418 | |
| 1419 | LV = getLVForClosure(DC: BD->getDeclContext()->getRedeclContext(), |
| 1420 | ContextDecl: BD->getBlockManglingContextDecl(), computation); |
| 1421 | } else { |
| 1422 | const auto *FD = cast<FunctionDecl>(Val: OuterD); |
| 1423 | if (!FD->isInlined() && |
| 1424 | !isTemplateInstantiation(Kind: FD->getTemplateSpecializationKind())) |
| 1425 | return LinkageInfo::none(); |
| 1426 | |
| 1427 | // If a function is hidden by -fvisibility-inlines-hidden option and |
| 1428 | // is not explicitly attributed as a hidden function, |
| 1429 | // we should not make static local variables in the function hidden. |
| 1430 | LV = getLVForDecl(D: FD, computation); |
| 1431 | if (isa<VarDecl>(Val: D) && useInlineVisibilityHidden(D: FD) && |
| 1432 | !LV.isVisibilityExplicit() && |
| 1433 | !Context.getLangOpts().VisibilityInlinesHiddenStaticLocalVar) { |
| 1434 | assert(cast<VarDecl>(D)->isStaticLocal()); |
| 1435 | // If this was an implicitly hidden inline method, check again for |
| 1436 | // explicit visibility on the parent class, and use that for static locals |
| 1437 | // if present. |
| 1438 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD)) |
| 1439 | LV = getLVForDecl(D: MD->getParent(), computation); |
| 1440 | if (!LV.isVisibilityExplicit()) { |
| 1441 | Visibility globalVisibility = |
| 1442 | computation.isValueVisibility() |
| 1443 | ? Context.getLangOpts().getValueVisibilityMode() |
| 1444 | : Context.getLangOpts().getTypeVisibilityMode(); |
| 1445 | return LinkageInfo(Linkage::VisibleNone, globalVisibility, |
| 1446 | /*visibilityExplicit=*/false); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | if (!isExternallyVisible(L: LV.getLinkage())) |
| 1451 | return LinkageInfo::none(); |
| 1452 | return LinkageInfo(Linkage::VisibleNone, LV.getVisibility(), |
| 1453 | LV.isVisibilityExplicit()); |
| 1454 | } |
| 1455 | |
| 1456 | LinkageInfo LinkageComputer::computeLVForDecl(const NamedDecl *D, |
| 1457 | LVComputationKind computation, |
| 1458 | bool IgnoreVarTypeLinkage) { |
| 1459 | // Internal_linkage attribute overrides other considerations. |
| 1460 | if (D->hasAttr<InternalLinkageAttr>()) |
| 1461 | return LinkageInfo::internal(); |
| 1462 | |
| 1463 | // Objective-C: treat all Objective-C declarations as having external |
| 1464 | // linkage. |
| 1465 | switch (D->getKind()) { |
| 1466 | default: |
| 1467 | break; |
| 1468 | |
| 1469 | // Per C++ [basic.link]p2, only the names of objects, references, |
| 1470 | // functions, types, templates, namespaces, and values ever have linkage. |
| 1471 | // |
| 1472 | // Note that the name of a typedef, namespace alias, using declaration, |
| 1473 | // and so on are not the name of the corresponding type, namespace, or |
| 1474 | // declaration, so they do *not* have linkage. |
| 1475 | case Decl::ImplicitParam: |
| 1476 | case Decl::Label: |
| 1477 | case Decl::NamespaceAlias: |
| 1478 | case Decl::ParmVar: |
| 1479 | case Decl::Using: |
| 1480 | case Decl::UsingEnum: |
| 1481 | case Decl::UsingShadow: |
| 1482 | case Decl::UsingDirective: |
| 1483 | return LinkageInfo::none(); |
| 1484 | |
| 1485 | case Decl::EnumConstant: |
| 1486 | // C++ [basic.link]p4: an enumerator has the linkage of its enumeration. |
| 1487 | if (D->getASTContext().getLangOpts().CPlusPlus) |
| 1488 | return getLVForDecl(D: cast<EnumDecl>(Val: D->getDeclContext()), computation); |
| 1489 | return LinkageInfo::visible_none(); |
| 1490 | |
| 1491 | case Decl::Typedef: |
| 1492 | case Decl::TypeAlias: |
| 1493 | // A typedef declaration has linkage if it gives a type a name for |
| 1494 | // linkage purposes. |
| 1495 | if (!cast<TypedefNameDecl>(Val: D) |
| 1496 | ->getAnonDeclWithTypedefName(/*AnyRedecl*/true)) |
| 1497 | return LinkageInfo::none(); |
| 1498 | break; |
| 1499 | |
| 1500 | case Decl::TemplateTemplateParm: // count these as external |
| 1501 | case Decl::NonTypeTemplateParm: |
| 1502 | case Decl::ObjCAtDefsField: |
| 1503 | case Decl::ObjCCategory: |
| 1504 | case Decl::ObjCCategoryImpl: |
| 1505 | case Decl::ObjCCompatibleAlias: |
| 1506 | case Decl::ObjCImplementation: |
| 1507 | case Decl::ObjCMethod: |
| 1508 | case Decl::ObjCProperty: |
| 1509 | case Decl::ObjCPropertyImpl: |
| 1510 | case Decl::ObjCProtocol: |
| 1511 | return getExternalLinkageFor(D); |
| 1512 | |
| 1513 | case Decl::CXXRecord: { |
| 1514 | const auto *Record = cast<CXXRecordDecl>(Val: D); |
| 1515 | if (Record->isLambda()) { |
| 1516 | if (Record->hasKnownLambdaInternalLinkage() || |
| 1517 | !Record->getLambdaManglingNumber()) { |
| 1518 | // This lambda has no mangling number, so it's internal. |
| 1519 | return LinkageInfo::internal(); |
| 1520 | } |
| 1521 | |
| 1522 | return getLVForClosure( |
| 1523 | DC: Record->getDeclContext()->getRedeclContext(), |
| 1524 | ContextDecl: Record->getLambdaContextDecl(), computation); |
| 1525 | } |
| 1526 | |
| 1527 | break; |
| 1528 | } |
| 1529 | |
| 1530 | case Decl::TemplateParamObject: { |
| 1531 | // The template parameter object can be referenced from anywhere its type |
| 1532 | // and value can be referenced. |
| 1533 | auto *TPO = cast<TemplateParamObjectDecl>(Val: D); |
| 1534 | LinkageInfo LV = getLVForType(T: *TPO->getType(), computation); |
| 1535 | LV.merge(other: getLVForValue(V: TPO->getValue(), computation)); |
| 1536 | return LV; |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | // Handle linkage for namespace-scope names. |
| 1541 | if (D->getDeclContext()->getRedeclContext()->isFileContext()) |
| 1542 | return getLVForNamespaceScopeDecl(D, computation, IgnoreVarTypeLinkage); |
| 1543 | |
| 1544 | // C++ [basic.link]p5: |
| 1545 | // In addition, a member function, static data member, a named |
| 1546 | // class or enumeration of class scope, or an unnamed class or |
| 1547 | // enumeration defined in a class-scope typedef declaration such |
| 1548 | // that the class or enumeration has the typedef name for linkage |
| 1549 | // purposes (7.1.3), has external linkage if the name of the class |
| 1550 | // has external linkage. |
| 1551 | if (D->getDeclContext()->isRecord()) |
| 1552 | return getLVForClassMember(D, computation, IgnoreVarTypeLinkage); |
| 1553 | |
| 1554 | // C++ [basic.link]p6: |
| 1555 | // The name of a function declared in block scope and the name of |
| 1556 | // an object declared by a block scope extern declaration have |
| 1557 | // linkage. If there is a visible declaration of an entity with |
| 1558 | // linkage having the same name and type, ignoring entities |
| 1559 | // declared outside the innermost enclosing namespace scope, the |
| 1560 | // block scope declaration declares that same entity and receives |
| 1561 | // the linkage of the previous declaration. If there is more than |
| 1562 | // one such matching entity, the program is ill-formed. Otherwise, |
| 1563 | // if no matching entity is found, the block scope entity receives |
| 1564 | // external linkage. |
| 1565 | if (D->getDeclContext()->isFunctionOrMethod()) |
| 1566 | return getLVForLocalDecl(D, computation); |
| 1567 | |
| 1568 | // C++ [basic.link]p6: |
| 1569 | // Names not covered by these rules have no linkage. |
| 1570 | return LinkageInfo::none(); |
| 1571 | } |
| 1572 | |
| 1573 | /// getLVForDecl - Get the linkage and visibility for the given declaration. |
| 1574 | LinkageInfo LinkageComputer::getLVForDecl(const NamedDecl *D, |
| 1575 | LVComputationKind computation) { |
| 1576 | // Internal_linkage attribute overrides other considerations. |
| 1577 | if (D->hasAttr<InternalLinkageAttr>()) |
| 1578 | return LinkageInfo::internal(); |
| 1579 | |
| 1580 | if (computation.IgnoreAllVisibility && D->hasCachedLinkage()) |
| 1581 | return LinkageInfo(D->getCachedLinkage(), DefaultVisibility, false); |
| 1582 | |
| 1583 | if (std::optional<LinkageInfo> LI = lookup(ND: D, Kind: computation)) |
| 1584 | return *LI; |
| 1585 | |
| 1586 | LinkageInfo LV = computeLVForDecl(D, computation); |
| 1587 | if (D->hasCachedLinkage()) |
| 1588 | assert(D->getCachedLinkage() == LV.getLinkage()); |
| 1589 | |
| 1590 | D->setCachedLinkage(LV.getLinkage()); |
| 1591 | cache(ND: D, Kind: computation, Info: LV); |
| 1592 | |
| 1593 | #ifndef NDEBUG |
| 1594 | // In C (because of gnu inline) and in c++ with microsoft extensions an |
| 1595 | // static can follow an extern, so we can have two decls with different |
| 1596 | // linkages. |
| 1597 | const LangOptions &Opts = D->getASTContext().getLangOpts(); |
| 1598 | if (!Opts.CPlusPlus || Opts.MicrosoftExt) |
| 1599 | return LV; |
| 1600 | |
| 1601 | // We have just computed the linkage for this decl. By induction we know |
| 1602 | // that all other computed linkages match, check that the one we just |
| 1603 | // computed also does. |
| 1604 | NamedDecl *Old = nullptr; |
| 1605 | for (auto *I : D->redecls()) { |
| 1606 | auto *T = cast<NamedDecl>(I); |
| 1607 | if (T == D) |
| 1608 | continue; |
| 1609 | if (!T->isInvalidDecl() && T->hasCachedLinkage()) { |
| 1610 | Old = T; |
| 1611 | break; |
| 1612 | } |
| 1613 | } |
| 1614 | assert(!Old || Old->getCachedLinkage() == D->getCachedLinkage()); |
| 1615 | #endif |
| 1616 | |
| 1617 | return LV; |
| 1618 | } |
| 1619 | |
| 1620 | LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { |
| 1621 | NamedDecl::ExplicitVisibilityKind EK = usesTypeVisibility(D) |
| 1622 | ? NamedDecl::VisibilityForType |
| 1623 | : NamedDecl::VisibilityForValue; |
| 1624 | LVComputationKind CK(EK); |
| 1625 | return getLVForDecl(D, computation: D->getASTContext().getLangOpts().IgnoreXCOFFVisibility |
| 1626 | ? CK.forLinkageOnly() |
| 1627 | : CK); |
| 1628 | } |
| 1629 | |
| 1630 | Module *Decl::getOwningModuleForLinkage() const { |
| 1631 | if (isa<NamespaceDecl>(Val: this)) |
| 1632 | // Namespaces never have module linkage. It is the entities within them |
| 1633 | // that [may] do. |
| 1634 | return nullptr; |
| 1635 | |
| 1636 | Module *M = getOwningModule(); |
| 1637 | if (!M) |
| 1638 | return nullptr; |
| 1639 | |
| 1640 | switch (M->Kind) { |
| 1641 | case Module::ModuleMapModule: |
| 1642 | // Module map modules have no special linkage semantics. |
| 1643 | return nullptr; |
| 1644 | |
| 1645 | case Module::ModuleInterfaceUnit: |
| 1646 | case Module::ModuleImplementationUnit: |
| 1647 | case Module::ModulePartitionInterface: |
| 1648 | case Module::ModulePartitionImplementation: |
| 1649 | return M; |
| 1650 | |
| 1651 | case Module::ModuleHeaderUnit: |
| 1652 | case Module::ExplicitGlobalModuleFragment: |
| 1653 | case Module::ImplicitGlobalModuleFragment: |
| 1654 | // The global module shouldn't change the linkage. |
| 1655 | return nullptr; |
| 1656 | |
| 1657 | case Module::PrivateModuleFragment: |
| 1658 | // The private module fragment is part of its containing module for linkage |
| 1659 | // purposes. |
| 1660 | return M->Parent; |
| 1661 | } |
| 1662 | |
| 1663 | llvm_unreachable("unknown module kind" ); |
| 1664 | } |
| 1665 | |
| 1666 | void NamedDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { |
| 1667 | Name.print(OS, Policy); |
| 1668 | } |
| 1669 | |
| 1670 | void NamedDecl::printName(raw_ostream &OS) const { |
| 1671 | printName(OS, Policy: getASTContext().getPrintingPolicy()); |
| 1672 | } |
| 1673 | |
| 1674 | std::string NamedDecl::getQualifiedNameAsString() const { |
| 1675 | std::string QualName; |
| 1676 | llvm::raw_string_ostream OS(QualName); |
| 1677 | printQualifiedName(OS, Policy: getASTContext().getPrintingPolicy()); |
| 1678 | return QualName; |
| 1679 | } |
| 1680 | |
| 1681 | void NamedDecl::printQualifiedName(raw_ostream &OS) const { |
| 1682 | printQualifiedName(OS, Policy: getASTContext().getPrintingPolicy()); |
| 1683 | } |
| 1684 | |
| 1685 | void NamedDecl::printQualifiedName(raw_ostream &OS, |
| 1686 | const PrintingPolicy &P) const { |
| 1687 | if (getDeclContext()->isFunctionOrMethod()) { |
| 1688 | // We do not print '(anonymous)' for function parameters without name. |
| 1689 | printName(OS, Policy: P); |
| 1690 | return; |
| 1691 | } |
| 1692 | printNestedNameSpecifier(OS, Policy: P); |
| 1693 | if (getDeclName()) |
| 1694 | OS << *this; |
| 1695 | else { |
| 1696 | // Give the printName override a chance to pick a different name before we |
| 1697 | // fall back to "(anonymous)". |
| 1698 | SmallString<64> NameBuffer; |
| 1699 | llvm::raw_svector_ostream NameOS(NameBuffer); |
| 1700 | printName(OS&: NameOS, Policy: P); |
| 1701 | if (NameBuffer.empty()) |
| 1702 | OS << "(anonymous)" ; |
| 1703 | else |
| 1704 | OS << NameBuffer; |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | void NamedDecl::printNestedNameSpecifier(raw_ostream &OS) const { |
| 1709 | printNestedNameSpecifier(OS, Policy: getASTContext().getPrintingPolicy()); |
| 1710 | } |
| 1711 | |
| 1712 | void NamedDecl::printNestedNameSpecifier(raw_ostream &OS, |
| 1713 | const PrintingPolicy &P) const { |
| 1714 | const DeclContext *Ctx = getDeclContext(); |
| 1715 | |
| 1716 | // For ObjC methods and properties, look through categories and use the |
| 1717 | // interface as context. |
| 1718 | if (auto *MD = dyn_cast<ObjCMethodDecl>(Val: this)) { |
| 1719 | if (auto *ID = MD->getClassInterface()) |
| 1720 | Ctx = ID; |
| 1721 | } else if (auto *PD = dyn_cast<ObjCPropertyDecl>(Val: this)) { |
| 1722 | if (auto *MD = PD->getGetterMethodDecl()) |
| 1723 | if (auto *ID = MD->getClassInterface()) |
| 1724 | Ctx = ID; |
| 1725 | } else if (auto *ID = dyn_cast<ObjCIvarDecl>(Val: this)) { |
| 1726 | if (auto *CI = ID->getContainingInterface()) |
| 1727 | Ctx = CI; |
| 1728 | } |
| 1729 | |
| 1730 | if (Ctx->isFunctionOrMethod()) |
| 1731 | return; |
| 1732 | |
| 1733 | using ContextsTy = SmallVector<const DeclContext *, 8>; |
| 1734 | ContextsTy Contexts; |
| 1735 | |
| 1736 | // Collect named contexts. |
| 1737 | DeclarationName NameInScope = getDeclName(); |
| 1738 | for (; Ctx; Ctx = Ctx->getParent()) { |
| 1739 | // Suppress anonymous namespace if requested. |
| 1740 | if (P.SuppressUnwrittenScope && isa<NamespaceDecl>(Val: Ctx) && |
| 1741 | cast<NamespaceDecl>(Val: Ctx)->isAnonymousNamespace()) |
| 1742 | continue; |
| 1743 | |
| 1744 | // Suppress inline namespace if it doesn't make the result ambiguous. |
| 1745 | if (Ctx->isInlineNamespace() && NameInScope) { |
| 1746 | if (P.SuppressInlineNamespace == |
| 1747 | PrintingPolicy::SuppressInlineNamespaceMode::All || |
| 1748 | (P.SuppressInlineNamespace == |
| 1749 | PrintingPolicy::SuppressInlineNamespaceMode::Redundant && |
| 1750 | cast<NamespaceDecl>(Val: Ctx)->isRedundantInlineQualifierFor( |
| 1751 | Name: NameInScope))) { |
| 1752 | continue; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | // Suppress transparent contexts like export or HLSLBufferDecl context |
| 1757 | if (Ctx->isTransparentContext()) |
| 1758 | continue; |
| 1759 | |
| 1760 | // Skip non-named contexts such as linkage specifications and ExportDecls. |
| 1761 | const NamedDecl *ND = dyn_cast<NamedDecl>(Val: Ctx); |
| 1762 | if (!ND) |
| 1763 | continue; |
| 1764 | |
| 1765 | Contexts.push_back(Elt: Ctx); |
| 1766 | NameInScope = ND->getDeclName(); |
| 1767 | } |
| 1768 | |
| 1769 | for (const DeclContext *DC : llvm::reverse(C&: Contexts)) { |
| 1770 | if (const auto *Spec = dyn_cast<ClassTemplateSpecializationDecl>(Val: DC)) { |
| 1771 | OS << Spec->getName(); |
| 1772 | const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs(); |
| 1773 | printTemplateArgumentList( |
| 1774 | OS, Args: TemplateArgs.asArray(), Policy: P, |
| 1775 | TPL: Spec->getSpecializedTemplate()->getTemplateParameters()); |
| 1776 | } else if (const auto *ND = dyn_cast<NamespaceDecl>(Val: DC)) { |
| 1777 | if (ND->isAnonymousNamespace()) { |
| 1778 | OS << (P.MSVCFormatting ? "`anonymous namespace\'" |
| 1779 | : "(anonymous namespace)" ); |
| 1780 | } |
| 1781 | else |
| 1782 | OS << *ND; |
| 1783 | } else if (const auto *RD = dyn_cast<RecordDecl>(Val: DC)) { |
| 1784 | if (!RD->getIdentifier()) |
| 1785 | OS << "(anonymous " << RD->getKindName() << ')'; |
| 1786 | else |
| 1787 | OS << *RD; |
| 1788 | } else if (const auto *FD = dyn_cast<FunctionDecl>(Val: DC)) { |
| 1789 | const FunctionProtoType *FT = nullptr; |
| 1790 | if (FD->hasWrittenPrototype()) |
| 1791 | FT = dyn_cast<FunctionProtoType>(Val: FD->getType()->castAs<FunctionType>()); |
| 1792 | |
| 1793 | OS << *FD << '('; |
| 1794 | if (FT) { |
| 1795 | unsigned NumParams = FD->getNumParams(); |
| 1796 | for (unsigned i = 0; i < NumParams; ++i) { |
| 1797 | if (i) |
| 1798 | OS << ", " ; |
| 1799 | OS << FD->getParamDecl(i)->getType().stream(Policy: P); |
| 1800 | } |
| 1801 | |
| 1802 | if (FT->isVariadic()) { |
| 1803 | if (NumParams > 0) |
| 1804 | OS << ", " ; |
| 1805 | OS << "..." ; |
| 1806 | } |
| 1807 | } |
| 1808 | OS << ')'; |
| 1809 | } else if (const auto *ED = dyn_cast<EnumDecl>(Val: DC)) { |
| 1810 | // C++ [dcl.enum]p10: Each enum-name and each unscoped |
| 1811 | // enumerator is declared in the scope that immediately contains |
| 1812 | // the enum-specifier. Each scoped enumerator is declared in the |
| 1813 | // scope of the enumeration. |
| 1814 | // For the case of unscoped enumerator, do not include in the qualified |
| 1815 | // name any information about its enum enclosing scope, as its visibility |
| 1816 | // is global. |
| 1817 | if (ED->isScoped()) |
| 1818 | OS << *ED; |
| 1819 | else |
| 1820 | continue; |
| 1821 | } else { |
| 1822 | OS << *cast<NamedDecl>(Val: DC); |
| 1823 | } |
| 1824 | OS << "::" ; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | void NamedDecl::getNameForDiagnostic(raw_ostream &OS, |
| 1829 | const PrintingPolicy &Policy, |
| 1830 | bool Qualified) const { |
| 1831 | if (Qualified) |
| 1832 | printQualifiedName(OS, P: Policy); |
| 1833 | else |
| 1834 | printName(OS, Policy); |
| 1835 | } |
| 1836 | |
| 1837 | template<typename T> static bool isRedeclarableImpl(Redeclarable<T> *) { |
| 1838 | return true; |
| 1839 | } |
| 1840 | static bool isRedeclarableImpl(...) { return false; } |
| 1841 | static bool isRedeclarable(Decl::Kind K) { |
| 1842 | switch (K) { |
| 1843 | #define DECL(Type, Base) \ |
| 1844 | case Decl::Type: \ |
| 1845 | return isRedeclarableImpl((Type##Decl *)nullptr); |
| 1846 | #define ABSTRACT_DECL(DECL) |
| 1847 | #include "clang/AST/DeclNodes.inc" |
| 1848 | } |
| 1849 | llvm_unreachable("unknown decl kind" ); |
| 1850 | } |
| 1851 | |
| 1852 | bool NamedDecl::declarationReplaces(const NamedDecl *OldD, |
| 1853 | bool IsKnownNewer) const { |
| 1854 | assert(getDeclName() == OldD->getDeclName() && "Declaration name mismatch" ); |
| 1855 | |
| 1856 | // Never replace one imported declaration with another; we need both results |
| 1857 | // when re-exporting. |
| 1858 | if (OldD->isFromASTFile() && isFromASTFile()) |
| 1859 | return false; |
| 1860 | |
| 1861 | // A kind mismatch implies that the declaration is not replaced. |
| 1862 | if (OldD->getKind() != getKind()) |
| 1863 | return false; |
| 1864 | |
| 1865 | // For method declarations, we never replace. (Why?) |
| 1866 | if (isa<ObjCMethodDecl>(Val: this)) |
| 1867 | return false; |
| 1868 | |
| 1869 | // For parameters, pick the newer one. This is either an error or (in |
| 1870 | // Objective-C) permitted as an extension. |
| 1871 | if (isa<ParmVarDecl>(Val: this)) |
| 1872 | return true; |
| 1873 | |
| 1874 | // Inline namespaces can give us two declarations with the same |
| 1875 | // name and kind in the same scope but different contexts; we should |
| 1876 | // keep both declarations in this case. |
| 1877 | if (!this->getDeclContext()->getRedeclContext()->Equals( |
| 1878 | DC: OldD->getDeclContext()->getRedeclContext())) |
| 1879 | return false; |
| 1880 | |
| 1881 | // Using declarations can be replaced if they import the same name from the |
| 1882 | // same context. |
| 1883 | if (const auto *UD = dyn_cast<UsingDecl>(Val: this)) { |
| 1884 | ASTContext &Context = getASTContext(); |
| 1885 | return Context.getCanonicalNestedNameSpecifier(NNS: UD->getQualifier()) == |
| 1886 | Context.getCanonicalNestedNameSpecifier( |
| 1887 | NNS: cast<UsingDecl>(Val: OldD)->getQualifier()); |
| 1888 | } |
| 1889 | if (const auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(Val: this)) { |
| 1890 | ASTContext &Context = getASTContext(); |
| 1891 | return Context.getCanonicalNestedNameSpecifier(NNS: UUVD->getQualifier()) == |
| 1892 | Context.getCanonicalNestedNameSpecifier( |
| 1893 | NNS: cast<UnresolvedUsingValueDecl>(Val: OldD)->getQualifier()); |
| 1894 | } |
| 1895 | |
| 1896 | if (isRedeclarable(K: getKind())) { |
| 1897 | if (getCanonicalDecl() != OldD->getCanonicalDecl()) |
| 1898 | return false; |
| 1899 | |
| 1900 | if (IsKnownNewer) |
| 1901 | return true; |
| 1902 | |
| 1903 | // Check whether this is actually newer than OldD. We want to keep the |
| 1904 | // newer declaration. This loop will usually only iterate once, because |
| 1905 | // OldD is usually the previous declaration. |
| 1906 | for (const auto *D : redecls()) { |
| 1907 | if (D == OldD) |
| 1908 | break; |
| 1909 | |
| 1910 | // If we reach the canonical declaration, then OldD is not actually older |
| 1911 | // than this one. |
| 1912 | // |
| 1913 | // FIXME: In this case, we should not add this decl to the lookup table. |
| 1914 | if (D->isCanonicalDecl()) |
| 1915 | return false; |
| 1916 | } |
| 1917 | |
| 1918 | // It's a newer declaration of the same kind of declaration in the same |
| 1919 | // scope: we want this decl instead of the existing one. |
| 1920 | return true; |
| 1921 | } |
| 1922 | |
| 1923 | // In all other cases, we need to keep both declarations in case they have |
| 1924 | // different visibility. Any attempt to use the name will result in an |
| 1925 | // ambiguity if more than one is visible. |
| 1926 | return false; |
| 1927 | } |
| 1928 | |
| 1929 | bool NamedDecl::hasLinkage() const { |
| 1930 | switch (getFormalLinkage()) { |
| 1931 | case Linkage::Invalid: |
| 1932 | llvm_unreachable("Linkage hasn't been computed!" ); |
| 1933 | case Linkage::None: |
| 1934 | return false; |
| 1935 | case Linkage::Internal: |
| 1936 | return true; |
| 1937 | case Linkage::UniqueExternal: |
| 1938 | case Linkage::VisibleNone: |
| 1939 | llvm_unreachable("Non-formal linkage is not allowed here!" ); |
| 1940 | case Linkage::Module: |
| 1941 | case Linkage::External: |
| 1942 | return true; |
| 1943 | } |
| 1944 | llvm_unreachable("Unhandled Linkage enum" ); |
| 1945 | } |
| 1946 | |
| 1947 | NamedDecl *NamedDecl::getUnderlyingDeclImpl() { |
| 1948 | NamedDecl *ND = this; |
| 1949 | if (auto *UD = dyn_cast<UsingShadowDecl>(Val: ND)) |
| 1950 | ND = UD->getTargetDecl(); |
| 1951 | |
| 1952 | if (auto *AD = dyn_cast<ObjCCompatibleAliasDecl>(Val: ND)) |
| 1953 | return AD->getClassInterface(); |
| 1954 | |
| 1955 | if (auto *AD = dyn_cast<NamespaceAliasDecl>(Val: ND)) |
| 1956 | return AD->getNamespace(); |
| 1957 | |
| 1958 | return ND; |
| 1959 | } |
| 1960 | |
| 1961 | bool NamedDecl::isCXXInstanceMember() const { |
| 1962 | if (!isCXXClassMember()) |
| 1963 | return false; |
| 1964 | |
| 1965 | const NamedDecl *D = this; |
| 1966 | if (isa<UsingShadowDecl>(Val: D)) |
| 1967 | D = cast<UsingShadowDecl>(Val: D)->getTargetDecl(); |
| 1968 | |
| 1969 | if (isa<FieldDecl>(Val: D) || isa<IndirectFieldDecl>(Val: D) || isa<MSPropertyDecl>(Val: D)) |
| 1970 | return true; |
| 1971 | if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(Val: D->getAsFunction())) |
| 1972 | return MD->isInstance(); |
| 1973 | return false; |
| 1974 | } |
| 1975 | |
| 1976 | //===----------------------------------------------------------------------===// |
| 1977 | // DeclaratorDecl Implementation |
| 1978 | //===----------------------------------------------------------------------===// |
| 1979 | |
| 1980 | template <typename DeclT> |
| 1981 | static SourceLocation getTemplateOrInnerLocStart(const DeclT *decl) { |
| 1982 | if (decl->getNumTemplateParameterLists() > 0) |
| 1983 | return decl->getTemplateParameterList(0)->getTemplateLoc(); |
| 1984 | return decl->getInnerLocStart(); |
| 1985 | } |
| 1986 | |
| 1987 | SourceLocation DeclaratorDecl::getTypeSpecStartLoc() const { |
| 1988 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1989 | if (TSI) return TSI->getTypeLoc().getBeginLoc(); |
| 1990 | return SourceLocation(); |
| 1991 | } |
| 1992 | |
| 1993 | SourceLocation DeclaratorDecl::getTypeSpecEndLoc() const { |
| 1994 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 1995 | if (TSI) return TSI->getTypeLoc().getEndLoc(); |
| 1996 | return SourceLocation(); |
| 1997 | } |
| 1998 | |
| 1999 | void DeclaratorDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 2000 | if (QualifierLoc) { |
| 2001 | // Make sure the extended decl info is allocated. |
| 2002 | if (!hasExtInfo()) { |
| 2003 | // Save (non-extended) type source info pointer. |
| 2004 | auto *savedTInfo = cast<TypeSourceInfo *>(Val&: DeclInfo); |
| 2005 | // Allocate external info struct. |
| 2006 | DeclInfo = new (getASTContext()) ExtInfo; |
| 2007 | // Restore savedTInfo into (extended) decl info. |
| 2008 | getExtInfo()->TInfo = savedTInfo; |
| 2009 | } |
| 2010 | // Set qualifier info. |
| 2011 | getExtInfo()->QualifierLoc = QualifierLoc; |
| 2012 | } else if (hasExtInfo()) { |
| 2013 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 2014 | getExtInfo()->QualifierLoc = QualifierLoc; |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | void DeclaratorDecl::setTrailingRequiresClause(const AssociatedConstraint &AC) { |
| 2019 | assert(AC); |
| 2020 | // Make sure the extended decl info is allocated. |
| 2021 | if (!hasExtInfo()) { |
| 2022 | // Save (non-extended) type source info pointer. |
| 2023 | auto *savedTInfo = cast<TypeSourceInfo *>(Val&: DeclInfo); |
| 2024 | // Allocate external info struct. |
| 2025 | DeclInfo = new (getASTContext()) ExtInfo; |
| 2026 | // Restore savedTInfo into (extended) decl info. |
| 2027 | getExtInfo()->TInfo = savedTInfo; |
| 2028 | } |
| 2029 | // Set requires clause info. |
| 2030 | getExtInfo()->TrailingRequiresClause = AC; |
| 2031 | } |
| 2032 | |
| 2033 | void DeclaratorDecl::setTemplateParameterListsInfo( |
| 2034 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 2035 | assert(!TPLists.empty()); |
| 2036 | // Make sure the extended decl info is allocated. |
| 2037 | if (!hasExtInfo()) { |
| 2038 | // Save (non-extended) type source info pointer. |
| 2039 | auto *savedTInfo = cast<TypeSourceInfo *>(Val&: DeclInfo); |
| 2040 | // Allocate external info struct. |
| 2041 | DeclInfo = new (getASTContext()) ExtInfo; |
| 2042 | // Restore savedTInfo into (extended) decl info. |
| 2043 | getExtInfo()->TInfo = savedTInfo; |
| 2044 | } |
| 2045 | // Set the template parameter lists info. |
| 2046 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
| 2047 | } |
| 2048 | |
| 2049 | SourceLocation DeclaratorDecl::getOuterLocStart() const { |
| 2050 | return getTemplateOrInnerLocStart(decl: this); |
| 2051 | } |
| 2052 | |
| 2053 | // Helper function: returns true if QT is or contains a type |
| 2054 | // having a postfix component. |
| 2055 | static bool typeIsPostfix(QualType QT) { |
| 2056 | while (true) { |
| 2057 | const Type* T = QT.getTypePtr(); |
| 2058 | switch (T->getTypeClass()) { |
| 2059 | default: |
| 2060 | return false; |
| 2061 | case Type::Pointer: |
| 2062 | QT = cast<PointerType>(Val: T)->getPointeeType(); |
| 2063 | break; |
| 2064 | case Type::BlockPointer: |
| 2065 | QT = cast<BlockPointerType>(Val: T)->getPointeeType(); |
| 2066 | break; |
| 2067 | case Type::MemberPointer: |
| 2068 | QT = cast<MemberPointerType>(Val: T)->getPointeeType(); |
| 2069 | break; |
| 2070 | case Type::LValueReference: |
| 2071 | case Type::RValueReference: |
| 2072 | QT = cast<ReferenceType>(Val: T)->getPointeeType(); |
| 2073 | break; |
| 2074 | case Type::PackExpansion: |
| 2075 | QT = cast<PackExpansionType>(Val: T)->getPattern(); |
| 2076 | break; |
| 2077 | case Type::Paren: |
| 2078 | case Type::ConstantArray: |
| 2079 | case Type::DependentSizedArray: |
| 2080 | case Type::IncompleteArray: |
| 2081 | case Type::VariableArray: |
| 2082 | case Type::FunctionProto: |
| 2083 | case Type::FunctionNoProto: |
| 2084 | return true; |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | SourceRange DeclaratorDecl::getSourceRange() const { |
| 2090 | SourceLocation RangeEnd = getLocation(); |
| 2091 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 2092 | // If the declaration has no name or the type extends past the name take the |
| 2093 | // end location of the type. |
| 2094 | if (!getDeclName() || typeIsPostfix(QT: TInfo->getType())) |
| 2095 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 2096 | } |
| 2097 | return SourceRange(getOuterLocStart(), RangeEnd); |
| 2098 | } |
| 2099 | |
| 2100 | void QualifierInfo::setTemplateParameterListsInfo( |
| 2101 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 2102 | // Free previous template parameters (if any). |
| 2103 | if (NumTemplParamLists > 0) { |
| 2104 | Context.Deallocate(Ptr: TemplParamLists); |
| 2105 | TemplParamLists = nullptr; |
| 2106 | NumTemplParamLists = 0; |
| 2107 | } |
| 2108 | // Set info on matched template parameter lists (if any). |
| 2109 | if (!TPLists.empty()) { |
| 2110 | TemplParamLists = new (Context) TemplateParameterList *[TPLists.size()]; |
| 2111 | NumTemplParamLists = TPLists.size(); |
| 2112 | llvm::copy(Range&: TPLists, Out: TemplParamLists); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | //===----------------------------------------------------------------------===// |
| 2117 | // VarDecl Implementation |
| 2118 | //===----------------------------------------------------------------------===// |
| 2119 | |
| 2120 | const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { |
| 2121 | switch (SC) { |
| 2122 | case SC_None: break; |
| 2123 | case SC_Auto: return "auto" ; |
| 2124 | case SC_Extern: return "extern" ; |
| 2125 | case SC_PrivateExtern: return "__private_extern__" ; |
| 2126 | case SC_Register: return "register" ; |
| 2127 | case SC_Static: return "static" ; |
| 2128 | } |
| 2129 | |
| 2130 | llvm_unreachable("Invalid storage class" ); |
| 2131 | } |
| 2132 | |
| 2133 | VarDecl::VarDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 2134 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2135 | const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, |
| 2136 | StorageClass SC) |
| 2137 | : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), |
| 2138 | redeclarable_base(C) { |
| 2139 | static_assert(sizeof(VarDeclBitfields) <= sizeof(unsigned), |
| 2140 | "VarDeclBitfields too large!" ); |
| 2141 | static_assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned), |
| 2142 | "ParmVarDeclBitfields too large!" ); |
| 2143 | static_assert(sizeof(NonParmVarDeclBitfields) <= sizeof(unsigned), |
| 2144 | "NonParmVarDeclBitfields too large!" ); |
| 2145 | AllBits = 0; |
| 2146 | VarDeclBits.SClass = SC; |
| 2147 | // Everything else is implicitly initialized to false. |
| 2148 | } |
| 2149 | |
| 2150 | VarDecl *VarDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation StartL, |
| 2151 | SourceLocation IdL, const IdentifierInfo *Id, |
| 2152 | QualType T, TypeSourceInfo *TInfo, StorageClass S) { |
| 2153 | return new (C, DC) VarDecl(Var, C, DC, StartL, IdL, Id, T, TInfo, S); |
| 2154 | } |
| 2155 | |
| 2156 | VarDecl *VarDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 2157 | return new (C, ID) |
| 2158 | VarDecl(Var, C, nullptr, SourceLocation(), SourceLocation(), nullptr, |
| 2159 | QualType(), nullptr, SC_None); |
| 2160 | } |
| 2161 | |
| 2162 | void VarDecl::setStorageClass(StorageClass SC) { |
| 2163 | assert(isLegalForVariable(SC)); |
| 2164 | VarDeclBits.SClass = SC; |
| 2165 | } |
| 2166 | |
| 2167 | VarDecl::TLSKind VarDecl::getTLSKind() const { |
| 2168 | switch (VarDeclBits.TSCSpec) { |
| 2169 | case TSCS_unspecified: |
| 2170 | if (!hasAttr<ThreadAttr>() && |
| 2171 | !(getASTContext().getLangOpts().OpenMPUseTLS && |
| 2172 | getASTContext().getTargetInfo().isTLSSupported() && |
| 2173 | hasAttr<OMPThreadPrivateDeclAttr>())) |
| 2174 | return TLS_None; |
| 2175 | return ((getASTContext().getLangOpts().isCompatibleWithMSVC( |
| 2176 | MajorVersion: LangOptions::MSVC2015)) || |
| 2177 | hasAttr<OMPThreadPrivateDeclAttr>()) |
| 2178 | ? TLS_Dynamic |
| 2179 | : TLS_Static; |
| 2180 | case TSCS___thread: // Fall through. |
| 2181 | case TSCS__Thread_local: |
| 2182 | return TLS_Static; |
| 2183 | case TSCS_thread_local: |
| 2184 | return TLS_Dynamic; |
| 2185 | } |
| 2186 | llvm_unreachable("Unknown thread storage class specifier!" ); |
| 2187 | } |
| 2188 | |
| 2189 | SourceRange VarDecl::getSourceRange() const { |
| 2190 | if (const Expr *Init = getInit()) { |
| 2191 | SourceLocation InitEnd = Init->getEndLoc(); |
| 2192 | // If Init is implicit, ignore its source range and fallback on |
| 2193 | // DeclaratorDecl::getSourceRange() to handle postfix elements. |
| 2194 | if (InitEnd.isValid() && InitEnd != getLocation()) |
| 2195 | return SourceRange(getOuterLocStart(), InitEnd); |
| 2196 | } |
| 2197 | return DeclaratorDecl::getSourceRange(); |
| 2198 | } |
| 2199 | |
| 2200 | template<typename T> |
| 2201 | static LanguageLinkage getDeclLanguageLinkage(const T &D) { |
| 2202 | // C++ [dcl.link]p1: All function types, function names with external linkage, |
| 2203 | // and variable names with external linkage have a language linkage. |
| 2204 | if (!D.hasExternalFormalLinkage()) |
| 2205 | return NoLanguageLinkage; |
| 2206 | |
| 2207 | // Language linkage is a C++ concept, but saying that everything else in C has |
| 2208 | // C language linkage fits the implementation nicely. |
| 2209 | if (!D.getASTContext().getLangOpts().CPlusPlus) |
| 2210 | return CLanguageLinkage; |
| 2211 | |
| 2212 | // C++ [dcl.link]p4: A C language linkage is ignored in determining the |
| 2213 | // language linkage of the names of class members and the function type of |
| 2214 | // class member functions. |
| 2215 | const DeclContext *DC = D.getDeclContext(); |
| 2216 | if (DC->isRecord()) |
| 2217 | return CXXLanguageLinkage; |
| 2218 | |
| 2219 | // If the first decl is in an extern "C" context, any other redeclaration |
| 2220 | // will have C language linkage. If the first one is not in an extern "C" |
| 2221 | // context, we would have reported an error for any other decl being in one. |
| 2222 | if (isFirstInExternCContext(&D)) |
| 2223 | return CLanguageLinkage; |
| 2224 | return CXXLanguageLinkage; |
| 2225 | } |
| 2226 | |
| 2227 | template<typename T> |
| 2228 | static bool isDeclExternC(const T &D) { |
| 2229 | // Since the context is ignored for class members, they can only have C++ |
| 2230 | // language linkage or no language linkage. |
| 2231 | const DeclContext *DC = D.getDeclContext(); |
| 2232 | if (DC->isRecord()) { |
| 2233 | assert(D.getASTContext().getLangOpts().CPlusPlus); |
| 2234 | return false; |
| 2235 | } |
| 2236 | |
| 2237 | return D.getLanguageLinkage() == CLanguageLinkage; |
| 2238 | } |
| 2239 | |
| 2240 | LanguageLinkage VarDecl::getLanguageLinkage() const { |
| 2241 | return getDeclLanguageLinkage(D: *this); |
| 2242 | } |
| 2243 | |
| 2244 | bool VarDecl::isExternC() const { |
| 2245 | return isDeclExternC(D: *this); |
| 2246 | } |
| 2247 | |
| 2248 | bool VarDecl::isInExternCContext() const { |
| 2249 | return getLexicalDeclContext()->isExternCContext(); |
| 2250 | } |
| 2251 | |
| 2252 | bool VarDecl::isInExternCXXContext() const { |
| 2253 | return getLexicalDeclContext()->isExternCXXContext(); |
| 2254 | } |
| 2255 | |
| 2256 | VarDecl *VarDecl::getCanonicalDecl() { return getFirstDecl(); } |
| 2257 | |
| 2258 | VarDecl::DefinitionKind |
| 2259 | VarDecl::isThisDeclarationADefinition(ASTContext &C) const { |
| 2260 | if (isThisDeclarationADemotedDefinition()) |
| 2261 | return DeclarationOnly; |
| 2262 | |
| 2263 | // C++ [basic.def]p2: |
| 2264 | // A declaration is a definition unless [...] it contains the 'extern' |
| 2265 | // specifier or a linkage-specification and neither an initializer [...], |
| 2266 | // it declares a non-inline static data member in a class declaration [...], |
| 2267 | // it declares a static data member outside a class definition and the variable |
| 2268 | // was defined within the class with the constexpr specifier [...], |
| 2269 | // C++1y [temp.expl.spec]p15: |
| 2270 | // An explicit specialization of a static data member or an explicit |
| 2271 | // specialization of a static data member template is a definition if the |
| 2272 | // declaration includes an initializer; otherwise, it is a declaration. |
| 2273 | // |
| 2274 | // FIXME: How do you declare (but not define) a partial specialization of |
| 2275 | // a static data member template outside the containing class? |
| 2276 | if (isStaticDataMember()) { |
| 2277 | if (isOutOfLine() && |
| 2278 | !(getCanonicalDecl()->isInline() && |
| 2279 | getCanonicalDecl()->isConstexpr()) && |
| 2280 | (hasInit() || |
| 2281 | // If the first declaration is out-of-line, this may be an |
| 2282 | // instantiation of an out-of-line partial specialization of a variable |
| 2283 | // template for which we have not yet instantiated the initializer. |
| 2284 | (getFirstDecl()->isOutOfLine() |
| 2285 | ? getTemplateSpecializationKind() == TSK_Undeclared |
| 2286 | : getTemplateSpecializationKind() != |
| 2287 | TSK_ExplicitSpecialization) || |
| 2288 | isa<VarTemplatePartialSpecializationDecl>(Val: this))) |
| 2289 | return Definition; |
| 2290 | if (!isOutOfLine() && isInline()) |
| 2291 | return Definition; |
| 2292 | return DeclarationOnly; |
| 2293 | } |
| 2294 | // C99 6.7p5: |
| 2295 | // A definition of an identifier is a declaration for that identifier that |
| 2296 | // [...] causes storage to be reserved for that object. |
| 2297 | // Note: that applies for all non-file-scope objects. |
| 2298 | // C99 6.9.2p1: |
| 2299 | // If the declaration of an identifier for an object has file scope and an |
| 2300 | // initializer, the declaration is an external definition for the identifier |
| 2301 | if (hasInit()) |
| 2302 | return Definition; |
| 2303 | |
| 2304 | if (hasDefiningAttr()) |
| 2305 | return Definition; |
| 2306 | |
| 2307 | if (const auto *SAA = getAttr<SelectAnyAttr>()) |
| 2308 | if (!SAA->isInherited()) |
| 2309 | return Definition; |
| 2310 | |
| 2311 | // A variable template specialization (other than a static data member |
| 2312 | // template or an explicit specialization) is a declaration until we |
| 2313 | // instantiate its initializer. |
| 2314 | if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Val: this)) { |
| 2315 | if (VTSD->getTemplateSpecializationKind() != TSK_ExplicitSpecialization && |
| 2316 | !isa<VarTemplatePartialSpecializationDecl>(Val: VTSD) && |
| 2317 | !VTSD->IsCompleteDefinition) |
| 2318 | return DeclarationOnly; |
| 2319 | } |
| 2320 | |
| 2321 | if (hasExternalStorage()) |
| 2322 | return DeclarationOnly; |
| 2323 | |
| 2324 | // [dcl.link] p7: |
| 2325 | // A declaration directly contained in a linkage-specification is treated |
| 2326 | // as if it contains the extern specifier for the purpose of determining |
| 2327 | // the linkage of the declared name and whether it is a definition. |
| 2328 | if (isSingleLineLanguageLinkage(D: *this)) |
| 2329 | return DeclarationOnly; |
| 2330 | |
| 2331 | // C99 6.9.2p2: |
| 2332 | // A declaration of an object that has file scope without an initializer, |
| 2333 | // and without a storage class specifier or the scs 'static', constitutes |
| 2334 | // a tentative definition. |
| 2335 | // No such thing in C++. |
| 2336 | if (!C.getLangOpts().CPlusPlus && isFileVarDecl()) |
| 2337 | return TentativeDefinition; |
| 2338 | |
| 2339 | // What's left is (in C, block-scope) declarations without initializers or |
| 2340 | // external storage. These are definitions. |
| 2341 | return Definition; |
| 2342 | } |
| 2343 | |
| 2344 | VarDecl *VarDecl::getActingDefinition() { |
| 2345 | DefinitionKind Kind = isThisDeclarationADefinition(); |
| 2346 | if (Kind != TentativeDefinition) |
| 2347 | return nullptr; |
| 2348 | |
| 2349 | VarDecl *LastTentative = nullptr; |
| 2350 | |
| 2351 | // Loop through the declaration chain, starting with the most recent. |
| 2352 | for (VarDecl *Decl = getMostRecentDecl(); Decl; |
| 2353 | Decl = Decl->getPreviousDecl()) { |
| 2354 | Kind = Decl->isThisDeclarationADefinition(); |
| 2355 | if (Kind == Definition) |
| 2356 | return nullptr; |
| 2357 | // Record the first (most recent) TentativeDefinition that is encountered. |
| 2358 | if (Kind == TentativeDefinition && !LastTentative) |
| 2359 | LastTentative = Decl; |
| 2360 | } |
| 2361 | |
| 2362 | return LastTentative; |
| 2363 | } |
| 2364 | |
| 2365 | VarDecl *VarDecl::getDefinition(ASTContext &C) { |
| 2366 | VarDecl *First = getFirstDecl(); |
| 2367 | for (auto *I : First->redecls()) { |
| 2368 | if (I->isThisDeclarationADefinition(C) == Definition) |
| 2369 | return I; |
| 2370 | } |
| 2371 | return nullptr; |
| 2372 | } |
| 2373 | |
| 2374 | VarDecl::DefinitionKind VarDecl::hasDefinition(ASTContext &C) const { |
| 2375 | DefinitionKind Kind = DeclarationOnly; |
| 2376 | |
| 2377 | const VarDecl *First = getFirstDecl(); |
| 2378 | for (auto *I : First->redecls()) { |
| 2379 | Kind = std::max(a: Kind, b: I->isThisDeclarationADefinition(C)); |
| 2380 | if (Kind == Definition) |
| 2381 | break; |
| 2382 | } |
| 2383 | |
| 2384 | return Kind; |
| 2385 | } |
| 2386 | |
| 2387 | const Expr *VarDecl::getAnyInitializer(const VarDecl *&D) const { |
| 2388 | for (auto *I : redecls()) { |
| 2389 | if (auto Expr = I->getInit()) { |
| 2390 | D = I; |
| 2391 | return Expr; |
| 2392 | } |
| 2393 | } |
| 2394 | return nullptr; |
| 2395 | } |
| 2396 | |
| 2397 | bool VarDecl::hasInit() const { |
| 2398 | if (auto *P = dyn_cast<ParmVarDecl>(Val: this)) |
| 2399 | if (P->hasUnparsedDefaultArg() || P->hasUninstantiatedDefaultArg()) |
| 2400 | return false; |
| 2401 | |
| 2402 | if (auto *Eval = getEvaluatedStmt()) |
| 2403 | return Eval->Value.isValid(); |
| 2404 | |
| 2405 | return !Init.isNull(); |
| 2406 | } |
| 2407 | |
| 2408 | Expr *VarDecl::getInit() { |
| 2409 | if (!hasInit()) |
| 2410 | return nullptr; |
| 2411 | |
| 2412 | if (auto *S = dyn_cast<Stmt *>(Val&: Init)) |
| 2413 | return cast<Expr>(Val: S); |
| 2414 | |
| 2415 | auto *Eval = getEvaluatedStmt(); |
| 2416 | |
| 2417 | return cast<Expr>(Val: Eval->Value.get( |
| 2418 | Source: Eval->Value.isOffset() ? getASTContext().getExternalSource() : nullptr)); |
| 2419 | } |
| 2420 | |
| 2421 | Stmt **VarDecl::getInitAddress() { |
| 2422 | if (auto *ES = Init.dyn_cast<EvaluatedStmt *>()) |
| 2423 | return ES->Value.getAddressOfPointer(Source: getASTContext().getExternalSource()); |
| 2424 | |
| 2425 | return Init.getAddrOfPtr1(); |
| 2426 | } |
| 2427 | |
| 2428 | VarDecl *VarDecl::getInitializingDeclaration() { |
| 2429 | VarDecl *Def = nullptr; |
| 2430 | for (auto *I : redecls()) { |
| 2431 | if (I->hasInit()) |
| 2432 | return I; |
| 2433 | |
| 2434 | if (I->isThisDeclarationADefinition()) { |
| 2435 | if (isStaticDataMember()) |
| 2436 | return I; |
| 2437 | Def = I; |
| 2438 | } |
| 2439 | } |
| 2440 | return Def; |
| 2441 | } |
| 2442 | |
| 2443 | bool VarDecl::hasInitWithSideEffects() const { |
| 2444 | if (!hasInit()) |
| 2445 | return false; |
| 2446 | |
| 2447 | EvaluatedStmt *ES = ensureEvaluatedStmt(); |
| 2448 | if (!ES->CheckedForSideEffects) { |
| 2449 | const Expr *E = getInit(); |
| 2450 | ES->HasSideEffects = |
| 2451 | E->HasSideEffects(Ctx: getASTContext()) && |
| 2452 | // We can get a value-dependent initializer during error recovery. |
| 2453 | (E->isValueDependent() || !evaluateValue()); |
| 2454 | ES->CheckedForSideEffects = true; |
| 2455 | } |
| 2456 | return ES->HasSideEffects; |
| 2457 | } |
| 2458 | |
| 2459 | bool VarDecl::isOutOfLine() const { |
| 2460 | if (Decl::isOutOfLine()) |
| 2461 | return true; |
| 2462 | |
| 2463 | if (!isStaticDataMember()) |
| 2464 | return false; |
| 2465 | |
| 2466 | // If this static data member was instantiated from a static data member of |
| 2467 | // a class template, check whether that static data member was defined |
| 2468 | // out-of-line. |
| 2469 | if (VarDecl *VD = getInstantiatedFromStaticDataMember()) |
| 2470 | return VD->isOutOfLine(); |
| 2471 | |
| 2472 | return false; |
| 2473 | } |
| 2474 | |
| 2475 | void VarDecl::setInit(Expr *I) { |
| 2476 | if (auto *Eval = dyn_cast_if_present<EvaluatedStmt *>(Val&: Init)) { |
| 2477 | Eval->~EvaluatedStmt(); |
| 2478 | getASTContext().Deallocate(Ptr: Eval); |
| 2479 | } |
| 2480 | |
| 2481 | Init = I; |
| 2482 | } |
| 2483 | |
| 2484 | bool VarDecl::mightBeUsableInConstantExpressions(const ASTContext &C) const { |
| 2485 | const LangOptions &Lang = C.getLangOpts(); |
| 2486 | |
| 2487 | // OpenCL permits const integral variables to be used in constant |
| 2488 | // expressions, like in C++98. |
| 2489 | if (!Lang.CPlusPlus && !Lang.OpenCL && !Lang.C23) |
| 2490 | return false; |
| 2491 | |
| 2492 | // Function parameters are never usable in constant expressions. |
| 2493 | if (isa<ParmVarDecl>(Val: this)) |
| 2494 | return false; |
| 2495 | |
| 2496 | // The values of weak variables are never usable in constant expressions. |
| 2497 | if (isWeak()) |
| 2498 | return false; |
| 2499 | |
| 2500 | // In C++11, any variable of reference type can be used in a constant |
| 2501 | // expression if it is initialized by a constant expression. |
| 2502 | if (Lang.CPlusPlus11 && getType()->isReferenceType()) |
| 2503 | return true; |
| 2504 | |
| 2505 | // Only const objects can be used in constant expressions in C++. C++98 does |
| 2506 | // not require the variable to be non-volatile, but we consider this to be a |
| 2507 | // defect. |
| 2508 | if (!getType().isConstant(Ctx: C) || getType().isVolatileQualified()) |
| 2509 | return false; |
| 2510 | |
| 2511 | // In C++, but not in C, const, non-volatile variables of integral or |
| 2512 | // enumeration types can be used in constant expressions. |
| 2513 | if (getType()->isIntegralOrEnumerationType() && !Lang.C23) |
| 2514 | return true; |
| 2515 | |
| 2516 | // C23 6.6p7: An identifier that is: |
| 2517 | // ... |
| 2518 | // - declared with storage-class specifier constexpr and has an object type, |
| 2519 | // is a named constant, ... such a named constant is a constant expression |
| 2520 | // with the type and value of the declared object. |
| 2521 | // Additionally, in C++11, non-volatile constexpr variables can be used in |
| 2522 | // constant expressions. |
| 2523 | return (Lang.CPlusPlus11 || Lang.C23) && isConstexpr(); |
| 2524 | } |
| 2525 | |
| 2526 | bool VarDecl::isUsableInConstantExpressions(const ASTContext &Context) const { |
| 2527 | // C++2a [expr.const]p3: |
| 2528 | // A variable is usable in constant expressions after its initializing |
| 2529 | // declaration is encountered... |
| 2530 | const VarDecl *DefVD = nullptr; |
| 2531 | const Expr *Init = getAnyInitializer(D&: DefVD); |
| 2532 | if (!Init || Init->isValueDependent() || getType()->isDependentType()) |
| 2533 | return false; |
| 2534 | // ... if it is a constexpr variable, or it is of reference type or of |
| 2535 | // const-qualified integral or enumeration type, ... |
| 2536 | if (!DefVD->mightBeUsableInConstantExpressions(C: Context)) |
| 2537 | return false; |
| 2538 | // ... and its initializer is a constant initializer. |
| 2539 | if ((Context.getLangOpts().CPlusPlus || getLangOpts().C23) && |
| 2540 | !DefVD->hasConstantInitialization()) |
| 2541 | return false; |
| 2542 | // C++98 [expr.const]p1: |
| 2543 | // An integral constant-expression can involve only [...] const variables |
| 2544 | // or static data members of integral or enumeration types initialized with |
| 2545 | // [integer] constant expressions (dcl.init) |
| 2546 | if ((Context.getLangOpts().CPlusPlus || Context.getLangOpts().OpenCL) && |
| 2547 | !Context.getLangOpts().CPlusPlus11 && !DefVD->hasICEInitializer(Context)) |
| 2548 | return false; |
| 2549 | return true; |
| 2550 | } |
| 2551 | |
| 2552 | /// Convert the initializer for this declaration to the elaborated EvaluatedStmt |
| 2553 | /// form, which contains extra information on the evaluated value of the |
| 2554 | /// initializer. |
| 2555 | EvaluatedStmt *VarDecl::ensureEvaluatedStmt() const { |
| 2556 | auto *Eval = dyn_cast_if_present<EvaluatedStmt *>(Val&: Init); |
| 2557 | if (!Eval) { |
| 2558 | // Note: EvaluatedStmt contains an APValue, which usually holds |
| 2559 | // resources not allocated from the ASTContext. We need to do some |
| 2560 | // work to avoid leaking those, but we do so in VarDecl::evaluateValue |
| 2561 | // where we can detect whether there's anything to clean up or not. |
| 2562 | Eval = new (getASTContext()) EvaluatedStmt; |
| 2563 | Eval->Value = cast<Stmt *>(Val&: Init); |
| 2564 | Init = Eval; |
| 2565 | } |
| 2566 | return Eval; |
| 2567 | } |
| 2568 | |
| 2569 | EvaluatedStmt *VarDecl::getEvaluatedStmt() const { |
| 2570 | return dyn_cast_if_present<EvaluatedStmt *>(Val&: Init); |
| 2571 | } |
| 2572 | |
| 2573 | APValue *VarDecl::evaluateValue() const { |
| 2574 | SmallVector<PartialDiagnosticAt, 8> Notes; |
| 2575 | return evaluateValueImpl(Notes, IsConstantInitialization: hasConstantInitialization()); |
| 2576 | } |
| 2577 | |
| 2578 | APValue *VarDecl::evaluateValueImpl(SmallVectorImpl<PartialDiagnosticAt> &Notes, |
| 2579 | bool IsConstantInitialization) const { |
| 2580 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2581 | |
| 2582 | const auto *Init = getInit(); |
| 2583 | assert(!Init->isValueDependent()); |
| 2584 | |
| 2585 | // We only produce notes indicating why an initializer is non-constant the |
| 2586 | // first time it is evaluated. FIXME: The notes won't always be emitted the |
| 2587 | // first time we try evaluation, so might not be produced at all. |
| 2588 | if (Eval->WasEvaluated) |
| 2589 | return Eval->Evaluated.isAbsent() ? nullptr : &Eval->Evaluated; |
| 2590 | |
| 2591 | if (Eval->IsEvaluating) { |
| 2592 | // FIXME: Produce a diagnostic for self-initialization. |
| 2593 | return nullptr; |
| 2594 | } |
| 2595 | |
| 2596 | Eval->IsEvaluating = true; |
| 2597 | |
| 2598 | ASTContext &Ctx = getASTContext(); |
| 2599 | bool Result = Init->EvaluateAsInitializer(Result&: Eval->Evaluated, Ctx, VD: this, Notes, |
| 2600 | IsConstantInitializer: IsConstantInitialization); |
| 2601 | |
| 2602 | // In C++, or in C23 if we're initialising a 'constexpr' variable, this isn't |
| 2603 | // a constant initializer if we produced notes. In that case, we can't keep |
| 2604 | // the result, because it may only be correct under the assumption that the |
| 2605 | // initializer is a constant context. |
| 2606 | if (IsConstantInitialization && |
| 2607 | (Ctx.getLangOpts().CPlusPlus || |
| 2608 | (isConstexpr() && Ctx.getLangOpts().C23)) && |
| 2609 | !Notes.empty()) |
| 2610 | Result = false; |
| 2611 | |
| 2612 | // Ensure the computed APValue is cleaned up later if evaluation succeeded, |
| 2613 | // or that it's empty (so that there's nothing to clean up) if evaluation |
| 2614 | // failed. |
| 2615 | if (!Result) |
| 2616 | Eval->Evaluated = APValue(); |
| 2617 | else if (Eval->Evaluated.needsCleanup()) |
| 2618 | Ctx.addDestruction(Ptr: &Eval->Evaluated); |
| 2619 | |
| 2620 | Eval->IsEvaluating = false; |
| 2621 | Eval->WasEvaluated = true; |
| 2622 | |
| 2623 | return Result ? &Eval->Evaluated : nullptr; |
| 2624 | } |
| 2625 | |
| 2626 | APValue *VarDecl::getEvaluatedValue() const { |
| 2627 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
| 2628 | if (Eval->WasEvaluated) |
| 2629 | return &Eval->Evaluated; |
| 2630 | |
| 2631 | return nullptr; |
| 2632 | } |
| 2633 | |
| 2634 | bool VarDecl::hasICEInitializer(const ASTContext &Context) const { |
| 2635 | const Expr *Init = getInit(); |
| 2636 | assert(Init && "no initializer" ); |
| 2637 | |
| 2638 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2639 | if (!Eval->CheckedForICEInit) { |
| 2640 | Eval->CheckedForICEInit = true; |
| 2641 | Eval->HasICEInit = Init->isIntegerConstantExpr(Ctx: Context); |
| 2642 | } |
| 2643 | return Eval->HasICEInit; |
| 2644 | } |
| 2645 | |
| 2646 | bool VarDecl::hasConstantInitialization() const { |
| 2647 | // In C, all globals and constexpr variables should have constant |
| 2648 | // initialization. For constexpr variables in C check that initializer is a |
| 2649 | // constant initializer because they can be used in constant expressions. |
| 2650 | if (hasGlobalStorage() && !getASTContext().getLangOpts().CPlusPlus && |
| 2651 | !isConstexpr()) |
| 2652 | return true; |
| 2653 | |
| 2654 | // In C++, it depends on whether the evaluation at the point of definition |
| 2655 | // was evaluatable as a constant initializer. |
| 2656 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
| 2657 | return Eval->HasConstantInitialization; |
| 2658 | |
| 2659 | return false; |
| 2660 | } |
| 2661 | |
| 2662 | bool VarDecl::checkForConstantInitialization( |
| 2663 | SmallVectorImpl<PartialDiagnosticAt> &Notes) const { |
| 2664 | EvaluatedStmt *Eval = ensureEvaluatedStmt(); |
| 2665 | // If we ask for the value before we know whether we have a constant |
| 2666 | // initializer, we can compute the wrong value (for example, due to |
| 2667 | // std::is_constant_evaluated()). |
| 2668 | assert(!Eval->WasEvaluated && |
| 2669 | "already evaluated var value before checking for constant init" ); |
| 2670 | assert((getASTContext().getLangOpts().CPlusPlus || |
| 2671 | getASTContext().getLangOpts().C23) && |
| 2672 | "only meaningful in C++/C23" ); |
| 2673 | |
| 2674 | assert(!getInit()->isValueDependent()); |
| 2675 | |
| 2676 | // Evaluate the initializer to check whether it's a constant expression. |
| 2677 | Eval->HasConstantInitialization = |
| 2678 | evaluateValueImpl(Notes, IsConstantInitialization: true) && Notes.empty(); |
| 2679 | |
| 2680 | // If evaluation as a constant initializer failed, allow re-evaluation as a |
| 2681 | // non-constant initializer if we later find we want the value. |
| 2682 | if (!Eval->HasConstantInitialization) |
| 2683 | Eval->WasEvaluated = false; |
| 2684 | |
| 2685 | return Eval->HasConstantInitialization; |
| 2686 | } |
| 2687 | |
| 2688 | template<typename DeclT> |
| 2689 | static DeclT *getDefinitionOrSelf(DeclT *D) { |
| 2690 | assert(D); |
| 2691 | if (auto *Def = D->getDefinition()) |
| 2692 | return Def; |
| 2693 | return D; |
| 2694 | } |
| 2695 | |
| 2696 | bool VarDecl::isEscapingByref() const { |
| 2697 | return hasAttr<BlocksAttr>() && NonParmVarDeclBits.EscapingByref; |
| 2698 | } |
| 2699 | |
| 2700 | bool VarDecl::isNonEscapingByref() const { |
| 2701 | return hasAttr<BlocksAttr>() && !NonParmVarDeclBits.EscapingByref; |
| 2702 | } |
| 2703 | |
| 2704 | bool VarDecl::hasDependentAlignment() const { |
| 2705 | QualType T = getType(); |
| 2706 | return T->isDependentType() || T->isUndeducedType() || |
| 2707 | llvm::any_of(Range: specific_attrs<AlignedAttr>(), P: [](const AlignedAttr *AA) { |
| 2708 | return AA->isAlignmentDependent(); |
| 2709 | }); |
| 2710 | } |
| 2711 | |
| 2712 | VarDecl *VarDecl::getTemplateInstantiationPattern() const { |
| 2713 | const VarDecl *VD = this; |
| 2714 | |
| 2715 | // If this is an instantiated member, walk back to the template from which |
| 2716 | // it was instantiated. |
| 2717 | if (MemberSpecializationInfo *MSInfo = VD->getMemberSpecializationInfo()) { |
| 2718 | if (isTemplateInstantiation(Kind: MSInfo->getTemplateSpecializationKind())) { |
| 2719 | VD = VD->getInstantiatedFromStaticDataMember(); |
| 2720 | while (auto *NewVD = VD->getInstantiatedFromStaticDataMember()) |
| 2721 | VD = NewVD; |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | // If it's an instantiated variable template specialization, find the |
| 2726 | // template or partial specialization from which it was instantiated. |
| 2727 | if (auto *VDTemplSpec = dyn_cast<VarTemplateSpecializationDecl>(Val: VD)) { |
| 2728 | if (isTemplateInstantiation(Kind: VDTemplSpec->getTemplateSpecializationKind())) { |
| 2729 | auto From = VDTemplSpec->getInstantiatedFrom(); |
| 2730 | if (auto *VTD = From.dyn_cast<VarTemplateDecl *>()) { |
| 2731 | while (!VTD->isMemberSpecialization()) { |
| 2732 | auto *NewVTD = VTD->getInstantiatedFromMemberTemplate(); |
| 2733 | if (!NewVTD) |
| 2734 | break; |
| 2735 | VTD = NewVTD; |
| 2736 | } |
| 2737 | return getDefinitionOrSelf(D: VTD->getTemplatedDecl()); |
| 2738 | } |
| 2739 | if (auto *VTPSD = |
| 2740 | From.dyn_cast<VarTemplatePartialSpecializationDecl *>()) { |
| 2741 | while (!VTPSD->isMemberSpecialization()) { |
| 2742 | auto *NewVTPSD = VTPSD->getInstantiatedFromMember(); |
| 2743 | if (!NewVTPSD) |
| 2744 | break; |
| 2745 | VTPSD = NewVTPSD; |
| 2746 | } |
| 2747 | return getDefinitionOrSelf<VarDecl>(D: VTPSD); |
| 2748 | } |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | // If this is the pattern of a variable template, find where it was |
| 2753 | // instantiated from. FIXME: Is this necessary? |
| 2754 | if (VarTemplateDecl *VarTemplate = VD->getDescribedVarTemplate()) { |
| 2755 | while (!VarTemplate->isMemberSpecialization()) { |
| 2756 | auto *NewVT = VarTemplate->getInstantiatedFromMemberTemplate(); |
| 2757 | if (!NewVT) |
| 2758 | break; |
| 2759 | VarTemplate = NewVT; |
| 2760 | } |
| 2761 | |
| 2762 | return getDefinitionOrSelf(D: VarTemplate->getTemplatedDecl()); |
| 2763 | } |
| 2764 | |
| 2765 | if (VD == this) |
| 2766 | return nullptr; |
| 2767 | return getDefinitionOrSelf(D: const_cast<VarDecl*>(VD)); |
| 2768 | } |
| 2769 | |
| 2770 | VarDecl *VarDecl::getInstantiatedFromStaticDataMember() const { |
| 2771 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2772 | return cast<VarDecl>(Val: MSI->getInstantiatedFrom()); |
| 2773 | |
| 2774 | return nullptr; |
| 2775 | } |
| 2776 | |
| 2777 | TemplateSpecializationKind VarDecl::getTemplateSpecializationKind() const { |
| 2778 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(Val: this)) |
| 2779 | return Spec->getSpecializationKind(); |
| 2780 | |
| 2781 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2782 | return MSI->getTemplateSpecializationKind(); |
| 2783 | |
| 2784 | return TSK_Undeclared; |
| 2785 | } |
| 2786 | |
| 2787 | TemplateSpecializationKind |
| 2788 | VarDecl::getTemplateSpecializationKindForInstantiation() const { |
| 2789 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2790 | return MSI->getTemplateSpecializationKind(); |
| 2791 | |
| 2792 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(Val: this)) |
| 2793 | return Spec->getSpecializationKind(); |
| 2794 | |
| 2795 | return TSK_Undeclared; |
| 2796 | } |
| 2797 | |
| 2798 | SourceLocation VarDecl::getPointOfInstantiation() const { |
| 2799 | if (const auto *Spec = dyn_cast<VarTemplateSpecializationDecl>(Val: this)) |
| 2800 | return Spec->getPointOfInstantiation(); |
| 2801 | |
| 2802 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 2803 | return MSI->getPointOfInstantiation(); |
| 2804 | |
| 2805 | return SourceLocation(); |
| 2806 | } |
| 2807 | |
| 2808 | VarTemplateDecl *VarDecl::getDescribedVarTemplate() const { |
| 2809 | return dyn_cast_if_present<VarTemplateDecl *>( |
| 2810 | Val: getASTContext().getTemplateOrSpecializationInfo(Var: this)); |
| 2811 | } |
| 2812 | |
| 2813 | void VarDecl::setDescribedVarTemplate(VarTemplateDecl *Template) { |
| 2814 | getASTContext().setTemplateOrSpecializationInfo(Inst: this, TSI: Template); |
| 2815 | } |
| 2816 | |
| 2817 | bool VarDecl::isKnownToBeDefined() const { |
| 2818 | const auto &LangOpts = getASTContext().getLangOpts(); |
| 2819 | // In CUDA mode without relocatable device code, variables of form 'extern |
| 2820 | // __shared__ Foo foo[]' are pointers to the base of the GPU core's shared |
| 2821 | // memory pool. These are never undefined variables, even if they appear |
| 2822 | // inside of an anon namespace or static function. |
| 2823 | // |
| 2824 | // With CUDA relocatable device code enabled, these variables don't get |
| 2825 | // special handling; they're treated like regular extern variables. |
| 2826 | if (LangOpts.CUDA && !LangOpts.GPURelocatableDeviceCode && |
| 2827 | hasExternalStorage() && hasAttr<CUDASharedAttr>() && |
| 2828 | isa<IncompleteArrayType>(Val: getType())) |
| 2829 | return true; |
| 2830 | |
| 2831 | return hasDefinition(); |
| 2832 | } |
| 2833 | |
| 2834 | bool VarDecl::isNoDestroy(const ASTContext &Ctx) const { |
| 2835 | if (!hasGlobalStorage()) |
| 2836 | return false; |
| 2837 | if (hasAttr<NoDestroyAttr>()) |
| 2838 | return true; |
| 2839 | if (hasAttr<AlwaysDestroyAttr>()) |
| 2840 | return false; |
| 2841 | |
| 2842 | using RSDKind = LangOptions::RegisterStaticDestructorsKind; |
| 2843 | RSDKind K = Ctx.getLangOpts().getRegisterStaticDestructors(); |
| 2844 | return K == RSDKind::None || |
| 2845 | (K == RSDKind::ThreadLocal && getTLSKind() == TLS_None); |
| 2846 | } |
| 2847 | |
| 2848 | QualType::DestructionKind |
| 2849 | VarDecl::needsDestruction(const ASTContext &Ctx) const { |
| 2850 | if (EvaluatedStmt *Eval = getEvaluatedStmt()) |
| 2851 | if (Eval->HasConstantDestruction) |
| 2852 | return QualType::DK_none; |
| 2853 | |
| 2854 | if (isNoDestroy(Ctx)) |
| 2855 | return QualType::DK_none; |
| 2856 | |
| 2857 | return getType().isDestructedType(); |
| 2858 | } |
| 2859 | |
| 2860 | bool VarDecl::hasFlexibleArrayInit(const ASTContext &Ctx) const { |
| 2861 | assert(hasInit() && "Expect initializer to check for flexible array init" ); |
| 2862 | auto *Ty = getType()->getAs<RecordType>(); |
| 2863 | if (!Ty || !Ty->getDecl()->hasFlexibleArrayMember()) |
| 2864 | return false; |
| 2865 | auto *List = dyn_cast<InitListExpr>(Val: getInit()->IgnoreParens()); |
| 2866 | if (!List) |
| 2867 | return false; |
| 2868 | const Expr *FlexibleInit = List->getInit(Init: List->getNumInits() - 1); |
| 2869 | auto InitTy = Ctx.getAsConstantArrayType(T: FlexibleInit->getType()); |
| 2870 | if (!InitTy) |
| 2871 | return false; |
| 2872 | return !InitTy->isZeroSize(); |
| 2873 | } |
| 2874 | |
| 2875 | CharUnits VarDecl::getFlexibleArrayInitChars(const ASTContext &Ctx) const { |
| 2876 | assert(hasInit() && "Expect initializer to check for flexible array init" ); |
| 2877 | auto *Ty = getType()->getAs<RecordType>(); |
| 2878 | if (!Ty || !Ty->getDecl()->hasFlexibleArrayMember()) |
| 2879 | return CharUnits::Zero(); |
| 2880 | auto *List = dyn_cast<InitListExpr>(Val: getInit()->IgnoreParens()); |
| 2881 | if (!List || List->getNumInits() == 0) |
| 2882 | return CharUnits::Zero(); |
| 2883 | const Expr *FlexibleInit = List->getInit(Init: List->getNumInits() - 1); |
| 2884 | auto InitTy = Ctx.getAsConstantArrayType(T: FlexibleInit->getType()); |
| 2885 | if (!InitTy) |
| 2886 | return CharUnits::Zero(); |
| 2887 | CharUnits FlexibleArraySize = Ctx.getTypeSizeInChars(T: InitTy); |
| 2888 | const ASTRecordLayout &RL = Ctx.getASTRecordLayout(D: Ty->getDecl()); |
| 2889 | CharUnits FlexibleArrayOffset = |
| 2890 | Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: RL.getFieldCount() - 1)); |
| 2891 | if (FlexibleArrayOffset + FlexibleArraySize < RL.getSize()) |
| 2892 | return CharUnits::Zero(); |
| 2893 | return FlexibleArrayOffset + FlexibleArraySize - RL.getSize(); |
| 2894 | } |
| 2895 | |
| 2896 | MemberSpecializationInfo *VarDecl::getMemberSpecializationInfo() const { |
| 2897 | if (isStaticDataMember()) |
| 2898 | // FIXME: Remove ? |
| 2899 | // return getASTContext().getInstantiatedFromStaticDataMember(this); |
| 2900 | return dyn_cast_if_present<MemberSpecializationInfo *>( |
| 2901 | Val: getASTContext().getTemplateOrSpecializationInfo(Var: this)); |
| 2902 | return nullptr; |
| 2903 | } |
| 2904 | |
| 2905 | void VarDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 2906 | SourceLocation PointOfInstantiation) { |
| 2907 | assert((isa<VarTemplateSpecializationDecl>(this) || |
| 2908 | getMemberSpecializationInfo()) && |
| 2909 | "not a variable or static data member template specialization" ); |
| 2910 | |
| 2911 | if (VarTemplateSpecializationDecl *Spec = |
| 2912 | dyn_cast<VarTemplateSpecializationDecl>(Val: this)) { |
| 2913 | Spec->setSpecializationKind(TSK); |
| 2914 | if (TSK != TSK_ExplicitSpecialization && |
| 2915 | PointOfInstantiation.isValid() && |
| 2916 | Spec->getPointOfInstantiation().isInvalid()) { |
| 2917 | Spec->setPointOfInstantiation(PointOfInstantiation); |
| 2918 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 2919 | L->InstantiationRequested(D: this); |
| 2920 | } |
| 2921 | } else if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) { |
| 2922 | MSI->setTemplateSpecializationKind(TSK); |
| 2923 | if (TSK != TSK_ExplicitSpecialization && PointOfInstantiation.isValid() && |
| 2924 | MSI->getPointOfInstantiation().isInvalid()) { |
| 2925 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 2926 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 2927 | L->InstantiationRequested(D: this); |
| 2928 | } |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | void |
| 2933 | VarDecl::setInstantiationOfStaticDataMember(VarDecl *VD, |
| 2934 | TemplateSpecializationKind TSK) { |
| 2935 | assert(getASTContext().getTemplateOrSpecializationInfo(this).isNull() && |
| 2936 | "Previous template or instantiation?" ); |
| 2937 | getASTContext().setInstantiatedFromStaticDataMember(Inst: this, Tmpl: VD, TSK); |
| 2938 | } |
| 2939 | |
| 2940 | //===----------------------------------------------------------------------===// |
| 2941 | // ParmVarDecl Implementation |
| 2942 | //===----------------------------------------------------------------------===// |
| 2943 | |
| 2944 | ParmVarDecl *ParmVarDecl::Create(ASTContext &C, DeclContext *DC, |
| 2945 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 2946 | const IdentifierInfo *Id, QualType T, |
| 2947 | TypeSourceInfo *TInfo, StorageClass S, |
| 2948 | Expr *DefArg) { |
| 2949 | return new (C, DC) ParmVarDecl(ParmVar, C, DC, StartLoc, IdLoc, Id, T, TInfo, |
| 2950 | S, DefArg); |
| 2951 | } |
| 2952 | |
| 2953 | QualType ParmVarDecl::getOriginalType() const { |
| 2954 | TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 2955 | QualType T = TSI ? TSI->getType() : getType(); |
| 2956 | if (const auto *DT = dyn_cast<DecayedType>(Val&: T)) |
| 2957 | return DT->getOriginalType(); |
| 2958 | return T; |
| 2959 | } |
| 2960 | |
| 2961 | ParmVarDecl *ParmVarDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 2962 | return new (C, ID) |
| 2963 | ParmVarDecl(ParmVar, C, nullptr, SourceLocation(), SourceLocation(), |
| 2964 | nullptr, QualType(), nullptr, SC_None, nullptr); |
| 2965 | } |
| 2966 | |
| 2967 | SourceRange ParmVarDecl::getSourceRange() const { |
| 2968 | if (!hasInheritedDefaultArg()) { |
| 2969 | SourceRange ArgRange = getDefaultArgRange(); |
| 2970 | if (ArgRange.isValid()) |
| 2971 | return SourceRange(getOuterLocStart(), ArgRange.getEnd()); |
| 2972 | } |
| 2973 | |
| 2974 | // DeclaratorDecl considers the range of postfix types as overlapping with the |
| 2975 | // declaration name, but this is not the case with parameters in ObjC methods. |
| 2976 | if (isa<ObjCMethodDecl>(Val: getDeclContext())) |
| 2977 | return SourceRange(DeclaratorDecl::getBeginLoc(), getLocation()); |
| 2978 | |
| 2979 | return DeclaratorDecl::getSourceRange(); |
| 2980 | } |
| 2981 | |
| 2982 | bool ParmVarDecl::isDestroyedInCallee() const { |
| 2983 | // ns_consumed only affects code generation in ARC |
| 2984 | if (hasAttr<NSConsumedAttr>()) |
| 2985 | return getASTContext().getLangOpts().ObjCAutoRefCount; |
| 2986 | |
| 2987 | // FIXME: isParamDestroyedInCallee() should probably imply |
| 2988 | // isDestructedType() |
| 2989 | const auto *RT = getType()->getAs<RecordType>(); |
| 2990 | if (RT && RT->getDecl()->isParamDestroyedInCallee() && |
| 2991 | getType().isDestructedType()) |
| 2992 | return true; |
| 2993 | |
| 2994 | return false; |
| 2995 | } |
| 2996 | |
| 2997 | Expr *ParmVarDecl::getDefaultArg() { |
| 2998 | assert(!hasUnparsedDefaultArg() && "Default argument is not yet parsed!" ); |
| 2999 | assert(!hasUninstantiatedDefaultArg() && |
| 3000 | "Default argument is not yet instantiated!" ); |
| 3001 | |
| 3002 | Expr *Arg = getInit(); |
| 3003 | if (auto *E = dyn_cast_if_present<FullExpr>(Val: Arg)) |
| 3004 | return E->getSubExpr(); |
| 3005 | |
| 3006 | return Arg; |
| 3007 | } |
| 3008 | |
| 3009 | void ParmVarDecl::setDefaultArg(Expr *defarg) { |
| 3010 | ParmVarDeclBits.DefaultArgKind = DAK_Normal; |
| 3011 | Init = defarg; |
| 3012 | } |
| 3013 | |
| 3014 | SourceRange ParmVarDecl::getDefaultArgRange() const { |
| 3015 | switch (ParmVarDeclBits.DefaultArgKind) { |
| 3016 | case DAK_None: |
| 3017 | case DAK_Unparsed: |
| 3018 | // Nothing we can do here. |
| 3019 | return SourceRange(); |
| 3020 | |
| 3021 | case DAK_Uninstantiated: |
| 3022 | return getUninstantiatedDefaultArg()->getSourceRange(); |
| 3023 | |
| 3024 | case DAK_Normal: |
| 3025 | if (const Expr *E = getInit()) |
| 3026 | return E->getSourceRange(); |
| 3027 | |
| 3028 | // Missing an actual expression, may be invalid. |
| 3029 | return SourceRange(); |
| 3030 | } |
| 3031 | llvm_unreachable("Invalid default argument kind." ); |
| 3032 | } |
| 3033 | |
| 3034 | void ParmVarDecl::setUninstantiatedDefaultArg(Expr *arg) { |
| 3035 | ParmVarDeclBits.DefaultArgKind = DAK_Uninstantiated; |
| 3036 | Init = arg; |
| 3037 | } |
| 3038 | |
| 3039 | Expr *ParmVarDecl::getUninstantiatedDefaultArg() { |
| 3040 | assert(hasUninstantiatedDefaultArg() && |
| 3041 | "Wrong kind of initialization expression!" ); |
| 3042 | return cast_if_present<Expr>(Val: cast<Stmt *>(Val&: Init)); |
| 3043 | } |
| 3044 | |
| 3045 | bool ParmVarDecl::hasDefaultArg() const { |
| 3046 | // FIXME: We should just return false for DAK_None here once callers are |
| 3047 | // prepared for the case that we encountered an invalid default argument and |
| 3048 | // were unable to even build an invalid expression. |
| 3049 | return hasUnparsedDefaultArg() || hasUninstantiatedDefaultArg() || |
| 3050 | !Init.isNull(); |
| 3051 | } |
| 3052 | |
| 3053 | void ParmVarDecl::setParameterIndexLarge(unsigned parameterIndex) { |
| 3054 | getASTContext().setParameterIndex(D: this, index: parameterIndex); |
| 3055 | ParmVarDeclBits.ParameterIndex = ParameterIndexSentinel; |
| 3056 | } |
| 3057 | |
| 3058 | unsigned ParmVarDecl::getParameterIndexLarge() const { |
| 3059 | return getASTContext().getParameterIndex(D: this); |
| 3060 | } |
| 3061 | |
| 3062 | //===----------------------------------------------------------------------===// |
| 3063 | // FunctionDecl Implementation |
| 3064 | //===----------------------------------------------------------------------===// |
| 3065 | |
| 3066 | FunctionDecl::FunctionDecl(Kind DK, ASTContext &C, DeclContext *DC, |
| 3067 | SourceLocation StartLoc, |
| 3068 | const DeclarationNameInfo &NameInfo, QualType T, |
| 3069 | TypeSourceInfo *TInfo, StorageClass S, |
| 3070 | bool UsesFPIntrin, bool isInlineSpecified, |
| 3071 | ConstexprSpecKind ConstexprKind, |
| 3072 | const AssociatedConstraint &TrailingRequiresClause) |
| 3073 | : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, |
| 3074 | StartLoc), |
| 3075 | DeclContext(DK), redeclarable_base(C), Body(), ODRHash(0), |
| 3076 | EndRangeLoc(NameInfo.getEndLoc()), DNLoc(NameInfo.getInfo()) { |
| 3077 | assert(T.isNull() || T->isFunctionType()); |
| 3078 | FunctionDeclBits.SClass = S; |
| 3079 | FunctionDeclBits.IsInline = isInlineSpecified; |
| 3080 | FunctionDeclBits.IsInlineSpecified = isInlineSpecified; |
| 3081 | FunctionDeclBits.IsVirtualAsWritten = false; |
| 3082 | FunctionDeclBits.IsPureVirtual = false; |
| 3083 | FunctionDeclBits.HasInheritedPrototype = false; |
| 3084 | FunctionDeclBits.HasWrittenPrototype = true; |
| 3085 | FunctionDeclBits.IsDeleted = false; |
| 3086 | FunctionDeclBits.IsTrivial = false; |
| 3087 | FunctionDeclBits.IsTrivialForCall = false; |
| 3088 | FunctionDeclBits.IsDefaulted = false; |
| 3089 | FunctionDeclBits.IsExplicitlyDefaulted = false; |
| 3090 | FunctionDeclBits.HasDefaultedOrDeletedInfo = false; |
| 3091 | FunctionDeclBits.IsIneligibleOrNotSelected = false; |
| 3092 | FunctionDeclBits.HasImplicitReturnZero = false; |
| 3093 | FunctionDeclBits.IsLateTemplateParsed = false; |
| 3094 | FunctionDeclBits.IsInstantiatedFromMemberTemplate = false; |
| 3095 | FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(ConstexprKind); |
| 3096 | FunctionDeclBits.BodyContainsImmediateEscalatingExpression = false; |
| 3097 | FunctionDeclBits.InstantiationIsPending = false; |
| 3098 | FunctionDeclBits.UsesSEHTry = false; |
| 3099 | FunctionDeclBits.UsesFPIntrin = UsesFPIntrin; |
| 3100 | FunctionDeclBits.HasSkippedBody = false; |
| 3101 | FunctionDeclBits.WillHaveBody = false; |
| 3102 | FunctionDeclBits.IsMultiVersion = false; |
| 3103 | FunctionDeclBits.DeductionCandidateKind = |
| 3104 | static_cast<unsigned char>(DeductionCandidate::Normal); |
| 3105 | FunctionDeclBits.HasODRHash = false; |
| 3106 | FunctionDeclBits.FriendConstraintRefersToEnclosingTemplate = false; |
| 3107 | |
| 3108 | if (TrailingRequiresClause) |
| 3109 | setTrailingRequiresClause(TrailingRequiresClause); |
| 3110 | } |
| 3111 | |
| 3112 | void FunctionDecl::getNameForDiagnostic( |
| 3113 | raw_ostream &OS, const PrintingPolicy &Policy, bool Qualified) const { |
| 3114 | NamedDecl::getNameForDiagnostic(OS, Policy, Qualified); |
| 3115 | const TemplateArgumentList *TemplateArgs = getTemplateSpecializationArgs(); |
| 3116 | if (TemplateArgs) |
| 3117 | printTemplateArgumentList(OS, Args: TemplateArgs->asArray(), Policy); |
| 3118 | } |
| 3119 | |
| 3120 | bool FunctionDecl::isVariadic() const { |
| 3121 | if (const auto *FT = getType()->getAs<FunctionProtoType>()) |
| 3122 | return FT->isVariadic(); |
| 3123 | return false; |
| 3124 | } |
| 3125 | |
| 3126 | FunctionDecl::DefaultedOrDeletedFunctionInfo * |
| 3127 | FunctionDecl::DefaultedOrDeletedFunctionInfo::Create( |
| 3128 | ASTContext &Context, ArrayRef<DeclAccessPair> Lookups, |
| 3129 | StringLiteral *DeletedMessage) { |
| 3130 | static constexpr size_t Alignment = |
| 3131 | std::max(l: {alignof(DefaultedOrDeletedFunctionInfo), |
| 3132 | alignof(DeclAccessPair), alignof(StringLiteral *)}); |
| 3133 | size_t Size = totalSizeToAlloc<DeclAccessPair, StringLiteral *>( |
| 3134 | Counts: Lookups.size(), Counts: DeletedMessage != nullptr); |
| 3135 | |
| 3136 | DefaultedOrDeletedFunctionInfo *Info = |
| 3137 | new (Context.Allocate(Size, Align: Alignment)) DefaultedOrDeletedFunctionInfo; |
| 3138 | Info->NumLookups = Lookups.size(); |
| 3139 | Info->HasDeletedMessage = DeletedMessage != nullptr; |
| 3140 | |
| 3141 | llvm::uninitialized_copy(Src&: Lookups, Dst: Info->getTrailingObjects<DeclAccessPair>()); |
| 3142 | if (DeletedMessage) |
| 3143 | *Info->getTrailingObjects<StringLiteral *>() = DeletedMessage; |
| 3144 | return Info; |
| 3145 | } |
| 3146 | |
| 3147 | void FunctionDecl::setDefaultedOrDeletedInfo( |
| 3148 | DefaultedOrDeletedFunctionInfo *Info) { |
| 3149 | assert(!FunctionDeclBits.HasDefaultedOrDeletedInfo && "already have this" ); |
| 3150 | assert(!Body && "can't replace function body with defaulted function info" ); |
| 3151 | |
| 3152 | FunctionDeclBits.HasDefaultedOrDeletedInfo = true; |
| 3153 | DefaultedOrDeletedInfo = Info; |
| 3154 | } |
| 3155 | |
| 3156 | void FunctionDecl::setDeletedAsWritten(bool D, StringLiteral *Message) { |
| 3157 | FunctionDeclBits.IsDeleted = D; |
| 3158 | |
| 3159 | if (Message) { |
| 3160 | assert(isDeletedAsWritten() && "Function must be deleted" ); |
| 3161 | if (FunctionDeclBits.HasDefaultedOrDeletedInfo) |
| 3162 | DefaultedOrDeletedInfo->setDeletedMessage(Message); |
| 3163 | else |
| 3164 | setDefaultedOrDeletedInfo(DefaultedOrDeletedFunctionInfo::Create( |
| 3165 | Context&: getASTContext(), /*Lookups=*/{}, DeletedMessage: Message)); |
| 3166 | } |
| 3167 | } |
| 3168 | |
| 3169 | void FunctionDecl::DefaultedOrDeletedFunctionInfo::setDeletedMessage( |
| 3170 | StringLiteral *Message) { |
| 3171 | // We should never get here with the DefaultedOrDeletedInfo populated, but |
| 3172 | // no space allocated for the deleted message, since that would require |
| 3173 | // recreating this, but setDefaultedOrDeletedInfo() disallows overwriting |
| 3174 | // an already existing DefaultedOrDeletedFunctionInfo. |
| 3175 | assert(HasDeletedMessage && |
| 3176 | "No space to store a delete message in this DefaultedOrDeletedInfo" ); |
| 3177 | *getTrailingObjects<StringLiteral *>() = Message; |
| 3178 | } |
| 3179 | |
| 3180 | FunctionDecl::DefaultedOrDeletedFunctionInfo * |
| 3181 | FunctionDecl::getDefalutedOrDeletedInfo() const { |
| 3182 | return FunctionDeclBits.HasDefaultedOrDeletedInfo ? DefaultedOrDeletedInfo |
| 3183 | : nullptr; |
| 3184 | } |
| 3185 | |
| 3186 | bool FunctionDecl::hasBody(const FunctionDecl *&Definition) const { |
| 3187 | for (const auto *I : redecls()) { |
| 3188 | if (I->doesThisDeclarationHaveABody()) { |
| 3189 | Definition = I; |
| 3190 | return true; |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | return false; |
| 3195 | } |
| 3196 | |
| 3197 | bool FunctionDecl::hasTrivialBody() const { |
| 3198 | const Stmt *S = getBody(); |
| 3199 | if (!S) { |
| 3200 | // Since we don't have a body for this function, we don't know if it's |
| 3201 | // trivial or not. |
| 3202 | return false; |
| 3203 | } |
| 3204 | |
| 3205 | if (isa<CompoundStmt>(Val: S) && cast<CompoundStmt>(Val: S)->body_empty()) |
| 3206 | return true; |
| 3207 | return false; |
| 3208 | } |
| 3209 | |
| 3210 | bool FunctionDecl::isThisDeclarationInstantiatedFromAFriendDefinition() const { |
| 3211 | if (!getFriendObjectKind()) |
| 3212 | return false; |
| 3213 | |
| 3214 | // Check for a friend function instantiated from a friend function |
| 3215 | // definition in a templated class. |
| 3216 | if (const FunctionDecl *InstantiatedFrom = |
| 3217 | getInstantiatedFromMemberFunction()) |
| 3218 | return InstantiatedFrom->getFriendObjectKind() && |
| 3219 | InstantiatedFrom->isThisDeclarationADefinition(); |
| 3220 | |
| 3221 | // Check for a friend function template instantiated from a friend |
| 3222 | // function template definition in a templated class. |
| 3223 | if (const FunctionTemplateDecl *Template = getDescribedFunctionTemplate()) { |
| 3224 | if (const FunctionTemplateDecl *InstantiatedFrom = |
| 3225 | Template->getInstantiatedFromMemberTemplate()) |
| 3226 | return InstantiatedFrom->getFriendObjectKind() && |
| 3227 | InstantiatedFrom->isThisDeclarationADefinition(); |
| 3228 | } |
| 3229 | |
| 3230 | return false; |
| 3231 | } |
| 3232 | |
| 3233 | bool FunctionDecl::isDefined(const FunctionDecl *&Definition, |
| 3234 | bool CheckForPendingFriendDefinition) const { |
| 3235 | for (const FunctionDecl *FD : redecls()) { |
| 3236 | if (FD->isThisDeclarationADefinition()) { |
| 3237 | Definition = FD; |
| 3238 | return true; |
| 3239 | } |
| 3240 | |
| 3241 | // If this is a friend function defined in a class template, it does not |
| 3242 | // have a body until it is used, nevertheless it is a definition, see |
| 3243 | // [temp.inst]p2: |
| 3244 | // |
| 3245 | // ... for the purpose of determining whether an instantiated redeclaration |
| 3246 | // is valid according to [basic.def.odr] and [class.mem], a declaration that |
| 3247 | // corresponds to a definition in the template is considered to be a |
| 3248 | // definition. |
| 3249 | // |
| 3250 | // The following code must produce redefinition error: |
| 3251 | // |
| 3252 | // template<typename T> struct C20 { friend void func_20() {} }; |
| 3253 | // C20<int> c20i; |
| 3254 | // void func_20() {} |
| 3255 | // |
| 3256 | if (CheckForPendingFriendDefinition && |
| 3257 | FD->isThisDeclarationInstantiatedFromAFriendDefinition()) { |
| 3258 | Definition = FD; |
| 3259 | return true; |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | return false; |
| 3264 | } |
| 3265 | |
| 3266 | Stmt *FunctionDecl::getBody(const FunctionDecl *&Definition) const { |
| 3267 | if (!hasBody(Definition)) |
| 3268 | return nullptr; |
| 3269 | |
| 3270 | assert(!Definition->FunctionDeclBits.HasDefaultedOrDeletedInfo && |
| 3271 | "definition should not have a body" ); |
| 3272 | if (Definition->Body) |
| 3273 | return Definition->Body.get(Source: getASTContext().getExternalSource()); |
| 3274 | |
| 3275 | return nullptr; |
| 3276 | } |
| 3277 | |
| 3278 | void FunctionDecl::setBody(Stmt *B) { |
| 3279 | FunctionDeclBits.HasDefaultedOrDeletedInfo = false; |
| 3280 | Body = LazyDeclStmtPtr(B); |
| 3281 | if (B) |
| 3282 | EndRangeLoc = B->getEndLoc(); |
| 3283 | } |
| 3284 | |
| 3285 | void FunctionDecl::setIsPureVirtual(bool P) { |
| 3286 | FunctionDeclBits.IsPureVirtual = P; |
| 3287 | if (P) |
| 3288 | if (auto *Parent = dyn_cast<CXXRecordDecl>(Val: getDeclContext())) |
| 3289 | Parent->markedVirtualFunctionPure(); |
| 3290 | } |
| 3291 | |
| 3292 | template<std::size_t Len> |
| 3293 | static bool isNamed(const NamedDecl *ND, const char (&Str)[Len]) { |
| 3294 | const IdentifierInfo *II = ND->getIdentifier(); |
| 3295 | return II && II->isStr(Str); |
| 3296 | } |
| 3297 | |
| 3298 | bool FunctionDecl::isImmediateEscalating() const { |
| 3299 | // C++23 [expr.const]/p17 |
| 3300 | // An immediate-escalating function is |
| 3301 | // - the call operator of a lambda that is not declared with the consteval |
| 3302 | // specifier, |
| 3303 | if (isLambdaCallOperator(DC: this) && !isConsteval()) |
| 3304 | return true; |
| 3305 | // - a defaulted special member function that is not declared with the |
| 3306 | // consteval specifier, |
| 3307 | if (isDefaulted() && !isConsteval()) |
| 3308 | return true; |
| 3309 | |
| 3310 | if (auto *CD = dyn_cast<CXXConstructorDecl>(Val: this); |
| 3311 | CD && CD->isInheritingConstructor()) |
| 3312 | return CD->getInheritedConstructor().getConstructor(); |
| 3313 | |
| 3314 | // - a function that results from the instantiation of a templated entity |
| 3315 | // defined with the constexpr specifier. |
| 3316 | TemplatedKind TK = getTemplatedKind(); |
| 3317 | if (TK != TK_NonTemplate && TK != TK_DependentNonTemplate && |
| 3318 | isConstexprSpecified()) |
| 3319 | return true; |
| 3320 | return false; |
| 3321 | } |
| 3322 | |
| 3323 | bool FunctionDecl::isImmediateFunction() const { |
| 3324 | // C++23 [expr.const]/p18 |
| 3325 | // An immediate function is a function or constructor that is |
| 3326 | // - declared with the consteval specifier |
| 3327 | if (isConsteval()) |
| 3328 | return true; |
| 3329 | // - an immediate-escalating function F whose function body contains an |
| 3330 | // immediate-escalating expression |
| 3331 | if (isImmediateEscalating() && BodyContainsImmediateEscalatingExpressions()) |
| 3332 | return true; |
| 3333 | |
| 3334 | if (auto *CD = dyn_cast<CXXConstructorDecl>(Val: this); |
| 3335 | CD && CD->isInheritingConstructor()) |
| 3336 | return CD->getInheritedConstructor() |
| 3337 | .getConstructor() |
| 3338 | ->isImmediateFunction(); |
| 3339 | |
| 3340 | if (FunctionDecl *P = getTemplateInstantiationPattern(); |
| 3341 | P && P->isImmediateFunction()) |
| 3342 | return true; |
| 3343 | |
| 3344 | if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: this); |
| 3345 | MD && MD->isLambdaStaticInvoker()) |
| 3346 | return MD->getParent()->getLambdaCallOperator()->isImmediateFunction(); |
| 3347 | |
| 3348 | return false; |
| 3349 | } |
| 3350 | |
| 3351 | bool FunctionDecl::isMain() const { |
| 3352 | return isNamed(ND: this, Str: "main" ) && !getLangOpts().Freestanding && |
| 3353 | !getLangOpts().HLSL && |
| 3354 | (getDeclContext()->getRedeclContext()->isTranslationUnit() || |
| 3355 | isExternC()); |
| 3356 | } |
| 3357 | |
| 3358 | bool FunctionDecl::isMSVCRTEntryPoint() const { |
| 3359 | const TranslationUnitDecl *TUnit = |
| 3360 | dyn_cast<TranslationUnitDecl>(Val: getDeclContext()->getRedeclContext()); |
| 3361 | if (!TUnit) |
| 3362 | return false; |
| 3363 | |
| 3364 | // Even though we aren't really targeting MSVCRT if we are freestanding, |
| 3365 | // semantic analysis for these functions remains the same. |
| 3366 | |
| 3367 | // MSVCRT entry points only exist on MSVCRT targets. |
| 3368 | if (!TUnit->getASTContext().getTargetInfo().getTriple().isOSMSVCRT() && |
| 3369 | !TUnit->getASTContext().getTargetInfo().getTriple().isUEFI()) |
| 3370 | return false; |
| 3371 | |
| 3372 | // Nameless functions like constructors cannot be entry points. |
| 3373 | if (!getIdentifier()) |
| 3374 | return false; |
| 3375 | |
| 3376 | return llvm::StringSwitch<bool>(getName()) |
| 3377 | .Cases(S0: "main" , // an ANSI console app |
| 3378 | S1: "wmain" , // a Unicode console App |
| 3379 | S2: "WinMain" , // an ANSI GUI app |
| 3380 | S3: "wWinMain" , // a Unicode GUI app |
| 3381 | S4: "DllMain" , // a DLL |
| 3382 | Value: true) |
| 3383 | .Default(Value: false); |
| 3384 | } |
| 3385 | |
| 3386 | bool FunctionDecl::isReservedGlobalPlacementOperator() const { |
| 3387 | if (!getDeclName().isAnyOperatorNewOrDelete()) |
| 3388 | return false; |
| 3389 | |
| 3390 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 3391 | return false; |
| 3392 | |
| 3393 | if (isTypeAwareOperatorNewOrDelete()) |
| 3394 | return false; |
| 3395 | |
| 3396 | const auto *proto = getType()->castAs<FunctionProtoType>(); |
| 3397 | if (proto->getNumParams() != 2 || proto->isVariadic()) |
| 3398 | return false; |
| 3399 | |
| 3400 | const ASTContext &Context = |
| 3401 | cast<TranslationUnitDecl>(Val: getDeclContext()->getRedeclContext()) |
| 3402 | ->getASTContext(); |
| 3403 | |
| 3404 | // The result type and first argument type are constant across all |
| 3405 | // these operators. The second argument must be exactly void*. |
| 3406 | return (proto->getParamType(i: 1).getCanonicalType() == Context.VoidPtrTy); |
| 3407 | } |
| 3408 | |
| 3409 | bool FunctionDecl::isUsableAsGlobalAllocationFunctionInConstantEvaluation( |
| 3410 | UnsignedOrNone *AlignmentParam, bool *IsNothrow) const { |
| 3411 | if (!getDeclName().isAnyOperatorNewOrDelete()) |
| 3412 | return false; |
| 3413 | |
| 3414 | if (isa<CXXRecordDecl>(Val: getDeclContext())) |
| 3415 | return false; |
| 3416 | |
| 3417 | // This can only fail for an invalid 'operator new' declaration. |
| 3418 | if (!getDeclContext()->getRedeclContext()->isTranslationUnit()) |
| 3419 | return false; |
| 3420 | |
| 3421 | if (isVariadic()) |
| 3422 | return false; |
| 3423 | |
| 3424 | if (isTypeAwareOperatorNewOrDelete()) { |
| 3425 | bool IsDelete = getDeclName().isAnyOperatorDelete(); |
| 3426 | unsigned RequiredParameterCount = |
| 3427 | IsDelete ? FunctionDecl::RequiredTypeAwareDeleteParameterCount |
| 3428 | : FunctionDecl::RequiredTypeAwareNewParameterCount; |
| 3429 | if (AlignmentParam) |
| 3430 | *AlignmentParam = |
| 3431 | /* type identity */ 1U + /* address */ IsDelete + /* size */ 1U; |
| 3432 | if (RequiredParameterCount == getNumParams()) |
| 3433 | return true; |
| 3434 | if (getNumParams() > RequiredParameterCount + 1) |
| 3435 | return false; |
| 3436 | if (!getParamDecl(i: RequiredParameterCount)->getType()->isNothrowT()) |
| 3437 | return false; |
| 3438 | |
| 3439 | if (IsNothrow) |
| 3440 | *IsNothrow = true; |
| 3441 | return true; |
| 3442 | } |
| 3443 | |
| 3444 | const auto *FPT = getType()->castAs<FunctionProtoType>(); |
| 3445 | if (FPT->getNumParams() == 0 || FPT->getNumParams() > 4) |
| 3446 | return false; |
| 3447 | |
| 3448 | // If this is a single-parameter function, it must be a replaceable global |
| 3449 | // allocation or deallocation function. |
| 3450 | if (FPT->getNumParams() == 1) |
| 3451 | return true; |
| 3452 | |
| 3453 | unsigned Params = 1; |
| 3454 | QualType Ty = FPT->getParamType(i: Params); |
| 3455 | const ASTContext &Ctx = getASTContext(); |
| 3456 | |
| 3457 | auto Consume = [&] { |
| 3458 | ++Params; |
| 3459 | Ty = Params < FPT->getNumParams() ? FPT->getParamType(i: Params) : QualType(); |
| 3460 | }; |
| 3461 | |
| 3462 | // In C++14, the next parameter can be a 'std::size_t' for sized delete. |
| 3463 | bool IsSizedDelete = false; |
| 3464 | if (Ctx.getLangOpts().SizedDeallocation && |
| 3465 | getDeclName().isAnyOperatorDelete() && |
| 3466 | Ctx.hasSameType(T1: Ty, T2: Ctx.getSizeType())) { |
| 3467 | IsSizedDelete = true; |
| 3468 | Consume(); |
| 3469 | } |
| 3470 | |
| 3471 | // In C++17, the next parameter can be a 'std::align_val_t' for aligned |
| 3472 | // new/delete. |
| 3473 | if (Ctx.getLangOpts().AlignedAllocation && !Ty.isNull() && Ty->isAlignValT()) { |
| 3474 | Consume(); |
| 3475 | if (AlignmentParam) |
| 3476 | *AlignmentParam = Params; |
| 3477 | } |
| 3478 | |
| 3479 | // If this is not a sized delete, the next parameter can be a |
| 3480 | // 'const std::nothrow_t&'. |
| 3481 | if (!IsSizedDelete && !Ty.isNull() && Ty->isReferenceType()) { |
| 3482 | Ty = Ty->getPointeeType(); |
| 3483 | if (Ty.getCVRQualifiers() != Qualifiers::Const) |
| 3484 | return false; |
| 3485 | if (Ty->isNothrowT()) { |
| 3486 | if (IsNothrow) |
| 3487 | *IsNothrow = true; |
| 3488 | Consume(); |
| 3489 | } |
| 3490 | } |
| 3491 | |
| 3492 | // Finally, recognize the not yet standard versions of new that take a |
| 3493 | // hot/cold allocation hint (__hot_cold_t). These are currently supported by |
| 3494 | // tcmalloc (see |
| 3495 | // https://github.com/google/tcmalloc/blob/220043886d4e2efff7a5702d5172cb8065253664/tcmalloc/malloc_extension.h#L53). |
| 3496 | if (!IsSizedDelete && !Ty.isNull() && Ty->isEnumeralType()) { |
| 3497 | QualType T = Ty; |
| 3498 | while (const auto *TD = T->getAs<TypedefType>()) |
| 3499 | T = TD->getDecl()->getUnderlyingType(); |
| 3500 | const IdentifierInfo *II = |
| 3501 | T->castAs<EnumType>()->getDecl()->getIdentifier(); |
| 3502 | if (II && II->isStr(Str: "__hot_cold_t" )) |
| 3503 | Consume(); |
| 3504 | } |
| 3505 | |
| 3506 | return Params == FPT->getNumParams(); |
| 3507 | } |
| 3508 | |
| 3509 | bool FunctionDecl::isInlineBuiltinDeclaration() const { |
| 3510 | if (!getBuiltinID()) |
| 3511 | return false; |
| 3512 | |
| 3513 | const FunctionDecl *Definition; |
| 3514 | if (!hasBody(Definition)) |
| 3515 | return false; |
| 3516 | |
| 3517 | if (!Definition->isInlineSpecified() || |
| 3518 | !Definition->hasAttr<AlwaysInlineAttr>()) |
| 3519 | return false; |
| 3520 | |
| 3521 | ASTContext &Context = getASTContext(); |
| 3522 | switch (Context.GetGVALinkageForFunction(FD: Definition)) { |
| 3523 | case GVA_Internal: |
| 3524 | case GVA_DiscardableODR: |
| 3525 | case GVA_StrongODR: |
| 3526 | return false; |
| 3527 | case GVA_AvailableExternally: |
| 3528 | case GVA_StrongExternal: |
| 3529 | return true; |
| 3530 | } |
| 3531 | llvm_unreachable("Unknown GVALinkage" ); |
| 3532 | } |
| 3533 | |
| 3534 | bool FunctionDecl::isDestroyingOperatorDelete() const { |
| 3535 | return getASTContext().isDestroyingOperatorDelete(FD: this); |
| 3536 | } |
| 3537 | |
| 3538 | void FunctionDecl::setIsDestroyingOperatorDelete(bool IsDestroyingDelete) { |
| 3539 | getASTContext().setIsDestroyingOperatorDelete(FD: this, IsDestroying: IsDestroyingDelete); |
| 3540 | } |
| 3541 | |
| 3542 | bool FunctionDecl::isTypeAwareOperatorNewOrDelete() const { |
| 3543 | return getASTContext().isTypeAwareOperatorNewOrDelete(FD: this); |
| 3544 | } |
| 3545 | |
| 3546 | void FunctionDecl::setIsTypeAwareOperatorNewOrDelete(bool IsTypeAware) { |
| 3547 | getASTContext().setIsTypeAwareOperatorNewOrDelete(FD: this, IsTypeAware); |
| 3548 | } |
| 3549 | |
| 3550 | LanguageLinkage FunctionDecl::getLanguageLinkage() const { |
| 3551 | return getDeclLanguageLinkage(D: *this); |
| 3552 | } |
| 3553 | |
| 3554 | bool FunctionDecl::isExternC() const { |
| 3555 | return isDeclExternC(D: *this); |
| 3556 | } |
| 3557 | |
| 3558 | bool FunctionDecl::isInExternCContext() const { |
| 3559 | if (DeviceKernelAttr::isOpenCLSpelling(A: getAttr<DeviceKernelAttr>())) |
| 3560 | return true; |
| 3561 | return getLexicalDeclContext()->isExternCContext(); |
| 3562 | } |
| 3563 | |
| 3564 | bool FunctionDecl::isInExternCXXContext() const { |
| 3565 | return getLexicalDeclContext()->isExternCXXContext(); |
| 3566 | } |
| 3567 | |
| 3568 | bool FunctionDecl::isGlobal() const { |
| 3569 | if (const auto *Method = dyn_cast<CXXMethodDecl>(Val: this)) |
| 3570 | return Method->isStatic(); |
| 3571 | |
| 3572 | if (getCanonicalDecl()->getStorageClass() == SC_Static) |
| 3573 | return false; |
| 3574 | |
| 3575 | for (const DeclContext *DC = getDeclContext(); |
| 3576 | DC->isNamespace(); |
| 3577 | DC = DC->getParent()) { |
| 3578 | if (const auto *Namespace = cast<NamespaceDecl>(Val: DC)) { |
| 3579 | if (!Namespace->getDeclName()) |
| 3580 | return false; |
| 3581 | } |
| 3582 | } |
| 3583 | |
| 3584 | return true; |
| 3585 | } |
| 3586 | |
| 3587 | bool FunctionDecl::isNoReturn() const { |
| 3588 | if (hasAttr<NoReturnAttr>() || hasAttr<CXX11NoReturnAttr>() || |
| 3589 | hasAttr<C11NoReturnAttr>()) |
| 3590 | return true; |
| 3591 | |
| 3592 | if (auto *FnTy = getType()->getAs<FunctionType>()) |
| 3593 | return FnTy->getNoReturnAttr(); |
| 3594 | |
| 3595 | return false; |
| 3596 | } |
| 3597 | |
| 3598 | bool FunctionDecl::isMemberLikeConstrainedFriend() const { |
| 3599 | // C++20 [temp.friend]p9: |
| 3600 | // A non-template friend declaration with a requires-clause [or] |
| 3601 | // a friend function template with a constraint that depends on a template |
| 3602 | // parameter from an enclosing template [...] does not declare the same |
| 3603 | // function or function template as a declaration in any other scope. |
| 3604 | |
| 3605 | // If this isn't a friend then it's not a member-like constrained friend. |
| 3606 | if (!getFriendObjectKind()) { |
| 3607 | return false; |
| 3608 | } |
| 3609 | |
| 3610 | if (!getDescribedFunctionTemplate()) { |
| 3611 | // If these friends don't have constraints, they aren't constrained, and |
| 3612 | // thus don't fall under temp.friend p9. Else the simple presence of a |
| 3613 | // constraint makes them unique. |
| 3614 | return !getTrailingRequiresClause().isNull(); |
| 3615 | } |
| 3616 | |
| 3617 | return FriendConstraintRefersToEnclosingTemplate(); |
| 3618 | } |
| 3619 | |
| 3620 | MultiVersionKind FunctionDecl::getMultiVersionKind() const { |
| 3621 | if (hasAttr<TargetAttr>()) |
| 3622 | return MultiVersionKind::Target; |
| 3623 | if (hasAttr<TargetVersionAttr>()) |
| 3624 | return MultiVersionKind::TargetVersion; |
| 3625 | if (hasAttr<CPUDispatchAttr>()) |
| 3626 | return MultiVersionKind::CPUDispatch; |
| 3627 | if (hasAttr<CPUSpecificAttr>()) |
| 3628 | return MultiVersionKind::CPUSpecific; |
| 3629 | if (hasAttr<TargetClonesAttr>()) |
| 3630 | return MultiVersionKind::TargetClones; |
| 3631 | return MultiVersionKind::None; |
| 3632 | } |
| 3633 | |
| 3634 | bool FunctionDecl::isCPUDispatchMultiVersion() const { |
| 3635 | return isMultiVersion() && hasAttr<CPUDispatchAttr>(); |
| 3636 | } |
| 3637 | |
| 3638 | bool FunctionDecl::isCPUSpecificMultiVersion() const { |
| 3639 | return isMultiVersion() && hasAttr<CPUSpecificAttr>(); |
| 3640 | } |
| 3641 | |
| 3642 | bool FunctionDecl::isTargetMultiVersion() const { |
| 3643 | return isMultiVersion() && |
| 3644 | (hasAttr<TargetAttr>() || hasAttr<TargetVersionAttr>()); |
| 3645 | } |
| 3646 | |
| 3647 | bool FunctionDecl::isTargetMultiVersionDefault() const { |
| 3648 | if (!isMultiVersion()) |
| 3649 | return false; |
| 3650 | if (hasAttr<TargetAttr>()) |
| 3651 | return getAttr<TargetAttr>()->isDefaultVersion(); |
| 3652 | return hasAttr<TargetVersionAttr>() && |
| 3653 | getAttr<TargetVersionAttr>()->isDefaultVersion(); |
| 3654 | } |
| 3655 | |
| 3656 | bool FunctionDecl::isTargetClonesMultiVersion() const { |
| 3657 | return isMultiVersion() && hasAttr<TargetClonesAttr>(); |
| 3658 | } |
| 3659 | |
| 3660 | bool FunctionDecl::isTargetVersionMultiVersion() const { |
| 3661 | return isMultiVersion() && hasAttr<TargetVersionAttr>(); |
| 3662 | } |
| 3663 | |
| 3664 | void |
| 3665 | FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { |
| 3666 | redeclarable_base::setPreviousDecl(PrevDecl); |
| 3667 | |
| 3668 | if (FunctionTemplateDecl *FunTmpl = getDescribedFunctionTemplate()) { |
| 3669 | FunctionTemplateDecl *PrevFunTmpl |
| 3670 | = PrevDecl? PrevDecl->getDescribedFunctionTemplate() : nullptr; |
| 3671 | assert((!PrevDecl || PrevFunTmpl) && "Function/function template mismatch" ); |
| 3672 | FunTmpl->setPreviousDecl(PrevFunTmpl); |
| 3673 | } |
| 3674 | |
| 3675 | if (PrevDecl && PrevDecl->isInlined()) |
| 3676 | setImplicitlyInline(true); |
| 3677 | } |
| 3678 | |
| 3679 | FunctionDecl *FunctionDecl::getCanonicalDecl() { return getFirstDecl(); } |
| 3680 | |
| 3681 | /// Returns a value indicating whether this function corresponds to a builtin |
| 3682 | /// function. |
| 3683 | /// |
| 3684 | /// The function corresponds to a built-in function if it is declared at |
| 3685 | /// translation scope or within an extern "C" block and its name matches with |
| 3686 | /// the name of a builtin. The returned value will be 0 for functions that do |
| 3687 | /// not correspond to a builtin, a value of type \c Builtin::ID if in the |
| 3688 | /// target-independent range \c [1,Builtin::First), or a target-specific builtin |
| 3689 | /// value. |
| 3690 | /// |
| 3691 | /// \param ConsiderWrapperFunctions If true, we should consider wrapper |
| 3692 | /// functions as their wrapped builtins. This shouldn't be done in general, but |
| 3693 | /// it's useful in Sema to diagnose calls to wrappers based on their semantics. |
| 3694 | unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const { |
| 3695 | unsigned BuiltinID = 0; |
| 3696 | |
| 3697 | if (const auto *ABAA = getAttr<ArmBuiltinAliasAttr>()) { |
| 3698 | BuiltinID = ABAA->getBuiltinName()->getBuiltinID(); |
| 3699 | } else if (const auto *BAA = getAttr<BuiltinAliasAttr>()) { |
| 3700 | BuiltinID = BAA->getBuiltinName()->getBuiltinID(); |
| 3701 | } else if (const auto *A = getAttr<BuiltinAttr>()) { |
| 3702 | BuiltinID = A->getID(); |
| 3703 | } |
| 3704 | |
| 3705 | if (!BuiltinID) |
| 3706 | return 0; |
| 3707 | |
| 3708 | // If the function is marked "overloadable", it has a different mangled name |
| 3709 | // and is not the C library function. |
| 3710 | if (!ConsiderWrapperFunctions && hasAttr<OverloadableAttr>() && |
| 3711 | (!hasAttr<ArmBuiltinAliasAttr>() && !hasAttr<BuiltinAliasAttr>())) |
| 3712 | return 0; |
| 3713 | |
| 3714 | if (getASTContext().getLangOpts().CPlusPlus && |
| 3715 | BuiltinID == Builtin::BI__builtin_counted_by_ref) |
| 3716 | return 0; |
| 3717 | |
| 3718 | const ASTContext &Context = getASTContext(); |
| 3719 | if (!Context.BuiltinInfo.isPredefinedLibFunction(ID: BuiltinID)) |
| 3720 | return BuiltinID; |
| 3721 | |
| 3722 | // This function has the name of a known C library |
| 3723 | // function. Determine whether it actually refers to the C library |
| 3724 | // function or whether it just has the same name. |
| 3725 | |
| 3726 | // If this is a static function, it's not a builtin. |
| 3727 | if (!ConsiderWrapperFunctions && getStorageClass() == SC_Static) |
| 3728 | return 0; |
| 3729 | |
| 3730 | // OpenCL v1.2 s6.9.f - The library functions defined in |
| 3731 | // the C99 standard headers are not available. |
| 3732 | if (Context.getLangOpts().OpenCL && |
| 3733 | Context.BuiltinInfo.isPredefinedLibFunction(ID: BuiltinID)) |
| 3734 | return 0; |
| 3735 | |
| 3736 | // CUDA does not have device-side standard library. printf and malloc are the |
| 3737 | // only special cases that are supported by device-side runtime. |
| 3738 | if (Context.getLangOpts().CUDA && hasAttr<CUDADeviceAttr>() && |
| 3739 | !hasAttr<CUDAHostAttr>() && |
| 3740 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
| 3741 | return 0; |
| 3742 | |
| 3743 | // As AMDGCN implementation of OpenMP does not have a device-side standard |
| 3744 | // library, none of the predefined library functions except printf and malloc |
| 3745 | // should be treated as a builtin i.e. 0 should be returned for them. |
| 3746 | if (Context.getTargetInfo().getTriple().isAMDGCN() && |
| 3747 | Context.getLangOpts().OpenMPIsTargetDevice && |
| 3748 | Context.BuiltinInfo.isPredefinedLibFunction(ID: BuiltinID) && |
| 3749 | !(BuiltinID == Builtin::BIprintf || BuiltinID == Builtin::BImalloc)) |
| 3750 | return 0; |
| 3751 | |
| 3752 | return BuiltinID; |
| 3753 | } |
| 3754 | |
| 3755 | /// getNumParams - Return the number of parameters this function must have |
| 3756 | /// based on its FunctionType. This is the length of the ParamInfo array |
| 3757 | /// after it has been created. |
| 3758 | unsigned FunctionDecl::getNumParams() const { |
| 3759 | const auto *FPT = getType()->getAs<FunctionProtoType>(); |
| 3760 | return FPT ? FPT->getNumParams() : 0; |
| 3761 | } |
| 3762 | |
| 3763 | void FunctionDecl::setParams(ASTContext &C, |
| 3764 | ArrayRef<ParmVarDecl *> NewParamInfo) { |
| 3765 | assert(!ParamInfo && "Already has param info!" ); |
| 3766 | assert(NewParamInfo.size() == getNumParams() && "Parameter count mismatch!" ); |
| 3767 | |
| 3768 | // Zero params -> null pointer. |
| 3769 | if (!NewParamInfo.empty()) { |
| 3770 | ParamInfo = new (C) ParmVarDecl*[NewParamInfo.size()]; |
| 3771 | llvm::copy(Range&: NewParamInfo, Out: ParamInfo); |
| 3772 | } |
| 3773 | } |
| 3774 | |
| 3775 | /// getMinRequiredArguments - Returns the minimum number of arguments |
| 3776 | /// needed to call this function. This may be fewer than the number of |
| 3777 | /// function parameters, if some of the parameters have default |
| 3778 | /// arguments (in C++) or are parameter packs (C++11). |
| 3779 | unsigned FunctionDecl::getMinRequiredArguments() const { |
| 3780 | if (!getASTContext().getLangOpts().CPlusPlus) |
| 3781 | return getNumParams(); |
| 3782 | |
| 3783 | // Note that it is possible for a parameter with no default argument to |
| 3784 | // follow a parameter with a default argument. |
| 3785 | unsigned NumRequiredArgs = 0; |
| 3786 | unsigned MinParamsSoFar = 0; |
| 3787 | for (auto *Param : parameters()) { |
| 3788 | if (!Param->isParameterPack()) { |
| 3789 | ++MinParamsSoFar; |
| 3790 | if (!Param->hasDefaultArg()) |
| 3791 | NumRequiredArgs = MinParamsSoFar; |
| 3792 | } |
| 3793 | } |
| 3794 | return NumRequiredArgs; |
| 3795 | } |
| 3796 | |
| 3797 | bool FunctionDecl::hasCXXExplicitFunctionObjectParameter() const { |
| 3798 | return getNumParams() != 0 && getParamDecl(i: 0)->isExplicitObjectParameter(); |
| 3799 | } |
| 3800 | |
| 3801 | unsigned FunctionDecl::getNumNonObjectParams() const { |
| 3802 | return getNumParams() - |
| 3803 | static_cast<unsigned>(hasCXXExplicitFunctionObjectParameter()); |
| 3804 | } |
| 3805 | |
| 3806 | unsigned FunctionDecl::getMinRequiredExplicitArguments() const { |
| 3807 | return getMinRequiredArguments() - |
| 3808 | static_cast<unsigned>(hasCXXExplicitFunctionObjectParameter()); |
| 3809 | } |
| 3810 | |
| 3811 | bool FunctionDecl::hasOneParamOrDefaultArgs() const { |
| 3812 | return getNumParams() == 1 || |
| 3813 | (getNumParams() > 1 && |
| 3814 | llvm::all_of(Range: llvm::drop_begin(RangeOrContainer: parameters()), |
| 3815 | P: [](ParmVarDecl *P) { return P->hasDefaultArg(); })); |
| 3816 | } |
| 3817 | |
| 3818 | /// The combination of the extern and inline keywords under MSVC forces |
| 3819 | /// the function to be required. |
| 3820 | /// |
| 3821 | /// Note: This function assumes that we will only get called when isInlined() |
| 3822 | /// would return true for this FunctionDecl. |
| 3823 | bool FunctionDecl::isMSExternInline() const { |
| 3824 | assert(isInlined() && "expected to get called on an inlined function!" ); |
| 3825 | |
| 3826 | const ASTContext &Context = getASTContext(); |
| 3827 | if (!Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 3828 | !hasAttr<DLLExportAttr>()) |
| 3829 | return false; |
| 3830 | |
| 3831 | for (const FunctionDecl *FD = getMostRecentDecl(); FD; |
| 3832 | FD = FD->getPreviousDecl()) |
| 3833 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
| 3834 | return true; |
| 3835 | |
| 3836 | return false; |
| 3837 | } |
| 3838 | |
| 3839 | static bool redeclForcesDefMSVC(const FunctionDecl *Redecl) { |
| 3840 | if (Redecl->getStorageClass() != SC_Extern) |
| 3841 | return false; |
| 3842 | |
| 3843 | for (const FunctionDecl *FD = Redecl->getPreviousDecl(); FD; |
| 3844 | FD = FD->getPreviousDecl()) |
| 3845 | if (!FD->isImplicit() && FD->getStorageClass() == SC_Extern) |
| 3846 | return false; |
| 3847 | |
| 3848 | return true; |
| 3849 | } |
| 3850 | |
| 3851 | static bool RedeclForcesDefC99(const FunctionDecl *Redecl) { |
| 3852 | // Only consider file-scope declarations in this test. |
| 3853 | if (!Redecl->getLexicalDeclContext()->isTranslationUnit()) |
| 3854 | return false; |
| 3855 | |
| 3856 | // Only consider explicit declarations; the presence of a builtin for a |
| 3857 | // libcall shouldn't affect whether a definition is externally visible. |
| 3858 | if (Redecl->isImplicit()) |
| 3859 | return false; |
| 3860 | |
| 3861 | if (!Redecl->isInlineSpecified() || Redecl->getStorageClass() == SC_Extern) |
| 3862 | return true; // Not an inline definition |
| 3863 | |
| 3864 | return false; |
| 3865 | } |
| 3866 | |
| 3867 | /// For a function declaration in C or C++, determine whether this |
| 3868 | /// declaration causes the definition to be externally visible. |
| 3869 | /// |
| 3870 | /// For instance, this determines if adding the current declaration to the set |
| 3871 | /// of redeclarations of the given functions causes |
| 3872 | /// isInlineDefinitionExternallyVisible to change from false to true. |
| 3873 | bool FunctionDecl::doesDeclarationForceExternallyVisibleDefinition() const { |
| 3874 | assert(!doesThisDeclarationHaveABody() && |
| 3875 | "Must have a declaration without a body." ); |
| 3876 | |
| 3877 | const ASTContext &Context = getASTContext(); |
| 3878 | |
| 3879 | if (Context.getLangOpts().MSVCCompat) { |
| 3880 | const FunctionDecl *Definition; |
| 3881 | if (hasBody(Definition) && Definition->isInlined() && |
| 3882 | redeclForcesDefMSVC(Redecl: this)) |
| 3883 | return true; |
| 3884 | } |
| 3885 | |
| 3886 | if (Context.getLangOpts().CPlusPlus) |
| 3887 | return false; |
| 3888 | |
| 3889 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
| 3890 | // With GNU inlining, a declaration with 'inline' but not 'extern', forces |
| 3891 | // an externally visible definition. |
| 3892 | // |
| 3893 | // FIXME: What happens if gnu_inline gets added on after the first |
| 3894 | // declaration? |
| 3895 | if (!isInlineSpecified() || getStorageClass() == SC_Extern) |
| 3896 | return false; |
| 3897 | |
| 3898 | const FunctionDecl *Prev = this; |
| 3899 | bool FoundBody = false; |
| 3900 | while ((Prev = Prev->getPreviousDecl())) { |
| 3901 | FoundBody |= Prev->doesThisDeclarationHaveABody(); |
| 3902 | |
| 3903 | if (Prev->doesThisDeclarationHaveABody()) { |
| 3904 | // If it's not the case that both 'inline' and 'extern' are |
| 3905 | // specified on the definition, then it is always externally visible. |
| 3906 | if (!Prev->isInlineSpecified() || |
| 3907 | Prev->getStorageClass() != SC_Extern) |
| 3908 | return false; |
| 3909 | } else if (Prev->isInlineSpecified() && |
| 3910 | Prev->getStorageClass() != SC_Extern) { |
| 3911 | return false; |
| 3912 | } |
| 3913 | } |
| 3914 | return FoundBody; |
| 3915 | } |
| 3916 | |
| 3917 | // C99 6.7.4p6: |
| 3918 | // [...] If all of the file scope declarations for a function in a |
| 3919 | // translation unit include the inline function specifier without extern, |
| 3920 | // then the definition in that translation unit is an inline definition. |
| 3921 | if (isInlineSpecified() && getStorageClass() != SC_Extern) |
| 3922 | return false; |
| 3923 | const FunctionDecl *Prev = this; |
| 3924 | bool FoundBody = false; |
| 3925 | while ((Prev = Prev->getPreviousDecl())) { |
| 3926 | FoundBody |= Prev->doesThisDeclarationHaveABody(); |
| 3927 | if (RedeclForcesDefC99(Redecl: Prev)) |
| 3928 | return false; |
| 3929 | } |
| 3930 | return FoundBody; |
| 3931 | } |
| 3932 | |
| 3933 | FunctionTypeLoc FunctionDecl::getFunctionTypeLoc() const { |
| 3934 | const TypeSourceInfo *TSI = getTypeSourceInfo(); |
| 3935 | |
| 3936 | if (!TSI) |
| 3937 | return FunctionTypeLoc(); |
| 3938 | |
| 3939 | TypeLoc TL = TSI->getTypeLoc(); |
| 3940 | FunctionTypeLoc FTL; |
| 3941 | |
| 3942 | while (!(FTL = TL.getAs<FunctionTypeLoc>())) { |
| 3943 | if (const auto PTL = TL.getAs<ParenTypeLoc>()) |
| 3944 | TL = PTL.getInnerLoc(); |
| 3945 | else if (const auto ATL = TL.getAs<AttributedTypeLoc>()) |
| 3946 | TL = ATL.getEquivalentTypeLoc(); |
| 3947 | else if (const auto MQTL = TL.getAs<MacroQualifiedTypeLoc>()) |
| 3948 | TL = MQTL.getInnerLoc(); |
| 3949 | else |
| 3950 | break; |
| 3951 | } |
| 3952 | |
| 3953 | return FTL; |
| 3954 | } |
| 3955 | |
| 3956 | SourceRange FunctionDecl::getReturnTypeSourceRange() const { |
| 3957 | FunctionTypeLoc FTL = getFunctionTypeLoc(); |
| 3958 | if (!FTL) |
| 3959 | return SourceRange(); |
| 3960 | |
| 3961 | // Skip self-referential return types. |
| 3962 | const SourceManager &SM = getASTContext().getSourceManager(); |
| 3963 | SourceRange RTRange = FTL.getReturnLoc().getSourceRange(); |
| 3964 | SourceLocation Boundary = getNameInfo().getBeginLoc(); |
| 3965 | if (RTRange.isInvalid() || Boundary.isInvalid() || |
| 3966 | !SM.isBeforeInTranslationUnit(LHS: RTRange.getEnd(), RHS: Boundary)) |
| 3967 | return SourceRange(); |
| 3968 | |
| 3969 | return RTRange; |
| 3970 | } |
| 3971 | |
| 3972 | SourceRange FunctionDecl::() const { |
| 3973 | unsigned NP = getNumParams(); |
| 3974 | SourceLocation EllipsisLoc = getEllipsisLoc(); |
| 3975 | |
| 3976 | if (NP == 0 && EllipsisLoc.isInvalid()) |
| 3977 | return SourceRange(); |
| 3978 | |
| 3979 | SourceLocation Begin = |
| 3980 | NP > 0 ? ParamInfo[0]->getSourceRange().getBegin() : EllipsisLoc; |
| 3981 | SourceLocation End = EllipsisLoc.isValid() |
| 3982 | ? EllipsisLoc |
| 3983 | : ParamInfo[NP - 1]->getSourceRange().getEnd(); |
| 3984 | |
| 3985 | return SourceRange(Begin, End); |
| 3986 | } |
| 3987 | |
| 3988 | SourceRange FunctionDecl::getExceptionSpecSourceRange() const { |
| 3989 | FunctionTypeLoc FTL = getFunctionTypeLoc(); |
| 3990 | return FTL ? FTL.getExceptionSpecRange() : SourceRange(); |
| 3991 | } |
| 3992 | |
| 3993 | /// For an inline function definition in C, or for a gnu_inline function |
| 3994 | /// in C++, determine whether the definition will be externally visible. |
| 3995 | /// |
| 3996 | /// Inline function definitions are always available for inlining optimizations. |
| 3997 | /// However, depending on the language dialect, declaration specifiers, and |
| 3998 | /// attributes, the definition of an inline function may or may not be |
| 3999 | /// "externally" visible to other translation units in the program. |
| 4000 | /// |
| 4001 | /// In C99, inline definitions are not externally visible by default. However, |
| 4002 | /// if even one of the global-scope declarations is marked "extern inline", the |
| 4003 | /// inline definition becomes externally visible (C99 6.7.4p6). |
| 4004 | /// |
| 4005 | /// In GNU89 mode, or if the gnu_inline attribute is attached to the function |
| 4006 | /// definition, we use the GNU semantics for inline, which are nearly the |
| 4007 | /// opposite of C99 semantics. In particular, "inline" by itself will create |
| 4008 | /// an externally visible symbol, but "extern inline" will not create an |
| 4009 | /// externally visible symbol. |
| 4010 | bool FunctionDecl::isInlineDefinitionExternallyVisible() const { |
| 4011 | assert((doesThisDeclarationHaveABody() || willHaveBody() || |
| 4012 | hasAttr<AliasAttr>()) && |
| 4013 | "Must be a function definition" ); |
| 4014 | assert(isInlined() && "Function must be inline" ); |
| 4015 | ASTContext &Context = getASTContext(); |
| 4016 | |
| 4017 | if (Context.getLangOpts().GNUInline || hasAttr<GNUInlineAttr>()) { |
| 4018 | // Note: If you change the logic here, please change |
| 4019 | // doesDeclarationForceExternallyVisibleDefinition as well. |
| 4020 | // |
| 4021 | // If it's not the case that both 'inline' and 'extern' are |
| 4022 | // specified on the definition, then this inline definition is |
| 4023 | // externally visible. |
| 4024 | if (Context.getLangOpts().CPlusPlus) |
| 4025 | return false; |
| 4026 | if (!(isInlineSpecified() && getStorageClass() == SC_Extern)) |
| 4027 | return true; |
| 4028 | |
| 4029 | // If any declaration is 'inline' but not 'extern', then this definition |
| 4030 | // is externally visible. |
| 4031 | for (auto *Redecl : redecls()) { |
| 4032 | if (Redecl->isInlineSpecified() && |
| 4033 | Redecl->getStorageClass() != SC_Extern) |
| 4034 | return true; |
| 4035 | } |
| 4036 | |
| 4037 | return false; |
| 4038 | } |
| 4039 | |
| 4040 | // The rest of this function is C-only. |
| 4041 | assert(!Context.getLangOpts().CPlusPlus && |
| 4042 | "should not use C inline rules in C++" ); |
| 4043 | |
| 4044 | // C99 6.7.4p6: |
| 4045 | // [...] If all of the file scope declarations for a function in a |
| 4046 | // translation unit include the inline function specifier without extern, |
| 4047 | // then the definition in that translation unit is an inline definition. |
| 4048 | for (auto *Redecl : redecls()) { |
| 4049 | if (RedeclForcesDefC99(Redecl)) |
| 4050 | return true; |
| 4051 | } |
| 4052 | |
| 4053 | // C99 6.7.4p6: |
| 4054 | // An inline definition does not provide an external definition for the |
| 4055 | // function, and does not forbid an external definition in another |
| 4056 | // translation unit. |
| 4057 | return false; |
| 4058 | } |
| 4059 | |
| 4060 | /// getOverloadedOperator - Which C++ overloaded operator this |
| 4061 | /// function represents, if any. |
| 4062 | OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |
| 4063 | if (getDeclName().getNameKind() == DeclarationName::CXXOperatorName) |
| 4064 | return getDeclName().getCXXOverloadedOperator(); |
| 4065 | return OO_None; |
| 4066 | } |
| 4067 | |
| 4068 | /// getLiteralIdentifier - The literal suffix identifier this function |
| 4069 | /// represents, if any. |
| 4070 | const IdentifierInfo *FunctionDecl::getLiteralIdentifier() const { |
| 4071 | if (getDeclName().getNameKind() == DeclarationName::CXXLiteralOperatorName) |
| 4072 | return getDeclName().getCXXLiteralIdentifier(); |
| 4073 | return nullptr; |
| 4074 | } |
| 4075 | |
| 4076 | FunctionDecl::TemplatedKind FunctionDecl::getTemplatedKind() const { |
| 4077 | if (TemplateOrSpecialization.isNull()) |
| 4078 | return TK_NonTemplate; |
| 4079 | if (const auto *ND = dyn_cast<NamedDecl *>(Val: TemplateOrSpecialization)) { |
| 4080 | if (isa<FunctionDecl>(Val: ND)) |
| 4081 | return TK_DependentNonTemplate; |
| 4082 | assert(isa<FunctionTemplateDecl>(ND) && |
| 4083 | "No other valid types in NamedDecl" ); |
| 4084 | return TK_FunctionTemplate; |
| 4085 | } |
| 4086 | if (isa<MemberSpecializationInfo *>(Val: TemplateOrSpecialization)) |
| 4087 | return TK_MemberSpecialization; |
| 4088 | if (isa<FunctionTemplateSpecializationInfo *>(Val: TemplateOrSpecialization)) |
| 4089 | return TK_FunctionTemplateSpecialization; |
| 4090 | if (isa<DependentFunctionTemplateSpecializationInfo *>( |
| 4091 | Val: TemplateOrSpecialization)) |
| 4092 | return TK_DependentFunctionTemplateSpecialization; |
| 4093 | |
| 4094 | llvm_unreachable("Did we miss a TemplateOrSpecialization type?" ); |
| 4095 | } |
| 4096 | |
| 4097 | FunctionDecl *FunctionDecl::getInstantiatedFromMemberFunction() const { |
| 4098 | if (MemberSpecializationInfo *Info = getMemberSpecializationInfo()) |
| 4099 | return cast<FunctionDecl>(Val: Info->getInstantiatedFrom()); |
| 4100 | |
| 4101 | return nullptr; |
| 4102 | } |
| 4103 | |
| 4104 | MemberSpecializationInfo *FunctionDecl::getMemberSpecializationInfo() const { |
| 4105 | if (auto *MSI = dyn_cast_if_present<MemberSpecializationInfo *>( |
| 4106 | Val: TemplateOrSpecialization)) |
| 4107 | return MSI; |
| 4108 | if (auto *FTSI = dyn_cast_if_present<FunctionTemplateSpecializationInfo *>( |
| 4109 | Val: TemplateOrSpecialization)) |
| 4110 | return FTSI->getMemberSpecializationInfo(); |
| 4111 | return nullptr; |
| 4112 | } |
| 4113 | |
| 4114 | void |
| 4115 | FunctionDecl::setInstantiationOfMemberFunction(ASTContext &C, |
| 4116 | FunctionDecl *FD, |
| 4117 | TemplateSpecializationKind TSK) { |
| 4118 | assert(TemplateOrSpecialization.isNull() && |
| 4119 | "Member function is already a specialization" ); |
| 4120 | MemberSpecializationInfo *Info |
| 4121 | = new (C) MemberSpecializationInfo(FD, TSK); |
| 4122 | TemplateOrSpecialization = Info; |
| 4123 | } |
| 4124 | |
| 4125 | FunctionTemplateDecl *FunctionDecl::getDescribedFunctionTemplate() const { |
| 4126 | return dyn_cast_if_present<FunctionTemplateDecl>( |
| 4127 | Val: dyn_cast_if_present<NamedDecl *>(Val: TemplateOrSpecialization)); |
| 4128 | } |
| 4129 | |
| 4130 | void FunctionDecl::setDescribedFunctionTemplate( |
| 4131 | FunctionTemplateDecl *Template) { |
| 4132 | assert(TemplateOrSpecialization.isNull() && |
| 4133 | "Member function is already a specialization" ); |
| 4134 | TemplateOrSpecialization = Template; |
| 4135 | } |
| 4136 | |
| 4137 | bool FunctionDecl::isFunctionTemplateSpecialization() const { |
| 4138 | return isa<FunctionTemplateSpecializationInfo *>(Val: TemplateOrSpecialization) || |
| 4139 | isa<DependentFunctionTemplateSpecializationInfo *>( |
| 4140 | Val: TemplateOrSpecialization); |
| 4141 | } |
| 4142 | |
| 4143 | void FunctionDecl::setInstantiatedFromDecl(FunctionDecl *FD) { |
| 4144 | assert(TemplateOrSpecialization.isNull() && |
| 4145 | "Function is already a specialization" ); |
| 4146 | TemplateOrSpecialization = FD; |
| 4147 | } |
| 4148 | |
| 4149 | FunctionDecl *FunctionDecl::getInstantiatedFromDecl() const { |
| 4150 | return dyn_cast_if_present<FunctionDecl>( |
| 4151 | Val: TemplateOrSpecialization.dyn_cast<NamedDecl *>()); |
| 4152 | } |
| 4153 | |
| 4154 | bool FunctionDecl::isImplicitlyInstantiable() const { |
| 4155 | // If the function is invalid, it can't be implicitly instantiated. |
| 4156 | if (isInvalidDecl()) |
| 4157 | return false; |
| 4158 | |
| 4159 | switch (getTemplateSpecializationKindForInstantiation()) { |
| 4160 | case TSK_Undeclared: |
| 4161 | case TSK_ExplicitInstantiationDefinition: |
| 4162 | case TSK_ExplicitSpecialization: |
| 4163 | return false; |
| 4164 | |
| 4165 | case TSK_ImplicitInstantiation: |
| 4166 | return true; |
| 4167 | |
| 4168 | case TSK_ExplicitInstantiationDeclaration: |
| 4169 | // Handled below. |
| 4170 | break; |
| 4171 | } |
| 4172 | |
| 4173 | // Find the actual template from which we will instantiate. |
| 4174 | const FunctionDecl *PatternDecl = getTemplateInstantiationPattern(); |
| 4175 | bool HasPattern = false; |
| 4176 | if (PatternDecl) |
| 4177 | HasPattern = PatternDecl->hasBody(Definition&: PatternDecl); |
| 4178 | |
| 4179 | // C++0x [temp.explicit]p9: |
| 4180 | // Except for inline functions, other explicit instantiation declarations |
| 4181 | // have the effect of suppressing the implicit instantiation of the entity |
| 4182 | // to which they refer. |
| 4183 | if (!HasPattern || !PatternDecl) |
| 4184 | return true; |
| 4185 | |
| 4186 | return PatternDecl->isInlined(); |
| 4187 | } |
| 4188 | |
| 4189 | bool FunctionDecl::isTemplateInstantiation() const { |
| 4190 | // FIXME: Remove this, it's not clear what it means. (Which template |
| 4191 | // specialization kind?) |
| 4192 | return clang::isTemplateInstantiation(Kind: getTemplateSpecializationKind()); |
| 4193 | } |
| 4194 | |
| 4195 | FunctionDecl * |
| 4196 | FunctionDecl::getTemplateInstantiationPattern(bool ForDefinition) const { |
| 4197 | // If this is a generic lambda call operator specialization, its |
| 4198 | // instantiation pattern is always its primary template's pattern |
| 4199 | // even if its primary template was instantiated from another |
| 4200 | // member template (which happens with nested generic lambdas). |
| 4201 | // Since a lambda's call operator's body is transformed eagerly, |
| 4202 | // we don't have to go hunting for a prototype definition template |
| 4203 | // (i.e. instantiated-from-member-template) to use as an instantiation |
| 4204 | // pattern. |
| 4205 | |
| 4206 | if (isGenericLambdaCallOperatorSpecialization( |
| 4207 | MD: dyn_cast<CXXMethodDecl>(Val: this))) { |
| 4208 | assert(getPrimaryTemplate() && "not a generic lambda call operator?" ); |
| 4209 | return getDefinitionOrSelf(D: getPrimaryTemplate()->getTemplatedDecl()); |
| 4210 | } |
| 4211 | |
| 4212 | // Check for a declaration of this function that was instantiated from a |
| 4213 | // friend definition. |
| 4214 | const FunctionDecl *FD = nullptr; |
| 4215 | if (!isDefined(Definition&: FD, /*CheckForPendingFriendDefinition=*/true)) |
| 4216 | FD = this; |
| 4217 | |
| 4218 | if (MemberSpecializationInfo *Info = FD->getMemberSpecializationInfo()) { |
| 4219 | if (ForDefinition && |
| 4220 | !clang::isTemplateInstantiation(Kind: Info->getTemplateSpecializationKind())) |
| 4221 | return nullptr; |
| 4222 | return getDefinitionOrSelf(D: cast<FunctionDecl>(Val: Info->getInstantiatedFrom())); |
| 4223 | } |
| 4224 | |
| 4225 | if (ForDefinition && |
| 4226 | !clang::isTemplateInstantiation(Kind: getTemplateSpecializationKind())) |
| 4227 | return nullptr; |
| 4228 | |
| 4229 | if (FunctionTemplateDecl *Primary = getPrimaryTemplate()) { |
| 4230 | // If we hit a point where the user provided a specialization of this |
| 4231 | // template, we're done looking. |
| 4232 | while (!ForDefinition || !Primary->isMemberSpecialization()) { |
| 4233 | auto *NewPrimary = Primary->getInstantiatedFromMemberTemplate(); |
| 4234 | if (!NewPrimary) |
| 4235 | break; |
| 4236 | Primary = NewPrimary; |
| 4237 | } |
| 4238 | |
| 4239 | return getDefinitionOrSelf(D: Primary->getTemplatedDecl()); |
| 4240 | } |
| 4241 | |
| 4242 | return nullptr; |
| 4243 | } |
| 4244 | |
| 4245 | FunctionTemplateDecl *FunctionDecl::getPrimaryTemplate() const { |
| 4246 | if (FunctionTemplateSpecializationInfo *Info = |
| 4247 | dyn_cast_if_present<FunctionTemplateSpecializationInfo *>( |
| 4248 | Val: TemplateOrSpecialization)) { |
| 4249 | return Info->getTemplate(); |
| 4250 | } |
| 4251 | return nullptr; |
| 4252 | } |
| 4253 | |
| 4254 | FunctionTemplateSpecializationInfo * |
| 4255 | FunctionDecl::getTemplateSpecializationInfo() const { |
| 4256 | return dyn_cast_if_present<FunctionTemplateSpecializationInfo *>( |
| 4257 | Val: TemplateOrSpecialization); |
| 4258 | } |
| 4259 | |
| 4260 | const TemplateArgumentList * |
| 4261 | FunctionDecl::getTemplateSpecializationArgs() const { |
| 4262 | if (FunctionTemplateSpecializationInfo *Info = |
| 4263 | dyn_cast_if_present<FunctionTemplateSpecializationInfo *>( |
| 4264 | Val: TemplateOrSpecialization)) { |
| 4265 | return Info->TemplateArguments; |
| 4266 | } |
| 4267 | return nullptr; |
| 4268 | } |
| 4269 | |
| 4270 | const ASTTemplateArgumentListInfo * |
| 4271 | FunctionDecl::getTemplateSpecializationArgsAsWritten() const { |
| 4272 | if (FunctionTemplateSpecializationInfo *Info = |
| 4273 | dyn_cast_if_present<FunctionTemplateSpecializationInfo *>( |
| 4274 | Val: TemplateOrSpecialization)) { |
| 4275 | return Info->TemplateArgumentsAsWritten; |
| 4276 | } |
| 4277 | if (DependentFunctionTemplateSpecializationInfo *Info = |
| 4278 | dyn_cast_if_present<DependentFunctionTemplateSpecializationInfo *>( |
| 4279 | Val: TemplateOrSpecialization)) { |
| 4280 | return Info->TemplateArgumentsAsWritten; |
| 4281 | } |
| 4282 | return nullptr; |
| 4283 | } |
| 4284 | |
| 4285 | void FunctionDecl::setFunctionTemplateSpecialization( |
| 4286 | ASTContext &C, FunctionTemplateDecl *Template, |
| 4287 | TemplateArgumentList *TemplateArgs, void *InsertPos, |
| 4288 | TemplateSpecializationKind TSK, |
| 4289 | const TemplateArgumentListInfo *TemplateArgsAsWritten, |
| 4290 | SourceLocation PointOfInstantiation) { |
| 4291 | assert((TemplateOrSpecialization.isNull() || |
| 4292 | isa<MemberSpecializationInfo *>(TemplateOrSpecialization)) && |
| 4293 | "Member function is already a specialization" ); |
| 4294 | assert(TSK != TSK_Undeclared && |
| 4295 | "Must specify the type of function template specialization" ); |
| 4296 | assert((TemplateOrSpecialization.isNull() || |
| 4297 | getFriendObjectKind() != FOK_None || |
| 4298 | TSK == TSK_ExplicitSpecialization) && |
| 4299 | "Member specialization must be an explicit specialization" ); |
| 4300 | FunctionTemplateSpecializationInfo *Info = |
| 4301 | FunctionTemplateSpecializationInfo::Create( |
| 4302 | C, FD: this, Template, TSK, TemplateArgs, TemplateArgsAsWritten, |
| 4303 | POI: PointOfInstantiation, |
| 4304 | MSInfo: dyn_cast_if_present<MemberSpecializationInfo *>( |
| 4305 | Val&: TemplateOrSpecialization)); |
| 4306 | TemplateOrSpecialization = Info; |
| 4307 | Template->addSpecialization(Info, InsertPos); |
| 4308 | } |
| 4309 | |
| 4310 | void FunctionDecl::setDependentTemplateSpecialization( |
| 4311 | ASTContext &Context, const UnresolvedSetImpl &Templates, |
| 4312 | const TemplateArgumentListInfo *TemplateArgs) { |
| 4313 | assert(TemplateOrSpecialization.isNull()); |
| 4314 | DependentFunctionTemplateSpecializationInfo *Info = |
| 4315 | DependentFunctionTemplateSpecializationInfo::Create(Context, Candidates: Templates, |
| 4316 | TemplateArgs); |
| 4317 | TemplateOrSpecialization = Info; |
| 4318 | } |
| 4319 | |
| 4320 | DependentFunctionTemplateSpecializationInfo * |
| 4321 | FunctionDecl::getDependentSpecializationInfo() const { |
| 4322 | return dyn_cast_if_present<DependentFunctionTemplateSpecializationInfo *>( |
| 4323 | Val: TemplateOrSpecialization); |
| 4324 | } |
| 4325 | |
| 4326 | DependentFunctionTemplateSpecializationInfo * |
| 4327 | DependentFunctionTemplateSpecializationInfo::Create( |
| 4328 | ASTContext &Context, const UnresolvedSetImpl &Candidates, |
| 4329 | const TemplateArgumentListInfo *TArgs) { |
| 4330 | const auto *TArgsWritten = |
| 4331 | TArgs ? ASTTemplateArgumentListInfo::Create(C: Context, List: *TArgs) : nullptr; |
| 4332 | return new (Context.Allocate( |
| 4333 | Size: totalSizeToAlloc<FunctionTemplateDecl *>(Counts: Candidates.size()))) |
| 4334 | DependentFunctionTemplateSpecializationInfo(Candidates, TArgsWritten); |
| 4335 | } |
| 4336 | |
| 4337 | DependentFunctionTemplateSpecializationInfo:: |
| 4338 | DependentFunctionTemplateSpecializationInfo( |
| 4339 | const UnresolvedSetImpl &Candidates, |
| 4340 | const ASTTemplateArgumentListInfo *TemplateArgsWritten) |
| 4341 | : NumCandidates(Candidates.size()), |
| 4342 | TemplateArgumentsAsWritten(TemplateArgsWritten) { |
| 4343 | std::transform(first: Candidates.begin(), last: Candidates.end(), result: getTrailingObjects(), |
| 4344 | unary_op: [](NamedDecl *ND) { |
| 4345 | return cast<FunctionTemplateDecl>(Val: ND->getUnderlyingDecl()); |
| 4346 | }); |
| 4347 | } |
| 4348 | |
| 4349 | TemplateSpecializationKind FunctionDecl::getTemplateSpecializationKind() const { |
| 4350 | // For a function template specialization, query the specialization |
| 4351 | // information object. |
| 4352 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
| 4353 | dyn_cast_if_present<FunctionTemplateSpecializationInfo *>( |
| 4354 | Val: TemplateOrSpecialization)) |
| 4355 | return FTSInfo->getTemplateSpecializationKind(); |
| 4356 | |
| 4357 | if (MemberSpecializationInfo *MSInfo = |
| 4358 | dyn_cast_if_present<MemberSpecializationInfo *>( |
| 4359 | Val: TemplateOrSpecialization)) |
| 4360 | return MSInfo->getTemplateSpecializationKind(); |
| 4361 | |
| 4362 | // A dependent function template specialization is an explicit specialization, |
| 4363 | // except when it's a friend declaration. |
| 4364 | if (isa<DependentFunctionTemplateSpecializationInfo *>( |
| 4365 | Val: TemplateOrSpecialization) && |
| 4366 | getFriendObjectKind() == FOK_None) |
| 4367 | return TSK_ExplicitSpecialization; |
| 4368 | |
| 4369 | return TSK_Undeclared; |
| 4370 | } |
| 4371 | |
| 4372 | TemplateSpecializationKind |
| 4373 | FunctionDecl::getTemplateSpecializationKindForInstantiation() const { |
| 4374 | // This is the same as getTemplateSpecializationKind(), except that for a |
| 4375 | // function that is both a function template specialization and a member |
| 4376 | // specialization, we prefer the member specialization information. Eg: |
| 4377 | // |
| 4378 | // template<typename T> struct A { |
| 4379 | // template<typename U> void f() {} |
| 4380 | // template<> void f<int>() {} |
| 4381 | // }; |
| 4382 | // |
| 4383 | // Within the templated CXXRecordDecl, A<T>::f<int> is a dependent function |
| 4384 | // template specialization; both getTemplateSpecializationKind() and |
| 4385 | // getTemplateSpecializationKindForInstantiation() will return |
| 4386 | // TSK_ExplicitSpecialization. |
| 4387 | // |
| 4388 | // For A<int>::f<int>(): |
| 4389 | // * getTemplateSpecializationKind() will return TSK_ExplicitSpecialization |
| 4390 | // * getTemplateSpecializationKindForInstantiation() will return |
| 4391 | // TSK_ImplicitInstantiation |
| 4392 | // |
| 4393 | // This reflects the facts that A<int>::f<int> is an explicit specialization |
| 4394 | // of A<int>::f, and that A<int>::f<int> should be implicitly instantiated |
| 4395 | // from A::f<int> if a definition is needed. |
| 4396 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
| 4397 | dyn_cast_if_present<FunctionTemplateSpecializationInfo *>( |
| 4398 | Val: TemplateOrSpecialization)) { |
| 4399 | if (auto *MSInfo = FTSInfo->getMemberSpecializationInfo()) |
| 4400 | return MSInfo->getTemplateSpecializationKind(); |
| 4401 | return FTSInfo->getTemplateSpecializationKind(); |
| 4402 | } |
| 4403 | |
| 4404 | if (MemberSpecializationInfo *MSInfo = |
| 4405 | dyn_cast_if_present<MemberSpecializationInfo *>( |
| 4406 | Val: TemplateOrSpecialization)) |
| 4407 | return MSInfo->getTemplateSpecializationKind(); |
| 4408 | |
| 4409 | if (isa<DependentFunctionTemplateSpecializationInfo *>( |
| 4410 | Val: TemplateOrSpecialization) && |
| 4411 | getFriendObjectKind() == FOK_None) |
| 4412 | return TSK_ExplicitSpecialization; |
| 4413 | |
| 4414 | return TSK_Undeclared; |
| 4415 | } |
| 4416 | |
| 4417 | void |
| 4418 | FunctionDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 4419 | SourceLocation PointOfInstantiation) { |
| 4420 | if (FunctionTemplateSpecializationInfo *FTSInfo = |
| 4421 | dyn_cast<FunctionTemplateSpecializationInfo *>( |
| 4422 | Val&: TemplateOrSpecialization)) { |
| 4423 | FTSInfo->setTemplateSpecializationKind(TSK); |
| 4424 | if (TSK != TSK_ExplicitSpecialization && |
| 4425 | PointOfInstantiation.isValid() && |
| 4426 | FTSInfo->getPointOfInstantiation().isInvalid()) { |
| 4427 | FTSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 4428 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 4429 | L->InstantiationRequested(D: this); |
| 4430 | } |
| 4431 | } else if (MemberSpecializationInfo *MSInfo = |
| 4432 | dyn_cast<MemberSpecializationInfo *>( |
| 4433 | Val&: TemplateOrSpecialization)) { |
| 4434 | MSInfo->setTemplateSpecializationKind(TSK); |
| 4435 | if (TSK != TSK_ExplicitSpecialization && |
| 4436 | PointOfInstantiation.isValid() && |
| 4437 | MSInfo->getPointOfInstantiation().isInvalid()) { |
| 4438 | MSInfo->setPointOfInstantiation(PointOfInstantiation); |
| 4439 | if (ASTMutationListener *L = getASTContext().getASTMutationListener()) |
| 4440 | L->InstantiationRequested(D: this); |
| 4441 | } |
| 4442 | } else |
| 4443 | llvm_unreachable("Function cannot have a template specialization kind" ); |
| 4444 | } |
| 4445 | |
| 4446 | SourceLocation FunctionDecl::getPointOfInstantiation() const { |
| 4447 | if (FunctionTemplateSpecializationInfo *FTSInfo |
| 4448 | = TemplateOrSpecialization.dyn_cast< |
| 4449 | FunctionTemplateSpecializationInfo*>()) |
| 4450 | return FTSInfo->getPointOfInstantiation(); |
| 4451 | if (MemberSpecializationInfo *MSInfo = |
| 4452 | TemplateOrSpecialization.dyn_cast<MemberSpecializationInfo *>()) |
| 4453 | return MSInfo->getPointOfInstantiation(); |
| 4454 | |
| 4455 | return SourceLocation(); |
| 4456 | } |
| 4457 | |
| 4458 | bool FunctionDecl::isOutOfLine() const { |
| 4459 | if (Decl::isOutOfLine()) |
| 4460 | return true; |
| 4461 | |
| 4462 | // If this function was instantiated from a member function of a |
| 4463 | // class template, check whether that member function was defined out-of-line. |
| 4464 | if (FunctionDecl *FD = getInstantiatedFromMemberFunction()) { |
| 4465 | const FunctionDecl *Definition; |
| 4466 | if (FD->hasBody(Definition)) |
| 4467 | return Definition->isOutOfLine(); |
| 4468 | } |
| 4469 | |
| 4470 | // If this function was instantiated from a function template, |
| 4471 | // check whether that function template was defined out-of-line. |
| 4472 | if (FunctionTemplateDecl *FunTmpl = getPrimaryTemplate()) { |
| 4473 | const FunctionDecl *Definition; |
| 4474 | if (FunTmpl->getTemplatedDecl()->hasBody(Definition)) |
| 4475 | return Definition->isOutOfLine(); |
| 4476 | } |
| 4477 | |
| 4478 | return false; |
| 4479 | } |
| 4480 | |
| 4481 | SourceRange FunctionDecl::getSourceRange() const { |
| 4482 | return SourceRange(getOuterLocStart(), EndRangeLoc); |
| 4483 | } |
| 4484 | |
| 4485 | unsigned FunctionDecl::getMemoryFunctionKind() const { |
| 4486 | IdentifierInfo *FnInfo = getIdentifier(); |
| 4487 | |
| 4488 | if (!FnInfo) |
| 4489 | return 0; |
| 4490 | |
| 4491 | // Builtin handling. |
| 4492 | switch (getBuiltinID()) { |
| 4493 | case Builtin::BI__builtin_memset: |
| 4494 | case Builtin::BI__builtin___memset_chk: |
| 4495 | case Builtin::BImemset: |
| 4496 | return Builtin::BImemset; |
| 4497 | |
| 4498 | case Builtin::BI__builtin_memcpy: |
| 4499 | case Builtin::BI__builtin___memcpy_chk: |
| 4500 | case Builtin::BImemcpy: |
| 4501 | return Builtin::BImemcpy; |
| 4502 | |
| 4503 | case Builtin::BI__builtin_mempcpy: |
| 4504 | case Builtin::BI__builtin___mempcpy_chk: |
| 4505 | case Builtin::BImempcpy: |
| 4506 | return Builtin::BImempcpy; |
| 4507 | |
| 4508 | case Builtin::BI__builtin_trivially_relocate: |
| 4509 | case Builtin::BI__builtin_memmove: |
| 4510 | case Builtin::BI__builtin___memmove_chk: |
| 4511 | case Builtin::BImemmove: |
| 4512 | return Builtin::BImemmove; |
| 4513 | |
| 4514 | case Builtin::BIstrlcpy: |
| 4515 | case Builtin::BI__builtin___strlcpy_chk: |
| 4516 | return Builtin::BIstrlcpy; |
| 4517 | |
| 4518 | case Builtin::BIstrlcat: |
| 4519 | case Builtin::BI__builtin___strlcat_chk: |
| 4520 | return Builtin::BIstrlcat; |
| 4521 | |
| 4522 | case Builtin::BI__builtin_memcmp: |
| 4523 | case Builtin::BImemcmp: |
| 4524 | return Builtin::BImemcmp; |
| 4525 | |
| 4526 | case Builtin::BI__builtin_bcmp: |
| 4527 | case Builtin::BIbcmp: |
| 4528 | return Builtin::BIbcmp; |
| 4529 | |
| 4530 | case Builtin::BI__builtin_strncpy: |
| 4531 | case Builtin::BI__builtin___strncpy_chk: |
| 4532 | case Builtin::BIstrncpy: |
| 4533 | return Builtin::BIstrncpy; |
| 4534 | |
| 4535 | case Builtin::BI__builtin_strncmp: |
| 4536 | case Builtin::BIstrncmp: |
| 4537 | return Builtin::BIstrncmp; |
| 4538 | |
| 4539 | case Builtin::BI__builtin_strncasecmp: |
| 4540 | case Builtin::BIstrncasecmp: |
| 4541 | return Builtin::BIstrncasecmp; |
| 4542 | |
| 4543 | case Builtin::BI__builtin_strncat: |
| 4544 | case Builtin::BI__builtin___strncat_chk: |
| 4545 | case Builtin::BIstrncat: |
| 4546 | return Builtin::BIstrncat; |
| 4547 | |
| 4548 | case Builtin::BI__builtin_strndup: |
| 4549 | case Builtin::BIstrndup: |
| 4550 | return Builtin::BIstrndup; |
| 4551 | |
| 4552 | case Builtin::BI__builtin_strlen: |
| 4553 | case Builtin::BIstrlen: |
| 4554 | return Builtin::BIstrlen; |
| 4555 | |
| 4556 | case Builtin::BI__builtin_bzero: |
| 4557 | case Builtin::BIbzero: |
| 4558 | return Builtin::BIbzero; |
| 4559 | |
| 4560 | case Builtin::BI__builtin_bcopy: |
| 4561 | case Builtin::BIbcopy: |
| 4562 | return Builtin::BIbcopy; |
| 4563 | |
| 4564 | case Builtin::BIfree: |
| 4565 | return Builtin::BIfree; |
| 4566 | |
| 4567 | default: |
| 4568 | if (isExternC()) { |
| 4569 | if (FnInfo->isStr(Str: "memset" )) |
| 4570 | return Builtin::BImemset; |
| 4571 | if (FnInfo->isStr(Str: "memcpy" )) |
| 4572 | return Builtin::BImemcpy; |
| 4573 | if (FnInfo->isStr(Str: "mempcpy" )) |
| 4574 | return Builtin::BImempcpy; |
| 4575 | if (FnInfo->isStr(Str: "memmove" )) |
| 4576 | return Builtin::BImemmove; |
| 4577 | if (FnInfo->isStr(Str: "memcmp" )) |
| 4578 | return Builtin::BImemcmp; |
| 4579 | if (FnInfo->isStr(Str: "bcmp" )) |
| 4580 | return Builtin::BIbcmp; |
| 4581 | if (FnInfo->isStr(Str: "strncpy" )) |
| 4582 | return Builtin::BIstrncpy; |
| 4583 | if (FnInfo->isStr(Str: "strncmp" )) |
| 4584 | return Builtin::BIstrncmp; |
| 4585 | if (FnInfo->isStr(Str: "strncasecmp" )) |
| 4586 | return Builtin::BIstrncasecmp; |
| 4587 | if (FnInfo->isStr(Str: "strncat" )) |
| 4588 | return Builtin::BIstrncat; |
| 4589 | if (FnInfo->isStr(Str: "strndup" )) |
| 4590 | return Builtin::BIstrndup; |
| 4591 | if (FnInfo->isStr(Str: "strlen" )) |
| 4592 | return Builtin::BIstrlen; |
| 4593 | if (FnInfo->isStr(Str: "bzero" )) |
| 4594 | return Builtin::BIbzero; |
| 4595 | if (FnInfo->isStr(Str: "bcopy" )) |
| 4596 | return Builtin::BIbcopy; |
| 4597 | } else if (isInStdNamespace()) { |
| 4598 | if (FnInfo->isStr(Str: "free" )) |
| 4599 | return Builtin::BIfree; |
| 4600 | } |
| 4601 | break; |
| 4602 | } |
| 4603 | return 0; |
| 4604 | } |
| 4605 | |
| 4606 | unsigned FunctionDecl::getODRHash() const { |
| 4607 | assert(hasODRHash()); |
| 4608 | return ODRHash; |
| 4609 | } |
| 4610 | |
| 4611 | unsigned FunctionDecl::getODRHash() { |
| 4612 | if (hasODRHash()) |
| 4613 | return ODRHash; |
| 4614 | |
| 4615 | if (auto *FT = getInstantiatedFromMemberFunction()) { |
| 4616 | setHasODRHash(true); |
| 4617 | ODRHash = FT->getODRHash(); |
| 4618 | return ODRHash; |
| 4619 | } |
| 4620 | |
| 4621 | class ODRHash Hash; |
| 4622 | Hash.AddFunctionDecl(Function: this); |
| 4623 | setHasODRHash(true); |
| 4624 | ODRHash = Hash.CalculateHash(); |
| 4625 | return ODRHash; |
| 4626 | } |
| 4627 | |
| 4628 | //===----------------------------------------------------------------------===// |
| 4629 | // FieldDecl Implementation |
| 4630 | //===----------------------------------------------------------------------===// |
| 4631 | |
| 4632 | FieldDecl *FieldDecl::Create(const ASTContext &C, DeclContext *DC, |
| 4633 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4634 | const IdentifierInfo *Id, QualType T, |
| 4635 | TypeSourceInfo *TInfo, Expr *BW, bool Mutable, |
| 4636 | InClassInitStyle InitStyle) { |
| 4637 | return new (C, DC) FieldDecl(Decl::Field, DC, StartLoc, IdLoc, Id, T, TInfo, |
| 4638 | BW, Mutable, InitStyle); |
| 4639 | } |
| 4640 | |
| 4641 | FieldDecl *FieldDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 4642 | return new (C, ID) FieldDecl(Field, nullptr, SourceLocation(), |
| 4643 | SourceLocation(), nullptr, QualType(), nullptr, |
| 4644 | nullptr, false, ICIS_NoInit); |
| 4645 | } |
| 4646 | |
| 4647 | bool FieldDecl::isAnonymousStructOrUnion() const { |
| 4648 | if (!isImplicit() || getDeclName()) |
| 4649 | return false; |
| 4650 | |
| 4651 | if (const auto *Record = getType()->getAs<RecordType>()) |
| 4652 | return Record->getDecl()->isAnonymousStructOrUnion(); |
| 4653 | |
| 4654 | return false; |
| 4655 | } |
| 4656 | |
| 4657 | Expr *FieldDecl::getInClassInitializer() const { |
| 4658 | if (!hasInClassInitializer()) |
| 4659 | return nullptr; |
| 4660 | |
| 4661 | LazyDeclStmtPtr InitPtr = BitField ? InitAndBitWidth->Init : Init; |
| 4662 | return cast_if_present<Expr>( |
| 4663 | Val: InitPtr.isOffset() ? InitPtr.get(Source: getASTContext().getExternalSource()) |
| 4664 | : InitPtr.get(Source: nullptr)); |
| 4665 | } |
| 4666 | |
| 4667 | void FieldDecl::setInClassInitializer(Expr *NewInit) { |
| 4668 | setLazyInClassInitializer(LazyDeclStmtPtr(NewInit)); |
| 4669 | } |
| 4670 | |
| 4671 | void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) { |
| 4672 | assert(hasInClassInitializer() && !getInClassInitializer()); |
| 4673 | if (BitField) |
| 4674 | InitAndBitWidth->Init = NewInit; |
| 4675 | else |
| 4676 | Init = NewInit; |
| 4677 | } |
| 4678 | |
| 4679 | unsigned FieldDecl::getBitWidthValue() const { |
| 4680 | assert(isBitField() && "not a bitfield" ); |
| 4681 | assert(isa<ConstantExpr>(getBitWidth())); |
| 4682 | assert(cast<ConstantExpr>(getBitWidth())->hasAPValueResult()); |
| 4683 | assert(cast<ConstantExpr>(getBitWidth())->getAPValueResult().isInt()); |
| 4684 | return cast<ConstantExpr>(Val: getBitWidth()) |
| 4685 | ->getAPValueResult() |
| 4686 | .getInt() |
| 4687 | .getZExtValue(); |
| 4688 | } |
| 4689 | |
| 4690 | bool FieldDecl::isZeroLengthBitField() const { |
| 4691 | return isUnnamedBitField() && !getBitWidth()->isValueDependent() && |
| 4692 | getBitWidthValue() == 0; |
| 4693 | } |
| 4694 | |
| 4695 | bool FieldDecl::isZeroSize(const ASTContext &Ctx) const { |
| 4696 | if (isZeroLengthBitField()) |
| 4697 | return true; |
| 4698 | |
| 4699 | // C++2a [intro.object]p7: |
| 4700 | // An object has nonzero size if it |
| 4701 | // -- is not a potentially-overlapping subobject, or |
| 4702 | if (!hasAttr<NoUniqueAddressAttr>()) |
| 4703 | return false; |
| 4704 | |
| 4705 | // -- is not of class type, or |
| 4706 | const auto *RT = getType()->getAs<RecordType>(); |
| 4707 | if (!RT) |
| 4708 | return false; |
| 4709 | const RecordDecl *RD = RT->getDecl()->getDefinition(); |
| 4710 | if (!RD) { |
| 4711 | assert(isInvalidDecl() && "valid field has incomplete type" ); |
| 4712 | return false; |
| 4713 | } |
| 4714 | |
| 4715 | // -- [has] virtual member functions or virtual base classes, or |
| 4716 | // -- has subobjects of nonzero size or bit-fields of nonzero length |
| 4717 | const auto *CXXRD = cast<CXXRecordDecl>(Val: RD); |
| 4718 | if (!CXXRD->isEmpty()) |
| 4719 | return false; |
| 4720 | |
| 4721 | // Otherwise, [...] the circumstances under which the object has zero size |
| 4722 | // are implementation-defined. |
| 4723 | if (!Ctx.getTargetInfo().getCXXABI().isMicrosoft()) |
| 4724 | return true; |
| 4725 | |
| 4726 | // MS ABI: has nonzero size if it is a class type with class type fields, |
| 4727 | // whether or not they have nonzero size |
| 4728 | return !llvm::any_of(Range: CXXRD->fields(), P: [](const FieldDecl *Field) { |
| 4729 | return Field->getType()->getAs<RecordType>(); |
| 4730 | }); |
| 4731 | } |
| 4732 | |
| 4733 | bool FieldDecl::isPotentiallyOverlapping() const { |
| 4734 | return hasAttr<NoUniqueAddressAttr>() && getType()->getAsCXXRecordDecl(); |
| 4735 | } |
| 4736 | |
| 4737 | void FieldDecl::setCachedFieldIndex() const { |
| 4738 | assert(this == getCanonicalDecl() && |
| 4739 | "should be called on the canonical decl" ); |
| 4740 | |
| 4741 | unsigned Index = 0; |
| 4742 | const RecordDecl *RD = getParent()->getDefinition(); |
| 4743 | assert(RD && "requested index for field of struct with no definition" ); |
| 4744 | |
| 4745 | for (auto *Field : RD->fields()) { |
| 4746 | Field->getCanonicalDecl()->CachedFieldIndex = Index + 1; |
| 4747 | assert(Field->getCanonicalDecl()->CachedFieldIndex == Index + 1 && |
| 4748 | "overflow in field numbering" ); |
| 4749 | ++Index; |
| 4750 | } |
| 4751 | |
| 4752 | assert(CachedFieldIndex && "failed to find field in parent" ); |
| 4753 | } |
| 4754 | |
| 4755 | SourceRange FieldDecl::getSourceRange() const { |
| 4756 | const Expr *FinalExpr = getInClassInitializer(); |
| 4757 | if (!FinalExpr) |
| 4758 | FinalExpr = getBitWidth(); |
| 4759 | if (FinalExpr) |
| 4760 | return SourceRange(getInnerLocStart(), FinalExpr->getEndLoc()); |
| 4761 | return DeclaratorDecl::getSourceRange(); |
| 4762 | } |
| 4763 | |
| 4764 | void FieldDecl::setCapturedVLAType(const VariableArrayType *VLAType) { |
| 4765 | assert((getParent()->isLambda() || getParent()->isCapturedRecord()) && |
| 4766 | "capturing type in non-lambda or captured record." ); |
| 4767 | assert(StorageKind == ISK_NoInit && !BitField && |
| 4768 | "bit-field or field with default member initializer cannot capture " |
| 4769 | "VLA type" ); |
| 4770 | StorageKind = ISK_CapturedVLAType; |
| 4771 | CapturedVLAType = VLAType; |
| 4772 | } |
| 4773 | |
| 4774 | void FieldDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { |
| 4775 | // Print unnamed members using name of their type. |
| 4776 | if (isAnonymousStructOrUnion()) { |
| 4777 | this->getType().print(OS, Policy); |
| 4778 | return; |
| 4779 | } |
| 4780 | // Otherwise, do the normal printing. |
| 4781 | DeclaratorDecl::printName(OS, Policy); |
| 4782 | } |
| 4783 | |
| 4784 | const FieldDecl *FieldDecl::findCountedByField() const { |
| 4785 | const auto *CAT = getType()->getAs<CountAttributedType>(); |
| 4786 | if (!CAT) |
| 4787 | return nullptr; |
| 4788 | |
| 4789 | const auto *CountDRE = cast<DeclRefExpr>(Val: CAT->getCountExpr()); |
| 4790 | const auto *CountDecl = CountDRE->getDecl(); |
| 4791 | if (const auto *IFD = dyn_cast<IndirectFieldDecl>(Val: CountDecl)) |
| 4792 | CountDecl = IFD->getAnonField(); |
| 4793 | |
| 4794 | return dyn_cast<FieldDecl>(Val: CountDecl); |
| 4795 | } |
| 4796 | |
| 4797 | //===----------------------------------------------------------------------===// |
| 4798 | // TagDecl Implementation |
| 4799 | //===----------------------------------------------------------------------===// |
| 4800 | |
| 4801 | TagDecl::TagDecl(Kind DK, TagKind TK, const ASTContext &C, DeclContext *DC, |
| 4802 | SourceLocation L, IdentifierInfo *Id, TagDecl *PrevDecl, |
| 4803 | SourceLocation StartL) |
| 4804 | : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), redeclarable_base(C), |
| 4805 | TypedefNameDeclOrQualifier((TypedefNameDecl *)nullptr) { |
| 4806 | assert((DK != Enum || TK == TagTypeKind::Enum) && |
| 4807 | "EnumDecl not matched with TagTypeKind::Enum" ); |
| 4808 | setPreviousDecl(PrevDecl); |
| 4809 | setTagKind(TK); |
| 4810 | setCompleteDefinition(false); |
| 4811 | setBeingDefined(false); |
| 4812 | setEmbeddedInDeclarator(false); |
| 4813 | setFreeStanding(false); |
| 4814 | setCompleteDefinitionRequired(false); |
| 4815 | TagDeclBits.IsThisDeclarationADemotedDefinition = false; |
| 4816 | } |
| 4817 | |
| 4818 | SourceLocation TagDecl::getOuterLocStart() const { |
| 4819 | return getTemplateOrInnerLocStart(decl: this); |
| 4820 | } |
| 4821 | |
| 4822 | SourceRange TagDecl::getSourceRange() const { |
| 4823 | SourceLocation RBraceLoc = BraceRange.getEnd(); |
| 4824 | SourceLocation E = RBraceLoc.isValid() ? RBraceLoc : getLocation(); |
| 4825 | return SourceRange(getOuterLocStart(), E); |
| 4826 | } |
| 4827 | |
| 4828 | TagDecl *TagDecl::getCanonicalDecl() { return getFirstDecl(); } |
| 4829 | |
| 4830 | void TagDecl::setTypedefNameForAnonDecl(TypedefNameDecl *TDD) { |
| 4831 | TypedefNameDeclOrQualifier = TDD; |
| 4832 | if (const Type *T = getTypeForDecl()) { |
| 4833 | (void)T; |
| 4834 | assert(T->isLinkageValid()); |
| 4835 | } |
| 4836 | assert(isLinkageValid()); |
| 4837 | } |
| 4838 | |
| 4839 | void TagDecl::startDefinition() { |
| 4840 | setBeingDefined(true); |
| 4841 | |
| 4842 | if (auto *D = dyn_cast<CXXRecordDecl>(Val: this)) { |
| 4843 | struct CXXRecordDecl::DefinitionData *Data = |
| 4844 | new (getASTContext()) struct CXXRecordDecl::DefinitionData(D); |
| 4845 | for (auto *I : redecls()) |
| 4846 | cast<CXXRecordDecl>(Val: I)->DefinitionData = Data; |
| 4847 | } |
| 4848 | } |
| 4849 | |
| 4850 | void TagDecl::completeDefinition() { |
| 4851 | assert((!isa<CXXRecordDecl>(this) || |
| 4852 | cast<CXXRecordDecl>(this)->hasDefinition()) && |
| 4853 | "definition completed but not started" ); |
| 4854 | |
| 4855 | setCompleteDefinition(true); |
| 4856 | setBeingDefined(false); |
| 4857 | |
| 4858 | if (ASTMutationListener *L = getASTMutationListener()) |
| 4859 | L->CompletedTagDefinition(D: this); |
| 4860 | } |
| 4861 | |
| 4862 | TagDecl *TagDecl::getDefinition() const { |
| 4863 | if (isCompleteDefinition()) |
| 4864 | return const_cast<TagDecl *>(this); |
| 4865 | |
| 4866 | // If it's possible for us to have an out-of-date definition, check now. |
| 4867 | if (mayHaveOutOfDateDef()) { |
| 4868 | if (IdentifierInfo *II = getIdentifier()) { |
| 4869 | if (II->isOutOfDate()) { |
| 4870 | updateOutOfDate(II&: *II); |
| 4871 | } |
| 4872 | } |
| 4873 | } |
| 4874 | |
| 4875 | if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: this)) |
| 4876 | return CXXRD->getDefinition(); |
| 4877 | |
| 4878 | for (auto *R : redecls()) |
| 4879 | if (R->isCompleteDefinition()) |
| 4880 | return R; |
| 4881 | |
| 4882 | return nullptr; |
| 4883 | } |
| 4884 | |
| 4885 | void TagDecl::setQualifierInfo(NestedNameSpecifierLoc QualifierLoc) { |
| 4886 | if (QualifierLoc) { |
| 4887 | // Make sure the extended qualifier info is allocated. |
| 4888 | if (!hasExtInfo()) |
| 4889 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
| 4890 | // Set qualifier info. |
| 4891 | getExtInfo()->QualifierLoc = QualifierLoc; |
| 4892 | } else { |
| 4893 | // Here Qualifier == 0, i.e., we are removing the qualifier (if any). |
| 4894 | if (hasExtInfo()) { |
| 4895 | if (getExtInfo()->NumTemplParamLists == 0) { |
| 4896 | getASTContext().Deallocate(Ptr: getExtInfo()); |
| 4897 | TypedefNameDeclOrQualifier = (TypedefNameDecl *)nullptr; |
| 4898 | } |
| 4899 | else |
| 4900 | getExtInfo()->QualifierLoc = QualifierLoc; |
| 4901 | } |
| 4902 | } |
| 4903 | } |
| 4904 | |
| 4905 | void TagDecl::printName(raw_ostream &OS, const PrintingPolicy &Policy) const { |
| 4906 | DeclarationName Name = getDeclName(); |
| 4907 | // If the name is supposed to have an identifier but does not have one, then |
| 4908 | // the tag is anonymous and we should print it differently. |
| 4909 | if (Name.isIdentifier() && !Name.getAsIdentifierInfo()) { |
| 4910 | // If the caller wanted to print a qualified name, they've already printed |
| 4911 | // the scope. And if the caller doesn't want that, the scope information |
| 4912 | // is already printed as part of the type. |
| 4913 | PrintingPolicy Copy(Policy); |
| 4914 | Copy.SuppressScope = true; |
| 4915 | getASTContext().getTagDeclType(Decl: this).print(OS, Policy: Copy); |
| 4916 | return; |
| 4917 | } |
| 4918 | // Otherwise, do the normal printing. |
| 4919 | Name.print(OS, Policy); |
| 4920 | } |
| 4921 | |
| 4922 | void TagDecl::setTemplateParameterListsInfo( |
| 4923 | ASTContext &Context, ArrayRef<TemplateParameterList *> TPLists) { |
| 4924 | assert(!TPLists.empty()); |
| 4925 | // Make sure the extended decl info is allocated. |
| 4926 | if (!hasExtInfo()) |
| 4927 | // Allocate external info struct. |
| 4928 | TypedefNameDeclOrQualifier = new (getASTContext()) ExtInfo; |
| 4929 | // Set the template parameter lists info. |
| 4930 | getExtInfo()->setTemplateParameterListsInfo(Context, TPLists); |
| 4931 | } |
| 4932 | |
| 4933 | //===----------------------------------------------------------------------===// |
| 4934 | // EnumDecl Implementation |
| 4935 | //===----------------------------------------------------------------------===// |
| 4936 | |
| 4937 | EnumDecl::EnumDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 4938 | SourceLocation IdLoc, IdentifierInfo *Id, EnumDecl *PrevDecl, |
| 4939 | bool Scoped, bool ScopedUsingClassTag, bool Fixed) |
| 4940 | : TagDecl(Enum, TagTypeKind::Enum, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
| 4941 | assert(Scoped || !ScopedUsingClassTag); |
| 4942 | IntegerType = nullptr; |
| 4943 | setNumPositiveBits(0); |
| 4944 | setNumNegativeBits(0); |
| 4945 | setScoped(Scoped); |
| 4946 | setScopedUsingClassTag(ScopedUsingClassTag); |
| 4947 | setFixed(Fixed); |
| 4948 | setHasODRHash(false); |
| 4949 | ODRHash = 0; |
| 4950 | } |
| 4951 | |
| 4952 | void EnumDecl::anchor() {} |
| 4953 | |
| 4954 | EnumDecl *EnumDecl::Create(ASTContext &C, DeclContext *DC, |
| 4955 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 4956 | IdentifierInfo *Id, |
| 4957 | EnumDecl *PrevDecl, bool IsScoped, |
| 4958 | bool IsScopedUsingClassTag, bool IsFixed) { |
| 4959 | auto *Enum = new (C, DC) EnumDecl(C, DC, StartLoc, IdLoc, Id, PrevDecl, |
| 4960 | IsScoped, IsScopedUsingClassTag, IsFixed); |
| 4961 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| 4962 | C.getTypeDeclType(Decl: Enum, PrevDecl); |
| 4963 | return Enum; |
| 4964 | } |
| 4965 | |
| 4966 | EnumDecl *EnumDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 4967 | EnumDecl *Enum = |
| 4968 | new (C, ID) EnumDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 4969 | nullptr, nullptr, false, false, false); |
| 4970 | Enum->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| 4971 | return Enum; |
| 4972 | } |
| 4973 | |
| 4974 | SourceRange EnumDecl::getIntegerTypeRange() const { |
| 4975 | if (const TypeSourceInfo *TI = getIntegerTypeSourceInfo()) |
| 4976 | return TI->getTypeLoc().getSourceRange(); |
| 4977 | return SourceRange(); |
| 4978 | } |
| 4979 | |
| 4980 | void EnumDecl::completeDefinition(QualType NewType, |
| 4981 | QualType NewPromotionType, |
| 4982 | unsigned NumPositiveBits, |
| 4983 | unsigned NumNegativeBits) { |
| 4984 | assert(!isCompleteDefinition() && "Cannot redefine enums!" ); |
| 4985 | if (!IntegerType) |
| 4986 | IntegerType = NewType.getTypePtr(); |
| 4987 | PromotionType = NewPromotionType; |
| 4988 | setNumPositiveBits(NumPositiveBits); |
| 4989 | setNumNegativeBits(NumNegativeBits); |
| 4990 | TagDecl::completeDefinition(); |
| 4991 | } |
| 4992 | |
| 4993 | bool EnumDecl::isClosed() const { |
| 4994 | if (const auto *A = getAttr<EnumExtensibilityAttr>()) |
| 4995 | return A->getExtensibility() == EnumExtensibilityAttr::Closed; |
| 4996 | return true; |
| 4997 | } |
| 4998 | |
| 4999 | bool EnumDecl::isClosedFlag() const { |
| 5000 | return isClosed() && hasAttr<FlagEnumAttr>(); |
| 5001 | } |
| 5002 | |
| 5003 | bool EnumDecl::isClosedNonFlag() const { |
| 5004 | return isClosed() && !hasAttr<FlagEnumAttr>(); |
| 5005 | } |
| 5006 | |
| 5007 | TemplateSpecializationKind EnumDecl::getTemplateSpecializationKind() const { |
| 5008 | if (MemberSpecializationInfo *MSI = getMemberSpecializationInfo()) |
| 5009 | return MSI->getTemplateSpecializationKind(); |
| 5010 | |
| 5011 | return TSK_Undeclared; |
| 5012 | } |
| 5013 | |
| 5014 | void EnumDecl::setTemplateSpecializationKind(TemplateSpecializationKind TSK, |
| 5015 | SourceLocation PointOfInstantiation) { |
| 5016 | MemberSpecializationInfo *MSI = getMemberSpecializationInfo(); |
| 5017 | assert(MSI && "Not an instantiated member enumeration?" ); |
| 5018 | MSI->setTemplateSpecializationKind(TSK); |
| 5019 | if (TSK != TSK_ExplicitSpecialization && |
| 5020 | PointOfInstantiation.isValid() && |
| 5021 | MSI->getPointOfInstantiation().isInvalid()) |
| 5022 | MSI->setPointOfInstantiation(PointOfInstantiation); |
| 5023 | } |
| 5024 | |
| 5025 | EnumDecl *EnumDecl::getTemplateInstantiationPattern() const { |
| 5026 | if (MemberSpecializationInfo *MSInfo = getMemberSpecializationInfo()) { |
| 5027 | if (isTemplateInstantiation(Kind: MSInfo->getTemplateSpecializationKind())) { |
| 5028 | EnumDecl *ED = getInstantiatedFromMemberEnum(); |
| 5029 | while (auto *NewED = ED->getInstantiatedFromMemberEnum()) |
| 5030 | ED = NewED; |
| 5031 | return getDefinitionOrSelf(D: ED); |
| 5032 | } |
| 5033 | } |
| 5034 | |
| 5035 | assert(!isTemplateInstantiation(getTemplateSpecializationKind()) && |
| 5036 | "couldn't find pattern for enum instantiation" ); |
| 5037 | return nullptr; |
| 5038 | } |
| 5039 | |
| 5040 | EnumDecl *EnumDecl::getInstantiatedFromMemberEnum() const { |
| 5041 | if (SpecializationInfo) |
| 5042 | return cast<EnumDecl>(Val: SpecializationInfo->getInstantiatedFrom()); |
| 5043 | |
| 5044 | return nullptr; |
| 5045 | } |
| 5046 | |
| 5047 | void EnumDecl::setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, |
| 5048 | TemplateSpecializationKind TSK) { |
| 5049 | assert(!SpecializationInfo && "Member enum is already a specialization" ); |
| 5050 | SpecializationInfo = new (C) MemberSpecializationInfo(ED, TSK); |
| 5051 | } |
| 5052 | |
| 5053 | unsigned EnumDecl::getODRHash() { |
| 5054 | if (hasODRHash()) |
| 5055 | return ODRHash; |
| 5056 | |
| 5057 | class ODRHash Hash; |
| 5058 | Hash.AddEnumDecl(Enum: this); |
| 5059 | setHasODRHash(true); |
| 5060 | ODRHash = Hash.CalculateHash(); |
| 5061 | return ODRHash; |
| 5062 | } |
| 5063 | |
| 5064 | SourceRange EnumDecl::getSourceRange() const { |
| 5065 | auto Res = TagDecl::getSourceRange(); |
| 5066 | // Set end-point to enum-base, e.g. enum foo : ^bar |
| 5067 | if (auto *TSI = getIntegerTypeSourceInfo()) { |
| 5068 | // TagDecl doesn't know about the enum base. |
| 5069 | if (!getBraceRange().getEnd().isValid()) |
| 5070 | Res.setEnd(TSI->getTypeLoc().getEndLoc()); |
| 5071 | } |
| 5072 | return Res; |
| 5073 | } |
| 5074 | |
| 5075 | void EnumDecl::getValueRange(llvm::APInt &Max, llvm::APInt &Min) const { |
| 5076 | unsigned Bitwidth = getASTContext().getIntWidth(T: getIntegerType()); |
| 5077 | unsigned NumNegativeBits = getNumNegativeBits(); |
| 5078 | unsigned NumPositiveBits = getNumPositiveBits(); |
| 5079 | |
| 5080 | if (NumNegativeBits) { |
| 5081 | unsigned NumBits = std::max(a: NumNegativeBits, b: NumPositiveBits + 1); |
| 5082 | Max = llvm::APInt(Bitwidth, 1) << (NumBits - 1); |
| 5083 | Min = -Max; |
| 5084 | } else { |
| 5085 | Max = llvm::APInt(Bitwidth, 1) << NumPositiveBits; |
| 5086 | Min = llvm::APInt::getZero(numBits: Bitwidth); |
| 5087 | } |
| 5088 | } |
| 5089 | |
| 5090 | //===----------------------------------------------------------------------===// |
| 5091 | // RecordDecl Implementation |
| 5092 | //===----------------------------------------------------------------------===// |
| 5093 | |
| 5094 | RecordDecl::RecordDecl(Kind DK, TagKind TK, const ASTContext &C, |
| 5095 | DeclContext *DC, SourceLocation StartLoc, |
| 5096 | SourceLocation IdLoc, IdentifierInfo *Id, |
| 5097 | RecordDecl *PrevDecl) |
| 5098 | : TagDecl(DK, TK, C, DC, IdLoc, Id, PrevDecl, StartLoc) { |
| 5099 | assert(classof(static_cast<Decl *>(this)) && "Invalid Kind!" ); |
| 5100 | setHasFlexibleArrayMember(false); |
| 5101 | setAnonymousStructOrUnion(false); |
| 5102 | setHasObjectMember(false); |
| 5103 | setHasVolatileMember(false); |
| 5104 | setHasLoadedFieldsFromExternalStorage(false); |
| 5105 | setNonTrivialToPrimitiveDefaultInitialize(false); |
| 5106 | setNonTrivialToPrimitiveCopy(false); |
| 5107 | setNonTrivialToPrimitiveDestroy(false); |
| 5108 | setHasNonTrivialToPrimitiveDefaultInitializeCUnion(false); |
| 5109 | setHasNonTrivialToPrimitiveDestructCUnion(false); |
| 5110 | setHasNonTrivialToPrimitiveCopyCUnion(false); |
| 5111 | setHasUninitializedExplicitInitFields(false); |
| 5112 | setParamDestroyedInCallee(false); |
| 5113 | setArgPassingRestrictions(RecordArgPassingKind::CanPassInRegs); |
| 5114 | setIsRandomized(false); |
| 5115 | setODRHash(0); |
| 5116 | } |
| 5117 | |
| 5118 | RecordDecl *RecordDecl::Create(const ASTContext &C, TagKind TK, DeclContext *DC, |
| 5119 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 5120 | IdentifierInfo *Id, RecordDecl* PrevDecl) { |
| 5121 | RecordDecl *R = new (C, DC) RecordDecl(Record, TK, C, DC, |
| 5122 | StartLoc, IdLoc, Id, PrevDecl); |
| 5123 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| 5124 | |
| 5125 | C.getTypeDeclType(Decl: R, PrevDecl); |
| 5126 | return R; |
| 5127 | } |
| 5128 | |
| 5129 | RecordDecl *RecordDecl::CreateDeserialized(const ASTContext &C, |
| 5130 | GlobalDeclID ID) { |
| 5131 | RecordDecl *R = new (C, ID) |
| 5132 | RecordDecl(Record, TagTypeKind::Struct, C, nullptr, SourceLocation(), |
| 5133 | SourceLocation(), nullptr, nullptr); |
| 5134 | R->setMayHaveOutOfDateDef(C.getLangOpts().Modules); |
| 5135 | return R; |
| 5136 | } |
| 5137 | |
| 5138 | bool RecordDecl::isInjectedClassName() const { |
| 5139 | return isImplicit() && getDeclName() && getDeclContext()->isRecord() && |
| 5140 | cast<RecordDecl>(Val: getDeclContext())->getDeclName() == getDeclName(); |
| 5141 | } |
| 5142 | |
| 5143 | bool RecordDecl::isLambda() const { |
| 5144 | if (auto RD = dyn_cast<CXXRecordDecl>(Val: this)) |
| 5145 | return RD->isLambda(); |
| 5146 | return false; |
| 5147 | } |
| 5148 | |
| 5149 | bool RecordDecl::isCapturedRecord() const { |
| 5150 | return hasAttr<CapturedRecordAttr>(); |
| 5151 | } |
| 5152 | |
| 5153 | void RecordDecl::setCapturedRecord() { |
| 5154 | addAttr(A: CapturedRecordAttr::CreateImplicit(Ctx&: getASTContext())); |
| 5155 | } |
| 5156 | |
| 5157 | bool RecordDecl::isOrContainsUnion() const { |
| 5158 | if (isUnion()) |
| 5159 | return true; |
| 5160 | |
| 5161 | if (const RecordDecl *Def = getDefinition()) { |
| 5162 | for (const FieldDecl *FD : Def->fields()) { |
| 5163 | const RecordType *RT = FD->getType()->getAs<RecordType>(); |
| 5164 | if (RT && RT->getDecl()->isOrContainsUnion()) |
| 5165 | return true; |
| 5166 | } |
| 5167 | } |
| 5168 | |
| 5169 | return false; |
| 5170 | } |
| 5171 | |
| 5172 | RecordDecl::field_iterator RecordDecl::field_begin() const { |
| 5173 | if (hasExternalLexicalStorage() && !hasLoadedFieldsFromExternalStorage()) |
| 5174 | LoadFieldsFromExternalStorage(); |
| 5175 | // This is necessary for correctness for C++ with modules. |
| 5176 | // FIXME: Come up with a test case that breaks without definition. |
| 5177 | if (RecordDecl *D = getDefinition(); D && D != this) |
| 5178 | return D->field_begin(); |
| 5179 | return field_iterator(decl_iterator(FirstDecl)); |
| 5180 | } |
| 5181 | |
| 5182 | /// completeDefinition - Notes that the definition of this type is now |
| 5183 | /// complete. |
| 5184 | void RecordDecl::completeDefinition() { |
| 5185 | assert(!isCompleteDefinition() && "Cannot redefine record!" ); |
| 5186 | TagDecl::completeDefinition(); |
| 5187 | |
| 5188 | ASTContext &Ctx = getASTContext(); |
| 5189 | |
| 5190 | // Layouts are dumped when computed, so if we are dumping for all complete |
| 5191 | // types, we need to force usage to get types that wouldn't be used elsewhere. |
| 5192 | // |
| 5193 | // If the type is dependent, then we can't compute its layout because there |
| 5194 | // is no way for us to know the size or alignment of a dependent type. Also |
| 5195 | // ignore declarations marked as invalid since 'getASTRecordLayout()' asserts |
| 5196 | // on that. |
| 5197 | if (Ctx.getLangOpts().DumpRecordLayoutsComplete && !isDependentType() && |
| 5198 | !isInvalidDecl()) |
| 5199 | (void)Ctx.getASTRecordLayout(D: this); |
| 5200 | } |
| 5201 | |
| 5202 | /// isMsStruct - Get whether or not this record uses ms_struct layout. |
| 5203 | /// This which can be turned on with an attribute, pragma, or the |
| 5204 | /// -mms-bitfields command-line option. |
| 5205 | bool RecordDecl::isMsStruct(const ASTContext &C) const { |
| 5206 | return hasAttr<MSStructAttr>() || C.getLangOpts().MSBitfields == 1; |
| 5207 | } |
| 5208 | |
| 5209 | void RecordDecl::reorderDecls(const SmallVectorImpl<Decl *> &Decls) { |
| 5210 | std::tie(args&: FirstDecl, args&: LastDecl) = DeclContext::BuildDeclChain(Decls, FieldsAlreadyLoaded: false); |
| 5211 | LastDecl->NextInContextAndBits.setPointer(nullptr); |
| 5212 | setIsRandomized(true); |
| 5213 | } |
| 5214 | |
| 5215 | void RecordDecl::LoadFieldsFromExternalStorage() const { |
| 5216 | ExternalASTSource *Source = getASTContext().getExternalSource(); |
| 5217 | assert(hasExternalLexicalStorage() && Source && "No external storage?" ); |
| 5218 | |
| 5219 | // Notify that we have a RecordDecl doing some initialization. |
| 5220 | ExternalASTSource::Deserializing TheFields(Source); |
| 5221 | |
| 5222 | SmallVector<Decl*, 64> Decls; |
| 5223 | setHasLoadedFieldsFromExternalStorage(true); |
| 5224 | Source->FindExternalLexicalDecls(DC: this, IsKindWeWant: [](Decl::Kind K) { |
| 5225 | return FieldDecl::classofKind(K) || IndirectFieldDecl::classofKind(K); |
| 5226 | }, Result&: Decls); |
| 5227 | |
| 5228 | #ifndef NDEBUG |
| 5229 | // Check that all decls we got were FieldDecls. |
| 5230 | for (unsigned i=0, e=Decls.size(); i != e; ++i) |
| 5231 | assert(isa<FieldDecl>(Decls[i]) || isa<IndirectFieldDecl>(Decls[i])); |
| 5232 | #endif |
| 5233 | |
| 5234 | if (Decls.empty()) |
| 5235 | return; |
| 5236 | |
| 5237 | auto [ExternalFirst, ExternalLast] = |
| 5238 | BuildDeclChain(Decls, |
| 5239 | /*FieldsAlreadyLoaded=*/false); |
| 5240 | ExternalLast->NextInContextAndBits.setPointer(FirstDecl); |
| 5241 | FirstDecl = ExternalFirst; |
| 5242 | if (!LastDecl) |
| 5243 | LastDecl = ExternalLast; |
| 5244 | } |
| 5245 | |
| 5246 | bool RecordDecl::(bool ) const { |
| 5247 | ASTContext &Context = getASTContext(); |
| 5248 | const SanitizerMask EnabledAsanMask = Context.getLangOpts().Sanitize.Mask & |
| 5249 | (SanitizerKind::Address | SanitizerKind::KernelAddress); |
| 5250 | if (!EnabledAsanMask || !Context.getLangOpts().SanitizeAddressFieldPadding) |
| 5251 | return false; |
| 5252 | const auto &NoSanitizeList = Context.getNoSanitizeList(); |
| 5253 | const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: this); |
| 5254 | // We may be able to relax some of these requirements. |
| 5255 | int ReasonToReject = -1; |
| 5256 | if (!CXXRD || CXXRD->isExternCContext()) |
| 5257 | ReasonToReject = 0; // is not C++. |
| 5258 | else if (CXXRD->hasAttr<PackedAttr>()) |
| 5259 | ReasonToReject = 1; // is packed. |
| 5260 | else if (CXXRD->isUnion()) |
| 5261 | ReasonToReject = 2; // is a union. |
| 5262 | else if (CXXRD->isTriviallyCopyable()) |
| 5263 | ReasonToReject = 3; // is trivially copyable. |
| 5264 | else if (CXXRD->hasTrivialDestructor()) |
| 5265 | ReasonToReject = 4; // has trivial destructor. |
| 5266 | else if (CXXRD->isStandardLayout()) |
| 5267 | ReasonToReject = 5; // is standard layout. |
| 5268 | else if (NoSanitizeList.containsLocation(Mask: EnabledAsanMask, Loc: getLocation(), |
| 5269 | Category: "field-padding" )) |
| 5270 | ReasonToReject = 6; // is in an excluded file. |
| 5271 | else if (NoSanitizeList.containsType( |
| 5272 | Mask: EnabledAsanMask, MangledTypeName: getQualifiedNameAsString(), Category: "field-padding" )) |
| 5273 | ReasonToReject = 7; // The type is excluded. |
| 5274 | |
| 5275 | if (EmitRemark) { |
| 5276 | if (ReasonToReject >= 0) |
| 5277 | Context.getDiagnostics().Report( |
| 5278 | Loc: getLocation(), |
| 5279 | DiagID: diag::remark_sanitize_address_insert_extra_padding_rejected) |
| 5280 | << getQualifiedNameAsString() << ReasonToReject; |
| 5281 | else |
| 5282 | Context.getDiagnostics().Report( |
| 5283 | Loc: getLocation(), |
| 5284 | DiagID: diag::remark_sanitize_address_insert_extra_padding_accepted) |
| 5285 | << getQualifiedNameAsString(); |
| 5286 | } |
| 5287 | return ReasonToReject < 0; |
| 5288 | } |
| 5289 | |
| 5290 | const FieldDecl *RecordDecl::findFirstNamedDataMember() const { |
| 5291 | for (const auto *I : fields()) { |
| 5292 | if (I->getIdentifier()) |
| 5293 | return I; |
| 5294 | |
| 5295 | if (const auto *RT = I->getType()->getAs<RecordType>()) |
| 5296 | if (const FieldDecl *NamedDataMember = |
| 5297 | RT->getDecl()->findFirstNamedDataMember()) |
| 5298 | return NamedDataMember; |
| 5299 | } |
| 5300 | |
| 5301 | // We didn't find a named data member. |
| 5302 | return nullptr; |
| 5303 | } |
| 5304 | |
| 5305 | unsigned RecordDecl::getODRHash() { |
| 5306 | if (hasODRHash()) |
| 5307 | return RecordDeclBits.ODRHash; |
| 5308 | |
| 5309 | // Only calculate hash on first call of getODRHash per record. |
| 5310 | ODRHash Hash; |
| 5311 | Hash.AddRecordDecl(Record: this); |
| 5312 | // For RecordDecl the ODRHash is stored in the remaining |
| 5313 | // bits of RecordDeclBits, adjust the hash to accommodate. |
| 5314 | static_assert(sizeof(Hash.CalculateHash()) * CHAR_BIT == 32); |
| 5315 | setODRHash(Hash.CalculateHash() >> (32 - NumOdrHashBits)); |
| 5316 | return RecordDeclBits.ODRHash; |
| 5317 | } |
| 5318 | |
| 5319 | //===----------------------------------------------------------------------===// |
| 5320 | // BlockDecl Implementation |
| 5321 | //===----------------------------------------------------------------------===// |
| 5322 | |
| 5323 | BlockDecl::BlockDecl(DeclContext *DC, SourceLocation CaretLoc) |
| 5324 | : Decl(Block, DC, CaretLoc), DeclContext(Block) { |
| 5325 | setIsVariadic(false); |
| 5326 | setCapturesCXXThis(false); |
| 5327 | setBlockMissingReturnType(true); |
| 5328 | setIsConversionFromLambda(false); |
| 5329 | setDoesNotEscape(false); |
| 5330 | setCanAvoidCopyToHeap(false); |
| 5331 | } |
| 5332 | |
| 5333 | void BlockDecl::setParams(ArrayRef<ParmVarDecl *> NewParamInfo) { |
| 5334 | assert(!ParamInfo && "Already has param info!" ); |
| 5335 | |
| 5336 | // Zero params -> null pointer. |
| 5337 | if (!NewParamInfo.empty()) { |
| 5338 | NumParams = NewParamInfo.size(); |
| 5339 | ParamInfo = new (getASTContext()) ParmVarDecl*[NewParamInfo.size()]; |
| 5340 | llvm::copy(Range&: NewParamInfo, Out: ParamInfo); |
| 5341 | } |
| 5342 | } |
| 5343 | |
| 5344 | void BlockDecl::setCaptures(ASTContext &Context, ArrayRef<Capture> Captures, |
| 5345 | bool CapturesCXXThis) { |
| 5346 | this->setCapturesCXXThis(CapturesCXXThis); |
| 5347 | this->NumCaptures = Captures.size(); |
| 5348 | |
| 5349 | if (Captures.empty()) { |
| 5350 | this->Captures = nullptr; |
| 5351 | return; |
| 5352 | } |
| 5353 | |
| 5354 | this->Captures = Captures.copy(A&: Context).data(); |
| 5355 | } |
| 5356 | |
| 5357 | bool BlockDecl::capturesVariable(const VarDecl *variable) const { |
| 5358 | for (const auto &I : captures()) |
| 5359 | // Only auto vars can be captured, so no redeclaration worries. |
| 5360 | if (I.getVariable() == variable) |
| 5361 | return true; |
| 5362 | |
| 5363 | return false; |
| 5364 | } |
| 5365 | |
| 5366 | SourceRange BlockDecl::getSourceRange() const { |
| 5367 | return SourceRange(getLocation(), Body ? Body->getEndLoc() : getLocation()); |
| 5368 | } |
| 5369 | |
| 5370 | //===----------------------------------------------------------------------===// |
| 5371 | // Other Decl Allocation/Deallocation Method Implementations |
| 5372 | //===----------------------------------------------------------------------===// |
| 5373 | |
| 5374 | void TranslationUnitDecl::anchor() {} |
| 5375 | |
| 5376 | TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { |
| 5377 | return new (C, (DeclContext *)nullptr) TranslationUnitDecl(C); |
| 5378 | } |
| 5379 | |
| 5380 | void TranslationUnitDecl::setAnonymousNamespace(NamespaceDecl *D) { |
| 5381 | AnonymousNamespace = D; |
| 5382 | |
| 5383 | if (ASTMutationListener *Listener = Ctx.getASTMutationListener()) |
| 5384 | Listener->AddedAnonymousNamespace(TU: this, AnonNamespace: D); |
| 5385 | } |
| 5386 | |
| 5387 | void PragmaCommentDecl::() {} |
| 5388 | |
| 5389 | PragmaCommentDecl *PragmaCommentDecl::(const ASTContext &C, |
| 5390 | TranslationUnitDecl *DC, |
| 5391 | SourceLocation , |
| 5392 | PragmaMSCommentKind , |
| 5393 | StringRef Arg) { |
| 5394 | PragmaCommentDecl *PCD = |
| 5395 | new (C, DC, additionalSizeToAlloc<char>(Counts: Arg.size() + 1)) |
| 5396 | PragmaCommentDecl(DC, CommentLoc, CommentKind); |
| 5397 | llvm::copy(Range&: Arg, Out: PCD->getTrailingObjects()); |
| 5398 | PCD->getTrailingObjects()[Arg.size()] = '\0'; |
| 5399 | return PCD; |
| 5400 | } |
| 5401 | |
| 5402 | PragmaCommentDecl *PragmaCommentDecl::(ASTContext &C, |
| 5403 | GlobalDeclID ID, |
| 5404 | unsigned ArgSize) { |
| 5405 | return new (C, ID, additionalSizeToAlloc<char>(Counts: ArgSize + 1)) |
| 5406 | PragmaCommentDecl(nullptr, SourceLocation(), PCK_Unknown); |
| 5407 | } |
| 5408 | |
| 5409 | void PragmaDetectMismatchDecl::anchor() {} |
| 5410 | |
| 5411 | PragmaDetectMismatchDecl * |
| 5412 | PragmaDetectMismatchDecl::Create(const ASTContext &C, TranslationUnitDecl *DC, |
| 5413 | SourceLocation Loc, StringRef Name, |
| 5414 | StringRef Value) { |
| 5415 | size_t ValueStart = Name.size() + 1; |
| 5416 | PragmaDetectMismatchDecl *PDMD = |
| 5417 | new (C, DC, additionalSizeToAlloc<char>(Counts: ValueStart + Value.size() + 1)) |
| 5418 | PragmaDetectMismatchDecl(DC, Loc, ValueStart); |
| 5419 | llvm::copy(Range&: Name, Out: PDMD->getTrailingObjects()); |
| 5420 | PDMD->getTrailingObjects()[Name.size()] = '\0'; |
| 5421 | llvm::copy(Range&: Value, Out: PDMD->getTrailingObjects() + ValueStart); |
| 5422 | PDMD->getTrailingObjects()[ValueStart + Value.size()] = '\0'; |
| 5423 | return PDMD; |
| 5424 | } |
| 5425 | |
| 5426 | PragmaDetectMismatchDecl * |
| 5427 | PragmaDetectMismatchDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID, |
| 5428 | unsigned NameValueSize) { |
| 5429 | return new (C, ID, additionalSizeToAlloc<char>(Counts: NameValueSize + 1)) |
| 5430 | PragmaDetectMismatchDecl(nullptr, SourceLocation(), 0); |
| 5431 | } |
| 5432 | |
| 5433 | void ExternCContextDecl::anchor() {} |
| 5434 | |
| 5435 | ExternCContextDecl *ExternCContextDecl::Create(const ASTContext &C, |
| 5436 | TranslationUnitDecl *DC) { |
| 5437 | return new (C, DC) ExternCContextDecl(DC); |
| 5438 | } |
| 5439 | |
| 5440 | void LabelDecl::anchor() {} |
| 5441 | |
| 5442 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 5443 | SourceLocation IdentL, IdentifierInfo *II) { |
| 5444 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, IdentL); |
| 5445 | } |
| 5446 | |
| 5447 | LabelDecl *LabelDecl::Create(ASTContext &C, DeclContext *DC, |
| 5448 | SourceLocation IdentL, IdentifierInfo *II, |
| 5449 | SourceLocation GnuLabelL) { |
| 5450 | assert(GnuLabelL != IdentL && "Use this only for GNU local labels" ); |
| 5451 | return new (C, DC) LabelDecl(DC, IdentL, II, nullptr, GnuLabelL); |
| 5452 | } |
| 5453 | |
| 5454 | LabelDecl *LabelDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 5455 | return new (C, ID) LabelDecl(nullptr, SourceLocation(), nullptr, nullptr, |
| 5456 | SourceLocation()); |
| 5457 | } |
| 5458 | |
| 5459 | void LabelDecl::setMSAsmLabel(StringRef Name) { |
| 5460 | char *Buffer = new (getASTContext(), 1) char[Name.size() + 1]; |
| 5461 | llvm::copy(Range&: Name, Out: Buffer); |
| 5462 | Buffer[Name.size()] = '\0'; |
| 5463 | MSAsmName = Buffer; |
| 5464 | } |
| 5465 | |
| 5466 | void ValueDecl::anchor() {} |
| 5467 | |
| 5468 | bool ValueDecl::isWeak() const { |
| 5469 | auto *MostRecent = getMostRecentDecl(); |
| 5470 | return MostRecent->hasAttr<WeakAttr>() || |
| 5471 | MostRecent->hasAttr<WeakRefAttr>() || isWeakImported(); |
| 5472 | } |
| 5473 | |
| 5474 | bool ValueDecl::isInitCapture() const { |
| 5475 | if (auto *Var = llvm::dyn_cast<VarDecl>(Val: this)) |
| 5476 | return Var->isInitCapture(); |
| 5477 | return false; |
| 5478 | } |
| 5479 | |
| 5480 | bool ValueDecl::isParameterPack() const { |
| 5481 | if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Val: this)) |
| 5482 | return NTTP->isParameterPack(); |
| 5483 | |
| 5484 | return isa_and_nonnull<PackExpansionType>(Val: getType().getTypePtrOrNull()); |
| 5485 | } |
| 5486 | |
| 5487 | void ImplicitParamDecl::anchor() {} |
| 5488 | |
| 5489 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, DeclContext *DC, |
| 5490 | SourceLocation IdLoc, |
| 5491 | IdentifierInfo *Id, QualType Type, |
| 5492 | ImplicitParamKind ParamKind) { |
| 5493 | return new (C, DC) ImplicitParamDecl(C, DC, IdLoc, Id, Type, ParamKind); |
| 5494 | } |
| 5495 | |
| 5496 | ImplicitParamDecl *ImplicitParamDecl::Create(ASTContext &C, QualType Type, |
| 5497 | ImplicitParamKind ParamKind) { |
| 5498 | return new (C, nullptr) ImplicitParamDecl(C, Type, ParamKind); |
| 5499 | } |
| 5500 | |
| 5501 | ImplicitParamDecl *ImplicitParamDecl::CreateDeserialized(ASTContext &C, |
| 5502 | GlobalDeclID ID) { |
| 5503 | return new (C, ID) ImplicitParamDecl(C, QualType(), ImplicitParamKind::Other); |
| 5504 | } |
| 5505 | |
| 5506 | FunctionDecl * |
| 5507 | FunctionDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, |
| 5508 | const DeclarationNameInfo &NameInfo, QualType T, |
| 5509 | TypeSourceInfo *TInfo, StorageClass SC, bool UsesFPIntrin, |
| 5510 | bool isInlineSpecified, bool hasWrittenPrototype, |
| 5511 | ConstexprSpecKind ConstexprKind, |
| 5512 | const AssociatedConstraint &TrailingRequiresClause) { |
| 5513 | FunctionDecl *New = new (C, DC) FunctionDecl( |
| 5514 | Function, C, DC, StartLoc, NameInfo, T, TInfo, SC, UsesFPIntrin, |
| 5515 | isInlineSpecified, ConstexprKind, TrailingRequiresClause); |
| 5516 | New->setHasWrittenPrototype(hasWrittenPrototype); |
| 5517 | return New; |
| 5518 | } |
| 5519 | |
| 5520 | FunctionDecl *FunctionDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 5521 | return new (C, ID) FunctionDecl( |
| 5522 | Function, C, nullptr, SourceLocation(), DeclarationNameInfo(), QualType(), |
| 5523 | nullptr, SC_None, false, false, ConstexprSpecKind::Unspecified, |
| 5524 | /*TrailingRequiresClause=*/{}); |
| 5525 | } |
| 5526 | |
| 5527 | bool FunctionDecl::isReferenceableKernel() const { |
| 5528 | return hasAttr<CUDAGlobalAttr>() || |
| 5529 | DeviceKernelAttr::isOpenCLSpelling(A: getAttr<DeviceKernelAttr>()); |
| 5530 | } |
| 5531 | |
| 5532 | BlockDecl *BlockDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 5533 | return new (C, DC) BlockDecl(DC, L); |
| 5534 | } |
| 5535 | |
| 5536 | BlockDecl *BlockDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 5537 | return new (C, ID) BlockDecl(nullptr, SourceLocation()); |
| 5538 | } |
| 5539 | |
| 5540 | OutlinedFunctionDecl::OutlinedFunctionDecl(DeclContext *DC, unsigned NumParams) |
| 5541 | : Decl(OutlinedFunction, DC, SourceLocation()), |
| 5542 | DeclContext(OutlinedFunction), NumParams(NumParams), |
| 5543 | BodyAndNothrow(nullptr, false) {} |
| 5544 | |
| 5545 | OutlinedFunctionDecl *OutlinedFunctionDecl::Create(ASTContext &C, |
| 5546 | DeclContext *DC, |
| 5547 | unsigned NumParams) { |
| 5548 | return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(Counts: NumParams)) |
| 5549 | OutlinedFunctionDecl(DC, NumParams); |
| 5550 | } |
| 5551 | |
| 5552 | OutlinedFunctionDecl * |
| 5553 | OutlinedFunctionDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID, |
| 5554 | unsigned NumParams) { |
| 5555 | return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(Counts: NumParams)) |
| 5556 | OutlinedFunctionDecl(nullptr, NumParams); |
| 5557 | } |
| 5558 | |
| 5559 | Stmt *OutlinedFunctionDecl::getBody() const { |
| 5560 | return BodyAndNothrow.getPointer(); |
| 5561 | } |
| 5562 | void OutlinedFunctionDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } |
| 5563 | |
| 5564 | bool OutlinedFunctionDecl::isNothrow() const { return BodyAndNothrow.getInt(); } |
| 5565 | void OutlinedFunctionDecl::setNothrow(bool Nothrow) { |
| 5566 | BodyAndNothrow.setInt(Nothrow); |
| 5567 | } |
| 5568 | |
| 5569 | CapturedDecl::CapturedDecl(DeclContext *DC, unsigned NumParams) |
| 5570 | : Decl(Captured, DC, SourceLocation()), DeclContext(Captured), |
| 5571 | NumParams(NumParams), ContextParam(0), BodyAndNothrow(nullptr, false) {} |
| 5572 | |
| 5573 | CapturedDecl *CapturedDecl::Create(ASTContext &C, DeclContext *DC, |
| 5574 | unsigned NumParams) { |
| 5575 | return new (C, DC, additionalSizeToAlloc<ImplicitParamDecl *>(Counts: NumParams)) |
| 5576 | CapturedDecl(DC, NumParams); |
| 5577 | } |
| 5578 | |
| 5579 | CapturedDecl *CapturedDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID, |
| 5580 | unsigned NumParams) { |
| 5581 | return new (C, ID, additionalSizeToAlloc<ImplicitParamDecl *>(Counts: NumParams)) |
| 5582 | CapturedDecl(nullptr, NumParams); |
| 5583 | } |
| 5584 | |
| 5585 | Stmt *CapturedDecl::getBody() const { return BodyAndNothrow.getPointer(); } |
| 5586 | void CapturedDecl::setBody(Stmt *B) { BodyAndNothrow.setPointer(B); } |
| 5587 | |
| 5588 | bool CapturedDecl::isNothrow() const { return BodyAndNothrow.getInt(); } |
| 5589 | void CapturedDecl::setNothrow(bool Nothrow) { BodyAndNothrow.setInt(Nothrow); } |
| 5590 | |
| 5591 | EnumConstantDecl::EnumConstantDecl(const ASTContext &C, DeclContext *DC, |
| 5592 | SourceLocation L, IdentifierInfo *Id, |
| 5593 | QualType T, Expr *E, const llvm::APSInt &V) |
| 5594 | : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt *)E) { |
| 5595 | setInitVal(C, V); |
| 5596 | } |
| 5597 | |
| 5598 | EnumConstantDecl *EnumConstantDecl::Create(ASTContext &C, EnumDecl *CD, |
| 5599 | SourceLocation L, |
| 5600 | IdentifierInfo *Id, QualType T, |
| 5601 | Expr *E, const llvm::APSInt &V) { |
| 5602 | return new (C, CD) EnumConstantDecl(C, CD, L, Id, T, E, V); |
| 5603 | } |
| 5604 | |
| 5605 | EnumConstantDecl *EnumConstantDecl::CreateDeserialized(ASTContext &C, |
| 5606 | GlobalDeclID ID) { |
| 5607 | return new (C, ID) EnumConstantDecl(C, nullptr, SourceLocation(), nullptr, |
| 5608 | QualType(), nullptr, llvm::APSInt()); |
| 5609 | } |
| 5610 | |
| 5611 | void IndirectFieldDecl::anchor() {} |
| 5612 | |
| 5613 | IndirectFieldDecl::IndirectFieldDecl(ASTContext &C, DeclContext *DC, |
| 5614 | SourceLocation L, DeclarationName N, |
| 5615 | QualType T, |
| 5616 | MutableArrayRef<NamedDecl *> CH) |
| 5617 | : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH.data()), |
| 5618 | ChainingSize(CH.size()) { |
| 5619 | // In C++, indirect field declarations conflict with tag declarations in the |
| 5620 | // same scope, so add them to IDNS_Tag so that tag redeclaration finds them. |
| 5621 | if (C.getLangOpts().CPlusPlus) |
| 5622 | IdentifierNamespace |= IDNS_Tag; |
| 5623 | } |
| 5624 | |
| 5625 | IndirectFieldDecl *IndirectFieldDecl::Create(ASTContext &C, DeclContext *DC, |
| 5626 | SourceLocation L, |
| 5627 | const IdentifierInfo *Id, |
| 5628 | QualType T, |
| 5629 | MutableArrayRef<NamedDecl *> CH) { |
| 5630 | return new (C, DC) IndirectFieldDecl(C, DC, L, Id, T, CH); |
| 5631 | } |
| 5632 | |
| 5633 | IndirectFieldDecl *IndirectFieldDecl::CreateDeserialized(ASTContext &C, |
| 5634 | GlobalDeclID ID) { |
| 5635 | return new (C, ID) IndirectFieldDecl(C, nullptr, SourceLocation(), |
| 5636 | DeclarationName(), QualType(), {}); |
| 5637 | } |
| 5638 | |
| 5639 | SourceRange EnumConstantDecl::getSourceRange() const { |
| 5640 | SourceLocation End = getLocation(); |
| 5641 | if (Init) |
| 5642 | End = Init->getEndLoc(); |
| 5643 | return SourceRange(getLocation(), End); |
| 5644 | } |
| 5645 | |
| 5646 | void TypeDecl::anchor() {} |
| 5647 | |
| 5648 | TypedefDecl *TypedefDecl::Create(ASTContext &C, DeclContext *DC, |
| 5649 | SourceLocation StartLoc, SourceLocation IdLoc, |
| 5650 | const IdentifierInfo *Id, |
| 5651 | TypeSourceInfo *TInfo) { |
| 5652 | return new (C, DC) TypedefDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
| 5653 | } |
| 5654 | |
| 5655 | void TypedefNameDecl::anchor() {} |
| 5656 | |
| 5657 | TagDecl *TypedefNameDecl::getAnonDeclWithTypedefName(bool AnyRedecl) const { |
| 5658 | if (auto *TT = getTypeSourceInfo()->getType()->getAs<TagType>()) { |
| 5659 | auto *OwningTypedef = TT->getDecl()->getTypedefNameForAnonDecl(); |
| 5660 | auto *ThisTypedef = this; |
| 5661 | if (AnyRedecl && OwningTypedef) { |
| 5662 | OwningTypedef = OwningTypedef->getCanonicalDecl(); |
| 5663 | ThisTypedef = ThisTypedef->getCanonicalDecl(); |
| 5664 | } |
| 5665 | if (OwningTypedef == ThisTypedef) |
| 5666 | return TT->getDecl(); |
| 5667 | } |
| 5668 | |
| 5669 | return nullptr; |
| 5670 | } |
| 5671 | |
| 5672 | bool TypedefNameDecl::isTransparentTagSlow() const { |
| 5673 | auto determineIsTransparent = [&]() { |
| 5674 | if (auto *TT = getUnderlyingType()->getAs<TagType>()) { |
| 5675 | if (auto *TD = TT->getDecl()) { |
| 5676 | if (TD->getName() != getName()) |
| 5677 | return false; |
| 5678 | SourceLocation TTLoc = getLocation(); |
| 5679 | SourceLocation TDLoc = TD->getLocation(); |
| 5680 | if (!TTLoc.isMacroID() || !TDLoc.isMacroID()) |
| 5681 | return false; |
| 5682 | SourceManager &SM = getASTContext().getSourceManager(); |
| 5683 | return SM.getSpellingLoc(Loc: TTLoc) == SM.getSpellingLoc(Loc: TDLoc); |
| 5684 | } |
| 5685 | } |
| 5686 | return false; |
| 5687 | }; |
| 5688 | |
| 5689 | bool isTransparent = determineIsTransparent(); |
| 5690 | MaybeModedTInfo.setInt((isTransparent << 1) | 1); |
| 5691 | return isTransparent; |
| 5692 | } |
| 5693 | |
| 5694 | TypedefDecl *TypedefDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 5695 | return new (C, ID) TypedefDecl(C, nullptr, SourceLocation(), SourceLocation(), |
| 5696 | nullptr, nullptr); |
| 5697 | } |
| 5698 | |
| 5699 | TypeAliasDecl *TypeAliasDecl::Create(ASTContext &C, DeclContext *DC, |
| 5700 | SourceLocation StartLoc, |
| 5701 | SourceLocation IdLoc, |
| 5702 | const IdentifierInfo *Id, |
| 5703 | TypeSourceInfo *TInfo) { |
| 5704 | return new (C, DC) TypeAliasDecl(C, DC, StartLoc, IdLoc, Id, TInfo); |
| 5705 | } |
| 5706 | |
| 5707 | TypeAliasDecl *TypeAliasDecl::CreateDeserialized(ASTContext &C, |
| 5708 | GlobalDeclID ID) { |
| 5709 | return new (C, ID) TypeAliasDecl(C, nullptr, SourceLocation(), |
| 5710 | SourceLocation(), nullptr, nullptr); |
| 5711 | } |
| 5712 | |
| 5713 | SourceRange TypedefDecl::getSourceRange() const { |
| 5714 | SourceLocation RangeEnd = getLocation(); |
| 5715 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) { |
| 5716 | if (typeIsPostfix(QT: TInfo->getType())) |
| 5717 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 5718 | } |
| 5719 | return SourceRange(getBeginLoc(), RangeEnd); |
| 5720 | } |
| 5721 | |
| 5722 | SourceRange TypeAliasDecl::getSourceRange() const { |
| 5723 | SourceLocation RangeEnd = getBeginLoc(); |
| 5724 | if (TypeSourceInfo *TInfo = getTypeSourceInfo()) |
| 5725 | RangeEnd = TInfo->getTypeLoc().getSourceRange().getEnd(); |
| 5726 | return SourceRange(getBeginLoc(), RangeEnd); |
| 5727 | } |
| 5728 | |
| 5729 | void FileScopeAsmDecl::anchor() {} |
| 5730 | |
| 5731 | FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC, |
| 5732 | Expr *Str, SourceLocation AsmLoc, |
| 5733 | SourceLocation RParenLoc) { |
| 5734 | return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc); |
| 5735 | } |
| 5736 | |
| 5737 | FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C, |
| 5738 | GlobalDeclID ID) { |
| 5739 | return new (C, ID) FileScopeAsmDecl(nullptr, nullptr, SourceLocation(), |
| 5740 | SourceLocation()); |
| 5741 | } |
| 5742 | |
| 5743 | std::string FileScopeAsmDecl::getAsmString() const { |
| 5744 | return GCCAsmStmt::ExtractStringFromGCCAsmStmtComponent(E: getAsmStringExpr()); |
| 5745 | } |
| 5746 | |
| 5747 | void TopLevelStmtDecl::anchor() {} |
| 5748 | |
| 5749 | TopLevelStmtDecl *TopLevelStmtDecl::Create(ASTContext &C, Stmt *Statement) { |
| 5750 | assert(C.getLangOpts().IncrementalExtensions && |
| 5751 | "Must be used only in incremental mode" ); |
| 5752 | |
| 5753 | SourceLocation Loc = Statement ? Statement->getBeginLoc() : SourceLocation(); |
| 5754 | DeclContext *DC = C.getTranslationUnitDecl(); |
| 5755 | |
| 5756 | return new (C, DC) TopLevelStmtDecl(DC, Loc, Statement); |
| 5757 | } |
| 5758 | |
| 5759 | TopLevelStmtDecl *TopLevelStmtDecl::CreateDeserialized(ASTContext &C, |
| 5760 | GlobalDeclID ID) { |
| 5761 | return new (C, ID) |
| 5762 | TopLevelStmtDecl(/*DC=*/nullptr, SourceLocation(), /*S=*/nullptr); |
| 5763 | } |
| 5764 | |
| 5765 | SourceRange TopLevelStmtDecl::getSourceRange() const { |
| 5766 | return SourceRange(getLocation(), Statement->getEndLoc()); |
| 5767 | } |
| 5768 | |
| 5769 | void TopLevelStmtDecl::setStmt(Stmt *S) { |
| 5770 | assert(S); |
| 5771 | Statement = S; |
| 5772 | setLocation(Statement->getBeginLoc()); |
| 5773 | } |
| 5774 | |
| 5775 | void EmptyDecl::anchor() {} |
| 5776 | |
| 5777 | EmptyDecl *EmptyDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L) { |
| 5778 | return new (C, DC) EmptyDecl(DC, L); |
| 5779 | } |
| 5780 | |
| 5781 | EmptyDecl *EmptyDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 5782 | return new (C, ID) EmptyDecl(nullptr, SourceLocation()); |
| 5783 | } |
| 5784 | |
| 5785 | HLSLBufferDecl::HLSLBufferDecl(DeclContext *DC, bool CBuffer, |
| 5786 | SourceLocation KwLoc, IdentifierInfo *ID, |
| 5787 | SourceLocation IDLoc, SourceLocation LBrace) |
| 5788 | : NamedDecl(Decl::Kind::HLSLBuffer, DC, IDLoc, DeclarationName(ID)), |
| 5789 | DeclContext(Decl::Kind::HLSLBuffer), LBraceLoc(LBrace), KwLoc(KwLoc), |
| 5790 | IsCBuffer(CBuffer), HasValidPackoffset(false), LayoutStruct(nullptr) {} |
| 5791 | |
| 5792 | HLSLBufferDecl *HLSLBufferDecl::Create(ASTContext &C, |
| 5793 | DeclContext *LexicalParent, bool CBuffer, |
| 5794 | SourceLocation KwLoc, IdentifierInfo *ID, |
| 5795 | SourceLocation IDLoc, |
| 5796 | SourceLocation LBrace) { |
| 5797 | // For hlsl like this |
| 5798 | // cbuffer A { |
| 5799 | // cbuffer B { |
| 5800 | // } |
| 5801 | // } |
| 5802 | // compiler should treat it as |
| 5803 | // cbuffer A { |
| 5804 | // } |
| 5805 | // cbuffer B { |
| 5806 | // } |
| 5807 | // FIXME: support nested buffers if required for back-compat. |
| 5808 | DeclContext *DC = LexicalParent; |
| 5809 | HLSLBufferDecl *Result = |
| 5810 | new (C, DC) HLSLBufferDecl(DC, CBuffer, KwLoc, ID, IDLoc, LBrace); |
| 5811 | return Result; |
| 5812 | } |
| 5813 | |
| 5814 | HLSLBufferDecl * |
| 5815 | HLSLBufferDecl::CreateDefaultCBuffer(ASTContext &C, DeclContext *LexicalParent, |
| 5816 | ArrayRef<Decl *> DefaultCBufferDecls) { |
| 5817 | DeclContext *DC = LexicalParent; |
| 5818 | IdentifierInfo *II = &C.Idents.get(Name: "$Globals" , TokenCode: tok::TokenKind::identifier); |
| 5819 | HLSLBufferDecl *Result = new (C, DC) HLSLBufferDecl( |
| 5820 | DC, true, SourceLocation(), II, SourceLocation(), SourceLocation()); |
| 5821 | Result->setImplicit(true); |
| 5822 | Result->setDefaultBufferDecls(DefaultCBufferDecls); |
| 5823 | return Result; |
| 5824 | } |
| 5825 | |
| 5826 | HLSLBufferDecl *HLSLBufferDecl::CreateDeserialized(ASTContext &C, |
| 5827 | GlobalDeclID ID) { |
| 5828 | return new (C, ID) HLSLBufferDecl(nullptr, false, SourceLocation(), nullptr, |
| 5829 | SourceLocation(), SourceLocation()); |
| 5830 | } |
| 5831 | |
| 5832 | void HLSLBufferDecl::addLayoutStruct(CXXRecordDecl *LS) { |
| 5833 | assert(LayoutStruct == nullptr && "layout struct has already been set" ); |
| 5834 | LayoutStruct = LS; |
| 5835 | addDecl(D: LS); |
| 5836 | } |
| 5837 | |
| 5838 | void HLSLBufferDecl::setDefaultBufferDecls(ArrayRef<Decl *> Decls) { |
| 5839 | assert(!Decls.empty()); |
| 5840 | assert(DefaultBufferDecls.empty() && "default decls are already set" ); |
| 5841 | assert(isImplicit() && |
| 5842 | "default decls can only be added to the implicit/default constant " |
| 5843 | "buffer $Globals" ); |
| 5844 | |
| 5845 | // allocate array for default decls with ASTContext allocator |
| 5846 | Decl **DeclsArray = new (getASTContext()) Decl *[Decls.size()]; |
| 5847 | llvm::copy(Range&: Decls, Out: DeclsArray); |
| 5848 | DefaultBufferDecls = ArrayRef<Decl *>(DeclsArray, Decls.size()); |
| 5849 | } |
| 5850 | |
| 5851 | HLSLBufferDecl::buffer_decl_iterator |
| 5852 | HLSLBufferDecl::buffer_decls_begin() const { |
| 5853 | return buffer_decl_iterator(llvm::iterator_range(DefaultBufferDecls.begin(), |
| 5854 | DefaultBufferDecls.end()), |
| 5855 | decl_range(decls_begin(), decls_end())); |
| 5856 | } |
| 5857 | |
| 5858 | HLSLBufferDecl::buffer_decl_iterator HLSLBufferDecl::buffer_decls_end() const { |
| 5859 | return buffer_decl_iterator( |
| 5860 | llvm::iterator_range(DefaultBufferDecls.end(), DefaultBufferDecls.end()), |
| 5861 | decl_range(decls_end(), decls_end())); |
| 5862 | } |
| 5863 | |
| 5864 | bool HLSLBufferDecl::buffer_decls_empty() { |
| 5865 | return DefaultBufferDecls.empty() && decls_empty(); |
| 5866 | } |
| 5867 | |
| 5868 | //===----------------------------------------------------------------------===// |
| 5869 | // HLSLRootSignatureDecl Implementation |
| 5870 | //===----------------------------------------------------------------------===// |
| 5871 | |
| 5872 | HLSLRootSignatureDecl::HLSLRootSignatureDecl( |
| 5873 | DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID, |
| 5874 | llvm::dxbc::RootSignatureVersion Version, unsigned NumElems) |
| 5875 | : NamedDecl(Decl::Kind::HLSLRootSignature, DC, Loc, DeclarationName(ID)), |
| 5876 | Version(Version), NumElems(NumElems) {} |
| 5877 | |
| 5878 | HLSLRootSignatureDecl *HLSLRootSignatureDecl::Create( |
| 5879 | ASTContext &C, DeclContext *DC, SourceLocation Loc, IdentifierInfo *ID, |
| 5880 | llvm::dxbc::RootSignatureVersion Version, |
| 5881 | ArrayRef<llvm::hlsl::rootsig::RootElement> RootElements) { |
| 5882 | HLSLRootSignatureDecl *RSDecl = |
| 5883 | new (C, DC, |
| 5884 | additionalSizeToAlloc<llvm::hlsl::rootsig::RootElement>( |
| 5885 | Counts: RootElements.size())) |
| 5886 | HLSLRootSignatureDecl(DC, Loc, ID, Version, RootElements.size()); |
| 5887 | auto *StoredElems = RSDecl->getElems(); |
| 5888 | llvm::uninitialized_copy(Src&: RootElements, Dst: StoredElems); |
| 5889 | return RSDecl; |
| 5890 | } |
| 5891 | |
| 5892 | HLSLRootSignatureDecl * |
| 5893 | HLSLRootSignatureDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 5894 | HLSLRootSignatureDecl *Result = new (C, ID) |
| 5895 | HLSLRootSignatureDecl(nullptr, SourceLocation(), nullptr, |
| 5896 | /*Version*/ llvm::dxbc::RootSignatureVersion::V1_1, |
| 5897 | /*NumElems=*/0); |
| 5898 | return Result; |
| 5899 | } |
| 5900 | |
| 5901 | //===----------------------------------------------------------------------===// |
| 5902 | // ImportDecl Implementation |
| 5903 | //===----------------------------------------------------------------------===// |
| 5904 | |
| 5905 | /// Retrieve the number of module identifiers needed to name the given |
| 5906 | /// module. |
| 5907 | static unsigned getNumModuleIdentifiers(Module *Mod) { |
| 5908 | unsigned Result = 1; |
| 5909 | while (Mod->Parent) { |
| 5910 | Mod = Mod->Parent; |
| 5911 | ++Result; |
| 5912 | } |
| 5913 | return Result; |
| 5914 | } |
| 5915 | |
| 5916 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
| 5917 | Module *Imported, |
| 5918 | ArrayRef<SourceLocation> IdentifierLocs) |
| 5919 | : Decl(Import, DC, StartLoc), ImportedModule(Imported), |
| 5920 | NextLocalImportAndComplete(nullptr, true) { |
| 5921 | assert(getNumModuleIdentifiers(Imported) == IdentifierLocs.size()); |
| 5922 | auto *StoredLocs = getTrailingObjects(); |
| 5923 | llvm::uninitialized_copy(Src&: IdentifierLocs, Dst: StoredLocs); |
| 5924 | } |
| 5925 | |
| 5926 | ImportDecl::ImportDecl(DeclContext *DC, SourceLocation StartLoc, |
| 5927 | Module *Imported, SourceLocation EndLoc) |
| 5928 | : Decl(Import, DC, StartLoc), ImportedModule(Imported), |
| 5929 | NextLocalImportAndComplete(nullptr, false) { |
| 5930 | *getTrailingObjects() = EndLoc; |
| 5931 | } |
| 5932 | |
| 5933 | ImportDecl *ImportDecl::Create(ASTContext &C, DeclContext *DC, |
| 5934 | SourceLocation StartLoc, Module *Imported, |
| 5935 | ArrayRef<SourceLocation> IdentifierLocs) { |
| 5936 | return new (C, DC, |
| 5937 | additionalSizeToAlloc<SourceLocation>(Counts: IdentifierLocs.size())) |
| 5938 | ImportDecl(DC, StartLoc, Imported, IdentifierLocs); |
| 5939 | } |
| 5940 | |
| 5941 | ImportDecl *ImportDecl::CreateImplicit(ASTContext &C, DeclContext *DC, |
| 5942 | SourceLocation StartLoc, |
| 5943 | Module *Imported, |
| 5944 | SourceLocation EndLoc) { |
| 5945 | ImportDecl *Import = new (C, DC, additionalSizeToAlloc<SourceLocation>(Counts: 1)) |
| 5946 | ImportDecl(DC, StartLoc, Imported, EndLoc); |
| 5947 | Import->setImplicit(); |
| 5948 | return Import; |
| 5949 | } |
| 5950 | |
| 5951 | ImportDecl *ImportDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID, |
| 5952 | unsigned NumLocations) { |
| 5953 | return new (C, ID, additionalSizeToAlloc<SourceLocation>(Counts: NumLocations)) |
| 5954 | ImportDecl(EmptyShell()); |
| 5955 | } |
| 5956 | |
| 5957 | ArrayRef<SourceLocation> ImportDecl::getIdentifierLocs() const { |
| 5958 | if (!isImportComplete()) |
| 5959 | return {}; |
| 5960 | |
| 5961 | return getTrailingObjects(N: getNumModuleIdentifiers(Mod: getImportedModule())); |
| 5962 | } |
| 5963 | |
| 5964 | SourceRange ImportDecl::getSourceRange() const { |
| 5965 | if (!isImportComplete()) |
| 5966 | return SourceRange(getLocation(), *getTrailingObjects()); |
| 5967 | |
| 5968 | return SourceRange(getLocation(), getIdentifierLocs().back()); |
| 5969 | } |
| 5970 | |
| 5971 | //===----------------------------------------------------------------------===// |
| 5972 | // ExportDecl Implementation |
| 5973 | //===----------------------------------------------------------------------===// |
| 5974 | |
| 5975 | void ExportDecl::anchor() {} |
| 5976 | |
| 5977 | ExportDecl *ExportDecl::Create(ASTContext &C, DeclContext *DC, |
| 5978 | SourceLocation ExportLoc) { |
| 5979 | return new (C, DC) ExportDecl(DC, ExportLoc); |
| 5980 | } |
| 5981 | |
| 5982 | ExportDecl *ExportDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID) { |
| 5983 | return new (C, ID) ExportDecl(nullptr, SourceLocation()); |
| 5984 | } |
| 5985 | |
| 5986 | bool clang::IsArmStreamingFunction(const FunctionDecl *FD, |
| 5987 | bool IncludeLocallyStreaming) { |
| 5988 | if (IncludeLocallyStreaming) |
| 5989 | if (FD->hasAttr<ArmLocallyStreamingAttr>()) |
| 5990 | return true; |
| 5991 | |
| 5992 | if (const Type *Ty = FD->getType().getTypePtrOrNull()) |
| 5993 | if (const auto *FPT = Ty->getAs<FunctionProtoType>()) |
| 5994 | if (FPT->getAArch64SMEAttributes() & |
| 5995 | FunctionType::SME_PStateSMEnabledMask) |
| 5996 | return true; |
| 5997 | |
| 5998 | return false; |
| 5999 | } |
| 6000 | |
| 6001 | bool clang::hasArmZAState(const FunctionDecl *FD) { |
| 6002 | const auto *T = FD->getType()->getAs<FunctionProtoType>(); |
| 6003 | return (T && FunctionType::getArmZAState(AttrBits: T->getAArch64SMEAttributes()) != |
| 6004 | FunctionType::ARM_None) || |
| 6005 | (FD->hasAttr<ArmNewAttr>() && FD->getAttr<ArmNewAttr>()->isNewZA()); |
| 6006 | } |
| 6007 | |
| 6008 | bool clang::hasArmZT0State(const FunctionDecl *FD) { |
| 6009 | const auto *T = FD->getType()->getAs<FunctionProtoType>(); |
| 6010 | return (T && FunctionType::getArmZT0State(AttrBits: T->getAArch64SMEAttributes()) != |
| 6011 | FunctionType::ARM_None) || |
| 6012 | (FD->hasAttr<ArmNewAttr>() && FD->getAttr<ArmNewAttr>()->isNewZT0()); |
| 6013 | } |
| 6014 | |