• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3 -i
2#
3# Copyright (c) 2013-2016 The Khronos Group Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import io,os,re,string,sys,copy
18import xml.etree.ElementTree as etree
19
20# matchAPIProfile - returns whether an API and profile
21#   being generated matches an element's profile
22# api - string naming the API to match
23# profile - string naming the profile to match
24# elem - Element which (may) have 'api' and 'profile'
25#   attributes to match to.
26# If a tag is not present in the Element, the corresponding API
27#   or profile always matches.
28# Otherwise, the tag must exactly match the API or profile.
29# Thus, if 'profile' = core:
30#   <remove> with no attribute will match
31#   <remove profile='core'> will match
32#   <remove profile='compatibility'> will not match
33# Possible match conditions:
34#   Requested   Element
35#   Profile     Profile
36#   ---------   --------
37#   None        None        Always matches
38#   'string'    None        Always matches
39#   None        'string'    Does not match. Can't generate multiple APIs
40#                           or profiles, so if an API/profile constraint
41#                           is present, it must be asked for explicitly.
42#   'string'    'string'    Strings must match
43#
44#   ** In the future, we will allow regexes for the attributes,
45#   not just strings, so that api="^(gl|gles2)" will match. Even
46#   this isn't really quite enough, we might prefer something
47#   like "gl(core)|gles1(common-lite)".
48def matchAPIProfile(api, profile, elem):
49    """Match a requested API & profile name to a api & profile attributes of an Element"""
50    match = True
51    # Match 'api', if present
52    if ('api' in elem.attrib):
53        if (api == None):
54            raise UserWarning("No API requested, but 'api' attribute is present with value '" +
55                              elem.get('api') + "'")
56        elif (api != elem.get('api')):
57            # Requested API doesn't match attribute
58            return False
59    if ('profile' in elem.attrib):
60        if (profile == None):
61            raise UserWarning("No profile requested, but 'profile' attribute is present with value '" +
62                elem.get('profile') + "'")
63        elif (profile != elem.get('profile')):
64            # Requested profile doesn't match attribute
65            return False
66    return True
67
68# BaseInfo - base class for information about a registry feature
69# (type/group/enum/command/API/extension).
70#   required - should this feature be defined during header generation
71#     (has it been removed by a profile or version)?
72#   declared - has this feature been defined already?
73#   elem - etree Element for this feature
74#   resetState() - reset required/declared to initial values. Used
75#     prior to generating a new API interface.
76class BaseInfo:
77    """Represents the state of a registry feature, used during API generation"""
78    def __init__(self, elem):
79        self.required = False
80        self.declared = False
81        self.elem = elem
82    def resetState(self):
83        self.required = False
84        self.declared = False
85
86# TypeInfo - registry information about a type. No additional state
87#   beyond BaseInfo is required.
88class TypeInfo(BaseInfo):
89    """Represents the state of a registry type"""
90    def __init__(self, elem):
91        BaseInfo.__init__(self, elem)
92        self.additionalValidity = []
93        self.removedValidity = []
94    def resetState(self):
95        BaseInfo.resetState(self)
96        self.additionalValidity = []
97        self.removedValidity = []
98
99# GroupInfo - registry information about a group of related enums
100# in an <enums> block, generally corresponding to a C "enum" type.
101class GroupInfo(BaseInfo):
102    """Represents the state of a registry <enums> group"""
103    def __init__(self, elem):
104        BaseInfo.__init__(self, elem)
105
106# EnumInfo - registry information about an enum
107#   type - numeric type of the value of the <enum> tag
108#     ( '' for GLint, 'u' for GLuint, 'ull' for GLuint64 )
109class EnumInfo(BaseInfo):
110    """Represents the state of a registry enum"""
111    def __init__(self, elem):
112        BaseInfo.__init__(self, elem)
113        self.type = elem.get('type')
114        if (self.type == None):
115            self.type = ''
116
117# CmdInfo - registry information about a command
118class CmdInfo(BaseInfo):
119    """Represents the state of a registry command"""
120    def __init__(self, elem):
121        BaseInfo.__init__(self, elem)
122        self.additionalValidity = []
123        self.removedValidity = []
124    def resetState(self):
125        BaseInfo.resetState(self)
126        self.additionalValidity = []
127        self.removedValidity = []
128
129# FeatureInfo - registry information about an API <feature>
130# or <extension>
131#   name - feature name string (e.g. 'VK_KHR_surface')
132#   version - feature version number (e.g. 1.2). <extension>
133#     features are unversioned and assigned version number 0.
134#     ** This is confusingly taken from the 'number' attribute of <feature>.
135#        Needs fixing.
136#   number - extension number, used for ordering and for
137#     assigning enumerant offsets. <feature> features do
138#     not have extension numbers and are assigned number 0.
139#   category - category, e.g. VERSION or khr/vendor tag
140#   emit - has this feature been defined already?
141class FeatureInfo(BaseInfo):
142    """Represents the state of an API feature (version/extension)"""
143    def __init__(self, elem):
144        BaseInfo.__init__(self, elem)
145        self.name = elem.get('name')
146        # Determine element category (vendor). Only works
147        # for <extension> elements.
148        if (elem.tag == 'feature'):
149            self.category = 'VERSION'
150            self.version = elem.get('number')
151            self.number = "0"
152            self.supported = None
153        else:
154            self.category = self.name.split('_', 2)[1]
155            self.version = "0"
156            self.number = elem.get('number')
157            self.supported = elem.get('supported')
158        self.emit = False
159
160from generator import write, GeneratorOptions, OutputGenerator
161
162# Registry - object representing an API registry, loaded from an XML file
163# Members
164#   tree - ElementTree containing the root <registry>
165#   typedict - dictionary of TypeInfo objects keyed by type name
166#   groupdict - dictionary of GroupInfo objects keyed by group name
167#   enumdict - dictionary of EnumInfo objects keyed by enum name
168#   cmddict - dictionary of CmdInfo objects keyed by command name
169#   apidict - dictionary of <api> Elements keyed by API name
170#   extensions - list of <extension> Elements
171#   extdict - dictionary of <extension> Elements keyed by extension name
172#   gen - OutputGenerator object used to write headers / messages
173#   genOpts - GeneratorOptions object used to control which
174#     fetures to write and how to format them
175#   emitFeatures - True to actually emit features for a version / extension,
176#     or False to just treat them as emitted
177# Public methods
178#   loadElementTree(etree) - load registry from specified ElementTree
179#   loadFile(filename) - load registry from XML file
180#   setGenerator(gen) - OutputGenerator to use
181#   parseTree() - parse the registry once loaded & create dictionaries
182#   dumpReg(maxlen, filehandle) - diagnostic to dump the dictionaries
183#     to specified file handle (default stdout). Truncates type /
184#     enum / command elements to maxlen characters (default 80)
185#   generator(g) - specify the output generator object
186#   apiGen(apiname, genOpts) - generate API headers for the API type
187#     and profile specified in genOpts, but only for the versions and
188#     extensions specified there.
189#   apiReset() - call between calls to apiGen() to reset internal state
190# Private methods
191#   addElementInfo(elem,info,infoName,dictionary) - add feature info to dict
192#   lookupElementInfo(fname,dictionary) - lookup feature info in dict
193class Registry:
194    """Represents an API registry loaded from XML"""
195    def __init__(self):
196        self.tree         = None
197        self.typedict     = {}
198        self.groupdict    = {}
199        self.enumdict     = {}
200        self.cmddict      = {}
201        self.apidict      = {}
202        self.extensions   = []
203        self.extdict      = {}
204        # A default output generator, so commands prior to apiGen can report
205        # errors via the generator object.
206        self.gen          = OutputGenerator()
207        self.genOpts      = None
208        self.emitFeatures = False
209    def loadElementTree(self, tree):
210        """Load ElementTree into a Registry object and parse it"""
211        self.tree = tree
212        self.parseTree()
213    def loadFile(self, file):
214        """Load an API registry XML file into a Registry object and parse it"""
215        self.tree = etree.parse(file)
216        self.parseTree()
217    def setGenerator(self, gen):
218        """Specify output generator object. None restores the default generator"""
219        self.gen = gen
220        self.gen.setRegistry(self)
221
222    # addElementInfo - add information about an element to the
223    # corresponding dictionary
224    #   elem - <type>/<enums>/<enum>/<command>/<feature>/<extension> Element
225    #   info - corresponding {Type|Group|Enum|Cmd|Feature}Info object
226    #   infoName - 'type' / 'group' / 'enum' / 'command' / 'feature' / 'extension'
227    #   dictionary - self.{type|group|enum|cmd|api|ext}dict
228    # If the Element has an 'api' attribute, the dictionary key is the
229    # tuple (name,api). If not, the key is the name. 'name' is an
230    # attribute of the Element
231    def addElementInfo(self, elem, info, infoName, dictionary):
232        if ('api' in elem.attrib):
233            key = (elem.get('name'),elem.get('api'))
234        else:
235            key = elem.get('name')
236        if key in dictionary:
237            self.gen.logMsg('warn', '*** Attempt to redefine',
238                            infoName, 'with key:', key)
239        else:
240            dictionary[key] = info
241    #
242    # lookupElementInfo - find a {Type|Enum|Cmd}Info object by name.
243    # If an object qualified by API name exists, use that.
244    #   fname - name of type / enum / command
245    #   dictionary - self.{type|enum|cmd}dict
246    def lookupElementInfo(self, fname, dictionary):
247        key = (fname, self.genOpts.apiname)
248        if (key in dictionary):
249            # self.gen.logMsg('diag', 'Found API-specific element for feature', fname)
250            return dictionary[key]
251        elif (fname in dictionary):
252            # self.gen.logMsg('diag', 'Found generic element for feature', fname)
253            return dictionary[fname]
254        else:
255            return None
256    def parseTree(self):
257        """Parse the registry Element, once created"""
258        # This must be the Element for the root <registry>
259        self.reg = self.tree.getroot()
260        #
261        # Create dictionary of registry types from toplevel <types> tags
262        # and add 'name' attribute to each <type> tag (where missing)
263        # based on its <name> element.
264        #
265        # There's usually one <types> block; more are OK
266        # Required <type> attributes: 'name' or nested <name> tag contents
267        self.typedict = {}
268        for type in self.reg.findall('types/type'):
269            # If the <type> doesn't already have a 'name' attribute, set
270            # it from contents of its <name> tag.
271            if (type.get('name') == None):
272                type.attrib['name'] = type.find('name').text
273            self.addElementInfo(type, TypeInfo(type), 'type', self.typedict)
274        #
275        # Create dictionary of registry enum groups from <enums> tags.
276        #
277        # Required <enums> attributes: 'name'. If no name is given, one is
278        # generated, but that group can't be identified and turned into an
279        # enum type definition - it's just a container for <enum> tags.
280        self.groupdict = {}
281        for group in self.reg.findall('enums'):
282            self.addElementInfo(group, GroupInfo(group), 'group', self.groupdict)
283        #
284        # Create dictionary of registry enums from <enum> tags
285        #
286        # <enums> tags usually define different namespaces for the values
287        #   defined in those tags, but the actual names all share the
288        #   same dictionary.
289        # Required <enum> attributes: 'name', 'value'
290        # For containing <enums> which have type="enum" or type="bitmask",
291        # tag all contained <enum>s are required. This is a stopgap until
292        # a better scheme for tagging core and extension enums is created.
293        self.enumdict = {}
294        for enums in self.reg.findall('enums'):
295            required = (enums.get('type') != None)
296            for enum in enums.findall('enum'):
297                enumInfo = EnumInfo(enum)
298                enumInfo.required = required
299                self.addElementInfo(enum, enumInfo, 'enum', self.enumdict)
300        #
301        # Create dictionary of registry commands from <command> tags
302        # and add 'name' attribute to each <command> tag (where missing)
303        # based on its <proto><name> element.
304        #
305        # There's usually only one <commands> block; more are OK.
306        # Required <command> attributes: 'name' or <proto><name> tag contents
307        self.cmddict = {}
308        for cmd in self.reg.findall('commands/command'):
309            # If the <command> doesn't already have a 'name' attribute, set
310            # it from contents of its <proto><name> tag.
311            if (cmd.get('name') == None):
312                cmd.attrib['name'] = cmd.find('proto/name').text
313            ci = CmdInfo(cmd)
314            self.addElementInfo(cmd, ci, 'command', self.cmddict)
315        #
316        # Create dictionaries of API and extension interfaces
317        #   from toplevel <api> and <extension> tags.
318        #
319        self.apidict = {}
320        for feature in self.reg.findall('feature'):
321            featureInfo = FeatureInfo(feature)
322            self.addElementInfo(feature, featureInfo, 'feature', self.apidict)
323        self.extensions = self.reg.findall('extensions/extension')
324        self.extdict = {}
325        for feature in self.extensions:
326            featureInfo = FeatureInfo(feature)
327            self.addElementInfo(feature, featureInfo, 'extension', self.extdict)
328
329            # Add additional enums defined only in <extension> tags
330            # to the corresponding core type.
331            # When seen here, the <enum> element, processed to contain the
332            # numeric enum value, is added to the corresponding <enums>
333            # element, as well as adding to the enum dictionary. It is
334            # *removed* from the <require> element it is introduced in.
335            # Not doing this will cause spurious genEnum()
336            # calls to be made in output generation, and it's easier
337            # to handle here than in genEnum().
338            #
339            # In lxml.etree, an Element can have only one parent, so the
340            # append() operation also removes the element. But in Python's
341            # ElementTree package, an Element can have multiple parents. So
342            # it must be explicitly removed from the <require> tag, leading
343            # to the nested loop traversal of <require>/<enum> elements
344            # below.
345            #
346            # This code also adds a 'extnumber' attribute containing the
347            # extension number, used for enumerant value calculation.
348            #
349            # For <enum> tags which are actually just constants, if there's
350            # no 'extends' tag but there is a 'value' or 'bitpos' tag, just
351            # add an EnumInfo record to the dictionary. That works because
352            # output generation of constants is purely dependency-based, and
353            # doesn't need to iterate through the XML tags.
354            #
355            # Something like this will need to be done for 'feature's up
356            # above, if we use the same mechanism for adding to the core
357            # API in 1.1.
358            #
359            for elem in feature.findall('require'):
360              for enum in elem.findall('enum'):
361                addEnumInfo = False
362                groupName = enum.get('extends')
363                if (groupName != None):
364                    # self.gen.logMsg('diag', '*** Found extension enum',
365                    #     enum.get('name'))
366                    # Add extension number attribute to the <enum> element
367                    enum.attrib['extnumber'] = featureInfo.number
368                    enum.attrib['extname'] = featureInfo.name
369                    enum.attrib['supported'] = featureInfo.supported
370                    # Look up the GroupInfo with matching groupName
371                    if (groupName in self.groupdict.keys()):
372                        # self.gen.logMsg('diag', '*** Matching group',
373                        #     groupName, 'found, adding element...')
374                        gi = self.groupdict[groupName]
375                        gi.elem.append(enum)
376                        # Remove element from parent <require> tag
377                        # This should be a no-op in lxml.etree
378                        elem.remove(enum)
379                    else:
380                        self.gen.logMsg('warn', '*** NO matching group',
381                            groupName, 'for enum', enum.get('name'), 'found.')
382                    addEnumInfo = True
383                elif (enum.get('value') or enum.get('bitpos')):
384                    # self.gen.logMsg('diag', '*** Adding extension constant "enum"',
385                    #     enum.get('name'))
386                    addEnumInfo = True
387                if (addEnumInfo):
388                    enumInfo = EnumInfo(enum)
389                    self.addElementInfo(enum, enumInfo, 'enum', self.enumdict)
390    def dumpReg(self, maxlen = 40, filehandle = sys.stdout):
391        """Dump all the dictionaries constructed from the Registry object"""
392        write('***************************************', file=filehandle)
393        write('    ** Dumping Registry contents **',     file=filehandle)
394        write('***************************************', file=filehandle)
395        write('// Types', file=filehandle)
396        for name in self.typedict:
397            tobj = self.typedict[name]
398            write('    Type', name, '->', etree.tostring(tobj.elem)[0:maxlen], file=filehandle)
399        write('// Groups', file=filehandle)
400        for name in self.groupdict:
401            gobj = self.groupdict[name]
402            write('    Group', name, '->', etree.tostring(gobj.elem)[0:maxlen], file=filehandle)
403        write('// Enums', file=filehandle)
404        for name in self.enumdict:
405            eobj = self.enumdict[name]
406            write('    Enum', name, '->', etree.tostring(eobj.elem)[0:maxlen], file=filehandle)
407        write('// Commands', file=filehandle)
408        for name in self.cmddict:
409            cobj = self.cmddict[name]
410            write('    Command', name, '->', etree.tostring(cobj.elem)[0:maxlen], file=filehandle)
411        write('// APIs', file=filehandle)
412        for key in self.apidict:
413            write('    API Version ', key, '->',
414                etree.tostring(self.apidict[key].elem)[0:maxlen], file=filehandle)
415        write('// Extensions', file=filehandle)
416        for key in self.extdict:
417            write('    Extension', key, '->',
418                etree.tostring(self.extdict[key].elem)[0:maxlen], file=filehandle)
419        # write('***************************************', file=filehandle)
420        # write('    ** Dumping XML ElementTree **', file=filehandle)
421        # write('***************************************', file=filehandle)
422        # write(etree.tostring(self.tree.getroot(),pretty_print=True), file=filehandle)
423    #
424    # typename - name of type
425    # required - boolean (to tag features as required or not)
426    def markTypeRequired(self, typename, required):
427        """Require (along with its dependencies) or remove (but not its dependencies) a type"""
428        self.gen.logMsg('diag', '*** tagging type:', typename, '-> required =', required)
429        # Get TypeInfo object for <type> tag corresponding to typename
430        type = self.lookupElementInfo(typename, self.typedict)
431        if (type != None):
432            if (required):
433                # Tag type dependencies in 'required' attributes as
434                # required. This DOES NOT un-tag dependencies in a <remove>
435                # tag. See comments in markRequired() below for the reason.
436                if ('requires' in type.elem.attrib):
437                    depType = type.elem.get('requires')
438                    self.gen.logMsg('diag', '*** Generating dependent type',
439                        depType, 'for type', typename)
440                    self.markTypeRequired(depType, required)
441                # Tag types used in defining this type (e.g. in nested
442                # <type> tags)
443                # Look for <type> in entire <command> tree,
444                # not just immediate children
445                for subtype in type.elem.findall('.//type'):
446                    self.gen.logMsg('diag', '*** markRequired: type requires dependent <type>', subtype.text)
447                    self.markTypeRequired(subtype.text, required)
448                # Tag enums used in defining this type, for example in
449                #   <member><name>member</name>[<enum>MEMBER_SIZE</enum>]</member>
450                for subenum in type.elem.findall('.//enum'):
451                    self.gen.logMsg('diag', '*** markRequired: type requires dependent <enum>', subenum.text)
452                    self.markEnumRequired(subenum.text, required)
453            type.required = required
454        else:
455            self.gen.logMsg('warn', '*** type:', typename , 'IS NOT DEFINED')
456    #
457    # enumname - name of enum
458    # required - boolean (to tag features as required or not)
459    def markEnumRequired(self, enumname, required):
460        self.gen.logMsg('diag', '*** tagging enum:', enumname, '-> required =', required)
461        enum = self.lookupElementInfo(enumname, self.enumdict)
462        if (enum != None):
463            enum.required = required
464        else:
465            self.gen.logMsg('warn', '*** enum:', enumname , 'IS NOT DEFINED')
466    #
467    # features - Element for <require> or <remove> tag
468    # required - boolean (to tag features as required or not)
469    def markRequired(self, features, required):
470        """Require or remove features specified in the Element"""
471        self.gen.logMsg('diag', '*** markRequired (features = <too long to print>, required =', required, ')')
472        # Loop over types, enums, and commands in the tag
473        # @@ It would be possible to respect 'api' and 'profile' attributes
474        #  in individual features, but that's not done yet.
475        for typeElem in features.findall('type'):
476            self.markTypeRequired(typeElem.get('name'), required)
477        for enumElem in features.findall('enum'):
478            self.markEnumRequired(enumElem.get('name'), required)
479        for cmdElem in features.findall('command'):
480            name = cmdElem.get('name')
481            self.gen.logMsg('diag', '*** tagging command:', name, '-> required =', required)
482            cmd = self.lookupElementInfo(name, self.cmddict)
483            if (cmd != None):
484                cmd.required = required
485                # Tag all parameter types of this command as required.
486                # This DOES NOT remove types of commands in a <remove>
487                # tag, because many other commands may use the same type.
488                # We could be more clever and reference count types,
489                # instead of using a boolean.
490                if (required):
491                    # Look for <type> in entire <command> tree,
492                    # not just immediate children
493                    for type in cmd.elem.findall('.//type'):
494                        self.gen.logMsg('diag', '*** markRequired: command implicitly requires dependent type', type.text)
495                        self.markTypeRequired(type.text, required)
496            else:
497                self.gen.logMsg('warn', '*** command:', name, 'IS NOT DEFINED')
498    #
499    # interface - Element for <version> or <extension>, containing
500    #   <require> and <remove> tags
501    # api - string specifying API name being generated
502    # profile - string specifying API profile being generated
503    def requireAndRemoveFeatures(self, interface, api, profile):
504        """Process <recquire> and <remove> tags for a <version> or <extension>"""
505        # <require> marks things that are required by this version/profile
506        for feature in interface.findall('require'):
507            if (matchAPIProfile(api, profile, feature)):
508                self.markRequired(feature,True)
509        # <remove> marks things that are removed by this version/profile
510        for feature in interface.findall('remove'):
511            if (matchAPIProfile(api, profile, feature)):
512                self.markRequired(feature,False)
513
514    def assignAdditionalValidity(self, interface, api, profile):
515        #
516        # Loop over all usage inside all <require> tags.
517        for feature in interface.findall('require'):
518            if (matchAPIProfile(api, profile, feature)):
519                for v in feature.findall('usage'):
520                    if v.get('command'):
521                        self.cmddict[v.get('command')].additionalValidity.append(copy.deepcopy(v))
522                    if v.get('struct'):
523                        self.typedict[v.get('struct')].additionalValidity.append(copy.deepcopy(v))
524
525        #
526        # Loop over all usage inside all <remove> tags.
527        for feature in interface.findall('remove'):
528            if (matchAPIProfile(api, profile, feature)):
529                for v in feature.findall('usage'):
530                    if v.get('command'):
531                        self.cmddict[v.get('command')].removedValidity.append(copy.deepcopy(v))
532                    if v.get('struct'):
533                        self.typedict[v.get('struct')].removedValidity.append(copy.deepcopy(v))
534
535    #
536    # generateFeature - generate a single type / enum group / enum / command,
537    # and all its dependencies as needed.
538    #   fname - name of feature (<type>/<enum>/<command>)
539    #   ftype - type of feature, 'type' | 'enum' | 'command'
540    #   dictionary - of *Info objects - self.{type|enum|cmd}dict
541    def generateFeature(self, fname, ftype, dictionary):
542        f = self.lookupElementInfo(fname, dictionary)
543        if (f == None):
544            # No such feature. This is an error, but reported earlier
545            self.gen.logMsg('diag', '*** No entry found for feature', fname,
546                            'returning!')
547            return
548        #
549        # If feature isn't required, or has already been declared, return
550        if (not f.required):
551            self.gen.logMsg('diag', '*** Skipping', ftype, fname, '(not required)')
552            return
553        if (f.declared):
554            self.gen.logMsg('diag', '*** Skipping', ftype, fname, '(already declared)')
555            return
556        # Always mark feature declared, as though actually emitted
557        f.declared = True
558        #
559        # Pull in dependent declaration(s) of the feature.
560        # For types, there may be one type in the 'required' attribute of
561        #   the element, as well as many in imbedded <type> and <enum> tags
562        #   within the element.
563        # For commands, there may be many in <type> tags within the element.
564        # For enums, no dependencies are allowed (though perhaps if you
565        #   have a uint64 enum, it should require that type).
566        genProc = None
567        if (ftype == 'type'):
568            genProc = self.gen.genType
569            if ('requires' in f.elem.attrib):
570                depname = f.elem.get('requires')
571                self.gen.logMsg('diag', '*** Generating required dependent type',
572                                depname)
573                self.generateFeature(depname, 'type', self.typedict)
574            for subtype in f.elem.findall('.//type'):
575                self.gen.logMsg('diag', '*** Generating required dependent <type>',
576                    subtype.text)
577                self.generateFeature(subtype.text, 'type', self.typedict)
578            for subtype in f.elem.findall('.//enum'):
579                self.gen.logMsg('diag', '*** Generating required dependent <enum>',
580                    subtype.text)
581                self.generateFeature(subtype.text, 'enum', self.enumdict)
582            # If the type is an enum group, look up the corresponding
583            # group in the group dictionary and generate that instead.
584            if (f.elem.get('category') == 'enum'):
585                self.gen.logMsg('diag', '*** Type', fname, 'is an enum group, so generate that instead')
586                group = self.lookupElementInfo(fname, self.groupdict)
587                if (group == None):
588                    # Unless this is tested for, it's probably fatal to call below
589                    genProc = None
590                    self.logMsg('warn', '*** NO MATCHING ENUM GROUP FOUND!!!')
591                else:
592                    genProc = self.gen.genGroup
593                    f = group
594        elif (ftype == 'command'):
595            genProc = self.gen.genCmd
596            for type in f.elem.findall('.//type'):
597                depname = type.text
598                self.gen.logMsg('diag', '*** Generating required parameter type',
599                                depname)
600                self.generateFeature(depname, 'type', self.typedict)
601        elif (ftype == 'enum'):
602            genProc = self.gen.genEnum
603        # Actually generate the type only if emitting declarations
604        if self.emitFeatures:
605            self.gen.logMsg('diag', '*** Emitting', ftype, 'decl for', fname)
606            genProc(f, fname)
607        else:
608            self.gen.logMsg('diag', '*** Skipping', ftype, fname,
609                            '(not emitting this feature)')
610    #
611    # generateRequiredInterface - generate all interfaces required
612    # by an API version or extension
613    #   interface - Element for <version> or <extension>
614    def generateRequiredInterface(self, interface):
615        """Generate required C interface for specified API version/extension"""
616
617        #
618        # Loop over all features inside all <require> tags.
619        for features in interface.findall('require'):
620            for t in features.findall('type'):
621                self.generateFeature(t.get('name'), 'type', self.typedict)
622            for e in features.findall('enum'):
623                self.generateFeature(e.get('name'), 'enum', self.enumdict)
624            for c in features.findall('command'):
625                self.generateFeature(c.get('name'), 'command', self.cmddict)
626
627    #
628    # apiGen(genOpts) - generate interface for specified versions
629    #   genOpts - GeneratorOptions object with parameters used
630    #   by the Generator object.
631    def apiGen(self, genOpts):
632        """Generate interfaces for the specified API type and range of versions"""
633        #
634        self.gen.logMsg('diag', '*******************************************')
635        self.gen.logMsg('diag', '  Registry.apiGen file:', genOpts.filename,
636                        'api:', genOpts.apiname,
637                        'profile:', genOpts.profile)
638        self.gen.logMsg('diag', '*******************************************')
639        #
640        self.genOpts = genOpts
641        # Reset required/declared flags for all features
642        self.apiReset()
643        #
644        # Compile regexps used to select versions & extensions
645        regVersions = re.compile(self.genOpts.versions)
646        regEmitVersions = re.compile(self.genOpts.emitversions)
647        regAddExtensions = re.compile(self.genOpts.addExtensions)
648        regRemoveExtensions = re.compile(self.genOpts.removeExtensions)
649        #
650        # Get all matching API versions & add to list of FeatureInfo
651        features = []
652        apiMatch = False
653        for key in self.apidict:
654            fi = self.apidict[key]
655            api = fi.elem.get('api')
656            if (api == self.genOpts.apiname):
657                apiMatch = True
658                if (regVersions.match(fi.version)):
659                    # Matches API & version #s being generated. Mark for
660                    # emission and add to the features[] list .
661                    # @@ Could use 'declared' instead of 'emit'?
662                    fi.emit = (regEmitVersions.match(fi.version) != None)
663                    features.append(fi)
664                    if (not fi.emit):
665                        self.gen.logMsg('diag', '*** NOT tagging feature api =', api,
666                            'name =', fi.name, 'version =', fi.version,
667                            'for emission (does not match emitversions pattern)')
668                else:
669                    self.gen.logMsg('diag', '*** NOT including feature api =', api,
670                        'name =', fi.name, 'version =', fi.version,
671                        '(does not match requested versions)')
672            else:
673                self.gen.logMsg('diag', '*** NOT including feature api =', api,
674                    'name =', fi.name,
675                    '(does not match requested API)')
676        if (not apiMatch):
677            self.gen.logMsg('warn', '*** No matching API versions found!')
678        #
679        # Get all matching extensions, in order by their extension number,
680        # and add to the list of features.
681        # Start with extensions tagged with 'api' pattern matching the API
682        # being generated. Add extensions matching the pattern specified in
683        # regExtensions, then remove extensions matching the pattern
684        # specified in regRemoveExtensions
685        for (extName,ei) in sorted(self.extdict.items(),key = lambda x : x[1].number):
686            extName = ei.name
687            include = False
688            #
689            # Include extension if defaultExtensions is not None and if the
690            # 'supported' attribute matches defaultExtensions. The regexp in
691            # 'supported' must exactly match defaultExtensions, so bracket
692            # it with ^(pat)$.
693            pat = '^(' + ei.elem.get('supported') + ')$'
694            if (self.genOpts.defaultExtensions and
695                     re.match(pat, self.genOpts.defaultExtensions)):
696                self.gen.logMsg('diag', '*** Including extension',
697                    extName, "(defaultExtensions matches the 'supported' attribute)")
698                include = True
699            #
700            # Include additional extensions if the extension name matches
701            # the regexp specified in the generator options. This allows
702            # forcing extensions into an interface even if they're not
703            # tagged appropriately in the registry.
704            if (regAddExtensions.match(extName) != None):
705                self.gen.logMsg('diag', '*** Including extension',
706                    extName, '(matches explicitly requested extensions to add)')
707                include = True
708            # Remove extensions if the name matches the regexp specified
709            # in generator options. This allows forcing removal of
710            # extensions from an interface even if they're tagged that
711            # way in the registry.
712            if (regRemoveExtensions.match(extName) != None):
713                self.gen.logMsg('diag', '*** Removing extension',
714                    extName, '(matches explicitly requested extensions to remove)')
715                include = False
716            #
717            # If the extension is to be included, add it to the
718            # extension features list.
719            if (include):
720                ei.emit = True
721                features.append(ei)
722            else:
723                self.gen.logMsg('diag', '*** NOT including extension',
724                    extName, '(does not match api attribute or explicitly requested extensions)')
725        #
726        # Sort the extension features list, if a sort procedure is defined
727        if (self.genOpts.sortProcedure):
728            self.genOpts.sortProcedure(features)
729        #
730        # Pass 1: loop over requested API versions and extensions tagging
731        #   types/commands/features as required (in an <require> block) or no
732        #   longer required (in an <remove> block). It is possible to remove
733        #   a feature in one version and restore it later by requiring it in
734        #   a later version.
735        # If a profile other than 'None' is being generated, it must
736        #   match the profile attribute (if any) of the <require> and
737        #   <remove> tags.
738        self.gen.logMsg('diag', '*** PASS 1: TAG FEATURES ********************************************')
739        for f in features:
740            self.gen.logMsg('diag', '*** PASS 1: Tagging required and removed features for',
741                f.name)
742            self.requireAndRemoveFeatures(f.elem, self.genOpts.apiname, self.genOpts.profile)
743            self.assignAdditionalValidity(f.elem, self.genOpts.apiname, self.genOpts.profile)
744        #
745        # Pass 2: loop over specified API versions and extensions printing
746        #   declarations for required things which haven't already been
747        #   generated.
748        self.gen.logMsg('diag', '*** PASS 2: GENERATE INTERFACES FOR FEATURES ************************')
749        self.gen.beginFile(self.genOpts)
750        for f in features:
751            self.gen.logMsg('diag', '*** PASS 2: Generating interface for',
752                f.name)
753            emit = self.emitFeatures = f.emit
754            if (not emit):
755                self.gen.logMsg('diag', '*** PASS 2: NOT declaring feature',
756                    f.elem.get('name'), 'because it is not tagged for emission')
757            # Generate the interface (or just tag its elements as having been
758            # emitted, if they haven't been).
759            self.gen.beginFeature(f.elem, emit)
760            self.generateRequiredInterface(f.elem)
761            self.gen.endFeature()
762        self.gen.endFile()
763    #
764    # apiReset - use between apiGen() calls to reset internal state
765    #
766    def apiReset(self):
767        """Reset type/enum/command dictionaries before generating another API"""
768        for type in self.typedict:
769            self.typedict[type].resetState()
770        for enum in self.enumdict:
771            self.enumdict[enum].resetState()
772        for cmd in self.cmddict:
773            self.cmddict[cmd].resetState()
774        for cmd in self.apidict:
775            self.apidict[cmd].resetState()
776    #
777    # validateGroups - check that group= attributes match actual groups
778    #
779    def validateGroups(self):
780        """Validate group= attributes on <param> and <proto> tags"""
781        # Keep track of group names not in <group> tags
782        badGroup = {}
783        self.gen.logMsg('diag', '*** VALIDATING GROUP ATTRIBUTES ***')
784        for cmd in self.reg.findall('commands/command'):
785            proto = cmd.find('proto')
786            funcname = cmd.find('proto/name').text
787            if ('group' in proto.attrib.keys()):
788                group = proto.get('group')
789                # self.gen.logMsg('diag', '*** Command ', funcname, ' has return group ', group)
790                if (group not in self.groupdict.keys()):
791                    # self.gen.logMsg('diag', '*** Command ', funcname, ' has UNKNOWN return group ', group)
792                    if (group not in badGroup.keys()):
793                        badGroup[group] = 1
794                    else:
795                        badGroup[group] = badGroup[group] +  1
796            for param in cmd.findall('param'):
797                pname = param.find('name')
798                if (pname != None):
799                    pname = pname.text
800                else:
801                    pname = type.get('name')
802                if ('group' in param.attrib.keys()):
803                    group = param.get('group')
804                    if (group not in self.groupdict.keys()):
805                        # self.gen.logMsg('diag', '*** Command ', funcname, ' param ', pname, ' has UNKNOWN group ', group)
806                        if (group not in badGroup.keys()):
807                            badGroup[group] = 1
808                        else:
809                            badGroup[group] = badGroup[group] +  1
810        if (len(badGroup.keys()) > 0):
811            self.gen.logMsg('diag', '*** SUMMARY OF UNRECOGNIZED GROUPS ***')
812            for key in sorted(badGroup.keys()):
813                self.gen.logMsg('diag', '    ', key, ' occurred ', badGroup[key], ' times')
814