dom/bindings/Codegen.py
author Ms2ger <ms2ger@gmail.com>
Tue, 21 Feb 2012 22:44:16 +0100
changeset 87463 7e6c70d95373
parent 87319 8d6b7cd7baee
child 87569 8a97b4c007fb
permissions -rw-r--r--
Merge m-c to d-b.
# 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 *

AUTOGENERATED_WARNING_COMMENT = \
    "/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n\n"
FINALIZE_HOOK_NAME = '_Finalize'
TRACE_HOOK_NAME = '_Trace'

def replaceFileIfChanged(filename, newContents):
    """
    Read a copy of the old file, so that we don't touch it if it hasn't changed.
    Returns True if the file was updated, false otherwise.
    """
    oldFileContents = ""
    try:
        oldFile = open(filename, 'rb')
        oldFileContents = ''.join(oldFile.readlines())
        oldFile.close()
    except:
        pass

    if newContents == oldFileContents:
        return False

    f = open(filename, 'wb')
    f.write(newContents)
    f.close()

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):
        traceHook = TRACE_HOOK_NAME if self.descriptor.customTrace else 'NULL'
        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,
    %s,
    NULL,                  /* reserved0 */
    NULL,                  /* checkAccess */
    NULL,                  /* call */
    NULL,                  /* construct */
    NULL,                  /* xdrObject */
    NULL,                  /* hasInstance */
    %s,
    NULL                   /* reserved1 */
  },
  { %s }, -1, %s
};
""" % (self.descriptor.interface.identifier.name, FINALIZE_HOOK_NAME,
       traceHook, 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, joiner=""):
        CGThing.__init__(self)
        self.children = children
        self.joiner = joiner
    def append(self, child):
        self.children.append(child)
    def prepend(self, child):
        self.children.insert(0, child)
    def declare(self):
        return self.joiner.join([child.declare() for child in self.children])
    def define(self):
        return self.joiner.join([child.define() for child in self.children])

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

class CGIndenter(CGThing):
    def __init__(self, child, indentLevel=2):
        CGThing.__init__(self)
        self.child = child
        self.indent = " " * indentLevel
        # We'll want to insert the indent at the beginnings of lines, but we
        # don't want to indent empty lines.  So only indent lines that have a
        # non-newline character on them.
        self.pattern = re.compile("^(?=[^\n#])", re.MULTILINE)
    def declare(self):
        decl = self.child.declare()
        if decl is not "":
            return re.sub(self.pattern, self.indent, decl)
        else:
            return ""
    def define(self):
        defn = self.child.define()
        if defn is not "":
            return re.sub(self.pattern, self.indent, defn)
        else:
            return ""

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,
                 declareOnly=False):
        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
        self.declareOnly = declareOnly
    def declare(self):
        return self.declarePre + self.child.declare() + self.declarePost
    def define(self):
        if self.declareOnly:
            return ''
        return self.definePre + self.child.define() + self.definePost

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

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.
        """
        def getInterfaceFilename(interface):
            basename = os.path.basename(interface.filename())
            return 'mozilla/dom/bindings/' + \
                   basename.replace('.webidl', 'Binding.h')

        # 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])
        bindingIncludes = [getInterfaceFilename(d) for d in interfaceDeps]

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

        # Now find all the things we'll need as arguments because we
        # need to wrap or unwrap them.
        bindingHeaders = []
        typeHeaders = []
        for d in descriptors:
            members = [m for m in d.interface.members]
            signatures = [s for m in members if m.isMethod() for s in m.signatures()]
            types = [s[0] for s in signatures]
            types.extend([t.type for s in signatures for t in s[1]])

            attrs = [a for a in members if a.isAttr()]
            types.extend([a.type for a in attrs])

            typeDescriptors = [d.getDescriptor(t.inner.identifier.name)
                               for t in types
                               if t.isInterface() and not t.isArrayBuffer()]
            for typeDesc in typeDescriptors:
                typeHeaders.append(typeDesc.headerFile)
                bindingHeaders.append(getInterfaceFilename(typeDesc.interface))

        implementationIncludes.extend([f for f in set(typeHeaders)])

        # 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(bindingIncludes) +
                                     _includeString(bindingHeaders) +
                                     _includeString(implementationIncludes))

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:
            return self._define()
        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 CGAbstractClassHook(CGAbstractStaticMethod):
    """
    Meant for implementing JSClass hooks, like Finalize or Trace. Does very raw
    'this' unwrapping as it assumes that the unwrapped type is always known.
    """

    def __init__(self, descriptor, name, returnType, args):
        CGAbstractStaticMethod.__init__(self, descriptor, name, returnType,
                                        args)

    def definition_body_prologue(self):
        return """
  MOZ_ASSERT(js::GetObjectJSClass(obj) == Class.ToJSClass());
  %s *self = UnwrapDOMObject<%s>(obj);
""" % (self.descriptor.nativeClass, self.descriptor.nativeClass)

    def definition_body(self):
        return self.definition_body_prologue() + self.generate_code()

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

class CGClassFinalizeHook(CGAbstractClassHook):
    def __init__(self, descriptor):
        args = [Argument('JSContext*', 'cx'), Argument('JSObject*', 'obj')]
        CGAbstractClassHook.__init__(self, descriptor, FINALIZE_HOOK_NAME,
                                         'void', args)

    def generate_code(self):
        if self.descriptor.customFinalize:
            return """
  if (self) {
#if 0
    self->%s(%s);
#endif
  }""" % (self.name, self.args[0].name)
        return "\n  self->Release();"

