revert: Ast Artifact (#1966)
This commit is contained in:
parent
d553111906
commit
04e0092adf
|
@ -16,9 +16,9 @@ use crate::{
|
|||
BytecodeOutputSelection, ContractOutputSelection, EvmOutputSelection,
|
||||
EwasmOutputSelection,
|
||||
},
|
||||
CompactContractBytecodeCow, DevDoc, Evm, Ewasm, FunctionDebugData, GasEstimates,
|
||||
GeneratedSource, LosslessAbi, LosslessMetadata, Metadata, Offsets, Settings, SourceUnit,
|
||||
StorageLayout, UserDoc,
|
||||
Ast, CompactContractBytecodeCow, DevDoc, Evm, Ewasm, FunctionDebugData, GasEstimates,
|
||||
GeneratedSource, LosslessAbi, LosslessMetadata, Metadata, Offsets, Settings, StorageLayout,
|
||||
UserDoc,
|
||||
},
|
||||
sources::VersionedSourceFile,
|
||||
ArtifactOutput, SolcConfig, SolcError, SourceFile,
|
||||
|
@ -68,7 +68,7 @@ pub struct ConfigurableContractArtifact {
|
|||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub ewasm: Option<Ewasm>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub ast: Option<SourceUnit>,
|
||||
pub ast: Option<Ast>,
|
||||
/// The identifier of the source file
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<u32>,
|
||||
|
|
|
@ -0,0 +1,217 @@
|
|||
//! Bindings for solc's `ast` output field
|
||||
|
||||
use crate::artifacts::serde_helpers;
|
||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||
use std::{collections::BTreeMap, fmt, fmt::Write, str::FromStr};
|
||||
|
||||
/// Represents the AST field in the solc output
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Ast {
|
||||
#[serde(rename = "absolutePath")]
|
||||
pub absolute_path: String,
|
||||
pub id: usize,
|
||||
#[serde(default, rename = "exportedSymbols")]
|
||||
pub exported_symbols: BTreeMap<String, Vec<usize>>,
|
||||
#[serde(rename = "nodeType")]
|
||||
pub node_type: NodeType,
|
||||
#[serde(with = "serde_helpers::display_from_str")]
|
||||
pub src: SourceLocation,
|
||||
#[serde(default)]
|
||||
pub nodes: Vec<Node>,
|
||||
|
||||
/// Node attributes that were not deserialized.
|
||||
#[serde(flatten)]
|
||||
pub other: BTreeMap<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct Node {
|
||||
/// The node ID.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub id: Option<usize>,
|
||||
|
||||
/// The node type.
|
||||
#[serde(rename = "nodeType")]
|
||||
pub node_type: NodeType,
|
||||
|
||||
/// The location of the node in the source file.
|
||||
#[serde(with = "serde_helpers::display_from_str")]
|
||||
pub src: SourceLocation,
|
||||
|
||||
/// Child nodes for some node types.
|
||||
#[serde(default)]
|
||||
pub nodes: Vec<Node>,
|
||||
|
||||
/// Body node for some node types.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub body: Option<Box<Node>>,
|
||||
|
||||
/// Node attributes that were not deserialized.
|
||||
#[serde(flatten)]
|
||||
pub other: BTreeMap<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
/// Deserialize a serialized node attribute.
|
||||
pub fn attribute<D: DeserializeOwned>(&self, key: impl AsRef<str>) -> Option<D> {
|
||||
// TODO: Can we avoid this clone?
|
||||
self.other.get(key.as_ref()).and_then(|v| serde_json::from_value(v.clone()).ok())
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the source location of a node: `<start byte>:<length>:<source index>`.
|
||||
///
|
||||
/// The `length` and `index` can be -1 which is represented as `None`
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct SourceLocation {
|
||||
pub start: usize,
|
||||
pub length: Option<usize>,
|
||||
pub index: Option<usize>,
|
||||
}
|
||||
|
||||
impl FromStr for SourceLocation {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
let invalid_location = move || format!("{s} invalid source location");
|
||||
|
||||
let mut split = s.split(':');
|
||||
let start = split
|
||||
.next()
|
||||
.ok_or_else(invalid_location)?
|
||||
.parse::<usize>()
|
||||
.map_err(|_| invalid_location())?;
|
||||
let length = split
|
||||
.next()
|
||||
.ok_or_else(invalid_location)?
|
||||
.parse::<isize>()
|
||||
.map_err(|_| invalid_location())?;
|
||||
let index = split
|
||||
.next()
|
||||
.ok_or_else(invalid_location)?
|
||||
.parse::<isize>()
|
||||
.map_err(|_| invalid_location())?;
|
||||
|
||||
let length = if length < 0 { None } else { Some(length as usize) };
|
||||
let index = if index < 0 { None } else { Some(index as usize) };
|
||||
|
||||
Ok(Self { start, length, index })
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SourceLocation {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
self.start.fmt(f)?;
|
||||
f.write_char(':')?;
|
||||
if let Some(length) = self.length {
|
||||
length.fmt(f)?;
|
||||
} else {
|
||||
f.write_str("-1")?;
|
||||
}
|
||||
f.write_char(':')?;
|
||||
if let Some(index) = self.index {
|
||||
index.fmt(f)?;
|
||||
} else {
|
||||
f.write_str("-1")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum NodeType {
|
||||
// Expressions
|
||||
Assignment,
|
||||
BinaryOperation,
|
||||
Conditional,
|
||||
ElementaryTypeNameExpression,
|
||||
FunctionCall,
|
||||
FunctionCallOptions,
|
||||
Identifier,
|
||||
IndexAccess,
|
||||
IndexRangeAccess,
|
||||
Literal,
|
||||
MemberAccess,
|
||||
NewExpression,
|
||||
TupleExpression,
|
||||
UnaryOperation,
|
||||
|
||||
// Statements
|
||||
Block,
|
||||
Break,
|
||||
Continue,
|
||||
DoWhileStatement,
|
||||
EmitStatement,
|
||||
ExpressionStatement,
|
||||
ForStatement,
|
||||
IfStatement,
|
||||
InlineAssembly,
|
||||
PlaceholderStatement,
|
||||
Return,
|
||||
RevertStatement,
|
||||
TryStatement,
|
||||
UncheckedBlock,
|
||||
VariableDeclarationStatement,
|
||||
VariableDeclaration,
|
||||
WhileStatement,
|
||||
|
||||
// Yul statements
|
||||
YulAssignment,
|
||||
YulBlock,
|
||||
YulBreak,
|
||||
YulContinue,
|
||||
YulExpressionStatement,
|
||||
YulLeave,
|
||||
YulForLoop,
|
||||
YulFunctionDefinition,
|
||||
YulIf,
|
||||
YulSwitch,
|
||||
YulVariableDeclaration,
|
||||
|
||||
// Yul expressions
|
||||
YulFunctionCall,
|
||||
YulIdentifier,
|
||||
YulLiteral,
|
||||
|
||||
// Yul literals
|
||||
YulLiteralValue,
|
||||
YulHexValue,
|
||||
|
||||
// Definitions
|
||||
ContractDefinition,
|
||||
FunctionDefinition,
|
||||
EventDefinition,
|
||||
ErrorDefinition,
|
||||
ModifierDefinition,
|
||||
StructDefinition,
|
||||
EnumDefinition,
|
||||
UserDefinedValueTypeDefinition,
|
||||
|
||||
// Directives
|
||||
PragmaDirective,
|
||||
ImportDirective,
|
||||
UsingForDirective,
|
||||
|
||||
// Misc
|
||||
SourceUnit,
|
||||
InheritanceSpecifier,
|
||||
ElementaryTypeName,
|
||||
FunctionTypeName,
|
||||
ParameterList,
|
||||
TryCatchClause,
|
||||
ModifierInvocation,
|
||||
|
||||
/// An unknown AST node type.
|
||||
Other(String),
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn can_parse_ast() {
|
||||
let ast = include_str!("../../../test-data/ast/ast-erc4626.json");
|
||||
let _ast: Ast = serde_json::from_str(ast).unwrap();
|
||||
}
|
||||
}
|
|
@ -17,6 +17,10 @@ pub use misc::*;
|
|||
pub mod util;
|
||||
pub mod visitor;
|
||||
|
||||
/// A low fidelity representation of the AST.
|
||||
pub(crate) mod lowfidelity;
|
||||
pub use lowfidelity::Ast;
|
||||
|
||||
/// Types for the Yul AST.
|
||||
///
|
||||
/// The Yul AST is embedded into the Solidity AST for inline assembly blocks.
|
||||
|
|
|
@ -22,7 +22,10 @@ pub mod contract;
|
|||
pub mod output_selection;
|
||||
pub mod serde_helpers;
|
||||
use crate::{
|
||||
artifacts::output_selection::{ContractOutputSelection, OutputSelection},
|
||||
artifacts::{
|
||||
lowfidelity::NodeType,
|
||||
output_selection::{ContractOutputSelection, OutputSelection},
|
||||
},
|
||||
filter::FilteredSources,
|
||||
};
|
||||
pub use bytecode::*;
|
||||
|
@ -1835,7 +1838,7 @@ pub struct SecondarySourceLocation {
|
|||
pub struct SourceFile {
|
||||
pub id: u32,
|
||||
#[serde(default, with = "serde_helpers::empty_json_object_opt")]
|
||||
pub ast: Option<SourceUnit>,
|
||||
pub ast: Option<Ast>,
|
||||
}
|
||||
|
||||
// === impl SourceFile ===
|
||||
|
@ -1846,10 +1849,7 @@ impl SourceFile {
|
|||
pub fn contains_contract_definition(&self) -> bool {
|
||||
if let Some(ref ast) = self.ast {
|
||||
// contract definitions are only allowed at the source-unit level <https://docs.soliditylang.org/en/latest/grammar.html>
|
||||
return ast
|
||||
.nodes
|
||||
.iter()
|
||||
.any(|node| matches!(node, SourceUnitPart::ContractDefinition(_)))
|
||||
return ast.nodes.iter().any(|node| node.node_type == NodeType::ContractDefinition)
|
||||
// abstract contract, interfaces: ContractDefinition
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue