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, typemap, iomap):
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:]
129 if self.direction=="in":
130 self.ctype = "const "+self.ctype
131 self.ctype = self.ctype+" *"
132 self.io = iomap[self.base_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)
141 elif self.size=="pname":
142 self.csize = "paramsize(pname)*sizeof(%s)"%self.base_ctype
144 s = self.func.get_param(self.size.split('*')[0])
145 if (s.type=="SizeI" or s.type.endswith("Int32") or s.type.startswith("BufferSize")):
147 self.csize = "%s"%self.size
148 if self.func.name.startswith("glUniformMatrix") and self.func.name[16]!='x':
149 self.csize += "*%d"%(int(self.func.name[15])**2)
150 elif self.func.name.startswith("glUniform") and self.func.name[9].isdigit():
151 self.csize += "*%s"%self.func.name[9]
152 if strip_name(self.base_ctype)!="void":
153 self.csize += "*sizeof(%s)"%self.base_ctype
154 elif s.kind=="array" and s.size==1:
155 self.csize = "*%s"%s.name
157 sys.stderr.write("Could not determine size for array parameter '%s[%s]' of function '%s'\n"%(self.name, self.size, self.func.name))
158 elif self.kind=="reference":
159 self.csize = "sizeof(%s)"%self.base_ctype
161 def __init__(self, name, pnames):
163 self.ret = Function.Parameter(self, "ret")
164 self.params = [Function.Parameter(self, n) for n in pnames]
167 def get_param(self, pname):
168 for p in self.params:
171 raise KeyError, pname
173 def set_category(self, cat):
176 def compsize(self, size, btype):
182 for c in size.replace(',', '/').split('/'):
183 param = self.get_param(c)
185 sys.stderr.write("Compsize '%s' for function '%s' failed: No parameter '%s'\n"%(size, self.name, c))
191 cn = strip_name(param.type)
192 if cn.endswith("Type"):
193 res += "typesize(%s)"%param.name
195 elif cn.endswith("Format"):
196 res += "formatsize(%s)"%param.name
197 elif param.name=="pname" or cn.endswith("Parameter") or (param.name=="target" and cn=="enum"):
198 res += "paramsize(%s)"%param.name
199 elif param.name=="buffer" and cn=="enum":
200 res += "buffersize(%s)"%param.name
201 elif (cn=="SizeI" or cn.endswith("Int32")) and not param.size:
204 sys.stderr.write("Compsize '%s' for function '%s' failed: Parameter '%s' has unknown type '%s'\n"%(size, self.name, param.name, param.type))
207 res += "*sizeof(%s)"%btype
210 def finalize(self, typemap, iomap):
211 self.ret.derive_ctype(typemap, iomap)
212 for p in self.params:
213 p.derive_ctype(typemap, iomap)
214 for p in self.params:
219 def __init__(self, name, category, value):
221 self.category = category
226 def __init__(self, fn):
227 self.mode = "functions"
233 for line in InputFile(fn):
235 if not literal and text:
236 self.add_section(text, literal)
238 text += line[1:]+"\n"
241 parts = line[1:].split()
242 if parts[0]=="handcode":
243 self.handcode.append(parts[1])
244 elif parts[0]=="mode":
247 sys.stderr.write("Unknown keyword '%s'\n"%parts[0])
250 self.add_section(text, literal)
255 self.add_section(text, literal)
257 def add_section(self, text, literal):
259 self.sections.append(text)
261 self.sections.append(compile(text, "-", "exec"))
263 def write(self, str, *args):
264 sys.stdout.write(str%args)
266 def writeln(self, str, *args):
267 sys.stdout.write(str%args+"\n")
269 def process_function(self, code, func):
270 if func.name in self.handcode:
278 "params": func.params
282 def process_enum(self, code, enum):
283 if enum.value is None:
293 def process_sections(self, objects, handler):
294 for sect in self.sections:
301 def process(self, apis):
302 if self.mode=="functions":
305 typemap = Typemap(api.typemap)
306 iomap = IOmap(api.iomap)
307 for spec in api.specs:
308 funcs = read_spec(spec, api.prefix)
309 funcs = [f for f in funcs if f.name not in api.ignore_funcs and f.category not in api.ignore_categs]
311 func.finalize(typemap, iomap)
312 names = [f.name for f in funcs]
313 functions = [f for f in functions if f.name not in names]+funcs
314 self.process_sections(functions, self.process_function)
315 elif self.mode=="enums":
318 for spec in api.enumspecs:
319 ens = read_enums(spec, api.enumprefix)
321 enums.sort(lambda x, y: cmp(x.value, y.value)*2+cmp(x.category, y.category))
322 self.process_sections(enums, self.process_enum)
326 def __init__(self, fn):
332 self.enumprefix = None
333 self.ignore_categs = []
334 self.ignore_funcs = []
336 for line in InputFile(fn):
338 if parts[0]=="typemap":
339 self.typemap = parts[1]
340 elif parts[0]=="iomap":
341 self.iomap = parts[1]
342 elif parts[0]=="spec":
343 self.specs.append(parts[1])
344 elif parts[0]=="prefix":
345 self.prefix = parts[1]
346 elif parts[0]=="enumspec":
347 self.enumspecs.append(parts[1])
348 elif parts[0]=="enumprefix":
349 self.enumprefix = parts[1]
350 elif parts[0]=="ignore":
351 if parts[1]=="category":
352 self.ignore_categs.append(parts[2])
353 elif parts[1]=="function":
354 self.ignore_funcs.append(parts[2])
356 sys.stderr.write("Unknown keyword '%s'\n", parts[0])
359 def read_spec(fn, prefix):
363 for line in InputFile(fn):
364 if line.find(':')>=0:
366 elif line[0]=='\t' and cur_func:
368 if parts[0]=="return":
369 cur_func.ret.set_type(parts[1], "out", "value")
370 elif parts[0]=="param":
371 bracket = parts[4].find('[')
373 parts.insert(5, parts[4][bracket:])
374 parts[4] = parts[4][:bracket]
376 param = cur_func.get_param(parts[1])
377 param.set_type(parts[2], parts[3], parts[4])
378 if len(parts)==6 or (len(parts)>6 and parts[6]!="retained"):
379 param.set_size(parts[5][1:-1])
380 elif parts[0]=="category":
381 cur_func.set_category(parts[1])
382 elif parts[0]=="glxvendorglx" and cur_func.category=="glx":
383 cur_func.set_category("glxext")
385 paren = line.find('(')
387 cparen = line.rfind(')')
389 pnames = [n.strip() for n in line[paren+1:cparen].split(",")]
392 cur_func = Function(prefix+line[:paren], pnames)
393 funcs.append(cur_func)
397 def read_enums(fn, prefix):
401 for line in InputFile(fn):
403 parts = line[:line.find(':')].split()
404 if len(parts)==2 and parts[1]=="enum":
409 enums.append(Enum(prefix+parts[2], cur_categ, None))
412 enums.append(Enum(prefix+parts[0], cur_categ, int(parts[2], 0)))
413 except ValueError, e:
414 sys.stderr.write("Syntax error in %s: %s\n"%(fn, e))
419 if n.name==e.name and n.value is not None:
422 sys.stderr.write("Could not find value for enum reference %s in category %s\n"%(e.name, e.category))
426 template = Template(sys.argv[1])
428 for i in sys.argv[2:]:
431 template.process(apis)