+#############################################################################
+#
+# Copyright 2008 Tungsten Graphics, Inc.
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#############################################################################
+
"""C basic types"""
+
+import debug
+
+
+all_types = {}
+
class Type:
def __init__(self, name):
def __str__(self):
return self.name
+ def identifier(self):
+ return self.name.replace(' ', '_')
+
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
+
+ def unwrap_instance(self, instance):
pass
-class Void(Type):
+class _Void(Type):
def __init__(self):
Type.__init__(self, "void")
-Void = Void()
+Void = _Void()
+
+class Concrete(Type):
-class Intrinsic(Type):
+ def __init__(self, name):
+ for char in name:
+ assert char.isalnum() or char in '_ '
- def __init__(self, name, format):
Type.__init__(self, name)
+
+ assert self.name not in all_types
+ if self.name not in all_types:
+ all_types[self.name] = self
+
+ def decl(self):
+ print 'void Dump%s(const %s &value);' % (self.identifier(), str(self))
+
+ def impl(self):
+ print 'void Dump%s(const %s &value) {' % (self.identifier(), str(self))
+ self._dump("value");
+ print '}'
+ print
+
+ def _dump(self, instance):
+ raise NotImplementedError
+
+ def dump(self, instance):
+ print ' Dump%s(%s);' % (self.identifier(), instance)
+
+
+class Intrinsic(Concrete):
+
+ def __init__(self, expr, format, name = None):
+ if name is None:
+ name = expr
+ Concrete.__init__(self, name)
+ self.expr = expr
self.format = format
+ def _dump(self, instance):
+ print ' Log::TextF("%s", %s);' % (self.format, instance)
+
+ def __str__(self):
+ return self.expr
+
class Const(Type):
Type.__init__(self, 'C' + type.name)
self.type = type
+ def dump(self, instance):
+ self.type.dump(instance)
+
def __str__(self):
return "const " + str(self.type)
def __str__(self):
return str(self.type) + " *"
+
+ def dump(self, instance):
+ print ' if(%s) {' % instance
+ print ' Log::BeginReference("%s", %s);' % (self.type, instance)
+ try:
+ self.type.dump("*" + instance)
+ except NotImplementedError:
+ pass
+ print ' Log::EndReference();'
+ print ' }'
+ print ' else'
+ print ' Log::Text("NULL");'
def wrap_instance(self, instance):
self.type.wrap_instance("*" + instance)
+ def unwrap_instance(self, instance):
+ self.type.wrap_instance("*" + instance)
-class Output(Type):
- def __init__(self, type):
- Type.__init__(self, type.name)
- self.type = type
+def ConstPointer(type):
+ return Pointer(Const(type))
- def __str__(self):
- return str(self.type)
+
+class OutPointer(Pointer):
def isoutput(self):
return True
- def wrap_instance(self, instance):
- self.type.wrap_instance(instance)
-
-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):
+ print ' switch(%s) {' % instance
+ for value in self.values:
+ print ' case %s:' % value
+ print ' Log::Text("%s");' % value
+ print ' break;'
+ print ' default:'
+ print ' Log::TextF("%%i", %s);' % instance
+ print ' break;'
+ print ' }'
+
+
+class FakeEnum(Enum):
+
+ __seq = 0
+
+ def __init__(self, type, values):
+ FakeEnum.__seq += 1
+ Enum.__init__(self, type.name + str(FakeEnum.__seq), values)
+ self.type = type
+
+ def __str__(self):
+ return str(self.type)
-class Flags(Type):
+class Flags(Concrete):
+ __seq = 0
+
def __init__(self, type, values):
- Type.__init__(self, type.name)
+ Flags.__seq += 1
+ Concrete.__init__(self, type.name + str(Flags.__seq))
+ self.type = type
self.values = values
+ def __str__(self):
+ return str(self.type)
+
+ def _dump(self, instance):
+ print ' %s l_Value = %s;' % (self.type, instance)
+ for value in self.values:
+ print ' if((l_Value & %s) == %s) {' % (value, value)
+ print ' Log::Text("%s | ");' % value
+ print ' l_Value &= ~%s;' % value
+ print ' }'
+ self.type.dump("l_Value");
+
-class Struct(Type):
+class Struct(Concrete):
def __init__(self, name, members):
- Type.__init__(self, name)
+ Concrete.__init__(self, name)
self.members = members
+ def _dump(self, instance):
+ for type, name in self.members:
+ print ' Log::BeginElement("%s", "%s");' % (type, name)
+ type.dump('(%s).%s' % (instance, name))
+ print ' Log::EndElement();'
+
class Alias(Type):
Type.__init__(self, name)
self.type = type
+ def dump(self, instance):
+ self.type.dump(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:
for method in self.itermethods():
print " " + method.prototype() + ";"
print
- print "private:"
- print " Log *m_pLog;"
+ #print "private:"
print " %s * m_pInstance;" % (self.name,)
print "};"
print
else:
print ' %s result;' % method.type
result = 'result = '
- print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name)
+ print ' Log::BeginCall("%s");' % (self.name + '::' + method.name)
+ print ' Log::BeginArg("%s *", "this");' % self.name
+ print ' Log::BeginReference("%s", m_pInstance);' % self.name
+ print ' Log::EndReference();'
+ print ' Log::EndArg();'
+ for type, name in method.args:
+ if not type.isoutput():
+ type.unwrap_instance(name)
+ print ' Log::BeginArg("%s", "%s");' % (type, name)
+ type.dump(name)
+ print ' Log::EndArg();'
print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args]))
- print ' g_pLog->EndCall();'
for type, name in method.args:
if type.isoutput():
+ print ' Log::BeginArg("%s", "%s");' % (type, name)
+ type.dump(name)
+ print ' Log::EndArg();'
type.wrap_instance(name)
if method.type is not Void:
+ print ' Log::BeginReturn("%s");' % method.type
+ method.type.dump("result")
+ print ' Log::EndReturn();'
method.type.wrap_instance('result')
+ print ' Log::EndCall();'
if method.name == 'QueryInterface':
print ' if(*ppvObj == m_pInstance)'
print ' *ppvObj = this;'
print " if(%s)" % instance
print " %s = new %s(%s);" % (instance, self.type.wrap_name(), instance)
-String = Intrinsic("char *", "%s")
+ def unwrap_instance(self, instance):
+ print " if(%s)" % instance
+ print " %s = static_cast<%s *>(%s)->m_pInstance;" % (instance, self.type.wrap_name(), instance)
+
+
+class _String(Type):
+
+ def __init__(self):
+ Type.__init__(self, "String")
+
+ def __str__(self):
+ return "char *"
+
+ def dump(self, instance):
+ print ' Log::DumpString((const char *)%s);' % instance
+
+String = _String()
+
+
+SChar = Intrinsic("signed char", "%i")
+UChar = Intrinsic("unsigned char", "%u")
+Short = Intrinsic("short", "%i")
Int = Intrinsic("int", "%i")
Long = Intrinsic("long", "%li")
+UShort = Intrinsic("unsigned short", "%u")
+UInt = Intrinsic("unsigned int", "%u")
+ULong = Intrinsic("unsigned long", "%lu")
Float = Intrinsic("float", "%f")
+Double = Intrinsic("double", "%f")
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