1 //===- TemplateName.h - C++ Template Name Representation --------*- 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 // This file defines the TemplateName interface and subclasses. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_AST_TEMPLATENAME_H 14 #define LLVM_CLANG_AST_TEMPLATENAME_H 15 16 #include "clang/AST/DependenceFlags.h" 17 #include "clang/AST/NestedNameSpecifier.h" 18 #include "clang/Basic/LLVM.h" 19 #include "llvm/ADT/FoldingSet.h" 20 #include "llvm/ADT/PointerIntPair.h" 21 #include "llvm/ADT/PointerUnion.h" 22 #include "llvm/Support/PointerLikeTypeTraits.h" 23 #include <cassert> 24 #include <optional> 25 26 namespace clang { 27 28 class ASTContext; 29 class Decl; 30 class DependentTemplateName; 31 class IdentifierInfo; 32 class NamedDecl; 33 class NestedNameSpecifier; 34 enum OverloadedOperatorKind : int; 35 class OverloadedTemplateStorage; 36 class AssumedTemplateStorage; 37 struct PrintingPolicy; 38 class QualifiedTemplateName; 39 class SubstTemplateTemplateParmPackStorage; 40 class SubstTemplateTemplateParmStorage; 41 class TemplateArgument; 42 class TemplateDecl; 43 class TemplateTemplateParmDecl; 44 class UsingShadowDecl; 45 46 /// Implementation class used to describe either a set of overloaded 47 /// template names or an already-substituted template template parameter pack. 48 class UncommonTemplateNameStorage { 49 protected: 50 enum Kind { 51 Overloaded, 52 Assumed, // defined in DeclarationName.h 53 SubstTemplateTemplateParm, 54 SubstTemplateTemplateParmPack 55 }; 56 57 struct BitsTag { 58 LLVM_PREFERRED_TYPE(Kind) 59 unsigned Kind : 2; 60 61 // The template parameter index. 62 unsigned Index : 15; 63 64 /// The pack index, or the number of stored templates 65 /// or template arguments, depending on which subclass we have. 66 unsigned Data : 15; 67 }; 68 69 union { 70 struct BitsTag Bits; 71 void *PointerAlignment; 72 }; 73 74 UncommonTemplateNameStorage(Kind Kind, unsigned Index, unsigned Data) { 75 Bits.Kind = Kind; 76 Bits.Index = Index; 77 Bits.Data = Data; 78 } 79 80 public: 81 OverloadedTemplateStorage *getAsOverloadedStorage() { 82 return Bits.Kind == Overloaded 83 ? reinterpret_cast<OverloadedTemplateStorage *>(this) 84 : nullptr; 85 } 86 87 AssumedTemplateStorage *getAsAssumedTemplateName() { 88 return Bits.Kind == Assumed 89 ? reinterpret_cast<AssumedTemplateStorage *>(this) 90 : nullptr; 91 } 92 93 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() { 94 return Bits.Kind == SubstTemplateTemplateParm 95 ? reinterpret_cast<SubstTemplateTemplateParmStorage *>(this) 96 : nullptr; 97 } 98 99 SubstTemplateTemplateParmPackStorage *getAsSubstTemplateTemplateParmPack() { 100 return Bits.Kind == SubstTemplateTemplateParmPack 101 ? reinterpret_cast<SubstTemplateTemplateParmPackStorage *>(this) 102 : nullptr; 103 } 104 }; 105 106 /// A structure for storing the information associated with an 107 /// overloaded template name. 108 class OverloadedTemplateStorage : public UncommonTemplateNameStorage { 109 friend class ASTContext; 110 111 OverloadedTemplateStorage(unsigned size) 112 : UncommonTemplateNameStorage(Overloaded, 0, size) {} 113 114 NamedDecl **getStorage() { 115 return reinterpret_cast<NamedDecl **>(this + 1); 116 } 117 NamedDecl * const *getStorage() const { 118 return reinterpret_cast<NamedDecl *const *>(this + 1); 119 } 120 121 public: 122 unsigned size() const { return Bits.Data; } 123 124 using iterator = NamedDecl *const *; 125 126 iterator begin() const { return getStorage(); } 127 iterator end() const { return getStorage() + Bits.Data; } 128 129 llvm::ArrayRef<NamedDecl*> decls() const { 130 return llvm::ArrayRef(begin(), end()); 131 } 132 }; 133 134 /// A structure for storing an already-substituted template template 135 /// parameter pack. 136 /// 137 /// This kind of template names occurs when the parameter pack has been 138 /// provided with a template template argument pack in a context where its 139 /// enclosing pack expansion could not be fully expanded. 140 class SubstTemplateTemplateParmPackStorage : public UncommonTemplateNameStorage, 141 public llvm::FoldingSetNode { 142 const TemplateArgument *Arguments; 143 llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndFinal; 144 145 public: 146 SubstTemplateTemplateParmPackStorage(ArrayRef<TemplateArgument> ArgPack, 147 Decl *AssociatedDecl, unsigned Index, 148 bool Final); 149 150 /// A template-like entity which owns the whole pattern being substituted. 151 /// This will own a set of template parameters. 152 Decl *getAssociatedDecl() const; 153 154 /// Returns the index of the replaced parameter in the associated declaration. 155 /// This should match the result of `getParameterPack()->getIndex()`. 156 unsigned getIndex() const { return Bits.Index; } 157 158 // When true the substitution will be 'Final' (subst node won't be placed). 159 bool getFinal() const; 160 161 /// Retrieve the template template parameter pack being substituted. 162 TemplateTemplateParmDecl *getParameterPack() const; 163 164 /// Retrieve the template template argument pack with which this 165 /// parameter was substituted. 166 TemplateArgument getArgumentPack() const; 167 168 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context); 169 170 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 171 const TemplateArgument &ArgPack, Decl *AssociatedDecl, 172 unsigned Index, bool Final); 173 }; 174 175 /// Represents a C++ template name within the type system. 176 /// 177 /// A C++ template name refers to a template within the C++ type 178 /// system. In most cases, a template name is simply a reference to a 179 /// class template, e.g. 180 /// 181 /// \code 182 /// template<typename T> class X { }; 183 /// 184 /// X<int> xi; 185 /// \endcode 186 /// 187 /// Here, the 'X' in \c X<int> is a template name that refers to the 188 /// declaration of the class template X, above. Template names can 189 /// also refer to function templates, C++0x template aliases, etc. 190 /// 191 /// Some template names are dependent. For example, consider: 192 /// 193 /// \code 194 /// template<typename MetaFun, typename T1, typename T2> struct apply2 { 195 /// typedef typename MetaFun::template apply<T1, T2>::type type; 196 /// }; 197 /// \endcode 198 /// 199 /// Here, "apply" is treated as a template name within the typename 200 /// specifier in the typedef. "apply" is a nested template, and can 201 /// only be understood in the context of a template instantiation, 202 /// hence is represented as a dependent template name. 203 class TemplateName { 204 // NameDecl is either a TemplateDecl or a UsingShadowDecl depending on the 205 // NameKind. 206 // !! There is no free low bits in 32-bit builds to discriminate more than 4 207 // pointer types in PointerUnion. 208 using StorageType = 209 llvm::PointerUnion<Decl *, UncommonTemplateNameStorage *, 210 QualifiedTemplateName *, DependentTemplateName *>; 211 212 StorageType Storage; 213 214 explicit TemplateName(void *Ptr); 215 216 public: 217 // Kind of name that is actually stored. 218 enum NameKind { 219 /// A single template declaration. 220 Template, 221 222 /// A set of overloaded template declarations. 223 OverloadedTemplate, 224 225 /// An unqualified-id that has been assumed to name a function template 226 /// that will be found by ADL. 227 AssumedTemplate, 228 229 /// A qualified template name, where the qualification is kept 230 /// to describe the source code as written. 231 QualifiedTemplate, 232 233 /// A dependent template name that has not been resolved to a 234 /// template (or set of templates). 235 DependentTemplate, 236 237 /// A template template parameter that has been substituted 238 /// for some other template name. 239 SubstTemplateTemplateParm, 240 241 /// A template template parameter pack that has been substituted for 242 /// a template template argument pack, but has not yet been expanded into 243 /// individual arguments. 244 SubstTemplateTemplateParmPack, 245 246 /// A template name that refers to a template declaration found through a 247 /// specific using shadow declaration. 248 UsingTemplate, 249 }; 250 251 TemplateName() = default; 252 explicit TemplateName(TemplateDecl *Template); 253 explicit TemplateName(OverloadedTemplateStorage *Storage); 254 explicit TemplateName(AssumedTemplateStorage *Storage); 255 explicit TemplateName(SubstTemplateTemplateParmStorage *Storage); 256 explicit TemplateName(SubstTemplateTemplateParmPackStorage *Storage); 257 explicit TemplateName(QualifiedTemplateName *Qual); 258 explicit TemplateName(DependentTemplateName *Dep); 259 explicit TemplateName(UsingShadowDecl *Using); 260 261 /// Determine whether this template name is NULL. 262 bool isNull() const; 263 264 // Get the kind of name that is actually stored. 265 NameKind getKind() const; 266 267 /// Retrieve the underlying template declaration that 268 /// this template name refers to, if known. 269 /// 270 /// \returns The template declaration that this template name refers 271 /// to, if any. If the template name does not refer to a specific 272 /// declaration because it is a dependent name, or if it refers to a 273 /// set of function templates, returns NULL. 274 TemplateDecl *getAsTemplateDecl() const; 275 276 /// Retrieve the underlying, overloaded function template 277 /// declarations that this template name refers to, if known. 278 /// 279 /// \returns The set of overloaded function templates that this template 280 /// name refers to, if known. If the template name does not refer to a 281 /// specific set of function templates because it is a dependent name or 282 /// refers to a single template, returns NULL. 283 OverloadedTemplateStorage *getAsOverloadedTemplate() const; 284 285 /// Retrieve information on a name that has been assumed to be a 286 /// template-name in order to permit a call via ADL. 287 AssumedTemplateStorage *getAsAssumedTemplateName() const; 288 289 /// Retrieve the substituted template template parameter, if 290 /// known. 291 /// 292 /// \returns The storage for the substituted template template parameter, 293 /// if known. Otherwise, returns NULL. 294 SubstTemplateTemplateParmStorage *getAsSubstTemplateTemplateParm() const; 295 296 /// Retrieve the substituted template template parameter pack, if 297 /// known. 298 /// 299 /// \returns The storage for the substituted template template parameter pack, 300 /// if known. Otherwise, returns NULL. 301 SubstTemplateTemplateParmPackStorage * 302 getAsSubstTemplateTemplateParmPack() const; 303 304 /// Retrieve the underlying qualified template name 305 /// structure, if any. 306 QualifiedTemplateName *getAsQualifiedTemplateName() const; 307 308 /// Retrieve the underlying dependent template name 309 /// structure, if any. 310 DependentTemplateName *getAsDependentTemplateName() const; 311 312 /// Retrieve the using shadow declaration through which the underlying 313 /// template declaration is introduced, if any. 314 UsingShadowDecl *getAsUsingShadowDecl() const; 315 316 TemplateName getUnderlying() const; 317 318 TemplateNameDependence getDependence() const; 319 320 /// Determines whether this is a dependent template name. 321 bool isDependent() const; 322 323 /// Determines whether this is a template name that somehow 324 /// depends on a template parameter. 325 bool isInstantiationDependent() const; 326 327 /// Determines whether this template name contains an 328 /// unexpanded parameter pack (for C++0x variadic templates). 329 bool containsUnexpandedParameterPack() const; 330 331 enum class Qualified { None, AsWritten }; 332 /// Print the template name. 333 /// 334 /// \param OS the output stream to which the template name will be 335 /// printed. 336 /// 337 /// \param Qual print the (Qualified::None) simple name, 338 /// (Qualified::AsWritten) any written (possibly partial) qualifier, or 339 /// (Qualified::Fully) the fully qualified name. 340 void print(raw_ostream &OS, const PrintingPolicy &Policy, 341 Qualified Qual = Qualified::AsWritten) const; 342 343 /// Debugging aid that dumps the template name. 344 void dump(raw_ostream &OS, const ASTContext &Context) const; 345 346 /// Debugging aid that dumps the template name to standard 347 /// error. 348 void dump() const; 349 350 void Profile(llvm::FoldingSetNodeID &ID); 351 352 /// Retrieve the template name as a void pointer. 353 void *getAsVoidPointer() const { return Storage.getOpaqueValue(); } 354 355 /// Build a template name from a void pointer. 356 static TemplateName getFromVoidPointer(void *Ptr) { 357 return TemplateName(Ptr); 358 } 359 360 /// Structural equality. 361 bool operator==(TemplateName Other) const { return Storage == Other.Storage; } 362 bool operator!=(TemplateName Other) const { return !operator==(Other); } 363 }; 364 365 /// Insertion operator for diagnostics. This allows sending TemplateName's 366 /// into a diagnostic with <<. 367 const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB, 368 TemplateName N); 369 370 /// A structure for storing the information associated with a 371 /// substituted template template parameter. 372 class SubstTemplateTemplateParmStorage 373 : public UncommonTemplateNameStorage, public llvm::FoldingSetNode { 374 friend class ASTContext; 375 376 TemplateName Replacement; 377 Decl *AssociatedDecl; 378 379 SubstTemplateTemplateParmStorage(TemplateName Replacement, 380 Decl *AssociatedDecl, unsigned Index, 381 std::optional<unsigned> PackIndex) 382 : UncommonTemplateNameStorage(SubstTemplateTemplateParm, Index, 383 PackIndex ? *PackIndex + 1 : 0), 384 Replacement(Replacement), AssociatedDecl(AssociatedDecl) { 385 assert(AssociatedDecl != nullptr); 386 } 387 388 public: 389 /// A template-like entity which owns the whole pattern being substituted. 390 /// This will own a set of template parameters. 391 Decl *getAssociatedDecl() const { return AssociatedDecl; } 392 393 /// Returns the index of the replaced parameter in the associated declaration. 394 /// This should match the result of `getParameter()->getIndex()`. 395 unsigned getIndex() const { return Bits.Index; } 396 397 std::optional<unsigned> getPackIndex() const { 398 if (Bits.Data == 0) 399 return std::nullopt; 400 return Bits.Data - 1; 401 } 402 403 TemplateTemplateParmDecl *getParameter() const; 404 TemplateName getReplacement() const { return Replacement; } 405 406 void Profile(llvm::FoldingSetNodeID &ID); 407 408 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName Replacement, 409 Decl *AssociatedDecl, unsigned Index, 410 std::optional<unsigned> PackIndex); 411 }; 412 413 inline TemplateName TemplateName::getUnderlying() const { 414 if (SubstTemplateTemplateParmStorage *subst 415 = getAsSubstTemplateTemplateParm()) 416 return subst->getReplacement().getUnderlying(); 417 return *this; 418 } 419 420 /// Represents a template name as written in source code. 421 /// 422 /// This kind of template name may refer to a template name that was 423 /// preceded by a nested name specifier, e.g., \c std::vector. Here, 424 /// the nested name specifier is "std::" and the template name is the 425 /// declaration for "vector". It may also have been written with the 426 /// 'template' keyword. The QualifiedTemplateName class is only 427 /// used to provide "sugar" for template names, so that they can 428 /// be differentiated from canonical template names. and has no 429 /// semantic meaning. In this manner, it is to TemplateName what 430 /// ElaboratedType is to Type, providing extra syntactic sugar 431 /// for downstream clients. 432 class QualifiedTemplateName : public llvm::FoldingSetNode { 433 friend class ASTContext; 434 435 /// The nested name specifier that qualifies the template name. 436 /// 437 /// The bit is used to indicate whether the "template" keyword was 438 /// present before the template name itself. Note that the 439 /// "template" keyword is always redundant in this case (otherwise, 440 /// the template name would be a dependent name and we would express 441 /// this name with DependentTemplateName). 442 llvm::PointerIntPair<NestedNameSpecifier *, 1> Qualifier; 443 444 /// The underlying template name, it is either 445 /// 1) a Template -- a template declaration that this qualified name refers 446 /// to. 447 /// 2) or a UsingTemplate -- a template declaration introduced by a 448 /// using-shadow declaration. 449 TemplateName UnderlyingTemplate; 450 451 QualifiedTemplateName(NestedNameSpecifier *NNS, bool TemplateKeyword, 452 TemplateName Template) 453 : Qualifier(NNS, TemplateKeyword ? 1 : 0), UnderlyingTemplate(Template) { 454 assert(UnderlyingTemplate.getKind() == TemplateName::Template || 455 UnderlyingTemplate.getKind() == TemplateName::UsingTemplate); 456 } 457 458 public: 459 /// Return the nested name specifier that qualifies this name. 460 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); } 461 462 /// Whether the template name was prefixed by the "template" 463 /// keyword. 464 bool hasTemplateKeyword() const { return Qualifier.getInt(); } 465 466 /// Return the underlying template name. 467 TemplateName getUnderlyingTemplate() const { return UnderlyingTemplate; } 468 469 void Profile(llvm::FoldingSetNodeID &ID) { 470 Profile(ID, getQualifier(), hasTemplateKeyword(), UnderlyingTemplate); 471 } 472 473 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, 474 bool TemplateKeyword, TemplateName TN) { 475 ID.AddPointer(NNS); 476 ID.AddBoolean(TemplateKeyword); 477 ID.AddPointer(TN.getAsVoidPointer()); 478 } 479 }; 480 481 /// Represents a dependent template name that cannot be 482 /// resolved prior to template instantiation. 483 /// 484 /// This kind of template name refers to a dependent template name, 485 /// including its nested name specifier (if any). For example, 486 /// DependentTemplateName can refer to "MetaFun::template apply", 487 /// where "MetaFun::" is the nested name specifier and "apply" is the 488 /// template name referenced. The "template" keyword is implied. 489 class DependentTemplateName : public llvm::FoldingSetNode { 490 friend class ASTContext; 491 492 /// The nested name specifier that qualifies the template 493 /// name. 494 /// 495 /// The bit stored in this qualifier describes whether the \c Name field 496 /// is interpreted as an IdentifierInfo pointer (when clear) or as an 497 /// overloaded operator kind (when set). 498 llvm::PointerIntPair<NestedNameSpecifier *, 1, bool> Qualifier; 499 500 /// The dependent template name. 501 union { 502 /// The identifier template name. 503 /// 504 /// Only valid when the bit on \c Qualifier is clear. 505 const IdentifierInfo *Identifier; 506 507 /// The overloaded operator name. 508 /// 509 /// Only valid when the bit on \c Qualifier is set. 510 OverloadedOperatorKind Operator; 511 }; 512 513 /// The canonical template name to which this dependent 514 /// template name refers. 515 /// 516 /// The canonical template name for a dependent template name is 517 /// another dependent template name whose nested name specifier is 518 /// canonical. 519 TemplateName CanonicalTemplateName; 520 521 DependentTemplateName(NestedNameSpecifier *Qualifier, 522 const IdentifierInfo *Identifier) 523 : Qualifier(Qualifier, false), Identifier(Identifier), 524 CanonicalTemplateName(this) {} 525 526 DependentTemplateName(NestedNameSpecifier *Qualifier, 527 const IdentifierInfo *Identifier, 528 TemplateName Canon) 529 : Qualifier(Qualifier, false), Identifier(Identifier), 530 CanonicalTemplateName(Canon) {} 531 532 DependentTemplateName(NestedNameSpecifier *Qualifier, 533 OverloadedOperatorKind Operator) 534 : Qualifier(Qualifier, true), Operator(Operator), 535 CanonicalTemplateName(this) {} 536 537 DependentTemplateName(NestedNameSpecifier *Qualifier, 538 OverloadedOperatorKind Operator, 539 TemplateName Canon) 540 : Qualifier(Qualifier, true), Operator(Operator), 541 CanonicalTemplateName(Canon) {} 542 543 public: 544 /// Return the nested name specifier that qualifies this name. 545 NestedNameSpecifier *getQualifier() const { return Qualifier.getPointer(); } 546 547 /// Determine whether this template name refers to an identifier. 548 bool isIdentifier() const { return !Qualifier.getInt(); } 549 550 /// Returns the identifier to which this template name refers. 551 const IdentifierInfo *getIdentifier() const { 552 assert(isIdentifier() && "Template name isn't an identifier?"); 553 return Identifier; 554 } 555 556 /// Determine whether this template name refers to an overloaded 557 /// operator. 558 bool isOverloadedOperator() const { return Qualifier.getInt(); } 559 560 /// Return the overloaded operator to which this template name refers. 561 OverloadedOperatorKind getOperator() const { 562 assert(isOverloadedOperator() && 563 "Template name isn't an overloaded operator?"); 564 return Operator; 565 } 566 567 void Profile(llvm::FoldingSetNodeID &ID) { 568 if (isIdentifier()) 569 Profile(ID, getQualifier(), getIdentifier()); 570 else 571 Profile(ID, getQualifier(), getOperator()); 572 } 573 574 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, 575 const IdentifierInfo *Identifier) { 576 ID.AddPointer(NNS); 577 ID.AddBoolean(false); 578 ID.AddPointer(Identifier); 579 } 580 581 static void Profile(llvm::FoldingSetNodeID &ID, NestedNameSpecifier *NNS, 582 OverloadedOperatorKind Operator) { 583 ID.AddPointer(NNS); 584 ID.AddBoolean(true); 585 ID.AddInteger(Operator); 586 } 587 }; 588 589 } // namespace clang. 590 591 namespace llvm { 592 593 /// The clang::TemplateName class is effectively a pointer. 594 template<> 595 struct PointerLikeTypeTraits<clang::TemplateName> { 596 static inline void *getAsVoidPointer(clang::TemplateName TN) { 597 return TN.getAsVoidPointer(); 598 } 599 600 static inline clang::TemplateName getFromVoidPointer(void *Ptr) { 601 return clang::TemplateName::getFromVoidPointer(Ptr); 602 } 603 604 // No bits are available! 605 static constexpr int NumLowBitsAvailable = 0; 606 }; 607 608 } // namespace llvm. 609 610 #endif // LLVM_CLANG_AST_TEMPLATENAME_H 611