Skip to content

Commit

Permalink
Merge pull request #961 from hash-org/merge-ty-term-enums
Browse files Browse the repository at this point in the history
TIR: Merge `Ty` and `Term` enums
  • Loading branch information
kontheocharis authored Sep 8, 2023
2 parents 766c922 + 0f3fe16 commit dc2adbf
Show file tree
Hide file tree
Showing 52 changed files with 533 additions and 936 deletions.
5 changes: 2 additions & 3 deletions compiler/hash-ast-expand/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ use hash_tir::{
node::{Node, NodeOrigin},
params::ParamIndex,
primitives::primitives,
terms::Term,
tys::{Ty, TyId},
terms::{Term, Ty, TyId},
utils::params::ParamUtils,
};

Expand Down Expand Up @@ -108,7 +107,7 @@ impl AstExpander<'_> {
};

match *param_ty.value() {
Ty::Data(data) => match data.data_def {
Ty::DataTy(data) => match data.data_def {
d if d == primitives().i32() => {
maybe_emit_err(matches!(value, AttrValueKind::Int(_)))
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-ast-expand/src/diagnostics/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use hash_reporting::{
reporter::{Reporter, Reports},
};
use hash_source::identifier::Identifier;
use hash_tir::{tys::TyId, utils::params::ParamError};
use hash_tir::{terms::TyId, utils::params::ParamError};
use hash_utils::derive_more::{Constructor, From};

#[derive(Constructor, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions compiler/hash-attrs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::LazyLock;

use hash_ast_utils::attr::AttrTarget;
use hash_source::identifier::Identifier;
use hash_tir::{building::gen, primitives::primitives, tys::Ty};
use hash_tir::{building::gen, primitives::primitives, terms::Ty};
use paste::paste;

use crate::ty::{AttrId, AttrTy, AttrTyMap};
Expand All @@ -15,7 +15,7 @@ macro_rules! make_ty {
($kind: ident) => {
// ##GeneratedOrigin: these attributes are generated, so they do not
// have an origin.
Ty::data(primitives().$kind(), hash_tir::node::NodeOrigin::Generated)
Ty::data_ty(primitives().$kind(), hash_tir::node::NodeOrigin::Generated)
};
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-exhaustiveness/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! used within the exhaustiveness sub-system to represent these values within
//! a single data type.
use hash_source::constant::InternedInt;
use hash_tir::tys::TyId;
use hash_tir::terms::TyId;

#[derive(Debug, Clone, Copy)]
pub struct Constant {
Expand Down
6 changes: 3 additions & 3 deletions compiler/hash-exhaustiveness/src/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use hash_storage::store::{statics::StoreId, SequenceStoreKey, Store};
use hash_tir::{
data::{CtorDefId, DataTy},
node::NodesId,
terms::Ty,
tuples::TupleTy,
tys::Ty,
};
use hash_utils::smallvec::{smallvec, SmallVec};

Expand Down Expand Up @@ -111,7 +111,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
// if it is a struct or enum, then we get that variant and
// we can count the fields from that variant or struct.
match *ctx.ty.value() {
Ty::Data(DataTy { data_def, .. }) => {
Ty::DataTy(DataTy { data_def, .. }) => {
// We need to extract the variant index from the constructor
let variant_idx = match ctor {
DeconstructedCtor::Single => 0,
Expand All @@ -122,7 +122,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
let ctor_id = data_def.borrow().ctors.assert_defined();
CtorDefId(ctor_id.elements(), variant_idx).borrow().params.len()
}
Ty::Tuple(TupleTy { data }) => data.len(),
Ty::TupleTy(TupleTy { data }) => data.len(),
ty => panic!("Unexpected type `{ty:?}` when computing arity"),
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/hash-exhaustiveness/src/deconstruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use hash_tir::{
data::{CtorDefId, DataTy},
node::NodesId,
pats::PatId,
tys::{Ty, TyId},
terms::{Ty, TyId},
};
use hash_utils::{itertools::Itertools, smallvec::SmallVec};

Expand Down Expand Up @@ -204,8 +204,8 @@ impl fmt::Debug for ExhaustivenessFmtCtx<'_, DeconstructedPatId> {
match ctor {
DeconstructedCtor::Single | DeconstructedCtor::Variant(_) => {
match *pat.ty.value() {
Ty::Tuple(_) => {}
Ty::Data(ty @ DataTy { data_def, .. }) => {
Ty::TupleTy(_) => {}
Ty::DataTy(ty @ DataTy { data_def, .. }) => {
write!(f, "{ty}")?;

// If we have a variant, we print the specific variant that is
Expand Down
6 changes: 3 additions & 3 deletions compiler/hash-exhaustiveness/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use hash_storage::store::{statics::StoreId, Store};
use hash_tir::{
data::{CtorDefId, DataDefCtors, DataTy},
node::NodesId,
terms::{Ty, TyId},
tuples::TupleTy,
tys::{Ty, TyId},
};
use hash_utils::itertools::Itertools;

Expand Down Expand Up @@ -77,12 +77,12 @@ impl<'tc> ExhaustivenessChecker<'tc> {
match ctor {
ctor @ (DeconstructedCtor::Single | DeconstructedCtor::Variant(_)) => {
match *ctx.ty.value() {
Ty::Tuple(TupleTy { data }) => {
Ty::TupleTy(TupleTy { data }) => {
let tys =
data.elements().borrow().iter().map(|member| member.ty).collect_vec();
self.wildcards_from_tys(tys)
}
Ty::Data(DataTy { data_def, .. }) => {
Ty::DataTy(DataTy { data_def, .. }) => {
// get the variant index from the deconstructed ctor
let variant_idx =
if let DeconstructedCtor::Variant(idx) = ctor { idx } else { 0 };
Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-exhaustiveness/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use hash_storage::store::CloneStore;
use hash_tir::{
environment::env::{AccessToEnv, Env},
pats::PatId,
tys::TyId,
terms::TyId,
};
use storage::{
DeconstructedCtorId, DeconstructedCtorStore, DeconstructedPatId, DeconstructedPatStore,
Expand Down
10 changes: 5 additions & 5 deletions compiler/hash-exhaustiveness/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use hash_tir::{
pats::{Pat, PatId, RangePat, Spread},
scopes::BindingPat,
symbols::SymbolId,
term_as_variant,
terms::{Ty, TyId},
tuples::{TuplePat, TupleTy},
ty_as_variant,
tys::{Ty, TyId},
};
use hash_utils::{itertools::Itertools, smallvec::SmallVec};

Expand Down Expand Up @@ -113,7 +113,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
Pat::Tuple(TuplePat { data, .. }) => {
// We need to read the tuple type from the ctx type and then create
// wildcard fields for all of the inner types
let tuple_ty = ty_as_variant!(ty, *ty_id.value(), Tuple);
let tuple_ty = term_as_variant!(ty, ty_id.value(), TupleTy);
let fields = self.deconstruct_pat_fields(data, tuple_ty.data);

// Create wild-cards for all of the tuple inner members
Expand Down Expand Up @@ -251,7 +251,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
let pat = match ctor {
DeconstructedCtor::Single | DeconstructedCtor::Variant(_) => {
match *ty.value() {
Ty::Data(DataTy { data_def, args }) => {
Ty::DataTy(DataTy { data_def, args }) => {
let ctor_def_id = data_def.borrow().ctors.assert_defined();

// We need to reconstruct the ctor-def-id...
Expand All @@ -265,7 +265,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {

Pat::Ctor(CtorPat { ctor, ctor_pat_args: pats, ctor_pat_args_spread: spread, data_args: args })
}
Ty::Tuple(TupleTy { data }) => {
Ty::TupleTy(TupleTy { data }) => {
let (pats, spread) = self.construct_pat_args(fields, data);
Pat::Tuple(TuplePat { data: pats, data_spread: spread })
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-exhaustiveness/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use hash_storage::store::Store;
use hash_tir::{
environment::env::AccessToEnv,
pats::{PatId, RangePat},
tys::TyId,
terms::TyId,
};

use crate::{
Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-exhaustiveness/src/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use std::iter::once;

use hash_storage::store::Store;
use hash_tir::{pats::PatId, tys::TyId};
use hash_tir::{pats::PatId, terms::TyId};
use hash_utils::{itertools::Itertools, stack::ensure_sufficient_stack};

use super::{
Expand Down
6 changes: 3 additions & 3 deletions compiler/hash-exhaustiveness/src/wildcard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hash_storage::store::{statics::StoreId, SequenceStoreKey, Store, TrivialSequ
use hash_target::size::Size;
use hash_tir::{
data::{DataDefCtors, DataTy, NumericCtorBits, PrimitiveCtorInfo},
tys::Ty,
terms::Ty,
};
use hash_utils::smallvec::{smallvec, SmallVec};

Expand Down Expand Up @@ -62,7 +62,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
// for `Option<!>`, we do not include `Some(_)` in the returned list of
// constructors.
let all_ctors = match *ctx.ty.value() {
Ty::Data(DataTy { data_def, .. }) => {
Ty::DataTy(DataTy { data_def, .. }) => {
let def = data_def.value();

match def.ctors {
Expand Down Expand Up @@ -129,7 +129,7 @@ impl<'tc> ExhaustivenessChecker<'tc> {
},
}
}
Ty::Tuple(..) => smallvec![DeconstructedCtor::Single],
Ty::TupleTy(..) => smallvec![DeconstructedCtor::Single],
_ => smallvec![DeconstructedCtor::NonExhaustive],
};

Expand Down
37 changes: 16 additions & 21 deletions compiler/hash-intrinsics/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use hash_storage::store::{statics::StoreId, DefaultPartialStore, PartialStore, S
use hash_tir::{
self,
building::gen::{
data_ty, never_ty, params, params_pos, sym, ty, universe_ty, void_term, void_ty,
data_ty, never_ty, params, params_pos, sym, ty, unit_term, unit_ty, universe_ty,
},
environment::env::{AccessToEnv, Env},
fns::{FnBody, FnDef, FnDefId, FnTy},
Expand All @@ -17,8 +17,7 @@ use hash_tir::{
node::{HasAstNodeId, Node, NodeOrigin},
primitives::primitives,
refs::RefKind,
terms::{Term, TermId},
tys::Ty,
terms::{Term, TermId, Ty},
};
use hash_utils::stream_less_writeln;
use num_enum::{IntoPrimitive, TryFromPrimitive};
Expand Down Expand Up @@ -277,7 +276,7 @@ impl DefinedIntrinsics {
}

// Handle each `T` parameter:
match env.try_use_ty_as_lit_ty(t.as_ty()) {
match env.try_use_ty_as_lit_ty(t) {
Some(lit_ty) => match lit_ty {
LitTy::I8 => handle_integer!(i8),
LitTy::I16 => handle_integer!(i16),
Expand Down Expand Up @@ -469,7 +468,7 @@ impl DefinedIntrinsics {
}

// Handle each `T` parameter:
match env.try_use_ty_as_lit_ty(t.as_ty()) {
match env.try_use_ty_as_lit_ty(t) {
Some(lit_ty) => match lit_ty {
LitTy::U8 => handle_integer!(u8),
LitTy::U16 => handle_integer!(u16),
Expand Down Expand Up @@ -607,7 +606,7 @@ impl DefinedIntrinsics {
}

// Handle each `T` parameter:
match env.try_use_ty_as_lit_ty(t.as_ty()) {
match env.try_use_ty_as_lit_ty(t) {
Some(lit_ty) => match lit_ty {
LitTy::U8 => handle_integer!(u8),
LitTy::U16 => handle_integer!(u16),
Expand Down Expand Up @@ -656,17 +655,13 @@ impl DefinedIntrinsics {
Err("Invalid arguments for type equality intrinsic. Only data types with no arguments can be compared".to_string())
};

if let (Term::Ty(lhs_ty), Term::Ty(rhs_ty)) = (*lhs.value(), *rhs.value()) {
if let (Ty::Data(lhs_data), Ty::Data(rhs_data)) =
(*lhs_ty.value(), *rhs_ty.value())
{
// @@MissingOrigin
if lhs_data.args.len() == 0 && rhs_data.args.len() == 0 {
return Ok(prim.new_bool_term(
lhs_data.data_def == rhs_data.data_def,
NodeOrigin::Generated,
));
}
if let (Ty::DataTy(lhs_data), Ty::DataTy(rhs_data)) = (*lhs.value(), *rhs.value()) {
// @@MissingOrigin
if lhs_data.args.len() == 0 && rhs_data.args.len() == 0 {
return Ok(prim.new_bool_term(
lhs_data.data_def == rhs_data.data_def,
NodeOrigin::Generated,
));
}
}

Expand Down Expand Up @@ -727,18 +722,18 @@ impl DefinedIntrinsics {
let t_sym = sym("T");
let a_sym = sym("a");
let params = params([(t_sym, universe_ty(), None), (a_sym, ty(t_sym), None)]);
let ret = void_ty();
let ret = unit_ty();
add("debug_print", FnTy::builder().params(params).return_ty(ret).build(), |_, args| {
stream_less_writeln!("{}", args[1]);
Ok(void_term())
Ok(unit_term())
})
};

let print_fn_directives = {
let t_sym = sym("T");
let a_sym = sym("a");
let params = params([(t_sym, universe_ty(), None), (a_sym, ty(t_sym), None)]);
let ret = void_ty();
let ret = unit_ty();
add(
"print_fn_directives",
FnTy::builder().params(params).return_ty(ret).build(),
Expand All @@ -748,7 +743,7 @@ impl DefinedIntrinsics {
stream_less_writeln!("{:?}", attrs);
});
}
Ok(void_term())
Ok(unit_term())
},
)
};
Expand Down
11 changes: 5 additions & 6 deletions compiler/hash-intrinsics/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use hash_tir::{
pats::{Pat, PatId},
primitives::primitives,
refs::{RefKind, RefTy},
terms::{Term, TermId},
tys::{Ty, TyId},
terms::{Term, TermId, Ty, TyId},
};

/// Primitive literal types.
Expand Down Expand Up @@ -150,7 +149,7 @@ pub trait PrimitiveUtils: AccessToEnv {
/// Get the given type as a primitive integer type if possible.
fn try_use_ty_as_int_ty(&self, ty: TyId) -> Option<IntTy> {
match *ty.value() {
Ty::Data(data) => match data.data_def {
Ty::DataTy(data) => match data.data_def {
d if d == primitives().i8() => Some(IntTy::Int(SIntTy::I8)),
d if d == primitives().u8() => Some(IntTy::UInt(UIntTy::U8)),
d if d == primitives().i16() => Some(IntTy::Int(SIntTy::I16)),
Expand All @@ -174,7 +173,7 @@ pub trait PrimitiveUtils: AccessToEnv {
/// Get the given type as a primitive float type if possible.
fn try_use_ty_as_float_ty(&self, ty: TyId) -> Option<FloatTy> {
match *ty.value() {
Ty::Data(data) => match data.data_def {
Ty::DataTy(data) => match data.data_def {
d if d == primitives().f32() => Some(FloatTy::F32),
d if d == primitives().f64() => Some(FloatTy::F64),
_ => None,
Expand All @@ -186,7 +185,7 @@ pub trait PrimitiveUtils: AccessToEnv {
/// Get the given type as a primitive array type if possible.
fn try_use_ty_as_array_ty(&self, ty: TyId) -> Option<ArrayCtorInfo> {
match *ty.value() {
Ty::Data(data) => match data.data_def.borrow().ctors {
Ty::DataTy(data) => match data.data_def.borrow().ctors {
DataDefCtors::Primitive(PrimitiveCtorInfo::Array(array)) => Some(array),
_ => None,
},
Expand All @@ -197,7 +196,7 @@ pub trait PrimitiveUtils: AccessToEnv {
/// Get the given type as a literal type if possible.
fn try_use_ty_as_lit_ty(&self, ty: TyId) -> Option<LitTy> {
match *ty.value() {
Ty::Data(data) => match data.data_def {
Ty::DataTy(data) => match data.data_def {
d if d == primitives().i8() => Some(LitTy::I8),
d if d == primitives().u8() => Some(LitTy::U8),
d if d == primitives().i16() => Some(LitTy::I16),
Expand Down
2 changes: 1 addition & 1 deletion compiler/hash-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use hash_storage::{store::SequenceStoreKey, stores};
use hash_tir::{
data::{DataDefId, DataTy},
fns::FnDefId,
tys::TyId,
terms::TyId,
};
use hash_utils::fxhash::FxHashMap;
use intrinsics::Intrinsics;
Expand Down
8 changes: 6 additions & 2 deletions compiler/hash-lower/src/build/category.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Defines a category of AST expressions which can be used to determine how to
//! lower them throughout the lowering stage.
use hash_tir::terms::Term;
use hash_tir::terms::{Term, Ty};

/// A [Category] represents what category [ast::Expr]s belong to
/// when they are being lowered. Depending on the category, we
Expand Down Expand Up @@ -65,7 +65,11 @@ impl Category {
| Term::Array(_)
| Term::Cast(_)
| Term::TypeOf(_)
| Term::Ty(_)
| Ty::DataTy(_)
| Ty::FnTy(_)
| Ty::TupleTy(_)
| Ty::RefTy(_)
| Ty::Universe
| Term::Hole(_) => Category::RValue(RValueKind::As),
}
}
Expand Down
Loading

0 comments on commit dc2adbf

Please sign in to comment.