@@ -12,25 +12,36 @@ use util::{self, ToSanitizedSnakeCase, ToSanitizedUpperCase, BITS_PER_BYTE};
12
12
use generate:: register;
13
13
14
14
pub fn render (
15
- p : & Peripheral ,
15
+ p_original : & Peripheral ,
16
16
all_peripherals : & [ Peripheral ] ,
17
17
defaults : & Defaults ,
18
18
nightly : bool ,
19
19
) -> Result < Vec < Tokens > > {
20
20
let mut out = vec ! [ ] ;
21
21
22
+ let p_derivedfrom = p_original. derived_from . as_ref ( ) . and_then ( |s| {
23
+ all_peripherals. iter ( ) . find ( |x| x. name == * s)
24
+ } ) ;
25
+
26
+ let p_merged = p_derivedfrom. map ( |x| p_original. derive_from ( x) ) ;
27
+ let p = p_merged. as_ref ( ) . unwrap_or ( p_original) ;
28
+
29
+ if p_original. derived_from . is_some ( ) && p_derivedfrom. is_none ( ) {
30
+ eprintln ! ( "Couldn't find derivedFrom original: {} for {}, skipping" ,
31
+ p_original. derived_from. as_ref( ) . unwrap( ) , p_original. name) ;
32
+ return Ok ( out) ;
33
+ }
34
+
22
35
let name_pc = Ident :: new ( & * p. name . to_sanitized_upper_case ( ) ) ;
23
36
let address = util:: hex ( p. base_address ) ;
24
37
let description = util:: respace ( p. description . as_ref ( ) . unwrap_or ( & p. name ) ) ;
38
+ let derive_regs = p_derivedfrom. is_some ( ) && p_original. registers . is_none ( ) ;
25
39
26
40
let name_sc = Ident :: new ( & * p. name . to_sanitized_snake_case ( ) ) ;
27
- let ( base, derived) = if let Some ( base) = p. derived_from . as_ref ( ) {
28
- // TODO Verify that base exists
29
- // TODO We don't handle inheritance style `derivedFrom`, we should raise
30
- // an error in that case
31
- ( Ident :: new ( & * base. to_sanitized_snake_case ( ) ) , true )
41
+ let base = if derive_regs {
42
+ Ident :: new ( & * p_derivedfrom. unwrap ( ) . name . to_sanitized_snake_case ( ) ) ;
32
43
} else {
33
- ( name_sc. clone ( ) , false )
44
+ name_sc. clone ( ) ;
34
45
} ;
35
46
36
47
// Insert the peripheral structure
@@ -56,9 +67,9 @@ pub fn render(
56
67
}
57
68
} ) ;
58
69
59
- // Derived peripherals do not require re-implementation, and will instead
60
- // use a single definition of the non-derived version
61
- if derived {
70
+ // Derived peripherals may not require re-implementation, and will instead
71
+ // use a single definition of the non-derived version.
72
+ if derive_regs {
62
73
return Ok ( out) ;
63
74
}
64
75
0 commit comments