Auto merge of #8020 - nox:codegen-derived, r=Ms2ger

Generate all Derived implementations in codegen

Follow-up of #7873.

@Ms2ger r? :)

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8020)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2015-10-15 12:53:08 -06:00
commit 417cf5738e
94 changed files with 230 additions and 1178 deletions

View file

@ -5885,6 +5885,20 @@ impl %(name)sCast {
hierarchy[descriptor.getParentName()].append(name)
# Define a `FooDerived` trait for superclasses to implement,
# as well as the `FooCast::to_*` methods that use it.
baseName = descriptor.prototypeChain[0]
typeIdPat = descriptor.prototypeChain[-1]
if upcast:
typeIdPat += "(_)"
for base in reversed(descriptor.prototypeChain[1:-1]):
typeIdPat = "%s(%sTypeId::%s)" % (base, base, typeIdPat)
typeIdPat = "%sTypeId::%s" % (baseName, typeIdPat)
args = {
'baseName': baseName,
'derivedTrait': name + 'Derived',
'methodName': 'is_' + name.lower(),
'name': name,
'typeIdPat': typeIdPat,
}
allprotos.append(CGGeneric("""\
/// Types which `%(name)s` derives from
pub trait %(derivedTrait)s: Sized {
@ -5922,7 +5936,16 @@ impl %(name)sCast {
}
}
""" % {'derivedTrait': name + 'Derived', 'name': name, 'methodName': 'is_' + name.lower()}))
impl %(derivedTrait)s for %(baseName)s {
fn %(methodName)s(&self) -> bool {
match *self.type_id() {
%(typeIdPat)s => true,
_ => false,
}
}
}
""" % args))
# Implement the `FooDerived` trait for non-root superclasses by deferring to
# the direct superclass. This leaves the implementation of the `FooDerived`