Add support for X Generic Extension events
[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     enum is the enum name this field refers to, if any.
15     '''
16     def __init__(self, type, field_type, field_name, visible, wire, auto, enum=None):
17         self.type = type
18         self.field_type = field_type
19         self.field_name = field_name
20         self.enum = enum
21         self.visible = visible
22         self.wire = wire
23         self.auto = auto
24
25
26 class Expression(object):
27     '''
28     Represents a mathematical expression for a list length or exprfield.
29
30     Public fields:
31     op is the operation (text +,*,/,<<,~) or None.
32     lhs and rhs are the sub-Expressions if op is set.
33     lenfield_name is the name of the length field, or None for request lists.
34     lenfield is the Field object for the length field, or None.
35     bitfield is True if the length field is a bitmask instead of a number.
36     nmemb is the fixed size (value)of the expression, or None
37     '''
38     def __init__(self, elt, parent):
39         self.parent = parent
40
41         self.nmemb = None
42
43         self.lenfield_name = None
44         self.lenfield_type = None
45         self.lenfield_parent = None
46         self.lenfield = None
47         self.lenwire = False
48         self.bitfield = False
49
50         self.op = None
51         self.lhs = None
52         self.rhs = None
53
54         if elt.tag == 'list':
55             # List going into a request, which has no length field (inferred by server)
56             self.lenfield_name = elt.get('name') + '_len'
57             self.lenfield_type = 'CARD32'
58
59         elif elt.tag == 'fieldref':
60             # Standard list with a fieldref
61             self.lenfield_name = elt.text
62
63         elif elt.tag == 'valueparam':
64             # Value-mask.  The length bitmask is described by attributes.
65             self.lenfield_name = elt.get('value-mask-name')
66             self.lenfield_type = elt.get('value-mask-type')
67             self.lenwire = True
68             self.bitfield = True
69
70         elif elt.tag == 'op':
71             # Op field.  Need to recurse.
72             self.op = elt.get('op')
73             self.lhs = Expression(list(elt)[0], parent)
74             self.rhs = Expression(list(elt)[1], parent)
75
76             # Hopefully we don't have two separate length fields...
77             self.lenfield_name = self.lhs.lenfield_name
78             if self.lenfield_name == None:
79                 self.lenfield_name = self.rhs.lenfield_name
80
81         elif elt.tag == 'unop':
82             # Op field.  Need to recurse.
83             self.op = elt.get('op')
84             self.rhs = Expression(list(elt)[0], parent)
85
86             self.lenfield_name = self.rhs.lenfield_name
87             
88         elif elt.tag == 'value':
89             # Constant expression
90             self.nmemb = int(elt.text, 0)
91
92         elif elt.tag == 'popcount':
93             self.op = 'popcount'
94             self.rhs = Expression(list(elt)[0], parent)
95             self.lenfield_name = self.rhs.lenfield_name
96             # xcb_popcount returns 'int' - handle the type in the language-specific part
97
98         elif elt.tag == 'enumref':
99             self.op = 'enumref'
100             self.lenfield_name = (elt.get('ref'), elt.text)
101             
102         elif elt.tag == 'sumof':
103             self.op = 'sumof'
104             self.lenfield_name = elt.get('ref')
105
106         else:
107             # Notreached
108             raise Exception("undefined tag '%s'" % elt.tag)
109
110     def fixed_size(self):
111         return self.nmemb != None
112
113     def resolve(self, module, parents):
114         if self.op == 'enumref':
115             self.lenfield_type = module.get_type(self.lenfield_name[0])
116             self.lenfield_name = self.lenfield_name[1]
117         elif self.op == 'sumof':
118             # need to find the field with lenfield_name
119             for p in reversed(parents): 
120                 fields = dict([(f.field_name, f) for f in p.fields])
121                 if self.lenfield_name in fields.keys():
122                     if p.is_bitcase:
123                         # switch is the anchestor 
124                         self.lenfield_parent = p.parents[-1]
125                     else:
126                         self.lenfield_parent = p
127                     self.lenfield_type = fields[self.lenfield_name].field_type
128                     break
129