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