ed4b975cab3b31f140716df1b1c9c3b083b28471
[free-sw/xcb/proto] / xcbgen / expr.py
1 '''
2 This module contains helper classes for structure fields and length expressions.
3 '''
4 class Field(object):
5     '''
6     Represents a field of a structure.
7
8     type is the datatype object for the field.
9     field_type is the name of the type (string tuple)
10     field_name is the name of the structure field.
11     visible is true iff the field should be in the request API.
12     wire is true iff the field should be in the request structure.
13     auto is true iff the field is on the wire but not in the request API (e.g. opcode)
14     '''
15     def __init__(self, type, field_type, field_name, visible, wire, auto):
16         self.type = type
17         self.field_type = field_type
18         self.field_name = field_name
19         self.visible = visible
20         self.wire = wire
21         self.auto = auto
22
23
24 class Expression(object):
25     '''
26     Represents a mathematical expression for a list length or exprfield.
27
28     Public fields:
29     op is the operation (text +,*,/,<<) or None.
30     lhs and rhs are the sub-Expressions if op is set.
31     lenfield_name is the name of the length field, or None for request lists.
32     lenfield is the Field object for the length field, or None.
33     bitfield is True if the length field is a bitmask instead of a number.
34     nmemb is the fixed size (value)of the expression, or None
35     '''
36     def __init__(self, elt, parent):
37         self.parent = parent
38
39         self.nmemb = None
40
41         self.lenfield_name = None
42         self.lenfield_type = None
43         self.lenfield = None
44         self.lenwire = False
45         self.bitfield = False
46
47         self.op = None
48         self.lhs = None
49         self.rhs = None
50
51         if elt.tag == 'list':
52             # List going into a request, which has no length field (inferred by server)
53             self.lenfield_name = elt.get('name') + '_len'
54             self.lenfield_type = 'CARD32'
55
56         elif elt.tag == 'fieldref':
57             # Standard list with a fieldref
58             self.lenfield_name = elt.text
59
60         elif elt.tag == 'valueparam':
61             # Value-mask.  The length bitmask is described by attributes.
62             self.lenfield_name = elt.get('value-mask-name')
63             self.lenfield_type = elt.get('value-mask-type')
64             self.lenwire = True
65             self.bitfield = True
66
67         elif elt.tag == 'op':
68             # Op field.  Need to recurse.
69             self.op = elt.get('op')
70             self.lhs = Expression(list(elt)[0], parent)
71             self.rhs = Expression(list(elt)[1], parent)
72
73             # Hopefully we don't have two separate length fields...
74             self.lenfield_name = self.lhs.lenfield_name
75             if self.lenfield_name == None:
76                 self.lenfield_name = self.rhs.lenfield_name
77
78         elif elt.tag == 'value':
79             # Constant expression
80             self.nmemb = int(elt.text, 0)
81
82         else:
83             # Notreached
84             raise Exception('XXX')
85
86
87     def fixed_size(self):
88         return self.nmemb != None