Skip to content
Permalink
Browse files

rustc_metadata: use a macro to deduplicate LazyPerDefTables and PerDe…

…fTableBuilders.
  • Loading branch information
eddyb committed Nov 14, 2019
1 parent d7444c1 commit a0556b3b79328c598a4f1539bbe81ceebc4a5b59
Showing with 47 additions and 69 deletions.
  1. +3 −48 src/librustc_metadata/rmeta/encoder.rs
  2. +44 −21 src/librustc_metadata/rmeta/mod.rs
@@ -1,5 +1,5 @@
use crate::rmeta::*;
use crate::rmeta::table::{FixedSizeEncoding, TableBuilder};
use crate::rmeta::table::FixedSizeEncoding;

use rustc::middle::cstore::{LinkagePreference, NativeLibrary,
EncodedMetadata, ForeignModule};
@@ -8,7 +8,7 @@ use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefIndex, DefId, LocalDefId,
use rustc::hir::{GenericParamKind, AnonConst};
use rustc::hir::map::definitions::DefPathTable;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_index::vec::{Idx, IndexVec};
use rustc_index::vec::Idx;
use rustc::middle::dependency_format::Linkage;
use rustc::middle::exported_symbols::{ExportedSymbol, SymbolExportLevel,
metadata_symbol_name};
@@ -60,30 +60,6 @@ struct EncodeContext<'tcx> {
source_file_cache: Lrc<SourceFile>,
}

#[derive(Default)]
struct PerDefTableBuilders<'tcx> {
kind: TableBuilder<DefIndex, Lazy<EntryKind<'tcx>>>,
visibility: TableBuilder<DefIndex, Lazy<ty::Visibility>>,
span: TableBuilder<DefIndex, Lazy<Span>>,
attributes: TableBuilder<DefIndex, Lazy<[ast::Attribute]>>,
children: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
stability: TableBuilder<DefIndex, Lazy<attr::Stability>>,
deprecation: TableBuilder<DefIndex, Lazy<attr::Deprecation>>,

ty: TableBuilder<DefIndex, Lazy<Ty<'tcx>>>,
fn_sig: TableBuilder<DefIndex, Lazy<ty::PolyFnSig<'tcx>>>,
impl_trait_ref: TableBuilder<DefIndex, Lazy<ty::TraitRef<'tcx>>>,
inherent_impls: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
variances: TableBuilder<DefIndex, Lazy<[ty::Variance]>>,
generics: TableBuilder<DefIndex, Lazy<ty::Generics>>,
explicit_predicates: TableBuilder<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
inferred_outlives: TableBuilder<DefIndex, Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
super_predicates: TableBuilder<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,

mir: TableBuilder<DefIndex, Lazy<mir::Body<'tcx>>>,
promoted_mir: TableBuilder<DefIndex, Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
}

