8 def __init__(self, fn):
14 if h==0 or (h>0 and l[h-1].isspace()):
25 """Strips any vendor suffix and GL prefix from a name (but not GLX prefix)"""
28 if name.endswith(" *"):
31 elif name.endswith("Pointer"):
36 if name.startswith("const "):
40 if name.startswith("GL") and not name.startswith("GLX"):
42 if name.endswith(("EXT", "ARB", "SGI", "IBM", "ATI")):
43 return prefix+name[:-3]+suffix
44 elif name.endswith("SGIX"):
45 return prefix+name[:-4]+suffix
46 elif name.endswith(("NV", "HP")):
47 return prefix+name[:-2]+suffix
49 return prefix+name+suffix
53 def __init__(self, fn):
55 for line in InputFile(fn):
56 parts = [p.strip() for p in line.split(',')]
59 elif parts[3][-1]=='*' and parts[3][-2]!=' ':
60 parts[3] = parts[3][:-1]+" *"
61 self.map[tuple(parts[0:3])] = tuple(parts[3:6])
63 def wildcard_match(self, a, b):
68 def __getitem__(self, key):
70 return self.map[(key[0], "*", "*")]
72 for k, v in self.map.iteritems():
73 if strip_name(k[0])==strip_name(key[0]) and self.wildcard_match(k[1], key[1]) and self.wildcard_match(k[2], key[2]):
77 def update(self, other):
78 self.map.update(other.map)
82 def __init__(self, fn):
84 self.map["void"] = None
85 for line in InputFile(fn):
86 parts = [p.strip() for p in line.split(',')]
87 self.map[parts[0]] = tuple(parts[1:])
89 def __getitem__(self, key):
90 return self.map[strip_name(key)]
95 def __init__(self, func, name):
106 def set_type(self, type, dir, kind):
111 def set_size(self, size):
112 if type(size)==str and size.isdigit():
113 self.size = int(size)
117 def derive_ctype(self):
118 m = typemap[(self.type, self.direction, self.kind)]
121 self.direction = m[1]
124 self.base_ctype = self.ctype
125 if self.kind=="value":
126 if self.base_ctype.startswith("const "):
127 self.base_ctype = self.base_ctype[6:]
128 self.io = iomap[self.base_ctype]
130 if self.direction=="in":
131 self.ctype = "const "+self.ctype
132 self.ctype = self.ctype+" *"
134 def derive_csize(self):
135 if self.kind=="array" and self.size is not None:
137 if type(self.size)==int:
138 self.csize = "%d*sizeof(%s)"%(self.size, self.base_ctype)
139 elif self.size.startswith("COMPSIZE("):
140 self.csize = self.func.compsize(self.size[9:-1], self.base_ctype)
142 s = self.func.get_param(self.size.split('*')[0])
143 if (s.type=="SizeI" or s.type.endswith("Int32") or s.type.startswith("BufferSize")) and s.kind=="value":
144 self.csize = "%s*sizeof(%s)"%(self.size, self.base_ctype)
145 if self.func.name.startswith("Uniform") and self.func.name[7].isdigit():
146 self.csize += "*%s"%func.name[7]
148 sys.stderr.write("Could not determine size for array parameter '%s[%s]' of function '%s'\n"%(self.name, self.size, self.func.name))
149 elif self.kind=="reference":
150 self.csize = "sizeof(%s)"%self.base_ctype
152 def __init__(self, name, pnames):
154 self.ret = Function.Parameter(self, "ret")
155 self.params = [Function.Parameter(self, n) for n in pnames]
158 def get_param(self, pname):
159 for p in self.params:
162 raise KeyError, pname
164 def set_category(self, cat):
167 def compsize(self, size, btype):
173 for c in size.replace(',', '/').split('/'):
174 param = self.get_param(c)
176 sys.stderr.write("Compsize '%s' for function '%s' failed: No parameter '%s'\n"%(size, self.name, c))
182 cn = strip_name(param.type)
183 if cn.endswith("Type"):
184 res += "typesize(%s)"%param.name
186 elif cn.endswith("Format"):
187 res += "formatsize(%s)"%param.name
188 elif cn.endswith(("Parameter", "ParameterPName", "ParameterName")) or cn=="GetPName":
189 res += "paramsize(%s)"%param.name
190 elif cn=="MapTarget":
191 res += "mapsize(%s)"%param.name
192 elif (cn=="SizeI" or cn.endswith("Int32")) and not param.size:
195 sys.stderr.write("Compsize '%s' for function '%s' failed: Parameter '%s' has unknown type '%s'\n"%(size, self.name, param.name, param.type))
198 res += "*sizeof(%s)"%btype
202 self.ret.derive_ctype()
203 for p in self.params:
205 for p in self.params:
210 def __init__(self, fn):
216 for line in InputFile(fn):
218 if not literal and text:
219 self.add_section(text, literal)
221 text += line[1:]+"\n"
224 parts = line[1:].split()
225 if parts[0]=="handcode":
226 self.handcode.append(parts[1])
229 self.add_section(text, literal)
234 self.add_section(text, literal)
236 def add_section(self, text, literal):
238 self.sections.append(text)
240 self.sections.append(compile(text, "-", "exec"))
242 def write(self, str, *args):
243 sys.stdout.write(str%args)
245 def writeln(self, str, *args):
246 sys.stdout.write(str%args+"\n")
248 def process(self, functions):
249 for sect in self.sections:
253 for func in functions:
254 if func.name in self.handcode:
261 "params": func.params
267 def __init__(self, fn):
272 self.ignore_categs = []
273 self.ignore_funcs = []
275 for line in InputFile(fn):
277 if parts[0]=="typemap":
278 self.typemap = parts[1]
279 elif parts[0]=="iomap":
280 self.iomap = parts[1]
281 elif parts[0]=="spec":
282 self.specs.append(parts[1])
283 elif parts[0]=="prefix":
284 self.prefix = parts[1]
285 elif parts[0]=="ignore":
286 if parts[1]=="category":
287 self.ignore_categs.append(parts[2])
288 elif parts[1]=="function":
289 self.ignore_funcs.append(parts[2])
291 sys.stderr.write("Unknown keyword '%s'\n", parts[0])
294 def read_spec(fn, prefix):
297 for line in InputFile(fn):
298 if line.find(':')>=0:
300 elif line[0]=='\t' and cur_func:
302 if parts[0]=="return":
303 cur_func.ret.set_type(parts[1], "out", "value")
304 elif parts[0]=="param":
305 bracket = parts[4].find('[')
307 parts.insert(5, parts[4][bracket:])
308 parts[4] = parts[4][:bracket]
310 param = cur_func.get_param(parts[1])
311 param.set_type(parts[2], parts[3], parts[4])
312 if len(parts)==6 or (len(parts)>6 and parts[6]!="retained"):
313 param.set_size(parts[5][1:-1])
314 elif parts[0]=="category":
315 cur_func.set_category(parts[1])
316 elif parts[0]=="glxvendorglx" and cur_func.category=="glx":
317 cur_func.set_category("glxext")
319 paren = line.find('(')
321 cparen = line.rfind(')')
323 pnames = [n.strip() for n in line[paren+1:cparen].split(",")]
326 cur_func = Function(prefix+line[:paren], pnames)
327 funcs.append(cur_func)
330 template = Template(sys.argv[1])
332 for i in sys.argv[2:]:
335 typemap = Typemap(files.typemap)
336 iomap = IOmap(files.iomap)
337 for s in files.specs:
338 funcs = read_spec(s, files.prefix)
339 funcs = [f for f in funcs if f.name not in files.ignore_funcs and f.category not in files.ignore_categs]
342 names = [f.name for f in funcs]
343 functions = [f for f in functions if f.name not in names]+funcs
345 template.process(functions)