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)
141 elif self.size=="" and (self.type=="charARB" or self.type=="Char"):
142 self.csize = "strlen"
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 cn.endswith(("Parameter", "ParameterPName", "ParameterName")) or cn=="GetPName":
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)"%param.ctype
204 self.ret.derive_ctype()
205 for p in self.params:
207 for p in self.params:
212 def __init__(self, fn):
217 for line in InputFile(fn):
219 if not literal and text:
220 self.add_section(text, literal)
222 text += line[1:]+"\n"
226 self.add_section(text, literal)
231 self.add_section(text, literal)
233 def add_section(self, text, literal):
235 self.sections.append(text)
237 self.sections.append(compile(text, "-", "exec"))
239 def write(self, str, *args):
240 sys.stdout.write(str%args)
242 def writeln(self, str, *args):
243 sys.stdout.write(str%args+"\n")
245 def process(self, functions):
246 for sect in self.sections:
250 for func in functions:
256 "params": func.params
262 def __init__(self, fn):
267 self.ignore_categs = []
268 self.ignore_funcs = []
270 for line in InputFile(fn):
272 if parts[0]=="typemap":
273 self.typemap = parts[1]
274 elif parts[0]=="iomap":
275 self.iomap = parts[1]
276 elif parts[0]=="spec":
277 self.specs.append(parts[1])
278 elif parts[0]=="prefix":
279 self.prefix = parts[1]
280 elif parts[0]=="ignore":
281 if parts[1]=="category":
282 self.ignore_categs.append(parts[2])
283 elif parts[1]=="function":
284 self.ignore_funcs.append(parts[2])
286 sys.stderr.write("Unknown keyword '%s'\n", parts[0])
289 def read_spec(fn, prefix):
292 for line in InputFile(fn):
293 if line.find(':')>=0:
295 elif line[0]=='\t' and cur_func:
297 if parts[0]=="return":
298 cur_func.ret.set_type(parts[1], "out", "value")
299 elif parts[0]=="param":
300 bracket = parts[4].find('[')
302 parts.insert(5, parts[4][bracket:])
303 parts[4] = parts[4][:bracket]
305 param = cur_func.get_param(parts[1])
306 param.set_type(parts[2], parts[3], parts[4])
307 if len(parts)==6 or (len(parts)>6 and parts[6]!="retained"):
308 param.set_size(parts[5][1:-1])
309 elif parts[0]=="category":
310 cur_func.set_category(parts[1])
311 elif parts[0]=="glxvendorglx" and cur_func.category=="glx":
312 cur_func.set_category("glxext")
314 paren = line.find('(')
316 cparen = line.rfind(')')
318 pnames = [n.strip() for n in line[paren+1:cparen].split(",")]
321 cur_func = Function(prefix+line[:paren], pnames)
322 funcs.append(cur_func)
325 template = Template(sys.argv[1])
327 for i in sys.argv[2:]:
330 typemap = Typemap(files.typemap)
331 iomap = IOmap(files.iomap)
332 for s in files.specs:
333 funcs = read_spec(s, files.prefix)
334 funcs = [f for f in funcs if f.name not in files.ignore_funcs and f.category not in files.ignore_categs]
337 names = [f.name for f in funcs]
338 functions = [f for f in functions if f.name not in names]+funcs
340 template.process(functions)