def visit_variable(self): self.__types_db.update( self.curr_decl ) self.__dependencies_manager.add_exported( self.curr_decl ) if self.curr_decl.expose_address: creator_type = None if isinstance( self.curr_decl.parent, declarations.namespace_t ): creator_type = code_creators.global_variable_addressof_t else: creator_type = code_creators.member_variable_addressof_t self.curr_code_creator.adopt_creator( creator_type(self.curr_decl) ) return if not self.curr_decl.expose_value: return if declarations.is_array( self.curr_decl.type ): if self._register_array_1( self.curr_decl.type ): array_1_registrator = code_creators.array_1_registrator_t( array_type=self.curr_decl.type ) self.curr_code_creator.adopt_creator( array_1_registrator ) if isinstance( self.curr_decl.parent, declarations.namespace_t ): maker = None wrapper = None if declarations.is_array( self.curr_decl.type ): wrapper = code_creators.array_gv_wrapper_t( variable=self.curr_decl ) maker = code_creators.array_gv_t( variable=self.curr_decl, wrapper=wrapper ) else: maker = code_creators.global_variable_t( variable=self.curr_decl ) if wrapper: self.__extmodule.adopt_declaration_creator( wrapper ) else: maker = None wrapper = None if self.curr_decl.bits != None: wrapper = code_creators.bit_field_wrapper_t( variable=self.curr_decl ) maker = code_creators.bit_field_t( variable=self.curr_decl, wrapper=wrapper ) elif declarations.is_array( self.curr_decl.type ): wrapper = code_creators.array_mv_wrapper_t( variable=self.curr_decl ) maker = code_creators.array_mv_t( variable=self.curr_decl, wrapper=wrapper ) elif declarations.is_pointer( self.curr_decl.type ): wrapper = code_creators.member_variable_wrapper_t( variable=self.curr_decl ) maker = code_creators.member_variable_t( variable=self.curr_decl, wrapper=wrapper ) elif declarations.is_reference( self.curr_decl.type ): if None is self.curr_decl.getter_call_policies: self.curr_decl.getter_call_policies = self.__call_policies_resolver( self.curr_decl, 'get' ) if None is self.curr_decl.setter_call_policies: self.curr_decl.setter_call_policies = self.__call_policies_resolver( self.curr_decl, 'set' ) wrapper = code_creators.mem_var_ref_wrapper_t( variable=self.curr_decl ) maker = code_creators.mem_var_ref_t( variable=self.curr_decl, wrapper=wrapper ) self.__opaque_types_manager.register_opaque( maker, self.curr_decl ) else: maker = code_creators.member_variable_t( variable=self.curr_decl ) if wrapper: self.curr_code_creator.wrapper.adopt_creator( wrapper ) self.curr_code_creator.adopt_creator( maker )
def _exportable_impl(self): if not self.name: return messages.W1033 if self.bits == 0 and self.name == "": return messages.W1034 if declarations.is_array( self.type) and declarations.array_size(self.type) < 1: return messages.W1045 type_ = declarations.remove_alias(self.type) type_ = declarations.remove_const(type_) if declarations.is_pointer(type_): if self.type_qualifiers.has_static: return messages.W1035 if python_traits.is_immutable(type_.base): return messages.W1036 units = declarations.decompose_type(type_) ptr2functions = filter( lambda unit: isinstance(unit, declarations.calldef_type_t), units) if ptr2functions: return messages.W1037 type_ = declarations.remove_pointer(type_) if declarations.class_traits.is_my_case(type_): cls = declarations.class_traits.get_declaration(type_) if not cls.name: return messages.W1038 if isinstance(self.parent, declarations.class_t): if self.access_type != declarations.ACCESS_TYPES.PUBLIC: return messages.W1039 return ''
def _exportable_impl( self ): if not self.name: return messages.W1033 if self.bits == 0 and self.name == "": return messages.W1034 if declarations.is_array( self.type ) and declarations.array_size( self.type ) < 1: return messages.W1045 type_ = declarations.remove_alias( self.type ) type_ = declarations.remove_const( type_ ) if declarations.is_pointer( type_ ): if self.type_qualifiers.has_static: return messages.W1035 if python_traits.is_immutable( type_.base ): return messages.W1036 units = declarations.decompose_type( type_ ) ptr2functions = filter( lambda unit: isinstance( unit, declarations.calldef_type_t ) , units ) if ptr2functions: return messages.W1037 type_ = declarations.remove_pointer( type_ ) if declarations.class_traits.is_my_case( type_ ): cls = declarations.class_traits.get_declaration( type_ ) if not cls.name: return messages.W1038 if isinstance( self.parent, declarations.class_t ): if self.access_type != declarations.ACCESS_TYPES.PUBLIC: return messages.W1039 return ''
def _exportable_impl(self): if self.transformations: # It is possible that the function asked for the user attention. # The user paid attention and created a transformation. # Py++ should be silent in this case. return "" if not self.parent.name: return messages.W1057 % str(self) all_types = [arg.type for arg in self.arguments] all_types.append(self.return_type) for some_type in all_types: if isinstance(some_type, declarations.ellipsis_t): return messages.W1053 % str(self) units = declarations.decompose_type(some_type) ptr2functions = filter(lambda unit: isinstance(unit, declarations.calldef_type_t), units) if ptr2functions: return messages.W1004 # Function that take as agrument some instance of non public class # will not be exported. Same to the return variable if isinstance(units[-1], declarations.declarated_t): dtype = units[-1] if isinstance(dtype.declaration.parent, declarations.class_t): if dtype.declaration not in dtype.declaration.parent.public_members: return messages.W1005 no_ref = declarations.remove_reference(some_type) no_ptr = declarations.remove_pointer(no_ref) no_const = declarations.remove_const(no_ptr) if declarations.is_array(no_const): return messages.W1006 return self._exportable_impl_derived()
def fix_unnamed_classes( classes, namespace ): for unnamed_cls in classes: named_parent = unnamed_cls.parent if not named_parent.name: named_parent = named_parent.parent if not named_parent or named_parent.ignore: continue for mvar in unnamed_cls.public_members: if not mvar.name: continue if mvar.ignore: continue if type (mvar) == type (declarations.destructor_t): continue try: if declarations.is_array (mvar.type): template = '''def_readonly("%(mvar)s", &%(ns)s::%(parent)s::%(mvar)s)''' else: template = '''def_readwrite("%(mvar)s", &%(ns)s::%(parent)s::%(mvar)s)''' except AttributeError: continue except: print ("**** Error in unnamed_classes", mvar) print ("Fixing Unnamed Class:", unnamed_cls, mvar, named_parent.name) named_parent.add_code( template % dict( ns=namespace, mvar=mvar.name, parent=named_parent.name ) )
def _exportable_impl(self): if not self.parent.name and self.is_wrapper_needed(): #return messages.W1057 % str( self ) return messages.W1058 % str(self) if not self.name: return messages.W1033 if self.bits == 0 and self.name == "": return messages.W1034 if not self.expose_address: if declarations.is_array( self.type) and declarations.array_size(self.type) < 1: return messages.W1045 type_ = declarations.remove_alias(self.type) type_ = declarations.remove_const(type_) if declarations.is_pointer(type_): if not self.expose_address and self.type_qualifiers.has_static: return messages.W1035 if not self.expose_address and python_traits.is_immutable( type_.base): return messages.W1036 units = declarations.decompose_type(type_) ptr2functions = [ unit for unit in units if isinstance(unit, declarations.calldef_type_t) ] if ptr2functions: return messages.W1037 type_ = declarations.remove_pointer(type_) if declarations.class_traits.is_my_case(type_): cls = declarations.class_traits.get_declaration(type_) if not cls.name: return messages.W1038 #if cls.class_type == declarations.CLASS_TYPES.UNION: # return messages.W1061 % ( str( self ), str( cls ) ) if isinstance(self.parent, declarations.class_t): if self.access_type != declarations.ACCESS_TYPES.PUBLIC: return messages.W1039 if declarations.is_array(type_): item_type = declarations.array_item_type(type_) if declarations.is_pointer(item_type): item_type_no_ptr = declarations.remove_pointer(item_type) if python_traits.is_immutable(item_type_no_ptr): return messages.W1056 return ''
def _exportable_impl( self ): if not self.parent.name and self.is_wrapper_needed(): #return messages.W1057 % str( self ) return messages.W1058 % str( self ) if not self.name: return messages.W1033 if self.bits == 0 and self.name == "": return messages.W1034 if not self.expose_address: if declarations.is_array( self.type ) and declarations.array_size( self.type ) < 1: return messages.W1045 type_ = declarations.remove_alias( self.type ) type_ = declarations.remove_const( type_ ) if declarations.is_pointer( type_ ): if not self.expose_address and self.type_qualifiers.has_static: return messages.W1035 if not self.expose_address and python_traits.is_immutable( type_.base ): return messages.W1036 units = declarations.decompose_type( type_ ) ptr2functions = filter( lambda unit: isinstance( unit, declarations.calldef_type_t ) , units ) if ptr2functions: return messages.W1037 type_ = declarations.remove_pointer( type_ ) if declarations.class_traits.is_my_case( type_ ): cls = declarations.class_traits.get_declaration( type_ ) if not cls.name: return messages.W1038 #if cls.class_type == declarations.CLASS_TYPES.UNION: # return messages.W1061 % ( str( self ), str( cls ) ) if isinstance( self.parent, declarations.class_t ): if self.access_type != declarations.ACCESS_TYPES.PUBLIC: return messages.W1039 if declarations.is_array( type_ ): item_type = declarations.array_item_type( type_ ) if declarations.is_pointer( item_type ): item_type_no_ptr = declarations.remove_pointer( item_type ) if python_traits.is_immutable( item_type_no_ptr ): return messages.W1056 return ''
def test6(self): code = 'char volatile arr[4] = {};' src_reader = parser.source_reader_t(self.config) global_ns = declarations.get_global_namespace( src_reader.read_string(code)) arr_type = global_ns.variable('arr').decl_type if self.config.xml_generator == "gccxml": self.assertTrue('char [4] volatile' == arr_type.decl_string, arr_type.decl_string) else: self.assertTrue('char volatile [4]' == arr_type.decl_string, arr_type.decl_string) self.assertTrue(declarations.is_array(arr_type)) self.assertTrue(declarations.is_volatile(arr_type))
def is_wrapper_needed(self): """returns an explanation( list of str ) why wrapper is needed. If wrapper is not needed than [] will be returned. """ explanation = [] if self.bits: explanation.append( messages.W1024 % self.name ) if declarations.is_pointer( self.decl_type ): explanation.append( messages.W1025 % self.name ) if declarations.is_reference( self.decl_type ): explanation.append( messages.W1026 % self.name ) if declarations.is_array( self.decl_type ): explanation.append( messages.W1027 % self.name) return explanation
def is_wrapper_needed(self): """returns an explanation( list of str ) why wrapper is needed. If wrapper is not needed than [] will be returned. """ explanation = [] if self.bits: explanation.append( messages.W1024 % self.name ) if declarations.is_pointer( self.type ): explanation.append( messages.W1025 % self.name ) if declarations.is_reference( self.type ): explanation.append( messages.W1026 % self.name ) if declarations.is_array( self.type ): explanation.append( messages.W1027 % self.name) return explanation
def __format_type_as_undecorated( self, type_, is_argument, hint ): result = [] type_ = declarations.remove_alias( type_ ) if declarations.is_array( type_ ): result.append( declarations.array_item_type( type_ ).decl_string ) result.append( '*' ) if is_argument: result.append( 'const' ) else: result.append( self.__remove_leading_scope( type_.decl_string ) ) result = ' '.join( result ) if hint == 'nm': for x in ( '*', '&' ): result = result.replace( ' ' + x, x ) return result
def __format_type_as_undecorated(self, type_, is_argument, hint): result = [] type_ = declarations.remove_alias(type_) if declarations.is_array(type_): result.append(declarations.array_item_type(type_).decl_string) result.append('*') if is_argument: result.append('const') else: result.append(self.__remove_leading_scope(type_.decl_string)) result = ' '.join(result) if hint == 'nm': for x in ('*', '&'): result = result.replace(' ' + x, x) return result
def expose_member_as_ndarray1d(klass, member_name, array_size): klass.include_files.append( "ndarray.hpp") z = klass.var(member_name) z.exclude() elem_type = _D.array_item_type(z.type) if _D.is_array(z.type) else _D.remove_pointer(z.type) klass.add_declaration_code(''' static sdcpp::ndarray CLASS_NAME_get_CLASS_NAME_MEMBER_NAME( CLASS_TYPE const & inst ){ return sdcpp::new_ndarray1d(ARRAY_SIZE, sdcpp::dtypeof< ELEM_TYPE >(), (void *)(inst.MEMBER_NAME)); } '''.replace("MEMBER_NAME", member_name) \ .replace("CLASS_NAME", klass.alias) \ .replace("CLASS_TYPE", klass.pds) \ .replace("ELEM_TYPE", elem_type.partial_decl_string) \ .replace("ARRAY_SIZE", str(array_size))) klass.add_registration_code('add_property( "MEMBER_NAME", &::CLASS_NAME_get_CLASS_NAME_MEMBER_NAME )' \ .replace("MEMBER_NAME", member_name).replace("CLASS_NAME", klass.alias))
def is_wrapper_needed(self): """returns an explanation( list of str ) why wrapper is needed. If wrapper is not needed than [] will be returned. """ explanation = [] if self.wrapper_code: explanation.append(messages.W1020) if self.null_constructor_body: explanation.append(messages.W1021) if self.copy_constructor_body: explanation.append(messages.W1022) redefined_funcs = self.redefined_funcs() if redefined_funcs: funcs = map(lambda f: f.name, redefined_funcs) explanation.append(messages.W1023 % ', '.join(funcs)) for member in self.get_exportable_members(): if isinstance(member, declarations.destructor_t): continue if isinstance(member, declarations.variable_t): if member.bits: explanation.append(messages.W1024 % member.name) if declarations.is_pointer(member.type): explanation.append(messages.W1025 % member.name) if declarations.is_reference(member.type): explanation.append(messages.W1026 % member.name) if declarations.is_array(member.type): explanation.append(messages.W1027 % member.name) if isinstance(member, declarations.class_t) and member.is_wrapper_needed(): explanation.append(messages.W1028 % member.name) if isinstance(member, declarations.calldef_t): if isinstance(member, declarations.constructor_t) and member.body: explanation.append(messages.W1029) if member.virtuality != VIRTUALITY_TYPES.NOT_VIRTUAL: explanation.append(messages.W1030 % member.name) if member.access_type in (ACCESS_TYPES.PROTECTED, ACCESS_TYPES.PRIVATE): explanation.append(messages.W1031 % member.name) return explanation
def _convert_opaque_to_token(x): """ convert either a string from re or a value from pygccxml to a struct token """ if type(x) is str: return { "BOOL": "?", "UINT8_T": "B", "INT8_T": "b", "UINT16_T": "H", "INT16_T": "h", "UINT32_T": "I", "INT32_T": "i", "UINT64_T": "Q", "INT64_T": "q", "FLOAT": "f", "DOUBLE": "d" }[x.upper()] elif isinstance(x, declarations.declarated_t) and not declarations.is_enum(x): return _convert_opaque_to_token(declarations.remove_alias(x)) elif declarations.is_integral(x) or declarations.is_enum(x): basetoken = {1: "b", 2: "h", 4: "i", 8: "q"}[int(x.byte_size)] try: if "unsigned" in x.CPPNAME: basetoken = basetoken.upper() if "bool" in x.CPPNAME: basetoken = '?' except AttributeError: pass return basetoken elif declarations.is_floating_point(x): if isinstance(x, declarations.float_t): return "f" else: return "d" elif declarations.is_array(x): basetoken = _convert_opaque_to_token(declarations.array_item_type(x)) return basetoken * x.size else: raise ValueError("unknown instance: " + repr(x))
def test6(self): code = 'char volatile arr[4] = {};' src_reader = parser.source_reader_t(self.config) global_ns = declarations.get_global_namespace( src_reader.read_string(code)) arr_type = global_ns.variable('arr').decl_type if self.config.xml_generator == "gccxml": self.assertTrue( 'char [4] volatile' == arr_type.decl_string, arr_type.decl_string) else: self.assertTrue( 'char volatile [4]' == arr_type.decl_string, arr_type.decl_string) self.assertTrue( declarations.is_array(arr_type)) self.assertTrue( declarations.is_volatile(arr_type))
def expose_member_as_ndarray1d(klass, member_name, array_size): klass.include_files.append("ndarray.hpp") z = klass.var(member_name) z.exclude() elem_type = _D.array_item_type(z.type) if _D.is_array( z.type) else _D.remove_pointer(z.type) klass.add_declaration_code(''' static sdcpp::ndarray CLASS_NAME_get_CLASS_NAME_MEMBER_NAME( CLASS_TYPE const & inst ){ return sdcpp::new_ndarray1d(ARRAY_SIZE, sdcpp::dtypeof< ELEM_TYPE >(), (void *)(inst.MEMBER_NAME)); } '''.replace("MEMBER_NAME", member_name) \ .replace("CLASS_NAME", klass.alias) \ .replace("CLASS_TYPE", klass.pds) \ .replace("ELEM_TYPE", elem_type.partial_decl_string) \ .replace("ARRAY_SIZE", str(array_size))) klass.add_registration_code('add_property( "MEMBER_NAME", &::CLASS_NAME_get_CLASS_NAME_MEMBER_NAME )' \ .replace("MEMBER_NAME", member_name).replace("CLASS_NAME", klass.alias))
def is_wrapper_needed(self): """returns an explanation( list of str ) why wrapper is needed. If wrapper is not needed than [] will be returned. """ explanation = [] if self.wrapper_code: explanation.append( messages.W1020 ) if self.null_constructor_body: explanation.append( messages.W1021 ) if self.copy_constructor_body: explanation.append( messages.W1022 ) redefined_funcs = self.redefined_funcs() if redefined_funcs: funcs = map( lambda f: f.name, redefined_funcs ) explanation.append( messages.W1023 % ', '.join(funcs) ) for member in self.get_exportable_members(): if isinstance( member, declarations.destructor_t ): continue if isinstance( member, declarations.variable_t ): if member.bits: explanation.append( messages.W1024 % member.name ) if declarations.is_pointer( member.type ): explanation.append( messages.W1025 % member.name ) if declarations.is_reference( member.type ): explanation.append( messages.W1026 % member.name ) if declarations.is_array( member.type ): explanation.append( messages.W1027 % member.name) if isinstance( member, declarations.class_t ) and member.is_wrapper_needed(): explanation.append( messages.W1028 % member.name) if isinstance( member, declarations.calldef_t ): if isinstance( member, declarations.constructor_t ) and member.body: explanation.append( messages.W1029 ) if member.virtuality != VIRTUALITY_TYPES.NOT_VIRTUAL: explanation.append( messages.W1030 % member.name ) if member.access_type in ( ACCESS_TYPES.PROTECTED, ACCESS_TYPES.PRIVATE ): explanation.append( messages.W1031 % member.name) return explanation
def _exportable_impl( self ): all_types = [ arg.type for arg in self.arguments ] all_types.append( self.return_type ) for some_type in all_types: units = declarations.decompose_type( some_type ) ptr2functions = filter( lambda unit: isinstance( unit, declarations.calldef_type_t ) , units ) if ptr2functions: return messages.W1004 #Function that take as agrument some instance of non public class #will not be exported. Same to the return variable if isinstance( units[-1], declarations.declarated_t ): dtype = units[-1] if isinstance( dtype.declaration.parent, declarations.class_t ): if dtype.declaration not in dtype.declaration.parent.public_members: return messages.W1005 no_ref = declarations.remove_reference( some_type ) no_ptr = declarations.remove_pointer( no_ref ) no_const = declarations.remove_const( no_ptr ) if declarations.is_array( no_const ): return messages.W1006 return self._exportable_impl_derived()
def is_ptr_or_array( type_ ): return declarations.is_pointer( type_ ) or declarations.is_array( type_ )