#!/usr/bin/env python # TODO handle overloads import libxml2 import sys doc = libxml2.parseFile("prototypes.xml") def dictFromElements(list, tagName): result = {} for item in list: if tagName is None or item.name == tagName: result[item.prop("id")] = item return(result) def findByName(name, d): for k, v in d.items(): if v.prop("name") == name: return k return None def findValueByName(name, d): r = findByName(name, d) if r is not None: return d[r] else: return None keyed = dictFromElements(doc.children, None) def getMembers(node): members = node.prop("members") if members is None: return [] else: return [keyed[k] for k in members.split(" ") if k != ""] def getMember(node, name): for m in getMembers(node): if m.prop("name") == name: return m return None namespaces = dictFromElements(doc.children, "Namespace") methods = dictFromElements(doc.children, "Method") classes = dictFromElements(doc.children, "Class") structs = dictFromElements(doc.children, "Struct") operatorMethods = dictFromElements(doc.children, "OperatorMethod") fields = dictFromElements(doc.children, "Field") constructors = dictFromElements(doc.children, "Constructor") destructors = dictFromElements(doc.children, "Destructor") globalNamespace = findValueByName("::", namespaces) def visit(indentation, node): nameAttr = node.prop("name") or "unknown" # FIXME nameAttr = nameAttr.replace("_", "Z") if node.name == "OperatorMethod": nameAttr = "(%s)" % (nameAttr, ) if node.name in ["Function", "Method", "OperatorMethod", "Constructor", "Destructor"]: mangled = node.prop("mangled") if node.prop("access") == "public": print indentation*" ", ("let %s %s := " % (nameAttr, node.name)), print "requireSharedLibrary filename 'C???? \"%s\" in " % (mangled, ) elif node.name in ["Field", "Variable"]: # TODO maybe auto-generate accessors? pass else: print indentation*" ", ("let %s %s := " % (nameAttr, node.name)), print "(" for member in getMembers(node): visit(indentation + 1, member) print indentation*" ", ") in " llvmNamespace = getMember(globalNamespace, "llvm") visit(0, llvmNamespace)