Skip to content

Commit a354a5e

Browse files
Fix prelude number detection (#89)
1 parent 46dd8d7 commit a354a5e

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

src/generation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3182,7 +3182,7 @@ fn generate_tag_check(deser_func: &mut dyn CodeBlock, ident: &RustIdent, tag: Op
31823182
}
31833183

31843184
// This is used mostly for when thing are tagged have specific ranges.
3185-
fn generate_wrapper_struct(gen_scope: &mut GenerationScope, types: &IntermediateTypes, type_name: &RustIdent, field_type: &RustType, min_max: Option<(Option<isize>, Option<isize>)>) {
3185+
fn generate_wrapper_struct(gen_scope: &mut GenerationScope, types: &IntermediateTypes, type_name: &RustIdent, field_type: &RustType, min_max: Option<(Option<i128>, Option<i128>)>) {
31863186
if min_max.is_some() {
31873187
assert!(types.can_new_fail(type_name));
31883188
}

src/intermediate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ pub enum RustStructType {
13421342
},
13431343
Wrapper{
13441344
wrapped: RustType,
1345-
min_max: Option<(Option<isize>, Option<isize>)>,
1345+
min_max: Option<(Option<i128>, Option<i128>)>,
13461346
},
13471347
/// This is a no-op in generation but to prevent lookups of things in the prelude
13481348
/// e.g. `int` from not being resolved while still being able to detect it when
@@ -1401,7 +1401,7 @@ impl RustStruct {
14011401
}
14021402
}
14031403

1404-
pub fn new_wrapper(ident: RustIdent, tag: Option<usize>, wrapped_type: RustType, min_max: Option<(Option<isize>, Option<isize>)>) -> Self {
1404+
pub fn new_wrapper(ident: RustIdent, tag: Option<usize>, wrapped_type: RustType, min_max: Option<(Option<i128>, Option<i128>)>) -> Self {
14051405
Self {
14061406
ident,
14071407
tag,

src/parsing.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::utils::{
2929

3030
#[derive(Clone, Debug)]
3131
enum ControlOperator {
32-
Range((Option<isize>, Option<isize>)),
32+
Range((Option<i128>, Option<i128>)),
3333
CBOR(RustType),
3434
}
3535

@@ -131,40 +131,40 @@ fn parse_control_operator(types: &mut IntermediateTypes, parent: &Type2AndParent
131131
Type2::IntValue{ value, ..} => value,
132132
_ => unimplemented!("unsupported type in range control operator: {:?}", operator),
133133
};
134-
ControlOperator::Range((Some(range_start), Some(if is_inclusive { range_end } else { range_end + 1 })))
134+
ControlOperator::Range((Some(range_start as i128), Some(if is_inclusive { range_end as i128 } else { (range_end + 1) as i128 })))
135135
},
136136
RangeCtlOp::CtlOp{ ctrl, .. } => match ctrl {
137137
".default" |
138138
".cborseq" |
139139
".within" |
140140
".and" => todo!("control operator {} not supported", ctrl),
141141
".cbor" => ControlOperator::CBOR(rust_type_from_type2(types, &Type2AndParent { type2: &operator.type2, parent: parent.parent, })),
142-
".eq" => ControlOperator::Range((Some(type2_to_number_literal(&operator.type2)), Some(type2_to_number_literal(&operator.type2)))),
142+
".eq" => ControlOperator::Range((Some(type2_to_number_literal(&operator.type2) as i128), Some(type2_to_number_literal(&operator.type2) as i128))),
143143
// TODO: this would be MUCH nicer (for error displaying, etc) to handle this in its own dedicated way
144144
// which might be necessary once we support other control operators anyway
145-
".ne" => ControlOperator::Range((Some(type2_to_number_literal(&operator.type2) + 1), Some(type2_to_number_literal(&operator.type2) - 1))),
146-
".le" => ControlOperator::Range((lower_bound, Some(type2_to_number_literal(&operator.type2)))),
147-
".lt" => ControlOperator::Range((lower_bound, Some(type2_to_number_literal(&operator.type2) - 1))),
148-
".ge" => ControlOperator::Range((Some(type2_to_number_literal(&operator.type2)), None)),
149-
".gt" => ControlOperator::Range((Some(type2_to_number_literal(&operator.type2) + 1), None)),
145+
".ne" => ControlOperator::Range((Some((type2_to_number_literal(&operator.type2) + 1) as i128), Some((type2_to_number_literal(&operator.type2) - 1) as i128))),
146+
".le" => ControlOperator::Range((lower_bound, Some(type2_to_number_literal(&operator.type2) as i128))),
147+
".lt" => ControlOperator::Range((lower_bound, Some((type2_to_number_literal(&operator.type2) - 1) as i128))),
148+
".ge" => ControlOperator::Range((Some(type2_to_number_literal(&operator.type2) as i128), None)),
149+
".gt" => ControlOperator::Range((Some((type2_to_number_literal(&operator.type2) + 1) as i128), None)),
150150
".size" => {
151151
let base_range = match &operator.type2 {
152-
Type2::UintValue{ value, .. } => ControlOperator::Range((None, Some(*value as isize))),
153-
Type2::IntValue{ value, .. } => ControlOperator::Range((None, Some(*value))),
152+
Type2::UintValue{ value, .. } => ControlOperator::Range((None, Some(*value as i128))),
153+
Type2::IntValue{ value, .. } => ControlOperator::Range((None, Some(*value as i128))),
154154
Type2::ParenthesizedType{ pt, .. } => {
155155
assert_eq!(pt.type_choices.len(), 1);
156156
let inner_type = &pt.type_choices.first().unwrap().type1;
157157
let min = match inner_type.type2 {
158-
Type2::UintValue{ value, .. } => Some(value as isize),
159-
Type2::IntValue{ value, .. } => Some(value),
158+
Type2::UintValue{ value, .. } => Some(value as i128),
159+
Type2::IntValue{ value, .. } => Some(value as i128),
160160
_ => unimplemented!("unsupported type in range control operator: {:?}", operator),
161161
};
162162
let max = match &inner_type.operator {
163163
Some(op) => match op.operator {
164164
RangeCtlOp::RangeOp{ is_inclusive, ..} => {
165165
let value = match op.type2 {
166-
Type2::UintValue{ value, .. } => value as isize,
167-
Type2::IntValue{ value, ..} => value,
166+
Type2::UintValue{ value, .. } => value as i128,
167+
Type2::IntValue{ value, ..} => value as i128,
168168
_ => unimplemented!("unsupported type in range control operator: {:?}", operator),
169169
};
170170
Some(if is_inclusive { value } else { value + 1 })
@@ -181,16 +181,16 @@ fn parse_control_operator(types: &mut IntermediateTypes, parent: &Type2AndParent
181181
Type2::Typename{ ident, .. } if ident.to_string() == "uint" => {
182182
// .size 3 means 24 bits
183183
match &base_range {
184-
ControlOperator::Range((Some(l), Some(h))) => ControlOperator::Range((Some(isize::pow(2, 8 * *l as u32)), Some(isize::pow(2, 8 * *h as u32) - 1))),
185-
ControlOperator::Range((None, Some(h))) => ControlOperator::Range((Some(0), Some(isize::pow(2, 8 * *h as u32) - 1))),
184+
ControlOperator::Range((Some(l), Some(h))) => ControlOperator::Range((Some(i128::pow(2, 8 * *l as u32)), Some(i128::pow(2, 8 * *h as u32) - 1))),
185+
ControlOperator::Range((None, Some(h))) => ControlOperator::Range((Some(0), Some(i128::pow(2, 8 * *h as u32) - 1))),
186186
_ => panic!("unexpected partial range in size control operator: {:?}", operator)
187187
}
188188
},
189189
Type2::Typename{ ident, .. } if ident.to_string() == "int" => {
190190
match &base_range {
191191
// this is complex to support since it requires two disjoint ranges of possible values
192192
ControlOperator::Range((Some(_), Some(_))) => panic!(".size range unsupported for signed int type: {:?}", operator),
193-
ControlOperator::Range((None, Some(h))) => ControlOperator::Range((Some(-isize::pow(2, 8 * (*h - 1) as u32)), Some(isize::pow(2, (8 * (*h - 1)) as u32) - 1))),
193+
ControlOperator::Range((None, Some(h))) => ControlOperator::Range((Some(-i128::pow(2, ((8 * *h) - 1) as u32)), Some(i128::pow(2, ((8 * *h) - 1) as u32) - 1))),
194194
_ => panic!("unexpected partial range in size control operator: {:?}", operator)
195195
}
196196
}
@@ -208,16 +208,16 @@ fn parse_control_operator(types: &mut IntermediateTypes, parent: &Type2AndParent
208208
}
209209
}
210210

211-
fn range_to_primitive(low: Option<isize>, high: Option<isize>) -> Option<RustType> {
211+
fn range_to_primitive(low: Option<i128>, high: Option<i128>) -> Option<RustType> {
212212
match (low, high) {
213-
(Some(l), Some(h)) if l == u8::MIN as isize && h == u8::MAX as isize => Some(RustType::Primitive(Primitive::U8)),
214-
(Some(l), Some(h)) if l == i8::MIN as isize && h == i8::MAX as isize => Some(RustType::Primitive(Primitive::I8)),
215-
(Some(l), Some(h)) if l == u16::MIN as isize && h == u16::MAX as isize => Some(RustType::Primitive(Primitive::U16)),
216-
(Some(l), Some(h)) if l == i16::MIN as isize && h == i16::MAX as isize => Some(RustType::Primitive(Primitive::I16)),
217-
(Some(l), Some(h)) if l == u32::MIN as isize && h == u32::MAX as isize => Some(RustType::Primitive(Primitive::U32)),
218-
(Some(l), Some(h)) if l == i32::MIN as isize && h == i32::MAX as isize => Some(RustType::Primitive(Primitive::I32)),
219-
(Some(l), Some(h)) if l == u64::MIN as isize && h == u64::MAX as isize => Some(RustType::Primitive(Primitive::U64)),
220-
(Some(l), Some(h)) if l == i64::MIN as isize && h == i64::MAX as isize => Some(RustType::Primitive(Primitive::I64)),
213+
(Some(l), Some(h)) if l == u8::MIN as i128 && h == u8::MAX as i128 => Some(RustType::Primitive(Primitive::U8)),
214+
(Some(l), Some(h)) if l == i8::MIN as i128 && h == i8::MAX as i128 => Some(RustType::Primitive(Primitive::I8)),
215+
(Some(l), Some(h)) if l == u16::MIN as i128 && h == u16::MAX as i128 => Some(RustType::Primitive(Primitive::U16)),
216+
(Some(l), Some(h)) if l == i16::MIN as i128 && h == i16::MAX as i128 => Some(RustType::Primitive(Primitive::I16)),
217+
(Some(l), Some(h)) if l == u32::MIN as i128 && h == u32::MAX as i128 => Some(RustType::Primitive(Primitive::U32)),
218+
(Some(l), Some(h)) if l == i32::MIN as i128 && h == i32::MAX as i128 => Some(RustType::Primitive(Primitive::I32)),
219+
(Some(l), Some(h)) if l == u64::MIN as i128 && h == u64::MAX as i128 => Some(RustType::Primitive(Primitive::U64)),
220+
(Some(l), Some(h)) if l == i64::MIN as i128 && h == i64::MAX as i128 => Some(RustType::Primitive(Primitive::I64)),
221221
_ => None
222222
}
223223
}

tests/core/input.cddl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ cbor_in_cbor = [foo_bytes, uint_bytes: bytes .cbor uint]
4040
u8 = uint .size 1
4141
u16 = uint .le 65535
4242
u32 = 0..4294967295
43+
u64 = uint .size 8 ; 8 bytes
4344
i8 = -128..127
45+
i64 = int .size 8 ; 8 bytes

tests/core/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,7 @@ mod tests {
115115
assert_eq!(0u16, U16::from(0u16));
116116
assert_eq!(0u32, U32::from(0u32));
117117
assert_eq!(0i8, I8::from(0i8));
118+
assert_eq!(0u64, U64::from(0u64));
119+
assert_eq!(0i64, I64::from(0i64));
118120
}
119121
}

0 commit comments

Comments
 (0)