17
17
//! within the CodeMap, which upon request can be converted to line and column
18
18
//! information, source code snippets, etc.
19
19
20
- pub use self :: MacroFormat :: * ;
20
+ pub use self :: ExpnFormat :: * ;
21
21
22
22
use std:: cell:: RefCell ;
23
23
use std:: ops:: { Add , Sub } ;
@@ -228,17 +228,17 @@ pub struct FileMapAndBytePos { pub fm: Rc<FileMap>, pub pos: BytePos }
228
228
229
229
230
230
// _____________________________________________________________________________
231
- // MacroFormat , NameAndSpan, ExpnInfo, ExpnId
231
+ // ExpnFormat , NameAndSpan, ExpnInfo, ExpnId
232
232
//
233
233
234
- /// The syntax with which a macro was invoked .
235
- #[ derive( Clone , Copy , Hash , Debug ) ]
236
- pub enum MacroFormat {
234
+ /// The source of expansion .
235
+ #[ derive( Clone , Copy , Hash , Debug , PartialEq , Eq ) ]
236
+ pub enum ExpnFormat {
237
237
/// e.g. #[derive(...)] <item>
238
238
MacroAttribute ,
239
239
/// e.g. `format!()`
240
240
MacroBang ,
241
- /// Expansion performed by the compiler (libsyntax::expand).
241
+ /// Syntax sugar expansion performed by the compiler (libsyntax::expand).
242
242
CompilerExpansion ,
243
243
}
244
244
@@ -248,7 +248,7 @@ pub struct NameAndSpan {
248
248
/// with this Span.
249
249
pub name : String ,
250
250
/// The format with which the macro was invoked.
251
- pub format : MacroFormat ,
251
+ pub format : ExpnFormat ,
252
252
/// Whether the macro is allowed to use #[unstable]/feature-gated
253
253
/// features internally without forcing the whole crate to opt-in
254
254
/// to them.
@@ -259,11 +259,11 @@ pub struct NameAndSpan {
259
259
pub span : Option < Span >
260
260
}
261
261
262
- /// Extra information for tracking macro expansion of spans
262
+ /// Extra information for tracking spans of macro and syntax sugar expansion
263
263
#[ derive( Hash , Debug ) ]
264
264
pub struct ExpnInfo {
265
- /// The location of the actual macro invocation, e.g. `let x =
266
- /// foo!();`
265
+ /// The location of the actual macro invocation or syntax sugar , e.g.
266
+ /// `let x = foo!();` or `if let Some(y) = x {} `
267
267
///
268
268
/// This may recursively refer to other macro invocations, e.g. if
269
269
/// `foo!()` invoked `bar!()` internally, and there was an
@@ -272,12 +272,7 @@ pub struct ExpnInfo {
272
272
/// call_site span would have its own ExpnInfo, with the call_site
273
273
/// pointing to the `foo!` invocation.
274
274
pub call_site : Span ,
275
- /// Information about the macro and its definition.
276
- ///
277
- /// The `callee` of the inner expression in the `call_site`
278
- /// example would point to the `macro_rules! bar { ... }` and that
279
- /// of the `bar!()` invocation would point to the `macro_rules!
280
- /// foo { ... }`.
275
+ /// Information about the expansion.
281
276
pub callee : NameAndSpan
282
277
}
283
278
@@ -677,7 +672,39 @@ impl CodeMap {
677
672
678
673
/// Lookup source information about a BytePos
679
674
pub fn lookup_char_pos ( & self , pos : BytePos ) -> Loc {
680
- self . lookup_pos ( pos)
675
+ let FileMapAndLine { fm : f, line : a} = self . lookup_line ( pos) ;
676
+ let line = a + 1 ; // Line numbers start at 1
677
+ let chpos = self . bytepos_to_file_charpos ( pos) ;
678
+ let linebpos = ( * f. lines . borrow ( ) ) [ a] ;
679
+ let linechpos = self . bytepos_to_file_charpos ( linebpos) ;
680
+ debug ! ( "byte pos {:?} is on the line at byte pos {:?}" ,
681
+ pos, linebpos) ;
682
+ debug ! ( "char pos {:?} is on the line at char pos {:?}" ,
683
+ chpos, linechpos) ;
684
+ debug ! ( "byte is on line: {}" , line) ;
685
+ assert ! ( chpos >= linechpos) ;
686
+ Loc {
687
+ file : f,
688
+ line : line,
689
+ col : chpos - linechpos
690
+ }
691
+ }
692
+
693
+ fn lookup_line ( & self , pos : BytePos ) -> FileMapAndLine {
694
+ let idx = self . lookup_filemap_idx ( pos) ;
695
+
696
+ let files = self . files . borrow ( ) ;
697
+ let f = ( * files) [ idx] . clone ( ) ;
698
+ let mut a = 0 ;
699
+ {
700
+ let lines = f. lines . borrow ( ) ;
701
+ let mut b = lines. len ( ) ;
702
+ while b - a > 1 {
703
+ let m = ( a + b) / 2 ;
704
+ if ( * lines) [ m] > pos { b = m; } else { a = m; }
705
+ }
706
+ }
707
+ FileMapAndLine { fm : f, line : a}
681
708
}
682
709
683
710
pub fn lookup_char_pos_adj ( & self , pos : BytePos ) -> LocWithOpt {
@@ -877,42 +904,6 @@ impl CodeMap {
877
904
return a;
878
905
}
879
906
880
- fn lookup_line ( & self , pos : BytePos ) -> FileMapAndLine {
881
- let idx = self . lookup_filemap_idx ( pos) ;
882
-
883
- let files = self . files . borrow ( ) ;
884
- let f = ( * files) [ idx] . clone ( ) ;
885
- let mut a = 0 ;
886
- {
887
- let lines = f. lines . borrow ( ) ;
888
- let mut b = lines. len ( ) ;
889
- while b - a > 1 {
890
- let m = ( a + b) / 2 ;
891
- if ( * lines) [ m] > pos { b = m; } else { a = m; }
892
- }
893
- }
894
- FileMapAndLine { fm : f, line : a}
895
- }
896
-
897
- fn lookup_pos ( & self , pos : BytePos ) -> Loc {
898
- let FileMapAndLine { fm : f, line : a} = self . lookup_line ( pos) ;
899
- let line = a + 1 ; // Line numbers start at 1
900
- let chpos = self . bytepos_to_file_charpos ( pos) ;
901
- let linebpos = ( * f. lines . borrow ( ) ) [ a] ;
902
- let linechpos = self . bytepos_to_file_charpos ( linebpos) ;
903
- debug ! ( "byte pos {:?} is on the line at byte pos {:?}" ,
904
- pos, linebpos) ;
905
- debug ! ( "char pos {:?} is on the line at char pos {:?}" ,
906
- chpos, linechpos) ;
907
- debug ! ( "byte is on line: {}" , line) ;
908
- assert ! ( chpos >= linechpos) ;
909
- Loc {
910
- file : f,
911
- line : line,
912
- col : chpos - linechpos
913
- }
914
- }
915
-
916
907
pub fn record_expansion ( & self , expn_info : ExpnInfo ) -> ExpnId {
917
908
let mut expansions = self . expansions . borrow_mut ( ) ;
918
909
expansions. push ( expn_info) ;
0 commit comments