class CGClassTraceHook(CGAbstractClassHook):
    def __init__(self, descriptor):
        args = [Argument('JSTracer*', 'trc'), Argument('JSObject*', 'obj')]
        CGAbstractClassHook.__init__(self, descriptor, TRACE_HOOK_NAME, 'void',
                                     args)

    def generate_code(self):
        return """
  if (self) {
#if 0
    self->%s(%s);
#endif
  }""" % (self.name, self.args[0].name)

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 = "JS_GetObjectPrototype(aCx, aGlobal)"
        else:
            parentProtoName = self.descriptor.prototypeChain[-2]
            getParentProto = "%s::GetProtoObject(aCx, aGlobal)" % (parentProtoName)

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

        needInterfaceObject = self.descriptor.interface.hasInterfaceObject

        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.int8: 'int8_t',
    IDLType.Tags.int16: 'int16_t',
    IDLType.Tags.int32: 'int32_t',
    IDLType.Tags.int64: 'int64_t',
    IDLType.Tags.uint8: 'uint8_t',
    IDLType.Tags.uint16: 'uint16_t',
    IDLType.Tags.uint32: 'uint32_t',
    IDLType.Tags.uint64: 'uint64_t',
    IDLType.Tags.float: 'float',
    IDLType.Tags.double: 'double'
}

class ConcreteObjectUnwrapper():
    """
    A class for unwrapping an object named by the "source" argument
    based on the passed-in descriptor and storing it in a variable
    called by the name in the "target" argument.

    codeOnFailure is the code to run if unwrapping fails.
    """
    def __init__(self, descriptor, source, target, codeOnFailure):
        self.substitution = { "type" : descriptor.nativeClass,
                              "protoID" : "id::" + descriptor.name,
                              "source" : source,
                              "target" : target,
                              "codeOnFailure" : codeOnFailure }

    def __str__(self):
        return string.Template("""
  {
    nsresult rv = UnwrapObject<${protoID}>(cx, ${source}, &${target});
    if (NS_FAILED(rv)) {
      ${codeOnFailure}
    }
  }""").substitute(self.substitution)

class FailureFatalConcreteObjectUnwrapper(ConcreteObjectUnwrapper):
    """
    As ConcreteObjectUnwrapper, but defaulting to throwing if unwrapping fails
    """
    def __init__(self, descriptor, source, target):
        ConcreteObjectUnwrapper.__init__(self, descriptor, source, target,
                                         "return Throw(cx, rv);")

