Revamped type declaration so the basic routines return a list of strings.

This allows variables to be declared as formal arguments. The bgenType.declare
method now simply outputs all declarations on separate lines ending
in semicolons.
This commit is contained in:
Jack Jansen 2005-06-22 20:35:23 +00:00
parent 8ceeaba012
commit ab16c35f46
6 changed files with 47 additions and 44 deletions

View file

@ -38,23 +38,25 @@ class FixedInputOutputBufferType(InputOnlyType):
self.sizeformat = sizeformat or type2format[sizetype] self.sizeformat = sizeformat or type2format[sizetype]
self.label_needed = 0 self.label_needed = 0
def declare(self, name): def getDeclarations(self, name, reference=False):
self.declareBuffer(name) if reference:
self.declareSize(name) raise RuntimeError, "Cannot pass buffer types by reference"
return self.getBufferDeclarations(name) + self.getSizeDeclarations(name)
def declareBuffer(self, name): def getBufferDeclarations(self, name):
self.declareInputBuffer(name) return self.getInputBufferDeclarations(name) + self.getOutputBufferDeclarations(name)
self.declareOutputBuffer(name)
def declareInputBuffer(self, name): def getInputBufferDeclarations(self, name):
Output("%s *%s__in__;", self.datatype, name) return ["%s *%s__in__" % (self.datatype, name)]
def declareOutputBuffer(self, name): def getOutputBufferDeclarations(self, name):
Output("%s %s__out__[%s];", self.datatype, name, self.size) return ["%s %s__out__[%s]" % (self.datatype, name, self.size)]
def declareSize(self, name): def getSizeDeclarations(self, name):
Output("%s %s__len__;", self.sizetype, name) return [
Output("int %s__in_len__;", name) "%s %s__len__" %(self.sizetype, name),
"int %s__in_len__" %(name)
]
def getargsFormat(self): def getargsFormat(self):
return "s#" return "s#"
@ -102,14 +104,14 @@ class FixedCombinedInputOutputBufferType(FixedInputOutputBufferType):
class InputOnlyBufferMixIn(InputOnlyMixIn): class InputOnlyBufferMixIn(InputOnlyMixIn):
def declareOutputBuffer(self, name): def getOutputBufferDeclarations(self, name):
pass return []
class OutputOnlyBufferMixIn(OutputOnlyMixIn): class OutputOnlyBufferMixIn(OutputOnlyMixIn):
def declareInputBuffer(self, name): def getInputBufferDeclarations(self, name):
pass return []
class OptionalInputBufferMixIn: class OptionalInputBufferMixIn:
@ -183,14 +185,14 @@ class StructInputOutputBufferType(FixedInputOutputBufferType):
FixedInputOutputBufferType.__init__(self, "sizeof(%s)" % type) FixedInputOutputBufferType.__init__(self, "sizeof(%s)" % type)
self.typeName = self.type = type self.typeName = self.type = type
def declareInputBuffer(self, name): def getInputBufferDeclarations(self, name):
Output("%s *%s__in__;", self.type, name) return ["%s *%s__in__" % (self.type, name)]
def declareSize(self, name): def getSizeDeclarations(self, name):
Output("int %s__in_len__;", name) return ["int %s__in_len__" % (name)]
def declareOutputBuffer(self, name): def getOutputBufferDeclarations(self, name):
Output("%s %s__out__;", self.type, name) return ["%s %s__out__" % (self.type, name)]
def getargsArgs(self, name): def getargsArgs(self, name):
return "(char **)&%s__in__, &%s__in_len__" % (name, name) return "(char **)&%s__in__, &%s__in_len__" % (name, name)
@ -243,8 +245,8 @@ class StructOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType)
Instantiate with the struct type as parameter. Instantiate with the struct type as parameter.
""" """
def declareSize(self, name): def getSizeDeclarations(self, name):
pass return []
def passOutput(self, name): def passOutput(self, name):
return "&%s__out__" % name return "&%s__out__" % name
@ -257,8 +259,8 @@ class ArrayOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType):
Instantiate with the struct type as parameter. Instantiate with the struct type as parameter.
""" """
def declareSize(self, name): def getSizeDeclarations(self, name):
pass return []
def passOutput(self, name): def passOutput(self, name):
return "%s__out__" % name return "%s__out__" % name

