From: José Fonseca Date: Thu, 25 Nov 2010 11:44:50 +0000 (+0000) Subject: base.py -> stdapi.py X-Git-Url: https://git.notmuchmail.org/git?a=commitdiff_plain;h=9796b8463dc518eb0c6cf92b4daf92d080a6a896;p=apitrace base.py -> stdapi.py --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 99dde68..d3e8832 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,7 +95,7 @@ if (WIN32) # add_custom_command ( # OUTPUT d3d8.cpp # COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d8.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d8.cpp - # DEPENDS d3d8.py d3d8types.py d3d8caps.py winapi.py base.py + # DEPENDS d3d8.py d3d8types.py d3d8caps.py winapi.py stdapi.py # ) # add_library (d3d8 SHARED d3d8.def d3d8.cpp log.cpp os_win32.cpp) # set_target_properties (d3d8 PROPERTIES PREFIX "") @@ -107,7 +107,7 @@ if (WIN32) # add_custom_command ( # OUTPUT d3d9.cpp # COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d9.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d9.cpp - # DEPENDS d3d9.py d3d9types.py d3d9caps.py d3dshader.py winapi.py base.py + # DEPENDS d3d9.py d3d9types.py d3d9caps.py d3dshader.py winapi.py stdapi.py # ) # add_library (d3d9 SHARED d3d9.def d3d9.cpp log.cpp os_win32.cpp) # set_target_properties (d3d9 PROPERTIES PREFIX "") @@ -119,7 +119,7 @@ if (WIN32) # add_custom_command ( # OUTPUT d3d10.cpp # COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/d3d10misc.py > ${CMAKE_CURRENT_BINARY_DIR}/d3d10.cpp - # DEPENDS d3d10misc.py winapi.py base.py + # DEPENDS d3d10misc.py winapi.py stdapi.py # ) # add_library (d3d10 SHARED d3d10.def d3d10.cpp log.cpp os_win32.cpp) # set_target_properties (d3d10 PROPERTIES PREFIX "") @@ -129,7 +129,7 @@ if (WIN32) add_custom_command ( OUTPUT opengl32.cpp COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/opengl32.py > ${CMAKE_CURRENT_BINARY_DIR}/opengl32.cpp - DEPENDS opengl32.py trace.py wglapi.py glapi.py winapi.py base.py + DEPENDS opengl32.py trace.py wglapi.py glapi.py winapi.py stdapi.py ) add_library (opengl SHARED opengl32.def opengl32.cpp log.cpp os_win32.cpp) set_target_properties (opengl PROPERTIES @@ -145,7 +145,7 @@ else () add_custom_command ( OUTPUT glxtrace.cpp COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glxtrace.py > ${CMAKE_CURRENT_BINARY_DIR}/glxtrace.cpp - DEPENDS glxtrace.py trace.py glapi.py base.py + DEPENDS glxtrace.py trace.py glapi.py stdapi.py ) add_library (glxtrace SHARED glxtrace.cpp log.cpp os_posix.cpp) @@ -160,7 +160,7 @@ if (GLEW_INCLUDE_DIR) add_custom_command ( OUTPUT glretrace.cpp COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/glretrace.py > ${CMAKE_CURRENT_BINARY_DIR}/glretrace.cpp - DEPENDS glretrace.py glapi.py base.py + DEPENDS glretrace.py glapi.py stdapi.py ) include_directories ( diff --git a/base.py b/base.py deleted file mode 100644 index 8213403..0000000 --- a/base.py +++ /dev/null @@ -1,557 +0,0 @@ -########################################################################## -# -# Copyright 2008-2009 VMware, Inc. -# All Rights Reserved. -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -# THE SOFTWARE. -# -##########################################################################/ - -"""C basic types""" - - -import debug - - -all_types = {} - - -class Visitor: - - def visit(self, type, *args, **kwargs): - return type.visit(self, *args, **kwargs) - - def visit_void(self, void, *args, **kwargs): - raise NotImplementedError - - def visit_literal(self, literal, *args, **kwargs): - raise NotImplementedError - - def visit_string(self, string, *args, **kwargs): - raise NotImplementedError - - def visit_const(self, const, *args, **kwargs): - raise NotImplementedError - - def visit_struct(self, struct, *args, **kwargs): - raise NotImplementedError - - def visit_array(self, array, *args, **kwargs): - raise NotImplementedError - - def visit_blob(self, blob, *args, **kwargs): - raise NotImplementedError - - def visit_enum(self, enum, *args, **kwargs): - raise NotImplementedError - - def visit_bitmask(self, bitmask, *args, **kwargs): - raise NotImplementedError - - def visit_pointer(self, pointer, *args, **kwargs): - raise NotImplementedError - - def visit_handle(self, handle, *args, **kwargs): - raise NotImplementedError - - def visit_alias(self, alias, *args, **kwargs): - raise NotImplementedError - - def visit_opaque(self, opaque, *args, **kwargs): - raise NotImplementedError - - def visit_interface(self, interface, *args, **kwargs): - raise NotImplementedError - - -class OnceVisitor(Visitor): - - def __init__(self): - self.__visited = set() - - def visit(self, type, *args, **kwargs): - if type not in self.__visited: - self.__visited.add(type) - return type.visit(self, *args, **kwargs) - return None - - -class Rebuilder(Visitor): - - def visit_void(self, void): - return void - - def visit_literal(self, literal): - return literal - - def visit_string(self, string): - return string - - def visit_const(self, const): - return Const(const.type) - - def visit_struct(self, struct): - members = [self.visit(member) for member in struct.members] - return Struct(struct.name, members) - - def visit_array(self, array): - type = self.visit(array.type) - return Array(type, array.length) - - def visit_blob(self, blob): - type = self.visit(blob.type) - return Blob(type, blob.size) - - def visit_enum(self, enum): - return enum - - def visit_bitmask(self, bitmask): - type = self.visit(bitmask.type) - return Bitmask(type, bitmask.values) - - def visit_pointer(self, pointer): - type = self.visit(pointer.type) - return Pointer(type) - - def visit_handle(self, handle): - type = self.visit(handle.type) - return Handle(handle.name, type) - - def visit_alias(self, alias): - type = self.visit(alias.type) - return Alias(alias.expr, type) - - def visit_opaque(self, opaque): - return opaque - - -class Type: - - __seq = 0 - - def __init__(self, expr, id = ''): - self.expr = expr - - for char in id: - assert char.isalnum() or char in '_ ' - - id = id.replace(' ', '_') - - if id in all_types: - Type.__seq += 1 - id += str(Type.__seq) - - assert id not in all_types - all_types[id] = self - - self.id = id - - def __str__(self): - return self.expr - - def visit(self, visitor, *args, **kwargs): - raise NotImplementedError - - - -class _Void(Type): - - def __init__(self): - Type.__init__(self, "void") - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_void(self, *args, **kwargs) - -Void = _Void() - - -class Concrete(Type): - - def decl(self): - print 'static void Dump%s(const %s &value);' % (self.id, self.expr) - - def impl(self): - print 'static void Dump%s(const %s &value) {' % (self.id, self.expr) - self._dump("value"); - print '}' - print - - def _dump(self, instance): - raise NotImplementedError - - def dump(self, instance): - print ' Dump%s(%s);' % (self.id, instance) - - -class Literal(Type): - - def __init__(self, expr, format, base=10): - Type.__init__(self, expr) - self.format = format - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_literal(self, *args, **kwargs) - - -class Const(Type): - - def __init__(self, type): - - if type.expr.startswith("const "): - expr = type.expr + " const" - else: - expr = "const " + type.expr - - Type.__init__(self, expr, 'C' + type.id) - - self.type = type - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_const(self, *args, **kwargs) - - -class Pointer(Type): - - def __init__(self, type): - Type.__init__(self, type.expr + " *", 'P' + type.id) - self.type = type - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_pointer(self, *args, **kwargs) - - -class Handle(Type): - - def __init__(self, name, type): - Type.__init__(self, type.expr, 'P' + type.id) - self.name = name - self.type = type - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_handle(self, *args, **kwargs) - - -def ConstPointer(type): - return Pointer(Const(type)) - - -class Enum(Concrete): - - def __init__(self, name, values): - Concrete.__init__(self, name) - self.values = values - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_enum(self, *args, **kwargs) - - -def FakeEnum(type, values): - return Enum(type.expr, values) - - -class Bitmask(Concrete): - - def __init__(self, type, values): - Concrete.__init__(self, type.expr) - self.type = type - self.values = values - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_bitmask(self, *args, **kwargs) - -Flags = Bitmask - - -class Array(Type): - - def __init__(self, type, length): - Type.__init__(self, type.expr + " *") - self.type = type - self.length = length - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_array(self, *args, **kwargs) - - -class Blob(Type): - - def __init__(self, type, size): - Type.__init__(self, type.expr + ' *') - self.type = type - self.size = size - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_blob(self, *args, **kwargs) - - -class Struct(Concrete): - - def __init__(self, name, members): - Concrete.__init__(self, name) - self.name = name - self.members = members - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_struct(self, *args, **kwargs) - - -class Alias(Type): - - def __init__(self, expr, type): - Type.__init__(self, expr) - self.type = type - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_alias(self, *args, **kwargs) - - -def Out(type, name): - arg = Arg(type, name, output=True) - return arg - - -class Arg: - - def __init__(self, type, name, output=False): - self.type = type - self.name = name - self.output = output - self.index = None - - def __str__(self): - return '%s %s' % (self.type, self.name) - - -class Function: - - def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False): - self.type = type - self.name = name - - self.args = [] - index = 0 - for arg in args: - if isinstance(arg, tuple): - arg_type, arg_name = arg - arg = Arg(arg_type, arg_name) - arg.index = index - index += 1 - self.args.append(arg) - - self.call = call - self.fail = fail - self.sideeffects = sideeffects - self.hidden = False - - def prototype(self, name=None): - if name is not None: - name = name.strip() - else: - name = self.name - s = name - if self.call: - s = self.call + ' ' + s - if name.startswith('*'): - s = '(' + s + ')' - s = self.type.expr + ' ' + s - s += "(" - if self.args: - s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args]) - else: - s += "void" - s += ")" - return s - - -def StdFunction(*args, **kwargs): - kwargs.setdefault('call', 'GLAPIENTRY') - return Function(*args, **kwargs) - - -def FunctionPointer(type, name, args, **kwargs): - # XXX - return Opaque(name) - - -class Interface(Type): - - def __init__(self, name, base=None): - Type.__init__(self, name) - self.name = name - self.base = base - self.methods = [] - - def itermethods(self): - if self.base is not None: - for method in self.base.itermethods(): - yield method - for method in self.methods: - yield method - raise StopIteration - - -class Method(Function): - - def __init__(self, type, name, args): - Function.__init__(self, type, name, args, call = '__stdcall') - - -towrap = [] - - -def WrapPointer(type): - return Pointer(type) - - -class String(Type): - - def __init__(self, expr = "char *", length = None): - Type.__init__(self, expr) - self.length = length - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_string(self, *args, **kwargs) - -CString = String() - - -class Opaque(Type): - '''Opaque pointer.''' - - def __init__(self, expr): - Type.__init__(self, expr) - - def visit(self, visitor, *args, **kwargs): - return visitor.visit_opaque(self, *args, **kwargs) - - -def OpaquePointer(type): - return Opaque(type.expr + ' *') - - -class Collector(Visitor): - '''Collect.''' - - def __init__(self): - self.__visited = set() - self.types = [] - - def visit(self, type): - if type in self.__visited: - return - self.__visited.add(type) - Visitor.visit(self, type) - self.types.append(type) - - def visit_void(self, literal): - pass - - def visit_literal(self, literal): - pass - - def visit_string(self, string): - pass - - def visit_const(self, const): - self.visit(const.type) - - def visit_struct(self, struct): - for type, name in struct.members: - self.visit(type) - - def visit_array(self, array): - self.visit(array.type) - - def visit_blob(self, array): - pass - - def visit_enum(self, enum): - pass - - def visit_bitmask(self, bitmask): - self.visit(bitmask.type) - - def visit_pointer(self, pointer): - self.visit(pointer.type) - - def visit_handle(self, handle): - self.visit(handle.type) - - def visit_alias(self, alias): - self.visit(alias.type) - - def visit_opaque(self, opaque): - pass - - def visit_interface(self, interface): - pass - - -class API: - - def __init__(self, name): - self.name = name - self.headers = [] - self.functions = [] - self.interfaces = [] - - def all_types(self): - collector = Collector() - for function in self.functions: - for arg in function.args: - collector.visit(arg.type) - collector.visit(function.type) - for interface in self.interfaces: - collector.visit(interface) - for method in interface.methods: - for arg in method.args: - collector.visit(arg.type) - collector.visit(method.type) - return collector.types - - def add_function(self, function): - self.functions.append(function) - - def add_functions(self, functions): - for function in functions: - self.add_function(function) - - def add_interface(self, interface): - self.interfaces.append(interface) - - def add_interfaces(self, interfaces): - self.interfaces.extend(interfaces) - - -Bool = Literal("bool", "Bool") -SChar = Literal("signed char", "SInt") -UChar = Literal("unsigned char", "UInt") -Short = Literal("short", "SInt") -Int = Literal("int", "SInt") -Long = Literal("long", "SInt") -LongLong = Literal("long long", "SInt") -UShort = Literal("unsigned short", "UInt") -UInt = Literal("unsigned int", "UInt") -ULong = Literal("unsigned long", "UInt") -ULongLong = Literal("unsigned long long", "UInt") -Float = Literal("float", "Float") -Double = Literal("double", "Float") -SizeT = Literal("size_t", "UInt") -WString = Literal("wchar_t *", "WString") - diff --git a/d3dshader.py b/d3dshader.py index b9e18ab..bb37678 100644 --- a/d3dshader.py +++ b/d3dshader.py @@ -24,7 +24,7 @@ ##########################################################################/ -from base import Type +from stdapi import Type class D3DShader(Type): diff --git a/glapi.py b/glapi.py index 219ae94..c94358c 100644 --- a/glapi.py +++ b/glapi.py @@ -24,7 +24,7 @@ ##########################################################################/ -from base import * +from stdapi import * GLboolean = Alias("GLboolean", Bool) diff --git a/glretrace.py b/glretrace.py index 9518320..e42a922 100644 --- a/glretrace.py +++ b/glretrace.py @@ -24,12 +24,12 @@ ##########################################################################/ -import base +import stdapi import glapi -class ConstRemover(base.Rebuilder): +class ConstRemover(stdapi.Rebuilder): def visit_const(self, const): return const.type @@ -38,10 +38,10 @@ class ConstRemover(base.Rebuilder): expr = opaque.expr if expr.startswith('const '): expr = expr[6:] - return base.Opaque(expr) + return stdapi.Opaque(expr) -class ValueExtractor(base.Visitor): +class ValueExtractor(stdapi.Visitor): def visit_literal(self, literal, lvalue, rvalue): if literal.format == 'Bool': @@ -100,7 +100,7 @@ class ValueExtractor(base.Visitor): -class ValueWrapper(base.Visitor): +class ValueWrapper(stdapi.Visitor): def visit_literal(self, literal, lvalue, rvalue): pass @@ -166,7 +166,7 @@ def retrace_function(function): print ' std::cerr << "warning: unsupported call %s\\n";' % function.name print ' return;' arg_names = ", ".join([arg.name for arg in function.args]) - if function.type is not base.Void: + if function.type is not stdapi.Void: print ' %s __result;' % (function.type) print ' __result = %s(%s);' % (function.name, arg_names) else: @@ -180,7 +180,7 @@ def retrace_function(function): ValueWrapper().visit(arg_type, lvalue, rvalue) except NotImplementedError: print ' // FIXME: %s' % arg.name - if function.type is not base.Void: + if function.type is not stdapi.Void: rvalue = '*call.ret' lvalue = '__result' try: @@ -223,7 +223,7 @@ def retrace_functions(functions): def retrace_api(api): types = api.all_types() - handles = [type for type in types if isinstance(type, base.Handle)] + handles = [type for type in types if isinstance(type, stdapi.Handle)] for handle in handles: print 'static std::map<%s, %s> __%s_map;' % (handle.type, handle.type, handle.name) print diff --git a/glxtrace.py b/glxtrace.py index 2fa5ae5..38bac45 100644 --- a/glxtrace.py +++ b/glxtrace.py @@ -24,7 +24,7 @@ ##########################################################################/ -from base import * +from stdapi import * from glapi import glapi import trace diff --git a/stdapi.py b/stdapi.py new file mode 100644 index 0000000..8326a72 --- /dev/null +++ b/stdapi.py @@ -0,0 +1,557 @@ +########################################################################## +# +# Copyright 2008-2009 VMware, Inc. +# All Rights Reserved. +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +##########################################################################/ + +"""C basic types""" + + +import debug + + +all_types = {} + + +class Visitor: + + def visit(self, type, *args, **kwargs): + return type.visit(self, *args, **kwargs) + + def visit_void(self, void, *args, **kwargs): + raise NotImplementedError + + def visit_literal(self, literal, *args, **kwargs): + raise NotImplementedError + + def visit_string(self, string, *args, **kwargs): + raise NotImplementedError + + def visit_const(self, const, *args, **kwargs): + raise NotImplementedError + + def visit_struct(self, struct, *args, **kwargs): + raise NotImplementedError + + def visit_array(self, array, *args, **kwargs): + raise NotImplementedError + + def visit_blob(self, blob, *args, **kwargs): + raise NotImplementedError + + def visit_enum(self, enum, *args, **kwargs): + raise NotImplementedError + + def visit_bitmask(self, bitmask, *args, **kwargs): + raise NotImplementedError + + def visit_pointer(self, pointer, *args, **kwargs): + raise NotImplementedError + + def visit_handle(self, handle, *args, **kwargs): + raise NotImplementedError + + def visit_alias(self, alias, *args, **kwargs): + raise NotImplementedError + + def visit_opaque(self, opaque, *args, **kwargs): + raise NotImplementedError + + def visit_interface(self, interface, *args, **kwargs): + raise NotImplementedError + + +class OnceVisitor(Visitor): + + def __init__(self): + self.__visited = set() + + def visit(self, type, *args, **kwargs): + if type not in self.__visited: + self.__visited.add(type) + return type.visit(self, *args, **kwargs) + return None + + +class Rebuilder(Visitor): + + def visit_void(self, void): + return void + + def visit_literal(self, literal): + return literal + + def visit_string(self, string): + return string + + def visit_const(self, const): + return Const(const.type) + + def visit_struct(self, struct): + members = [self.visit(member) for member in struct.members] + return Struct(struct.name, members) + + def visit_array(self, array): + type = self.visit(array.type) + return Array(type, array.length) + + def visit_blob(self, blob): + type = self.visit(blob.type) + return Blob(type, blob.size) + + def visit_enum(self, enum): + return enum + + def visit_bitmask(self, bitmask): + type = self.visit(bitmask.type) + return Bitmask(type, bitmask.values) + + def visit_pointer(self, pointer): + type = self.visit(pointer.type) + return Pointer(type) + + def visit_handle(self, handle): + type = self.visit(handle.type) + return Handle(handle.name, type) + + def visit_alias(self, alias): + type = self.visit(alias.type) + return Alias(alias.expr, type) + + def visit_opaque(self, opaque): + return opaque + + +class Type: + + __seq = 0 + + def __init__(self, expr, id = ''): + self.expr = expr + + for char in id: + assert char.isalnum() or char in '_ ' + + id = id.replace(' ', '_') + + if id in all_types: + Type.__seq += 1 + id += str(Type.__seq) + + assert id not in all_types + all_types[id] = self + + self.id = id + + def __str__(self): + return self.expr + + def visit(self, visitor, *args, **kwargs): + raise NotImplementedError + + + +class _Void(Type): + + def __init__(self): + Type.__init__(self, "void") + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_void(self, *args, **kwargs) + +Void = _Void() + + +class Concrete(Type): + + def decl(self): + print 'static void Dump%s(const %s &value);' % (self.id, self.expr) + + def impl(self): + print 'static void Dump%s(const %s &value) {' % (self.id, self.expr) + self._dump("value"); + print '}' + print + + def _dump(self, instance): + raise NotImplementedError + + def dump(self, instance): + print ' Dump%s(%s);' % (self.id, instance) + + +class Literal(Type): + + def __init__(self, expr, format, base=10): + Type.__init__(self, expr) + self.format = format + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_literal(self, *args, **kwargs) + + +class Const(Type): + + def __init__(self, type): + + if type.expr.startswith("const "): + expr = type.expr + " const" + else: + expr = "const " + type.expr + + Type.__init__(self, expr, 'C' + type.id) + + self.type = type + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_const(self, *args, **kwargs) + + +class Pointer(Type): + + def __init__(self, type): + Type.__init__(self, type.expr + " *", 'P' + type.id) + self.type = type + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_pointer(self, *args, **kwargs) + + +class Handle(Type): + + def __init__(self, name, type): + Type.__init__(self, type.expr, 'P' + type.id) + self.name = name + self.type = type + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_handle(self, *args, **kwargs) + + +def ConstPointer(type): + return Pointer(Const(type)) + + +class Enum(Concrete): + + def __init__(self, name, values): + Concrete.__init__(self, name) + self.values = values + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_enum(self, *args, **kwargs) + + +def FakeEnum(type, values): + return Enum(type.expr, values) + + +class Bitmask(Concrete): + + def __init__(self, type, values): + Concrete.__init__(self, type.expr) + self.type = type + self.values = values + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_bitmask(self, *args, **kwargs) + +Flags = Bitmask + + +class Array(Type): + + def __init__(self, type, length): + Type.__init__(self, type.expr + " *") + self.type = type + self.length = length + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_array(self, *args, **kwargs) + + +class Blob(Type): + + def __init__(self, type, size): + Type.__init__(self, type.expr + ' *') + self.type = type + self.size = size + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_blob(self, *args, **kwargs) + + +class Struct(Concrete): + + def __init__(self, name, members): + Concrete.__init__(self, name) + self.name = name + self.members = members + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_struct(self, *args, **kwargs) + + +class Alias(Type): + + def __init__(self, expr, type): + Type.__init__(self, expr) + self.type = type + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_alias(self, *args, **kwargs) + + +def Out(type, name): + arg = Arg(type, name, output=True) + return arg + + +class Arg: + + def __init__(self, type, name, output=False): + self.type = type + self.name = name + self.output = output + self.index = None + + def __str__(self): + return '%s %s' % (self.type, self.name) + + +class Function: + + def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, hidden=False): + self.type = type + self.name = name + + self.args = [] + index = 0 + for arg in args: + if isinstance(arg, tuple): + arg_type, arg_name = arg + arg = Arg(arg_type, arg_name) + arg.index = index + index += 1 + self.args.append(arg) + + self.call = call + self.fail = fail + self.sideeffects = sideeffects + self.hidden = False + + def prototype(self, name=None): + if name is not None: + name = name.strip() + else: + name = self.name + s = name + if self.call: + s = self.call + ' ' + s + if name.startswith('*'): + s = '(' + s + ')' + s = self.type.expr + ' ' + s + s += "(" + if self.args: + s += ", ".join(["%s %s" % (arg.type, arg.name) for arg in self.args]) + else: + s += "void" + s += ")" + return s + + +def StdFunction(*args, **kwargs): + kwargs.setdefault('call', 'GLAPIENTRY') + return Function(*args, **kwargs) + + +def FunctionPointer(type, name, args, **kwargs): + # XXX + return Opaque(name) + + +class Interface(Type): + + def __init__(self, name, base=None): + Type.__init__(self, name) + self.name = name + self.base = base + self.methods = [] + + def itermethods(self): + if self.base is not None: + for method in self.stdapi.itermethods(): + yield method + for method in self.methods: + yield method + raise StopIteration + + +class Method(Function): + + def __init__(self, type, name, args): + Function.__init__(self, type, name, args, call = '__stdcall') + + +towrap = [] + + +def WrapPointer(type): + return Pointer(type) + + +class String(Type): + + def __init__(self, expr = "char *", length = None): + Type.__init__(self, expr) + self.length = length + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_string(self, *args, **kwargs) + +CString = String() + + +class Opaque(Type): + '''Opaque pointer.''' + + def __init__(self, expr): + Type.__init__(self, expr) + + def visit(self, visitor, *args, **kwargs): + return visitor.visit_opaque(self, *args, **kwargs) + + +def OpaquePointer(type): + return Opaque(type.expr + ' *') + + +class Collector(Visitor): + '''Collect.''' + + def __init__(self): + self.__visited = set() + self.types = [] + + def visit(self, type): + if type in self.__visited: + return + self.__visited.add(type) + Visitor.visit(self, type) + self.types.append(type) + + def visit_void(self, literal): + pass + + def visit_literal(self, literal): + pass + + def visit_string(self, string): + pass + + def visit_const(self, const): + self.visit(const.type) + + def visit_struct(self, struct): + for type, name in struct.members: + self.visit(type) + + def visit_array(self, array): + self.visit(array.type) + + def visit_blob(self, array): + pass + + def visit_enum(self, enum): + pass + + def visit_bitmask(self, bitmask): + self.visit(bitmask.type) + + def visit_pointer(self, pointer): + self.visit(pointer.type) + + def visit_handle(self, handle): + self.visit(handle.type) + + def visit_alias(self, alias): + self.visit(alias.type) + + def visit_opaque(self, opaque): + pass + + def visit_interface(self, interface): + pass + + +class API: + + def __init__(self, name): + self.name = name + self.headers = [] + self.functions = [] + self.interfaces = [] + + def all_types(self): + collector = Collector() + for function in self.functions: + for arg in function.args: + collector.visit(arg.type) + collector.visit(function.type) + for interface in self.interfaces: + collector.visit(interface) + for method in interface.methods: + for arg in method.args: + collector.visit(arg.type) + collector.visit(method.type) + return collector.types + + def add_function(self, function): + self.functions.append(function) + + def add_functions(self, functions): + for function in functions: + self.add_function(function) + + def add_interface(self, interface): + self.interfaces.append(interface) + + def add_interfaces(self, interfaces): + self.interfaces.extend(interfaces) + + +Bool = Literal("bool", "Bool") +SChar = Literal("signed char", "SInt") +UChar = Literal("unsigned char", "UInt") +Short = Literal("short", "SInt") +Int = Literal("int", "SInt") +Long = Literal("long", "SInt") +LongLong = Literal("long long", "SInt") +UShort = Literal("unsigned short", "UInt") +UInt = Literal("unsigned int", "UInt") +ULong = Literal("unsigned long", "UInt") +ULongLong = Literal("unsigned long long", "UInt") +Float = Literal("float", "Float") +Double = Literal("double", "Float") +SizeT = Literal("size_t", "UInt") +WString = Literal("wchar_t *", "WString") + diff --git a/trace.py b/trace.py index ab55555..f7f10b8 100644 --- a/trace.py +++ b/trace.py @@ -26,13 +26,13 @@ """C basic types""" -import base +import stdapi all_types = {} -class DumpDeclarator(base.OnceVisitor): +class DumpDeclarator(stdapi.OnceVisitor): '''Declare helper functions to dump complex types.''' def visit_void(self, literal): @@ -111,7 +111,7 @@ class DumpDeclarator(base.OnceVisitor): pass -class DumpImplementer(base.Visitor): +class DumpImplementer(stdapi.Visitor): '''Dump an instance.''' def visit_literal(self, literal, instance): @@ -180,7 +180,7 @@ dump_instance = DumpImplementer().visit -class Wrapper(base.Visitor): +class Wrapper(stdapi.Visitor): '''Wrap an instance.''' def visit_void(self, type, instance): @@ -290,7 +290,7 @@ class Tracer: def trace_function_fail(self, function): if function.fail is not None: - if function.type is base.Void: + if function.type is stdapi.Void: assert function.fail == '' print ' return;' else: @@ -314,7 +314,7 @@ class Tracer: def trace_function_impl(self, function): pvalue = self.function_pointer_value(function) print function.prototype() + ' {' - if function.type is base.Void: + if function.type is stdapi.Void: result = '' else: print ' %s __result;' % function.type @@ -330,10 +330,10 @@ class Tracer: if arg.output: self.dump_arg(function, arg) self.wrap_arg(function, arg) - if function.type is not base.Void: + if function.type is not stdapi.Void: self.dump_ret(function, "__result") print ' Log::EndCall();' - if function.type is not base.Void: + if function.type is not stdapi.Void: self.wrap_ret(function, "__result") print ' return __result;' print '}' diff --git a/winapi.py b/winapi.py index eec3fde..30826e3 100644 --- a/winapi.py +++ b/winapi.py @@ -25,7 +25,7 @@ """Win32 API type description.""" -from base import * +from stdapi import * SHORT = Alias("SHORT", Short) USHORT = Alias("USHORT", UShort)