def test_int16(self) -> None: parser = cpptypeinfo.TypeParser() cpptypeinfo.parse_source(parser, 'typedef short T;', debug=True) typedef = parser.root_namespace.user_type_map['T'] if not isinstance(typedef, cpptypeinfo.usertype.Typedef): raise Exception() self.assertEqual(typedef.typeref.ref, cpptypeinfo.Int16())
def get_primitive_type(t: cindex.Type) -> Optional[TypeRef]: ''' TypeKind.VOID = TypeKind(2) TypeKind.BOOL = TypeKind(3) TypeKind.CHAR_U = TypeKind(4) TypeKind.UCHAR = TypeKind(5) TypeKind.CHAR16 = TypeKind(6) TypeKind.CHAR32 = TypeKind(7) TypeKind.USHORT = TypeKind(8) TypeKind.UINT = TypeKind(9) TypeKind.ULONG = TypeKind(10) TypeKind.ULONGLONG = TypeKind(11) TypeKind.UINT128 = TypeKind(12) TypeKind.CHAR_S = TypeKind(13) TypeKind.SCHAR = TypeKind(14) TypeKind.WCHAR = TypeKind(15) TypeKind.SHORT = TypeKind(16) TypeKind.INT = TypeKind(17) TypeKind.LONG = TypeKind(18) TypeKind.LONGLONG = TypeKind(19) TypeKind.INT128 = TypeKind(20) TypeKind.FLOAT = TypeKind(21) TypeKind.DOUBLE = TypeKind(22) TypeKind.LONGDOUBLE = TypeKind(23) TypeKind.NULLPTR = TypeKind(24) ''' # void if t.kind == cindex.TypeKind.VOID: # void return TypeRef(cpptypeinfo.Void(), t.is_const_qualified()) # bool elif t.kind == cindex.TypeKind.BOOL: # void assert (t.get_size() == 1) return TypeRef(cpptypeinfo.Bool(), t.is_const_qualified()) # int elif t.kind == cindex.TypeKind.CHAR_S: # char assert (t.get_size() == 1) return TypeRef(cpptypeinfo.Int8(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.SCHAR: # signed char assert (t.get_size() == 1) return TypeRef(cpptypeinfo.Int8(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.SHORT: # short assert (t.get_size() == 2) return TypeRef(cpptypeinfo.Int16(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.INT: # int assert (t.get_size() == 4) return TypeRef(cpptypeinfo.Int32(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.LONG: # long assert (t.get_size() == 4) return TypeRef(cpptypeinfo.Int32(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.LONGLONG: # long long assert (t.get_size() == 8) return TypeRef(cpptypeinfo.Int64(), t.is_const_qualified()) # unsigned elif t.kind == cindex.TypeKind.UCHAR: # unsigned char assert (t.get_size() == 1) return TypeRef(cpptypeinfo.UInt8(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.WCHAR: # wchar_t assert (t.get_size() == 2) return TypeRef(cpptypeinfo.UInt16(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.USHORT: # unsigned short assert (t.get_size() == 2) return TypeRef(cpptypeinfo.UInt16(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.UINT: # unsigned int assert (t.get_size() == 4) return TypeRef(cpptypeinfo.UInt32(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.ULONG: # unsigned long assert (t.get_size() == 4) return TypeRef(cpptypeinfo.UInt32(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.ULONGLONG: # unsigned __int64 assert (t.get_size() == 8) return TypeRef(cpptypeinfo.UInt64(), t.is_const_qualified()) # float elif t.kind == cindex.TypeKind.FLOAT: # float assert (t.get_size() == 4) return TypeRef(cpptypeinfo.Float(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.DOUBLE: # double assert (t.get_size() == 8) return TypeRef(cpptypeinfo.Double(), t.is_const_qualified()) elif t.kind == cindex.TypeKind.LONGDOUBLE: # double size = t.get_size() assert (size == 8) return TypeRef(cpptypeinfo.Double(), t.is_const_qualified()) return None
HEADLINE = f'// generated cpptypeinfo-{cpptypeinfo.VERSION}' USING = '''using System; using System.Runtime.InteropServices; using System.Numerics; ''' class CSMarshalType(NamedTuple): type: str marshal_as: str = '' cstype_map: Dict[cpptypeinfo.Type, CSMarshalType] = { cpptypeinfo.Int8(): CSMarshalType('sbyte'), cpptypeinfo.Int16(): CSMarshalType('short'), cpptypeinfo.Int32(): CSMarshalType('int'), cpptypeinfo.Int64(): CSMarshalType('long'), cpptypeinfo.UInt8(): CSMarshalType('byte'), cpptypeinfo.UInt16(): CSMarshalType('ushort'), cpptypeinfo.UInt32(): CSMarshalType('uint'), cpptypeinfo.UInt64(): CSMarshalType('ulong'), cpptypeinfo.Float(): CSMarshalType('float'), cpptypeinfo.Double(): CSMarshalType('double'), cpptypeinfo.Bool(): CSMarshalType('bool', 'MarshalAs(UnmanagedType.U1)'), } # for function param cstype_pointer_map: Dict[cpptypeinfo.Type, CSMarshalType] = { cpptypeinfo.Void(): CSMarshalType('IntPtr'),
float b; float a; } ''' snippet_map = { 'd3d11': D3D11_SNIPPET, 'd2d1': D2D1_SNIPPET, 'd2dbasetypes': D2D_BASETYPES, } dlang_map: Dict[cpptypeinfo.Type, str] = { cpptypeinfo.Void(): 'void', cpptypeinfo.Int8(): 'byte', cpptypeinfo.Int16(): 'short', cpptypeinfo.Int32(): 'int', cpptypeinfo.Int64(): 'long', cpptypeinfo.UInt8(): 'ubyte', cpptypeinfo.UInt16(): 'ushort', cpptypeinfo.UInt32(): 'uint', cpptypeinfo.UInt64(): 'ulong', cpptypeinfo.Float(): 'float', cpptypeinfo.Double(): 'double', } def is_const(typeref: TypeRef) -> bool: if typeref.is_const: return True if not isinstance(typeref.ref, Typedef):
'ImGuiInputTextCallback', Function(cpptypeinfo.Int32(), [ Param(Pointer(parser.parse('struct ImGuiInputTextCallbackData'))) ])), 'ImGuiSizeCallback': parser.typedef( 'ImGuiSizeCallback', Function( cpptypeinfo.Void(), [Param(Pointer(parser.parse('struct ImGuiSizeCallbackData')))])), 'ImS8': parser.typedef('ImS8', cpptypeinfo.Int8()), 'ImU8': parser.typedef('ImU8', cpptypeinfo.UInt8()), 'ImS16': parser.typedef('ImS16', cpptypeinfo.Int16()), 'ImU16': parser.typedef('ImU16', cpptypeinfo.UInt16()), 'ImS32': parser.typedef('ImS32', cpptypeinfo.Int32()), 'ImU32': parser.typedef('ImU32', cpptypeinfo.UInt32()), 'ImS64': parser.typedef('ImS64', cpptypeinfo.Int64()), 'ImU64': parser.typedef('ImU64', cpptypeinfo.UInt64()), 'ImVec2': parser.struct( 'ImVec2', [Field(cpptypeinfo.Float(), 'x'), Field(cpptypeinfo.Float(), 'y')]),