| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2014, 2015, 2016, 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 "ReduceClassTemplateParameter.h" |
| 16 | |
| 17 | #include "llvm/ADT/DenseMap.h" |
| 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "clang/AST/RecursiveASTVisitor.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | |
| 23 | #include "TransformationManager.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | |
| 27 | static const char *DescriptionMsg = |
| 28 | "This pass tries to remove one unused parameter from a class template \ |
| 29 | declaration and also erase the corresponding template argument \ |
| 30 | from template instantiations/specializations. Note that this pass \ |
| 31 | does not target those templates with single argument, and skips \ |
| 32 | variadic templates as well. "; |
| 33 | |
| 34 | static RegisterTransformation<ReduceClassTemplateParameter> |
| 35 | Trans("reduce-class-template-param", DescriptionMsg); |
| 36 | |
| 37 | class ReduceClassTemplateParameterASTVisitor : public |
| 38 | RecursiveASTVisitor<ReduceClassTemplateParameterASTVisitor> { |
| 39 | |
| 40 | public: |
| 41 | explicit ReduceClassTemplateParameterASTVisitor( |
| 42 | ReduceClassTemplateParameter *Instance) |
| 43 | : ConsumerInstance(Instance) |
| 44 | { } |
| 45 | |
| 46 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
| 47 | |
| 48 | private: |
| 49 | ReduceClassTemplateParameter *ConsumerInstance; |
| 50 | |
| 51 | }; |
| 52 | |
| 53 | namespace { |
| 54 | |
| 55 | typedef llvm::SmallPtrSet<const NamedDecl *, 8> TemplateParameterSet; |
| 56 | |
| 57 | class TemplateParameterVisitor : public |
| 58 | RecursiveASTVisitor<TemplateParameterVisitor> { |
| 59 | |
| 60 | public: |
| 61 | explicit TemplateParameterVisitor(TemplateParameterSet &Params) |
| 62 | : UsedParameters(Params) |
| 63 | { } |
| 64 | |
| 65 | ~TemplateParameterVisitor() { }; |
| 66 | |
| 67 | bool VisitTemplateTypeParmType(TemplateTypeParmType *Ty); |
| 68 | |
| 69 | private: |
| 70 | |
| 71 | TemplateParameterSet &UsedParameters; |
| 72 | }; |
| 73 | |
| 74 | bool TemplateParameterVisitor::VisitTemplateTypeParmType( |
| 75 | TemplateTypeParmType *Ty) |
| 76 | { |
| 77 | const TemplateTypeParmDecl *D = Ty->getDecl(); |
| 78 | UsedParameters.insert(D); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | class ArgumentDependencyVisitor : public |
| 83 | RecursiveASTVisitor<ArgumentDependencyVisitor> { |
| 84 | |
| 85 | public: |
| 86 | typedef llvm::DenseMap<const Type *, unsigned> TypeToVisitsCountSet; |
| 87 | |
| 88 | explicit ArgumentDependencyVisitor(TypeToVisitsCountSet &CounterSet) |
| 89 | : VisitsCountSet(CounterSet) |
| 90 | { } |
| 91 | |
| 92 | bool VisitTemplateTypeParmType(TemplateTypeParmType *Ty); |
| 93 | |
| 94 | private: |
| 95 | |
| 96 | TypeToVisitsCountSet &VisitsCountSet; |
| 97 | }; |
| 98 | |
| 99 | bool ArgumentDependencyVisitor::VisitTemplateTypeParmType( |
| 100 | TemplateTypeParmType *Ty) |
| 101 | { |
| 102 | TypeToVisitsCountSet::iterator I = VisitsCountSet.find(Ty); |
| 103 | if (I != VisitsCountSet.end()) { |
| 104 | unsigned Count = (*I).second + 1; |
| 105 | VisitsCountSet[(*I).first] = Count; |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | class ClassTemplateMethodVisitor : public |
| 111 | RecursiveASTVisitor<ClassTemplateMethodVisitor> { |
| 112 | |
| 113 | public: |
| 114 | ClassTemplateMethodVisitor(ReduceClassTemplateParameter *Instance, |
| 115 | unsigned Idx) |
| 116 | : ConsumerInstance(Instance), TheParameterIndex(Idx) |
| 117 | { } |
| 118 | |
| 119 | bool VisitFunctionDecl(FunctionDecl *FD); |
| 120 | |
| 121 | private: |
| 122 | ReduceClassTemplateParameter *ConsumerInstance; |
| 123 | |
| 124 | unsigned TheParameterIndex; |
| 125 | }; |
| 126 | |
| 127 | bool ClassTemplateMethodVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 128 | { |
| 129 | FunctionTemplateDecl *TD = FD->getDescribedFunctionTemplate(); |
| 130 | for (FunctionDecl::redecl_iterator I = FD->redecls_begin(), |
| 131 | E = FD->redecls_end(); I != E; ++I) { |
| 132 | unsigned Num = (*I)->getNumTemplateParameterLists(); |
| 133 | for (unsigned Idx = 0; Idx < Num; ++Idx) { |
| 134 | const TemplateParameterList *TPList = (*I)->getTemplateParameterList(Idx); |
| 135 | // We don't want to mistakenly rewrite template parameters associated |
| 136 | // with the FD if FD is a function template. |
| 137 | if (TD && TPList == TD->getTemplateParameters()) |
| 138 | continue; |
| 139 | const NamedDecl *Param = TPList->getParam(TheParameterIndex); |
| 140 | SourceRange Range = Param->getSourceRange(); |
| 141 | ConsumerInstance->removeParameterByRange(Range, TPList, |
| 142 | TheParameterIndex); |
| 143 | } |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | class ReduceClassTemplateParameterRewriteVisitor : public |
| 151 | RecursiveASTVisitor<ReduceClassTemplateParameterRewriteVisitor> { |
| 152 | |
| 153 | public: |
| 154 | explicit ReduceClassTemplateParameterRewriteVisitor( |
| 155 | ReduceClassTemplateParameter *Instance) |
| 156 | : ConsumerInstance(Instance) |
| 157 | { } |
| 158 | |
| 159 | bool VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc Loc); |
| 160 | |
| 161 | private: |
| 162 | |
| 163 | ReduceClassTemplateParameter *ConsumerInstance; |
| 164 | }; |
| 165 | |
| 166 | bool ReduceClassTemplateParameterRewriteVisitor:: |
| 167 | VisitTemplateSpecializationTypeLoc(TemplateSpecializationTypeLoc Loc) |
| 168 | { |
| 169 | // Invalidation can be introduced by constructor's initialization list, e.g.: |
| 170 | // template<typename T1, typename T2> class A { }; |
| 171 | // class B : public A<int, int> { |
| 172 | // int m; |
| 173 | // B(int x) : m(x) {} |
| 174 | // }; |
| 175 | // In RecursiveASTVisitor.h, TraverseConstructorInitializer will visit the part |
| 176 | // of initializing base class's, i.e. through base's default constructor |
| 177 | if (Loc.getBeginLoc().isInvalid()) |
| 178 | return true; |
| 179 | const TemplateSpecializationType *Ty = |
| 180 | dyn_cast<TemplateSpecializationType>(Loc.getTypePtr()); |
| 181 | TransAssert(Ty && "Invalid TemplateSpecializationType!"); |
| 182 | |
| 183 | TemplateName TmplName = Ty->getTemplateName(); |
| 184 | if (!ConsumerInstance->referToTheTemplateDecl(TmplName)) |
| 185 | return true; |
| 186 | |
| 187 | unsigned NumArgs = Loc.getNumArgs(); |
| 188 | // I would put a stronger assert here, i.e., |
| 189 | // " (ConsumerInstance->TheParameterIndex >= NumArgs) && |
| 190 | // ConsumerInstance->hasDefaultArg " |
| 191 | // but sometimes ill-formed input could yield incomplete |
| 192 | // info, e.g., for two template decls which refer to the same |
| 193 | // template def, one decl could have a non-null default arg, |
| 194 | // while another decl's default arg field could be null. |
| 195 | if (ConsumerInstance->TheParameterIndex >= NumArgs) |
| 196 | return true; |
| 197 | |
| 198 | TransAssert((ConsumerInstance->TheParameterIndex < NumArgs) && |
| 199 | "TheParameterIndex cannot be greater than NumArgs!"); |
| 200 | TemplateArgumentLoc ArgLoc = Loc.getArgLoc(ConsumerInstance->TheParameterIndex); |
| 201 | SourceRange Range = ArgLoc.getSourceRange(); |
| 202 | |
| 203 | if (NumArgs == 1) { |
| 204 | ConsumerInstance->TheRewriter.ReplaceText(SourceRange(Loc.getLAngleLoc(), |
| 205 | Loc.getRAngleLoc()), |
| 206 | "<>"); |
| 207 | } |
| 208 | else if ((ConsumerInstance->TheParameterIndex + 1) == NumArgs) { |
| 209 | SourceLocation EndLoc = Loc.getRAngleLoc(); |
| 210 | EndLoc = EndLoc.getLocWithOffset(-1); |
| 211 | ConsumerInstance->RewriteHelper->removeTextFromLeftAt( |
| 212 | Range, ',', EndLoc); |
| 213 | } |
| 214 | else { |
| 215 | ConsumerInstance->RewriteHelper->removeTextUntil(Range, ','); |
| 216 | } |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | bool ReduceClassTemplateParameterASTVisitor::VisitClassTemplateDecl( |
| 221 | ClassTemplateDecl *D) |
| 222 | { |
| 223 | if (ConsumerInstance->isInIncludedFile(D)) |
| 224 | return true; |
| 225 | |
| 226 | ClassTemplateDecl *CanonicalD = D->getCanonicalDecl(); |
| 227 | if (ConsumerInstance->VisitedDecls.count(CanonicalD)) |
| 228 | return true; |
| 229 | |
| 230 | ConsumerInstance->VisitedDecls.insert(CanonicalD); |
| 231 | if (!ConsumerInstance->isValidClassTemplateDecl(D)) |
| 232 | return true; |
| 233 | |
| 234 | TemplateParameterSet ParamsSet; |
| 235 | TemplateParameterVisitor ParameterVisitor(ParamsSet); |
| 236 | CXXRecordDecl *CXXRD = D->getTemplatedDecl(); |
| 237 | CXXRecordDecl *Def = CXXRD->getDefinition(); |
| 238 | if (Def) |
| 239 | ParameterVisitor.TraverseDecl(Def); |
| 240 | |
| 241 | // ISSUE: we should also check the parameter usage for partial template |
| 242 | // specializations. For example: |
| 243 | // template<typename T1, typename T2> struct S{}; |
| 244 | // template<typename T1, typename T2> struct<T1 *, T2 *> S{...}; |
| 245 | // T1 or T2 could be used in "..." |
| 246 | // Also, we could have another bad transformation, for example, |
| 247 | // template<bool, typename T> struct S{}; |
| 248 | // template<typename T> struct<true, T> S{}; |
| 249 | // if we remove bool and true, we will have two definitions for S |
| 250 | TemplateParameterList *TPList; |
| 251 | if (Def) { |
| 252 | // make sure we use the params as in ParameterVisitor |
| 253 | const ClassTemplateDecl *CT = Def->getDescribedClassTemplate(); |
| 254 | TransAssert(CT && "NULL DescribedClassTemplate!"); |
| 255 | TPList = CT->getTemplateParameters(); |
| 256 | } |
| 257 | else { |
| 258 | TPList = CanonicalD->getTemplateParameters(); |
| 259 | } |
| 260 | |
| 261 | unsigned Index = 0; |
| 262 | for (TemplateParameterList::const_iterator I = TPList->begin(), |
| 263 | E = TPList->end(); I != E; ++I) { |
| 264 | const NamedDecl *ND = (*I); |
| 265 | if (ParamsSet.count(ND)) { |
| 266 | Index++; |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | ConsumerInstance->ValidInstanceNum++; |
| 271 | if (ConsumerInstance->ValidInstanceNum == |
| 272 | ConsumerInstance->TransformationCounter) { |
| 273 | ConsumerInstance->TheClassTemplateDecl = CanonicalD; |
| 274 | ConsumerInstance->TheParameterIndex = Index; |
| 275 | ConsumerInstance->TheTemplateName = new TemplateName(CanonicalD); |
| 276 | ConsumerInstance->setDefaultArgFlag(ND); |
| 277 | } |
| 278 | Index++; |
| 279 | } |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | void ReduceClassTemplateParameter::Initialize(ASTContext &context) |
| 285 | { |
| 286 | Transformation::Initialize(context); |
| 287 | CollectionVisitor = new ReduceClassTemplateParameterASTVisitor(this); |
| 288 | ArgRewriteVisitor = new ReduceClassTemplateParameterRewriteVisitor(this); |
| 289 | } |
| 290 | |
| 291 | void ReduceClassTemplateParameter::HandleTranslationUnit(ASTContext &Ctx) |
| 292 | { |
| 293 | if (TransformationManager::isCLangOpt() || |
| 294 | TransformationManager::isOpenCLLangOpt()) { |
| 295 | ValidInstanceNum = 0; |
| 296 | } |
| 297 | else { |
| 298 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 299 | } |
| 300 | |
| 301 | if (QueryInstanceOnly) |
| 302 | return; |
| 303 | |
| 304 | if (TransformationCounter > ValidInstanceNum) { |
| 305 | TransError = TransMaxInstanceError; |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | TransAssert(TheClassTemplateDecl && "NULL TheClassTemplateDecl!"); |
| 310 | TransAssert(ArgRewriteVisitor && "NULL ArgRewriteVisitor!"); |
| 311 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 312 | |
| 313 | removeParameterFromDecl(); |
| 314 | removeParameterFromMethods(); |
| 315 | removeParameterFromPartialSpecs(); |
| 316 | ArgRewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 317 | |
| 318 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 319 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 320 | TransError = TransInternalError; |
| 321 | } |
| 322 | |
| 323 | void ReduceClassTemplateParameter::removeParameterByRange(SourceRange Range, |
| 324 | const TemplateParameterList *TPList, |
| 325 | unsigned Index) |
| 326 | { |
| 327 | SourceLocation LocStart = SrcManager->getSpellingLoc(Range.getBegin()); |
| 328 | SourceLocation LocEnd = SrcManager->getSpellingLoc(Range.getEnd()); |
| 329 | |
| 330 | SourceRange NewRange(LocStart, LocEnd); |
| 331 | unsigned NumParams = TPList->size(); |
| 332 | |
| 333 | SourceLocation LAngleLoc = |
| 334 | SrcManager->getSpellingLoc(TPList->getLAngleLoc()); |
| 335 | SourceLocation RAngleLoc = |
| 336 | SrcManager->getSpellingLoc(TPList->getRAngleLoc()); |
| 337 | // if the parameter is the last one |
| 338 | if (NumParams == 1) { |
| 339 | TheRewriter.ReplaceText(SourceRange(LAngleLoc, RAngleLoc), "<>"); |
| 340 | } |
| 341 | else if ((Index + 1) == NumParams) { |
| 342 | SourceLocation EndLoc = RAngleLoc; |
| 343 | EndLoc = EndLoc.getLocWithOffset(-1); |
| 344 | RewriteHelper->removeTextFromLeftAt(NewRange, ',', EndLoc); |
| 345 | } |
| 346 | else { |
| 347 | RewriteHelper->removeTextUntil(NewRange, ','); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void ReduceClassTemplateParameter::removeParameterFromDecl() |
| 352 | { |
| 353 | unsigned NumParams = TheClassTemplateDecl->getTemplateParameters()->size(); |
| 354 | |
| 355 | TransAssert((NumParams > 1) && "Bad size of TheClassTemplateDecl!"); |
| 356 | (void)NumParams; |
| 357 | |
| 358 | for (ClassTemplateDecl::redecl_iterator |
| 359 | I = TheClassTemplateDecl->redecls_begin(), |
| 360 | E = TheClassTemplateDecl->redecls_end(); |
| 361 | I != E; ++I) { |
| 362 | const TemplateParameterList *TPList = (*I)->getTemplateParameters(); |
| 363 | const NamedDecl *Param = TPList->getParam(TheParameterIndex); |
| 364 | SourceRange Range = Param->getSourceRange(); |
| 365 | removeParameterByRange(Range, TPList, TheParameterIndex); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | void ReduceClassTemplateParameter::removeParameterFromMethods() |
| 370 | { |
| 371 | CXXRecordDecl *CXXRD = TheClassTemplateDecl->getTemplatedDecl(); |
| 372 | for (auto I = CXXRD->method_begin(), E = CXXRD->method_end(); |
| 373 | I != E; ++I) { |
| 374 | ClassTemplateMethodVisitor V(this, TheParameterIndex); |
| 375 | V.TraverseDecl(*I); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | void ReduceClassTemplateParameter::removeOneParameterByArgExpression( |
| 380 | const ClassTemplatePartialSpecializationDecl *PartialD, |
| 381 | const TemplateArgument &Arg) |
| 382 | { |
| 383 | TransAssert((Arg.getKind() == TemplateArgument::Expression) && |
| 384 | "Arg is not TemplateArgument::Expression!"); |
| 385 | |
| 386 | const Expr *E = Arg.getAsExpr(); |
| 387 | TransAssert(E && "Bad Expression!"); |
| 388 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 389 | TransAssert(DRE && "Bad DeclRefExpr!"); |
| 390 | const NonTypeTemplateParmDecl *ParmD = |
| 391 | dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 392 | TransAssert(ParmD && "Invalid NonTypeTemplateParmDecl!"); |
| 393 | |
| 394 | const TemplateParameterList *TPList = PartialD->getTemplateParameters(); |
| 395 | unsigned Idx = 0; |
| 396 | for (TemplateParameterList::const_iterator I = TPList->begin(), |
| 397 | E = TPList->end(); I != E; ++I) { |
| 398 | if ((*I) == ParmD) |
| 399 | break; |
| 400 | Idx++; |
| 401 | } |
| 402 | |
| 403 | unsigned NumParams = TPList->size(); |
| 404 | TransAssert((Idx < NumParams) && "Cannot find valid TemplateParameter!"); |
| 405 | (void)NumParams; |
| 406 | SourceRange Range = ParmD->getSourceRange(); |
| 407 | removeParameterByRange(Range, TPList, Idx); |
| 408 | } |
| 409 | |
| 410 | void ReduceClassTemplateParameter::removeOneParameterByArgType( |
| 411 | const ClassTemplatePartialSpecializationDecl *PartialD, |
| 412 | const TemplateArgument &Arg) |
| 413 | { |
| 414 | TransAssert((Arg.getKind() == TemplateArgument::Type) && |
| 415 | "Arg is not TemplateArgument::Type!"); |
| 416 | llvm::DenseMap<const Type *, unsigned> TypeToVisitsCount; |
| 417 | llvm::DenseMap<const Type *, const NamedDecl *> TypeToNamedDecl; |
| 418 | llvm::DenseMap<const Type *, unsigned> TypeToIndex; |
| 419 | |
| 420 | // retrieve all TemplateTypeParmType |
| 421 | const TemplateParameterList *TPList = PartialD->getTemplateParameters(); |
| 422 | unsigned Idx = 0; |
| 423 | for (TemplateParameterList::const_iterator I = TPList->begin(), |
| 424 | E = TPList->end(); I != E; ++I) { |
| 425 | const NamedDecl *ND = (*I); |
| 426 | const TemplateTypeParmDecl *TypeD = dyn_cast<TemplateTypeParmDecl>(ND); |
| 427 | if (!TypeD) { |
| 428 | Idx++; |
| 429 | continue; |
| 430 | } |
| 431 | const Type *ParmTy = TypeD->getTypeForDecl(); |
| 432 | TypeToVisitsCount[ParmTy] = 0; |
| 433 | TypeToNamedDecl[ParmTy] = ND; |
| 434 | TypeToIndex[ParmTy] = Idx; |
| 435 | } |
| 436 | |
| 437 | QualType QTy = Arg.getAsType(); |
| 438 | ArgumentDependencyVisitor V(TypeToVisitsCount); |
| 439 | // collect TemplateTypeParmType being used by Arg |
| 440 | V.TraverseType(QTy); |
| 441 | |
| 442 | llvm::DenseMap<const Type *, unsigned> DependentTypeToVisitsCount; |
| 443 | for (llvm::DenseMap<const Type *, unsigned>::iterator |
| 444 | I = TypeToVisitsCount.begin(), E = TypeToVisitsCount.end(); |
| 445 | I != E; ++I) { |
| 446 | if ((*I).second > 0) |
| 447 | DependentTypeToVisitsCount[(*I).first] = 1; |
| 448 | } |
| 449 | |
| 450 | // check if the used TemplateTypeParmType[s] have dependencies |
| 451 | // on other Args. If yes, we cannot remove it from the parameter list. |
| 452 | // For example: |
| 453 | // template <typename T> |
| 454 | // struct S <T*, T&> {}; |
| 455 | // removing either of the arguments needs to keep the template |
| 456 | // parameter |
| 457 | ArgumentDependencyVisitor AccumV(DependentTypeToVisitsCount); |
| 458 | |
| 459 | const ASTTemplateArgumentListInfo *ArgList = |
| 460 | PartialD->getTemplateArgsAsWritten(); |
| 461 | |
| 462 | const TemplateArgumentLoc *ArgLocs = ArgList->getTemplateArgs(); |
| 463 | unsigned NumArgs = ArgList->NumTemplateArgs; |
| 464 | TransAssert((TheParameterIndex < NumArgs) && |
| 465 | "Bad NumArgs from partial template decl!"); |
| 466 | for (unsigned I = 0; I < NumArgs; ++I) { |
| 467 | if (I == TheParameterIndex) |
| 468 | continue; |
| 469 | |
| 470 | const TemplateArgumentLoc ArgLoc = ArgLocs[I]; |
| 471 | TemplateArgument OtherArg = ArgLoc.getArgument(); |
| 472 | if (OtherArg.isInstantiationDependent() && |
| 473 | (OtherArg.getKind() == TemplateArgument::Type)) { |
| 474 | QualType QTy = OtherArg.getAsType(); |
| 475 | AccumV.TraverseType(QTy); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | for (llvm::DenseMap<const Type *, unsigned>::iterator |
| 480 | I = DependentTypeToVisitsCount.begin(), |
| 481 | E = DependentTypeToVisitsCount.end(); |
| 482 | I != E; ++I) { |
| 483 | if ((*I).second != 1) |
| 484 | continue; |
| 485 | |
| 486 | const NamedDecl *Param = TypeToNamedDecl[(*I).first]; |
| 487 | TransAssert(Param && "NULL Parameter!"); |
| 488 | SourceRange Range = Param->getSourceRange(); |
| 489 | removeParameterByRange(Range, TPList, TypeToIndex[(*I).first]); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void ReduceClassTemplateParameter::removeOneParameterByArgTemplate( |
| 494 | const ClassTemplatePartialSpecializationDecl *PartialD, |
| 495 | const TemplateArgument &Arg) |
| 496 | { |
| 497 | TransAssert((Arg.getKind() == TemplateArgument::Template) && |
| 498 | "Arg is not TemplateArgument::Template!"); |
| 499 | TemplateName TmplName = Arg.getAsTemplate(); |
| 500 | TransAssert((TmplName.getKind() == TemplateName::Template) && |
| 501 | "Invalid TemplateName Kind!"); |
| 502 | const TemplateDecl *TmplD = TmplName.getAsTemplateDecl(); |
| 503 | |
| 504 | const TemplateParameterList *TPList = PartialD->getTemplateParameters(); |
| 505 | unsigned Idx = 0; |
| 506 | for (TemplateParameterList::const_iterator I = TPList->begin(), |
| 507 | E = TPList->end(); I != E; ++I) { |
| 508 | if ((*I) == TmplD) |
| 509 | break; |
| 510 | Idx++; |
| 511 | } |
| 512 | |
| 513 | unsigned NumParams = TPList->size(); |
| 514 | TransAssert((Idx < NumParams) && "Cannot find valid TemplateParameter!"); |
| 515 | (void)NumParams; |
| 516 | SourceRange Range = TmplD->getSourceRange(); |
| 517 | removeParameterByRange(Range, TPList, Idx); |
| 518 | |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | void ReduceClassTemplateParameter::removeOneParameterFromPartialDecl( |
| 523 | const ClassTemplatePartialSpecializationDecl *PartialD, |
| 524 | const TemplateArgument &Arg) |
| 525 | { |
| 526 | if (!Arg.isInstantiationDependent()) |
| 527 | return; |
| 528 | |
| 529 | TemplateArgument::ArgKind K = Arg.getKind(); |
| 530 | switch (K) { |
| 531 | case TemplateArgument::Expression: |
| 532 | removeOneParameterByArgExpression(PartialD, Arg); |
| 533 | return; |
| 534 | |
| 535 | case TemplateArgument::Template: |
| 536 | removeOneParameterByArgTemplate(PartialD, Arg); |
| 537 | return; |
| 538 | |
| 539 | case TemplateArgument::Type: |
| 540 | removeOneParameterByArgType(PartialD, Arg); |
| 541 | return; |
| 542 | |
| 543 | default: |
| 544 | TransAssert(0 && "Uncatched ArgKind!"); |
| 545 | } |
| 546 | TransAssert(0 && "Unreachable code!"); |
| 547 | } |
| 548 | |
| 549 | const NamedDecl *ReduceClassTemplateParameter::getNamedDecl( |
| 550 | const TemplateArgument &Arg) |
| 551 | { |
| 552 | if (!Arg.isInstantiationDependent()) |
| 553 | return NULL; |
| 554 | |
| 555 | TemplateArgument::ArgKind K = Arg.getKind(); |
| 556 | switch (K) { |
| 557 | case TemplateArgument::Expression: { |
| 558 | const Expr *E = Arg.getAsExpr(); |
| 559 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 560 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 561 | } |
| 562 | else { |
| 563 | return NULL; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | case TemplateArgument::Template: { |
| 568 | TemplateName TmplName = Arg.getAsTemplate(); |
| 569 | TransAssert((TmplName.getKind() == TemplateName::Template) && |
| 570 | "Invalid TemplateName Kind!"); |
| 571 | return TmplName.getAsTemplateDecl(); |
| 572 | } |
| 573 | |
| 574 | case TemplateArgument::Type: { |
| 575 | const Type *Ty = Arg.getAsType().getTypePtr(); |
| 576 | if (const TemplateTypeParmType *TmplTy = |
| 577 | dyn_cast<TemplateTypeParmType>(Ty)) { |
| 578 | return TmplTy->getDecl(); |
| 579 | } |
| 580 | else { |
| 581 | return NULL; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | default: |
| 586 | return NULL; |
| 587 | } |
| 588 | |
| 589 | TransAssert(0 && "Unreachable code!"); |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | bool ReduceClassTemplateParameter::referToAParameter( |
| 594 | const ClassTemplatePartialSpecializationDecl *PartialD, |
| 595 | const TemplateArgument &Arg) |
| 596 | { |
| 597 | const NamedDecl *ArgND = getNamedDecl(Arg); |
| 598 | if (!ArgND) |
| 599 | return false; |
| 600 | |
| 601 | const TemplateParameterList *TPList = PartialD->getTemplateParameters(); |
| 602 | for (TemplateParameterList::const_iterator PI = TPList->begin(), |
| 603 | PE = TPList->end(); PI != PE; ++PI) { |
| 604 | if (ArgND != (*PI)) |
| 605 | return false; |
| 606 | } |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | bool ReduceClassTemplateParameter::isValidForReduction( |
| 611 | const ClassTemplatePartialSpecializationDecl *PartialD) |
| 612 | { |
| 613 | const ASTTemplateArgumentListInfo *ArgList = |
| 614 | PartialD->getTemplateArgsAsWritten(); |
| 615 | |
| 616 | unsigned NumArgsAsWritten = ArgList->NumTemplateArgs; |
| 617 | unsigned NumArgs = PartialD->getTemplateInstantiationArgs().size(); |
| 618 | |
| 619 | if ((NumArgsAsWritten > 0) && |
| 620 | (TheParameterIndex >= NumArgsAsWritten) && |
| 621 | hasDefaultArg && |
| 622 | ((NumArgsAsWritten + 1) == NumArgs)) { |
| 623 | |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | if (NumArgsAsWritten != NumArgs) |
| 628 | return false; |
| 629 | |
| 630 | const TemplateArgumentLoc *ArgLocs = ArgList->getTemplateArgs(); |
| 631 | for (unsigned AI = 0; AI < NumArgsAsWritten; ++AI) { |
| 632 | if (AI == TheParameterIndex) |
| 633 | continue; |
| 634 | const TemplateArgumentLoc ArgLoc = ArgLocs[AI]; |
| 635 | TemplateArgument Arg = ArgLoc.getArgument(); |
| 636 | if (!referToAParameter(PartialD, Arg)) |
| 637 | return false; |
| 638 | } |
| 639 | |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | bool ReduceClassTemplateParameter::reducePartialSpec( |
| 644 | const ClassTemplatePartialSpecializationDecl *PartialD) |
| 645 | { |
| 646 | const CXXRecordDecl *CXXRD = TheClassTemplateDecl->getTemplatedDecl(); |
| 647 | // it CXXRD has definition, skip it to avoid duplication |
| 648 | if (CXXRD->hasDefinition()) |
| 649 | return false; |
| 650 | |
| 651 | if (!isValidForReduction(PartialD)) |
| 652 | return false; |
| 653 | |
| 654 | const ASTTemplateArgumentListInfo *ArgList = |
| 655 | PartialD->getTemplateArgsAsWritten(); |
| 656 | const TemplateArgumentLoc *ArgLocs = ArgList->getTemplateArgs(); |
| 657 | unsigned NumArgsAsWritten = ArgList->NumTemplateArgs; |
| 658 | |
| 659 | const TemplateArgumentLoc FirstArgLoc = ArgLocs[0]; |
| 660 | SourceRange FirstRange = FirstArgLoc.getSourceRange(); |
| 661 | SourceLocation StartLoc = FirstRange.getBegin(); |
| 662 | |
| 663 | const TemplateArgumentLoc LastArgLoc = ArgLocs[NumArgsAsWritten - 1]; |
| 664 | SourceRange LastRange = LastArgLoc.getSourceRange(); |
| 665 | SourceLocation EndLoc = |
| 666 | RewriteHelper->getEndLocationUntil(LastRange, '>'); |
| 667 | |
| 668 | RewriteHelper->removeTextFromLeftAt(SourceRange(StartLoc, EndLoc), '<', EndLoc); |
| 669 | return true; |
| 670 | } |
| 671 | |
| 672 | // ISSUE: The transformation is known to go wrong in the following case: |
| 673 | // template<typename T1, typename T2> struct S; |
| 674 | // template<typename T1, typename T2> struct S<T2, T1>; |
| 675 | void ReduceClassTemplateParameter::removeParameterFromPartialSpecs() |
| 676 | { |
| 677 | SmallVector<ClassTemplatePartialSpecializationDecl *, 10> PartialDecls; |
| 678 | TheClassTemplateDecl->getPartialSpecializations(PartialDecls); |
| 679 | |
| 680 | for (SmallVector<ClassTemplatePartialSpecializationDecl *, 10>::iterator |
| 681 | I = PartialDecls.begin(), E = PartialDecls.end(); I != E; ++I) { |
| 682 | const ClassTemplatePartialSpecializationDecl *PartialD = (*I); |
| 683 | |
| 684 | const ASTTemplateArgumentListInfo *ArgList = |
| 685 | PartialD->getTemplateArgsAsWritten(); |
| 686 | const TemplateArgumentLoc *ArgLocs = ArgList->getTemplateArgs(); |
| 687 | unsigned NumArgs = ArgList->NumTemplateArgs; |
| 688 | |
| 689 | if (!ArgLocs) |
| 690 | continue; |
| 691 | |
| 692 | // handle a special case where we could reduce a partial specialization |
| 693 | // to a class template definition, e.g.: |
| 694 | // template<typename T1, typename T2> struct A; |
| 695 | // template<typename T1> struct A<T1, int> { }; |
| 696 | // ==> |
| 697 | // template<typename T1> struct A; |
| 698 | // template<typename T1> struct A { }; |
| 699 | if (reducePartialSpec(PartialD)) |
| 700 | continue; |
| 701 | |
| 702 | if ((TheParameterIndex >= NumArgs) && hasDefaultArg) |
| 703 | return; |
| 704 | |
| 705 | TransAssert((TheParameterIndex < NumArgs) && |
| 706 | "Bad NumArgs from partial template decl!"); |
| 707 | TemplateArgumentLoc ArgLoc = ArgLocs[TheParameterIndex]; |
| 708 | |
| 709 | TemplateArgument Arg = ArgLoc.getArgument(); |
| 710 | removeOneParameterFromPartialDecl(PartialD, Arg); |
| 711 | |
| 712 | SourceRange Range = ArgLoc.getSourceRange(); |
| 713 | |
| 714 | if (NumArgs == 1) { |
| 715 | SourceLocation StartLoc = Range.getBegin(); |
| 716 | SourceLocation EndLoc = |
| 717 | RewriteHelper->getEndLocationUntil(Range, '>'); |
| 718 | EndLoc = EndLoc.getLocWithOffset(-1); |
| 719 | TheRewriter.RemoveText(SourceRange(StartLoc, EndLoc)); |
| 720 | } |
| 721 | else if ((TheParameterIndex + 1) == NumArgs) { |
| 722 | // Seems there is no getRAngleLoc() utility for |
| 723 | // template arguments from a partial specialization |
| 724 | SourceLocation EndLoc = |
| 725 | RewriteHelper->getEndLocationUntil(Range, '>'); |
| 726 | EndLoc = EndLoc.getLocWithOffset(-1); |
| 727 | RewriteHelper->removeTextFromLeftAt(Range, ',', EndLoc); |
| 728 | } |
| 729 | else { |
| 730 | RewriteHelper->removeTextUntil(Range, ','); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | bool ReduceClassTemplateParameter::isValidClassTemplateDecl( |
| 736 | const ClassTemplateDecl *D) |
| 737 | { |
| 738 | const TemplateParameterList *TPList = D->getTemplateParameters(); |
| 739 | if (TPList->size() <= 1) |
| 740 | return false; |
| 741 | |
| 742 | // FIXME: need to handle parameter pack later |
| 743 | for (TemplateParameterList::const_iterator I = TPList->begin(), |
| 744 | E = TPList->end(); I != E; ++I) { |
| 745 | if (isParameterPack(*I)) |
| 746 | return false; |
| 747 | } |
| 748 | return true; |
| 749 | } |
| 750 | |
| 751 | void ReduceClassTemplateParameter::setDefaultArgFlag(const NamedDecl *ND) |
| 752 | { |
| 753 | if (const NonTypeTemplateParmDecl *D = |
| 754 | dyn_cast<NonTypeTemplateParmDecl>(ND)) { |
| 755 | hasDefaultArg = D->hasDefaultArgument(); |
| 756 | } |
| 757 | else if (const TemplateTypeParmDecl *D = |
| 758 | dyn_cast<TemplateTypeParmDecl>(ND)) { |
| 759 | hasDefaultArg = D->hasDefaultArgument(); |
| 760 | } |
| 761 | else if (const TemplateTemplateParmDecl *D = |
| 762 | dyn_cast<TemplateTemplateParmDecl>(ND)) { |
| 763 | hasDefaultArg = D->hasDefaultArgument(); |
| 764 | } |
| 765 | else { |
| 766 | TransAssert(0 && "Unknown template parameter type!"); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | bool ReduceClassTemplateParameter::referToTheTemplateDecl( |
| 771 | TemplateName TmplName) |
| 772 | { |
| 773 | return Context->hasSameTemplateName(*TheTemplateName, TmplName); |
| 774 | } |
| 775 | |
| 776 | ReduceClassTemplateParameter::~ReduceClassTemplateParameter() |
| 777 | { |
| 778 | delete TheTemplateName; |
| 779 | delete CollectionVisitor; |
| 780 | delete ArgRewriteVisitor; |
| 781 | } |
| 782 |