1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use crate::{utils, Solc};
use semver::VersionReq;
use solang_parser::pt::{
    ContractPart, ContractTy, FunctionAttribute, FunctionDefinition, Import, Loc, SourceUnitPart,
    Visibility,
};
use std::{
    ops::Range,
    path::{Path, PathBuf},
};

/// Represents various information about a solidity file parsed via [solang_parser]
#[derive(Debug)]
#[allow(unused)]
pub struct SolData {
    pub license: Option<SolDataUnit<String>>,
    pub version: Option<SolDataUnit<String>>,
    pub experimental: Option<SolDataUnit<String>>,
    pub imports: Vec<SolDataUnit<SolImport>>,
    pub version_req: Option<VersionReq>,
    pub libraries: Vec<SolLibrary>,
    pub contracts: Vec<SolContract>,
}

impl SolData {
    #[allow(unused)]
    pub fn fmt_version<W: std::fmt::Write>(
        &self,
        f: &mut W,
    ) -> std::result::Result<(), std::fmt::Error> {
        if let Some(ref version) = self.version {
            write!(f, "({})", version.data)?;
        }
        Ok(())
    }

    /// Extracts the useful data from a solidity source
    ///
    /// This will attempt to parse the solidity AST and extract the imports and version pragma. If
    /// parsing fails, we'll fall back to extract that info via regex
    pub fn parse(content: &str, file: &Path) -> Self {
        let mut version = None;
        let mut experimental = None;
        let mut imports = Vec::<SolDataUnit<SolImport>>::new();
        let mut libraries = Vec::new();
        let mut contracts = Vec::new();

        match solang_parser::parse(content, 0) {
            Ok((units, _)) => {
                for unit in units.0 {
                    match unit {
                        SourceUnitPart::PragmaDirective(loc, pragma, value) => {
                            if pragma.name == "solidity" {
                                // we're only interested in the solidity version pragma
                                version = Some(SolDataUnit::from_loc(value.string.clone(), loc));
                            }

                            if pragma.name == "experimental" {
                                experimental =
                                    Some(SolDataUnit::from_loc(value.string.clone(), loc));
                            }
                        }
                        SourceUnitPart::ImportDirective(import) => {
                            let (import, ids, loc) = match import {
                                Import::Plain(s, l) => (s, vec![], l),
                                Import::GlobalSymbol(s, i, l) => (s, vec![(i, None)], l),
                                Import::Rename(s, i, l) => (s, i, l),
                            };
                            let sol_import = SolImport::new(PathBuf::from(import.string))
                                .set_aliases(
                                    ids.into_iter()
                                        .map(|(id, alias)| match alias {
                                            Some(al) => SolImportAlias::Contract(al.name, id.name),
                                            None => SolImportAlias::File(id.name),
                                        })
                                        .collect(),
                                );
                            imports.push(SolDataUnit::from_loc(sol_import, loc));
                        }
                        SourceUnitPart::ContractDefinition(def) => {
                            let functions = def
                                .parts
                                .into_iter()
                                .filter_map(|part| match part {
                                    ContractPart::FunctionDefinition(f) => Some(*f),
                                    _ => None,
                                })
                                .collect();
                            let name = def.name.name;
                            match def.ty {
                                ContractTy::Contract(_) => {
                                    contracts.push(SolContract { name, functions });
                                }
                                ContractTy::Library(_) => {
                                    libraries.push(SolLibrary { name, functions });
                                }
                                _ => {}
                            }
                        }
                        _ => {}
                    }
                }
            }
            Err(err) => {
                tracing::trace!(
                    "failed to parse \"{}\" ast: \"{:?}\". Falling back to regex to extract data",
                    file.display(),
                    err
                );
                version =
                    capture_outer_and_inner(content, &utils::RE_SOL_PRAGMA_VERSION, &["version"])
                        .first()
                        .map(|(cap, name)| SolDataUnit::new(name.as_str().to_owned(), cap.range()));
                imports = capture_imports(content);
            }
        };
        let license = content.lines().next().and_then(|line| {
            capture_outer_and_inner(line, &utils::RE_SOL_SDPX_LICENSE_IDENTIFIER, &["license"])
                .first()
                .map(|(cap, l)| SolDataUnit::new(l.as_str().to_owned(), cap.range()))
        });
        let version_req = version.as_ref().and_then(|v| Solc::version_req(v.data()).ok());

        Self { version_req, version, experimental, imports, license, libraries, contracts }
    }

    /// Returns `true` if the solidity file associated with this type contains a solidity library
    /// that won't be inlined
    pub fn has_link_references(&self) -> bool {
        self.libraries.iter().any(|lib| !lib.is_inlined())
    }
}

/// Minimal representation of a contract inside a solidity file
#[derive(Debug)]
pub struct SolContract {
    pub name: String,
    pub functions: Vec<FunctionDefinition>,
}

#[derive(Debug, Clone)]
pub struct SolImport {
    path: PathBuf,
    aliases: Vec<SolImportAlias>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SolImportAlias {
    File(String),
    Contract(String, String),
}

impl SolImport {
    pub fn new(path: PathBuf) -> Self {
        Self { path, aliases: vec![] }
    }

    pub fn path(&self) -> &PathBuf {
        &self.path
    }

    pub fn aliases(&self) -> &Vec<SolImportAlias> {
        &self.aliases
    }

