From 7e53f41a07fef18b6e71e49b1cca5419baaf491f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sat, 28 Apr 2018 02:09:53 +0200 Subject: [PATCH] Add inline attributes to syntax_pos --- src/libsyntax_pos/hygiene.rs | 9 +++++ src/libsyntax_pos/lib.rs | 27 ++++++++++++++ src/libsyntax_pos/span_encoding.rs | 4 +++ src/libsyntax_pos/symbol.rs | 56 ++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) diff --git a/src/libsyntax_pos/hygiene.rs b/src/libsyntax_pos/hygiene.rs index 658408519b9c7..3bd9903ba852b 100644 --- a/src/libsyntax_pos/hygiene.rs +++ b/src/libsyntax_pos/hygiene.rs @@ -53,6 +53,7 @@ pub enum MarkKind { } impl Mark { + #[inline] pub fn fresh(parent: Mark) -> Self { HygieneData::with(|data| { data.marks.push(MarkData { parent: parent, kind: MarkKind::Legacy, expn_info: None }); @@ -171,6 +172,7 @@ impl HygieneData { } } + #[inline] fn with T>(f: F) -> T { GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut())) } @@ -181,6 +183,7 @@ pub fn clear_markings() { } impl SyntaxContext { + #[inline] pub const fn empty() -> Self { SyntaxContext(0) } @@ -450,6 +453,7 @@ pub struct NameAndSpan { } impl NameAndSpan { + #[inline] pub fn name(&self) -> Symbol { match self.format { ExpnFormat::MacroAttribute(s) | @@ -479,6 +483,7 @@ pub enum CompilerDesugaringKind { } impl CompilerDesugaringKind { + #[inline] pub fn as_symbol(&self) -> Symbol { use CompilerDesugaringKind::*; let s = match *self { @@ -491,18 +496,21 @@ impl CompilerDesugaringKind { } impl Encodable for SyntaxContext { + #[inline] fn encode(&self, _: &mut E) -> Result<(), E::Error> { Ok(()) // FIXME(jseyfried) intercrate hygiene } } impl Decodable for SyntaxContext { + #[inline] fn decode(_: &mut D) -> Result { Ok(SyntaxContext::empty()) // FIXME(jseyfried) intercrate hygiene } } impl Symbol { + #[inline] pub fn from_ident(ident: Ident) -> Symbol { HygieneData::with(|data| { let gensym = ident.name.gensymed(); @@ -511,6 +519,7 @@ impl Symbol { }) } + #[inline] pub fn to_ident(self) -> Ident { HygieneData::with(|data| { match data.gensym_to_ctxt.get(&self) { diff --git a/src/libsyntax_pos/lib.rs b/src/libsyntax_pos/lib.rs index 8b4a3ea26a1ef..e781e332d8347 100644 --- a/src/libsyntax_pos/lib.rs +++ b/src/libsyntax_pos/lib.rs @@ -191,11 +191,13 @@ impl !Send for Span {} impl !Sync for Span {} impl PartialOrd for Span { + #[inline] fn partial_cmp(&self, rhs: &Self) -> Option { PartialOrd::partial_cmp(&self.data(), &rhs.data()) } } impl Ord for Span { + #[inline] fn cmp(&self, rhs: &Self) -> Ordering { Ord::cmp(&self.data(), &rhs.data()) } @@ -253,11 +255,13 @@ impl Span { } /// Returns `self` if `self` is not the dummy span, and `other` otherwise. + #[inline] pub fn substitute_dummy(self, other: Span) -> Span { if self.source_equal(&DUMMY_SP) { other } else { self } } /// Return true if `self` fully encloses `other`. + #[inline] pub fn contains(self, other: Span) -> bool { let span = self.data(); let other = other.data(); @@ -268,6 +272,7 @@ impl Span { /// /// Use this instead of `==` when either span could be generated code, /// and you only care that they point to the same bytes of source text. + #[inline] pub fn source_equal(&self, other: &Span) -> bool { let span = self.data(); let other = other.data(); @@ -275,6 +280,7 @@ impl Span { } /// Returns `Some(span)`, where the start is trimmed by the end of `other` + #[inline] pub fn trim_start(self, other: Span) -> Option { let span = self.data(); let other = other.data(); @@ -406,6 +412,7 @@ impl Span { } /// Return a `Span` between the end of `self` to the beginning of `end`. + #[inline] pub fn between(self, end: Span) -> Span { let span = self.data(); let end = end.data(); @@ -417,6 +424,7 @@ impl Span { } /// Return a `Span` between the beginning of `self` to the beginning of `end`. + #[inline] pub fn until(self, end: Span) -> Span { let span = self.data(); let end = end.data(); @@ -488,6 +496,7 @@ pub struct SpanLabel { } impl Default for Span { + #[inline] fn default() -> Self { DUMMY_SP } @@ -539,6 +548,7 @@ impl fmt::Debug for SpanData { } impl MultiSpan { + #[inline] pub fn new() -> MultiSpan { MultiSpan { primary_spans: vec![], @@ -546,6 +556,7 @@ impl MultiSpan { } } + #[inline] pub fn from_span(primary_span: Span) -> MultiSpan { MultiSpan { primary_spans: vec![primary_span], @@ -553,6 +564,7 @@ impl MultiSpan { } } + #[inline] pub fn from_spans(vec: Vec) -> MultiSpan { MultiSpan { primary_spans: vec, @@ -560,16 +572,19 @@ impl MultiSpan { } } + #[inline] pub fn push_span_label(&mut self, span: Span, label: String) { self.span_labels.push((span, label)); } /// Selects the first primary span (if any) + #[inline] pub fn primary_span(&self) -> Option { self.primary_spans.first().cloned() } /// Returns all primary spans. + #[inline] pub fn primary_spans(&self) -> &[Span] { &self.primary_spans } @@ -625,12 +640,14 @@ impl MultiSpan { } impl From for MultiSpan { + #[inline] fn from(span: Span) -> MultiSpan { MultiSpan::from_span(span) } } impl From> for MultiSpan { + #[inline] fn from(spans: Vec) -> MultiSpan { MultiSpan::from_spans(spans) } @@ -659,6 +676,7 @@ pub enum NonNarrowChar { } impl NonNarrowChar { + #[inline] fn new(pos: BytePos, width: usize) -> Self { match width { 0 => NonNarrowChar::ZeroWidth(pos), @@ -669,6 +687,7 @@ impl NonNarrowChar { } /// Returns the absolute offset of the character in the CodeMap + #[inline] pub fn pos(&self) -> BytePos { match *self { NonNarrowChar::ZeroWidth(p) | @@ -678,6 +697,7 @@ impl NonNarrowChar { } /// Returns the width of the character, 0 (zero-width) or 2 (wide) + #[inline] pub fn width(&self) -> usize { match *self { NonNarrowChar::ZeroWidth(_) => 0, @@ -690,6 +710,7 @@ impl NonNarrowChar { impl Add for NonNarrowChar { type Output = Self; + #[inline] fn add(self, rhs: BytePos) -> Self { match self { NonNarrowChar::ZeroWidth(pos) => NonNarrowChar::ZeroWidth(pos + rhs), @@ -702,6 +723,7 @@ impl Add for NonNarrowChar { impl Sub for NonNarrowChar { type Output = Self; + #[inline] fn sub(self, rhs: BytePos) -> Self { match self { NonNarrowChar::ZeroWidth(pos) => NonNarrowChar::ZeroWidth(pos - rhs), @@ -962,6 +984,7 @@ impl FileMap { /// and CodeMap will append a newline when adding a filemap without a newline at the end, /// so the safe way to call this is with value calculated as /// filemap.start_pos + newline_offset_relative_to_the_start_of_filemap. + #[inline] pub fn next_line(&self, pos: BytePos) { // the new charpos must be > the last one (or it's the first one). let mut lines = self.lines.borrow_mut(); @@ -1037,6 +1060,7 @@ impl FileMap { } } + #[inline] pub fn record_multibyte_char(&self, pos: BytePos, bytes: usize) { assert!(bytes >=2 && bytes <= 4); let mbc = MultiByteChar { @@ -1046,6 +1070,7 @@ impl FileMap { self.multibyte_chars.borrow_mut().push(mbc); } + #[inline] pub fn record_width(&self, pos: BytePos, ch: char) { let width = match ch { '\t' => @@ -1176,12 +1201,14 @@ impl Sub for BytePos { } impl Encodable for BytePos { + #[inline] fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_u32(self.0) } } impl Decodable for BytePos { + #[inline] fn decode(d: &mut D) -> Result { Ok(BytePos(d.read_u32()?)) } diff --git a/src/libsyntax_pos/span_encoding.rs b/src/libsyntax_pos/span_encoding.rs index b55fe4bcb2672..cb5fae49808b2 100644 --- a/src/libsyntax_pos/span_encoding.rs +++ b/src/libsyntax_pos/span_encoding.rs @@ -31,11 +31,13 @@ pub struct Span(u32); impl Copy for Span {} impl Clone for Span { + #[inline] fn clone(&self) -> Span { *self } } impl PartialEq for Span { + #[inline] fn eq(&self, other: &Span) -> bool { let a = self.0; let b = other.0; @@ -44,6 +46,7 @@ impl PartialEq for Span { } impl Eq for Span {} impl Hash for Span { + #[inline] fn hash(&self, state: &mut H) { let a = self.0; a.hash(state) @@ -139,6 +142,7 @@ pub struct SpanInterner { } impl SpanInterner { + #[inline] fn intern(&mut self, span_data: &SpanData) -> u32 { if let Some(index) = self.spans.get(span_data) { return *index; diff --git a/src/libsyntax_pos/symbol.rs b/src/libsyntax_pos/symbol.rs index 4a8b1e8b1c1e4..b8772d047bf60 100644 --- a/src/libsyntax_pos/symbol.rs +++ b/src/libsyntax_pos/symbol.rs @@ -38,40 +38,48 @@ impl Ident { } /// Maps an interned string to an identifier with an empty syntax context. + #[inline] pub fn from_interned_str(string: InternedString) -> Ident { Ident::with_empty_ctxt(string.as_symbol()) } /// Maps a string to an identifier with an empty syntax context. + #[inline] pub fn from_str(string: &str) -> Ident { Ident::with_empty_ctxt(Symbol::intern(string)) } /// Replace `lo` and `hi` with those from `span`, but keep hygiene context. + #[inline] pub fn with_span_pos(self, span: Span) -> Ident { Ident::new(self.name, span.with_ctxt(self.span.ctxt())) } + #[inline] pub fn without_first_quote(self) -> Ident { Ident::new(Symbol::intern(self.name.as_str().trim_left_matches('\'')), self.span) } + #[inline] pub fn modern(self) -> Ident { Ident::new(self.name, self.span.modern()) } + #[inline] pub fn gensym(self) -> Ident { Ident::new(self.name.gensymed(), self.span) } } impl PartialEq for Ident { + #[inline] fn eq(&self, rhs: &Self) -> bool { self.name == rhs.name && self.span.ctxt() == rhs.span.ctxt() } } impl Hash for Ident { + #[inline] fn hash(&self, state: &mut H) { self.name.hash(state); self.span.ctxt().hash(state); @@ -85,12 +93,14 @@ impl fmt::Debug for Ident { } impl fmt::Display for Ident { + #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.name, f) } } impl Encodable for Ident { + #[inline] fn encode(&self, s: &mut S) -> Result<(), S::Error> { if self.span.ctxt().modern() == SyntaxContext::empty() { s.emit_str(&self.name.as_str()) @@ -103,6 +113,7 @@ impl Encodable for Ident { } impl Decodable for Ident { + #[inline] fn decode(d: &mut D) -> Result { let string = d.read_str()?; Ok(if !string.starts_with('#') { @@ -127,23 +138,28 @@ impl !Sync for Symbol { } impl Symbol { /// Maps a string to its interned representation. + #[inline] pub fn intern(string: &str) -> Self { with_interner(|interner| interner.intern(string)) } + #[inline] pub fn interned(self) -> Self { with_interner(|interner| interner.interned(self)) } /// gensym's a new usize, using the current interner. + #[inline] pub fn gensym(string: &str) -> Self { with_interner(|interner| interner.gensym(string)) } + #[inline] pub fn gensymed(self) -> Self { with_interner(|interner| interner.gensymed(self)) } + #[inline] pub fn as_str(self) -> LocalInternedString { with_interner(|interner| unsafe { LocalInternedString { @@ -152,12 +168,14 @@ impl Symbol { }) } + #[inline] pub fn as_interned_str(self) -> InternedString { with_interner(|interner| InternedString { symbol: interner.interned(self) }) } + #[inline] pub fn as_u32(self) -> u32 { self.0 } @@ -175,24 +193,28 @@ impl fmt::Debug for Symbol { } impl fmt::Display for Symbol { + #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.as_str(), f) } } impl Encodable for Symbol { + #[inline] fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_str(&self.as_str()) } } impl Decodable for Symbol { + #[inline] fn decode(d: &mut D) -> Result { Ok(Symbol::intern(&d.read_str()?)) } } impl> PartialEq for Symbol { + #[inline] fn eq(&self, other: &T) -> bool { self.as_str() == other.deref() } @@ -218,11 +240,15 @@ impl Interner { this } + #[inline] pub fn intern(&mut self, string: &str) -> Symbol { if let Some(&name) = self.names.get(string) { return name; } + self.insert(string) + } + fn insert(&mut self, string: &str) -> Symbol { let name = Symbol(self.strings.len() as u32); let string = string.to_string().into_boxed_str(); self.strings.push(string.clone()); @@ -230,6 +256,7 @@ impl Interner { name } + #[inline] pub fn interned(&self, symbol: Symbol) -> Symbol { if (symbol.0 as usize) < self.strings.len() { symbol @@ -248,10 +275,12 @@ impl Interner { Symbol(!0 - self.gensyms.len() as u32 + 1) } + #[inline] fn is_gensymed(&mut self, symbol: Symbol) -> bool { symbol.0 as usize >= self.strings.len() } + #[inline] pub fn get(&self, symbol: Symbol) -> &str { match self.strings.get(symbol.0 as usize) { Some(ref string) => string, @@ -388,6 +417,7 @@ pub struct LocalInternedString { } impl LocalInternedString { + #[inline] pub fn as_interned_str(self) -> InternedString { InternedString { symbol: Symbol::intern(self.string) @@ -399,36 +429,42 @@ impl ::std::convert::AsRef for LocalInternedString where str: ::std::convert::AsRef { + #[inline] fn as_ref(&self) -> &U { self.string.as_ref() } } impl> ::std::cmp::PartialEq for LocalInternedString { + #[inline] fn eq(&self, other: &T) -> bool { self.string == other.deref() } } impl ::std::cmp::PartialEq for str { + #[inline] fn eq(&self, other: &LocalInternedString) -> bool { self == other.string } } impl<'a> ::std::cmp::PartialEq for &'a str { + #[inline] fn eq(&self, other: &LocalInternedString) -> bool { *self == other.string } } impl ::std::cmp::PartialEq for String { + #[inline] fn eq(&self, other: &LocalInternedString) -> bool { self == other.string } } impl<'a> ::std::cmp::PartialEq for &'a String { + #[inline] fn eq(&self, other: &LocalInternedString) -> bool { *self == other.string } @@ -439,6 +475,7 @@ impl !Sync for LocalInternedString {} impl ::std::ops::Deref for LocalInternedString { type Target = str; + #[inline] fn deref(&self) -> &str { self.string } } @@ -449,18 +486,21 @@ impl fmt::Debug for LocalInternedString { } impl fmt::Display for LocalInternedString { + #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(self.string, f) } } impl Decodable for LocalInternedString { + #[inline] fn decode(d: &mut D) -> Result { Ok(Symbol::intern(&d.read_str()?).as_str()) } } impl Encodable for LocalInternedString { + #[inline] fn encode(&self, s: &mut S) -> Result<(), S::Error> { s.emit_str(self.string) } @@ -473,6 +513,7 @@ pub struct InternedString { } impl InternedString { + #[inline] pub fn with R, R>(self, f: F) -> R { let str = with_interner(|interner| { interner.get(self.symbol) as *const str @@ -483,22 +524,26 @@ impl InternedString { unsafe { f(&*str) } } + #[inline] pub fn as_symbol(self) -> Symbol { self.symbol } + #[inline] pub fn as_str(self) -> LocalInternedString { self.symbol.as_str() } } impl Hash for InternedString { + #[inline] fn hash(&self, state: &mut H) { self.with(|str| str.hash(state)) } } impl PartialOrd for InternedString { + #[inline] fn partial_cmp(&self, other: &InternedString) -> Option { if self.symbol == other.symbol { return Some(Ordering::Equal); @@ -508,6 +553,7 @@ impl PartialOrd for InternedString { } impl Ord for InternedString { + #[inline] fn cmp(&self, other: &InternedString) -> Ordering { if self.symbol == other.symbol { return Ordering::Equal; @@ -517,42 +563,49 @@ impl Ord for InternedString { } impl> PartialEq for InternedString { + #[inline] fn eq(&self, other: &T) -> bool { self.with(|string| string == other.deref()) } } impl PartialEq for InternedString { + #[inline] fn eq(&self, other: &InternedString) -> bool { self.symbol == other.symbol } } impl PartialEq for str { + #[inline] fn eq(&self, other: &InternedString) -> bool { other.with(|string| self == string) } } impl<'a> PartialEq for &'a str { + #[inline] fn eq(&self, other: &InternedString) -> bool { other.with(|string| *self == string) } } impl PartialEq for String { + #[inline] fn eq(&self, other: &InternedString) -> bool { other.with(|string| self == string) } } impl<'a> PartialEq for &'a String { + #[inline] fn eq(&self, other: &InternedString) -> bool { other.with(|string| *self == string) } } impl ::std::convert::From for String { + #[inline] fn from(val: InternedString) -> String { val.as_symbol().to_string() } @@ -565,18 +618,21 @@ impl fmt::Debug for InternedString { } impl fmt::Display for InternedString { + #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.with(|str| fmt::Display::fmt(&str, f)) } } impl Decodable for InternedString { + #[inline] fn decode(d: &mut D) -> Result { Ok(Symbol::intern(&d.read_str()?).as_interned_str()) } } impl Encodable for InternedString { + #[inline] fn encode(&self, s: &mut S) -> Result<(), S::Error> { self.with(|string| s.emit_str(string)) }