def __init__(self, function, buffer_arg_ref, size_arg_ref): """Constructor. :param buffer_arg_ref: "reference" to the buffer argument :param buffer_arg_ref: "reference" to argument, which holds buffer size """ transformer.transformer_t.__init__(self, function) self.buffer_arg = self.get_argument(buffer_arg_ref) self.buffer_arg_index = self.function.arguments.index(self.buffer_arg) self.size_arg = self.get_argument(size_arg_ref) self.size_arg_index = self.function.arguments.index(self.size_arg) if not is_ptr_or_array(self.buffer_arg.type): raise ValueError( '%s\nin order to use "input_c_buffer" transformation, "buffer" argument %s type must be a array or a pointer (got %s).' ) % (function, self.buffer_arg.name, self.buffer_arg.type) if not declarations.is_integral(self.size_arg.type): raise ValueError( '%s\nin order to use "input_c_buffer" transformation, "size" argument %s type must be an integral type (got %s).' ) % (function, self.size_arg.name, self.size_arg.type) self.buffer_item_type = declarations.remove_const(declarations.array_item_type(self.buffer_arg.type))
def visit_array( self ): item_visitor = self.create_converter( declarations.array_item_type(self.user_type) ) item_type = declarations.apply_visitor( item_visitor, item_visitor.user_type ) size = declarations.array_size( self.user_type ) if size == declarations.array_t.SIZE_UNKNOWN: size = 0 return "( %s * %d )" % ( item_type, size )
def wrapper_type( self ): tmpl = "%(namespace)s::%(constness)sarray_1_t< %(item_type)s, %(array_size)d>" item_type = declarations.array_item_type(self.declaration.decl_type) is_noncopyable = not declarations.is_fundamental(item_type) and \ declarations.is_noncopyable(item_type) constness = '' if declarations.is_const(self.declaration.decl_type) or is_noncopyable: constness = 'const_' result = tmpl % { 'namespace' : code_repository.array_1.namespace , 'constness' : constness , 'item_type' : declarations.array_item_type( self.declaration.decl_type ).decl_string , 'array_size': declarations.array_size( self.declaration.decl_type ) } return declarations.dummy_type_t( result )
def visit_array(self): item_visitor = self.create_converter( declarations.array_item_type(self.user_type)) item_type = declarations.apply_visitor(item_visitor, item_visitor.user_type) size = declarations.array_size(self.user_type) if size == declarations.array_t.SIZE_UNKNOWN: size = 0 return "( %s * %d )" % (item_type, size)
def _create_impl(self): templates = declarations.templates call_invocation = declarations.call_invocation ns_name = code_repository.array_1.namespace item_type = declarations.array_item_type(self.array_type) is_noncopyable = not declarations.is_fundamental(item_type) and declarations.is_noncopyable(item_type) if declarations.is_const(self.array_type) or is_noncopyable: fn_name = 'register_const_array_1' else: fn_name = 'register_array_1' fn_def_tmpl_args = [ declarations.array_item_type(self.array_type).decl_string , str( declarations.array_size(self.array_type) ) ] if not self.call_policies.is_default(): fn_def_tmpl_args.append( self.call_policies.create(self, call_policies.CREATION_POLICY.AS_TEMPLATE_ARGUMENT ) ) fn_def = templates.join( '::'.join( [ns_name, fn_name] ), fn_def_tmpl_args ) return call_invocation.join( fn_def, [ '"%s"' % self._create_name() ] ) + ';'
def __init__(self, function, arg_ref, rows, columns): """Constructor. :param rows, columns: The fixed size of the input matrix :type rows, columns: int """ transformer.transformer_t.__init__(self, function) self.arg = self.get_argument(arg_ref) self.arg_index = self.function.arguments.index(self.arg) if not is_ptr_or_array(self.arg.type): raise ValueError( '%s\nin order to use "input_matrix" transformation, argument %s type must be a array or a pointer (got %s).' ) \ % ( function, self.arg.name, self.arg.type) self.rows = rows self.columns = columns self.matrix_item_type = declarations.remove_const( declarations.array_item_type( declarations.array_item_type(self.arg.type)))
def __init__(self, function, arg_ref, rows, columns): """Constructor. :param rows, columns: The fixed size of the input matrix :type rows, columns: int """ transformer.transformer_t.__init__(self, function) self.arg = self.get_argument(arg_ref) self.arg_index = self.function.arguments.index(self.arg) if not is_ptr_or_array(self.arg.type): raise ValueError( '%s\nin order to use "input_matrix" transformation, argument %s type must be a array or a pointer (got %s).' ) % (function, self.arg.name, self.arg.type) self.rows = rows self.columns = columns self.matrix_item_type = declarations.remove_const( declarations.array_item_type(declarations.array_item_type(self.arg.type)) )
def wrapper_type( self ): tmpl = "%(namespace)s::%(constness)sarray_1_t< %(item_type)s, %(array_size)d>" constness = '' if declarations.is_const( self.declaration.type ): constness = 'const_' result = tmpl % { 'namespace' : code_repository.array_1.namespace , 'constness' : constness , 'item_type' : declarations.array_item_type( self.declaration.type ).decl_string , 'array_size': declarations.array_size( self.declaration.type ) } return declarations.dummy_type_t( result )
def _get_wrapper_type( self ): ns_name = code_repository.array_1.namespace if declarations.is_const( self.declaration.type ): class_name = 'const_array_1_t' else: class_name = 'array_1_t' decl_string = declarations.templates.join( '::'.join( [ns_name, class_name] ) , [ declarations.array_item_type( self.declaration.type ).decl_string , str( declarations.array_size( self.declaration.type ) ) ]) return declarations.dummy_type_t( decl_string )
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 _create_impl(self): templates = declarations.templates call_invocation = declarations.call_invocation ns_name = code_repository.array_1.namespace if declarations.is_const( self.array_type ): fn_name = 'register_const_array_1' else: fn_name = 'register_array_1' fn_def_tmpl_args = [ declarations.array_item_type(self.array_type).decl_string , str( declarations.array_size(self.array_type) ) ] if not self.call_policies.is_default(): fn_def_tmpl_args.append( self.call_policies.create(self, call_policies.CREATION_POLICY.AS_TEMPLATE_ARGUMENT ) ) fn_def = templates.join( '::'.join( [ns_name, fn_name] ), fn_def_tmpl_args ) return call_invocation.join( fn_def, [ '"%s"' % self._create_name() ] ) + ';'
def __init__(self, function, arg_ref, size): """Constructor. @param size: The fixed size of the input array @type size: int """ transformer.transformer_t.__init__( self, function ) self.arg = self.get_argument( arg_ref ) self.arg_index = self.function.arguments.index( self.arg ) if not is_ptr_or_array( self.arg.type ): raise ValueError( '%s\nin order to use "input_array" transformation, argument %s type must be a array or a pointer (got %s).' ) \ % ( function, self.arg.name, self.arg.type) self.array_size = size self.array_item_type = declarations.remove_const( declarations.array_item_type( self.arg.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 _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 _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 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 _create_impl(self): templates = declarations.templates call_invocation = declarations.call_invocation ns_name = code_repository.array_1.namespace if declarations.is_const(self.array_type): fn_name = "register_const_array_1" else: fn_name = "register_array_1" fn_def_tmpl_args = [ declarations.array_item_type(self.array_type).decl_string, str(declarations.array_size(self.array_type)), ] if not self.call_policies.is_default(): fn_def_tmpl_args.append(self.call_policies.create(self, call_policies.CREATION_POLICY.AS_TEMPLATE_ARGUMENT)) fn_def = templates.join("::".join([ns_name, fn_name]), fn_def_tmpl_args) return call_invocation.join(fn_def, ['"%s"' % self._create_name()]) + ";"
def __init__(self, function, arg_ref, size): """Constructor. :param maxsize: The maximum string size we will allow... :type maxsize: int """ transformer.transformer_t.__init__( self, function ) self.arg = self.get_argument( arg_ref ) self.arg_index = self.function.arguments.index( self.arg ) if not is_ptr_or_array( self.arg.type ): raise ValueError( '%s\nin order to use "input_array" transformation, argument %s type must be a array or a pointer (got %s).' ) \ % ( function, self.arg.name, self.arg.type) self.max_size = size self.array_item_type = declarations.remove_const( declarations.array_item_type( self.arg.type ) ) self.array_item_rawtype = declarations.remove_cv( self.arg.type ) self.array_item_rawtype = declarations.pointer_t( self.array_item_type )
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 __init__(self, function, buffer_arg_ref, size_arg_ref): """Constructor. :param buffer_arg_ref: "reference" to the buffer argument :param buffer_arg_ref: "reference" to argument, which holds buffer size """ transformer.transformer_t.__init__( self, function ) self.buffer_arg = self.get_argument( buffer_arg_ref ) self.buffer_arg_index = self.function.arguments.index( self.buffer_arg ) self.size_arg = self.get_argument( size_arg_ref ) self.size_arg_index = self.function.arguments.index( self.size_arg ) if not is_ptr_or_array( self.buffer_arg.type ): raise ValueError( '%s\nin order to use "input_c_buffer" transformation, "buffer" argument %s type must be a array or a pointer (got %s).' ) \ % ( function, self.buffer_arg.name, self.buffer_arg.type) if not declarations.is_integral( self.size_arg.type ): raise ValueError( '%s\nin order to use "input_c_buffer" transformation, "size" argument %s type must be an integral type (got %s).' ) \ % ( function, self.size_arg.name, self.size_arg.type) self.buffer_item_type = declarations.remove_const( declarations.array_item_type( self.buffer_arg.type ) )
def _guess_call_policies(self): item_type = declarations.array_item_type( self.array_type ) if python_traits.is_immutable( item_type ): return call_policies.default_call_policies() else: return call_policies.return_internal_reference()
def _create_name( self ): item_type = declarations.array_item_type(self.array_type) return "__array_1_%(type)s_%(size)d" \ % dict( type=algorithm.create_valid_name( item_type.decl_string ) , size=declarations.array_size(self.array_type) )