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`](driver),
6//! and in particular [`driver::CharonCallbacks`](driver::CharonCallbacks),
7//! which implements the callback which we provide to Rustc.
8//!
9//! The ASTs are in [`ullbc_ast`](ullbc_ast) (Unstructured LLBC - basically
10//! a cleaned-up version of MIR) and [`llbc_ast`](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#![feature(assert_matches)]
17#![feature(box_patterns)]
18#![feature(deref_pure_trait)]
19#![feature(if_let_guard)]
20#![feature(impl_trait_in_assoc_type)]
21#![feature(iterator_try_collect)]
22#![feature(let_chains)]
23#![feature(trait_alias)]
24#![feature(register_tool)]
25// For when we use charon on itself :3
26#![register_tool(charon)]
27
28#[macro_use]
29pub mod ids;
30#[macro_use]
31pub mod logger;
32pub mod ast;
33pub mod common;
34pub mod errors;
35pub mod export;
36pub mod name_matcher;
37pub mod options;
38pub mod pretty;
39pub mod transform;
40
41// Re-export all the ast modules so we can keep the old import structure.
42pub use ast::{builtins, expressions, gast, llbc_ast, meta, names, types, ullbc_ast, values};
43pub use pretty::formatter;
44pub use transform::{graphs, reorder_decls, ullbc_to_llbc};
45
46/// The version of the crate, as defined in `Cargo.toml`.
47const VERSION: &str = env!("CARGO_PKG_VERSION");
48
49/// Read a `.llbc` file.
50pub fn deserialize_llbc(path: &std::path::Path) -> anyhow::Result<ast::TranslatedCrate> {
51    use crate::export::CrateData;
52    use anyhow::Context;
53    use serde::Deserialize;
54    use std::fs::File;
55    use std::io::BufReader;
56    let file = File::open(&path)
57        .with_context(|| format!("Failed to read llbc file {}", path.display()))?;
58    let reader = BufReader::new(file);
59    let mut deserializer = serde_json::Deserializer::from_reader(reader);
60    // Deserialize without recursion limit.
61    deserializer.disable_recursion_limit();
62    // Grow stack space as needed.
63    let deserializer = serde_stacker::Deserializer::new(&mut deserializer);
64    Ok(CrateData::deserialize(deserializer)?.translated)
65}