Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_container_or_none(self, type_):
"""returns reference to the class declaration or None"""
type_ = type_traits.remove_alias(type_)
type_ = type_traits.remove_cv(type_)
cls = None
if isinstance(type_, cpptypes.declarated_t):
cls = type_traits.remove_alias(type_.declaration)
elif isinstance(type_, class_declaration.class_t):
cls = type_
elif isinstance(type_, class_declaration.class_declaration_t):
cls = type_
else:
return
if not cls.name.startswith(self.name() + '<'):
return
for ns in std_namespaces:
if type_traits.impl_details.is_defined_in_xxx(ns, cls):
return cls
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()) +
_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 len(secondary) == 2 # general solution could be provided
types = decompose_type(given)
if isinstance(types[0], main):
return True
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 _is_ostream(self, cpp_type):
return (isinstance(cpp_type, cpptypes.reference_t)
and not isinstance(cpp_type.base, cpptypes.const_t)
and str(cpp_type.base) == 'std::ostream')
recursive=False, allow_empty=True):
type_from_name = normalize_name(str(alias.type))
type_to_name = normalize_name(utils.ascii('::'.join([module.cpp_namespace_prefix, alias.name])))
for sym in '', '*', '&':
typehandlers.base.add_type_alias(type_from_name+sym, type_to_name+sym)
pygen_sink = self._get_pygen_sink_for_definition(alias)
if pygen_sink:
pygen_sink.writeln("typehandlers.add_type_alias(%r, %r)" % (type_from_name+sym, type_to_name+sym))
## Look for forward declarations of class/structs like
## "typedef struct _Foo Foo"; these are represented in
## pygccxml by a typedef whose .type.declaration is a
## class_declaration_t instead of class_t.
if isinstance(alias.type, cpptypes.declarated_t):
cls = alias.type.declaration
if templates.is_instantiation(cls.decl_string):
continue # typedef to template instantiations, must be fully defined
if isinstance(cls, class_declaration_t):
global_annotations, param_annotations = annotations_scanner.get_annotations(cls)
for hook in self._pre_scan_hooks:
hook(self, cls, global_annotations, param_annotations)
if 'ignore' in global_annotations:
continue
kwargs = dict()
self._apply_class_annotations(cls, global_annotations, kwargs)
kwargs.setdefault("incomplete_type", True)
kwargs.setdefault("automatic_type_narrowing", False)
kwargs.setdefault("allow_subclassing", False)
_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 len(secondary) == 2 # general solution could be provided
types = decompose_type(given)
if isinstance(types[0], main):
return True
if len(types) >= 2:
cond1 = isinstance(types[0], main)
cond2 = isinstance(types[1], secondary)
cond3 = isinstance(types[1], main)
def dig_declarations(depend_on_it):
if isinstance(depend_on_it, declaration.declaration_t):
return [depend_on_it]
base_type = type_traits.base_type(
type_traits.remove_alias(depend_on_it))
if isinstance(base_type, cpptypes.declarated_t):
return [base_type.declaration]
elif isinstance(base_type, cpptypes.calldef_type_t):
result = []
result.extend(impl_details.dig_declarations(base_type.return_type))
for argtype in base_type.arguments_types:
result.extend(impl_details.dig_declarations(argtype))
if isinstance(base_type, cpptypes.member_function_type_t):
result.extend(
impl_details.dig_declarations(
base_type.class_inst))
return result
return []
def visit_typedef(self):
typedef = self.decl
base_type = dump_type(typedef.decl_type)
if base_type == typedef.name:
# Ignore `typedef struct Foo Foo;`
return
if base_type == '':
if isinstance(typedef.decl_type, declarations.cpptypes.declarated_t):
base_decl = typedef.decl_type.declaration
self.visit_struct(typedef.name, base_decl)
return
print(r'%s = Alias(%r, %s)' % (typedef.name, typedef.name, base_type))
print()
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_