Skip to content

Commit 62d6031

Browse files
committed
refactor: unify Tables implementation with bridge types and re-export IndexedVal
define bridge types for `***Def`s. consolidate scattered `Tables` implementations into single inherent impl.
1 parent 45cf29d commit 62d6031

File tree

13 files changed

+285
-194
lines changed

13 files changed

+285
-194
lines changed

compiler/rustc_smir/src/rustc_internal/mod.rs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ use std::ops::Index;
1010

1111
use rustc_data_structures::fx;
1212
use rustc_data_structures::fx::FxIndexMap;
13-
use rustc_middle::mir::interpret::AllocId;
14-
use rustc_middle::ty;
1513
use rustc_middle::ty::TyCtxt;
16-
use rustc_span::Span;
17-
use rustc_span::def_id::{CrateNum, DefId};
14+
use rustc_span::def_id::CrateNum;
1815
use scoped_tls::scoped_thread_local;
1916
use stable_mir::Error;
2017
use stable_mir::convert::{RustcInternal, Stable};
@@ -58,37 +55,6 @@ where
5855
with_container(|tables, cx| item.internal(tables, cx))
5956
}
6057

61-
impl<'tcx, B: Bridge> Index<B::DefId> for Tables<'tcx, B> {
62-
type Output = DefId;
63-
64-
#[inline(always)]
65-
fn index(&self, index: B::DefId) -> &Self::Output {
66-
&self.def_ids[index]
67-
}
68-
}
69-
70-
impl<'tcx, B: Bridge> Tables<'tcx, B> {
71-
pub fn create_def_id(&mut self, did: DefId) -> B::DefId {
72-
self.def_ids.create_or_fetch(did)
73-
}
74-
75-
pub fn create_alloc_id(&mut self, aid: AllocId) -> B::AllocId {
76-
self.alloc_ids.create_or_fetch(aid)
77-
}
78-
79-
pub fn create_span(&mut self, span: Span) -> B::Span {
80-
self.spans.create_or_fetch(span)
81-
}
82-
83-
pub fn instance_def(&mut self, instance: ty::Instance<'tcx>) -> B::InstanceDef {
84-
self.instances.create_or_fetch(instance)
85-
}
86-
87-
pub fn layout_id(&mut self, layout: rustc_abi::Layout<'tcx>) -> B::Layout {
88-
self.layouts.create_or_fetch(layout)
89-
}
90-
}
91-
9258
pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
9359
item.id.into()
9460
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//! Defines a set of traits that is used for abstracting
2+
//! stable_mir's components that are needed in rustc_smir.
3+
//!
4+
//! These traits are really useful when programming
5+
//! in stable_mir-agnostic settings.
6+
7+
use std::fmt::Debug;
8+
9+
use super::Bridge;
10+
11+
pub trait SmirError {
12+
fn new(msg: String) -> Self;
13+
fn from_internal<T: Debug>(err: T) -> Self;
14+
}
15+
16+
pub trait Prov<B: Bridge> {
17+
fn new(aid: B::AllocId) -> Self;
18+
}
19+
20+
macro_rules! make_bridge_trait {
21+
($name:ident) => {
22+
pub trait $name<B: Bridge> {
23+
fn new(did: B::DefId) -> Self;
24+
}
25+
};
26+
}
27+
28+
make_bridge_trait!(CrateItem);
29+
make_bridge_trait!(AdtDef);
30+
make_bridge_trait!(ForeignModuleDef);
31+
make_bridge_trait!(ForeignDef);
32+
make_bridge_trait!(FnDef);
33+
make_bridge_trait!(ClosureDef);
34+
make_bridge_trait!(CoroutineDef);
35+
make_bridge_trait!(CoroutineClosureDef);
36+
make_bridge_trait!(AliasDef);
37+
make_bridge_trait!(ParamDef);
38+
make_bridge_trait!(BrNamedDef);
39+
make_bridge_trait!(TraitDef);
40+
make_bridge_trait!(GenericDef);
41+
make_bridge_trait!(ConstDef);
42+
make_bridge_trait!(ImplDef);
43+
make_bridge_trait!(RegionDef);
44+
make_bridge_trait!(CoroutineWitnessDef);
45+
make_bridge_trait!(AssocDef);
46+
make_bridge_trait!(OpaqueDef);
47+
make_bridge_trait!(StaticDef);

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 157 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99
1010
use std::cell::RefCell;
1111
use std::fmt::Debug;
12+
use std::ops::Index;
1213

