+##########################################################################
+#
+# 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 Type:
- def __init__(self, name):
- self.name = name
+ __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.name
+ return self.expr
def isoutput(self):
return False
+ def decl(self):
+ pass
+
+ def impl(self):
+ pass
+
def dump(self, instance):
raise NotImplementedError
def wrap_instance(self, instance):
- pass
+ pass
def unwrap_instance(self, instance):
pass
-class Void(Type):
+class _Void(Type):
def __init__(self):
Type.__init__(self, "void")
-Void = Void()
+Void = _Void()
-class Intrinsic(Type):
+class Concrete(Type):
- def __init__(self, name, format):
- Type.__init__(self, name)
+ 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(Concrete):
+
+ def __init__(self, expr, format, base=10):
+ Concrete.__init__(self, expr)
self.format = format
- def dump(self, instance):
- print ' g_pLog->TextF("%s", %s);' % (self.format, instance)
+ def _dump(self, instance):
+ print ' Log::Literal%s(%s);' % (self.format, instance)
class Const(Type):
def __init__(self, type):
- Type.__init__(self, 'C' + type.name)
+
+ if isinstance(type, Pointer):
+ expr = type.expr + " const"
+ else:
+ expr = "const " + type.expr
+
+ Type.__init__(self, expr, 'C' + type.id)
+
self.type = type
def dump(self, instance):
self.type.dump(instance)
- def __str__(self):
- return "const " + str(self.type)
-
class Pointer(Type):
def __init__(self, type):
- Type.__init__(self, 'P' + type.name)
+ Type.__init__(self, type.expr + " *", 'P' + type.id)
self.type = type
- def __str__(self):
- return str(self.type) + " *"
-
def dump(self, instance):
print ' if(%s) {' % instance
+ print ' Log::BeginPointer("%s", (const void *)%s);' % (self.type, instance)
try:
self.type.dump("*" + instance)
except NotImplementedError:
- print ' g_pLog->TextF("%%p", %s);' % instance
+ pass
+ print ' Log::EndPointer();'
print ' }'
print ' else'
- print ' g_pLog->Text("NULL");'
+ print ' Log::LiteralNull();'
def wrap_instance(self, instance):
self.type.wrap_instance("*" + instance)
self.type.wrap_instance("*" + instance)
+def ConstPointer(type):
+ return Pointer(Const(type))
+
+
class OutPointer(Pointer):
def isoutput(self):
return True
-class Enum(Type):
+class Enum(Concrete):
def __init__(self, name, values):
- Type.__init__(self, name)
+ Concrete.__init__(self, name)
self.values = values
- def dump(self, instance):
+ def _dump(self, instance):
print ' switch(%s) {' % instance
for value in self.values:
print ' case %s:' % value
- print ' g_pLog->Text("%s");' % value
+ print ' Log::LiteralNamedConstant("%s");' % value
print ' break;'
print ' default:'
- print ' g_pLog->TextF("%%i", %s);' % instance
+ print ' Log::LiteralSInt(%s);' % instance
print ' break;'
print ' }'
-class Flags(Type):
+class FakeEnum(Enum):
+
+ def __init__(self, type, values):
+ Enum.__init__(self, type.expr, values)
+ self.type = type
+
+
+class Flags(Concrete):
def __init__(self, type, values):
- Type.__init__(self, type.name)
+ Concrete.__init__(self, type.expr)
+ self.type = type
self.values = values
+ def _dump(self, instance):
+ print ' %s l_Value = %s;' % (self.type, instance)
+ print ' Log::BeginBitmask("%s");' % (self.type,)
+ for value in self.values:
+ print ' if((l_Value & %s) == %s) {' % (value, value)
+ print ' Log::LiteralNamedConstant("%s");' % value
+ print ' l_Value &= ~%s;' % value
+ print ' }'
+ print ' if(l_Value) {'
+ self.type.dump("l_Value");
+ print ' }'
+ print ' Log::EndBitmask();'
+
-class Struct(Type):
+class Array(Type):
+
+ def __init__(self, type, length):
+ Type.__init__(self, type.expr + " *", 'P' + type.id)
+ self.type = type
+ self.length = length
+
+ def dump(self, instance):
+ index = '__i' + self.type.id
+ print ' Log::BeginArray("%s", %s);' % (self.type, self.length)
+ print ' for (int %s = 0; %s < %s; ++%s) {' % (index, index, self.length, index)
+ print ' Log::BeginElement("%s");' % (self.type,)
+ self.type.dump('(%s)[%s]' % (instance, index))
+ print ' Log::EndElement();'
+ print ' }'
+ print ' Log::EndArray();'
+
+ def wrap_instance(self, instance):
+ self.type.wrap_instance("*" + instance)
+
+ def unwrap_instance(self, instance):
+ self.type.wrap_instance("*" + instance)
+
+
+class OutArray(Array):
+
+ def isoutput(self):
+ return True
+
+
+class Struct(Concrete):
def __init__(self, name, members):
- Type.__init__(self, name)
+ Concrete.__init__(self, name)
+ self.name = name
self.members = members
- def dump(self, instance):
- print ' g_pLog->Text("{");'
- first = True
+ def _dump(self, instance):
+ print ' Log::BeginStruct("%s");' % (self.name,)
for type, name in self.members:
- if first:
- first = False
- else:
- print ' g_pLog->Text(", ");'
+ print ' Log::BeginMember("%s", "%s");' % (type, name)
type.dump('(%s).%s' % (instance, name))
- print ' g_pLog->Text("}");'
+ print ' Log::EndMember();'
+ print ' Log::EndStruct();'
class Alias(Type):
self.type.dump(instance)
+class Out(Type):
+
+ def __init__(self, type):
+ Type.__init__(self, type.expr)
+ self.type = type
+
+ def isoutput(self):
+ return True
+
+ def decl(self):
+ self.type.decl()
+
+ def impl(self):
+ self.type.impl()
+
+ def dump(self, instance):
+ self.type.dump(instance)
+
+ def wrap_instance(self, instance):
+ self.type.wrap_instance(instance)
+
+ def unwrap_instance(self, instance):
+ self.type.unwrap_instance(instance)
+
+
class Function:
- def __init__(self, type, name, args, call = '__stdcall'):
+ def __init__(self, type, name, args, call = '__stdcall', fail = None):
self.type = type
self.name = name
self.args = args
self.call = call
+ self.fail = fail
def prototype(self, name=None):
if name is not None:
s = self.call + ' ' + s
if name.startswith('*'):
s = '(' + s + ')'
- s = str(self.type) + ' ' + s
+ s = self.type.expr + ' ' + s
s += "("
if self.args:
s += ", ".join(["%s %s" % (type, name) for type, name in self.args])
s += ")"
return s
+ def pointer_type(self):
+ return 'P' + self.name
+
+ def pointer_value(self):
+ return 'p' + self.name
+
+ def wrap_decl(self):
+ ptype = self.pointer_type()
+ pvalue = self.pointer_value()
+ print 'typedef ' + self.prototype('* %s' % ptype) + ';'
+ print 'static %s %s = NULL;' % (ptype, pvalue)
+ print
+
+ def get_true_pointer(self):
+ raise NotImplementedError
+
+ def exit_impl(self):
+ print ' ExitProcess(0);'
+
+ def fail_impl(self):
+ if self.fail is not None:
+ if self.type is Void:
+ assert self.fail == ''
+ print ' return;'
+ else:
+ assert self.fail != ''
+ print ' return %s;' % self.fail
+ else:
+ self.exit_impl()
+
+ def wrap_impl(self):
+ pvalue = self.pointer_value()
+ print self.prototype() + ' {'
+ if self.type is Void:
+ result = ''
+ else:
+ print ' %s result;' % self.type
+ result = 'result = '
+ self.get_true_pointer()
+ print ' Log::BeginCall("%s");' % (self.name)
+ for type, name in self.args:
+ if not type.isoutput():
+ type.unwrap_instance(name)
+ print ' Log::BeginArg("%s", "%s");' % (type, name)
+ type.dump(name)
+ print ' Log::EndArg();'
+ print ' %s%s(%s);' % (result, pvalue, ', '.join([str(name) for type, name in self.args]))
+ for type, name in self.args:
+ if type.isoutput():
+ print ' Log::BeginArg("%s", "%s");' % (type, name)
+ type.dump(name)
+ print ' Log::EndArg();'
+ type.wrap_instance(name)
+ if self.type is not Void:
+ print ' Log::BeginReturn("%s");' % self.type
+ self.type.dump("result")
+ print ' Log::EndReturn();'
+ self.type.wrap_instance('result')
+ print ' Log::EndCall();'
+ self.post_call_impl()
+ if self.type is not Void:
+ print ' return result;'
+ print '}'
+ print
+
+ def post_call_impl(self):
+ pass
+
class Interface(Type):
def __init__(self, name, base=None):
Type.__init__(self, name)
+ self.name = name
self.base = base
self.methods = []
raise StopIteration
def wrap_name(self):
- return "Wrap" + self.name
+ return "Wrap" + self.expr
def wrap_pre_decl(self):
print "class %s;" % self.wrap_name()
else:
print ' %s result;' % method.type
result = 'result = '
- print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
- print ' g_pLog->BeginParam("this", "%s *");' % self.name
- print ' g_pLog->TextF("%p", m_pInstance);'
- print ' g_pLog->EndParam();'
+ print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
+ print ' Log::BeginArg("%s *", "this");' % self.name
+ print ' Log::BeginPointer("%s", (const void *)m_pInstance);' % self.name
+ print ' Log::EndPointer();'
+ print ' Log::EndArg();'
for type, name in method.args:
if not type.isoutput():
type.unwrap_instance(name)
- print ' g_pLog->BeginParam("%s", "%s");' % (name, type)
+ print ' Log::BeginArg("%s", "%s");' % (type, name)
type.dump(name)
- print ' g_pLog->EndParam();'
+ print ' Log::EndArg();'
print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
for type, name in method.args:
if type.isoutput():
- print ' g_pLog->BeginParam("%s", "%s");' % (name, type)
+ print ' Log::BeginArg("%s", "%s");' % (type, name)
type.dump(name)
- print ' g_pLog->EndParam();'
+ print ' Log::EndArg();'
type.wrap_instance(name)
if method.type is not Void:
- print ' g_pLog->BeginReturn("%s");' % method.type
+ print ' Log::BeginReturn("%s");' % method.type
method.type.dump("result")
- print ' g_pLog->EndReturn();'
+ print ' Log::EndReturn();'
method.type.wrap_instance('result')
- print ' g_pLog->EndCall();'
+ print ' Log::EndCall();'
if method.name == 'QueryInterface':
print ' if(*ppvObj == m_pInstance)'
print ' *ppvObj = this;'
class Method(Function):
def __init__(self, type, name, args):
- Function.__init__(self, type, name, args)
+ Function.__init__(self, type, name, args, call = '__stdcall')
towrap = []
print " if(%s)" % instance
print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
-String = Intrinsic("char *", "%s")
-Int = Intrinsic("int", "%i")
-Long = Intrinsic("long", "%li")
-Float = Intrinsic("float", "%f")
+
+class _String(Type):
+
+ def __init__(self):
+ Type.__init__(self, "char *")
+
+ def dump(self, instance):
+ print ' Log::LiteralString((const char *)%s);' % instance
+
+String = _String()
+
+
+class _Opaque(Type):
+
+ def __init__(self):
+ Type.__init__(self, "void")
+
+ def dump(self, instance):
+ print ' Log::LiteralOpaque();'
+
+Opaque = Pointer(_Opaque())
+
+
+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")
+Float = Literal("float", "Float")
+Double = Literal("double", "Float")
+SizeT = Literal("size_t", "UInt")
+WString = Literal("wchar_t *", "WString")
def wrap():
+ for type in all_types.itervalues():
+ type.decl()
+ print
+ for type in all_types.itervalues():
+ type.impl()
+ print
for type in towrap:
type.wrap_pre_decl()
print