[cfe-dev] C++ to Python Project Proposal

Jeff Kunkel jdkunk3 at gmail.com
Sat Nov 6 13:27:21 PDT 2010


Out of code here is what and how I wish to generate.

Start: { Push( Module( arg_input_name ) ) } Global { Pop() }

Global:
   | Namespace { Push( new Module(Top, $1.name) ) } Global { Pop(); }
   | Object { Push( new Object(Top, $1.name) ) } ObjectElements { Pop(); }
   | Function { Top.GenerateFunction( $1 ) }
   | Enum Enumerations { for_each( e in $2 ) Top.GenerateVariable( e ) }
   | Variable { Top.GenerateVariable( $1 ) }

ObjectElements:
   | Methods { Top.GenerateMethod( $1 ) }
   | Variable { Top.GenerateVariable( $1 ) }

// Here is my start to an implementation

static std::stack<Base*> STACK;
Push( Base * base ) { STACK.push(base); }
Pop() { STACK.top().finish(); STACK.pop(); }

struct Base {
   static std::vector<std::stringstream> modules;
   static std::ostream & createNewStream() {
      modules.push_back( std::stringstream() );
      std::ostream & out = modules.back();
      out << HEADERS;
   }

  virutal std::ostream & getStream() = 0;
  virtual void GenerateFunction( clang::FunctionDecl * ) { throw new
std::exception("Function is not implemented."); }
  virtual void GenerateObject( clang::ObjectDecl * ) { throw new
std::exception("Function is not implemented."); }
  virtual void GenerateVariable( clang::VariableDecl * ) { throw new
std::exception("Function is not implemented."); }
  virtual void GenerateMethod( clang::FuctionDecl * ) { throw new
std::exception("Function is not implemented."); }
  virtual void finish();
};


struct Module : Base {

   std::string name;
   std::ostream & out;
   virutal std::ostream & getStream() { return out; }

   Module( const Module * createdFrom, std::string NamespaceName ) :
out( createNewStream() ) {
     this->name = createdFrom.name;
     this->name.append(".").append(name);

     out << "#include<boost\python.hpp>" << std::endl;
out << "BOOST_PYTHON_MODULE("<<this->name<<"){"<< std::endl;
out << "using namespace boost::python;"<< std::endl;
   }
   void GenerateFunction( clang::FunctionDecl * fn ) {
      if( basic(fn) )
        out <<
"def(\""<<fn->getName()<<"\","<<fn->getName()<<");"<<std::endl;
      // Add descriptors from the function.
      // see
http://www.boost.org/doc/libs/1_44_0/libs/python/doc/v2/reference.html#models_of_call_policies
   }
   void GenerateVariable( clang::VariableDecl * decl ) {
      if( basic(decl) )
        out <<
"def(\""<<decl->getName()<<"\","<<decl->getName()<<");"<<std::endl;
      // FIXME: Add more variable properties.
   }
   void GenerateObject( clang::ObjectDecl * obj ) {
      out << ";" << std::endl;
   }

   void * operator new() { std::allocator( /* I forget how exactly to do
this */ ).alloc( sizeof(Module) ); }
   void delete() { /*... I forget how to use the std::allocator again. */ }
};

struct Object : Base {

   std::ostream & out;
   // Since we cannot rely on all the constructors being seen until the
whole object has been finalized.

   Object( Object * obj ) : out( createNewStream() ) {
     out << /* output object header definition. */ ;
     // Something like:
     // class_<World>("World", /*add constructors as they are seen.*/ init<
std::string>())
   }

  void GenerateMethod( clang::FuctionDecl * fn );
  void GenerateVariable( clang::VariableDecl * var );
  void GenerateObject( clang::ObjectDecl * obj );
  void finish() {
    if( ! haveBody() )
      ss.clear();
  }

   void * operator new() { std::allocator( /* I forget how exactly to do
this */ ).alloc( sizeof(Module) ); }
   void delete() { /*... I forget how to use the std::allocator again. */ }
};

On Sat, Nov 6, 2010 at 9:25 AM, Jeff Kunkel <jdkunk3 at gmail.com> wrote:

> Has anyone tried to build and extend LLVM and Clang with SWIG?
>
> - Thanks
> - Jeff Kunkel
>
>
> On Sat, Nov 6, 2010 at 8:21 AM, Rolf Banting <rolf.b.mr at gmail.com> wrote:
>
>>
>>
>> On Fri, Nov 5, 2010 at 7:56 PM, Jeff Kunkel <jdkunk3 at gmail.com> wrote:
>>
>>> I would like to use Clang to take many C++ classes and wrap them with the
>>> Boost::Python for use with python code.
>>>
>>> Does anyone have any advice?
>>> Has this been done already?
>>>
>>>
>>>  Does SWIG (swig.org) not do what you want?
>>
>> Rolf
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20101106/c3052bf4/attachment.html>


More information about the cfe-dev mailing list