summaryrefslogtreecommitdiff
path: root/solenv/gdb/libreoffice/util/uno.py
blob: 23cf06ad6f68e96e3382f434d2e2bb004b4870e7 (plain)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# Version: MPL 1.1 / GPLv3+ / LGPLv3+
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License or as specified alternatively below. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Initial Developer of the Original Code is
#       David Tardon, Red Hat Inc. <dtardon@redhat.com>
# Portions created by the Initial Developer are Copyright (C) 2010 the
# Initial Developer. All Rights Reserved.
#
# Major Contributor(s):
#
# For minor contributions see the git repository.
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 3 or later (the "GPLv3+"), or
# the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
# in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
# instead of those above.

import gdb
import re

class UnsupportedType(Exception):
    '''Represents exception thrown when an unsupported UNO type(like
        array or union) is used.'''

    def __init__(self, type):
        self.type = type

class UnknownType(Exception):
    '''Represents exception thrown when an unknown UNO type is used.'''

    def __init__(self, type):
        self.type = type

class TypeClass(object):
    '''Represents type class of UNO type.'''

    # type class of void
    VOID = 0
    # type class of char
    CHAR = 1
    # type class of boolean
    BOOLEAN = 2
    # type class of byte
    BYTE = 3
    # type class of short
    SHORT = 4
    # type class of unsigned short
    UNSIGNED_SHORT = 5
    # type class of long
    LONG = 6
    # type class of unsigned long
    UNSIGNED_LONG = 7
    # type class of hyper
    HYPER = 8
    # type class of unsigned hyper
    UNSIGNED_HYPER = 9
    # type class of float
    FLOAT = 10
    # type class of double
    DOUBLE = 11
    # type class of string
    STRING = 12
    # type class of type
    TYPE = 13
    # type class of any
    ANY = 14
    # type class of enum
    ENUM = 15
    # type class of typedef
    TYPEDEF = 16
    # type class of struct
    STRUCT = 17
    # type class of union (not implemented)
    UNION = 18
    # type class of exception
    EXCEPTION = 19
    # type class of sequence
    SEQUENCE = 20
    # type class of array (not implemented)
    ARRAY = 21
    # type class of interface
    INTERFACE = 22
    # type class of service (not implemented)
    SERVICE = 23
    # type class of module (not implemented)
    MODULE = 24
    # type class of interface method
    INTERFACE_METHOD = 25
    # type class of interface attribute
    INTERFACE_ATTRIBUTE = 26
    # type class of unknown type
    UNKNOWN = 27
    # type class of properties
    PROPERTY = 28
    # type class of constants
    CONSTANT = 29
    # type class of constants groups
    CONSTANTS = 30
    # type class of singletons
    SINGLETON = 31

class TemplateType(object):

    def __init__(self, template, *args):
        self.template = template
        self.args = args

    def __str__(self):
        argtypes = [str(gdb.lookup_type(arg).strip_typedefs()) for arg in self.args]
        return self.template + '<' + ', '.join(argtypes) + '>'

class Type(object):
    '''Describes a UNO type.'''

    def __init__(self, typeclass, tag):
        '''Constructs a new Type.
            @param[in] typeclass value of com::sun::star::uno::TypeClass
            @param[in] tag UNO name of the type
        '''
        self.typeclass = typeclass
        self.tag = tag
        # C++ name of the type
        self.typename = None

    def type(self):
        '''Gets gdb.Type for the type'''
        if self.typename:
            return gdb.lookup_type(str(self.typename))
        return None

    @staticmethod
    def uno2cpp(typename):
        return str(typename).replace('.', '::')[1:-1]

    def strip_typedefs(self):
        copy = self.copy()
        copy.typename = self._strip_typedefs(self.typename)
        return copy

    def _strip_typedefs(self, typename):
        template_args = re.compile('([^<]+)(<.*>)')
        match = template_args.match(typename)
        type = self._lookup_type(match.group(1))
        types = []
        if match.group(2):
            list_delim = re.compile(', *')
            # FIXME: this does not work with nested templates
            for arg in match.group(2).split(list_delim):
                types.append(self._lookup_type(arg))

        typename = str(type)
        if not types.empty():
            typename += '<' + types.join(', ') + '>'

        return typename

    def _lookup_type(self, typename):
        if typename != '':
            type = gdb.lookup_type(typename)
            if type:
                type = type.strip_typedefs()
        return type

def make_uno_type(val):
    '''Creates a UNO type from gdb.Value of type
        com::sun::star::uno::Type, typelib_TypeDescription, or
        typelib_TypeDescriptionReference
    '''

    cssu_type = 'com::sun::star::uno::Type'
    type_desc = '_typelib_TypeDescription'
    type_descs =(
            type_desc,
            '_typelib_CompoundTypeDescription',
            '_typelib_StructTypeDescription',
            '_typelib_IndirectTypeDescription',
            '_typelib_EnumTypeDescription',
            '_typelib_InterfaceMemberTypeDescription',
            '_typelib_InterfaceMethodTypeDescription',
            '_typelib_InterfaceAttributeTypeDescription',
            '_typelib_InterfaceTypeDescription'
    )
    type_desc_ref = '_typelib_TypeDescriptionReference'

    type = val.type.strip_typedefs()

    if type.tag == cssu_type:
        pvalue = val['_pType']
        assert pvalue
        val = pvalue.dereference()
        type = val.type.strip_typedefs()

    while type.tag == type_desc_ref:
        pvalue = val['pType']
        assert pvalue
        val = pvalue.dereference()
        type = val.type.strip_typedefs()

    if type.tag not in type_descs:
        return None

    # determination of the UNO type
    full_val = val
    if type.tag != type_desc:
        while 'aBase' in val:
            val = val['aBase']
    type_class = int(val['eTypeClass'])
    name = val['pTypeName'].dereference()
    uno_type = None
    if type_class == TypeClass.VOID:
        uno_type = VoidType()
    elif type_class == TypeClass.CHAR:
        uno_type = PrimitiveType(type_class, name, 'sal_Char')
    elif type_class == TypeClass.BOOLEAN:
        uno_type = PrimitiveType(type_class, name, 'sal_Bool')
    elif type_class == TypeClass.BYTE:
        uno_type = PrimitiveType(type_class, name, 'sal_Int8')
    elif type_class == TypeClass.SHORT:
        uno_type = PrimitiveType(type_class, name, 'sal_Int16')
    elif type_class == TypeClass.UNSIGNED_SHORT:
        uno_type = PrimitiveType(type_class, name, 'sal_uInt16')
    elif type_class == TypeClass.LONG:
        uno_type = PrimitiveType(type_class, name, 'sal_Int32')
    elif type_class == TypeClass.UNSIGNED_LONG:
        uno_type = PrimitiveType(type_class, name, 'sal_uInt32')
    elif type_class == TypeClass.HYPER:
        uno_type = PrimitiveType(type_class, name, 'sal_Int64')
    elif type_class == TypeClass.UNSIGNED_HYPER:
        uno_type = PrimitiveType(type_class, name, 'sal_uInt64')
    elif type_class == TypeClass.FLOAT:
        uno_type = PrimitiveType(type_class, name, 'float')
    elif type_class == TypeClass.DOUBLE:
        uno_type = PrimitiveType(type_class, name, 'double')
    elif type_class == TypeClass.STRING:
        uno_type = PrimitiveType(type_class, name, 'rtl::OUString')
    elif type_class == TypeClass.TYPE:
        uno_type = PrimitiveType(type_class, name, 'com::sun::star::uno::Type')
    elif type_class == TypeClass.ANY:
        uno_type = PrimitiveType(type_class, name, 'com::sun::star::uno::Any')
    elif type_class == TypeClass.ENUM:
        uno_type = EnumType(val, full_val)
    elif type_class == TypeClass.TYPEDEF:
        pass
    elif type_class == TypeClass.STRUCT:
        uno_type = StructType(val, full_val)
    elif type_class == TypeClass.UNION:
        raise UnsupportedType('union')
    elif type_class == TypeClass.EXCEPTION:
        uno_type = CompoundType(val, full_val)
    elif type_class == TypeClass.SEQUENCE:
        uno_type = IndirectType(val, full_val)
    elif type_class == TypeClass.ARRAY:
        raise UnsupportedType('array')
    elif type_class == TypeClass.INTERFACE:
        uno_type = InterfaceType(val, full_val)
    elif type_class == TypeClass.SERVICE:
        raise UnsupportedType('service')
    elif type_class == TypeClass.MODULE:
        raise UnsupportedType('module')
    elif type_class == TypeClass.INTERFACE_METHOD:
        uno_type = InterfaceMethodType(val, full_val)
    elif type_class == TypeClass.INTERFACE_ATTRIBUTE:
        uno_type = InterfaceAttributeType(val, full_val)
    elif type_class == TypeClass.UNKNOWN:
        raise UnknownType(type)
    elif type_class == TypeClass.PROPERTY:
        pass
    elif type_class == TypeClass.CONSTANT:
        pass
    elif type_class == TypeClass.CONSTANTS:
        pass
    elif type_class == TypeClass.SINGLETON:
        pass
    else:
        raise UnknownType(type)

    assert uno_type
    return uno_type

