solar-sema 0.1.8

Solidity and Yul semantic analysis
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use crate::{
    ParsingContext, Sources, fmt_bytes,
    ty::{Gcx, GcxMut, GlobalCtxt},
};
use solar_data_structures::trustme;
use solar_interface::{Result, Session, diagnostics::DiagCtxt};
use std::{
    fmt,
    marker::PhantomPinned,
    mem::{ManuallyDrop, MaybeUninit},
    ops::ControlFlow,
    pin::Pin,
};
use thread_local::ThreadLocal;

/// The compiler.
///
/// This is the main entry point and driver for the compiler.
///
/// It must be [`enter`ed](Self::enter) to perform most operations, as it makes use of thread-local
/// storage, which is only available inside of a closure.
/// [`enter_mut`](Self::enter_mut) is only necessary when parsing sources and lowering the ASTs. All
/// accesses after can make use of `gcx`, passed by immutable reference.
///
/// Once a stage-advancing operation is performed, such as `parse`, `lower`, etc., the compiler may
/// not perform the same or a previous operation again, with the exception of `parse`.
///
/// # Examples
///
/// ```
/// # mod solar { pub use {solar_interface as interface, solar_sema as sema}; }
/// # fn main() {}
#[doc = include_str!("../doc-examples/hir.rs")]
/// ```
pub struct Compiler(ManuallyDrop<Pin<Box<CompilerInner<'static>>>>);

impl fmt::Debug for Compiler {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.enter_sequential(|compiler| compiler.debug_fmt("Compiler", f))
    }
}

struct CompilerInner<'a> {
    sess: Session,
    gcx: GlobalCtxt<'a>,
    /// Lifetimes in this struct are self-referential.
    _pinned: PhantomPinned,
}

/// `$x->$y` like in C.
macro_rules! project_ptr {
    ($x:ident -> $y:ident) => {
        &raw mut (*$x).$y
    };
}

