[asterisk-scf-commits] asterisk-scf/release/ice.git branch "visitor-generators" updated.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Thu Jan 6 22:21:48 UTC 2011


branch "visitor-generators" has been updated
       via  3f6f12b9137aef4f937bbb42e3c3256cf23d3968 (commit)
      from  80ee5f1875885581ad24e4982a96742b519938cb (commit)

Summary of changes:
 cpp/include/Slice/PythonUtil.h |    3 +-
 cpp/src/Slice/PythonUtil.cpp   |   94 +++++++++++++++++++++++++++++++++++-----
 cpp/src/slice2py/Main.cpp      |    6 ++-
 py/modules/IcePy/Slice.cpp     |    2 +-
 4 files changed, 91 insertions(+), 14 deletions(-)


- Log -----------------------------------------------------------------
commit 3f6f12b9137aef4f937bbb42e3c3256cf23d3968
Author: Kevin P. Fleming <kpfleming at digium.com>
Date:   Thu Jan 6 16:21:21 2011 -0600

    slice2py now supports plugins.
    
    The Python code generator now knows how to emit GeneratedOperations.

diff --git a/cpp/include/Slice/PythonUtil.h b/cpp/include/Slice/PythonUtil.h
index bd6bab1..a104de9 100644
--- a/cpp/include/Slice/PythonUtil.h
+++ b/cpp/include/Slice/PythonUtil.h
@@ -21,7 +21,8 @@ namespace Python
 //
 // Generate Python code for a translation unit.
 //
-SLICE_API void generate(const Slice::UnitPtr&, bool, bool, const std::vector<std::string>&, IceUtilInternal::Output&);
+SLICE_API void generate(const Slice::UnitPtr&, bool, bool, const std::vector<std::string>&, IceUtilInternal::Output&,
+                        const std::vector<std::string>&);
 
 //
 // Convert a scoped name into a Python name.
diff --git a/cpp/src/Slice/PythonUtil.cpp b/cpp/src/Slice/PythonUtil.cpp
index 08c30f4..8cf0296 100644
--- a/cpp/src/Slice/PythonUtil.cpp
+++ b/cpp/src/Slice/PythonUtil.cpp
@@ -10,9 +10,11 @@
 #include <Slice/PythonUtil.h>
 #include <Slice/Checksum.h>
 #include <Slice/Util.h>
+#include <Slice/Plugin.h>
 #include <IceUtil/IceUtil.h>
 #include <IceUtil/StringUtil.h>
 #include <IceUtil/InputUtil.h>
+#include <IceUtil/DynamicLibrary.h>
 #include <climits>
 #include <iterator>
 
@@ -409,7 +411,9 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
     ClassList bases = p->bases();
     ClassDefPtr base;
     OperationList ops = p->operations();
-    OperationList::iterator oli;
+    OperationList::const_iterator oli;
+    GeneratedOperationList gops = p->generatedOperations();
+    GeneratedOperationList::const_iterator goli;
     bool isAbstract = p->isInterface() || p->allOperations().size() > 0; // Don't use isAbstract() - see bug 3739
 
     //
