@@ -54,6 +54,7 @@ pub struct Attribute {
5454}
5555
5656impl Attribute {
57+ /// Creates a new attribute with given span.
5758 pub fn new ( value : AttributeValue , key_span : Span , value_span : Span )
5859 -> Attribute {
5960 Attribute {
@@ -63,6 +64,7 @@ impl Attribute {
6364 }
6465 }
6566
67+ /// Creates a new attribute with DUMMY_SP for key and value.
6668 pub fn new_nosp ( value : AttributeValue ) -> Attribute {
6769 Attribute {
6870 value : value,
@@ -72,8 +74,14 @@ impl Attribute {
7274 }
7375}
7476
77+ /// Node builder is a function that generates code based on the node content or
78+ /// mutates other nodes.
7579pub type NodeBuilderFn = fn ( & mut Builder , & mut ExtCtxt , Rc < Node > ) ;
7680
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).
7785pub struct Subnodes {
7886 by_index : Vec < Rc < Node > > ,
7987 by_path : HashMap < String , Weak < Node > > ,
@@ -87,20 +95,28 @@ impl Subnodes {
8795 }
8896 }
8997
98+ /// Adds a node to subnodes.
99+ ///
100+ /// The node must not be present in the subnodes.
90101 pub fn push ( & mut self , node : Rc < Node > ) {
91102 let weak = node. downgrade ( ) ;
92103 self . by_path . insert ( node. path . clone ( ) , weak) ;
93104 self . by_index . push ( node) ;
94105 }
95106
107+ /// Returns a vector representation of subnodes.
96108 pub fn as_vec < ' a > ( & ' a self ) -> & ' a Vec < Rc < Node > > {
97109 & self . by_index
98110 }
99111
112+ /// Returns a map representation of subnodes.
100113 pub fn as_map < ' a > ( & ' a self ) -> & ' a HashMap < String , Weak < Node > > {
101114 & self . by_path
102115 }
103116
117+ /// A helper method to move data from other subnodes into current instance.
118+ ///
119+ /// Used as a helper for wrapping in RefCell.
104120 pub fn clone_from ( & mut self , other : Subnodes ) {
105121 self . by_index = other. by_index ;
106122 self . by_path = other. by_path ;
@@ -113,31 +129,52 @@ impl Subnodes {
113129/// path_span. Attributes are stored by name, subnodes are stored by path.
114130/// Type_name, if present, must specify the type path for the node's
115131/// materialized object.
132+ ///
133+ /// Two nodes are equal if their full paths are equal.
116134pub struct Node {
135+ /// Node name, might be optional.
117136 pub name : Option < String > ,
137+
138+ /// Name span if name is present or path span otherwise.
118139 pub name_span : Span ,
119140
141+ /// Node path, which is unique among all sibling nodes.
120142 pub path : String ,
143+
144+ /// Path span.
121145 pub path_span : Span ,
122146
147+ /// A map of node's attributes.
123148 pub attributes : RefCell < HashMap < String , Rc < Attribute > > > ,
124- subnodes : RefCell < Subnodes > ,
125- pub parent : Option < Weak < Node > > ,
126149
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 > > ,
129152
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.
131158 pub materializer : Cell < Option < NodeBuilderFn > > ,
132159
133160 /// Present iff this node will modify state of any other nodes.
161+ ///
162+ /// Mutators are executed before materializers in no specific order.
134163 pub mutator : Cell < Option < NodeBuilderFn > > ,
135164
136165 /// 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.
137170 pub depends_on : RefCell < Vec < Weak < Node > > > ,
138171
139172 /// List of nodes that may be materialized before this node.
140173 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 > > ,
141178}
142179
143180impl Node {
@@ -160,41 +197,60 @@ impl Node {
160197 }
161198 }
162199
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`.
163205 pub fn set_type_name ( & self , tn : String ) {
164206 let mut borrow = self . type_name . borrow_mut ( ) ;
165207 borrow. deref_mut ( ) . clone_from ( & Some ( tn) ) ;
166208 }
167209
210+ /// Get type name for the generated struct.
168211 pub fn type_name ( & self ) -> Option < String > {
169212 self . type_name . borrow ( ) . clone ( )
170213 }
171214
215+ /// Get type parameters for the generated object.
172216 pub fn type_params ( & self ) -> Vec < String > {
173217 self . type_params . borrow ( ) . clone ( )
174218 }
175219
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>`.
176226 pub fn set_type_params ( & self , params : Vec < String > ) {
177227 let mut borrow = self . type_params . borrow_mut ( ) ;
178228 borrow. deref_mut ( ) . clone_from ( & params) ;
179229 }
180230
231+ /// Returns a cloned vec of node's subnodes.
181232 pub fn subnodes ( & self ) -> Vec < Rc < Node > > {
182233 self . subnodes . borrow ( ) . as_vec ( ) . clone ( )
183234 }
184235
236+ /// Invokes the closure for each node from node's subnodes passing a path and
237+ /// weak node reference.
185238 pub fn with_subnodes_map ( & self , f: |& HashMap < String , Weak < Node > > |) {
186239 let borrow = self . subnodes . borrow ( ) ;
187240 f ( borrow. as_map ( ) ) ;
188241 }
189242
243+ /// Sets the node's subnodes from a passed object.
190244 pub fn set_subnodes ( & self , new : Subnodes ) {
191245 self . subnodes . borrow_mut ( ) . clone_from ( new) ;
192246 }
193247
248+ /// Returns a clones string of current node's path.
194249 pub fn path ( & self ) -> String {
195250 self . path . clone ( )
196251 }
197252
253+ /// Returns a fully-qualified path of the current node.
198254 pub fn full_path ( & self ) -> String {
199255 let pp = match self . parent {
200256 Some ( ref parent) => parent. clone ( ) . upgrade ( ) . unwrap ( ) . full_path ( ) + "::" ,
@@ -372,6 +428,9 @@ impl fmt::Show for Node {
372428///
373429/// Root nodes are stored by path in `nodes`, All the nmaed nodes are also
374430/// 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.
375434pub struct PlatformTree {
376435 nodes : HashMap < String , Rc < Node > > ,
377436 named : HashMap < String , Weak < Node > > ,
0 commit comments