This file is indexed.

/usr/lib/python2.7/dist-packages/pygccxml/declarations/type_traits.py is in python-pygccxml 1.8.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
540
541
542
# Copyright 2014-2016 Insight Software Consortium.
# Copyright 2004-2008 Roman Yakovenko.
# Distributed under the Boost Software License, Version 1.0.
# See http://www.boost.org/LICENSE_1_0.txt

"""
defines few algorithms, that deals with different C++ type properties

Are you aware of `boost::type_traits <http://www.boost.org/doc/libs/1_37_0/
libs/type_traits/doc/html/boost_typetraits/intro.html>`_
library? pygccxml implements the same functionality.

This module contains a set of very specific traits functions\\classes, each of
which encapsulate a single trait from the C++ type system. For example:
* is a type a pointer or a reference type ?
* does a type have a trivial constructor ?
* does a type have a const-qualifier ?

"""

from . import cpptypes
from . import typedef
from . import namespace
from .. import utils


def __remove_alias(type_):
    """implementation details"""
    if isinstance(type_, typedef.typedef_t):
        return __remove_alias(type_.decl_type)
    if isinstance(type_, cpptypes.declarated_t) and \
            isinstance(type_.declaration, typedef.typedef_t):
        return __remove_alias(type_.declaration.decl_type)
    if isinstance(type_, cpptypes.compound_t):
        type_.base = __remove_alias(type_.base)
        return type_
    return type_


def remove_alias(type_):
    """returns type without typedefs"""
    type_ref = None
    if isinstance(type_, cpptypes.type_t):
        type_ref = type_
    elif isinstance(type_, typedef.typedef_t):
        type_ref = type_.decl_type
    else:
        pass  # not a valid input, just return it
    if not type_ref:
        return type_
    if type_ref.cache.remove_alias:
        return type_ref.cache.remove_alias
    no_alias = __remove_alias(type_ref.clone())
    type_ref.cache.remove_alias = no_alias
    return no_alias


def decompose_type(tp):
    """implementation details"""
    # implementation of this function is important
    if isinstance(tp, cpptypes.compound_t):
        return [tp] + decompose_type(tp.base)
    elif isinstance(tp, typedef.typedef_t):
        return decompose_type(tp.decl_type)
    elif isinstance(tp, cpptypes.declarated_t) and \
            isinstance(tp.declaration, typedef.typedef_t):
        return decompose_type(tp.declaration.decl_type)
    else:
        return [tp]


def decompose_class(type_):
    """implementation details"""
    types = decompose_type(type_)
    return [tp.__class__ for tp in types]


def base_type(type_):
    """returns base type.

    For `const int` will return `int`
    """
    types = decompose_type(type_)
    return types[-1]


def _create_cv_types(base):
    """
    Implementation detail.

    """
    return (
        [base,
         cpptypes.const_t(base),
         cpptypes.volatile_t(base),
         cpptypes.volatile_t(cpptypes.const_t(base))]
    )

# Some tuples containing combinations of different types
# These are created once the module is loaded, so that when they are used
# they do not need to be re-created.
_void_def = _create_cv_types(cpptypes.void_t())
_bool_def = _create_cv_types(cpptypes.bool_t())
_float_def = (
    _create_cv_types(cpptypes.float_t()) +
    _create_cv_types(cpptypes.double_t()) +
    _create_cv_types(cpptypes.long_double_t()))
_integral_def = (
    _create_cv_types(cpptypes.char_t()) +
    _create_cv_types(cpptypes.unsigned_char_t()) +
    _create_cv_types(cpptypes.signed_char_t()) +
    _create_cv_types(cpptypes.wchar_t()) +
    _create_cv_types(cpptypes.short_int_t()) +
    _create_cv_types(cpptypes.short_unsigned_int_t()) +
    _create_cv_types(cpptypes.bool_t()) +
    _create_cv_types(cpptypes.int_t()) +
    _create_cv_types(cpptypes.unsigned_int_t()) +
    _create_cv_types(cpptypes.long_int_t()) +
    _create_cv_types(cpptypes.long_unsigned_int_t()) +
    _create_cv_types(cpptypes.long_long_int_t()) +
    _create_cv_types(cpptypes.long_long_unsigned_int_t()) +
    _create_cv_types(cpptypes.int128_t()) +
    _create_cv_types(cpptypes.uint128_t()))


