dom/bindings/Codegen.py
author Boris Zbarsky <bzbarsky@mit.edu>
Tue, 07 Feb 2012 15:55:55 -0500
changeset 86011 9a987b9287c584246387b9470211b0bf2eba2312
parent 86010 61b7ed2e15b26d8ae45d8e617e2f1cfa1d438b34
child 86012 c22458a8e35d46f417c1b1938444c69f6d9fb2e5
permissions -rw-r--r--
Move all the wrap template getting into the helper function

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

# Common codegen classes.

import os
import string

from WebIDL import *

class CGThing():
    """
    Abstract base case for things that spit out code.
    """
    def __init__(self):
        pass # Nothing for now
    def declare(self):
        """Produce code for a header file."""
        assert(False)  # Override me!
    def define(self):
        """Produce code for a cpp file."""
        assert(False) # Override me!

class CGDOMJSClass(CGThing):
    def __init__(self, descriptor):
        CGThing.__init__(self)
        self.descriptor = descriptor
    def declare(self):
        return "  extern DOMJSClass Class;\n"
    def define(self):
        prototypeChainString = ', '.join(['id::' + proto \
                                          for proto in self.descriptor.prototypeChain])
        return """
DOMJSClass Class = {
  { "%s",
    JSCLASS_IS_DOMJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(1),
    JS_PropertyStub,       /* addProperty */
    JS_PropertyStub,       /* delProperty */
    JS_PropertyStub,       /* getProperty */
    JS_StrictPropertyStub, /* setProperty */
    JS_EnumerateStub,
    JS_ResolveStub,
    JS_ConvertStub,
    NULL,                  /* finalize */
    NULL,                  /* reserved0 */
    NULL,                  /* checkAccess */
    NULL,                  /* call */
    NULL,                  /* construct */
    NULL,                  /* xdrObject */
    NULL,                  /* hasInstance */
    NULL,                  /* trace */
    NULL                   /* reserved1 */
  },
  { %s }, -1, %s
};
""" % (self.descriptor.interface.identifier.name, prototypeChainString,
       str(self.descriptor.nativeIsISupports).lower())

class CGPrototypeJSClass(CGThing):
    def __init__(self, descriptor):
        CGThing.__init__(self)
        self.descriptor = descriptor
    def declare(self):
        # We're purely for internal consumption
        return ""
    def define(self):
        return """
static JSClass PrototypeClass = {
  "%s Prototype", 0,
  JS_PropertyStub,       /* addProperty */
  JS_PropertyStub,       /* delProperty */
  JS_PropertyStub,       /* getProperty */
  JS_StrictPropertyStub, /* setProperty */
  JS_EnumerateStub,
  JS_ResolveStub,
  JS_ConvertStub,
  NULL,                  /* finalize */
  NULL,                  /* reserved0 */
  NULL,                  /* checkAccess */
  NULL,                  /* call */
  NULL,                  /* construct */
  NULL,                  /* xdrObject */
  NULL,                  /* hasInstance */
  NULL,                  /* trace */
  NULL                   /* reserved1 */
};
""" % (self.descriptor.interface.identifier.name)

class CGConstructorJSClass(CGThing):
    def __init__(self, descriptor):
        CGThing.__init__(self)
        self.descriptor = descriptor
    def declare(self):
        # We're purely for internal consumption
        return ""
    def define(self):
        return """
static JSClass ConstructorClass = {
  "Function", 0,
  JS_PropertyStub,       /* addProperty */
  JS_PropertyStub,       /* delProperty */
  JS_PropertyStub,       /* getProperty */
  JS_StrictPropertyStub, /* setProperty */
  JS_EnumerateStub,
  JS_ResolveStub,
  JS_ConvertStub,
  NULL,                  /* finalize */
  NULL,                  /* reserved0 */
  NULL,                  /* checkAccess */
  // XXXbz This may need a useful call hook
  NULL,                  /* call */
  NULL,                  /* construct */
  NULL,                  /* xdrObject */
  // XXXbz This needs a useful hasInstance hook
  NULL,                  /* hasInstance */
  NULL,                  /* trace */
  NULL                   /* reserved1 */
};
"""

