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