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 } ;
@@ -226,17 +226,17 @@ pub struct FileMapAndBytePos { pub fm: Rc<FileMap>, pub pos: BytePos }
226
226
227
227
228
228
// _____________________________________________________________________________
229
- // MacroFormat , NameAndSpan, ExpnInfo, ExpnId
229
+ // ExpnFormat , NameAndSpan, ExpnInfo, ExpnId
230
230
//
231
231
232
- /// The syntax with which a macro was invoked .
233
- #[ derive( Clone , Copy , Hash , Debug ) ]
234
- pub enum MacroFormat {
232
+ /// The source of expansion .
233
+ #[ derive( Clone , Copy , Hash , Debug , PartialEq , Eq ) ]
234
+ pub enum ExpnFormat {
235
235
/// e.g. #[derive(...)] <item>
236
236
MacroAttribute ,
237
237
/// e.g. `format!()`
238
238
MacroBang ,
239
- /// Expansion performed by the compiler (libsyntax::expand).
239
+ /// Syntax sugar expansion performed by the compiler (libsyntax::expand).
240
240
CompilerExpansion ,
241
241
}
242
242
@@ -246,7 +246,7 @@ pub struct NameAndSpan {
246
246
/// with this Span.
247
247
pub name : String ,
248
248
/// The format with which the macro was invoked.
249
- pub format : MacroFormat ,
249
+ pub format : ExpnFormat ,
250
250
/// Whether the macro is allowed to use #[unstable]/feature-gated
251
251
/// features internally without forcing the whole crate to opt-in
252
252
/// to them.
@@ -257,11 +257,11 @@ pub struct NameAndSpan {
257
257
pub span : Option < Span >
258
258
}
259
259
260
- /// Extra information for tracking macro expansion of spans
260
+ /// Extra information for tracking spans of macro and syntax sugar expansion
261
261
#[ derive( Hash , Debug ) ]
262
262
pub struct ExpnInfo {
263
- /// The location of the actual macro invocation, e.g. `let x =
264
- /// foo!();`
263
+ /// The location of the actual macro invocation or syntax sugar , e.g.
264
+ /// `let x = foo!();` or `if let Some(y) = x {} `
265
265
///
266
266
/// This may recursively refer to other macro invocations, e.g. if
267
267
/// `foo!()` invoked `bar!()` internally, and there was an
@@ -270,12 +270,7 @@ pub struct ExpnInfo {
270
270
/// call_site span would have its own ExpnInfo, with the call_site
271
271
/// pointing to the `foo!` invocation.
272
272
pub call_site : Span ,
273
- /// Information about the macro and its definition.
274
- ///
275
- /// The `callee` of the inner expression in the `call_site`
276
- /// example would point to the `macro_rules! bar { ... }` and that
277
- /// of the `bar!()` invocation would point to the `macro_rules!
278
- /// foo { ... }`.
273
+ /// Information about the expansion.
279
274
pub callee : NameAndSpan
280
275
}
281
276
@@ -633,7 +628,39 @@ impl CodeMap {
633
628
634
629
/// Lookup source information about a BytePos
635
630
pub fn lookup_char_pos ( & self , pos : BytePos ) -> Loc {
636
- self . lookup_pos ( pos)
631
+ let FileMapAndLine { fm : f, line : a} = self . lookup_line ( pos) ;
632
+ let line = a + 1 ; // Line numbers start at 1
633
+ let chpos = self . bytepos_to_file_charpos ( pos) ;
634
+ let linebpos = ( * f. lines . borrow ( ) ) [ a] ;
635
+ let linechpos = self . bytepos_to_file_charpos ( linebpos) ;
636
+ debug ! ( "byte pos {:?} is on the line at byte pos {:?}" ,
637
+ pos, linebpos) ;
638
+ debug ! ( "char pos {:?} is on the line at char pos {:?}" ,
639
+ chpos, linechpos) ;
640
+ debug ! ( "byte is on line: {}" , line) ;
641
+ assert ! ( chpos >= linechpos) ;
642
+ Loc {
643
+ file : f,
644
+ line : line,
645
+ col : chpos - linechpos
646
+ }
647
+ }
648
+
649
+ fn lookup_line ( & self , pos : BytePos ) -> FileMapAndLine {
650
+ let idx = self . lookup_filemap_idx ( pos) ;
651
+
652
+ let files = self . files . borrow ( ) ;
653
+ let f = ( * files) [ idx] . clone ( ) ;
654
+ let mut a = 0 ;
655
+ {
656
+ let lines = f. lines . borrow ( ) ;
657
+ let mut b = lines. len ( ) ;
658
+ while b - a > 1 {
659
+ let m = ( a + b) / 2 ;
660
+ if ( * lines) [ m] > pos { b = m; } else { a = m; }
661
+ }
662
+ }
663
+ FileMapAndLine { fm : f, line : a}
637
664
}
638
665
639
666
pub fn lookup_char_pos_adj ( & self , pos : BytePos ) -> LocWithOpt {
@@ -833,42 +860,6 @@ impl CodeMap {
833
860
return a;
834
861
}
835
862
836
- fn lookup_line ( & self , pos : BytePos ) -> FileMapAndLine {
837
- let idx = self . lookup_filemap_idx ( pos) ;
838
-
839
- let files = self . files . borrow ( ) ;
840
- let f = ( * files) [ idx] . clone ( ) ;
841
- let mut a = 0 ;
842
- {
843
- let lines = f. lines . borrow ( ) ;
844
- let mut b = lines. len ( ) ;
845
- while b - a > 1 {
846
- let m = ( a + b) / 2 ;
847
- if ( * lines) [ m] > pos { b = m; } else { a = m; }
848
- }
849
- }
850
- FileMapAndLine { fm : f, line : a}
851
- }
852
-
853
- fn lookup_pos ( & self , pos : BytePos ) -> Loc {
854
- let FileMapAndLine { fm : f, line : a} = self . lookup_line ( pos) ;
855
- let line = a + 1 ; // Line numbers start at 1
856
- let chpos = self . bytepos_to_file_charpos ( pos) ;
857
- let linebpos = ( * f. lines . borrow ( ) ) [ a] ;
858
- let linechpos = self . bytepos_to_file_charpos ( linebpos) ;
859
- debug ! ( "byte pos {:?} is on the line at byte pos {:?}" ,
860
- pos, linebpos) ;
861
- debug ! ( "char pos {:?} is on the line at char pos {:?}" ,
862
- chpos, linechpos) ;
863
- debug ! ( "byte is on line: {}" , line) ;
864
- assert ! ( chpos >= linechpos) ;
865
- Loc {
866
- file : f,
867
- line : line,
868
- col : chpos - linechpos
869
- }
870
- }
871
-
872
863
pub fn record_expansion ( & self , expn_info : ExpnInfo ) -> ExpnId {
873
864
let mut expansions = self . expansions . borrow_mut ( ) ;
874
865
expansions. push ( expn_info) ;
0 commit comments