summaryrefslogtreecommitdiff
path: root/registry/vulkan/scripts/scriptgenerator.py
blob: 204639dea4837ff6e3c94e0dbbaa3e87492ed0f8 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/python3 -i
#
# Copyright 2013-2021 The Khronos Group Inc.
#
# SPDX-License-Identifier: Apache-2.0

from generator import OutputGenerator, enquote, noneStr

def mostOfficial(api, newapi):
    """Return the 'most official' of two related names, api and newapi.
       KHR is more official than EXT is more official than everything else.
       If there is ambiguity, return api."""

    if api[-3:] == 'KHR':
        return api
    if newapi[-3:] == 'KHR':
        return newapi;
    if api[-3:] == 'EXT':
        return api
    if newapi[-3:] == 'EXT':
        return newapi;
    return api

class ScriptOutputGenerator(OutputGenerator):
    """ScriptOutputGenerator - subclass of OutputGenerator.
    Base class to Generate script (Python/Ruby/etc.) data structures
    describing API names and relationships.
    Similar to DocOutputGenerator, but writes a single file."""

    def apiName(self, name):
        """Return True if name is in the reserved API namespace.

        Delegates to the conventions object. """
        return self.genOpts.conventions.is_api_name(name)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Track features being generated
        self.features = []

        # Reverse map from interface names to features requiring them
        self.apimap = {}

        # Reverse map from unsupported APIs in this build to aliases which
        # are supported
        self.nonexistent = {}

    def beginFile(self, genOpts):
        OutputGenerator.beginFile(self, genOpts)
        #
        # Dictionaries are keyed by the name of the entity (e.g.
        # self.structs is keyed by structure names). Values are
        # the names of related entities (e.g. structs contain
        # a list of type names of members, enums contain a list
        # of enumerants belong to the enumerated type, etc.), or
        # just None if there are no directly related entities.
        #
        # Collect the mappings, then emit the Python script in endFile
        self.basetypes = {}
        self.consts = {}
        self.enums = {}
        self.flags = {}
        self.funcpointers = {}
        self.protos = {}
        self.structs = {}
        self.handles = {}
        self.defines = {}
        self.alias = {}
        # Dictionary containing the type of a type name
        # (e.g. the string name of the dictionary with its contents).
        self.typeCategory = {}
        self.mapDict = {}

    def addInterfaceMapping(self, api, feature, required):
        """Add a reverse mapping in self.apimap from an API to a feature
           requiring that API.

        - api - name of the API
        - feature - name of the feature requiring it
        - required - None, or an additional feature dependency within
          'feature' """

        # Each entry in self.apimap contains one or more
        # ( feature, required ) tuples.
        deps = ( feature, required )

        if api in self.apimap:
            self.apimap[api].append(deps)
        else:
            self.apimap[api] = [ deps ]

    def mapInterfaceKeys(self, feature, key):
        """Construct reverse mapping of APIs to features requiring them in
           self.apimap.

        - feature - name of the feature being generated
        - key - API category - 'define', 'basetype', etc."""

        dict = self.featureDictionary[feature][key]

        if dict:
            # Not clear why handling of command vs. type APIs is different -
            # see interfacedocgenerator.py, which this was based on.
            if key == 'command':
                for required in dict:
                    for api in dict[required]:
                        self.addInterfaceMapping(api, feature, required)
            else:
                for required in dict:
                    for parent in dict[required]:
                        for api in dict[required][parent]:
                            self.addInterfaceMapping(api, feature, required)

    def mapInterfaces(self, feature):
        """Construct reverse mapping of APIs to features requiring them in
           self.apimap.

        - feature - name of the feature being generated"""

        # Map each category of interface
        self.mapInterfaceKeys(feature, 'basetype')
        self.mapInterfaceKeys(feature, 'bitmask')
        self.mapInterfaceKeys(feature, 'command')
        self.mapInterfaceKeys(feature, 'define')
        self.mapInterfaceKeys(feature, 'enum')
        self.mapInterfaceKeys(feature, 'enumconstant')
        self.mapInterfaceKeys(feature, 'funcpointer')
        self.mapInterfaceKeys(feature, 'handle')
        self.mapInterfaceKeys(feature, 'include')
        self.mapInterfaceKeys(feature, 'struct')
        self.mapInterfaceKeys(feature, 'union')

    def endFile(self):
        super().endFile()

    def beginFeature(self, interface, emit):
        # Start processing in superclass
        OutputGenerator.beginFeature(self, interface, emit)

        # Add this feature to the list being tracked
        self.features.append( self.featureName )

    def endFeature(self):
        # Finish processing in superclass
        OutputGenerator.endFeature(self)

    def addName(self, dict, name, value):
        """Add a string entry to the dictionary, quoting it so it gets
           printed out correctly in self.endFile()."""
        dict[name] = value

    def addMapping(self, baseType, refType):
        """Add a mapping between types to mapDict.

        Only include API types, so we don't end up with a lot of useless uint32_t and void types."""
        if not self.apiName(baseType) or not self.apiName(refType):
            self.logMsg('diag', 'ScriptOutputGenerator::addMapping: IGNORE map from', baseType, '<->', refType)
            return

        self.logMsg('diag', 'ScriptOutputGenerator::addMapping: map from',
                    baseType, '<->', refType)

        if baseType not in self.mapDict:
            baseDict = {}
            self.mapDict[baseType] = baseDict
        else:
            baseDict = self.mapDict[baseType]
        if refType not in self.mapDict:
            refDict = {}
            self.mapDict[refType] = refDict
        else:
            refDict = self.mapDict[refType]

        baseDict[refType] = None
        refDict[baseType] = None

    def breakCheck(self, procname, name):
        """Debugging aid - call from procname to break on API 'name' if it
           matches logic in this call."""

        pat = 'VkExternalFenceFeatureFlagBits'
        if name[0:len(pat)] == pat:
            print('{}(name = {}) matches {}'.format(procname, name, pat))
            import pdb
            pdb.set_trace()

    def genType(self, typeinfo, name, alias):
        """Generate type.

        - For 'struct' or 'union' types, defer to genStruct() to
          add to the dictionary.
        - For 'bitmask' types, add the type name to the 'flags' dictionary,
          with the value being the corresponding 'enums' name defining
          the acceptable flag bits.
        - For 'enum' types, add the type name to the 'enums' dictionary,
          with the value being '@STOPHERE@' (because this case seems
          never to happen).
        - For 'funcpointer' types, add the type name to the 'funcpointers'
          dictionary.
        - For 'handle' and 'define' types, add the handle or #define name
          to the 'struct' dictionary, because that's how the spec sources
          tag these types even though they aren't structs."""
        OutputGenerator.genType(self, typeinfo, name, alias)

        typeElem = typeinfo.elem
        # If the type is a struct type, traverse the embedded <member> tags
        # generating a structure. Otherwise, emit the tag text.
        category = typeElem.get('category')

        # Add a typeCategory{} entry for the category of this type.
        self.addName(self.typeCategory, name, category)

        if category in ('struct', 'union'):
            self.genStruct(typeinfo, name, alias)
        else:
            if alias:
                # Add name -> alias mapping
                self.addName(self.alias, name, alias)

                # Always emit an alias (?!)
                count = 1

                # May want to only emit full type definition when not an alias?
            else:
                # Extract the type name
                # (from self.genOpts). Copy other text through unchanged.
                # If the resulting text is an empty string, don't emit it.
                count = len(noneStr(typeElem.text))
                for elem in typeElem:
                    count += len(noneStr(elem.text)) + len(noneStr(elem.tail))

            if count > 0:
                if category == 'bitmask':
                    requiredEnum = typeElem.get('requires')
                    self.addName(self.flags, name, requiredEnum)

                    # This happens when the Flags type is defined, but no
                    # FlagBits are defined yet.
                    if requiredEnum is not None:
                        self.addMapping(name, requiredEnum)
                elif category == 'enum':
                    # This case does not seem to come up. It nominally would
                    # result from
                    #   <type name="Something" category="enum"/>,
                    # but the output generator doesn't emit them directly.
                    self.logMsg('warn', 'ScriptOutputGenerator::genType: invalid \'enum\' category for name:', name)
                elif category == 'funcpointer':
                    self.funcpointers[name] = None
                elif category == 'handle':
                    self.handles[name] = None
                elif category == 'define':
                    self.defines[name] = None
                elif category == 'basetype':
                    # Don't add an entry for base types that are not API types
                    # e.g. an API Bool type gets an entry, uint32_t does not
                    if self.apiName(name):
                        self.basetypes[name] = None
                        self.addName(self.typeCategory, name, 'basetype')
                    else:
                        self.logMsg('diag', 'ScriptOutputGenerator::genType: unprocessed type:', name, 'category:', category)
            else:
                self.logMsg('diag', 'ScriptOutputGenerator::genType: unprocessed type:', name)

    def genStruct(self, typeinfo, typeName, alias):
        """Generate struct (e.g. C "struct" type).

        Add the struct name to the 'structs' dictionary, with the
        value being an ordered list of the struct member names."""
        OutputGenerator.genStruct(self, typeinfo, typeName, alias)

        if alias:
            # Add name -> alias mapping
            self.addName(self.alias, typeName, alias)
        else:
            # May want to only emit definition on this branch
            True

        members = [member.text for member in typeinfo.elem.findall('.//member/name')]
        self.structs[typeName] = members
        memberTypes = [member.text for member in typeinfo.elem.findall('.//member/type')]
        for member_type in memberTypes:
            self.addMapping(typeName, member_type)

    def genGroup(self, groupinfo, groupName, alias):
        """Generate group (e.g. C "enum" type).

        These are concatenated together with other types.

        - Add the enum type name to the 'enums' dictionary, with
          the value being an ordered list of the enumerant names.
        - Add each enumerant name to the 'consts' dictionary, with
          the value being the enum type the enumerant is part of."""
        OutputGenerator.genGroup(self, groupinfo, groupName, alias)
        groupElem = groupinfo.elem

        # Add a typeCategory{} entry for the category of this type.
        self.addName(self.typeCategory, groupName, 'group')

        if alias:
            # Add name -> alias mapping
            self.addName(self.alias, groupName, alias)
        else:
            # May want to only emit definition on this branch
            True

        # Add each nested 'enum' tag
        enumerants = [elem.get('name') for elem in groupElem.findall('enum')]
        for name in enumerants:
            self.addName(self.consts, name, groupName)

        # Sort enums for output stability, since their order is irrelevant
        self.enums[groupName] = sorted(enumerants)

    def genEnum(self, enuminfo, name, alias):
        """Generate enumerant (compile-time constants).

        - Add the constant name to the 'consts' dictionary, with the
          value being None to indicate that the constant isn't
          an enumeration value."""
        OutputGenerator.genEnum(self, enuminfo, name, alias)

        if name not in self.consts:
            # Add a typeCategory{} entry for the category of this type.
            self.addName(self.typeCategory, name, 'consts')
            self.consts[name] = None
        # Otherwise, don't add it to the consts dictionary because it's
        # already present. This happens due to the generator 'reparentEnums'
        # parameter being False, so each extension enum appears in both the
        # <enums> type and in the <extension> or <feature> it originally
        # came from.

    def genCmd(self, cmdinfo, name, alias):
        """Generate command.

        - Add the command name to the 'protos' dictionary, with the
          value being an ordered list of the parameter names."""
        OutputGenerator.genCmd(self, cmdinfo, name, alias)

        # Add a typeCategory{} entry for the category of this type.
        self.addName(self.typeCategory, name, 'protos')

        if alias:
            # Add name -> alias mapping
            self.addName(self.alias, name, alias)
        else:
            # May want to only emit definition on this branch
            True

        params = [param.text for param in cmdinfo.elem.findall('param/name')]
        self.protos[name] = params
        paramTypes = [param.text for param in cmdinfo.elem.findall('param/type')]
        for param_type in paramTypes:
            self.addMapping(name, param_type)

    def createInverseMap(self):
        """This creates the inverse mapping of nonexistent APIs in this
           build to their aliases which are supported. Must be called by
           language-specific subclasses before emitting that mapping."""

        # Map from APIs not supported in this build to aliases that are.
        # When there are multiple valid choices for remapping, choose the
        # most-official suffixed one (KHR > EXT > vendor).
        for key in self.alias:
            # If the API key is aliased to something which doesn't exist,
            # then add the thing that doesn't exist to the nonexistent map.
            # This is used in spec macros to make promoted extension links
            # in specs built without the promoted interface refer to the
            # older interface instead.

            invkey = self.alias[key]

            if invkey not in self.typeCategory:
                if invkey in self.nonexistent:
                    # Potentially remap existing mapping to a more official
                    # alias.
                    self.nonexistent[invkey] = mostOfficial(self.nonexistent[invkey], key)
                else:
                    # Create remapping to an alias
                    self.nonexistent[invkey] = key