Skip to content

Commit d5afea5

Browse files
committed
Add CrateDef trait and methods to get def names
1 parent c07a6d5 commit d5afea5

File tree

10 files changed

+285
-51
lines changed

10 files changed

+285
-51
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_hir::def::DefKind;
1414
use rustc_middle::mir;
1515
use rustc_middle::mir::interpret::{alloc_range, AllocId};
1616
use rustc_middle::mir::mono::MonoItem;
17+
use rustc_middle::ty::print::{with_forced_trimmed_paths, with_no_trimmed_paths};
1718
use rustc_middle::ty::{self, Instance, ParamEnv, ScalarInt, Ty, TyCtxt, Variance};
1819
use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE};
1920
use rustc_target::abi::FieldIdx;
@@ -28,7 +29,7 @@ use stable_mir::ty::{
2829
EarlyParamRegion, FloatTy, FnDef, GenericArgs, GenericParamDef, IntTy, LineInfo, Movability,
2930
RigidTy, Span, TyKind, UintTy,
3031
};
31-
use stable_mir::{self, opaque, Context, CrateItem, Error, Filename, ItemKind};
32+
use stable_mir::{self, opaque, Context, Crate, CrateItem, Error, Filename, ItemKind, Symbol};
3233
use std::cell::RefCell;
3334
use tracing::debug;
3435

@@ -61,9 +62,18 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
6162
crates
6263
}
6364

