Skip to content

Commit a0556b3

Browse files
committed
rustc_metadata: use a macro to deduplicate LazyPerDefTables and PerDefTableBuilders.
1 parent d7444c1 commit a0556b3

File tree

2 files changed

+47
-69
lines changed

2 files changed

+47
-69
lines changed

src/librustc_metadata/rmeta/encoder.rs

+3-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::rmeta::*;
2-
use crate::rmeta::table::{FixedSizeEncoding, TableBuilder};
2+
use crate::rmeta::table::FixedSizeEncoding;
33

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

63-
#[derive(Default)]
64-
struct PerDefTableBuilders<'tcx> {
65-
kind: TableBuilder<DefIndex, Lazy<EntryKind<'tcx>>>,
66-
visibility: TableBuilder<DefIndex, Lazy<ty::Visibility>>,
67-
span: TableBuilder<DefIndex, Lazy<Span>>,
68-
attributes: TableBuilder<DefIndex, Lazy<[ast::Attribute]>>,
69-
children: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
70-
stability: TableBuilder<DefIndex, Lazy<attr::Stability>>,
71-
deprecation: TableBuilder<DefIndex, Lazy<attr::Deprecation>>,
72-
73-
ty: TableBuilder<DefIndex, Lazy<Ty<'tcx>>>,
74-
fn_sig: TableBuilder<DefIndex, Lazy<ty::PolyFnSig<'tcx>>>,
75-
impl_trait_ref: TableBuilder<DefIndex, Lazy<ty::TraitRef<'tcx>>>,
76-
inherent_impls: TableBuilder<DefIndex, Lazy<[DefIndex]>>,
77-
variances: TableBuilder<DefIndex, Lazy<[ty::Variance]>>,
78-
generics: TableBuilder<DefIndex, Lazy<ty::Generics>>,
79-
explicit_predicates: TableBuilder<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
80-
inferred_outlives: TableBuilder<DefIndex, Lazy<&'tcx [(ty::Predicate<'tcx>, Span)]>>,
81-
super_predicates: TableBuilder<DefIndex, Lazy<ty::GenericPredicates<'tcx>>>,
82-
83-
mir: TableBuilder<DefIndex, Lazy<mir::Body<'tcx>>>,
84-
promoted_mir: TableBuilder<DefIndex, Lazy<IndexVec<mir::Promoted, mir::Body<'tcx>>>>,
85-
}
86-
8763
macro_rules! encoder_methods {
8864
($($name:ident($ty:ty);)*) => {
8965
$(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
@@ -509,28 +485,7 @@ impl<'tcx> EncodeContext<'tcx> {
509485

510486

511487
i = self.position();
512-
let per_def = LazyPerDefTables {
513-
kind: self.per_def.kind.encode(&mut self.opaque),
514-
visibility: self.per_def.visibility.encode(&mut self.opaque),
515-
span: self.per_def.span.encode(&mut self.opaque),
516-
attributes: self.per_def.attributes.encode(&mut self.opaque),
517-
children: self.per_def.children.encode(&mut self.opaque),
518-
stability: self.per_def.stability.encode(&mut self.opaque),
519-
deprecation: self.per_def.deprecation.encode(&mut self.opaque),
520-
521-
ty: self.per_def.ty.encode(&mut self.opaque),
522-
fn_sig: self.per_def.fn_sig.encode(&mut self.opaque),
523-
impl_trait_ref: self.per_def.impl_trait_ref.encode(&mut self.opaque),
524-
inherent_impls: self.per_def.inherent_impls.encode(&mut self.opaque),
525-
variances: self.per_def.variances.encode(&mut self.opaque),
526-
generics: self.per_def.generics.encode(&mut self.opaque),
527-
explicit_predicates: self.per_def.explicit_predicates.encode(&mut self.opaque),
528-
inferred_outlives: self.per_def.inferred_outlives.encode(&mut self.opaque),
529-
super_predicates: self.per_def.super_predicates.encode(&mut self.opaque),
530-
531-
mir: self.per_def.mir.encode(&mut self.opaque),
532-
promoted_mir: self.per_def.promoted_mir.encode(&mut self.opaque),
533-
};
488+
let per_def = self.per_def.encode(&mut self.opaque);
534489
let per_def_bytes = self.position() - i;
535490

536491
// Encode the proc macro data

src/librustc_metadata/rmeta/mod.rs

+44-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use decoder::Metadata;
2-
use table::Table;
2+
use table::{Table, TableBuilder};
33

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

233-
#[derive(RustcEncodable, RustcDecodable)]
234-
crate struct LazyPerDefTables<'tcx> {
235-
kind: Lazy!(Table<DefIndex, Lazy!(EntryKind<'tcx>)>),
236-
visibility: Lazy!(Table<DefIndex, Lazy<ty::Visibility>>),
237-
span: Lazy!(Table<DefIndex, Lazy<Span>>),
238-
attributes: Lazy!(Table<DefIndex, Lazy<[ast::Attribute]>>),
239-
children: Lazy!(Table<DefIndex, Lazy<[DefIndex]>>),
240-
stability: Lazy!(Table<DefIndex, Lazy<attr::Stability>>),
241-
deprecation: Lazy!(Table<DefIndex, Lazy<attr::Deprecation>>),
242-
ty: Lazy!(Table<DefIndex, Lazy!(Ty<'tcx>)>),
243-
fn_sig: Lazy!(Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>),
244-
impl_trait_ref: Lazy!(Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>),
245-
inherent_impls: Lazy!(Table<DefIndex, Lazy<[DefIndex]>>),
246-
variances: Lazy!(Table<DefIndex, Lazy<[ty::Variance]>>),
247-
generics: Lazy!(Table<DefIndex, Lazy<ty::Generics>>),
248-
explicit_predicates: Lazy!(Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>),
234+
/// Define `LazyPerDefTables` and `PerDefTableBuilders` at the same time.
235+
macro_rules! define_per_def_tables {
236+
($($name:ident: Table<DefIndex, $T:ty>),+ $(,)?) => {
237+
#[derive(RustcEncodable, RustcDecodable)]
238+
crate struct LazyPerDefTables<'tcx> {
239+
$($name: Lazy!(Table<DefIndex, $T>)),+
240+
}
241+
242+
#[derive(Default)]
243+
struct PerDefTableBuilders<'tcx> {
244+
$($name: TableBuilder<DefIndex, $T>),+
245+
}
246+
247+
impl PerDefTableBuilders<'tcx> {
248+
fn encode(&self, buf: &mut Encoder) -> LazyPerDefTables<'tcx> {
249+
LazyPerDefTables {
250+
$($name: self.$name.encode(buf)),+
251+
}
252+
}
253+
}
254+
}
255+
}
256+
257+
define_per_def_tables! {
258+
kind: Table<DefIndex, Lazy!(EntryKind<'tcx>)>,
259+
visibility: Table<DefIndex, Lazy<ty::Visibility>>,
260+
span: Table<DefIndex, Lazy<Span>>,
261+
attributes: Table<DefIndex, Lazy<[ast::Attribute]>>,
262+
children: Table<DefIndex, Lazy<[DefIndex]>>,
263+
stability: Table<DefIndex, Lazy<attr::Stability>>,
264+
deprecation: Table<DefIndex, Lazy<attr::Deprecation>>,
265+
ty: Table<DefIndex, Lazy!(Ty<'tcx>)>,
266+
fn_sig: Table<DefIndex, Lazy!(ty::PolyFnSig<'tcx>)>,
267+
impl_trait_ref: Table<DefIndex, Lazy!(ty::TraitRef<'tcx>)>,
268+
inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
269+
variances: Table<DefIndex, Lazy<[ty::Variance]>>,
270+
generics: Table<DefIndex, Lazy<ty::Generics>>,
271+
explicit_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
249272
// FIXME(eddyb) this would ideally be `Lazy<[...]>` but `ty::Predicate`
250273
// doesn't handle shorthands in its own (de)serialization impls,
251274
// as it's an `enum` for which we want to derive (de)serialization,
252275
// so the `ty::codec` APIs handle the whole `&'tcx [...]` at once.
253276
// Also, as an optimization, a missing entry indicates an empty `&[]`.
254-
inferred_outlives: Lazy!(Table<DefIndex, Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>),
255-
super_predicates: Lazy!(Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>),
256-
mir: Lazy!(Table<DefIndex, Lazy!(mir::Body<'tcx>)>),
257-
promoted_mir: Lazy!(Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>),
277+
inferred_outlives: Table<DefIndex, Lazy!(&'tcx [(ty::Predicate<'tcx>, Span)])>,
278+
super_predicates: Table<DefIndex, Lazy!(ty::GenericPredicates<'tcx>)>,
279+
mir: Table<DefIndex, Lazy!(mir::Body<'tcx>)>,
280+
promoted_mir: Table<DefIndex, Lazy!(IndexVec<mir::Promoted, mir::Body<'tcx>>)>,
258281
}
259282

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

0 commit comments

Comments
 (0)