Implement indexing for arrays
This commit is contained in:
parent
ece497cc84
commit
efd203aa0b
4 changed files with 35 additions and 1 deletions
|
|
@ -142,3 +142,7 @@ unsafe fn call_uninit() -> u8 {
|
|||
/*unsafe fn deref_str_ptr(s: *const str) -> &'static str {
|
||||
&*s
|
||||
}*/
|
||||
|
||||
fn use_array(arr: [u8; 3]) -> u8 {
|
||||
arr[1]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -141,3 +141,17 @@ pub mod intrinsics {
|
|||
pub fn uninit<T>() -> T;
|
||||
}
|
||||
}
|
||||
|
||||
#[lang = "index"]
|
||||
pub trait Index<Idx: ?Sized> {
|
||||
type Output: ?Sized;
|
||||
fn index(&self, index: Idx) -> &Self::Output;
|
||||
}
|
||||
|
||||
impl<T> Index<usize> for [T; 3] {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
&self[index]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -800,7 +800,8 @@ pub fn trans_place<'a, 'tcx: 'a>(
|
|||
),
|
||||
ProjectionElem::Field(field, _ty) => base.place_field(fx, field),
|
||||
ProjectionElem::Index(local) => {
|
||||
unimplemented!("projection index {:?} {:?}", projection.base, local)
|
||||
let index = fx.get_local_place(local).to_cvalue(fx).load_value(fx);
|
||||
base.place_index(fx, index)
|
||||
}
|
||||
ProjectionElem::ConstantIndex {
|
||||
offset,
|
||||
|
|
|
|||
|
|
@ -297,6 +297,21 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
|
|||
CPlace::Addr(field_ptr, field_layout)
|
||||
}
|
||||
|
||||
pub fn place_index(self, fx: &mut FunctionCx<'a, 'tcx>, index: Value) -> CPlace<'tcx> {
|
||||
let addr = self.expect_addr();
|
||||
let layout = self.layout();
|
||||
match layout.ty.sty {
|
||||
TypeVariants::TyArray(elem_ty, _) => {
|
||||
let elem_layout = fx.layout_of(elem_ty);
|
||||
let size = fx.bcx.ins().iconst(types::I64, elem_layout.size.bytes() as i64);
|
||||
let offset = fx.bcx.ins().imul(size, index);
|
||||
CPlace::Addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
|
||||
}
|
||||
TypeVariants::TySlice(_elem_ty) => unimplemented!("place_index(TySlice)"),
|
||||
_ => bug!("place_index({:?})", layout.ty),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
|
||||
match self {
|
||||
CPlace::Var(var, _) => CPlace::Var(var, layout),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue