Skip to content

Commit c05a2b2

Browse files
committed
proc_macro: move SourceFile to the bridge.
1 parent 56668e6 commit c05a2b2

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/libproc_macro/bridge.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
2222
use std::cell::Cell;
2323
use std::fmt;
24+
use std::path::PathBuf;
2425
use std::ptr::NonNull;
2526

2627
use self::storage::{FromConcrete, ToConcrete};
@@ -195,6 +196,8 @@ storage_concrete_passthrough! {
195196
[] ::Span,
196197
[] ::Delimiter,
197198
[] ::LexError,
199+
200+
[] PathBuf,
198201
// NOTE(eddyb) this will need some `extern "C" fn write`.
199202
['a, 'b] &'a mut fmt::Formatter<'b>,
200203
[] fmt::Error
@@ -243,6 +246,17 @@ macro_rules! each_frontend_method {
243246
});
244247
$meth!(fn token_cursor_next(&self, cursor: &mut Self::TokenCursor)
245248
-> Option<Self::TokenStream>;);
249+
250+
$meth!(fn source_file_cleanup(&self, _file: Self::SourceFile) -> () {});
251+
$meth!(fn source_file_clone(&self, file: &Self::SourceFile) -> Self::SourceFile {
252+
file.clone()
253+
});
254+
$meth!(fn source_file_eq(&self, file1: &Self::SourceFile,
255+
file2: &Self::SourceFile) -> bool;);
256+
$meth!(fn source_file_path(&self, file: &Self::SourceFile) -> PathBuf;);
257+
$meth!(fn source_file_is_real(&self, file: &Self::SourceFile) -> bool;);
258+
259+
$meth!(fn span_source_file(&self, span: ::Span) -> Self::SourceFile;);
246260
}
247261
}
248262

@@ -253,6 +267,7 @@ pub trait FrontendInterface {
253267
type TokenStream: 'static + Clone + fmt::Debug + fmt::Display;
254268
type TokenStreamBuilder: 'static;
255269
type TokenCursor: 'static + Clone;
270+
type SourceFile: 'static + Clone;
256271
each_frontend_method!(define_frontend_trait_method);
257272
}
258273

@@ -310,6 +325,9 @@ define_boxed! {
310325
},
311326
TokenCursor {
312327
cleanup: token_cursor_cleanup
328+
},
329+
SourceFile {
330+
cleanup: source_file_cleanup
313331
}
314332
}
315333

@@ -337,6 +355,12 @@ impl Clone for TokenCursor {
337355
}
338356
}
339357

358+
impl Clone for SourceFile {
359+
fn clone(&self) -> Self {
360+
Frontend.source_file_clone(self)
361+
}
362+
}
363+
340364
pub(crate) struct Frontend;
341365

