Refactor MarkData field modern: bool to kind: MarkKind.

This commit is contained in:
Jeffrey Seyfried 2017-12-12 01:14:45 -08:00
parent 8dbe79ae5e
commit f79c7e442d

View File

@ -27,7 +27,7 @@ use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
pub struct SyntaxContext(pub(super) u32); pub struct SyntaxContext(pub(super) u32);
#[derive(Copy, Clone, Default)] #[derive(Copy, Clone)]
pub struct SyntaxContextData { pub struct SyntaxContextData {
pub outer_mark: Mark, pub outer_mark: Mark,
pub prev_ctxt: SyntaxContext, pub prev_ctxt: SyntaxContext,
@ -35,20 +35,26 @@ pub struct SyntaxContextData {
} }
/// A mark is a unique id associated with a macro expansion. /// A mark is a unique id associated with a macro expansion.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Default, RustcEncodable, RustcDecodable)] #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
pub struct Mark(u32); pub struct Mark(u32);
#[derive(Default)]
struct MarkData { struct MarkData {
parent: Mark, parent: Mark,
modern: bool, kind: MarkKind,
expn_info: Option<ExpnInfo>, expn_info: Option<ExpnInfo>,
} }
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum MarkKind {
Modern,
Builtin,
Legacy,
}
impl Mark { impl Mark {
pub fn fresh(parent: Mark) -> Self { pub fn fresh(parent: Mark) -> Self {
HygieneData::with(|data| { HygieneData::with(|data| {
data.marks.push(MarkData { parent: parent, modern: false, expn_info: None }); data.marks.push(MarkData { parent: parent, kind: MarkKind::Legacy, expn_info: None });
Mark(data.marks.len() as u32 - 1) Mark(data.marks.len() as u32 - 1)
}) })
} }
@ -77,7 +83,7 @@ impl Mark {
pub fn modern(mut self) -> Mark { pub fn modern(mut self) -> Mark {
HygieneData::with(|data| { HygieneData::with(|data| {
loop { loop {
if self == Mark::root() || data.marks[self.0 as usize].modern { if self == Mark::root() || data.marks[self.0 as usize].kind == MarkKind::Modern {
return self; return self;
} }
self = data.marks[self.0 as usize].parent; self = data.marks[self.0 as usize].parent;
@ -85,12 +91,12 @@ impl Mark {
}) })
} }
pub fn is_modern(self) -> bool { pub fn kind(self) -> MarkKind {
HygieneData::with(|data| data.marks[self.0 as usize].modern) HygieneData::with(|data| data.marks[self.0 as usize].kind)
} }
pub fn set_modern(self) { pub fn set_kind(self, kind: MarkKind) {
HygieneData::with(|data| data.marks[self.0 as usize].modern = true) HygieneData::with(|data| data.marks[self.0 as usize].kind = kind)
} }
pub fn is_descendant_of(mut self, ancestor: Mark) -> bool { pub fn is_descendant_of(mut self, ancestor: Mark) -> bool {
@ -116,8 +122,16 @@ struct HygieneData {
impl HygieneData { impl HygieneData {
fn new() -> Self { fn new() -> Self {
HygieneData { HygieneData {
marks: vec![MarkData::default()], marks: vec![MarkData {
syntax_contexts: vec![SyntaxContextData::default()], parent: Mark::root(),
kind: MarkKind::Builtin,
expn_info: None,
}],
syntax_contexts: vec![SyntaxContextData {
outer_mark: Mark::root(),
prev_ctxt: SyntaxContext(0),
modern: SyntaxContext(0),
}],
markings: HashMap::new(), markings: HashMap::new(),
gensym_to_ctxt: HashMap::new(), gensym_to_ctxt: HashMap::new(),
} }
@ -150,7 +164,7 @@ impl SyntaxContext {
HygieneData::with(|data| { HygieneData::with(|data| {
data.marks.push(MarkData { data.marks.push(MarkData {
parent: Mark::root(), parent: Mark::root(),
modern: false, kind: MarkKind::Legacy,
expn_info: Some(expansion_info) expn_info: Some(expansion_info)
}); });
@ -170,7 +184,7 @@ impl SyntaxContext {
HygieneData::with(|data| { HygieneData::with(|data| {
let syntax_contexts = &mut data.syntax_contexts; let syntax_contexts = &mut data.syntax_contexts;
let mut modern = syntax_contexts[self.0 as usize].modern; let mut modern = syntax_contexts[self.0 as usize].modern;
if data.marks[mark.0 as usize].modern { if data.marks[mark.0 as usize].kind == MarkKind::Modern {
modern = *data.markings.entry((modern, mark)).or_insert_with(|| { modern = *data.markings.entry((modern, mark)).or_insert_with(|| {
let len = syntax_contexts.len() as u32; let len = syntax_contexts.len() as u32;
syntax_contexts.push(SyntaxContextData { syntax_contexts.push(SyntaxContextData {