Add APIs for fetching foreign items including foreign modules, their ABIs, and their items
This commit is contained in:
parent
bdc15928c8
commit
213748749e
9 changed files with 259 additions and 38 deletions
|
@ -11,9 +11,10 @@ use crate::mir::mono::{Instance, InstanceDef, StaticDef};
|
|||
use crate::mir::Body;
|
||||
use crate::target::MachineInfo;
|
||||
use crate::ty::{
|
||||
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, GenericArgs,
|
||||
GenericPredicates, Generics, ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy, Span, TraitDecl,
|
||||
TraitDef, Ty, TyKind, VariantDef,
|
||||
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, Const, FieldDef, FnDef, ForeignDef,
|
||||
ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
|
||||
ImplDef, ImplTrait, LineInfo, PolyFnSig, RigidTy, Span, TraitDecl, TraitDef, Ty, TyKind,
|
||||
VariantDef,
|
||||
};
|
||||
use crate::{
|
||||
mir, Crate, CrateItem, CrateItems, CrateNum, DefId, Error, Filename, ImplTraitDecls, ItemKind,
|
||||
|
@ -31,6 +32,9 @@ pub trait Context {
|
|||
fn mir_body(&self, item: DefId) -> mir::Body;
|
||||
/// Check whether the body of a function is available.
|
||||
fn has_body(&self, item: DefId) -> bool;
|
||||
fn foreign_modules(&self, crate_num: CrateNum) -> Vec<ForeignModuleDef>;
|
||||
fn foreign_module(&self, mod_def: ForeignModuleDef) -> ForeignModule;
|
||||
fn foreign_items(&self, mod_def: ForeignModuleDef) -> Vec<ForeignDef>;
|
||||
fn all_trait_decls(&self) -> TraitDecls;
|
||||
fn trait_decls(&self, crate_num: CrateNum) -> TraitDecls;
|
||||
fn trait_decl(&self, trait_def: &TraitDef) -> TraitDecl;
|
||||
|
@ -66,6 +70,9 @@ pub trait Context {
|
|||
/// Returns whether this is a foreign item.
|
||||
fn is_foreign_item(&self, item: DefId) -> bool;
|
||||
|
||||
/// Returns the kind of a given foreign item.
|
||||
fn foreign_item_kind(&self, def: ForeignDef) -> Option<ForeignItemKind>;
|
||||
|
||||
/// Returns the kind of a given algebraic data type
|
||||
fn adt_kind(&self, def: AdtDef) -> AdtKind;
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ pub use crate::error::*;
|
|||
use crate::mir::pretty::function_name;
|
||||
use crate::mir::Body;
|
||||
use crate::mir::Mutability;
|
||||
use crate::ty::{ImplDef, IndexedVal, Span, TraitDef, Ty};
|
||||
use crate::ty::{ForeignModuleDef, ImplDef, IndexedVal, Span, TraitDef, Ty};
|
||||
|
||||
pub mod abi;
|
||||
#[macro_use]
|
||||
|
@ -86,6 +86,11 @@ pub struct Crate {
|
|||
}
|
||||
|
||||
impl Crate {
|
||||
/// The list of foreign modules in this crate.
|
||||
pub fn foreign_modules(&self) -> Vec<ForeignModuleDef> {
|
||||
with(|cx| cx.foreign_modules(self.id))
|
||||
}
|
||||
|
||||
/// The list of traits declared in this crate.
|
||||
pub fn trait_decls(&self) -> TraitDecls {
|
||||
with(|cx| cx.trait_decls(self.id))
|
||||
|
|
|
@ -4,9 +4,9 @@ use super::{
|
|||
with, DefId, Error, Symbol,
|
||||
};
|
||||
use crate::abi::Layout;
|
||||
use crate::crate_def::CrateDef;
|
||||
use crate::mir::alloc::{read_target_int, read_target_uint, AllocId};
|
||||
use crate::target::MachineInfo;
|
||||
use crate::{crate_def::CrateDef, mir::mono::StaticDef};
|
||||
use crate::{Filename, Opaque};
|
||||
use std::fmt::{self, Debug, Display, Formatter};
|
||||
use std::ops::Range;
|
||||
|
@ -539,11 +539,45 @@ pub enum Movability {
|
|||
Movable,
|
||||
}
|
||||
|
||||
crate_def! {
|
||||
pub ForeignModuleDef;
|
||||
}
|
||||
|
||||
impl ForeignModuleDef {
|
||||
pub fn module(&self) -> ForeignModule {
|
||||
with(|cx| cx.foreign_module(*self))
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ForeignModule {
|
||||
pub def_id: ForeignModuleDef,
|
||||
pub abi: Abi,
|
||||
}
|
||||
|
||||
impl ForeignModule {
|
||||
pub fn items(&self) -> Vec<ForeignDef> {
|
||||
with(|cx| cx.foreign_items(self.def_id))
|
||||
}
|
||||
}
|
||||
|
||||
crate_def! {
|
||||
/// Hold information about a ForeignItem in a crate.
|
||||
pub ForeignDef;
|
||||
}
|
||||
|
||||
impl ForeignDef {
|
||||
pub fn kind(&self) -> Option<ForeignItemKind> {
|
||||
with(|cx| cx.foreign_item_kind(*self))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
|
||||
pub enum ForeignItemKind {
|
||||
Fn(FnDef),
|
||||
Static(StaticDef),
|
||||
Type(Ty),
|
||||
}
|
||||
|
||||
crate_def! {
|
||||
/// Hold information about a function definition in a crate.
|
||||
pub FnDef;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue