1use annotate_snippets::Level;
3use clap::ValueEnum;
4use indoc::indoc;
5use itertools::Itertools;
6use macros::EnumAsGetters;
7use serde::{Deserialize, Serialize};
8use std::path::PathBuf;
9
10use crate::{
11 ast::*,
12 errors::{ErrorCtx, display_unspanned_error},
13 name_matcher::NamePattern,
14 raise_error, register_error,
15};
16
17pub const CHARON_ARGS: &str = "CHARON_ARGS";
20
21#[derive(Debug, Default, Clone, clap::Args, PartialEq, Eq, Serialize, Deserialize)]
30#[clap(name = "Charon")]
31#[cfg_attr(feature = "charon_on_charon", charon::rename("cli_options"))]
32pub struct CliOpts {
33 #[clap(long)]
35 #[serde(default)]
36 pub ullbc: bool,
37 #[clap(long)]
43 #[serde(default)]
44 pub precise_drops: bool,
45 #[clap(long)]
47 #[serde(default)]
48 pub skip_borrowck: bool,
49 #[arg(long)]
52 pub mir: Option<MirLevel>,
53 #[clap(long = "rustc-arg")]
55 #[serde(default)]
56 pub rustc_args: Vec<String>,
57 #[clap(long, value_delimiter = ',')]
62 #[serde(default)]
63 pub targets: Vec<String>,
64 #[clap(long)]
69 #[serde(default)]
70 pub sysroot: Option<String>,
71
72 #[clap(long, visible_alias = "mono")]
76 #[serde(default)]
77 pub monomorphize: bool,
78 #[clap(
82 long,
83 value_name("INCLUDE_TYPES"),
84 num_args(0..=1),
85 require_equals(true),
86 default_missing_value("all"),
87 )]
88 #[serde(default)]
89 pub monomorphize_mut: Option<MonomorphizeMut>,
90
91 #[clap(long, value_delimiter = ',')]
95 #[serde(default)]
96 pub start_from: Vec<String>,
97 #[clap(long, value_delimiter = ',')]
100 #[serde(default)]
101 pub start_from_if_exists: Vec<String>,
102 #[clap(
106 long,
107 value_name("ATTRIBUTE"),
108 num_args(0..),
109 require_equals(true),
110 value_delimiter = ',',
111 default_missing_value("verify::start_from"),
112 )]
113 #[serde(default)]
114 pub start_from_attribute: Vec<String>,
115 #[clap(long)]
117 #[serde(default)]
118 pub start_from_pub: bool,
119
120 #[clap(
122 long,
123 help = indoc!("
124 Whitelist of items to translate. These use the name-matcher syntax (note: this differs
125 a bit from the ocaml NameMatcher).
126
127 Note: This is very rough at the moment. E.g. this parses `u64` as a path instead of the
128 built-in type. It is also not possible to filter a trait impl (this will only filter
129 its methods). Please report bugs or missing features.
130
131 Examples:
132 - `crate::module1::module2::item`: refers to this item and all its subitems (e.g.
133 submodules or trait methods);
134 - `crate::module1::module2::item::_`: refers only to the subitems of this item;
135 - `core::convert::{impl core::convert::Into<_> for _}`: retrieve the body of this
136 very useful impl;
137
138 When multiple patterns in the `--include` and `--opaque` options match the same item,
139 the most precise pattern wins. E.g.: `charon --opaque crate::module --include
140 crate::module::_` makes the `module` opaque (we won't explore its contents), but the
141 items in it transparent (we will translate them if we encounter them.)
142 "))]
143 #[serde(default)]
144 #[cfg_attr(feature = "charon_on_charon", charon::rename("included"))]
145 pub include: Vec<String>,
146 #[clap(long)]
148 #[serde(default)]
149 pub opaque: Vec<String>,
150 #[clap(long)]
152 #[serde(default)]
153 pub exclude: Vec<String>,
154 #[clap(long)]
157 #[serde(default)]
158 pub extract_opaque_bodies: bool,
159 #[clap(long)]
162 #[serde(default)]
163 pub translate_all_methods: bool,
164 #[clap(long)]
168 #[serde(default)]
169 pub duplicate_defaulted_methods: bool,
170
171 #[clap(long, alias = "remove-associated-types")]
174 #[serde(default)]
175 pub lift_associated_types: Vec<String>,
176 #[clap(long)]
179 #[serde(default)]
180 pub hide_marker_traits: bool,
181 #[clap(long)]
183 #[serde(default)]
184 pub hide_allocator: bool,
185
186 #[clap(long)]
190 #[serde(default)]
191 pub remove_unused_clauses: bool,
192 #[clap(long)]
197 #[serde(default)]
198 pub remove_unused_self_clauses: bool,
199 #[clap(long)]
202 #[serde(default)]
203 pub remove_adt_clauses: bool,
204
205 #[clap(long)]
207 #[serde(default)]
208 pub desugar_drops: bool,
209 #[clap(long)]
212 #[serde(default)]
213 pub ops_to_function_calls: bool,
214 #[clap(long)]
218 #[serde(default)]
219 pub index_to_function_calls: bool,
220 #[clap(long)]
222 #[serde(default)]
223 pub treat_box_as_builtin: bool,
224 #[clap(long)]
226 #[serde(default)]
227 pub raw_consts: bool,
228 #[clap(long)]
233 #[serde(default)]
234 pub consts: Option<ConstHandling>,
235 #[clap(long)]
238 #[serde(default)]
239 pub unsized_strings: bool,
240 #[clap(long)]
243 #[serde(default)]
244 pub reconstruct_fallible_operations: bool,
245 #[clap(long)]
247 #[serde(default)]
248 pub reconstruct_asserts: bool,
249 #[clap(long)]
253 #[serde(default)]
254 pub unbind_item_vars: bool,
255
256 #[clap(long)]
258 #[serde(default)]
259 pub print_original_ullbc: bool,
260 #[clap(long)]
262 #[serde(default)]
263 pub print_ullbc: bool,
264 #[clap(long)]
266 #[serde(default)]
267 pub print_built_llbc: bool,
268 #[clap(long)]
270 #[serde(default)]
271 pub print_llbc: bool,
272 #[clap(long = "dest", value_parser)]
276 #[serde(default)]
277 pub dest_dir: Option<PathBuf>,
278 #[clap(long, value_parser)]
282 #[serde(default)]
283 pub dest_file: Option<PathBuf>,
284 #[clap(long)]
286 #[serde(default)]
287 pub no_dedup_serialized_ast: bool,
288 #[clap(long, value_enum)]
290 #[serde(default)]
291 pub format: Option<SerializationFormatArg>,
292 #[clap(long)]
294 #[serde(default)]
295 pub no_serialize: bool,
296 #[clap(long)]
298 #[serde(default)]
299 pub no_typecheck: bool,
300 #[clap(long)]
302 #[serde(default)]
303 pub no_normalize: bool,
304 #[clap(long)]
306 #[serde(default)]
307 pub abort_on_error: bool,
308 #[clap(long)]
310 #[serde(default)]
311 pub error_on_warnings: bool,
312
313 #[clap(long)]
315 #[arg(value_enum)]
316 pub preset: Option<Preset>,
317}
318
319#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize)]
322pub enum MirLevel {
323 Built,
325 Promoted,
327 Elaborated,
330 Optimized,
333}
334
335#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize)]
338#[non_exhaustive]
339pub enum Preset {
340 OldDefaults,
343 RawMir,
346 Fast,
348 Aeneas,
349 Eurydice,
350 Soteria,
351 Tests,
352}
353
354#[derive(
356 Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize,
357)]
358pub enum ConstHandling {
359 #[default]
362 Initializers,
363 Values,
366}
367
368#[derive(
369 Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize,
370)]
371pub enum MonomorphizeMut {
372 #[default]
374 All,
375 ExceptTypes,
377}
378
379#[derive(Debug, Copy, Clone, PartialEq, Eq, ValueEnum, Serialize, Deserialize)]
380pub enum SerializationFormatArg {
381 Json,
382 Postcard,
383 #[cfg_attr(feature = "charon_on_charon", charon::rename("AllFormats"))]
384 All,
385}
386
387#[derive(
388 Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize,
389)]
390pub enum SerializationFormat {
391 #[default]
392 Json,
393 Postcard,
394}
395
396impl SerializationFormatArg {
397 pub fn as_format(self) -> Option<SerializationFormat> {
398 match self {
399 SerializationFormatArg::Json => Some(SerializationFormat::Json),
400 SerializationFormatArg::Postcard => Some(SerializationFormat::Postcard),
401 SerializationFormatArg::All => None,
402 }
403 }
404}
405
406impl From<SerializationFormat> for SerializationFormatArg {
407 fn from(format: SerializationFormat) -> SerializationFormatArg {
408 match format {
409 SerializationFormat::Json => SerializationFormatArg::Json,
410 SerializationFormat::Postcard => SerializationFormatArg::Postcard,
411 }
412 }
413}
414
415impl SerializationFormat {
416 pub fn output_extension(self, ullbc: bool) -> &'static str {
417 match (ullbc, self) {
418 (true, SerializationFormat::Json) => "ullbc",
419 (false, SerializationFormat::Json) => "llbc",
420 (true, SerializationFormat::Postcard) => "ullbc.postcard",
421 (false, SerializationFormat::Postcard) => "llbc.postcard",
422 }
423 }
424}
425
426impl CliOpts {
427 pub fn apply_preset(&mut self) {
428 if let Some(preset) = self.preset {
429 match preset {
430 Preset::OldDefaults => {
431 self.treat_box_as_builtin = true;
432 self.hide_allocator = true;
433 self.ops_to_function_calls = true;
434 self.index_to_function_calls = true;
435 self.reconstruct_fallible_operations = true;
436 self.reconstruct_asserts = true;
437 self.unbind_item_vars = true;
438 self.duplicate_defaulted_methods = true;
439 }
440 Preset::RawMir => {
441 self.extract_opaque_bodies = true;
442 self.raw_consts = true;
443 self.ullbc = true;
444 }
445 Preset::Fast => {
446 self.ullbc = true;
447 self.no_typecheck = true;
448 self.no_normalize = true;
449 self.hide_marker_traits = true;
450 self.raw_consts = true;
451 }
452 Preset::Aeneas => {
453 self.lift_associated_types.push("*".to_owned());
454 self.treat_box_as_builtin = true;
455 self.ops_to_function_calls = true;
456 self.index_to_function_calls = true;
457 self.reconstruct_fallible_operations = true;
458 self.reconstruct_asserts = true;
459 self.hide_marker_traits = true;
460 self.hide_allocator = true;
461 self.remove_unused_self_clauses = true;
462 self.remove_adt_clauses = true;
463 self.unbind_item_vars = true;
464 self.duplicate_defaulted_methods = true;
465 }
466 Preset::Eurydice => {
467 self.hide_allocator = true;
468 self.treat_box_as_builtin = true;
469 self.ops_to_function_calls = true;
470 self.index_to_function_calls = true;
471 self.reconstruct_fallible_operations = true;
472 self.reconstruct_asserts = true;
473 self.lift_associated_types.push("*".to_owned());
474 self.unbind_item_vars = true;
475 self.duplicate_defaulted_methods = true;
476 self.include.push("core::marker::MetaSized".to_owned());
478 }
479 Preset::Soteria => {
480 self.desugar_drops = true;
481 self.extract_opaque_bodies = true;
482 self.mir = Some(MirLevel::Elaborated);
483 self.monomorphize = true;
484 self.no_normalize = true;
485 self.precise_drops = true;
486 self.consts = Some(ConstHandling::Values);
487 self.ullbc = true;
488 }
489 Preset::Tests => {
490 self.no_dedup_serialized_ast = true; self.treat_box_as_builtin = true;
492 self.hide_allocator = true;
493 self.reconstruct_fallible_operations = true;
494 self.reconstruct_asserts = true;
495 self.ops_to_function_calls = true;
496 self.index_to_function_calls = true;
497 self.duplicate_defaulted_methods = true;
498 self.rustc_args.push("--edition=2021".to_owned());
499 self.rustc_args
500 .push("-Zcrate-attr=feature(register_tool)".to_owned());
501 self.rustc_args
502 .push("-Zcrate-attr=register_tool(charon)".to_owned());
503 self.exclude.push("core::fmt".to_owned());
504 }
505 }
506 }
507 }
508
509 pub fn validate(&self) -> anyhow::Result<()> {
511 if self.dest_dir.is_some() {
512 display_unspanned_error(
513 Level::WARNING,
514 "`--dest` is deprecated, use `--dest-file` instead",
515 )
516 }
517
518 if self.remove_adt_clauses && self.lift_associated_types.is_empty() {
519 anyhow::bail!(
520 "`--remove-adt-clauses` should be used with `--lift-associated-types='*'` \
521 to avoid missing clause errors",
522 )
523 }
524 if matches!(self.monomorphize_mut, Some(MonomorphizeMut::ExceptTypes))
525 && !self.remove_adt_clauses
526 {
527 anyhow::bail!(
528 "`--monomorphize-mut=except-types` should be used with `--remove-adt-clauses` \
529 to avoid generics mismatches"
530 )
531 }
532 if self.no_serialize && self.format.is_some() {
533 anyhow::bail!(
534 "`--no-serialize` is not compatible with `--format`, the format is only relevant if we serialize"
535 );
536 }
537 Ok(())
538 }
539
540 fn target_filename(
541 &self,
542 path_base: PathBuf,
543 format: SerializationFormat,
544 ) -> (PathBuf, SerializationFormat) {
545 let extension = format.output_extension(self.ullbc);
546 let target_filename = path_base.with_added_extension(extension);
547 (target_filename, format)
548 }
549
550 pub fn targets(&self, crate_name: &str) -> Vec<(PathBuf, SerializationFormat)> {
551 if self.no_serialize {
552 return vec![];
553 }
554
555 let format = self.format.unwrap_or(SerializationFormatArg::Json);
556 let mut path_base = self.dest_dir.clone().unwrap_or_default();
557 path_base.push(crate_name);
558
559 match format.as_format() {
560 Some(format) => match self.dest_file.clone() {
561 Some(dest) => vec![(dest, format)],
562 None => vec![self.target_filename(path_base, format)],
563 },
564 None => {
565 let path_base = self.dest_file.clone().unwrap_or(path_base);
566 vec![
567 self.target_filename(path_base.clone(), SerializationFormat::Json),
568 self.target_filename(path_base, SerializationFormat::Postcard),
569 ]
570 }
571 }
572 }
573}
574
575#[derive(Debug, Clone, EnumAsGetters)]
577pub enum StartFrom {
578 Pattern { pattern: NamePattern, strict: bool },
581 Attribute(String),
583 Pub,
586}
587
588impl StartFrom {
589 pub fn matches(&self, ctx: &TranslatedCrate, item_meta: &ItemMeta) -> bool {
590 match self {
591 StartFrom::Pattern { pattern, .. } => pattern.matches(ctx, &item_meta.name),
592 StartFrom::Attribute(attr) => item_meta
593 .attr_info
594 .attributes
595 .iter()
596 .filter_map(|a| a.as_unknown())
597 .any(|raw_attr| raw_attr.path == *attr),
598 StartFrom::Pub => item_meta.attr_info.public && item_meta.is_local,
599 }
600 }
601}
602
603pub struct TranslateOptions {
605 pub start_from: Vec<StartFrom>,
607 pub mir_level: MirLevel,
609 pub translate_all_methods: bool,
612 pub duplicate_defaulted_methods: bool,
614 pub monomorphize_mut: Option<MonomorphizeMut>,
617 pub hide_marker_traits: bool,
620 pub hide_allocator: bool,
622 pub hide_traits: Vec<NamePattern>,
625 pub remove_unused_clauses: bool,
629 pub remove_unused_self_clauses: bool,
631 pub remove_adt_clauses: bool,
633 pub monomorphize_with_hax: bool,
635 pub ops_to_function_calls: bool,
638 pub index_to_function_calls: bool,
640 pub print_built_llbc: bool,
642 pub treat_box_as_builtin: bool,
644 pub raw_consts: bool,
646 pub consts: ConstHandling,
649 pub unsized_strings: bool,
652 pub reconstruct_fallible_operations: bool,
655 pub reconstruct_asserts: bool,
657 pub unbind_item_vars: bool,
659 pub item_opacities: Vec<(NamePattern, ItemOpacity)>,
662 pub lift_associated_types: Vec<NamePattern>,
664 pub no_typecheck: bool,
666 pub no_normalize: bool,
668 pub desugar_drops: bool,
670 pub add_destruct_bounds: bool,
672}
673
674impl TranslateOptions {
675 pub fn new(error_ctx: &mut ErrorCtx, options: &CliOpts) -> Self {
676 let mut parse_pattern = |s: &str| -> Result<_, Error> {
677 match NamePattern::parse(s) {
678 Ok(p) => Ok(p),
679 Err(e) => raise_error!(error_ctx, no_crate, "failed to parse pattern `{s}` ({e})"),
680 }
681 };
682
683 let mut mir_level = options.mir.unwrap_or(MirLevel::Promoted);
684 if options.precise_drops {
685 mir_level = std::cmp::max(mir_level, MirLevel::Elaborated);
686 }
687
688 let mut start_from = options
689 .start_from
690 .iter()
691 .filter_map(|path| parse_pattern(path).ok())
692 .map(|p| StartFrom::Pattern {
693 pattern: p,
694 strict: true,
695 })
696 .collect_vec();
697 start_from.extend(
698 options
699 .start_from_if_exists
700 .iter()
701 .filter_map(|path| parse_pattern(path).ok())
702 .map(|p| StartFrom::Pattern {
703 pattern: p,
704 strict: false,
705 }),
706 );
707 for attr in options.start_from_attribute.iter().cloned() {
708 start_from.push(StartFrom::Attribute(attr));
709 }
710 if options.start_from_pub {
711 start_from.push(StartFrom::Pub);
712 }
713 if start_from.is_empty() {
714 start_from.push(StartFrom::Pattern {
715 pattern: parse_pattern("crate").unwrap(),
716 strict: true,
717 });
718 }
719
720 let hide_traits = options
721 .hide_marker_traits
722 .then_some([
723 "core::marker::Sized",
724 "core::marker::MetaSized",
725 "core::marker::PointeeSized",
726 "core::marker::Tuple",
727 "core::clone::TrivialClone",
728 ])
729 .into_iter()
730 .flatten()
731 .chain(options.hide_allocator.then_some("core::alloc::Allocator"))
732 .filter_map(|s| parse_pattern(s).ok())
733 .collect_vec();
734
735 let item_opacities = {
736 use ItemOpacity::*;
737 let mut opacities = vec![];
738
739 if options.extract_opaque_bodies {
741 opacities.push(("_".to_string(), Transparent));
742 } else {
743 opacities.push(("_".to_string(), Foreign));
744 }
745
746 if options.treat_box_as_builtin {
747 opacities.push((
749 "alloc::boxed::box_assume_init_into_vec_unsafe".to_string(),
750 Transparent,
751 ));
752 }
753
754 opacities.push(("crate".to_owned(), Transparent));
756
757 for pat in options.include.iter() {
758 opacities.push((pat.to_string(), Transparent));
759 }
760 for pat in options.opaque.iter() {
761 opacities.push((pat.to_string(), Opaque));
762 }
763 for pat in options.exclude.iter() {
764 opacities.push((pat.to_string(), Invisible));
765 }
766
767 for trait_name in &hide_traits {
768 opacities.push((trait_name.to_string(), Invisible));
769 }
770
771 let hide_traits = hide_traits
773 .iter()
774 .cloned()
775 .flat_map(|pat| [pat.clone(), NamePattern::impl_for(pat)])
776 .map(|pat| (pat, Invisible));
777 opacities
778 .into_iter()
779 .filter_map(|(s, opacity)| parse_pattern(&s).ok().map(|pat| (pat, opacity)))
780 .chain(hide_traits)
781 .collect()
782 };
783
784 let lift_associated_types = options
785 .lift_associated_types
786 .iter()
787 .filter_map(|s| parse_pattern(s).ok())
788 .collect();
789
790 TranslateOptions {
791 start_from,
792 mir_level,
793 monomorphize_mut: options.monomorphize_mut,
794 hide_marker_traits: options.hide_marker_traits,
795 hide_allocator: options.hide_allocator,
796 hide_traits,
797 remove_unused_clauses: options.remove_unused_clauses,
798 remove_unused_self_clauses: options.remove_unused_self_clauses,
799 remove_adt_clauses: options.remove_adt_clauses,
800 monomorphize_with_hax: options.monomorphize,
801 ops_to_function_calls: options.ops_to_function_calls,
802 index_to_function_calls: options.index_to_function_calls,
803 print_built_llbc: options.print_built_llbc,
804 item_opacities,
805 treat_box_as_builtin: options.treat_box_as_builtin,
806 raw_consts: options.raw_consts,
807 consts: options.consts.unwrap_or_default(),
808 unsized_strings: options.unsized_strings,
809 reconstruct_fallible_operations: options.reconstruct_fallible_operations,
810 reconstruct_asserts: options.reconstruct_asserts,
811 lift_associated_types,
812 unbind_item_vars: options.unbind_item_vars,
813 translate_all_methods: options.translate_all_methods,
814 duplicate_defaulted_methods: options.duplicate_defaulted_methods,
815 no_typecheck: options.no_typecheck,
816 no_normalize: options.no_normalize,
817 desugar_drops: options.desugar_drops,
818 add_destruct_bounds: options.precise_drops,
819 }
820 }
821
822 #[tracing::instrument(skip(self, krate), ret)]
825 pub fn opacity_for_name(&self, krate: &TranslatedCrate, name: &Name) -> ItemOpacity {
826 let (_, opacity) = self
830 .item_opacities
831 .iter()
832 .filter(|(pat, _)| pat.matches(krate, name))
833 .max()
834 .unwrap();
835 *opacity
836 }
837}