| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Copyright (c) 2012, 2013, 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 "InstantiateTemplateTypeParamToInt.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 | // TODO: probably need to handle more cases where this transformation |
| 26 | // generates invalid code, e.g.: |
| 27 | // template <typename T> struct S { |
| 28 | // typedef T type; |
| 29 | // type &foo(); |
| 30 | // }; |
| 31 | // struct A { |
| 32 | // void foo() {}; |
| 33 | // }; |
| 34 | // void bar() { |
| 35 | // struct S<A> s; |
| 36 | // struct A a = s.foo(); |
| 37 | // } |
| 38 | |
| 39 | static const char *DescriptionMsg = |
| 40 | "This pass tries to instantiate a template type parameter of a class \ |
| 41 | inside the class's definition. For example, \n\ |
| 42 | template <typename T> struct A { T foo(void); } \n\ |
| 43 | => \n\ |
| 44 | template <typename T> struct A { int foo(void); } \n\ |
| 45 | \n\ |
| 46 | Currently, just simply replace any reference of T with int.\n"; |
| 47 | |
| 48 | static RegisterTransformation<InstantiateTemplateTypeParamToInt> |
| 49 | Trans("instantiate-template-type-param-to-int", DescriptionMsg); |
| 50 | |
| 51 | namespace { |
| 52 | |
| 53 | typedef llvm::SmallPtrSet<const NamedDecl *, 8> TemplateParameterSet; |
| 54 | |
| 55 | class TemplateParameterVisitor : public |
| 56 | RecursiveASTVisitor<TemplateParameterVisitor> { |
| 57 | |
| 58 | public: |
| 59 | explicit TemplateParameterVisitor(TemplateParameterSet &Params) |
| 60 | : UsedParameters(Params) |
| 61 | { } |
| 62 | |
| 63 | ~TemplateParameterVisitor() { }; |
| 64 | |
| 65 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc); |
| 66 | |
| 67 | private: |
| 68 | |
| 69 | TemplateParameterSet &UsedParameters; |
| 70 | }; |
| 71 | |
| 72 | // seems clang can't detect the T in T::* in the following case: |
| 73 | // struct B; |
| 74 | // template <typename T> struct C { |
| 75 | // C(void (T::*)()) { } |
| 76 | // }; |
| 77 | // struct D { C<B> m; }; |
| 78 | bool TemplateParameterVisitor::VisitTemplateTypeParmTypeLoc( |
| 79 | TemplateTypeParmTypeLoc Loc) |
| 80 | { |
| 81 | const TemplateTypeParmDecl *D = Loc.getDecl(); |
| 82 | UsedParameters.insert(D); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | } // end anonymous namespace |
| 87 | |
| 88 | // In order to generate less uncompilable code, filter out cases such as |
| 89 | // template <typename T> struct S { |
| 90 | // T::type foo(void); |
| 91 | // }; |
| 92 | class TemplateParameterFilterVisitor : public |
| 93 | RecursiveASTVisitor<TemplateParameterFilterVisitor> { |
| 94 | |
| 95 | public: |
| 96 | TemplateParameterFilterVisitor(TemplateParameterSet &Params, |
| 97 | InstantiateTemplateTypeParamToInt *Instance) |
| 98 | : Parameters(Params), |
| 99 | ConsumerInstance(Instance) |
| 100 | { } |
| 101 | |
| 102 | ~TemplateParameterFilterVisitor() { }; |
| 103 | |
| 104 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc); |
| 105 | |
| 106 | private: |
| 107 | TemplateParameterSet &Parameters; |
| 108 | |
| 109 | InstantiateTemplateTypeParamToInt *ConsumerInstance; |
| 110 | }; |
| 111 | |
| 112 | bool TemplateParameterFilterVisitor::VisitTemplateTypeParmTypeLoc( |
| 113 | TemplateTypeParmTypeLoc Loc) |
| 114 | { |
| 115 | const NamedDecl *ND = Loc.getDecl(); |
| 116 | if (!Parameters.count(ND)) |
| 117 | return true; |
| 118 | |
| 119 | if (ConsumerInstance->isBeforeColonColon(Loc)) |
| 120 | Parameters.erase(ND); |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | // ISSUE: maybe also try to instantiate a type parm with |
| 126 | // its default value, if it exists? |
| 127 | class InstantiateTemplateTypeParamToIntASTVisitor : public |
| 128 | RecursiveASTVisitor<InstantiateTemplateTypeParamToIntASTVisitor> { |
| 129 | |
| 130 | public: |
| 131 | explicit InstantiateTemplateTypeParamToIntASTVisitor( |
| 132 | InstantiateTemplateTypeParamToInt *Instance) |
| 133 | : ConsumerInstance(Instance) |
| 134 | { } |
| 135 | |
| 136 | bool VisitClassTemplateDecl(ClassTemplateDecl *D); |
| 137 | |
| 138 | bool VisitFunctionTemplateDecl(FunctionTemplateDecl *D); |
| 139 | |
| 140 | private: |
| 141 | InstantiateTemplateTypeParamToInt *ConsumerInstance; |
| 142 | |
| 143 | }; |
| 144 | |
| 145 | class InstantiateTemplateTypeParamToIntRewriteVisitor : public |
| 146 | RecursiveASTVisitor<InstantiateTemplateTypeParamToIntRewriteVisitor> { |
| 147 | |
| 148 | public: |
| 149 | explicit InstantiateTemplateTypeParamToIntRewriteVisitor( |
| 150 | InstantiateTemplateTypeParamToInt *Instance) |
| 151 | : ConsumerInstance(Instance) |
| 152 | { } |
| 153 | |
| 154 | bool VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc Loc); |
| 155 | |
| 156 | private: |
| 157 | InstantiateTemplateTypeParamToInt *ConsumerInstance; |
| 158 | |
| 159 | }; |
| 160 | |
| 161 | bool InstantiateTemplateTypeParamToIntASTVisitor::VisitClassTemplateDecl( |
| 162 | ClassTemplateDecl *D) |
| 163 | { |
| 164 | // only care about declarations |
| 165 | if (D->isThisDeclarationADefinition()) |
| 166 | ConsumerInstance->handleOneTemplateDecl(D); |
| 167 | return true; |
| 168 | } |
| 169 | |
| 170 | bool InstantiateTemplateTypeParamToIntASTVisitor::VisitFunctionTemplateDecl( |
| 171 | FunctionTemplateDecl *D) |
| 172 | { |
| 173 | // only care about declarations |
| 174 | if (D->isThisDeclarationADefinition()) |
| 175 | ConsumerInstance->handleOneTemplateDecl(D); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | bool |
| 180 | InstantiateTemplateTypeParamToIntRewriteVisitor::VisitTemplateTypeParmTypeLoc( |
| 181 | TemplateTypeParmTypeLoc Loc) |
| 182 | { |
| 183 | const TemplateTypeParmDecl *D = Loc.getDecl(); |
| 184 | if (D != ConsumerInstance->TheParameter) |
| 185 | return true; |
| 186 | |
| 187 | void *Ptr = Loc.getBeginLoc().getPtrEncoding(); |
| 188 | if (ConsumerInstance->VisitedLocs.count(Ptr)) |
| 189 | return true; |
| 190 | ConsumerInstance->VisitedLocs.insert(Ptr); |
| 191 | |
| 192 | SourceRange Range = Loc.getSourceRange(); |
| 193 | ConsumerInstance->TheRewriter.ReplaceText(Range, "int"); |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | void InstantiateTemplateTypeParamToInt::Initialize(ASTContext &context) |
| 198 | { |
| 199 | Transformation::Initialize(context); |
| 200 | CollectionVisitor = new InstantiateTemplateTypeParamToIntASTVisitor(this); |
| 201 | ParamRewriteVisitor = new InstantiateTemplateTypeParamToIntRewriteVisitor(this); |
| 202 | } |
| 203 | |
| 204 | void InstantiateTemplateTypeParamToInt::HandleTranslationUnit(ASTContext &Ctx) |
| 205 | { |
| 206 | if (TransformationManager::isCLangOpt() || |
| 207 | TransformationManager::isOpenCLLangOpt()) { |
| 208 | ValidInstanceNum = 0; |
| 209 | } |
| 210 | else { |
| 211 | CollectionVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 212 | } |
| 213 | |
| 214 | if (QueryInstanceOnly) |
| 215 | return; |
| 216 | |
| 217 | if (TransformationCounter > ValidInstanceNum) { |
| 218 | TransError = TransMaxInstanceError; |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 223 | |
| 224 | TransAssert(TheParameter && "NULL TheParameter!"); |
| 225 | TransAssert(ParamRewriteVisitor && "NULL ParamRewriteVisitor!"); |
| 226 | ParamRewriteVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 227 | |
| 228 | if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 229 | Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 230 | TransError = TransInternalError; |
| 231 | } |
| 232 | |
| 233 | void InstantiateTemplateTypeParamToInt::handleOneTemplateDecl(const TemplateDecl *D) |
| 234 | { |
| 235 | if (isInIncludedFile(D)) |
| 236 | return; |
| 237 | |
| 238 | // doesn't handle TypeAliasTemplateDecl |
| 239 | TransAssert((!dyn_cast<TypeAliasTemplateDecl>(D)) && |
| 240 | "Doesn't support TypeAliasTemplateDecl!"); |
| 241 | const TemplateDecl *CanonicalD = |
| 242 | dyn_cast<TemplateDecl>(D->getCanonicalDecl()); |
| 243 | TransAssert(CanonicalD && "Invalid TemplateDecl!"); |
| 244 | TransAssert((VisitedTemplateDecls.count(CanonicalD) == 0) && |
| 245 | "Duplicate visitation to TemplateDecl!"); |
| 246 | VisitedTemplateDecls.insert(CanonicalD); |
| 247 | |
| 248 | NamedDecl *ND = D->getTemplatedDecl(); |
| 249 | TemplateParameterSet ParamsSet; |
| 250 | TemplateParameterVisitor ParameterVisitor(ParamsSet); |
| 251 | ParameterVisitor.TraverseDecl(ND); |
| 252 | filterInvalidParams(D, ParamsSet); |
| 253 | |
| 254 | if (ParamsSet.size() == 0) |
| 255 | return; |
| 256 | |
| 257 | TemplateParameterList *TPList = D->getTemplateParameters(); |
| 258 | for (TemplateParameterList::const_iterator I = TPList->begin(), |
| 259 | E = TPList->end(); I != E; ++I) { |
| 260 | const NamedDecl *ND = (*I); |
| 261 | if (!ParamsSet.count(ND)) |
| 262 | continue; |
| 263 | ValidInstanceNum++; |
| 264 | if (ValidInstanceNum == TransformationCounter) |
| 265 | TheParameter = ND; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void InstantiateTemplateTypeParamToInt::filterInvalidParams(const TemplateDecl *D, |
| 270 | TemplateParameterSet &Params) |
| 271 | { |
| 272 | NamedDecl *ND = D->getTemplatedDecl(); |
| 273 | TemplateParameterFilterVisitor Filter(Params, this); |
| 274 | Filter.TraverseDecl(ND); |
| 275 | |
| 276 | const ClassTemplateDecl *CD = dyn_cast<ClassTemplateDecl>(D); |
| 277 | if (!CD) |
| 278 | return; |
| 279 | CXXRecordDecl *Def = CD->getTemplatedDecl()->getDefinition(); |
| 280 | TransAssert(Def && "No Definition?"); |
| 281 | if (!Def->isCompleteDefinition()) |
| 282 | return; |
| 283 | for (CXXRecordDecl::base_class_const_iterator I = Def->bases_begin(), |
| 284 | E = Def->bases_end(); I != E; ++I) { |
| 285 | const CXXBaseSpecifier *BS = I; |
| 286 | const Type *Ty = BS->getType().getTypePtr(); |
| 287 | const TemplateTypeParmType *ParmTy = dyn_cast<TemplateTypeParmType>(Ty); |
| 288 | if (!ParmTy) |
| 289 | continue; |
| 290 | const TemplateTypeParmDecl *ParmD = ParmTy->getDecl(); |
| 291 | Params.erase(ParmD); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | InstantiateTemplateTypeParamToInt::~InstantiateTemplateTypeParamToInt() |
| 296 | { |
| 297 | delete CollectionVisitor; |
| 298 | delete ParamRewriteVisitor; |
| 299 | } |
| 300 |