dom/bindings/Codegen.py
author Bobby Holley <bobbyholley@gmail.com>
Wed, 08 Feb 2012 13:46:41 -0800
changeset 86464 979451d9d4167e15d2eb787458652e673877e394
parent 86463 bab09b5e0e9fb52b17668a3aac86e1baf27b3138
child 86465 3d27529bc495956a38f878bb559b242bd540bd6e
permissions -rw-r--r--
Add functionality for getting the correct descriptor for an interface given context (ie, worker vs non-worker).

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

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

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

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

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

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

%s
%s

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

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

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

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

builtinNames = {
    IDLType.Tags.bool: 'bool',
    IDLType.Tags.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'
}

def getArgumentConversionTemplate(type):
    if type.isInterface():
        # XXXbz need more checking to figure out what to do.  In particular,
        # need to know here how interface names map to internal types
        return """
  // XXXbz Need conversion for argument type '%s'""" % type

    # XXXbz handle strings here eventually.  Need to figure
    # out string behavior?  Also, how to detect them?
#            return """
#  xpc_qsDOMString arg%(index)d(cx, %(argVal)s, %(argPtr)s,
#                        xpc_qsDOMString::eDefaultNullBehavior,
#                        xpc_qsDOMString::eDefaultUndefinedBehavior);
#  if (!arg%(index)d.IsValid()) {
#    return false;
#  }"""

    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):
        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"])
        if argument.type.isPrimitive():
            self.replacementVariables["typeName"] = builtinNames[argument.type.tag()]

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

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

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

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

    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(result));
  return true;"""

    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(result), ${jsvalPtr});"""

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

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

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


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

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

    We also need to know whether this is a method or a getter/setter
    to do error reporting correctly.

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

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

    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()) for
                i in range(len(self.arguments))]
        return "".join([str(arg) for arg in args])

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

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

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

    def getErrorReport(self):
        return """
    return xpc_qsThrowMethodFailedWithDetails(cx, rv, "%s", "%s");""" % (self.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):
        PerSignatureCall.__init__(self, returnType, arguments, nativeMethodName,
                                  descriptor, method)
    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):
        PerSignatureCall.__init__(self, returnType, arguments, nativeMethodName,
                                  descriptor, attr)
    def getArgv(self):
        return "vp"

class GetterCall(GetterSetterCall):
    def __init__(self, returnType, nativeMethodName,
                 descriptor, attr):
        GetterSetterCall.__init__(self, returnType, [], nativeMethodName,
                                  descriptor, attr)
    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):
        GetterSetterCall.__init__(self, None, [FakeArgument(argType)],
                                  nativeMethodName, descriptor, attr)
    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):
        returnStr = ' false' if self.returnType == 'JSBool' else ''
        return """
  %s *self;
  if (!UnwrapThis(cx, obj, %s, %s, &self))
    return%s;
""" % (self.descriptor.nativeClass, "id::" + self.descriptor.name,
       "depth::" + self.descriptor.name, returnStr)

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

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

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

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

        return str(callGenerators[0]);

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

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

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.
        curr = CGList([CGDescriptor(x) for x in config.getConcreteDescriptors(webIDLFile)])

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