342366
macro_rules! define_frontend_current_method {
@@ -351,6 +375,7 @@ impl FrontendInterface for Frontend {
351375
type TokenStream = TokenStream;
352376
type TokenStreamBuilder = TokenStreamBuilder;
353377
type TokenCursor = TokenCursor;
378+
type SourceFile = SourceFile;
354379
each_frontend_method!(define_frontend_current_method);
355380
}
356381

@@ -359,6 +384,7 @@ type CurrentFrontend<'a> = FrontendInterface<
359384
TokenStream = TokenStream,
360385
TokenStreamBuilder = TokenStreamBuilder,
361386
TokenCursor = TokenCursor,
387+
SourceFile = SourceFile,
362388
> + 'a;
363389

364390
// Emulate scoped_thread_local!() here essentially
@@ -421,6 +447,7 @@ fn erase_concrete_frontend<F, G, R>(ng: extern "C" fn() -> generation::Generatio
421447
type TokenStream = TokenStream;
422448
type TokenStreamBuilder = TokenStreamBuilder;
423449
type TokenCursor = TokenCursor;
450+
type SourceFile = SourceFile;
424451
each_frontend_method!(define_frontend_erase_concrete_method);
425452
}
426453

src/libproc_macro/lib.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ pub use diagnostic::{Diagnostic, Level};
6565

6666
use std::{ascii, fmt, iter};
6767
use std::path::PathBuf;
68-
use rustc_data_structures::sync::Lrc;
6968
use std::str::FromStr;
7069

7170
use syntax::parse::token;
7271
use syntax::symbol::Symbol;
7372
use syntax_pos::DUMMY_SP;
74-
use syntax_pos::{FileMap, Pos, SyntaxContext, FileName};
73+
use syntax_pos::{Pos, SyntaxContext};
7574

7675
/// The main type provided by this crate, representing an abstract stream of
7776
/// tokens.
@@ -224,9 +223,7 @@ impl Span {
224223
/// The original source file into which this span points.
225224
#[unstable(feature = "proc_macro", issue = "38356")]
226225
pub fn source_file(&self) -> SourceFile {
227-
SourceFile {
228-
filemap: __internal::lookup_char_pos(self.0.lo()).file,
229-
}
226+
SourceFile(Frontend.span_source_file(*self))
230227
}
231228

232229
/// The `Span` for the tokens in the previous macro expansion from which
@@ -313,14 +310,7 @@ pub struct LineColumn {
313310
/// The source file of a given `Span`.
314311
#[unstable(feature = "proc_macro", issue = "38356")]
315312
#[derive(Clone)]
316-
pub struct SourceFile {
317-
filemap: Lrc<FileMap>,
318-
}
319-
320-
#[unstable(feature = "proc_macro", issue = "38356")]
321-
impl !Send for SourceFile {}
322-
#[unstable(feature = "proc_macro", issue = "38356")]
323-
impl !Sync for SourceFile {}
313+
pub struct SourceFile(bridge::SourceFile);
324314

325315
impl SourceFile {
326316
/// Get the path to this source file.
@@ -335,10 +325,7 @@ impl SourceFile {
335325
/// [`is_real`]: #method.is_real
336326
#[unstable(feature = "proc_macro", issue = "38356")]
337327
pub fn path(&self) -> PathBuf {
338-
match self.filemap.name {
339-
FileName::Real(ref path) => path.clone(),
340-
_ => PathBuf::from(self.filemap.name.to_string())
341-
}
328+
Frontend.source_file_path(&self.0)
342329
}
343330

344331
/// Returns `true` if this source file is a real source file, and not generated by an external
@@ -348,7 +335,7 @@ impl SourceFile {
348335
// This is a hack until intercrate spans are implemented and we can have real source files
349336
// for spans generated in external macros.
350337
// https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368
351-
self.filemap.is_real_file()
338+
Frontend.source_file_is_real(&self.0)
352339
}
353340
}
354341

@@ -366,7 +353,7 @@ impl fmt::Debug for SourceFile {
366353
#[unstable(feature = "proc_macro", issue = "38356")]
367354
impl PartialEq for SourceFile {
368355
fn eq(&self, other: &Self) -> bool {
369-
Lrc::ptr_eq(&self.filemap, &other.filemap)
356+
Frontend.source_file_eq(&self.0, &other.0)
370357
}
371358
}
372359

src/libproc_macro/rustc.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
use {__internal, Delimiter, Spacing, Span, Term, TokenNode, TokenTree};
1212

13-
use syntax_pos::{self, SyntaxContext, FileName};
13+
use rustc_data_structures::sync::Lrc;
14+
use std::path::PathBuf;
15+
use syntax_pos::{self, SyntaxContext, FileMap, FileName};
1416
use syntax_pos::hygiene::Mark;
1517
use syntax::ast;
1618
use syntax::ext::base::{ExtCtxt, ProcMacro};
@@ -58,7 +60,7 @@ impl ::bridge::FrontendInterface for Rustc {
5860
type TokenStream = tokenstream::TokenStream;
5961
type TokenStreamBuilder = tokenstream::TokenStreamBuilder;
6062
type TokenCursor = tokenstream::Cursor;
61-
63+
type SourceFile = Lrc<FileMap>;
6264

6365
fn token_stream_empty(&self) -> Self::TokenStream {
6466
tokenstream::TokenStream::empty()
@@ -279,4 +281,21 @@ impl ::bridge::FrontendInterface for Rustc {
279281
}
280282
None
281283
}
284+
285+
fn source_file_eq(&self, file1: &Self::SourceFile, file2: &Self::SourceFile) -> bool {
286+
Lrc::ptr_eq(file1, file2)
287+
}
288+
fn source_file_path(&self, file: &Self::SourceFile) -> PathBuf {
289+
match file.name {
290+
FileName::Real(ref path) => path.clone(),
291+
_ => PathBuf::from(file.name.to_string())
292+
}
293+
}
294+
fn source_file_is_real(&self, file: &Self::SourceFile) -> bool {
295+
file.is_real_file()
296+
}
297+
298+
fn span_source_file(&self, span: ::Span) -> Self::SourceFile {
299+
::__internal::lookup_char_pos(span.0.lo()).file
300+
}
282301
}

0 commit comments

Comments
 (0)