def getArgumentConversionTemplate(type, descriptor):
    if type.isSequence() or type.isArray():
        raise TypeError("Can't handle sequence or array arguments yet")

    if descriptor is not None:
        assert(type.isInterface())
        # This is an interface that we implement as a concrete class
        # or an XPCOM interface.
        if type.nullable():
            nameSuffix = ""
        else:
            nameSuffix = "_ptr"
        template = "  ${typeName} *${name}%s;\n" % nameSuffix

        template += "  if (${argVal}.isObject()) {"
        if descriptor.concrete:
            template += str(FailureFatalConcreteObjectUnwrapper(
                    descriptor,
                    "&${argVal}.toObject()",
                    "${name}"+nameSuffix)).replace("\n", "\n  ") + "\n"
        else:
            raise TypeError("Can't handle this interface type yet, becase we "
                            "have no support for unwrapping non-concrete types: " + type)
        if type.nullable():
            template += (
                "  } else if (${argVal}.isNullOrUndefined()) {\n"
                "    ${name}%s = NULL;\n" % nameSuffix)

        template += (
            "  } else {\n"
            "    return Throw(cx, NS_ERROR_XPC_BAD_CONVERT_JS);\n"
            "  }\n")

        if not type.nullable():
            template += "  ${typeName} &${name} = *${name}_ptr;\n"
            
        return template

    if type.isInterface():
        raise TypeError("Interface type with no descriptor: " + type)

    if type.isString():
        # XXXbz Need to figure out string behavior?  Also, how to
        # detect them?  Also, nullability?

        return (
            "  xpc_qsDOMString ${name}(cx, ${argVal}, ${argPtr},\n"
            "                       xpc_qsDOMString::eDefaultNullBehavior,\n"
            "                       xpc_qsDOMString::eDefaultUndefinedBehavior);\n"
            "  if (!${name}.IsValid()) {\n"
            "    return false;\n"
            "  }\n")

    if type.isEnum():
        if type.nullable():
            raise TypeError("We don't support nullable enumerated arguments "
                            "yet")
        enum = type.inner.identifier.name
        return (
            "  %(enumtype)s ${name};\n"
            "  {\n"
            "    bool ok;\n"
            "    $name = (%(enumtype)s) FindEnumStringIndex(cx, ${argVal}, %(values)s, &ok);\n"
            "    if (!ok) {\n"
            "      return false;\n"
            "    }\n"
            "  }" % { "enumtype" : enum + "::value",
                      "values" : enum + "::strings" })

    if type.isCallback():
        # XXXbz we're going to assume that callback types are always
        # nullable and always have [TreatNonCallableAsNull] for now.
        return (
            "  JSObject *${name};\n"
            "  if (${argVal}.isObject() && JS_ObjectIsCallable(cx, &${argVal}.toObject())) {\n"
            "    ${name} = &${argVal}.toObject();\n"
            "  } else {\n"
            "    ${name} = NULL;\n"
            "  }\n")

    if type.isAny():
        return "  JS::Value ${name} = ${argVal};\n"

    if not type.isPrimitive():
        raise TypeError("Need conversion for argument type '%s'" % type)

    tag = type.tag()
    replacements = dict()
    if type.nullable():
        replacements["declareArg"] = (
            "  Nullable<${typeName}> ${name};\n"
            "  if (${argVal}.isNullOrUndefined()) {\n"
            "    ${name}.SetNull();\n"
            "  } else"
            )
        replacements["finalValueSetter"] = "${name}.SetValue"
    else:
        replacements["declareArg"] = "  ${typeName} ${name};\n"
        replacements["finalValueSetter"] = "${name} = "

    replacements["intermediateCast"] = ""
        
    if tag == IDLType.Tags.bool:
        replacements["jstype"] = "JSBool"
        replacements["converter"] = "JS_ValueToBoolean"
    elif tag in [IDLType.Tags.int8, IDLType.Tags.uint8, IDLType.Tags.int16,
                 IDLType.Tags.int32, IDLType.Tags.uint32]:
        # XXXbz need to add support for [EnforceRange] and [Clamp]
        # The output of JS_ValueToECMAInt32 is determined as follows:
        #   1) The value is converted to a double
        #   2) Anything that's not a finite double returns 0
        #   3) The double is rounded towards zero to the nearest integer
        #   4) The resulting integer is reduced mod 2^32.  The output of this
        #      operation is an integer in the range [0, 2^32).
        #   5) If the resulting number is >= 2^31, 2^32 is subtracted from it.
        #
        # The result of all this is a number in the range [-2^31, 2^31)
        #
        # WebIDL conversions for the 8-bit, 16-bit, and 32-bit integer types
        # are defined in the same way, except that step 4 uses reduction mod
        # 2^8 and 2^16 for the 8-bit and 16-bit types respectively, and step 5
        # is only done for the signed types.
        #
        # C/C++ define integer conversion semantics to unsigned types as taking
        # your input integer mod (1 + largest value representable in the
        # unsigned type).  Since 2^32 is zero mod 2^8, 2^16, and 2^32,
        # converting to the unsigned int of the relevant width will correctly
        # perform step 4; in particular, the 2^32 possibly subtracted in step 5
        # will become 0.
        #
        # Once we have step 4 done, we're just going to assume 2s-complement
        # representation and cast directly to the type we really want.
        #
        # So we can cast directly for all unsigned types and for int32_t; for
        # the smaller-width signed types we need to cast through the
        # corresponding unsigned type.
        replacements["jstype"] = "int32_t"
        replacements["converter"] = "JS_ValueToECMAInt32"
        if tag is IDLType.Tags.int8:
            replacements["intermediateCast"] = "(uint8_t)"
        elif tag is IDLType.Tags.int16:
            replacements["intermediateCast"] = "(uint16_t)"
        else:
            replacements["intermediateCast"] = ""
    elif tag is IDLType.Tags.int64:
        # XXXbz this may not match what WebIDL says to do in terms of reducing
        # mod 2^64.  Should we check?
        replacements["jstype"] = "PRInt64"
        replacements["converter"] = "xpc_qsValueToInt64"
    elif tag is IDLType.Tags.uint64:
        # XXXbz this may not match what WebIDL says to do in terms of reducing
        # mod 2^64.  Should we check?
        replacements["jstype"] = "PRUint64"
        replacements["converter"] = "xpc_qsValueToUint64"
    elif tag in [IDLType.Tags.float, IDLType.Tags.double]:
        replacements["jstype"] = "jsdouble"
        replacements["converter"] = "JS_ValueToNumber"
    else:
        raise TypeError("Unknown primitive type '%s'" % type);

    # We substitute the %(name)s things here.  Our caller will
    # substitute the ${name} things.
    return ("  %(jstype)s ${name}_jstype;\n"
            "%(declareArg)s" # No leading whitespace or newline here, on purpose
            "  if (%(converter)s(cx, ${argVal}, &${name}_jstype)) {\n"
            "    %(finalValueSetter)s((${typeName})%(intermediateCast)s${name}_jstype);\n"
            "  } else {\n"
            "    return false;\n"
            "  }\n" % replacements)

class CGArgumentConverter(CGThing):
    """
    A class that takes an IDL argument object, its index in the
    argument list, and the argv and argc strings and generates code to
    unwrap the argument to the right native type.
    """
    def __init__(self, argument, index, argv, argc, descriptorProvider):
        CGThing.__init__(self)
        self.argument = argument
        # XXXbz should optional jsval args get JSVAL_VOID? What about
        # others?
        self.replacementVariables = {
            "index" : index,
            "argc" : argc,
            "argv" : argv,
            "defaultValue" : "JSVAL_NULL",
            "name" : "arg%d" % index
            }
        if argument.optional:
            self.replacementVariables["argVal"] = string.Template(
                "(${index} < ${argc} ? ${argv}[${index}] : ${defaultValue})"
                ).substitute(self.replacementVariables)
            self.replacementVariables["argPtr"] = string.Template(
                "(${index} < ${argc} ? &${argv}[${index}] : NULL)"
                ).substitute(self.replacementVariables)
        else:
            self.replacementVariables["argVal"] = string.Template(
                "${argv}[${index}]"
                ).substitute(self.replacementVariables)
            self.replacementVariables["argPtr"] = (
                "&" + self.replacementVariables["argVal"])
        self.descriptor = None
        if argument.type.isPrimitive():
            self.replacementVariables["typeName"] = builtinNames[argument.type.tag()]
        elif argument.type.isInterface() and not argument.type.isArrayBuffer():
            descriptor = descriptorProvider.getDescriptor(
                argument.type.inner.identifier.name)
            self.descriptor = descriptor
            self.replacementVariables["typeName"] = descriptor.typeName

    def define(self):
        return string.Template(
            "\n" + getArgumentConversionTemplate(self.argument.type,
                                                 self.descriptor)
            ).substitute(self.replacementVariables)