impl Compiler {
    /// Creates a new compiler.
    #[expect(clippy::missing_transmute_annotations)]
    pub fn new(sess: Session) -> Self {
        let mut inner = Box::pin(MaybeUninit::<CompilerInner<'_>>::uninit());

        // SAFETY: Valid pointer, `init` initializes all fields.
        unsafe {
            let inner = Pin::get_unchecked_mut(Pin::as_mut(&mut inner));
            let inner = inner.as_mut_ptr();
            CompilerInner::init(inner, sess);
        }

        // SAFETY: `inner` has been initialized, `MaybeUninit<T>` is transmuted to `T`.
        Self(ManuallyDrop::new(unsafe { std::mem::transmute(inner) }))
    }

    /// Returns a reference to the compiler session.
    #[inline]
    pub fn sess(&self) -> &Session {
        &self.0.sess
    }

    /// Returns a mutable reference to the compiler session.
    #[inline]
    pub fn sess_mut(&mut self) -> &mut Session {
        self.as_mut().sess_mut()
    }

    /// Returns a reference to the diagnostics context.
    #[inline]
    pub fn dcx(&self) -> &DiagCtxt {
        &self.sess().dcx
    }

    /// Returns a mutable reference to the diagnostics context.
    #[inline]
    pub fn dcx_mut(&mut self) -> &mut DiagCtxt {
        &mut self.sess_mut().dcx
    }

    /// Enters the compiler context.
    ///
    /// See [`Session::enter`](Session::enter) for more details.
    pub fn enter<T: Send>(&self, f: impl FnOnce(&CompilerRef<'_>) -> T + Send) -> T {
        self.0.sess.enter(|| f(CompilerRef::new(&self.0)))
    }

    /// Enters the compiler context with mutable access.
    ///
    /// This is currently only necessary when parsing sources and lowering the ASTs.
    /// All accesses after can make use of `gcx`, passed by immutable reference.
    ///
    /// See [`Session::enter`](Session::enter) for more details.
    pub fn enter_mut<T: Send>(&mut self, f: impl FnOnce(&mut CompilerRef<'_>) -> T + Send) -> T {
        // SAFETY: `CompilerRef` does not allow mutable access to the session.
        let sess = unsafe { trustme::decouple_lt(&self.0.sess) };
        sess.enter(|| f(self.as_mut()))
    }

    /// Enters the compiler context.
    ///
    /// Note that this does not set up the rayon thread pool. This is only useful when parsing
    /// sequentially, like manually using `Parser`. Otherwise, it might cause panics later on if a
    /// thread pool is expected to be set up correctly.
    ///
    /// See [`enter`](Self::enter) for more details.
    pub fn enter_sequential<T>(&self, f: impl FnOnce(&CompilerRef<'_>) -> T) -> T {
        self.0.sess.enter_sequential(|| f(CompilerRef::new(&self.0)))
    }

    /// Enters the compiler context with mutable access.
    ///
    /// Note that this does not set up the rayon thread pool. This is only useful when parsing
    /// sequentially, like manually using `Parser`. Otherwise, it might cause panics later on if a
    /// thread pool is expected to be set up correctly.
    ///
    /// See [`enter_mut`](Self::enter_mut) for more details.
    pub fn enter_sequential_mut<T>(&mut self, f: impl FnOnce(&mut CompilerRef<'_>) -> T) -> T {
        // SAFETY: `CompilerRef` does not allow mutable access to the session.
        let sess = unsafe { trustme::decouple_lt(&self.0.sess) };
        sess.enter_sequential(|| f(self.as_mut()))
    }

    fn as_mut(&mut self) -> &mut CompilerRef<'_> {
        // SAFETY: `CompilerRef` does not allow invalidating the `Pin`.
        let inner = unsafe { Pin::get_unchecked_mut(Pin::as_mut(&mut self.0)) };
        let inner = unsafe {
            std::mem::transmute::<&mut CompilerInner<'static>, &mut CompilerInner<'_>>(inner)
        };
        CompilerRef::new_mut(inner)
    }
}

impl CompilerInner<'_> {
    #[inline]
    #[allow(elided_lifetimes_in_paths)]
    unsafe fn init(this: *mut Self, sess: Session) {
        unsafe {
            let sess_p = project_ptr!(this->sess);
            sess_p.write(sess);

            let sess = &*sess_p;
            project_ptr!(this->gcx).write(GlobalCtxt::new(sess));
        }
    }
}

impl Drop for CompilerInner<'_> {
    fn drop(&mut self) {
        log_ast_arenas_stats(&mut self.gcx.ast_arenas);
        debug!(hir_allocated = %fmt_bytes(self.gcx.hir_arenas.iter_mut().map(|a| a.allocated_bytes()).sum::<usize>()));
    }
}

impl Drop for Compiler {
    fn drop(&mut self) {
        let _guard = debug_span!("Compiler::drop").entered();
        unsafe { ManuallyDrop::drop(&mut self.0) };
    }
}

/// A reference to the compiler.
///
/// This is only available inside the [`Compiler::enter`] closure, and has access to the global
/// context.
#[repr(transparent)]
pub struct CompilerRef<'c> {
    inner: CompilerInner<'c>,
}

impl fmt::Debug for CompilerRef<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.debug_fmt("CompilerRef", f)
    }
}

impl<'c> CompilerRef<'c> {
    #[inline]
    fn new<'a>(inner: &'a CompilerInner<'c>) -> &'a Self {
        // SAFETY: `repr(transparent)`
        unsafe { std::mem::transmute(inner) }
    }

    #[inline]
    fn new_mut<'a>(inner: &'a mut CompilerInner<'c>) -> &'a mut Self {
        // SAFETY: `repr(transparent)`
        unsafe { std::mem::transmute(inner) }
    }

    /// Returns a reference to the compiler session.
    #[inline]
    pub fn sess(&self) -> &'c Session {
        self.gcx().sess
    }

    // NOTE: Do not expose mutable access to the session directly! See `replace_entered_session`.
    /// Returns a mutable reference to the compiler session.
    #[inline]
    fn sess_mut(&mut self) -> &mut Session {
        &mut self.inner.sess
    }

    /// Returns a reference to the diagnostics context.
    #[inline]
    pub fn dcx(&self) -> &'c DiagCtxt {
        &self.sess().dcx
    }

    /// Returns a mutable reference to the diagnostics context.
    #[inline]
    pub fn dcx_mut(&mut self) -> &mut DiagCtxt {
        &mut self.sess_mut().dcx
    }

    /// Returns a reference to the sources.
    #[inline]
    pub fn sources(&self) -> &'c Sources<'c> {
        &self.gcx().sources
    }

    /// Returns a mutable reference to the sources.
    #[inline]
    pub fn sources_mut(&mut self) -> &mut Sources<'c> {
        &mut self.gcx_mut().get_mut().sources
    }

    /// Returns a reference to the global context.
    #[inline]
    pub fn gcx(&self) -> Gcx<'c> {
        // SAFETY: `CompilerRef` is only accessible in the `Compiler::enter` closure.
        Gcx::new(unsafe { trustme::decouple_lt(&self.inner.gcx) })
    }

    #[inline]
    pub(crate) fn gcx_mut(&mut self) -> GcxMut<'c> {
        // SAFETY: `CompilerRef` is only accessible in the `Compiler::enter` closure.
        GcxMut::new(&mut self.inner.gcx)
    }

    /// Drops the sources, ASTs, and AST arenas in a separate thread.
    ///
    /// This is not done by default in the pipeline, but it can be called after `lower_asts` to
    /// free up memory.
    pub fn drop_asts(&mut self) {
        // TODO: Do we want to drop all the sources instead of just the ASTs?
        let sources = std::mem::take(&mut self.inner.gcx.sources);
        // SAFETY: `sources` points into `ast_arenas`, which we move together into the closure.
        let sources = unsafe { std::mem::transmute::<Sources<'_>, Sources<'static>>(sources) };
        let mut ast_arenas = std::mem::take(&mut self.inner.gcx.ast_arenas);
        self.inner.gcx.sess.spawn(move || {
            let _guard = debug_span!("drop_asts").entered();
            log_ast_arenas_stats(&mut ast_arenas);
            drop(sources);
            drop(ast_arenas);
        });
    }

    /// Returns a builder for parsing sources.
    ///
    /// [`ParsingContext::parse`](ParsingContext::parse) must be called at the end to actually parse
    /// the sources.
    pub fn parse(&mut self) -> ParsingContext<'c> {
        ParsingContext::new(self.gcx_mut())
    }

    /// Performs AST lowering.
    ///
    /// Lowers the entire program to HIR, populating `gcx.hir`.
    pub fn lower_asts(&mut self) -> Result<ControlFlow<()>> {
        crate::lower(self)
    }

    pub fn analysis(&self) -> Result<ControlFlow<()>> {
        crate::analysis(self.gcx())
    }

    fn debug_fmt(&self, name: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct(name).field("gcx", &self.gcx()).finish_non_exhaustive()
    }
}

