Add a stable MIR way to get the main function
This commit is contained in:
parent
fab99073b0
commit
e404e77c0b
@ -40,6 +40,10 @@ pub fn all_local_items() -> stable_mir::CrateItems {
|
|||||||
with(|tcx| tcx.mir_keys(()).iter().map(|item| crate_item(item.to_def_id())).collect())
|
with(|tcx| tcx.mir_keys(()).iter().map(|item| crate_item(item.to_def_id())).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn entry_fn() -> Option<stable_mir::CrateItem> {
|
||||||
|
with(|tcx| Some(crate_item(tcx.entry_fn(())?.0)))
|
||||||
|
}
|
||||||
|
|
||||||
/// Build a stable mir crate from a given crate number.
|
/// Build a stable mir crate from a given crate number.
|
||||||
fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
|
fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
|
||||||
let crate_name = tcx.crate_name(crate_num).to_string();
|
let crate_name = tcx.crate_name(crate_num).to_string();
|
||||||
|
@ -45,6 +45,13 @@ impl CrateItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return the function where execution starts if the current
|
||||||
|
/// crate defines that. This is usually `main`, but could be
|
||||||
|
/// `start` if the crate is a no-std crate.
|
||||||
|
pub fn entry_fn() -> Option<CrateItem> {
|
||||||
|
crate::rustc_smir::entry_fn()
|
||||||
|
}
|
||||||
|
|
||||||
/// Access to the local crate.
|
/// Access to the local crate.
|
||||||
pub fn local_crate() -> Crate {
|
pub fn local_crate() -> Crate {
|
||||||
crate::rustc_smir::local_crate()
|
crate::rustc_smir::local_crate()
|
||||||
|
@ -29,6 +29,8 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
|
|||||||
let local = stable_mir::local_crate();
|
let local = stable_mir::local_crate();
|
||||||
assert_eq!(&local.name, CRATE_NAME);
|
assert_eq!(&local.name, CRATE_NAME);
|
||||||
|
|
||||||
|
assert_eq!(stable_mir::entry_fn(), None);
|
||||||
|
|
||||||
// Find items in the local crate.
|
// Find items in the local crate.
|
||||||
let items = stable_mir::all_local_items();
|
let items = stable_mir::all_local_items();
|
||||||
assert!(get_item(tcx, &items, (DefKind::Fn, "foo_bar")).is_some());
|
assert!(get_item(tcx, &items, (DefKind::Fn, "foo_bar")).is_some());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user