Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TemplateParamDuplicator {
- public:
- TemplateParamDuplicator(
- clang::Parser &parser,
- bool bypassLocation = true)
- : Parser(parser),
- BypassLocation(bypassLocation),
- AttrFactory(parser.getAttrFactory()),
- Actions(parser.getActions()) {}
- NamedDecl *GetCopyOf(NamedDecl *D) {
- if (auto *NonTypeTPD = dyn_cast_or_null<NonTypeTemplateParmDecl>(D)) {
- return Copy(NonTypeTPD);
- } else if (auto *TemplateTypePD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) {
- return Copy(TemplateTypePD);
- } else if (auto *TemplateTemplatePD = dyn_cast_or_null<TemplateTemplateParmDecl>(D)) {
- return Copy(TemplateTemplatePD);
- } else {
- llvm_unreachable("Invalid template parameter declaration");
- }
- }
- protected:
- // TODO: May be we should clone identifier as well.
- // auto *OldIdentifier = D->getIdentifier();
- // TODO: May be use CreateDeserialized?
- NonTypeTemplateParmDecl *Copy(const NonTypeTemplateParmDecl *D) {
- auto &Context = Actions.getASTContext();
- NonTypeTemplateParmDecl *Param = NonTypeTemplateParmDecl::Create(
- Context, Context.getTranslationUnitDecl(),
- BypassLocation ? D->getBeginLoc() : SourceLocation(),
- BypassLocation ? D->getLocation() : SourceLocation(),
- D->getDepth(),
- D->getPosition(),
- D->getIdentifier(),
- D->getType(),
- D->isParameterPack(),
- D->getTypeSourceInfo()
- );
- Param->setAccess(D->getAccess());
- // Add the template parameter into the current scope.
- Actions.getCurScope()->AddDecl(Param);
- Actions.IdResolver.AddDecl(Param);
- if (D->isInvalidDecl())
- Param->setInvalidDecl();
- else {
- Param->setDefaultArgument(D->getDefaultArgument());
- }
- return Param;
- }
- TemplateTypeParmDecl *Copy(const TemplateTypeParmDecl *D) {
- auto &Context = Actions.getASTContext();
- auto *TypeForDecl = cast<TemplateTypeParmType>(D->getTypeForDecl());
- TemplateTypeParmDecl *Param = TemplateTypeParmDecl::Create(
- Context,
- Context.getTranslationUnitDecl(),
- BypassLocation ? D->getBeginLoc() : SourceLocation(),
- BypassLocation ? D->getLocation() : SourceLocation(),
- D->getDepth(),
- TypeForDecl->getIndex(),
- D->getIdentifier(),
- D->wasDeclaredWithTypename(),
- D->isParameterPack()
- );
- Param->setAccess(D->getAccess());
- // Add the template parameter into the current scope.
- Actions.getCurScope()->AddDecl(Param);
- Actions.IdResolver.AddDecl(Param);
- if (D->isInvalidDecl())
- Param->setInvalidDecl();
- else {
- Param->setDefaultArgument(D->getDefaultArgumentInfo());
- }
- return Param;
- }
- TemplateTemplateParmDecl *Copy(const TemplateTemplateParmDecl *D) {
- auto &Context = Actions.getASTContext();
- TemplateParameterList *NewTemplateTemplateParametersList;
- {
- auto *OldParams = D->getTemplateParameters();
- Parser::ParseScope TemplateParmScope(&Parser, clang::Scope::TemplateParamScope);
- SmallVector<NamedDecl *, 4> ParamsVector;
- for (auto *OldP : OldParams->asArray()) {
- ParamsVector.push_back(GetCopyOf(OldP));
- }
- // Note, there is no requires clause for template template params,
- // even in C++20
- NewTemplateTemplateParametersList = Actions.ActOnTemplateParameterList(
- OldParams->getDepth(),
- SourceLocation(),
- OldParams->getTemplateLoc(),
- OldParams->getLAngleLoc(),
- ParamsVector,
- OldParams->getRAngleLoc(),
- nullptr
- );
- }
- TemplateTemplateParmDecl *Param = TemplateTemplateParmDecl::Create(
- Context,
- Context.getTranslationUnitDecl(),
- BypassLocation ? D->getLocation() : SourceLocation(),
- D->getDepth(),
- D->getPosition(),
- D->isParameterPack(),
- D->getIdentifier(),
- NewTemplateTemplateParametersList
- );
- Param->setAccess(D->getAccess());
- // Add the template parameter into the current scope.
- Actions.getCurScope()->AddDecl(Param);
- Actions.IdResolver.AddDecl(Param);
- if (D->isInvalidDecl())
- Param->setInvalidDecl();
- else {
- Param->setDefaultArgument(Context, D->getDefaultArgument());
- }
- return Param;
- }
- clang::Parser &Parser;
- bool BypassLocation;
- AttributeFactory &AttrFactory;
- Sema &Actions;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement