charon_lib/
lib.rs

1//! This library contains utilities to extract the MIR from a Rust project,
2//! by compiling it to an easy-to-use AST called LLBC (Low-Level Borrow Calculus).
3//! This AST is serialized into JSON files.
4//!
5//! A good entry point to explore the project is [`driver`](../charon_driver/index.html),
6//! and in particular [`driver::CharonCallbacks`](../charon_driver/driver/struct.CharonCallbacks.html),
7//! which implements the callback which we provide to Rustc.
8//!
9//! The ASTs are in [`ullbc_ast`] (Unstructured LLBC - basically
10//! a cleaned-up version of MIR) and [`llbc_ast`] (same as ULLBC, but
11//! we reconstructed the control-flow to have `if ... then ... else ...`,
12//! loops, etc. instead of `GOTO`s).
13
14// For rustdoc: prevents overflows
15#![recursion_limit = "256"]
16#![expect(incomplete_features)]
17#![feature(assert_matches)]
18#![feature(box_patterns)]
19#![feature(deref_patterns)]
20#![feature(deref_pure_trait)]
21#![feature(if_let_guard)]
22#![feature(impl_trait_in_assoc_type)]
23#![feature(iterator_try_collect)]
24#![feature(trait_alias)]
25#![feature(register_tool)]
26// For when we use charon on itself :3
27#![register_tool(charon)]
28
29#[macro_use]
30pub mod ids;
31#[macro_use]
32pub mod logger;
33pub mod ast;
34pub mod common;
35pub mod errors;
36pub mod export;
37pub mod name_matcher;
38pub mod options;
39pub mod pretty;
40pub mod transform;
41
42// Re-export all the ast modules so we can keep the old import structure.
43pub use ast::{builtins, expressions, gast, llbc_ast, meta, names, types, ullbc_ast, values};
44pub use pretty::formatter;
45pub use transform::{graphs, reorder_decls, ullbc_to_llbc};
46
47/// The version of the crate, as defined in `Cargo.toml`.
48pub const VERSION: &str = env!("CARGO_PKG_VERSION");
49
50/// Read a `.llbc` file.
51pub fn deserialize_llbc(path: &std::path::Path) -> anyhow::Result<ast::TranslatedCrate> {
52    use crate::export::CrateData;
53    use anyhow::Context;
54    use serde::Deserialize;
55    use std::fs::File;
56    use std::io::BufReader;
57    let file = File::open(&path)
58        .with_context(|| format!("Failed to read llbc file {}", path.display()))?;
59    let reader = BufReader::new(file);
60    let mut deserializer = serde_json::Deserializer::from_reader(reader);
61    // Deserialize without recursion limit.
62    deserializer.disable_recursion_limit();
63    // Grow stack space as needed.
64    let deserializer = serde_stacker::Deserializer::new(&mut deserializer);
65    Ok(CrateData::deserialize(deserializer)?.translated)
66}