steveire updated this revision to Diff 181395.
steveire added a comment.

Updates


Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55491/new/

https://reviews.llvm.org/D55491

Files:
  include/clang/AST/TemplateArgumentVisitor.h
  include/clang/AST/TextNodeDumper.h
  lib/AST/ASTDumper.cpp
  lib/AST/TextNodeDumper.cpp

Index: lib/AST/TextNodeDumper.cpp
===================================================================
--- lib/AST/TextNodeDumper.cpp
+++ lib/AST/TextNodeDumper.cpp
@@ -64,6 +64,19 @@
   ConstAttrVisitor<TextNodeDumper>::Visit(A);
 }
 
+void TextNodeDumper::Visit(const TemplateArgument &TA, SourceRange R,
+                           const Decl *From, StringRef Label) {
+  OS << "TemplateArgument";
+  if (R.isValid())
+    dumpSourceRange(R);
+
+  if (From) {
+    dumpDeclRef(From, Label);
+  }
+
+  ConstTemplateArgumentVisitor<TextNodeDumper>::Visit(TA);
+}
+
 void TextNodeDumper::dumpPointer(const void *Ptr) {
   ColorScope Color(OS, ShowColors, AddressColor);
   OS << ' ' << Ptr;
@@ -184,12 +197,12 @@
   OS << ")";
 }
 
