paralegal_flow/
ctx.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
use std::rc::Rc;

use flowistry_pdg_construction::body_cache::BodyCache;
use rustc_errors::DiagMessage;
use rustc_middle::ty::TyCtxt;
use rustc_span::Span;

use crate::{ann::db::MarkerDatabase, MarkerCtx};

#[derive(Clone)]
pub struct Pctx<'tcx>(Rc<PctxPayload<'tcx>>);

impl<'tcx> Pctx<'tcx> {
    pub fn new(tcx: TyCtxt<'tcx>, opts: &'static crate::Args) -> Self {
        let body_cache = Rc::new(BodyCache::new(tcx));
        let marker_ctx = MarkerDatabase::init(tcx, opts, body_cache.clone()).into();
        let payload = PctxPayload {
            tcx,
            body_cache,
            opts,
            marker_ctx,
        };
        Self(Rc::new(payload))
    }

    pub fn tcx(&self) -> TyCtxt<'tcx> {
        self.0.tcx
    }

    pub fn body_cache(&self) -> &Rc<BodyCache<'tcx>> {
        &self.0.body_cache
    }

    pub fn opts(&self) -> &'static crate::Args {
        self.0.opts
    }

    pub fn marker_ctx(&self) -> &MarkerCtx<'tcx> {
        &self.0.marker_ctx
    }

    /// Emit an errror or a warning if relaxed.
    pub fn maybe_span_err(&self, span: Span, msg: impl Into<DiagMessage>) {
        if self.opts().relaxed() {
            self.tcx().dcx().span_warn(span, msg);
        } else {
            self.tcx().dcx().span_err(span, msg);
        }
    }
}

struct PctxPayload<'tcx> {
    tcx: TyCtxt<'tcx>,
    body_cache: Rc<BodyCache<'tcx>>,
    opts: &'static crate::Args,
    marker_ctx: MarkerCtx<'tcx>,
}