~jc-berentsen/pidl/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

from idlclass import *
from idlmethod import *
from idltype import *

class IDL_AST(object):
    def __init__(self, classes):
        self.classes = []
        self.namespaceincludes = []
        self.extend (classes)

    def resolve(self):
        self.reset_lists ()
        self.resolve_classes (self.classes)
        self.resolve_copyability (self.classes)
        new_classes = self.make_implicit_types ()
        self.resolve_classes (new_classes)
        self.resolve_copyability (new_classes)
        self.classes.append (new_classes)

    def resolve_classes (self, classes):
        self.update_class_dict (classes)
        classes.sort (lambda cla, clb: cla.inheritance_depth (self.classdict) < clb.inheritance_depth (self.classdict))
        for cl in classes:
            cl.resolve (self.classdict)

    def resolve_copyability (self, classes):
        for cl in classes:
            cl.resolve_copyability (self.classdict)

    def extend (self, classes):
        self.classes.extend ([ cls for cls in classes if isinstance(cls, IDLClass) ])
        self.namespaceincludes.extend ([ [cls.nsname, [cls.includes, cls.external]] 
                                   for cls in classes if not isinstance(cls, IDLClass) ])

    def update_class_dict (self, classes):
        for cl in classes:
            self.update_new_class (cl)

    def update_new_class (self, cl):
        name = cl.name.nsname + "::" + cl.name.clsname
        self.classdict[name] = cl
        self.classnames.append(name)
        self.add_new_namespaces (cl)

    def add_new_namespaces (self, cl):
        namespace = cl.name.nsname
        if namespace not in self.namespaces:
            self.namespaces.append(namespace)
            if cl.is_external:
                self.external_namespaces.append (namespace)

    def make_implicit_types (self):
        new_classes = []
        for namespace in self.namespaces:
            if self.has_any_event_class_in_namespace (namespace):
                new_classes.append (self.define_dispatcher_class (namespace))
        new_classes.extend (self.define_container_types ())
        new_classes.extend (self.define_all_synchronizer_types ())
        return new_classes

    def define_container_types (self):
        new_classes = []
        for prop in self.get_property_list ():
            if prop.type.contained_type:
                new_classes.append (self.make_list_type (prop.type, prop.name))
        return new_classes

    def define_all_synchronizer_types (self):
        result = []
        for cl in self.get_synchronized_classes ():
            result.extend (self.make_synchronizer_types_for_class (cl))

        return result

    def make_synchronizer_types_for_class (self, cl):
        result = []
        assert (cl.kind == 'synchronized')
        namespace = cl.name.nsname
        name = cl.name.clsname + 'Synchronizer'
        typ = IDLtype (namespace, name)
        base_refs = [cl.name]
        constructor = Method (('new', None), '', None, [(cl.name, 'impl')], stat='static')
        defs = [constructor]
        #add the defined properties
        synchronized_props = self.copy_props_mark_synchronized (cl.props)
        defs.extend (synchronized_props)
        synchronizer = IDLClass (typ, defs, base_refs=base_refs, safe='safe')
        result.append (synchronizer)
        result.extend (self.define_synchronizer_impl_classes (cl))
        return result

    def copy_props_mark_synchronized (self, props):
        res = copy.deepcopy (props)
        map (lambda prop: prop.modifiers.append ('synchronized'), res)
        return res

    def define_synchronizer_impl_classes (self, cl):
        namespace = cl.name.nsname
        impl_name = cl.name.clsname + 'Impl'
        impl_type = IDLtype (namespace, impl_name)
        impl_base_refs = [cl.name]
        private_block = PrivateBlock ('', [namespace+cl.name.clsname+'Synchronizer * synchronizer;'], '')
        impl_defs = [cl.synchronizer_constructors[0]] # TODO find impl constructor more generally
        impl_defs.append (private_block)
        #add the defined properties
        impl_defs.extend (cl.props)
        impl = IDLClass (impl_type, impl_defs, base_refs=impl_base_refs)
        return [impl]

    def has_any_event_class_in_namespace (self, namespace):
        for classdetails in self.classdict.iteritems():
            cl = classdetails[1]
            if cl.kind == 'events' and cl.name.nsname == namespace: return True
        return False

    def get_property_list (self):
        all_properties = []
        for classdetails in self.classdict.iteritems():
            props = classdetails[1].props
            all_properties.extend (props)
        return all_properties

    def define_dispatcher_class (self, namespace):
        constructor = Method (('new', None), '', None, [], stat='static')
        destructor = Method (('del', None), '', None, [])
        notify_pending_events_method = Method (('notify_pending_events', None), '', None, [], stat='', is_virtual=True)        
        dispatch_events_method = Method (('dispatch_events', None), '', None, [])
        value_array_type = IDLtype ("G", "ValueArray")
        add_event_method = Method (('add_event', None), '', None,
                                [(value_array_type, 'closure')], is_internal=True)
        method_defs = [constructor, destructor, notify_pending_events_method, dispatch_events_method, add_event_method]

        private_block = PrivateBlock ('', ['GAsyncQueue * queue;'], '')
        defs = method_defs + [private_block]
        
        dispatcher_type = IDLtype (namespace, "Dispatcher")
        dispatcher_class = IDLClass (dispatcher_type, defs)
        return dispatcher_class

    def make_list_type (self, full_type, element_name):
        element_type = full_type.contained_type
        list_constructor = Method (('new', None), '', None, [], stat='static')
        list_destructor = Method (('del', None), '', None, [])
        prepend_method = Method (('prepend', None), '', None,
                                [(element_type, 'element')])        
        append_method = Method (('append', None), '', None,
                                [(element_type, 'element')])

        has_item_method = Method (('has_item', None), '', IDLtype ('', 'boolean'),
                                [(element_type, 'element')])

        remove_method = Method (('remove', None), '', None,
                                [(element_type, 'element')])

        get_at_method = Method (('get_at', None), '', element_type,
                                [(IDLtype ('','uint'), 'index')])

        size_method = Method (('size', ''), '', IDLtype ('','int'), [])
        for_each_method = Method (('for_each', None), '', None,
                                  [(IDLtype ('', element_type.fullname () +'Worker'), 'func'),
                                   (IDLtype ('', 'gpointer'), 'data'),
                                  ],
                                  is_internal=True)
        method_defs = [list_constructor, list_destructor, prepend_method, append_method, has_item_method, remove_method, get_at_method,
            size_method, for_each_method]

        private_block = PrivateBlock ('', ['GArray * array;'], '')
        defs = method_defs + [private_block]
        
        list_class = IDLClass (full_type, defs)
        return list_class

    def reset_lists (self):
        self.namespaces = []
        self.external_namespaces = []
        self.classdict = {}
        self.classnames = []
        self.enums = []

    def get_interfaces (self):
        return [cl for cl in self.classes if cl.is_interface ]

    def get_synchronized_classes (self):
        return [cl for cl in self.classes if cl.kind == 'synchronized' ]

    def set_namespace (self, name):
        for i in self.classes: i.name.nsname = name
        for o in self.namespaceincludes: o[0] = name


    def set_external (self, ext):
        for i in self.classes: i.is_external = ext
        for o in self.namespaceincludes: o[1][1] = ext