int DeclarationName::compare(DeclarationName LHS, DeclarationName RHS) { if (LHS.getNameKind() != RHS.getNameKind()) return (LHS.getNameKind() < RHS.getNameKind() ? -1 : 1); switch (LHS.getNameKind()) { case DeclarationName::Identifier: { IdentifierInfo *LII = LHS.getAsIdentifierInfo(); IdentifierInfo *RII = RHS.getAsIdentifierInfo(); if (!LII) return RII ? -1 : 0; if (!RII) return 1; return LII->getName().compare(RII->getName()); } case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: case DeclarationName::ObjCMultiArgSelector: { Selector LHSSelector = LHS.getObjCSelector(); Selector RHSSelector = RHS.getObjCSelector(); unsigned LN = LHSSelector.getNumArgs(), RN = RHSSelector.getNumArgs(); for (unsigned I = 0, N = std::min(LN, RN); I != N; ++I) { switch (LHSSelector.getNameForSlot(I).compare( RHSSelector.getNameForSlot(I))) { case -1: return true; case 1: return false; default: break; } } return compareInt(LN, RN); } case DeclarationName::CXXConstructorName: case DeclarationName::CXXDestructorName: case DeclarationName::CXXConversionFunctionName: if (QualTypeOrdering()(LHS.getCXXNameType(), RHS.getCXXNameType())) return -1; if (QualTypeOrdering()(RHS.getCXXNameType(), LHS.getCXXNameType())) return 1; return 0; case DeclarationName::CXXOperatorName: return compareInt(LHS.getCXXOverloadedOperator(), RHS.getCXXOverloadedOperator()); case DeclarationName::CXXLiteralOperatorName: return LHS.getCXXLiteralIdentifier()->getName().compare( RHS.getCXXLiteralIdentifier()->getName()); case DeclarationName::CXXUsingDirective: return 0; } return 0; }
void ODRHash::AddDeclarationName(DeclarationName Name) { // Index all DeclarationName and use index numbers to refer to them. auto Result = DeclNameMap.insert(std::make_pair(Name, DeclNameMap.size())); ID.AddInteger(Result.first->second); if (!Result.second) { // If found in map, the the DeclarationName has previously been processed. return; } // First time processing each DeclarationName, also process its details. AddBoolean(Name.isEmpty()); if (Name.isEmpty()) return; auto Kind = Name.getNameKind(); ID.AddInteger(Kind); switch (Kind) { case DeclarationName::Identifier: AddIdentifierInfo(Name.getAsIdentifierInfo()); break; case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: case DeclarationName::ObjCMultiArgSelector: { Selector S = Name.getObjCSelector(); AddBoolean(S.isNull()); AddBoolean(S.isKeywordSelector()); AddBoolean(S.isUnarySelector()); unsigned NumArgs = S.getNumArgs(); for (unsigned i = 0; i < NumArgs; ++i) { AddIdentifierInfo(S.getIdentifierInfoForSlot(i)); } break; } case DeclarationName::CXXConstructorName: case DeclarationName::CXXDestructorName: AddQualType(Name.getCXXNameType()); break; case DeclarationName::CXXOperatorName: ID.AddInteger(Name.getCXXOverloadedOperator()); break; case DeclarationName::CXXLiteralOperatorName: AddIdentifierInfo(Name.getCXXLiteralIdentifier()); break; case DeclarationName::CXXConversionFunctionName: AddQualType(Name.getCXXNameType()); break; case DeclarationName::CXXUsingDirective: break; case DeclarationName::CXXDeductionGuideName: { auto *Template = Name.getCXXDeductionGuideTemplate(); AddBoolean(Template); if (Template) { AddDecl(Template); } } } }
void ODRHash::AddDeclarationName(DeclarationName Name) { AddBoolean(Name.isEmpty()); if (Name.isEmpty()) return; auto Kind = Name.getNameKind(); ID.AddInteger(Kind); switch (Kind) { case DeclarationName::Identifier: AddIdentifierInfo(Name.getAsIdentifierInfo()); break; case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: case DeclarationName::ObjCMultiArgSelector: { Selector S = Name.getObjCSelector(); AddBoolean(S.isNull()); AddBoolean(S.isKeywordSelector()); AddBoolean(S.isUnarySelector()); unsigned NumArgs = S.getNumArgs(); for (unsigned i = 0; i < NumArgs; ++i) { AddIdentifierInfo(S.getIdentifierInfoForSlot(i)); } break; } case DeclarationName::CXXConstructorName: case DeclarationName::CXXDestructorName: AddQualType(Name.getCXXNameType()); break; case DeclarationName::CXXOperatorName: ID.AddInteger(Name.getCXXOverloadedOperator()); break; case DeclarationName::CXXLiteralOperatorName: AddIdentifierInfo(Name.getCXXLiteralIdentifier()); break; case DeclarationName::CXXConversionFunctionName: AddQualType(Name.getCXXNameType()); break; case DeclarationName::CXXUsingDirective: break; case DeclarationName::CXXDeductionGuideName: { auto *Template = Name.getCXXDeductionGuideTemplate(); AddBoolean(Template); if (Template) { AddDecl(Template); } } } }
bool operator<(DeclarationName LHS, DeclarationName RHS) { if (LHS.getNameKind() != RHS.getNameKind()) return LHS.getNameKind() < RHS.getNameKind(); switch (LHS.getNameKind()) { case DeclarationName::Identifier: return LHS.getAsIdentifierInfo()->getName() < RHS.getAsIdentifierInfo()->getName(); case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: case DeclarationName::ObjCMultiArgSelector: { Selector LHSSelector = LHS.getObjCSelector(); Selector RHSSelector = RHS.getObjCSelector(); for (unsigned I = 0, N = std::min(LHSSelector.getNumArgs(), RHSSelector.getNumArgs()); I != N; ++I) { IdentifierInfo *LHSId = LHSSelector.getIdentifierInfoForSlot(I); IdentifierInfo *RHSId = RHSSelector.getIdentifierInfoForSlot(I); if (!LHSId || !RHSId) return LHSId && !RHSId; switch (LHSId->getName().compare(RHSId->getName())) { case -1: return true; case 1: return false; default: break; } } return LHSSelector.getNumArgs() < RHSSelector.getNumArgs(); } case DeclarationName::CXXConstructorName: case DeclarationName::CXXDestructorName: case DeclarationName::CXXConversionFunctionName: return QualTypeOrdering()(LHS.getCXXNameType(), RHS.getCXXNameType()); case DeclarationName::CXXOperatorName: return LHS.getCXXOverloadedOperator() < RHS.getCXXOverloadedOperator(); case DeclarationName::CXXUsingDirective: return false; } return false; }
bool RewriteUtils::replaceCXXDestructorDeclName( const CXXDestructorDecl *DtorDecl, const std::string &Name) { SourceLocation StartLoc = DtorDecl->getLocation(); const char *StartBuf = SrcManager->getCharacterData(StartLoc); TransAssert((*StartBuf == '~') && "Invalid Destructor Location"); // FIXME: it's quite ugly, better to use clang's Lexer unsigned Off = 0; StartBuf++; while (isspace(*StartBuf)) { StartBuf++; Off++; } std::string DName = DtorDecl->getNameAsString(); DeclarationNameInfo NameInfo = DtorDecl->getNameInfo(); DeclarationName DeclName = NameInfo.getName(); const Type *Ty = DeclName.getCXXNameType().getTypePtr(); size_t NameLen; if (Ty->getTypeClass() == Type::InjectedClassName) { const CXXRecordDecl *CXXRD = Ty->getAsCXXRecordDecl(); std::string RDName = CXXRD->getNameAsString(); NameLen = DName.find(RDName); TransAssert((NameLen != std::string::npos) && "Cannot find RecordDecl Name!"); NameLen += RDName.length(); } else { NameLen = DName.length(); } NameLen += Off; return !TheRewriter->ReplaceText(StartLoc, NameLen, "~" + Name); }
bool RewriteUtils::replaceFunctionDeclName(const FunctionDecl *FD, const std::string &NameStr) { // We cannot naively use FD->getNameAsString() here. // For example, for a template class // template<typename T> // class SomeClass { // public: // SomeClass() {} // }; // applying getNameAsString() on SomeClass() gives us SomeClass<T>. DeclarationNameInfo NameInfo = FD->getNameInfo(); DeclarationName DeclName = NameInfo.getName(); DeclarationName::NameKind K = DeclName.getNameKind(); TransAssert((K != DeclarationName::CXXDestructorName) && "Cannot rename CXXDestructorName here!"); std::string FDName = FD->getNameAsString(); size_t FDNameLen = FD->getNameAsString().length(); if (K == DeclarationName::CXXConstructorName) { const Type *Ty = DeclName.getCXXNameType().getTypePtr(); if (Ty->getTypeClass() == Type::InjectedClassName) { const CXXRecordDecl *CXXRD = Ty->getAsCXXRecordDecl(); std::string RDName = CXXRD->getNameAsString(); FDNameLen = FDName.find(RDName); TransAssert((FDNameLen != std::string::npos) && "Cannot find RecordDecl Name!"); FDNameLen += RDName.length(); } } return !TheRewriter->ReplaceText(NameInfo.getLoc(), FDNameLen, NameStr); }