class CGList(CGThing):
    def __init__(self, children):
        CGThing.__init__(self)
        self.children = children
    def append(self, child):
        self.children.append(child)
    def prepend(self, child):
        self.children.insert(0, child)
    def declare(self):
        return ''.join([child.declare() for child in self.children])
    def define(self):
        return ''.join([child.define() for child in self.children])

class CGGeneric(CGThing):
    def __init__(self, declare="", define=""):
        self.declareText = declare
        self.defineText = define
    def declare(self):
        return self.declareText
    def define(self):
        return self.defineText

class CGWrapper(CGThing):
    """
    Generic CGThing that wraps other CGThings with pre and post text.
    """
    def __init__(self, child, pre="", post="",
                 declarePre=None, declarePost=None,
                 definePre=None, definePost=None):
        CGThing.__init__(self)
        self.child = child
        self.declarePre = declarePre or pre
        self.declarePost = declarePost or post
        self.definePre = definePre or pre
        self.definePost = definePost or post
    def declare(self):
        return self.declarePre + self.child.declare() + self.declarePost
    def define(self):
        return self.definePre + self.child.define() + self.definePost

class CGNamespace(CGWrapper):
    def __init__(self, namespace, child):
        pre = "namespace %s {\n" % namespace
        post="} // namespace %s\n" % namespace
        CGWrapper.__init__(self, child, pre=pre, post=post)
    @staticmethod
    def build(namespaces, child):
        """
        Static helper method to build multiple wrapped namespaces.
        """
        if not namespaces:
            return child
        return CGNamespace(namespaces[0], CGNamespace.build(namespaces[1:], child))

class CGIncludeGuard(CGWrapper):
    """
    Generates include guards for a header.
    """
    def __init__(self, prefix, child):
        """|prefix| is the filename without the extension."""
        define = 'mozilla_dom_bindings_%s_h__' % prefix
        CGWrapper.__init__(self, child,
                           declarePre='#ifndef %s\n#define %s\n\n' % (define, define),
                           declarePost='\n#endif // %s\n' % define)

class CGHeaders(CGWrapper):
    """
    Generates the appropriate include statements.
    """
    def __init__(self, descriptors, declareIncludes, child):
        """
        Builds a set of include to cover |descriptors|.

        Also includes the files in |declareIncludes| in the header file.
        """
        # Determine the filenames for which we need headers.
        interfaceDeps = [d.interface for d in descriptors]
        interfaceDeps.extend([d.parent for d in interfaceDeps if d.parent])
        filenames = set([os.path.basename(d.filename()) for d in interfaceDeps])
        bindingIncludes = ['mozilla/dom/bindings/' + f.replace('.webidl', 'Binding.h')
                           for f in filenames]

        # Grab all the implementation declaration files we need.
        implementationIncludes = [f for f in set([d.headerFile for d in descriptors])]

        # Let the machinery do its thing.
        def _includeString(includes):
            return ''.join(['#include "%s"\n' % i for i in includes]) + '\n'
        CGWrapper.__init__(self, child,
                           declarePre=_includeString(declareIncludes),
                           definePre=_includeString(implementationIncludes)
                                     + _includeString(bindingIncludes))

class Argument():
    def __init__(self, argType, name):
        self.argType = argType
        self.name = name
    def __str__(self):
        return self.argType + ' ' + self.name

