| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 2015, 2017, 2019, 2020 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 "InstantiateTemplateParam.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 | "This pass tries to instantiate a template parameter with \ |
| 27 | its actual argument if this parameter has been instantiated \n\ |
| 28 | only once. \n"; |
| 29 | |
| 30 | static RegisterTransformation<InstantiateTemplateParam> |
| 31 | Trans("instantiate-template-param", DescriptionMsg); |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | typedef llvm::SmallPtrSet<const NamedDecl *, 8> TemplateParameterSet; |
| 36 | |
| 37 | class TemplateParameterVisitor : public |
| 38 | RecursiveASTVisitor<TemplateParameterVisitor> { |
| 39 | |
| 40 | public: |
| 41 | explicit TemplateParameterVisitor(TemplateParameterSet &Params) |
| 42 | : UsedParameters(Params) |
| 43 | { } |
| 44 | |
| 45 | ~TemplateParameterVisitor() { }; |
| 46 | |
| 47 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc); |
| 48 | |
| 49 | private: |
| 50 | |
| 51 | TemplateParameterSet &UsedParameters; |
| 52 | }; |
| 53 | |
| 54 | // seems clang can't detect the T in T::* in the following case: |
| 55 | // struct B; |
| 56 | // template <typename T> struct C { |
| 57 | // C(void (T::*)()) { } |
| 58 | // }; |
| 59 | // struct D { C<B> m; }; |
| 60 | bool TemplateParameterVisitor::VisitTemplateTypeParmTypeLoc( |
| 61 | TemplateTypeParmTypeLoc Loc) |
| 62 | { |
| 63 | const TemplateTypeParmDecl *D = Loc.getDecl(); |
| 64 | UsedParameters.insert(D); |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | } // end anonymous namespace |
| 69 | |
| 70 | class InstantiateTemplateParamASTVisitor : public |
| 71 | RecursiveASTVisitor<InstantiateTemplateParamASTVisitor> { |
| 72 | |
| 73 | public: |
| 74 | explicit InstantiateTemplateParamASTVisitor( |
| 75 | InstantiateTemplateParam *Instance) |
| 76 | : ConsumerInstance(Instance) |
| 77 | { } |
| 78 | |
| 79 | bool VisitRecordDecl(RecordDecl *D); |
| 80 | |
| 81 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
| 82 | |
| 83 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
| 84 | |
| 85 | private: |
| 86 | InstantiateTemplateParam *ConsumerInstance; |
| 87 | |
| 88 | }; |
| 89 | |
| 90 | bool InstantiateTemplateParamASTVisitor::VisitRecordDecl(RecordDecl *D) |
| 91 | { |
| 92 | ConsumerInstance->AvailableRecordDecls.insert( |
| 93 | dyn_cast<RecordDecl>(D->getCanonicalDecl())); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | bool InstantiateTemplateParamASTVisitor::VisitClassTemplateDecl( |
| 98 | ClassTemplateDecl *D) |
| 99 | { |
| 100 | if (D->isThisDeclarationADefinition()) |
| 101 | ConsumerInstance->handleOneClassTemplateDecl(D); |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool InstantiateTemplateParamASTVisitor::VisitFunctionTemplateDecl( |
| 106 | FunctionTemplateDecl *D) |
| 107 | { |
| 108 | if (D->isThisDeclarationADefinition()) |
| 109 | ConsumerInstance->handleOneFunctionTemplateDecl(D); |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | class InstantiateTemplateParamRewriteVisitor : public |
| 114 | RecursiveASTVisitor<InstantiateTemplateParamRewriteVisitor> { |
| 115 | |
| 116 | public: |
| 117 | explicit InstantiateTemplateParamRewriteVisitor( |
| 118 | InstantiateTemplateParam *Instance) |
| 119 | : ConsumerInstance(Instance) |
| 120 | { } |
| 121 | |
| 122 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc); |
| 123 | |
| 124 | private: |
| 125 | InstantiateTemplateParam *ConsumerInstance; |
| 126 | |
| 127 | }; |
| 128 | |
| 129 | bool |
| 130 | InstantiateTemplateParamRewriteVisitor::VisitTemplateTypeParmTypeLoc( |
| 131 | TemplateTypeParmTypeLoc Loc) |
| 132 | { |
| 133 | const TemplateTypeParmDecl *D = Loc.getDecl(); |
| 134 | if (D != ConsumerInstance->TheParameter) |
| 135 | return true; |
| 136 | |
| 137 | // I know it's ugly, but seems sometimes Clang injects some extra |
| 138 | // TypeLoc which causes the problem, for example, in the code below, |
| 139 | // template<typename T> class A { |
| 140 | // public: |
| 141 | // template<typename T1> struct C { typedef A other; }; |
| 142 | // }; |
| 143 | // template<typename T1, typename T2> class B { |
| 144 | // typedef typename T2::template C<int>::other type; |
| 145 | // }; |
| 146 | // class B<char, A<char> >; |
| 147 | // the "typedef typename T2 ..." is treated as |
| 148 | // typedef typename T2::template T2::C<int>::other type; |
| 149 | // where the second T2 is injected by Clang |
| 150 | void *Ptr = Loc.getBeginLoc().getPtrEncoding(); |
| 151 | if (ConsumerInstance->VisitedLocs.count(Ptr)) |
| 152 | return true; |
| 153 | ConsumerInstance->VisitedLocs.insert(Ptr); |
| 154 | |
| 155 | SourceRange Range = Loc.getSourceRange(); |
| 156 | ConsumerInstance->TheRewriter.ReplaceText(Range, |
| 157 | ConsumerInstance->TheInstantiationString); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | void InstantiateTemplateParam::Initialize(ASTContext &context) |
| 162 | { |
| 163 | Transformation::Initialize(context); |
| 164 | CollectionVisitor = new InstantiateTemplateParamASTVisitor(this); |
| 165 | ParamRewriteVisitor = new InstantiateTemplateParamRewriteVisitor(this); |
| 166 | } |
| 167 | |
| 168 | void InstantiateTemplateParam::HandleTranslationUnit(ASTContext &Ctx) |
| 169 | { |
| 170 | if (TransformationManager::isCLangOpt() || |
| 171 | TransformationManager::isOpenCLLangOpt()) { |
| 172 | ValidInstanceNum = 0; |
| 173 | } |
| 174 | else { |
| 175 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 176 | } |
| 177 | |
| 178 | if (QueryInstanceOnly) |
| 179 | return; |
| 180 | |
| 181 | if (TransformationCounter > ValidInstanceNum) { |
| 182 | TransError = TransMaxInstanceError; |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 187 | TransAssert(TheParameter && "NULL TheParameter!"); |
| 188 | TransAssert((TheInstantiationString != "") && "Invalid InstantiationString!"); |
| 189 | TransAssert(ParamRewriteVisitor && "NULL ParamRewriteVisitor!"); |
| 190 | ParamRewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 191 | removeTemplateKeyword(); |
| 192 | addForwardDecl(); |
| 193 | |
| 194 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 195 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 196 | TransError = TransInternalError; |
| 197 | } |
| 198 | |
| 199 | void InstantiateTemplateParam::removeTemplateKeyword() |
| 200 | { |
| 201 | if (dyn_cast<ClassTemplateDecl>(TheTemplateDecl)) |
| 202 | return; |
| 203 | TemplateParameterList *TPList = TheTemplateDecl->getTemplateParameters(); |
| 204 | if (TPList->size() != 1) |
| 205 | return; |
| 206 | const NamedDecl *ND = TPList->getParam(0); (void)ND; |
| 207 | TransAssert((ND == TheParameter) && "Invalid template parameter!"); |
| 208 | TheRewriter.RemoveText(SourceRange(TPList->getTemplateLoc(), |
| 209 | TPList->getRAngleLoc())); |
| 210 | } |
| 211 | |
| 212 | void InstantiateTemplateParam::addForwardDecl() |
| 213 | { |
| 214 | TransAssert(TheTemplateDecl && "NULL TheTemplateDecl!"); |
| 215 | if (TheForwardDeclString == "") |
| 216 | return; |
| 217 | RewriteHelper->insertStringBeforeTemplateDecl(TheTemplateDecl, |
| 218 | TheForwardDeclString); |
| 219 | } |
| 220 | |
| 221 | void InstantiateTemplateParam::addOneForwardDeclStr( |
| 222 | const RecordDecl *RD, |
| 223 | std::string &ForwardStr, |
| 224 | RecordDeclSet &TempAvailableRecordDecls) |
| 225 | { |
| 226 | const RecordDecl *CanonicalRD = dyn_cast<RecordDecl>(RD->getCanonicalDecl()); |
| 227 | if (AvailableRecordDecls.count(CanonicalRD) || |
| 228 | TempAvailableRecordDecls.count(CanonicalRD)) |
| 229 | return; |
| 230 | |
| 231 | ForwardStr += RD->getKindName(); |
| 232 | ForwardStr += " "; |
| 233 | ForwardStr += RD->getNameAsString() + ";\n"; |
| 234 | TempAvailableRecordDecls.insert(CanonicalRD); |
| 235 | } |
| 236 | |
| 237 | void InstantiateTemplateParam::addForwardTemplateDeclStr( |
| 238 | const ClassTemplateDecl *ClassTD, |
| 239 | std::string &ForwardStr, |
| 240 | RecordDeclSet &TempAvailableRecordDecls) |
| 241 | { |
| 242 | const CXXRecordDecl *RD = ClassTD->getTemplatedDecl(); |
| 243 | const RecordDecl *CanonicalRD = dyn_cast<RecordDecl>(RD->getCanonicalDecl()); |
| 244 | if (AvailableRecordDecls.count(CanonicalRD) || |
| 245 | TempAvailableRecordDecls.count(CanonicalRD)) |
| 246 | return; |
| 247 | |
| 248 | std::string TemplateStr = ""; |
| 249 | RewriteHelper->getStringBetweenLocs(TemplateStr, |
| 250 | ClassTD->getSourceRange().getBegin(), |
| 251 | RD->getInnerLocStart()); |
| 252 | ForwardStr += TemplateStr; |
| 253 | ForwardStr += RD->getKindName(); |
| 254 | ForwardStr += " "; |
| 255 | ForwardStr += RD->getNameAsString() + ";\n"; |
| 256 | TempAvailableRecordDecls.insert(CanonicalRD); |
| 257 | } |
| 258 | |
| 259 | void InstantiateTemplateParam::getForwardDeclStr( |
| 260 | const Type *Ty, |
| 261 | std::string &ForwardStr, |
| 262 | RecordDeclSet &TempAvailableRecordDecls) |
| 263 | { |
| 264 | if (const RecordType *RT = Ty->getAsUnionType()) { |
| 265 | const RecordDecl *RD = RT->getDecl(); |
| 266 | addOneForwardDeclStr(RD, ForwardStr, TempAvailableRecordDecls); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | const CXXRecordDecl *CXXRD = Ty->getAsCXXRecordDecl(); |
| 271 | if (!CXXRD) |
| 272 | return; |
| 273 | |
| 274 | const ClassTemplateSpecializationDecl *SpecD = |
| 275 | dyn_cast<ClassTemplateSpecializationDecl>(CXXRD); |
| 276 | if (!SpecD) { |
| 277 | addOneForwardDeclStr(CXXRD, ForwardStr, TempAvailableRecordDecls); |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | addForwardTemplateDeclStr(SpecD->getSpecializedTemplate(), |
| 282 | ForwardStr, |
| 283 | TempAvailableRecordDecls); |
| 284 | |
| 285 | const TemplateArgumentList &ArgList = SpecD->getTemplateArgs(); |
| 286 | unsigned NumArgs = ArgList.size(); |
| 287 | for (unsigned I = 0; I < NumArgs; ++I) { |
| 288 | const TemplateArgument Arg = ArgList[I]; |
| 289 | if (Arg.getKind() != TemplateArgument::Type) |
| 290 | continue; |
| 291 | getForwardDeclStr(Arg.getAsType().getTypePtr(), |
| 292 | ForwardStr, |
| 293 | TempAvailableRecordDecls); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | bool InstantiateTemplateParam::getTypeString( |
| 298 | const QualType &QT, std::string &Str, std::string &ForwardStr) |
| 299 | { |
| 300 | const Type *Ty = QT.getTypePtr(); |
| 301 | Type::TypeClass TC = Ty->getTypeClass(); |
| 302 | |
| 303 | switch (TC) { |
| 304 | case Type::Elaborated: { |
| 305 | const ElaboratedType *ETy = dyn_cast<ElaboratedType>(Ty); |
| 306 | return getTypeString(ETy->getNamedType(), Str, ForwardStr); |
| 307 | } |
| 308 | |
| 309 | case Type::Typedef: { |
| 310 | const TypedefType *TdefTy = dyn_cast<TypedefType>(Ty); |
| 311 | const TypedefNameDecl *TdefD = TdefTy->getDecl(); |
| 312 | return getTypeString(TdefD->getUnderlyingType(), Str, ForwardStr); |
| 313 | } |
| 314 | |
| 315 | case Type::Record: { |
| 316 | RecordDeclSet TempAvailableRecordDecls; |
| 317 | getForwardDeclStr(Ty, ForwardStr, TempAvailableRecordDecls); |
| 318 | QT.getAsStringInternal(Str, Context->getPrintingPolicy()); |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | case Type::Builtin: { |
| 323 | QT.getAsStringInternal(Str, Context->getPrintingPolicy()); |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | default: |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | TransAssert(0 && "Unreachable code!"); |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | bool |
| 336 | InstantiateTemplateParam::getTemplateArgumentString(const TemplateArgument &Arg, |
| 337 | std::string &ArgStr, |
| 338 | std::string &ForwardStr) |
| 339 | { |
| 340 | ArgStr = ""; |
| 341 | ForwardStr = ""; |
| 342 | if (Arg.getKind() != TemplateArgument::Type) |
| 343 | return false; |
| 344 | QualType QT = Arg.getAsType(); |
| 345 | return getTypeString(QT, ArgStr, ForwardStr); |
| 346 | } |
| 347 | |
| 348 | void InstantiateTemplateParam::handleOneTemplateSpecialization( |
| 349 | const TemplateDecl *D, const TemplateArgumentList & ArgList) |
| 350 | { |
| 351 | if (isInIncludedFile(D)) |
| 352 | return; |
| 353 | |
| 354 | NamedDecl *ND = D->getTemplatedDecl(); |
| 355 | TemplateParameterSet ParamsSet; |
| 356 | TemplateParameterVisitor ParameterVisitor(ParamsSet); |
| 357 | ParameterVisitor.TraverseDecl(ND); |
| 358 | |
| 359 | if (ParamsSet.size() == 0) |
| 360 | return; |
| 361 | |
| 362 | unsigned NumArgs = ArgList.size(); (void)NumArgs; |
| 363 | unsigned Idx = 0; |
| 364 | TemplateParameterList *TPList = D->getTemplateParameters(); |
| 365 | for (TemplateParameterList::const_iterator I = TPList->begin(), |
| 366 | E = TPList->end(); I != E; ++I) { |
| 367 | const NamedDecl *ND = (*I); |
| 368 | // make it simple, skip NonTypeTemplateParmDecl and |
| 369 | // TemplateTemplateParmDecl for now |
| 370 | const TemplateTypeParmDecl *TyParmDecl = |
| 371 | dyn_cast<TemplateTypeParmDecl>(ND); |
| 372 | if (!TyParmDecl || TyParmDecl->isParameterPack() || !ParamsSet.count(ND)) { |
| 373 | Idx++; |
| 374 | continue; |
| 375 | } |
| 376 | |
| 377 | TransAssert((Idx < NumArgs) && "Invalid Idx!"); |
| 378 | const TemplateArgument &Arg = ArgList.get(Idx); |
| 379 | std::string ArgStr; |
| 380 | std::string ForwardStr; |
| 381 | if (!getTemplateArgumentString(Arg, ArgStr, ForwardStr)) |
| 382 | continue; |
| 383 | // in case the argument has the same name as the parameter |
| 384 | if (ArgStr == ND->getNameAsString()) |
| 385 | continue; |
| 386 | ValidInstanceNum++; |
| 387 | if (ValidInstanceNum == TransformationCounter) { |
| 388 | TheInstantiationString = ArgStr; |
| 389 | TheParameter = ND; |
| 390 | TheTemplateDecl = D; |
| 391 | TheForwardDeclString = ForwardStr; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // TODO: handle partial specialization |
| 397 | void InstantiateTemplateParam::handleOneClassTemplateDecl( |
| 398 | const ClassTemplateDecl *D) |
| 399 | { |
| 400 | ClassTemplateDecl::spec_iterator I = D->spec_begin(); |
| 401 | ClassTemplateDecl::spec_iterator E = D->spec_end(); |
| 402 | if (I == E) |
| 403 | return; |
| 404 | ClassTemplateSpecializationDecl *SpecD = (*I); |
| 405 | ++I; |
| 406 | if (I != D->spec_end()) |
| 407 | return; |
| 408 | handleOneTemplateSpecialization(D, SpecD->getTemplateArgs()); |
| 409 | } |
| 410 | |
| 411 | void InstantiateTemplateParam::handleOneFunctionTemplateDecl( |
| 412 | const FunctionTemplateDecl *D) |
| 413 | { |
| 414 | FunctionTemplateDecl::spec_iterator I = D->spec_begin(); |
| 415 | FunctionTemplateDecl::spec_iterator E = D->spec_end(); |
| 416 | if (I == E) |
| 417 | return; |
| 418 | const FunctionDecl *FD = (*I); |
| 419 | ++I; |
| 420 | if (I != D->spec_end()) |
| 421 | return; |
| 422 | if (const FunctionTemplateSpecializationInfo *Info = |
| 423 | FD->getTemplateSpecializationInfo()) { |
| 424 | handleOneTemplateSpecialization(D, *(Info->TemplateArguments)); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | InstantiateTemplateParam::~InstantiateTemplateParam() |
| 429 | { |
| 430 | delete CollectionVisitor; |
| 431 | delete ParamRewriteVisitor; |
| 432 | } |
| 433 |