summaryrefslogtreecommitdiffstats
path: root/src/mesa/main/APIspecutil.py
blob: 27a8fe8a6d4361f463bd9c638fa5eef998a3fccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/python
#
# Copyright (C) 2009 Chia-I Wu <olv@0xlab.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# on the rights to use, copy, modify, merge, publish, distribute, sub
# license, and/or sell copies of the Software, and to permit persons to whom
# the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
# IBM AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
"""
Minimal apiutil.py interface for use by es_generator.py.
"""

import sys
import libxml2

import APIspec

__spec = {}
__functions = {}
__aliases = {}

def _ParseXML(filename, apiname):
    conversions = {
        # from           to
        'GLfloat':  [ 'GLdouble' ],
        'GLclampf': [ 'GLclampd' ],
        'GLubyte':  [ 'GLfloat', 'GLdouble' ],
        'GLint':    [ 'GLfloat', 'GLdouble' ],
        'GLfixed':  [ 'GLfloat', 'GLdouble' ],
        'GLclampx': [ 'GLclampf', 'GLclampd' ],
    }

    doc = libxml2.readFile(filename, None,
            libxml2.XML_PARSE_DTDLOAD +
            libxml2.XML_PARSE_DTDVALID +
            libxml2.XML_PARSE_NOBLANKS)
    spec = APIspec.Spec(doc)
    impl = spec.get_impl()
    api = spec.get_api(apiname)
    doc.freeDoc()

    __spec["impl"] = impl
    __spec["api"] = api

    for func in api.functions:
        alias, need_conv = impl.match(func, conversions)
        if not alias:
            # external functions are manually dispatched
            if not func.is_external:
                print >>sys.stderr, "Error: unable to dispatch %s" % func.name
            alias = func
            need_conv = False

        __functions[func.name] = func
        __aliases[func.name] = (alias, need_conv)


def AllSpecials(notused=None):
    """Return a list of all external functions in the API."""
    api = __spec["api"]

    specials = []
    for func in api.functions:
        if func.is_external:
            specials.append(func.name)

    return specials


def GetAllFunctions(filename, api):
    """Return sorted list of all functions in the API."""
    if not __spec:
        _ParseXML(filename, api)

    api = __spec["api"]
    names = []
    for func in api.functions:
        names.append(func.name)
    names.sort()
    return names


def ReturnType(funcname):
    """Return the C return type of named function."""
    func = __functions[funcname]
    return func.return_type


def Properties(funcname):
    """Return list of properties of the named GL function."""
    func = __functions[funcname]
    return [func.direction]


def _ValidValues(func, param):
    """Return the valid values of a parameter."""
    valid_values = []
    switch = func.checker.switches.get(param.name, [])
    for desc in switch:
        # no dependent vector
        if not desc.checker.switches:
            for val in desc.values:
                valid_values.append((val, None, None, [], desc.error, None))
            continue

        items = desc.checker.switches.items()
        if len(items) > 1:
            print >>sys.stderr, "%s: more than one parameter depend on %s" % \
                    (func.name, desc.name)
        dep_name, dep_switch = items[0]

        for dep_desc in dep_switch:
            if dep_desc.index >= 0 and dep_desc.index != 0:
                print >>sys.stderr, "%s: not first element of a vector" % func.name
            if dep_desc.checker.switches:
                print >>sys.stderr, "%s: deep nested dependence" % func.name

            convert = None if dep_desc.convert else "noconvert"
            for val in desc.values:
                valid_values.append((val, dep_desc.size_str, dep_desc.name,
                                     dep_desc.values, dep_desc.error, convert))
    return valid_values


def _Conversion(func, src_param):
    """Return the destination type of the conversion, or None."""
    alias, need_conv = __aliases[func.name]
    if need_conv:
        dst_param = alias.get_param(src_param.name)
        if src_param.type == dst_param.type:
            need_conv = False
    if not need_conv:
        return (None, "none")

    converts = { True: 0, False: 0 }

    # In Fogx, for example,  pname may be GL_FOG_DENSITY/GL_FOG_START/GL_FOG_END
    # or GL_FOG_MODE.  In the former three cases, param is not checked and the
    # default is to convert.
    if not func.checker.always_check(src_param.name):
        converts[True] += 1

    for desc in func.checker.flatten(src_param.name):
        converts[desc.convert] += 1
        if converts[True] and converts[False]:
            break

    # it should be "never", "sometimes", and "always"...
    if converts[False]:
        if converts[True]:
            conversion = "some"
        else:
            conversion = "none"
    else:
        conversion = "all"

    return (dst_param.base_type(), conversion)


def _MaxVecSize(func, param):
    """Return the largest possible size of a vector."""
    if not param.is_vector:
        return 0
    if param.size:
        return param.size

    # need to look at all descriptions
    size = 0
    for desc in func.checker.flatten(param.name):
        if desc.size_str and desc.size_str.isdigit():
            s = int(desc.size_str)
            if s > size:
                size = s
    if not size:
        need_conv = __aliases[func.name][1]
        if need_conv:
            print >>sys.stderr, \
                    "Error: unable to dicide the max size of %s in %s" % \
                    (param.name, func.name)
    return size


def _ParameterTuple(func, param):
    """Return a parameter tuple.

    [0] -- parameter name
    [1] -- parameter type
    [2] -- max vector size or 0
    [3] -- dest type the parameter converts to, or None
    [4] -- valid values
    [5] -- how often does the conversion happen

    """
    vec_size = _MaxVecSize(func, param)
    dst_type, conversion = _Conversion(func, param)
    valid_values = _ValidValues(func, param)

    return (param.name, param.type, vec_size, dst_type, valid_values, conversion)


def Parameters(funcname):
    """Return list of tuples of function parameters."""
    func = __functions[funcname]
    params = []
    for param in func.params:
        params.append(_ParameterTuple(func, param))

    return params


def FindParamIndex(params, paramname):
    """Find the index of a named parameter."""
    for i in xrange(len(params)):
        if params[i][0] == paramname:
            return i
    return None


def MakeDeclarationString(params):
    """Return a C-style parameter declaration string."""
    string = []
    for p in params:
        sep = "" if p[1].endswith("*") else " "
        string.append("%s%s%s" % (p[1], sep, p[0]))
    if not string:
        return "void"
    return ", ".join(string)


def AliasPrefix(funcname):
    """Return the prefix of the function the named function is an alias of."""
    alias = __aliases[funcname][0]
    return alias.prefix


def Alias(funcname):
    """Return the name of the function the named function is an alias of."""
    alias, need_conv = __aliases[funcname]
    return alias.name if not need_conv else None


def ConversionFunction(funcname):
    """Return the name of the function the named function converts to."""
    alias, need_conv = __aliases[funcname]
    return alias.name if need_conv else None


def Categories(funcname):
    """Return all the categories of the named GL function."""
    api = __spec["api"]
    return [api.name]