|
| 1 | +// Copyright © Aptos Foundation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use move_model::{ |
| 5 | + ast::{Exp, Spec, SpecBlockTarget}, |
| 6 | + model::{FunId, GlobalEnv, NodeId, QualifiedId, SpecFunId}, |
| 7 | +}; |
| 8 | +use std::{ |
| 9 | + collections::{BTreeMap, BTreeSet}, |
| 10 | + mem, |
| 11 | +}; |
| 12 | + |
| 13 | +/// Represents a target for rewriting. |
| 14 | +#[derive(Debug, PartialOrd, Ord, PartialEq, Eq, Clone)] |
| 15 | +pub enum RewriteTarget { |
| 16 | + MoveFun(QualifiedId<FunId>), |
| 17 | + SpecFun(QualifiedId<SpecFunId>), |
| 18 | + SpecBlock(SpecBlockTarget), |
| 19 | +} |
| 20 | + |
| 21 | +/// Represents the state of a rewriting target. |
| 22 | +#[derive(Debug, Clone, Eq, PartialEq)] |
| 23 | +pub enum RewriteState { |
| 24 | + Unchanged, |
| 25 | + Def(Exp), |
| 26 | + Spec(Spec), |
| 27 | + Abstract, |
| 28 | +} |
| 29 | + |
| 30 | +/// Scope for collecting targets. |
| 31 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] |
| 32 | +pub enum RewritingScope { |
| 33 | + CompilationTarget, |
| 34 | + Everything, |
| 35 | +} |
| 36 | + |
| 37 | +/// Represents a set of rewriting targets in the given state. |
| 38 | +#[derive(Clone)] |
| 39 | +pub struct RewriteTargets { |
| 40 | + pub targets: BTreeMap<RewriteTarget, RewriteState>, |
| 41 | +} |
| 42 | + |
| 43 | +impl RewriteTargets { |
| 44 | + /// Create a new set of rewrite targets, collecting them as specified by `scope`. |
| 45 | + /// Those targets are initially associated with `Unchanged` state. |
| 46 | + pub fn create(env: &GlobalEnv, scope: RewritingScope) -> Self { |
| 47 | + let mut targets = vec![]; |
| 48 | + let add_spec = |
| 49 | + |targets: &mut Vec<RewriteTarget>, sb_target: SpecBlockTarget, spec: &Spec| { |
| 50 | + if !spec.is_empty() { |
| 51 | + targets.push(RewriteTarget::SpecBlock(sb_target)) |
| 52 | + } |
| 53 | + }; |
| 54 | + for module in env.get_modules() { |
| 55 | + if scope == RewritingScope::Everything || module.is_target() { |
| 56 | + for func in module.get_functions() { |
| 57 | + let id = func.get_qualified_id(); |
| 58 | + targets.push(RewriteTarget::MoveFun(id)); |
| 59 | + add_spec( |
| 60 | + &mut targets, |
| 61 | + SpecBlockTarget::Function(id.module_id, id.id), |
| 62 | + &func.get_spec(), |
| 63 | + ); |
| 64 | + } |
| 65 | + for (spec_fun_id, _) in module.get_spec_funs() { |
| 66 | + targets.push(RewriteTarget::SpecFun( |
| 67 | + module.get_id().qualified(*spec_fun_id), |
| 68 | + )); |
| 69 | + } |
| 70 | + for struct_env in module.get_structs() { |
| 71 | + add_spec( |
| 72 | + &mut targets, |
| 73 | + SpecBlockTarget::Struct(module.get_id(), struct_env.get_id()), |
| 74 | + &struct_env.get_spec(), |
| 75 | + ) |
| 76 | + } |
| 77 | + if !module.get_spec().is_empty() { |
| 78 | + add_spec( |
| 79 | + &mut targets, |
| 80 | + SpecBlockTarget::Module(module.get_id()), |
| 81 | + &module.get_spec(), |
| 82 | + ); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + Self { |
| 87 | + targets: targets |
| 88 | + .into_iter() |
| 89 | + .map(|target| (target, RewriteState::Unchanged)) |
| 90 | + .collect(), |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /// Filters the targets according to the predicate. |
| 95 | + pub fn filter(&mut self, pred: impl Fn(&RewriteTarget, &RewriteState) -> bool) { |
| 96 | + self.targets = mem::take(&mut self.targets) |
| 97 | + .into_iter() |
| 98 | + .filter(|(t, s)| pred(t, s)) |
| 99 | + .collect(); |
| 100 | + } |
| 101 | + |
| 102 | + /// Iterates all targets. |
| 103 | + pub fn iter(&self) -> impl Iterator<Item = (&RewriteTarget, &RewriteState)> + '_ { |
| 104 | + self.targets.iter() |
| 105 | + } |
| 106 | + |
| 107 | + /// Returns an iteration of the target keys. |
| 108 | + pub fn keys(&self) -> impl Iterator<Item = RewriteTarget> + '_ { |
| 109 | + self.targets.keys().cloned() |
| 110 | + } |
| 111 | + |
| 112 | + /// Adds a new rewrite target in state `Unchanged` if it doesn't exist yet. Returns |
| 113 | + /// a boolean whether the entry is new and a mutable reference to the state. |
| 114 | + pub fn entry(&mut self, target: RewriteTarget) -> (bool, &mut RewriteState) { |
| 115 | + let mut is_new = false; |
| 116 | + let state = self.targets.entry(target).or_insert_with(|| { |
| 117 | + is_new = true; |
| 118 | + RewriteState::Unchanged |
| 119 | + }); |
| 120 | + (is_new, state) |
| 121 | + } |
| 122 | + |
| 123 | + /// Gets the current state of the target. |
| 124 | + pub fn state(&self, target: &RewriteTarget) -> &RewriteState { |
| 125 | + self.targets.get(target).expect("state defined") |
| 126 | + } |
| 127 | + |
| 128 | + /// Gets the mutable current state of the target. |
| 129 | + pub fn state_mut(&mut self, target: &RewriteTarget) -> &mut RewriteState { |
| 130 | + self.targets.get_mut(target).expect("state defined") |
| 131 | + } |
| 132 | + |
| 133 | + /// Updates the global env based on the current state. This consumes |
| 134 | + /// the rewrite targets. |
| 135 | + pub fn write_to_env(self, env: &mut GlobalEnv) { |
| 136 | + for (target, state) in self.targets { |
| 137 | + use RewriteState::*; |
| 138 | + use RewriteTarget::*; |
| 139 | + match (target, state) { |
| 140 | + (_, Unchanged) => {}, |
| 141 | + (MoveFun(fnid), Def(def)) => env.set_function_def(fnid, def), |
| 142 | + (SpecFun(fnid), Def(def)) => env.get_spec_fun_mut(fnid).body = Some(def), |
| 143 | + (SpecBlock(sb_target), Spec(spec)) => { |
| 144 | + *env.get_spec_block_mut(&sb_target) = spec; |
| 145 | + }, |
| 146 | + _ => panic!("unexpected rewrite target and result combination"), |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +impl RewriteTarget { |
| 153 | + /// Gets the call sites for the target. |
| 154 | + pub fn called_funs_with_call_sites( |
| 155 | + &self, |
| 156 | + env: &GlobalEnv, |
| 157 | + ) -> BTreeMap<QualifiedId<FunId>, BTreeSet<NodeId>> { |
| 158 | + use RewriteTarget::*; |
| 159 | + match self { |
| 160 | + MoveFun(id) => env |
| 161 | + .get_function(*id) |
| 162 | + .get_def() |
| 163 | + .map(|e| e.called_funs_with_callsites()) |
| 164 | + .unwrap_or_default(), |
| 165 | + SpecFun(id) => env |
| 166 | + .get_spec_fun(*id) |
| 167 | + .body |
| 168 | + .as_ref() |
| 169 | + .map(|e| e.called_funs_with_callsites()) |
| 170 | + .unwrap_or_default(), |
| 171 | + SpecBlock(target) => { |
| 172 | + let spec = env.get_spec_block(target); |
| 173 | + spec.called_funs_with_callsites() |
| 174 | + }, |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + /// Get the environment state of this target in form of a RewriteState. |
| 179 | + pub fn get_env_state(&self, env: &GlobalEnv) -> RewriteState { |
| 180 | + use RewriteState::*; |
| 181 | + use RewriteTarget::*; |
| 182 | + match self { |
| 183 | + MoveFun(fid) => env |
| 184 | + .get_function(*fid) |
| 185 | + .get_def() |
| 186 | + .map(|e| Def(e.clone())) |
| 187 | + .unwrap_or(Abstract), |
| 188 | + SpecFun(fid) => env |
| 189 | + .get_spec_fun(*fid) |
| 190 | + .body |
| 191 | + .clone() |
| 192 | + .map(Def) |
| 193 | + .unwrap_or(Abstract), |
| 194 | + SpecBlock(sb_target) => Spec(env.get_spec_block(sb_target).clone()), |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments