1 //===- USRGeneration.cpp - Routines for USR generation --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Index/USRGeneration.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/DeclTemplate.h"
13 #include "clang/AST/DeclVisitor.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/Path.h"
16 #include "llvm/Support/raw_ostream.h"
18 using namespace clang;
19 using namespace clang::index;
21 //===----------------------------------------------------------------------===//
23 //===----------------------------------------------------------------------===//
26 class USRGenerator : public ConstDeclVisitor<USRGenerator> {
27 SmallVectorImpl<char> &Buf;
28 llvm::raw_svector_ostream Out;
33 llvm::DenseMap<const Type *, unsigned> TypeSubstitutions;
36 explicit USRGenerator(ASTContext *Ctx, SmallVectorImpl<char> &Buf)
43 // Add the USR space prefix.
44 Out << getUSRSpacePrefix();
47 bool ignoreResults() const { return IgnoreResults; }
49 // Visitation methods from generating USRs from AST elements.
50 void VisitDeclContext(const DeclContext *D);
51 void VisitFieldDecl(const FieldDecl *D);
52 void VisitFunctionDecl(const FunctionDecl *D);
53 void VisitNamedDecl(const NamedDecl *D);
54 void VisitNamespaceDecl(const NamespaceDecl *D);
55 void VisitNamespaceAliasDecl(const NamespaceAliasDecl *D);
56 void VisitFunctionTemplateDecl(const FunctionTemplateDecl *D);
57 void VisitClassTemplateDecl(const ClassTemplateDecl *D);
58 void VisitObjCContainerDecl(const ObjCContainerDecl *CD);
59 void VisitObjCMethodDecl(const ObjCMethodDecl *MD);
60 void VisitObjCPropertyDecl(const ObjCPropertyDecl *D);
61 void VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D);
62 void VisitTagDecl(const TagDecl *D);
63 void VisitTypedefDecl(const TypedefDecl *D);
64 void VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D);
65 void VisitVarDecl(const VarDecl *D);
66 void VisitNonTypeTemplateParmDecl(const NonTypeTemplateParmDecl *D);
67 void VisitTemplateTemplateParmDecl(const TemplateTemplateParmDecl *D);
68 void VisitLinkageSpecDecl(const LinkageSpecDecl *D) {
71 void VisitUsingDirectiveDecl(const UsingDirectiveDecl *D) {
74 void VisitUsingDecl(const UsingDecl *D) {
77 void VisitUnresolvedUsingValueDecl(const UnresolvedUsingValueDecl *D) {
80 void VisitUnresolvedUsingTypenameDecl(const UnresolvedUsingTypenameDecl *D) {
84 /// Generate the string component containing the location of the
86 bool GenLoc(const Decl *D);
88 /// String generation methods used both by the visitation methods
89 /// and from other clients that want to directly generate USRs. These
90 /// methods do not construct complete USRs (which incorporate the parents
91 /// of an AST element), but only the fragments concerning the AST element
94 /// Generate a USR for an Objective-C class.
95 void GenObjCClass(StringRef cls) {
96 generateUSRForObjCClass(cls, Out);
98 /// Generate a USR for an Objective-C class category.
99 void GenObjCCategory(StringRef cls, StringRef cat) {
100 generateUSRForObjCCategory(cls, cat, Out);
102 /// Generate a USR fragment for an Objective-C instance variable. The
103 /// complete USR can be created by concatenating the USR for the
104 /// encompassing class with this USR fragment.
105 void GenObjCIvar(StringRef ivar) {
106 generateUSRForObjCIvar(ivar, Out);
108 /// Generate a USR fragment for an Objective-C method.
109 void GenObjCMethod(StringRef sel, bool isInstanceMethod) {
110 generateUSRForObjCMethod(sel, isInstanceMethod, Out);
112 /// Generate a USR fragment for an Objective-C property.
113 void GenObjCProperty(StringRef prop) {
114 generateUSRForObjCProperty(prop, Out);
116 /// Generate a USR for an Objective-C protocol.
117 void GenObjCProtocol(StringRef prot) {
118 generateUSRForObjCProtocol(prot, Out);
121 void VisitType(QualType T);
122 void VisitTemplateParameterList(const TemplateParameterList *Params);
123 void VisitTemplateName(TemplateName Name);
124 void VisitTemplateArgument(const TemplateArgument &Arg);
126 /// Emit a Decl's name using NamedDecl::printName() and return true if
127 /// the decl had no name.
128 bool EmitDeclName(const NamedDecl *D);
131 } // end anonymous namespace
133 //===----------------------------------------------------------------------===//
134 // Generating USRs from ASTS.
135 //===----------------------------------------------------------------------===//
137 bool USRGenerator::EmitDeclName(const NamedDecl *D) {
139 const unsigned startSize = Buf.size();
142 const unsigned endSize = Buf.size();
143 return startSize == endSize;
146 static inline bool ShouldGenerateLocation(const NamedDecl *D) {
147 return !D->isExternallyVisible();
150 void USRGenerator::VisitDeclContext(const DeclContext *DC) {
151 if (const NamedDecl *D = dyn_cast<NamedDecl>(DC))
155 void USRGenerator::VisitFieldDecl(const FieldDecl *D) {
156 // The USR for an ivar declared in a class extension is based on the
157 // ObjCInterfaceDecl, not the ObjCCategoryDecl.
158 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
161 VisitDeclContext(D->getDeclContext());
162 Out << (isa<ObjCIvarDecl>(D) ? "@" : "@FI@");
163 if (EmitDeclName(D)) {
164 // Bit fields can be anonymous.
165 IgnoreResults = true;
170 void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
171 if (ShouldGenerateLocation(D) && GenLoc(D))
174 VisitDeclContext(D->getDeclContext());
175 if (FunctionTemplateDecl *FunTmpl = D->getDescribedFunctionTemplate()) {
177 VisitTemplateParameterList(FunTmpl->getTemplateParameters());
182 ASTContext &Ctx = *Context;
183 if (!Ctx.getLangOpts().CPlusPlus || D->isExternC())
186 if (const TemplateArgumentList *
187 SpecArgs = D->getTemplateSpecializationArgs()) {
189 for (unsigned I = 0, N = SpecArgs->size(); I != N; ++I) {
191 VisitTemplateArgument(SpecArgs->get(I));
196 // Mangle in type information for the arguments.
197 for (FunctionDecl::param_const_iterator I = D->param_begin(),
201 if (ParmVarDecl *PD = *I)
202 VisitType(PD->getType());
207 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) {
210 if (unsigned quals = MD->getTypeQualifiers())
211 Out << (char)('0' + quals);
215 void USRGenerator::VisitNamedDecl(const NamedDecl *D) {
216 VisitDeclContext(D->getDeclContext());
219 if (EmitDeclName(D)) {
220 // The string can be empty if the declaration has no name; e.g., it is
221 // the ParmDecl with no name for declaration of a function pointer type,
222 // e.g.: void (*f)(void *);
223 // In this case, don't generate a USR.
224 IgnoreResults = true;
228 void USRGenerator::VisitVarDecl(const VarDecl *D) {
229 // VarDecls can be declared 'extern' within a function or method body,
230 // but their enclosing DeclContext is the function, not the TU. We need
231 // to check the storage class to correctly generate the USR.
232 if (ShouldGenerateLocation(D) && GenLoc(D))
235 VisitDeclContext(D->getDeclContext());
237 // Variables always have simple names.
238 StringRef s = D->getName();
240 // The string can be empty if the declaration has no name; e.g., it is
241 // the ParmDecl with no name for declaration of a function pointer type, e.g.:
242 // void (*f)(void *);
243 // In this case, don't generate a USR.
245 IgnoreResults = true;
250 void USRGenerator::VisitNonTypeTemplateParmDecl(
251 const NonTypeTemplateParmDecl *D) {
256 void USRGenerator::VisitTemplateTemplateParmDecl(
257 const TemplateTemplateParmDecl *D) {
262 void USRGenerator::VisitNamespaceDecl(const NamespaceDecl *D) {
263 if (D->isAnonymousNamespace()) {
268 VisitDeclContext(D->getDeclContext());
270 Out << "@N@" << D->getName();
273 void USRGenerator::VisitFunctionTemplateDecl(const FunctionTemplateDecl *D) {
274 VisitFunctionDecl(D->getTemplatedDecl());
277 void USRGenerator::VisitClassTemplateDecl(const ClassTemplateDecl *D) {
278 VisitTagDecl(D->getTemplatedDecl());
281 void USRGenerator::VisitNamespaceAliasDecl(const NamespaceAliasDecl *D) {
282 VisitDeclContext(D->getDeclContext());
284 Out << "@NA@" << D->getName();
287 void USRGenerator::VisitObjCMethodDecl(const ObjCMethodDecl *D) {
288 const DeclContext *container = D->getDeclContext();
289 if (const ObjCProtocolDecl *pd = dyn_cast<ObjCProtocolDecl>(container)) {
293 // The USR for a method declared in a class extension or category is based on
294 // the ObjCInterfaceDecl, not the ObjCCategoryDecl.
295 const ObjCInterfaceDecl *ID = D->getClassInterface();
297 IgnoreResults = true;
302 // Ideally we would use 'GenObjCMethod', but this is such a hot path
303 // for Objective-C code that we don't want to use
304 // DeclarationName::getAsString().
305 Out << (D->isInstanceMethod() ? "(im)" : "(cm)")
306 << DeclarationName(D->getSelector());
309 void USRGenerator::VisitObjCContainerDecl(const ObjCContainerDecl *D) {
310 switch (D->getKind()) {
312 llvm_unreachable("Invalid ObjC container.");
313 case Decl::ObjCInterface:
314 case Decl::ObjCImplementation:
315 GenObjCClass(D->getName());
317 case Decl::ObjCCategory: {
318 const ObjCCategoryDecl *CD = cast<ObjCCategoryDecl>(D);
319 const ObjCInterfaceDecl *ID = CD->getClassInterface();
321 // Handle invalid code where the @interface might not
322 // have been specified.
323 // FIXME: We should be able to generate this USR even if the
324 // @interface isn't available.
325 IgnoreResults = true;
328 // Specially handle class extensions, which are anonymous categories.
329 // We want to mangle in the location to uniquely distinguish them.
330 if (CD->IsClassExtension()) {
331 Out << "objc(ext)" << ID->getName() << '@';
335 GenObjCCategory(ID->getName(), CD->getName());
339 case Decl::ObjCCategoryImpl: {
340 const ObjCCategoryImplDecl *CD = cast<ObjCCategoryImplDecl>(D);
341 const ObjCInterfaceDecl *ID = CD->getClassInterface();
343 // Handle invalid code where the @interface might not
344 // have been specified.
345 // FIXME: We should be able to generate this USR even if the
346 // @interface isn't available.
347 IgnoreResults = true;
350 GenObjCCategory(ID->getName(), CD->getName());
353 case Decl::ObjCProtocol:
354 GenObjCProtocol(cast<ObjCProtocolDecl>(D)->getName());
359 void USRGenerator::VisitObjCPropertyDecl(const ObjCPropertyDecl *D) {
360 // The USR for a property declared in a class extension or category is based
361 // on the ObjCInterfaceDecl, not the ObjCCategoryDecl.
362 if (const ObjCInterfaceDecl *ID = Context->getObjContainingInterface(D))
365 Visit(cast<Decl>(D->getDeclContext()));
366 GenObjCProperty(D->getName());
369 void USRGenerator::VisitObjCPropertyImplDecl(const ObjCPropertyImplDecl *D) {
370 if (ObjCPropertyDecl *PD = D->getPropertyDecl()) {
371 VisitObjCPropertyDecl(PD);
375 IgnoreResults = true;
378 void USRGenerator::VisitTagDecl(const TagDecl *D) {
379 // Add the location of the tag decl to handle resolution across
380 // translation units.
381 if (ShouldGenerateLocation(D) && GenLoc(D))
384 D = D->getCanonicalDecl();
385 VisitDeclContext(D->getDeclContext());
387 bool AlreadyStarted = false;
388 if (const CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(D)) {
389 if (ClassTemplateDecl *ClassTmpl = CXXRecord->getDescribedClassTemplate()) {
390 AlreadyStarted = true;
392 switch (D->getTagKind()) {
394 case TTK_Struct: Out << "@ST"; break;
395 case TTK_Class: Out << "@CT"; break;
396 case TTK_Union: Out << "@UT"; break;
397 case TTK_Enum: llvm_unreachable("enum template");
399 VisitTemplateParameterList(ClassTmpl->getTemplateParameters());
400 } else if (const ClassTemplatePartialSpecializationDecl *PartialSpec
401 = dyn_cast<ClassTemplatePartialSpecializationDecl>(CXXRecord)) {
402 AlreadyStarted = true;
404 switch (D->getTagKind()) {
406 case TTK_Struct: Out << "@SP"; break;
407 case TTK_Class: Out << "@CP"; break;
408 case TTK_Union: Out << "@UP"; break;
409 case TTK_Enum: llvm_unreachable("enum partial specialization");
411 VisitTemplateParameterList(PartialSpec->getTemplateParameters());
415 if (!AlreadyStarted) {
416 switch (D->getTagKind()) {
418 case TTK_Struct: Out << "@S"; break;
419 case TTK_Class: Out << "@C"; break;
420 case TTK_Union: Out << "@U"; break;
421 case TTK_Enum: Out << "@E"; break;
427 assert(Buf.size() > 0);
428 const unsigned off = Buf.size() - 1;
430 if (EmitDeclName(D)) {
431 if (const TypedefNameDecl *TD = D->getTypedefNameForAnonDecl()) {
439 // For a class template specialization, mangle the template arguments.
440 if (const ClassTemplateSpecializationDecl *Spec
441 = dyn_cast<ClassTemplateSpecializationDecl>(D)) {
442 const TemplateArgumentList &Args = Spec->getTemplateInstantiationArgs();
444 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
446 VisitTemplateArgument(Args.get(I));
451 void USRGenerator::VisitTypedefDecl(const TypedefDecl *D) {
452 if (ShouldGenerateLocation(D) && GenLoc(D))
454 const DeclContext *DC = D->getDeclContext();
455 if (const NamedDecl *DCN = dyn_cast<NamedDecl>(DC))
461 void USRGenerator::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
466 bool USRGenerator::GenLoc(const Decl *D) {
468 return IgnoreResults;
471 // Guard against null declarations in invalid code.
473 IgnoreResults = true;
477 // Use the location of canonical decl.
478 D = D->getCanonicalDecl();
480 const SourceManager &SM = Context->getSourceManager();
481 SourceLocation L = D->getLocStart();
483 IgnoreResults = true;
486 L = SM.getExpansionLoc(L);
487 const std::pair<FileID, unsigned> &Decomposed = SM.getDecomposedLoc(L);
488 const FileEntry *FE = SM.getFileEntryForID(Decomposed.first);
490 Out << llvm::sys::path::filename(FE->getName());
493 // This case really isn't interesting.
494 IgnoreResults = true;
497 // Use the offest into the FileID to represent the location. Using
498 // a line/column can cause us to look back at the original source file,
499 // which is expensive.
500 Out << '@' << Decomposed.second;
501 return IgnoreResults;
504 void USRGenerator::VisitType(QualType T) {
505 // This method mangles in USR information for types. It can possibly
506 // just reuse the naming-mangling logic used by codegen, although the
507 // requirements for USRs might not be the same.
508 ASTContext &Ctx = *Context;
511 T = Ctx.getCanonicalType(T);
512 Qualifiers Q = T.getQualifiers();
521 Out << ((char) ('0' + qVal));
523 // Mangle in ObjC GC qualifiers?
525 if (const PackExpansionType *Expansion = T->getAs<PackExpansionType>()) {
527 T = Expansion->getPattern();
530 if (const BuiltinType *BT = T->getAs<BuiltinType>()) {
531 unsigned char c = '\0';
532 switch (BT->getKind()) {
533 case BuiltinType::Void:
535 case BuiltinType::Bool:
537 case BuiltinType::Char_U:
538 case BuiltinType::UChar:
540 case BuiltinType::Char16:
542 case BuiltinType::Char32:
544 case BuiltinType::UShort:
546 case BuiltinType::UInt:
548 case BuiltinType::ULong:
550 case BuiltinType::ULongLong:
552 case BuiltinType::UInt128:
554 case BuiltinType::Char_S:
555 case BuiltinType::SChar:
557 case BuiltinType::WChar_S:
558 case BuiltinType::WChar_U:
560 case BuiltinType::Short:
562 case BuiltinType::Int:
564 case BuiltinType::Long:
566 case BuiltinType::LongLong:
568 case BuiltinType::Int128:
570 case BuiltinType::Half:
572 case BuiltinType::Float:
574 case BuiltinType::Double:
576 case BuiltinType::LongDouble:
578 case BuiltinType::NullPtr:
580 #define BUILTIN_TYPE(Id, SingletonId)
581 #define PLACEHOLDER_TYPE(Id, SingletonId) case BuiltinType::Id:
582 #include "clang/AST/BuiltinTypes.def"
583 case BuiltinType::Dependent:
584 case BuiltinType::OCLImage1d:
585 case BuiltinType::OCLImage1dArray:
586 case BuiltinType::OCLImage1dBuffer:
587 case BuiltinType::OCLImage2d:
588 case BuiltinType::OCLImage2dArray:
589 case BuiltinType::OCLImage3d:
590 case BuiltinType::OCLEvent:
591 case BuiltinType::OCLSampler:
592 IgnoreResults = true;
594 case BuiltinType::ObjCId:
596 case BuiltinType::ObjCClass:
598 case BuiltinType::ObjCSel:
605 // If we have already seen this (non-built-in) type, use a substitution
607 llvm::DenseMap<const Type *, unsigned>::iterator Substitution
608 = TypeSubstitutions.find(T.getTypePtr());
609 if (Substitution != TypeSubstitutions.end()) {
610 Out << 'S' << Substitution->second << '_';
613 // Record this as a substitution.
614 unsigned Number = TypeSubstitutions.size();
615 TypeSubstitutions[T.getTypePtr()] = Number;
618 if (const PointerType *PT = T->getAs<PointerType>()) {
620 T = PT->getPointeeType();
623 if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
625 T = RT->getPointeeType();
628 if (const FunctionProtoType *FT = T->getAs<FunctionProtoType>()) {
630 VisitType(FT->getResultType());
631 for (FunctionProtoType::arg_type_iterator
632 I = FT->arg_type_begin(), E = FT->arg_type_end(); I!=E; ++I) {
635 if (FT->isVariadic())
639 if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) {
641 T = BT->getPointeeType();
644 if (const ComplexType *CT = T->getAs<ComplexType>()) {
646 T = CT->getElementType();
649 if (const TagType *TT = T->getAs<TagType>()) {
651 VisitTagDecl(TT->getDecl());
654 if (const TemplateTypeParmType *TTP = T->getAs<TemplateTypeParmType>()) {
655 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
658 if (const TemplateSpecializationType *Spec
659 = T->getAs<TemplateSpecializationType>()) {
661 VisitTemplateName(Spec->getTemplateName());
662 Out << Spec->getNumArgs();
663 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
664 VisitTemplateArgument(Spec->getArg(I));
674 void USRGenerator::VisitTemplateParameterList(
675 const TemplateParameterList *Params) {
678 Out << '>' << Params->size();
679 for (TemplateParameterList::const_iterator P = Params->begin(),
680 PEnd = Params->end();
683 if (isa<TemplateTypeParmDecl>(*P)) {
684 if (cast<TemplateTypeParmDecl>(*P)->isParameterPack())
690 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(*P)) {
691 if (NTTP->isParameterPack())
694 VisitType(NTTP->getType());
698 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(*P);
699 if (TTP->isParameterPack())
702 VisitTemplateParameterList(TTP->getTemplateParameters());
706 void USRGenerator::VisitTemplateName(TemplateName Name) {
707 if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
708 if (TemplateTemplateParmDecl *TTP
709 = dyn_cast<TemplateTemplateParmDecl>(Template)) {
710 Out << 't' << TTP->getDepth() << '.' << TTP->getIndex();
718 // FIXME: Visit dependent template names.
721 void USRGenerator::VisitTemplateArgument(const TemplateArgument &Arg) {
722 switch (Arg.getKind()) {
723 case TemplateArgument::Null:
726 case TemplateArgument::Declaration:
727 Visit(Arg.getAsDecl());
730 case TemplateArgument::NullPtr:
733 case TemplateArgument::TemplateExpansion:
734 Out << 'P'; // pack expansion of...
736 case TemplateArgument::Template:
737 VisitTemplateName(Arg.getAsTemplateOrTemplatePattern());
740 case TemplateArgument::Expression:
741 // FIXME: Visit expressions.
744 case TemplateArgument::Pack:
745 Out << 'p' << Arg.pack_size();
746 for (TemplateArgument::pack_iterator P = Arg.pack_begin(), PEnd = Arg.pack_end();
748 VisitTemplateArgument(*P);
751 case TemplateArgument::Type:
752 VisitType(Arg.getAsType());
755 case TemplateArgument::Integral:
757 VisitType(Arg.getIntegralType());
758 Out << Arg.getAsIntegral();
763 //===----------------------------------------------------------------------===//
764 // USR generation functions.
765 //===----------------------------------------------------------------------===//
767 void clang::index::generateUSRForObjCClass(StringRef Cls, raw_ostream &OS) {
768 OS << "objc(cs)" << Cls;
771 void clang::index::generateUSRForObjCCategory(StringRef Cls, StringRef Cat,
773 OS << "objc(cy)" << Cls << '@' << Cat;
776 void clang::index::generateUSRForObjCIvar(StringRef Ivar, raw_ostream &OS) {
780 void clang::index::generateUSRForObjCMethod(StringRef Sel,
781 bool IsInstanceMethod,
783 OS << (IsInstanceMethod ? "(im)" : "(cm)") << Sel;
786 void clang::index::generateUSRForObjCProperty(StringRef Prop, raw_ostream &OS) {
787 OS << "(py)" << Prop;
790 void clang::index::generateUSRForObjCProtocol(StringRef Prot, raw_ostream &OS) {
791 OS << "objc(pl)" << Prot;
794 bool clang::index::generateUSRForDecl(const Decl *D,
795 SmallVectorImpl<char> &Buf) {
796 // Don't generate USRs for things with invalid locations.
797 if (!D || D->getLocStart().isInvalid())
800 USRGenerator UG(&D->getASTContext(), Buf);
802 return UG.ignoreResults();