Skip to content

Commit

Permalink
Merge pull request #1198 from hacspec/add-fields-to-defid
Browse files Browse the repository at this point in the history
Frontend/exporter: Add a `kind` field to `DefIdContents`
  • Loading branch information
W95Psp authored Dec 18, 2024
2 parents a598467 + b28e6ac commit ee8e17b
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 42 deletions.
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
pkgs.cargo-release
pkgs.cargo-insta
pkgs.openssl.dev
pkgs.libz.dev
pkgs.pkg-config
pkgs.rust-analyzer
pkgs.toml2json
Expand Down
94 changes: 93 additions & 1 deletion frontend/exporter/src/types/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::prelude::*;
use crate::{AdtInto, JsonSchema};

#[cfg(feature = "rustc")]
use rustc_span::def_id::DefId as RDefId;
use {rustc_hir as hir, rustc_span::def_id::DefId as RDefId};

pub type Symbol = String;

Expand All @@ -29,6 +29,94 @@ impl<'t, S> SInto<S, Symbol> for rustc_span::symbol::Symbol {
}
}

/// Reflects [`hir::Safety`]
#[cfg_attr(not(feature = "extract_names_mode"), derive(AdtInto, JsonSchema))]
#[cfg_attr(not(feature = "extract_names_mode"), args(<S>, from: hir::Safety, state: S as _s))]
#[derive_group(Serializers)]
#[derive(Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Safety {
Unsafe,
Safe,
}

pub type Mutability = bool;

/// Reflects [`hir::def::CtorKind`]
#[derive_group(Serializers)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(not(feature = "extract_names_mode"), derive(JsonSchema, AdtInto))]
#[cfg_attr(not(feature = "extract_names_mode"), args(<S>, from: hir::def::CtorKind, state: S as _s))]
pub enum CtorKind {
Fn,
Const,
}

/// Reflects [`hir::def::CtorOf`]
#[derive_group(Serializers)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(not(feature = "extract_names_mode"), derive(JsonSchema, AdtInto))]
#[cfg_attr(not(feature = "extract_names_mode"), args(<S>, from: hir::def::CtorOf, state: S as _s))]
pub enum CtorOf {
Struct,
Variant,
}

/// Reflects [`rustc_span::hygiene::MacroKind`]
#[derive_group(Serializers)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(not(feature = "extract_names_mode"), derive(JsonSchema, AdtInto))]
#[cfg_attr(not(feature = "extract_names_mode"), args(<S>, from: rustc_span::hygiene::MacroKind, state: S as _s))]
pub enum MacroKind {
Bang,
Attr,
Derive,
}

/// Reflects [`rustc_hir::def::DefKind`]
#[derive_group(Serializers)]
#[cfg_attr(not(feature = "extract_names_mode"), derive(JsonSchema, AdtInto))]
#[cfg_attr(not(feature = "extract_names_mode"),args(<S>, from: rustc_hir::def::DefKind, state: S as tcx))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum DefKind {
Mod,
Struct,
Union,
Enum,
Variant,
Trait,
TyAlias,
ForeignTy,
TraitAlias,
AssocTy,
TyParam,
Fn,
Const,
ConstParam,
Static {
safety: Safety,
mutability: Mutability,
nested: bool,
},
Ctor(CtorOf, CtorKind),
AssocFn,
AssocConst,
Macro(MacroKind),
ExternCrate,
Use,
ForeignMod,
AnonConst,
InlineConst,
OpaqueTy,
Field,
LifetimeParam,
GlobalAsm,
Impl {
of_trait: bool,
},
Closure,
SyntheticCoroutineBody,
}

/// Reflects [`rustc_hir::def_id::DefId`]
#[derive_group(Serializers)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -54,6 +142,9 @@ pub struct DefIdContents {
/// indexes unless you cannot do otherwise.
pub index: (u32, u32),
pub is_local: bool,

/// The kind of definition this `DefId` points to.
pub kind: crate::DefKind,
}

#[cfg(feature = "rustc")]
Expand Down Expand Up @@ -140,6 +231,7 @@ pub(crate) fn translate_def_id<'tcx, S: BaseState<'tcx>>(s: &S, def_id: RDefId)
rustc_hir::def_id::DefIndex::as_u32(def_id.index),
),
is_local: def_id.is_local(),
kind: tcx.def_kind(def_id).sinto(s),
};
let contents =
s.with_global_cache(|cache| id_table::Node::new(contents, &mut cache.id_table_session));
Expand Down
31 changes: 0 additions & 31 deletions frontend/exporter/src/types/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ pub enum Movability {
Movable,
}

pub type Mutability = bool;

#[cfg(feature = "rustc")]
impl<S> SInto<S, Mutability> for hir::Mutability {
fn sinto(&self, _s: &S) -> Mutability {
Expand All @@ -60,24 +58,6 @@ impl<S> SInto<S, Mutability> for hir::Mutability {
}
}

/// Reflects [`hir::def::CtorKind`]
#[derive_group(Serializers)]
#[derive(AdtInto, Clone, Debug, JsonSchema)]
#[args(<S>, from: hir::def::CtorKind, state: S as _s)]
pub enum CtorKind {
Fn,
Const,
}

/// Reflects [`hir::def::CtorOf`]
#[derive_group(Serializers)]
#[derive(AdtInto, Clone, Debug, JsonSchema)]
#[args(<S>, from: hir::def::CtorOf, state: S as _s)]
pub enum CtorOf {
Struct,
Variant,
}

/// Reflects [`hir::RangeEnd`]
#[derive(AdtInto)]
#[args(<S>, from: hir::RangeEnd, state: S as _s)]
Expand All @@ -88,16 +68,6 @@ pub enum RangeEnd {
Excluded,
}

/// Reflects [`hir::Safety`]
#[derive(AdtInto)]
#[args(<S>, from: hir::Safety, state: S as _s)]
#[derive_group(Serializers)]
#[derive(Clone, Debug, JsonSchema, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Safety {
Unsafe,
Safe,
}

/// Reflects [`hir::ImplicitSelfKind`]
#[derive(AdtInto)]
#[args(<S>, from: hir::ImplicitSelfKind, state: S as _s)]
Expand Down Expand Up @@ -1177,7 +1147,6 @@ pub enum AttrKind {
DocComment(CommentKind, Symbol),
}

sinto_todo!(rustc_hir::def, DefKind);
sinto_todo!(rustc_hir, GenericArgs<'a> as HirGenericArgs);
sinto_todo!(rustc_hir, InlineAsm<'a>);
sinto_todo!(rustc_hir, MissingLifetimeKind);
Expand Down
10 changes: 0 additions & 10 deletions frontend/exporter/src/types/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,6 @@ pub enum AstPass {
ProcMacroHarness,
}

/// Reflects [`rustc_span::hygiene::MacroKind`]
#[derive_group(Serializers)]
#[derive(AdtInto, Clone, Debug, JsonSchema)]
#[args(<S>, from: rustc_span::hygiene::MacroKind, state: S as _s)]
pub enum MacroKind {
Bang,
Attr,
Derive,
}

/// Reflects [`rustc_span::hygiene::ExpnKind`]
#[derive(AdtInto)]
#[args(<'tcx, S: BaseState<'tcx>>, from: rustc_span::hygiene::ExpnKind, state: S as gstate)]
Expand Down

0 comments on commit ee8e17b

Please sign in to comment.