View file

@ -16,8 +16,8 @@ class HeapInputOutputBufferType(FixedInputOutputBufferType):
def __init__(self, datatype = 'char', sizetype = 'int', sizeformat = None): def __init__(self, datatype = 'char', sizetype = 'int', sizeformat = None):
FixedInputOutputBufferType.__init__(self, "0", datatype, sizetype, sizeformat) FixedInputOutputBufferType.__init__(self, "0", datatype, sizetype, sizeformat)
def declareOutputBuffer(self, name): def getOutputBufferDeclarations(self, name):
Output("%s *%s__out__;", self.datatype, name) return ["%s *%s__out__" % (self.datatype, name)]
def getargsCheck(self, name): def getargsCheck(self, name):
Output("if ((%s__out__ = malloc(%s__in_len__)) == NULL)", name, name) Output("if ((%s__out__ = malloc(%s__in_len__)) == NULL)", name, name)
@ -74,8 +74,8 @@ class HeapOutputBufferType(OutputOnlyMixIn, HeapInputOutputBufferType):
Call from Python with buffer size. Call from Python with buffer size.
""" """
def declareInputBuffer(self, name): def getInputBufferDeclarations(self, name):
pass return []
def getargsFormat(self): def getargsFormat(self):
return "i" return "i"

View file

@ -22,8 +22,8 @@ class VarStackOutputBufferType(StackOutputBufferType):
Instantiate with the buffer size as parameter. Instantiate with the buffer size as parameter.
""" """
def declareSize(self, name): def getSizeDeclarations(self, name):
Output("int %s__len__ = %s;", name, self.size) return ["int %s__len__ = %s" % (name, self.size)]
def passOutput(self, name): def passOutput(self, name):
return "%s__out__, &%s__len__" % (name, name) return "%s__out__, &%s__len__" % (name, name)

View file

@ -23,8 +23,8 @@ class StringBufferMixIn:
less common. I'll write the classes when there is demand.) less common. I'll write the classes when there is demand.)
""" """
def declareSize(self, name): def getSizeDeclarations(self, name):
pass return []
def getargsFormat(self): def getargsFormat(self):
return "s" return "s"

View file

@ -24,15 +24,16 @@ class Type:
Example: int.declare('spam') prints "int spam;" Example: int.declare('spam') prints "int spam;"
""" """
Output("%s;", self.getDeclaration(name, reference)) for decl in self.getDeclarations(name, reference):
Output("%s;", decl)
def getDeclaration(self, name, reference=False): def getDeclarations(self, name, reference=False):
"""Return a string declaring a variable or argument, without """Return a string declaring a variable or argument, without
any syntactic adornment""" any syntactic adornment"""
if reference: if reference:
return "%s& %s" % (self.typeName, name) return ["%s& %s" % (self.typeName, name)]
else: else:
return "%s %s" % (self.typeName, name) return ["%s %s" % (self.typeName, name)]
def getargs(self): def getargs(self):
return self.getargsFormat(), self.getargsArgs() return self.getargsFormat(), self.getargsArgs()
@ -185,9 +186,9 @@ class FakeType(InputOnlyType):
def __init__(self, substitute): def __init__(self, substitute):
self.substitute = substitute self.substitute = substitute
self.typeName = None # Don't show this argument in __doc__ string self.typeName = None # Don't show this argument in __doc__ string
def declare(self, name, reference=False): def getDeclarations(self, name, reference=False):
pass return []
def getargsFormat(self): def getargsFormat(self):
return "" return ""

View file

@ -45,11 +45,11 @@ class Variable:
elif self.flags != SelfMode: elif self.flags != SelfMode:
self.type.declare(self.name) self.type.declare(self.name)
def getDeclaration(self): def getDeclarations(self):
"""Return the unadorned declaration of the variable, """Return the unadorned declaration of the variable,
suitable for use in a formal parameter list.""" suitable for use in a formal parameter list."""
refmode = (self.flags & RefMode) refmode = (self.flags & RefMode)
return self.type.getDeclaration(self.name, reference=refmode) return self.type.getDeclarations(self.name, reference=refmode)
def getargsFormat(self): def getargsFormat(self):
"""Call the type's getargsFormatmethod.""" """Call the type's getargsFormatmethod."""