Commit ab16c35f authored by Jack Jansen's avatar Jack Jansen

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.
parent 8ceeaba0
......@@ -38,23 +38,25 @@ class FixedInputOutputBufferType(InputOnlyType):
self.sizeformat = sizeformat or type2format[sizetype]
self.label_needed = 0
def declare(self, name):
self.declareBuffer(name)
self.declareSize(name)
def getDeclarations(self, name, reference=False):
if reference:
raise RuntimeError, "Cannot pass buffer types by reference"
return self.getBufferDeclarations(name) + self.getSizeDeclarations(name)
def declareBuffer(self, name):
self.declareInputBuffer(name)
self.declareOutputBuffer(name)
def getBufferDeclarations(self, name):
return self.getInputBufferDeclarations(name) + self.getOutputBufferDeclarations(name)
def declareInputBuffer(self, name):
Output("%s *%s__in__;", self.datatype, name)
def getInputBufferDeclarations(self, name):
return ["%s *%s__in__" % (self.datatype, name)]
def declareOutputBuffer(self, name):
Output("%s %s__out__[%s];", self.datatype, name, self.size)
def getOutputBufferDeclarations(self, name):
return ["%s %s__out__[%s]" % (self.datatype, name, self.size)]
def declareSize(self, name):
Output("%s %s__len__;", self.sizetype, name)
Output("int %s__in_len__;", name)
def getSizeDeclarations(self, name):
return [
"%s %s__len__" %(self.sizetype, name),
"int %s__in_len__" %(name)
]
def getargsFormat(self):
return "s#"
......@@ -102,14 +104,14 @@ class FixedCombinedInputOutputBufferType(FixedInputOutputBufferType):
class InputOnlyBufferMixIn(InputOnlyMixIn):
def declareOutputBuffer(self, name):
pass
def getOutputBufferDeclarations(self, name):
return []
class OutputOnlyBufferMixIn(OutputOnlyMixIn):
def declareInputBuffer(self, name):
pass
def getInputBufferDeclarations(self, name):
return []
class OptionalInputBufferMixIn:
......@@ -183,14 +185,14 @@ class StructInputOutputBufferType(FixedInputOutputBufferType):
FixedInputOutputBufferType.__init__(self, "sizeof(%s)" % type)
self.typeName = self.type = type
def declareInputBuffer(self, name):
Output("%s *%s__in__;", self.type, name)
def getInputBufferDeclarations(self, name):
return ["%s *%s__in__" % (self.type, name)]
def declareSize(self, name):
Output("int %s__in_len__;", name)
def getSizeDeclarations(self, name):
return ["int %s__in_len__" % (name)]
def declareOutputBuffer(self, name):
Output("%s %s__out__;", self.type, name)
def getOutputBufferDeclarations(self, name):
return ["%s %s__out__" % (self.type, name)]
def getargsArgs(self, 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.
"""
def declareSize(self, name):
pass
def getSizeDeclarations(self, name):
return []
def passOutput(self, name):
return "&%s__out__" % name
......@@ -257,8 +259,8 @@ class ArrayOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType):
Instantiate with the struct type as parameter.
"""
def declareSize(self, name):
pass
def getSizeDeclarations(self, name):
return []
def passOutput(self, name):
return "%s__out__" % name
......@@ -16,8 +16,8 @@ class HeapInputOutputBufferType(FixedInputOutputBufferType):
def __init__(self, datatype = 'char', sizetype = 'int', sizeformat = None):
FixedInputOutputBufferType.__init__(self, "0", datatype, sizetype, sizeformat)
def declareOutputBuffer(self, name):
Output("%s *%s__out__;", self.datatype, name)
def getOutputBufferDeclarations(self, name):
return ["%s *%s__out__" % (self.datatype, name)]
def getargsCheck(self, 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.
"""
def declareInputBuffer(self, name):
pass
def getInputBufferDeclarations(self, name):
return []
def getargsFormat(self):
return "i"
......
......@@ -22,8 +22,8 @@ class VarStackOutputBufferType(StackOutputBufferType):
Instantiate with the buffer size as parameter.
"""
def declareSize(self, name):
Output("int %s__len__ = %s;", name, self.size)
def getSizeDeclarations(self, name):
return ["int %s__len__ = %s" % (name, self.size)]
def passOutput(self, name):
return "%s__out__, &%s__len__" % (name, name)
......
......@@ -23,8 +23,8 @@ class StringBufferMixIn:
less common. I'll write the classes when there is demand.)
"""
def declareSize(self, name):
pass
def getSizeDeclarations(self, name):
return []
def getargsFormat(self):
return "s"
......
......@@ -24,15 +24,16 @@ class Type:
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
any syntactic adornment"""
if reference:
return "%s& %s" % (self.typeName, name)
return ["%s& %s" % (self.typeName, name)]
else:
return "%s %s" % (self.typeName, name)
return ["%s %s" % (self.typeName, name)]
def getargs(self):
return self.getargsFormat(), self.getargsArgs()
......@@ -185,9 +186,9 @@ class FakeType(InputOnlyType):
def __init__(self, substitute):
self.substitute = substitute
self.typeName = None # Don't show this argument in __doc__ string
def declare(self, name, reference=False):
pass
def getDeclarations(self, name, reference=False):
return []
def getargsFormat(self):
return ""
......
......@@ -45,11 +45,11 @@ class Variable:
elif self.flags != SelfMode:
self.type.declare(self.name)
def getDeclaration(self):
def getDeclarations(self):
"""Return the unadorned declaration of the variable,
suitable for use in a formal parameter list."""
refmode = (self.flags & RefMode)
return self.type.getDeclaration(self.name, reference=refmode)
return self.type.getDeclarations(self.name, reference=refmode)
def getargsFormat(self):
"""Call the type's getargsFormatmethod."""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment