9
9
// except according to those terms.
10
10
11
11
use bridge:: { server, TokenTree } ;
12
- use { Delimiter , Level , LineColumn , Spacing , __internal } ;
12
+ use { Delimiter , Level , LineColumn , Spacing } ;
13
13
14
14
use rustc_data_structures:: sync:: Lrc ;
15
15
use rustc_errors:: { self as errors, Diagnostic , DiagnosticBuilder } ;
16
16
use std:: ascii;
17
17
use std:: ops:: Bound ;
18
18
use syntax:: ast;
19
+ use syntax:: ext:: base:: ExtCtxt ;
19
20
use syntax:: parse:: lexer:: comments;
20
- use syntax:: parse:: { self , token} ;
21
+ use syntax:: parse:: { self , token, ParseSess } ;
21
22
use syntax:: tokenstream:: { self , DelimSpan , TokenStream } ;
23
+ use syntax_pos:: hygiene:: { SyntaxContext , Transparency } ;
22
24
use syntax_pos:: symbol:: { keywords, Symbol } ;
23
25
use syntax_pos:: { BytePos , FileName , MultiSpan , Pos , SourceFile , Span } ;
24
26
@@ -43,7 +45,7 @@ impl Delimiter {
43
45
}
44
46
45
47
impl TokenTree < Group , Punct , Ident , Literal > {
46
- fn from_internal ( stream : TokenStream , stack : & mut Vec < Self > ) -> Self {
48
+ fn from_internal ( stream : TokenStream , sess : & ParseSess , stack : & mut Vec < Self > ) -> Self {
47
49
use syntax:: parse:: token:: * ;
48
50
49
51
let ( tree, joint) = stream. as_tree ( ) ;
@@ -188,14 +190,14 @@ impl TokenTree<Group, Punct, Ident, Literal> {
188
190
} )
189
191
}
190
192
191
- Interpolated ( _) => __internal :: with_sess ( |sess , _| {
193
+ Interpolated ( _) => {
192
194
let stream = token. interpolated_to_tokenstream ( sess, span) ;
193
195
TokenTree :: Group ( Group {
194
196
delimiter : Delimiter :: None ,
195
197
stream,
196
198
span : DelimSpan :: from_single ( span) ,
197
199
} )
198
- } ) ,
200
+ }
199
201
200
202
DotEq => op ! ( '.' , '=' ) ,
201
203
OpenDelim ( ..) | CloseDelim ( ..) => unreachable ! ( ) ,
@@ -337,9 +339,31 @@ pub struct Literal {
337
339
span : Span ,
338
340
}
339
341
340
- pub struct Rustc ;
342
+ pub struct Rustc < ' a > {
343
+ sess : & ' a ParseSess ,
344
+ def_site : Span ,
345
+ call_site : Span ,
346
+ }
347
+
348
+ impl < ' a > Rustc < ' a > {
349
+ pub fn new ( cx : & ' a ExtCtxt ) -> Self {
350
+ // No way to determine def location for a proc macro right now, so use call location.
351
+ let location = cx. current_expansion . mark . expn_info ( ) . unwrap ( ) . call_site ;
352
+ let to_span = |transparency| {
353
+ location. with_ctxt (
354
+ SyntaxContext :: empty ( )
355
+ . apply_mark_with_transparency ( cx. current_expansion . mark , transparency) ,
356
+ )
357
+ } ;
358
+ Rustc {
359
+ sess : cx. parse_sess ,
360
+ def_site : to_span ( Transparency :: Opaque ) ,
361
+ call_site : to_span ( Transparency :: Transparent ) ,
362
+ }
363
+ }
364
+ }
341
365
342
- impl server:: Types for Rustc {
366
+ impl server:: Types for Rustc < ' _ > {
343
367
type TokenStream = TokenStream ;
344
368
type TokenStreamBuilder = tokenstream:: TokenStreamBuilder ;
345
369
type TokenStreamIter = TokenStreamIter ;
@@ -353,22 +377,20 @@ impl server::Types for Rustc {
353
377
type Span = Span ;
354
378
}
355
379
356
- impl server:: TokenStream for Rustc {
380
+ impl server:: TokenStream for Rustc < ' _ > {
357
381
fn new ( & mut self ) -> Self :: TokenStream {
358
382
TokenStream :: empty ( )
359
383
}
360
384
fn is_empty ( & mut self , stream : & Self :: TokenStream ) -> bool {
361
385
stream. is_empty ( )
362
386
}
363
387
fn from_str ( & mut self , src : & str ) -> Self :: TokenStream {
364
- :: __internal:: with_sess ( |sess, data| {
365
- parse:: parse_stream_from_source_str (
366
- FileName :: ProcMacroSourceCode ,
367
- src. to_string ( ) ,
368
- sess,
369
- Some ( data. call_site ) ,
370
- )
371
- } )
388
+ parse:: parse_stream_from_source_str (
389
+ FileName :: ProcMacroSourceCode ,
390
+ src. to_string ( ) ,
391
+ self . sess ,
392
+ Some ( self . call_site ) ,
393
+ )
372
394
}
373
395
fn to_string ( & mut self , stream : & Self :: TokenStream ) -> String {
374
396
stream. to_string ( )
@@ -387,7 +409,7 @@ impl server::TokenStream for Rustc {
387
409
}
388
410
}
389
411
390
- impl server:: TokenStreamBuilder for Rustc {
412
+ impl server:: TokenStreamBuilder for Rustc < ' _ > {
391
413
fn new ( & mut self ) -> Self :: TokenStreamBuilder {
392
414
tokenstream:: TokenStreamBuilder :: new ( )
393
415
}
@@ -399,15 +421,15 @@ impl server::TokenStreamBuilder for Rustc {
399
421
}
400
422
}
401
423
402
- impl server:: TokenStreamIter for Rustc {
424
+ impl server:: TokenStreamIter for Rustc < ' _ > {
403
425
fn next (
404
426
& mut self ,
405
427
iter : & mut Self :: TokenStreamIter ,
406
428
) -> Option < TokenTree < Self :: Group , Self :: Punct , Self :: Ident , Self :: Literal > > {
407
429
loop {
408
430
let tree = iter. stack . pop ( ) . or_else ( || {
409
431
let next = iter. cursor . next_as_stream ( ) ?;
410
- Some ( TokenTree :: from_internal ( next, & mut iter. stack ) )
432
+ Some ( TokenTree :: from_internal ( next, self . sess , & mut iter. stack ) )
411
433
} ) ?;
412
434
// HACK: The condition "dummy span + group with empty delimiter" represents an AST
413
435
// fragment approximately converted into a token stream. This may happen, for
@@ -426,7 +448,7 @@ impl server::TokenStreamIter for Rustc {
426
448
}
427
449
}
428
450
429
- impl server:: Group for Rustc {
451
+ impl server:: Group for Rustc < ' _ > {
430
452
fn new ( & mut self , delimiter : Delimiter , stream : Self :: TokenStream ) -> Self :: Group {
431
453
Group {
432
454
delimiter,
@@ -454,7 +476,7 @@ impl server::Group for Rustc {
454
476
}
455
477
}
456
478
457
- impl server:: Punct for Rustc {
479
+ impl server:: Punct for Rustc < ' _ > {
458
480
fn new ( & mut self , ch : char , spacing : Spacing ) -> Self :: Punct {
459
481
Punct {
460
482
ch,
@@ -480,7 +502,7 @@ impl server::Punct for Rustc {
480
502
}
481
503
}
482
504
483
- impl server:: Ident for Rustc {
505
+ impl server:: Ident for Rustc < ' _ > {
484
506
fn new ( & mut self , string : & str , span : Self :: Span , is_raw : bool ) -> Self :: Ident {
485
507
let sym = Symbol :: intern ( string) ;
486
508
if is_raw
@@ -499,7 +521,7 @@ impl server::Ident for Rustc {
499
521
}
500
522
}
501
523
502
- impl server:: Literal for Rustc {
524
+ impl server:: Literal for Rustc < ' _ > {
503
525
// FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
504
526
fn debug ( & mut self , literal : & Self :: Literal ) -> String {
505
527
format ! ( "{:?}" , literal)
@@ -616,7 +638,7 @@ impl server::Literal for Rustc {
616
638
}
617
639
}
618
640
619
- impl server:: SourceFile for Rustc {
641
+ impl < ' a > server:: SourceFile for Rustc < ' a > {
620
642
fn eq ( & mut self , file1 : & Self :: SourceFile , file2 : & Self :: SourceFile ) -> bool {
621
643
Lrc :: ptr_eq ( file1, file2)
622
644
}
@@ -634,7 +656,7 @@ impl server::SourceFile for Rustc {
634
656
}
635
657
}
636
658
637
- impl server:: MultiSpan for Rustc {
659
+ impl server:: MultiSpan for Rustc < ' _ > {
638
660
fn new ( & mut self ) -> Self :: MultiSpan {
639
661
vec ! [ ]
640
662
}
@@ -643,7 +665,7 @@ impl server::MultiSpan for Rustc {
643
665
}
644
666
}
645
667
646
- impl server:: Diagnostic for Rustc {
668
+ impl server:: Diagnostic for Rustc < ' _ > {
647
669
fn new ( & mut self , level : Level , msg : & str , spans : Self :: MultiSpan ) -> Self :: Diagnostic {
648
670
let mut diag = Diagnostic :: new ( level. to_internal ( ) , msg) ;
649
671
diag. set_span ( MultiSpan :: from_spans ( spans) ) ;
@@ -659,24 +681,22 @@ impl server::Diagnostic for Rustc {
659
681
diag. sub ( level. to_internal ( ) , msg, MultiSpan :: from_spans ( spans) , None ) ;
660
682
}
661
683
fn emit ( & mut self , diag : Self :: Diagnostic ) {
662
- :: __internal:: with_sess ( move |sess, _| {
663
- DiagnosticBuilder :: new_diagnostic ( & sess. span_diagnostic , diag) . emit ( )
664
- } ) ;
684
+ DiagnosticBuilder :: new_diagnostic ( & self . sess . span_diagnostic , diag) . emit ( )
665
685
}
666
686
}
667
687
668
- impl server:: Span for Rustc {
688
+ impl server:: Span for Rustc < ' _ > {
669
689
fn debug ( & mut self , span : Self :: Span ) -> String {
670
690
format ! ( "{:?} bytes({}..{})" , span. ctxt( ) , span. lo( ) . 0 , span. hi( ) . 0 )
671
691
}
672
692
fn def_site ( & mut self ) -> Self :: Span {
673
- :: __internal :: with_sess ( |_ , data| data . def_site )
693
+ self . def_site
674
694
}
675
695
fn call_site ( & mut self ) -> Self :: Span {
676
- :: __internal :: with_sess ( |_ , data| data . call_site )
696
+ self . call_site
677
697
}
678
698
fn source_file ( & mut self , span : Self :: Span ) -> Self :: SourceFile {
679
- :: __internal :: lookup_char_pos ( span. lo ( ) ) . file
699
+ self . sess . source_map ( ) . lookup_char_pos ( span. lo ( ) ) . file
680
700
}
681
701
fn parent ( & mut self , span : Self :: Span ) -> Option < Self :: Span > {
682
702
span. ctxt ( ) . outer ( ) . expn_info ( ) . map ( |i| i. call_site )
@@ -685,22 +705,22 @@ impl server::Span for Rustc {
685
705
span. source_callsite ( )
686
706
}
687
707
fn start ( & mut self , span : Self :: Span ) -> LineColumn {
688
- let loc = :: __internal :: lookup_char_pos ( span. lo ( ) ) ;
708
+ let loc = self . sess . source_map ( ) . lookup_char_pos ( span. lo ( ) ) ;
689
709
LineColumn {
690
710
line : loc. line ,
691
711
column : loc. col . to_usize ( ) ,
692
712
}
693
713
}
694
714
fn end ( & mut self , span : Self :: Span ) -> LineColumn {
695
- let loc = :: __internal :: lookup_char_pos ( span. hi ( ) ) ;
715
+ let loc = self . sess . source_map ( ) . lookup_char_pos ( span. hi ( ) ) ;
696
716
LineColumn {
697
717
line : loc. line ,
698
718
column : loc. col . to_usize ( ) ,
699
719
}
700
720
}
701
721
fn join ( & mut self , first : Self :: Span , second : Self :: Span ) -> Option < Self :: Span > {
702
- let self_loc = :: __internal :: lookup_char_pos ( first. lo ( ) ) ;
703
- let other_loc = :: __internal :: lookup_char_pos ( second. lo ( ) ) ;
722
+ let self_loc = self . sess . source_map ( ) . lookup_char_pos ( first. lo ( ) ) ;
723
+ let other_loc = self . sess . source_map ( ) . lookup_char_pos ( second. lo ( ) ) ;
704
724
705
725
if self_loc. file . name != other_loc. file . name {
706
726
return None ;
0 commit comments