class CGAbstractMethod(CGThing):
    def __init__(self, descriptor, name, returnType, args, inline=False, static=False):
        CGThing.__init__(self)
        self.descriptor = descriptor
        self.name = name
        self.returnType = returnType
        self.args = args
        self.inline = inline
        self.static = static
    def _argstring(self):
        return ', '.join([str(a) for a in self.args])
    def _decorators(self):
        decorators = []
        if self.inline:
            decorators.append('inline')
        if self.static:
            decorators.append('static')
        decorators.append(self.returnType)
        return ' '.join(decorators)
    def declare(self):
        if self.inline:
            # Make sure to indent our definition properly
            return self._define().replace("\n", "\n  ")
        return "\n  %s %s(%s);\n" % (self._decorators(), self.name, self._argstring())
    def _define(self):
        return self.definition_prologue() + self.definition_body() + self.definition_epilogue()
    def define(self):
        return "" if self.inline else self._define()
    def definition_prologue(self):
        maybeNewline = " " if self.inline else "\n"
        return "\n%s%s%s(%s)\n{" % (self._decorators(), maybeNewline,
                                    self.name, self._argstring())
    def definition_epilogue(self):
        return "\n}\n"
    def definition_body(self):
        assert(False) # Override me!

class CGAbstractStaticMethod(CGAbstractMethod):
    """
    Abstract base class for codegen of implementation-only (no
    declaration) static methods.
    """
    def __init__(self, descriptor, name, returnType, args):
        CGAbstractMethod.__init__(self, descriptor, name, returnType, args,
                                  inline=False, static=True)
    def declare(self):
        # We only have implementation
        return ""

class MethodDefiner:
    def __init__(self, descriptor):
        self.descriptor = descriptor
        self.methods = [m for m in self.descriptor.interface.members if
                        m.isMethod()]
    def hasMethods(self):
        return len(self.methods) != 0
    def __str__(self):
        if not self.hasMethods():
            return ""

        # The length of a method is the maximum of the lengths of the
        # argument lists of all its overloads.
        def methodLength(method):
            signatures = method.signatures()
            return max([len(s[1]) for s in signatures])

        funcdecls = ['    JS_FN("%s", %s, %s, JSPROP_ENUMERATE)' %
                     (m.identifier.name, m.identifier.name,
                      methodLength(m)) for m in self.methods]
        # And add our JS_FS_END
        funcdecls.append('    JS_FS_END')

        return ("  static JSFunctionSpec methods[] = {\n" +
                ',\n'.join(funcdecls) + "\n" +
                "  };\n")

class AttrDefiner:
    def __init__(self, descriptor):
        self.descriptor = descriptor
        self.attrs = [m for m in self.descriptor.interface.members if
                      m.isAttr()]
    def hasAttrs(self):
        return len(self.attrs) != 0
    def __str__(self):
        if not self.hasAttrs():
            return ""

        def flags(attr):
            flags = "JSPROP_NATIVE_ACCESSORS | JSPROP_SHARED | JSPROP_ENUMERATE"
            if attr.readonly:
                return "JSPROP_READONLY | " + flags
            return flags

        def getter(attr):
            return "get_" + attr.identifier.name

        def setter(attr):
            if attr.readonly:
                return "NULL"
            return "set_" + attr.identifier.name

        attrdecls = ['    { "%s", 0, %s, %s, %s }' %
                     (attr.identifier.name, flags(attr), getter(attr),
                      setter(attr)) for attr in self.attrs]
        attrdecls.append('    { 0, 0, 0, 0, 0 }')

        return ("  static JSPropertySpec props[] = {\n" +
                ',\n'.join(attrdecls) + "\n" +
                "  };\n")