def getWrapTemplateForTypeImpl(type, result, descriptorProvider,
                               resultAlreadyAddRefed):
    if type.isSequence() or type.isArray():
        raise TypeError("Can't handle sequence or array return values yet")

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

    if type.isInterface() and not type.isArrayBuffer():
        descriptor = descriptorProvider.getDescriptor(type.inner.identifier.name)
        wrappingCode = ("""
  if (!%s) {
    ${jsvalRef} = JSVAL_NULL;
    return true;
  }""" % result) if type.nullable() else ""
        if descriptor.concrete:
            wrappingCode += """
  if (WrapNewBindingObject(cx, obj, %s, ${jsvalPtr})) {
    return true;
  }""" % result
            if descriptor.workers:
                # Worker bindings can only fail to wrap as a new-binding object
                # if they already threw an exception
                wrappingCode += """
  MOZ_ASSERT(JS_IsExceptionPending(cx));
  return false;"""
            else:
                # Try old-style wrapping for non-worker bindings
                wrappingCode += """
  return HandleNewBindingWrappingFailure(cx, obj, %s, ${jsvalPtr});""" % result
        else:
            raise TypeError("Don't know how to wrap non-concrete types yet")
        return wrappingCode

    if type.isString():
        if type.nullable():
            return """
  return xpc::StringToJsval(cx, %s, ${jsvalPtr});""" % result
        else:
            return """
  return xpc::NonVoidStringToJsval(cx, %s, ${jsvalPtr});""" % result

    if type.isEnum():
        if type.nullable():
            raise TypeError("We don't support nullable enumerated return types "
                            "yet")
        return """
  MOZ_ASSERT(uint32_t(%(result)s) < ArrayLength(%(strings)s));
  JSString* result_str = JS_NewStringCopyZ(cx, %(strings)s[uint32_t(%(result)s)]);
  if (!result_str) {
    return false;
  }
  ${jsvalRef} = JS::StringValue(result_str);
  return true;""" % { "result" : result,
                      "strings" : type.inner.identifier.name + "::strings" }

    if type.isCallback() and not type.isInterface():
        # XXXbz we're going to assume that callback types are always
        # nullable and always have [TreatNonCallableAsNull] for now.
        return """
  ${jsvalRef} = JS::ObjectOrNullValue(%s);
  return true;""" % result

    if type.tag() == IDLType.Tags.any:
        return """
  ${jsvalRef} = %s;\n
  return true;""" % result

    if not type.isPrimitive():
        raise TypeError("Need to learn to wrap %s" % type)

    if type.nullable():
        return """
  if (%s.IsNull()) {
    ${jsvalRef} = JSVAL_NULL;
    return true;
  }
%s""" % (result, getWrapTemplateForTypeImpl(type.inner, "%s.Value()" % result,
                                            descriptorProvider,
                                            resultAlreadyAddRefed))
    
    tag = type.tag()
    
    if tag in [IDLType.Tags.int8, IDLType.Tags.uint8, IDLType.Tags.int16,
               IDLType.Tags.uint16, IDLType.Tags.int32]:
        return """
  ${jsvalRef} = INT_TO_JSVAL(int32_t(%s));
  return true;""" % result

    elif tag in [IDLType.Tags.int64, IDLType.Tags.uint64, IDLType.Tags.float,
                 IDLType.Tags.double]:
        # XXXbz will cast to double do the "even significand" thing that webidl
        # calls for for 64-bit ints?  Do we care?
        return """
  return JS_NewNumberValue(cx, double(%s), ${jsvalPtr});""" % result

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

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

    else:
        raise TypeError("Need to learn to wrap primitive: %s" % type)

def getWrapTemplateForType(type, descriptorProvider, resultAlreadyAddRefed):
    return getWrapTemplateForTypeImpl(type, "result", descriptorProvider,
                                      resultAlreadyAddRefed)

