Don't use enums in generated C code - use integer constants instead.
[free-sw/xcb/libxcb] / src / c_client.py
index 7bcf5e7..299ad54 100755 (executable)
@@ -1,7 +1,8 @@
 #!/usr/bin/env python
 from xml.etree.cElementTree import *
-from sys import argv
 from os.path import basename
+import getopt
+import sys
 import re
 
 # Jump to the bottom of this file for the main routine
@@ -16,18 +17,6 @@ _cplusplus_annoyances = {'class' : '_class',
                          'new'   : '_new',
                          'delete': '_delete'}
 
-_cardinal_types = ['CARD8', 'uint8_t',
-                   'CARD16','uint16_t',
-                   'CARD32','uint32_t',
-                   'INT8', 'int8_t',
-                   'INT16', 'int16_t',
-                   'INT32', 'int32_t',
-                   'BYTE',
-                   'BOOL',
-                   'char',
-                   'void',
-                   'float',
-                   'double']
 _hlines = []
 _hlevel = 0
 _clines = []
@@ -175,6 +164,12 @@ def c_open(self):
         for (n, h) in self.imports:
             _hc('#include "%s.h"', h)
 
+    _h('')
+    _h('#ifdef __cplusplus')
+    _h('extern "C" {')
+    _h('#endif')
+
+    if _ns.is_ext:
         _h('')
         _h('#define XCB_%s_MAJOR_VERSION %s', _ns.ext_name.upper(), _ns.major_version)
         _h('#define XCB_%s_MINOR_VERSION %s', _ns.ext_name.upper(), _ns.minor_version)
@@ -182,7 +177,7 @@ def c_open(self):
         _h('extern xcb_extension_t %s;', _ns.c_ext_global_name)
 
         _c('')
-        _c('xcb_extension_t %s = { "%s" };', _ns.c_ext_global_name, _ns.ext_xname)
+        _c('xcb_extension_t %s = { "%s", 0 };', _ns.c_ext_global_name, _ns.ext_xname)
 
 def c_close(self):
     '''
@@ -192,6 +187,12 @@ def c_close(self):
     _h_setlevel(2)
     _c_setlevel(2)
     _hc('')
+
+    _h('')
+    _h('#ifdef __cplusplus')
+    _h('}')
+    _h('#endif')
+
     _h('')
     _h('#endif')
     _h('')
@@ -221,17 +222,15 @@ def c_enum(self, name):
     '''
     _h_setlevel(0)
     _h('')
-    _h('typedef enum %s {', _t(name))
 
-    count = len(self.values)
+    next = -1
 
     for (enam, eval) in self.values:
-        count = count - 1
-        equals = ' = ' if eval != '' else ''
-        comma = ',' if count > 0 else ''
-        _h('    %s%s%s%s', _n(name + (enam,)).upper(), equals, eval, comma)
-
-    _h('} %s;', _t(name))
+        if eval == '':
+            next += 1
+        else:
+            next = int(eval)
+        _h('static const uint32_t %s = %d;', _n(name + (enam,)).upper(), next)
 
 def _c_type_setup(self, name, postfix):
     '''
@@ -306,7 +305,7 @@ def _c_iterator_get_end(field, accum):
         return _c_iterator_get_end(field.type.last_varsized_field, accum)
     if field.type.is_list:
         # XXX we can always use the first way
-        if field.type.c_type in _cardinal_types:
+        if field.type.member.is_simple:
             return field.c_end_name + '(' + accum + ')'
         else:
             return field.type.member.c_end_name + '(' + field.c_iterator_name + '(' + accum + '))'
@@ -442,7 +441,7 @@ def _c_accessors_field(self, field):
     '''
     Declares the accessor functions for a non-list field that follows a variable-length field.
     '''
-    if field.field_type[0] in _cardinal_types:
+    if field.type.is_simple:
         _hc('')
         _hc('')
         _hc('/*****************************************************************************')
@@ -534,7 +533,7 @@ def _c_accessors_list(self, field):
     _c('    return %s;', _c_accessor_get_expr(field.type.expr, 'R'))
     _c('}')
 
-    if field.field_type[0] in _cardinal_types:
+    if field.type.member.is_simple:
         _hc('')
         _hc('')
         _hc('/*****************************************************************************')
@@ -634,9 +633,15 @@ def _c_complex(self):
     struct_fields = []
     maxtypelen = 0
 
+    varfield = None
     for field in self.fields:
         if not field.type.fixed_size():
-            break
+            varfield = field.c_field_name
+            continue
+        if varfield != None and not field.type.is_pad and field.wire:
+            errmsg = '%s: warning: variable field %s followed by fixed field %s\n' % (self.c_type, varfield, field.c_field_name)
+            sys.stderr.write(errmsg)
+            # sys.exit(1)
         if field.wire:
             struct_fields.append(field)
         
@@ -982,6 +987,18 @@ output = {'open'    : c_open,
 
 # Boilerplate below this point
 
+# Check for the argument that specifies path to the xcbgen python package.
+try:
+    opts, args = getopt.getopt(sys.argv[1:], 'p:')
+except getopt.GetoptError, err:
+    print str(err)
+    print 'Usage: c_client.py [-p path] file.xml'
+    sys.exit(1)
+
+for (opt, arg) in opts:
+    if opt == '-p':
+        sys.path.append(arg)
+
 # Import the module class
 try:
     from xcbgen.state import Module
@@ -989,13 +1006,14 @@ except ImportError:
     print ''
     print 'Failed to load the xcbgen Python package!'
     print 'Make sure that xcb/proto installed it on your Python path.'
-    print 'If not, you will need to create a .pth file to extend the path.'
+    print 'If not, you will need to create a .pth file or define $PYTHONPATH'
+    print 'to extend the path.'
     print 'Refer to the README file in xcb/proto for more info.'
     print ''
     raise
 
 # Parse the xml header
-module = Module(argv[1], output)
+module = Module(args[0], output)
 
 # Build type-registry and resolve type dependencies
 module.register()