@@ -54,6 +54,7 @@ pub struct Attribute {
54
54
}
55
55
56
56
impl Attribute {
57
+ /// Creates a new attribute with given span.
57
58
pub fn new ( value : AttributeValue , key_span : Span , value_span : Span )
58
59
-> Attribute {
59
60
Attribute {
@@ -63,6 +64,7 @@ impl Attribute {
63
64
}
64
65
}
65
66
67
+ /// Creates a new attribute with DUMMY_SP for key and value.
66
68
pub fn new_nosp ( value : AttributeValue ) -> Attribute {
67
69
Attribute {
68
70
value : value,
@@ -72,8 +74,14 @@ impl Attribute {
72
74
}
73
75
}
74
76
77
+ /// Node builder is a function that generates code based on the node content or
78
+ /// mutates other nodes.
75
79
pub type NodeBuilderFn = fn ( & mut Builder , & mut ExtCtxt , Rc < Node > ) ;
76
80
81
+ /// Subnodes is effectively an ordered map.
82
+ ///
83
+ /// We still address nodes by path for most of use cases, but we need to know
84
+ /// the original order of appearance (it makes things deterministic).
77
85
pub struct Subnodes {
78
86
by_index : Vec < Rc < Node > > ,
79
87
by_path : HashMap < String , Weak < Node > > ,
@@ -87,20 +95,28 @@ impl Subnodes {
87
95
}
88
96
}
89
97
98
+ /// Adds a node to subnodes.
99
+ ///
100
+ /// The node must not be present in the subnodes.
90
101
pub fn push ( & mut self , node : Rc < Node > ) {
91
102
let weak = node. downgrade ( ) ;
92
103
self . by_path . insert ( node. path . clone ( ) , weak) ;
93
104
self . by_index . push ( node) ;
94
105
}
95
106
107
+ /// Returns a vector representation of subnodes.
96
108
pub fn as_vec < ' a > ( & ' a self ) -> & ' a Vec < Rc < Node > > {
97
109
& self . by_index
98
110
}
99
111
112
+ /// Returns a map representation of subnodes.
100
113
pub fn as_map < ' a > ( & ' a self ) -> & ' a HashMap < String , Weak < Node > > {
101
114
& self . by_path
102
115
}
103
116
117
+ /// A helper method to move data from other subnodes into current instance.
118
+ ///
119
+ /// Used as a helper for wrapping in RefCell.
104
120
pub fn clone_from ( & mut self , other : Subnodes ) {
105
121
self . by_index = other. by_index ;
106
122
self . by_path = other. by_path ;
@@ -113,31 +129,52 @@ impl Subnodes {
113
129
/// path_span. Attributes are stored by name, subnodes are stored by path.
114
130
/// Type_name, if present, must specify the type path for the node's
115
131
/// materialized object.
132
+ ///
133
+ /// Two nodes are equal if their full paths are equal.
116
134
pub struct Node {
135
+ /// Node name, might be optional.
117
136
pub name : Option < String > ,
137
+
138
+ /// Name span if name is present or path span otherwise.
118
139
pub name_span : Span ,
119
140
141
+ /// Node path, which is unique among all sibling nodes.
120
142
pub path : String ,
143
+
144
+ /// Path span.
121
145
pub path_span : Span ,
122
146
147
+ /// A map of node's attributes.
123
148
pub attributes : RefCell < HashMap < String , Rc < Attribute > > > ,
124
- subnodes : RefCell < Subnodes > ,
125
- pub parent : Option < Weak < Node > > ,
126
149
127
- type_name : RefCell < Option < String > > ,
128
- type_params : RefCell < Vec < String > > ,
150
+ /// A weak reference to parent node, None for root nodes.
151
+ pub parent : Option < Weak < Node > > ,
129
152
130
- /// A function that materializes this node.
153
+ /// A function that materializes this node, i.e. generates some actionable
154
+ /// code.
155
+ ///
156
+ /// Materializers are exectuted in order of dependencies resolution, so having
157
+ /// a fully built tree of dependencies is a must.
131
158
pub materializer : Cell < Option < NodeBuilderFn > > ,
132
159
133
160
/// Present iff this node will modify state of any other nodes.
161
+ ///
162
+ /// Mutators are executed before materializers in no specific order.
134
163
pub mutator : Cell < Option < NodeBuilderFn > > ,
135
164
136
165
/// List of nodes that must be materialized before this node.
166
+ ///
167
+ /// Generally, a node must depend on something to be materialized. The root
168
+ /// node that all other nodes must depend on implicitly or explicitly is
169
+ /// mcu::clock, which must always be present in PT.
137
170
pub depends_on : RefCell < Vec < Weak < Node > > > ,
138
171
139
172
/// List of nodes that may be materialized before this node.
140
173
pub rev_depends_on : RefCell < Vec < Weak < Node > > > ,
174
+
175
+ subnodes : RefCell < Subnodes > ,
176
+ type_name : RefCell < Option < String > > ,
177
+ type_params : RefCell < Vec < String > > ,
141
178
}
142
179
143
180
impl Node {
@@ -160,41 +197,60 @@ impl Node {
160
197
}
161
198
}
162
199
200
+ /// Set type name for the generated struct.
201
+ ///
202
+ /// If this node generates an object in main(), type_name references the type
203
+ /// of that object, e.g. for DHT22 driver that would be
204
+ /// `zinc::drivers::dht22::DHT22`.
163
205
pub fn set_type_name ( & self , tn : String ) {
164
206
let mut borrow = self . type_name . borrow_mut ( ) ;
165
207
borrow. deref_mut ( ) . clone_from ( & Some ( tn) ) ;
166
208
}
167
209
210
+ /// Get type name for the generated struct.
168
211
pub fn type_name ( & self ) -> Option < String > {
169
212
self . type_name . borrow ( ) . clone ( )
170
213
}
171
214
215
+ /// Get type parameters for the generated object.
172
216
pub fn type_params ( & self ) -> Vec < String > {
173
217
self . type_params . borrow ( ) . clone ( )
174
218
}
175
219
220
+ /// Set type parameters for the generated object, including lifetimes.
221
+ ///
222
+ /// A default lifetime if this is going to end as a task argument is 'a. Other
223
+ /// lifetimes or fully-qualified types may be used as well. DHT22 driver uses
224
+ /// this to provide `zinc::hal::timer::Timer` and `zinc::hal::pin::GPIO` for
225
+ /// its public struct of `pub struct DHT22<'a, T, P>`.
176
226
pub fn set_type_params ( & self , params : Vec < String > ) {
177
227
let mut borrow = self . type_params . borrow_mut ( ) ;
178
228
borrow. deref_mut ( ) . clone_from ( & params) ;
179
229
}
180
230
231
+ /// Returns a cloned vec of node's subnodes.
181
232
pub fn subnodes ( & self ) -> Vec < Rc < Node > > {
182
233
self . subnodes . borrow ( ) . as_vec ( ) . clone ( )
183
234
}
184
235
236
+ /// Invokes the closure for each node from node's subnodes passing a path and
237
+ /// weak node reference.
185
238
pub fn with_subnodes_map ( & self , f: |& HashMap < String , Weak < Node > > |) {
186
239
let borrow = self . subnodes . borrow ( ) ;
187
240
f ( borrow. as_map ( ) ) ;
188
241
}
189
242
243
+ /// Sets the node's subnodes from a passed object.
190
244
pub fn set_subnodes ( & self , new : Subnodes ) {
191
245
self . subnodes . borrow_mut ( ) . clone_from ( new) ;
192
246
}
193
247
248
+ /// Returns a clones string of current node's path.
194
249
pub fn path ( & self ) -> String {
195
250
self . path . clone ( )
196
251
}
197
252
253
+ /// Returns a fully-qualified path of the current node.
198
254
pub fn full_path ( & self ) -> String {
199
255
let pp = match self . parent {
200
256
Some ( ref parent) => parent. clone ( ) . upgrade ( ) . unwrap ( ) . full_path ( ) + "::" ,
@@ -372,6 +428,9 @@ impl fmt::Show for Node {
372
428
///
373
429
/// Root nodes are stored by path in `nodes`, All the nmaed nodes are also
374
430
/// stored by name in `named`.
431
+ ///
432
+ /// TODO(farcaller): this could be really refactored into transient root node
433
+ /// object that can depend on mcu::clock.
375
434
pub struct PlatformTree {
376
435
nodes : HashMap < String , Rc < Node > > ,
377
436
named : HashMap < String , Weak < Node > > ,
0 commit comments