-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.rs
213 lines (178 loc) · 5.72 KB
/
source.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use crate::{
context::Context,
diagnostic::{error, fmt},
utility::monotonic::MonotonicVec,
};
use std::{
fmt,
path::{Path, PathBuf},
};
#[derive(Default)]
pub(crate) struct SourceMap {
files: MonotonicVec<SourceFile>,
}
impl SourceMap {
fn offset(&self) -> u32 {
const PADDING: u32 = 1;
self.files.last().map(|file| file.span.end).unwrap_or_default() + PADDING
}
// FIXME: Detect circular/cyclic imports here.
pub(crate) fn add(
&self,
path: Spanned<&Path>,
cx: Context<'_>,
) -> crate::error::Result<SourceFileRef<'_>> {
// We don't care about canonicalization (this is just for caching).
// We don't care about TOC-TOU (not safety critical).
if let Some(file) = self.files.iter().find(|file| file.path == path.bare) {
// FIXME: Safety comment.
return Ok(unsafe { file.as_ref() });
}
// FIXME: On `--force` (hypoth), we could suppress the error and
// create a sham/dummy SourceFile.
let contents = std::fs::read_to_string(path.bare).map_err(|error| {
self::error(fmt!("failed to read `{}`", path.bare.display()))
.highlight(path.span, cx)
.note(fmt!("{error}"))
.done()
})?;
let file = SourceFile::new(path.bare.to_owned(), contents, self.offset());
// FIXME: Safety comment.
let result = unsafe { file.as_ref() };
self.files.push(file);
Ok(result)
}
// FIXME: Use this comment again:
// UNSAFETY: `project` must return an address to `R` that remains valid even if `T` is moved.
pub(crate) fn by_span(&self, span: Span) -> Option<SourceFileRef<'_>> {
if span.is_sham() {
return None;
}
// FIXME: Perform a binary search by span instead.
let file = self.files.iter().find(|file| file.span.contains(span.start)).unwrap();
// FIXME: Safety comment.
Some(unsafe { file.as_ref() })
}
}
struct SourceFile {
path: PathBuf,
contents: String,
span: Span,
}
impl SourceFile {
fn new(path: PathBuf, contents: String, offset: u32) -> Self {
let span = Span::new(offset, offset + u32::try_from(contents.len()).unwrap());
Self { path, contents, span }
}
// FIXME: Safety conditions.
unsafe fn as_ref<'a>(&self) -> SourceFileRef<'a> {
// FIXME: Safety comments
SourceFileRef {
path: unsafe { &*std::ptr::from_ref(self.path.as_path()) },
contents: unsafe { &*std::ptr::from_ref(self.contents.as_str()) },
span: self.span,
}
}
}
#[derive(Clone, Copy)]
pub(crate) struct SourceFileRef<'a> {
pub(crate) path: &'a Path,
pub(crate) contents: &'a str,
pub(crate) span: Span<{ Locality::Global }>,
}
#[derive(Clone, Copy)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub(crate) struct Span<const L: Locality = { Locality::Global }> {
pub(crate) start: u32,
pub(crate) end: u32,
}
impl<const L: Locality> Span<L> {
pub(crate) const fn new(start: u32, end: u32) -> Self {
Self { start, end }
}
pub(crate) const fn empty(index: u32) -> Self {
Self::new(index, index)
}
pub(crate) fn with_len(start: u32, length: u32) -> Self {
Self::new(start, start + length)
}
pub(crate) const fn is_empty(self) -> bool {
self.start == self.end
}
const fn contains(self, index: u32) -> bool {
self.start <= index && index <= self.end
}
const fn unshift(self, offset: u32) -> Self {
Self::new(self.start - offset, self.end - offset)
}
pub(crate) const fn shift(self, offset: u32) -> Self {
Self::new(self.start + offset, self.end + offset)
}
pub(crate) const fn reinterpret<const P: Locality>(self) -> Span<P> {
Span::new(self.start, self.end)
}
}
impl Span {
pub(crate) const SHAM: Self = Self::new(0, 0);
pub(crate) const fn local(self, file: SourceFileRef<'_>) -> LocalSpan {
self.unshift(file.span.start).reinterpret()
}
pub(crate) const fn is_sham(self) -> bool {
self.start == 0 && self.end == 0
}
}
#[cfg(test)]
impl<const L: Locality> fmt::Debug for Span<L> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}..{}", self.start, self.end)
}
}
pub(crate) type LocalSpan = Span<{ Locality::Local }>;
impl LocalSpan {
#[allow(dead_code)] // FIXME: actually use it
pub(crate) fn global(self, file: SourceFileRef<'_>) -> Span {
self.shift(file.span.start).reinterpret()
}
pub(crate) fn range(self) -> std::ops::Range<usize> {
self.start as usize..self.end as usize
}
}
#[derive(PartialEq, Eq, std::marker::ConstParamTy)]
pub(crate) enum Locality {
Local,
Global,
}
#[derive(Clone, Copy)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub(crate) struct Spanned<T> {
pub(crate) span: Span,
pub(crate) bare: T,
}
impl<T> Spanned<T> {
pub(crate) fn new(span: Span, bare: T) -> Self {
Self { span, bare }
}
pub(crate) fn sham(bare: T) -> Self {
Self::new(Span::SHAM, bare)
}
pub(crate) fn map<U>(self, mapper: impl FnOnce(T) -> U) -> Spanned<U> {
Spanned::new(self.span, mapper(self.bare))
}
pub(crate) fn as_deref(&self) -> Spanned<&T::Target>
where
T: std::ops::Deref,
{
Spanned::new(self.span, &self.bare)
}
}
#[cfg(test)]
impl<T: fmt::Debug> fmt::Debug for Spanned<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{:?} {:?}", self.span, self.bare)
}
}
impl<T: fmt::Display> fmt::Display for Spanned<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.bare.fmt(f)
}
}