@@ -19,7 +19,7 @@ use lib::llvm::llvm;
19
19
use lib:: llvm:: ModuleRef ;
20
20
use lib;
21
21
use metadata:: common:: LinkMeta ;
22
- use metadata:: { encoder, cstore, filesearch, csearch, loader} ;
22
+ use metadata:: { encoder, cstore, filesearch, csearch, loader, creader } ;
23
23
use middle:: trans:: context:: CrateContext ;
24
24
use middle:: trans:: common:: gensym_name;
25
25
use middle:: ty;
@@ -40,9 +40,8 @@ use syntax::abi;
40
40
use syntax:: ast;
41
41
use syntax:: ast_map:: { PathElem , PathElems , PathName } ;
42
42
use syntax:: ast_map;
43
- use syntax:: attr;
44
43
use syntax:: attr:: AttrMetaMethods ;
45
- use syntax:: crateid :: CrateId ;
44
+ use syntax:: codemap :: Span ;
46
45
use syntax:: parse:: token;
47
46
48
47
#[ deriving( Clone , PartialEq , PartialOrd , Ord , Eq ) ]
@@ -547,18 +546,49 @@ pub mod write {
547
546
*/
548
547
549
548
// FIXME (#9639): This needs to handle non-utf8 `out_filestem` values
550
- pub fn find_crate_id ( attrs : & [ ast:: Attribute ] , out_filestem : & str ) -> CrateId {
551
- match attr:: find_crateid ( attrs) {
552
- None => from_str ( out_filestem) . unwrap_or_else ( || {
553
- let mut s = out_filestem. chars ( ) . filter ( |c| c. is_XID_continue ( ) ) ;
554
- from_str ( s. collect :: < String > ( ) . as_slice ( ) )
555
- . or ( from_str ( "rust-out" ) ) . unwrap ( )
556
- } ) ,
557
- Some ( s) => s,
549
+ pub fn find_crate_name ( sess : Option < & Session > ,
550
+ attrs : & [ ast:: Attribute ] ,
551
+ out_filestem : & str ) -> String {
552
+ use syntax:: crateid:: CrateId ;
553
+
554
+ let validate = |s : String , span : Option < Span > | {
555
+ creader:: validate_crate_name ( sess, s. as_slice ( ) , span) ;
556
+ s
557
+ } ;
558
+
559
+ let crate_name = attrs. iter ( ) . find ( |at| at. check_name ( "crate_name" ) )
560
+ . and_then ( |at| at. value_str ( ) . map ( |s| ( at, s) ) ) ;
561
+ match crate_name {
562
+ Some ( ( attr, s) ) => return validate ( s. get ( ) . to_string ( ) , Some ( attr. span ) ) ,
563
+ None => { }
564
+ }
565
+ let crate_id = attrs. iter ( ) . find ( |at| at. check_name ( "crate_id" ) )
566
+ . and_then ( |at| at. value_str ( ) . map ( |s| ( at, s) ) )
567
+ . and_then ( |( at, s) | {
568
+ from_str :: < CrateId > ( s. get ( ) ) . map ( |id| ( at, id) )
569
+ } ) ;
570
+ match crate_id {
571
+ Some ( ( attr, id) ) => {
572
+ match sess {
573
+ Some ( sess) => {
574
+ sess. span_warn ( attr. span , "the #[crate_id] attribute is \
575
+ deprecated for the \
576
+ #[crate_name] attribute") ;
577
+ }
578
+ None => { }
579
+ }
580
+ return validate ( id. name , Some ( attr. span ) )
581
+ }
582
+ None => { }
558
583
}
584
+ return validate ( from_str ( out_filestem) . unwrap_or_else ( || {
585
+ let mut s = out_filestem. chars ( ) . filter ( |c| c. is_XID_continue ( ) ) ;
586
+ from_str ( s. collect :: < String > ( ) . as_slice ( ) )
587
+ . or ( from_str ( "rust-out" ) ) . unwrap ( )
588
+ } ) , None )
559
589
}
560
590
561
- pub fn crate_id_hash ( crate_id : & CrateId ) -> String {
591
+ pub fn crate_name_hash ( sess : & Session , crate_name : & str ) -> String {
562
592
// This calculates CMH as defined above. Note that we don't use the path of
563
593
// the crate id in the hash because lookups are only done by (name/vers),
564
594
// not by path.
@@ -567,10 +597,9 @@ pub fn crate_id_hash(crate_id: &CrateId) -> String {
567
597
truncated_hash_result ( & mut s) . as_slice ( ) . slice_to ( 8 ) . to_string ( )
568
598
}
569
599
570
- // FIXME (#9639): This needs to handle non-utf8 `out_filestem` values
571
- pub fn build_link_meta ( krate : & ast:: Crate , out_filestem : & str ) -> LinkMeta {
600
+ pub fn build_link_meta ( krate : & ast:: Crate , name : String ) -> LinkMeta {
572
601
let r = LinkMeta {
573
- crateid : find_crate_id ( krate . attrs . as_slice ( ) , out_filestem ) ,
602
+ crate_name : name ,
574
603
crate_hash : Svh :: calculate ( krate) ,
575
604
} ;
576
605
info ! ( "{}" , r) ;
@@ -594,7 +623,7 @@ fn symbol_hash(tcx: &ty::ctxt,
594
623
// to be independent of one another in the crate.
595
624
596
625
symbol_hasher. reset ( ) ;
597
- symbol_hasher. input_str ( link_meta. crateid . name . as_slice ( ) ) ;
626
+ symbol_hasher. input_str ( link_meta. crate_name . as_slice ( ) ) ;
598
627
symbol_hasher. input_str ( "-" ) ;
599
628
symbol_hasher. input_str ( link_meta. crate_hash . as_str ( ) ) ;
600
629
symbol_hasher. input_str ( "-" ) ;
@@ -666,8 +695,7 @@ pub fn sanitize(s: &str) -> String {
666
695
}
667
696
668
697
pub fn mangle < PI : Iterator < PathElem > > ( mut path : PI ,
669
- hash : Option < & str > ,
670
- vers : Option < & str > ) -> String {
698
+ hash : Option < & str > ) -> String {
671
699
// Follow C++ namespace-mangling style, see
672
700
// http://en.wikipedia.org/wiki/Name_mangling for more info.
673
701
//
@@ -698,25 +726,13 @@ pub fn mangle<PI: Iterator<PathElem>>(mut path: PI,
698
726
Some ( s) => push ( & mut n, s) ,
699
727
None => { }
700
728
}
701
- match vers {
702
- Some ( s) => push ( & mut n, s) ,
703
- None => { }
704
- }
705
729
706
730
n. push_char ( 'E' ) ; // End name-sequence.
707
731
n
708
732
}
709
733
710
- pub fn exported_name ( path : PathElems , hash : & str , vers : & str ) -> String {
711
- // The version will get mangled to have a leading '_', but it makes more
712
- // sense to lead with a 'v' b/c this is a version...
713
- let vers = if vers. len ( ) > 0 && !char:: is_XID_start ( vers. char_at ( 0 ) ) {
714
- format ! ( "v{}" , vers)
715
- } else {
716
- vers. to_string ( )
717
- } ;
718
-
719
- mangle ( path, Some ( hash) , Some ( vers. as_slice ( ) ) )
734
+ pub fn exported_name ( path : PathElems , hash : & str ) -> String {
735
+ mangle ( path, Some ( hash) )
720
736
}
721
737
722
738
pub fn mangle_exported_name ( ccx : & CrateContext , path : PathElems ,
@@ -741,9 +757,7 @@ pub fn mangle_exported_name(ccx: &CrateContext, path: PathElems,
741
757
hash. push_char ( EXTRA_CHARS . as_bytes ( ) [ extra2] as char ) ;
742
758
hash. push_char ( EXTRA_CHARS . as_bytes ( ) [ extra3] as char ) ;
743
759
744
- exported_name ( path,
745
- hash. as_slice ( ) ,
746
- ccx. link_meta . crateid . version_or_default ( ) )
760
+ exported_name ( path, hash. as_slice ( ) )
747
761
}
748
762
749
763
pub fn mangle_internal_name_by_type_and_seq ( ccx : & CrateContext ,
@@ -753,15 +767,11 @@ pub fn mangle_internal_name_by_type_and_seq(ccx: &CrateContext,
753
767
let path = [ PathName ( token:: intern ( s. as_slice ( ) ) ) ,
754
768
gensym_name ( name) ] ;
755
769
let hash = get_symbol_hash ( ccx, t) ;
756
- mangle ( ast_map:: Values ( path. iter ( ) ) , Some ( hash. as_slice ( ) ) , None )
770
+ mangle ( ast_map:: Values ( path. iter ( ) ) , Some ( hash. as_slice ( ) ) )
757
771
}
758
772
759
773
pub fn mangle_internal_name_by_path_and_seq ( path : PathElems , flav : & str ) -> String {
760
- mangle ( path. chain ( Some ( gensym_name ( flav) ) . move_iter ( ) ) , None , None )
761
- }
762
-
763
- pub fn output_lib_filename ( id : & CrateId ) -> String {
764
- format ! ( "{}-{}-{}" , id. name, crate_id_hash( id) , id. version_or_default( ) )
774
+ mangle ( path. chain ( Some ( gensym_name ( flav) ) . move_iter ( ) ) , None )
765
775
}
766
776
767
777
pub fn get_cc_prog ( sess : & Session ) -> String {
@@ -803,14 +813,15 @@ fn remove(sess: &Session, path: &Path) {
803
813
pub fn link_binary ( sess : & Session ,
804
814
trans : & CrateTranslation ,
805
815
outputs : & OutputFilenames ,
806
- id : & CrateId ) -> Vec < Path > {
816
+ crate_name : & str ) -> Vec < Path > {
807
817
let mut out_filenames = Vec :: new ( ) ;
808
818
for & crate_type in sess. crate_types . borrow ( ) . iter ( ) {
809
819
if invalid_output_for_target ( sess, crate_type) {
810
820
sess. bug ( format ! ( "invalid output type `{}` for target os `{}`" ,
811
821
crate_type, sess. targ_cfg. os) . as_slice ( ) ) ;
812
822
}
813
- let out_file = link_binary_output ( sess, trans, crate_type, outputs, id) ;
823
+ let out_file = link_binary_output ( sess, trans, crate_type, outputs,
824
+ crate_name) ;
814
825
out_filenames. push ( out_file) ;
815
826
}
816
827
@@ -859,9 +870,11 @@ fn is_writeable(p: &Path) -> bool {
859
870
}
860
871
}
861
872
862
- pub fn filename_for_input ( sess : & Session , crate_type : config:: CrateType ,
863
- id : & CrateId , out_filename : & Path ) -> Path {
864
- let libname = output_lib_filename ( id) ;
873
+ pub fn filename_for_input ( sess : & Session ,
874
+ crate_type : config:: CrateType ,
875
+ name : & str ,
876
+ out_filename : & Path ) -> Path {
877
+ let libname = format ! ( "{}-{}" , name, crate_name_hash( sess, name) ) ;
865
878
match crate_type {
866
879
config:: CrateTypeRlib => {
867
880
out_filename. with_filename ( format ! ( "lib{}.rlib" , libname) )
@@ -891,13 +904,13 @@ fn link_binary_output(sess: &Session,
891
904
trans : & CrateTranslation ,
892
905
crate_type : config:: CrateType ,
893
906
outputs : & OutputFilenames ,
894
- id : & CrateId ) -> Path {
907
+ crate_name : & str ) -> Path {
895
908
let obj_filename = outputs. temp_path ( OutputTypeObject ) ;
896
909
let out_filename = match outputs. single_output_file {
897
910
Some ( ref file) => file. clone ( ) ,
898
911
None => {
899
912
let out_filename = outputs. path ( OutputTypeExe ) ;
900
- filename_for_input ( sess, crate_type, id , & out_filename)
913
+ filename_for_input ( sess, crate_type, crate_name , & out_filename)
901
914
}
902
915
} ;
903
916
0 commit comments