Skip to content

Commit

Permalink
Improved conforming rules for parameter passing to reduce overload co…
Browse files Browse the repository at this point in the history
…nflicts
  • Loading branch information
IsaacShelton committed Sep 29, 2024
1 parent 7902d59 commit 8cb5fb7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/resolve/conform/from_c_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ pub fn from_c_integer_to_c_integer<O: Objective>(
to_sign: Option<IntegerSign>,
source: Source,
) -> ObjectiveResult<O> {
if !mode.allow_lossless_integer() {
return O::fail();
}

let target_type = TypeKind::CInteger(to_c_integer, to_sign).at(source);

let is_smaller_likeness = from_sign == to_sign && from_c_integer <= to_c_integer;
Expand Down
7 changes: 6 additions & 1 deletion src/resolve/conform/from_float.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Objective, ObjectiveResult};
use super::{ConformMode, Objective, ObjectiveResult};
use crate::{
ast::FloatSize,
resolved::{Cast, Expr, ExprKind, Type, TypeKind, TypedExpr},
Expand All @@ -7,9 +7,14 @@ use crate::{

pub fn from_float<O: Objective>(
expr: &TypedExpr,
mode: ConformMode,
from_size: FloatSize,
to_type: &Type,
) -> ObjectiveResult<O> {
if !mode.allow_lossless_float() {
return O::fail();
}

match &to_type.kind {
TypeKind::Floating(to_size) => {
from_float_to_float::<O>(&expr.expr, from_size, *to_size, to_type.source)
Expand Down
20 changes: 17 additions & 3 deletions src/resolve/conform/from_integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn from_integer<O: Objective>(
TypeKind::Integer(to_bits, to_sign) => match behavior {
ConformBehavior::Adept(_) => from_integer_adept_mode::<O>(
&expr.expr,
mode,
from_bits,
from_sign,
*to_bits,
Expand Down Expand Up @@ -50,20 +51,24 @@ pub fn from_integer<O: Objective>(

fn from_integer_c_mode<O: Objective>(
expr: &Expr,
conform_mode: ConformMode,
mode: ConformMode,
from_bits: IntegerBits,
from_sign: IntegerSign,
to_bits: IntegerBits,
to_sign: IntegerSign,
type_source: Source,
) -> ObjectiveResult<O> {
if !mode.allow_lossless_integer() {
return O::fail();
}

let target_type = TypeKind::Integer(to_bits, to_sign).at(type_source);

if from_bits == to_bits && from_sign == to_sign {
return O::success(|| TypedExpr::new(target_type, expr.clone()));
}

if conform_mode.allow_lossy_integer() {
if mode.allow_lossy_integer() {
let cast = Cast::new(target_type.clone(), expr.clone());

let kind = if from_bits < to_bits {
Expand All @@ -83,7 +88,7 @@ fn from_integer_c_mode<O: Objective>(
});
}

todo!("conform_integer_value_c {:?}", conform_mode);
todo!("conform_integer_value_c {:?}", mode);
}

fn conform_from_integer_to_c_integer<O: Objective>(
Expand All @@ -96,6 +101,10 @@ fn conform_from_integer_to_c_integer<O: Objective>(
to_sign: Option<IntegerSign>,
source: Source,
) -> ObjectiveResult<O> {
if !mode.allow_lossless_integer() {
return O::fail();
}

let target_type = TypeKind::CInteger(to_c_integer, to_sign).at(source);
let assumptions = behavior.c_integer_assumptions();

Expand Down Expand Up @@ -124,12 +133,17 @@ fn conform_from_integer_to_c_integer<O: Objective>(

fn from_integer_adept_mode<O: Objective>(
expr: &Expr,
mode: ConformMode,
from_bits: IntegerBits,
from_sign: IntegerSign,
to_bits: IntegerBits,
to_sign: IntegerSign,
type_source: Source,
) -> ObjectiveResult<O> {
if !mode.allow_lossless_integer() {
return O::fail();
}

if to_bits < from_bits || (from_sign != to_sign && to_bits == from_bits) {
return O::fail();
}
Expand Down
2 changes: 1 addition & 1 deletion src/resolve/conform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn conform_expr<O: Objective>(
from_integer::<O>(expr, mode, behavior, *from_bits, *from_sign, to_type)
}
TypeKind::FloatLiteral(from) => from_float_literal::<O>(*from, to_type, conform_source),
TypeKind::Floating(from_size) => from_float::<O>(expr, *from_size, to_type),
TypeKind::Floating(from_size) => from_float::<O>(expr, mode, *from_size, to_type),
TypeKind::Pointer(from_inner) => from_pointer::<O>(expr, mode, from_inner, to_type),
TypeKind::CInteger(from_size, from_sign) => {
from_c_integer::<O>(expr, mode, *from_size, *from_sign, to_type, conform_source)
Expand Down
8 changes: 8 additions & 0 deletions src/resolve/conform/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,12 @@ impl ConformMode {
pub fn allow_lossy_integer(&self) -> bool {
matches!(self, Self::Explicit)
}

pub fn allow_lossless_integer(&self) -> bool {
matches!(self, Self::Normal | Self::Explicit)
}

pub fn allow_lossless_float(&self) -> bool {
matches!(self, Self::Normal | Self::Explicit)
}
}
4 changes: 1 addition & 3 deletions src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,7 @@ pub fn resolve<'a>(
function_search_context
.available
.entry(name)
.and_modify(|funcs| {
funcs.push(function_ref);
})
.and_modify(|funcs| funcs.push(function_ref))
.or_insert_with(|| vec![function_ref]);
}
}
Expand Down

0 comments on commit 8cb5fb7

Please sign in to comment.