dom/bindings/Codegen.py
author Ms2ger <ms2ger@gmail.com>
Sun, 12 Feb 2012 18:03:36 +0100
changeset 86761 107a869cc8618ed6c0863b08bb49e5dd10c6aafb
parent 86760 ca2b13f5f434665d26b6ff56efc612d39ab8ee55
child 86762 b91d5bfbf6587600c194ce7c98adf945574e07ec
permissions -rw-r--r--
Wrap string return values.

# 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 *

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

        # Now find all the things we'll need as arguments because we
        # need to wrap or unwrap them.
        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()]
            typeHeaders.extend(desc.headerFile for desc in typeDescriptors)
            
        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(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 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);

        # 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.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,
                              "protoIDIndex" : "depth::" + descriptor.name,
                              "source" : source,
                              "target" : target,
                              "codeOnFailure" : codeOnFailure }

    def __str__(self):
        return string.Template("""
  {
    nsresult rv = UnwrapObject(cx, ${source}, ${protoID},
                               ${protoIDIndex}, &${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 xpc_qsThrow(cx, rv);")

def getArgumentConversionTemplate(type, descriptor):
    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 xpc_qsThrow(cx, NS_ERROR_XPC_BAD_CONVERT_JS);\n"
            "  }\n")

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

    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():
        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 not type.isPrimitive():
        return """
  // XXXbz 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 repreesntable 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 an 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 ArgumentConverter():
    """
    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):
        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
            if descriptor.concrete:
                self.replacementVariables["typeName"] = descriptor.nativeClass
            else:
                self.replacementVariables["typeName"] = descriptor.nativeInterface

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

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

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

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

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

    if type.nullable():
        raise TypeError("We don't support nullable primitive return types yet")
    
    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:
        return """
  // XXXbz need to learn to wrap other things
  return false;"""

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


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.

    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):
        self.returnType = returnType
        self.arguments = arguments
        self.nativeMethodName = nativeMethodName
        self.descriptor = descriptor
        self.idlNode = idlNode
        self.extendedAttributes = extendedAttributes

    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 unwrap_arguments(self):

        # XXXbholley - This is just proof of concept code for bz to do something
        # useful with.
        for i in range(len(self.arguments)):
            argType = self.arguments[i].type
            if argType.isInterface():
                argDescriptor = self.descriptor.getDescriptor(str(argType))
                assert self.descriptor.workers == argDescriptor.workers

        args = [ArgumentConverter(self.arguments[i], i, self.getArgv(),
                                  self.getArgc(), self.descriptor) for
                i in range(len(self.arguments))]
        return "".join([str(arg) for arg in args])

    def generate_call(self):
        nativeArgs = ["arg" + str(i) for i in range(len(self.arguments))]
        # XXXbz arguments that have to go in outparams go here?
        if self.isFallible():
            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;\n" % builtinNames[self.returnType.tag()]
        elif self.returnType.isString():
            resultDeclaration = "  nsAutoString result;\n"
        else:
            resultDeclaration = "  // XXX need to declare |result| for type %s.\n" % self.returnType

        if self.isFallible():
            rvDeclaration =  '  nsresult rv = NS_OK;\n'
            errorCheckClause = '  if (NS_FAILED(rv)) {\n    %s\n  }\n' \
                               % self.getErrorReport()
        else:
            rvDeclaration = ''
            errorCheckClause = ''

        return """
%s%s  // XXXbz need to actually make those methods exist!
#if 0
  self->%s(%s);
#endif
%s""" % (rvDeclaration, resultDeclaration, self.nativeMethodName,
         ', '.join(nativeArgs), errorCheckClause)

    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.descriptor.name, self.idlNode.identifier.name)

    def __str__(self):
        return (self.unwrap_arguments() + self.generate_call() +
                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)
    def getArgv(self):
        return "argv" if len(self.arguments) > 0 else ""
    def getArgc(self):
        return "argc"
    def unwrap_arguments(self):
        requiredArgs = len(self.arguments)
        while requiredArgs and self.arguments[requiredArgs-1].optional:
            requiredArgs -= 1
        argv = "jsval *argv = JS_ARGV(cx, vp);\n" if len(self.arguments) > 0 else ""
        return ("""
  // XXXbz is this the right place for this check?  Or should it be more
  // up-front somewhere, not per-signature?
  if (argc < %d) {
    return xpc_qsThrow(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
  }
  %s""" % (requiredArgs, argv)) + PerSignatureCall.unwrap_arguments(self)

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 str(callGenerators[0]);

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 str(GetterCall(self.attr.type, nativeMethodName, self.descriptor,
                              self.attr, self.extendedAttributes))

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 str(SetterCall(self.attr.type, nativeMethodName, self.descriptor,
                              self.attr, self.extendedAttributes))

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

        # 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, webIDLFile):

        # 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"))

        # 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="/* 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()