def uno_cast(type, val):
    '''Casts val or pointer to UNO type represented by type'''
    if val.type.code == gdb.TYPE_CODE_PTR:
        return val.cast(type.type().pointer())
    else:
        return val.cast(type.type())

class VoidType(Type):

    def __init__(self):
        super(VoidType, self).__init__(TypeClass.VOID, "void")
        self.typename = "void"

class PrimitiveType(Type):

    def __init__(self, typeclass, typename_uno, typename_cpp):
        super(PrimitiveType, self).__init__(typeclass, typename_uno)
        self.typename = str(typename_cpp)

class CompoundType(Type):

    def __init__(self, type, full_type):
        super(CompoundType, self).__init__(type['eTypeClass'], type['pTypeName'].dereference())
        self.typename = self.uno2cpp(self.tag)
        self._type = full_type

    class _iterator(object):

        def __init__(self, count, types, names):
            self.count = count
            self.members = members
            self.names = names
            self.pos = 0

        def __iter__(self):
            return self

        def next(self):
            assert self.pos >= 0 and self.pos <= self.count
            if self.pos == self.count:
                raise StopIteration

            pmember = self.members[self.pos]
            assert pmember
            pname = self.names[self.i]
            assert pname
            self.pos = self.pos + 1
            member = make_uno_type(pmember.dereference())
            assert member
            name = str(pname.dereference())
            return (name, member)

    def attributes(self):
        return _iterator(self._type['nMembers'], self._type['ppTypeRefs'],
                self._type['ppMemberNames'])

class StructType(CompoundType):

    def __init__(self, type, full_type):
        full_type = full_type.cast(gdb.lookup_type('_typelib_StructTypeDescription'))
        super(StructType, self).__init__(type, full_type['aBase'])

class IndirectType(Type):

    def __init__(self, type, full_type):
        super(IndirectType, self).__init__(type['eTypeClass'], type['pTypeName'].dereference())
        full_type = full_type.cast(gdb.lookup_type('_typelib_IndirectTypeDescription'))
        pelem = full_type['pType']
        assert pelem
        self.element = make_uno_type(pelem.dereference())
        assert self.element
        self.typename = TemplateType('com::sun::star::uno::Sequence', self.element.typename)

class EnumType(Type):

    def __init__(self, type, full_type):
        super(EnumType, self).__init__(TypeClass.ENUM, type['pTypeName'].dereference())
        self.typename = self.uno2cpp(self.tag)
        self._type = full_type.cast(gdb.lookup_type('_typelib_EnumTypeDescription'))

    class _iterator(object):

        def __init__(self, count, values, names):
            self.count = count
            self.values = values
            self.names = names
            self.pos = 0

        def __iter__(self):
            return self

        def next(self):
            assert self.pos >= 0 and self.pos <= self.count
            if self.pos == self.count:
                raise StopIteration

            pvalue = self.values[self.pos]
            assert pvalue
            pname = self.names[self.pos]
            assert pname
            self.pos = self.pos + 1
            val = int(pvalue.dereference())
            name = str(pname.dereference())
            return (name, val)

    def values(self):
        return _iterator(self._type['nEnumValues'],
                self._type['ppEnumNames'], self._type['pEnumValues'])

    def default_value(self):
        return self._type['nDefaultEnumValue']

