| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2017, 2019 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 "ClassTemplateToClass.h" |
| 16 | |
| 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/Basic/SourceManager.h" |
| 20 | |
| 21 | #include "TransformationManager.h" |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
| 25 | static const char *DescriptionMsg = |
| 26 | "Change a class template to a class if this class template: \n\ |
| 27 | * has only one parameter, and \n\ |
| 28 | * the parameter is unused. \n"; |
| 29 | |
| 30 | static RegisterTransformation<ClassTemplateToClass> |
| 31 | Trans("class-template-to-class", DescriptionMsg); |
| 32 | |
| 33 | class ClassTemplateToClassASTVisitor : public |
| 34 | RecursiveASTVisitor<ClassTemplateToClassASTVisitor> { |
| 35 | |
| 36 | public: |
| 37 | explicit ClassTemplateToClassASTVisitor(ClassTemplateToClass *Instance) |
| 38 | : ConsumerInstance(Instance) |
| 39 | { } |
| 40 | |
| 41 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
| 42 | |
| 43 | private: |
| 44 | ClassTemplateToClass *ConsumerInstance; |
| 45 | |
| 46 | }; |
| 47 | |
| 48 | class ClassTemplateToClassSpecializationTypeRewriteVisitor : public |
| 49 | RecursiveASTVisitor<ClassTemplateToClassSpecializationTypeRewriteVisitor> { |
| 50 | |
| 51 | public: |
| 52 | explicit ClassTemplateToClassSpecializationTypeRewriteVisitor( |
| 53 | ClassTemplateToClass *Instance) |
| 54 | : ConsumerInstance(Instance) |
| 55 | { } |
| 56 | |
| 57 | bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc Loc); |
| 58 | |
| 59 | private: |
| 60 | |
| 61 | ClassTemplateToClass *ConsumerInstance; |
| 62 | }; |
| 63 | |
| 64 | namespace { |
| 65 | |
| 66 | class TemplateParameterTypeVisitor : public |
| 67 | RecursiveASTVisitor<TemplateParameterTypeVisitor> { |
| 68 | |
| 69 | public: |
| 70 | typedef llvm::SmallPtrSet<TemplateTypeParmDecl *, 8> TypeParmDeclSet; |
| 71 | |
| 72 | typedef llvm::SmallPtrSet<TemplateName *, 8> TemplateNameSet; |
| 73 | |
| 74 | ~TemplateParameterTypeVisitor(void) { |
| 75 | for (TemplateNameSet::iterator I = TmplNames.begin(), E = TmplNames.end(); |
| 76 | I != E; ++I) |
| 77 | delete (*I); |
| 78 | } |
| 79 | |
| 80 | explicit TemplateParameterTypeVisitor(ASTContext *Ctx) |
| 81 | : Context(Ctx) |
| 82 | { } |
| 83 | |
| 84 | bool VisitTemplateTypeParmType(TemplateTypeParmType *Ty); |
| 85 | |
| 86 | bool VisitTemplateSpecializationType(TemplateSpecializationType *Ty); |
| 87 | |
| 88 | bool isAUsedParameter(NamedDecl *ND); |
| 89 | |
| 90 | private: |
| 91 | TypeParmDeclSet ParmDecls; |
| 92 | |
| 93 | TemplateNameSet TmplNames; |
| 94 | |
| 95 | ASTContext *Context; |
| 96 | }; |
| 97 | |
| 98 | bool TemplateParameterTypeVisitor::VisitTemplateTypeParmType( |
| 99 | TemplateTypeParmType *Ty) |
| 100 | { |
| 101 | TemplateTypeParmDecl *D = Ty->getDecl(); |
| 102 | ParmDecls.insert(D); |
| 103 | return true; |
| 104 | } |
| 105 | |
| 106 | bool TemplateParameterTypeVisitor::VisitTemplateSpecializationType( |
| 107 | TemplateSpecializationType *Ty) |
| 108 | { |
| 109 | TemplateName Name = Ty->getTemplateName(); |
| 110 | if (Name.getKind() != TemplateName::Template) |
| 111 | return true; |
| 112 | |
| 113 | TemplateName *NewName = new TemplateName(Name.getAsTemplateDecl()); |
| 114 | TmplNames.insert(NewName); |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | bool TemplateParameterTypeVisitor::isAUsedParameter(NamedDecl *ND) |
| 119 | { |
| 120 | if (TemplateTypeParmDecl *ParmD = dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 121 | return ParmDecls.count(ParmD); |
| 122 | } |
| 123 | |
| 124 | if (TemplateTemplateParmDecl *ParmD = |
| 125 | dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 126 | TemplateName Name(ParmD); |
| 127 | for (TemplateNameSet::iterator I = TmplNames.begin(), E = TmplNames.end(); |
| 128 | I != E; ++I) { |
| 129 | if (Context->hasSameTemplateName(*(*I), Name)) |
| 130 | return true; |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | TransAssert(0 && "Uncatched Template Parameter Kind!"); |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | } |
| 140 | |
| 141 | bool ClassTemplateToClassASTVisitor::VisitClassTemplateDecl( |
| 142 | ClassTemplateDecl *D) |
| 143 | { |
| 144 | if (ConsumerInstance->isInIncludedFile(D)) |
| 145 | return true; |
| 146 | |
| 147 | ClassTemplateDecl *CanonicalD = D->getCanonicalDecl(); |
| 148 | if (ConsumerInstance->VisitedDecls.count(CanonicalD)) |
| 149 | return true; |
| 150 | |
| 151 | ConsumerInstance->VisitedDecls.insert(CanonicalD); |
| 152 | if (ConsumerInstance->isValidClassTemplateDecl(D)) { |
| 153 | ConsumerInstance->ValidInstanceNum++; |
| 154 | if (ConsumerInstance->ValidInstanceNum == |
| 155 | ConsumerInstance->TransformationCounter) { |
| 156 | ConsumerInstance->TheClassTemplateDecl = CanonicalD; |
| 157 | ConsumerInstance->TheTemplateName = new TemplateName(CanonicalD); |
| 158 | } |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | bool ClassTemplateToClassSpecializationTypeRewriteVisitor:: |
| 164 | VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc Loc) |
| 165 | { |
| 166 | const TemplateSpecializationType *Ty = |
| 167 | dyn_cast<TemplateSpecializationType>(Loc.getTypePtr()); |
| 168 | TransAssert(Ty && "Invalid TemplateSpecializationType!"); |
| 169 | |
| 170 | TemplateName TmplName = Ty->getTemplateName(); |
| 171 | if (!ConsumerInstance->referToTheTemplateDecl(TmplName)) |
| 172 | return true; |
| 173 | |
| 174 | SourceLocation TmplKeyLoc = Loc.getTemplateKeywordLoc(); |
| 175 | if (TmplKeyLoc.isValid()) |
| 176 | ConsumerInstance->TheRewriter.RemoveText(TmplKeyLoc, 8); |
| 177 | |
| 178 | // it's necessary to check the validity of locations, otherwise |
| 179 | // we will get assertion errors... |
| 180 | // note that some implicit typeloc has been visited by Clang, e.g. |
| 181 | // template < typename > struct A { }; |
| 182 | // struct B:A < int > { |
| 183 | // B ( ) { } |
| 184 | // }; |
| 185 | // base initializer A<int> is not presented in the code but visited, |
| 186 | // so, we need to make sure locations are valid. |
| 187 | SourceLocation LAngleLoc = Loc.getLAngleLoc(); |
| 188 | if (LAngleLoc.isInvalid()) |
| 189 | return true; |
| 190 | SourceLocation RAngleLoc = Loc.getRAngleLoc(); |
| 191 | if (RAngleLoc.isInvalid()) |
| 192 | return true; |
| 193 | ConsumerInstance->TheRewriter.RemoveText(SourceRange(LAngleLoc, |
| 194 | RAngleLoc)); |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | void ClassTemplateToClass::Initialize(ASTContext &context) |
| 199 | { |
| 200 | Transformation::Initialize(context); |
| 201 | CollectionVisitor = new ClassTemplateToClassASTVisitor(this); |
| 202 | RewriteVisitor = |
| 203 | new ClassTemplateToClassSpecializationTypeRewriteVisitor(this); |
| 204 | } |
| 205 | |
| 206 | void ClassTemplateToClass::HandleTranslationUnit(ASTContext &Ctx) |
| 207 | { |
| 208 | if (TransformationManager::isCLangOpt() || |
| 209 | TransformationManager::isOpenCLLangOpt()) { |
| 210 | ValidInstanceNum = 0; |
| 211 | } |
| 212 | else { |
| 213 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 214 | } |
| 215 | |
| 216 | if (QueryInstanceOnly) |
| 217 | return; |
| 218 | |
| 219 | if (TransformationCounter > ValidInstanceNum) { |
| 220 | TransError = TransMaxInstanceError; |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | TransAssert(TheClassTemplateDecl && "NULL TheClassTemplateDecl!"); |
| 225 | TransAssert(RewriteVisitor && "NULL RewriteVisitor!"); |
| 226 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 227 | rewriteClassTemplateDecls(); |
| 228 | rewriteClassTemplatePartialSpecs(); |
| 229 | RewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 230 | |
| 231 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 232 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 233 | TransError = TransInternalError; |
| 234 | } |
| 235 | |
| 236 | void ClassTemplateToClass::removeTemplateAndParameter(SourceLocation LocStart, |
| 237 | const TemplateParameterList *TPList) |
| 238 | { |
| 239 | SourceLocation LocEnd = TPList->getRAngleLoc(); |
| 240 | TheRewriter.RemoveText(SourceRange(LocStart, LocEnd)); |
| 241 | } |
| 242 | |
| 243 | void ClassTemplateToClass::rewriteClassTemplateDecls(void) |
| 244 | { |
| 245 | for (ClassTemplateDecl::redecl_iterator |
| 246 | I = TheClassTemplateDecl->redecls_begin(), |
| 247 | E = TheClassTemplateDecl->redecls_end(); |
| 248 | I != E; ++I) { |
| 249 | const TemplateParameterList *TPList = (*I)->getTemplateParameters(); |
| 250 | SourceLocation LocStart = (*I)->getBeginLoc(); |
| 251 | removeTemplateAndParameter(LocStart, TPList); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void ClassTemplateToClass::rewriteClassTemplatePartialSpecs(void) |
| 256 | { |
| 257 | SmallVector<ClassTemplatePartialSpecializationDecl *, 10> PartialDecls; |
| 258 | TheClassTemplateDecl->getPartialSpecializations(PartialDecls); |
| 259 | |
| 260 | for (SmallVector<ClassTemplatePartialSpecializationDecl *, 10>::iterator |
| 261 | I = PartialDecls.begin(), E = PartialDecls.end(); I != E; ++I) { |
| 262 | const ClassTemplatePartialSpecializationDecl *PartialD = (*I); |
| 263 | removeTemplateAndParameter(PartialD->getSourceRange().getBegin(), |
| 264 | PartialD->getTemplateParameters()); |
| 265 | |
| 266 | const ASTTemplateArgumentListInfo *ArgList = |
| 267 | PartialD->getTemplateArgsAsWritten(); |
| 268 | TransAssert(ArgList && "Invalid ArgList!"); |
| 269 | |
| 270 | const TemplateArgumentLoc *ArgLocs = ArgList->getTemplateArgs(); |
| 271 | TransAssert(ArgLocs && "Invalid ArcLogs!"); |
| 272 | |
| 273 | unsigned NumArgs = ArgList->NumTemplateArgs; |
| 274 | TransAssert((NumArgs > 0) && "Invalid NumArgs!"); |
| 275 | |
| 276 | const TemplateArgumentLoc FirstArgLoc = ArgLocs[0]; |
| 277 | SourceLocation StartLoc = FirstArgLoc.getSourceRange().getBegin(); |
| 278 | |
| 279 | const TemplateArgumentLoc LastArgLoc = ArgLocs[NumArgs - 1]; |
| 280 | SourceRange LastRange = LastArgLoc.getSourceRange(); |
| 281 | SourceLocation EndLoc = RewriteHelper->getEndLocationUntil(LastRange, '>'); |
| 282 | |
| 283 | RewriteHelper->removeTextFromLeftAt(SourceRange(StartLoc, EndLoc), |
| 284 | '<', EndLoc); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | bool ClassTemplateToClass::isUsedNamedDecl(NamedDecl *ND, |
| 289 | Decl *D) |
| 290 | { |
| 291 | TemplateParameterTypeVisitor ParamVisitor(Context); |
| 292 | ParamVisitor.TraverseDecl(D); |
| 293 | |
| 294 | return ParamVisitor.isAUsedParameter(ND); |
| 295 | } |
| 296 | |
| 297 | bool ClassTemplateToClass::hasUsedNameDecl( |
| 298 | ClassTemplatePartialSpecializationDecl *PartialD) |
| 299 | { |
| 300 | if (!PartialD->isCompleteDefinition()) |
| 301 | return false; |
| 302 | |
| 303 | llvm::SmallPtrSet<NamedDecl *, 8> Params; |
| 304 | TemplateParameterList *PartialTPList = PartialD->getTemplateParameters(); |
| 305 | for (unsigned PI = 0; PI < PartialTPList->size(); ++PI) { |
| 306 | NamedDecl *ND = PartialTPList->getParam(PI); |
| 307 | if (dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 308 | continue; |
| 309 | Params.insert(ND); |
| 310 | } |
| 311 | |
| 312 | TemplateParameterTypeVisitor ParamVisitor(Context); |
| 313 | |
| 314 | // Skip visiting parameters and arguments |
| 315 | for (CXXRecordDecl::base_class_iterator I = PartialD->bases_begin(), |
| 316 | E = PartialD->bases_end(); I != E; ++I) { |
| 317 | ParamVisitor.TraverseType(I->getType()); |
| 318 | } |
| 319 | |
| 320 | DeclContext *Ctx = dyn_cast<DeclContext>(PartialD); |
| 321 | for (DeclContext::decl_iterator DI = Ctx->decls_begin(), |
| 322 | DE = Ctx->decls_end(); DI != DE; ++DI) { |
| 323 | ParamVisitor.TraverseDecl(*DI); |
| 324 | } |
| 325 | |
| 326 | for (llvm::SmallPtrSet<NamedDecl *, 8>::iterator I = Params.begin(), |
| 327 | E = Params.end(); I != E; ++I) { |
| 328 | if (ParamVisitor.isAUsedParameter(*I)) |
| 329 | return true; |
| 330 | } |
| 331 | return false; |
| 332 | } |
| 333 | |
| 334 | bool ClassTemplateToClass::isValidClassTemplateDecl(ClassTemplateDecl *TmplD) |
| 335 | { |
| 336 | TemplateParameterList *TPList = TmplD->getTemplateParameters(); |
| 337 | if (TPList->size() != 1) |
| 338 | return false; |
| 339 | |
| 340 | CXXRecordDecl *CXXRD = TmplD->getTemplatedDecl(); |
| 341 | CXXRecordDecl *Def = CXXRD->getDefinition(); |
| 342 | if (!Def) |
| 343 | return true; |
| 344 | |
| 345 | NamedDecl *ND = TPList->getParam(0); |
| 346 | if (dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 347 | return true; |
| 348 | |
| 349 | if (isUsedNamedDecl(ND, Def)) |
| 350 | return false; |
| 351 | |
| 352 | SmallVector<ClassTemplatePartialSpecializationDecl *, 10> PartialDecls; |
| 353 | TmplD->getPartialSpecializations(PartialDecls); |
| 354 | for (SmallVector<ClassTemplatePartialSpecializationDecl *, 10>::iterator |
| 355 | I = PartialDecls.begin(), E = PartialDecls.end(); I != E; ++I) { |
| 356 | if (hasUsedNameDecl(*I)) |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | bool ClassTemplateToClass::referToTheTemplateDecl(TemplateName TmplName) |
| 364 | { |
| 365 | return Context->hasSameTemplateName(*TheTemplateName, TmplName); |
| 366 | } |
| 367 | |
| 368 | ClassTemplateToClass::~ClassTemplateToClass(void) |
| 369 | { |
| 370 | delete TheTemplateName; |
| 371 | delete CollectionVisitor; |
| 372 | delete RewriteVisitor; |
| 373 | } |
| 374 |