-void TextNodeDumper::dumpDeclRef(const Decl *D, const char *Label) {
+void TextNodeDumper::dumpDeclRef(const Decl *D, StringRef Label) {
   if (!D)
     return;
 
   AddChild([=] {
-    if (Label)
+    if (!Label.empty())
       OS << Label << ' ';
     dumpBareDeclRef(D);
   });
@@ -317,3 +330,45 @@
     const comments::VerbatimLineComment *C, const comments::FullComment *) {
   OS << " Text=\"" << C->getText() << "\"";
 }
+
+void TextNodeDumper::VisitNullTemplateArgument(const TemplateArgument &) {
+  OS << " null";
+}
+
+void TextNodeDumper::VisitTypeTemplateArgument(const TemplateArgument &TA) {
+  OS << " type";
+  dumpType(TA.getAsType());
+}
+
+void TextNodeDumper::VisitDeclarationTemplateArgument(
+    const TemplateArgument &TA) {
+  OS << " decl";
+  dumpDeclRef(TA.getAsDecl());
+}
+
+void TextNodeDumper::VisitNullPtrTemplateArgument(const TemplateArgument &) {
+  OS << " nullptr";
+}
+
+void TextNodeDumper::VisitIntegralTemplateArgument(const TemplateArgument &TA) {
+  OS << " integral " << TA.getAsIntegral();
+}
+
+void TextNodeDumper::VisitTemplateTemplateArgument(const TemplateArgument &TA) {
+  OS << " template ";
+  TA.getAsTemplate().dump(OS);
+}
+
+void TextNodeDumper::VisitTemplateExpansionTemplateArgument(
+    const TemplateArgument &TA) {
+  OS << " template expansion ";
+  TA.getAsTemplateOrTemplatePattern().dump(OS);
+}
+
+void TextNodeDumper::VisitExpressionTemplateArgument(const TemplateArgument &) {
+  OS << " expr";
+}
+
+void TextNodeDumper::VisitPackTemplateArgument(const TemplateArgument &) {
+  OS << " pack";
+}
Index: lib/AST/ASTDumper.cpp
===================================================================
--- lib/AST/ASTDumper.cpp
+++ lib/AST/ASTDumper.cpp
@@ -24,6 +24,7 @@
 #include "clang/AST/DeclVisitor.h"
 #include "clang/AST/LocInfoType.h"
 #include "clang/AST/StmtVisitor.h"
+#include "clang/AST/TemplateArgumentVisitor.h"
 #include "clang/AST/TextNodeDumper.h"
 #include "clang/AST/TypeVisitor.h"
 #include "clang/Basic/Builtins.h"
@@ -44,7 +45,8 @@
         public ConstStmtVisitor<ASTDumper>,
         public ConstCommentVisitor<ASTDumper, void, const FullComment *>,
         public TypeVisitor<ASTDumper>,
-        public ConstAttrVisitor<ASTDumper> {
+        public ConstAttrVisitor<ASTDumper>,
+        public ConstTemplateArgumentVisitor<ASTDumper> {
 
     TextNodeDumper NodeDumper;
 
@@ -440,6 +442,14 @@
     // Comments.
     void dumpComment(const Comment *C, const FullComment *FC);
 
+    void VisitExpressionTemplateArgument(const TemplateArgument &TA) {
+      dumpStmt(TA.getAsExpr());
+    }
+    void VisitPackTemplateArgument(const TemplateArgument &TA) {
+      for (const auto& TArg : TA.pack_elements())
+        dumpTemplateArgument(TArg);
+    }
+
 // Implements Visit methods for Attrs.
 #include "clang/AST/AttrNodeTraverse.inc"
   };
@@ -670,49 +680,8 @@
 void ASTDumper::dumpTemplateArgument(const TemplateArgument &A, SourceRange R,
                                      const Decl *From, const char *Label) {
   dumpChild([=] {
-    OS << "TemplateArgument";
-    if (R.isValid())
-      NodeDumper.dumpSourceRange(R);
-
-    if (From)
-      NodeDumper.dumpDeclRef(From, Label);
-
-    switch (A.getKind()) {
-    case TemplateArgument::Null:
-      OS << " null";
-      break;
-    case TemplateArgument::Type:
-      OS << " type";
-      NodeDumper.dumpType(A.getAsType());
-      break;
-    case TemplateArgument::Declaration:
-      OS << " decl";
-      NodeDumper.dumpDeclRef(A.getAsDecl());
-      break;
-    case TemplateArgument::NullPtr:
-      OS << " nullptr";
-      break;
-    case TemplateArgument::Integral:
-      OS << " integral " << A.getAsIntegral();
-      break;
-    case TemplateArgument::Template:
-      OS << " template ";
-      A.getAsTemplate().dump(OS);
-      break;
-    case TemplateArgument::TemplateExpansion:
-      OS << " template expansion ";
-      A.getAsTemplateOrTemplatePattern().dump(OS);
-      break;
-    case TemplateArgument::Expression:
-      OS << " expr";
-      dumpStmt(A.getAsExpr());
-      break;
-    case TemplateArgument::Pack:
-      OS << " pack";
-      for (const auto& TArg : A.pack_elements())
-        dumpTemplateArgument(TArg);
-      break;
-    }
+    NodeDumper.Visit(A, R, From, Label);
+    ConstTemplateArgumentVisitor<ASTDumper>::Visit(A);
   });
 }
 
Index: include/clang/AST/TextNodeDumper.h
===================================================================
--- include/clang/AST/TextNodeDumper.h
+++ include/clang/AST/TextNodeDumper.h
@@ -20,6 +20,7 @@
 #include "clang/AST/CommentCommandTraits.h"
 #include "clang/AST/CommentVisitor.h"
 #include "clang/AST/ExprCXX.h"
+#include "clang/AST/TemplateArgumentVisitor.h"
 
 namespace clang {
 
@@ -123,7 +124,8 @@
     : public TextTreeStructure,
       public comments::ConstCommentVisitor<TextNodeDumper, void,
                                            const comments::FullComment *>,
-      public ConstAttrVisitor<TextNodeDumper> {
+      public ConstAttrVisitor<TextNodeDumper>,
+      public ConstTemplateArgumentVisitor<TextNodeDumper> {
   raw_ostream &OS;
   const bool ShowColors;
 
@@ -150,6 +152,9 @@
 
   void Visit(const Attr *A);
 
+  void Visit(const TemplateArgument &TA, SourceRange R,
+             const Decl *From = nullptr, StringRef Label = {});
+
   void dumpPointer(const void *Ptr);
   void dumpLocation(SourceLocation Loc);
   void dumpSourceRange(SourceRange R);
@@ -160,7 +165,7 @@
   void dumpAccessSpecifier(AccessSpecifier AS);
   void dumpCXXTemporary(const CXXTemporary *Temporary);
 
-  void dumpDeclRef(const Decl *D, const char *Label = nullptr);
+  void dumpDeclRef(const Decl *D, StringRef Label = {});
 
   void visitTextComment(const comments::TextComment *C,
                         const comments::FullComment *);
@@ -186,6 +191,16 @@
 
 // Implements Visit methods for Attrs.
 #include "clang/AST/AttrTextNodeDump.inc"
+
+  void VisitNullTemplateArgument(const TemplateArgument &TA);
+  void VisitTypeTemplateArgument(const TemplateArgument &TA);
+  void VisitDeclarationTemplateArgument(const TemplateArgument &TA);
+  void VisitNullPtrTemplateArgument(const TemplateArgument &TA);
+  void VisitIntegralTemplateArgument(const TemplateArgument &TA);
+  void VisitTemplateTemplateArgument(const TemplateArgument &TA);
+  void VisitTemplateExpansionTemplateArgument(const TemplateArgument &TA);
+  void VisitExpressionTemplateArgument(const TemplateArgument &TA);
+  void VisitPackTemplateArgument(const TemplateArgument &TA);
 };
 
 } // namespace clang
Index: include/clang/AST/TemplateArgumentVisitor.h
===================================================================
--- /dev/null
+++ include/clang/AST/TemplateArgumentVisitor.h
@@ -0,0 +1,99 @@
+//===- TemplateArgumentVisitor.h - Visitor for TArg subclasses --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+//  This file defines the TemplateArgumentVisitor interface.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_AST_TEMPLATEARGUMENTVISITOR_H
+#define LLVM_CLANG_AST_TEMPLATEARGUMENTVISITOR_H
+
+#include "clang/AST/TemplateBase.h"
+
+namespace clang {
+
+namespace templateargumentvisitor {
+
+/// A simple visitor class that helps create template argument visitors.
+template <template <typename> class Ref, typename ImplClass,
+          typename RetTy = void, typename... ParamTys>
+class Base {
+public:
+#define REF(CLASS) typename Ref<CLASS>::type
+#define DISPATCH(NAME)                                                         \
+  case TemplateArgument::NAME:                                                 \
+    return static_cast<ImplClass *>(this)->Visit##NAME##TemplateArgument(      \
+        TA, std::forward<ParamTys>(P)...)
+
+  RetTy Visit(REF(TemplateArgument) TA, ParamTys... P) {
+    switch (TA.getKind()) {
+      DISPATCH(Null);
+      DISPATCH(Type);
+      DISPATCH(Declaration);
+      DISPATCH(NullPtr);
+      DISPATCH(Integral);
+      DISPATCH(Template);
+      DISPATCH(TemplateExpansion);
+      DISPATCH(Expression);
+      DISPATCH(Pack);
+    }
+    llvm_unreachable("TemplateArgument is not covered in switch!");
+  }
+
+  // If the implementation chooses not to implement a certain visit
+  // method, fall back to the parent.
+
+#define VISIT_METHOD(CATEGORY)                                                 \
+  RetTy Visit##CATEGORY##TemplateArgument(REF(TemplateArgument) TA,            \
+                                          ParamTys... P) {                     \
+    return VisitTemplateArgument(TA, std::forward<ParamTys>(P)...);            \
+  }
+
+  VISIT_METHOD(Null);
+  VISIT_METHOD(Type);
+  VISIT_METHOD(Declaration);
+  VISIT_METHOD(NullPtr);
+  VISIT_METHOD(Integral);
+  VISIT_METHOD(Template);
+  VISIT_METHOD(TemplateExpansion);
+  VISIT_METHOD(Expression);
+  VISIT_METHOD(Pack);
+
+  RetTy VisitTemplateArgument(REF(TemplateArgument), ParamTys...) {
+    return RetTy();
+  }
+
+#undef REF
+#undef DISPATCH
+#undef VISIT_METHOD
+};
+
+} // namespace templateargumentvisitor
+
+/// A simple visitor class that helps create template argument visitors.
+///
+/// This class does not preserve constness of TemplateArgument references (see
+/// also ConstTemplateArgumentVisitor).
+template <typename ImplClass, typename RetTy = void, typename... ParamTys>
+class TemplateArgumentVisitor
+    : public templateargumentvisitor::Base<std::add_lvalue_reference, ImplClass,
+                                           RetTy, ParamTys...> {};
+
+/// A simple visitor class that helps create template argument visitors.
+///
+/// This class preserves constness of TemplateArgument references (see also
+/// TemplateArgumentVisitor).
+template <typename ImplClass, typename RetTy = void, typename... ParamTys>
+class ConstTemplateArgumentVisitor
+    : public templateargumentvisitor::Base<llvm::make_const_ref, ImplClass,
+                                           RetTy, ParamTys...> {};
+
+} // namespace clang
+
+#endif // LLVM_CLANG_AST_TEMPLATEARGUMENTVISITOR_H
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to