class CGCreateProtoObjectMethod(CGAbstractMethod):
    def __init__(self, descriptor):
        args = [Argument('JSContext*', 'aCx'), Argument('JSObject*', 'aGlobal')]
        CGAbstractMethod.__init__(self, descriptor, 'CreateProtoObject', 'JSObject*', args)
    def definition_body(self):
        protoChain = self.descriptor.prototypeChain
        if len(protoChain) == 1:
            getParentProto = "GetCanonicalProto(aCx, aGlobal, JSProto_Object)"
        else:
            parentProtoName = self.descriptor.prototypeChain[-2]
            getParentProto = "%s::GetProtoObject(aCx, aGlobal)" % (parentProtoName)

        defineMethods = MethodDefiner(self.descriptor)
        defineAttributes = AttrDefiner(self.descriptor);

        # XXXbz this should be False if we're [NoInterfaceObject]
        needInterfaceObject = True

        return """
  JSObject* parentProto = %s;
  if (!parentProto) {
    return NULL;
  }

%s
%s

  return bindings::CreateProtoObject(aCx, parentProto, &PrototypeClass,
                                     %s, %s, %s,
                                     aGlobal, "%s");""" % (
            getParentProto, defineMethods, defineAttributes,
            "&ConstructorClass" if needInterfaceObject else "NULL",
            "methods" if defineMethods.hasMethods() else "NULL",
            "props" if defineAttributes.hasAttrs() else "NULL",
            self.descriptor.interface.identifier.name if needInterfaceObject else "NULL")

class CGGetProtoObjectMethod(CGAbstractMethod):
    def __init__(self, descriptor):
        args = [Argument('JSContext*', 'aCx'), Argument('JSObject*', 'aGlobal')]
        CGAbstractMethod.__init__(self, descriptor, 'GetProtoObject',
                                  'JSObject*', args, inline=True)
    def definition_body(self):
        return """
  /* Get the prototype object for this class.  This will create the prototype
     as needed. */

  /* Make sure our global is sane.  Hopefully we can remove this sometime */
  if (!(js::GetObjectClass(aGlobal)->flags & JSCLASS_DOM_GLOBAL)) {
    return NULL;
  }
  /* Check to see whether the prototype is already installed */
  JSObject **protoArray = GetProtoArray(aGlobal);
  JSObject *ourProto = protoArray[id::%s];
  if (!ourProto) {
    ourProto = protoArray[id::%s] = CreateProtoObject(aCx, aGlobal);
  }

  /* ourProto might _still_ be null, but that's OK */
  return ourProto;""" % (self.descriptor.name, self.descriptor.name)

builtinNames = {
    IDLType.Tags.bool: 'bool',
    IDLType.Tags.uint8: 'uint8_t',
    IDLType.Tags.int16: 'int16_t',
    IDLType.Tags.int32: 'int32_t',
    IDLType.Tags.int64: 'int64_t',
    IDLType.Tags.uint16: 'uint16_t',
    IDLType.Tags.uint32: 'uint32_t',
    IDLType.Tags.uint64: 'uint64_t',
    IDLType.Tags.float: 'float',
    IDLType.Tags.double: 'double'
}

def getWrapTemplateForType(type):
    if type.isVoid():
        return """
  ${jsvalRef} = JSVAL_VOID;
  return true;"""

    if type.isInterface():
        # Wrap the object
            return """
  // XXXbz need to learn to wrap objects
  return false;"""

    if not type.isPrimitive():
        return """
  // XXXbz need to learn to wrap other things
  return false;"""

    tag = type.tag()
    
    if tag in [IDLType.Tags.uint8, IDLType.Tags.int16, IDLType.Tags.int32, IDLType.Tags.uint16]:
        return """
  ${jsvalRef} = INT_TO_JSVAL(int32_t(result));
  return true;"""

    elif tag in [IDLType.Tags.int64, IDLType.Tags.uint64, IDLType.Tags.float, IDLType.Tags.double]:
        return """
  return JS_NewNumberValue(cx, double(result), ${jsvalPtr});"""

    elif tag == IDLType.Tags.uint32:
        return """
  ${jsvalRef} = UINT_TO_JSVAL(result);
  return true;"""

    elif tag == IDLType.Tags.bool:
        return """
  ${jsvalRef} = BOOLEAN_TO_JSVAL(result);
  return true;"""

    else:
        return """
  // XXXbz need to learn to wrap other things
  return false;"""


