Skip to content

Commit 9f45d34

Browse files
committed
Only output reached decls
1 parent 2881fe8 commit 9f45d34

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/compiler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ mod tests {
138138

139139
compiler.parse(code.into(), &paths[0]);
140140
assert!(compiler.check());
141-
compiler.specialize();
141+
// compiler.specialize();
142142
assert!(compiler.decls.decls.len() > 0);
143143
compiler.run();
144144
}

src/monomorph_pass.rs

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ pub struct MonomorphPass {
1111
recursion_detector: RecursionDetector,
1212

1313
/// Newly generated specialized declarations
14-
specialized_decls: Vec<Decl>,
14+
out_decls: Vec<Decl>,
1515
}
1616

1717
impl MonomorphPass {
1818
pub fn new() -> Self {
1919
Self {
2020
instantiations: HashMap::new(),
2121
recursion_detector: RecursionDetector::new(),
22-
specialized_decls: Vec::new(),
22+
out_decls: Vec::new(),
2323
}
2424
}
2525

@@ -40,17 +40,32 @@ impl MonomorphPass {
4040
return Err(format!("Entry point function '{}' not found", entry_point));
4141
}
4242

43+
if func_decls.len() > 1 {
44+
return Err(format!(
45+
"Multiple overloads found for entry point function '{}'",
46+
entry_point
47+
));
48+
}
49+
4350
// Process each overload of the entry point
4451
for decl in func_decls {
4552
if let Decl::Func(fdecl) = decl {
46-
self.process_function(fdecl, decls)?;
53+
self.process_function(&fdecl, decls)?;
54+
self.out_decls.push(decl.clone());
55+
}
56+
}
57+
58+
for decl in decls.decls.iter() {
59+
if let Decl::Func(_) = decl {
60+
// Do nothing - functions are processed on demand
61+
} else {
62+
// Non-function declarations - include as is
63+
self.out_decls.push(decl.clone());
4764
}
4865
}
4966

5067
// Collect all declarations: original + specialized
51-
let mut all_decls = decls.decls.clone();
52-
all_decls.extend(self.specialized_decls.clone());
53-
Ok(all_decls)
68+
Ok(self.out_decls.clone())
5469
}
5570

