@@ -148,17 +148,6 @@ enum LoadResult {
148
148
Loaded ( Library ) ,
149
149
}
150
150
151
- pub struct Macros {
152
- pub macro_rules : Vec < ast:: MacroDef > ,
153
-
154
- /// An array of pairs where the first element is the name of the custom
155
- /// derive (e.g. the trait being derived) and the second element is the
156
- /// index of the definition.
157
- pub custom_derive_registrar : Option < DefIndex > ,
158
- pub svh : Svh ,
159
- pub dylib : Option < PathBuf > ,
160
- }
161
-
162
151
impl < ' a > CrateLoader < ' a > {
163
152
pub fn new ( sess : & ' a Session ,
164
153
cstore : & ' a CStore ,
@@ -554,18 +543,13 @@ impl<'a> CrateLoader<'a> {
554
543
}
555
544
}
556
545
557
- pub fn read_macros ( & mut self , item : & ast:: Item ) -> Macros {
546
+ pub fn read_macros ( & mut self , item : & ast:: Item ) -> LoadedMacros {
558
547
let ci = self . extract_crate_info ( item) . unwrap ( ) ;
559
548
let ekrate = self . read_extension_crate ( item. span , & ci) ;
560
-
561
549
let root = ekrate. metadata . get_root ( ) ;
562
550
let source_name = format ! ( "<{} macros>" , item. ident) ;
563
- let mut ret = Macros {
564
- macro_rules : Vec :: new ( ) ,
565
- custom_derive_registrar : None ,
566
- svh : root. hash ,
567
- dylib : None ,
568
- } ;
551
+ let mut macro_rules = Vec :: new ( ) ;
552
+
569
553
for def in root. macro_defs . decode ( & * ekrate. metadata ) {
570
554
// NB: Don't use parse::parse_tts_from_source_str because it parses with
571
555
// quote_depth > 0.
@@ -589,7 +573,7 @@ impl<'a> CrateLoader<'a> {
589
573
attr:: mark_used ( attr) ;
590
574
}
591
575
592
- ret . macro_rules . push ( ast:: MacroDef {
576
+ macro_rules. push ( ast:: MacroDef {
593
577
ident : ast:: Ident :: with_empty_ctxt ( def. name ) ,
594
578
id : ast:: DUMMY_NODE_ID ,
595
579
span : local_span,
@@ -602,37 +586,34 @@ impl<'a> CrateLoader<'a> {
602
586
. insert ( local_span, ( def. name . as_str ( ) . to_string ( ) , def. span ) ) ;
603
587
}
604
588
605
- match root. macro_derive_registrar {
606
- Some ( id) => ret. custom_derive_registrar = Some ( id) ,
589
+ if let Some ( id) = root. macro_derive_registrar {
590
+ let dylib = match ekrate. dylib . clone ( ) {
591
+ Some ( dylib) => dylib,
592
+ None => span_bug ! ( item. span, "proc-macro crate not dylib" ) ,
593
+ } ;
594
+ if ekrate. target_only {
595
+ let message = format ! ( "proc-macro crate is not available for \
596
+ triple `{}` (only found {})",
597
+ config:: host_triple( ) ,
598
+ self . sess. opts. target_triple) ;
599
+ self . sess . span_fatal ( item. span , & message) ;
600
+ }
607
601
602
+ // custom derive crates currently should not have any macro_rules!
603
+ // exported macros, enforced elsewhere
604
+ assert_eq ! ( macro_rules. len( ) , 0 ) ;
605
+ LoadedMacros :: ProcMacros ( self . load_derive_macros ( item, id, root. hash , dylib) )
606
+ } else {
608
607
// If this crate is not a proc-macro crate then we might be able to
609
608
// register it with the local crate store to prevent loading the
610
609
// metadata twice.
611
610
//
612
611
// If it's a proc-macro crate, though, then we definitely don't
613
612
// want to register it with the local crate store as we're just
614
613
// going to use it as we would a plugin.
615
- None => {
616
- ekrate. register ( self ) ;
617
- return ret
618
- }
619
- }
620
-
621
- self . cstore . add_used_for_derive_macros ( item) ;
622
- ret. dylib = ekrate. dylib . clone ( ) ;
623
- if ret. dylib . is_none ( ) {
624
- span_bug ! ( item. span, "proc-macro crate not dylib" ) ;
614
+ ekrate. register ( self ) ;
615
+ LoadedMacros :: MacroRules ( macro_rules)
625
616
}
626
-
627
- if ekrate. target_only {
628
- let message = format ! ( "proc-macro crate is not available for \
629
- triple `{}` (only found {})",
630
- config:: host_triple( ) ,
631
- self . sess. opts. target_triple) ;
632
- self . sess . span_fatal ( item. span , & message) ;
633
- }
634
-
635
- return ret
636
617
}
637
618
638
619
/// Load custom derive macros.
@@ -642,27 +623,28 @@ impl<'a> CrateLoader<'a> {
642
623
/// implemented as dynamic libraries, but we have a possible future where
643
624
/// custom derive (and other macro-1.1 style features) are implemented via
644
625
/// executables and custom IPC.
645
- fn load_derive_macros ( & mut self , span : Span , macros : & Macros , index : DefIndex )
626
+ fn load_derive_macros ( & mut self , item : & ast :: Item , index : DefIndex , svh : Svh , path : PathBuf )
646
627
-> Vec < ( ast:: Name , SyntaxExtension ) > {
647
628
use std:: { env, mem} ;
648
629
use proc_macro:: TokenStream ;
649
630
use proc_macro:: __internal:: Registry ;
650
631
use rustc_back:: dynamic_lib:: DynamicLibrary ;
651
632
use syntax_ext:: deriving:: custom:: CustomDerive ;
652
633
634
+ self . cstore . add_used_for_derive_macros ( item) ;
635
+
653
636
// Make sure the path contains a / or the linker will search for it.
654
- let path = macros. dylib . as_ref ( ) . unwrap ( ) ;
655
637
let path = env:: current_dir ( ) . unwrap ( ) . join ( path) ;
656
638
let lib = match DynamicLibrary :: open ( Some ( & path) ) {
657
639
Ok ( lib) => lib,
658
- Err ( err) => self . sess . span_fatal ( span, & err) ,
640
+ Err ( err) => self . sess . span_fatal ( item . span , & err) ,
659
641
} ;
660
642
661
- let sym = self . sess . generate_derive_registrar_symbol ( & macros . svh , index) ;
643
+ let sym = self . sess . generate_derive_registrar_symbol ( & svh, index) ;
662
644
let registrar = unsafe {
663
645
let sym = match lib. symbol ( & sym) {
664
646
Ok ( f) => f,
665
- Err ( err) => self . sess . span_fatal ( span, & err) ,
647
+ Err ( err) => self . sess . span_fatal ( item . span , & err) ,
666
648
} ;
667
649
mem:: transmute :: < * mut u8 , fn ( & mut Registry ) > ( sym)
668
650
} ;
@@ -1079,16 +1061,6 @@ impl<'a> middle::cstore::CrateLoader for CrateLoader<'a> {
1079
1061
}
1080
1062
1081
1063
fn load_macros ( & mut self , extern_crate : & ast:: Item ) -> LoadedMacros {
1082
- let macros = self . read_macros ( extern_crate) ;
1083
-
1084
- if let Some ( index) = macros. custom_derive_registrar {
1085
- // custom derive crates currently should not have any macro_rules!
1086
- // exported macros, enforced elsewhere
1087
- assert_eq ! ( macros. macro_rules. len( ) , 0 ) ;
1088
- let custom_derives = self . load_derive_macros ( extern_crate. span , & macros, index) ;
1089
- LoadedMacros :: ProcMacros ( custom_derives)
1090
- } else {
1091
- LoadedMacros :: MacroRules ( macros. macro_rules )
1092
- }
1064
+ self . read_macros ( extern_crate)
1093
1065
}
1094
1066
}
0 commit comments