class PerSignatureCall():
    """
    This class handles the guts of generating code for a particular
    call signature.  A call signature consists of three things:

    1) A return type, which can be None to indicate that there is no
       actual return value (e.g. this is an attribute setter) or an
       IDLType if there's an IDL type involved (including |void|).
    2) An argument list, which is allowed to be empty.
    3) A name of a native method to call.

    We also need to know whether this is a method or a getter/setter
    to do error reporting correctly.
    """
    # XXXbz For now each entry in the argument list is an IDLType, but
    # longer-term we may want to have ways of flagging things like
    # JSContext* or optional_argc in there.
    
    # XXXbz void methods have a signature with a isVoid() type object
    #       as first element.  Methods with no args have length-0 arg
    #       lists as second element in signaure.
    # XXXbz if isInterface() true on a type, type.inner is the interface object
    # XXXbz is isPrimitive() true on a type, then .tag() will return an
    #       IDLType.Tags value.  So you can compare
    #       type.tag() == IDLType.Tags.int8 or whatever.
    def __init__(self, returnType, arguments, nativeMethodName,
                 scriptableInterfaceAndName):
        self.returnType = returnType
        self.arguments = arguments
        self.nativeMethodName = nativeMethodName
        self.scriptableInterfaceAndName = scriptableInterfaceAndName

    def unwrap_arguments(self):
        return ""

    def generate_call(self):
        # XXXbz add provisions for infallible calls here?
        nativeArgs = ["arg" + str(i) for i in range(len(self.arguments))]
        # XXXbz arguments that have to go in outparams go here?
        nativeArgs.append("&rv")
        # XXXbz need to have full config to do this retval business right
        if self.returnType is None or self.returnType.isVoid():
            # Nothing to declare.
            resultDeclaration = ""
        elif self.returnType.isPrimitive() and self.returnType.tag() in builtinNames:
            resultDeclaration = "%s result = 0;" % builtinNames[self.returnType.tag()]
        else:
            resultDeclaration = "// XXX need to declare |result| for type %s." % self.returnType

        return """
  nsresult rv = NS_OK;
  %s
  // XXXbz need to actually make those methods exist!
#if 0
  self->%s(%s);
#endif
  if (NS_FAILED(rv)) {%s
  }""" % (resultDeclaration, self.nativeMethodName, ', '.join(nativeArgs), self.getErrorReport())

    def wrap_return_value(self):
        resultTemplateValues = {'jsvalRef': '*vp', 'jsvalPtr': 'vp'}
        return string.Template(
            getWrapTemplateForType(self.returnType)
            ).substitute(resultTemplateValues)

    def getErrorReport(self):
        return """
    return xpc_qsThrowMethodFailedWithDetails(cx, rv, "%s", "%s");""" % self.scriptableInterfaceAndName

    def __str__(self):
        return (self.unwrap_arguments() + self.generate_call() +
                self.wrap_return_value())

class PerSignatureMethodCall(PerSignatureCall):
    def __init__(self, returnType, arguments, nativeMethodName,
                 scriptableInterfaceAndName):
        PerSignatureCall.__init__(self, returnType, arguments, nativeMethodName,
                                  scriptableInterfaceAndName)

class GetterSetterCall(PerSignatureCall):
    def __init__(self, returnType, arguments, nativeMethodName,
                 scriptableInterfaceAndName):
        PerSignatureCall.__init__(self, returnType, arguments, nativeMethodName,
                                  scriptableInterfaceAndName)

class GetterCall(GetterSetterCall):
    def __init__(self, returnType, nativeMethodName,
                 scriptableInterfaceAndName):
        GetterSetterCall.__init__(self, returnType, [], nativeMethodName,
                                  scriptableInterfaceAndName)

class SetterCall(GetterSetterCall):
    def __init__(self, argType, nativeMethodName, scriptableInterfaceAndName):
        GetterSetterCall.__init__(self, None, [argType], nativeMethodName,
                                  scriptableInterfaceAndName)
    def wrap_return_value(self):
        # We have no return value
        return "\n  return true;"

