Make names more explicit.

This commit is contained in:
Camille GILLOT 2022-07-04 09:40:30 +02:00
parent b2a17e9bd1
commit 3e67bded92

View File

@ -28,12 +28,17 @@ use rustc_data_structures::fx::FxIndexSet;
/// Inline (compressed) format: /// Inline (compressed) format:
/// - `span.base_or_index == span_data.lo` /// - `span.base_or_index == span_data.lo`
/// - `span.len_or_tag == len == span_data.hi - span_data.lo` (must be `<= MAX_LEN`) /// - `span.len_or_tag == len == span_data.hi - span_data.lo` (must be `<= MAX_LEN`)
/// - `span.ctxt == span_data.ctxt` (must be `< MAX_CTXT`) /// - `span.ctxt_or_tag == span_data.ctxt` (must be `<= MAX_CTXT`)
///
/// Interned format with inline `SyntaxContext`:
/// - `span.base_or_index == index` (indexes into the interner table)
/// - `span.len_or_tag == LEN_TAG` (high bit set, all other bits are zero)
/// - `span.ctxt_or_tag == span_data.ctxt` (must be `<= MAX_CTXT`)
/// ///
/// Interned format: /// Interned format:
/// - `span.base_or_index == index` (indexes into the interner table) /// - `span.base_or_index == index` (indexes into the interner table)
/// - `span.len_or_tag == LEN_TAG` (high bit set, all other bits are zero) /// - `span.len_or_tag == LEN_TAG` (high bit set, all other bits are zero)
/// - `span.ctxt == span_data.ctxt` (must be < `MAX_CTXT`) or `MAX_CTXT` otherwise /// - `span.ctxt_or_tag == CTXT_TAG`
/// ///
/// The inline form uses 0 for the tag value (rather than 1) so that we don't /// The inline form uses 0 for the tag value (rather than 1) so that we don't
/// need to mask out the tag bit when getting the length, and so that the /// need to mask out the tag bit when getting the length, and so that the
@ -50,10 +55,10 @@ use rustc_data_structures::fx::FxIndexSet;
/// at 3 or 4, and then it drops off quickly from 8 onwards. 15 bits is enough /// at 3 or 4, and then it drops off quickly from 8 onwards. 15 bits is enough
/// for 99.99%+ of cases, but larger values (sometimes 20+ bits) might occur /// for 99.99%+ of cases, but larger values (sometimes 20+ bits) might occur
/// dozens of times in a typical crate. /// dozens of times in a typical crate.
/// - `ctxt` is 16 bits in `Span` and 32 bits in `SpanData`, which means that /// - `ctxt_or_tag` is 16 bits in `Span` and 32 bits in `SpanData`, which means that
/// large `ctxt` values will cause interning. The number of bits needed for /// large `ctxt` values will cause interning. The number of bits needed for
/// `ctxt` values depend partly on the crate size and partly on the form of /// `ctxt` values depend partly on the crate size and partly on the form of
/// the code. No crates in `rustc-perf` need more than 15 bits for `ctxt`, /// the code. No crates in `rustc-perf` need more than 15 bits for `ctxt_or_tag`,
/// but larger crates might need more than 16 bits. /// but larger crates might need more than 16 bits.
/// ///
/// In order to reliably use parented spans in incremental compilation, /// In order to reliably use parented spans in incremental compilation,
@ -65,15 +70,16 @@ use rustc_data_structures::fx::FxIndexSet;
pub struct Span { pub struct Span {
base_or_index: u32, base_or_index: u32,
len_or_tag: u16, len_or_tag: u16,
ctxt_or_max: u16, ctxt_or_tag: u16,
} }
const LEN_TAG: u16 = 0b1000_0000_0000_0000; const LEN_TAG: u16 = 0b1000_0000_0000_0000;
const MAX_LEN: u32 = 0b0111_1111_1111_1111; const MAX_LEN: u32 = 0b0111_1111_1111_1111;
const MAX_CTXT: u32 = 0b1111_1111_1111_1111; const CTXT_TAG: u32 = 0b1111_1111_1111_1111;
const MAX_CTXT: u32 = CTXT_TAG - 1;
/// Dummy span, both position and length are zero, syntax context is zero as well. /// Dummy span, both position and length are zero, syntax context is zero as well.
pub const DUMMY_SP: Span = Span { base_or_index: 0, len_or_tag: 0, ctxt_or_max: 0 }; pub const DUMMY_SP: Span = Span { base_or_index: 0, len_or_tag: 0, ctxt_or_tag: 0 };
impl Span { impl Span {
#[inline] #[inline]
@ -89,15 +95,15 @@ impl Span {
let (base, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32()); let (base, len, ctxt2) = (lo.0, hi.0 - lo.0, ctxt.as_u32());
if len <= MAX_LEN && ctxt2 < MAX_CTXT && parent.is_none() { if len <= MAX_LEN && ctxt2 <= MAX_CTXT && parent.is_none() {
// Inline format. // Inline format.
Span { base_or_index: base, len_or_tag: len as u16, ctxt_or_max: ctxt2 as u16 } Span { base_or_index: base, len_or_tag: len as u16, ctxt_or_tag: ctxt2 as u16 }
} else { } else {
// Interned format. // Interned format.
let index = let index =
with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt, parent })); with_span_interner(|interner| interner.intern(&SpanData { lo, hi, ctxt, parent }));
let ctxt_or_max = if ctxt2 < MAX_CTXT { ctxt2 } else { MAX_CTXT } as u16; let ctxt_or_tag = if ctxt2 <= MAX_CTXT { ctxt2 } else { CTXT_TAG } as u16;
Span { base_or_index: index, len_or_tag: LEN_TAG, ctxt_or_max } Span { base_or_index: index, len_or_tag: LEN_TAG, ctxt_or_tag }
} }
} }
@ -120,7 +126,7 @@ impl Span {
SpanData { SpanData {
lo: BytePos(self.base_or_index), lo: BytePos(self.base_or_index),
hi: BytePos(self.base_or_index + self.len_or_tag as u32), hi: BytePos(self.base_or_index + self.len_or_tag as u32),
ctxt: SyntaxContext::from_u32(self.ctxt_or_max as u32), ctxt: SyntaxContext::from_u32(self.ctxt_or_tag as u32),
parent: None, parent: None,
} }
} else { } else {
@ -133,10 +139,10 @@ impl Span {
/// This function is used as a fast path when decoding the full `SpanData` is not necessary. /// This function is used as a fast path when decoding the full `SpanData` is not necessary.
#[inline] #[inline]
pub fn ctxt(self) -> SyntaxContext { pub fn ctxt(self) -> SyntaxContext {
let ctxt_or_max = self.ctxt_or_max as u32; let ctxt_or_tag = self.ctxt_or_tag as u32;
if ctxt_or_max < MAX_CTXT { if ctxt_or_tag <= MAX_CTXT {
// Inline format or interned format with inline ctxt. // Inline format or interned format with inline ctxt.
SyntaxContext::from_u32(ctxt_or_max) SyntaxContext::from_u32(ctxt_or_tag)
} else { } else {
// Interned format. // Interned format.
let index = self.base_or_index; let index = self.base_or_index;