2 This module contains the classes which represent XCB data types.
4 from expr import Field, Expression
9 Abstract base class for all XCB data types.
10 Contains default fields, and some abstract methods.
12 def __init__(self, name):
14 Default structure initializer. Sets up default fields.
17 name is a tuple of strings specifying the full type name.
18 size is the size of the datatype in bytes, or None if variable-sized.
19 nmemb is 1 for non-list types, None for variable-sized lists, otherwise number of elts.
20 booleans for identifying subclasses, because I can't figure out isinstance().
28 self.is_simple = False
31 self.is_container = False
36 def resolve(self, module):
38 Abstract method for resolving a type.
39 This should make sure any referenced types are already declared.
41 raise Exception('abstract resolve method not overridden!')
45 Abstract method for outputting code.
46 These are declared in the language-specific modules, and
47 there must be a dictionary containing them declared when this module is imported!
49 raise Exception('abstract out method not overridden!')
53 Abstract method for determining if the data type is fixed-size.
55 raise Exception('abstract fixed_size method not overridden!')
57 def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto):
59 Default method for making a data type a member of a structure.
60 Extend this if the data type needs to add an additional length field or something.
62 module is the global module object.
63 complex_type is the structure object.
64 see Field for the meaning of the other parameters.
66 new_field = Field(self, field_type, field_name, visible, wire, auto)
68 # We dump the _placeholder_byte if any fields are added.
69 for (idx, field) in enumerate(complex_type.fields):
70 if field == _placeholder_byte:
71 complex_type.fields[idx] = new_field
74 complex_type.fields.append(new_field)
76 class SimpleType(Type):
78 Derived class which represents a cardinal type like CARD32 or char.
79 Any type which is typedef'ed to cardinal will be one of these.
84 def __init__(self, name, size):
85 Type.__init__(self, name)
90 def resolve(self, module):
96 out = __main__.output['simple']
99 # Cardinal datatype globals. See module __init__ method.
100 tcard8 = SimpleType(('uint8_t',), 1)
101 tcard16 = SimpleType(('uint16_t',), 2)
102 tcard32 = SimpleType(('uint32_t',), 4)
103 tint8 = SimpleType(('int8_t',), 1)
104 tint16 = SimpleType(('int16_t',), 2)
105 tint32 = SimpleType(('int32_t',), 4)
106 tchar = SimpleType(('char',), 1)
107 tfloat = SimpleType(('float',), 4)
108 tdouble = SimpleType(('double',), 8)
111 class Enum(SimpleType):
113 Derived class which represents an enum. Fixed-size.
116 values contains a list of (name, value) tuples. value is empty, or a number.
117 bits contains a list of (name, bitnum) tuples. items only appear if specified as a bit. bitnum is a number.
119 def __init__(self, name, elt):
120 SimpleType.__init__(self, name, 4)
123 for item in list(elt):
124 # First check if we're using a default value
125 if len(list(item)) == 0:
126 self.values.append((item.get('name'), ''))
129 # An explicit value or bit was specified.
130 value = list(item)[0]
131 if value.tag == 'value':
132 self.values.append((item.get('name'), value.text))
133 elif value.tag == 'bit':
134 self.values.append((item.get('name'), '%u' % (1 << int(value.text, 0))))
135 self.bits.append((item.get('name'), value.text))
137 def resolve(self, module):
140 def fixed_size(self):
143 out = __main__.output['enum']
146 class ListType(Type):
148 Derived class which represents a list of some other datatype. Fixed- or variable-sized.
151 member is the datatype of the list elements.
152 parent is the structure type containing the list.
153 expr is an Expression object containing the length information, for variable-sized lists.
155 def __init__(self, elt, member, *parent):
156 Type.__init__(self, member.name)
161 if elt.tag == 'list':
163 self.expr = Expression(elts[0] if len(elts) else elt, self)
164 elif elt.tag == 'valueparam':
165 self.expr = Expression(elt, self)
167 self.size = member.size if member.fixed_size() else None
168 self.nmemb = self.expr.nmemb if self.expr.fixed_size() else None
170 def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto):
171 if not self.fixed_size():
172 # We need a length field.
173 # Ask our Expression object for it's name, type, and whether it's on the wire.
174 lenfid = self.expr.lenfield_type
175 lenfield_name = self.expr.lenfield_name
176 lenwire = self.expr.lenwire
179 # See if the length field is already in the structure.
180 for parent in self.parent:
181 for field in parent.fields:
182 if field.field_name == lenfield_name:
185 # It isn't, so we need to add it to the structure ourself.
187 type = module.get_type(lenfid)
188 lenfield_type = module.get_type_name(lenfid)
189 type.make_member_of(module, complex_type, lenfield_type, lenfield_name, True, lenwire, False)
191 # Add ourself to the structure by calling our original method.
192 Type.make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto)
194 def resolve(self, module):
197 self.member.resolve(module)
199 # Find my length field again. We need the actual Field object in the expr.
200 # This is needed because we might have added it ourself above.
201 if not self.fixed_size():
202 for parent in self.parent:
203 for field in parent.fields:
204 if field.field_name == self.expr.lenfield_name and field.wire:
205 self.expr.lenfield = field
210 def fixed_size(self):
211 return self.member.fixed_size() and self.expr.fixed_size()
213 class ExprType(Type):
215 Derived class which represents an exprfield. Fixed size.
218 expr is an Expression object containing the value of the field.
220 def __init__(self, elt, member, *parent):
221 Type.__init__(self, member.name)
226 self.expr = Expression(list(elt)[0], self)
228 self.size = member.size
231 def resolve(self, module):
234 self.member.resolve(module)
237 def fixed_size(self):
242 Derived class which represents a padding field.
244 def __init__(self, elt):
245 Type.__init__(self, tcard8.name)
248 self.nmemb = 1 if (elt == None) else int(elt.get('bytes'), 0)
250 def resolve(self, module):
253 def fixed_size(self):
257 class ComplexType(Type):
259 Derived class which represents a structure. Base type for all structure types.
262 fields is an array of Field objects describing the structure fields.
264 def __init__(self, name, elt):
265 Type.__init__(self, name)
266 self.is_container = True
271 self.lenfield_parent = [self]
273 def resolve(self, module):
278 # Resolve all of our field datatypes.
279 for child in list(self.elt):
280 if child.tag == 'pad':
281 field_name = 'pad' + str(pads)
283 type = PadType(child)
286 elif child.tag == 'field':
287 field_name = child.get('name')
288 fkey = child.get('type')
289 type = module.get_type(fkey)
291 elif child.tag == 'exprfield':
292 field_name = child.get('name')
293 fkey = child.get('type')
294 type = ExprType(child, module.get_type(fkey), *self.lenfield_parent)
296 elif child.tag == 'list':
297 field_name = child.get('name')
298 fkey = child.get('type')
299 type = ListType(child, module.get_type(fkey), *self.lenfield_parent)
301 elif child.tag == 'valueparam':
302 field_name = child.get('value-list-name')
304 type = ListType(child, module.get_type(fkey), *self.lenfield_parent)
310 # Get the full type name for the field
311 field_type = module.get_type_name(fkey)
312 # Add the field to ourself
313 type.make_member_of(module, self, field_type, field_name, visible, True, False)
314 # Recursively resolve the type (could be another structure, list)
317 self.calc_size() # Figure out how big we are
322 for m in self.fields:
325 if m.type.fixed_size():
326 self.size = self.size + (m.type.size * m.type.nmemb)
331 def fixed_size(self):
332 for m in self.fields:
333 if not m.type.fixed_size():
338 class Struct(ComplexType):
340 Derived class representing a struct data type.
342 out = __main__.output['struct']
345 class Union(ComplexType):
347 Derived class representing a union data type.
349 def __init__(self, name, elt):
350 ComplexType.__init__(self, name, elt)
353 out = __main__.output['union']
356 class Reply(ComplexType):
358 Derived class representing a reply. Only found as a field of Request.
360 def __init__(self, name, elt):
361 ComplexType.__init__(self, name, elt)
364 def resolve(self, module):
367 # Add the automatic protocol fields
368 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
369 self.fields.append(_placeholder_byte)
370 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
371 self.fields.append(Field(tcard32, tcard32.name, 'length', False, True, True))
372 ComplexType.resolve(self, module)
375 class Request(ComplexType):
377 Derived class representing a request.
380 reply contains the reply datatype or None for void requests.
381 opcode contains the request number.
383 def __init__(self, name, elt):
384 ComplexType.__init__(self, name, elt)
386 self.opcode = elt.get('opcode')
388 for child in list(elt):
389 if child.tag == 'reply':
390 self.reply = Reply(name, child)
392 def resolve(self, module):
395 # Add the automatic protocol fields
396 if module.namespace.is_ext:
397 self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
398 self.fields.append(Field(tcard8, tcard8.name, 'minor_opcode', False, True, True))
399 self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
400 ComplexType.resolve(self, module)
402 self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
403 self.fields.append(_placeholder_byte)
404 self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
405 ComplexType.resolve(self, module)
408 self.reply.resolve(module)
410 out = __main__.output['request']
413 class Event(ComplexType):
415 Derived class representing an event data type.
418 opcodes is a dictionary of name -> opcode number, for eventcopies.
420 def __init__(self, name, elt):
421 ComplexType.__init__(self, name, elt)
424 tmp = elt.get('no-sequence-number')
425 self.has_seq = (tmp == None or tmp.lower() == 'false' or tmp == '0')
427 def add_opcode(self, opcode, name, main):
428 self.opcodes[name] = opcode
432 def resolve(self, module):
436 # Add the automatic protocol fields
437 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
439 self.fields.append(_placeholder_byte)
440 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
441 ComplexType.resolve(self, module)
443 out = __main__.output['event']
446 class Error(ComplexType):
448 Derived class representing an error data type.
451 opcodes is a dictionary of name -> opcode number, for errorcopies.
453 def __init__(self, name, elt):
454 ComplexType.__init__(self, name, elt)
457 def add_opcode(self, opcode, name, main):
458 self.opcodes[name] = opcode
462 def resolve(self, module):
466 # Add the automatic protocol fields
467 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
468 self.fields.append(Field(tcard8, tcard8.name, 'error_code', False, True, True))
469 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
470 ComplexType.resolve(self, module)
472 out = __main__.output['error']
474 _placeholder_byte = Field(PadType(None), tcard8.name, 'pad0', False, True, False)