+ result += get_text_contents(c)
+ return result
+
+def parse_command(cmd):
+ proto = get_first_child(cmd, "proto")
+ name = get_text_contents(get_first_child(proto, "name"))
+ func = things.get(name)
+ if not func:
+ func = Function(name)
+ things[name] = func
+
+ aliases = get_nested_elements(cmd, "alias")
+ func.aliases = [a.getAttribute("name") for a in aliases]
+
+ ptype = get_first_child(proto, "ptype")
+ if ptype:
+ func.return_type = get_text_contents(ptype)
+ else:
+ for c in proto.childNodes:
+ if c.nodeType==xml.dom.Node.TEXT_NODE and c.data.strip():
+ func.return_type = c.data.strip()
+ break
+
+ params = get_nested_elements(cmd, "param")
+ func.params = map(get_text_contents, params)
+
+def parse_enum(en):
+ name = en.getAttribute("name")
+ enum = things.get(name)
+ if not enum:
+ enum = Enum(name)
+ things[name] = enum
+
+ enum.value = int(en.getAttribute("value"), 16)
+
+def parse_feature(feat):
+ api = feat.getAttribute("api")
+ version = feat.getAttribute("number")
+ if version:
+ version = map(int, version.split('.'))
+ else:
+ version = None
+
+ requires = get_nested_elements(feat, "require")
+ for req in requires:
+ commands = get_nested_elements(req, "command")
+ enums = get_nested_elements(req, "enum")
+ for t in itertools.chain(commands, enums):
+ name = t.getAttribute("name")
+ thing = things.get(name)
+ if thing:
+ thing.supported_apis.setdefault(api, version)
+
+ if not api or api==target_api:
+ removes = get_nested_elements(feat, "remove")
+ for rem in removes:
+ profile = rem.getAttribute("profile")
+ commands = get_nested_elements(rem, "command")
+ enums = get_nested_elements(rem, "enum")
+
+ for t in itertools.chain(commands, enums):
+ name = t.getAttribute("name")
+ if profile!="core" and name in things:
+ del things[name]
+
+def parse_extension(ext):
+ ext_name = ext.getAttribute("name")
+ if ext_name.startswith("GL_"):
+ ext_name = ext_name[3:]
+
+ supported = ext.getAttribute("supported").split('|')
+ if target_api not in supported and ext_name!=target_ext:
+ return
+
+ extension = extensions.get(ext_name)
+ if not extension:
+ extension = Extension(ext_name)
+ extensions[ext_name] = extension
+
+ extension.supported_apis = supported
+
+ requires = get_nested_elements(ext, "require")
+ for req in requires:
+ api = req.getAttribute("api")
+ if api:
+ supported = [api]
+ else:
+ supported = extension.supported_apis
+
+ commands = get_nested_elements(req, "command")
+ enums = get_nested_elements(req, "enum")
+ for t in itertools.chain(commands, enums):
+ name = t.getAttribute("name")
+ thing = things.get(name)
+ if thing:
+ if thing.extension and extension.name!=target_ext:
+ if thing.extension.ext_type=="ARB" or thing.extension.name==target_ext:
+ continue
+ if thing.extension.ext_type=="EXT" and extension.ext_type!="ARB":
+ continue
+
+ thing.extension = extension
+ for a in supported:
+ thing.supported_apis.setdefault(a, "ext")
+
+def parse_file(fn):
+ doc = xml.dom.minidom.parse(fn)
+ root = doc.documentElement
+
+ commands = get_nested_elements(root, "commands/command")
+ for cmd in commands:
+ parse_command(cmd)
+
+ enums = get_nested_elements(root, "enums/enum")
+ for en in enums:
+ parse_enum(en)
+
+ features = get_nested_elements(root, "feature")
+ for feat in features:
+ parse_feature(feat)
+
+ extensions = get_nested_elements(root, "extensions/extension")
+ for ext in extensions:
+ parse_extension(ext)
+
+parse_file("gl.xml")
+parse_file("gl.fixes.xml")
+
+### Additional processing ###
+
+if target_ext in extensions:
+ target_ext = extensions[target_ext]
+else:
+ print "Extension %s not found"%target_ext
+ sys.exit(1)
+
+# Find aliases for enums
+enums = [t for t in things.itervalues() if t.kind==Thing.ENUM]
+core_enums = [e for e in enums if any(v!="ext" for v in e.supported_apis.itervalues())]
+core_enums_by_value = dict((e.value, None) for e in core_enums)
+
+def get_key_api(things):
+ common_apis = set(target_ext.supported_apis)
+ for t in things:
+ common_apis.intersection_update(t.supported_apis.keys())
+ if common_apis:
+ return common_apis.pop()
+ else:
+ return target_api
+
+for e in enums:
+ if all(v=="ext" for v in e.supported_apis.values()) and e.value in core_enums_by_value:
+ if core_enums_by_value[e.value] is None:
+ candidates = [ce for ce in core_enums if ce.value==e.value]
+ key_api = get_key_api(candidates)
+ core_enums_by_value[e.value] = list(sorted(candidates, key=(lambda x: x.supported_apis.get(key_api, "ext"))))
+ for ce in core_enums_by_value[e.value]:
+ if ce.bitmask==e.bitmask:
+ e.aliases.append(ce.name)
+ break
+
+# Create references from core things to their extension counterparts
+for t in things.itervalues():
+ if t.extension:
+ for a in t.aliases:
+ alias = things.get(a)
+ if alias:
+ if target_api in t.supported_apis:
+ alias.sources.insert(0, t)