def does_match_definition(given, main, secondary):
    """implementation details"""
    assert isinstance(secondary, tuple)
    assert 2 == len(secondary)  # general solution could be provided
    types = decompose_type(given)
    if isinstance(types[0], main):
        return True
    elif 2 <= len(types) and \
        ((isinstance(types[0], main) and isinstance(types[1], secondary)) or
            (isinstance(types[1], main) and isinstance(types[0], secondary))):
        return True
    elif 3 <= len(types):
        classes = set([tp.__class__ for tp in types[:3]])
        desired = set([main] + list(secondary))
        diff = classes.symmetric_difference(desired)
        if not diff:
            return True
        if len(diff) == 2:
            items = list(diff)
            return (
                issubclass(
                    items[0], items[1]) or issubclass(items[1], items[0]))
        else:
            return False
    else:
        return False


def is_bool(type_):
    """
    Check if type is of boolean type.

    Args:
        type_ (type_t): The type to be checked

    Returns:
        bool: True if type is a boolean, False otherwise.
    """
    return remove_alias(type_) in _bool_def


def is_void(type_):
    """
    Check if type is of void type.

    Args:
        type_ (type_t): The type to be checked

    Returns:
        bool: True if type is void, False otherwise.
    """
    return remove_alias(type_) in _void_def


def is_void_pointer(type_):
    """returns True, if type represents `void*`, False otherwise"""
    return is_same(type_, cpptypes.pointer_t(cpptypes.void_t()))


def is_integral(type_):
    """
    Check if type is a C++ integral type

    Args:
        type_ (type_t): The type to be checked

    Returns:
        bool: True if type is a C++ integral type, False otherwise.
    """
    return remove_alias(type_) in _integral_def


def is_floating_point(type_):
    """returns True, if type represents C++ floating point type,
    False otherwise"""

    return remove_alias(type_) in _float_def


def is_arithmetic(type_):
    """returns True, if type represents C++ integral or floating point type,
    False otherwise"""
    return is_integral(type_) or is_floating_point(type_)


def is_pointer(type_):
    """returns True, if type represents C++ pointer type, False otherwise"""
    return does_match_definition(type_,
                                 cpptypes.pointer_t,
                                 (cpptypes.const_t, cpptypes.volatile_t)) \
        or does_match_definition(type_,
                                 cpptypes.pointer_t,
                                 (cpptypes.volatile_t, cpptypes.const_t))


def is_calldef_pointer(type_):
    """returns True, if type represents pointer to free/member function,
    False otherwise"""
    if not is_pointer(type_):
        return False
    nake_type = remove_alias(type_)
    nake_type = remove_cv(nake_type)
    return isinstance(nake_type, cpptypes.compound_t) \
        and isinstance(nake_type.base, cpptypes.calldef_type_t)


def remove_pointer(type_):
    """removes pointer from the type definition

    If type is not pointer type, it will be returned as is.
    """
    nake_type = remove_alias(type_)
    if not is_pointer(nake_type):
        return type_
    elif isinstance(nake_type, cpptypes.volatile_t) and \
            isinstance(nake_type.base, cpptypes.pointer_t):
        return cpptypes.volatile_t(nake_type.base.base)
    elif isinstance(nake_type, cpptypes.const_t) and \
            isinstance(nake_type.base, cpptypes.pointer_t):
        return cpptypes.const_t(nake_type.base.base)
    elif isinstance(nake_type, cpptypes.volatile_t) \
            and isinstance(nake_type.base, cpptypes.const_t) \
            and isinstance(nake_type.base.base, cpptypes.pointer_t):
        return (
            cpptypes.volatile_t(cpptypes.const_t(nake_type.base.base.base))
        )
    else:
        return nake_type.base


