Advertisement
Guest User

Template Parameters Duplicator

a guest
Apr 17th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.66 KB | None | 0 0
  1. class TemplateParamDuplicator {
  2. public:
  3.     TemplateParamDuplicator(
  4.             clang::Parser &parser,
  5.             bool bypassLocation = true)
  6.     : Parser(parser),
  7.       BypassLocation(bypassLocation),
  8.       AttrFactory(parser.getAttrFactory()),
  9.       Actions(parser.getActions()) {}
  10.  
  11.     NamedDecl *GetCopyOf(NamedDecl *D) {
  12.       if (auto *NonTypeTPD = dyn_cast_or_null<NonTypeTemplateParmDecl>(D)) {
  13.         return Copy(NonTypeTPD);
  14.       } else if (auto *TemplateTypePD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
  15.         return Copy(TemplateTypePD);
  16.       } else if (auto *TemplateTemplatePD = dyn_cast_or_null<TemplateTemplateParmDecl>(D)) {
  17.         return Copy(TemplateTemplatePD);
  18.       } else {
  19.         llvm_unreachable("Invalid template parameter declaration");
  20.       }
  21.     }
  22.  
  23. protected:
  24.  
  25.     // TODO: May be we should clone identifier as well.
  26.     // auto *OldIdentifier = D->getIdentifier();
  27.  
  28.     // TODO: May be use CreateDeserialized?
  29.  
  30.     NonTypeTemplateParmDecl *Copy(const NonTypeTemplateParmDecl *D) {
  31.  
  32.       auto &Context = Actions.getASTContext();
  33.  
  34.       NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create(
  35.           Context, Context.getTranslationUnitDecl(),
  36.           BypassLocation ? D->getBeginLoc() : SourceLocation(),
  37.           BypassLocation ? D->getLocation() : SourceLocation(),
  38.           D->getDepth(),
  39.           D->getPosition(),
  40.           D->getIdentifier(),
  41.           D->getType(),
  42.           D->isParameterPack(),
  43.           D->getTypeSourceInfo()
  44.       );
  45.  
  46.       Param->setAccess(D->getAccess());
  47.  
  48.       // Add the template parameter into the current scope.
  49.       Actions.getCurScope()->AddDecl(Param);
  50.       Actions.IdResolver.AddDecl(Param);
  51.  
  52.       if (D->isInvalidDecl())
  53.         Param->setInvalidDecl();
  54.       else {
  55.         Param->setDefaultArgument(D->getDefaultArgument());
  56.       }
  57.  
  58.       return Param;
  59.     }
  60.  
  61.     TemplateTypeParmDecl *Copy(const TemplateTypeParmDecl *D) {
  62.       auto &Context = Actions.getASTContext();
  63.       auto *TypeForDecl = cast<TemplateTypeParmType>(D->getTypeForDecl());
  64.  
  65.       TemplateTypeParmDecl *Param = TemplateTypeParmDecl::Create(
  66.           Context,
  67.           Context.getTranslationUnitDecl(),
  68.           BypassLocation ? D->getBeginLoc() : SourceLocation(),
  69.           BypassLocation ? D->getLocation() : SourceLocation(),
  70.           D->getDepth(),
  71.           TypeForDecl->getIndex(),
  72.           D->getIdentifier(),
  73.           D->wasDeclaredWithTypename(),
  74.           D->isParameterPack()
  75.       );
  76.  
  77.       Param->setAccess(D->getAccess());
  78.  
  79.       // Add the template parameter into the current scope.
  80.       Actions.getCurScope()->AddDecl(Param);
  81.       Actions.IdResolver.AddDecl(Param);
  82.  
  83.       if (D->isInvalidDecl())
  84.         Param->setInvalidDecl();
  85.       else {
  86.         Param->setDefaultArgument(D->getDefaultArgumentInfo());
  87.       }
  88.  
  89.       return Param;
  90.     }
  91.  
  92.     TemplateTemplateParmDecl *Copy(const TemplateTemplateParmDecl *D) {
  93.       auto &Context = Actions.getASTContext();
  94.  
  95.       TemplateParameterList *NewTemplateTemplateParametersList;
  96.       {
  97.         auto *OldParams = D->getTemplateParameters();
  98.  
  99.         Parser::ParseScope TemplateParmScope(&Parser, clang::Scope::TemplateParamScope);
  100.         SmallVector<NamedDecl *, 4> ParamsVector;
  101.  
  102.         for (auto *OldP : OldParams->asArray()) {
  103.           ParamsVector.push_back(GetCopyOf(OldP));
  104.         }
  105.  
  106.         // Note, there is no requires clause for template template params,
  107.         // even in C++20
  108.         NewTemplateTemplateParametersList = Actions.ActOnTemplateParameterList(
  109.             OldParams->getDepth(),
  110.             SourceLocation(),
  111.             OldParams->getTemplateLoc(),
  112.             OldParams->getLAngleLoc(),
  113.             ParamsVector,
  114.             OldParams->getRAngleLoc(),
  115.             nullptr
  116.         );
  117.       }
  118.  
  119.       TemplateTemplateParmDecl *Param = TemplateTemplateParmDecl::Create(
  120.           Context,
  121.           Context.getTranslationUnitDecl(),
  122.           BypassLocation ? D->getLocation() : SourceLocation(),
  123.           D->getDepth(),
  124.           D->getPosition(),
  125.           D->isParameterPack(),
  126.           D->getIdentifier(),
  127.           NewTemplateTemplateParametersList
  128.       );
  129.  
  130.       Param->setAccess(D->getAccess());
  131.  
  132.       // Add the template parameter into the current scope.
  133.       Actions.getCurScope()->AddDecl(Param);
  134.       Actions.IdResolver.AddDecl(Param);
  135.  
  136.       if (D->isInvalidDecl())
  137.         Param->setInvalidDecl();
  138.       else {
  139.         Param->setDefaultArgument(Context, D->getDefaultArgument());
  140.       }
  141.  
  142.       return Param;
  143.     }
  144.  
  145.     clang::Parser &Parser;
  146.     bool BypassLocation;
  147.  
  148.     AttributeFactory &AttrFactory;
  149.     Sema &Actions;
  150. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement