2 This module contains the classes which represent XCB data types.
4 from xcbgen.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
35 self.is_switch = False
36 self.is_bitcase = False
38 def resolve(self, module):
40 Abstract method for resolving a type.
41 This should make sure any referenced types are already declared.
43 raise Exception('abstract resolve method not overridden!')
47 Abstract method for outputting code.
48 These are declared in the language-specific modules, and
49 there must be a dictionary containing them declared when this module is imported!
51 raise Exception('abstract out method not overridden!')
55 Abstract method for determining if the data type is fixed-size.
57 raise Exception('abstract fixed_size method not overridden!')
59 def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto, enum=None):
61 Default method for making a data type a member of a structure.
62 Extend this if the data type needs to add an additional length field or something.
64 module is the global module object.
65 complex_type is the structure object.
66 see Field for the meaning of the other parameters.
68 new_field = Field(self, field_type, field_name, visible, wire, auto, enum)
70 # We dump the _placeholder_byte if any fields are added.
71 for (idx, field) in enumerate(complex_type.fields):
72 if field == _placeholder_byte:
73 complex_type.fields[idx] = new_field
76 complex_type.fields.append(new_field)
78 def make_fd_of(self, module, complex_type, fd_name):
80 Method for making a fd member of a structure.
82 new_fd = Field(self, module.get_type_name('INT32'), fd_name, True, False, False, None, True)
83 # We dump the _placeholder_byte if any fields are added.
84 for (idx, field) in enumerate(complex_type.fields):
85 if field == _placeholder_byte:
86 complex_type.fields[idx] = new_fd
89 complex_type.fields.append(new_fd)
91 class SimpleType(Type):
93 Derived class which represents a cardinal type like CARD32 or char.
94 Any type which is typedef'ed to cardinal will be one of these.
99 def __init__(self, name, size):
100 Type.__init__(self, name)
101 self.is_simple = True
105 def resolve(self, module):
108 def fixed_size(self):
111 out = __main__.output['simple']
114 # Cardinal datatype globals. See module __init__ method.
115 tcard8 = SimpleType(('uint8_t',), 1)
116 tcard16 = SimpleType(('uint16_t',), 2)
117 tcard32 = SimpleType(('uint32_t',), 4)
118 tcard64 = SimpleType(('uint64_t',), 8)
119 tint8 = SimpleType(('int8_t',), 1)
120 tint16 = SimpleType(('int16_t',), 2)
121 tint32 = SimpleType(('int32_t',), 4)
122 tint64 = SimpleType(('int64_t',), 8)
123 tchar = SimpleType(('char',), 1)
124 tfloat = SimpleType(('float',), 4)
125 tdouble = SimpleType(('double',), 8)
128 class Enum(SimpleType):
130 Derived class which represents an enum. Fixed-size.
133 values contains a list of (name, value) tuples. value is empty, or a number.
134 bits contains a list of (name, bitnum) tuples. items only appear if specified as a bit. bitnum is a number.
136 def __init__(self, name, elt):
137 SimpleType.__init__(self, name, 4)
141 for item in list(elt):
142 if item.tag == 'doc':
143 self.doc = Doc(name, item)
145 # First check if we're using a default value
146 if len(list(item)) == 0:
147 self.values.append((item.get('name'), ''))
150 # An explicit value or bit was specified.
151 value = list(item)[0]
152 if value.tag == 'value':
153 self.values.append((item.get('name'), value.text))
154 elif value.tag == 'bit':
155 self.values.append((item.get('name'), '%u' % (1 << int(value.text, 0))))
156 self.bits.append((item.get('name'), value.text))
158 def resolve(self, module):
161 def fixed_size(self):
164 out = __main__.output['enum']
167 class ListType(Type):
169 Derived class which represents a list of some other datatype. Fixed- or variable-sized.
172 member is the datatype of the list elements.
173 parent is the structure type containing the list.
174 expr is an Expression object containing the length information, for variable-sized lists.
176 def __init__(self, elt, member, *parent):
177 Type.__init__(self, member.name)
180 self.parents = list(parent)
182 if elt.tag == 'list':
184 self.expr = Expression(elts[0] if len(elts) else elt, self)
185 elif elt.tag == 'valueparam':
186 self.expr = Expression(elt, self)
188 self.size = member.size if member.fixed_size() else None
189 self.nmemb = self.expr.nmemb if self.expr.fixed_size() else None
191 def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto, enum=None):
192 if not self.fixed_size():
193 # We need a length field.
194 # Ask our Expression object for it's name, type, and whether it's on the wire.
195 lenfid = self.expr.lenfield_type
196 lenfield_name = self.expr.lenfield_name
197 lenwire = self.expr.lenwire
200 # See if the length field is already in the structure.
201 for parent in self.parents:
202 for field in parent.fields:
203 if field.field_name == lenfield_name:
206 # It isn't, so we need to add it to the structure ourself.
208 type = module.get_type(lenfid)
209 lenfield_type = module.get_type_name(lenfid)
210 type.make_member_of(module, complex_type, lenfield_type, lenfield_name, True, lenwire, False, enum)
212 # Add ourself to the structure by calling our original method.
213 Type.make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto, enum)
215 def resolve(self, module):
218 self.member.resolve(module)
219 self.expr.resolve(module, self.parents)
221 # Find my length field again. We need the actual Field object in the expr.
222 # This is needed because we might have added it ourself above.
223 if not self.fixed_size():
224 for parent in self.parents:
225 for field in parent.fields:
226 if field.field_name == self.expr.lenfield_name and field.wire:
227 self.expr.lenfield = field
232 def fixed_size(self):
233 return self.member.fixed_size() and self.expr.fixed_size()
235 class ExprType(Type):
237 Derived class which represents an exprfield. Fixed size.
240 expr is an Expression object containing the value of the field.
242 def __init__(self, elt, member, *parents):
243 Type.__init__(self, member.name)
246 self.parents = parents
248 self.expr = Expression(list(elt)[0], self)
250 self.size = member.size
253 def resolve(self, module):
256 self.member.resolve(module)
259 def fixed_size(self):
264 Derived class which represents a padding field.
266 def __init__(self, elt):
267 Type.__init__(self, tcard8.name)
270 self.nmemb = 1 if (elt == None) else int(elt.get('bytes'), 0)
272 def resolve(self, module):
275 def fixed_size(self):
279 class ComplexType(Type):
281 Derived class which represents a structure. Base type for all structure types.
284 fields is an array of Field objects describing the structure fields.
286 def __init__(self, name, elt):
287 Type.__init__(self, name)
288 self.is_container = True
293 self.lenfield_parent = [self]
296 def resolve(self, module):
302 # Resolve all of our field datatypes.
303 for child in list(self.elt):
304 if child.tag == 'pad':
305 field_name = 'pad' + str(pads)
307 type = PadType(child)
310 elif child.tag == 'field':
311 field_name = child.get('name')
312 enum = child.get('enum')
313 fkey = child.get('type')
314 type = module.get_type(fkey)
316 elif child.tag == 'exprfield':
317 field_name = child.get('name')
318 fkey = child.get('type')
319 type = ExprType(child, module.get_type(fkey), *self.lenfield_parent)
321 elif child.tag == 'list':
322 field_name = child.get('name')
323 fkey = child.get('type')
324 type = ListType(child, module.get_type(fkey), *self.lenfield_parent)
326 elif child.tag == 'valueparam':
327 field_name = child.get('value-list-name')
329 type = ListType(child, module.get_type(fkey), *self.lenfield_parent)
331 elif child.tag == 'switch':
332 field_name = child.get('name')
333 # construct the switch type name from the parent type and the field name
334 field_type = self.name + (field_name,)
335 type = SwitchType(field_type, child, *self.lenfield_parent)
337 type.make_member_of(module, self, field_type, field_name, visible, True, False)
340 elif child.tag == 'fd':
341 fd_name = child.get('name')
342 type = module.get_type('INT32')
343 type.make_fd_of(module, self, fd_name)
349 # Get the full type name for the field
350 field_type = module.get_type_name(fkey)
351 # Add the field to ourself
352 type.make_member_of(module, self, field_type, field_name, visible, True, False, enum)
353 # Recursively resolve the type (could be another structure, list)
356 self.calc_size() # Figure out how big we are
361 for m in self.fields:
364 if m.type.fixed_size():
365 self.size = self.size + (m.type.size * m.type.nmemb)
370 def fixed_size(self):
371 for m in self.fields:
372 if not m.type.fixed_size():
376 class SwitchType(ComplexType):
378 Derived class which represents a List of Items.
381 bitcases is an array of Bitcase objects describing the list items
384 def __init__(self, name, elt, *parents):
385 ComplexType.__init__(self, name, elt)
386 self.parents = parents
387 # FIXME: switch cannot store lenfields, so it should just delegate the parents
388 self.lenfield_parent = list(parents) + [self]
389 # self.fields contains all possible fields collected from the Bitcase objects,
390 # whereas self.items contains the Bitcase objects themselves
393 self.is_switch = True
395 self.expr = Expression(elts[0] if len(elts) else elt, self)
397 def resolve(self, module):
402 parents = list(self.parents) + [self]
404 # Resolve all of our field datatypes.
405 for index, child in enumerate(list(self.elt)):
406 if child.tag == 'bitcase':
407 field_name = child.get('name')
408 if field_name is None:
409 field_type = self.name + ('bitcase%d' % index,)
411 field_type = self.name + (field_name,)
413 # use self.parent to indicate anchestor,
414 # as switch does not contain named fields itself
415 type = BitcaseType(index, field_type, child, *parents)
416 # construct the switch type name from the parent type and the field name
417 if field_name is None:
418 type.has_name = False
419 # Get the full type name for the field
420 field_type = type.name
423 # add the field to ourself
424 type.make_member_of(module, self, field_type, field_name, visible, True, False)
426 # recursively resolve the type (could be another structure, list)
429 for new_field in type.fields:
430 # We dump the _placeholder_byte if any fields are added.
431 for (idx, field) in enumerate(self.fields):
432 if field == _placeholder_byte:
433 self.fields[idx] = new_field
436 if False == inserted:
437 self.fields.append(new_field)
439 self.calc_size() # Figure out how big we are
442 def make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto, enum=None):
443 if not self.fixed_size():
444 # We need a length field.
445 # Ask our Expression object for it's name, type, and whether it's on the wire.
446 lenfid = self.expr.lenfield_type
447 lenfield_name = self.expr.lenfield_name
448 lenwire = self.expr.lenwire
451 # See if the length field is already in the structure.
452 for parent in self.parents:
453 for field in parent.fields:
454 if field.field_name == lenfield_name:
457 # It isn't, so we need to add it to the structure ourself.
459 type = module.get_type(lenfid)
460 lenfield_type = module.get_type_name(lenfid)
461 type.make_member_of(module, complex_type, lenfield_type, lenfield_name, True, lenwire, False, enum)
463 # Add ourself to the structure by calling our original method.
464 Type.make_member_of(self, module, complex_type, field_type, field_name, visible, wire, auto, enum)
466 # size for switch can only be calculated at runtime
470 # note: switch is _always_ of variable size, but we indicate here wether
471 # it contains elements that are variable-sized themselves
472 def fixed_size(self):
474 # for m in self.fields:
475 # if not m.type.fixed_size():
480 class Struct(ComplexType):
482 Derived class representing a struct data type.
484 out = __main__.output['struct']
487 class Union(ComplexType):
489 Derived class representing a union data type.
491 def __init__(self, name, elt):
492 ComplexType.__init__(self, name, elt)
495 out = __main__.output['union']
498 class BitcaseType(ComplexType):
500 Derived class representing a struct data type.
502 def __init__(self, index, name, elt, *parent):
507 if elt.tag == 'enumref':
508 self.expr.append(Expression(elt, self))
511 ComplexType.__init__(self, name, fields)
514 self.lenfield_parent = list(parent) + [self]
515 self.parents = list(parent)
516 self.is_bitcase = True
518 def make_member_of(self, module, switch_type, field_type, field_name, visible, wire, auto, enum=None):
520 register BitcaseType with the corresponding SwitchType
522 module is the global module object.
523 complex_type is the structure object.
524 see Field for the meaning of the other parameters.
526 new_field = Field(self, field_type, field_name, visible, wire, auto, enum)
528 # We dump the _placeholder_byte if any bitcases are added.
529 for (idx, field) in enumerate(switch_type.bitcases):
530 if field == _placeholder_byte:
531 switch_type.bitcases[idx] = new_field
534 switch_type.bitcases.append(new_field)
536 def resolve(self, module):
541 e.resolve(module, self.parents+[self])
543 # Resolve the bitcase expression
544 ComplexType.resolve(self, module)
547 class Reply(ComplexType):
549 Derived class representing a reply. Only found as a field of Request.
551 def __init__(self, name, elt):
552 ComplexType.__init__(self, name, elt)
556 for child in list(elt):
557 if child.tag == 'doc':
558 self.doc = Doc(name, child)
560 def resolve(self, module):
563 # Add the automatic protocol fields
564 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
565 self.fields.append(_placeholder_byte)
566 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
567 self.fields.append(Field(tcard32, tcard32.name, 'length', False, True, True))
568 ComplexType.resolve(self, module)
571 class Request(ComplexType):
573 Derived class representing a request.
576 reply contains the reply datatype or None for void requests.
577 opcode contains the request number.
579 def __init__(self, name, elt):
580 ComplexType.__init__(self, name, elt)
583 self.opcode = elt.get('opcode')
585 for child in list(elt):
586 if child.tag == 'reply':
587 self.reply = Reply(name, child)
588 if child.tag == 'doc':
589 self.doc = Doc(name, child)
591 def resolve(self, module):
594 # Add the automatic protocol fields
595 if module.namespace.is_ext:
596 self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
597 self.fields.append(Field(tcard8, tcard8.name, 'minor_opcode', False, True, True))
598 self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
599 ComplexType.resolve(self, module)
601 self.fields.append(Field(tcard8, tcard8.name, 'major_opcode', False, True, True))
602 self.fields.append(_placeholder_byte)
603 self.fields.append(Field(tcard16, tcard16.name, 'length', False, True, True))
604 ComplexType.resolve(self, module)
607 self.reply.resolve(module)
609 out = __main__.output['request']
612 class Event(ComplexType):
614 Derived class representing an event data type.
617 opcodes is a dictionary of name -> opcode number, for eventcopies.
619 def __init__(self, name, elt):
620 ComplexType.__init__(self, name, elt)
623 self.has_seq = not bool(elt.get('no-sequence-number'))
625 self.is_ge_event = bool(elt.get('xge'))
628 for item in list(elt):
629 if item.tag == 'doc':
630 self.doc = Doc(name, item)
632 def add_opcode(self, opcode, name, main):
633 self.opcodes[name] = opcode
637 def resolve(self, module):
638 def add_event_header():
639 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
641 self.fields.append(_placeholder_byte)
642 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
644 def add_ge_event_header():
645 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
646 self.fields.append(Field(tcard8, tcard8.name, 'extension', False, True, True))
647 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
648 self.fields.append(Field(tcard32, tcard32.name, 'length', False, True, True))
649 self.fields.append(Field(tcard16, tcard16.name, 'event_type', False, True, True))
654 # Add the automatic protocol fields
656 add_ge_event_header()
660 ComplexType.resolve(self, module)
662 out = __main__.output['event']
665 class Error(ComplexType):
667 Derived class representing an error data type.
670 opcodes is a dictionary of name -> opcode number, for errorcopies.
672 def __init__(self, name, elt):
673 ComplexType.__init__(self, name, elt)
676 def add_opcode(self, opcode, name, main):
677 self.opcodes[name] = opcode
681 def resolve(self, module):
685 # Add the automatic protocol fields
686 self.fields.append(Field(tcard8, tcard8.name, 'response_type', False, True, True))
687 self.fields.append(Field(tcard8, tcard8.name, 'error_code', False, True, True))
688 self.fields.append(Field(tcard16, tcard16.name, 'sequence', False, True, True))
689 ComplexType.resolve(self, module)
691 out = __main__.output['error']
696 Class representing a <doc> tag.
698 def __init__(self, name, elt):
700 self.description = None
701 self.brief = 'BRIEF DESCRIPTION MISSING'
707 for child in list(elt):
708 text = child.text if child.text else ''
709 if child.tag == 'description':
710 self.description = text.strip()
711 if child.tag == 'brief':
712 self.brief = text.strip()
713 if child.tag == 'field':
714 self.fields[child.get('name')] = text.strip()
715 if child.tag == 'error':
716 self.errors[child.get('type')] = text.strip()
717 if child.tag == 'see':
718 self.see[child.get('name')] = child.get('type')
719 if child.tag == 'example':
720 self.example = text.strip()
724 _placeholder_byte = Field(PadType(None), tcard8.name, 'pad0', False, True, False)