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:]
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")) and s.kind=="value":
146 self.csize = "%s*sizeof(%s)"%(self.size, self.base_ctype)
147 if self.func.name.startswith("Uniform") and self.func.name[7].isdigit():
148 self.csize += "*%s"%func.name[7]
150 sys.stderr.write("Could not determine size for array parameter '%s[%s]' of function '%s'\n"%(self.name, self.size, self.func.name))
151 elif self.kind=="reference":
152 self.csize = "sizeof(%s)"%self.base_ctype
154 def __init__(self, name, pnames):
156 self.ret = Function.Parameter(self, "ret")
157 self.params = [Function.Parameter(self, n) for n in pnames]
160 def get_param(self, pname):
161 for p in self.params:
164 raise KeyError, pname
166 def set_category(self, cat):
169 def compsize(self, size, btype):
175 for c in size.replace(',', '/').split('/'):
176 param = self.get_param(c)
178 sys.stderr.write("Compsize '%s' for function '%s' failed: No parameter '%s'\n"%(size, self.name, c))
184 cn = strip_name(param.type)
185 if cn.endswith("Type"):
186 res += "typesize(%s)"%param.name
188 elif cn.endswith("Format"):
189 res += "formatsize(%s)"%param.name
190 elif param.name=="pname" or cn.endswith("Parameter"):
191 res += "paramsize(%s)"%param.name
192 elif cn=="MapTarget":
193 res += "mapsize(%s)"%param.name
194 elif (cn=="SizeI" or cn.endswith("Int32")) and not param.size:
197 sys.stderr.write("Compsize '%s' for function '%s' failed: Parameter '%s' has unknown type '%s'\n"%(size, self.name, param.name, param.type))
200 res += "*sizeof(%s)"%btype
204 self.ret.derive_ctype()
205 for p in self.params:
207 for p in self.params:
212 def __init__(self, fn):
218 for line in InputFile(fn):
220 if not literal and text:
221 self.add_section(text, literal)
223 text += line[1:]+"\n"
226 parts = line[1:].split()
227 if parts[0]=="handcode":
228 self.handcode.append(parts[1])
231 self.add_section(text, literal)
236 self.add_section(text, literal)
238 def add_section(self, text, literal):
240 self.sections.append(text)
242 self.sections.append(compile(text, "-", "exec"))
244 def write(self, str, *args):
245 sys.stdout.write(str%args)
247 def writeln(self, str, *args):
248 sys.stdout.write(str%args+"\n")
250 def process(self, functions):
251 for sect in self.sections:
255 for func in functions:
256 if func.name in self.handcode:
263 "params": func.params
269 def __init__(self, fn):
274 self.ignore_categs = []
275 self.ignore_funcs = []
277 for line in InputFile(fn):
279 if parts[0]=="typemap":
280 self.typemap = parts[1]
281 elif parts[0]=="iomap":
282 self.iomap = parts[1]
283 elif parts[0]=="spec":
284 self.specs.append(parts[1])
285 elif parts[0]=="prefix":
286 self.prefix = parts[1]
287 elif parts[0]=="ignore":
288 if parts[1]=="category":
289 self.ignore_categs.append(parts[2])
290 elif parts[1]=="function":
291 self.ignore_funcs.append(parts[2])
293 sys.stderr.write("Unknown keyword '%s'\n", parts[0])
296 def read_spec(fn, prefix):
299 for line in InputFile(fn):
300 if line.find(':')>=0:
302 elif line[0]=='\t' and cur_func:
304 if parts[0]=="return":
305 cur_func.ret.set_type(parts[1], "out", "value")
306 elif parts[0]=="param":
307 bracket = parts[4].find('[')
309 parts.insert(5, parts[4][bracket:])
310 parts[4] = parts[4][:bracket]
312 param = cur_func.get_param(parts[1])
313 param.set_type(parts[2], parts[3], parts[4])
314 if len(parts)==6 or (len(parts)>6 and parts[6]!="retained"):
315 param.set_size(parts[5][1:-1])
316 elif parts[0]=="category":
317 cur_func.set_category(parts[1])
318 elif parts[0]=="glxvendorglx" and cur_func.category=="glx":
319 cur_func.set_category("glxext")
321 paren = line.find('(')
323 cparen = line.rfind(')')
325 pnames = [n.strip() for n in line[paren+1:cparen].split(",")]
328 cur_func = Function(prefix+line[:paren], pnames)
329 funcs.append(cur_func)
332 template = Template(sys.argv[1])
334 for i in sys.argv[2:]:
337 typemap = Typemap(files.typemap)
338 iomap = IOmap(files.iomap)
339 for s in files.specs:
340 funcs = read_spec(s, files.prefix)
341 funcs = [f for f in funcs if f.name not in files.ignore_funcs and f.category not in files.ignore_categs]
344 names = [f.name for f in funcs]
345 functions = [f for f in functions if f.name not in names]+funcs
347 template.process(functions)