-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract mutable references (without drop/reassignment)
- Loading branch information
1 parent
39a242f
commit 1ed66d6
Showing
10 changed files
with
189 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use super::*; | ||
|
||
mod mut_ref; | ||
mod std_option; | ||
|
||
/// This is just a proxy, a mutable reference to the base extractor. The idea of | ||
/// `Synth` is just to scope some methods under a common namespace, therefore | ||
/// the struct mainly provides a namespace/scope. | ||
pub struct Synth<'a, 'l, 'tcx> { | ||
base: &'a mut BaseExtractor<'l, 'tcx>, | ||
} | ||
|
||
impl<'l, 'tcx> BaseExtractor<'l, 'tcx> { | ||
/// Returns a `Synth` which provides access to all methods that synthesise | ||
/// items/trees. | ||
pub fn synth<'a>(&'a mut self) -> Synth<'a, 'l, 'tcx> { | ||
Synth { base: self } | ||
} | ||
} | ||
|
||
impl<'b, 'l, 'tcx> BodyExtractor<'b, 'l, 'tcx> { | ||
/// Returns a `Synth` which provides access to all methods that synthesise | ||
/// items/trees. | ||
pub fn synth<'a>(&'a mut self) -> Synth<'a, 'l, 'tcx> { | ||
self.base.synth() | ||
} | ||
} | ||
|
||
impl<'l> Synth<'_, 'l, '_> { | ||
fn factory(&self) -> &'l st::Factory { | ||
self.base.factory() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use super::*; | ||
use stainless_data::ast::ADTSort; | ||
|
||
impl<'a, 'l, 'tcx> Synth<'a, 'l, 'tcx> { | ||
pub fn mut_ref(&mut self, tpe: st::Type<'l>, arg: st::Expr<'l>) -> st::Expr<'l> { | ||
self | ||
.factory() | ||
.ADT(self.mut_ref_id(), vec![tpe], vec![arg]) | ||
.into() | ||
} | ||
|
||
pub fn mut_ref_type(&mut self, tpe: st::Type<'l>) -> st::Type<'l> { | ||
self.factory().ADTType(self.mut_ref_id(), vec![tpe]).into() | ||
} | ||
|
||
pub fn mut_ref_value_id(&mut self) -> StainlessSymId<'l> { | ||
self.get_or_create_adt().constructors[0].fields[0].v.id | ||
} | ||
|
||
fn mut_ref_id(&mut self) -> StainlessSymId<'l> { | ||
self.get_or_create_adt().id | ||
} | ||
|
||
fn get_or_create_adt(&mut self) -> &'l ADTSort<'l> { | ||
self | ||
.base | ||
.with_extraction(|xt| xt.mut_ref_id.and_then(|id| xt.adts.get(id).copied())) | ||
.unwrap_or_else(|| { | ||
let f = self.factory(); | ||
|
||
let adt_id = self.base.fresh_id("MutRef".into()); | ||
self | ||
.base | ||
.with_extraction_mut(|xt| xt.mut_ref_id = Some(adt_id)); | ||
|
||
let field_id = self.base.fresh_id("t".into()); | ||
let tparam = &*f.TypeParameter(self.base.fresh_id("T".into()), vec![f.IsMutable().into()]); | ||
|
||
let adt = f.ADTSort( | ||
adt_id, | ||
vec![f.TypeParameterDef(tparam)], | ||
vec![f.ADTConstructor( | ||
adt_id, | ||
adt_id, | ||
vec![f.ValDef(f.Variable(field_id, tparam.into(), vec![f.IsVar().into()]))], | ||
)], | ||
vec![f.Synthetic().into()], | ||
); | ||
self.base.add_adt(adt); | ||
adt | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use super::*; | ||
|
||
/// Synthetisation of std::option::Option trees in Stainless AST. | ||
impl<'a, 'l, 'tcx> Synth<'a, 'l, 'tcx> { | ||
pub fn std_option_type(&mut self, tpe: st::Type<'l>) -> st::Type<'l> { | ||
self | ||
.factory() | ||
.ADTType(self.option_adt().id, vec![tpe]) | ||
.into() | ||
} | ||
|
||
pub fn std_option_none(&mut self, tpe: st::Type<'l>) -> st::Expr<'l> { | ||
self.factory().ADT(self.none_id(), vec![tpe], vec![]).into() | ||
} | ||
|
||
pub fn std_option_some(&mut self, val: st::Expr<'l>, tpe: st::Type<'l>) -> st::Expr<'l> { | ||
self | ||
.factory() | ||
.ADT(self.some_id(), vec![tpe], vec![val]) | ||
.into() | ||
} | ||
|
||
pub fn std_option_some_type(&mut self, tpe: st::Type<'l>) -> st::Type<'l> { | ||
self.factory().ADTType(self.some_id(), vec![tpe]).into() | ||
} | ||
|
||
pub fn std_option_some_value(&mut self, some: st::Expr<'l>) -> st::Expr<'l> { | ||
self | ||
.factory() | ||
.ClassSelector(some, self.some_value_id()) | ||
.into() | ||
} | ||
|
||
fn option_adt(&mut self) -> &'l st::ADTSort<'l> { | ||
let def_id = self | ||
.base | ||
.std_items | ||
.item_to_def(StdItem::CrateItem(CrateItem::OptionType)); | ||
self.base.get_or_extract_adt(def_id) | ||
} | ||
|
||
fn none_id(&mut self) -> StainlessSymId<'l> { | ||
self.option_adt().constructors[0].id | ||
} | ||
|
||
fn some_id(&mut self) -> StainlessSymId<'l> { | ||
self.option_adt().constructors[1].id | ||
} | ||
|
||
fn some_value_id(&mut self) -> StainlessSymId<'l> { | ||
self.option_adt().constructors[1].fields[0].v.id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters