1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
#![allow(non_camel_case_types)]
use libc::c_char;
use crate::arena::Handle;
use crate::graph::File;
use crate::graph::Node;
use crate::graph::NodeID;
use crate::graph::StackGraph;
use crate::graph::Symbol;
use crate::paths::Path;
use crate::paths::PathEdge;
use crate::paths::PathEdgeList;
use crate::paths::Paths;
use crate::paths::ScopeStack;
use crate::paths::ScopedSymbol;
use crate::paths::SymbolStack;
pub struct sg_stack_graph {
pub inner: StackGraph,
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_new() -> *mut sg_stack_graph {
Box::into_raw(Box::new(sg_stack_graph {
inner: StackGraph::new(),
}))
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_free(graph: *mut sg_stack_graph) {
drop(unsafe { Box::from_raw(graph) })
}
pub struct sg_path_arena {
pub inner: Paths,
}
#[no_mangle]
pub extern "C" fn sg_path_arena_new() -> *mut sg_path_arena {
Box::into_raw(Box::new(sg_path_arena {
inner: Paths::new(),
}))
}
#[no_mangle]
pub extern "C" fn sg_path_arena_free(paths: *mut sg_path_arena) {
drop(unsafe { Box::from_raw(paths) })
}
#[repr(C)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum sg_deque_direction {
SG_DEQUE_FORWARDS,
SG_DEQUE_BACKWARDS,
}
impl Default for sg_deque_direction {
fn default() -> sg_deque_direction {
sg_deque_direction::SG_DEQUE_FORWARDS
}
}
#[repr(C)]
pub struct sg_symbol {
pub symbol: *const c_char,
pub symbol_len: usize,
}
pub type sg_symbol_handle = u32;
#[repr(C)]
pub struct sg_symbols {
pub symbols: *const sg_symbol,
pub count: usize,
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_symbols(graph: *const sg_stack_graph) -> sg_symbols {
let graph = unsafe { &(*graph).inner };
sg_symbols {
symbols: graph.symbols.as_ptr() as *const sg_symbol,
count: graph.symbols.len(),
}
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_add_symbols(
graph: *mut sg_stack_graph,
count: usize,
symbols: *const *const c_char,
lengths: *const usize,
handles_out: *mut sg_symbol_handle,
) {
let graph = unsafe { &mut (*graph).inner };
let symbols = unsafe { std::slice::from_raw_parts(symbols as *const *const u8, count) };
let lengths = unsafe { std::slice::from_raw_parts(lengths, count) };
let handles_out = unsafe {
std::slice::from_raw_parts_mut(handles_out as *mut Option<Handle<Symbol>>, count)
};
for i in 0..count {
let symbol = unsafe { std::slice::from_raw_parts(symbols[i], lengths[i]) };
handles_out[i] = match std::str::from_utf8(symbol) {
Ok(symbol) => Some(graph.add_symbol(symbol)),
Err(_) => None,
};
}
}
#[repr(C)]
pub struct sg_file {
pub name: *const c_char,
pub name_len: usize,
}
pub type sg_file_handle = u32;
#[repr(C)]
pub struct sg_files {
pub files: *const sg_file,
pub count: usize,
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_files(graph: *const sg_stack_graph) -> sg_files {
let graph = unsafe { &(*graph).inner };
sg_files {
files: graph.files.as_ptr() as *const sg_file,
count: graph.files.len(),
}
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_add_files(
graph: *mut sg_stack_graph,
count: usize,
files: *const *const c_char,
lengths: *const usize,
handles_out: *mut sg_file_handle,
) {
let graph = unsafe { &mut (*graph).inner };
let files = unsafe { std::slice::from_raw_parts(files as *const *const u8, count) };
let lengths = unsafe { std::slice::from_raw_parts(lengths, count) };
let handles_out =
unsafe { std::slice::from_raw_parts_mut(handles_out as *mut Option<Handle<File>>, count) };
for i in 0..count {
let file = unsafe { std::slice::from_raw_parts(files[i], lengths[i]) };
handles_out[i] = match std::str::from_utf8(file) {
Ok(file) => Some(graph.get_or_create_file(file)),
Err(_) => None,
};
}
}
#[repr(C)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct sg_node_id {
pub file: sg_file_handle,
pub local_id: u32,
}
impl Into<NodeID> for sg_node_id {
fn into(self) -> NodeID {
unsafe { std::mem::transmute(self) }
}
}
pub const SG_ROOT_NODE_ID: u32 = 0;
pub const SG_JUMP_TO_NODE_ID: u32 = 1;
#[repr(C)]
#[derive(Clone)]
pub struct sg_node {
pub kind: sg_node_kind,
pub id: sg_node_id,
pub symbol: sg_symbol_handle,
pub scope: sg_node_handle,
pub is_clickable: bool,
}
impl Into<Node> for sg_node {
fn into(self) -> Node {
unsafe { std::mem::transmute(self) }
}
}
#[repr(C)]
#[derive(Clone, Copy)]
pub enum sg_node_kind {
SG_NODE_KIND_DROP_SCOPES,
SG_NODE_KIND_EXPORTED_SCOPE,
SG_NODE_KIND_INTERNAL_SCOPE,
SG_NODE_KIND_JUMP_TO,
SG_NODE_KIND_POP_SCOPED_SYMBOL,
SG_NODE_KIND_POP_SYMBOL,
SG_NODE_KIND_PUSH_SCOPED_SYMBOL,
SG_NODE_KIND_PUSH_SYMBOL,
SG_NODE_KIND_ROOT,
}
pub type sg_node_handle = u32;
impl Into<Handle<Node>> for sg_node_handle {
fn into(self) -> Handle<Node> {
unsafe { std::mem::transmute(self) }
}
}
pub const SG_ROOT_NODE_HANDLE: sg_node_handle = 1;
pub const SG_JUMP_TO_NODE_HANDLE: sg_node_handle = 2;
#[repr(C)]
pub struct sg_nodes {
pub nodes: *const sg_node,
pub count: usize,
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_nodes(graph: *const sg_stack_graph) -> sg_nodes {
let graph = unsafe { &(*graph).inner };
sg_nodes {
nodes: graph.nodes.as_ptr() as *const sg_node,
count: graph.nodes.len(),
}
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_add_nodes(
graph: *mut sg_stack_graph,
count: usize,
nodes: *const sg_node,
handles_out: *mut sg_node_handle,
) {
let graph = unsafe { &mut (*graph).inner };
let nodes = unsafe { std::slice::from_raw_parts(nodes, count) };
let handles_out =
unsafe { std::slice::from_raw_parts_mut(handles_out as *mut Option<Handle<Node>>, count) };
for i in 0..count {
let node_id = nodes[i].id;
handles_out[i] =
validate_node(graph, &nodes[i]).and_then(|node| graph.add_node(node_id.into(), node));
}
}
fn validate_node_id(graph: &StackGraph, node_id: sg_node_id) -> Option<()> {
if node_id.file == 0 || node_id.file >= (graph.files.len() as u32) {
return None;
}
Some(())
}
fn validate_node(graph: &StackGraph, node: &sg_node) -> Option<Node> {
if matches!(
&node.kind,
sg_node_kind::SG_NODE_KIND_JUMP_TO | sg_node_kind::SG_NODE_KIND_ROOT
) {
return None;
}
validate_node_id(graph, node.id)?;
if (node.symbol != 0)
!= matches!(
&node.kind,
sg_node_kind::SG_NODE_KIND_POP_SCOPED_SYMBOL
| sg_node_kind::SG_NODE_KIND_POP_SYMBOL
| sg_node_kind::SG_NODE_KIND_PUSH_SCOPED_SYMBOL
| sg_node_kind::SG_NODE_KIND_PUSH_SYMBOL
)
{
return None;
}
if (node.scope != 0) != matches!(&node.kind, sg_node_kind::SG_NODE_KIND_PUSH_SCOPED_SYMBOL) {
return None;
}
Some(node.clone().into())
}
#[repr(C)]
pub struct sg_edge {
pub source: sg_node_handle,
pub sink: sg_node_handle,
pub precedence: i32,
}
#[no_mangle]
pub extern "C" fn sg_stack_graph_add_edges(
graph: *mut sg_stack_graph,
count: usize,
edges: *const sg_edge,
) {
let graph = unsafe { &mut (*graph).inner };
let edges = unsafe { std::slice::from_raw_parts(edges, count) };
for i in 0..count {
let source = unsafe { std::mem::transmute(edges[i].source) };
let sink = unsafe { std::mem::transmute(edges[i].sink) };
graph.add_edge(source, sink, edges[i].precedence);
}
}
#[repr(C)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct sg_scoped_symbol {
pub symbol: sg_symbol_handle,
pub scopes: sg_scope_stack,
}
impl Into<ScopedSymbol> for sg_scoped_symbol {
fn into(self) -> ScopedSymbol {
unsafe { std::mem::transmute(self) }
}
}
#[repr(C)]
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub struct sg_symbol_stack {
pub cells: sg_symbol_stack_cell_handle,
pub length: usize,
}
impl From<SymbolStack> for sg_symbol_stack {
fn from(stack: SymbolStack) -> sg_symbol_stack {
unsafe { std::mem::transmute(stack) }
}
}
pub type sg_symbol_stack_cell_handle = u32;
pub const SG_SYMBOL_STACK_EMPTY_HANDLE: sg_symbol_stack_cell_handle = 0xffffffff;
#[repr(C)]
pub struct sg_symbol_stack_cell {
pub head: sg_scoped_symbol,
pub tail: sg_symbol_stack_cell_handle,
}
#[repr(C)]
pub struct sg_symbol_stack_cells {
pub cells: *const sg_symbol_stack_cell,
pub count: usize,
}
#[no_mangle]
pub extern "C" fn sg_path_arena_symbol_stack_cells(
paths: *const sg_path_arena,
) -> sg_symbol_stack_cells {
let paths = unsafe { &(*paths).inner };
sg_symbol_stack_cells {
cells: paths.symbol_stacks.as_ptr() as *const sg_symbol_stack_cell,
count: paths.symbol_stacks.len(),
}
}
#[no_mangle]
pub extern "C" fn sg_path_arena_add_symbol_stacks(
paths: *mut sg_path_arena,
count: usize,
mut symbols: *const sg_scoped_symbol,
lengths: *const usize,
out: *mut sg_symbol_stack,
) {
let paths = unsafe { &mut (*paths).inner };
let lengths = unsafe { std::slice::from_raw_parts(lengths, count) };
let out = unsafe { std::slice::from_raw_parts_mut(out, count) };
for i in 0..count {
let length = lengths[i];
let symbols_slice = unsafe { std::slice::from_raw_parts(symbols, length) };
let mut stack = SymbolStack::empty();
for j in (0..length).rev() {
let symbol = symbols_slice[j].into();
stack.push_front(paths, symbol);
}
out[i] = stack.into();
unsafe { symbols = symbols.add(length) };
}
}
#[repr(C)]
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub struct sg_scope_stack {
pub cells: sg_scope_stack_cell_handle,
}
impl From<ScopeStack> for sg_scope_stack {
fn from(stack: ScopeStack) -> sg_scope_stack {
unsafe { std::mem::transmute(stack) }
}
}
pub type sg_scope_stack_cell_handle = u32;
pub const SG_SCOPE_STACK_EMPTY_HANDLE: sg_scope_stack_cell_handle = 0xffffffff;
#[repr(C)]
pub struct sg_scope_stack_cell {
pub head: sg_node_handle,
pub tail: sg_scope_stack_cell_handle,
}
#[repr(C)]
pub struct sg_scope_stack_cells {
pub cells: *const sg_scope_stack_cell,
pub count: usize,
}
#[no_mangle]
pub extern "C" fn sg_path_arena_scope_stack_cells(
paths: *const sg_path_arena,
) -> sg_scope_stack_cells {
let paths = unsafe { &(*paths).inner };
sg_scope_stack_cells {
cells: paths.scope_stacks.as_ptr() as *const sg_scope_stack_cell,
count: paths.scope_stacks.len(),
}
}
#[no_mangle]
pub extern "C" fn sg_path_arena_add_scope_stacks(
paths: *mut sg_path_arena,
count: usize,
mut scopes: *const sg_node_handle,
lengths: *const usize,
out: *mut sg_scope_stack,
) {
let paths = unsafe { &mut (*paths).inner };
let lengths = unsafe { std::slice::from_raw_parts(lengths, count) };
let out = unsafe { std::slice::from_raw_parts_mut(out, count) };
for i in 0..count {
let length = lengths[i];
let scopes_slice = unsafe { std::slice::from_raw_parts(scopes, length) };
let mut stack = ScopeStack::empty();
for j in (0..length).rev() {
let node = scopes_slice[j].into();
stack.push_front(paths, node);
}
out[i] = stack.into();
unsafe { scopes = scopes.add(length) };
}
}
#[repr(C)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct sg_path_edge {
pub source_node_id: sg_node_id,
pub precedence: i32,
}
impl Into<PathEdge> for sg_path_edge {
fn into(self) -> PathEdge {
unsafe { std::mem::transmute(self) }
}
}
#[repr(C)]
#[derive(Clone, Copy, Default, Eq, PartialEq)]
pub struct sg_path_edge_list {
pub cells: sg_path_edge_list_cell_handle,
pub direction: sg_deque_direction,
pub length: usize,
}
impl From<PathEdgeList> for sg_path_edge_list {
fn from(edges: PathEdgeList) -> sg_path_edge_list {
unsafe { std::mem::transmute(edges) }
}
}
pub type sg_path_edge_list_cell_handle = u32;
pub const SG_PATH_EDGE_LIST_EMPTY_HANDLE: sg_path_edge_list_cell_handle = 0xffffffff;
#[repr(C)]
pub struct sg_path_edge_list_cell {
pub head: sg_path_edge,
pub tail: sg_path_edge_list_cell_handle,
pub reversed: sg_path_edge_list_cell_handle,
}
#[repr(C)]
pub struct sg_path_edge_list_cells {
pub cells: *const sg_path_edge_list_cell,
pub count: usize,
}
#[no_mangle]
pub extern "C" fn sg_path_arena_path_edge_list_cells(
paths: *const sg_path_arena,
) -> sg_path_edge_list_cells {
let paths = unsafe { &(*paths).inner };
sg_path_edge_list_cells {
cells: paths.path_edges.as_ptr() as *const sg_path_edge_list_cell,
count: paths.path_edges.len(),
}
}
#[no_mangle]
pub extern "C" fn sg_path_arena_add_path_edge_lists(
paths: *mut sg_path_arena,
count: usize,
mut edges: *const sg_path_edge,
lengths: *const usize,
out: *mut sg_path_edge_list,
) {
let paths = unsafe { &mut (*paths).inner };
let lengths = unsafe { std::slice::from_raw_parts(lengths, count) };
let out = unsafe { std::slice::from_raw_parts_mut(out, count) };
for i in 0..count {
let length = lengths[i];
let edges_slice = unsafe { std::slice::from_raw_parts(edges, length) };
let mut list = PathEdgeList::empty();
for j in 0..length {
let edge: PathEdge = edges_slice[j].into();
list.push_back(paths, edge);
}
let _ = list.iter(paths);
out[i] = list.into();
unsafe { edges = edges.add(length) };
}
}
#[repr(C)]
pub struct sg_path {
pub start_node: sg_node_handle,
pub end_node: sg_node_handle,
pub symbol_stack: sg_symbol_stack,
pub scope_stack: sg_scope_stack,
pub edges: sg_path_edge_list,
}
#[derive(Default)]
pub struct sg_path_list {
paths: Vec<Path>,
}
#[no_mangle]
pub extern "C" fn sg_path_list_new() -> *mut sg_path_list {
Box::into_raw(Box::new(sg_path_list::default()))
}
#[no_mangle]
pub extern "C" fn sg_path_list_free(path_list: *mut sg_path_list) {
drop(unsafe { Box::from_raw(path_list) });
}
#[no_mangle]
pub extern "C" fn sg_path_list_count(path_list: *const sg_path_list) -> usize {
let path_list = unsafe { &*path_list };
path_list.paths.len()
}
#[no_mangle]
pub extern "C" fn sg_path_list_paths(path_list: *const sg_path_list) -> *const sg_path {
let path_list = unsafe { &*path_list };
path_list.paths.as_ptr() as *const _
}
#[no_mangle]
pub extern "C" fn sg_path_arena_find_all_complete_paths(
graph: *const sg_stack_graph,
paths: *mut sg_path_arena,
starting_node_count: usize,
starting_nodes: *const sg_node_handle,
path_list: *mut sg_path_list,
) {
let graph = unsafe { &(*graph).inner };
let paths = unsafe { &mut (*paths).inner };
let starting_nodes = unsafe { std::slice::from_raw_parts(starting_nodes, starting_node_count) };
let path_list = unsafe { &mut *path_list };
paths.find_all_paths(
graph,
starting_nodes.iter().copied().map(sg_node_handle::into),
|graph, _paths, path| {
if path.is_complete(graph) {
path_list.paths.push(path);
}
},
);
}