omniidl — The omniORB IDL CompilerDuncan Grisby |
This manual describes omniidl, the omniORB IDL compiler. It is intended for developers who wish to write their own IDL compiler back-ends, or to modify existing ones.
If you just wish to use omniidl to create stubs for C++ or Python, you should read the omniORB or omniORBpy manuals instead of this one.
Back-ends for omniidl are written in Python, so to use it you must have an up-to-date Python interpreter. You must also understand Python to be able to follow this manual and write back-ends. You can download Python and associated documentation from http://www.python.org/.
The front-end scanner and parser are written using flex and bison; the rest of the front-end is written in C++. The code intentionally avoids using any advanced (and useful) features of C++, such as templates, so as to make it as portable as possible.
On all platforms, there is a command named omniidl. On Unix
platforms, omniidl is a Python script which runs Python via the
#!
mechanism. On Windows NT, there is an executable named
omniidl.exe.
The omniidl command line has the form:
omniidl [options] -b<back-end> [back-end options] <file 1> <file 2> …
The supported flags are:
-Dname[=value] | Define name for the preprocessor. |
-Uname | Undefine name for the preprocessor. |
-Idir | Include dir in the preprocessor search path. |
-E | Only run the preprocessor, sending its output to stdout. |
-Ycmd | Use cmd as the preprocessor, rather than the normal C preprocessor. |
-N | Do not run the preprocessor. |
-T | Use a temporary file, not a pipe, for preprocessor output. |
-Wparg[,arg…] | Send arguments to the preprocessor. |
-bback-end | Run the specified back-end. For the C++ ORB, use -bcxx. |
-Wbarg[,arg…] | Send arguments to the back-end. |
-nf | Do not warn about unresolved forward declarations. |
-k | Keep comments after declarations, to be used by some back-ends. |
-K | Keep comments before declarations, to be used by some back-ends. |
-Cdir | Change directory to dir before writing output files. |
-i | Run the front end and back-ends, then enter the interactive loop. |
-d | Dump the parsed IDL then exit, without running a back-end. |
-pdir | Use dir as a path to find omniidl back-ends. |
-V | Print version information then exit. |
-u | Print usage information. |
-v | Verbose: trace compilation stages. |
If you do not specify any back-ends (with the -b flag), omniidl just runs the compiler front-end, checking that the IDL is valid. If you specify more than one back-end, the back-ends are run in turn on the abstract syntax tree of each file. This permits you to generate stubs for more than one language in a single run. It also permits you to write back-ends which annotate or modify the abstract syntax tree to be used by later back-ends.
For example, the command:
omniidl -bdump -bpython foo.idl bar.idl
first reads and parses foo.idl, and runs the dump and python back-ends on it in turn. Then it reads and parses bar.idl and runs the two back-ends on that.
IDL is processed by the C preprocessor before omniidl parses it. omniidl always uses the GNU C preprocessor (which it builds with the name omnicpp). The -D, -U, and -I options are just sent to the preprocessor. Note that the current directory is not on the include search path by default—use ‘-I.’ for that. The -Y option can be used to specify a different preprocessor to omnicpp. Beware that line directives inserted by other preprocessors are likely to confuse omniidl.
If you have an IDL file like:
then omniidl will normally issue a warning:
test.idl:1: Warning: Forward declared interface `::I' was never
fully defined
It is illegal to declare such IDL in isolation, but it is valid to define interface I in a separate file. If you have a lot of IDL with this sort of construct, you will drown under the warning messages. Use the -nf option to suppress them.
By default, omniidl discards comments in the input IDL. However, with the -k and -K options, it preserves the comments for use by the back-ends.
The two different options relate to how comments are attached to declarations within the IDL. Given IDL like:
the -k flag will attach the comment to op1(); the -K flag will attach it to op2().
When omniidl is given the -i option, it runs the compiler front-end and any back-ends specified, and then drops into Python’s interactive command loop. Within the interactive loop, you can import omniidl. The parsed AST is then available as omniidl.idlast.tree. This mode is useful for investigating the parsed tree.
All parts of omniidl are licensed under the GNU General Public License, available in the file COPYING.
As a special exception to the terms of the GPL, we do not consider back-ends to be derived works of omniidl. This means that you may distribute back-ends you write under any terms you like. The back-ends we distribute are licensed under the GPL, so you must abide by its terms if you distribute or modify our back-ends.
As another exception, we do not consider the output of the back-ends we distribute to be derived works of those back-ends. You may therefore use generated stubs with no restrictions.
There are three elements to the back-end interface: requirements on the back-end modules themselves, a set of output and utility functions, and the interface to the parsed IDL.
omniidl back-ends are just normal Python modules. When you specify a back-end with -bfoo, omniidl first tries to open the Python module named omniidl_be.foo. If that fails, it tries to open the module just named foo, using the normal PYTHONPATH mechanism. As with any Python module, the module foo can either be implemented as a single file named foo.py, or as a directory foo containing a file named __init__.py.
The only requirement on back-end modules is that they contain a function with the signature run(tree, args), where tree is an AST object as described in section 2.3.3, and args is a list of argument strings passed to the back-end.
Back-ends may also optionally provide a variable named cpp_args which contains a list of strings containing arguments to be given to the C preprocessor. For example, the Python back-end contains the line:
The purpose of most back-ends is to output source code in some language. It is often the case that much of the output is independent of the specifics of the IDL input. The output for an IDL interface, for example, might be an extensive class definition containing configuration and initialisation code which is largely independent of the specifics of the interface. At various places throughout the class definition, there would be items which were dependent on the interface definition.
omniidl supports this with template based output functions. Templates are simply strings containing the code to be output, including expressions surrounded by ‘@’ characters. When the templates are output, the keys inside the ‘@’ expressions are replaced with values according to the output arguments. An ‘@’ symbol can be output by putting ‘@@’ in the template.
The output facilities are provided in the omniidl.output module by the Stream class. The primary method of Stream objects is out(), which takes arguments of a template string and a set of key/value pairs to be used in @ substitutions. For example, if st is a Stream object, then the code:
would result in output:
When @ expressions are substituted, the expression is actually evaluated, not just textually replaced. This means that you can write templates containing strings like ‘@obj.name()@’. Expressions must evaluate to strings. This feature should not be over-used—it is very easy to write incomprehensible template expressions. The vast majority of templates should only use simple string substitutions.
Commonly, it is necessary to nest definitions which are output inside other definitions. Stream objects keep track of a current indentation level to aid this. The methods inc_indent() and dec_indent() increment and decrement the current indent level respectively. The number of spaces corresponding to a single indent level is configured when the Stream is created. Occasionally, you may need to output code which ignores the current indent level (preprocessor directives in C, for example). The niout() method is identical to out() except that it performs no indentation.
The Stream constructor takes two arguments, a file opened for writing, and an integer specifying how many spaces to use for each indent level.
The omniidl.idlutil module contains a number of useful functions:
>>> pruneScope(['A','B','C','D'],['A','B','D']) ['C','D']
The main meat of the back-end interface is in the omniidl.idlast and omniidl.idltype modules. When the compiler parses an IDL file, it creates a tree of objects representing the IDL declarations. The classes for these declarations are defined in the idlast module. The way an IDL declaration is split into objects closely follows the terms within the IDL grammar presented in chapter 3 of the CORBA 2.3 specification.
All objects within the back-end interface support the visitor pattern. They have an accept(visitor) method which acts on a visitor adhering to the interfaces in the omniidl.idlvisitor module. Note that Python’s dynamic type system means that visitor objects need not actually derive from the classes defined in idlvisitor1. Also note that you do not have to use the visitor pattern if you do not wish to.
Any unknown #pragmas encountered in the IDL are attached to nodes within the AST. Similarly, comments are attached if omniidl is run with the -k or -K fields.
The back-end’s run() function (described in section 2.1) is passed an object of class AST.
All declarations in the tree are derived from the Decl class:
Some classes of declaration object also inherit from the DeclRepoId mixin class:
The declaration objects making up the tree have the following classes:
typedef long MyLong; const MyLong foo = 123;constKind() will return tk_long, but constType() will return an idltype.Declared object (see page ??) which refers to MyLong’s typedef Declarator object.
typedef struct foo { long l; } bar;
struct S { struct T { long l; } the_T; };
struct S { long l; sequence <S> ss; };
attribute long a, b;
identifiers() will return [’a’,’b’].
No non-inherited functions.
All type objects are derived from the base class Type:
The basic CORBA types (null, void, short, long, unsigned short, unsigned long, float, double, boolean, char, octet, any, TypeCode, Principal, long long, unsigned long long, long double, and wide char) are represented by objects of type omniidl.idltype.Base, derived from Type, with no extra methods.
The template types—string, wstring, sequence, and fixed—do not have associated Decl objects since they are not explicitly declared. They are always implicitly declared as part of another declaration.
All other types (interface, struct, union, enum, typedef, exception, valuetype) must be explicitly declared. They are represented with Declared objects:
Normally, back-ends walk over the tree of Decl objects, dealing with the declarations as they encounter them. Occasionally, however, it may be useful to find a declaration by its scoped name. Only Decls which inherit from DeclRepoId can be found in this way.
The following code is an extremely simple back-end which just prints the names of all operations declared within an IDL file. Unfortunately, it is so simple that it does not show many features of the back-end interface. You should look at the dump.py and python.py back-ends for a more extensive example.
from omniidl import idlast, idlvisitor, idlutil import string class ExampleVisitor (idlvisitor.AstVisitor): def visitAST(self, node): for n in node.declarations(): n.accept(self) def visitModule(self, node): for n in node.definitions(): n.accept(self) def visitInterface(self, node): name = idlutil.ccolonName(node.scopedName()) if node.mainFile(): for c in node.callables(): if isinstance(c, idlast.Operation): print(name + "::" + c.identifier() + "()") def run(tree, args): visitor = ExampleVisitor() tree.accept(visitor)
The visitor object simply recurses through the AST and Module objects, and prints the operation names it finds in Interface objects.
Note that since AstVisitor (and similarly TypeVisitor which is not used in the example) has all operations declared to be no-ops, the ExampleVisitor class does not have to declare visit functions for all node types. This can be a disadvantage if your back-end is supposed to perform some action for all node types, since there will be no error if you accidentally miss a node type. In those situations it is better to declare a visitor class which does not derive from the visitor base classes.
This document was translated from LATEX by HEVEA.