def typeConstPtrCode(pType): const = pType.strip() deRef = '' if const.find('*') is -1: deRef = '*' # Special case for char ** if reCharPtrPtr.match(const): const = 'const cgiArgs *' deRef = '' indConst = const.find('const') indPtr = const.find('*') if indConst is -1 or (indPtr is not -1 and indPtr < indConst): const = 'const ' + const # Convert any '**' to '* const *'. while rePtrPtr.search(const): const = rePtrPtr.sub('* const *', const) const = typeCode(const) return const, deRef
def apiDispatchFuncInitCode(apis, args, dispatchName, exclude=[], filter = lambda x : True): categoryPrev = None code = '' for api in apis: code += '\n' if api.name in cond: code += '#if %s\n' % cond[api.name] for function in api.functions: if not function.needsContext: continue if not filter(function): continue if getattr(function,'regalOnly',False)==True: continue if function.name in exclude or function.category in exclude: continue name = function.name params = paramsDefaultCode(function.parameters, True) callParams = paramsNameCode(function.parameters) rType = typeCode(function.ret.type) category = getattr(function, 'category', None) version = getattr(function, 'version', None) if category: category = category.replace('_DEPRECATED', '') elif version: category = version.replace('.', '_') category = 'GL_VERSION_' + category # Close prev category block. if categoryPrev and not (category == categoryPrev): code += '\n' # Begin new category block. if category and not (category == categoryPrev): code += ' // %s\n\n' % category categoryPrev = category code += ' tbl.%s = %s_%s;\n' % ( name, dispatchName, name ) if api.name in cond: code += '#endif // %s\n' % cond[api.name] code += '\n' # Close pending if block. if categoryPrev: code += '\n' return code
def paramStateUpdateCode(parameter, stateType, key, value, pSize='', stateWarn=''): # Check if parameter's state type has a state attribute. state = getattr(stateType, 'state', None) if not state: print 'parameter has no state attribute for state update' return [] # Obtain core type for state. pType = typeCode(parameter.type) sType = pType.replace('const', '').replace(' ', ' ').strip(' *') default = getattr(stateType, 'default', '0') lines = [] if stateType.dim == 0: # Scalar case. updateCode = 'update<%s>(%s, %s, %s, %s)' % (sType, state, key, value, default) if len(stateWarn): lines.append('if (!%s)' % updateCode) lines.append(' %s' % stateWarn) else: lines.append('%s;' % updateCode) elif stateType.dim == 1 and len(pSize): # 1D array case. updateCode = 'update<%s>(%s, %s[_i], %s[_i], %s)' % (sType, state, key, value, default) lines.append('if (%s && %s)' % (key, value)) lines.append('{') lines.append(' for (size_t _i = 0; _i < size_t(%s); ++_i)' % pSize) if len(stateWarn): lines.append(' {') lines.append(' if (!%s)' % updateCode) lines.append(' %s' % stateWarn) lines.append(' }') else: lines.append(' %s;' % updateCode) lines.append('}') else: # Arrays with dim > 1 or with no size expression are not supported. print 'unsupported type for state update' return lines
def paramStateUpdateCode(parameter, stateType, key, value, pSize = '', stateWarn = ''): # Check if parameter's state type has a state attribute. state = getattr(stateType, 'state', None) if not state: print 'parameter has no state attribute for state update' return [] # Obtain core type for state. pType = typeCode(parameter.type) sType = pType.replace('const', '').replace(' ', ' ').strip(' *') default = getattr(stateType, 'default', '0') lines = [] if stateType.dim == 0: # Scalar case. updateCode = 'update<%s>(%s, %s, %s, %s)' % (sType, state, key, value, default) if len(stateWarn): lines.append('if (!%s)' % updateCode) lines.append(' %s' % stateWarn) else: lines.append('%s;' % updateCode) elif stateType.dim == 1 and len(pSize): # 1D array case. updateCode = 'update<%s>(%s, %s[_i], %s[_i], %s)' % (sType, state, key, value, default) lines.append('if (%s && %s)' % (key, value)) lines.append('{') lines.append(' for (size_t _i = 0; _i < size_t(%s); ++_i)' % pSize) if len(stateWarn): lines.append(' {') lines.append(' if (!%s)' % updateCode) lines.append(' %s' % stateWarn) lines.append(' }') else: lines.append(' %s;' % updateCode) lines.append('}') else: # Arrays with dim > 1 or with no size expression are not supported. print 'unsupported type for state update' return lines
def generateGMockFunctionApi(apis): for api in apis: if api.name != 'gl': continue for function in api.functions: if not function.needsContext: continue if getattr(function, 'regalOnly', False): continue if not function.category in [ 'GL_VERSION_1_0', 'GL_VERSION_1_1', 'GL_VERSION_2_0'] and \ not function.name in [ ]: continue yield dict( PREFIX="gmock_", NAME=function.name, RTYPE=typeCode(function.ret.type).strip(), PARAM_COUNT=len(function.parameters), PARAM_TYPES=paramsTypeCode(function.parameters, True), PARAM_NAMES=paramsNameCode(function.parameters), PARAM_TYPES_NAMES=paramsDefaultCode(function.parameters, True))
def generateGMockFunctionApi(apis): for api in apis: if api.name != 'gl': continue for function in api.functions: if not function.needsContext: continue if getattr(function, 'regalOnly', False): continue if (function.category not in functionCategoriesToMock and function.name not in explicitFunctionsToMock): continue yield dict( PREFIX="gmock_", NAME=function.name, RTYPE=typeCode(function.ret.type).strip(), PARAM_COUNT=len(function.parameters), PARAM_TYPES=paramsTypeCode(function.parameters, True), PARAM_NAMES=paramsNameCode(function.parameters), PARAM_TYPES_NAMES=paramsDefaultCode(function.parameters, True))
def paramDeclCode(parameter, types=typesBasic, size=None, name=None): pType = typeCode(parameter.type) aType = findType(pType, types) # Check max size for array. maxSize = parameter.maxSize if not maxSize: maxSize = size # Check for an integer size for array. intSize = None try: intSize = int(maxSize) except ValueError: pass # If no input <name>, use parameter name. if not name: name = parameter.name.strip() decl = None if aType.dim > 0 and pType[-1] == '*': # Array case. declType = pType[:-1] if intSize: # Use static array for integer sized array. decl = 'static %s%s[%d];' % (declType, name, intSize) else: # Use boost scoped_array for non-integer sized array. decl = 'boost::scoped_array<%s> %s(new %s[%s]);' % ( declType.strip(), name, declType, maxSize) else: # Scalar case. decl = '%s%s;' % (pType, name) return decl
def paramDeclCode(parameter, types = typesBasic, size = None, name = None): pType = typeCode(parameter.type) aType = findType(pType, types) # Check max size for array. maxSize = parameter.maxSize if not maxSize: maxSize = size # Check for an integer size for array. intSize = None try: intSize = int(maxSize) except ValueError: pass # If no input <name>, use parameter name. if not name: name = parameter.name.strip() decl = None if aType.dim > 0 and pType[-1] == '*': # Array case. declType = pType[:-1] if intSize: # Use static array for integer sized array. decl = 'static %s%s[%d];' % (declType, name, intSize) else: # Use boost scoped_array for non-integer sized array. decl = 'boost::scoped_array<%s> %s(new %s[%s]);' % (declType.strip(), name, declType, maxSize) else: # Scalar case. decl = '%s%s;' % (pType, name) return decl
def emuFindEntry(func, emuFormulae, member): if emuFormulae==None: return None name = func.name # list of function parameter names arglist = [ i.name.strip() for i in func.parameters ] # arg is a mapping from arg0 to function parameter name... arg = {} for i in range(len(arglist)): arg['arg%d' % i] = arglist[i] # ... and mappings from arg0plus to lists of function parameters for i in range(0,5): label = 'arg%dplus' % i; if len(arglist) > 0 : arg[label] = ', '.join(arglist) arglist.pop(0) else : arg[label] = '' # Iterator over the formulae # # k is the key # i is the formula for k,i in emuFormulae.iteritems(): # Cache the compiled regular expressions, as needed if 'entries_re' not in i: i['entries_re'] = [ re.compile( '^%s$' % j ) for j in i['entries'] ] # A list of matches containing (match object, formula name, formula) # Look for matches, ideally only one m = [ [j.match(name),k,i] for k,i in emuFormulae.iteritems() for j in i['entries_re'] ] m = [ j for j in m if j[0] ] assert len(m)<=1, 'Ambiguous match (%s) for %s - giving up.'%(', '.join([j[1] for j in m]),name) if len(m): match = m[0][0] formula = m[0][2] rType = typeCode(func.ret.type) dummyRetVal = '' if not typeIsVoid(rType): dummyRetVal = '(( %s )0)' % rType emue = { 'name' : name, 'member' : member, 'dummyretval' : dummyRetVal } subs = deepcopy(arg) for l in range( len(match.groups()) + 1): subs['m%d' % l] = match.group( l ) subs['name'] = name subs['dummyretval'] = dummyRetVal addSubstitution( name, formula, subs ) substitute( emue, formula, 'impl', subs ) substitute( emue, formula, 'init', subs ) substitute( emue, formula, 'cond', subs ) substitute( emue, formula, 'prefix', subs ) substitute( emue, formula, 'suffix', subs ) substitute( emue, formula, 'pre', subs ) substitute( emue, formula, 'post', subs ) emue['cond'] = None if 'cond' in formula: emue['cond'] = formula['cond'] # plugin is optional, default to False emue['plugin'] = False if 'plugin' in formula: emue['plugin'] = formula['plugin'] return emue return None
def apiDispatchFuncInitCode(apis, args, dispatchName, exclude=[], filter = lambda x : True, cond = None): if not cond: cond = condDefault categoryPrev = None code = '' for api in apis: code += '\n' if api.name in cond: code += '#if %s\n' % cond[api.name] for function in api.functions: if not function.needsContext: continue if not filter(function): continue if getattr(function,'regalOnly',False)==True: continue if function.name in exclude or function.category in exclude: continue name = function.name params = paramsDefaultCode(function.parameters, True) callParams = paramsNameCode(function.parameters) rType = typeCode(function.ret.type) category = getattr(function, 'category', None) version = getattr(function, 'version', None) if category: category = category.replace('_DEPRECATED', '') elif version: category = version.replace('.', '_') category = 'GL_VERSION_' + category # Close prev category block. if categoryPrev and not (category == categoryPrev): code += '\n' # Begin new category block. if category and not (category == categoryPrev): code += ' // %s\n\n' % category categoryPrev = category if dispatchName!=None: code += ' tbl.%s = %s_%s;\n' % ( name, dispatchName, name ) else: code += ' tbl.%s = %s;\n' % ( name, name ) if api.name in cond: code += '#endif // %s\n' % cond[api.name] code += '\n' # Close pending if block. if categoryPrev: code += '\n' return code
def paramInterceptInputCode(parameter, interceptType, pSize = None, name = None, pDefault = '0', cMode = False): # Find Type using parameter type. pType = typeCode(parameter.type) if not interceptType: return None, [] # Obtain core type for state. sType = pType.replace('const', '').replace(' ', ' ').strip(' *') # Check max size for array. maxSize = parameter.maxSize if not maxSize: maxSize = pSize # Check for an integer size for array. intSize = None try: intSize = int(maxSize) except ValueError: pass # If no input <name>, use parameter name. if not name: name = parameter.name.strip() # Get the intercept value for initialization. interceptAttr = getattr(parameter, 'intercept') # Intercept function of the form Name(Param) interceptFunc = reIntercept.match(interceptAttr) if interceptFunc: interceptName = interceptFunc.group('name') interceptParam = interceptFunc.group('param') interceptHelper = helperFuncCode(interceptAttr) if interceptHelper: interceptValue = interceptHelper elif interceptFunc: pName = name.replace('intercept_', '') if cMode: interceptValue = '%s ? &(cb_%s) : NULL' % (pName, interceptName) else: interceptValue = '%s ? &(::cb_%s) : NULL' % (pName, interceptName) else: interceptValue = interceptAttr interceptDecl = '' interceptFree = '' interceptAssign = [] interceptUpdate = [] if interceptType.dim == 0: # Scalar case. if cMode: # Intercept declare (C). if pDefault: interceptDecl = '%s%s = %s;' % (pType, name, pDefault) else: interceptDecl = '%s%s;' % (pType, name) # Intercept assign (C) if interceptValue: interceptAssign.append('%s = %s;' % (name, interceptValue)) else: # Intercept delcare and assign (C++). if interceptValue: interceptDecl = '%s%s = %s;' % (pType, name, interceptValue) else: interceptDecl = '%s%s;' % (pType, name) # Intercept update. if not interceptHelper and interceptFunc: pName = name.replace('intercept_', '') if cMode: interceptUpdate.append('update%s(&_state.%sMap, %s, %s);' % (sType, interceptName, interceptParam, pName)) else: interceptUpdate.append('update%s(_state.%sMap, %s, %s);' % (sType, interceptName, interceptParam, pName)) elif interceptType.dim == 1 and pType[:-1] == '*' and pSize: # 1D array case. declType = pType[:-1] if intSize: # Use static array for integer sized array. if cMode: # Intercept declare (C). # TODO: Add initialization? interceptDecl = 'static %s%s[%d];' % (declType, name, intSize) # Intercept assign (C). if interceptValue: interceptAssign.append('%s = %s;' % (name, interceptValue)) else: # Intercept declare and assign (C++). if interceptValue: interceptDecl = 'static %s%s[%d] = %s;' % (declType, name, intSize, interceptValue) else: interceptDecl = 'static %s%s[%d];' % (declType, name, intSize) else: if cMode: # Intercept declare (C). if pDefault: interceptDecl = '%s*%s = %s;' % (declType, name, pDefault) else: interceptDecl = '%s*%s;' % (declType, name) # Intercept assign (C). # TODO: Assign arrays in C. # Intercept free (C). # TODO: Free arrays in C. else: # Use boot scoped_array for non-integer sized array (C++). if interceptValue: interceptDecl = 'boost::scoped_array<%s> %s(%s);' % (declType.strip(), name, interceptValue) else: interceptDecl = 'boost::scoped_array<%s> %s(new %s[%s]);' % (declType.strip(), name, declType, maxSize) # Intercept update. # TODO: Not supported for now. return interceptDecl, interceptFree, interceptAssign, interceptUpdate
def paramStateMapCode(parameter, stateType, key, value, pSize = '', stateWarn = ''): lookup = getattr(parameter, 'lookup', None) state = getattr(stateType, 'state', None) # Obtain core type for state. pType = typeCode(parameter.type) sType = pType.replace('const', '').replace(' ', ' ').strip(' *') default = getattr(stateType, 'default', '0') lines = [] if stateType.dim == 0: # Scalar case. if lookup: # Use lookup attribute for parameter. lookupCode = lookup lookupHelper = helperFuncCode(lookupCode) if lookupHelper: lookupCode = lookupHelper lines.append('%s%s = %s;' % (pType, value, lookupCode)) else: # Use state attribute from parameter's state type. lines.append('%s%s = lookup<%s>(%s, %s, %s);' % (pType, value, sType, state, key, default)) # Check if valid state value. lines.append('if (%s && !%s)' % (key, value)) lines.append('{') if len(stateWarn): lines.append(' %s' % stateWarn) lines.append(' return false;') lines.append('}') elif stateType.dim == 1 and len(pSize): # 1D array case. valueDecl = paramDeclCode(parameter, [stateType], pSize, value) valueDecl = valueDecl.replace('const ', '') # Declare variable for state values. lines.append('%s' % valueDecl) lines.append('if (!%s)' % key) lines.append(' %s.reset();' % value) lines.append('else') lines.append('{') if lookup: # Use lookup attribute of parameter. lookupCode = lookup lookupHelper = helperFuncCode(lookupCode) if lookupHelper: lookupCode = lookupHelper lines.append(' %s.reset(%s);' % (value, lookupCode)) # Check if valid state values in array. lines.append(' if ((%s) && !%s.get())' % (pSize, value)) lines.append(' {') if len(stateWarn): lines.append(' %s' % stateWarn) lines.append(' return false;') lines.append(' }') else: # Use state attribute from parameter's state type. lines.append(' for (size_t _i = 0; _i < size_t(%s); ++_i)' % pSize) lines.append(' {') # Lookup state value for each array element and check if valid. lines.append(' %s[_i] = lookup<%s>(%s, %s[_i], %s);' % (value, sType, state, key, default)) lines.append(' if (%s[_i] && !%s[_i])' % (key, value)) lines.append(' {') if len(stateWarn): lines.append(' %s' % stateWarn) lines.append(' return false;') lines.append(' }') lines.append(' }') lines.append('}') else: # Arrays with dim > 1 or with no size expression are not supported. print 'unsupported type for state lookup' return lines
def paramInterceptInputCode(parameter, interceptType, pSize=None, name=None, pDefault='0', cMode=False): # Find Type using parameter type. pType = typeCode(parameter.type) if not interceptType: return None, [] # Obtain core type for state. sType = pType.replace('const', '').replace(' ', ' ').strip(' *') # Check max size for array. maxSize = parameter.maxSize if not maxSize: maxSize = pSize # Check for an integer size for array. intSize = None try: intSize = int(maxSize) except ValueError: pass # If no input <name>, use parameter name. if not name: name = parameter.name.strip() # Get the intercept value for initialization. interceptAttr = getattr(parameter, 'intercept') # Intercept function of the form Name(Param) interceptFunc = reIntercept.match(interceptAttr) if interceptFunc: interceptName = interceptFunc.group('name') interceptParam = interceptFunc.group('param') interceptHelper = helperFuncCode(interceptAttr) if interceptHelper: interceptValue = interceptHelper elif interceptFunc: pName = name.replace('intercept_', '') if cMode: interceptValue = '%s ? &(cb_%s) : NULL' % (pName, interceptName) else: interceptValue = '%s ? &(::cb_%s) : NULL' % (pName, interceptName) else: interceptValue = interceptAttr interceptDecl = '' interceptFree = '' interceptAssign = [] interceptUpdate = [] if interceptType.dim == 0: # Scalar case. if cMode: # Intercept declare (C). if pDefault: interceptDecl = '%s%s = %s;' % (pType, name, pDefault) else: interceptDecl = '%s%s;' % (pType, name) # Intercept assign (C) if interceptValue: interceptAssign.append('%s = %s;' % (name, interceptValue)) else: # Intercept delcare and assign (C++). if interceptValue: interceptDecl = '%s%s = %s;' % (pType, name, interceptValue) else: interceptDecl = '%s%s;' % (pType, name) # Intercept update. if not interceptHelper and interceptFunc: pName = name.replace('intercept_', '') if cMode: interceptUpdate.append( 'update%s(&_state.%sMap, %s, %s);' % (sType, interceptName, interceptParam, pName)) else: interceptUpdate.append( 'update%s(_state.%sMap, %s, %s);' % (sType, interceptName, interceptParam, pName)) elif interceptType.dim == 1 and pType[:-1] == '*' and pSize: # 1D array case. declType = pType[:-1] if intSize: # Use static array for integer sized array. if cMode: # Intercept declare (C). # TODO: Add initialization? interceptDecl = 'static %s%s[%d];' % (declType, name, intSize) # Intercept assign (C). if interceptValue: interceptAssign.append('%s = %s;' % (name, interceptValue)) else: # Intercept declare and assign (C++). if interceptValue: interceptDecl = 'static %s%s[%d] = %s;' % ( declType, name, intSize, interceptValue) else: interceptDecl = 'static %s%s[%d];' % (declType, name, intSize) else: if cMode: # Intercept declare (C). if pDefault: interceptDecl = '%s*%s = %s;' % (declType, name, pDefault) else: interceptDecl = '%s*%s;' % (declType, name) # Intercept assign (C). # TODO: Assign arrays in C. # Intercept free (C). # TODO: Free arrays in C. else: # Use boot scoped_array for non-integer sized array (C++). if interceptValue: interceptDecl = 'boost::scoped_array<%s> %s(%s);' % ( declType.strip(), name, interceptValue) else: interceptDecl = 'boost::scoped_array<%s> %s(new %s[%s]);' % ( declType.strip(), name, declType, maxSize) # Intercept update. # TODO: Not supported for now. return interceptDecl, interceptFree, interceptAssign, interceptUpdate
def paramStateMapCode(parameter, stateType, key, value, pSize='', stateWarn=''): lookup = getattr(parameter, 'lookup', None) state = getattr(stateType, 'state', None) # Obtain core type for state. pType = typeCode(parameter.type) sType = pType.replace('const', '').replace(' ', ' ').strip(' *') default = getattr(stateType, 'default', '0') lines = [] if stateType.dim == 0: # Scalar case. if lookup: # Use lookup attribute for parameter. lookupCode = lookup lookupHelper = helperFuncCode(lookupCode) if lookupHelper: lookupCode = lookupHelper lines.append('%s%s = %s;' % (pType, value, lookupCode)) else: # Use state attribute from parameter's state type. lines.append('%s%s = lookup<%s>(%s, %s, %s);' % (pType, value, sType, state, key, default)) # Check if valid state value. lines.append('if (%s && !%s)' % (key, value)) lines.append('{') if len(stateWarn): lines.append(' %s' % stateWarn) lines.append(' return false;') lines.append('}') elif stateType.dim == 1 and len(pSize): # 1D array case. valueDecl = paramDeclCode(parameter, [stateType], pSize, value) valueDecl = valueDecl.replace('const ', '') # Declare variable for state values. lines.append('%s' % valueDecl) lines.append('if (!%s)' % key) lines.append(' %s.reset();' % value) lines.append('else') lines.append('{') if lookup: # Use lookup attribute of parameter. lookupCode = lookup lookupHelper = helperFuncCode(lookupCode) if lookupHelper: lookupCode = lookupHelper lines.append(' %s.reset(%s);' % (value, lookupCode)) # Check if valid state values in array. lines.append(' if ((%s) && !%s.get())' % (pSize, value)) lines.append(' {') if len(stateWarn): lines.append(' %s' % stateWarn) lines.append(' return false;') lines.append(' }') else: # Use state attribute from parameter's state type. lines.append(' for (size_t _i = 0; _i < size_t(%s); ++_i)' % pSize) lines.append(' {') # Lookup state value for each array element and check if valid. lines.append(' %s[_i] = lookup<%s>(%s, %s[_i], %s);' % (value, sType, state, key, default)) lines.append(' if (%s[_i] && !%s[_i])' % (key, value)) lines.append(' {') if len(stateWarn): lines.append(' %s' % stateWarn) lines.append(' return false;') lines.append(' }') lines.append(' }') lines.append('}') else: # Arrays with dim > 1 or with no size expression are not supported. print 'unsupported type for state lookup' return lines
def emuFindEntry(func, emuFormulae, member): if emuFormulae == None: return None name = func.name # list of function parameter names arglist = [i.name.strip() for i in func.parameters] # arg is a mapping from arg0 to function parameter name... arg = {} for i in range(len(arglist)): arg['arg%d' % i] = arglist[i] # ... and mappings from arg0plus to lists of function parameters for i in range(0, 5): label = 'arg%dplus' % i if len(arglist) > 0: arg[label] = ', '.join(arglist) arglist.pop(0) else: arg[label] = '' # Iterator over the formulae # # k is the key # i is the formula for k, i in emuFormulae.iteritems(): # Cache the compiled regular expressions, as needed if 'entries_re' not in i: i['entries_re'] = [re.compile('^%s$' % j) for j in i['entries']] # A list of matches containing (match object, formula name, formula) # Look for matches, ideally only one m = [[j.match(name), k, i] for k, i in emuFormulae.iteritems() for j in i['entries_re']] m = [j for j in m if j[0]] assert len(m) <= 1, 'Ambiguous match (%s) for %s - giving up.' % ( ', '.join([j[1] for j in m]), name) if len(m): match = m[0][0] formula = m[0][2] rType = typeCode(func.ret.type) dummyRetVal = '' if not typeIsVoid(rType): dummyRetVal = '(( %s )0)' % rType emue = {'name': name, 'member': member, 'dummyretval': dummyRetVal} subs = deepcopy(arg) for l in range(len(match.groups()) + 1): subs['m%d' % l] = match.group(l) subs['name'] = name subs['dummyretval'] = dummyRetVal addSubstitution(name, formula, subs) substitute(emue, formula, 'impl', subs) substitute(emue, formula, 'init', subs) substitute(emue, formula, 'cond', subs) substitute(emue, formula, 'prefix', subs) substitute(emue, formula, 'suffix', subs) substitute(emue, formula, 'pre', subs) substitute(emue, formula, 'post', subs) emue['cond'] = None if 'cond' in formula: emue['cond'] = formula['cond'] # plugin is optional, default to False emue['plugin'] = False if 'plugin' in formula: emue['plugin'] = formula['plugin'] return emue return None