@@ -480,7 +484,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
         if(base)
         {
             _out << nl << getSymbol(base) << ".__init__(self";
-            for(MemberInfoList::iterator q = allMembers.begin(); q != allMembers.end(); ++q)
+            for(MemberInfoList::const_iterator q = allMembers.begin(); q != allMembers.end(); ++q)
             {
                 if(q->inherited)
                 {
@@ -489,7 +493,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
             }
             _out << ')';
         }
-        for(MemberInfoList::iterator q = allMembers.begin(); q != allMembers.end(); ++q)
+        for(MemberInfoList::const_iterator q = allMembers.begin(); q != allMembers.end(); ++q)
         {
             if(!q->inherited)
             {
@@ -526,7 +530,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
         _out << sp << nl << "def ice_ids(self, current=None):";
         _out.inc();
         _out << nl << "return (";
-        for(StringList::iterator q = ids.begin(); q != ids.end(); ++q)
+        for(StringList::const_iterator q = ids.begin(); q != ids.end(); ++q)
         {
             if(q != ids.begin())
             {
@@ -569,7 +573,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
 
                 ParamDeclList params = (*oli)->parameters();
 
-                for(ParamDeclList::iterator pli = params.begin(); pli != params.end(); ++pli)
+                for(ParamDeclList::const_iterator pli = params.begin(); pli != params.end(); ++pli)
                 {
                     if(!(*pli)->isOutParam())
                     {
@@ -596,7 +600,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
 
                 ParamDeclList params = (*oli)->parameters();
 
-                for(ParamDeclList::iterator pli = params.begin(); pli != params.end(); ++pli)
+                for(ParamDeclList::const_iterator pli = params.begin(); pli != params.end(); ++pli)
                 {
                     if(!(*pli)->isOutParam())
                     {
@@ -620,6 +624,48 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
         }
     }
 
+    if(!gops.empty())
+    {
+        //
+        // Emit each GeneratedOperation.
+        //
+        for(goli = gops.begin(); goli != gops.end(); ++goli)
+        {
+            string fixedOpName = fixIdent((*goli)->name());
+            _out << sp << nl << "def " << fixedOpName << "(self";
+
+            ParamDeclList params = (*goli)->parameters();
+
+            for(ParamDeclList::const_iterator pli = params.begin(); pli != params.end(); ++pli)
+            {
+                if(!(*pli)->isOutParam())
+                {
+                    _out << ", " << fixIdent((*pli)->name());
+                }
+            }
+            _out << "):";
+            _out.inc();
+            if((*goli)->isAbstract())
+            {
+                _out << nl << "pass";
+            }
+            else
+            {
+                StringList body = (*goli)->body();
+                for(StringList::const_iterator it = body.begin(); it != body.end(); it++)
+                {
+                    _out << nl << *it;
+                }
+            }
+            comment = (*goli)->comment();
+            if(!comment.empty())
+            {
+                _out << nl << "'''" << editComment(comment) << "'''";
+            }
+            _out.dec();
+        }
+    }
+
     //
     // __str__
     //
@@ -842,7 +888,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
         _out << nl;
     }
     bool isProtected = p->hasMetaData("protected");
-    for(DataMemberList::iterator r = members.begin(); r != members.end(); ++r)
+    for(DataMemberList::const_iterator r = members.begin(); r != members.end(); ++r)
     {
         if(r != members.begin())
         {
@@ -885,10 +931,10 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
         {
             _out << sp;
         }
-        for(OperationList::iterator s = ops.begin(); s != ops.end(); ++s)
+        for(OperationList::const_iterator s = ops.begin(); s != ops.end(); ++s)
         {
             ParamDeclList params = (*s)->parameters();
-            ParamDeclList::iterator t;
+            ParamDeclList::const_iterator t;
             int count;
 
             _out << nl << name << "._op_" << (*s)->name() << " = IcePy.Operation('" << (*s)->name() << "', "
@@ -949,7 +995,7 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
             }
             _out << ", (";
             ExceptionList exceptions = (*s)->throws();
-            for(ExceptionList::iterator u = exceptions.begin(); u != exceptions.end(); ++u)
+            for(ExceptionList::const_iterator u = exceptions.begin(); u != exceptions.end(); ++u)
             {
                 if(u != exceptions.begin())
                 {
@@ -2194,11 +2240,37 @@ Slice::Python::CodeVisitor::editComment(const string& comment)
 }
 
 void
-Slice::Python::generate(const UnitPtr& un, bool all, bool checksum, const vector<string>& includePaths, Output& out)
+Slice::Python::generate(const UnitPtr& un, bool all, bool checksum, const vector<string>& includePaths, Output& out,
+                        const vector<string>& plugins)
 {
     Slice::Python::MetaDataVisitor visitor;
     un->visit(&visitor, false);
 
+    for(vector<string>::const_iterator plugin = plugins.begin(); plugin != plugins.end(); plugin++)
+    {
+        IceUtil::DynamicLibraryPtr library = new IceUtil::DynamicLibrary();
+        IceUtil::DynamicLibrary::symbol_type sym = library->loadEntryPoint(*plugin, false);
+        if(sym == 0)
+        {
+            string msg = library->getErrorMessage();
+            cerr << "Unable to find " << *plugin << " = " << msg << endl;
+        }
+        
+        Plugin::VisitorList* pluginVisitors;
+        Plugin::FACTORY factory = (Plugin::FACTORY) sym;
+        pluginVisitors = factory(Plugin::LanguagePython);
+        
+        if(pluginVisitors)
+        {
+            for(Plugin::VisitorList::const_iterator it = pluginVisitors->begin(); it != pluginVisitors->end(); it++)
+            {
+                un->visit(*it, true);
+                delete *it;
+            }
+            delete pluginVisitors;
+        }
+    }
+
     out << nl << "import Ice, IcePy, __builtin__";
 
     if(!all)
diff --git a/cpp/src/slice2py/Main.cpp b/cpp/src/slice2py/Main.cpp
index 0eed265..e31e8a8 100644
--- a/cpp/src/slice2py/Main.cpp
+++ b/cpp/src/slice2py/Main.cpp
@@ -378,6 +378,7 @@ usage(const char* n)
         "--all                Generate code for Slice definitions in included files.\n"
         "--checksum           Generate checksums for Slice definitions.\n"
         "--prefix PREFIX      Prepend filenames of Python modules with PREFIX.\n"
+        "--plugin PATH            Load and apply plugin before generating output.\n"
         ;
 }
 
@@ -400,6 +401,7 @@ compile(int argc, char* argv[])
     opts.addOpt("", "no-package");
     opts.addOpt("", "checksum");
     opts.addOpt("", "prefix", IceUtilInternal::Options::NeedArg);
+    opts.addOpt("", "plugin", IceUtilInternal::Options::NeedArg, "", IceUtilInternal::Options::Repeat);
 
     vector<string> args;
     try
@@ -445,6 +447,8 @@ compile(int argc, char* argv[])
         cppArgs.push_back("-I" + Preprocessor::normalizeIncludePath(*i));
     }
 
+    vector<string> plugins = opts.argVec("plugin");
+
     bool preprocess = opts.isSet("E");
 
     string output = opts.optArg("output-dir");
@@ -597,7 +601,7 @@ compile(int argc, char* argv[])
                         //
                         // Generate the Python mapping.
                         //
-                        generate(u, all, checksum, includePaths, out);
+                        generate(u, all, checksum, includePaths, out, plugins);
     
                         out.close();
 
diff --git a/py/modules/IcePy/Slice.cpp b/py/modules/IcePy/Slice.cpp
index b0d33be..41daf98 100644
--- a/py/modules/IcePy/Slice.cpp
+++ b/py/modules/IcePy/Slice.cpp
@@ -160,7 +160,7 @@ IcePy_loadSlice(PyObject* /*self*/, PyObject* args)
         ostringstream codeStream;
         IceUtilInternal::Output out(codeStream);
         out.setUseTab(false);
-        generate(u, all, checksum, includePaths, out);
+        generate(u, all, checksum, includePaths, out, vector<string>());
         u->destroy();
 
         string code = codeStream.str();

-----------------------------------------------------------------------


-- 
asterisk-scf/release/ice.git



More information about the asterisk-scf-commits mailing list