def _get_alias(self): if not self._alias: directives = self.__select_alias_directives(be_smart=True) if 1 == len(directives): self._alias = directives[0].name else: if declarations.templates.is_instantiation(self.name): container_aliases = [ 'value_type', 'key_type', 'mapped_type' ] if isinstance( self, declarations.class_t ) \ and 1 == len( set( map( lambda typedef: typedef.name, self.aliases ) ) ) \ and self.aliases[0].name not in container_aliases: self._alias = self.aliases[0].name else: self._alias = algorithm.create_valid_name( self.partial_name) else: if declarations.is_class( self) or declarations.is_class_declaration(self): self._alias = algorithm.create_valid_name( self.partial_name) else: self._alias = self.partial_name return self._alias
def _treat_indexing_suite(self): def create_explanation(cls): msg = '//WARNING: the next line of code will not compile, because "%s" does not have operator== !' msg = msg % cls.indexing_suite.element_type.decl_string return code_creators.custom_text_t(msg, False) def create_cls_cc(cls): if isinstance(cls, declarations.class_t): return code_creators.class_t(class_inst=cls) else: return code_creators.class_declaration_t(class_inst=cls) if not self.__types_db.used_containers: return creators = [] created_value_traits = set() for cls in self.__get_exposed_containers(): self.__print_readme(cls) cls_creator = create_cls_cc(cls) self.__dependencies_manager.add_exported(cls) creators.append(cls_creator) try: element_type = cls.indexing_suite.element_type except: element_type = None if isinstance(cls.indexing_suite, decl_wrappers.indexing_suite1_t): if not ( None is element_type ) \ and declarations.is_class( element_type ) \ and not declarations.has_public_equal( element_type ): cls_creator.adopt_creator(create_explanation(cls)) cls_creator.adopt_creator(code_creators.indexing_suite1_t(cls)) else: class_traits = declarations.class_traits if not (None is element_type) and class_traits.is_my_case( element_type): value_cls = class_traits.get_declaration(element_type) has_prerequisits = value_cls.less_than_comparable \ and value_cls.equality_comparable if (not has_prerequisits) and ( value_cls not in created_value_traits): created_value_traits.add(value_cls) element_type_cc = code_creators.value_traits_t( value_cls) self.__extmodule.adopt_declaration_creator( element_type_cc) cls_creator.adopt_creator(code_creators.indexing_suite2_t(cls)) scfo = self.__std_containers_free_operators if cls in scfo: for operator in scfo[cls]: self.__dependencies_manager.add_exported(operator) cls_creator.adopt_creator( code_creators.operator_t(operator=operator)) creators.reverse() self.__module_body.adopt_creators(creators, 0)
def find_class(type_): type_ = declarations.remove_reference(type_) if declarations.is_class(type_): return declarations.class_traits.get_declaration(type_) elif declarations.is_class_declaration(type_): return declarations.class_declaration_traits.get_declaration(type_) else: return None
def find_class( type_ ): type_ = declarations.remove_reference( type_ ) if declarations.is_class( type_ ): return declarations.class_traits.get_declaration( type_ ) elif declarations.is_class_declaration( type_ ): return declarations.class_declaration_traits.get_declaration( type_ ) else: return None
def _treat_indexing_suite( self ): def create_explanation(cls): msg = '//WARNING: the next line of code will not compile, because "%s" does not have operator== !' msg = msg % cls.indexing_suite.element_type.decl_string return code_creators.custom_text_t( msg, False ) def create_cls_cc( cls ): if isinstance( cls, declarations.class_t ): return code_creators.class_t( class_inst=cls ) else: return code_creators.class_declaration_t( class_inst=cls ) if not self.__types_db.used_containers: return creators = [] created_value_traits = set() cmp_by_name = lambda cls1, cls2: cmp( cls1.decl_string, cls2.decl_string ) used_containers = list( self.__types_db.used_containers ) used_containers = filter( lambda cls: cls.indexing_suite.include_files , used_containers ) used_containers.sort( cmp_by_name ) for cls in used_containers: self.__print_readme( cls ) if cls.already_exposed: continue cls_creator = create_cls_cc( cls ) self.__dependencies_manager.add_exported( cls ) creators.append( cls_creator ) try: element_type = cls.indexing_suite.element_type except: element_type = None if isinstance( cls.indexing_suite, decl_wrappers.indexing_suite1_t ): if not ( None is element_type ) \ and declarations.is_class( element_type ) \ and not declarations.has_public_equal( element_type ): cls_creator.adopt_creator( create_explanation( cls ) ) cls_creator.adopt_creator( code_creators.indexing_suite1_t(cls) ) else: class_traits = declarations.class_traits if not ( None is element_type ) and class_traits.is_my_case( element_type ): value_cls = class_traits.get_declaration( element_type ) has_prerequisits = value_cls.less_than_comparable \ and value_cls.equality_comparable if ( not has_prerequisits ) and ( value_cls not in created_value_traits ): created_value_traits.add( value_cls ) element_type_cc = code_creators.value_traits_t( value_cls ) self.__extmodule.adopt_declaration_creator( element_type_cc ) cls_creator.adopt_creator( code_creators.indexing_suite2_t(cls) ) creators.reverse() self.__module_body.adopt_creators( creators, 0 )
def _treat_indexing_suite(self): def create_explanation(cls): msg = '//WARNING: the next line of code will not compile, because "%s" does not have operator== !' msg = msg % cls.indexing_suite.element_type.decl_string return code_creators.custom_text_t(msg, False) def create_cls_cc(cls): if isinstance(cls, declarations.class_t): return code_creators.class_t(class_inst=cls) else: return code_creators.class_declaration_t(class_inst=cls) if not self.__types_db.used_containers: return creators = [] created_value_traits = set() for cls in self.__get_exposed_containers(): self.__print_readme(cls) cls_creator = create_cls_cc(cls) self.__dependencies_manager.add_exported(cls) creators.append(cls_creator) try: element_type = cls.indexing_suite.element_type except: element_type = None if isinstance(cls.indexing_suite, decl_wrappers.indexing_suite1_t): if ( not (None is element_type) and declarations.is_class(element_type) and not declarations.has_public_equal(element_type) ): cls_creator.adopt_creator(create_explanation(cls)) cls_creator.adopt_creator(code_creators.indexing_suite1_t(cls)) else: class_traits = declarations.class_traits if not (None is element_type) and class_traits.is_my_case(element_type): value_cls = class_traits.get_declaration(element_type) has_prerequisits = value_cls.less_than_comparable and value_cls.equality_comparable if (not has_prerequisits) and (value_cls not in created_value_traits): created_value_traits.add(value_cls) element_type_cc = code_creators.value_traits_t(value_cls) self.__extmodule.adopt_declaration_creator(element_type_cc) cls_creator.adopt_creator(code_creators.indexing_suite2_t(cls)) scfo = self.__std_containers_free_operators if cls in scfo: for operator in scfo[cls]: self.__dependencies_manager.add_exported(operator) cls_creator.adopt_creator(code_creators.operator_t(operator=operator)) creators.reverse() self.__module_body.adopt_creators(creators, 0)
def applyDefaultReturnPolicies(functions): for f in functions: if not f.call_policies: return_type = f.return_type if declarations.is_reference(return_type) or declarations.is_pointer(return_type): type_info = return_type type_info = declarations.remove_pointer(type_info) type_info = declarations.remove_reference(type_info) type_info = declarations.remove_const(type_info) # Se il tipo non e' esposto (potrebbe essere una classe, ma non ci sono informazioni perche' la dichiarazione non e' stata incontrata), viene gestito tramite return_opaque_pointer if declarations.is_class(type_info): f.call_policies = call_policies.return_value_policy(call_policies.reference_existing_object) else: f.call_policies = call_policies.return_value_policy(call_policies.return_opaque_pointer)
def _get_alias(self): if not self._alias: if declarations.templates.is_instantiation(self.name): self._alias = algorithm.create_valid_name(self.partial_name) else: if declarations.is_class( self) or declarations.is_class_declaration(self): self._alias = algorithm.create_valid_name( self.partial_name) else: self._alias = self.partial_name if callable(self._alias): return self._alias() return self._alias
def getTypePPA(ptype): if decls.is_pointer(ptype): return TypedefSeq(ptype) # specify return value elif decls.is_fundamental(ptype): return [] elif decls.is_class(ptype): return pyscope.CClass(ptype, pyresolvable.resolveNS(ptype)) # unknown / not yet implemented type. # This is treated as class => there may be some complications. else: print "WARNING: unknown type:" + str(ptype) return pyscope.CClass(ptype, pyresolvable.resolveNS(ptype))
def getTypePPA(ptype): if decls.is_pointer(ptype): return TypedefSeq(ptype) # specify return value elif decls.is_fundamental(ptype): return [] elif decls.is_class(ptype): return pyscope.CClass(ptype, pyresolvable.resolveNS(ptype)) # unknown / not yet implemented type. # This is treated as class => there may be some complications. else: print 'WARNING: unknown type:' + str(ptype) return pyscope.CClass(ptype, pyresolvable.resolveNS(ptype))
def finalize_doc_lines(self, doc_lines): if not doc_lines: return None final_doc_lines = [ line.replace("\n","\\n") for line in doc_lines[:-1] ] final_doc_lines.append(doc_lines[-1].replace("\n","")) # remove blank lines at the beginning and end while final_doc_lines and final_doc_lines[0] == "": final_doc_lines = final_doc_lines[1:] while final_doc_lines and final_doc_lines[-1] == "": final_doc_lines = final_doc_lines[:-1] # class docstrings should start and end with a blank line # http://www.python.org/dev/peps/pep-0257/ if declarations.is_class(self.declaration): # print "is class", self.declaration final_doc_lines.append("") final_doc_lines.insert(0, "") pass return '\"' + '\\n'.join(final_doc_lines) + '\"'
def finalize_doc_lines(self, doc_lines): if not doc_lines: return None final_doc_lines = [ line.replace("\n", "\\n") for line in doc_lines[:-1] ] final_doc_lines.append(doc_lines[-1].replace("\n", "")) # remove blank lines at the beginning and end while final_doc_lines and final_doc_lines[0] == "": final_doc_lines = final_doc_lines[1:] while final_doc_lines and final_doc_lines[-1] == "": final_doc_lines = final_doc_lines[:-1] # class docstrings should start and end with a blank line # http://www.python.org/dev/peps/pep-0257/ if declarations.is_class(self.declaration): # print "is class", self.declaration final_doc_lines.append("") final_doc_lines.insert(0, "") pass return '\"' + '\\n'.join(final_doc_lines) + '\"'
def _get_alias(self): if not self._alias: directives = self.__select_alias_directives(be_smart=True) if 1 == len( directives ): self._alias = directives[0].name else: if declarations.templates.is_instantiation( self.name ): container_aliases = [ 'value_type', 'key_type', 'mapped_type' ] if isinstance( self, declarations.class_t ) \ and 1 == len( set( [typedef.name for typedef in self.aliases] ) ) \ and self.aliases[0].name not in container_aliases: self._alias = self.aliases[0].name else: self._alias = algorithm.create_valid_name( self.partial_name ) else: if declarations.is_class( self ) or declarations.is_class_declaration( self ): self._alias = algorithm.create_valid_name( self.partial_name ) else: self._alias = self.partial_name return self._alias
def find_out_opaque_decl( type_, ensure_opaque_decl ): naked_type = declarations.remove_cv( type_ ) if not declarations.is_pointer( naked_type ): return None naked_type = declarations.remove_pointer( declarations.remove_cv( type_ ) ) if decl_wrappers.python_traits.is_immutable( naked_type ): return None#immutable types could not be opaque decl = None if declarations.is_class( naked_type ): decl = declarations.class_traits.get_declaration( naked_type ) elif declarations.is_class_declaration( naked_type ):#class declaration: decl = declarations.class_declaration_traits.get_declaration( naked_type ) else: return None if ensure_opaque_decl: if decl.opaque: return decl else: return None else: return decl
def find_out_opaque_decl(type_, ensure_opaque_decl): naked_type = declarations.remove_cv(type_) if not declarations.is_pointer(naked_type): return None naked_type = declarations.remove_pointer(declarations.remove_cv(type_)) if decl_wrappers.python_traits.is_immutable(naked_type): return None #immutable types could not be opaque decl = None if declarations.is_class(naked_type): decl = declarations.class_traits.get_declaration(naked_type) elif declarations.is_class_declaration(naked_type): #class declaration: decl = declarations.class_declaration_traits.get_declaration( naked_type) else: return None if ensure_opaque_decl: if decl.opaque: return decl else: return None else: return decl
def finalize_doc_lines(self, doc_lines): if not doc_lines: return None final_doc_lines = [ line.replace("\n","\\n") for line in doc_lines[:-1] ] final_doc_lines.append(doc_lines[-1].replace("\n","")) # remove blank lines at the beginning and end while final_doc_lines and final_doc_lines[0] == "": final_doc_lines = final_doc_lines[1:] while final_doc_lines and final_doc_lines[-1] == "": final_doc_lines = final_doc_lines[:-1] # class docstrings should start and end with a blank line # http://www.python.org/dev/peps/pep-0257/ if declarations.is_class(self.declaration): # print "is class", self.declaration final_doc_lines.append("") final_doc_lines.insert(0, "") pass result = '\"' + '\\n'.join(final_doc_lines) + '\"' if isinstance( result, unicode ): result = unicodedata.normalize('NFKD', result).encode('ascii','ignore') assert isinstance(result, str) return result
def _create_member_list(struct_info): new_type_members = [] for i in struct_info.variables(): if i.type_qualifiers.has_static: continue bitfield = None enumer = None if declarations.is_integral(i.decl_type): try: for enumerator in struct_info.enumerations(): if enumerator.byte_size != declarations.remove_alias( i.decl_type).byte_size: continue elif enumerator.name != snake_to_camel(i.name): continue else: bitfield = enumerator.get_name2value_dict() break except RuntimeError: bitfield = None elif declarations.is_enum(i.decl_type): try: enumer = global_namespace.enumeration( i.decl_type.decl_string).get_name2value_dict() except RuntimeError: enumer = {} if declarations.is_class(i.decl_type): new_type_members.append( DeclaredMemberStruct( i.name, _create_member_list(i.decl_type.declaration))) else: new_type_members.append( DeclaredMember(i.name, _convert_opaque_to_token(i.decl_type), bitfield=bitfield, enumer=enumer)) return new_type_members
def _create_member_list(struct_info): new_type_members = [] for i in struct_info.variables(): bitfield = None if declarations.is_integral(i.decl_type): try: enumerator = struct_info.enumerations()[0] if enumerator.byte_size != i.decl_type.byte_size: bitfield = None else: bitfield = enumerator.get_name2value_dict() except RuntimeError: bitfield = None if declarations.is_class(i.decl_type): new_type_members.append( DeclaredMemberStruct( i.name, _create_member_list(i.decl_type.declaration))) else: new_type_members.append( DeclaredMember(i.name, _convert_opaque_to_token(i.decl_type), bitfield)) return new_type_members
def Auto_Functional_Transformation ( mb, ignore_funs=[], special_vars=[]): toprocess = [] aliases={} for fun in mb.member_functions(allow_empty=True): toprocess.append( fun ) for fun in mb.free_functions(allow_empty=True): toprocess.append( fun ) for fun in toprocess: fun_demangled = fun.demangled # need to check as extern functions don't have demangled name... if fun_demangled: # try: # ugly wrapping in a try :( fullname = fun.demangled.split('(')[0] if fullname not in ignore_funs and not fun.ignore: outputonly = False arg_position = 0 trans=[] desc="" ft_type = None ctypes_conversion = False for arg in fun.arguments: rawarg = declarations.remove_declarated( declarations.remove_const( declarations.remove_reference( declarations.remove_pointer ( arg.type )))) ## now check if the arg is a fundemental type (int float etc), a void ## or a special .. if declarations.is_arithmetic (rawarg)\ or declarations.is_void(rawarg)\ or arg.type.decl_string in special_vars: if declarations.is_pointer(arg.type): #we convert any pointers to unsigned int's # now look to see if it's a char * and if so we treat it as a string.. # # print "**" , declarations.remove_alias( rawarg ), declarations.type_traits.create_cv_types( declarations.cpptypes.char_t()) if declarations.remove_alias( rawarg ) in declarations.type_traits.create_cv_types( declarations.cpptypes.char_t() ): print ("MATCHED CString", fun) trans.append( ft.input_c_string(arg_position, 4096 ) ) desc = desc +"Argument: "+arg.name+ "( pos:" + str(arg_position) + " - " +\ arg.type.decl_string + " ) takes a python string. \\n" ctypes_conversion = True ctypes_arg = arg.type.decl_string.split()[0] ft_type = 'CTYPES' else: trans.append( ft.modify_type(arg_position,_ReturnUnsignedInt ) ) desc = desc +"Argument: "+arg.name+ "( pos:" + str(arg_position) + " - " +\ arg.type.decl_string + " ) takes a CTypes.addressof(xx). \\n" ctypes_conversion = True ctypes_arg = arg.type.decl_string.split()[0] ft_type = 'CTYPES' elif declarations.is_reference(arg.type)and not declarations.is_const(declarations.remove_reference( arg.type)): # seen functions passing const ref's trans.append( ft.inout(arg_position ) ) desc = desc + "Argument: "+arg.name+ "( pos:" + str(arg_position) + " - " +\ arg.type.decl_string + " ) converted to an input/output (change to return types).\\n" ft_type = 'INOUT' elif declarations.is_reference(arg.type): print ("Warning: - possible code change.", fun,arg," not wrapped as const reference to base type invalid") else: pass # it isn't a pointer or reference so doesn't need wrapping else: pass # it's not a var we need to handle arg_position += 1 if trans: const_return = False # declarations.is_const(fun) if fun.decl_string.endswith('const'): const_return=True simple_return = declarations.is_arithmetic(fun.return_type) or declarations.is_void(fun.return_type) nonpublic_destructor = declarations.is_class(fun.parent) and declarations.has_destructor(fun.parent) and\ not declarations.has_public_destructor(fun.parent) if fun.documentation or fun.transformations: # it's already be tweaked: print ("AUTOFT ERROR: Duplicate Tranforms.", fun, fun.documentation) # if the class has a protected destruction AND the return value is const or a non arithmatic value then exclude it. elif nonpublic_destructor and const_return: print ("AUTOFT ERROR Const: Parent has non public destructor and const return.", fun.parent.name, fun.return_type.decl_string, fun) fun.documentation="Python-Ogre Warning: function required transformation - not possible due to non public destructor and const return value.." elif nonpublic_destructor and not simple_return: print ("AUTOFT ERROR Const: Parent has non public destructor and complex return value.", fun.parent.name, fun.return_type.decl_string, fun) fun.documentation="Python-Ogre Warning: function required transformation - not possible due to non public destructor and complex return value.." else: new_alias = fun.name if ctypes_conversion: # only manage name changes if ctypes changing # now lets look for a duplicate function name with the same number arguments f= [None]*len(fun.arguments) s = mb.member_functions("::" + fullname, arg_types=f, allow_empty=True) if len (s) > 1: # there are duplicate names so need to create something unique ctypes_arg = ctypes_arg.replace("::", "_") # to clean up function names... new_alias = fun.name + ctypes_arg[0].upper() + ctypes_arg[1:] # now for REAL ugly code -- we have faked a new alias and it may not be unique # so we track previous alias + class name to ensure unique names are generated keyname = fullname + new_alias # we use the full class + function name + alias as the key if keyname in aliases: # already exists, need to fake another version.. new_alias = new_alias + "_" + str( aliases[keyname] ) aliases[keyname] = aliases[keyname] + 1 else: aliases[keyname] = 1 desc = desc + "\\\nWARNING FUNCTION NAME CHANGE - from "+fun.name + " -- " + fun.decl_string +" to " + new_alias + " \\n" print ("INFO: Adjusting Alias as multiple overlapping functions:", new_alias) print ("AUTOFT OK: Tranformed ", fun.return_type.decl_string, fun, "(",new_alias,")") fun.add_transformation ( * trans , **{"alias":new_alias} ) fun.documentation = docit ("Auto Modified Arguments:", desc, "...")
def expose_single( self, sp_instantiation ): sp_instantiation.exclude() # we don't want to export SharedPtr< X > print ("SharedPointer Excluded", sp_instantiation, sp_instantiation.decl_string) # print dir (sp_instantiation) sp_instantiation.disable_warnings( messages.W1040 ) pointee = self.get_pointee( sp_instantiation ) pointee.include_files.append( 'py_shared_ptr.h' ) if sp_instantiation.derived: # print "SHARED Derived!" #We have struct XPtr : public SharedPtr<X> assert 1 == len( sp_instantiation.derived ) sp_derived = sp_instantiation.derived[0].related_class sp_derived.exclude() print ("SharedPointer Excluded Derived", sp_derived, pointee) sp_derived.disable_warnings( messages.W1040 ) if Version1: pointee.add_declaration_code( OGRE_SP_HELD_TYPE_TMPL % { 'class_name': pointee.decl_string ,'base_class_name': pointee.name , 'class_ptr_name': sp_derived.decl_string , 'name_space' : self.name_space } ) else: pointee.add_declaration_code( OGRE_SP_HELD_TYPE_TMPL_VER2 % { 'class_name': pointee.decl_string ,'base_class_name': pointee.name , 'class_ptr_name': sp_derived.decl_string , 'name_space' : self.name_space } ) pointee.add_registration_code( REGISTER_SPTR_CONVERSION % { 'derived' : sp_derived.decl_string , 'base' : sp_instantiation.decl_string } , works_on_instance=False ) pointee.add_registration_code( REGISTER_SP_TO_PYTHON % { 'sp_inst_class_name' : sp_derived.decl_string } , works_on_instance=False ) if declarations.is_class( pointee ) and pointee.is_wrapper_needed(): # print "SHARED is class and wrapper needed!" pointee.held_type = '::Ogre::SharedPtr< %s >' % pointee.wrapper_alias pointee.add_registration_code( REGISTER_SPTR_CONVERSION % { 'derived' : pointee.held_type , 'base' : sp_instantiation.decl_string } , works_on_instance=False ) pointee.add_registration_code( REGISTER_SP_TO_PYTHON % { 'sp_inst_class_name' : sp_instantiation.decl_string } , works_on_instance=False ) else: # print "SHARED - not class or wrapper" pointee.held_type = sp_instantiation.decl_string #print pointee.name, pointee.bases try: base_classes = filter( lambda hi: hi.access_type == 'public', pointee.bases ) for base in base_classes: pointee.add_registration_code( REGISTER_SPTR_CONVERSION % { 'derived' : sp_instantiation.decl_string , 'base' : '::Ogre::SharedPtr< %s >' % base.related_class.decl_string } , works_on_instance=False) except: print ("WARNING: Problem with ", pointee.name, " it possibly doesn't have any bases") pass
def getDeclType(arg): """Determines arg type""" if declarations.is_pointer(arg): return "Pointer" elif declarations.is_fundamental(arg): return "Fundamental" elif declarations.is_class(arg): return "Class" else: return "Unknown"