diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 2129b5773..6bd975435 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -2070,25 +2070,24 @@ bool Converter::VisitCXXBoolLiteralExpr(clang::CXXBoolLiteralExpr *expr) { void Converter::ConvertIntegerToEnumeralCast(clang::Expr *to, clang::Expr *from) { - // Short circuit `Enum::from(X as i32)` to `X` + // Short circuit `(X as i32) as Enum` to `X` if (auto ref = clang::dyn_cast(from->IgnoreParenImpCasts())) { if (auto ec = clang::dyn_cast(ref->getDecl())) { auto src_enum = clang::dyn_cast(ec->getDeclContext()); auto dst_enum = to->getType()->getAs(); if (src_enum && dst_enum && dst_enum->getDecl() == src_enum) { - StrCat(std::format("{}::{}", GetRecordName(src_enum), - std::string_view(ec->getName()))); + StrCat(EnumeratorName(ec)); return; } } } - StrCat(GetUnsafeTypeAsString(to->getType()), "::from"); PushParen paren(*this); - Convert(from); - if (!from->getType()->isSpecificBuiltinType(clang::BuiltinType::Int)) { - StrCat(keyword::kAs, "i32"); + { + PushParen inner(*this); + Convert(from); } + StrCat(keyword::kAs, GetUnsafeTypeAsString(to->getType())); } void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) { @@ -2107,11 +2106,7 @@ void Converter::ConvertIntegralToBooleanCast(clang::ImplicitCastExpr *expr) { PushParen paren(*this); Convert(sub_expr); StrCat(token::kDiff); - if (sub_expr->getType()->isEnumeralType()) { - StrCat(GetUnsafeTypeAsString(sub_expr->getType()), "::from(0)"); - } else /* sub_expr->getType()->isIntegerType() */ { - StrCat(token::kZero); - } + StrCat(token::kZero); } bool Converter::IsCastRedundantInRust(clang::Expr *expr, @@ -2657,14 +2652,11 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) { } if (auto enum_constant = clang::dyn_cast(decl)) { - auto qualified = std::format("{}::{}", - GetRecordName(clang::dyn_cast( - enum_constant->getDeclContext())), - std::string_view(enum_constant->getName())); + auto name = EnumeratorName(enum_constant); if (!expr->getType()->isEnumeralType()) { - return std::format("({} as i32)", qualified); + return std::format("({} as i32)", name); } - return qualified; + return name; } if (IsGlobalVar(expr)) { @@ -3237,50 +3229,23 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) { return false; } Mapper::AddRuleForUserDefinedType(decl); - Mapper::SetDerives(ctx_.getCanonicalTagType(decl), - {"Clone", "Copy", "PartialEq", "Debug", "Default"}); - StrCat("#[derive(Clone, Copy, PartialEq, Debug, Default)]"); - StrCat(std::format("enum {}", GetRecordName(decl))); - StrCat('{'); - bool first_enumerator = true; + auto name = GetRecordName(decl); + StrCat(std::format("pub type {} = {};", name, + GetUnsafeTypeAsString(decl->getIntegerType()))); for (auto e : decl->enumerators()) { llvm::SmallVector init; e->getInitVal().toString(init, 10); - if (first_enumerator) { - StrCat("#[default]"); - first_enumerator = false; - } - StrCat(std::format("{} = {},", std::string_view(e->getName()), + StrCat(std::format("pub const {}: {} = {};", EnumeratorName(e), name, std::string_view(init.data(), init.size()))); } - StrCat('}'); - - AddFromImpl(decl); - AddIncDecImpls(decl); - AddByteReprTrait(decl); return false; } -void Converter::AddFromImpl(clang::EnumDecl *decl) { - auto name = GetRecordName(decl); - StrCat(std::format("impl From for {}", name)); - PushBrace impl(*this); - StrCat(std::format("fn from(n: i32) -> {}", name)); - PushBrace fn(*this); - StrCat("match n"); - PushBrace match(*this); - for (auto e : decl->enumerators()) { - llvm::SmallVector init; - e->getInitVal().toString(init, 10); - StrCat(std::format("{} => {}::{},", - std::string_view(init.data(), init.size()), name, - std::string_view(e->getName()))); - } - StrCat(std::format("_ => panic!(\"invalid {} value: {{}}\", n),", name)); -} - -void Converter::AddIncDecImpls(clang::EnumDecl *decl) { - StrCat(std::format("libcc2rs::impl_enum_inc_dec!({});", GetRecordName(decl))); +std::string +Converter::EnumeratorName(const clang::EnumConstantDecl *decl) const { + auto *enum_decl = clang::cast(decl->getDeclContext()); + return std::format("{}_{}", GetRecordName(enum_decl), + std::string_view(decl->getName())); } bool Converter::VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr) { @@ -3570,9 +3535,10 @@ std::string Converter::GetDefaultAsStringFallback(clang::QualType qual_type) { if (qual_type->isEnumeralType()) { auto enum_decl = qual_type->castAs()->getDecl(); - return std::format( - "{}::{}", GetRecordName(enum_decl), - std::string_view(enum_decl->enumerator_begin()->getName())); + if (enum_decl->enumerators().empty()) { + return std::string(1, token::kZero); + } + return EnumeratorName(*enum_decl->enumerator_begin()); } return std::format("<{}>::default()", ToString(qual_type)); @@ -4049,8 +4015,6 @@ void Converter::EmitDefaultStructLiteral(const clang::RecordDecl *decl) { void Converter::AddByteReprTrait(const clang::RecordDecl *decl) {} -void Converter::AddByteReprTrait(const clang::EnumDecl *decl) {} - void Converter::ConvertUnsignedArithBinaryOperator(clang::BinaryOperator *op, clang::Expr *expr) { StrCat(token::kDot); diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 33d3121e2..c70bbc765 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -374,9 +374,7 @@ class Converter : public clang::RecursiveASTVisitor { virtual bool VisitEnumDecl(clang::EnumDecl *decl); - virtual void AddFromImpl(clang::EnumDecl *decl); - - virtual void AddIncDecImpls(clang::EnumDecl *decl); + virtual std::string EnumeratorName(const clang::EnumConstantDecl *decl) const; virtual bool VisitCXXDefaultArgExpr(clang::CXXDefaultArgExpr *expr); @@ -560,8 +558,6 @@ class Converter : public clang::RecursiveASTVisitor { virtual void AddByteReprTrait(const clang::RecordDecl *decl); - virtual void AddByteReprTrait(const clang::EnumDecl *decl); - virtual void ConvertUnsignedArithBinaryOperator(clang::BinaryOperator *binary_operator, clang::Expr *expr); diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index a20cbb860..26ea8e0b8 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -654,17 +654,6 @@ void ConverterRefCount::AddByteReprTrait(const clang::RecordDecl *decl) { } } -void ConverterRefCount::AddByteReprTrait(const clang::EnumDecl *decl) { - auto name = GetRecordName(decl); - StrCat(std::format("impl ByteRepr for {}", name)); - PushBrace impl_brace(*this); - StrCat( - "fn to_bytes(&self, buf: &mut [u8]) { (*self as i32).to_bytes(buf); }"); - StrCat(std::format("fn from_bytes(buf: &[u8]) -> Self {{ " - "<{}>::from(i32::from_bytes(buf)) }}", - name)); -} - std::string ConverterRefCount::GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl) { return "&self"; diff --git a/cpp2rust/converter/models/converter_refcount.h b/cpp2rust/converter/models/converter_refcount.h index a554c18e6..0ee37a5fc 100644 --- a/cpp2rust/converter/models/converter_refcount.h +++ b/cpp2rust/converter/models/converter_refcount.h @@ -45,8 +45,6 @@ class ConverterRefCount final : public Converter { void AddByteReprTrait(const clang::RecordDecl *decl) override; - void AddByteReprTrait(const clang::EnumDecl *decl) override; - bool VisitUnaryExprOrTypeTraitExpr(clang::UnaryExprOrTypeTraitExpr *expr) override; diff --git a/libcc2rs/src/inc.rs b/libcc2rs/src/inc.rs index c83c68f14..0d653e510 100644 --- a/libcc2rs/src/inc.rs +++ b/libcc2rs/src/inc.rs @@ -124,38 +124,6 @@ impl UnsafePrefixInc for *mut T { } } -#[macro_export] -macro_rules! impl_enum_inc_dec { - ($t:ty) => { - impl PostfixInc for $t { - fn postfix_inc(&mut self) -> Self { - let copy = *self; - *self = <$t>::from((*self as i32).wrapping_add(1)); - copy - } - } - impl PrefixInc for $t { - fn prefix_inc(&mut self) -> Self { - *self = <$t>::from((*self as i32).wrapping_add(1)); - *self - } - } - impl PostfixDec for $t { - fn postfix_dec(&mut self) -> Self { - let copy = *self; - *self = <$t>::from((*self as i32).wrapping_sub(1)); - copy - } - } - impl PrefixDec for $t { - fn prefix_dec(&mut self) -> Self { - *self = <$t>::from((*self as i32).wrapping_sub(1)); - *self - } - } - }; -} - #[cfg(test)] mod tests { use super::*; diff --git a/tests/multi-file/cross_tu_anon_enum_collision/out/refcount/cross_tu_anon_enum_collision.rs b/tests/multi-file/cross_tu_anon_enum_collision/out/refcount/cross_tu_anon_enum_collision.rs index 0ce84bdb8..7cf386448 100644 --- a/tests/multi-file/cross_tu_anon_enum_collision/out/refcount/cross_tu_anon_enum_collision.rs +++ b/tests/multi-file/cross_tu_anon_enum_collision/out/refcount/cross_tu_anon_enum_collision.rs @@ -6,31 +6,11 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_0 { - #[default] - ALPHA = 7, -} -impl From for anon_0 { - fn from(n: i32) -> anon_0 { - match n { - 7 => anon_0::ALPHA, - _ => panic!("invalid anon_0 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_0); -impl ByteRepr for anon_0 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type anon_0 = u32; +pub const anon_0_ALPHA: anon_0 = 7; pub fn a_value_1() -> i32 { let x: Value = Rc::new(RefCell::new(0)); - (*x.borrow_mut()) |= (anon_0::ALPHA as i32); + (*x.borrow_mut()) |= (anon_0_ALPHA as i32); return (*x.borrow()); } pub fn main() { @@ -41,30 +21,10 @@ fn main_0() -> i32 { assert!((((({ b_value_2() }) == 9) as i32) != 0)); return 0; } -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_3 { - #[default] - BETA = 9, -} -impl From for anon_3 { - fn from(n: i32) -> anon_3 { - match n { - 9 => anon_3::BETA, - _ => panic!("invalid anon_3 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_3); -impl ByteRepr for anon_3 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type anon_3 = u32; +pub const anon_3_BETA: anon_3 = 9; pub fn b_value_2() -> i32 { let x: Value = Rc::new(RefCell::new(0)); - (*x.borrow_mut()) |= (anon_3::BETA as i32); + (*x.borrow_mut()) |= (anon_3_BETA as i32); return (*x.borrow()); } diff --git a/tests/multi-file/cross_tu_anon_enum_collision/out/unsafe/cross_tu_anon_enum_collision.rs b/tests/multi-file/cross_tu_anon_enum_collision/out/unsafe/cross_tu_anon_enum_collision.rs index 3bcc3f17f..85d80f283 100644 --- a/tests/multi-file/cross_tu_anon_enum_collision/out/unsafe/cross_tu_anon_enum_collision.rs +++ b/tests/multi-file/cross_tu_anon_enum_collision/out/unsafe/cross_tu_anon_enum_collision.rs @@ -6,23 +6,11 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_0 { - #[default] - ALPHA = 7, -} -impl From for anon_0 { - fn from(n: i32) -> anon_0 { - match n { - 7 => anon_0::ALPHA, - _ => panic!("invalid anon_0 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_0); +pub type anon_0 = u32; +pub const anon_0_ALPHA: anon_0 = 7; pub unsafe fn a_value_1() -> i32 { let mut x: i32 = 0; - x |= (anon_0::ALPHA as i32); + x |= (anon_0_ALPHA as i32); return x; } pub fn main() { @@ -35,22 +23,10 @@ unsafe fn main_0() -> i32 { assert!(((((unsafe { b_value_2() }) == (9)) as i32) != 0)); return 0; } -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_3 { - #[default] - BETA = 9, -} -impl From for anon_3 { - fn from(n: i32) -> anon_3 { - match n { - 9 => anon_3::BETA, - _ => panic!("invalid anon_3 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_3); +pub type anon_3 = u32; +pub const anon_3_BETA: anon_3 = 9; pub unsafe fn b_value_2() -> i32 { let mut x: i32 = 0; - x |= (anon_3::BETA as i32); + x |= (anon_3_BETA as i32); return x; } diff --git a/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs b/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs index 3493dccea..e5499920f 100644 --- a/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs +++ b/tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs @@ -43,33 +43,11 @@ fn main_0() -> i32 { assert!((((({ b_value_1() }) == 2) as i32) != 0)); return 0; } -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum widget_enum { - #[default] - WIDGET_A = 0, - WIDGET_B = 1, - WIDGET_C = 2, -} -impl From for widget_enum { - fn from(n: i32) -> widget_enum { - match n { - 0 => widget_enum::WIDGET_A, - 1 => widget_enum::WIDGET_B, - 2 => widget_enum::WIDGET_C, - _ => panic!("invalid widget_enum value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(widget_enum); -impl ByteRepr for widget_enum { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type widget_enum = u32; +pub const widget_enum_WIDGET_A: widget_enum = 0; +pub const widget_enum_WIDGET_B: widget_enum = 1; +pub const widget_enum_WIDGET_C: widget_enum = 2; pub fn b_value_1() -> i32 { - let w: Value = Rc::new(RefCell::new(widget_enum::WIDGET_C)); + let w: Value = Rc::new(RefCell::new(widget_enum_WIDGET_C)); return ((*w.borrow()) as i32); } diff --git a/tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs b/tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs index cc6712183..3da000215 100644 --- a/tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs +++ b/tests/multi-file/cross_tu_tag_collision/out/unsafe/cross_tu_tag_collision.rs @@ -26,25 +26,11 @@ unsafe fn main_0() -> i32 { assert!(((((unsafe { b_value_1() }) == (2)) as i32) != 0)); return 0; } -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum widget_enum { - #[default] - WIDGET_A = 0, - WIDGET_B = 1, - WIDGET_C = 2, -} -impl From for widget_enum { - fn from(n: i32) -> widget_enum { - match n { - 0 => widget_enum::WIDGET_A, - 1 => widget_enum::WIDGET_B, - 2 => widget_enum::WIDGET_C, - _ => panic!("invalid widget_enum value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(widget_enum); +pub type widget_enum = u32; +pub const widget_enum_WIDGET_A: widget_enum = 0; +pub const widget_enum_WIDGET_B: widget_enum = 1; +pub const widget_enum_WIDGET_C: widget_enum = 2; pub unsafe fn b_value_1() -> i32 { - let mut w: widget_enum = widget_enum::WIDGET_C; + let mut w: widget_enum = widget_enum_WIDGET_C; return (w as i32); } diff --git a/tests/ub/enum_out_of_range_cast.cpp b/tests/ub/enum_out_of_range_cast.cpp index ba0ce22da..a12847ecf 100644 --- a/tests/ub/enum_out_of_range_cast.cpp +++ b/tests/ub/enum_out_of_range_cast.cpp @@ -1,4 +1,4 @@ -// panic-ub +// nondet-result enum Color { RED, GREEN, BLUE }; diff --git a/tests/ub/enum_out_of_range_increment.c b/tests/ub/enum_out_of_range_increment.c index 8464fd792..4869daa2f 100644 --- a/tests/ub/enum_out_of_range_increment.c +++ b/tests/ub/enum_out_of_range_increment.c @@ -1,4 +1,4 @@ -// panic-ub +// nondet-result enum color { RED, GREEN, BLUE }; diff --git a/tests/ub/out/refcount/enum_out_of_range_cast.rs b/tests/ub/out/refcount/enum_out_of_range_cast.rs index cdd50986f..3ca857102 100644 --- a/tests/ub/out/refcount/enum_out_of_range_cast.rs +++ b/tests/ub/out/refcount/enum_out_of_range_cast.rs @@ -6,39 +6,17 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Color { - #[default] - RED = 0, - GREEN = 1, - BLUE = 2, -} -impl From for Color { - fn from(n: i32) -> Color { - match n { - 0 => Color::RED, - 1 => Color::GREEN, - 2 => Color::BLUE, - _ => panic!("invalid Color value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Color); -impl ByteRepr for Color { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type Color = u32; +pub const Color_RED: Color = 0; +pub const Color_GREEN: Color = 1; +pub const Color_BLUE: Color = 2; pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { let n: Value = Rc::new(RefCell::new(3)); - let c: Value = Rc::new(RefCell::new(Color::from((*n.borrow())))); - return if (((*c.borrow()) as i32) == (Color::BLUE as i32)) { + let c: Value = Rc::new(RefCell::new(((*n.borrow()) as Color))); + return if (((*c.borrow()) as i32) == (Color_BLUE as i32)) { 0 } else { 1 diff --git a/tests/ub/out/refcount/enum_out_of_range_increment.rs b/tests/ub/out/refcount/enum_out_of_range_increment.rs index 1b764f171..0b88267bb 100644 --- a/tests/ub/out/refcount/enum_out_of_range_increment.rs +++ b/tests/ub/out/refcount/enum_out_of_range_increment.rs @@ -6,39 +6,17 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum color { - #[default] - RED = 0, - GREEN = 1, - BLUE = 2, -} -impl From for color { - fn from(n: i32) -> color { - match n { - 0 => color::RED, - 1 => color::GREEN, - 2 => color::BLUE, - _ => panic!("invalid color value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(color); -impl ByteRepr for color { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type color = u32; +pub const color_RED: color = 0; +pub const color_GREEN: color = 1; +pub const color_BLUE: color = 2; pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let c: Value = Rc::new(RefCell::new(color::BLUE)); + let c: Value = Rc::new(RefCell::new(color_BLUE)); (*c.borrow_mut()).postfix_inc(); - return if (((((*c.borrow()) as u32) == ((color::RED as i32) as u32)) as i32) != 0) { + return if (((((*c.borrow()) as u32) == ((color_RED as i32) as u32)) as i32) != 0) { 0 } else { 1 diff --git a/tests/ub/out/unsafe/enum_out_of_range_cast.rs b/tests/ub/out/unsafe/enum_out_of_range_cast.rs index 79ef0be64..981690e48 100644 --- a/tests/ub/out/unsafe/enum_out_of_range_cast.rs +++ b/tests/ub/out/unsafe/enum_out_of_range_cast.rs @@ -6,24 +6,10 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Color { - #[default] - RED = 0, - GREEN = 1, - BLUE = 2, -} -impl From for Color { - fn from(n: i32) -> Color { - match n { - 0 => Color::RED, - 1 => Color::GREEN, - 2 => Color::BLUE, - _ => panic!("invalid Color value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Color); +pub type Color = u32; +pub const Color_RED: Color = 0; +pub const Color_GREEN: Color = 1; +pub const Color_BLUE: Color = 2; pub fn main() { unsafe { std::process::exit(main_0() as i32); @@ -31,8 +17,8 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut n: i32 = 3; - let mut c: Color = Color::from(n); - return if ((c as i32) == (Color::BLUE as i32)) { + let mut c: Color = ((n) as Color); + return if ((c as i32) == (Color_BLUE as i32)) { 0 } else { 1 diff --git a/tests/ub/out/unsafe/enum_out_of_range_increment.rs b/tests/ub/out/unsafe/enum_out_of_range_increment.rs index ce87da65e..993fec1b0 100644 --- a/tests/ub/out/unsafe/enum_out_of_range_increment.rs +++ b/tests/ub/out/unsafe/enum_out_of_range_increment.rs @@ -6,33 +6,19 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum color { - #[default] - RED = 0, - GREEN = 1, - BLUE = 2, -} -impl From for color { - fn from(n: i32) -> color { - match n { - 0 => color::RED, - 1 => color::GREEN, - 2 => color::BLUE, - _ => panic!("invalid color value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(color); +pub type color = u32; +pub const color_RED: color = 0; +pub const color_GREEN: color = 1; +pub const color_BLUE: color = 2; pub fn main() { unsafe { std::process::exit(main_0() as i32); } } unsafe fn main_0() -> i32 { - let mut c: color = color::BLUE; + let mut c: color = color_BLUE; c.postfix_inc(); - return if ((((c as u32) == ((color::RED as i32) as u32)) as i32) != 0) { + return if ((((c as u32) == ((color_RED as i32) as u32)) as i32) != 0) { 0 } else { 1 diff --git a/tests/unit/enum_comparisson_in_globals.cpp b/tests/unit/enum_comparisson_in_globals.cpp new file mode 100644 index 000000000..eff70a78d --- /dev/null +++ b/tests/unit/enum_comparisson_in_globals.cpp @@ -0,0 +1,10 @@ +#include + +enum E { A, B }; + +int global = E::A != E::B; + +int main() { + assert(global == 1); + return 0; +} diff --git a/tests/unit/enum_with_duplicated_values.cpp b/tests/unit/enum_with_duplicated_values.cpp new file mode 100644 index 000000000..6bbd178bf --- /dev/null +++ b/tests/unit/enum_with_duplicated_values.cpp @@ -0,0 +1,16 @@ +#include + +enum E { + A = 1 << 1, + B = 1 << 2, + C = A, + D = 1 << 2, +}; + +int main() { + assert(E::A == 1 << 1); + assert(E::B == 1 << 2); + assert(E::C == 1 << 1); + assert(E::D == 1 << 2); + return 0; +} diff --git a/tests/unit/out/refcount/anonymous_enum.rs b/tests/unit/out/refcount/anonymous_enum.rs index 780a7f05e..4929546d5 100644 --- a/tests/unit/out/refcount/anonymous_enum.rs +++ b/tests/unit/out/refcount/anonymous_enum.rs @@ -6,54 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_0 { - #[default] - FIRST_A = 0, - FIRST_B = 1, -} -impl From for anon_0 { - fn from(n: i32) -> anon_0 { - match n { - 0 => anon_0::FIRST_A, - 1 => anon_0::FIRST_B, - _ => panic!("invalid anon_0 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_0); -impl ByteRepr for anon_0 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_1 { - #[default] - SECOND_A = 0, - SECOND_B = 1, -} -impl From for anon_1 { - fn from(n: i32) -> anon_1 { - match n { - 0 => anon_1::SECOND_A, - 1 => anon_1::SECOND_B, - _ => panic!("invalid anon_1 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_1); -impl ByteRepr for anon_1 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type anon_0 = u32; +pub const anon_0_FIRST_A: anon_0 = 0; +pub const anon_0_FIRST_B: anon_0 = 1; +pub type anon_1 = u32; +pub const anon_1_SECOND_A: anon_1 = 0; +pub const anon_1_SECOND_B: anon_1 = 1; #[derive(Default)] pub struct S { pub a: Value, @@ -79,54 +37,12 @@ impl ByteRepr for S { } } } -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum TdEnum { - #[default] - TD_A = 0, - TD_B = 1, -} -impl From for TdEnum { - fn from(n: i32) -> TdEnum { - match n { - 0 => TdEnum::TD_A, - 1 => TdEnum::TD_B, - _ => panic!("invalid TdEnum value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(TdEnum); -impl ByteRepr for TdEnum { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_2 { - #[default] - FIELD_A = 0, - FIELD_B = 1, -} -impl From for anon_2 { - fn from(n: i32) -> anon_2 { - match n { - 0 => anon_2::FIELD_A, - 1 => anon_2::FIELD_B, - _ => panic!("invalid anon_2 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_2); -impl ByteRepr for anon_2 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type TdEnum = u32; +pub const TdEnum_TD_A: TdEnum = 0; +pub const TdEnum_TD_B: TdEnum = 1; +pub type anon_2 = u32; +pub const anon_2_FIELD_A: anon_2 = 0; +pub const anon_2_FIELD_B: anon_2 = 1; #[derive(Default)] pub struct WithAnonField { pub a: Value, @@ -160,41 +76,20 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - #[derive(Clone, Copy, PartialEq, Debug, Default)] - enum anon_3 { - #[default] - THIRD_A = 0, - THIRD_B = 1, - } - impl From for anon_3 { - fn from(n: i32) -> anon_3 { - match n { - 0 => anon_3::THIRD_A, - 1 => anon_3::THIRD_B, - _ => panic!("invalid anon_3 value: {}", n), - } - } - } - libcc2rs::impl_enum_inc_dec!(anon_3); - impl ByteRepr for anon_3 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } - }; - assert!(((anon_0::FIRST_A as i32) != (anon_0::FIRST_B as i32))); - assert!(((anon_1::SECOND_A as i32) != (anon_1::SECOND_B as i32))); - assert!(((anon_3::THIRD_A as i32) != (anon_3::THIRD_B as i32))); - let td: Value = Rc::new(RefCell::new(TdEnum::TD_A)); - assert!((((*td.borrow()) as i32) == (TdEnum::TD_A as i32))); - (*td.borrow_mut()) = TdEnum::TD_B; - assert!((((*td.borrow()) as i32) == (TdEnum::TD_B as i32))); + pub type anon_3 = u32; + pub const anon_3_THIRD_A: anon_3 = 0; + pub const anon_3_THIRD_B: anon_3 = 1;; + assert!(((anon_0_FIRST_A as i32) != (anon_0_FIRST_B as i32))); + assert!(((anon_1_SECOND_A as i32) != (anon_1_SECOND_B as i32))); + assert!(((anon_3_THIRD_A as i32) != (anon_3_THIRD_B as i32))); + let td: Value = Rc::new(RefCell::new(TdEnum_TD_A)); + assert!((((*td.borrow()) as i32) == (TdEnum_TD_A as i32))); + (*td.borrow_mut()) = TdEnum_TD_B; + assert!((((*td.borrow()) as i32) == (TdEnum_TD_B as i32))); let w: Value = Rc::new(RefCell::new(::default())); - (*(*w.borrow()).field.borrow_mut()) = anon_2::FIELD_A; - assert!((((*(*w.borrow()).field.borrow()) as i32) == (anon_2::FIELD_A as i32))); - (*(*w.borrow()).field.borrow_mut()) = anon_2::FIELD_B; - assert!((((*(*w.borrow()).field.borrow()) as i32) == (anon_2::FIELD_B as i32))); + (*(*w.borrow()).field.borrow_mut()) = anon_2_FIELD_A; + assert!((((*(*w.borrow()).field.borrow()) as i32) == (anon_2_FIELD_A as i32))); + (*(*w.borrow()).field.borrow_mut()) = anon_2_FIELD_B; + assert!((((*(*w.borrow()).field.borrow()) as i32) == (anon_2_FIELD_B as i32))); return 0; } diff --git a/tests/unit/out/refcount/anonymous_enum_c.rs b/tests/unit/out/refcount/anonymous_enum_c.rs index 5aa61233d..82240be28 100644 --- a/tests/unit/out/refcount/anonymous_enum_c.rs +++ b/tests/unit/out/refcount/anonymous_enum_c.rs @@ -6,54 +6,12 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_0 { - #[default] - FIRST_A = 0, - FIRST_B = 1, -} -impl From for anon_0 { - fn from(n: i32) -> anon_0 { - match n { - 0 => anon_0::FIRST_A, - 1 => anon_0::FIRST_B, - _ => panic!("invalid anon_0 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_0); -impl ByteRepr for anon_0 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_1 { - #[default] - SECOND_A = 0, - SECOND_B = 1, -} -impl From for anon_1 { - fn from(n: i32) -> anon_1 { - match n { - 0 => anon_1::SECOND_A, - 1 => anon_1::SECOND_B, - _ => panic!("invalid anon_1 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_1); -impl ByteRepr for anon_1 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type anon_0 = u32; +pub const anon_0_FIRST_A: anon_0 = 0; +pub const anon_0_FIRST_B: anon_0 = 1; +pub type anon_1 = u32; +pub const anon_1_SECOND_A: anon_1 = 0; +pub const anon_1_SECOND_B: anon_1 = 1; #[derive(Default)] pub struct S { pub a: Value, @@ -78,54 +36,12 @@ impl ByteRepr for S { } } } -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum TdEnum_enum { - #[default] - TD_A = 0, - TD_B = 1, -} -impl From for TdEnum_enum { - fn from(n: i32) -> TdEnum_enum { - match n { - 0 => TdEnum_enum::TD_A, - 1 => TdEnum_enum::TD_B, - _ => panic!("invalid TdEnum_enum value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(TdEnum_enum); -impl ByteRepr for TdEnum_enum { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum anon_2 { - #[default] - FIELD_A = 0, - FIELD_B = 1, -} -impl From for anon_2 { - fn from(n: i32) -> anon_2 { - match n { - 0 => anon_2::FIELD_A, - 1 => anon_2::FIELD_B, - _ => panic!("invalid anon_2 value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(anon_2); -impl ByteRepr for anon_2 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type TdEnum_enum = u32; +pub const TdEnum_enum_TD_A: TdEnum_enum = 0; +pub const TdEnum_enum_TD_B: TdEnum_enum = 1; +pub type anon_2 = u32; +pub const anon_2_FIELD_A: anon_2 = 0; +pub const anon_2_FIELD_B: anon_2 = 1; #[derive(Default)] pub struct WithAnonField { pub a: Value, @@ -158,46 +74,25 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - #[derive(Clone, Copy, PartialEq, Debug, Default)] - enum anon_3 { - #[default] - THIRD_A = 0, - THIRD_B = 1, - } - impl From for anon_3 { - fn from(n: i32) -> anon_3 { - match n { - 0 => anon_3::THIRD_A, - 1 => anon_3::THIRD_B, - _ => panic!("invalid anon_3 value: {}", n), - } - } - } - libcc2rs::impl_enum_inc_dec!(anon_3); - impl ByteRepr for anon_3 { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } - }; - assert!(((((anon_0::FIRST_A as i32) != (anon_0::FIRST_B as i32)) as i32) != 0)); - assert!(((((anon_1::SECOND_A as i32) != (anon_1::SECOND_B as i32)) as i32) != 0)); - assert!(((((anon_3::THIRD_A as i32) != (anon_3::THIRD_B as i32)) as i32) != 0)); - let td: Value = Rc::new(RefCell::new(TdEnum_enum::TD_A)); - assert!((((((*td.borrow()) as u32) == ((TdEnum_enum::TD_A as i32) as u32)) as i32) != 0)); - (*td.borrow_mut()) = TdEnum_enum::TD_B; - assert!((((((*td.borrow()) as u32) == ((TdEnum_enum::TD_B as i32) as u32)) as i32) != 0)); + pub type anon_3 = u32; + pub const anon_3_THIRD_A: anon_3 = 0; + pub const anon_3_THIRD_B: anon_3 = 1;; + assert!(((((anon_0_FIRST_A as i32) != (anon_0_FIRST_B as i32)) as i32) != 0)); + assert!(((((anon_1_SECOND_A as i32) != (anon_1_SECOND_B as i32)) as i32) != 0)); + assert!(((((anon_3_THIRD_A as i32) != (anon_3_THIRD_B as i32)) as i32) != 0)); + let td: Value = Rc::new(RefCell::new(TdEnum_enum_TD_A)); + assert!((((((*td.borrow()) as u32) == ((TdEnum_enum_TD_A as i32) as u32)) as i32) != 0)); + (*td.borrow_mut()) = TdEnum_enum_TD_B; + assert!((((((*td.borrow()) as u32) == ((TdEnum_enum_TD_B as i32) as u32)) as i32) != 0)); let w: Value = >::default(); - (*(*w.borrow()).field.borrow_mut()) = anon_2::FIELD_A; + (*(*w.borrow()).field.borrow_mut()) = anon_2_FIELD_A; assert!( - (((((*(*w.borrow()).field.borrow()) as u32) == ((anon_2::FIELD_A as i32) as u32)) as i32) + (((((*(*w.borrow()).field.borrow()) as u32) == ((anon_2_FIELD_A as i32) as u32)) as i32) != 0) ); - (*(*w.borrow()).field.borrow_mut()) = anon_2::FIELD_B; + (*(*w.borrow()).field.borrow_mut()) = anon_2_FIELD_B; assert!( - (((((*(*w.borrow()).field.borrow()) as u32) == ((anon_2::FIELD_B as i32) as u32)) as i32) + (((((*(*w.borrow()).field.borrow()) as u32) == ((anon_2_FIELD_B as i32) as u32)) as i32) != 0) ); return 0; diff --git a/tests/unit/out/refcount/bool_condition_enum.rs b/tests/unit/out/refcount/bool_condition_enum.rs index 540855e4c..4d32a20e9 100644 --- a/tests/unit/out/refcount/bool_condition_enum.rs +++ b/tests/unit/out/refcount/bool_condition_enum.rs @@ -6,53 +6,31 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Code { - #[default] - CODE_OK = 0, - CODE_ERR = 1, - CODE_FATAL = 2, -} -impl From for Code { - fn from(n: i32) -> Code { - match n { - 0 => Code::CODE_OK, - 1 => Code::CODE_ERR, - 2 => Code::CODE_FATAL, - _ => panic!("invalid Code value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Code); -impl ByteRepr for Code { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type Code = u32; +pub const Code_CODE_OK: Code = 0; +pub const Code_CODE_ERR: Code = 1; +pub const Code_CODE_FATAL: Code = 2; pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let code: Value = Rc::new(RefCell::new(Code::CODE_OK)); - let err: Value = Rc::new(RefCell::new(Code::CODE_ERR)); - if ((*code.borrow()) != Code::from(0)) { + let code: Value = Rc::new(RefCell::new(Code_CODE_OK)); + let err: Value = Rc::new(RefCell::new(Code_CODE_ERR)); + if ((*code.borrow()) != 0) { assert!(false); } - if !((*code.borrow()) != Code::from(0)) { + if !((*code.borrow()) != 0) { assert!(true); } - if ((*err.borrow()) != Code::from(0)) { + if ((*err.borrow()) != 0) { assert!(true); } - if !((*err.borrow()) != Code::from(0)) { + if !((*err.borrow()) != 0) { assert!(false); } - let t9: Value = Rc::new(RefCell::new((!((*code.borrow()) != Code::from(0)) as i32))); + let t9: Value = Rc::new(RefCell::new((!((*code.borrow()) != 0) as i32))); assert!(((*t9.borrow()) == 1)); - let b4: Value = Rc::new(RefCell::new(((*code.borrow()) != Code::from(0)))); + let b4: Value = Rc::new(RefCell::new(((*code.borrow()) != 0))); assert!(!(*b4.borrow())); return 0; } diff --git a/tests/unit/out/refcount/bool_condition_enum_c.rs b/tests/unit/out/refcount/bool_condition_enum_c.rs index f8ed68b4e..c36390048 100644 --- a/tests/unit/out/refcount/bool_condition_enum_c.rs +++ b/tests/unit/out/refcount/bool_condition_enum_c.rs @@ -6,53 +6,31 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Code { - #[default] - CODE_OK = 0, - CODE_ERR = 1, - CODE_FATAL = 2, -} -impl From for Code { - fn from(n: i32) -> Code { - match n { - 0 => Code::CODE_OK, - 1 => Code::CODE_ERR, - 2 => Code::CODE_FATAL, - _ => panic!("invalid Code value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Code); -impl ByteRepr for Code { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type Code = u32; +pub const Code_CODE_OK: Code = 0; +pub const Code_CODE_ERR: Code = 1; +pub const Code_CODE_FATAL: Code = 2; pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - let code: Value = Rc::new(RefCell::new(Code::CODE_OK)); - let err: Value = Rc::new(RefCell::new(Code::CODE_ERR)); - if ((*code.borrow()) != Code::from(0)) { + let code: Value = Rc::new(RefCell::new(Code_CODE_OK)); + let err: Value = Rc::new(RefCell::new(Code_CODE_ERR)); + if ((*code.borrow()) != 0) { assert!((0 != 0)); } - if !((*code.borrow()) != Code::from(0)) { + if !((*code.borrow()) != 0) { assert!((1 != 0)); } - if ((*err.borrow()) != Code::from(0)) { + if ((*err.borrow()) != 0) { assert!((1 != 0)); } - if !((*err.borrow()) != Code::from(0)) { + if !((*err.borrow()) != 0) { assert!((0 != 0)); } - let t9: Value = Rc::new(RefCell::new((!((*code.borrow()) != Code::from(0)) as i32))); + let t9: Value = Rc::new(RefCell::new((!((*code.borrow()) != 0) as i32))); assert!(((((*t9.borrow()) == 1) as i32) != 0)); - let b4: Value = Rc::new(RefCell::new(((*code.borrow()) != Code::from(0)))); + let b4: Value = Rc::new(RefCell::new(((*code.borrow()) != 0))); assert!(((!(*b4.borrow()) as i32) != 0)); return 0; } diff --git a/tests/unit/out/refcount/bool_condition_logical.rs b/tests/unit/out/refcount/bool_condition_logical.rs index 815ea3f86..25ebf4e35 100644 --- a/tests/unit/out/refcount/bool_condition_logical.rs +++ b/tests/unit/out/refcount/bool_condition_logical.rs @@ -6,32 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Code { - #[default] - CODE_OK = 0, - CODE_ERR = 1, - CODE_FATAL = 2, -} -impl From for Code { - fn from(n: i32) -> Code { - match n { - 0 => Code::CODE_OK, - 1 => Code::CODE_ERR, - 2 => Code::CODE_FATAL, - _ => panic!("invalid Code value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Code); -impl ByteRepr for Code { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type Code = u32; +pub const Code_CODE_OK: Code = 0; +pub const Code_CODE_ERR: Code = 1; +pub const Code_CODE_FATAL: Code = 2; thread_local!( pub static side_effect_0: Value = Rc::new(RefCell::new(0)); ); @@ -56,7 +34,7 @@ fn main_0() -> i32 { let p: Value> = Rc::new(RefCell::new((storage.as_pointer()))); let np: Value> = Rc::new(RefCell::new(Ptr::::null())); let u: Value = Rc::new(RefCell::new(4_u32)); - let code: Value = Rc::new(RefCell::new(Code::CODE_OK)); + let code: Value = Rc::new(RefCell::new(Code_CODE_OK)); if ((*n.borrow()) != 0) && (!(*p.borrow()).is_null()) { assert!(true); } @@ -70,7 +48,7 @@ fn main_0() -> i32 { assert!(false); } if ((((*n.borrow()) != 0) && ((*u.borrow()) != 0)) && (!(*p.borrow()).is_null())) - && (((*code.borrow()) as i32) == (Code::CODE_OK as i32)) + && (((*code.borrow()) as i32) == (Code_CODE_OK as i32)) { assert!(true); } diff --git a/tests/unit/out/refcount/bool_condition_logical_c.rs b/tests/unit/out/refcount/bool_condition_logical_c.rs index e0bae2740..210c7b3e9 100644 --- a/tests/unit/out/refcount/bool_condition_logical_c.rs +++ b/tests/unit/out/refcount/bool_condition_logical_c.rs @@ -6,32 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Code { - #[default] - CODE_OK = 0, - CODE_ERR = 1, - CODE_FATAL = 2, -} -impl From for Code { - fn from(n: i32) -> Code { - match n { - 0 => Code::CODE_OK, - 1 => Code::CODE_ERR, - 2 => Code::CODE_FATAL, - _ => panic!("invalid Code value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Code); -impl ByteRepr for Code { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type Code = u32; +pub const Code_CODE_OK: Code = 0; +pub const Code_CODE_ERR: Code = 1; +pub const Code_CODE_FATAL: Code = 2; thread_local!( pub static side_effect_0: Value = Rc::new(RefCell::new(0)); ); @@ -56,7 +34,7 @@ fn main_0() -> i32 { let p: Value> = Rc::new(RefCell::new((storage.as_pointer()))); let np: Value> = Rc::new(RefCell::new(Ptr::::null())); let u: Value = Rc::new(RefCell::new(4_u32)); - let code: Value = Rc::new(RefCell::new(Code::CODE_OK)); + let code: Value = Rc::new(RefCell::new(Code_CODE_OK)); if (((((*n.borrow()) != 0) && (!(*p.borrow()).is_null())) as i32) != 0) { assert!((1 != 0)); } @@ -72,7 +50,7 @@ fn main_0() -> i32 { if (((((((((((*n.borrow()) != 0) && ((*u.borrow()) != 0)) as i32) != 0) && (!(*p.borrow()).is_null())) as i32) != 0) - && (((((*code.borrow()) as u32) == ((Code::CODE_OK as i32) as u32)) as i32) != 0)) + && (((((*code.borrow()) as u32) == ((Code_CODE_OK as i32) as u32)) as i32) != 0)) as i32) != 0) { diff --git a/tests/unit/out/refcount/c_struct.rs b/tests/unit/out/refcount/c_struct.rs index d70a75539..fd1e1b3ee 100644 --- a/tests/unit/out/refcount/c_struct.rs +++ b/tests/unit/out/refcount/c_struct.rs @@ -90,32 +90,10 @@ impl ByteRepr for Node { } } } -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Color { - #[default] - RED = 0, - GREEN = 1, - BLUE = 2, -} -impl From for Color { - fn from(n: i32) -> Color { - match n { - 0 => Color::RED, - 1 => Color::GREEN, - 2 => Color::BLUE, - _ => panic!("invalid Color value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Color); -impl ByteRepr for Color { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type Color = u32; +pub const Color_RED: Color = 0; +pub const Color_GREEN: Color = 1; +pub const Color_BLUE: Color = 2; #[derive(Default)] pub struct Inner { pub a: Value, @@ -223,18 +201,17 @@ fn main_0() -> i32 { a: Rc::new(RefCell::new(5)), b: Rc::new(RefCell::new(6)), })), - color: Rc::new(RefCell::new(Color::GREEN)), + color: Rc::new(RefCell::new(Color_GREEN)), count: Rc::new(RefCell::new(42)), })); assert!(((((*(*(*c.borrow()).inner.borrow()).a.borrow()) == 5) as i32) != 0)); assert!(((((*(*(*c.borrow()).inner.borrow()).b.borrow()) == 6) as i32) != 0)); assert!( - (((((*(*c.borrow()).color.borrow()) as u32) == ((Color::GREEN as i32) as u32)) as i32) - != 0) + (((((*(*c.borrow()).color.borrow()) as u32) == ((Color_GREEN as i32) as u32)) as i32) != 0) ); assert!(((((*(*c.borrow()).count.borrow()) == 42) as i32) != 0)); let c2: Value = >::default(); - (*(*c2.borrow()).color.borrow_mut()) = Color::BLUE; + (*(*c2.borrow()).color.borrow_mut()) = Color_BLUE; assert!((((((*(*c2.borrow()).color.borrow()) as u32) == 2_u32) as i32) != 0)); return 0; } diff --git a/tests/unit/out/refcount/enum_comparisson_in_globals.rs b/tests/unit/out/refcount/enum_comparisson_in_globals.rs new file mode 100644 index 000000000..c5cb7a392 --- /dev/null +++ b/tests/unit/out/refcount/enum_comparisson_in_globals.rs @@ -0,0 +1,22 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub type E = u32; +pub const E_A: E = 0; +pub const E_B: E = 1; +thread_local!( + pub static global_0: Value = + Rc::new(RefCell::new((((E_A as i32) != (E_B as i32)) as i32))); +); +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!(((*global_0.with(Value::clone).borrow()) == 1)); + return 0; +} diff --git a/tests/unit/out/refcount/enum_default_in_static.rs b/tests/unit/out/refcount/enum_default_in_static.rs index 16aac0053..6c8ee7a36 100644 --- a/tests/unit/out/refcount/enum_default_in_static.rs +++ b/tests/unit/out/refcount/enum_default_in_static.rs @@ -6,32 +6,10 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Mode { - #[default] - MODE_NONE = 0, - MODE_ONE = 1, - MODE_TWO = 2, -} -impl From for Mode { - fn from(n: i32) -> Mode { - match n { - 0 => Mode::MODE_NONE, - 1 => Mode::MODE_ONE, - 2 => Mode::MODE_TWO, - _ => panic!("invalid Mode value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Mode); -impl ByteRepr for Mode { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type Mode = u32; +pub const Mode_MODE_NONE: Mode = 0; +pub const Mode_MODE_ONE: Mode = 1; +pub const Mode_MODE_TWO: Mode = 2; #[derive(Default)] pub struct Config { pub count: Value, @@ -70,7 +48,7 @@ fn main_0() -> i32 { assert!(((((*(*config_0.with(Value::clone).borrow()).count.borrow()) == 0) as i32) != 0)); assert!( (((((*(*config_0.with(Value::clone).borrow()).mode.borrow()) as u32) - == ((Mode::MODE_NONE as i32) as u32)) as i32) + == ((Mode_MODE_NONE as i32) as u32)) as i32) != 0) ); return 0; diff --git a/tests/unit/out/refcount/enum_increment.rs b/tests/unit/out/refcount/enum_increment.rs index 387ac2b60..7df0bc1a6 100644 --- a/tests/unit/out/refcount/enum_increment.rs +++ b/tests/unit/out/refcount/enum_increment.rs @@ -6,57 +6,34 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum color { - #[default] - RED = 0, - GREEN = 1, - BLUE = 2, - COLOR_LAST = 3, -} -impl From for color { - fn from(n: i32) -> color { - match n { - 0 => color::RED, - 1 => color::GREEN, - 2 => color::BLUE, - 3 => color::COLOR_LAST, - _ => panic!("invalid color value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(color); -impl ByteRepr for color { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} +pub type color = u32; +pub const color_RED: color = 0; +pub const color_GREEN: color = 1; +pub const color_BLUE: color = 2; +pub const color_COLOR_LAST: color = 3; pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { let count: Value = Rc::new(RefCell::new(0)); - let c: Value = Rc::new(RefCell::new(color::RED)); - 'loop_: while (((((*c.borrow()) as u32) < ((color::COLOR_LAST as i32) as u32)) as i32) != 0) { + let c: Value = Rc::new(RefCell::new(color_RED)); + 'loop_: while (((((*c.borrow()) as u32) < ((color_COLOR_LAST as i32) as u32)) as i32) != 0) { (*count.borrow_mut()).postfix_inc(); (*c.borrow_mut()).postfix_inc(); } assert!(((((*count.borrow()) == 3) as i32) != 0)); - let c: Value = Rc::new(RefCell::new(color::RED)); + let c: Value = Rc::new(RefCell::new(color_RED)); assert!( - (((((*c.borrow_mut()).postfix_inc() as u32) == ((color::RED as i32) as u32)) as i32) != 0) + (((((*c.borrow_mut()).postfix_inc() as u32) == ((color_RED as i32) as u32)) as i32) != 0) ); assert!( - (((((*c.borrow_mut()).prefix_inc() as u32) == ((color::BLUE as i32) as u32)) as i32) != 0) + (((((*c.borrow_mut()).prefix_inc() as u32) == ((color_BLUE as i32) as u32)) as i32) != 0) ); assert!( - (((((*c.borrow_mut()).postfix_dec() as u32) == ((color::BLUE as i32) as u32)) as i32) != 0) + (((((*c.borrow_mut()).postfix_dec() as u32) == ((color_BLUE as i32) as u32)) as i32) != 0) ); assert!( - (((((*c.borrow_mut()).prefix_dec() as u32) == ((color::RED as i32) as u32)) as i32) != 0) + (((((*c.borrow_mut()).prefix_dec() as u32) == ((color_RED as i32) as u32)) as i32) != 0) ); return 0; } diff --git a/tests/unit/out/refcount/enum_int_interop.rs b/tests/unit/out/refcount/enum_int_interop.rs index 99a43eefd..40d9bbee9 100644 --- a/tests/unit/out/refcount/enum_int_interop.rs +++ b/tests/unit/out/refcount/enum_int_interop.rs @@ -6,86 +6,19 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Color { - #[default] - RED = 0, - GREEN = 1, - BLUE = 2, -} -impl From for Color { - fn from(n: i32) -> Color { - match n { - 0 => Color::RED, - 1 => Color::GREEN, - 2 => Color::BLUE, - _ => panic!("invalid Color value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Color); -impl ByteRepr for Color { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { - ::from(i32::from_bytes(buf)) - } -} -#[derive(Clone, Copy, PartialEq, Debug, Default)] -enum Option { - #[default] - OPT_NONE = 0, - OPT_A = 10, - OPT_B = 20, - OPT_C = 30, -} -impl From for Option { - fn from(n: i32) -> Option { - match n { - 0 => Option::OPT_NONE, - 10 => Option::OPT_A, - 20 => Option::OPT_B, - 30 => Option::OPT_C, - _ => panic!("invalid Option value: {}", n), - } - } -} -libcc2rs::impl_enum_inc_dec!(Option); -impl ByteRepr for Option { - fn to_bytes(&self, buf: &mut [u8]) { - (*self as i32).to_bytes(buf); - } - fn from_bytes(buf: &[u8]) -> Self { -