Skip to content

Commit 7062164

Browse files
committed
Added interpreter mode to compiler interface, interpreter parsing functionality
1 parent 22bc9e1 commit 7062164

File tree

31 files changed

+335
-114
lines changed

31 files changed

+335
-114
lines changed

src/librustc/arena.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ macro_rules! arena_types {
2323
[] generics: rustc::ty::Generics,
2424
[] trait_def: rustc::ty::TraitDef,
2525
[] adt_def: rustc::ty::AdtDef,
26-
[] steal_mir: rustc::ty::steal::Steal<rustc::mir::Body<$tcx>>,
26+
[] steal_mir: rustc_data_structures::steal::Steal<rustc::mir::Body<$tcx>>,
2727
[] mir: rustc::mir::Body<$tcx>,
28-
[] steal_promoted: rustc::ty::steal::Steal<
28+
[] steal_promoted: rustc_data_structures::steal::Steal<
2929
rustc_index::vec::IndexVec<
3030
rustc::mir::Promoted,
3131
rustc::mir::Body<$tcx>

src/librustc/hir/lowering.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,7 @@ impl<'a> LoweringContext<'a> {
20772077
span: l.span,
20782078
attrs: l.attrs.clone(),
20792079
source: hir::LocalSource::Normal,
2080+
interp_tag: l.interp_tag.clone(),
20802081
}, ids)
20812082
}
20822083

@@ -3046,6 +3047,7 @@ impl<'a> LoweringContext<'a> {
30463047
source,
30473048
span,
30483049
ty: None,
3050+
interp_tag: None,
30493051
};
30503052
self.stmt(span, hir::StmtKind::Local(P(local)))
30513053
}

src/librustc/hir/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,6 +1273,8 @@ pub struct Local {
12731273
/// Can be `ForLoopDesugar` if the `let` statement is part of a `for` loop
12741274
/// desugaring. Otherwise will be `Normal`.
12751275
pub source: LocalSource,
1276+
/// See comment on `syntax::ast::Local`.
1277+
pub interp_tag: Option<ast::LocalInterpTag>,
12761278
}
12771279

12781280
/// Represents a single arm of a `match` expression, e.g.

src/librustc/ich/impls_hir.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,14 @@ impl<'hir> HashStable<StableHashingContext<'hir>> for attr::OptimizeAttr {
392392
mem::discriminant(self).hash_stable(hcx, hasher);
393393
}
394394
}
395+
396+
impl_stable_hash_for!(enum ast::LocalInterpState {
397+
Uninitialized,
398+
Set,
399+
Moved,
400+
});
401+
402+
impl_stable_hash_for!(struct ast::LocalInterpTag {
403+
id,
404+
state,
405+
});

src/librustc/ich/impls_ty.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! This module contains `HashStable` implementations for various data types
2-
//! from rustc::ty in no particular order.
2+
//! from `rustc::ty` in no particular order.
33
44
use crate::ich::{Fingerprint, StableHashingContext, NodeIdHashingMode};
5+
use crate::middle::region;
6+
use crate::mir;
7+
use crate::ty;
8+
59
use rustc_data_structures::fx::FxHashMap;
610
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher};
711
use std::cell::RefCell;
812
use std::mem;
9-
use crate::middle::region;
10-
use crate::ty;
11-
use crate::mir;
1213

1314
impl<'a, 'tcx, T> HashStable<StableHashingContext<'a>> for &'tcx ty::List<T>
1415
where
@@ -197,15 +198,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for ty::FloatVid {
197198
}
198199
}
199200

