generator: _c_accessor_get_length: remove buggy special case
[free-sw/xcb/libxcb] / src / c_client.py
index 409054b..88321f4 100644 (file)
@@ -552,18 +552,22 @@ def _c_field_is_member_of_case_or_bitcase(field):
 def _c_helper_fieldaccess_expr(prefix, field=None):
     """
     turn prefix, which is a list of tuples (name, separator, Type obj) into a string
-    representing a valid name in C (based on the context)
-    if field is not None, append the field name as well
+    representing a valid field-access-expression in C (based on the context)
+    if field is not None, append access to the field as well.
+
+    "separator" is one of the C-operators "." or "->".
+
+    A field access expression can consist of the following components:
+    * struct/union member access from a value with the "."-operator
+    * struct/union member access from a pointer with "->"-operator
+    * function-call of an accessor function:
+      This is used when a xcb-field is not contained in a struct.
+      This can, e.g., happen for fields after var-sized fields, etc.
     """
     prefix_str = ''
     last_sep =''
     for name, sep, obj in prefix:
         prefix_str += last_sep + name
-        if '' == sep:
-            sep = '->'
-            if ((obj.is_case_or_bitcase and obj.has_name) or     # named bitcase
-                (obj.is_switch and len(obj.parents)>1)):
-                sep = '.'
         last_sep = sep
 
     if field is None:
@@ -1586,12 +1590,7 @@ def _c_accessor_get_length(expr, field_mapping=None):
         if field_mapping is not None:
             lenfield_name = field_mapping[lenfield_name][0]
 
-    if expr.lenfield is not None and expr.lenfield.prev_varsized_field is not None:
-        # special case: variable and fixed size fields are intermixed
-        # if the lenfield is among the fixed size fields, there is no need
-        # to call a special accessor function like <expr.lenfield.c_accessor_name + '(' + prefix + ')'>
-        return field_mapping(expr.lenfield_name)
-    elif expr.lenfield_name is not None:
+    if expr.lenfield_name is not None:
         return lenfield_name
     else:
         return str(expr.nmemb)
@@ -3071,7 +3070,18 @@ def c_event(self, name):
                 force_packed = any(f.type.size == 8 and f.type.is_simple for f in self.fields[(idx+1):])
                 break
 
-    _c_type_setup(self, name, ('event',))
+    if self.name == name:
+        _c_type_setup(self, name, ('event',))
+        # generate accessors
+        # (needed for fields after var-sized fields, for lists with var-sized elements,
+        # switches, ...)
+        _c_accessors(self, name, name)
+    else:
+        # no type-setup needed for eventcopies
+        # (the type-setup of an eventcopy would overwrite members of the original
+        # event, and it would create sizeof-etc funtions which
+        # called undefined accessor functions)
+        pass
 
     # Opcode define
     _c_opcode(name, self.opcodes[name])
@@ -3084,6 +3094,22 @@ def c_event(self, name):
         _h('')
         _h('typedef %s %s;', _t(self.name + ('event',)), _t(name + ('event',)))
 
+        # Create sizeof-function for eventcopies for compatibility reasons
+        if self.c_need_sizeof:
+            _h_setlevel(1)
+            _c_setlevel(1)
+            _h('')
+            _h('int')
+            _h('%s (const void  *_buffer  /**< */);', _n(name + ('sizeof',)))
+            _c('')
+            _c('int')
+            _c('%s (const void  *_buffer  /**< */)', _n(name + ('sizeof',)))
+            _c('{');
+            _c('    return %s(_buffer);', _n(self.name + ('sizeof',)))
+            _c('}');
+            _h_setlevel(0)
+            _c_setlevel(0)
+
     _man_event(self, name)
 
 def c_error(self, name):