mirror of
https://github.com/servo/servo.git
synced 2025-07-22 23:03:42 +01:00
Generate all Derived implementations in codegen
This commit is contained in:
parent
32daa17d5c
commit
617fc08783
93 changed files with 137 additions and 1027 deletions
|
@ -5880,6 +5880,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 {
|
||||
|
@ -5917,7 +5931,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`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue