def test_void_int(self) -> None: parser = cpptypeinfo.TypeParser() cpptypeinfo.parse_source(parser, 'void func(int a);', debug=True) func = parser.root_namespace.functions[0] self.assertEqual(TypeRef(cpptypeinfo.Void(), False), func.result) self.assertEqual(1, len(func.params)) self.assertEqual(TypeRef(cpptypeinfo.Int32()), func.params[0].typeref)
def test_int32(self) -> None: parser = cpptypeinfo.TypeParser() cpptypeinfo.parse_source(parser, 'typedef int 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.Int32())
def test_excpt(self) -> None: parser = cpptypeinfo.TypeParser() cpptypeinfo.parse_source(parser, ''' int __cdecl __C_specific_handler( struct _EXCEPTION_RECORD* ExceptionRecord, void* EstablisherFrame, struct _CONTEXT* ContextRecord, struct _DISPATCHER_CONTEXT* DispatcherContext ); ''', debug=True) func = parser.root_namespace.functions[0] self.assertEqual(TypeRef(cpptypeinfo.Void(), False), func.result) self.assertEqual(1, len(func.params)) self.assertEqual(TypeRef(cpptypeinfo.Int32()), func.params[0].typeref)
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'), cpptypeinfo.Int8():
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): return False
'ImGuiStorage': parser.parse('struct ImGuiStorage'), 'ImGuiStyle': parser.parse('struct ImGuiStyle'), 'ImGuiTextBuffer': parser.parse('struct ImGuiTextBuffer'), 'ImGuiTextFilter': parser.parse('struct ImGuiTextFilter'), 'ImTextureID': parser.typedef('ImTextureID', Pointer(cpptypeinfo.Void())), 'ImGuiID': parser.typedef('ImGuiID', cpptypeinfo.UInt32()), 'ImWchar': parser.typedef('ImWchar', cpptypeinfo.UInt16()), 'ImGuiCol': parser.typedef('ImGuiCol', cpptypeinfo.Int32()), 'ImGuiCond': parser.typedef('ImGuiCond', cpptypeinfo.Int32()), 'ImGuiDataType': parser.typedef('ImGuiDataType', cpptypeinfo.Int32()), 'ImGuiDir': parser.typedef('ImGuiDir', cpptypeinfo.Int32()), 'ImGuiKey': parser.typedef('ImGuiKey', cpptypeinfo.Int32()), 'ImGuiNavInput': parser.typedef('ImGuiNavInput', cpptypeinfo.Int32()), 'ImGuiMouseCursor': parser.typedef('ImGuiMouseCursor', cpptypeinfo.Int32()), 'ImGuiStyleVar': parser.typedef('ImGuiStyleVar', cpptypeinfo.Int32()), 'ImDrawCornerFlags':