Hi klimek,

For consistency, following the pattern set by llvm::Pass as much as
possible. Not all features of Pass registration are obviously applicable
to Transform registration at this point.

http://llvm-reviews.chandlerc.com/D221

Files:
  cpp11-migrate/CMakeLists.txt
  cpp11-migrate/Transform.cpp
  cpp11-migrate/Transform.h
  cpp11-migrate/TransformRegistry.cpp
  cpp11-migrate/TransformRegistry.h
  cpp11-migrate/TransformSupport.h
Index: cpp11-migrate/CMakeLists.txt
===================================================================
--- cpp11-migrate/CMakeLists.txt
+++ cpp11-migrate/CMakeLists.txt
@@ -3,6 +3,8 @@
 
 add_clang_executable(cpp11-migrate
   Cpp11Migrate.cpp
+  TransformRegistry.cpp
+  Transform.cpp
   )
 
 target_link_libraries(cpp11-migrate
Index: cpp11-migrate/Transform.cpp
===================================================================
--- /dev/null
+++ cpp11-migrate/Transform.cpp
@@ -0,0 +1,28 @@
+//===-- cpp11-migrate/Transform.cpp - Transform Impl ------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides implementations for the Transform class and other
+/// support classes.
+///
+//===----------------------------------------------------------------------===//
+
+#include "Transform.h"
+#include "TransformSupport.h"
+
+TransformRegistrationListener::TransformRegistrationListener() {
+  TransformRegistry::getRegistry()->addRegistrationListener(this);
+}
+
+TransformRegistrationListener::~TransformRegistrationListener() {
+  TransformRegistry::getRegistry()->removeRegistrationListener(this);
+}
+
+void TransformRegistrationListener::enumerateTransforms() {
+  TransformRegistry::getRegistry()->enumerateWith(this);
+}
Index: cpp11-migrate/Transform.h
===================================================================
--- /dev/null
+++ cpp11-migrate/Transform.h
@@ -0,0 +1,20 @@
+//===-- cpp11-migrate/Transform.h - Transform Base Class Def'n --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides the definition for the base Transform class from
+/// which all transforms must subclass.
+///
+//===----------------------------------------------------------------------===//
+#ifndef _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_H_
+#define _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_H_
+
+class Transform {
+};
+
+#endif // _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_H_
Index: cpp11-migrate/TransformRegistry.cpp
===================================================================
--- /dev/null
+++ cpp11-migrate/TransformRegistry.cpp
@@ -0,0 +1,73 @@
+//===- cpp11-migrate/TransformRegistry.cpp - Registration Impl --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides the implementation of the TransformRegistry.
+///
+//===----------------------------------------------------------------------===//
+
+#include "TransformRegistry.h"
+#include "TransformSupport.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/StringMap.h"
+#include <vector>
+
+llvm::ManagedStatic<TransformRegistry> TheRegistry;
+
+class TransformRegistryImpl {
+public:
+  typedef llvm::StringMap<const TransformInfo*> StringMapType;
+  StringMapType StringInfoMap;
+
+  typedef std::vector<TransformRegistrationListener*> ListenerVec;
+  ListenerVec Listeners;
+};
+
+TransformRegistry *TransformRegistry::getRegistry() {
+  return &*TheRegistry;
+}
+
+void TransformRegistry::registerTransform(const TransformInfo &TI) {
+  if (Impl->StringInfoMap.find(TI.getName()) !=
+      Impl->StringInfoMap.end()) {
+    assert(!"Transform registered multiple times!");
+  }
+  Impl->StringInfoMap[TI.getName()] = &TI;
+
+  for (TransformRegistryImpl::ListenerVec::iterator
+       I = Impl->Listeners.begin();
+       I != Impl->Listeners.end(); ++I) {
+    (*I)->notifyRegistered(&TI);
+  }
+}
+
+void TransformRegistry::enumerateWith(TransformRegistrationListener *L) {
+  for (TransformRegistryImpl::StringMapType::const_iterator
+       I = Impl->StringInfoMap.begin();
+       I != Impl->StringInfoMap.end(); ++I) {
+    L->enumerateTransform(I->getValue());
+  }
+}
+
+void TransformRegistry::addRegistrationListener(
+                                             TransformRegistrationListener *L) {
+  Impl->Listeners.push_back(L);
+}
+
+void TransformRegistry::removeRegistrationListener(
+                                             TransformRegistrationListener *L) {
+  if (!Impl)
+    return;
+
+  TransformRegistryImpl::ListenerVec::iterator I =
+    std::find(Impl->Listeners.begin(), Impl->Listeners.end(), L);
+  assert(I != Impl->Listeners.end() &&
+         "TranformRegistrationListener not registered!");
+  Impl->Listeners.erase(I);
+}
Index: cpp11-migrate/TransformRegistry.h
===================================================================
--- /dev/null
+++ cpp11-migrate/TransformRegistry.h
@@ -0,0 +1,46 @@
+//===-- cpp11-migrate/TransformRegistry.h - Registration Def'n --*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides the definition for TransformRegistry.h.
+///
+//===----------------------------------------------------------------------===//
+#ifndef _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_REGISTRY_H_
+#define _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_REGISTRY_H_
+
+class TransformInfo;
+class TransformRegistryImpl;
+class TransformRegistrationListener;
+
+class TransformRegistry {
+  TransformRegistryImpl *Impl;
+
+public:
+
+  /// Returns a pointer to the global TransformRegistry.
+  ///
+  /// No longer valid after llvm_shutdown.
+  static TransformRegistry* getRegistry();
+
+  /// Register a Transform with the registry.
+  void registerTransform(const TransformInfo &TI);
+
+  /// Call the provided listener's enumerateTransform() member function for
+  /// each registered Transform.
+  void enumerateWith(TransformRegistrationListener *L);
+
+  /// Register a listener to be notified via its notifyRegistered() member
+  /// function when a new Transform is registered.
+  void addRegistrationListener(TransformRegistrationListener *L);
+
+  /// Cause a listener to no longer be notified when new Transforms are
+  /// registered.
+  void removeRegistrationListener(TransformRegistrationListener *L);
+};
+
+#endif // _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_REGISTRY_H_
Index: cpp11-migrate/TransformSupport.h
===================================================================
--- /dev/null
+++ cpp11-migrate/TransformSupport.h
@@ -0,0 +1,65 @@
+//===-- cpp11-migrate/TransformSupport.h - Transform registration*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// \brief This file provides definitions for classes involved with transform
+/// registration.
+///
+//===----------------------------------------------------------------------===//
+#ifndef _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_SUPPORT_H_
+#define _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_SUPPORT_H_
+
+#include "TransformRegistry.h"
+
+class Transform;
+
+class TransformInfo {
+public:
+  typedef Transform* (*TransformCreator)();
+
+public:
+  TransformInfo(const char *Name, const char *Desc, TransformCreator Creator)
+    : Name(Name), Desc(Desc), Creator(Creator) {}
+
+  const char *getName() const { return Name; }
+  const char *getDesc() const { return Desc; }
+  TransformCreator getCreator() const { return Creator; }
+
+
+private:
+  const char *Name;
+  const char *Desc;
+  TransformCreator Creator;
+};
+
+template <typename TransformType>
+Transform* createTransform() { return new TransformType(); }
+
+template <typename TransformType>
+class RegisterTransform : public TransformInfo {
+public:
+  RegisterTransform(const char *Name)
+    : TransformInfo(Name, &createTransform<TransformType>) {
+    TransformRegistry::getRegistry()->registerTransform(*this);
+  }
+};
+
+class TransformRegistrationListener {
+public:
+  TransformRegistrationListener();
+
+  virtual ~TransformRegistrationListener();
+
+  virtual void notifyRegistered(const TransformInfo *) {}
+
+  void enumerateTransforms();
+
+  virtual void enumerateTransform(const TransformInfo *) {}
+};
+
+#endif // _LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_TRANSFORM_SUPPORT_H_
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to