tuple it up

This commit is contained in:
nora 2023-05-23 21:19:08 +02:00
parent 05c617f6de
commit dba217c18e
6 changed files with 43 additions and 79 deletions

View file

@ -4,7 +4,7 @@ mod typeck;
use std::cell::{Cell, RefCell};
use parser::{
ast::{self, ExprBinary, IntTy, IntTyKind, IntSign},
ast::{self, ExprBinary, IntSign, IntTy, IntTyKind},
Span, Symbol,
};
use rustc_hash::{FxHashMap, FxHashSet};
@ -89,7 +89,7 @@ impl<'cx> LoweringCx<'cx> {
let layout = match *ty {
TyKind::Void => Layout::size_align(0, 1),
TyKind::Char => Layout::size_align(1, 1),
TyKind::Int(int) => match int.kind {
TyKind::Int(int) => match int.1 {
IntTyKind::Bool => Layout::size_align(1, 1),
IntTyKind::Char => Layout::size_align(1, 1),
IntTyKind::Short => Layout::size_align(2, 2),
@ -588,7 +588,7 @@ fn lower_func<'cx>(
impl<'cx> CommonTypes<'cx> {
fn new(lcx: &LoweringCx<'cx>) -> Self {
let int = |sign, kind| lcx.intern_ty(TyKind::Int(IntTy { sign, kind }));
let int = |sign, kind| lcx.intern_ty(TyKind::Int(IntTy(sign, kind)));
let int_pair = |kind| CommonInt {
signed: int(IntSign::Signed, kind),
unsigned: int(IntSign::Unsigned, kind),

View file

@ -49,11 +49,11 @@ impl<'a, 'cx> FnLoweringCtxt<'a, 'cx> {
let lhs_prom_int = lhs_prom.unwrap_int();
let rhs_prom_int = rhs_prom.unwrap_int();
let lhs_sign = lhs_prom_int.sign;
let rhs_sign = rhs_prom_int.sign;
let lhs_sign = lhs_prom_int.0;
let rhs_sign = rhs_prom_int.0;
let lhs_kind = lhs_prom_int.kind;
let rhs_kind = rhs_prom_int.kind;
let lhs_kind = lhs_prom_int.1;
let rhs_kind = rhs_prom_int.1;
// If both operands have the same type, then no further conversion is needed.
let result = if lhs_prom == rhs_prom {
@ -61,7 +61,7 @@ impl<'a, 'cx> FnLoweringCtxt<'a, 'cx> {
// Otherwise, if both operands have signed integer types or both have unsigned
// integer types, the operand with the type of lesser integer conversion rank is
// converted to the type of the operand with greater rank.
} else if (lhs_sign.unsigned() && rhs_prom_int.sign.unsigned())
} else if (lhs_sign.unsigned() && rhs_prom_int.0.unsigned())
|| (lhs_sign.signed() && rhs_sign.signed())
{
if lhs_kind > rhs_kind {
@ -109,10 +109,9 @@ impl<'a, 'cx> FnLoweringCtxt<'a, 'cx> {
} else {
rhs_kind
};
let ty = self.lcx.intern_ty(TyKind::Int(IntTy {
kind,
sign: IntSign::Unsigned,
}));
let ty = self
.lcx
.intern_ty(TyKind::Int(IntTy(IntSign::Unsigned, kind)));
lhs_coerce.extend(self.coerce(lhs_prom, ty)?);
ty
};
@ -127,36 +126,18 @@ impl<'a, 'cx> FnLoweringCtxt<'a, 'cx> {
return Ok(smallvec![]);
}
Ok(match (*from, *to) {
(
TyKind::Char,
TyKind::Int(IntTy {
sign: IntSign::Signed,
..
}),
) => {
(TyKind::Char, TyKind::Int(IntTy(IntSign::Signed, _))) => {
smallvec![(Coercion::SignExt, to)]
}
(
TyKind::Int(IntTy {
sign: IntSign::Signed,
kind: from_kind,
}),
TyKind::Int(IntTy {
sign: IntSign::Signed,
kind: to_kind,
}),
TyKind::Int(IntTy(IntSign::Signed, from_kind)),
TyKind::Int(IntTy(IntSign::Signed, to_kind)),
) if from_kind < to_kind => {
smallvec![(Coercion::SignExt, to)]
}
(
TyKind::Int(IntTy {
sign: IntSign::Unsigned,
kind: from_kind,
}),
TyKind::Int(IntTy {
sign: IntSign::Unsigned,
kind: to_kind,
}),
TyKind::Int(IntTy(IntSign::Unsigned, from_kind)),
TyKind::Int(IntTy(IntSign::Unsigned, to_kind)),
) if from_kind < to_kind => {
smallvec![(Coercion::ZeroExt, to)]
}
@ -168,7 +149,7 @@ impl<'a, 'cx> FnLoweringCtxt<'a, 'cx> {
fn promote(&self, ty: Ty<'cx>, span: Span) -> Result<Coercions<'cx>> {
Ok(match *ty {
TyKind::Char => smallvec![(Coercion::SignExt, self.types.int.signed)],
TyKind::Int(int) if int.kind < IntTyKind::Int => match int.sign {
TyKind::Int(int) if int.1 < IntTyKind::Int => match int.0 {
IntSign::Signed => smallvec![(Coercion::SignExt, self.types.int.signed)],
IntSign::Unsigned => {
smallvec![(Coercion::ZeroExt, self.types.int.unsigned)]

View file

@ -6,7 +6,7 @@ use std::{
use indexmap::IndexMap;
use parser::{
ast::{IntTy, IntTyKind, IntSign},
ast::{IntSign, IntTy, IntTyKind},
Symbol,
};
@ -76,11 +76,11 @@ impl Display for Ty<'_> {
TyKind::Void => f.write_str("void"),
TyKind::Char => f.write_str("char"),
TyKind::Int(int) => {
match int.sign {
match int.0 {
IntSign::Signed => f.write_str("signed "),
IntSign::Unsigned => f.write_str("unsigned "),
}?;
match int.kind {
match int.1 {
IntTyKind::Bool => f.write_str("_Bool"),
IntTyKind::Char => f.write_str("char"),
IntTyKind::Short => f.write_str("short"),