33use std:: borrow:: Cow ;
44use std:: collections:: btree_map:: Entry ;
55use std:: collections:: { BTreeMap , BTreeSet } ;
6- use std:: path:: PathBuf ;
76
87use itertools:: Itertools ;
98use proc_macro2:: Span ;
@@ -71,9 +70,6 @@ pub struct FlatGraphBuilder {
7170 /// Use statements.
7271 uses : Vec < ItemUse > ,
7372
74- /// In order to make import!() statements relative to the current file, we need to know where the file is that is building the flat graph.
75- invocating_file_path : PathBuf ,
76-
7773 /// If the flat graph is being loaded as a module, then two initial ModuleBoundary nodes are inserted into the graph. One
7874 /// for the input into the module and one for the output out of the module.
7975 module_boundary_nodes : Option < ( GraphNodeId , GraphNodeId ) > ,
@@ -86,37 +82,8 @@ impl FlatGraphBuilder {
8682 }
8783
8884 /// Convert the Hydroflow code AST into a graph builder.
89- pub fn from_hfcode ( input : HfCode , macro_invocation_path : PathBuf ) -> Self {
90- let mut builder = Self {
91- invocating_file_path : macro_invocation_path,
92- ..Default :: default ( )
93- } ;
94- builder. process_statements ( input. statements ) ;
95- builder
96- }
97-
98- /// Convert the Hydroflow code AST into a graph builder.
99- pub fn from_hfmodule ( input : HfCode , root_path : PathBuf ) -> Self {
85+ pub fn from_hfcode ( input : HfCode ) -> Self {
10086 let mut builder = Self :: default ( ) ;
101- builder. invocating_file_path = root_path; // imports inside of modules should be relative to the importing file.
102- builder. module_boundary_nodes = Some ( (
103- builder. flat_graph . insert_node (
104- GraphNode :: ModuleBoundary {
105- input : true ,
106- import_expr : Span :: call_site ( ) ,
107- } ,
108- Some ( Ident :: new ( "input" , Span :: call_site ( ) ) ) ,
109- None ,
110- ) ,
111- builder. flat_graph . insert_node (
112- GraphNode :: ModuleBoundary {
113- input : false ,
114- import_expr : Span :: call_site ( ) ,
115- } ,
116- Some ( Ident :: new ( "output" , Span :: call_site ( ) ) ) ,
117- None ,
118- ) ,
119- ) ) ;
12087 builder. process_statements ( input. statements ) ;
12188 builder
12289 }
@@ -276,118 +243,7 @@ impl FlatGraphBuilder {
276243 out : Some ( ( PortIndexValue :: Elided ( op_span) , GraphDet :: Determined ( nid) ) ) ,
277244 }
278245 }
279- Pipeline :: Import ( import) => {
280- // TODO: https://github.com/rust-lang/rfcs/pull/3200
281- // this would be way better...
282- let file_path = {
283- let mut dir = self . invocating_file_path . clone ( ) ;
284- dir. pop ( ) ;
285- dir. join ( import. filename . value ( ) )
286- } ;
287-
288- let file_contents = match std:: fs:: read_to_string ( & file_path) {
289- Ok ( contents) => contents,
290- Err ( err) => {
291- self . diagnostics . push ( Diagnostic :: spanned (
292- import. filename . span ( ) ,
293- Level :: Error ,
294- format ! ( "filename: {}, err: {err}" , import. filename. value( ) ) ,
295- ) ) ;
296-
297- return Ends {
298- inn : None ,
299- out : None ,
300- } ;
301- }
302- } ;
303-
304- let statements = match syn:: parse_str :: < HfCode > ( & file_contents) {
305- Ok ( code) => code,
306- Err ( err) => {
307- self . diagnostics . push ( Diagnostic :: spanned (
308- import. span ( ) ,
309- Level :: Error ,
310- format ! ( "Error in module: {}" , err) ,
311- ) ) ;
312-
313- return Ends {
314- inn : None ,
315- out : None ,
316- } ;
317- }
318- } ;
319-
320- let flat_graph_builder = FlatGraphBuilder :: from_hfmodule ( statements, file_path) ;
321- let ( flat_graph, _uses, diagnostics) = flat_graph_builder. build ( ) ;
322- diagnostics. iter ( ) . for_each ( Diagnostic :: emit) ;
323-
324- self . merge_in ( flat_graph, import. span ( ) )
325- }
326- }
327- }
328-
329- /// Merge one flatgraph into the current flatgraph
330- /// other must be a flatgraph and not be partitioned yet.
331- fn merge_in ( & mut self , other : HydroflowGraph , parent_span : Span ) -> Ends {
332- assert_eq ! ( other. subgraphs( ) . count( ) , 0 ) ;
333-
334- let mut ends = Ends {
335- inn : None ,
336- out : None ,
337- } ;
338-
339- let mut node_mapping = BTreeMap :: new ( ) ;
340-
341- for ( other_node_id, node) in other. nodes ( ) {
342- match node {
343- GraphNode :: Operator ( _) => {
344- let varname = other. node_varname ( other_node_id) ;
345- let new_id = self . flat_graph . insert_node ( node. clone ( ) , varname, None ) ;
346- node_mapping. insert ( other_node_id, new_id) ;
347- }
348- GraphNode :: ModuleBoundary { input, .. } => {
349- let new_id = self . flat_graph . insert_node (
350- GraphNode :: ModuleBoundary {
351- input : * input,
352- import_expr : parent_span,
353- } ,
354- Some ( Ident :: new ( & format ! ( "module_{}" , input) , parent_span) ) ,
355- None ,
356- ) ;
357- node_mapping. insert ( other_node_id, new_id) ;
358-
359- // in the case of nested imports, this module boundary might not be the module boundary into or out of the top-most module
360- // So we have to be careful to only target those two boundaries.
361- // There should be no inputs to it, if it is an input boundary, if it is the top-most one.
362- // and there should be no outputs from it, if it is an output boundary, if it is the top-most one.
363- if * input && other. node_predecessor_nodes ( other_node_id) . count ( ) == 0 {
364- if other. node_predecessor_nodes ( other_node_id) . count ( ) == 0 {
365- ends. inn =
366- Some ( ( PortIndexValue :: Elided ( None ) , GraphDet :: Determined ( new_id) ) ) ;
367- }
368- } else if !( * input) && other. node_successor_nodes ( other_node_id) . count ( ) == 0 {
369- ends. out =
370- Some ( ( PortIndexValue :: Elided ( None ) , GraphDet :: Determined ( new_id) ) ) ;
371- }
372- }
373- GraphNode :: Handoff { .. } => {
374- panic ! ( "Handoff in graph that is being merged into self" )
375- }
376- }
377- }
378-
379- for ( other_edge_id, ( other_src, other_dst) ) in other. edges ( ) {
380- let ( src_port, dst_port) = other. edge_ports ( other_edge_id) ;
381-
382- let _new_edge_id = self . flat_graph . insert_edge (
383- * node_mapping. get ( & other_src) . unwrap ( ) ,
384- src_port. clone ( ) ,
385- * node_mapping. get ( & other_dst) . unwrap ( ) ,
386- dst_port. clone ( ) ,
387- ) ;
388246 }
389-
390- ends
391247 }
392248
393249 /// Connects operator links as a final building step. Processes all the links stored in
0 commit comments