14+
use bridge::*;
1315
use context::SmirCtxt;
1416
use rustc_hir::def::DefKind;
1517
use rustc_middle::mir;
1618
use rustc_middle::mir::interpret::AllocId;
1719
use rustc_middle::ty::{self, Ty, TyCtxt};
20+
use rustc_span::Span;
1821
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1922

2023
use crate::rustc_internal::IndexMap;
2124

2225
pub mod alloc;
26+
pub mod bridge;
2327
mod builder;
2428
pub mod context;
2529

@@ -30,14 +34,14 @@ pub struct SmirContainer<'tcx, B: Bridge> {
3034
}
3135

3236
pub struct Tables<'tcx, B: Bridge> {
33-
pub(crate) def_ids: IndexMap<DefId, B::DefId>,
34-
pub(crate) alloc_ids: IndexMap<AllocId, B::AllocId>,
35-
pub(crate) spans: IndexMap<rustc_span::Span, B::Span>,
36-
pub(crate) types: IndexMap<Ty<'tcx>, B::Ty>,
37-
pub(crate) instances: IndexMap<ty::Instance<'tcx>, B::InstanceDef>,
38-
pub(crate) ty_consts: IndexMap<ty::Const<'tcx>, B::TyConstId>,
39-
pub(crate) mir_consts: IndexMap<mir::Const<'tcx>, B::MirConstId>,
40-
pub(crate) layouts: IndexMap<rustc_abi::Layout<'tcx>, B::Layout>,
37+
pub def_ids: IndexMap<DefId, B::DefId>,
38+
pub alloc_ids: IndexMap<AllocId, B::AllocId>,
39+
pub spans: IndexMap<rustc_span::Span, B::Span>,
40+
pub types: IndexMap<Ty<'tcx>, B::Ty>,
41+
pub instances: IndexMap<ty::Instance<'tcx>, B::InstanceDef>,
42+
pub ty_consts: IndexMap<ty::Const<'tcx>, B::TyConstId>,
43+
pub mir_consts: IndexMap<mir::Const<'tcx>, B::MirConstId>,
44+
pub layouts: IndexMap<rustc_abi::Layout<'tcx>, B::Layout>,
4145
}
4246

4347
impl<'tcx, B: Bridge> Default for Tables<'tcx, B> {
@@ -55,23 +59,142 @@ impl<'tcx, B: Bridge> Default for Tables<'tcx, B> {
5559
}
5660
}
5761

