neo_devpack_solidity/frontend/frontend_ir.rs
1/// Natspec documentation extracted from source comments.
2#[derive(Debug, Clone, Default)]
3pub struct NatspecDocIR {
4 /// @title - Contract title
5 pub title: Option<String>,
6 /// @author - Author information
7 pub author: Option<String>,
8 /// @notice - User-facing description
9 pub notice: Option<String>,
10 /// @dev - Developer-facing notes
11 pub dev: Option<String>,
12 /// @param name description
13 pub params: Vec<(String, String)>,
14 /// @return descriptions
15 pub returns: Vec<String>,
16 /// @custom:tag value pairs
17 pub custom: Vec<(String, String)>,
18}
19
20/// Representation of a Solidity contract.
21#[derive(Debug, Clone)]
22pub struct ContractIR {
23 pub name: String,
24 pub kind: ContractKind,
25 /// Inheritance specifiers (`contract X is A, B(...) { ... }`).
26 pub bases: Vec<Base>,
27 pub functions: Vec<FunctionIR>,
28 pub events: Vec<EventIR>,
29 /// Declared custom `error` definitions (contract-level plus file-level
30 /// merged in by `parse_source`). Used to resolve EVM custom-error
31 /// selectors from the DECLARED parameter types rather than the types
32 /// inferred from `revert`-site argument expressions.
33 pub errors: Vec<ErrorIR>,
34 pub state_variables: Vec<StateVariableIR>,
35 pub structs: Vec<StructIR>,
36 pub enums: Vec<EnumIR>,
37 /// Natspec documentation for this contract
38 pub doc: NatspecDocIR,
39 /// Whether this contract contains `using X for Y` directives.
40 ///
41 /// The compiler merges library functions into the contract wholesale,
42 /// so basic `using LibName for Type` works implicitly. This flag is
43 /// set when advanced forms (`using X for *`, `using { f, g } for Y`)
44 /// are present so that diagnostics can be emitted.
45 pub has_using_for_star: bool,
46 pub has_using_function_list: bool,
47 /// Library names referenced by `using X for Y` directives.
48 ///
49 /// The compiler merges all non-builtin library functions into the contract
50 /// wholesale, so `using LibName for Type` member-call syntax (e.g. `x.add(y)`)
51 /// resolves to `LibName.add(x, y)` automatically. This list is kept for
52 /// diagnostic purposes.
53 pub using_for_libraries: Vec<String>,
54 /// Parsed `using` directives with enough structure for type-aware lowering.
55 pub using_directives: Vec<UsingDirectiveIR>,
56 /// Whether this contract contains `type X is Y` definitions.
57 pub has_type_definitions: bool,
58 /// User-defined value type aliases (`type X is Y`).
59 ///
60 /// Maps the user-defined type name to its underlying Solidity type string.
61 /// During type resolution, `X` is transparently replaced by `Y`.
62 /// `X.wrap(v)` and `X.unwrap(v)` compile to no-ops.
63 pub type_aliases: std::collections::HashMap<String, String>,
64 /// Mapping from original method name to the renamed super-method name.
65 ///
66 /// When inheritance flattening detects an override, the base version of the
67 /// function is preserved as `__super_{methodName}` and this map records the
68 /// relationship so that `super.method()` can be resolved during IR lowering.
69 pub super_method_map: std::collections::HashMap<String, String>,
70}
71
72/// Parsed `using` directive (`using <list> for <type | *>`).
73#[derive(Debug, Clone)]
74pub struct UsingDirectiveIR {
75 /// `None` means wildcard target (`for *`), otherwise normalized target type.
76 pub target_type: Option<String>,
77 /// Function-name allowlist for `using {f, g} for T`.
78 ///
79 /// `None` means library-form directive (`using Lib for T`) where all compatible
80 /// library functions are available.
81 pub function_names: Option<Vec<String>>,
82}
83
84/// Classification of contract kinds.
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
86pub enum ContractKind {
87 Contract,
88 AbstractContract,
89 Interface,
90 Library,
91}
92
93/// Representation of a Solidity function or constructor.
94#[derive(Debug, Clone)]
95pub struct FunctionIR {
96 pub name: String,
97 pub ty: FunctionTy,
98 pub parameters: Vec<ParameterIR>,
99 pub returns: Vec<ParameterIR>,
100 pub mutability: MutabilityKind,
101 pub visibility: VisibilityKind,
102 /// Whether this function is marked `virtual`.
103 pub is_virtual: bool,
104 /// Whether this function is marked `override`.
105 pub is_override: bool,
106 /// Modifier applications and constructor base invocations.
107 pub base_or_modifiers: Vec<Base>,
108 pub body: Option<Statement>,
109 /// Natspec documentation for this function
110 pub doc: NatspecDocIR,
111 /// Task #114 — set during modifier expansion when at least one applied
112 /// modifier has an epilogue (statements after the `_;` placeholder).
113 /// Signals the IR lowerer to redirect `return expr;` inside the expanded
114 /// body to synthetic return slots + a jump past the epilogue, so
115 /// modifier tail statements like `locked = 0;` still run before the
116 /// function returns.
117 pub had_modifier_epilogue: bool,
118}
119
120/// Function mutability classification based on Solidity state mutability.
121#[derive(Debug, Clone, Copy, PartialEq, Eq)]
122pub enum MutabilityKind {
123 Pure,
124 View,
125 Payable,
126 NonPayable,
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq)]
130pub enum VisibilityKind {
131 External,
132 Public,
133 Internal,
134 Private,
135}
136
137/// Representation of a Solidity parameter.
138#[derive(Debug, Clone)]
139pub struct ParameterIR {
140 pub name: Option<String>,
141 pub ty: String,
142 pub storage: Option<String>,
143}
144
145/// Representation of a Solidity event.
146#[derive(Debug, Clone)]
147pub struct EventIR {
148 pub name: String,
149 pub parameters: Vec<EventParameterIR>,
150 /// `true` when the event was declared with the `anonymous` keyword.
151 /// Anonymous events suppress the `keccak256(signature)` topic0 slot so
152 /// they can carry up to 4 indexed topics (vs. 3 for non-anonymous).
153 pub anonymous: bool,
154}
155
156/// Representation of a Solidity event parameter.
157#[derive(Debug, Clone)]
158pub struct EventParameterIR {
159 pub name: Option<String>,
160 pub ty: String,
161 pub indexed: bool,
162}
163
164/// Representation of a Solidity custom `error` declaration.
165#[derive(Debug, Clone)]
166pub struct ErrorIR {
167 pub name: String,
168 /// Declared parameters in declaration order (`storage` is always `None`
169 /// — error parameters cannot carry a data location).
170 pub parameters: Vec<ParameterIR>,
171}
172
173/// Representation of a state variable.
174#[derive(Debug, Clone)]
175pub struct StateVariableIR {
176 pub name: Option<String>,
177 pub ty: String,
178 pub is_constant: bool,
179 pub is_immutable: bool,
180 pub visibility: Option<String>,
181 pub has_initializer: bool,
182 pub initializer: Option<Expression>,
183}
184
185#[derive(Debug, Clone)]
186pub struct StructIR {
187 pub name: String,
188 pub fields: Vec<StructFieldIR>,
189}
190
191#[derive(Debug, Clone)]
192pub struct StructFieldIR {
193 pub name: String,
194 pub ty: String,
195}
196
197#[derive(Debug, Clone)]
198pub struct EnumIR {
199 pub name: String,
200 pub values: Vec<String>,
201}