Skip to content

Commit

Permalink
fix: better compatibility with older AST
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Jul 26, 2024
1 parent ea34637 commit 4f8abd5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
25 changes: 17 additions & 8 deletions crates/artifacts/solc/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
mod macros;
mod misc;
pub use misc::*;
pub mod util;
pub mod utils;
pub mod visitor;

/// A low fidelity representation of the AST.
Expand Down Expand Up @@ -173,7 +173,7 @@ ast_node!(
contract_dependencies: Vec<usize>,
#[serde(rename = "contractKind")]
kind: ContractKind,
documentation: Option<StructuredDocumentation>,
documentation: Option<Documentation>,
fully_implemented: bool,
linearized_base_contracts: Vec<usize>,
nodes: Vec<ContractDefinitionPart>,
Expand Down Expand Up @@ -526,7 +526,7 @@ ast_node!(
/// [`VariableDeclaration::mutability()`].
#[serde(default)]
state_variable: bool,
documentation: Option<StructuredDocumentation>,
documentation: Option<Documentation>,
function_selector: Option<String>, // TODO
#[serde(default)]
indexed: bool,
Expand Down Expand Up @@ -568,6 +568,13 @@ ast_node!(
}
);

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Documentation {
Structured(StructuredDocumentation),
Raw(String),
}

ast_node!(
/// An override specifier.
struct OverrideSpecifier {
Expand Down Expand Up @@ -670,7 +677,7 @@ ast_node!(
name: String,
#[serde(default, with = "serde_helpers::display_from_str_opt")]
name_location: Option<SourceLocation>,
documentation: Option<StructuredDocumentation>,
documentation: Option<Documentation>,
error_selector: Option<String>, // TODO
parameters: ParameterList,
}
Expand All @@ -684,7 +691,7 @@ ast_node!(
name_location: Option<SourceLocation>,
anonymous: bool,
event_selector: Option<String>, // TODO
documentation: Option<StructuredDocumentation>,
documentation: Option<Documentation>,
parameters: ParameterList,
}
);
Expand All @@ -698,7 +705,7 @@ ast_node!(
#[serde(default, deserialize_with = "serde_helpers::default_for_null")]
base_functions: Vec<usize>,
body: Option<Block>,
documentation: Option<StructuredDocumentation>,
documentation: Option<Documentation>,
function_selector: Option<String>, // TODO
implemented: bool,
modifiers: Vec<ModifierInvocation>,
Expand Down Expand Up @@ -865,9 +872,11 @@ ast_node!(
struct InlineAssembly {
documentation: Option<String>,
#[serde(rename = "AST")]
ast: YulBlock,
ast: Option<YulBlock>,
operations: Option<String>,
// TODO: We need this camel case for the AST, but pascal case other places in ethers-solc
//evm_version: EvmVersion,
#[serde(deserialize_with = "utils::deserialize_external_assembly_references")]
external_references: Vec<ExternalInlineAssemblyReference>,
#[serde(default, deserialize_with = "serde_helpers::default_for_null")]
flags: Vec<InlineAssemblyFlag>,
Expand Down Expand Up @@ -1001,7 +1010,7 @@ ast_node!(
#[serde(default, deserialize_with = "serde_helpers::default_for_null")]
base_modifiers: Vec<usize>,
body: Option<Block>,
documentation: Option<StructuredDocumentation>,
documentation: Option<Documentation>,
overrides: Option<OverrideSpecifier>,
parameters: ParameterList,
#[serde(default, rename = "virtual")]
Expand Down
1 change: 0 additions & 1 deletion crates/artifacts/solc/src/ast/util.rs

This file was deleted.

26 changes: 26 additions & 0 deletions crates/artifacts/solc/src/ast/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::ExternalInlineAssemblyReference;
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::BTreeMap;

pub fn deserialize_external_assembly_references<'de, D>(
deserializer: D,
) -> Result<Vec<ExternalInlineAssemblyReference>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum ExternalReferencesHelper {
Plain(Vec<ExternalInlineAssemblyReference>),
/// Older solc versions produce external referrences as arrays of mappings {"variable" =>
/// external reference object}, so we have to handle this.
Map(Vec<BTreeMap<String, ExternalInlineAssemblyReference>>),
}

ExternalReferencesHelper::deserialize(deserializer).map(|v| match v {
ExternalReferencesHelper::Plain(vec) => vec,
ExternalReferencesHelper::Map(vec) => {
vec.into_iter().flat_map(|v| v.into_values()).collect()
}
})
}
3 changes: 3 additions & 0 deletions crates/compilers/src/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ impl Flattener {
});

docs.for_each(|doc| {
let Documentation::Structured(doc) = doc else {
return
};
let src_start = doc.src.start.unwrap();
let src_end = src_start + doc.src.length.unwrap();

Expand Down

0 comments on commit 4f8abd5

Please sign in to comment.