Skip to content

Commit

Permalink
New flattening impl (#52)
Browse files Browse the repository at this point in the history
Ref foundry-rs/foundry#4034.

## Solution
solang-parser AST was not sufficient to rename stuff, because it doesn't
collect IDs of declarations and we can't match specific identifier to
the declaration. Native solc AST gives such ability, so I've implemented
native solc AST visitor.

The implementation of flattening itself consits of several steps:
1. Find all sources that are needed for target (imports of any depth).
2. Expore ASTs for all sources and find all top-level declarations
(contracts, events, structs, etc).
3. Find if any top-level declarations names are same
4. If any duplicates are found, figure out new names for them (e.g. 2
`OracleLike` interfaces become `OracleLike_0` and `OracleLike_1`)
5. Walk AST and find all references to top-level declarations. Replace
every reference with declaration name. We should update names even for
unchanged declarations to deal with aliased imports.
6. Collect and remove all import directives.
7. Collect and remove all pragmas and license identifiers.

This flattener implementation can be a full replacer of the current impl
for all cases when source being flattened can be compiled via solc.
  • Loading branch information
klkvr authored Jan 29, 2024
1 parent dcd4ebf commit 6768886
Show file tree
Hide file tree
Showing 6 changed files with 1,730 additions and 176 deletions.
3 changes: 2 additions & 1 deletion src/artifacts/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ ast_node!(
#[serde(default, deserialize_with = "serde_helpers::default_for_null")]
used_events: Vec<usize>,
#[serde(default, rename = "internalFunctionIDs")]
internal_function_ids: BTreeMap<usize, usize>,
internal_function_ids: BTreeMap<String, usize>,
}
);

Expand Down Expand Up @@ -908,6 +908,7 @@ pub enum AssemblyReferenceSuffix {
/// Inline assembly flags.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum InlineAssemblyFlag {
#[serde(rename = "memory-safe")]
MemorySafe,
}

Expand Down
Loading

0 comments on commit 6768886

Please sign in to comment.