@@ -396,6 +396,24 @@ impl SourceContainer for SourceCode {
396
396
}
397
397
}
398
398
399
+ impl From < & str > for SourceCode {
400
+ fn from ( src : & str ) -> Self {
401
+ SourceCode {
402
+ source : src. into ( ) ,
403
+ path : "external_file.st" . into ( ) ,
404
+ }
405
+ }
406
+ }
407
+
408
+ impl From < String > for SourceCode {
409
+ fn from ( source : String ) -> Self {
410
+ SourceCode {
411
+ source,
412
+ path : "external_file.st" . into ( ) ,
413
+ }
414
+ }
415
+ }
416
+
399
417
fn create_source_code < T : Read > (
400
418
reader : & mut T ,
401
419
encoding : Option < & ' static Encoding > ,
@@ -466,6 +484,7 @@ fn compile_to_obj<T: SourceContainer>(
466
484
/// # Arguments
467
485
///
468
486
/// * `sources` - the source to be compiled
487
+ /// * `encoding` - The encoding to parse the files, None for UTF-8
469
488
/// * `output` - the location on disk to save the output
470
489
/// * `target` - an optional llvm target triple
471
490
/// If not provided, the machine's triple will be used.
@@ -489,6 +508,7 @@ pub fn compile_to_static_obj<T: SourceContainer>(
489
508
/// # Arguments
490
509
///
491
510
/// * `sources` - the source to be compiled
511
+ /// * `encoding` - The encoding to parse the files, None for UTF-8
492
512
/// * `output` - the location on disk to save the output
493
513
/// * `target` - an optional llvm target triple
494
514
/// If not provided, the machine's triple will be used.
@@ -512,6 +532,7 @@ pub fn compile_to_shared_pic_object<T: SourceContainer>(
512
532
/// # Arguments
513
533
///
514
534
/// * `sources` - the source to be compiled
535
+ /// * `encoding` - The encoding to parse the files, None for UTF-8
515
536
/// * `output` - the location on disk to save the output
516
537
/// * `target` - an optional llvm target triple
517
538
/// If not provided, the machine's triple will be used.
@@ -550,30 +571,47 @@ pub fn compile_to_bitcode<T: SourceContainer>(
550
571
}
551
572
552
573
///
553
- /// Compiles the given source into LLVM IR and returns it
574
+ /// Compiles the given source into LLVM IR and saves it to the given output location
554
575
///
555
576
/// # Arguments
556
577
///
557
578
/// * `sources` - the source to be compiled
579
+ /// * `encoding` - The encoding to parse the files, None for UTF-8
580
+ /// * `output` - The location to save the generated ir file
558
581
pub fn compile_to_ir < T : SourceContainer > (
559
582
sources : Vec < T > ,
560
583
encoding : Option < & ' static Encoding > ,
561
584
output : & str ,
562
585
) -> Result < ( ) , CompileError > {
563
- let c = Context :: create ( ) ;
564
- let code_gen = compile_module ( & c, sources, encoding) ?;
565
- let ir = code_gen. module . print_to_string ( ) . to_string ( ) ;
586
+ let ir = compile_to_string ( sources, encoding) ?;
566
587
fs:: write ( output, ir)
567
588
. map_err ( |err| CompileError :: io_write_error ( output. into ( ) , err. to_string ( ) ) )
568
589
}
569
590
591
+ ///
592
+ /// Compiles the given source into LLVM IR and returns it
593
+ ///
594
+ /// # Arguments
595
+ ///
596
+ /// * `sources` - the source to be compiled
597
+ /// * `encoding` - The encoding to parse the files, None for UTF-8
598
+ pub fn compile_to_string < T : SourceContainer > (
599
+ sources : Vec < T > ,
600
+ encoding : Option < & ' static Encoding > ,
601
+ ) -> Result < String , CompileError > {
602
+ let c = Context :: create ( ) ;
603
+ let code_gen = compile_module ( & c, sources, encoding) ?;
604
+ Ok ( code_gen. module . print_to_string ( ) . to_string ( ) )
605
+ }
606
+
570
607
///
571
608
/// Compiles the given source into a `codegen::CodeGen` using the provided context
572
609
///
573
610
/// # Arguments
574
611
///
575
612
/// * `context` - the LLVM Context to be used for the compilation
576
613
/// * `sources` - the source to be compiled
614
+ /// * `encoding` - The encoding to parse the files, None for UTF-8
577
615
pub fn compile_module < ' c , T : SourceContainer > (
578
616
context : & ' c Context ,
579
617
sources : Vec < T > ,
@@ -609,8 +647,9 @@ pub fn compile_module<'c, T: SourceContainer>(
609
647
610
648
// ### PHASE 2 ###
611
649
// annotation & validation everything
612
- type AnnotatedAst < ' a > = ( & ' a CompilationUnit , AnnotationMap ) ;
613
- let mut annotated_units: Vec < AnnotatedAst > = Vec :: new ( ) ;
650
+ // type AnnotatedAst<'a> = (&'a CompilationUnit, AnnotationMap);
651
+ let mut annotated_units: Vec < & CompilationUnit > = Vec :: new ( ) ;
652
+ let mut all_annotations = AnnotationMap :: default ( ) ;
614
653
for ( file_id, syntax_errors, unit) in all_units. iter ( ) {
615
654
let annotations = TypeAnnotator :: visit_unit ( & full_index, unit) ;
616
655
@@ -620,15 +659,17 @@ pub fn compile_module<'c, T: SourceContainer>(
620
659
report_diagnostics ( * file_id, syntax_errors. iter ( ) , & files) ?;
621
660
report_diagnostics ( * file_id, validator. diagnostics ( ) . iter ( ) , & files) ?;
622
661
623
- annotated_units. push ( ( unit, annotations) ) ;
662
+ annotated_units. push ( unit) ;
663
+ all_annotations. import ( annotations) ;
624
664
}
625
665
626
666
// ### PHASE 3 ###
627
667
// - codegen
628
668
let code_generator = codegen:: CodeGen :: new ( context, "main" ) ;
629
-
630
- for ( unit, annotations) in annotated_units {
631
- code_generator. generate ( unit, & annotations, & full_index) ?;
669
+ //Associate the index type with LLVM types
670
+ let llvm_index = code_generator. generate_llvm_index ( & all_annotations, & full_index) ?;
671
+ for unit in annotated_units {
672
+ code_generator. generate ( unit, & all_annotations, & full_index, & llvm_index) ?;
632
673
}
633
674
Ok ( code_generator)
634
675
}
@@ -667,6 +708,8 @@ fn report_diagnostics(
667
708
668
709
#[ cfg( test) ]
669
710
mod tests {
711
+ mod multi_files;
712
+
670
713
use inkwell:: targets:: TargetMachine ;
671
714
672
715
use crate :: { create_source_code, get_target_triple} ;
0 commit comments