1e5dd7070Spatrick //===- USRGeneration.cpp - Routines for USR generation --------------------===// 2e5dd7070Spatrick // 3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information. 5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e5dd7070Spatrick // 7e5dd7070Spatrick //===----------------------------------------------------------------------===// 8e5dd7070Spatrick 9e5dd7070Spatrick #include "clang/Index/USRGeneration.h" 10e5dd7070Spatrick #include "clang/AST/ASTContext.h" 11e5dd7070Spatrick #include "clang/AST/Attr.h" 12e5dd7070Spatrick #include "clang/AST/DeclTemplate.h" 13e5dd7070Spatrick #include "clang/AST/DeclVisitor.h" 14ec727ea7Spatrick #include "clang/Basic/FileManager.h" 15e5dd7070Spatrick #include "clang/Lex/PreprocessingRecord.h" 16e5dd7070Spatrick #include "llvm/Support/Path.h" 17e5dd7070Spatrick #include "llvm/Support/raw_ostream.h" 18e5dd7070Spatrick 19e5dd7070Spatrick using namespace clang; 20e5dd7070Spatrick using namespace clang::index; 21e5dd7070Spatrick 22e5dd7070Spatrick //===----------------------------------------------------------------------===// 23e5dd7070Spatrick // USR generation. 24e5dd7070Spatrick //===----------------------------------------------------------------------===// 25e5dd7070Spatrick 26e5dd7070Spatrick /// \returns true on error. 27e5dd7070Spatrick static bool printLoc(llvm::raw_ostream &OS, SourceLocation Loc, 28e5dd7070Spatrick const SourceManager &SM, bool IncludeOffset) { 29e5dd7070Spatrick if (Loc.isInvalid()) { 30e5dd7070Spatrick return true; 31e5dd7070Spatrick } 32e5dd7070Spatrick Loc = SM.getExpansionLoc(Loc); 33e5dd7070Spatrick const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(Loc); 34e5dd7070Spatrick const FileEntry *FE = SM.getFileEntryForID(Decomposed.first); 35e5dd7070Spatrick if (FE) { 36e5dd7070Spatrick OS << llvm::sys::path::filename(FE->getName()); 37e5dd7070Spatrick } else { 38e5dd7070Spatrick // This case really isn't interesting. 39e5dd7070Spatrick return true; 40e5dd7070Spatrick } 41e5dd7070Spatrick if (IncludeOffset) { 42e5dd7070Spatrick // Use the offest into the FileID to represent the location. Using 43e5dd7070Spatrick // a line/column can cause us to look back at the original source file, 44e5dd7070Spatrick // which is expensive. 45e5dd7070Spatrick OS << '@' << Decomposed.second; 46e5dd7070Spatrick } 47e5dd7070Spatrick return false; 48e5dd7070Spatrick } 49e5dd7070Spatrick 50e5dd7070Spatrick static StringRef GetExternalSourceContainer(const NamedDecl *D) { 51e5dd7070Spatrick if (!D) 52e5dd7070Spatrick return StringRef(); 53e5dd7070Spatrick if (auto *attr = D->getExternalSourceSymbolAttr()) { 54e5dd7070Spatrick return attr->getDefinedIn(); 55e5dd7070Spatrick } 56e5dd7070Spatrick return StringRef(); 57e5dd7070Spatrick } 58e5dd7070Spatrick 59e5dd7070Spatrick namespace { 60e5dd7070Spatrick class USRGenerator : public ConstDeclVisitor<USRGenerator> { 61e5dd7070Spatrick SmallVectorImpl<char> &Buf; 62e5dd7070Spatrick llvm::raw_svector_ostream Out; 63e5dd7070Spatrick bool IgnoreResults; 64e5dd7070Spatrick ASTContext *Context; 65e5dd7070Spatrick bool generatedLoc; 66e5dd7070Spatrick 67e5dd7070Spatrick llvm::DenseMap<const Type *, unsigned> TypeSubstitutions; 68e5dd7070Spatrick 69e5dd7070Spatrick public: 70e5dd7070Spatrick explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf) 71e5dd7070Spatrick : Buf(Buf), 72e5dd7070Spatrick Out(Buf), 73e5dd7070Spatrick IgnoreResults(false), 74e5dd7070Spatrick Context(Ctx), 75e5dd7070Spatrick generatedLoc(false) 76e5dd7070Spatrick { 77e5dd7070Spatrick // Add the USR space prefix. 78e5dd7070Spatrick Out << getUSRSpacePrefix(); 79e5dd7070Spatrick } 80e5dd7070Spatrick 81e5dd7070Spatrick bool ignoreResults() const { return IgnoreResults; } 82e5dd7070Spatrick 83e5dd7070Spatrick // Visitation methods from generating USRs from AST elements. 84e5dd7070Spatrick void VisitDeclContext(const DeclContext *D); 85e5dd7070Spatrick void VisitFieldDecl(const FieldDecl *D); 86e5dd7070Spatrick void VisitFunctionDecl(const FunctionDecl *D); 87e5dd7070Spatrick void VisitNamedDecl(const NamedDecl *D); 88e5dd7070Spatrick void VisitNamespaceDecl(const NamespaceDecl *D); 89e5dd7070Spatrick void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D); 90e5dd7070Spatrick void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D); 91e5dd7070Spatrick void VisitClassTemplateDecl(const ClassTemplateDecl *D); 92e5dd7070Spatrick void VisitObjCContainerDecl(const ObjCContainerDecl *CD, 93e5dd7070Spatrick const ObjCCategoryDecl *CatD = nullptr); 94e5dd7070Spatrick void VisitObjCMethodDecl(const ObjCMethodDecl *MD); 95e5dd7070Spatrick void VisitObjCPropertyDecl(const ObjCPropertyDecl *D); 96e5dd7070Spatrick void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D); 97e5dd7070Spatrick void VisitTagDecl(const TagDecl *D); 98e5dd7070Spatrick void VisitTypedefDecl(const TypedefDecl *D); 99e5dd7070Spatrick void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D); 100e5dd7070Spatrick void VisitVarDecl(const VarDecl *D); 101e5dd7070Spatrick void VisitBindingDecl(const BindingDecl *D); 102e5dd7070Spatrick void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D); 103e5dd7070Spatrick void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D); 104e5dd7070Spatrick void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D); 105e5dd7070Spatrick void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D); 106e5dd7070Spatrick 107e5dd7070Spatrick void VisitLinkageSpecDecl(const LinkageSpecDecl *D) { 108e5dd7070Spatrick IgnoreResults = true; // No USRs for linkage specs themselves. 109e5dd7070Spatrick } 110e5dd7070Spatrick 111e5dd7070Spatrick void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) { 112e5dd7070Spatrick IgnoreResults = true; 113e5dd7070Spatrick } 114e5dd7070Spatrick 115e5dd7070Spatrick void VisitUsingDecl(const UsingDecl *D) { 116e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 117e5dd7070Spatrick Out << "@UD@"; 118e5dd7070Spatrick 119e5dd7070Spatrick bool EmittedDeclName = !EmitDeclName(D); 120e5dd7070Spatrick assert(EmittedDeclName && "EmitDeclName can not fail for UsingDecls"); 121e5dd7070Spatrick (void)EmittedDeclName; 122e5dd7070Spatrick } 123e5dd7070Spatrick 124e5dd7070Spatrick bool ShouldGenerateLocation(const NamedDecl *D); 125e5dd7070Spatrick 126e5dd7070Spatrick bool isLocal(const NamedDecl *D) { 127e5dd7070Spatrick return D->getParentFunctionOrMethod() != nullptr; 128e5dd7070Spatrick } 129e5dd7070Spatrick 130e5dd7070Spatrick void GenExtSymbolContainer(const NamedDecl *D); 131e5dd7070Spatrick 132e5dd7070Spatrick /// Generate the string component containing the location of the 133e5dd7070Spatrick /// declaration. 134e5dd7070Spatrick bool GenLoc(const Decl *D, bool IncludeOffset); 135e5dd7070Spatrick 136e5dd7070Spatrick /// String generation methods used both by the visitation methods 137e5dd7070Spatrick /// and from other clients that want to directly generate USRs. These 138e5dd7070Spatrick /// methods do not construct complete USRs (which incorporate the parents 139e5dd7070Spatrick /// of an AST element), but only the fragments concerning the AST element 140e5dd7070Spatrick /// itself. 141e5dd7070Spatrick 142e5dd7070Spatrick /// Generate a USR for an Objective-C class. 143e5dd7070Spatrick void GenObjCClass(StringRef cls, StringRef ExtSymDefinedIn, 144e5dd7070Spatrick StringRef CategoryContextExtSymbolDefinedIn) { 145e5dd7070Spatrick generateUSRForObjCClass(cls, Out, ExtSymDefinedIn, 146e5dd7070Spatrick CategoryContextExtSymbolDefinedIn); 147e5dd7070Spatrick } 148e5dd7070Spatrick 149e5dd7070Spatrick /// Generate a USR for an Objective-C class category. 150e5dd7070Spatrick void GenObjCCategory(StringRef cls, StringRef cat, 151e5dd7070Spatrick StringRef clsExt, StringRef catExt) { 152e5dd7070Spatrick generateUSRForObjCCategory(cls, cat, Out, clsExt, catExt); 153e5dd7070Spatrick } 154e5dd7070Spatrick 155e5dd7070Spatrick /// Generate a USR fragment for an Objective-C property. 156e5dd7070Spatrick void GenObjCProperty(StringRef prop, bool isClassProp) { 157e5dd7070Spatrick generateUSRForObjCProperty(prop, isClassProp, Out); 158e5dd7070Spatrick } 159e5dd7070Spatrick 160e5dd7070Spatrick /// Generate a USR for an Objective-C protocol. 161e5dd7070Spatrick void GenObjCProtocol(StringRef prot, StringRef ext) { 162e5dd7070Spatrick generateUSRForObjCProtocol(prot, Out, ext); 163e5dd7070Spatrick } 164e5dd7070Spatrick 165e5dd7070Spatrick void VisitType(QualType T); 166e5dd7070Spatrick void VisitTemplateParameterList(const TemplateParameterList *Params); 167e5dd7070Spatrick void VisitTemplateName(TemplateName Name); 168e5dd7070Spatrick void VisitTemplateArgument(const TemplateArgument &Arg); 169e5dd7070Spatrick 170e5dd7070Spatrick /// Emit a Decl's name using NamedDecl::printName() and return true if 171e5dd7070Spatrick /// the decl had no name. 172e5dd7070Spatrick bool EmitDeclName(const NamedDecl *D); 173e5dd7070Spatrick }; 174e5dd7070Spatrick } // end anonymous namespace 175e5dd7070Spatrick 176e5dd7070Spatrick //===----------------------------------------------------------------------===// 177e5dd7070Spatrick // Generating USRs from ASTS. 178e5dd7070Spatrick //===----------------------------------------------------------------------===// 179e5dd7070Spatrick 180e5dd7070Spatrick bool USRGenerator::EmitDeclName(const NamedDecl *D) { 181e5dd7070Spatrick const unsigned startSize = Buf.size(); 182e5dd7070Spatrick D->printName(Out); 183e5dd7070Spatrick const unsigned endSize = Buf.size(); 184e5dd7070Spatrick return startSize == endSize; 185e5dd7070Spatrick } 186e5dd7070Spatrick 187e5dd7070Spatrick bool USRGenerator::ShouldGenerateLocation(const NamedDecl *D) { 188e5dd7070Spatrick if (D->isExternallyVisible()) 189e5dd7070Spatrick return false; 190e5dd7070Spatrick if (D->getParentFunctionOrMethod()) 191e5dd7070Spatrick return true; 192e5dd7070Spatrick SourceLocation Loc = D->getLocation(); 193e5dd7070Spatrick if (Loc.isInvalid()) 194e5dd7070Spatrick return false; 195e5dd7070Spatrick const SourceManager &SM = Context->getSourceManager(); 196e5dd7070Spatrick return !SM.isInSystemHeader(Loc); 197e5dd7070Spatrick } 198e5dd7070Spatrick 199e5dd7070Spatrick void USRGenerator::VisitDeclContext(const DeclContext *DC) { 200e5dd7070Spatrick if (const NamedDecl *D = dyn_cast<NamedDecl>(DC)) 201e5dd7070Spatrick Visit(D); 202e5dd7070Spatrick else if (isa<LinkageSpecDecl>(DC)) // Linkage specs are transparent in USRs. 203e5dd7070Spatrick VisitDeclContext(DC->getParent()); 204e5dd7070Spatrick } 205e5dd7070Spatrick 206e5dd7070Spatrick void USRGenerator::VisitFieldDecl(const FieldDecl *D) { 207e5dd7070Spatrick // The USR for an ivar declared in a class extension is based on the 208e5dd7070Spatrick // ObjCInterfaceDecl, not the ObjCCategoryDecl. 209e5dd7070Spatrick if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) 210e5dd7070Spatrick Visit(ID); 211e5dd7070Spatrick else 212e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 213e5dd7070Spatrick Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@"); 214e5dd7070Spatrick if (EmitDeclName(D)) { 215e5dd7070Spatrick // Bit fields can be anonymous. 216e5dd7070Spatrick IgnoreResults = true; 217e5dd7070Spatrick return; 218e5dd7070Spatrick } 219e5dd7070Spatrick } 220e5dd7070Spatrick 221e5dd7070Spatrick void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) { 222e5dd7070Spatrick if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 223e5dd7070Spatrick return; 224e5dd7070Spatrick 225e5dd7070Spatrick const unsigned StartSize = Buf.size(); 226e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 227e5dd7070Spatrick if (Buf.size() == StartSize) 228e5dd7070Spatrick GenExtSymbolContainer(D); 229e5dd7070Spatrick 230e5dd7070Spatrick bool IsTemplate = false; 231e5dd7070Spatrick if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) { 232e5dd7070Spatrick IsTemplate = true; 233e5dd7070Spatrick Out << "@FT@"; 234e5dd7070Spatrick VisitTemplateParameterList(FunTmpl->getTemplateParameters()); 235e5dd7070Spatrick } else 236e5dd7070Spatrick Out << "@F@"; 237e5dd7070Spatrick 238e5dd7070Spatrick PrintingPolicy Policy(Context->getLangOpts()); 239e5dd7070Spatrick // Forward references can have different template argument names. Suppress the 240e5dd7070Spatrick // template argument names in constructors to make their USR more stable. 241e5dd7070Spatrick Policy.SuppressTemplateArgsInCXXConstructors = true; 242e5dd7070Spatrick D->getDeclName().print(Out, Policy); 243e5dd7070Spatrick 244e5dd7070Spatrick ASTContext &Ctx = *Context; 245e5dd7070Spatrick if ((!Ctx.getLangOpts().CPlusPlus || D->isExternC()) && 246e5dd7070Spatrick !D->hasAttr<OverloadableAttr>()) 247e5dd7070Spatrick return; 248e5dd7070Spatrick 249e5dd7070Spatrick if (const TemplateArgumentList * 250e5dd7070Spatrick SpecArgs = D->getTemplateSpecializationArgs()) { 251e5dd7070Spatrick Out << '<'; 252e5dd7070Spatrick for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) { 253e5dd7070Spatrick Out << '#'; 254e5dd7070Spatrick VisitTemplateArgument(SpecArgs->get(I)); 255e5dd7070Spatrick } 256e5dd7070Spatrick Out << '>'; 257e5dd7070Spatrick } 258e5dd7070Spatrick 259e5dd7070Spatrick // Mangle in type information for the arguments. 260e5dd7070Spatrick for (auto PD : D->parameters()) { 261e5dd7070Spatrick Out << '#'; 262e5dd7070Spatrick VisitType(PD->getType()); 263e5dd7070Spatrick } 264e5dd7070Spatrick if (D->isVariadic()) 265e5dd7070Spatrick Out << '.'; 266e5dd7070Spatrick if (IsTemplate) { 267e5dd7070Spatrick // Function templates can be overloaded by return type, for example: 268e5dd7070Spatrick // \code 269e5dd7070Spatrick // template <class T> typename T::A foo() {} 270e5dd7070Spatrick // template <class T> typename T::B foo() {} 271e5dd7070Spatrick // \endcode 272e5dd7070Spatrick Out << '#'; 273e5dd7070Spatrick VisitType(D->getReturnType()); 274e5dd7070Spatrick } 275e5dd7070Spatrick Out << '#'; 276e5dd7070Spatrick if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { 277e5dd7070Spatrick if (MD->isStatic()) 278e5dd7070Spatrick Out << 'S'; 279e5dd7070Spatrick // FIXME: OpenCL: Need to consider address spaces 280e5dd7070Spatrick if (unsigned quals = MD->getMethodQualifiers().getCVRUQualifiers()) 281e5dd7070Spatrick Out << (char)('0' + quals); 282e5dd7070Spatrick switch (MD->getRefQualifier()) { 283e5dd7070Spatrick case RQ_None: break; 284e5dd7070Spatrick case RQ_LValue: Out << '&'; break; 285e5dd7070Spatrick case RQ_RValue: Out << "&&"; break; 286e5dd7070Spatrick } 287e5dd7070Spatrick } 288e5dd7070Spatrick } 289e5dd7070Spatrick 290e5dd7070Spatrick void USRGenerator::VisitNamedDecl(const NamedDecl *D) { 291e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 292e5dd7070Spatrick Out << "@"; 293e5dd7070Spatrick 294e5dd7070Spatrick if (EmitDeclName(D)) { 295e5dd7070Spatrick // The string can be empty if the declaration has no name; e.g., it is 296e5dd7070Spatrick // the ParmDecl with no name for declaration of a function pointer type, 297e5dd7070Spatrick // e.g.: void (*f)(void *); 298e5dd7070Spatrick // In this case, don't generate a USR. 299e5dd7070Spatrick IgnoreResults = true; 300e5dd7070Spatrick } 301e5dd7070Spatrick } 302e5dd7070Spatrick 303e5dd7070Spatrick void USRGenerator::VisitVarDecl(const VarDecl *D) { 304e5dd7070Spatrick // VarDecls can be declared 'extern' within a function or method body, 305e5dd7070Spatrick // but their enclosing DeclContext is the function, not the TU. We need 306e5dd7070Spatrick // to check the storage class to correctly generate the USR. 307e5dd7070Spatrick if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 308e5dd7070Spatrick return; 309e5dd7070Spatrick 310e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 311e5dd7070Spatrick 312e5dd7070Spatrick if (VarTemplateDecl *VarTmpl = D->getDescribedVarTemplate()) { 313e5dd7070Spatrick Out << "@VT"; 314e5dd7070Spatrick VisitTemplateParameterList(VarTmpl->getTemplateParameters()); 315e5dd7070Spatrick } else if (const VarTemplatePartialSpecializationDecl *PartialSpec 316e5dd7070Spatrick = dyn_cast<VarTemplatePartialSpecializationDecl>(D)) { 317e5dd7070Spatrick Out << "@VP"; 318e5dd7070Spatrick VisitTemplateParameterList(PartialSpec->getTemplateParameters()); 319e5dd7070Spatrick } 320e5dd7070Spatrick 321e5dd7070Spatrick // Variables always have simple names. 322e5dd7070Spatrick StringRef s = D->getName(); 323e5dd7070Spatrick 324e5dd7070Spatrick // The string can be empty if the declaration has no name; e.g., it is 325e5dd7070Spatrick // the ParmDecl with no name for declaration of a function pointer type, e.g.: 326e5dd7070Spatrick // void (*f)(void *); 327e5dd7070Spatrick // In this case, don't generate a USR. 328e5dd7070Spatrick if (s.empty()) 329e5dd7070Spatrick IgnoreResults = true; 330e5dd7070Spatrick else 331e5dd7070Spatrick Out << '@' << s; 332e5dd7070Spatrick 333e5dd7070Spatrick // For a template specialization, mangle the template arguments. 334e5dd7070Spatrick if (const VarTemplateSpecializationDecl *Spec 335e5dd7070Spatrick = dyn_cast<VarTemplateSpecializationDecl>(D)) { 336e5dd7070Spatrick const TemplateArgumentList &Args = Spec->getTemplateArgs(); 337e5dd7070Spatrick Out << '>'; 338e5dd7070Spatrick for (unsigned I = 0, N = Args.size(); I != N; ++I) { 339e5dd7070Spatrick Out << '#'; 340e5dd7070Spatrick VisitTemplateArgument(Args.get(I)); 341e5dd7070Spatrick } 342e5dd7070Spatrick } 343e5dd7070Spatrick } 344e5dd7070Spatrick 345e5dd7070Spatrick void USRGenerator::VisitBindingDecl(const BindingDecl *D) { 346e5dd7070Spatrick if (isLocal(D) && GenLoc(D, /*IncludeOffset=*/true)) 347e5dd7070Spatrick return; 348e5dd7070Spatrick VisitNamedDecl(D); 349e5dd7070Spatrick } 350e5dd7070Spatrick 351e5dd7070Spatrick void USRGenerator::VisitNonTypeTemplateParmDecl( 352e5dd7070Spatrick const NonTypeTemplateParmDecl *D) { 353e5dd7070Spatrick GenLoc(D, /*IncludeOffset=*/true); 354e5dd7070Spatrick } 355e5dd7070Spatrick 356e5dd7070Spatrick void USRGenerator::VisitTemplateTemplateParmDecl( 357e5dd7070Spatrick const TemplateTemplateParmDecl *D) { 358e5dd7070Spatrick GenLoc(D, /*IncludeOffset=*/true); 359e5dd7070Spatrick } 360e5dd7070Spatrick 361e5dd7070Spatrick void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) { 362e5dd7070Spatrick if (D->isAnonymousNamespace()) { 363e5dd7070Spatrick Out << "@aN"; 364e5dd7070Spatrick return; 365e5dd7070Spatrick } 366e5dd7070Spatrick 367e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 368e5dd7070Spatrick if (!IgnoreResults) 369e5dd7070Spatrick Out << "@N@" << D->getName(); 370e5dd7070Spatrick } 371e5dd7070Spatrick 372e5dd7070Spatrick void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) { 373e5dd7070Spatrick VisitFunctionDecl(D->getTemplatedDecl()); 374e5dd7070Spatrick } 375e5dd7070Spatrick 376e5dd7070Spatrick void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) { 377e5dd7070Spatrick VisitTagDecl(D->getTemplatedDecl()); 378e5dd7070Spatrick } 379e5dd7070Spatrick 380e5dd7070Spatrick void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) { 381e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 382e5dd7070Spatrick if (!IgnoreResults) 383e5dd7070Spatrick Out << "@NA@" << D->getName(); 384e5dd7070Spatrick } 385e5dd7070Spatrick 386ec727ea7Spatrick static const ObjCCategoryDecl *getCategoryContext(const NamedDecl *D) { 387ec727ea7Spatrick if (auto *CD = dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) 388ec727ea7Spatrick return CD; 389ec727ea7Spatrick if (auto *ICD = dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) 390ec727ea7Spatrick return ICD->getCategoryDecl(); 391ec727ea7Spatrick return nullptr; 392ec727ea7Spatrick } 393ec727ea7Spatrick 394e5dd7070Spatrick void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) { 395e5dd7070Spatrick const DeclContext *container = D->getDeclContext(); 396e5dd7070Spatrick if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) { 397e5dd7070Spatrick Visit(pd); 398e5dd7070Spatrick } 399e5dd7070Spatrick else { 400e5dd7070Spatrick // The USR for a method declared in a class extension or category is based on 401e5dd7070Spatrick // the ObjCInterfaceDecl, not the ObjCCategoryDecl. 402e5dd7070Spatrick const ObjCInterfaceDecl *ID = D->getClassInterface(); 403e5dd7070Spatrick if (!ID) { 404e5dd7070Spatrick IgnoreResults = true; 405e5dd7070Spatrick return; 406e5dd7070Spatrick } 407e5dd7070Spatrick auto *CD = getCategoryContext(D); 408e5dd7070Spatrick VisitObjCContainerDecl(ID, CD); 409e5dd7070Spatrick } 410e5dd7070Spatrick // Ideally we would use 'GenObjCMethod', but this is such a hot path 411e5dd7070Spatrick // for Objective-C code that we don't want to use 412e5dd7070Spatrick // DeclarationName::getAsString(). 413e5dd7070Spatrick Out << (D->isInstanceMethod() ? "(im)" : "(cm)") 414e5dd7070Spatrick << DeclarationName(D->getSelector()); 415e5dd7070Spatrick } 416e5dd7070Spatrick 417e5dd7070Spatrick void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D, 418e5dd7070Spatrick const ObjCCategoryDecl *CatD) { 419e5dd7070Spatrick switch (D->getKind()) { 420e5dd7070Spatrick default: 421e5dd7070Spatrick llvm_unreachable("Invalid ObjC container."); 422e5dd7070Spatrick case Decl::ObjCInterface: 423e5dd7070Spatrick case Decl::ObjCImplementation: 424e5dd7070Spatrick GenObjCClass(D->getName(), GetExternalSourceContainer(D), 425e5dd7070Spatrick GetExternalSourceContainer(CatD)); 426e5dd7070Spatrick break; 427e5dd7070Spatrick case Decl::ObjCCategory: { 428e5dd7070Spatrick const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D); 429e5dd7070Spatrick const ObjCInterfaceDecl *ID = CD->getClassInterface(); 430e5dd7070Spatrick if (!ID) { 431e5dd7070Spatrick // Handle invalid code where the @interface might not 432e5dd7070Spatrick // have been specified. 433e5dd7070Spatrick // FIXME: We should be able to generate this USR even if the 434e5dd7070Spatrick // @interface isn't available. 435e5dd7070Spatrick IgnoreResults = true; 436e5dd7070Spatrick return; 437e5dd7070Spatrick } 438e5dd7070Spatrick // Specially handle class extensions, which are anonymous categories. 439e5dd7070Spatrick // We want to mangle in the location to uniquely distinguish them. 440e5dd7070Spatrick if (CD->IsClassExtension()) { 441e5dd7070Spatrick Out << "objc(ext)" << ID->getName() << '@'; 442e5dd7070Spatrick GenLoc(CD, /*IncludeOffset=*/true); 443e5dd7070Spatrick } 444e5dd7070Spatrick else 445e5dd7070Spatrick GenObjCCategory(ID->getName(), CD->getName(), 446e5dd7070Spatrick GetExternalSourceContainer(ID), 447e5dd7070Spatrick GetExternalSourceContainer(CD)); 448e5dd7070Spatrick 449e5dd7070Spatrick break; 450e5dd7070Spatrick } 451e5dd7070Spatrick case Decl::ObjCCategoryImpl: { 452e5dd7070Spatrick const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D); 453e5dd7070Spatrick const ObjCInterfaceDecl *ID = CD->getClassInterface(); 454e5dd7070Spatrick if (!ID) { 455e5dd7070Spatrick // Handle invalid code where the @interface might not 456e5dd7070Spatrick // have been specified. 457e5dd7070Spatrick // FIXME: We should be able to generate this USR even if the 458e5dd7070Spatrick // @interface isn't available. 459e5dd7070Spatrick IgnoreResults = true; 460e5dd7070Spatrick return; 461e5dd7070Spatrick } 462e5dd7070Spatrick GenObjCCategory(ID->getName(), CD->getName(), 463e5dd7070Spatrick GetExternalSourceContainer(ID), 464e5dd7070Spatrick GetExternalSourceContainer(CD)); 465e5dd7070Spatrick break; 466e5dd7070Spatrick } 467e5dd7070Spatrick case Decl::ObjCProtocol: { 468e5dd7070Spatrick const ObjCProtocolDecl *PD = cast<ObjCProtocolDecl>(D); 469e5dd7070Spatrick GenObjCProtocol(PD->getName(), GetExternalSourceContainer(PD)); 470e5dd7070Spatrick break; 471e5dd7070Spatrick } 472e5dd7070Spatrick } 473e5dd7070Spatrick } 474e5dd7070Spatrick 475e5dd7070Spatrick void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) { 476e5dd7070Spatrick // The USR for a property declared in a class extension or category is based 477e5dd7070Spatrick // on the ObjCInterfaceDecl, not the ObjCCategoryDecl. 478e5dd7070Spatrick if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D)) 479ec727ea7Spatrick VisitObjCContainerDecl(ID, getCategoryContext(D)); 480e5dd7070Spatrick else 481e5dd7070Spatrick Visit(cast<Decl>(D->getDeclContext())); 482e5dd7070Spatrick GenObjCProperty(D->getName(), D->isClassProperty()); 483e5dd7070Spatrick } 484e5dd7070Spatrick 485e5dd7070Spatrick void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) { 486e5dd7070Spatrick if (ObjCPropertyDecl *PD = D->getPropertyDecl()) { 487e5dd7070Spatrick VisitObjCPropertyDecl(PD); 488e5dd7070Spatrick return; 489e5dd7070Spatrick } 490e5dd7070Spatrick 491e5dd7070Spatrick IgnoreResults = true; 492e5dd7070Spatrick } 493e5dd7070Spatrick 494e5dd7070Spatrick void USRGenerator::VisitTagDecl(const TagDecl *D) { 495e5dd7070Spatrick // Add the location of the tag decl to handle resolution across 496e5dd7070Spatrick // translation units. 497e5dd7070Spatrick if (!isa<EnumDecl>(D) && 498e5dd7070Spatrick ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 499e5dd7070Spatrick return; 500e5dd7070Spatrick 501e5dd7070Spatrick GenExtSymbolContainer(D); 502e5dd7070Spatrick 503e5dd7070Spatrick D = D->getCanonicalDecl(); 504e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 505e5dd7070Spatrick 506e5dd7070Spatrick bool AlreadyStarted = false; 507e5dd7070Spatrick if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) { 508e5dd7070Spatrick if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) { 509e5dd7070Spatrick AlreadyStarted = true; 510e5dd7070Spatrick 511e5dd7070Spatrick switch (D->getTagKind()) { 512e5dd7070Spatrick case TTK_Interface: 513e5dd7070Spatrick case TTK_Class: 514e5dd7070Spatrick case TTK_Struct: Out << "@ST"; break; 515e5dd7070Spatrick case TTK_Union: Out << "@UT"; break; 516e5dd7070Spatrick case TTK_Enum: llvm_unreachable("enum template"); 517e5dd7070Spatrick } 518e5dd7070Spatrick VisitTemplateParameterList(ClassTmpl->getTemplateParameters()); 519e5dd7070Spatrick } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec 520e5dd7070Spatrick = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) { 521e5dd7070Spatrick AlreadyStarted = true; 522e5dd7070Spatrick 523e5dd7070Spatrick switch (D->getTagKind()) { 524e5dd7070Spatrick case TTK_Interface: 525e5dd7070Spatrick case TTK_Class: 526e5dd7070Spatrick case TTK_Struct: Out << "@SP"; break; 527e5dd7070Spatrick case TTK_Union: Out << "@UP"; break; 528e5dd7070Spatrick case TTK_Enum: llvm_unreachable("enum partial specialization"); 529e5dd7070Spatrick } 530e5dd7070Spatrick VisitTemplateParameterList(PartialSpec->getTemplateParameters()); 531e5dd7070Spatrick } 532e5dd7070Spatrick } 533e5dd7070Spatrick 534e5dd7070Spatrick if (!AlreadyStarted) { 535e5dd7070Spatrick switch (D->getTagKind()) { 536e5dd7070Spatrick case TTK_Interface: 537e5dd7070Spatrick case TTK_Class: 538e5dd7070Spatrick case TTK_Struct: Out << "@S"; break; 539e5dd7070Spatrick case TTK_Union: Out << "@U"; break; 540e5dd7070Spatrick case TTK_Enum: Out << "@E"; break; 541e5dd7070Spatrick } 542e5dd7070Spatrick } 543e5dd7070Spatrick 544e5dd7070Spatrick Out << '@'; 545e5dd7070Spatrick assert(Buf.size() > 0); 546e5dd7070Spatrick const unsigned off = Buf.size() - 1; 547e5dd7070Spatrick 548e5dd7070Spatrick if (EmitDeclName(D)) { 549e5dd7070Spatrick if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) { 550e5dd7070Spatrick Buf[off] = 'A'; 551e5dd7070Spatrick Out << '@' << *TD; 552e5dd7070Spatrick } 553e5dd7070Spatrick else { 554e5dd7070Spatrick if (D->isEmbeddedInDeclarator() && !D->isFreeStanding()) { 555e5dd7070Spatrick printLoc(Out, D->getLocation(), Context->getSourceManager(), true); 556e5dd7070Spatrick } else { 557e5dd7070Spatrick Buf[off] = 'a'; 558e5dd7070Spatrick if (auto *ED = dyn_cast<EnumDecl>(D)) { 559e5dd7070Spatrick // Distinguish USRs of anonymous enums by using their first enumerator. 560e5dd7070Spatrick auto enum_range = ED->enumerators(); 561e5dd7070Spatrick if (enum_range.begin() != enum_range.end()) { 562e5dd7070Spatrick Out << '@' << **enum_range.begin(); 563e5dd7070Spatrick } 564e5dd7070Spatrick } 565e5dd7070Spatrick } 566e5dd7070Spatrick } 567e5dd7070Spatrick } 568e5dd7070Spatrick 569e5dd7070Spatrick // For a class template specialization, mangle the template arguments. 570e5dd7070Spatrick if (const ClassTemplateSpecializationDecl *Spec 571e5dd7070Spatrick = dyn_cast<ClassTemplateSpecializationDecl>(D)) { 572e5dd7070Spatrick const TemplateArgumentList &Args = Spec->getTemplateArgs(); 573e5dd7070Spatrick Out << '>'; 574e5dd7070Spatrick for (unsigned I = 0, N = Args.size(); I != N; ++I) { 575e5dd7070Spatrick Out << '#'; 576e5dd7070Spatrick VisitTemplateArgument(Args.get(I)); 577e5dd7070Spatrick } 578e5dd7070Spatrick } 579e5dd7070Spatrick } 580e5dd7070Spatrick 581e5dd7070Spatrick void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) { 582e5dd7070Spatrick if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 583e5dd7070Spatrick return; 584e5dd7070Spatrick const DeclContext *DC = D->getDeclContext(); 585e5dd7070Spatrick if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC)) 586e5dd7070Spatrick Visit(DCN); 587e5dd7070Spatrick Out << "@T@"; 588e5dd7070Spatrick Out << D->getName(); 589e5dd7070Spatrick } 590e5dd7070Spatrick 591e5dd7070Spatrick void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) { 592e5dd7070Spatrick GenLoc(D, /*IncludeOffset=*/true); 593e5dd7070Spatrick } 594e5dd7070Spatrick 595e5dd7070Spatrick void USRGenerator::GenExtSymbolContainer(const NamedDecl *D) { 596e5dd7070Spatrick StringRef Container = GetExternalSourceContainer(D); 597e5dd7070Spatrick if (!Container.empty()) 598e5dd7070Spatrick Out << "@M@" << Container; 599e5dd7070Spatrick } 600e5dd7070Spatrick 601e5dd7070Spatrick bool USRGenerator::GenLoc(const Decl *D, bool IncludeOffset) { 602e5dd7070Spatrick if (generatedLoc) 603e5dd7070Spatrick return IgnoreResults; 604e5dd7070Spatrick generatedLoc = true; 605e5dd7070Spatrick 606e5dd7070Spatrick // Guard against null declarations in invalid code. 607e5dd7070Spatrick if (!D) { 608e5dd7070Spatrick IgnoreResults = true; 609e5dd7070Spatrick return true; 610e5dd7070Spatrick } 611e5dd7070Spatrick 612e5dd7070Spatrick // Use the location of canonical decl. 613e5dd7070Spatrick D = D->getCanonicalDecl(); 614e5dd7070Spatrick 615e5dd7070Spatrick IgnoreResults = 616e5dd7070Spatrick IgnoreResults || printLoc(Out, D->getBeginLoc(), 617e5dd7070Spatrick Context->getSourceManager(), IncludeOffset); 618e5dd7070Spatrick 619e5dd7070Spatrick return IgnoreResults; 620e5dd7070Spatrick } 621e5dd7070Spatrick 622e5dd7070Spatrick static void printQualifier(llvm::raw_ostream &Out, ASTContext &Ctx, NestedNameSpecifier *NNS) { 623e5dd7070Spatrick // FIXME: Encode the qualifier, don't just print it. 624e5dd7070Spatrick PrintingPolicy PO(Ctx.getLangOpts()); 625e5dd7070Spatrick PO.SuppressTagKeyword = true; 626e5dd7070Spatrick PO.SuppressUnwrittenScope = true; 627e5dd7070Spatrick PO.ConstantArraySizeAsWritten = false; 628e5dd7070Spatrick PO.AnonymousTagLocations = false; 629e5dd7070Spatrick NNS->print(Out, PO); 630e5dd7070Spatrick } 631e5dd7070Spatrick 632e5dd7070Spatrick void USRGenerator::VisitType(QualType T) { 633e5dd7070Spatrick // This method mangles in USR information for types. It can possibly 634e5dd7070Spatrick // just reuse the naming-mangling logic used by codegen, although the 635e5dd7070Spatrick // requirements for USRs might not be the same. 636e5dd7070Spatrick ASTContext &Ctx = *Context; 637e5dd7070Spatrick 638e5dd7070Spatrick do { 639e5dd7070Spatrick T = Ctx.getCanonicalType(T); 640e5dd7070Spatrick Qualifiers Q = T.getQualifiers(); 641e5dd7070Spatrick unsigned qVal = 0; 642e5dd7070Spatrick if (Q.hasConst()) 643e5dd7070Spatrick qVal |= 0x1; 644e5dd7070Spatrick if (Q.hasVolatile()) 645e5dd7070Spatrick qVal |= 0x2; 646e5dd7070Spatrick if (Q.hasRestrict()) 647e5dd7070Spatrick qVal |= 0x4; 648e5dd7070Spatrick if(qVal) 649e5dd7070Spatrick Out << ((char) ('0' + qVal)); 650e5dd7070Spatrick 651e5dd7070Spatrick // Mangle in ObjC GC qualifiers? 652e5dd7070Spatrick 653e5dd7070Spatrick if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) { 654e5dd7070Spatrick Out << 'P'; 655e5dd7070Spatrick T = Expansion->getPattern(); 656e5dd7070Spatrick } 657e5dd7070Spatrick 658e5dd7070Spatrick if (const BuiltinType *BT = T->getAs<BuiltinType>()) { 659e5dd7070Spatrick unsigned char c = '\0'; 660e5dd7070Spatrick switch (BT->getKind()) { 661e5dd7070Spatrick case BuiltinType::Void: 662e5dd7070Spatrick c = 'v'; break; 663e5dd7070Spatrick case BuiltinType::Bool: 664e5dd7070Spatrick c = 'b'; break; 665e5dd7070Spatrick case BuiltinType::UChar: 666e5dd7070Spatrick c = 'c'; break; 667e5dd7070Spatrick case BuiltinType::Char8: 668e5dd7070Spatrick c = 'u'; break; // FIXME: Check this doesn't collide 669e5dd7070Spatrick case BuiltinType::Char16: 670e5dd7070Spatrick c = 'q'; break; 671e5dd7070Spatrick case BuiltinType::Char32: 672e5dd7070Spatrick c = 'w'; break; 673e5dd7070Spatrick case BuiltinType::UShort: 674e5dd7070Spatrick c = 's'; break; 675e5dd7070Spatrick case BuiltinType::UInt: 676e5dd7070Spatrick c = 'i'; break; 677e5dd7070Spatrick case BuiltinType::ULong: 678e5dd7070Spatrick c = 'l'; break; 679e5dd7070Spatrick case BuiltinType::ULongLong: 680e5dd7070Spatrick c = 'k'; break; 681e5dd7070Spatrick case BuiltinType::UInt128: 682e5dd7070Spatrick c = 'j'; break; 683e5dd7070Spatrick case BuiltinType::Char_U: 684e5dd7070Spatrick case BuiltinType::Char_S: 685e5dd7070Spatrick c = 'C'; break; 686e5dd7070Spatrick case BuiltinType::SChar: 687e5dd7070Spatrick c = 'r'; break; 688e5dd7070Spatrick case BuiltinType::WChar_S: 689e5dd7070Spatrick case BuiltinType::WChar_U: 690e5dd7070Spatrick c = 'W'; break; 691e5dd7070Spatrick case BuiltinType::Short: 692e5dd7070Spatrick c = 'S'; break; 693e5dd7070Spatrick case BuiltinType::Int: 694e5dd7070Spatrick c = 'I'; break; 695e5dd7070Spatrick case BuiltinType::Long: 696e5dd7070Spatrick c = 'L'; break; 697e5dd7070Spatrick case BuiltinType::LongLong: 698e5dd7070Spatrick c = 'K'; break; 699e5dd7070Spatrick case BuiltinType::Int128: 700e5dd7070Spatrick c = 'J'; break; 701e5dd7070Spatrick case BuiltinType::Float16: 702e5dd7070Spatrick case BuiltinType::Half: 703e5dd7070Spatrick c = 'h'; break; 704e5dd7070Spatrick case BuiltinType::Float: 705e5dd7070Spatrick c = 'f'; break; 706e5dd7070Spatrick case BuiltinType::Double: 707e5dd7070Spatrick c = 'd'; break; 708e5dd7070Spatrick case BuiltinType::LongDouble: 709e5dd7070Spatrick c = 'D'; break; 710e5dd7070Spatrick case BuiltinType::Float128: 711e5dd7070Spatrick c = 'Q'; break; 712e5dd7070Spatrick case BuiltinType::NullPtr: 713e5dd7070Spatrick c = 'n'; break; 714e5dd7070Spatrick #define BUILTIN_TYPE(Id, SingletonId) 715e5dd7070Spatrick #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id: 716e5dd7070Spatrick #include "clang/AST/BuiltinTypes.def" 717e5dd7070Spatrick case BuiltinType::Dependent: 718e5dd7070Spatrick #define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \ 719e5dd7070Spatrick case BuiltinType::Id: 720e5dd7070Spatrick #include "clang/Basic/OpenCLImageTypes.def" 721e5dd7070Spatrick #define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \ 722e5dd7070Spatrick case BuiltinType::Id: 723e5dd7070Spatrick #include "clang/Basic/OpenCLExtensionTypes.def" 724e5dd7070Spatrick case BuiltinType::OCLEvent: 725e5dd7070Spatrick case BuiltinType::OCLClkEvent: 726e5dd7070Spatrick case BuiltinType::OCLQueue: 727e5dd7070Spatrick case BuiltinType::OCLReserveID: 728e5dd7070Spatrick case BuiltinType::OCLSampler: 729e5dd7070Spatrick #define SVE_TYPE(Name, Id, SingletonId) \ 730e5dd7070Spatrick case BuiltinType::Id: 731e5dd7070Spatrick #include "clang/Basic/AArch64SVEACLETypes.def" 732*a9ac8606Spatrick #define PPC_VECTOR_TYPE(Name, Id, Size) \ 733*a9ac8606Spatrick case BuiltinType::Id: 734*a9ac8606Spatrick #include "clang/Basic/PPCTypes.def" 735*a9ac8606Spatrick #define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id: 736*a9ac8606Spatrick #include "clang/Basic/RISCVVTypes.def" 737e5dd7070Spatrick case BuiltinType::ShortAccum: 738e5dd7070Spatrick case BuiltinType::Accum: 739e5dd7070Spatrick case BuiltinType::LongAccum: 740e5dd7070Spatrick case BuiltinType::UShortAccum: 741e5dd7070Spatrick case BuiltinType::UAccum: 742e5dd7070Spatrick case BuiltinType::ULongAccum: 743e5dd7070Spatrick case BuiltinType::ShortFract: 744e5dd7070Spatrick case BuiltinType::Fract: 745e5dd7070Spatrick case BuiltinType::LongFract: 746e5dd7070Spatrick case BuiltinType::UShortFract: 747e5dd7070Spatrick case BuiltinType::UFract: 748e5dd7070Spatrick case BuiltinType::ULongFract: 749e5dd7070Spatrick case BuiltinType::SatShortAccum: 750e5dd7070Spatrick case BuiltinType::SatAccum: 751e5dd7070Spatrick case BuiltinType::SatLongAccum: 752e5dd7070Spatrick case BuiltinType::SatUShortAccum: 753e5dd7070Spatrick case BuiltinType::SatUAccum: 754e5dd7070Spatrick case BuiltinType::SatULongAccum: 755e5dd7070Spatrick case BuiltinType::SatShortFract: 756e5dd7070Spatrick case BuiltinType::SatFract: 757e5dd7070Spatrick case BuiltinType::SatLongFract: 758e5dd7070Spatrick case BuiltinType::SatUShortFract: 759e5dd7070Spatrick case BuiltinType::SatUFract: 760e5dd7070Spatrick case BuiltinType::SatULongFract: 761ec727ea7Spatrick case BuiltinType::BFloat16: 762e5dd7070Spatrick IgnoreResults = true; 763e5dd7070Spatrick return; 764e5dd7070Spatrick case BuiltinType::ObjCId: 765e5dd7070Spatrick c = 'o'; break; 766e5dd7070Spatrick case BuiltinType::ObjCClass: 767e5dd7070Spatrick c = 'O'; break; 768e5dd7070Spatrick case BuiltinType::ObjCSel: 769e5dd7070Spatrick c = 'e'; break; 770e5dd7070Spatrick } 771e5dd7070Spatrick Out << c; 772e5dd7070Spatrick return; 773e5dd7070Spatrick } 774e5dd7070Spatrick 775e5dd7070Spatrick // If we have already seen this (non-built-in) type, use a substitution 776e5dd7070Spatrick // encoding. 777e5dd7070Spatrick llvm::DenseMap<const Type *, unsigned>::iterator Substitution 778e5dd7070Spatrick = TypeSubstitutions.find(T.getTypePtr()); 779e5dd7070Spatrick if (Substitution != TypeSubstitutions.end()) { 780e5dd7070Spatrick Out << 'S' << Substitution->second << '_'; 781e5dd7070Spatrick return; 782e5dd7070Spatrick } else { 783e5dd7070Spatrick // Record this as a substitution. 784e5dd7070Spatrick unsigned Number = TypeSubstitutions.size(); 785e5dd7070Spatrick TypeSubstitutions[T.getTypePtr()] = Number; 786e5dd7070Spatrick } 787e5dd7070Spatrick 788e5dd7070Spatrick if (const PointerType *PT = T->getAs<PointerType>()) { 789e5dd7070Spatrick Out << '*'; 790e5dd7070Spatrick T = PT->getPointeeType(); 791e5dd7070Spatrick continue; 792e5dd7070Spatrick } 793e5dd7070Spatrick if (const ObjCObjectPointerType *OPT = T->getAs<ObjCObjectPointerType>()) { 794e5dd7070Spatrick Out << '*'; 795e5dd7070Spatrick T = OPT->getPointeeType(); 796e5dd7070Spatrick continue; 797e5dd7070Spatrick } 798e5dd7070Spatrick if (const RValueReferenceType *RT = T->getAs<RValueReferenceType>()) { 799e5dd7070Spatrick Out << "&&"; 800e5dd7070Spatrick T = RT->getPointeeType(); 801e5dd7070Spatrick continue; 802e5dd7070Spatrick } 803e5dd7070Spatrick if (const ReferenceType *RT = T->getAs<ReferenceType>()) { 804e5dd7070Spatrick Out << '&'; 805e5dd7070Spatrick T = RT->getPointeeType(); 806e5dd7070Spatrick continue; 807e5dd7070Spatrick } 808e5dd7070Spatrick if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) { 809e5dd7070Spatrick Out << 'F'; 810e5dd7070Spatrick VisitType(FT->getReturnType()); 811e5dd7070Spatrick Out << '('; 812e5dd7070Spatrick for (const auto &I : FT->param_types()) { 813e5dd7070Spatrick Out << '#'; 814e5dd7070Spatrick VisitType(I); 815e5dd7070Spatrick } 816e5dd7070Spatrick Out << ')'; 817e5dd7070Spatrick if (FT->isVariadic()) 818e5dd7070Spatrick Out << '.'; 819e5dd7070Spatrick return; 820e5dd7070Spatrick } 821e5dd7070Spatrick if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) { 822e5dd7070Spatrick Out << 'B'; 823e5dd7070Spatrick T = BT->getPointeeType(); 824e5dd7070Spatrick continue; 825e5dd7070Spatrick } 826e5dd7070Spatrick if (const ComplexType *CT = T->getAs<ComplexType>()) { 827e5dd7070Spatrick Out << '<'; 828e5dd7070Spatrick T = CT->getElementType(); 829e5dd7070Spatrick continue; 830e5dd7070Spatrick } 831e5dd7070Spatrick if (const TagType *TT = T->getAs<TagType>()) { 832e5dd7070Spatrick Out << '$'; 833e5dd7070Spatrick VisitTagDecl(TT->getDecl()); 834e5dd7070Spatrick return; 835e5dd7070Spatrick } 836e5dd7070Spatrick if (const ObjCInterfaceType *OIT = T->getAs<ObjCInterfaceType>()) { 837e5dd7070Spatrick Out << '$'; 838e5dd7070Spatrick VisitObjCInterfaceDecl(OIT->getDecl()); 839e5dd7070Spatrick return; 840e5dd7070Spatrick } 841e5dd7070Spatrick if (const ObjCObjectType *OIT = T->getAs<ObjCObjectType>()) { 842e5dd7070Spatrick Out << 'Q'; 843e5dd7070Spatrick VisitType(OIT->getBaseType()); 844e5dd7070Spatrick for (auto *Prot : OIT->getProtocols()) 845e5dd7070Spatrick VisitObjCProtocolDecl(Prot); 846e5dd7070Spatrick return; 847e5dd7070Spatrick } 848e5dd7070Spatrick if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) { 849e5dd7070Spatrick Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); 850e5dd7070Spatrick return; 851e5dd7070Spatrick } 852e5dd7070Spatrick if (const TemplateSpecializationType *Spec 853e5dd7070Spatrick = T->getAs<TemplateSpecializationType>()) { 854e5dd7070Spatrick Out << '>'; 855e5dd7070Spatrick VisitTemplateName(Spec->getTemplateName()); 856e5dd7070Spatrick Out << Spec->getNumArgs(); 857e5dd7070Spatrick for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 858e5dd7070Spatrick VisitTemplateArgument(Spec->getArg(I)); 859e5dd7070Spatrick return; 860e5dd7070Spatrick } 861e5dd7070Spatrick if (const DependentNameType *DNT = T->getAs<DependentNameType>()) { 862e5dd7070Spatrick Out << '^'; 863e5dd7070Spatrick printQualifier(Out, Ctx, DNT->getQualifier()); 864e5dd7070Spatrick Out << ':' << DNT->getIdentifier()->getName(); 865e5dd7070Spatrick return; 866e5dd7070Spatrick } 867e5dd7070Spatrick if (const InjectedClassNameType *InjT = T->getAs<InjectedClassNameType>()) { 868e5dd7070Spatrick T = InjT->getInjectedSpecializationType(); 869e5dd7070Spatrick continue; 870e5dd7070Spatrick } 871e5dd7070Spatrick if (const auto *VT = T->getAs<VectorType>()) { 872e5dd7070Spatrick Out << (T->isExtVectorType() ? ']' : '['); 873e5dd7070Spatrick Out << VT->getNumElements(); 874e5dd7070Spatrick T = VT->getElementType(); 875e5dd7070Spatrick continue; 876e5dd7070Spatrick } 877e5dd7070Spatrick if (const auto *const AT = dyn_cast<ArrayType>(T)) { 878e5dd7070Spatrick Out << '{'; 879e5dd7070Spatrick switch (AT->getSizeModifier()) { 880e5dd7070Spatrick case ArrayType::Static: 881e5dd7070Spatrick Out << 's'; 882e5dd7070Spatrick break; 883e5dd7070Spatrick case ArrayType::Star: 884e5dd7070Spatrick Out << '*'; 885e5dd7070Spatrick break; 886e5dd7070Spatrick case ArrayType::Normal: 887e5dd7070Spatrick Out << 'n'; 888e5dd7070Spatrick break; 889e5dd7070Spatrick } 890e5dd7070Spatrick if (const auto *const CAT = dyn_cast<ConstantArrayType>(T)) 891e5dd7070Spatrick Out << CAT->getSize(); 892e5dd7070Spatrick 893e5dd7070Spatrick T = AT->getElementType(); 894e5dd7070Spatrick continue; 895e5dd7070Spatrick } 896e5dd7070Spatrick 897e5dd7070Spatrick // Unhandled type. 898e5dd7070Spatrick Out << ' '; 899e5dd7070Spatrick break; 900e5dd7070Spatrick } while (true); 901e5dd7070Spatrick } 902e5dd7070Spatrick 903e5dd7070Spatrick void USRGenerator::VisitTemplateParameterList( 904e5dd7070Spatrick const TemplateParameterList *Params) { 905e5dd7070Spatrick if (!Params) 906e5dd7070Spatrick return; 907e5dd7070Spatrick Out << '>' << Params->size(); 908e5dd7070Spatrick for (TemplateParameterList::const_iterator P = Params->begin(), 909e5dd7070Spatrick PEnd = Params->end(); 910e5dd7070Spatrick P != PEnd; ++P) { 911e5dd7070Spatrick Out << '#'; 912e5dd7070Spatrick if (isa<TemplateTypeParmDecl>(*P)) { 913e5dd7070Spatrick if (cast<TemplateTypeParmDecl>(*P)->isParameterPack()) 914e5dd7070Spatrick Out<< 'p'; 915e5dd7070Spatrick Out << 'T'; 916e5dd7070Spatrick continue; 917e5dd7070Spatrick } 918e5dd7070Spatrick 919e5dd7070Spatrick if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) { 920e5dd7070Spatrick if (NTTP->isParameterPack()) 921e5dd7070Spatrick Out << 'p'; 922e5dd7070Spatrick Out << 'N'; 923e5dd7070Spatrick VisitType(NTTP->getType()); 924e5dd7070Spatrick continue; 925e5dd7070Spatrick } 926e5dd7070Spatrick 927e5dd7070Spatrick TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P); 928e5dd7070Spatrick if (TTP->isParameterPack()) 929e5dd7070Spatrick Out << 'p'; 930e5dd7070Spatrick Out << 't'; 931e5dd7070Spatrick VisitTemplateParameterList(TTP->getTemplateParameters()); 932e5dd7070Spatrick } 933e5dd7070Spatrick } 934e5dd7070Spatrick 935e5dd7070Spatrick void USRGenerator::VisitTemplateName(TemplateName Name) { 936e5dd7070Spatrick if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 937e5dd7070Spatrick if (TemplateTemplateParmDecl *TTP 938e5dd7070Spatrick = dyn_cast<TemplateTemplateParmDecl>(Template)) { 939e5dd7070Spatrick Out << 't' << TTP->getDepth() << '.' << TTP->getIndex(); 940e5dd7070Spatrick return; 941e5dd7070Spatrick } 942e5dd7070Spatrick 943e5dd7070Spatrick Visit(Template); 944e5dd7070Spatrick return; 945e5dd7070Spatrick } 946e5dd7070Spatrick 947e5dd7070Spatrick // FIXME: Visit dependent template names. 948e5dd7070Spatrick } 949e5dd7070Spatrick 950e5dd7070Spatrick void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) { 951e5dd7070Spatrick switch (Arg.getKind()) { 952e5dd7070Spatrick case TemplateArgument::Null: 953e5dd7070Spatrick break; 954e5dd7070Spatrick 955e5dd7070Spatrick case TemplateArgument::Declaration: 956e5dd7070Spatrick Visit(Arg.getAsDecl()); 957e5dd7070Spatrick break; 958e5dd7070Spatrick 959e5dd7070Spatrick case TemplateArgument::NullPtr: 960e5dd7070Spatrick break; 961e5dd7070Spatrick 962e5dd7070Spatrick case TemplateArgument::TemplateExpansion: 963e5dd7070Spatrick Out << 'P'; // pack expansion of... 964e5dd7070Spatrick LLVM_FALLTHROUGH; 965e5dd7070Spatrick case TemplateArgument::Template: 966e5dd7070Spatrick VisitTemplateName(Arg.getAsTemplateOrTemplatePattern()); 967e5dd7070Spatrick break; 968e5dd7070Spatrick 969e5dd7070Spatrick case TemplateArgument::Expression: 970e5dd7070Spatrick // FIXME: Visit expressions. 971e5dd7070Spatrick break; 972e5dd7070Spatrick 973e5dd7070Spatrick case TemplateArgument::Pack: 974e5dd7070Spatrick Out << 'p' << Arg.pack_size(); 975e5dd7070Spatrick for (const auto &P : Arg.pack_elements()) 976e5dd7070Spatrick VisitTemplateArgument(P); 977e5dd7070Spatrick break; 978e5dd7070Spatrick 979e5dd7070Spatrick case TemplateArgument::Type: 980e5dd7070Spatrick VisitType(Arg.getAsType()); 981e5dd7070Spatrick break; 982e5dd7070Spatrick 983e5dd7070Spatrick case TemplateArgument::Integral: 984e5dd7070Spatrick Out << 'V'; 985e5dd7070Spatrick VisitType(Arg.getIntegralType()); 986e5dd7070Spatrick Out << Arg.getAsIntegral(); 987e5dd7070Spatrick break; 988e5dd7070Spatrick } 989e5dd7070Spatrick } 990e5dd7070Spatrick 991e5dd7070Spatrick void USRGenerator::VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) { 992e5dd7070Spatrick if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 993e5dd7070Spatrick return; 994e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 995e5dd7070Spatrick Out << "@UUV@"; 996e5dd7070Spatrick printQualifier(Out, D->getASTContext(), D->getQualifier()); 997e5dd7070Spatrick EmitDeclName(D); 998e5dd7070Spatrick } 999e5dd7070Spatrick 1000e5dd7070Spatrick void USRGenerator::VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) { 1001e5dd7070Spatrick if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D))) 1002e5dd7070Spatrick return; 1003e5dd7070Spatrick VisitDeclContext(D->getDeclContext()); 1004e5dd7070Spatrick Out << "@UUT@"; 1005e5dd7070Spatrick printQualifier(Out, D->getASTContext(), D->getQualifier()); 1006e5dd7070Spatrick Out << D->getName(); // Simple name. 1007e5dd7070Spatrick } 1008e5dd7070Spatrick 1009e5dd7070Spatrick 1010e5dd7070Spatrick 1011e5dd7070Spatrick //===----------------------------------------------------------------------===// 1012e5dd7070Spatrick // USR generation functions. 1013e5dd7070Spatrick //===----------------------------------------------------------------------===// 1014e5dd7070Spatrick 1015e5dd7070Spatrick static void combineClassAndCategoryExtContainers(StringRef ClsSymDefinedIn, 1016e5dd7070Spatrick StringRef CatSymDefinedIn, 1017e5dd7070Spatrick raw_ostream &OS) { 1018e5dd7070Spatrick if (ClsSymDefinedIn.empty() && CatSymDefinedIn.empty()) 1019e5dd7070Spatrick return; 1020e5dd7070Spatrick if (CatSymDefinedIn.empty()) { 1021e5dd7070Spatrick OS << "@M@" << ClsSymDefinedIn << '@'; 1022e5dd7070Spatrick return; 1023e5dd7070Spatrick } 1024e5dd7070Spatrick OS << "@CM@" << CatSymDefinedIn << '@'; 1025e5dd7070Spatrick if (ClsSymDefinedIn != CatSymDefinedIn) { 1026e5dd7070Spatrick OS << ClsSymDefinedIn << '@'; 1027e5dd7070Spatrick } 1028e5dd7070Spatrick } 1029e5dd7070Spatrick 1030e5dd7070Spatrick void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS, 1031e5dd7070Spatrick StringRef ExtSymDefinedIn, 1032e5dd7070Spatrick StringRef CategoryContextExtSymbolDefinedIn) { 1033e5dd7070Spatrick combineClassAndCategoryExtContainers(ExtSymDefinedIn, 1034e5dd7070Spatrick CategoryContextExtSymbolDefinedIn, OS); 1035e5dd7070Spatrick OS << "objc(cs)" << Cls; 1036e5dd7070Spatrick } 1037e5dd7070Spatrick 1038e5dd7070Spatrick void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat, 1039e5dd7070Spatrick raw_ostream &OS, 1040e5dd7070Spatrick StringRef ClsSymDefinedIn, 1041e5dd7070Spatrick StringRef CatSymDefinedIn) { 1042e5dd7070Spatrick combineClassAndCategoryExtContainers(ClsSymDefinedIn, CatSymDefinedIn, OS); 1043e5dd7070Spatrick OS << "objc(cy)" << Cls << '@' << Cat; 1044e5dd7070Spatrick } 1045e5dd7070Spatrick 1046e5dd7070Spatrick void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) { 1047e5dd7070Spatrick OS << '@' << Ivar; 1048e5dd7070Spatrick } 1049e5dd7070Spatrick 1050e5dd7070Spatrick void clang::index::generateUSRForObjCMethod(StringRef Sel, 1051e5dd7070Spatrick bool IsInstanceMethod, 1052e5dd7070Spatrick raw_ostream &OS) { 1053e5dd7070Spatrick OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel; 1054e5dd7070Spatrick } 1055e5dd7070Spatrick 1056e5dd7070Spatrick void clang::index::generateUSRForObjCProperty(StringRef Prop, bool isClassProp, 1057e5dd7070Spatrick raw_ostream &OS) { 1058e5dd7070Spatrick OS << (isClassProp ? "(cpy)" : "(py)") << Prop; 1059e5dd7070Spatrick } 1060e5dd7070Spatrick 1061e5dd7070Spatrick void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS, 1062e5dd7070Spatrick StringRef ExtSymDefinedIn) { 1063e5dd7070Spatrick if (!ExtSymDefinedIn.empty()) 1064e5dd7070Spatrick OS << "@M@" << ExtSymDefinedIn << '@'; 1065e5dd7070Spatrick OS << "objc(pl)" << Prot; 1066e5dd7070Spatrick } 1067e5dd7070Spatrick 1068e5dd7070Spatrick void clang::index::generateUSRForGlobalEnum(StringRef EnumName, raw_ostream &OS, 1069e5dd7070Spatrick StringRef ExtSymDefinedIn) { 1070e5dd7070Spatrick if (!ExtSymDefinedIn.empty()) 1071e5dd7070Spatrick OS << "@M@" << ExtSymDefinedIn; 1072e5dd7070Spatrick OS << "@E@" << EnumName; 1073e5dd7070Spatrick } 1074e5dd7070Spatrick 1075e5dd7070Spatrick void clang::index::generateUSRForEnumConstant(StringRef EnumConstantName, 1076e5dd7070Spatrick raw_ostream &OS) { 1077e5dd7070Spatrick OS << '@' << EnumConstantName; 1078e5dd7070Spatrick } 1079e5dd7070Spatrick 1080e5dd7070Spatrick bool clang::index::generateUSRForDecl(const Decl *D, 1081e5dd7070Spatrick SmallVectorImpl<char> &Buf) { 1082e5dd7070Spatrick if (!D) 1083e5dd7070Spatrick return true; 1084e5dd7070Spatrick // We don't ignore decls with invalid source locations. Implicit decls, like 1085e5dd7070Spatrick // C++'s operator new function, can have invalid locations but it is fine to 1086e5dd7070Spatrick // create USRs that can identify them. 1087e5dd7070Spatrick 1088e5dd7070Spatrick USRGenerator UG(&D->getASTContext(), Buf); 1089e5dd7070Spatrick UG.Visit(D); 1090e5dd7070Spatrick return UG.ignoreResults(); 1091e5dd7070Spatrick } 1092e5dd7070Spatrick 1093e5dd7070Spatrick bool clang::index::generateUSRForMacro(const MacroDefinitionRecord *MD, 1094e5dd7070Spatrick const SourceManager &SM, 1095e5dd7070Spatrick SmallVectorImpl<char> &Buf) { 1096e5dd7070Spatrick if (!MD) 1097e5dd7070Spatrick return true; 1098e5dd7070Spatrick return generateUSRForMacro(MD->getName()->getName(), MD->getLocation(), 1099e5dd7070Spatrick SM, Buf); 1100e5dd7070Spatrick 1101e5dd7070Spatrick } 1102e5dd7070Spatrick 1103e5dd7070Spatrick bool clang::index::generateUSRForMacro(StringRef MacroName, SourceLocation Loc, 1104e5dd7070Spatrick const SourceManager &SM, 1105e5dd7070Spatrick SmallVectorImpl<char> &Buf) { 1106*a9ac8606Spatrick if (MacroName.empty()) 1107e5dd7070Spatrick return true; 1108e5dd7070Spatrick 1109e5dd7070Spatrick llvm::raw_svector_ostream Out(Buf); 1110e5dd7070Spatrick 1111e5dd7070Spatrick // Assume that system headers are sane. Don't put source location 1112e5dd7070Spatrick // information into the USR if the macro comes from a system header. 1113*a9ac8606Spatrick bool ShouldGenerateLocation = Loc.isValid() && !SM.isInSystemHeader(Loc); 1114e5dd7070Spatrick 1115e5dd7070Spatrick Out << getUSRSpacePrefix(); 1116e5dd7070Spatrick if (ShouldGenerateLocation) 1117e5dd7070Spatrick printLoc(Out, Loc, SM, /*IncludeOffset=*/true); 1118e5dd7070Spatrick Out << "@macro@"; 1119e5dd7070Spatrick Out << MacroName; 1120e5dd7070Spatrick return false; 1121e5dd7070Spatrick } 1122e5dd7070Spatrick 1123e5dd7070Spatrick bool clang::index::generateUSRForType(QualType T, ASTContext &Ctx, 1124e5dd7070Spatrick SmallVectorImpl<char> &Buf) { 1125e5dd7070Spatrick if (T.isNull()) 1126e5dd7070Spatrick return true; 1127e5dd7070Spatrick T = T.getCanonicalType(); 1128e5dd7070Spatrick 1129e5dd7070Spatrick USRGenerator UG(&Ctx, Buf); 1130e5dd7070Spatrick UG.VisitType(T); 1131e5dd7070Spatrick return UG.ignoreResults(); 1132e5dd7070Spatrick } 1133e5dd7070Spatrick 1134e5dd7070Spatrick bool clang::index::generateFullUSRForModule(const Module *Mod, 1135e5dd7070Spatrick raw_ostream &OS) { 1136e5dd7070Spatrick if (!Mod->Parent) 1137e5dd7070Spatrick return generateFullUSRForTopLevelModuleName(Mod->Name, OS); 1138e5dd7070Spatrick if (generateFullUSRForModule(Mod->Parent, OS)) 1139e5dd7070Spatrick return true; 1140e5dd7070Spatrick return generateUSRFragmentForModule(Mod, OS); 1141e5dd7070Spatrick } 1142e5dd7070Spatrick 1143e5dd7070Spatrick bool clang::index::generateFullUSRForTopLevelModuleName(StringRef ModName, 1144e5dd7070Spatrick raw_ostream &OS) { 1145e5dd7070Spatrick OS << getUSRSpacePrefix(); 1146e5dd7070Spatrick return generateUSRFragmentForModuleName(ModName, OS); 1147e5dd7070Spatrick } 1148e5dd7070Spatrick 1149e5dd7070Spatrick bool clang::index::generateUSRFragmentForModule(const Module *Mod, 1150e5dd7070Spatrick raw_ostream &OS) { 1151e5dd7070Spatrick return generateUSRFragmentForModuleName(Mod->Name, OS); 1152e5dd7070Spatrick } 1153e5dd7070Spatrick 1154e5dd7070Spatrick bool clang::index::generateUSRFragmentForModuleName(StringRef ModName, 1155e5dd7070Spatrick raw_ostream &OS) { 1156e5dd7070Spatrick OS << "@M@" << ModName; 1157e5dd7070Spatrick return false; 1158e5dd7070Spatrick } 1159