class CGAbstractBindingMethod(CGAbstractStaticMethod):
    def __init__(self, descriptor, name, returnType, args):
        CGAbstractStaticMethod.__init__(self, descriptor, name,
                                        returnType, args)
    def definition_body(self):
        return (self.unwrap_this() + self.generate_code())

    def unwrap_this(self):
        return """
  %s *self;
  if (!UnwrapThis(cx, obj, %s, %s, &self))
    return false;
""" % (self.descriptor.nativeClass, "id::" + self.descriptor.name,
       "depth::" + self.descriptor.name)

    def generate_code(self):
        assert(False) # Override me

def MakeNativeName(name):
    return name[0].upper() + name[1:]

class CGNativeMethod(CGAbstractBindingMethod):
    def __init__(self, descriptor, method):
        self.method = method
        args = [Argument('JSContext*', 'cx'), Argument('uintN', 'argc'),
                Argument('JS::Value*', 'vp')]
        CGAbstractBindingMethod.__init__(self, descriptor,
                                         method.identifier.name,
                                         'JSBool', args)
    def unwrap_this(self):
         return """
  JSObject *obj = JS_THIS_OBJECT(cx, vp);
  if (!obj)
    return false;""" + CGAbstractBindingMethod.unwrap_this(self)

    def generate_code(self):
        signatures = self.method.signatures()
        nativeName = MakeNativeName(self.method.identifier.name)
        callGenerators = [PerSignatureMethodCall(s[0], s[1], nativeName,
                                                 (self.descriptor.name,
                                                  self.method.identifier.name))
                          for s in signatures]
        if len(callGenerators) != 1:
            raise TypeError("Don't know how to handle overloads yet.  Will need to generate code to pick the right overload based on the arguments, then jump to the right generated code")

        return str(callGenerators[0]);

class CGNativeGetter(CGAbstractBindingMethod):
    def __init__(self, descriptor, attr):
        self.attr = attr
        args = [Argument('JSContext*', 'cx'), Argument('JSObject*', 'obj'),
                Argument('jsid', 'id'), Argument('JS::Value*', 'vp')]
        CGAbstractBindingMethod.__init__(self, descriptor,
                                         'get_'+attr.identifier.name,
                                         'JSBool', args)
    def generate_code(self):
        nativeMethodName = "Get" + MakeNativeName(self.attr.identifier.name)
        return str(GetterCall(self.attr.type, nativeMethodName,
                              (self.descriptor.name, self.attr.identifier.name)))

class CGNativeSetter(CGAbstractBindingMethod):
    def __init__(self, descriptor, attr):
        self.attr = attr
        args = [Argument('JSContext*', 'cx'), Argument('JSObject*', 'obj'),
                Argument('jsid', 'id'), Argument('JSBool', 'strict'),
                Argument('JS::Value*', 'vp')]
        CGAbstractBindingMethod.__init__(self, descriptor,
                                         'set_'+attr.identifier.name,
                                         'JSBool', args)
    def generate_code(self):
        nativeMethodName = "Set" + MakeNativeName(self.attr.identifier.name)
        return str(SetterCall(self.attr.type, nativeMethodName,
                              (self.descriptor.name, self.attr.identifier.name)))

class CGDescriptor(CGThing):
    def __init__(self, descriptor):
        CGThing.__init__(self)

        # XXXbholley - Not everything should actually have a jsclass.
        cgThings = [CGNativeMethod(descriptor, m) for m in
                    descriptor.interface.members if m.isMethod()]
        cgThings.extend([CGNativeGetter(descriptor, a) for a in
                         descriptor.interface.members if a.isAttr()])
        cgThings.extend([CGNativeSetter(descriptor, a) for a in
                         descriptor.interface.members if
                         a.isAttr() and not a.readonly])
        # XXXbz this should check for [NoInterfaceObject]
        if True:
            cgThings.append(CGConstructorJSClass(descriptor))
        cgThings.extend([CGDOMJSClass(descriptor),
                         CGPrototypeJSClass(descriptor),
                         CGCreateProtoObjectMethod(descriptor),
                         CGGetProtoObjectMethod(descriptor)])

        allCGThings = CGList(cgThings)
        allCGThings = CGWrapper(allCGThings, post="\n")
        self.cgRoot = CGWrapper(CGNamespace(descriptor.name, allCGThings), post="\n")
    def declare(self):
        return self.cgRoot.declare()
    def define(self):
        return self.cgRoot.define()

