[omniORB] How to write a IDL compiler back-end in C++?

Duncan Grisby dgrisby@uk.research.att.com
Tue, 20 Jun 2000 15:03:29 +0100


On Tuesday 20 June, Marcus Gruendler wrote:

> I would like to write my own IDL compiler back-end for the new OmniORB
> 3.0 IDL compiler. I would like to write this back-end in C++ - not in
> Python - is this possible?

Yes it is possible, although as it stands there are a couple of
problems in doing so. The first problem is that C++ is a pain, so it's
usually much easier to do back-end like things in Python, but I
suppose there are some circumstances in which it would make sense to
use C++.

The severity of the second problem depends on whether you mind having
Python involved or not. If you want to make an IDL compiler which is
pure C++, with no Python anywhere, then life is slightly harder than
if you keep some Python.

To see how to use omniidl without any Python, look at idlc.cc in the
src/tool/omniidl/cxx directory. It is a minimal C++ program which runs
the front-end and the "dump" C++ back-end directly. The file isn't
normally compiled, but the make rule for it is at the end of dir.mk,
commented out. idldump.h and idldump.cc implement the back-end; that
is the only example of a C++ back-end.

The biggest difficulty with this is that omniidl's command line
interface is implemented in Python. Forking the C pre-processor and
getting its output through a pipe are done from Python. The simple
idlc.cc example doesn't run the pre-processor or do anything with
command line arguments. If you want to avoid Python altogether, you'll
have to reimplement all of the things which are currently done by the
Python bits of front-end. That won't be too hard if you only have one
target platform, but it's an enormous pain if you want to support lots
of different platforms.

If you don't mind having Python around, it is reasonably easy to do
what you want. You can just make a Python extension module (which is
just a DLL with a little bit of boiler-plate stuff to register it with
Python), which looks like a normal back-end as far as omniidl is
concerned. Basically this means having a run() function which is
visible from Python. See "Extending and Embedding the Python
Interpreter", available from

  http://www.python.org/doc/current/ext/ext.html

The abstract syntax tree for the parsed IDL is accessible from the
static AST::tree() function, so it will be easy for your back-end to
get hold of the tree it needs. Or at least it would be easy, if it
wasn't for the fact that the current version of omniidl deletes the
C++ tree before the back-ends are executed. I've just checked in a
small modification which means it waits until after the back-ends have
run before deleting the C++ tree. Update from CVS, or wait for
tomorrow's FTP snapshot to get it.

Hope that helps. Let me know if you need any more details.

Cheers,

Duncan.

-- 
 -- Duncan Grisby  \  Research Engineer  --
  -- AT&T Laboratories Cambridge          --
   -- http://www.uk.research.att.com/~dpg1 --