11use eyre:: { Context , Result } ;
22use foundry_common:: { compact_to_contract, strip_bytecode_placeholders} ;
33use foundry_compilers:: {
4- Artifact , Compiler , ProjectCompileOutput ,
4+ Artifact , ProjectCompileOutput ,
55 artifacts:: {
6- Bytecode , Contract , ContractBytecodeSome , Libraries , Source ,
6+ Bytecode , ContractBytecodeSome , Libraries , Source ,
77 sourcemap:: { SourceElement , SourceMap } ,
88 } ,
99 multi:: MultiCompilerLanguage ,
1010} ;
1111use foundry_evm_core:: ic:: PcIcMap ;
1212use foundry_linking:: Linker ;
1313use rayon:: prelude:: * ;
14- use solar:: parse:: { Parser , interface:: Session } ;
1514use std:: {
1615 collections:: { BTreeMap , HashMap , HashSet } ,
1716 fmt:: Write ,
@@ -31,7 +30,13 @@ pub struct SourceData {
3130}
3231
3332impl SourceData {
34- pub fn new ( source : Arc < String > , language : MultiCompilerLanguage , path : PathBuf ) -> Self {
33+ pub fn new (
34+ output : & ProjectCompileOutput ,
35+ source : Arc < String > ,
36+ language : MultiCompilerLanguage ,
37+ path : PathBuf ,
38+ root : & Path ,
39+ ) -> Self {
3540 let mut contract_definitions = Vec :: new ( ) ;
3641
3742 match language {
@@ -42,21 +47,21 @@ impl SourceData {
4247 }
4348 }
4449 MultiCompilerLanguage :: Solc ( _) => {
45- let sess = Session :: builder ( ) . with_silent_emitter ( None ) . build ( ) ;
46- let _ = sess. enter ( || -> solar:: parse:: interface:: Result < ( ) > {
47- let arena = solar:: parse:: ast:: Arena :: new ( ) ;
48- let filename = path. clone ( ) . into ( ) ;
49- let mut parser =
50- Parser :: from_source_code ( & sess, & arena, filename, source. to_string ( ) ) ?;
51- let ast = parser. parse_file ( ) . map_err ( |e| e. emit ( ) ) ?;
52- for item in ast. items {
53- if let solar:: parse:: ast:: ItemKind :: Contract ( contract) = & item. kind {
54- let range = item. span . lo ( ) . to_usize ( ) ..item. span . hi ( ) . to_usize ( ) ;
55- contract_definitions. push ( ( contract. name . to_string ( ) , range) ) ;
50+ let r = output. parser ( ) . solc ( ) . compiler ( ) . enter ( |compiler| -> Option < ( ) > {
51+ let ( _, source) = compiler. gcx ( ) . get_ast_source ( root. join ( & path) ) ?;
52+ for item in source. ast . as_ref ( ) ?. items . iter ( ) {
53+ if let solar:: ast:: ItemKind :: Contract ( contract) = & item. kind {
54+ contract_definitions. push ( (
55+ contract. name . to_string ( ) ,
56+ compiler. sess ( ) . source_map ( ) . span_to_source ( item. span ) . unwrap ( ) . 1 ,
57+ ) ) ;
5658 }
5759 }
58- Ok ( ( ) )
60+ Some ( ( ) )
5961 } ) ;
62+ if r. is_none ( ) {
63+ warn ! ( "failed to parse contract definitions for {}" , path. display( ) ) ;
64+ }
6065 }
6166 }
6267
@@ -135,15 +140,12 @@ impl ContractSources {
135140 Ok ( sources)
136141 }
137142
138- pub fn insert < C : Compiler < CompilerContract = Contract > > (
143+ pub fn insert (
139144 & mut self ,
140- output : & ProjectCompileOutput < C > ,
145+ output : & ProjectCompileOutput ,
141146 root : & Path ,
142147 libraries : Option < & Libraries > ,
143- ) -> Result < ( ) >
144- where
145- C :: Language : Into < MultiCompilerLanguage > ,
146- {
148+ ) -> Result < ( ) > {
147149 let link_data = libraries. map ( |libraries| {
148150 let linker = Linker :: new ( root, output. artifact_ids ( ) . collect ( ) ) ;
149151 ( linker, libraries)
@@ -197,9 +199,11 @@ impl ContractSources {
197199 } ) ?;
198200 let stripped = path. strip_prefix ( root) . unwrap_or ( path) . to_path_buf ( ) ;
199201 let source_data = Arc :: new ( SourceData :: new (
202+ output,
200203 source. content . clone ( ) ,
201- build. language . into ( ) ,
204+ build. language ,
202205 stripped,
206+ root,
203207 ) ) ;
204208 entry. insert ( source_data. clone ( ) ) ;
205209 source_data
0 commit comments