Make TopTypeId an untagged union

This commit is contained in:
Anthony Ramine 2016-09-11 15:10:23 +02:00
parent 5f5266a3e5
commit c5ab84638d
2 changed files with 22 additions and 14 deletions

View file

@ -1984,15 +1984,15 @@ def DOMClassTypeId(desc):
inner = "" inner = ""
if desc.hasDescendants(): if desc.hasDescendants():
if desc.interface.getExtendedAttribute("Abstract"): if desc.interface.getExtendedAttribute("Abstract"):
return "::dom::bindings::codegen::InheritTypes::TopTypeId::Abstract" return "::dom::bindings::codegen::InheritTypes::TopTypeId { abstract_: () }"
name = desc.interface.identifier.name name = desc.interface.identifier.name
inner = "(::dom::bindings::codegen::InheritTypes::%sTypeId::%s)" % (name, name) inner = "(::dom::bindings::codegen::InheritTypes::%sTypeId::%s)" % (name, name)
elif len(protochain) == 1: elif len(protochain) == 1:
return "::dom::bindings::codegen::InheritTypes::TopTypeId::Alone" return "::dom::bindings::codegen::InheritTypes::TopTypeId { alone: () }"
reversed_protochain = list(reversed(protochain)) reversed_protochain = list(reversed(protochain))
for (child, parent) in zip(reversed_protochain, reversed_protochain[1:]): for (child, parent) in zip(reversed_protochain, reversed_protochain[1:]):
inner = "(::dom::bindings::codegen::InheritTypes::%sTypeId::%s%s)" % (parent, child, inner) inner = "(::dom::bindings::codegen::InheritTypes::%sTypeId::%s%s)" % (parent, child, inner)
return "::dom::bindings::codegen::InheritTypes::TopTypeId::%s%s" % (protochain[0], inner) return "::dom::bindings::codegen::InheritTypes::TopTypeId { %s: %s }" % (protochain[0].lower(), inner)
def DOMClass(descriptor): def DOMClass(descriptor):
@ -6931,18 +6931,26 @@ class GlobalGenRoots():
typeIdCode = [] typeIdCode = []
topTypeVariants = [ topTypeVariants = [
("ID used by abstract interfaces.", "Abstract"), ("ID used by abstract interfaces.", "pub abstract_: ()"),
("ID used by interfaces that are not castable.", "Alone"), ("ID used by interfaces that are not castable.", "pub alone: ()"),
] ]
topTypeVariants += [ topTypeVariants += [
("ID used by interfaces that derive from %s." % typeName, "%s(%sTypeId)" % (typeName, typeName)) ("ID used by interfaces that derive from %s." % typeName,
"pub %s: %sTypeId" % (typeName.lower(), typeName))
for typeName in topTypes for typeName in topTypes
] ]
topTypeVariantsAsStrings = [CGGeneric("/// %s\n%s," % variant) for variant in topTypeVariants] topTypeVariantsAsStrings = [CGGeneric("/// %s\n%s," % variant) for variant in topTypeVariants]
typeIdCode.append(CGWrapper(CGIndenter(CGList(topTypeVariantsAsStrings, "\n"), 4), typeIdCode.append(CGWrapper(CGIndenter(CGList(topTypeVariantsAsStrings, "\n"), 4),
pre="#[derive(Clone, Copy, Debug)]\npub enum TopTypeId {\n", pre="#[derive(Copy)]\npub union TopTypeId {\n",
post="\n}\n\n")) post="\n}\n\n"))
typeIdCode.append(CGGeneric("""\
impl Clone for TopTypeId {
fn clone(&self) -> Self { *self }
}
"""))
def type_id_variant(name): def type_id_variant(name):
# If `name` is present in the hierarchy keys', that means some other interfaces # If `name` is present in the hierarchy keys', that means some other interfaces
# derive from it and this enum variant should have an argument with its own # derive from it and this enum variant should have an argument with its own
@ -6962,17 +6970,16 @@ class GlobalGenRoots():
typeIdCode.append(CGGeneric("""\ typeIdCode.append(CGGeneric("""\
impl %(base)s { impl %(base)s {
pub fn type_id(&self) -> &'static %(base)sTypeId { pub fn type_id(&self) -> &'static %(base)sTypeId {
let domclass = unsafe { unsafe {
get_dom_class(self.reflector().get_jsobject().get()).unwrap() &get_dom_class(self.reflector().get_jsobject().get())
}; .unwrap()
match domclass.type_id { .type_id
TopTypeId::%(base)s(ref type_id) => type_id, .%(field)s
_ => unreachable!(),
} }
} }
} }
""" % {'base': base})) """ % {'base': base, 'field': base.lower()}))
curr = CGList(imports + typeIdCode + allprotos) curr = CGList(imports + typeIdCode + allprotos)
curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT) curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT)

View file

@ -19,6 +19,7 @@
#![feature(stmt_expr_attributes)] #![feature(stmt_expr_attributes)]
#![feature(try_borrow)] #![feature(try_borrow)]
#![feature(try_from)] #![feature(try_from)]
#![feature(untagged_unions)]
#![deny(unsafe_code)] #![deny(unsafe_code)]
#![allow(non_snake_case)] #![allow(non_snake_case)]