fn log_ast_arenas_stats(arenas: &mut ThreadLocal<solar_ast::Arena>) {
    if arenas.iter_mut().len() == 0 {
        return;
    }
    debug!(asts_allocated = %fmt_bytes(arenas.iter_mut().map(|a| a.allocated_bytes()).sum::<usize>()));
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    // --- copy from `crates/interface/src/session.rs`
    use solar_ast::{Span, Symbol};
    use solar_interface::{BytePos, ColorChoice};

    /// Session to test `enter`.
    fn enter_tests_session() -> Session {
        let sess = Session::builder().with_buffer_emitter(ColorChoice::Never).build();
        sess.source_map().new_source_file(PathBuf::from("test"), "abcd").unwrap();
        sess
    }

    #[track_caller]
    fn use_globals_parallel(sess: &Session) {
        use rayon::prelude::*;

        use_globals();
        sess.spawn(|| use_globals());
        sess.join(|| use_globals(), || use_globals());
        [1, 2, 3].par_iter().for_each(|_| use_globals());
        use_globals();
    }

    #[track_caller]
    fn use_globals() {
        use_globals_no_sm();

        let span = Span::new(BytePos(0), BytePos(1));
        assert_eq!(format!("{span:?}"), "test:1:1: 1:2");
        assert_eq!(format!("{span:#?}"), "test:1:1: 1:2");
    }

    #[track_caller]
    fn use_globals_no_sm() {
        let s = "hello";
        let sym = Symbol::intern(s);
        assert_eq!(sym.as_str(), s);
    }
    // --- end copy

    #[test]
    fn parse_multiple_times() {
        let sess = Session::builder().with_test_emitter().build();
        let mut compiler = Compiler::new(sess);

        assert!(compiler.enter(|c| c.gcx().sources.is_empty()));
        compiler.enter_mut(|c| {
            let pcx = c.parse();
            pcx.parse();
        });
        assert!(compiler.enter(|c| c.gcx().sources.is_empty()));

        compiler.enter_mut(|c| {
            let mut pcx = c.parse();
            pcx.add_file(
                c.sess().source_map().new_source_file(PathBuf::from("test.sol"), "").unwrap(),
            );
            pcx.parse();
        });
        assert_eq!(compiler.enter(|c| c.gcx().sources.len()), 1);
        assert_eq!(compiler.enter(|c| c.gcx().sources.asts().count()), 1);

        compiler.enter_mut(|c| {
            let mut pcx = c.parse();
            pcx.add_file(
                c.sess().source_map().new_source_file(PathBuf::from("test2.sol"), "").unwrap(),
            );
            pcx.parse();
        });
        assert_eq!(compiler.enter(|c| c.gcx().sources.len()), 2);
        assert_eq!(compiler.enter(|c| c.gcx().sources.asts().count()), 2);

        compiler.enter_mut(|c| c.drop_asts());
        assert_eq!(compiler.enter(|c| c.gcx().sources.len()), 0);
        assert_eq!(compiler.enter(|c| c.gcx().sources.asts().count()), 0);
    }

    fn stage_test(expected: Result<(), &str>, f: fn(&mut CompilerRef<'_>)) {
        let sess =
            Session::builder().with_buffer_emitter(solar_interface::ColorChoice::Never).build();
        let mut compiler = Compiler::new(sess);
        let r = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| compiler.enter_mut(f)));
        let errs = compiler.sess().dcx.emitted_errors().unwrap();
        match expected {
            Ok(()) => assert!(r.is_ok(), "panicked: {errs:#?}"),
            Err(e) => {
                assert!(r.is_err(), "didn't panic: {errs:#?}");
                let errs = errs.unwrap_err();
                let d = errs.to_string();
                assert!(d.contains("invalid compiler stage transition:"), "{d}");
                assert!(d.contains(e), "{d}");
                assert!(d.contains("stages must be advanced sequentially"), "{d}");
            }
        }
    }

    fn parse_dummy_file(c: &mut CompilerRef<'_>) {
        let mut pcx = c.parse();
        pcx.add_file(c.sess().source_map().new_source_file(PathBuf::from("test.sol"), "").unwrap());
        pcx.parse();
    }

    #[test]
    fn stage_tests() {
        // Backwards.
        stage_test(Err("from `lowering` to `parsing`"), |c| {
            parse_dummy_file(c);
            assert_eq!(c.lower_asts(), Ok(ControlFlow::Continue(())));
            parse_dummy_file(c);
        });

        // Too far ahead.
        stage_test(Err("from `none` to `analysis`"), |c| {
            assert_eq!(c.analysis(), Ok(ControlFlow::Continue(())));
        });

        // Same stage.
        stage_test(Err("from `lowering` to `lowering`"), |c| {
            parse_dummy_file(c);
            assert_eq!(c.lower_asts(), Ok(ControlFlow::Continue(())));
            assert_eq!(c.lower_asts(), Ok(ControlFlow::Continue(())));
            assert_eq!(c.analysis(), Ok(ControlFlow::Continue(())));
        });
        stage_test(Err("from `analysis` to `analysis`"), |c| {
            parse_dummy_file(c);
            assert_eq!(c.lower_asts(), Ok(ControlFlow::Continue(())));
            assert_eq!(c.analysis(), Ok(ControlFlow::Continue(())));
            assert_eq!(c.analysis(), Ok(ControlFlow::Continue(())));
        });
        // Parsing is special cased.
        stage_test(Ok(()), |c| {
            parse_dummy_file(c);
            parse_dummy_file(c);
        });
    }

    #[test]
    fn replace_session() {
        let mut compiler = Compiler::new(Session::builder().with_test_emitter().build());
        compiler.dcx().err("test").emit();
        assert!(compiler.sess().dcx.has_errors().is_err());
        *compiler.sess_mut() = Session::builder().with_test_emitter().build();
        assert!(compiler.sess().dcx.has_errors().is_ok());
    }

    #[test]
    fn replace_entered_session() {
        let mut compiler = Compiler::new(enter_tests_session());
        compiler.enter_mut(|compiler| {
            use_globals_parallel(compiler.sess());

            compiler.dcx().err("test").emit();
            assert!(compiler.sess().dcx.has_errors().is_err());

            // Replacing `Session` here drops the internal thread pool, which is currently in use,
            // so we must not expose mutable access to the session.
            *compiler.dcx_mut() = enter_tests_session().dcx;
            assert!(compiler.sess().dcx.has_errors().is_ok());

            use_globals_parallel(compiler.sess());
        });
        assert!(compiler.sess().dcx.has_errors().is_ok());
    }
}