Skip to content

Commit 755e5d6

Browse files
committed
rust: Vendor buffer and lit from syn crate
TokenBuffer and literal parsing are very commonly used in procedural macros, so vendor in relevant bits from syn. Signed-off-by: Gary Guo <[email protected]>
1 parent be4c42a commit 755e5d6

File tree

4 files changed

+726
-0
lines changed

4 files changed

+726
-0
lines changed

rust/macros/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//! Crate for all kernel procedural macros.
44
mod module;
5+
mod syn;
56

67
use proc_macro::TokenStream;
78

rust/macros/syn/buffer.rs

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
// SPDX-License-Identifier: MIT or Apache-2.0
2+
3+
use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Span, TokenStream, TokenTree};
4+
use std::marker::PhantomData;
5+
use std::ptr;
6+
7+
/// Internal type which is used instead of `TokenTree` to represent a token tree
8+
/// within a `TokenBuffer`.
9+
enum Entry {
10+
// Mimicking types from proc-macro.
11+
Group(Group, TokenBuffer),
12+
Ident(Ident),
13+
Punct(Punct),
14+
Literal(Literal),
15+
// End entries contain a raw pointer to the entry from the containing
16+
// token tree, or null if this is the outermost level.
17+
End(*const Entry),
18+
}
19+
20+
/// A buffer that can be efficiently traversed multiple times, unlike
21+
/// `TokenStream` which requires a deep copy in order to traverse more than
22+
/// once.
23+
///
24+
/// *This type is available only if Syn is built with the `"parsing"` feature.*
25+
pub struct TokenBuffer {
26+
// NOTE: Do not derive clone on this - there are raw pointers inside which
27+
// will be messed up. Moving the `TokenBuffer` itself is safe as the actual
28+
// backing slices won't be moved.
29+
data: Box<[Entry]>,
30+
}
31+
32+
impl TokenBuffer {
33+
// NOTE: DO NOT MUTATE THE `Vec` RETURNED FROM THIS FUNCTION ONCE IT
34+
// RETURNS, THE ADDRESS OF ITS BACKING MEMORY MUST REMAIN STABLE.
35+
fn inner_new(stream: TokenStream, up: *const Entry) -> TokenBuffer {
36+
// Build up the entries list, recording the locations of any Groups
37+
// in the list to be processed later.
38+
let mut entries = Vec::new();
39+
let mut seqs = Vec::new();
40+
for tt in stream {
41+
match tt {
42+
TokenTree::Ident(sym) => {
43+
entries.push(Entry::Ident(sym));
44+
}
45+
TokenTree::Punct(op) => {
46+
entries.push(Entry::Punct(op));
47+
}
48+
TokenTree::Literal(l) => {
49+
entries.push(Entry::Literal(l));
50+
}
51+
TokenTree::Group(g) => {
52+
// Record the index of the interesting entry, and store an
53+
// `End(null)` there temporarially.
54+
seqs.push((entries.len(), g));
55+
entries.push(Entry::End(ptr::null()));
56+
}
57+
}
58+
}
59+
// Add an `End` entry to the end with a reference to the enclosing token
60+
// stream which was passed in.
61+
entries.push(Entry::End(up));
62+
63+
// NOTE: This is done to ensure that we don't accidentally modify the
64+
// length of the backing buffer. The backing buffer must remain at a
65+
// constant address after this point, as we are going to store a raw
66+
// pointer into it.
67+
let mut entries = entries.into_boxed_slice();
68+
for (idx, group) in seqs {
69+
// We know that this index refers to one of the temporary
70+
// `End(null)` entries, and we know that the last entry is
71+
// `End(up)`, so the next index is also valid.
72+
let seq_up = &entries[idx + 1] as *const Entry;
73+
74+
// The end entry stored at the end of this Entry::Group should
75+
// point to the Entry which follows the Group in the list.
76+
let inner = Self::inner_new(group.stream(), seq_up);
77+
entries[idx] = Entry::Group(group, inner);
78+
}
79+
80+
TokenBuffer { data: entries }
81+
}
82+
83+
/// Creates a `TokenBuffer` containing all the tokens from the input
84+
/// `TokenStream`.
85+
pub fn new(stream: TokenStream) -> TokenBuffer {
86+
Self::inner_new(stream, ptr::null())
87+
}
88+
89+
/// Creates a cursor referencing the first token in the buffer and able to
90+
/// traverse until the end of the buffer.
91+
pub fn begin(&self) -> Cursor {
92+
unsafe { Cursor::create(&self.data[0], &self.data[self.data.len() - 1]) }
93+
}
94+
}
95+
96+
/// A cheaply copyable cursor into a `TokenBuffer`.
97+
///
98+
/// This cursor holds a shared reference into the immutable data which is used
99+
/// internally to represent a `TokenStream`, and can be efficiently manipulated
100+
/// and copied around.
101+
///
102+
/// An empty `Cursor` can be created directly, or one may create a `TokenBuffer`
103+
/// object and get a cursor to its first token with `begin()`.
104+
///
105+
/// Two cursors are equal if they have the same location in the same input
106+
/// stream, and have the same scope.
107+
///
108+
/// *This type is available only if Syn is built with the `"parsing"` feature.*
109+
pub struct Cursor<'a> {
110+
// The current entry which the `Cursor` is pointing at.
111+
ptr: *const Entry,
112+
// This is the only `Entry::End(..)` object which this cursor is allowed to
113+
// point at. All other `End` objects are skipped over in `Cursor::create`.
114+
scope: *const Entry,
115+
// Cursor is covariant in 'a. This field ensures that our pointers are still
116+
// valid.
117+
marker: PhantomData<&'a Entry>,
118+
}
119+
120+
impl<'a> Cursor<'a> {
121+
/// Creates a cursor referencing a static empty TokenStream.
122+
pub fn empty() -> Self {
123+
// It's safe in this situation for us to put an `Entry` object in global
124+
// storage, despite it not actually being safe to send across threads
125+
// (`Ident` is a reference into a thread-local table). This is because
126+
// this entry never includes a `Ident` object.
127+
//
128+
// This wrapper struct allows us to break the rules and put a `Sync`
129+
// object in global storage.
130+
struct UnsafeSyncEntry(Entry);
131+
unsafe impl Sync for UnsafeSyncEntry {}
132+
static EMPTY_ENTRY: UnsafeSyncEntry = UnsafeSyncEntry(Entry::End(0 as *const Entry));
133+
134+
Cursor {
135+
ptr: &EMPTY_ENTRY.0,
136+
scope: &EMPTY_ENTRY.0,
137+
marker: PhantomData,
138+
}
139+
}
140+
141+
/// This create method intelligently exits non-explicitly-entered
142+
/// `None`-delimited scopes when the cursor reaches the end of them,
143+
/// allowing for them to be treated transparently.
144+
unsafe fn create(mut ptr: *const Entry, scope: *const Entry) -> Self {
145+
// NOTE: If we're looking at a `End(..)`, we want to advance the cursor
146+
// past it, unless `ptr == scope`, which means that we're at the edge of
147+
// our cursor's scope. We should only have `ptr != scope` at the exit
148+
// from None-delimited groups entered with `ignore_none`.
149+
while let Entry::End(exit) = *ptr {
150+
if ptr == scope {
151+
break;
152+
}
153+
ptr = exit;
154+
}
155+
156+
Cursor {
157+
ptr,
158+
scope,
159+
marker: PhantomData,
160+
}
161+
}
162+
163+
/// Get the current entry.
164+
fn entry(self) -> &'a Entry {
165+
unsafe { &*self.ptr }
166+
}
167+
168+
/// Bump the cursor to point at the next token after the current one. This
169+
/// is undefined behavior if the cursor is currently looking at an
170+
/// `Entry::End`.
171+
unsafe fn bump(self) -> Cursor<'a> {
172+
Cursor::create(self.ptr.offset(1), self.scope)
173+
}
174+
175+
/// While the cursor is looking at a `None`-delimited group, move it to look
176+
/// at the first token inside instead. If the group is empty, this will move
177+
/// the cursor past the `None`-delimited group.
178+
///
179+
/// WARNING: This mutates its argument.
180+
fn ignore_none(&mut self) {
181+
while let Entry::Group(group, buf) = self.entry() {
182+
if group.delimiter() == Delimiter::None {
183+
// NOTE: We call `Cursor::create` here to make sure that
184+
// situations where we should immediately exit the span after
185+
// entering it are handled correctly.
186+
unsafe {
187+
*self = Cursor::create(&buf.data[0], self.scope);
188+
}
189+
} else {
190+
break;
191+
}
192+
}
193+
}
194+
195+
/// Checks whether the cursor is currently pointing at the end of its valid
196+
/// scope.
197+
pub fn eof(self) -> bool {
198+
// We're at eof if we're at the end of our scope.
199+
self.ptr == self.scope
200+
}
201+
202+
/// If the cursor is pointing at a `Group` with the given delimiter, returns
203+
/// a cursor into that group and one pointing to the next `TokenTree`.
204+
pub fn group(mut self, delim: Delimiter) -> Option<(Cursor<'a>, Span, Cursor<'a>)> {
205+
// If we're not trying to enter a none-delimited group, we want to
206+
// ignore them. We have to make sure to _not_ ignore them when we want
207+
// to enter them, of course. For obvious reasons.
208+
if delim != Delimiter::None {
209+
self.ignore_none();
210+
}
211+
212+
if let Entry::Group(group, buf) = self.entry() {
213+
if group.delimiter() == delim {
214+
return Some((buf.begin(), group.span(), unsafe { self.bump() }));
215+
}
216+
}
217+
218+
None
219+
}
220+
221+
/// If the cursor is pointing at a `Ident`, returns it along with a cursor
222+
/// pointing at the next `TokenTree`.
223+
pub fn ident(mut self) -> Option<(Ident, Cursor<'a>)> {
224+
self.ignore_none();
225+
match self.entry() {
226+
Entry::Ident(ident) => Some((ident.clone(), unsafe { self.bump() })),
227+
_ => None,
228+
}
229+
}
230+
231+
/// If the cursor is pointing at an `Punct`, returns it along with a cursor
232+
/// pointing at the next `TokenTree`.
233+
pub fn punct(mut self) -> Option<(Punct, Cursor<'a>)> {
234+
self.ignore_none();
235+
match self.entry() {
236+
Entry::Punct(op) if op.as_char() != '\'' => Some((op.clone(), unsafe { self.bump() })),
237+
_ => None,
238+
}
239+
}
240+
241+
/// If the cursor is pointing at a `Literal`, return it along with a cursor
242+
/// pointing at the next `TokenTree`.
243+
pub fn literal(mut self) -> Option<(Literal, Cursor<'a>)> {
244+
self.ignore_none();
245+
match self.entry() {
246+
Entry::Literal(lit) => Some((lit.clone(), unsafe { self.bump() })),
247+
_ => None,
248+
}
249+
}
250+
251+
/// Copies all remaining tokens visible from this cursor into a
252+
/// `TokenStream`.
253+
pub fn token_stream(self) -> TokenStream {
254+
let mut tts = Vec::new();
255+
let mut cursor = self;
256+
while let Some((tt, rest)) = cursor.token_tree() {
257+
tts.push(tt);
258+
cursor = rest;
259+
}
260+
tts.into_iter().collect()
261+
}
262+
263+
/// If the cursor is pointing at a `TokenTree`, returns it along with a
264+
/// cursor pointing at the next `TokenTree`.
265+
///
266+
/// Returns `None` if the cursor has reached the end of its stream.
267+
///
268+
/// This method does not treat `None`-delimited groups as transparent, and
269+
/// will return a `Group(None, ..)` if the cursor is looking at one.
270+
pub fn token_tree(self) -> Option<(TokenTree, Cursor<'a>)> {
271+
let tree = match self.entry() {
272+
Entry::Group(group, _) => group.clone().into(),
273+
Entry::Literal(lit) => lit.clone().into(),
274+
Entry::Ident(ident) => ident.clone().into(),
275+
Entry::Punct(op) => op.clone().into(),
276+
Entry::End(..) => {
277+
return None;
278+
}
279+
};
280+
281+
Some((tree, unsafe { self.bump() }))
282+
}
283+
284+
/// Returns the `Span` of the current token, or `Span::call_site()` if this
285+
/// cursor points to eof.
286+
pub fn span(self) -> Span {
287+
match self.entry() {
288+
Entry::Group(group, _) => group.span(),
289+
Entry::Literal(l) => l.span(),
290+
Entry::Ident(t) => t.span(),
291+
Entry::Punct(o) => o.span(),
292+
Entry::End(..) => Span::call_site(),
293+
}
294+
}
295+
}
296+
297+
impl<'a> Copy for Cursor<'a> {}
298+
299+
impl<'a> Clone for Cursor<'a> {
300+
fn clone(&self) -> Self {
301+
*self
302+
}
303+
}
304+
305+
impl<'a> Eq for Cursor<'a> {}
306+
307+
impl<'a> PartialEq for Cursor<'a> {
308+
fn eq(&self, other: &Self) -> bool {
309+
let Cursor { ptr, scope, marker } = self;
310+
let _ = marker;
311+
*ptr == other.ptr && *scope == other.scope
312+
}
313+
}

0 commit comments

Comments
 (0)