def is_reference(type_):
    """returns True, if type represents C++ reference type, False otherwise"""
    nake_type = remove_alias(type_)
    return isinstance(nake_type, cpptypes.reference_t)


def is_array(type_):
    """returns True, if type represents C++ array type, False otherwise"""
    nake_type = remove_alias(type_)
    nake_type = remove_reference(nake_type)
    nake_type = remove_cv(nake_type)
    return isinstance(nake_type, cpptypes.array_t)


def array_size(type_):
    """returns array size"""
    nake_type = remove_alias(type_)
    nake_type = remove_reference(nake_type)
    nake_type = remove_cv(nake_type)
    assert isinstance(nake_type, cpptypes.array_t)
    return nake_type.size


def array_item_type(type_):
    """returns array item type"""
    if is_array(type_):
        type_ = remove_alias(type_)
        type_ = remove_cv(type_)
        return type_.base
    elif is_pointer(type_):
        return remove_pointer(type_)
    else:
        raise RuntimeError(
            "array_item_type functions takes as argument array or pointer " +
            "types")


def remove_reference(type_):
    """removes reference from the type definition

    If type is not reference type, it will be returned as is.
    """
    nake_type = remove_alias(type_)
    if not is_reference(nake_type):
        return type_
    else:
        return nake_type.base


def is_const(type_):
    """returns True, if type represents C++ const type, False otherwise"""
    nake_type = remove_alias(type_)
    if isinstance(nake_type, cpptypes.const_t):
        return True
    elif isinstance(nake_type, cpptypes.volatile_t):
        return is_const(nake_type.base)
    elif isinstance(nake_type, cpptypes.array_t):
        return is_const(nake_type.base)
    return False


def remove_const(type_):
    """removes const from the type definition

    If type is not const type, it will be returned as is
    """

    nake_type = remove_alias(type_)
    if not is_const(nake_type):
        return type_
    else:
        # Handling for const and volatile qualified types. There is a
        # difference in behavior between GCCXML and CastXML for cv-qual arrays.
        # GCCXML produces the following nesting of types:
        #       ->  volatile_t(const_t(array_t))
        # while CastXML produces the following nesting:
        #       ->  array_t(volatile_t(const_t))
        # For both cases, we must unwrap the types, remove const_t, and add
        # back the outer layers
        if isinstance(nake_type, cpptypes.array_t):
            is_v = is_volatile(nake_type)
            if is_v:
                result_type = nake_type.base.base.base
            else:
                result_type = nake_type.base.base
            if is_v:
                result_type = cpptypes.volatile_t(result_type)
            return cpptypes.array_t(result_type, nake_type.size)

        elif isinstance(nake_type, cpptypes.volatile_t):
            return cpptypes.volatile_t(nake_type.base.base)

        return nake_type.base


def remove_declarated(type_):
    """removes type-declaration class-binder :class:`declarated_t` from
    the `type_`

    If `type_` is not :class:`declarated_t`, it will be returned as is
    """
    type_ = remove_alias(type_)
    if isinstance(type_, cpptypes.declarated_t):
        type_ = type_.declaration
    return type_


def is_same(type1, type2):
    """returns True, if type1 and type2 are same types"""
    nake_type1 = remove_declarated(type1)
    nake_type2 = remove_declarated(type2)
    return nake_type1 == nake_type2


def is_volatile(type_):
    """returns True, if type represents C++ volatile type, False otherwise"""
    nake_type = remove_alias(type_)
    if isinstance(nake_type, cpptypes.volatile_t):
        return True
    elif isinstance(nake_type, cpptypes.const_t):
        return is_volatile(nake_type.base)
    elif isinstance(nake_type, cpptypes.array_t):
        return is_volatile(nake_type.base)
    return False


def remove_volatile(type_):
    """removes volatile from the type definition

    If type is not volatile type, it will be returned as is
    """
    nake_type = remove_alias(type_)
    if not is_volatile(nake_type):
        return type_
    else:
        if isinstance(nake_type, cpptypes.array_t):
            is_c = is_const(nake_type)
            if is_c:
                base_type = nake_type.base.base.base
            else:
                base_type = nake_type.base.base
            result_type = base_type
            if is_c:
                result_type = cpptypes.const_t(result_type)
            return cpptypes.array_t(result_type, nake_type.size)
        return nake_type.base


