Skip to content

Commit 3d70f0f

Browse files
devraymondshviferga
authored andcommitted
Pass the parsed data from rustc_driver to the main thread
1 parent 6856aa4 commit 3d70f0f

File tree

5 files changed

+126
-147
lines changed

5 files changed

+126
-147
lines changed

source/loaders/rs_loader/rust/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ opt-level = "z"
1515

1616
[workspace]
1717
members = ["parser"]
18+
19+
[dependencies]
20+
parser = { path = "./parser" }
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use rustc_ast::{ast::ItemKind::Fn, Crate, FnKind, FnRetTy, PatKind::Ident, TyKind};
2+
use std::borrow::Borrow;
3+
4+
#[derive(Debug)]
5+
pub struct ParsedRustFunctionArguments {
6+
name: String,
7+
order_number: usize,
8+
value_type: TyKind,
9+
}
10+
#[derive(Debug)]
11+
pub struct ParsedRustFunction {
12+
name: String,
13+
asyncness: rustc_ast::Async,
14+
return_type: FnRetTy,
15+
arguments_length: usize,
16+
arguments: Vec<ParsedRustFunctionArguments>,
17+
}
18+
19+
unsafe impl Send for ParsedRustFunction {}
20+
unsafe impl Sync for ParsedRustFunction {}
21+
22+
pub fn parse_functions(parse_source: Crate) -> Vec<ParsedRustFunction> {
23+
let mut parsed_rust_functions: Vec<ParsedRustFunction> = Vec::new();
24+
25+
for parsed_item in parse_source.items.iter() {
26+
if let Fn(function) = parsed_item.kind.borrow() {
27+
match function.borrow() {
28+
FnKind(_the_final, function_signature, _generic, _some) => {
29+
let mut arguments: Vec<ParsedRustFunctionArguments> = Vec::new();
30+
31+
let mut i: usize = 0;
32+
for function_input in function_signature.decl.inputs.iter() {
33+
arguments.push(ParsedRustFunctionArguments {
34+
order_number: i,
35+
name: match function_input.pat.kind {
36+
Ident(_by_value, name, _) => name.to_string(),
37+
_ => String::new(),
38+
},
39+
value_type: function_input.ty.kind.clone(),
40+
});
41+
42+
i += 1;
43+
}
44+
45+
let return_type = function_signature.decl.output.clone();
46+
47+
parsed_rust_functions.push(ParsedRustFunction {
48+
arguments,
49+
return_type,
50+
name: parsed_item.ident.to_string(),
51+
asyncness: function_signature.header.asyncness,
52+
arguments_length: function_signature.decl.inputs.len(),
53+
});
54+
}
55+
}
56+
}
57+
}
58+
59+
parsed_rust_functions
60+
}

source/loaders/rs_loader/rust/parser/src/lib.rs

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,76 +14,70 @@ use rustc_hash::{FxHashMap, FxHashSet};
1414
use rustc_session::config;
1515
use rustc_session::config::CrateType;
1616
use rustc_span::source_map;
17-
use std::rc::Rc;
18-
use std::sync::Mutex;
1917

20-
mod funtions_extractor;
18+
mod function_parser;
2119

2220
pub struct Source {
23-
code: String,
24-
filename: String,
21+
code: String,
22+
filename: String,
2523
}
2624
impl Source {
27-
pub fn new(code: String, filename: String) -> Source {
28-
Source { code, filename }
29-
}
25+
pub fn new(code: String, filename: String) -> Source {
26+
Source { code, filename }
27+
}
3028
}
3129

32-
pub fn extract_functions(source: Source) {
33-
let Source { code, filename } = source;
30+
pub fn parse_functions(source: Source) {
31+
let Source { code, filename } = source;
3432

35-
let config = rustc_interface::Config {
36-
// Command line options
37-
opts: config::Options {
38-
crate_types: vec![CrateType::Cdylib],
39-
..config::Options::default()
40-
},
41-
// cfg! configuration in addition to the default ones
42-
crate_cfg: FxHashSet::default(), // FxHashSet<(String, Option<String>)>
43-
input: config::Input::Str {
44-
name: source_map::FileName::Custom(filename.clone()),
45-
input: code.clone(),
46-
},
47-
input_path: None, // Option<PathBuf>
48-
output_dir: None, // Option<PathBuf>
49-
output_file: None, // Option<PathBuf>
50-
file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>>
51-
diagnostic_output: rustc_session::DiagnosticOutput::Default,
52-
// Set to capture stderr output during compiler execution
53-
stderr: None, // Option<Arc<Mutex<Vec<u8>>>>
54-
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
55-
// This is a callback from the driver that is called when [`ParseSess`] is created.
56-
parse_sess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
57-
// This is a callback from the driver that is called when we're registering lints;
58-
// it is called during plugin registration when we have the LintStore in a non-shared state.
59-
//
60-
// Note that if you find a Some here you probably want to call that function in the new
61-
// function being registered.
62-
register_lints: None, // Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>
63-
// This is a callback from the driver that is called just after we have populated
64-
// the list of queries.
65-
//
66-
// The second parameter is local providers and the third parameter is external providers.
67-
override_queries: None, // Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>
68-
// Registry of diagnostics codes.
69-
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
70-
make_codegen_backend: None,
71-
};
33+
let config = rustc_interface::Config {
34+
// Command line options
35+
opts: config::Options {
36+
crate_types: vec![CrateType::Cdylib],
37+
..config::Options::default()
38+
},
39+
// cfg! configuration in addition to the default ones
40+
crate_cfg: FxHashSet::default(), // FxHashSet<(String, Option<String>)>
41+
input: config::Input::Str {
42+
name: source_map::FileName::Custom(filename.clone()),
43+
input: code.clone(),
44+
},
45+
input_path: None, // Option<PathBuf>
46+
output_dir: None, // Option<PathBuf>
47+
output_file: None, // Option<PathBuf>
48+
file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>>
49+
diagnostic_output: rustc_session::DiagnosticOutput::Default,
50+
// Set to capture stderr output during compiler execution
51+
stderr: None, // Option<Arc<Mutex<Vec<u8>>>>
52+
lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
53+
// This is a callback from the driver that is called when [`ParseSess`] is created.
54+
parse_sess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
55+
// This is a callback from the driver that is called when we're registering lints;
56+
// it is called during plugin registration when we have the LintStore in a non-shared state.
57+
//
58+
// Note that if you find a Some here you probably want to call that function in the new
59+
// function being registered.
60+
register_lints: None, // Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>
61+
// This is a callback from the driver that is called just after we have populated
62+
// the list of queries.
63+
//
64+
// The second parameter is local providers and the third parameter is external providers.
65+
override_queries: None, // Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>
66+
// Registry of diagnostics codes.
67+
registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
68+
make_codegen_backend: None,
69+
};
7270

73-
let rc = Rc::new(Mutex::new<Option<Vec<funtions_extractor::ParsedRustFunction>>>(None));
71+
let parsed_functions = rustc_interface::run_compiler(config, move |compiler| {
72+
compiler.enter(move |queries| {
73+
// Parse the program and print the syntax tree.
74+
let parsed_source: rustc_ast::Crate = queries.parse().unwrap().take();
7475

75-
let rc_compiler = Rc::clone(&rc);
76+
let extracted_functions = function_parser::parse_functions(parsed_source);
7677

77-
rustc_interface::run_compiler(config, move |compiler| {
78-
compiler.enter(move |queries| {
79-
// Parse the program and print the syntax tree.
80-
let parsed_source: rustc_ast::Crate = queries.parse().unwrap().take();
78+
extracted_functions
79+
})
80+
});
8181

82-
let extracted_functions = rc_compiler.lock().unwrap();
83-
84-
extracted_functions = funtions_extractor::functionss_extractor(parsed_source)
85-
86-
// tx.send(extracted_functions).unwrap();
87-
})
88-
});
82+
println!("Parsed: {:#?}", parsed_functions);
8983
}

source/loaders/rs_loader/rust/parser/src/parser.rs

Lines changed: 0 additions & 87 deletions
This file was deleted.

source/loaders/rs_loader/rust/src/lifecycle/execution_path.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use crate::{bridge_api, c_char, c_int, c_void, CStr, PathBuf};
2+
use parser::{self, Source};
3+
4+
use std::fs;
25

36
#[no_mangle]
47
pub extern "C" fn rs_loader_impl_execution_path(
@@ -17,5 +20,11 @@ pub extern "C" fn rs_loader_impl_execution_path(
1720
.push(PathBuf::from(path_slice));
1821
}
1922

23+
parser::parse_functions(Source::new(
24+
fs::read_to_string("/home/raymond/projects/side-projects/metacall/core/build/test.rs")
25+
.unwrap(),
26+
String::from("test.rs"),
27+
));
28+
2029
0 as c_int
2130
}

0 commit comments

Comments
 (0)