mesa: Remove _mesa_lookup_enum_by_name().
[mesa.git] / src / mapi / glapi / gen / gl_enums.py
1 #!/usr/bin/python2
2 # -*- Mode: Python; py-indent-offset: 8 -*-
3
4 # (C) Copyright Zack Rusin 2005
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the "Software"),
9 # to deal in the Software without restriction, including without limitation
10 # on the rights to use, copy, modify, merge, publish, distribute, sub
11 # license, and/or sell copies of the Software, and to permit persons to whom
12 # the Software is furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice (including the next
15 # paragraph) shall be included in all copies or substantial portions of the
16 # Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 # IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # IN THE SOFTWARE.
25 #
26 # Authors:
27 # Zack Rusin <zack@kde.org>
28
29 import license
30 import gl_XML
31 import sys, getopt
32
33 class PrintGlEnums(gl_XML.gl_print_base):
34
35 def __init__(self):
36 gl_XML.gl_print_base.__init__(self)
37
38 self.name = "gl_enums.py (from Mesa)"
39 self.license = license.bsd_license_template % ( \
40 """Copyright (C) 1999-2005 Brian Paul All Rights Reserved.""", "BRIAN PAUL")
41 self.enum_table = {}
42
43
44 def printRealHeader(self):
45 print '#include "main/glheader.h"'
46 print '#include "main/enums.h"'
47 print '#include "main/imports.h"'
48 print '#include "main/mtypes.h"'
49 print ''
50 print 'typedef struct {'
51 print ' size_t offset;'
52 print ' int n;'
53 print '} enum_elt;'
54 print ''
55 return
56
57 def print_code(self):
58 print """
59 typedef int (*cfunc)(const void *, const void *);
60
61 /**
62 * Compare a key enum value to an element in the \c all_enums array.
63 *
64 * \c bsearch always passes the key as the first parameter and the pointer
65 * to the array element as the second parameter. We can elimiate some
66 * extra work by taking advantage of that fact.
67 *
68 * \param a Pointer to the desired enum name.
69 * \param b Pointer to an index into the \c all_enums array.
70 */
71 static int compar_nr( const int *a, const unsigned *b )
72 {
73 return a[0] - all_enums[*b].n;
74 }
75
76
77 static char token_tmp[20];
78
79 const char *_mesa_lookup_enum_by_nr( int nr )
80 {
81 unsigned * i;
82
83 i = (unsigned *) _mesa_bsearch(& nr, reduced_enums,
84 Elements(reduced_enums),
85 sizeof(reduced_enums[0]),
86 (cfunc) compar_nr);
87
88 if ( i != NULL ) {
89 return & enum_string_table[ all_enums[ *i ].offset ];
90 }
91 else {
92 /* this is not re-entrant safe, no big deal here */
93 _mesa_snprintf(token_tmp, sizeof(token_tmp) - 1, "0x%x", nr);
94 token_tmp[sizeof(token_tmp) - 1] = '\\0';
95 return token_tmp;
96 }
97 }
98
99 /**
100 * Primitive names
101 */
102 static const char *prim_names[PRIM_MAX+3] = {
103 "GL_POINTS",
104 "GL_LINES",
105 "GL_LINE_LOOP",
106 "GL_LINE_STRIP",
107 "GL_TRIANGLES",
108 "GL_TRIANGLE_STRIP",
109 "GL_TRIANGLE_FAN",
110 "GL_QUADS",
111 "GL_QUAD_STRIP",
112 "GL_POLYGON",
113 "GL_LINES_ADJACENCY",
114 "GL_LINE_STRIP_ADJACENCY",
115 "GL_TRIANGLES_ADJACENCY",
116 "GL_TRIANGLE_STRIP_ADJACENCY",
117 "outside begin/end",
118 "unknown state"
119 };
120
121
122 /* Get the name of an enum given that it is a primitive type. Avoids
123 * GL_FALSE/GL_POINTS ambiguity and others.
124 */
125 const char *
126 _mesa_lookup_prim_by_nr(GLuint nr)
127 {
128 if (nr < Elements(prim_names))
129 return prim_names[nr];
130 else
131 return "invalid mode";
132 }
133
134
135 """
136 return
137
138
139 def printBody(self, api_list):
140 self.enum_table = {}
141 for api in api_list:
142 self.process_enums( api )
143
144 keys = self.enum_table.keys()
145 keys.sort()
146
147 name_table = []
148 enum_table = {}
149
150 for enum in keys:
151 low_pri = 9
152 for [name, pri] in self.enum_table[ enum ]:
153 name_table.append( [name, enum] )
154
155 if pri < low_pri:
156 low_pri = pri
157 enum_table[enum] = name
158
159
160 name_table.sort()
161
162 string_offsets = {}
163 i = 0;
164 print 'LONGSTRING static const char enum_string_table[] = '
165 for [name, enum] in name_table:
166 print ' "%s\\0"' % (name)
167 string_offsets[ name ] = i
168 i += len(name) + 1
169
170 print ' ;'
171 print ''
172
173
174 print 'static const enum_elt all_enums[%u] =' % (len(name_table))
175 print '{'
176 for [name, enum] in name_table:
177 print ' { %5u, 0x%08X }, /* %s */' % (string_offsets[name], enum, name)
178 print '};'
179 print ''
180
181 print 'static const unsigned reduced_enums[%u] =' % (len(keys))
182 print '{'
183 for enum in keys:
184 name = enum_table[ enum ]
185 if [name, enum] not in name_table:
186 print ' /* Error! %s, 0x%04x */ 0,' % (name, enum)
187 else:
188 i = name_table.index( [name, enum] )
189
190 print ' %4u, /* %s */' % (i, name)
191 print '};'
192
193
194 self.print_code()
195 return
196
197
198 def process_enums(self, api):
199 for obj in api.enumIterateByName():
200 if obj.value not in self.enum_table:
201 self.enum_table[ obj.value ] = []
202
203
204 enum = self.enum_table[ obj.value ]
205 name = "GL_" + obj.name
206 priority = obj.priority()
207 already_in = False;
208 for n, p in enum:
209 if n == name:
210 already_in = True
211 if not already_in:
212 enum.append( [name, priority] )
213
214
215 def show_usage():
216 print "Usage: %s [-f input_file_name]" % sys.argv[0]
217 sys.exit(1)
218
219 if __name__ == '__main__':
220 try:
221 (args, trail) = getopt.getopt(sys.argv[1:], "f:")
222 except Exception,e:
223 show_usage()
224
225 api_list = []
226 for (arg,val) in args:
227 if arg == "-f":
228 api = gl_XML.parse_GL_API( val )
229 api_list.append(api);
230
231 printer = PrintGlEnums()
232 printer.Print( api_list )