def remove_cv(type_):
    """removes const and volatile from the type definition"""

    nake_type = remove_alias(type_)
    if not is_const(nake_type) and not is_volatile(nake_type):
        return type_
    result = nake_type
    if is_const(result):
        result = remove_const(result)
    if is_volatile(result):
        result = remove_volatile(result)
    if is_const(result):
        result = remove_const(result)
    return result


def is_fundamental(type_):
    """returns True, if type represents C++ fundamental type"""
    return does_match_definition(
        type_,
        cpptypes.fundamental_t,
        (cpptypes.const_t, cpptypes.volatile_t)) \
        or does_match_definition(
            type_,
            cpptypes.fundamental_t,
            (cpptypes.volatile_t, cpptypes.const_t))


def is_defined_in_xxx(xxx, cls):
    """
    small helper function, that checks whether the class `cls` is defined
    under `::xxx` namespace
    """
    if not cls.parent:
        return False

    if not isinstance(cls.parent, namespace.namespace_t):
        return False

    if xxx != cls.parent.name:
        return False

    xxx_ns = cls.parent
    if not xxx_ns.parent:
        return False

    if not isinstance(xxx_ns.parent, namespace.namespace_t):
        return False

    if '::' != xxx_ns.parent.name:
        return False

    global_ns = xxx_ns.parent
    return None is global_ns.parent


string_equivalences = [
    ('::std::basic_string<char,std::char_traits<char>,' +
        'std::allocator<char> >'),
    ('::std::basic_string<char, std::char_traits<char>, ' +
        'std::allocator<char> >'),
    '::std::basic_string<char>', '::std::string']

wstring_equivalences = [
    ('::std::basic_string<wchar_t,std::char_traits<wchar_t>,' +
        'std::allocator<wchar_t> >'),
    ('::std::basic_string<wchar_t, std::char_traits<wchar_t>, ' +
        'std::allocator<wchar_t> >'),
    '::std::basic_string<wchar_t>', '::std::wstring']

ostream_equivalences = [
    '::std::basic_ostream<char, std::char_traits<char> >',
    '::std::basic_ostream<char,std::char_traits<char> >',
    '::std::basic_ostream<char>', '::std::ostream']

wostream_equivalences = [
    '::std::basic_ostream<wchar_t, std::char_traits<wchar_t> >',
    '::std::basic_ostream<wchar_t,std::char_traits<wchar_t> >',
    '::std::basic_ostream<wchar_t>', '::std::wostream']


def is_std_string(type_):
    """
    Returns True, if type represents C++ `std::string`, False otherwise.

    """

    if utils.is_str(type_):
        return type_ in string_equivalences
    else:
        type_ = remove_alias(type_)
        type_ = remove_reference(type_)
        type_ = remove_cv(type_)
        return type_.decl_string in string_equivalences


def is_std_wstring(type_):
    """
    Returns True, if type represents C++ `std::wstring`, False otherwise.

    """

    if utils.is_str(type_):
        return type_ in wstring_equivalences
    else:
        type_ = remove_alias(type_)
        type_ = remove_reference(type_)
        type_ = remove_cv(type_)
        return type_.decl_string in wstring_equivalences


def is_std_ostream(type_):
    """
    Returns True, if type represents C++ std::ostream, False otherwise.

    """

    if utils.is_str(type_):
        return type_ in ostream_equivalences
    else:
        type_ = remove_alias(type_)
        type_ = remove_reference(type_)
        type_ = remove_cv(type_)
        return type_.decl_string in ostream_equivalences


def is_std_wostream(type_):
    """
    Returns True, if type represents C++ std::wostream, False otherwise.

    """

    if utils.is_str(type_):
        return type_ in wostream_equivalences
    else:
        type_ = remove_alias(type_)
        type_ = remove_reference(type_)
        type_ = remove_cv(type_)
        return type_.decl_string in wostream_equivalences