| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2017 The University of Utah |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // This file is distributed under the University of Illinois Open Source |
| 7 | // License. See the file COPYING for details. |
| 8 | // |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | #if HAVE_CONFIG_H |
| 12 | # include <config.h> |
| 13 | #endif |
| 14 | |
| 15 | #include "ReplaceClassWithBaseTemplateSpec.h" |
| 16 | |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/AST/RecursiveASTVisitor.h" |
| 19 | #include "clang/AST/ASTContext.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "This pass tries to replace a class with its base class if \n\ |
| 27 | * this class has only one base class, and \n\ |
| 28 | * this class doesn't have any explicit declaration, and \n\ |
| 29 | * the base class is a class template specialization \n\ |
| 30 | "; |
| 31 | |
| 32 | static RegisterTransformation<ReplaceClassWithBaseTemplateSpec> |
| 33 | Trans("replace-class-with-base-template-spec", DescriptionMsg); |
| 34 | |
| 35 | class ReplaceClassWithBaseTemplateSpecVisitor : public |
| 36 | RecursiveASTVisitor<ReplaceClassWithBaseTemplateSpecVisitor> { |
| 37 | |
| 38 | public: |
| 39 | explicit ReplaceClassWithBaseTemplateSpecVisitor( |
| 40 | ReplaceClassWithBaseTemplateSpec *Instance) |
| 41 | : ConsumerInstance(Instance) |
| 42 | { } |
| 43 | |
| 44 | bool VisitCXXRecordDecl(CXXRecordDecl *CXXRD); |
| 45 | |
| 46 | private: |
| 47 | ReplaceClassWithBaseTemplateSpec *ConsumerInstance; |
| 48 | }; |
| 49 | |
| 50 | bool ReplaceClassWithBaseTemplateSpecVisitor::VisitCXXRecordDecl( |
| 51 | CXXRecordDecl *CXXRD) |
| 52 | { |
| 53 | if (ConsumerInstance->isInIncludedFile(CXXRD) || |
| 54 | ConsumerInstance->isSpecialRecordDecl(CXXRD) || |
| 55 | !CXXRD->hasDefinition()) |
| 56 | return true; |
| 57 | ConsumerInstance->handleOneCXXRecordDecl(CXXRD->getDefinition()); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | class ReplaceClassWithBaseTemplateSpecRewriteVisitor : public |
| 62 | RecursiveASTVisitor<ReplaceClassWithBaseTemplateSpecRewriteVisitor> { |
| 63 | public: |
| 64 | explicit ReplaceClassWithBaseTemplateSpecRewriteVisitor( |
| 65 | ReplaceClassWithBaseTemplateSpec *Instance) |
| 66 | : ConsumerInstance(Instance) |
| 67 | { } |
| 68 | |
| 69 | bool VisitRecordTypeLoc(RecordTypeLoc TLoc); |
| 70 | |
| 71 | private: |
| 72 | ReplaceClassWithBaseTemplateSpec *ConsumerInstance; |
| 73 | }; |
| 74 | |
| 75 | bool ReplaceClassWithBaseTemplateSpecRewriteVisitor::VisitRecordTypeLoc( |
| 76 | RecordTypeLoc TLoc) |
| 77 | { |
| 78 | const Type *Ty = TLoc.getTypePtr(); |
| 79 | if (Ty->isUnionType()) |
| 80 | return true; |
| 81 | |
| 82 | const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(TLoc.getDecl()); |
| 83 | if (!RD || (RD->getCanonicalDecl() != ConsumerInstance->TheCXXRecord)) |
| 84 | return true; |
| 85 | |
| 86 | ConsumerInstance->RewriteHelper->replaceRecordType( |
| 87 | TLoc, ConsumerInstance->TheBaseName + " "); |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | void ReplaceClassWithBaseTemplateSpec::Initialize(ASTContext &context) |
| 92 | { |
| 93 | Transformation::Initialize(context); |
| 94 | CollectionVisitor = new ReplaceClassWithBaseTemplateSpecVisitor(this); |
| 95 | RewriteVisitor = new ReplaceClassWithBaseTemplateSpecRewriteVisitor(this); |
| 96 | } |
| 97 | |
| 98 | void ReplaceClassWithBaseTemplateSpec::HandleTranslationUnit(ASTContext &Ctx) |
| 99 | { |
| 100 | if (TransformationManager::isCLangOpt() || |
| 101 | TransformationManager::isOpenCLLangOpt()) { |
| 102 | ValidInstanceNum = 0; |
| 103 | } |
| 104 | else { |
| 105 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 106 | } |
| 107 | |
| 108 | if (QueryInstanceOnly) |
| 109 | return; |
| 110 | |
| 111 | if (TransformationCounter > ValidInstanceNum) { |
| 112 | TransError = TransMaxInstanceError; |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | TransAssert(TheCXXRecord && "TheCXXRecord is NULL!"); |
| 117 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 118 | TransAssert(RewriteVisitor && "NULL RewriteVisitor!"); |
| 119 | RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 120 | removeBaseSpecifier(); |
| 121 | |
| 122 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 123 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 124 | TransError = TransInternalError; |
| 125 | } |
| 126 | |
| 127 | void ReplaceClassWithBaseTemplateSpec::handleOneCXXRecordDecl( |
| 128 | const CXXRecordDecl *CXXRD) |
| 129 | { |
| 130 | TransAssert(CXXRD && "NULL CXXRD!"); |
| 131 | TransAssert(CXXRD->isThisDeclarationADefinition() && "Not a definition!"); |
| 132 | |
| 133 | if (getNumExplicitDecls(CXXRD)) |
| 134 | return; |
| 135 | if (CXXRD->getNumBases() != 1) |
| 136 | return; |
| 137 | |
| 138 | CXXRecordDecl::base_class_const_iterator I = CXXRD->bases_begin(); |
| 139 | const CXXBaseSpecifier *BS = I; |
| 140 | const Type *Ty = BS->getType().getTypePtr(); |
| 141 | const CXXRecordDecl *Base = getBaseDeclFromType(Ty); |
| 142 | if (!Base || !Base->hasDefinition() || !Base->getDescribedClassTemplate()) |
| 143 | return; |
| 144 | |
| 145 | ValidInstanceNum++; |
| 146 | if (ValidInstanceNum == TransformationCounter) { |
| 147 | BS->getType().getAsStringInternal(TheBaseName, |
| 148 | Context->getPrintingPolicy()); |
| 149 | TheCXXRecord = CXXRD; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void ReplaceClassWithBaseTemplateSpec::removeBaseSpecifier(void) |
| 154 | { |
| 155 | unsigned NumBases = TheCXXRecord->getNumBases(); (void)NumBases; |
| 156 | TransAssert((NumBases == 1) && "TheCXXRecord can have only one base!"); |
| 157 | SourceLocation StartLoc = |
| 158 | SrcManager->getSpellingLoc(TheCXXRecord->getSourceRange().getBegin()); |
| 159 | StartLoc = RewriteHelper->getLocationUntil(StartLoc, ':'); |
| 160 | SourceLocation EndLoc = RewriteHelper->getLocationUntil(StartLoc, '{'); |
| 161 | EndLoc = EndLoc.getLocWithOffset(-1); |
| 162 | |
| 163 | TheRewriter.RemoveText(SourceRange(StartLoc, EndLoc)); |
| 164 | } |
| 165 | |
| 166 | ReplaceClassWithBaseTemplateSpec::~ReplaceClassWithBaseTemplateSpec(void) |
| 167 | { |
| 168 | delete CollectionVisitor; |
| 169 | delete RewriteVisitor; |
| 170 | } |
| 171 |