    fn set_aliases(mut self, aliases: Vec<SolImportAlias>) -> Self {
        self.aliases = aliases;
        self
    }
}

/// Minimal representation of a contract inside a solidity file
#[derive(Debug)]
pub struct SolLibrary {
    pub name: String,
    pub functions: Vec<FunctionDefinition>,
}

impl SolLibrary {
    /// Returns `true` if all functions of this library will be inlined.
    ///
    /// This checks if all functions are either internal or private, because internal functions can
    /// only be accessed from within the current contract or contracts deriving from it. They cannot
    /// be accessed externally. Since they are not exposed to the outside through the contract’s
    /// ABI, they can take parameters of internal types like mappings or storage references.
    ///
    /// See also <https://docs.soliditylang.org/en/latest/contracts.html#libraries>
    pub fn is_inlined(&self) -> bool {
        for f in self.functions.iter() {
            for attr in f.attributes.iter() {
                if let FunctionAttribute::Visibility(vis) = attr {
                    match vis {
                        Visibility::External(_) | Visibility::Public(_) => return false,
                        _ => {}
                    }
                }
            }
        }
        true
    }
}

/// Represents an item in a solidity file with its location in the file
#[derive(Debug, Clone)]
pub struct SolDataUnit<T> {
    loc: Range<usize>,
    data: T,
}

/// Solidity Data Unit decorated with its location within the file
impl<T> SolDataUnit<T> {
    pub fn new(data: T, loc: Range<usize>) -> Self {
        Self { data, loc }
    }

    pub fn from_loc(data: T, loc: Loc) -> Self {
        Self {
            data,
            loc: match loc {
                Loc::File(_, start, end) => Range { start, end: end + 1 },
                _ => Range { start: 0, end: 0 },
            },
        }
    }

    /// Returns the underlying data for the unit
    pub fn data(&self) -> &T {
        &self.data
    }

    /// Returns the location of the given data unit
    pub fn loc(&self) -> Range<usize> {
        self.loc.clone()
    }

    /// Returns the location of the given data unit adjusted by an offset.
    /// Used to determine new position of the unit within the file after
    /// content manipulation.
    pub fn loc_by_offset(&self, offset: isize) -> Range<usize> {
        utils::range_by_offset(&self.loc, offset)
    }
}

/// Given the regex and the target string, find all occurrences
/// of named groups within the string. This method returns
/// the tuple of matches `(a, b)` where `a` is the match for the
/// entire regex and `b` is the match for the first named group.
///
/// NOTE: This method will return the match for the first named
/// group, so the order of passed named groups matters.
fn capture_outer_and_inner<'a>(
    content: &'a str,
    regex: &regex::Regex,
    names: &[&str],
) -> Vec<(regex::Match<'a>, regex::Match<'a>)> {
    regex
        .captures_iter(content)
        .filter_map(|cap| {
            let cap_match = names.iter().find_map(|name| cap.name(name));
            cap_match.and_then(|m| cap.get(0).map(|outer| (outer.to_owned(), m)))
        })
        .collect()
}
/// Capture the import statement information together with aliases
pub fn capture_imports(content: &str) -> Vec<SolDataUnit<SolImport>> {
    let mut imports = vec![];
    for cap in utils::RE_SOL_IMPORT.captures_iter(content) {
        if let Some(name_match) =
            vec!["p1", "p2", "p3", "p4"].iter().find_map(|name| cap.name(name))
        {
            let statement_match = cap.get(0).unwrap();
            let mut aliases = vec![];
            for alias_cap in utils::RE_SOL_IMPORT_ALIAS.captures_iter(statement_match.as_str()) {
                if let Some(alias) = alias_cap.name("alias") {
                    let alias = alias.as_str().to_owned();
                    let import_alias = match alias_cap.name("target") {
                        Some(target) => SolImportAlias::Contract(alias, target.as_str().to_owned()),
                        None => SolImportAlias::File(alias),
                    };
                    aliases.push(import_alias);
                }
            }
            let sol_import =
                SolImport::new(PathBuf::from(name_match.as_str())).set_aliases(aliases);
            imports.push(SolDataUnit::new(sol_import, statement_match.range()));
        }
    }
    imports
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_capture_curly_imports() {
        let content = r#"
import { T } from "../Test.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {DsTest} from "ds-test/test.sol";
"#;

        let captured_imports =
            capture_imports(content).into_iter().map(|s| s.data.path).collect::<Vec<_>>();

        let expected =
            utils::find_import_paths(content).map(|m| m.as_str().into()).collect::<Vec<PathBuf>>();

        assert_eq!(captured_imports, expected);

        assert_eq!(
            captured_imports,
            vec![
                PathBuf::from("../Test.sol"),
                "@openzeppelin/contracts/utils/ReentrancyGuard.sol".into(),
                "ds-test/test.sol".into(),
            ]
        );
    }

    #[test]
    fn cap_capture_aliases() {
        let content = r#"
import * as T from "./Test.sol";
import { DsTest as Test } from "ds-test/test.sol";
import "ds-test/test.sol" as Test;
import { FloatMath as Math, Math as FloatMath } from "./Math.sol";
"#;

        let caputred_imports =
            capture_imports(content).into_iter().map(|s| s.data.aliases).collect::<Vec<_>>();
        assert_eq!(
            caputred_imports,
            vec![
                vec![SolImportAlias::File("T".into())],
                vec![SolImportAlias::Contract("Test".into(), "DsTest".into())],
                vec![SolImportAlias::File("Test".into())],
                vec![
                    SolImportAlias::Contract("Math".into(), "FloatMath".into()),
                    SolImportAlias::Contract("FloatMath".into(), "Math".into()),
                ],
            ]
        );
    }
}