Skip to content

Commit

Permalink
Update the rs_loader compiler stage 1
Browse files Browse the repository at this point in the history
  • Loading branch information
devraymondsh committed May 22, 2023
1 parent efe08a3 commit d8e0c45
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 84 deletions.
12 changes: 6 additions & 6 deletions source/loaders/rs_loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_RS)
return()
endif()

if(OPTION_BUILD_MUSL)
# TODO: Implement musl support and remove this
message(WARNING "Rust Loader is not implemented yet for musl toolchain, turning off Rust Loader build")
set(OPTION_BUILD_LOADERS_RS OFF CACHE BOOL "" FORCE)
return()
endif()
# if(OPTION_BUILD_MUSL)
# # TODO: Implement musl support and remove this
# message(WARNING "Rust Loader is not implemented yet for musl toolchain, turning off Rust Loader build")
# set(OPTION_BUILD_LOADERS_RS OFF CACHE BOOL "" FORCE)
# return()
# endif()

#
# External dependencies
Expand Down
1 change: 0 additions & 1 deletion source/loaders/rs_loader/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ opt-level = "z"
members = ["compiler"]

[dependencies]
# api = { path = "./api" }
compiler = { path = "./compiler" }
4 changes: 2 additions & 2 deletions source/loaders/rs_loader/rust/compiler/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ pub fn handle_ty(ty: &rustc_ast::Ty) -> FunctionParameter {
}
result.name = symbol_string;
}
TyKind::Rptr(_, MutTy { ty, mutbl }) => {
TyKind::Ptr(MutTy { ty, mutbl: mutble }) => {
let mut inner_ty = handle_ty(ty);
inner_ty.reference = Reference::Yes;
match mutbl {
match mutble {
rustc_hir::Mutability::Mut => inner_ty.mutability = Mutability::Yes,
rustc_hir::Mutability::Not => inner_ty.mutability = Mutability::No,
}
Expand Down
124 changes: 64 additions & 60 deletions source/loaders/rs_loader/rust/compiler/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![feature(rustc_private)]
#![feature(once_cell)]
#![feature(rustc_private, lazy_cell)]
// allow us to match on Box<T>s:
#![feature(box_patterns)]
#![feature(let_else)]
#![feature(iter_zip)]
// allow us to get file prefix
#![feature(path_file_prefix)]
extern crate rustc_ast;
Expand All @@ -23,17 +20,19 @@ extern crate rustc_span;
use dlopen;
use itertools::Itertools;
use rustc_ast::{visit, Impl, Item, ItemKind, VariantData};
use rustc_errors::TerminalUrl;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_interface::{interface::Compiler, Config, Queries};
use rustc_middle::hir::exports::Export;
use rustc_middle::metadata::ModChild;
use rustc_middle::ty::Visibility;
use rustc_session::config::{
self, CrateType, ErrorOutputType, ExternEntry, ExternLocation, Externs, Input,
};
use rustc_session::search_paths::SearchPath;
use rustc_session::utils::CanonicalizedPath;
use rustc_span::source_map;
use std::borrow::BorrowMut;
use std::io::Write;
use std::iter::{self, FromIterator};
use std::{
Expand Down Expand Up @@ -392,28 +391,23 @@ impl CompilerCallbacks {
let krate = queries
.parse()
.expect("no Result<Query<Crate>> found")
.take();
.steal();
let mut item_visitor = ItemVisitor::new();
visit::walk_crate(&mut item_visitor, &krate);
self.classes = item_visitor.classes.into_values().collect();
self.functions = item_visitor.functions;
}
fn analyze_metadata<'tcx>(&mut self, queries: &'tcx Queries<'tcx>) {
let mut class_map: HashMap<DefId, Class> = HashMap::new();

let krates = queries
.expansion()
.pre_configure()
.expect("Unable to get Expansion")
.peek_mut()
.1
.borrow_mut()
.access(|resolver| {
let c_store = resolver.cstore().clone();
c_store.crates_untracked()
});
.get_mut();

queries
.global_ctxt()
.expect("Unable to get global ctxt")
.peek_mut()
.enter(|ctxt| {
// since we are loading a package, input_path should be lib<crate_name>.rlib
let crate_name = &self
Expand All @@ -423,16 +417,20 @@ impl CompilerCallbacks {
.expect("Unable to get file prefix.")
.to_str()
.expect("Unable to cast OsStr to str")[3..];

// find our krate
let crate_num = krates
.iter()
.find_or_first(|&&x| {
ctxt.crate_name(x) == rustc_span::Symbol::intern(crate_name)
.1
.into_iter()
.find_or_first(|x| {
ctxt.crate_name(x.id.as_usize().into())
== rustc_span::Symbol::intern(crate_name)
})
.expect("unable to find crate");

// parse public functions and structs
for child in ctxt.item_children(crate_num.as_def_id()) {
let Export {
for child in ctxt.module_children(crate_num.ident()) {
let ModChild {
ident, res, vis, ..
} = child;
// skip non-public items
Expand All @@ -444,16 +442,16 @@ impl CompilerCallbacks {
let class = class_map.entry(*def_id).or_default();
class.name = ident.to_string();

for field in ctxt.item_children(*def_id) {
for field in ctxt.module_children(def_id) {
if let Some(field) =
middle::extract_attribute_from_export(&ctxt, field)
{
class.attributes.push(field);
}
}

for inherent_impl in ctxt.inherent_impls(*def_id) {
for method in ctxt.item_children(*inherent_impl) {
for inherent_impl in ctxt.inherent_impls(def_id) {
for method in ctxt.module_children(inherent_impl) {
if let Some(function) =
middle::extract_fn_from_export(&ctxt, method)
{
Expand All @@ -472,33 +470,31 @@ impl CompilerCallbacks {
}
Res::Def(DefKind::Fn, def_id) => {
// https://doc.rust-lang.org/stable/nightly-rustc/rustc_middle/ty/struct.Binder.html
let fn_sig = ctxt.fn_sig(*def_id);
let names = ctxt.fn_arg_names(*def_id);
let fn_sig = ctxt.fn_sig(def_id);
let names = ctxt.fn_arg_names(def_id);
self.functions.push(middle::handle_fn(
ident.to_string(),
&fn_sig,
&fn_sig.0,
names,
));
}
_ => {}
}
}
// after parsing all structs, parse tarit implementations.
for trait_impl in ctxt.all_trait_implementations(*crate_num) {
use rustc_middle::ty::fast_reject::SimplifiedTypeGen::AdtSimplifiedType;
if let Some(AdtSimplifiedType(def_id)) = trait_impl.1 {
if let Some(class) = class_map.get_mut(&def_id) {
for func in ctxt.item_children(trait_impl.0) {
if let Some(function) = middle::extract_fn_from_export(&ctxt, func)
{
if function.name == "drop" {
class.destructor = Some(function);
for def_id in ctxt.trait_impls_in_crate(crate_num) {
// use rustc_middle::ty::fast_reject::SimplifiedType;

if let Some(class) = class_map.get_mut(&def_id) {
for func in ctxt.module_children(def_id) {
if let Some(function) = middle::extract_fn_from_export(&ctxt, func) {
if function.name == "drop" {
class.destructor = Some(function);
} else {
if function.has_self() {
class.methods.push(function);
} else {
if function.has_self() {
class.methods.push(function);
} else {
class.static_methods.push(function);
}
class.static_methods.push(function);
}
}
}
Expand Down Expand Up @@ -542,6 +538,8 @@ impl rustc_driver::Callbacks for CompilerCallbacks {
location: ExternLocation::ExactPaths(files),
is_private_dep: false,
add_prelude: true,
force: true,
nounused_dep: true,
});
}
_ => {
Expand Down Expand Up @@ -574,11 +572,13 @@ impl rustc_driver::Callbacks for CompilerCallbacks {
}

config.input = Input::File(wrapped_script_path.clone()); // self.source.input.clone().0;
config.input_path = Some(wrapped_script_path);

// config.input_path = Some(wrapped_script_path);
} else {
// Set up inputs
config.input = self.source.input.clone().0;
config.input_path = Some(self.source.input_path.clone());

// config.input_path = Some(self.source.input_path.clone());
}
// Set up output
if self.is_parsing {
Expand Down Expand Up @@ -757,9 +757,9 @@ impl std::io::Write for DiagnosticSink {

const BUG_REPORT_URL: &str = "https://github.com/metacall/core/issues/new";

static ICE_HOOK: std::lazy::SyncLazy<
static ICE_HOOK: std::sync::LazyLock<
Box<dyn Fn(&std::panic::PanicInfo<'_>) + Sync + Send + 'static>,
> = std::lazy::SyncLazy::new(|| {
> = std::sync::LazyLock::new(|| {
let hook = std::panic::take_hook();
std::panic::set_hook(Box::new(|info| report_ice(info, BUG_REPORT_URL)));
hook
Expand All @@ -775,25 +775,30 @@ fn report_ice(info: &std::panic::PanicInfo<'_>, bug_report_url: &str) {
let emitter = Box::new(rustc_errors::emitter::EmitterWriter::stderr(
rustc_errors::ColorConfig::Auto,
None,
None,
ICE_HOOK,
false,
false,
None,
false,
false,
TerminalUrl::Auto,
));
let handler = rustc_errors::Handler::with_emitter(true, None, emitter);

// a .span_bug or .bug call has already printed what it wants to print
if !info.payload().is::<rustc_errors::ExplicitBug>() {
let d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");
handler.emit_diagnostic(&d);
let mut d = rustc_errors::Diagnostic::new(rustc_errors::Level::Bug, "unexpected panic");

handler.emit_diagnostic(&mut d);
}

let xs: Vec<std::borrow::Cow<'static, str>> = vec![
"the compiler unexpectedly panicked. this is a bug.".into(),
format!("we would appreciate a bug report: {}", bug_report_url).into(),
];

for note in &xs {
for note in xs {
handler.note_without_error(note);
}

Expand All @@ -807,14 +812,14 @@ fn report_ice(info: &std::panic::PanicInfo<'_>, bug_report_url: &str) {

pub fn initialize() {
rustc_driver::init_rustc_env_logger();
std::lazy::SyncLazy::force(&ICE_HOOK);
std::sync::LazyLock::force(&ICE_HOOK);
}

fn run_compiler(
callbacks: &mut (dyn rustc_driver::Callbacks + Send),
diagnostics_buffer: &sync::Arc<sync::Mutex<Vec<u8>>>,
errors_buffer: &sync::Arc<sync::Mutex<Vec<u8>>>,
) -> Result<(), rustc_errors::ErrorReported> {
) -> Result<(), rustc_errors::ErrorGuaranteed> {
let mut config = Config {
// Command line options
opts: config::Options {
Expand All @@ -825,15 +830,15 @@ fn run_compiler(
// cfg! configuration in addition to the default ones
crate_cfg: rustc_hash::FxHashSet::default(), // FxHashSet<(String, Option<String>)>
input: config::Input::File(PathBuf::new()),
input_path: None, // Option<PathBuf>
// input_path: None, // Option<PathBuf>
output_dir: None, // Option<PathBuf>
output_file: None, // Option<PathBuf>
file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>>
diagnostic_output: rustc_session::DiagnosticOutput::Raw(Box::from(DiagnosticSink(
diagnostics_buffer.clone(),
))),
// diagnostic_output: rustc_session::DiagnosticOutput::Raw(Box::from(DiagnosticSink(
// diagnostics_buffer.clone(),
// ))),
// Set to capture stderr output during compiler execution
stderr: Some(errors_buffer.clone()), // Option<Arc<Mutex<Vec<u8>>>>
// stderr: Some(errors_buffer.clone()), // Option<Arc<Mutex<Vec<u8>>>>
lint_caps: rustc_hash::FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level>
// This is a callback from the driver that is called when [`ParseSess`] is created.
parse_sess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>>
Expand All @@ -851,6 +856,8 @@ fn run_compiler(
// Registry of diagnostics codes.
registry: rustc_errors::registry::Registry::new(&rustc_error_codes::DIAGNOSTICS),
make_codegen_backend: None,
crate_check_cfg: Default::default(),
locale_resources: &[],
};

callbacks.config(&mut config);
Expand All @@ -867,19 +874,16 @@ fn run_compiler(
return early_exit();
}

queries.expansion()?;
queries.pre_configure()?;
if callbacks.after_expansion(compiler, queries) == rustc_driver::Compilation::Stop {
return early_exit();
}

queries.prepare_outputs()?;
// queries.prepare_outputs()?;

queries.global_ctxt()?;

queries
.global_ctxt()?
.peek_mut()
.enter(|tcx| tcx.analysis(()))?;
queries.global_ctxt()?.enter(|tcx| tcx.analysis(()))?;

if callbacks.after_analysis(compiler, queries) == rustc_driver::Compilation::Stop {
return early_exit();
Expand Down
Loading

0 comments on commit d8e0c45

Please sign in to comment.