return visitor.visitLiteral(self, *args, **kwargs)
+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", "Double")
+SizeT = Literal("size_t", "UInt")
+
+Char = Literal("char", "SInt")
+WChar = Literal("wchar_t", "SInt")
+
+Int8 = Literal("int8_t", "SInt")
+UInt8 = Literal("uint8_t", "UInt")
+Int16 = Literal("int16_t", "SInt")
+UInt16 = Literal("uint16_t", "UInt")
+Int32 = Literal("int32_t", "SInt")
+UInt32 = Literal("uint32_t", "UInt")
+Int64 = Literal("int64_t", "SInt")
+UInt64 = Literal("uint64_t", "UInt")
+
+IntPtr = Literal("intptr_t", "SInt")
+UIntPtr = Literal("uintptr_t", "UInt")
+
class Const(Type):
def __init__(self, type):
# While "const foo" and "foo const" are synonymous, "const foo *" and
# "foo * const" are not quite the same, and some compilers do enforce
# strict const correctness.
- if isinstance(type, String) or type is WString:
- # For strings we never intend to say a const pointer to chars, but
- # rather a point to const chars.
- expr = "const " + type.expr
- elif type.expr.startswith("const ") or '*' in type.expr:
+ if type.expr.startswith("const ") or '*' in type.expr:
expr = type.expr + " const"
else:
# The most legible
# 0-3 are reserved to memcpy, malloc, free, and realloc
__id = 4
- def __init__(self, type, name, args, call = '', fail = None, sideeffects=True):
+ def __init__(self, type, name, args, call = '', fail = None, sideeffects=True, internal=False):
self.id = Function.__id
Function.__id += 1
self.call = call
self.fail = fail
self.sideeffects = sideeffects
+ self.internal = internal
def prototype(self, name=None):
if name is not None:
return visitor.visitInterface(self, *args, **kwargs)
def getMethodByName(self, name):
- for methods in self.methods:
- if methods.name == name:
- return methods
+ for method in self.iterMethods():
+ if method.name == name:
+ return method
return None
def iterMethods(self):
class Method(Function):
- def __init__(self, type, name, args, call = '__stdcall', const=False, sideeffects=True):
+ def __init__(self, type, name, args, call = '', const=False, sideeffects=True):
+ assert call == '__stdcall'
Function.__init__(self, type, name, args, call = call, sideeffects=sideeffects)
for index in range(len(self.args)):
self.args[index].index = index + 1
class String(Type):
+ '''Human-legible character string.'''
- def __init__(self, expr = "char *", length = None, kind = 'String'):
- Type.__init__(self, expr)
+ def __init__(self, type = Char, length = None, wide = False):
+ assert isinstance(type, Type)
+ Type.__init__(self, type.expr + ' *')
+ self.type = type
self.length = length
- self.kind = kind
+ self.wide = wide
def visit(self, visitor, *args, **kwargs):
return visitor.visitString(self, *args, **kwargs)
return literal
def visitString(self, string):
- return string
+ string_type = self.visit(string.type)
+ if string_type is string.type:
+ return string
+ else:
+ return String(string_type, string.length, string.wide)
def visitConst(self, const):
const_type = self.visit(const.type)
class MutableRebuilder(Rebuilder):
'''Type visitor which derives a mutable type.'''
+ def visitString(self, string):
+ return string
+
def visitConst(self, const):
# Strip out const qualifier
return const.type
-class API:
- '''API abstraction.
-
- Essentially, a collection of types, functions, and interfaces.
- '''
+class Module:
+ '''A collection of functions.'''
def __init__(self, name = None):
self.name = name
self.functions = []
self.interfaces = []
+ def addFunctions(self, functions):
+ self.functions.extend(functions)
+
+ def addInterfaces(self, interfaces):
+ self.interfaces.extend(interfaces)
+
+ def mergeModule(self, module):
+ self.headers.extend(module.headers)
+ self.functions.extend(module.functions)
+ self.interfaces.extend(module.interfaces)
+
+ def getFunctionByName(self, name):
+ for function in self.functions:
+ if function.name == name:
+ return function
+ return None
+
+
+class API:
+ '''API abstraction.
+
+ Essentially, a collection of types, functions, and interfaces.
+ '''
+
+ def __init__(self, modules = None):
+ self.modules = []
+ if modules is not None:
+ self.modules.extend(modules)
+
def getAllTypes(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.iterMethods():
- for arg in method.args:
+ for module in self.modules:
+ for function in module.functions:
+ for arg in function.args:
collector.visit(arg.type)
- collector.visit(method.type)
+ collector.visit(function.type)
+ for interface in module.interfaces:
+ collector.visit(interface)
+ for method in interface.iterMethods():
+ for arg in method.args:
+ collector.visit(arg.type)
+ collector.visit(method.type)
return collector.types
+ def getAllFunctions(self):
+ functions = []
+ for module in self.modules:
+ functions.extend(module.functions)
+ return functions
+
def getAllInterfaces(self):
types = self.getAllTypes()
interfaces = [type for type in types if isinstance(type, Interface)]
- for interface in self.interfaces:
- if interface not in interfaces:
- interfaces.append(interface)
+ for module in self.modules:
+ for interface in module.interfaces:
+ if interface not in interfaces:
+ interfaces.append(interface)
return interfaces
- def addFunction(self, function):
- self.functions.append(function)
-
- def addFunctions(self, functions):
- for function in functions:
- self.addFunction(function)
-
- def addInterface(self, interface):
- self.interfaces.append(interface)
-
- def addInterfaces(self, interfaces):
- self.interfaces.extend(interfaces)
-
- def addApi(self, api):
- self.headers.extend(api.headers)
- self.addFunctions(api.functions)
- self.addInterfaces(api.interfaces)
+ def addModule(self, module):
+ self.modules.append(module)
def getFunctionByName(self, name):
- for function in self.functions:
- if function.name == name:
- return function
+ for module in self.modules:
+ for function in module.functions:
+ if function.name == name:
+ return function
return None
-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", "Double")
-SizeT = Literal("size_t", "UInt")
-
# C string (i.e., zero terminated)
-CString = String()
-WString = String("wchar_t *", kind="WString")
-
-Int8 = Literal("int8_t", "SInt")
-UInt8 = Literal("uint8_t", "UInt")
-Int16 = Literal("int16_t", "SInt")
-UInt16 = Literal("uint16_t", "UInt")
-Int32 = Literal("int32_t", "SInt")
-UInt32 = Literal("uint32_t", "UInt")
-Int64 = Literal("int64_t", "SInt")
-UInt64 = Literal("uint64_t", "UInt")
+CString = String(Char)
+WString = String(WChar, wide=True)
+ConstCString = String(Const(Char))
+ConstWString = String(Const(WChar), wide=True)