Add method to get all attributes on a definition
This commit is contained in:
parent
84071e2662
commit
9387b0bad9
5 changed files with 85 additions and 30 deletions
|
@ -232,7 +232,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
|
|||
&self,
|
||||
def_id: stable_mir::DefId,
|
||||
attr: &[stable_mir::Symbol],
|
||||
) -> Vec<stable_mir::ty::Attribute> {
|
||||
) -> Vec<stable_mir::crate_def::Attribute> {
|
||||
let mut tables = self.0.borrow_mut();
|
||||
let tcx = tables.tcx;
|
||||
let did = tables[def_id];
|
||||
|
@ -242,7 +242,28 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
|
|||
.map(|attribute| {
|
||||
let attr_str = rustc_ast_pretty::pprust::attribute_to_string(attribute);
|
||||
let span = attribute.span;
|
||||
stable_mir::ty::Attribute::new(attr_str, span.stable(&mut *tables))
|
||||
stable_mir::crate_def::Attribute::new(attr_str, span.stable(&mut *tables))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_all_attrs(&self, def_id: stable_mir::DefId) -> Vec<stable_mir::crate_def::Attribute> {
|
||||
let mut tables = self.0.borrow_mut();
|
||||
let tcx = tables.tcx;
|
||||
let did = tables[def_id];
|
||||
let filter_fn = move |a: &&rustc_ast::ast::Attribute| {
|
||||
matches!(a.kind, rustc_ast::ast::AttrKind::Normal(_))
|
||||
};
|
||||
let attrs_iter = if let Some(did) = did.as_local() {
|
||||
tcx.hir().attrs(tcx.local_def_id_to_hir_id(did)).iter().filter(filter_fn)
|
||||
} else {
|
||||
tcx.item_attrs(did).iter().filter(filter_fn)
|
||||
};
|
||||
attrs_iter
|
||||
.map(|attribute| {
|
||||
let attr_str = rustc_ast_pretty::pprust::attribute_to_string(attribute);
|
||||
let span = attribute.span;
|
||||
stable_mir::crate_def::Attribute::new(attr_str, span.stable(&mut *tables))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
|
|
@ -6,12 +6,13 @@
|
|||
use std::cell::Cell;
|
||||
|
||||
use crate::abi::{FnAbi, Layout, LayoutShape};
|
||||
use crate::crate_def::Attribute;
|
||||
use crate::mir::alloc::{AllocId, GlobalAlloc};
|
||||
use crate::mir::mono::{Instance, InstanceDef, StaticDef};
|
||||
use crate::mir::{BinOp, Body, Place, UnOp};
|
||||
use crate::target::MachineInfo;
|
||||
use crate::ty::{
|
||||
AdtDef, AdtKind, Allocation, Attribute, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
|
||||
AdtDef, AdtKind, Allocation, ClosureDef, ClosureKind, FieldDef, FnDef, ForeignDef,
|
||||
ForeignItemKind, ForeignModule, ForeignModuleDef, GenericArgs, GenericPredicates, Generics,
|
||||
ImplDef, ImplTrait, IntrinsicDef, LineInfo, MirConst, PolyFnSig, RigidTy, Span, TraitDecl,
|
||||
TraitDef, Ty, TyConst, TyConstId, TyKind, UintTy, VariantDef,
|
||||
|
@ -55,9 +56,15 @@ pub trait Context {
|
|||
/// Returns the name of given `DefId`
|
||||
fn def_name(&self, def_id: DefId, trimmed: bool) -> Symbol;
|
||||
|
||||
/// Get all attributes with the given attribute name.
|
||||
/// Return attributes with the given attribute name.
|
||||
///
|
||||
/// Single segmented name like `#[inline]` is specified as `&["inline".to_string()]`.
|
||||
/// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
|
||||
fn get_attrs_by_path(&self, def_id: DefId, attr: &[Symbol]) -> Vec<Attribute>;
|
||||
|
||||
/// Get all attributes of a definition.
|
||||
fn get_all_attrs(&self, def_id: DefId) -> Vec<Attribute>;
|
||||
|
||||
/// Returns printable, human readable form of `Span`
|
||||
fn span_to_string(&self, span: Span) -> String;
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
//! Module that define a common trait for things that represent a crate definition,
|
||||
//! such as, a function, a trait, an enum, and any other definitions.
|
||||
|
||||
use crate::ty::{Attribute, GenericArgs, Span, Ty};
|
||||
use crate::ty::{GenericArgs, Span, Ty};
|
||||
use crate::{with, Crate, Symbol};
|
||||
|
||||
/// A unique identification number for each item accessible for the current compilation unit.
|
||||
|
@ -52,10 +52,19 @@ pub trait CrateDef {
|
|||
}
|
||||
|
||||
/// Return attributes with the given attribute name.
|
||||
///
|
||||
/// Single segmented name like `#[inline]` is specified as `&["inline".to_string()]`.
|
||||
/// Multi-segmented name like `#[rustfmt::skip]` is specified as `&["rustfmt".to_string(), "skip".to_string()]`.
|
||||
fn attrs_by_path(&self, attr: &[Symbol]) -> Vec<Attribute> {
|
||||
let def_id = self.def_id();
|
||||
with(|cx| cx.get_attrs_by_path(def_id, attr))
|
||||
}
|
||||
|
||||
/// Return all attributes of this definition.
|
||||
fn all_attrs(&self) -> Vec<Attribute> {
|
||||
let def_id = self.def_id();
|
||||
with(|cx| cx.get_all_attrs(def_id))
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait that can be used to retrieve a definition's type.
|
||||
|
@ -75,6 +84,28 @@ pub trait CrateDefType: CrateDef {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Attribute {
|
||||
value: String,
|
||||
span: Span,
|
||||
}
|
||||
|
||||
impl Attribute {
|
||||
pub fn new(value: String, span: Span) -> Attribute {
|
||||
Attribute { value, span }
|
||||
}
|
||||
|
||||
/// Get the span of this attribute.
|
||||
pub fn span(&self) -> Span {
|
||||
self.span
|
||||
}
|
||||
|
||||
/// Get the string representation of this attribute.
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! crate_def {
|
||||
( $(#[$attr:meta])*
|
||||
$vis:vis $name:ident $(;)?
|
||||
|
|
|
@ -248,28 +248,6 @@ pub struct Placeholder<T> {
|
|||
pub bound: T,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Attribute {
|
||||
value: String,
|
||||
span: Span,
|
||||
}
|
||||
|
||||
impl Attribute {
|
||||
pub fn new(value: String, span: Span) -> Attribute {
|
||||
Attribute { value, span }
|
||||
}
|
||||
|
||||
/// Get the span of this attribute.
|
||||
pub fn span(&self) -> Span {
|
||||
self.span
|
||||
}
|
||||
|
||||
/// Get the string representation of this attribute.
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
pub struct Span(usize);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue