def cpp_type_to_python(self, ot: str): """ convert to basic type combination :param t: full name of type :return: """ t = ot t = remove_cvref(t) t = self._remove_variable_type_prefix(t) try: return cpp_base_type_to_python(t) except KeyError: pass if is_function_pointer_type(t): func = function_pointer_type_info(t) args = ",".join( [self.cpp_type_to_python(arg.type) for arg in func.args]) return f'Callable[[{args}], {self.cpp_type_to_python(func.ret_type)}]' if is_pointer_type(t): cpp_base = self.resolve_to_basic_type_remove_const(pointer_base(t)) if is_pointer_type(cpp_base) or is_array_type(cpp_base): return f'"level 2 pointer:{t}"' # un-convertible: level 2 pointer if cpp_base in STRING_ARRAY_BASES: return 'str' return self.cpp_type_to_python(cpp_base) if is_array_type(t): b = array_base(t) if b in STRING_ARRAY_BASES: # special case: string array return 'str' base = self.cpp_type_to_python(b) return f'List[{base}]' if is_tuple_type(t): es = tuple_elements(t) bases = [self.cpp_type_to_python(i) for i in es] bases_str = ",".join(bases) return f'Tuple[{bases_str}]' # check classes objects = self.objects if t in objects: o = objects[t] if isinstance(o, GeneratorClass) or isinstance(o, GeneratorEnum): return t.replace("::", ".").strip(" .") # todo fix this if isinstance(o, GeneratorTypedef): return self.cpp_type_to_python(o.target) if t.startswith("(anonymous"): return f'"{t}"' # this means this is logger.warning( "%s might be an internal symbol, failed to resolve to basic type", t) return t
def _is_type_supported(self, t: str): t = self.type_manager.resolve_to_basic_type_remove_const(t) if is_pointer_type(t): b = pointer_base(t) if is_pointer_type(b): return False # level 2+ pointers if is_array_type(b): return False if is_array_type(t): b = array_base(t) if is_pointer_type(b): return False # level 2+ pointers if is_array_type(b): return False return True
def is_string_array_type(t: str): if is_array_type(t): base = array_base(t) elif is_pointer_type(t): base = pointer_base(t) else: return False return is_string_type(remove_cvref(base))
def remove_decorations(self, ot: str): """ remove pointers, array, cvref """ t = remove_cvref(ot) if is_pointer_type(t): return self.remove_decorations(pointer_base(t)) if is_array_type(t): return self.remove_decorations(array_base(t)) return t
def match(self, f: GeneratorFunction, i: int, a: GeneratorVariable): if is_reference_type(a.type): if not is_const_type(a.type): return True t = self.type_manager.resolve_to_basic_type_remove_const(a.type) if is_string_type(t): return False # in most of the case char * is a input string if is_pointer_type(t): base = pointer_base(t) if is_integer_type(base): return True if is_string_type(base): return True
def resolve_to_basic_type(self, ot: str): t = remove_cvref(ot) if is_pointer_type(t): return self.resolve_to_basic_type(pointer_base(t)) + " *" if is_array_type(t): base = self.resolve_to_basic_type(array_base(t)) if is_std_vector(t): return f'std::vector<{self.resolve_to_basic_type(base)}>' return f'{base} [{array_count_str(t)}]' try: obj = self.objects[t] if isinstance(obj, GeneratorTypedef) and obj.full_name != obj.target: return self.resolve_to_basic_type(obj.target) except KeyError: pass return t