64-
fn name_of_def_id(&self, def_id: stable_mir::DefId) -> String {
65+
fn def_name(&self, def_id: stable_mir::DefId, trimmed: bool) -> Symbol {
6566
let tables = self.0.borrow();
66-
tables.tcx.def_path_str(tables[def_id])
67+
if trimmed {
68+
with_forced_trimmed_paths!(tables.tcx.def_path_str(tables[def_id]))
69+
} else {
70+
with_no_trimmed_paths!(tables.tcx.def_path_str(tables[def_id]))
71+
}
72+
}
73+
74+
fn krate(&self, def_id: stable_mir::DefId) -> Crate {
75+
let tables = self.0.borrow();
76+
smir_crate(tables.tcx, tables[def_id].krate)
6777
}
6878

6979
fn span_to_string(&self, span: stable_mir::ty::Span) -> String {
@@ -240,12 +250,26 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
240250
tables.create_def_id(def_id)
241251
}
242252

243-
fn instance_mangled_name(&self, def: InstanceDef) -> String {
253+
fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol {
244254
let tables = self.0.borrow_mut();
245-
let instance = tables.instances[def];
255+
let instance = tables.instances[instance];
246256
tables.tcx.symbol_name(instance).name.to_string()
247257
}
248258

259+
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol {
260+
let tables = self.0.borrow_mut();
261+
let instance = tables.instances[def];
262+
if trimmed {
263+
with_forced_trimmed_paths!(
264+
tables.tcx.def_path_str_with_args(instance.def_id(), instance.args)
265+
)
266+
} else {
267+
with_no_trimmed_paths!(
268+
tables.tcx.def_path_str_with_args(instance.def_id(), instance.args)
269+
)
270+
}
271+
}
272+
249273
fn mono_instance(&self, item: stable_mir::CrateItem) -> stable_mir::mir::mono::Instance {
250274
let mut tables = self.0.borrow_mut();
251275
let def_id = tables[item.0];

compiler/stable_mir/src/crate_def.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Module that define a common trait for things that represent a crate definition.
2+
3+
use crate::ty::Span;
4+
use crate::{with, Crate, Symbol};
5+
6+
/// A unique identification number for each item accessible for the current compilation unit.
7+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
8+
pub struct DefId(pub(crate) usize);
9+
10+
/// A trait for retrieving information about a crate definition.
11+
///
12+
/// Implementors must provide the implementation of `def_id` which will be used to retrieve
13+
/// information about its definition.
14+
pub trait CrateDef {
15+
/// Retrieve the unique identifier for the given definition.
16+
fn def_id(&self) -> DefId;
17+
18+
/// Return the fully qualified name of the given definition.
19+
fn name(&self) -> Symbol {
20+
let def_id = self.def_id();
21+
with(|cx| cx.def_name(def_id, false))
22+
}
23+
24+
/// Return a trimmed name of the given definition.
25+
///
26+
/// If a symbol name can only be imported from one place for a type, and as
27+
/// long as it was not glob-imported anywhere in the current crate, we trim its
28+
/// path and print only the name.
29+
///
30+
/// For example, this function may shorten `std::vec::Vec` to just `Vec`,
31+
/// as long as there is no other `Vec` importable anywhere.
32+
fn trimmed_name(&self) -> Symbol {
33+
let def_id = self.def_id();
34+
with(|cx| cx.def_name(def_id, true))
35+
}
36+
37+
/// Return information about the crate where this definition is declared.
38+
///
39+
/// This will return the crate number and its name.
40+
fn krate(&self) -> Crate {
41+
let def_id = self.def_id();
42+
with(|cx| cx.krate(def_id))
43+
}
44+
45+
/// Return the span of this definition.
46+
fn span(&self) -> Span {
47+
let def_id = self.def_id();
48+
with(|cx| cx.span_of_an_item(def_id))
49+
}
50+
}
51+
52+
macro_rules! crate_def {
53+
( $(#[$attr:meta])*
54+
$vis:vis $name:ident $(;)?
55+
) => {
56+
$(#[$attr])*
57+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
58+
$vis struct $name(pub DefId);
59+
60+
impl CrateDef for $name {
61+
fn def_id(&self) -> DefId {
62+
self.0
63+
}
64+
}
65+
};
66+
}

compiler/stable_mir/src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ use self::ty::{
3131
#[macro_use]
3232
extern crate scoped_tls;
3333

34+
#[macro_use]
35+
pub mod crate_def;
3436
#[macro_use]
3537
pub mod error;
3638
pub mod mir;
3739
pub mod ty;
3840
pub mod visitor;
3941

42+
pub use crate::crate_def::CrateDef;
43+
pub use crate::crate_def::DefId;
4044
use crate::mir::alloc::{AllocId, GlobalAlloc};
4145
use crate::mir::pretty::function_name;
4246
use crate::mir::Mutability;
@@ -51,15 +55,11 @@ pub type Symbol = String;
5155
/// The number that identifies a crate.
5256
pub type CrateNum = usize;
5357

54-
/// A unique identification number for each item accessible for the current compilation unit.
55-
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
56-
pub struct DefId(usize);
57-
5858
impl Debug for DefId {
5959
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6060
f.debug_struct("DefId")
6161
.field("id", &self.0)
62-
.field("name", &with(|cx| cx.name_of_def_id(*self)))
62+
.field("name", &with(|cx| cx.def_name(*self, false)))
6363
.finish()
6464
}
6565
}
@@ -100,9 +100,10 @@ pub enum ItemKind {
100100

101101
pub type Filename = String;
102102

103-
/// Holds information about an item in the crate.
104-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
105-
pub struct CrateItem(pub DefId);
103+
crate_def! {
104+
/// Holds information about an item in a crate.
105+
pub CrateItem;
106+
}
106107

107108
impl CrateItem {
108109
pub fn body(&self) -> mir::Body {
@@ -113,10 +114,6 @@ impl CrateItem {
113114
with(|cx| cx.span_of_an_item(self.0))
114115
}
115116

116-
pub fn name(&self) -> String {
117-
with(|cx| cx.name_of_def_id(self.0))
118-
}
119-
120117
pub fn kind(&self) -> ItemKind {
121118
with(|cx| cx.item_kind(*self))
122119
}
@@ -205,7 +202,7 @@ pub trait Context {
205202
fn find_crates(&self, name: &str) -> Vec<Crate>;
206203

207204
/// Returns the name of given `DefId`
208-
fn name_of_def_id(&self, def_id: DefId) -> String;
205+
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
209206

210207
/// Returns printable, human readable form of `Span`
211208
fn span_to_string(&self, span: Span) -> String;
@@ -260,7 +257,7 @@ pub trait Context {
260257
fn instance_def_id(&self, instance: InstanceDef) -> DefId;
261258

262259
/// Get the instance mangled name.
263-
fn instance_mangled_name(&self, instance: InstanceDef) -> String;
260+
fn instance_mangled_name(&self, instance: InstanceDef) -> Symbol;
264261

265262
/// Convert a non-generic crate item into an instance.
266263
/// This function will panic if the item is generic.
@@ -294,6 +291,8 @@ pub trait Context {
294291

295292
/// Retrieve the id for the virtual table.
296293
fn vtable_allocation(&self, global_alloc: &GlobalAlloc) -> Option<AllocId>;
294+
fn krate(&self, def_id: DefId) -> Crate;
295+
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
297296
}
298297

299298
// A thread local variable that stores a pointer to the tables mapping between TyCtxt

compiler/stable_mir/src/mir/mono.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use crate::crate_def::CrateDef;
12
use crate::mir::Body;
23
use crate::ty::{Allocation, ClosureDef, ClosureKind, FnDef, GenericArgs, IndexedVal, Ty};
3-
use crate::{with, CrateItem, DefId, Error, ItemKind, Opaque};
4+
use crate::{with, CrateItem, DefId, Error, ItemKind, Opaque, Symbol};
45
use std::fmt::{Debug, Formatter};
56

67
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
@@ -47,10 +48,23 @@ impl Instance {
4748
with(|context| context.instance_ty(self.def))
4849
}
4950

50-
pub fn mangled_name(&self) -> String {
51+
pub fn mangled_name(&self) -> Symbol {
5152
with(|context| context.instance_mangled_name(self.def))
5253
}
5354

55+
pub fn name(&self) -> Symbol {
56+
with(|context| context.instance_name(self.def, false))
57+
}
58+
59+
/// Return a trimmed name of the given instance including its args.
60+
///
61+
/// If a symbol name can only be imported from one place for a type, and as
62+
/// long as it was not glob-imported anywhere in the current crate, we trim its
63+
/// path and print only the name.
64+
pub fn trimmed_name(&self) -> Symbol {
65+
with(|context| context.instance_name(self.def, true))
66+
}
67+
5468
/// Resolve an instance starting from a function definition and generic arguments.
5569
pub fn resolve(def: FnDef, args: &GenericArgs) -> Result<Instance, crate::Error> {
5670
with(|context| {
@@ -104,6 +118,8 @@ impl TryFrom<CrateItem> for Instance {
104118

105119
fn try_from(item: CrateItem) -> Result<Self, Self::Error> {
106120
with(|context| {
121+
/// FIXME(celinval):
122+
/// - Check `has_body`.
107123
if !context.requires_monomorphization(item.0) {
108124
Ok(context.mono_instance(item))
109125
} else {
@@ -148,8 +164,10 @@ impl From<StaticDef> for CrateItem {
148164
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
149165
pub struct InstanceDef(usize);
150166

151-
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
152-
pub struct StaticDef(pub DefId);
167+
crate_def! {
168+
/// Holds information about a static variable definition.
169+
pub StaticDef;
170+
}
153171

154172
impl TryFrom<CrateItem> for StaticDef {
155173
type Error = crate::Error;

compiler/stable_mir/src/mir/pretty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::crate_def::CrateDef;
12
use crate::mir::{Operand, Rvalue, StatementKind};
23
use crate::ty::{DynKind, FloatTy, IntTy, RigidTy, TyKind, UintTy};
34
use crate::{with, Body, CrateItem, Mutability};

compiler/stable_mir/src/ty.rs

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use super::{
33
mir::{Body, Mutability},
44
with, DefId, Error, Symbol,
55
};
6+
use crate::crate_def::CrateDef;
67
use crate::mir::alloc::AllocId;
78
use crate::{Filename, Opaque};
89
use std::fmt::{self, Debug, Display, Formatter};
@@ -295,32 +296,41 @@ pub enum Movability {
295296
Movable,
296297
}
297298

298-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
299-
pub struct ForeignDef(pub DefId);
299+
crate_def! {
300+
/// Hold information about a ForeignItem in a crate.
301+
pub ForeignDef;
302+
}
300303

301-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
302-
pub struct FnDef(pub DefId);
304+
crate_def! {
305+
/// Hold information about a function definition in a crate.
306+
pub FnDef;
307+
}
303308

304309
impl FnDef {
305310
pub fn body(&self) -> Body {
306311
with(|ctx| ctx.mir_body(self.0))
307312
}
308313
}
309314

310-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
311-
pub struct ClosureDef(pub DefId);
315+
crate_def! {
316+
pub ClosureDef;
317+
}
312318

313-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
314-
pub struct CoroutineDef(pub DefId);
319+
crate_def! {
320+
pub CoroutineDef;
321+
}
315322

316-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
317-
pub struct ParamDef(pub DefId);
323+
crate_def! {
324+
pub ParamDef;
325+
}
318326

319-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
320-
pub struct BrNamedDef(pub DefId);
327+
crate_def! {
328+
pub BrNamedDef;
329+
}
321330

322-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
323-
pub struct AdtDef(pub DefId);
331+
crate_def! {
332+
pub AdtDef;
333+
}
324334

325335
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
326336
pub enum AdtKind {
@@ -363,26 +373,33 @@ impl AdtKind {
363373
}
364374
}
365375

366-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
367-
pub struct AliasDef(pub DefId);
376+
crate_def! {
377+
pub AliasDef;
378+
}
368379

369-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
370-
pub struct TraitDef(pub DefId);
380+
crate_def! {
381+
pub TraitDef;
382+
}
371383

372-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
373-
pub struct GenericDef(pub DefId);
384+
crate_def! {
385+
pub GenericDef;
386+
}
374387

375-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
376-
pub struct ConstDef(pub DefId);
388+
crate_def! {
389+
pub ConstDef;
390+
}
377391

378-
#[derive(Clone, PartialEq, Eq, Debug)]
379-
pub struct ImplDef(pub DefId);
392+
crate_def! {
393+
pub ImplDef;
394+
}
380395

381-
#[derive(Clone, PartialEq, Eq, Debug)]
382-
pub struct RegionDef(pub DefId);
396+
crate_def! {
397+
pub RegionDef;
398+
}
383399

384-
#[derive(Clone, PartialEq, Eq, Debug)]
385-
pub struct CoroutineWitnessDef(pub DefId);
400+
crate_def! {
401+
pub CoroutineWitnessDef;
402+
}
386403

387404
/// A list of generic arguments.
388405
#[derive(Clone, Debug, Eq, PartialEq)]

0 commit comments

Comments
 (0)