200-
impl<'a, T> HashStable<StableHashingContext<'a>> for ty::steal::Steal<T>
201-
where
202-
T: HashStable<StableHashingContext<'a>>,
203-
{
204-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
205-
self.borrow().hash_stable(hcx, hasher);
206-
}
207-
}
208-
209201
impl<'a> HashStable<StableHashingContext<'a>>
210202
for crate::middle::privacy::AccessLevels {
211203
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {

src/librustc/query/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,8 +1120,7 @@ rustc_queries! {
11201120
}
11211121

11221122
// Get an estimate of the size of an InstanceDef based on its MIR for CGU partitioning.
1123-
query instance_def_size_estimate(def: ty::InstanceDef<'tcx>)
1124-
-> usize {
1123+
query instance_def_size_estimate(def: ty::InstanceDef<'tcx>) -> usize {
11251124
no_force
11261125
desc { |tcx| "estimating size for `{}`", tcx.def_path_str(def.def_id()) }
11271126
}
@@ -1130,5 +1129,10 @@ rustc_queries! {
11301129
eval_always
11311130
desc { "looking up enabled feature gates" }
11321131
}
1132+
1133+
query interp_user_fn(_: CrateNum) -> DefId {
1134+
eval_always
1135+
desc { "locating interpreter user fn in HIR" }
1136+
}
11331137
}
11341138
}

src/librustc/session/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ top_level_options!(
391391
// can influence whether overflow checks are done or not.
392392
debug_assertions: bool [TRACKED],
393393
debuginfo: DebugInfo [TRACKED],
394+
interp_mode: bool [TRACKED],
394395
lint_opts: Vec<(String, lint::Level)> [TRACKED],
395396
lint_cap: Option<lint::Level> [TRACKED],
396397
describe_lints: bool [UNTRACKED],
@@ -599,6 +600,7 @@ impl Default for Options {
599600
crate_types: Vec::new(),
600601
optimize: OptLevel::No,
601602
debuginfo: DebugInfo::None,
603+
interp_mode: false,
602604
lint_opts: Vec::new(),
603605
lint_cap: None,
604606
describe_lints: false,
@@ -2467,6 +2469,7 @@ pub fn build_session_options_and_crate_config(
24672469
crate_types,
24682470
optimize: opt_level,
24692471
debuginfo,
2472+
interp_mode: false,
24702473
lint_opts,
24712474
lint_cap,
24722475
describe_lints,

src/librustc/ty/context.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ use crate::ty::{InferConst, ParamConst};
3838
use crate::ty::GenericParamDefKind;
3939
use crate::ty::layout::{LayoutDetails, TargetDataLayout, VariantIdx};
4040
use crate::ty::query;
41-
use crate::ty::steal::Steal;
4241
use crate::ty::subst::{UserSubsts, GenericArgKind};
4342
use crate::ty::{BoundVar, BindingMode};
4443
use crate::ty::CanonicalPolyFnSig;
@@ -49,12 +48,13 @@ use crate::util::nodemap::{FxHashMap, FxHashSet};
4948
use errors::DiagnosticBuilder;
5049
use arena::SyncDroplessArena;
5150
use smallvec::SmallVec;
51+
use rustc_index::vec::{Idx, IndexVec};
5252
use rustc_data_structures::stable_hasher::{
5353
HashStable, StableHasher, StableVec, hash_stable_hashmap,
5454
};
55-
use rustc_index::vec::{Idx, IndexVec};
5655
use rustc_data_structures::sharded::ShardedHashMap;
5756
use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal};
57+
use rustc_data_structures::steal::Steal;
5858
use std::any::Any;
5959
use std::borrow::Borrow;
6060
use std::cmp::Ordering;
@@ -2859,6 +2859,44 @@ fn ptr_eq<T, U>(t: *const T, u: *const U) -> bool {
28592859
t as *const () == u as *const ()
28602860
}
28612861

2862+
/// In interpreter mode, locates the `DefId` of the user fn (a closure marked by an attribute named
2863+
/// `rustc_interp_user_fn`) by visiting the local crate's HIR.
2864+
fn find_interp_user_fn(tcx: TyCtxt<'_>) -> DefId {
2865+
use hir::intravisit::{self, Visitor, NestedVisitorMap};
2866+
2867+
struct InterpUserFnVisitor<'tcx> {
2868+
tcx: TyCtxt<'tcx>,
2869+
def_id: Option<DefId>,
2870+
}
2871+
2872+
impl<'tcx> Visitor<'tcx> for InterpUserFnVisitor<'tcx> {
2873+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
2874+
NestedVisitorMap::OnlyBodies(&self.tcx.hir())
2875+
}
2876+
2877+
fn visit_expr(&mut self, ex: &'tcx hir::Expr) {
2878+
if syntax::attr::contains_name(&ex.attrs, sym::rustc_interp_user_fn) {
2879+
self.def_id = Some(self.tcx.hir().local_def_id(ex.hir_id));
2880+
return;
2881+
}
2882+
2883+
intravisit::walk_expr(self, ex);
2884+
}
2885+
}
2886+
2887+
let mut visitor = InterpUserFnVisitor {
2888+
tcx,
2889+
def_id: None,
2890+
};
2891+
tcx.hir().krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
2892+
visitor.def_id
2893+
.unwrap_or_else(|| tcx.sess.fatal(&format!(
2894+
"could not find interpreter user fn in HIR; it should be a closure expression \
2895+
marked with the `#[{}]` attribute",
2896+
sym::rustc_interp_user_fn
2897+
)))
2898+
}
2899+
28622900
pub fn provide(providers: &mut ty::query::Providers<'_>) {
28632901
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id);
28642902
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).map(|v| &v[..]);
@@ -2936,4 +2974,8 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
29362974
assert_eq!(cnum, LOCAL_CRATE);
29372975
attr::contains_name(tcx.hir().krate_attrs(), sym::compiler_builtins)
29382976
};
2977+
providers.interp_user_fn = |tcx, cnum| {
2978+
assert_eq!(cnum, LOCAL_CRATE);
2979+
find_interp_user_fn(tcx)
2980+
};
29392981
}

src/librustc/ty/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ pub mod outlives;
105105
pub mod print;
106106
pub mod query;
107107
pub mod relate;
108-
pub mod steal;
109108
pub mod subst;
110109
pub mod trait_def;
111110
pub mod walk;

src/librustc/ty/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::traits::query::outlives_bounds::OutlivesBound;
3434
use crate::traits::specialization_graph;
3535
use crate::traits::Clauses;
3636
use crate::ty::{self, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt, AdtSizedConstraint};
37-
use crate::ty::steal::Steal;
37+
use rustc_data_structures::steal::Steal;
3838
use crate::ty::util::NeedsDrop;
3939
use crate::ty::subst::SubstsRef;
4040
use crate::util::nodemap::{DefIdSet, DefIdMap, ItemLocalSet};

0 commit comments

Comments
 (0)