62+
impl<'tcx, B: Bridge> Index<B::DefId> for Tables<'tcx, B> {
63+
type Output = DefId;
64+
65+
#[inline(always)]
66+
fn index(&self, index: B::DefId) -> &Self::Output {
67+
&self.def_ids[index]
68+
}
69+
}
70+
5871
impl<'tcx, B: Bridge> Tables<'tcx, B> {
59-
pub(crate) fn intern_ty(&mut self, ty: Ty<'tcx>) -> B::Ty {
72+
pub fn intern_ty(&mut self, ty: Ty<'tcx>) -> B::Ty {
6073
self.types.create_or_fetch(ty)
6174
}
6275

63-
pub(crate) fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> B::TyConstId {
76+
pub fn intern_ty_const(&mut self, ct: ty::Const<'tcx>) -> B::TyConstId {
6477
self.ty_consts.create_or_fetch(ct)
6578
}
6679

67-
pub(crate) fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> B::MirConstId {
80+
pub fn intern_mir_const(&mut self, constant: mir::Const<'tcx>) -> B::MirConstId {
6881
self.mir_consts.create_or_fetch(constant)
6982
}
83+
84+
pub fn create_def_id(&mut self, did: DefId) -> B::DefId {
85+
self.def_ids.create_or_fetch(did)
86+
}
87+
88+
pub fn create_alloc_id(&mut self, aid: AllocId) -> B::AllocId {
89+
self.alloc_ids.create_or_fetch(aid)
90+
}
91+
92+
pub fn create_span(&mut self, span: Span) -> B::Span {
93+
self.spans.create_or_fetch(span)
94+
}
95+
96+
pub fn instance_def(&mut self, instance: ty::Instance<'tcx>) -> B::InstanceDef {
97+
self.instances.create_or_fetch(instance)
98+
}
99+
100+
pub fn layout_id(&mut self, layout: rustc_abi::Layout<'tcx>) -> B::Layout {
101+
self.layouts.create_or_fetch(layout)
102+
}
103+
104+
pub fn crate_item(&mut self, did: rustc_span::def_id::DefId) -> B::CrateItem {
105+
B::CrateItem::new(self.create_def_id(did))
106+
}
107+
108+
pub fn adt_def(&mut self, did: rustc_span::def_id::DefId) -> B::AdtDef {
109+
B::AdtDef::new(self.create_def_id(did))
110+
}
111+
112+
pub fn foreign_module_def(&mut self, did: rustc_span::def_id::DefId) -> B::ForeignModuleDef {
113+
B::ForeignModuleDef::new(self.create_def_id(did))
114+
}
115+
116+
pub fn foreign_def(&mut self, did: rustc_span::def_id::DefId) -> B::ForeignDef {
117+
B::ForeignDef::new(self.create_def_id(did))
118+
}
119+
120+
pub fn fn_def(&mut self, did: rustc_span::def_id::DefId) -> B::FnDef {
121+
B::FnDef::new(self.create_def_id(did))
122+
}
123+
124+
pub fn closure_def(&mut self, did: rustc_span::def_id::DefId) -> B::ClosureDef {
125+
B::ClosureDef::new(self.create_def_id(did))
126+
}
127+
128+
pub fn coroutine_def(&mut self, did: rustc_span::def_id::DefId) -> B::CoroutineDef {
129+
B::CoroutineDef::new(self.create_def_id(did))
130+
}
131+
132+
pub fn coroutine_closure_def(
133+
&mut self,
134+
did: rustc_span::def_id::DefId,
135+
) -> B::CoroutineClosureDef {
136+
B::CoroutineClosureDef::new(self.create_def_id(did))
137+
}
138+
139+
pub fn alias_def(&mut self, did: rustc_span::def_id::DefId) -> B::AliasDef {
140+
B::AliasDef::new(self.create_def_id(did))
141+
}
142+
143+
pub fn param_def(&mut self, did: rustc_span::def_id::DefId) -> B::ParamDef {
144+
B::ParamDef::new(self.create_def_id(did))
145+
}
146+
147+
pub fn br_named_def(&mut self, did: rustc_span::def_id::DefId) -> B::BrNamedDef {
148+
B::BrNamedDef::new(self.create_def_id(did))
149+
}
150+
151+
pub fn trait_def(&mut self, did: rustc_span::def_id::DefId) -> B::TraitDef {
152+
B::TraitDef::new(self.create_def_id(did))
153+
}
154+
155+
pub fn generic_def(&mut self, did: rustc_span::def_id::DefId) -> B::GenericDef {
156+
B::GenericDef::new(self.create_def_id(did))
157+
}
158+
159+
pub fn const_def(&mut self, did: rustc_span::def_id::DefId) -> B::ConstDef {
160+
B::ConstDef::new(self.create_def_id(did))
161+
}
162+
163+
pub fn impl_def(&mut self, did: rustc_span::def_id::DefId) -> B::ImplDef {
164+
B::ImplDef::new(self.create_def_id(did))
165+
}
166+
167+
pub fn region_def(&mut self, did: rustc_span::def_id::DefId) -> B::RegionDef {
168+
B::RegionDef::new(self.create_def_id(did))
169+
}
170+
171+
pub fn coroutine_witness_def(
172+
&mut self,
173+
did: rustc_span::def_id::DefId,
174+
) -> B::CoroutineWitnessDef {
175+
B::CoroutineWitnessDef::new(self.create_def_id(did))
176+
}
177+
178+
pub fn assoc_def(&mut self, did: rustc_span::def_id::DefId) -> B::AssocDef {
179+
B::AssocDef::new(self.create_def_id(did))
180+
}
181+
182+
pub fn opaque_def(&mut self, did: rustc_span::def_id::DefId) -> B::OpaqueDef {
183+
B::OpaqueDef::new(self.create_def_id(did))
184+
}
185+
186+
pub fn prov(&mut self, aid: rustc_middle::mir::interpret::AllocId) -> B::Prov {
187+
B::Prov::new(self.create_alloc_id(aid))
188+
}
189+
190+
pub fn static_def(&mut self, did: rustc_span::def_id::DefId) -> B::StaticDef {
191+
B::StaticDef::new(self.create_def_id(did))
192+
}
70193
}
71194

72195
/// A trait defining types that are used to emulate StableMIR components, which is really
73196
/// useful when programming in stable_mir-agnostic settings.
74-
pub trait Bridge {
197+
pub trait Bridge: Sized {
75198
type DefId: Copy + Debug + PartialEq + IndexedVal;
76199
type AllocId: Copy + Debug + PartialEq + IndexedVal;
77200
type Span: Copy + Debug + PartialEq + IndexedVal;
@@ -80,12 +203,29 @@ pub trait Bridge {
80203
type TyConstId: Copy + Debug + PartialEq + IndexedVal;
81204
type MirConstId: Copy + Debug + PartialEq + IndexedVal;
82205
type Layout: Copy + Debug + PartialEq + IndexedVal;
83-
type Error: SmirError;
84-
}
85206

86-
pub trait SmirError {
87-
fn new(msg: String) -> Self;
88-
fn from_internal<T: Debug>(err: T) -> Self;
207+
type Error: SmirError;
208+
type CrateItem: CrateItem<Self>;
209+
type AdtDef: AdtDef<Self>;
210+
type ForeignModuleDef: ForeignModuleDef<Self>;
211+
type ForeignDef: ForeignDef<Self>;
212+
type FnDef: FnDef<Self>;
213+
type ClosureDef: ClosureDef<Self>;
214+
type CoroutineDef: CoroutineDef<Self>;
215+
type CoroutineClosureDef: CoroutineClosureDef<Self>;
216+
type AliasDef: AliasDef<Self>;
217+
type ParamDef: ParamDef<Self>;
218+
type BrNamedDef: BrNamedDef<Self>;
219+
type TraitDef: TraitDef<Self>;
220+
type GenericDef: GenericDef<Self>;
221+
type ConstDef: ConstDef<Self>;
222+
type ImplDef: ImplDef<Self>;
223+
type RegionDef: RegionDef<Self>;
224+
type CoroutineWitnessDef: CoroutineWitnessDef<Self>;
225+
type AssocDef: AssocDef<Self>;
226+
type OpaqueDef: OpaqueDef<Self>;
227+
type Prov: Prov<Self>;
228+
type StaticDef: StaticDef<Self>;
89229
}
90230

91231
pub trait IndexedVal {

compiler/rustc_smir/src/stable_mir/abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl Layout {
119119
}
120120
}
121121

122-
impl crate::rustc_smir::IndexedVal for Layout {
122+
impl stable_mir::IndexedVal for Layout {
123123
fn to_val(index: usize) -> Self {
124124
Layout(index)
125125
}

compiler/rustc_smir/src/stable_mir/alloc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use rustc_abi::Align;
88
use rustc_middle::mir::ConstValue;
99
use rustc_middle::mir::interpret::AllocRange;
10+
use rustc_smir::bridge::SmirError;
1011
use rustc_smir::context::SmirCtxt;
11-
use rustc_smir::{SmirError, Tables, alloc};
12+
use rustc_smir::{Tables, alloc};
1213

1314
use super::Error;
1415
use super::compiler_interface::BridgeTys;

0 commit comments

Comments
 (0)