5671
/// Process a single function, finding all generic calls within it
@@ -286,7 +301,7 @@ impl MonomorphPass {
286301
self.instantiations.insert(key, mangled_name);
287302

288303
// Add to specialized decls
289-
self.specialized_decls.push(Decl::Func(specialized.clone()));
304+
self.out_decls.push(Decl::Func(specialized.clone()));
290305

291306
// Recursively process the specialized function's body immediately
292307
self.process_function(&specialized, decls)?;
@@ -298,7 +313,7 @@ impl MonomorphPass {
298313

299314
/// Get all generated specialized declarations
300315
pub fn specialized_declarations(&self) -> &[Decl] {
301-
&self.specialized_decls
316+
&self.out_decls
302317
}
303318

304319
/// Get the mangled name for a specific instantiation, if it exists
@@ -337,7 +352,7 @@ mod tests {
337352
fn test_monomorph_pass_creation() {
338353
let pass = MonomorphPass::new();
339354
assert_eq!(pass.instantiations.len(), 0);
340-
assert_eq!(pass.specialized_decls.len(), 0);
355+
assert_eq!(pass.out_decls.len(), 0);
341356
}
342357

343358
#[test]
@@ -357,7 +372,7 @@ mod tests {
357372
assert!(result.is_ok());
358373
let mangled = result.unwrap();
359374
assert_eq!(mangled, Name::str("id$i32"));
360-
assert_eq!(pass.specialized_decls.len(), 1);
375+
assert_eq!(pass.out_decls.len(), 1);
361376
}
362377

363378
#[test]
@@ -388,7 +403,7 @@ mod tests {
388403
assert_eq!(result1.unwrap(), result2.unwrap());
389404

390405
// Should only have one specialized version
391-
assert_eq!(pass.specialized_decls.len(), 1);
406+
assert_eq!(pass.out_decls.len(), 1);
392407
}
393408

394409
#[test]
@@ -418,7 +433,7 @@ mod tests {
418433
assert_eq!(result2.unwrap(), Name::str("id$bool"));
419434

420435
// Should have two specialized versions
421-
assert_eq!(pass.specialized_decls.len(), 2);
436+
assert_eq!(pass.out_decls.len(), 2);
422437
}
423438

424439
#[test]
@@ -636,7 +651,7 @@ mod tests {
636651
).unwrap();
637652

638653
// Check the specialized declaration
639-
let specialized = &pass.specialized_decls[0];
654+
let specialized = &pass.out_decls[0];
640655
if let Decl::Func(fdecl) = specialized {
641656
assert_eq!(fdecl.name, Name::str("id$i32"));
642657
assert_eq!(fdecl.typevars.len(), 0); // No longer generic
@@ -691,7 +706,7 @@ mod tests {
691706
assert_eq!(result.unwrap(), Name::str("process$i32"));
692707

693708
// Check that the specialized version has the correct array type
694-
let specialized = &pass.specialized_decls[0];
709+
let specialized = &pass.out_decls[0];
695710
if let Decl::Func(fdecl) = specialized {
696711
assert_eq!(fdecl.params.len(), 1);
697712
if let Type::Array(elem_ty, size) = &*fdecl.params[0].ty.unwrap() {
@@ -727,7 +742,7 @@ mod tests {
727742
}
728743

729744
// Should have 3 specialized versions
730-
assert_eq!(pass.specialized_decls.len(), 3);
745+
assert_eq!(pass.out_decls.len(), 3);
731746
assert_eq!(pass.instantiations.len(), 3);
732747
}
733748

@@ -764,7 +779,7 @@ mod tests {
764779
);
765780

766781
assert!(result.is_ok());
767-
assert_eq!(pass.specialized_decls.len(), 0);
782+
assert_eq!(pass.out_decls.len(), 0);
768783
}
769784

770785
#[test]
@@ -789,7 +804,7 @@ mod tests {
789804
assert!(result.is_ok());
790805

791806
// Check that constraints are preserved (they're on the original, not the specialized)
792-
let specialized = &pass.specialized_decls[0];
807+
let specialized = &pass.out_decls[0];
793808
if let Decl::Func(fdecl) = specialized {
794809
// Specialized version should have constraints copied
795810
assert_eq!(fdecl.constraints.len(), 1);
@@ -830,7 +845,7 @@ mod tests {
830845
&decls,
831846
).unwrap();
832847

833-
assert_eq!(pass.specialized_decls.len(), 1);
848+
assert_eq!(pass.out_decls.len(), 1);
834849

835850
// Same instantiation again - should not create duplicate
836851
pass.instantiate_function(
@@ -840,7 +855,7 @@ mod tests {
840855
&decls,
841856
).unwrap();
842857

843-
assert_eq!(pass.specialized_decls.len(), 1);
858+
assert_eq!(pass.out_decls.len(), 1);
844859
assert!(pass.instantiations.contains_key(&key1));
845860
}
846861

@@ -962,8 +977,13 @@ mod tests {
962977
assert!(result.is_ok());
963978
let all_decls = result.unwrap();
964979

965-
// Should have 3 decls: id (generic), main, id$i32 (specialized)
966-
assert_eq!(all_decls.len(), 3);
980+
println!("All Decls:");
981+
for decl in &all_decls {
982+
println!("{:?}", decl.name().to_string());
983+
}
984+
985+
// Should have 2 decls: main, id$i32 (specialized)
986+
assert_eq!(all_decls.len(), 2);
967987

968988
// Find the specialized version
969989
let specialized_id = all_decls.iter().find(|d| {

0 commit comments

Comments
 (0)