class CGNamespacedEnum(CGThing):
    def __init__(self, namespace, enumName, names, values, comment=""):

        if not values:
            values = [None for i in range(0, len(names))]

        # Account for explicit enum values.
        entries = []
        for i in range(0, len(names)):
            entry = names[i] if values[i] is None else "%s = %s" % (names[i], values[i])
            entries.append(entry)

        # Append a Count.
        entries.append('Count')

        # Indent.
        entries = ['  ' + e for e in entries]

        # Buildthe enum body.
        enumstr = comment + 'enum %s\n{\n%s\n};\n' % (enumName, ',\n'.join(entries))
        curr = CGGeneric(declare=enumstr)

        # Add some whitespace padding.
        curr = CGWrapper(curr, pre='\n',post='\n')

        # Add the namespace.
        curr = CGNamespace(namespace, curr)

        # Add the typedef
        typedef = '\ntypedef %s::%s %s;\n\n' % (namespace, enumName, enumName)
        curr = CGList([curr, CGGeneric(declare=typedef)])

        # Save the result.
        self.node = curr

    def declare(self):
        return self.node.declare()
    def define(self):
        assert False # Only for headers.

class CGBindingRoot(CGThing):
    """
    Root codegen class for binding generation. Instantiate the class, and call
    declare or define to generate header or cpp code (respectively).
    """
    def __init__(self, config, prefix):

        # Do codegen for all the descriptors.
        curr = CGList([CGDescriptor(x) for x in config.getConcreteDescriptors()])

        # Wrap all of that in our namespaces.
        curr = CGNamespace.build(['mozilla', 'dom', 'bindings', 'prototypes'],
                                 CGWrapper(curr, pre="\n"))

        # Add header includes.
        curr = CGHeaders(config.getConcreteDescriptors(), ['DOMJSClass.h', 'Utils.h'], curr)

        # Add include guards.
        curr = CGIncludeGuard(prefix, curr)

        # Add the auto-generated comment.
        curr = CGWrapper(curr, pre="/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n\n")

        # Store the final result.
        self.root = curr

    def declare(self):
        return self.root.declare()
    def define(self):
        return self.root.define()


class CGGlobalRoot():
    """
    Root codegen class for global code generation. Instantiate the class, and call

    It's possible that we may at some point wish to generate more than just
    PrototypeList.h. As such, this class eschews the define/declare
    architecture (and thus does not inherit from CGThing).

    To generate code, call the method associated with the target.
    """
    def __init__(self, config, prefix):
        self.config = config
        self.prefix = prefix

    def prototypeList_h(self):

        # Prototype ID enum.
        protos = [d.name for d in self.config.getConcreteDescriptors()]
        idThing = CGNamespacedEnum('id', 'ID', protos, None)

        # Depth enum.
        depths = [d.interface.inheritanceDepth() for d in self.config.getConcreteDescriptors()]
        depthComment = '/* The depths at which we expect the above IDs in inheritance chains;\n' + \
                       '   0 means there is no superclass */\n'
        depthThing = CGNamespacedEnum('depth', 'Depth', protos, depths, depthComment)

        # Combine.
        curr = CGList([idThing, depthThing])

        # Wrap all of that in our namespaces.
        curr = CGNamespace.build(['mozilla', 'dom', 'bindings', 'prototypes'],
                                 CGWrapper(curr, pre="\n"))

        # Add include guards.
        curr = CGIncludeGuard(self.prefix, curr)

        # Add the auto-generated comment.
        curr = CGWrapper(curr, pre="/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n\n")

        # Do header generation on the reuslt.
        return curr.declare()