@@ -19,7 +19,7 @@ use lib::llvm::llvm;
1919use lib:: llvm:: ModuleRef ;
2020use lib;
2121use metadata:: common:: LinkMeta ;
22- use metadata:: { encoder, cstore, filesearch, csearch, loader} ;
22+ use metadata:: { encoder, cstore, filesearch, csearch, loader, creader } ;
2323use middle:: trans:: context:: CrateContext ;
2424use middle:: trans:: common:: gensym_name;
2525use middle:: ty;
@@ -40,9 +40,8 @@ use syntax::abi;
4040use syntax:: ast;
4141use syntax:: ast_map:: { PathElem , PathElems , PathName } ;
4242use syntax:: ast_map;
43- use syntax:: attr;
4443use syntax:: attr:: AttrMetaMethods ;
45- use syntax:: crateid :: CrateId ;
44+ use syntax:: codemap :: Span ;
4645use syntax:: parse:: token;
4746
4847#[ deriving( Clone , PartialEq , PartialOrd , Ord , Eq ) ]
@@ -547,18 +546,49 @@ pub mod write {
547546 */
548547
549548// 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 => { }
558583 }
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 )
559589}
560590
561- pub fn crate_id_hash ( crate_id : & CrateId ) -> String {
591+ pub fn crate_name_hash ( sess : & Session , crate_name : & str ) -> String {
562592 // This calculates CMH as defined above. Note that we don't use the path of
563593 // the crate id in the hash because lookups are only done by (name/vers),
564594 // not by path.
@@ -567,10 +597,9 @@ pub fn crate_id_hash(crate_id: &CrateId) -> String {
567597 truncated_hash_result ( & mut s) . as_slice ( ) . slice_to ( 8 ) . to_string ( )
568598}
569599
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 {
572601 let r = LinkMeta {
573- crateid : find_crate_id ( krate . attrs . as_slice ( ) , out_filestem ) ,
602+ crate_name : name ,
574603 crate_hash : Svh :: calculate ( krate) ,
575604 } ;
576605 info ! ( "{}" , r) ;
@@ -594,7 +623,7 @@ fn symbol_hash(tcx: &ty::ctxt,
594623 // to be independent of one another in the crate.
595624
596625 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 ( ) ) ;
598627 symbol_hasher. input_str ( "-" ) ;
599628 symbol_hasher. input_str ( link_meta. crate_hash . as_str ( ) ) ;
600629 symbol_hasher. input_str ( "-" ) ;
@@ -666,8 +695,7 @@ pub fn sanitize(s: &str) -> String {
666695}
667696
668697pub fn mangle < PI : Iterator < PathElem > > ( mut path : PI ,
669- hash : Option < & str > ,
670- vers : Option < & str > ) -> String {
698+ hash : Option < & str > ) -> String {
671699 // Follow C++ namespace-mangling style, see
672700 // http://en.wikipedia.org/wiki/Name_mangling for more info.
673701 //
@@ -698,25 +726,13 @@ pub fn mangle<PI: Iterator<PathElem>>(mut path: PI,
698726 Some ( s) => push ( & mut n, s) ,
699727 None => { }
700728 }
701- match vers {
702- Some ( s) => push ( & mut n, s) ,
703- None => { }
704- }
705729
706730 n. push_char ( 'E' ) ; // End name-sequence.
707731 n
708732}
709733
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) )
720736}
721737
722738pub fn mangle_exported_name ( ccx : & CrateContext , path : PathElems ,
@@ -741,9 +757,7 @@ pub fn mangle_exported_name(ccx: &CrateContext, path: PathElems,
741757 hash. push_char ( EXTRA_CHARS . as_bytes ( ) [ extra2] as char ) ;
742758 hash. push_char ( EXTRA_CHARS . as_bytes ( ) [ extra3] as char ) ;
743759
744- exported_name ( path,
745- hash. as_slice ( ) ,
746- ccx. link_meta . crateid . version_or_default ( ) )
760+ exported_name ( path, hash. as_slice ( ) )
747761}
748762
749763pub 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,
753767 let path = [ PathName ( token:: intern ( s. as_slice ( ) ) ) ,
754768 gensym_name ( name) ] ;
755769 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 ( ) ) )
757771}
758772
759773pub 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 )
765775}
766776
767777pub fn get_cc_prog ( sess : & Session ) -> String {
@@ -803,14 +813,15 @@ fn remove(sess: &Session, path: &Path) {
803813pub fn link_binary ( sess : & Session ,
804814 trans : & CrateTranslation ,
805815 outputs : & OutputFilenames ,
806- id : & CrateId ) -> Vec < Path > {
816+ crate_name : & str ) -> Vec < Path > {
807817 let mut out_filenames = Vec :: new ( ) ;
808818 for & crate_type in sess. crate_types . borrow ( ) . iter ( ) {
809819 if invalid_output_for_target ( sess, crate_type) {
810820 sess. bug ( format ! ( "invalid output type `{}` for target os `{}`" ,
811821 crate_type, sess. targ_cfg. os) . as_slice ( ) ) ;
812822 }
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) ;
814825 out_filenames. push ( out_file) ;
815826 }
816827
@@ -859,9 +870,11 @@ fn is_writeable(p: &Path) -> bool {
859870 }
860871}
861872
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) ) ;
865878 match crate_type {
866879 config:: CrateTypeRlib => {
867880 out_filename. with_filename ( format ! ( "lib{}.rlib" , libname) )
@@ -891,13 +904,13 @@ fn link_binary_output(sess: &Session,
891904 trans : & CrateTranslation ,
892905 crate_type : config:: CrateType ,
893906 outputs : & OutputFilenames ,
894- id : & CrateId ) -> Path {
907+ crate_name : & str ) -> Path {
895908 let obj_filename = outputs. temp_path ( OutputTypeObject ) ;
896909 let out_filename = match outputs. single_output_file {
897910 Some ( ref file) => file. clone ( ) ,
898911 None => {
899912 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)
901914 }
902915 } ;
903916
0 commit comments