diff options
| author | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-05-30 08:59:22 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <nnethercote@mozilla.com> | 2019-05-30 08:59:22 +1000 |
| commit | 95ea7fd735619089ea9a0e95e2f41170127df567 (patch) | |
| tree | 0978e91207320f2a31ea0c0ee08a52a7e0618f16 /src/libsyntax_pos | |
| parent | 2232321ac7d03ed8c1d191de0653d1c32db877d6 (diff) | |
| download | rust-95ea7fd735619089ea9a0e95e2f41170127df567.tar.gz rust-95ea7fd735619089ea9a0e95e2f41170127df567.zip | |
Add `HygieneData::{outer,expn_info,is_descendant_of}` methods.
This commit factors out some repeated code.
Diffstat (limited to 'src/libsyntax_pos')
| -rw-r--r-- | src/libsyntax_pos/hygiene.rs | 50 |
1 files changed, 24 insertions, 26 deletions
diff --git a/src/libsyntax_pos/hygiene.rs b/src/libsyntax_pos/hygiene.rs index b4bb6d9c5e8..445d4271d89 100644 --- a/src/libsyntax_pos/hygiene.rs +++ b/src/libsyntax_pos/hygiene.rs @@ -112,31 +112,14 @@ impl Mark { HygieneData::with(|data| data.marks[self.0 as usize].default_transparency = transparency) } - pub fn is_descendant_of(mut self, ancestor: Mark) -> bool { - HygieneData::with(|data| { - while self != ancestor { - if self == Mark::root() { - return false; - } - self = data.marks[self.0 as usize].parent; - } - true - }) + pub fn is_descendant_of(self, ancestor: Mark) -> bool { + HygieneData::with(|data| data.is_descendant_of(self, ancestor)) } /// `mark.outer_is_descendant_of(ctxt)` is equivalent to but faster than /// `mark.is_descendant_of(ctxt.outer())`. - pub fn outer_is_descendant_of(mut self, ctxt: SyntaxContext) -> bool { - HygieneData::with(|data| { - let outer = data.syntax_contexts[ctxt.0 as usize].outer_mark; - while self != outer { - if self == Mark::root() { - return false; - } - self = data.marks[self.0 as usize].parent; - } - true - }) + pub fn outer_is_descendant_of(self, ctxt: SyntaxContext) -> bool { + HygieneData::with(|data| data.is_descendant_of(self, data.outer(ctxt))) } /// Computes a mark such that both input marks are descendants of (or equal to) the returned @@ -216,6 +199,24 @@ impl HygieneData { fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T { GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut())) } + + fn outer(&self, ctxt: SyntaxContext) -> Mark { + self.syntax_contexts[ctxt.0 as usize].outer_mark + } + + fn expn_info(&self, mark: Mark) -> Option<ExpnInfo> { + self.marks[mark.0 as usize].expn_info.clone() + } + + fn is_descendant_of(&self, mut mark: Mark, ancestor: Mark) -> bool { + while mark != ancestor { + if mark == Mark::root() { + return false; + } + mark = self.marks[mark.0 as usize].parent; + } + true + } } pub fn clear_markings() { @@ -514,17 +515,14 @@ impl SyntaxContext { #[inline] pub fn outer(self) -> Mark { - HygieneData::with(|data| data.syntax_contexts[self.0 as usize].outer_mark) + HygieneData::with(|data| data.outer(self)) } /// `ctxt.outer_expn_info()` is equivalent to but faster than /// `ctxt.outer().expn_info()`. #[inline] pub fn outer_expn_info(self) -> Option<ExpnInfo> { - HygieneData::with(|data| { - let outer = data.syntax_contexts[self.0 as usize].outer_mark; - data.marks[outer.0 as usize].expn_info.clone() - }) + HygieneData::with(|data| data.expn_info(data.outer(self))) } pub fn dollar_crate_name(self) -> Symbol { |
