Skip to main content

charon_lib/transform/
ctx.rs

1use crate::ast::*;
2use crate::errors::{ErrorCtx, Level};
3use crate::formatter::{FmtCtx, IntoFormatter};
4use crate::llbc_ast;
5use crate::options::TranslateOptions;
6use crate::pretty::FmtWithCtx;
7use crate::ullbc_ast;
8use std::cell::RefCell;
9use std::{fmt, mem};
10
11/// Simpler context used for rustc-independent code transformation. This only depends on rustc for
12/// its error reporting machinery.
13pub struct TransformCtx {
14    /// The options that control transformation.
15    pub options: TranslateOptions,
16    /// The translated data.
17    pub translated: TranslatedCrate,
18    /// Context for tracking and reporting errors.
19    pub errors: RefCell<ErrorCtx>,
20}
21
22/// A pass that modifies ullbc bodies.
23pub trait UllbcPass: Sync {
24    /// Whether the pass should run.
25    fn should_run(&self, _options: &TranslateOptions) -> bool {
26        true
27    }
28
29    /// Transform a body.
30    fn transform_body(&self, _ctx: &mut TransformCtx, _body: &mut ullbc_ast::ExprBody) {}
31
32    /// Transform a function declaration. This forwards to `transform_body` by default.
33    fn transform_function(&self, ctx: &mut TransformCtx, decl: &mut FunDecl) {
34        if let Some(body) = decl.body.as_unstructured_mut() {
35            self.transform_body(ctx, body)
36        }
37    }
38
39    /// Transform the given context. This forwards to the other methods by default.
40    fn transform_ctx(&self, ctx: &mut TransformCtx) {
41        ctx.for_each_fun_decl(|ctx, decl| {
42            self.log_before_body(ctx, &decl.item_meta.name, &decl.body);
43            self.transform_function(ctx, decl);
44        });
45    }
46
47    /// The name of the pass, used for debug logging. The default implementation uses the type
48    /// name.
49    fn name(&self) -> &str {
50        std::any::type_name::<Self>()
51    }
52
53    /// Log that the pass is about to be run on this body.
54    fn log_before_body(&self, ctx: &TransformCtx, name: &Name, body: &Body) {
55        let fmt_ctx = &ctx.into_fmt();
56        trace!(
57            "# About to run pass [{}] on `{}`:\n{}",
58            self.name(),
59            name.with_ctx(fmt_ctx),
60            body.with_ctx(fmt_ctx),
61        );
62    }
63}
64
65/// A pass that modifies ullbc bodies and can be fused with previous passes so that we run all of
66/// them on a given body.
67pub trait FusedUllbcPass: Sync {
68    /// Whether the pass should run.
69    fn should_run(&self, _options: &TranslateOptions) -> bool {
70        true
71    }
72
73    /// Transform a body.
74    fn transform_body(&self, _ctx: &mut TransformCtx, _body: &mut ullbc_ast::ExprBody) {}
75
76    /// Transform a function declaration. This forwards to `transform_body` by default.
77    fn transform_function(&self, ctx: &mut TransformCtx, decl: &mut FunDecl) {
78        if let Some(body) = decl.body.as_unstructured_mut() {
79            self.transform_body(ctx, body)
80        }
81    }
82
83    /// The name of the pass, used for debug logging. The default implementation uses the type
84    /// name.
85    fn name(&self) -> &str {
86        std::any::type_name::<Self>()
87    }
88}
89
90/// A pass that modifies llbc bodies.
91pub trait LlbcPass: Sync {
92    /// Whether the pass should run.
93    fn should_run(&self, _options: &TranslateOptions) -> bool {
94        true
95    }
96
97    /// Transform a body.
98    fn transform_body(&self, _ctx: &mut TransformCtx, _body: &mut llbc_ast::ExprBody) {}
99
100    /// Transform a function declaration. This forwards to `transform_body` by default.
101    fn transform_function(&self, ctx: &mut TransformCtx, decl: &mut FunDecl) {
102        if let Some(body) = decl.body.as_structured_mut() {
103            self.transform_body(ctx, body)
104        }
105    }
106
107    /// Transform the given context. This forwards to the other methods by default.
108    fn transform_ctx(&self, ctx: &mut TransformCtx) {
109        ctx.for_each_fun_decl(|ctx, decl| {
110            self.log_before_body(ctx, &decl.item_meta.name, &decl.body);
111            self.transform_function(ctx, decl);
112        });
113    }
114
115    /// The name of the pass, used for debug logging. The default implementation uses the type
116    /// name.
117    fn name(&self) -> &str {
118        std::any::type_name::<Self>()
119    }
120
121    /// Log that the pass is about to be run on this body.
122    fn log_before_body(&self, ctx: &TransformCtx, name: &Name, body: &Body) {
123        let fmt_ctx = &ctx.into_fmt();
124        let body_str = body.to_string_with_ctx(fmt_ctx);
125        trace!(
126            "# About to run pass [{}] on `{}`:\n{}",
127            self.name(),
128            name.with_ctx(fmt_ctx),
129            body_str,
130        );
131    }
132}
133
134/// A pass that transforms the crate data.
135pub trait TransformPass: Sync {
136    /// Whether the pass should run.
137    fn should_run(&self, _options: &TranslateOptions) -> bool {
138        true
139    }
140
141    fn transform_ctx(&self, ctx: &mut TransformCtx);
142
143    /// The name of the pass, used for debug logging. The default implementation uses the type
144    /// name.
145    fn name(&self) -> &str {
146        std::any::type_name::<Self>()
147    }
148}
149
150impl TransformCtx {
151    pub(crate) fn has_errors(&self) -> bool {
152        self.errors.borrow().has_errors()
153    }
154
155    /// Span an error and register the error.
156    pub(crate) fn span_err(&self, span: Span, msg: &str, level: Level) -> Error {
157        self.errors
158            .borrow_mut()
159            .span_err(&self.translated, span, msg, level)
160    }
161
162    pub(crate) fn opacity_for_name(&self, name: &Name) -> ItemOpacity {
163        self.options.opacity_for_name(&self.translated, name)
164    }
165
166    pub(crate) fn with_def_id<F, T>(
167        &mut self,
168        def_id: impl Into<ItemId>,
169        def_id_is_local: bool,
170        f: F,
171    ) -> T
172    where
173        F: FnOnce(&mut Self) -> T,
174    {
175        let mut errors = self.errors.borrow_mut();
176        let current_def_id = errors.def_id.replace(def_id.into());
177        let current_def_id_is_local = mem::replace(&mut errors.def_id_is_local, def_id_is_local);
178        drop(errors); // important: release the refcell "lock"
179        let ret = f(self);
180        let mut errors = self.errors.borrow_mut();
181        errors.def_id = current_def_id;
182        errors.def_id_is_local = current_def_id_is_local;
183        ret
184    }
185
186    /// Mutably iterate over the bodies.
187    /// Warning: we replace each body with `Err(Opaque)` while inspecting it so we can keep access
188    /// to the rest of the crate.
189    pub(crate) fn for_each_body(&mut self, mut f: impl FnMut(&mut Self, &mut Body)) {
190        let fn_ids = self.translated.fun_decls.all_indices();
191        for id in fn_ids {
192            if let Some(decl) = self.translated.fun_decls.get_mut(id)
193                && decl.body.has_contents()
194            {
195                let mut body = mem::replace(&mut decl.body, Body::Opaque);
196                let fun_decl_id = decl.def_id;
197                let is_local = decl.item_meta.is_local;
198                self.with_def_id(fun_decl_id, is_local, |ctx| f(ctx, &mut body));
199                self.translated.fun_decls[id].body = body;
200            }
201        }
202    }
203
204    /// Mutably iterate over the function declarations.
205    /// Warning: each inspected fundecl becomes inaccessible from `ctx` during the course of this function.
206    pub(crate) fn for_each_fun_decl(&mut self, mut f: impl FnMut(&mut Self, &mut FunDecl)) {
207        let fn_ids = self.translated.fun_decls.all_indices();
208        for id in fn_ids {
209            if let Some(mut decl) = self.translated.fun_decls.remove(id) {
210                let fun_decl_id = decl.def_id;
211                let is_local = decl.item_meta.is_local;
212                self.with_def_id(fun_decl_id, is_local, |ctx| f(ctx, &mut decl));
213                self.translated.fun_decls.set_slot(id, decl);
214            }
215        }
216    }
217
218    /// Iterate mutably over all items, keeping access to `self`. To make this work, we move out
219    /// each item before iterating over it. Items added during traversal will not be iterated over.
220    pub fn for_each_item_mut(&mut self, mut f: impl for<'a> FnMut(&'a mut Self, ItemRefMut<'a>)) {
221        for id in self.translated.all_ids() {
222            if let Some(mut decl) = self.translated.remove_item_temporarily(id) {
223                f(self, decl.as_mut());
224                self.translated.set_item_slot(id, decl);
225            }
226        }
227    }
228}
229
230impl<'a> IntoFormatter for &'a TransformCtx {
231    type C = FmtCtx<'a>;
232
233    fn into_fmt(self) -> Self::C {
234        self.translated.into_fmt()
235    }
236}
237
238impl fmt::Display for TransformCtx {
239    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
240        self.translated.fmt(f)
241    }
242}
243
244/// A helper trait that captures common operations in body transformation.
245pub trait BodyTransformCtx: Sized {
246    fn get_crate(&self) -> &TranslatedCrate;
247    fn get_options(&self) -> &TranslateOptions;
248    fn get_params(&self) -> &GenericParams;
249    fn get_locals_mut(&mut self) -> &mut Locals;
250
251    fn insert_storage_live_stmt(&mut self, local: LocalId);
252    fn insert_storage_dead_stmt(&mut self, local: LocalId);
253    fn insert_assn_stmt(&mut self, place: Place, rvalue: Rvalue);
254
255    fn to_fmt(&self) -> FmtCtx<'_> {
256        self.get_crate().into_fmt()
257    }
258
259    /// Create a local & return the place pointing to it
260    fn fresh_var(&mut self, name: Option<String>, ty: Ty) -> Place {
261        let var = self.get_locals_mut().new_var(name, ty);
262        self.insert_storage_live_stmt(var.local_id().unwrap());
263        var
264    }
265
266    /// Assign an rvalue to a place, unless the rvalue is a move in which case we just use the
267    /// moved place.
268    fn rval_to_place(&mut self, rvalue: Rvalue, ty: Ty) -> Place {
269        if let Rvalue::Use(Operand::Move(place)) = rvalue {
270            place
271        } else {
272            let var = self.fresh_var(None, ty);
273            self.insert_assn_stmt(var.clone(), rvalue);
274            var
275        }
276    }
277
278    /// When `from_end` is true, we need to compute `len(p) - last_arg` instead of just using `last_arg`.
279    /// Otherwise, we simply return `last_arg`.
280    /// New local variables are created as needed.
281    ///
282    /// The `last_arg` is either the `offset` for `Index` or the `to` for `Subslice` for the projections.
283    fn compute_subslice_end_idx(
284        &mut self,
285        len_place: &Place,
286        last_arg: Operand,
287        from_end: bool,
288    ) -> Operand {
289        if from_end {
290            // `storage_live(len_var)`
291            // `len_var = len(p)`
292            let len_var = self.fresh_var(None, Ty::mk_usize());
293            let len = match len_place.ty().kind() {
294                TyKind::Array(_, len) => Some(len.clone()),
295                TyKind::Slice(_) => None,
296                _ => panic!(
297                    "called `compute_subslice_end_idx` on something that isn't an array or slice: {:?}",
298                    len_place.ty()
299                ),
300            };
301            self.insert_assn_stmt(
302                len_var.clone(),
303                Rvalue::Len(len_place.clone(), len_place.ty().clone(), len),
304            );
305
306            // `storage_live(index_var)`
307            // `index_var = len_var - last_arg`
308            // `storage_dead(len_var)`
309            let index_var = self.fresh_var(None, Ty::mk_usize());
310            self.insert_assn_stmt(
311                index_var.clone(),
312                Rvalue::BinaryOp(
313                    BinOp::Sub(OverflowMode::UB),
314                    Operand::Copy(len_var.clone()),
315                    last_arg,
316                ),
317            );
318            self.insert_storage_dead_stmt(len_var.local_id().unwrap());
319            Operand::Copy(index_var)
320        } else {
321            last_arg
322        }
323    }
324
325    fn is_sized_type_var(&mut self, ty: &Ty) -> bool {
326        match ty.kind() {
327            TyKind::TypeVar(..) => {
328                if self.get_options().hide_marker_traits {
329                    // If we're hiding `Sized`, let's consider everything to be sized.
330                    return true;
331                }
332                let params = self.get_params();
333                for clause in &params.trait_clauses {
334                    let tref = clause.trait_.clone().erase();
335                    // Check if it is `Sized<T>`
336                    if tref.generics.types[0] == *ty
337                        && self
338                            .get_crate()
339                            .trait_decls
340                            .get(tref.id)
341                            .and_then(|decl| decl.item_meta.lang_item.clone())
342                            == Some("sized".into())
343                    {
344                        return true;
345                    }
346                }
347                false
348            }
349            _ => false,
350        }
351    }
352
353    /// Emit statements that compute the metadata of the given place. Returns an operand containing the
354    /// metadata value.
355    ///
356    /// E.g., for:
357    /// ```ignore
358    /// let x = &(*ptr).field;
359    /// ```
360    /// if `(*ptr).field` is a DST like `[i32]`, this will get the metadata from the appropriate
361    /// pointer:
362    /// ```ignore
363    /// let len = ptr.metadata;
364    /// ```
365    /// and return `Operand::Move(len)`.
366    ///
367    fn compute_place_metadata(&mut self, place: &Place) -> Operand {
368        /// Compute the metadata for a place. Return `None` if the place has no metadata.
369        fn compute_place_metadata_inner<T: BodyTransformCtx>(
370            ctx: &mut T,
371            place: &Place,
372            metadata_ty: &Ty,
373        ) -> Option<Operand> {
374            let (subplace, proj) = place.as_projection()?;
375            match proj {
376                // The outermost deref we encountered gives us the metadata of the place.
377                ProjectionElem::Deref => {
378                    let metadata_place = subplace
379                        .clone()
380                        .project(ProjectionElem::PtrMetadata, metadata_ty.clone());
381                    Some(Operand::Copy(metadata_place))
382                }
383                ProjectionElem::Field { .. } => {
384                    compute_place_metadata_inner(ctx, subplace, metadata_ty)
385                }
386                // Indexing for array & slice will only result in sized types, hence no metadata
387                ProjectionElem::Index { .. } => None,
388                // Ptr metadata is always sized.
389                ProjectionElem::PtrMetadata => None,
390                // Subslice must have metadata length, compute the metadata here as `to` - `from`
391                ProjectionElem::Subslice { from, to, from_end } => {
392                    let to_idx = ctx.compute_subslice_end_idx(subplace, *to.clone(), *from_end);
393                    let diff_place = ctx.fresh_var(None, Ty::mk_usize());
394                    ctx.insert_assn_stmt(
395                        diff_place.clone(),
396                        // Overflow is UB and should have been prevented by a bound check beforehand.
397                        Rvalue::BinaryOp(BinOp::Sub(OverflowMode::UB), to_idx, *from.clone()),
398                    );
399                    Some(Operand::Copy(diff_place))
400                }
401            }
402        }
403        trace!(
404            "getting ptr metadata for place: {}",
405            place.with_ctx(&self.to_fmt())
406        );
407        let metadata_ty = place.ty().get_ptr_metadata(self.get_crate()).into_type();
408        if metadata_ty.is_unit()
409            || matches!(metadata_ty.kind(), TyKind::PtrMetadata(ty) if self.is_sized_type_var(ty))
410        {
411            // If the type var is known to be `Sized`, then no metadata is needed
412            return Operand::mk_const_unit();
413        }
414        trace!(
415            "computed metadata type: {}",
416            metadata_ty.with_ctx(&self.to_fmt())
417        );
418        compute_place_metadata_inner(self, place, &metadata_ty)
419            .unwrap_or_else(Operand::mk_const_unit)
420    }
421
422    /// Create a `&` borrow of the place.
423    fn borrow(&mut self, place: Place, kind: BorrowKind) -> Rvalue {
424        let ptr_metadata = self.compute_place_metadata(&place);
425        Rvalue::Ref {
426            place,
427            kind,
428            ptr_metadata,
429        }
430    }
431    /// Create a `&raw` borrow of the place.
432    fn raw_borrow(&mut self, place: Place, kind: RefKind) -> Rvalue {
433        let ptr_metadata = self.compute_place_metadata(&place);
434        Rvalue::RawPtr {
435            place,
436            kind,
437            ptr_metadata,
438        }
439    }
440
441    /// Store a `&` borrow of the place into a new place.
442    fn borrow_to_new_var(&mut self, place: Place, kind: BorrowKind, name: Option<String>) -> Place {
443        let ref_ty = TyKind::Ref(Region::Erased, place.ty().clone(), kind.into()).into_ty();
444        let target_place = self.fresh_var(name, ref_ty);
445        let rvalue = self.borrow(place, kind);
446        self.insert_assn_stmt(target_place.clone(), rvalue);
447        target_place
448    }
449    /// Store a `&raw` borrow of the place into a new place.
450    fn raw_borrow_to_new_var(
451        &mut self,
452        place: Place,
453        kind: RefKind,
454        name: Option<String>,
455    ) -> Place {
456        let ref_ty = TyKind::RawPtr(place.ty().clone(), kind).into_ty();
457        let target_place = self.fresh_var(name, ref_ty);
458        let rvalue = self.raw_borrow(place, kind);
459        self.insert_assn_stmt(target_place.clone(), rvalue);
460        target_place
461    }
462}
463
464pub struct UllbcStatementTransformCtx<'a> {
465    pub ctx: &'a mut TransformCtx,
466    pub params: &'a GenericParams,
467    pub locals: &'a mut Locals,
468    /// Span of the statement being explored
469    pub span: Span,
470    /// Statements to prepend to the statement currently being explored.
471    pub statements: Vec<ullbc_ast::Statement>,
472}
473
474impl BodyTransformCtx for UllbcStatementTransformCtx<'_> {
475    fn get_crate(&self) -> &TranslatedCrate {
476        &self.ctx.translated
477    }
478    fn get_options(&self) -> &TranslateOptions {
479        &self.ctx.options
480    }
481    fn get_params(&self) -> &GenericParams {
482        self.params
483    }
484    fn get_locals_mut(&mut self) -> &mut Locals {
485        self.locals
486    }
487
488    fn insert_storage_live_stmt(&mut self, local: LocalId) {
489        self.statements.push(ullbc_ast::Statement::new(
490            self.span,
491            ullbc_ast::StatementKind::StorageLive(local),
492        ));
493    }
494
495    fn insert_assn_stmt(&mut self, place: Place, rvalue: Rvalue) {
496        self.statements.push(ullbc_ast::Statement::new(
497            self.span,
498            ullbc_ast::StatementKind::Assign(place, rvalue),
499        ));
500    }
501
502    fn insert_storage_dead_stmt(&mut self, local: LocalId) {
503        self.statements.push(ullbc_ast::Statement::new(
504            self.span,
505            ullbc_ast::StatementKind::StorageDead(local),
506        ));
507    }
508}
509
510pub struct LlbcStatementTransformCtx<'a> {
511    pub ctx: &'a mut TransformCtx,
512    pub params: &'a GenericParams,
513    pub locals: &'a mut Locals,
514    /// Span of the statement being explored
515    pub span: Span,
516    /// Statements to prepend to the statement currently being explored.
517    pub statements: Vec<llbc_ast::Statement>,
518}
519
520impl BodyTransformCtx for LlbcStatementTransformCtx<'_> {
521    fn get_crate(&self) -> &TranslatedCrate {
522        &self.ctx.translated
523    }
524    fn get_options(&self) -> &TranslateOptions {
525        &self.ctx.options
526    }
527    fn get_params(&self) -> &GenericParams {
528        self.params
529    }
530    fn get_locals_mut(&mut self) -> &mut Locals {
531        self.locals
532    }
533
534    fn insert_storage_live_stmt(&mut self, local: LocalId) {
535        self.statements.push(llbc_ast::Statement::new(
536            self.span,
537            llbc_ast::StatementKind::StorageLive(local),
538        ));
539    }
540
541    fn insert_assn_stmt(&mut self, place: Place, rvalue: Rvalue) {
542        self.statements.push(llbc_ast::Statement::new(
543            self.span,
544            llbc_ast::StatementKind::Assign(place, rvalue),
545        ));
546    }
547
548    fn insert_storage_dead_stmt(&mut self, local: LocalId) {
549        self.statements.push(llbc_ast::Statement::new(
550            self.span,
551            llbc_ast::StatementKind::StorageDead(local),
552        ));
553    }
554}
555
556impl FunDecl {
557    pub fn transform_ullbc_statements(
558        &mut self,
559        ctx: &mut TransformCtx,
560        mut f: impl FnMut(&mut UllbcStatementTransformCtx, &mut ullbc_ast::Statement),
561    ) {
562        if let Some(body) = self.body.as_unstructured_mut() {
563            let mut ctx = UllbcStatementTransformCtx {
564                ctx,
565                params: &self.generics,
566                locals: &mut body.locals,
567                span: self.item_meta.span,
568                statements: Vec::new(),
569            };
570            body.body.iter_mut().for_each(|block| {
571                ctx.statements = Vec::with_capacity(block.statements.len());
572                for mut st in mem::take(&mut block.statements) {
573                    ctx.span = st.span;
574                    f(&mut ctx, &mut st);
575                    ctx.statements.push(st);
576                }
577                block.statements = mem::take(&mut ctx.statements);
578            });
579        }
580    }
581
582    pub fn transform_ullbc_terminators(
583        &mut self,
584        ctx: &mut TransformCtx,
585        mut f: impl FnMut(&mut UllbcStatementTransformCtx, &mut ullbc_ast::Terminator),
586    ) {
587        if let Some(body) = self.body.as_unstructured_mut() {
588            let mut ctx = UllbcStatementTransformCtx {
589                ctx,
590                params: &self.generics,
591                locals: &mut body.locals,
592                span: self.item_meta.span,
593                statements: Vec::new(),
594            };
595            body.body.iter_mut().for_each(|block| {
596                ctx.span = block.terminator.span;
597                ctx.statements = mem::take(&mut block.statements);
598                f(&mut ctx, &mut block.terminator);
599                block.statements = mem::take(&mut ctx.statements);
600            });
601        }
602    }
603
604    pub fn transform_ullbc_operands(
605        &mut self,
606        ctx: &mut TransformCtx,
607        mut f: impl FnMut(&mut UllbcStatementTransformCtx, &mut Operand),
608    ) {
609        self.transform_ullbc_statements(ctx, |ctx, st| {
610            st.kind.dyn_visit_in_body_mut(|op: &mut Operand| f(ctx, op));
611        });
612        self.transform_ullbc_terminators(ctx, |ctx, st| {
613            st.kind.dyn_visit_in_body_mut(|op: &mut Operand| f(ctx, op));
614        });
615    }
616
617    pub fn transform_llbc_statements(
618        &mut self,
619        ctx: &mut TransformCtx,
620        mut f: impl FnMut(&mut LlbcStatementTransformCtx, &mut llbc_ast::Statement),
621    ) {
622        if let Some(body) = self.body.as_structured_mut() {
623            let mut ctx = LlbcStatementTransformCtx {
624                ctx,
625                locals: &mut body.locals,
626                statements: Vec::new(),
627                span: self.item_meta.span,
628                params: &self.generics,
629            };
630            body.body.visit_blocks_bwd(|block: &mut llbc_ast::Block| {
631                ctx.statements = Vec::with_capacity(block.statements.len());
632                for mut st in mem::take(&mut block.statements) {
633                    ctx.span = st.span;
634                    f(&mut ctx, &mut st);
635                    ctx.statements.push(st);
636                }
637                block.statements = mem::take(&mut ctx.statements)
638            })
639        }
640    }
641}