class InterfaceMemberType(Type):

    def __init__(self, type, full_type):
        super(InterfaceMemberType, self).__init__(type['eTypeClass'], type['pTypeName'].dereference())
        (interface, delim, member) = self.tag.partition('::')
        self.typename = self.uno2cpp(interface) + '::*' + member
        full_type = full_type.cast(gdb.lookup_type('_typelib_InterfaceMemberTypeDescription'))
        self.position = full_type['nPosition']
        pname = full_type['pMemberName']
        assert pname
        self.name = pname.dereference()

class InterfaceMethodType(InterfaceMemberType):

    def __init__(self, type, full_type):
        full_type = full_type.cast(gdb.lookup_type('_typelib_InterfaceMethodTypeDescription'))
        super(InterfaceMethodType, self).__init__(type, full_type['aBase'])
        pret = full_type['pReturnTypeRef']
        assert pret
        self.return_type = make_uno_type(pret.dereference())
        assert self.return_type
        self.oneway = full_type['bOneWay']
        self._type = full_type

    class _iterator(object):

        def __init__(self, count, values):
            self.count = count
            self.values = values
            self.pos = 0
            assert values

        def __iter__(self):
            return self

        def next(self):
            assert self.pos >= 0 and self.pos <= self.count
            if self.pos == self.count:
                raise StopIteration

            val = self.values[self.pos]
            self.pos = self.pos + 1
            return val

    class parameter(tuple):

        def __init__(self, type):
            self.__init_tuple(type)
            self.input = type['bIn']
            self.output = type['bOut']

        def _init_tuple(self, type):
            pname = self['pName']
            assert pname
            ptype = self['pTypeRef']
            assert ptype
            name = str(pname.dereference())
            type = make_uno_type(ptype.dereference())
            assert type
            super(parameter, self).__init__(name, type)

    def parameters(self):
        for param in _iterator(self._type['nParams'], self._type['pParams']):
            yield parameter(param)

    def exceptions(self):
        def make_exception(self, pex):
            assert pex
            ex = make_uno_type(pex.dereference())
            assert ex
            return ex

        for ex in _iterator(
                self._type['nExceptions'], self._type['ppExceptions']):
            yield make_exception(ex)

class InterfaceAttributeType(InterfaceMemberType):

    def __init__(self, type, full_type):
        full_type = full_type.cast(gdb.lookup_type('_typelib_InterfaceAttributeTypeDescription'))
        super(InterfaceAttributeType, self).__init__(type, full_type['aBase'])
        self.readonly = full_type['bReadOnly']
        ptype = full_type['pAttributeTypeRef']
        assert ptype
        self.type = make_uno_type(ptype.dereference())
        assert self.type

class MembersNotInitialized(Exception):
    '''Represents exception raised when interface type' members haven't
        been initialized(i.e. just level 1 initialization has been
        performed)'''
    pass

class InterfaceType(Type):

    def __init__(self, type, full_type):
        super(InterfaceType, self).__init__(TypeClass.INTERFACE, type['pTypeName'].dereference())
        assert int(type['eTypeClass']) == TypeClass.INTERFACE
        self.typename = self.uno2cpp(self.tag)
        full_type = full_type.cast(gdb.lookup_type('_typelib_InterfaceTypeDescription'))
        self.uik = full_type['aUik']
        self._type = full_type

    class _iterator(object):

        def __init__(self, count, values):
            assert values
            self.count = count
            self.values = values
            self.pos = 0

        def __iter__(self):
            return self

        def next(self):
            assert self.pos >= 0 and self.pos <= self.count
            pvalue = self.values[self.pos]
            assert pvalue
            self.pos = self.pos + 1
            uno = make_uno_type(pvalue.dereference())
            assert uno
            return uno

    def members(self):
        return __members(self._type['nMembers'], self._type['ppMembers'])

    def all_members(self):
        return __members(self._type['nAllMembers'], self._type['ppAllMembers'])

    def __members(count, values):
        if values == 0:
            raise MembersNotInitialized
        return _iterator(count, values)

    def bases(self):
        return _iterator(self._type['nBaseTypes'], self._type['ppBaseTypes'])

# vim:set shiftwidth=4 softtabstop=4 expandtab: