1
Fork 0

Add stable Instance::body() and RustcInternal trait

The `Instance::body()` returns a monomorphized body.

For that, we had to implement visitor that monomorphize types and
constants. We are also introducing the RustcInternal trait that will
allow us to convert back from Stable to Internal.

Note that this trait is not yet visible for our users as it depends on
Tables. We should probably add a new trait that can be exposed.
This commit is contained in:
Celina G. Val 2023-10-19 17:06:53 -07:00
parent cc705b8012
commit 6ed2a76bcc
10 changed files with 218 additions and 7 deletions

View file

@ -4,6 +4,7 @@
//! - [CompilerError]: This represents errors that can be raised when invoking the compiler.
//! - [Error]: Generic error that represents the reason why a request that could not be fulfilled.
use std::convert::From;
use std::fmt::{Debug, Display, Formatter};
use std::{error, fmt};
@ -31,6 +32,12 @@ impl Error {
}
}
impl From<&str> for Error {
fn from(value: &str) -> Self {
Self(value.into())
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)

View file

@ -39,6 +39,7 @@ pub mod visitor;
pub use error::*;
use mir::mono::Instance;
use ty::{FnDef, GenericArgs};
/// Use String for now but we should replace it.
pub type Symbol = String;
@ -233,6 +234,9 @@ pub trait Context {
/// Item requires monomorphization.
fn requires_monomorphization(&self, def_id: DefId) -> bool;
/// Resolve an instance from the given function definition and generic arguments.
fn resolve_instance(&mut self, def: FnDef, args: &GenericArgs) -> Option<Instance>;
}
// A thread local variable that stores a pointer to the tables mapping between TyCtxt

View file

@ -5,9 +5,11 @@ use crate::{ty::Ty, Span};
#[derive(Clone, Debug)]
pub struct Body {
pub blocks: Vec<BasicBlock>,
pub locals: Vec<LocalDecl>,
pub locals: LocalDecls,
}
type LocalDecls = Vec<LocalDecl>;
#[derive(Clone, Debug)]
pub struct LocalDecl {
pub ty: Ty,
@ -344,6 +346,7 @@ pub enum Operand {
#[derive(Clone, Debug)]
pub struct Place {
pub local: Local,
/// projection out of a place (access a field, deref a pointer, etc)
pub projection: String,
}
@ -462,3 +465,25 @@ pub enum NullOp {
/// Returns the offset of a field.
OffsetOf(Vec<FieldIdx>),
}
impl Operand {
pub fn ty(&self, locals: &LocalDecls) -> Ty {
match self {
Operand::Copy(place) | Operand::Move(place) => place.ty(locals),
Operand::Constant(c) => c.ty(),
}
}
}
impl Constant {
pub fn ty(&self) -> Ty {
self.literal.ty
}
}
impl Place {
pub fn ty(&self, locals: &LocalDecls) -> Ty {
let _start_ty = locals[self.local].ty;
todo!("Implement projection")
}
}

View file

@ -1,5 +1,5 @@
use crate::mir::Body;
use crate::ty::{IndexedVal, Ty};
use crate::ty::{FnDef, GenericArgs, IndexedVal, Ty};
use crate::{with, CrateItem, DefId, Error, Opaque};
use std::fmt::Debug;
@ -41,6 +41,15 @@ impl Instance {
pub fn ty(&self) -> Ty {
with(|context| context.instance_ty(self.def))
}
/// Resolve an instance starting from a function definition and generic arguments.
pub fn resolve(def: FnDef, args: &GenericArgs) -> Result<Instance, crate::Error> {
with(|context| {
context.resolve_instance(def, args).ok_or_else(|| {
crate::Error::new(format!("Failed to resolve `{def:?}` with `{args:?}`"))
})
})
}
}
/// Try to convert a crate item into an instance.

View file

@ -225,6 +225,8 @@ pub struct ImplDef(pub DefId);
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct RegionDef(pub DefId);
/// A list of generic arguments.
/// The second field is for internal usage to allow retrieving the internal representation.
#[derive(Clone, Debug)]
pub struct GenericArgs(pub Vec<GenericArgKind>);