1"""Variables, arguments and argument transfer modes etc.""" 2 3 4# Values to represent argument transfer modes 5InMode = 1 # input-only argument 6OutMode = 2 # output-only argument 7InOutMode = 3 # input-output argument 8ModeMask = 3 # bits to keep for mode 9 10 11# Special cases for mode/flags argument 12# XXX This is still a mess! 13SelfMode = 4+InMode # this is 'self' -- don't declare it 14ReturnMode = 8+OutMode # this is the function return value 15ErrorMode = 16+OutMode # this is an error status -- turn it into an exception 16RefMode = 32 17ConstMode = 64 18 19class Variable: 20 21 """A Variable holds a type, a name, a transfer mode and flags. 22 23 Most of its methods call the corresponding type method with the 24 variable name. 25 """ 26 27 def __init__(self, type, name = None, flags = InMode): 28 """Call with a type, a name and flags. 29 30 If name is None, it muse be set later. 31 flags defaults to InMode. 32 """ 33 self.type = type 34 self.name = name 35 self.flags = flags 36 self.mode = flags & ModeMask 37 38 def declare(self): 39 """Declare the variable if necessary. 40 41 If it is "self", it is not declared. 42 """ 43 if self.flags == ReturnMode+RefMode: 44 self.type.declare(self.name, reference=True) 45 elif self.flags != SelfMode: 46 self.type.declare(self.name) 47 48 def getArgDeclarations(self, fullmodes=False): 49 refmode = (self.flags & RefMode) 50 constmode = False 51 outmode = False 52 if fullmodes: 53 constmode = (self.flags & ConstMode) 54 outmode = (self.flags & OutMode) 55 return self.type.getArgDeclarations(self.name, 56 reference=refmode, constmode=constmode, outmode=outmode) 57 58 def getAuxDeclarations(self): 59 return self.type.getAuxDeclarations(self.name) 60 61 def getargsFormat(self): 62 """Call the type's getargsFormatmethod.""" 63 return self.type.getargsFormat() 64 65 def getargsArgs(self): 66 """Call the type's getargsArgsmethod.""" 67 return self.type.getargsArgs(self.name) 68 69 def getargsCheck(self): 70 return self.type.getargsCheck(self.name) 71 72 def getargsPreCheck(self): 73 return self.type.getargsPreCheck(self.name) 74 75 def passArgument(self): 76 """Return the string required to pass the variable as argument. 77 78 For "in" arguments, return the variable name. 79 For "out" and "in out" arguments, 80 return its name prefixed with "&". 81 """ 82 if self.mode == InMode: 83 return self.type.passInput(self.name) 84 if self.mode & RefMode: 85 return self.type.passReference(self.name) 86 if self.mode in (OutMode, InOutMode): 87 return self.type.passOutput(self.name) 88 # XXX Shouldn't get here 89 return "/*mode?*/" + self.type.passInput(self.name) 90 91 def errorCheck(self): 92 """Check for an error if necessary. 93 94 This only generates code if the variable's mode is ErrorMode. 95 """ 96 if self.flags == ErrorMode: 97 self.type.errorCheck(self.name) 98 99 def mkvalueFormat (self): 100 """Call the type's mkvalueFormat method.""" 101 return self.type.mkvalueFormat() 102 103 def mkvalueArgs(self): 104 """Call the type's mkvalueArgs method.""" 105 return self.type.mkvalueArgs(self.name) 106 107 def mkvaluePreCheck(self): 108 return self.type.mkvaluePreCheck(self.name) 109 110 def cleanup(self): 111 """Call the type's cleanup method.""" 112 return self.type.cleanup(self.name) 113