macro_rules! encoder_methods {
($($name:ident($ty:ty);)*) => {
$(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
@@ -509,28 +485,7 @@ impl<'tcx> EncodeContext<'tcx> {


i = self.position();
let per_def = LazyPerDefTables {
kind: self.per_def.kind.encode(&mut self.opaque),
visibility: self.per_def.visibility.encode(&mut self.opaque),
span: self.per_def.span.encode(&mut self.opaque),
attributes: self.per_def.attributes.encode(&mut self.opaque),
children: self.per_def.children.encode(&mut self.opaque),
stability: self.per_def.stability.encode(&mut self.opaque),
deprecation: self.per_def.deprecation.encode(&mut self.opaque),

ty: self.per_def.ty.encode(&mut self.opaque),
fn_sig: self.per_def.fn_sig.encode(&mut self.opaque),
impl_trait_ref: self.per_def.impl_trait_ref.encode(&mut self.opaque),
inherent_impls: self.per_def.inherent_impls.encode(&mut self.opaque),
variances: self.per_def.variances.encode(&mut self.opaque),
generics: self.per_def.generics.encode(&mut self.opaque),
explicit_predicates: self.per_def.explicit_predicates.encode(&mut self.opaque),
inferred_outlives: self.per_def.inferred_outlives.encode(&mut self.opaque),
super_predicates: self.per_def.super_predicates.encode(&mut self.opaque),

mir: self.per_def.mir.encode(&mut self.opaque),
promoted_mir: self.per_def.promoted_mir.encode(&mut self.opaque),
};
let per_def = self.per_def.encode(&mut self.opaque);
let per_def_bytes = self.position() - i;

// Encode the proc macro data
@@ -1,5 +1,5 @@
use decoder::Metadata;
use table::Table;
use table::{Table, TableBuilder};

use rustc::hir;
use rustc::hir::def::{self, CtorKind};
@@ -15,6 +15,7 @@ use rustc_target::spec::{PanicStrategy, TargetTriple};
use rustc_index::vec::IndexVec;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef;
use rustc_serialize::opaque::Encoder;
use syntax::{ast, attr};
use syntax::edition::Edition;
use syntax::symbol::Symbol;
@@ -230,31 +231,53 @@ crate struct TraitImpls {
impls: Lazy<[DefIndex]>,
}

#[derive(RustcEncodable, RustcDecodable)]
crate struct LazyPerDefTables<'tcx> {
kind: Lazy!(Table<DefIndex, Lazy!(EntryKind<'tcx>)>),
visibility: Lazy!(Table<DefIndex, Lazy<ty::Visibility>>),
span: Lazy!(Table<DefIndex, Lazy<Span>>),
attributes: Lazy!(Table<DefIndex, Lazy<[ast::Attribute]>>),
children: Lazy!(Table<DefIndex, Lazy<[DefIndex]>>),
stability: Lazy!(Table<DefIndex, Lazy<attr::Stability>>),
deprecation: Lazy!(Table<DefIndex, Lazy<attr::Deprecation>>),
ty: Lazy!(Table<DefIndex, Lazy!(Ty<'tcx>)>),
fn_sig: Lazy!(Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>),
impl_trait_ref: Lazy!(Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>),
inherent_impls: Lazy!(Table<DefIndex, Lazy<[DefIndex]>>),
variances: Lazy!(Table<DefIndex, Lazy<[ty::Variance]>>),
generics: Lazy!(Table<DefIndex, Lazy<ty::Generics>>),
explicit_predicates: Lazy!(Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>),
/// Define `LazyPerDefTables` and `PerDefTableBuilders` at the same time.
macro_rules! define_per_def_tables {
($($name:ident: Table<DefIndex, $T:ty>),+ $(,)?) => {
#[derive(RustcEncodable, RustcDecodable)]
crate struct LazyPerDefTables<'tcx> {
$($name: Lazy!(Table<DefIndex, $T>)),+
}

#[derive(Default)]
struct PerDefTableBuilders<'tcx> {
$($name: TableBuilder<DefIndex, $T>),+
}

impl PerDefTableBuilders<'tcx> {
fn encode(&self, buf: &mut Encoder) -> LazyPerDefTables<'tcx> {
LazyPerDefTables {
$($name: self.$name.encode(buf)),+
}
}
}
}
}

define_per_def_tables! {
kind: Table<DefIndex, Lazy!(EntryKind<'tcx>)>,
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
span: Table<DefIndex, Lazy<Span>>,
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
children: Table<DefIndex, Lazy<[DefIndex]>>,
stability: Table<DefIndex, Lazy<attr::Stability>>,
deprecation: Table<DefIndex, Lazy<attr::Deprecation>>,
ty: Table<DefIndex, Lazy!(Ty<'tcx>)>,
fn_sig: Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>,
impl_trait_ref: Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>,
inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
variances: Table<DefIndex, Lazy<[ty::Variance]>>,
generics: Table<DefIndex, Lazy<ty::Generics>>,
explicit_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
// FIXME(eddyb) this would ideally be `Lazy<[...]>` but `ty::Predicate`
// doesn't handle shorthands in its own (de)serialization impls,
// as it's an `enum` for which we want to derive (de)serialization,
// so the `ty::codec` APIs handle the whole `&'tcx [...]` at once.
// Also, as an optimization, a missing entry indicates an empty `&[]`.
inferred_outlives: Lazy!(Table<DefIndex, Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>),
super_predicates: Lazy!(Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>),
mir: Lazy!(Table<DefIndex, Lazy!(mir::Body<'tcx>)>),
promoted_mir: Lazy!(Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>),
inferred_outlives: Table<DefIndex, Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>,
super_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
}

#[derive(Copy, Clone, RustcEncodable, RustcDecodable)]

0 comments on commit a0556b3

Please sign in to comment.
You can’t perform that action at this time.