Skip to content

Commit

Permalink
fix inherits
Browse files Browse the repository at this point in the history
  • Loading branch information
brockelmore committed Jul 23, 2024
1 parent 2c0a834 commit 71be9a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions crates/graph/src/nodes/context/typing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ impl ContextNode {
.underlying(analyzer)?
.inherits
.iter()
.filter_map(|i| i.as_ref())
.any(|inherited| *inherited == fn_ctrt))
} else {
Ok(false)
Expand Down
36 changes: 23 additions & 13 deletions crates/graph/src/nodes/contract_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl ContractNode {
self.underlying_mut(analyzer)
.unwrap()
.inherits
.push(ContractNode::from(*found));
.push(Some(ContractNode::from(*found)));
analyzer.add_edge(*found, *self, Edge::InheritedContract);
});
self.order_inherits(analyzer);
Expand All @@ -119,17 +119,25 @@ impl ContractNode {
let inherits = self.underlying(analyzer).unwrap().inherits.clone();

let mut tmp_inherits = vec![];
tmp_inherits.resize(inherits.len(), ContractNode::from(NodeIdx::from(0)));
tmp_inherits.resize(inherits.len(), None);
inherits.into_iter().for_each(|inherited| {
let i_name = inherited.name(analyzer).unwrap();
let position = raw_inherits.iter().position(|raw| &i_name == raw).unwrap();
tmp_inherits[position] = inherited;
if let Some(inherited) = inherited {
let i_name = inherited.name(analyzer).unwrap();
let position = raw_inherits.iter().position(|raw| &i_name == raw).unwrap();
tmp_inherits[position] = Some(inherited);
}
});
self.underlying_mut(analyzer).unwrap().inherits = tmp_inherits;
}

pub fn direct_inherited_contracts(&self, analyzer: &impl GraphBackend) -> Vec<ContractNode> {
self.underlying(analyzer).unwrap().inherits.clone()
self.underlying(analyzer)
.unwrap()
.inherits
.iter()
.filter_map(|i| i.as_ref())
.cloned()
.collect()
}

pub fn all_inherited_contracts(&self, analyzer: &impl GraphBackend) -> Vec<ContractNode> {
Expand Down Expand Up @@ -384,7 +392,7 @@ pub struct Contract {
/// Raw inherited strings, ordered by least base to most base
pub raw_inherits: Vec<String>,
/// A list of contracts that this contract inherits (TODO: inheritance linearization)
pub inherits: Vec<ContractNode>,
pub inherits: Vec<Option<ContractNode>>,
/// Cached linearized functions
pub cached_functions: Option<BTreeMap<String, FunctionNode>>,
}
Expand Down Expand Up @@ -416,7 +424,7 @@ impl Contract {
{
let name = ContractNode::from(contract).name(analyzer).unwrap();
if &name == inherited_name {
inherits.push(ContractNode::from(contract));
inherits.push(Some(ContractNode::from(contract)));
found = true;
break;
}
Expand All @@ -430,7 +438,7 @@ impl Contract {
{
let name = ContractNode::from(contract).name(analyzer).unwrap();
if &name == inherited_name {
inherits.push(ContractNode::from(contract));
inherits.push(Some(ContractNode::from(contract)));
found = true;
break;
}
Expand Down Expand Up @@ -462,11 +470,13 @@ impl Contract {
let inherits = self.inherits.clone();

let mut tmp_inherits = vec![];
tmp_inherits.resize(inherits.len(), ContractNode::from(NodeIdx::from(0)));
tmp_inherits.resize(raw_inherits.len(), None);
inherits.into_iter().for_each(|inherited| {
let i_name = inherited.name(analyzer).unwrap();
let position = raw_inherits.iter().position(|raw| &i_name == raw).unwrap();
tmp_inherits[position] = inherited;
if let Some(inherited) = inherited {
let i_name = inherited.name(analyzer).unwrap();
let position = raw_inherits.iter().position(|raw| &i_name == raw).unwrap();
tmp_inherits[position] = Some(inherited);
}
});
self.inherits = tmp_inherits;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/pyrometer/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,8 +1049,8 @@ impl Analyzer {
node.into()
};

inherits.iter().for_each(|contract_node| {
self.add_edge(*contract_node, con_node, Edge::InheritedContract);
inherits.into_iter().flatten().for_each(|contract_node| {
self.add_edge(contract_node, con_node, Edge::InheritedContract);
});

let mut usings = vec![];
Expand Down

0 comments on commit 71be9a7

Please sign in to comment.