1
1
use super :: { Objective , ObjectiveResult } ;
2
2
use crate :: {
3
+ asg:: { Expr , ExprKind , Type , TypeKind , TypedExpr } ,
3
4
ast:: { CInteger , CIntegerAssumptions , FloatSize , IntegerBits , IntegerKnown , IntegerRigidity } ,
4
5
data_units:: BitUnits ,
5
6
ir:: IntegerSign ,
6
- asg:: { Expr , ExprKind , Type , TypeKind , TypedExpr } ,
7
7
source_files:: Source ,
8
8
} ;
9
9
use num:: { BigInt , Zero } ;
@@ -28,6 +28,9 @@ pub fn from_integer_literal<O: Objective>(
28
28
TypeKind :: Integer ( to_bits, to_sign) => {
29
29
from_integer_literal_to_integer :: < O > ( value, * to_bits, * to_sign, source)
30
30
}
31
+ TypeKind :: SizeInteger ( to_sign) => {
32
+ from_integer_literal_to_size_integer :: < O > ( value, * to_sign, source)
33
+ }
31
34
_ => O :: fail ( ) ,
32
35
}
33
36
}
@@ -91,6 +94,34 @@ fn from_integer_literal_to_c_integer<O: Objective>(
91
94
O :: fail ( )
92
95
}
93
96
97
+ fn from_integer_literal_to_size_integer < O : Objective > (
98
+ value : & BigInt ,
99
+ to_sign : IntegerSign ,
100
+ source : Source ,
101
+ ) -> ObjectiveResult < O > {
102
+ // Size types (i.e. size_t, ssize_t, usize, isize) are guananteed to be at least 16 bits
103
+ // Anything more than that will require explicit casts
104
+ let does_fit = match to_sign {
105
+ IntegerSign :: Signed => i16:: try_from ( value) . is_ok ( ) ,
106
+ IntegerSign :: Unsigned => u16:: try_from ( value) . is_ok ( ) ,
107
+ } ;
108
+
109
+ if does_fit {
110
+ return O :: success ( || {
111
+ TypedExpr :: new (
112
+ TypeKind :: SizeInteger ( to_sign) . at ( source) ,
113
+ ExprKind :: IntegerKnown ( Box :: new ( IntegerKnown {
114
+ rigidity : IntegerRigidity :: Size ( to_sign) ,
115
+ value : value. clone ( ) ,
116
+ } ) )
117
+ . at ( source) ,
118
+ )
119
+ } ) ;
120
+ }
121
+
122
+ O :: fail ( )
123
+ }
124
+
94
125
fn from_integer_literal_to_float < O : Objective > (
95
126
value : & BigInt ,
96
127
to_size : FloatSize ,
0 commit comments