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.
118 def __init__(self, name, elt):
119 SimpleType.__init__(self, name, 4)
121 for item in list(elt):
122 # First check if we're using a default value
123 if len(list(item)) == 0:
124 self.values.append((item.get('name'), ''))
127 # An explicit value or bit was specified.
128 value = list(item)[0]
129 if value.tag == 'value':
130 self.values.append((item.get('name'), value.text))
131 elif value.tag == 'bit':
132 self.values.append((item.get('name'), '%u' % (1 << int(value.text))))
134 def resolve(self, module):
137 def fixed_size(self):
140 out = __main__.output['enum']
143 class ListType(Type):
145 Derived class which represents a list of some other datatype. Fixed- or variable-sized.
148 member is the datatype of the list elements.
149 parent is the structure type containing the list.
150 expr is an Expression object containing the length information, for variable-sized lists.
152 def __init__(self, elt, member, parent):
153 Type.__init__(self, member.name)
158 if elt.tag == 'list':
160 self.expr = Expression(elts[0] if len(elts) else elt, self)
161 elif elt.tag == 'valueparam':
162 self.expr = Expression(elt, self)
164 self.size = member.size if member.fixed_size() else None
165 self.nmemb = self.expr.nmemb if self.expr.fixed_size() else None
167 def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto):
168 if not self.fixed_size():
169 # We need a length field.
170 # Ask our Expression object for it's name, type, and whether it's on the wire.
171 lenfid = self.expr.lenfield_type
172 lenfield_name = self.expr.lenfield_name
173 lenwire = self.expr.lenwire
176 # See if the length field is already in the structure.
177 for field in self.parent.fields:
178 if field.field_name == lenfield_name:
181 # It isn't, so we need to add it to the structure ourself.
183 type = module.get_type(lenfid)
184 lenfield_type = module.get_type_name(lenfid)
185 type.make_member_of(module, complex_type, lenfield_type, lenfield_name, True, lenwire, False)
187 # Add ourself to the structure by calling our original method.
188 Type.make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto)
190 def resolve(self, module):
193 self.member.resolve(module)
195 # Find my length field again. We need the actual Field object in the expr.
196 # This is needed because we might have added it ourself above.
197 if not self.fixed_size():
198 for field in self.parent.fields:
199 if field.field_name == self.expr.lenfield_name and field.wire:
200 self.expr.lenfield = field
205 def fixed_size(self):
206 return self.member.fixed_size() and self.expr.fixed_size()
208 class ExprType(Type):
210 Derived class which represents an exprfield. Fixed size.
213 expr is an Expression object containing the value of the field.
215 def __init__(self, elt, member, parent):
216 Type.__init__(self, member.name)
221 self.expr = Expression(list(elt)[0], self)
223 self.size = member.size
226 def resolve(self, module):
229 self.member.resolve(module)
232 def fixed_size(self):
235 class SizedPadType(Type):
237 Derived class which represents a padding field of given size.
239 def __init__(self, size):
240 Type.__init__(self, tcard8.name)
243 self.nmemb = int(size)
245 def resolve(self, module):
248 def fixed_size(self):
251 class PadType(SizedPadType):
253 Derived class which represents a padding field of given type.
255 def __init__(self, elt):
256 self.nmemb = "1" if (elt == None) else elt.get('bytes')
257 SizedPadType.__init__(self, self.nmemb)
259 class ComplexType(Type):
261 Derived class which represents a structure. Base type for all structure types.
264 fields is an array of Field objects describing the structure fields.
266 def __init__(self, name, elt):
267 Type.__init__(self, name)
268 self.is_container = True
274 def resolve(self, module):
279 # Resolve all of our field datatypes.
280 for child in list(self.elt):
281 value_mask_pad = None
282 if child.tag == 'pad':
283 field_name = 'pad' + str(pads)
285 type = PadType(child)
288 elif child.tag == 'field':
289 field_name = child.get('name')
290 fkey = child.get('type')
291 type = module.get_type(fkey)
293 elif child.tag == 'exprfield':
294 field_name = child.get('name')
295 fkey = child.get('type')
296 type = ExprType(child, module.get_type(fkey), self)
298 elif child.tag == 'list':
299 field_name = child.get('name')
300 fkey = child.get('type')
301 type = ListType(child, module.get_type(fkey), self)
303 elif child.tag == 'valueparam':
304 field_name = child.get('value-list-name')
306 type = ListType(child, module.get_type(fkey), self)
308 value_mask_pad = child.get('value-mask-pad')
313 # Get the full type name for the field
314 field_type = module.get_type_name(fkey)
315 # Add the field to ourself
316 type.make_member_of(module, self, field_type, field_name, visible, True, False)
317 # Recursively resolve the type (could be another structure, list)
320 # Add a value-mask-pad if necessary
321 if value_mask_pad != None:
322 vmp_field_name = 'pad' + str(pads)
324 vmp_type = SizedPadType(value_mask_pad)
327 vmp_field_type = module.get_type_name(vmp_fkey)
328 vmp_type.make_member_of(module, self, vmp_field_type, vmp_field_name, vmp_visible, True, False)
329 vmp_type.resolve(module)
331 self.calc_size() # Figure out how big we are
336 for m in self.fields:
339 if m.type.fixed_size():
340 self.size = self.size + (m.type.size * m.type.nmemb)
345 def fixed_size(self):
346 for m in self.fields:
347 if not m.type.fixed_size():
352 class Struct(ComplexType):
354 Derived class representing a struct data type.
356 out = __main__.output['struct']
359 class Union(ComplexType):
361 Derived class representing a union data type.
363 def __init__(self, name, elt):
364 ComplexType.__init__(self, name, elt)
367 out = __main__.output['union']
370 class Reply(ComplexType):
372 Derived class representing a reply. Only found as a field of Request.
374 def __init__(self, name, elt):
375 ComplexType.__init__(self, name, elt)
378 def resolve(self, module):
381 # Add the automatic protocol fields
382 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
383 self.fields.append(_placeholder_byte)
384 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
385 self.fields.append(Field(tcard32, tcard32.name, 'length', False, True, True))
386 ComplexType.resolve(self, module)
389 class Request(ComplexType):
391 Derived class representing a request.
394 reply contains the reply datatype or None for void requests.
395 opcode contains the request number.
397 def __init__(self, name, elt):
398 ComplexType.__init__(self, name, elt)
400 self.opcode = elt.get('opcode')
402 for child in list(elt):
403 if child.tag == 'reply':
404 self.reply = Reply(name, child)
406 def resolve(self, module):
409 # Add the automatic protocol fields
410 if module.namespace.is_ext:
411 self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
412 self.fields.append(Field(tcard8, tcard8.name, 'minor_opcode', False, True, True))
413 self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
414 ComplexType.resolve(self, module)
416 self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
417 self.fields.append(_placeholder_byte)
418 self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
419 ComplexType.resolve(self, module)
422 self.reply.resolve(module)
424 out = __main__.output['request']
427 class Event(ComplexType):
429 Derived class representing an event data type.
432 opcodes is a dictionary of name -> opcode number, for eventcopies.
434 def __init__(self, name, elt):
435 ComplexType.__init__(self, name, elt)
438 tmp = elt.get('no-sequence-number')
439 self.has_seq = (tmp == None or tmp.lower() == 'false' or tmp == '0')
441 def add_opcode(self, opcode, name, main):
442 self.opcodes[name] = opcode
446 def resolve(self, module):
450 # Add the automatic protocol fields
451 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
453 self.fields.append(_placeholder_byte)
454 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
455 ComplexType.resolve(self, module)
457 out = __main__.output['event']
460 class Error(ComplexType):
462 Derived class representing an error data type.
465 opcodes is a dictionary of name -> opcode number, for errorcopies.
467 def __init__(self, name, elt):
468 ComplexType.__init__(self, name, elt)
471 def add_opcode(self, opcode, name, main):
472 self.opcodes[name] = opcode
476 def resolve(self, module):
480 # Add the automatic protocol fields
481 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
482 self.fields.append(Field(tcard8, tcard8.name, 'error_code', False, True, True))
483 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
484 ComplexType.resolve(self, module)
486 out = __main__.output['error']
488 _placeholder_byte = Field(PadType(None), tcard8.name, 'pad0', False, True, False)