Skip to content

Commit

Permalink
Cleanup a bit and explore crates starting with the root module
Browse files Browse the repository at this point in the history
  • Loading branch information
sonmarcho committed Nov 21, 2023
1 parent 829d33e commit 0d96b46
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 35 deletions.
4 changes: 2 additions & 2 deletions charon-ml/src/PrintGAst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ let gfun_decl_to_string (env : ('a, 'b) fmt_env) (indent : string)
in

(* Put everything together *)
fun_sig_with_name_to_string env indent indent_incr (Some "opaque")
(Some name) (Some inputs) sg
fun_sig_with_name_to_string env indent indent_incr None (Some name)
(Some inputs) sg
^ indent ^ "\n{\n" ^ locals ^ "\n\n" ^ body ^ "\n" ^ indent ^ "}"

let trait_decl_to_string (env : ('a, 'b) fmt_env) (indent : string)
Expand Down
10 changes: 5 additions & 5 deletions charon/src/assumed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub fn is_marker_trait(name: &Name) -> bool {
false
}

pub fn get_type_id_from_name(name: &TypeName) -> Option<AssumedTy> {
pub fn get_type_id_from_name(name: &Name) -> Option<AssumedTy> {
if name.equals_ref_name(&BOX_NAME) {
Option::Some(AssumedTy::Box)
} else if name.equals_ref_name(&PTR_UNIQUE_NAME) {
Expand All @@ -103,7 +103,7 @@ pub fn get_name_from_type_id(id: AssumedTy) -> Vec<String> {
}
}

fn get_fun_id_from_name_full(name: &FunName) -> Option<FunId> {
fn get_fun_id_from_name_full(name: &Name) -> Option<FunId> {
if name.equals_ref_name(&PANIC_NAME) {
Option::Some(FunId::Panic)
} else if name.equals_ref_name(&BEGIN_PANIC_NAME) {
Expand Down Expand Up @@ -148,7 +148,7 @@ fn get_fun_id_from_name_full(name: &FunName) -> Option<FunId> {
}
}

pub fn get_fun_id_from_name(name: &FunName) -> Option<ullbc_ast::AssumedFunId> {
pub fn get_fun_id_from_name(name: &Name) -> Option<ullbc_ast::AssumedFunId> {
match get_fun_id_from_name_full(name) {
Option::Some(id) => {
let id = match id {
Expand All @@ -166,7 +166,7 @@ pub fn get_fun_id_from_name(name: &FunName) -> Option<ullbc_ast::AssumedFunId> {
/// assumed types.
/// For instance, many types like box or vec are parameterized (in MIR) by an allocator
/// (`std::alloc::Allocator`): we ignore it.
pub fn type_to_used_params(name: &TypeName) -> Option<Vec<bool>> {
pub fn type_to_used_params(name: &Name) -> Option<Vec<bool>> {
trace!("{:?}", name);
match get_type_id_from_name(name) {
Option::None => Option::None,
Expand Down Expand Up @@ -195,7 +195,7 @@ pub struct FunInfo {
}

/// See the comments for [type_to_used_params]
pub fn function_to_info(name: &FunName) -> Option<FunInfo> {
pub fn function_to_info(name: &Name) -> Option<FunInfo> {
trace!("{:?}", name);
match get_fun_id_from_name_full(name) {
Option::None => Option::None,
Expand Down
7 changes: 3 additions & 4 deletions charon/src/gast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
pub use crate::expressions::*;
pub use crate::gast_utils::*;
use crate::meta::Meta;
use crate::names::GlobalName;
use crate::names::{FunName, Name};
use crate::names::Name;
pub use crate::types::GlobalDeclId;
pub use crate::types::TraitClauseId;
use crate::types::*;
Expand Down Expand Up @@ -95,7 +94,7 @@ pub struct GFunDecl<T> {
pub def_id: FunDeclId::Id,
/// The meta data associated with the declaration.
pub meta: Meta,
pub name: FunName,
pub name: Name,
/// The signature contains the inputs/output types *with* non-erased regions.
/// It also contains the list of region and type parameters.
pub signature: FunSig,
Expand All @@ -113,7 +112,7 @@ pub struct GGlobalDecl<T> {
pub def_id: GlobalDeclId::Id,
/// The meta data associated with the declaration.
pub meta: Meta,
pub name: GlobalName,
pub name: Name,
pub ty: Ty,
pub body: Option<GExprBody<T>>,
}
Expand Down
8 changes: 0 additions & 8 deletions charon/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,3 @@ pub struct ImplElem {
pub struct Name {
pub name: Vec<PathElem>,
}

// TODO: remove those
pub type ModuleName = Name;
pub type TypeName = Name;
pub type ItemName = Name;
pub type FunName = Name;
pub type GlobalName = Name;
pub type HirItemName = Name;
8 changes: 4 additions & 4 deletions charon/src/names_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Serialize for Name {

impl<'tcx, 'ctx> TransCtx<'tcx, 'ctx> {
/// Retrieve an item name from a [DefId].
pub fn extended_def_id_to_name(&mut self, def_id: &hax::ExtendedDefId) -> ItemName {
pub fn extended_def_id_to_name(&mut self, def_id: &hax::ExtendedDefId) -> Name {
trace!("{:?}", def_id);

// We have to be a bit careful when retrieving names from def ids. For instance,
Expand Down Expand Up @@ -395,7 +395,7 @@ impl<'tcx, 'ctx> TransCtx<'tcx, 'ctx> {
///
/// Rk.: this function is only used by [crate::register], and implemented with this
/// context in mind.
pub fn hir_item_to_name(&mut self, item: &Item) -> Option<HirItemName> {
pub fn hir_item_to_name(&mut self, item: &Item) -> Option<Name> {
// We have to create a hax state, which is annoying...
let state = self.make_hax_state_with_id(item.owner_id.to_def_id());
let def_id = item.owner_id.to_def_id().sinto(&state);
Expand Down Expand Up @@ -429,12 +429,12 @@ impl<'tcx, 'ctx> TransCtx<'tcx, 'ctx> {
}

// TODO: remove
pub fn item_def_id_to_name(&mut self, def_id: rustc_span::def_id::DefId) -> ItemName {
pub fn item_def_id_to_name(&mut self, def_id: rustc_span::def_id::DefId) -> Name {
let state = self.make_hax_state_with_id(def_id);
self.extended_def_id_to_name(&def_id.sinto(&state))
}

pub fn def_id_to_name(&mut self, def_id: &hax::DefId) -> ItemName {
pub fn def_id_to_name(&mut self, def_id: &hax::DefId) -> Name {
// We have to create a hax state, which is annoying...
let state = self.make_hax_state_with_id(def_id.rust_def_id.unwrap());
self.extended_def_id_to_name(&def_id.rust_def_id.unwrap().sinto(&state))
Expand Down
23 changes: 17 additions & 6 deletions charon/src/translate_crate_to_ullbc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,19 @@ impl<'tcx, 'ctx> TransCtx<'tcx, 'ctx> {
match self.hir_item_to_name(item) {
Option::None => {
// This kind of item is to be ignored
trace!("Ignoring {:?} (ignoring item kind)", item.item_id());
return;
}
Option::Some(item_name) => {
if self.crate_info.is_opaque_decl(&item_name) {
trace!("Ignoring {:?} (marked as opaque)", item.item_id());
return;
}
// Continue
}
}
}
trace!("Registering {:?}", item.item_id());

// Case disjunction on the item kind.
let def_id = item.owner_id.to_def_id();
Expand Down Expand Up @@ -239,13 +242,21 @@ pub fn translate<'tcx, 'ctx>(

// First push all the items in the stack of items to translate.
//
// The way rustc works is as follows:
// - we call it on the root of the crate (for instance "main.rs"), and it
// explores all the files from there (typically listed through statements
// of the form "mod MODULE_NAME")
// - the other files in the crate are Module items in the HIR graph
// We explore the crate by starting with the root module.
//
// Remark: It is important to do like this (and not iterate over all the items)
// if we want the "opaque" options (to ignore parts of the crate) to work.
// For instance, if we mark "foo::bar" as opaque, we will ignore the module
// "foo::bar" altogether (we will not even look at the items).
// If we look at the items, we risk registering items just by looking
// at their name. For instance, if we check the item `foo::bar::{foo::bar::Ty}::f`,
// then by converting the Rust name to an LLBC name, we will actually register
// the name "foo::bar::Ty" (so that we can generate the "impl" path element
// `{foo::bar::Ty}`), which means we will register the item `foo::bar::Ty`.
// We could make the name translation work differently if we do have to
// explore all the items in the crate.
let hir = tcx.hir();
for item_id in hir.items() {
for item_id in hir.root_module().item_ids {
let item_id = item_id.hir_id();
let node = hir.find(item_id).unwrap();
let item = match node {
Expand Down
4 changes: 1 addition & 3 deletions charon/src/translate_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,7 @@ impl<'tcx, 'ctx> TransCtx<'tcx, 'ctx> {
{
// Retrieve the id of the implemented trait decl
let id = self.tcx.trait_id_of_impl(rust_id).unwrap();
if self.register_trait_decl_id(id).is_none() {
return None;
}
let _ = self.register_trait_decl_id(id)?;
}

match self.trait_impl_id_map.get(&rust_id) {
Expand Down
3 changes: 3 additions & 0 deletions charon/src/translate_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,9 @@ impl<'tcx, 'ctx> TransCtx<'tcx, 'ctx> {
// If yes, ignore it, otherwise, dive into the body. Note that for
// external types we have to check the body: if the body is
// public, we translate it, otherwise we consider the type as opaque.
// For instance, because `core::option::Option` is public, we can
// manipulate its variants. If we encounter this type, we must retrieve
// its definition.
let is_local = rust_id.is_local();
let kind = if !is_transparent {
TypeDeclKind::Opaque
Expand Down
4 changes: 2 additions & 2 deletions charon/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pub use crate::gast::TraitItemName;
use crate::meta::Meta;
use crate::names::TypeName;
use crate::names::Name;
pub use crate::types_utils::*;
use crate::values::Literal;
use derivative::Derivative;
Expand Down Expand Up @@ -314,7 +314,7 @@ pub struct TypeDecl {
/// [true] if the type decl is a local type decl, [false] if is comes from
/// an external crate.
pub is_local: bool,
pub name: TypeName,
pub name: Name,
pub generics: GenericParams,
pub preds: Predicates,
/// The type kind: enum, struct, or opaque.
Expand Down
2 changes: 1 addition & 1 deletion tests-polonius/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CURRENT_DIR = $(shell pwd)
CHARON ?= $(CURRENT_DIR)/../bin/charon
DEST ?= .
OPTIONS = --polonius
OPTIONS += --polonius
NOT_ALL_TESTS ?=

.PHONY: all
Expand Down

0 comments on commit 0d96b46

Please sign in to comment.