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 # XXX replace this with a simple number, please.
133 self.values.append((item.get('name'), '(1 << %s)' % value.text))
135 def resolve(self, module):
138 def fixed_size(self):
141 out = __main__.output['enum']
144 class ListType(Type):
146 Derived class which represents a list of some other datatype. Fixed- or variable-sized.
149 member is the datatype of the list elements.
150 parent is the structure type containing the list.
151 expr is an Expression object containing the length information, for variable-sized lists.
153 def __init__(self, elt, member, parent):
154 Type.__init__(self, member.name)
159 if elt.tag == 'list':
161 self.expr = Expression(elts[0] if len(elts) else elt, self)
162 elif elt.tag == 'valueparam':
163 self.expr = Expression(elt, self)
165 self.size = member.size if member.fixed_size() else None
166 self.nmemb = self.expr.nmemb if self.expr.fixed_size() else None
168 def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto):
169 if not self.fixed_size():
170 # We need a length field.
171 # Ask our Expression object for it's name, type, and whether it's on the wire.
172 lenfid = self.expr.lenfield_type
173 lenfield_name = self.expr.lenfield_name
174 lenwire = self.expr.lenwire
177 # See if the length field is already in the structure.
178 for field in self.parent.fields:
179 if field.field_name == lenfield_name:
182 # It isn't, so we need to add it to the structure ourself.
184 type = module.get_type(lenfid)
185 lenfield_type = module.get_type_name(lenfid)
186 type.make_member_of(module, complex_type, lenfield_type, lenfield_name, True, lenwire, False)
188 # Add ourself to the structure by calling our original method.
189 Type.make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto)
191 def resolve(self, module):
194 self.member.resolve(module)
196 # Find my length field again. We need the actual Field object in the expr.
197 # This is needed because we might have added it ourself above.
198 if not self.fixed_size():
199 for field in self.parent.fields:
200 if field.field_name == self.expr.lenfield_name and field.wire:
201 self.expr.lenfield = field
206 def fixed_size(self):
207 return self.member.fixed_size() and self.expr.fixed_size()
209 class ExprType(Type):
211 Derived class which represents an exprfield. Fixed size.
214 expr is an Expression object containing the value of the field.
216 def __init__(self, elt, member, parent):
217 Type.__init__(self, member.name)
222 self.expr = Expression(list(elt)[0], self)
224 self.size = member.size
227 def resolve(self, module):
230 self.member.resolve(module)
233 def fixed_size(self):
238 Derived class which represents a padding field.
240 def __init__(self, elt):
241 Type.__init__(self, tcard8.name)
244 self.nmemb = 1 if (elt == None) else int(elt.get('bytes'))
246 def resolve(self, module):
249 def fixed_size(self):
253 class ComplexType(Type):
255 Derived class which represents a structure. Base type for all structure types.
258 fields is an array of Field objects describing the structure fields.
260 def __init__(self, name, elt):
261 Type.__init__(self, name)
262 self.is_container = True
268 def resolve(self, module):
273 # Resolve all of our field datatypes.
274 for child in list(self.elt):
275 if child.tag == 'pad':
276 field_name = 'pad' + str(pads)
278 type = PadType(child)
281 elif child.tag == 'field':
282 field_name = child.get('name')
283 fkey = child.get('type')
284 type = module.get_type(fkey)
286 elif child.tag == 'exprfield':
287 field_name = child.get('name')
288 fkey = child.get('type')
289 type = ExprType(child, module.get_type(fkey), self)
291 elif child.tag == 'list':
292 field_name = child.get('name')
293 fkey = child.get('type')
294 type = ListType(child, module.get_type(fkey), self)
296 elif child.tag == 'valueparam':
297 field_name = child.get('value-list-name')
299 type = ListType(child, module.get_type(fkey), self)
305 # Get the full type name for the field
306 field_type = module.get_type_name(fkey)
307 # Add the field to ourself
308 type.make_member_of(module, self, field_type, field_name, visible, True, False)
309 # Recursively resolve the type (could be another structure, list)
312 self.calc_size() # Figure out how big we are
317 for m in self.fields:
320 if m.type.fixed_size():
321 self.size = self.size + (m.type.size * m.type.nmemb)
326 def fixed_size(self):
327 for m in self.fields:
328 if not m.type.fixed_size():
333 class Struct(ComplexType):
335 Derived class representing a struct data type.
337 out = __main__.output['struct']
340 class Union(ComplexType):
342 Derived class representing a union data type.
344 def __init__(self, name, elt):
345 ComplexType.__init__(self, name, elt)
348 out = __main__.output['union']
351 class Reply(ComplexType):
353 Derived class representing a reply. Only found as a field of Request.
355 def __init__(self, name, elt):
356 ComplexType.__init__(self, name, elt)
359 def resolve(self, module):
362 # Add the automatic protocol fields
363 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
364 self.fields.append(_placeholder_byte)
365 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
366 self.fields.append(Field(tcard32, tcard32.name, 'length', False, True, True))
367 ComplexType.resolve(self, module)
370 class Request(ComplexType):
372 Derived class representing a request.
375 reply contains the reply datatype or None for void requests.
376 opcode contains the request number.
378 def __init__(self, name, elt):
379 ComplexType.__init__(self, name, elt)
381 self.opcode = elt.get('opcode')
383 for child in list(elt):
384 if child.tag == 'reply':
385 self.reply = Reply(name, child)
387 def resolve(self, module):
390 # Add the automatic protocol fields
391 if module.namespace.is_ext:
392 self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
393 self.fields.append(Field(tcard8, tcard8.name, 'minor_opcode', False, True, True))
394 self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
395 ComplexType.resolve(self, module)
397 self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
398 self.fields.append(_placeholder_byte)
399 self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
400 ComplexType.resolve(self, module)
403 self.reply.resolve(module)
405 out = __main__.output['request']
408 class Event(ComplexType):
410 Derived class representing an event data type.
413 opcodes is a dictionary of name -> opcode number, for eventcopies.
415 def __init__(self, name, elt):
416 ComplexType.__init__(self, name, elt)
419 tmp = elt.get('no-sequence-number')
420 self.has_seq = (tmp == None or tmp.lower() == 'false' or tmp == '0')
422 def add_opcode(self, opcode, name, main):
423 self.opcodes[name] = opcode
427 def resolve(self, module):
431 # Add the automatic protocol fields
432 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
434 self.fields.append(_placeholder_byte)
435 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
436 ComplexType.resolve(self, module)
438 out = __main__.output['event']
441 class Error(ComplexType):
443 Derived class representing an error data type.
446 opcodes is a dictionary of name -> opcode number, for errorcopies.
448 def __init__(self, name, elt):
449 ComplexType.__init__(self, name, elt)
452 def add_opcode(self, opcode, name, main):
453 self.opcodes[name] = opcode
457 def resolve(self, module):
461 # Add the automatic protocol fields
462 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
463 self.fields.append(Field(tcard8, tcard8.name, 'error_code', False, True, True))
464 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
465 ComplexType.resolve(self, module)
467 out = __main__.output['error']
469 _placeholder_byte = Field(PadType(None), tcard8.name, 'pad0', False, True, False)