Skip to content

Commit

Permalink
rustc_metadata: use a macro to deduplicate LazyPerDefTables and PerDe…
Browse files Browse the repository at this point in the history
…fTableBuilders.
  • Loading branch information
eddyb committed Nov 27, 2019
1 parent d7444c1 commit a0556b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 69 deletions.
51 changes: 3 additions & 48 deletions src/librustc_metadata/rmeta/encoder.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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};
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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
Expand Down
65 changes: 44 additions & 21 deletions src/librustc_metadata/rmeta/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use decoder::Metadata;
use table::Table;
use table::{Table, TableBuilder};

use rustc::hir;
use rustc::hir::def::{self, CtorKind};
Expand All @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit a0556b3

Please sign in to comment.