More factorization to help C++ support.

This commit is contained in:
Jack Jansen 2005-06-28 15:14:35 +00:00
parent 02c64d5684
commit b6216dd2e7
6 changed files with 51 additions and 18 deletions

View File

@ -38,10 +38,11 @@ class FixedInputOutputBufferType(InputOnlyType):
self.sizeformat = sizeformat or type2format[sizetype] self.sizeformat = sizeformat or type2format[sizetype]
self.label_needed = 0 self.label_needed = 0
def getDeclarations(self, name, reference=False): def getArgDeclarations(self, name, reference=False):
if reference: if reference:
raise RuntimeError, "Cannot pass buffer types by reference" raise RuntimeError, "Cannot pass buffer types by reference"
return self.getBufferDeclarations(name) + self.getSizeDeclarations(name) return (self.getBufferDeclarations(name) +
self.getSizeDeclarations(name))
def getBufferDeclarations(self, name): def getBufferDeclarations(self, name):
return self.getInputBufferDeclarations(name) + self.getOutputBufferDeclarations(name) return self.getInputBufferDeclarations(name) + self.getOutputBufferDeclarations(name)
@ -53,10 +54,10 @@ class FixedInputOutputBufferType(InputOnlyType):
return ["%s %s__out__[%s]" % (self.datatype, name, self.size)] return ["%s %s__out__[%s]" % (self.datatype, name, self.size)]
def getSizeDeclarations(self, name): def getSizeDeclarations(self, name):
return [ return ["%s %s__len__" %(self.sizetype, name)]
"%s %s__len__" %(self.sizetype, name),
"int %s__in_len__" %(name) def getAuxDeclarations(self, name):
] return ["int %s__in_len__" %(name)]
def getargsFormat(self): def getargsFormat(self):
return "s#" return "s#"
@ -189,6 +190,9 @@ class StructInputOutputBufferType(FixedInputOutputBufferType):
return ["%s *%s__in__" % (self.type, name)] return ["%s *%s__in__" % (self.type, name)]
def getSizeDeclarations(self, name): def getSizeDeclarations(self, name):
return []
def getAuxDeclarations(self, name):
return ["int %s__in_len__" % (name)] return ["int %s__in_len__" % (name)]
def getOutputBufferDeclarations(self, name): def getOutputBufferDeclarations(self, name):
@ -248,6 +252,9 @@ class StructOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType)
def getSizeDeclarations(self, name): def getSizeDeclarations(self, name):
return [] return []
def getAuxDeclarations(self, name):
return []
def passOutput(self, name): def passOutput(self, name):
return "&%s__out__" % name return "&%s__out__" % name
@ -262,5 +269,8 @@ class ArrayOutputBufferType(OutputOnlyBufferMixIn, StructInputOutputBufferType):
def getSizeDeclarations(self, name): def getSizeDeclarations(self, name):
return [] return []
def getAuxDeclarations(self, name):
return []
def passOutput(self, name): def passOutput(self, name):
return "%s__out__" % name return "%s__out__" % name

View File

@ -23,6 +23,9 @@ class VarStackOutputBufferType(StackOutputBufferType):
""" """
def getSizeDeclarations(self, name): def getSizeDeclarations(self, name):
return []
def getAuxDeclarations(self, name):
return ["int %s__len__ = %s" % (name, self.size)] return ["int %s__len__ = %s" % (name, self.size)]
def passOutput(self, name): def passOutput(self, name):

View File

@ -26,6 +26,9 @@ class StringBufferMixIn:
def getSizeDeclarations(self, name): def getSizeDeclarations(self, name):
return [] return []
def getAuxDeclarations(self, name):
return []
def getargsFormat(self): def getargsFormat(self):
return "s" return "s"

View File

@ -24,17 +24,25 @@ class Type:
Example: int.declare('spam') prints "int spam;" Example: int.declare('spam') prints "int spam;"
""" """
for decl in self.getDeclarations(name, reference): for decl in self.getArgDeclarations(name, reference):
Output("%s;", decl)
for decl in self.getAuxDeclarations(name):
Output("%s;", decl) Output("%s;", decl)
def getDeclarations(self, name, reference=False): def getArgDeclarations(self, name, reference=False):
"""Return a string declaring a variable or argument, without """Return the main part of the declarations for this type: the items
any syntactic adornment""" that will be passed as arguments in the C/C++ function call."""
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 getAuxDeclarations(self, name):
"""Return any auxiliary declarations needed for implementing this
type, such as helper variables used to hold sizes, etc. These declarations
are not part of the C/C++ function call interface."""
return []
def getargs(self): def getargs(self):
return self.getargsFormat(), self.getargsArgs() return self.getargsFormat(), self.getargsArgs()
@ -187,7 +195,10 @@ class FakeType(InputOnlyType):
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 getDeclarations(self, name, reference=False): def getArgDeclarations(self, name, reference=False):
return []
def getAuxDeclarations(self, name, reference=False):
return [] return []
def getargsFormat(self): def getargsFormat(self):

View File

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

View File

@ -482,8 +482,7 @@ if missing: raise "Missing Types"
modifiers = self.getmodifiers(match) modifiers = self.getmodifiers(match)
type = self.pythonizename(type) type = self.pythonizename(type)
name = self.pythonizename(name) name = self.pythonizename(name)
if name in self.alreadydone: if self.checkduplicate(name):
self.report("Name has already been defined: %r", name)
return return
self.report("==> %s %s <==", type, name) self.report("==> %s %s <==", type, name)
if self.blacklisted(type, name): if self.blacklisted(type, name):
@ -499,7 +498,6 @@ if missing: raise "Missing Types"
## self.report(" %r", arg) ## self.report(" %r", arg)
self.report("*** %s %s unmanageable", type, name) self.report("*** %s %s unmanageable", type, name)
return return
self.alreadydone.append(name)
if modifiers: if modifiers:
self.generate(type, name, arglist, modifiers) self.generate(type, name, arglist, modifiers)
else: else:
@ -508,6 +506,13 @@ if missing: raise "Missing Types"
def getmodifiers(self, match): def getmodifiers(self, match):
return [] return []
def checkduplicate(self, name):
if name in self.alreadydone:
self.report("Name has already been defined: %r", name)
return True
self.alreadydone.append(name)
return False
def pythonizename(self, name): def pythonizename(self, name):
name = re.sub("\*", " ptr", name) name = re.sub("\*", " ptr", name)
name = name.strip() name = name.strip()