class CGCallGenerator(CGThing):
    """
    A class to generate an actual call to a C++ object.  Assumes that the C++
    object is stored in a variable named "self".
    """
    def __init__(self, errorReport, argCount, returnType, resultAlreadyAddRefed,
                 descriptorProvider, nativeMethodName):
        CGThing.__init__(self)

        isFallible = errorReport is not None

        # XXXbz return values that have to go in outparams go here?
        args = CGList([CGGeneric("arg" + str(i)) for i in range(argCount)], ", ")
        resultOutParam = returnType is not None and returnType.isString()
        # Return values that go in outparams go here
        if resultOutParam:
            args.append(CGGeneric("result"))
        if isFallible:
            args.append(CGGeneric("rv"))

        if returnType is None or returnType.isVoid():
            # Nothing to declare
            result = None
        elif returnType.isPrimitive() and returnType.tag() in builtinNames:
            result = CGGeneric(builtinNames[returnType.tag()])
            if returnType.nullable():
                result = CGWrapper(result, pre="Nullable<", post=">")
        elif returnType.isString():
            result = CGGeneric("nsString")
        elif returnType.isEnum():
            if returnType.nullable():
                raise TypeError("We don't support nullable enum return values")
            result = CGGeneric(returnType.inner.identifier.name + "::value")
        elif returnType.isInterface() and not returnType.isArrayBuffer():
            result = CGGeneric(descriptorProvider.getDescriptor(
                returnType.inner.identifier.name).typeName)
            if resultAlreadyAddRefed:
                result = CGWrapper(result, pre="nsRefPtr<", post=">")
            else:
                result = CGWrapper(result, post="*")
        elif returnType.isCallback():
            # XXXbz we're going to assume that callback types are always
            # nullable for now.
            result = CGGeneric("JSObject*")
        elif returnType.tag() is IDLType.Tags.any:
            result = CGGeneric("jsval")
        else:
            raise TypeError("Don't know how to declare return value for %s" %
                            returnType)

        # Build up our actual call
        self.cgRoot = CGList([], "\n")

        call = CGGeneric(nativeMethodName)
        call = CGWrapper(call, pre="self->")
        call = CGList([call, CGWrapper(args, pre="(", post=");")])
        if result is not None:
            result = CGWrapper(result, post=" result;")
            self.cgRoot.prepend(result)
            if not resultOutParam:
                call = CGWrapper(call, pre="result = ")

        call = CGWrapper(call, pre="#if 0\n", post="\n#endif")
        self.cgRoot.append(call)

        if isFallible:
            self.cgRoot.prepend(CGGeneric("nsresult rv = NS_OK;"))
            self.cgRoot.append(CGGeneric("if (NS_FAILED(rv)) {"))
            self.cgRoot.append(CGIndenter(CGGeneric(errorReport)))
            self.cgRoot.append(CGGeneric("}"))

    def define(self):
        return self.cgRoot.define()

class PerSignatureCall(CGThing):
    """
    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.

    The idlNode parameter can be either a method or an attr. We can query
    |idlNode.identifier| in both cases, so we can be agnostic between the two.
    """
    # XXXbz For now each entry in the argument list is either an
    # IDLArgument or a FakeArgument, 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,
                 descriptor, idlNode, extendedAttributes):
        CGThing.__init__(self)
        self.returnType = returnType
        self.descriptor = descriptor
        self.idlNode = idlNode
        self.extendedAttributes = extendedAttributes
        # Default to already_AddRefed on the main thread, raw pointer in workers
        self.resultAlreadyAddRefed = not descriptor.workers
        
        self.argCount = len(arguments)
        cgThings = [CGArgumentConverter(arguments[i], i, self.getArgv(),
                                        self.getArgc(), self.descriptor) for
                    i in range(self.argCount)]
        cgThings.append(CGGeneric("\n"))
        cgThings.append(CGIndenter(CGCallGenerator(
                    self.getErrorReport() if self.isFallible() else None,
                    self.argCount, returnType, self.resultAlreadyAddRefed,
                    descriptor, nativeMethodName)))
        self.cgRoot = CGList(cgThings)

    def getArgv(self):
        assert(False) # Override me
    def getArgc(self):
        assert(False) # Override me
    def getErrorReport(self):
        assert(False) # Override me

    def isFallible(self):
        return not 'infallible' in self.extendedAttributes

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

    def getErrorReport(self):
        return 'return ThrowMethodFailedWithDetails(cx, rv, "%s", "%s");'\
               % (self.descriptor.interface.identifier.name,
                  self.idlNode.identifier.name)

    def define(self):
        return (self.cgRoot.define() + self.wrap_return_value())

class PerSignatureMethodCall(PerSignatureCall):
    def __init__(self, returnType, arguments, nativeMethodName, descriptor,
                 method, extendedAttributes):
        PerSignatureCall.__init__(self, returnType, arguments, nativeMethodName,
                                  descriptor, method, extendedAttributes)

        # Insert our argv-unwrapping at the beginning of our CGThing list
        if len(arguments) > 0:
            requiredArgs = len(arguments)
            while requiredArgs and arguments[requiredArgs-1].optional:
                requiredArgs -= 1
            if requiredArgs > 0:
                argv = [CGGeneric(
                        "// XXXbz is this the right place for this check?  Or should it be more\n"
                        "// up-front somewhere, not per-signature?\n"
                        "if (argc < %d) {\n"
                        "  return Throw(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);\n"
                        "}" % requiredArgs)]
            else:
                argv = []
            argv.append(CGGeneric("JS::Value *argv = JS_ARGV(cx, vp);"))
            self.cgRoot.prepend(CGWrapper(CGIndenter(CGList(argv, "\n")),
                                          pre="\n",
                                          post="\n"))

    def getArgv(self):
        return "argv" if self.argCount > 0 else ""
    def getArgc(self):
        return "argc"

class GetterSetterCall(PerSignatureCall):
    def __init__(self, returnType, arguments, nativeMethodName, descriptor,
                 attr, extendedAttributes):
        PerSignatureCall.__init__(self, returnType, arguments, nativeMethodName,
                                  descriptor, attr, extendedAttributes)
    def getArgv(self):
        return "vp"

class GetterCall(GetterSetterCall):
    def __init__(self, returnType, nativeMethodName, descriptor, attr,
                 extendedAttributes):
        GetterSetterCall.__init__(self, returnType, [], nativeMethodName,
                                  descriptor, attr, extendedAttributes)
    def getArgc(self):
        return "0"

class FakeArgument():
    def __init__(self, type):
        self.type = type
        self.optional = False

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

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;""" % self.descriptor.nativeClass) +
                str(FailureFatalConcreteObjectUnwrapper(self.descriptor,
                                                        "obj", "self")))

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

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

class CGNativeBindingMethod(CGAbstractBindingMethod):
    """ Class common to all interface methods and attributes. """

    def __init__(self, descriptor, name, returnType, args, baseName):
        self.extendedAttributes = {}
        if baseName in descriptor.infallible:
            self.extendedAttributes['infallible'] = True
        CGAbstractBindingMethod.__init__(self, descriptor, name, returnType,
                                         args)

class CGNativeMethod(CGNativeBindingMethod):
    def __init__(self, descriptor, method):
        self.method = method
        baseName = method.identifier.name
        args = [Argument('JSContext*', 'cx'), Argument('uintN', 'argc'),
                Argument('JS::Value*', 'vp')]
        CGNativeBindingMethod.__init__(self, descriptor, baseName, 'JSBool',
                                       args, baseName)
    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, self.method,
                                                 self.extendedAttributes)
                          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 callGenerators[0].define();

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

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

def getEnumValueName(value):
    # Some enum values can be empty strings.  Others might have weird
    # characters in them.  Deal with the former by returning "_empty",
    # deal with possible name collisions from that by throwing if the
    # enum value is actually "_empty", and throw on any value
    # containing chars other than [a-z] for now.
    if value == "_empty":
        raise SyntaxError('"_empty" is not an IDL enum value we support yet')
    if value == "":
        return "_empty"
    if not re.match("^[a-z]+$", value):
        raise SyntaxError('Enum value "' + value + '" contains characters '
                          'outside [a-z]')
    return value

class CGEnum(CGThing):
    def __init__(self, enum):
        CGThing.__init__(self)
        self.enum = enum

    def declare(self):
        return """
  enum value {
    %s
  };

  const char* strings [] = {
    %s,
    NULL
  };

""" % (",\n    ".join(map(getEnumValueName, self.enum.values())),
       ",\n    ".join(['"' + val + '"' for val in self.enum.values()]))

    def define(self):
        # We only go in the header
        return "";

class ClassItem:
    """ Use with CGClass """
    def __init__(self, name, visibility):
        self.name = name
        self.visibility = visibility
    def declare(self, cgClass):
        assert False
    def define(self, cgClass):
        assert False

class ClassBase(ClassItem):
    def __init__(self, name, visibility='public'):
        ClassItem.__init__(self, name, visibility)
    def declare(self, cgClass):
        return '%s %s' % (self.visibility, self.name)
    def define(self, cgClass):
        # Only in the header
        return ''

class ClassMethod(ClassItem):
    def __init__(self, name, returnType, args, inline=False, static=False,
                 virtual=False, const=False, bodyInHeader=False,
                 templateArgs=None, visibility='public', body=None):
        self.returnType = returnType
        self.args = args
        self.inline = inline or bodyInHeader
        self.static = static
        self.virtual = virtual
        self.const = const
        self.bodyInHeader = bodyInHeader
        self.templateArgs = templateArgs
        self.body = body
        ClassItem.__init__(self, name, visibility)

    def getDecorators(self, declaring):
        decorators = []
        if self.inline:
            decorators.append('inline')
        if declaring:
            if self.static:
                decorators.append('static')
            if self.virtual:
                decorators.append('virtual')
        if decorators:
            return ' '.join(decorators) + ' '
        return ''

    def getBody(self):
        # Override me or pass a string to constructor
        assert self.body is not None
        return self.body

    def declare(self, cgClass):
        templateClause = 'template <%s>\n' % ', '.join(self.templateArgs) \
                         if self.bodyInHeader and self.templateArgs else ''
        args = ', '.join([str(a) for a in self.args])
        if self.bodyInHeader:
            body = '  ' + self.getBody();
            body = body.replace('\n', '\n  ').rstrip(' ')
            body = '\n{\n' + body + '\n}'
        else:
           body = ';'

        return string.Template("""${templateClause}${decorators}${returnType}
${name}(${args})${const}${body}
""").substitute({ 'templateClause': templateClause,
                  'decorators': self.getDecorators(True),
                  'returnType': self.returnType,
                  'name': self.name,
                  'const': ' const' if self.const else '',
                  'args': args,
                  'body': body })

    def define(self, cgClass):
        if self.bodyInHeader:
            return ''

        templateArgs = cgClass.templateArgs
        if templateArgs:
            if cgClass.templateSpecialization:
                templateArgs = \
                    templateArgs[len(cgClass.templateSpecialization):]

        if templateArgs:
            templateClause = \
                'template <%s>\n' % ', '.join([str(a) for a in templateArgs])
        else:
            templateClause = ''

        args = ', '.join([str(a) for a in self.args])

        body = '  ' + self.getBody()
        body = body.replace('\n', '\n  ').rstrip(' ')

        return string.Template("""${templateClause}${decorators}${returnType}
${className}::${name}(${args})${const}
{
${body}
}\n
""").substitute({ 'templateClause': templateClause,
                  'decorators': self.getDecorators(False),
                  'returnType': self.returnType,
                  'className': cgClass.getNameString(),
                  'name': self.name,
                  'args': args,
                  'const': ' const' if self.const else '',
                  'body': body })

class ClassMember(ClassItem):
    def __init__(self, name, type, visibility="private", static=False,
                 body=None):
        self.type = type;
        self.static = static
        self.body = body
        ClassItem.__init__(self, name, visibility)

    def getBody(self):
        assert self.body is not None
        return self.body

    def declare(self, cgClass):
        return '%s%s %s;\n' % ('static ' if self.static else '', self.type,
                               self.name)

    def define(self, cgClass):
        if not self.static:
            return ''
        return '%s %s::%s = %s;\n' % (self.type, cgClass.getNameString(),
                                      self.name, self.getBody())

class ClassTypedef(ClassItem):
    def __init__(self, name, type, visibility="public"):
        self.type = type
        ClassItem.__init__(self, name, visibility)

    def declare(self, cgClass):
        return 'typedef %s %s;\n' % (self.type, self.name)

    def define(self, cgClass):
        # Only goes in the header
        return ''

class ClassEnum(ClassItem):
    def __init__(self, name, entries, values=None, visibility="public"):
        self.entries = entries
        self.values = values
        ClassItem.__init__(self, name, visibility)

    def declare(self, cgClass):
        entries = []
        for i in range(0, len(self.entries)):
            if i >= len(self.values):
                entry = '%s' % self.entries[i]
            else:
                entry = '%s = %s' % (self.entries[i], self.values[i])
            entries.append(entry)
        name = '' if not self.name else ' ' + self.name
        return 'enum%s\n{\n  %s\n};\n' % (name, ',\n  '.join(entries))

    def define(self, cgClass):
        # Only goes in the header
        return ''

class CGClass(CGThing):
    def __init__(self, name, bases=[], members=[], methods=[], typedefs = [],
                 enums=[], templateArgs=[], templateSpecialization=[],
                 isStruct=False, indent=''):
        CGThing.__init__(self)
        self.name = name
        self.bases = bases
        self.members = members
        self.methods = methods
        self.typedefs = typedefs
        self.enums = enums
        self.templateArgs = templateArgs
        self.templateSpecialization = templateSpecialization
        self.isStruct = isStruct
        self.indent = indent
        self.defaultVisibility ='public' if isStruct else 'private'

    def getNameString(self):
        className = self.name
        if self.templateSpecialization:
            className = className + \
                '<%s>' % ', '.join([str(a) for a
                                    in self.templateSpecialization])
        return className

    def declare(self):
        result = ''
        if self.templateArgs:
            templateArgs = [str(a) for a in self.templateArgs]
            templateArgs = templateArgs[len(self.templateSpecialization):]
            result = result + self.indent + 'template <%s>\n' \
                     % ','.join([str(a) for a in templateArgs])

        type = 'struct' if self.isStruct else 'class'

        if self.templateSpecialization:
            specialization = \
                '<%s>' % ', '.join([str(a) for a in self.templateSpecialization])
        else:
            specialization = ''

        result = result + '%s%s %s%s' \
                 % (self.indent, type, self.name, specialization)

        if self.bases:
            result = result + ' : %s' % ', '.join([d.declare(self) for d in self.bases])

        result = result + '\n%s{\n' % self.indent

        def declareMembers(cgClass, memberList, defaultVisibility, itemCount,
                           separator=''):
            members = { 'private': [], 'protected': [], 'public': [] }

            for member in memberList:
                members[member.visibility].append(member)


            if defaultVisibility == 'public':
                order = [ 'public', 'protected', 'private' ]
            else:
                order = [ 'private', 'protected', 'public' ]

            result = ''

            lastVisibility = defaultVisibility
            for visibility in order:
                list = members[visibility]
                if list:
                    if visibility != lastVisibility:
                        if itemCount:
                            result = result + '\n'
                        result = result + visibility + ':\n'
                        itemCount = 0
                    for member in list:
                        if itemCount == 0:
                            result = result + '  '
                        else:
                            result = result + separator + '  '
                        declaration = member.declare(cgClass)
                        declaration = declaration.replace('\n', '\n  ')
                        declaration = declaration.rstrip(' ')
                        result = result + declaration
                        itemCount = itemCount + 1
                    lastVisibility = visibility
            return (result, lastVisibility, itemCount)

        order = [(self.enums, ''), (self.typedefs, ''), (self.members, ''),
                 (self.methods, '\n')]

        lastVisibility = self.defaultVisibility
        itemCount = 0
        for (memberList, separator) in order:
            (memberString, lastVisibility, itemCount) = \
                declareMembers(self, memberList, lastVisibility, itemCount,
                               separator)
            if self.indent:
                memberString = self.indent + memberString
                memberString = memberString.replace('\n', '\n' + self.indent)
                memberString = memberString.rstrip(' ')
            result = result + memberString

        result = result + self.indent + '};\n\n'
        return result

    def define(self):
        def defineMembers(cgClass, memberList, itemCount, separator=''):
            result = ''
            for member in memberList:
                if itemCount != 0:
                    result = result + separator
                result = result + member.define(cgClass)
                itemCount = itemCount + 1
            return (result, itemCount)

        order = [(self.members, '\n'), (self.methods, '\n')]

        result = ''
        itemCount = 0
        for (memberList, separator) in order:
            (memberString, itemCount) = defineMembers(self, memberList,
                                                      itemCount, separator)
            result = result + memberString
        return result

class CGPrototypeTraitsClass(CGClass):
    def __init__(self, descriptor, indent=''):
        templateArgs = [Argument('prototypes::ID', 'PrototypeID')]
        templateSpecialization = ['prototypes::id::' + descriptor.name]
        enums = [ClassEnum('', ['Depth'],
                           [descriptor.interface.inheritanceDepth()])]
        typedefs = [ClassTypedef('NativeClass', descriptor.nativeClass)]
        CGClass.__init__(self, 'PrototypeTraits', indent=indent,
                         templateArgs=templateArgs,
                         templateSpecialization=templateSpecialization,
                         enums=enums, typedefs=typedefs, isStruct=True)

class CGPrototypeIDMapClass(CGClass):
    def __init__(self, descriptor, indent=''):
        templateArgs = [Argument('class', 'ConcreteClass')]
        templateSpecialization = [descriptor.nativeClass]
        enums = [ClassEnum('', ['PrototypeID'],
                           ['prototypes::id::' + descriptor.name])]
        CGClass.__init__(self, 'PrototypeIDMap', indent=indent,
                         templateArgs=templateArgs,
                         templateSpecialization=templateSpecialization,
                         enums=enums, isStruct=True)

class CGClassForwardDeclare(CGThing):
    def __init__(self, name, isStruct=False):
        CGThing.__init__(self)
        self.name = name
        self.isStruct = isStruct
    def declare(self):
        type = 'struct' if self.isStruct else 'class'
        return '%s %s;\n' % (type, self.name)
    def define(self):
        # Header only
        return ''

def stripTrailingWhitespace(text):
    lines = text.splitlines()
    for i in range(len(lines)):
        lines[i] = lines[i].rstrip()
    return '\n'.join(lines)

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])

        # Always have a finalize hook, regardless of whether the class wants a
        # custom hook.
        cgThings.append(CGClassFinalizeHook(descriptor))

        # Only generate a trace hook if the class wants a custom hook.
        if (descriptor.customTrace):
            cgThings.append(CGClassTraceHook(descriptor))

        if descriptor.interface.hasInterfaceObject:
            cgThings.append(CGConstructorJSClass(descriptor))

        cgThings.extend([CGDOMJSClass(descriptor),
                         CGPrototypeJSClass(descriptor),
                         CGCreateProtoObjectMethod(descriptor),
                         CGIndenter(CGGetProtoObjectMethod(descriptor))])

        cgThings = CGList(cgThings)
        cgThings = CGWrapper(cgThings, post='\n')
        self.cgRoot = CGWrapper(CGNamespace(descriptor.name, cgThings),
                                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 = []

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

        # Append a Count.
        entries.append('_' + enumName + '_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, webIDLFile):
        descriptors = config.getConcreteDescriptors(webIDLFile)

        forwardDeclares = []

        for x in descriptors:
            nativeClass = x.nativeClass
            components = x.nativeClass.split('::')
            declare = CGClassForwardDeclare(components[-1])
            if len(components) > 1:
                declare = CGNamespace.build(components[:-1],
                                            CGWrapper(declare, declarePre='\n',
                                                      declarePost='\n'),
                                            declareOnly=True)
            forwardDeclares.append(CGWrapper(declare, declarePost='\n'))

        forwardDeclares = CGList(forwardDeclares)

        traitsClasses = [CGPrototypeTraitsClass(d) for d in descriptors]

        # We must have a 1:1 mapping here, skip for prototypes that have more
        # than one concrete class implementation.
        traitsClasses.extend([CGPrototypeIDMapClass(d) for d in descriptors
                              if d.uniqueImplementation])

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

        # Do codegen for all the descriptors and enums.
        cgthings = [CGWrapper(CGNamespace.build([e.identifier.name],
                                                CGEnum(e)),
                              post="\n") for e in config.getEnums(webIDLFile)]
        cgthings.extend([CGDescriptor(x) for x
                         in config.getConcreteDescriptors(webIDLFile)])
        curr = CGList(cgthings)

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

        curr = CGList([forwardDeclares, traitsClasses, curr])

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

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

        # Add the auto-generated comment.
        curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT)

        # Store the final result.
        self.root = curr

    def declare(self):
        return stripTrailingWhitespace(self.root.declare())
    def define(self):
        return stripTrailingWhitespace(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()]
        idEnum = CGNamespacedEnum('id', 'ID', protos, [0])

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

        traitsDecl = CGGeneric(declare="""
template <prototypes::ID PrototypeID>
struct PrototypeTraits;

template <class ConcreteClass>
struct PrototypeIDMap;
""")

        traitsDecl = CGNamespace.build(['mozilla', 'dom', 'bindings'],
                                        CGWrapper(traitsDecl, post='\n'))

        curr = CGList([idEnum, traitsDecl])

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

        # Add the auto-generated comment.
        curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT)

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