-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some utilities to libsyntax
#50192
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ use symbol::{Ident, Symbol}; | |
|
||
use serialize::{Encodable, Decodable, Encoder, Decoder}; | ||
use std::collections::HashMap; | ||
use rustc_data_structures::fx::FxHashSet; | ||
use std::fmt; | ||
|
||
/// A SyntaxContext represents a chain of macro expansions (represented by marks). | ||
|
@@ -117,6 +118,32 @@ impl Mark { | |
true | ||
}) | ||
} | ||
|
||
/// Computes a mark such that both input marks are descendants of (or equal to) the returned | ||
/// mark. That is, the following holds: | ||
/// | ||
/// ```rust | ||
/// let lub = lub(a, b); | ||
/// assert!(a.is_descendant_of(lub)) | ||
/// assert!(b.is_descendant_of(lub)) | ||
/// ``` | ||
pub fn lub(mut a: Mark, mut b: Mark) -> Mark { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe |
||
HygieneData::with(|data| { | ||
// Compute the path from a to the root | ||
let mut a_path = FxHashSet::<Mark>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, |
||
while a != Mark::root() { | ||
a_path.insert(a); | ||
a = data.marks[a.0 as usize].parent; | ||
} | ||
|
||
// While the path from b to the root hasn't intersected, move up the tree | ||
while !a_path.contains(&b) { | ||
b = data.marks[b.0 as usize].parent; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: two spaces after |
||
} | ||
|
||
b | ||
}) | ||
} | ||
} | ||
|
||
pub struct HygieneData { | ||
|
@@ -238,6 +265,22 @@ impl SyntaxContext { | |
}) | ||
} | ||
|
||
/// Pulls a single mark off of the syntax context. This effectively moves the | ||
/// context up one macro definition level. That is, if we have a nested macro | ||
/// definition as follows: | ||
/// | ||
/// ```rust | ||
/// macro_rules! f { | ||
/// macro_rules! g { | ||
/// ... | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// and we have a SyntaxContext that is referring to something declared by an invocation | ||
/// of g (call it g1), calling remove_mark will result in the SyntaxContext for the | ||
/// invocation of f that created g1. | ||
/// Returns the mark that was removed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
pub fn remove_mark(&mut self) -> Mark { | ||
HygieneData::with(|data| { | ||
let outer_mark = data.syntax_contexts[self.0 as usize].outer_mark; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,6 +291,12 @@ impl Span { | |
self.ctxt().outer().expn_info().map(|info| info.call_site.source_callsite()).unwrap_or(self) | ||
} | ||
|
||
/// The `Span for the tokens in the previous macro expansion from which `self` was generated, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
/// if any | ||
pub fn parent(self) -> Option<Span> { | ||
self.ctxt().outer().expn_info().map(|i| i.call_site) | ||
} | ||
|
||
/// Return the source callee. | ||
/// | ||
/// Returns None if the supplied span has no expansion trace, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
.map(Span)