diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index d4a591edc..067a79ed5 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -116,7 +116,7 @@ bool Converter::VisitRecoveryExpr(clang::RecoveryExpr *expr) { bool Converter::Convert(clang::QualType qual_type) { // Catch va_list before desugaring if (IsVaListType(qual_type)) { - StrCat("VaList"); + StrCat("*const VaArg"); return false; } @@ -311,7 +311,7 @@ bool Converter::VisitPointerType(clang::PointerType *type) { } if (IsVaListType(clang::QualType(type, 0))) { - StrCat("VaList"); + StrCat("*const VaArg"); return false; } @@ -480,7 +480,8 @@ void Converter::ConvertVaListVarDecl(clang::VarDecl *decl) { // va_list local variable StrCat(keyword::kLet); } - StrCat(keyword_mut_, GetNamedDeclAsString(decl), token::kColon, "VaList"); + StrCat(keyword_mut_, GetNamedDeclAsString(decl), token::kColon, + "*const VaArg"); } bool Converter::NeedsMut(const clang::VarDecl *decl, clang::QualType type, @@ -1838,8 +1839,7 @@ void Converter::ConvertVariadicArg(clang::Expr *arg) { void Converter::ConvertVAArgCall(clang::CallExpr *expr) { if (IsBuiltinVaStart(expr)) { - StrCat(ToString(expr->getArg(0)->IgnoreImpCasts()), - "= VaList::new(__args)"); + StrCat(ToString(expr->getArg(0)->IgnoreImpCasts()), "= __args.as_ptr()"); return; } if (IsBuiltinVaEnd(expr)) { @@ -4252,7 +4252,7 @@ std::string Converter::GetDefaultAsString(clang::QualType qual_type) { if (IsVaListType(qual_type)) { computed_expr_type_ = ComputedExprType::FreshValue; - return "VaList::default()"; + return "::std::ptr::null()"; } if (auto arr = GetArrayDefaultAsString(qual_type); !arr.empty()) { diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index b96f5d0e4..99772d742 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -259,7 +259,7 @@ std::string ConverterRefCount::BoxValue(std::string &&str) const { bool ConverterRefCount::Convert(clang::QualType qual_type) { // Catch va_list before desugaring if (IsVaListType(qual_type)) { - StrCat(BoxType("VaList")); + StrCat(BoxType("Ptr")); return false; } @@ -291,7 +291,7 @@ bool ConverterRefCount::VisitReferenceType(clang::ReferenceType *type) { PushConversionKind push(*this, ConversionKind::Unboxed); return Convert(ctx_.getPointerType(pointee_type)); } - if (pointee_type->isArrayType()) { + if (pointee_type->isArrayType() && !IsVaListType(pointee_type)) { // A reference to an array decays straight to a pointer to its first // element, the same way a by-value array parameter would, instead of // going through a pointer to the whole boxed array. @@ -325,7 +325,7 @@ bool ConverterRefCount::VisitPointerType(clang::PointerType *type) { } if (IsVaListType(clang::QualType(type, 0))) { - StrCat("VaList"); + StrCat("Ptr"); return false; } @@ -335,10 +335,11 @@ bool ConverterRefCount::VisitPointerType(clang::PointerType *type) { } auto pointee_type = type->getPointeeType(); - PushConversionKind push1(*this, ConversionKind::Ptr, - !pointee_type->isArrayType()); + bool pointee_is_array = + pointee_type->isArrayType() && !IsVaListType(pointee_type); + PushConversionKind push1(*this, ConversionKind::Ptr, !pointee_is_array); PushConversionKind push2(*this, ConversionKind::FullRefCount, - pointee_type->isArrayType()); + pointee_is_array); if (pointee_type->isRecordType() && abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) { StrCat("PtrDynisVariadic()) { + StrCat("let __args: Value> = " + "Rc::new(RefCell::new(__args.into()));"); + } auto params = decl->getDefinition() ? decl->getDefinition()->parameters() : decl->parameters(); for (auto *param : params) { @@ -853,7 +858,7 @@ void ConverterRefCount::ConvertVaListVarDecl(clang::VarDecl *decl) { StrCat(keyword::kLet); } - StrCat(GetNamedDeclAsString(decl), token::kColon, "Value"); + StrCat(GetNamedDeclAsString(decl), token::kColon, "Value>"); } bool ConverterRefCount::ConvertVarDeclSkipInit(clang::VarDecl *decl) { @@ -1291,7 +1296,12 @@ void ConverterRefCount::ConvertPrintf(clang::CallExpr *expr) { } bool ConverterRefCount::VisitCallExpr(clang::CallExpr *expr) { - if (IsBuiltinVaStart(expr) || IsBuiltinVaEnd(expr) || IsBuiltinVaCopy(expr)) { + if (IsBuiltinVaStart(expr)) { + StrCat(ToString(expr->getArg(0)->IgnoreImpCasts()), + "= __args.as_pointer()"); + return false; + } + if (IsBuiltinVaEnd(expr) || IsBuiltinVaCopy(expr)) { ConvertVAArgCall(expr); return false; } @@ -2471,13 +2481,18 @@ bool ConverterRefCount::VisitVAArgExpr(clang::VAArgExpr *expr) { if (auto *cast = clang::dyn_cast(va_list_expr)) { va_list_expr = cast->getSubExpr(); } - StrCat(ConvertLValue(va_list_expr)); - StrCat(".arg::<"); + auto str = ConvertLValue(va_list_expr); + std::string arg_type; { PushConversionKind push(*this, ConversionKind::Unboxed); - StrCat(ToString(expr->getType())); + arg_type = ToString(expr->getType()); + } + if (!pending_deref_.empty()) { + StrCat(pending_deref_.take(), ".with_mut(|__v| __v.arg::<", arg_type, + ">())"); + } else { + StrCat(str, ".arg::<", arg_type, ">()"); } - StrCat(">()"); SetFreshType(expr->getType()); return false; } @@ -2529,7 +2544,7 @@ std::string ConverterRefCount::GetDefaultAsString(clang::QualType qual_type) { if (IsVaListType(qual_type)) { computed_expr_type_ = ComputedExprType::FreshValue; - return BoxValue("VaList::default()"); + return BoxValue("Ptr::::default()"); } if (auto arr = GetArrayDefaultAsString(qual_type); !arr.empty()) { diff --git a/libcc2rs/src/va_args.rs b/libcc2rs/src/va_args.rs index 941b20a3b..b982d8502 100644 --- a/libcc2rs/src/va_args.rs +++ b/libcc2rs/src/va_args.rs @@ -5,6 +5,8 @@ use std::any::Any; use std::ffi::c_void; use std::rc::Rc; +use crate::inc::PrefixInc; +use crate::rc::Ptr; use crate::void::AnyPtr; #[derive(Clone)] @@ -52,21 +54,27 @@ impl From> for VaAr } } -#[derive(Clone, Copy, Default)] -pub struct VaList<'a> { - args: &'a [VaArg], - pos: usize, +impl crate::reinterpret::ByteRepr for VaArg {} + +pub trait VaArgs { + fn arg(&mut self) -> T; } -impl<'a> VaList<'a> { - pub fn new(args: &'a [VaArg]) -> Self { - VaList { args, pos: 0 } +impl VaArgs for Ptr { + fn arg(&mut self) -> T { + let val = self.with(T::get); + self.prefix_inc(); + val } +} - pub fn arg(&mut self) -> T { - let val = &self.args[self.pos]; - self.pos += 1; - T::get(val) +impl VaArgs for *const VaArg { + fn arg(&mut self) -> T { + unsafe { + let val = T::get(&**self); + *self = self.add(1); + val + } } } diff --git a/tests/unit/out/refcount/va_arg_chain.rs b/tests/unit/out/refcount/va_arg_chain.rs index 11a179bcf..c6d738694 100644 --- a/tests/unit/out/refcount/va_arg_chain.rs +++ b/tests/unit/out/refcount/va_arg_chain.rs @@ -6,8 +6,8 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn extract_nth_0(mut n: i32, ap: VaList) -> i32 { - let ap: Value = Rc::new(RefCell::new(ap)); +pub fn extract_nth_0(mut n: i32, ap: Ptr) -> i32 { + let ap: Value> = Rc::new(RefCell::new(ap)); let mut i: i32 = 0; 'loop_: while (((i < n) as i32) != 0) { (*ap.borrow_mut()).arg::(); @@ -15,14 +15,15 @@ pub fn extract_nth_0(mut n: i32, ap: VaList) -> i32 { } return (*ap.borrow_mut()).arg::(); } -pub fn middle_layer_1(mut n: i32, ap: VaList) -> i32 { - let ap: Value = Rc::new(RefCell::new(ap)); +pub fn middle_layer_1(mut n: i32, ap: Ptr) -> i32 { + let ap: Value> = Rc::new(RefCell::new(ap)); return ({ extract_nth_0(n, (*ap.borrow()).clone()) }); } pub fn top_level_2(n: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let n: Value = Rc::new(RefCell::new(n)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut result: i32 = ({ middle_layer_1((*n.borrow()), (*ap.borrow()).clone()) }); return result; } diff --git a/tests/unit/out/refcount/va_arg_concat.rs b/tests/unit/out/refcount/va_arg_concat.rs index e80ea00c7..02efefa8c 100644 --- a/tests/unit/out/refcount/va_arg_concat.rs +++ b/tests/unit/out/refcount/va_arg_concat.rs @@ -7,10 +7,11 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn sum_ints_0(first: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let first: Value = Rc::new(RefCell::new(first)); - let args: Value = Rc::new(RefCell::new(VaList::default())); + let args: Value> = Rc::new(RefCell::new(Ptr::::default())); let mut total: i32 = (*first.borrow()); - (*args.borrow_mut()) = VaList::new(__args); + (*args.borrow_mut()) = __args.as_pointer(); let mut val: i32 = 0_i32; 'loop_: while (((({ val = (*args.borrow_mut()).arg::(); diff --git a/tests/unit/out/refcount/va_arg_conditional.rs b/tests/unit/out/refcount/va_arg_conditional.rs index 3a7e44b2e..d514efe14 100644 --- a/tests/unit/out/refcount/va_arg_conditional.rs +++ b/tests/unit/out/refcount/va_arg_conditional.rs @@ -7,10 +7,11 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn conditional_log_0(mut verbose: i32, fmt: Ptr, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let fmt: Value> = Rc::new(RefCell::new(fmt)); if (verbose != 0) { - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut result: i32 = (*ap.borrow_mut()).arg::(); return result; } diff --git a/tests/unit/out/refcount/va_arg_copy.rs b/tests/unit/out/refcount/va_arg_copy.rs index dc26a093d..2aabfaf04 100644 --- a/tests/unit/out/refcount/va_arg_copy.rs +++ b/tests/unit/out/refcount/va_arg_copy.rs @@ -7,10 +7,11 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn sum_with_copy_0(count: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - let aq: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + let aq: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); (*aq.borrow_mut()) = (*ap.borrow_mut()).clone(); let mut sum1: i32 = 0; let mut i: i32 = 0; diff --git a/tests/unit/out/refcount/va_arg_fn_ptr.rs b/tests/unit/out/refcount/va_arg_fn_ptr.rs index 430bd2293..b5945c798 100644 --- a/tests/unit/out/refcount/va_arg_fn_ptr.rs +++ b/tests/unit/out/refcount/va_arg_fn_ptr.rs @@ -16,17 +16,19 @@ pub fn add_2(mut a: i32, mut b: i32) -> i32 { return (a + b); } pub fn apply_unary_3(x: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let x: Value = Rc::new(RefCell::new(x)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut fn_: FnPtr i32> = (*ap.borrow_mut()).arg:: i32>>(); let mut result: i32 = ({ fn_.call((*x.borrow())) }); return result; } pub fn apply_binary_4(mut a: i32, b: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let b: Value = Rc::new(RefCell::new(b)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut fn_: FnPtr i32> = (*ap.borrow_mut()).arg:: i32>>(); let mut result: i32 = ({ fn_.call(a, (*b.borrow())) }); diff --git a/tests/unit/out/refcount/va_arg_forward.rs b/tests/unit/out/refcount/va_arg_forward.rs index 972f46857..26265a108 100644 --- a/tests/unit/out/refcount/va_arg_forward.rs +++ b/tests/unit/out/refcount/va_arg_forward.rs @@ -6,8 +6,8 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn inner_0(mut count: i32, ap: VaList) -> i32 { - let ap: Value = Rc::new(RefCell::new(ap)); +pub fn inner_0(mut count: i32, ap: Ptr) -> i32 { + let ap: Value> = Rc::new(RefCell::new(ap)); let mut total: i32 = 0; let mut i: i32 = 0; 'loop_: while (((i < count) as i32) != 0) { @@ -17,9 +17,10 @@ pub fn inner_0(mut count: i32, ap: VaList) -> i32 { return total; } pub fn outer_1(count: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut result: i32 = ({ inner_0((*count.borrow()), (*ap.borrow()).clone()) }); return result; } diff --git a/tests/unit/out/refcount/va_arg_mixed_int_ptr.rs b/tests/unit/out/refcount/va_arg_mixed_int_ptr.rs index 553d9d4ea..e312c7d3c 100644 --- a/tests/unit/out/refcount/va_arg_mixed_int_ptr.rs +++ b/tests/unit/out/refcount/va_arg_mixed_int_ptr.rs @@ -7,9 +7,10 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn mixed_args_0(count: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut total: i32 = 0; let mut i: i32 = 0; 'loop_: while (((i < (*count.borrow())) as i32) != 0) { diff --git a/tests/unit/out/refcount/va_arg_mixed_types.rs b/tests/unit/out/refcount/va_arg_mixed_types.rs index de68481b6..41142abdc 100644 --- a/tests/unit/out/refcount/va_arg_mixed_types.rs +++ b/tests/unit/out/refcount/va_arg_mixed_types.rs @@ -15,9 +15,10 @@ pub struct pair { pub b: i32, } pub fn sum_mixed_0(count: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut total: i32 = 0; let mut i: i32 = 0; 'loop_: while (((i < (*count.borrow())) as i32) != 0) { diff --git a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs index 6b90a28a4..8e8237ade 100644 --- a/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs +++ b/tests/unit/out/refcount/va_arg_non_primitive_ptrs.rs @@ -21,9 +21,10 @@ pub const opt_OPT_FILE: opt = 1; pub const opt_OPT_NODE: opt = 2; pub const opt_OPT_NODE_OUT: opt = 3; pub fn dispatch_0(option: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let option: Value = Rc::new(RefCell::new(option)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut result: i32 = 0; 'switch: { match { (*option.borrow()) } { diff --git a/tests/unit/out/refcount/va_arg_null_int_ptr.rs b/tests/unit/out/refcount/va_arg_null_int_ptr.rs index 271656863..0db9d3745 100644 --- a/tests/unit/out/refcount/va_arg_null_int_ptr.rs +++ b/tests/unit/out/refcount/va_arg_null_int_ptr.rs @@ -7,9 +7,10 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn first_nonnull_0(count: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut result: i32 = -1_i32; let mut i: i32 = 0; 'loop_: while (((i < (*count.borrow())) as i32) != 0) { diff --git a/tests/unit/out/refcount/va_arg_printf.rs b/tests/unit/out/refcount/va_arg_printf.rs index 6f914b62e..03c6ababb 100644 --- a/tests/unit/out/refcount/va_arg_printf.rs +++ b/tests/unit/out/refcount/va_arg_printf.rs @@ -6,22 +6,24 @@ use std::io::prelude::*; use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; -pub fn logf_impl_0(mut fmt: Ptr, ap: VaList) -> i32 { - let ap: Value = Rc::new(RefCell::new(ap)); +pub fn logf_impl_0(mut fmt: Ptr, ap: Ptr) -> i32 { + let ap: Value> = Rc::new(RefCell::new(ap)); &(fmt); return ({ (*ap.borrow_mut()).arg::() } + { (*ap.borrow_mut()).arg::() }); } pub fn logf_1(fmt: Ptr, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let fmt: Value> = Rc::new(RefCell::new(fmt)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut result: i32 = ({ logf_impl_0((*fmt.borrow()).clone(), (*ap.borrow()).clone()) }); return result; } pub fn lenf_2(fmt: Ptr, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let fmt: Value> = Rc::new(RefCell::new(fmt)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut s: Ptr = (*ap.borrow_mut()).arg::>(); let mut result: i32 = (s.to_c_string_iterator().count() as i32); return result; diff --git a/tests/unit/out/refcount/va_arg_promotion.rs b/tests/unit/out/refcount/va_arg_promotion.rs index b645f3d80..2c6adf40b 100644 --- a/tests/unit/out/refcount/va_arg_promotion.rs +++ b/tests/unit/out/refcount/va_arg_promotion.rs @@ -7,9 +7,10 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn test_promotions_0(count: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let count: Value = Rc::new(RefCell::new(count)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut a: i32 = (*ap.borrow_mut()).arg::(); let mut b: i32 = (*ap.borrow_mut()).arg::(); let mut c: f64 = (*ap.borrow_mut()).arg::(); diff --git a/tests/unit/out/refcount/va_arg_snprintf.rs b/tests/unit/out/refcount/va_arg_snprintf.rs index 48f15699a..c9885c375 100644 --- a/tests/unit/out/refcount/va_arg_snprintf.rs +++ b/tests/unit/out/refcount/va_arg_snprintf.rs @@ -7,9 +7,10 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn extract_first_0(mut buf: Ptr, mut size: i32, fmt: Ptr, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let fmt: Value> = Rc::new(RefCell::new(fmt)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let mut n: i32 = (*ap.borrow_mut()).arg::(); elem!(buf, 0).write({ (n as i8) }); return n; diff --git a/tests/unit/out/refcount/va_arg_struct_ctx.rs b/tests/unit/out/refcount/va_arg_struct_ctx.rs index caa695fb4..410332747 100644 --- a/tests/unit/out/refcount/va_arg_struct_ctx.rs +++ b/tests/unit/out/refcount/va_arg_struct_ctx.rs @@ -15,10 +15,11 @@ pub struct context { pub last_error: i32, } pub fn set_error_0(mut ctx: Ptr, fmt: Ptr, __args: &[VaArg]) { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let fmt: Value> = Rc::new(RefCell::new(fmt)); if (ctx.with(|__s| __s.verbose) != 0) { - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let __rhs = (*ap.borrow_mut()).arg::(); field!(ctx, last_error).write(__rhs); } @@ -46,9 +47,10 @@ impl Default for value { } } pub fn pick_1(use_long: i32, __args: &[VaArg]) -> i64 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let use_long: Value = Rc::new(RefCell::new(use_long)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); let v: Value = Rc::new(RefCell::new((*ap.borrow_mut()).arg::())); if ((*use_long.borrow()) != 0) { return (value::l(v.as_pointer()).read()); diff --git a/tests/unit/out/refcount/va_arg_two_passes.rs b/tests/unit/out/refcount/va_arg_two_passes.rs index d6a4d6a47..1e9f8e86f 100644 --- a/tests/unit/out/refcount/va_arg_two_passes.rs +++ b/tests/unit/out/refcount/va_arg_two_passes.rs @@ -7,11 +7,12 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn sum_then_product_0(first: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let first: Value = Rc::new(RefCell::new(first)); - let ap: Value = Rc::new(RefCell::new(VaList::default())); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); let mut sum: i32 = (*first.borrow()); let mut product: i32 = (*first.borrow()); - (*ap.borrow_mut()) = VaList::new(__args); + (*ap.borrow_mut()) = __args.as_pointer(); let mut val: i32 = 0_i32; 'loop_: while (((({ val = (*ap.borrow_mut()).arg::(); @@ -21,7 +22,7 @@ pub fn sum_then_product_0(first: i32, __args: &[VaArg]) -> i32 { { sum += val; } - (*ap.borrow_mut()) = VaList::new(__args); + (*ap.borrow_mut()) = __args.as_pointer(); 'loop_: while (((({ val = (*ap.borrow_mut()).arg::(); val diff --git a/tests/unit/out/refcount/va_arg_void_ptr.rs b/tests/unit/out/refcount/va_arg_void_ptr.rs index 17ea4b67a..2f311d552 100644 --- a/tests/unit/out/refcount/va_arg_void_ptr.rs +++ b/tests/unit/out/refcount/va_arg_void_ptr.rs @@ -19,10 +19,11 @@ pub type field = u32; pub const field_FIELD_SLOT: field = 0; pub const field_FIELD_LEVEL: field = 1; pub fn registry_update_0(mut r: Ptr, field: field, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); let field: Value = Rc::new(RefCell::new(field)); let mut result: i32 = 0; - let ap: Value = Rc::new(RefCell::new(VaList::default())); - (*ap.borrow_mut()) = VaList::new(__args); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); 'switch: { match { ((*field.borrow()) as u32) } { __v if __v == ((field_FIELD_SLOT as i32) as u32) => { diff --git a/tests/unit/out/refcount/va_list_field.rs b/tests/unit/out/refcount/va_list_field.rs new file mode 100644 index 000000000..1f7f1e5a4 --- /dev/null +++ b/tests/unit/out/refcount/va_list_field.rs @@ -0,0 +1,81 @@ +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 fn sum_0(n: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); + let n: Value = Rc::new(RefCell::new(n)); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); + let guard: Value = Rc::new(RefCell::new(Guard_1::new({ ap.as_pointer() }))); + let _dtor_guard = ScopedDestructor::new(&guard, |__p| __p.destructor()); + let mut total: i32 = 0; + let mut i: i32 = 0; + 'loop_: while (i < (*n.borrow())) { + total += { (*guard.borrow()).ap.clone() }.with_mut(|__v| __v.arg::()); + i.prefix_inc(); + } + return total; +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Destructor, Default)] +#[byte_size(16)] +pub struct Guard_1 { + #[offset(0)] + #[byte_size(8)] + pub ap: Ptr>, + #[offset(8)] + pub active: bool, +} +impl Guard_1 { + pub fn new(val: Ptr>) -> Self { + Self { + ap: (val).clone(), + active: true, + } + } +} +pub fn sum_ptr_2(n: i32, __args: &[VaArg]) -> i32 { + let __args: Value> = Rc::new(RefCell::new(__args.into())); + let n: Value = Rc::new(RefCell::new(n)); + let ap: Value> = Rc::new(RefCell::new(Ptr::::default())); + (*ap.borrow_mut()) = __args.as_pointer(); + let mut cursor: Cursor_3 = Cursor_3 { + ap: (ap.as_pointer()), + }; + let mut total: i32 = 0; + let mut i: i32 = 0; + 'loop_: while (i < (*n.borrow())) { + total += cursor.ap.with_mut(|__v| __v.arg::()); + i.prefix_inc(); + } + return total; +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(8)] +pub struct Cursor_3 { + #[offset(0)] + #[byte_size(8)] + pub ap: Ptr>, +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + assert!((({ sum_0(3, &[(1).into(), (2).into(), (3).into(),]) }) == 6)); + assert!((({ sum_ptr_2(2, &[(4).into(), (5).into(),]) }) == 9)); + return 0; +} +pub trait Guard_1Impl { + fn destructor(&self); +} +impl Guard_1Impl for Ptr { + fn destructor(&self) { + if (*self).with(|__s| __s.active) {} + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/va_arg_chain.rs b/tests/unit/out/unsafe/va_arg_chain.rs index ac557ee28..0f1b373b1 100644 --- a/tests/unit/out/unsafe/va_arg_chain.rs +++ b/tests/unit/out/unsafe/va_arg_chain.rs @@ -6,7 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub unsafe fn extract_nth_0(mut n: i32, mut ap: VaList) -> i32 { +pub unsafe fn extract_nth_0(mut n: i32, mut ap: *const VaArg) -> i32 { let mut i: i32 = 0; 'loop_: while ((((i) < (n)) as i32) != 0) { ap.arg::(); @@ -14,12 +14,12 @@ pub unsafe fn extract_nth_0(mut n: i32, mut ap: VaList) -> i32 { } return ap.arg::(); } -pub unsafe fn middle_layer_1(mut n: i32, mut ap: VaList) -> i32 { +pub unsafe fn middle_layer_1(mut n: i32, mut ap: *const VaArg) -> i32 { return (unsafe { extract_nth_0(n, ap) }); } pub unsafe fn top_level_2(mut n: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut result: i32 = (unsafe { middle_layer_1(n, ap) }); return result; } diff --git a/tests/unit/out/unsafe/va_arg_concat.rs b/tests/unit/out/unsafe/va_arg_concat.rs index f225947b0..41bba3cb5 100644 --- a/tests/unit/out/unsafe/va_arg_concat.rs +++ b/tests/unit/out/unsafe/va_arg_concat.rs @@ -7,9 +7,9 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn sum_ints_0(mut first: i32, __args: &[VaArg]) -> i32 { - let mut args: VaList = VaList::default(); + let mut args: *const VaArg = ::std::ptr::null(); let mut total: i32 = first; - args = VaList::new(__args); + args = __args.as_ptr(); let mut val: i32 = 0_i32; 'loop_: while (((({ val = args.arg::(); diff --git a/tests/unit/out/unsafe/va_arg_conditional.rs b/tests/unit/out/unsafe/va_arg_conditional.rs index bb3e4429b..2baf70b2d 100644 --- a/tests/unit/out/unsafe/va_arg_conditional.rs +++ b/tests/unit/out/unsafe/va_arg_conditional.rs @@ -12,8 +12,8 @@ pub unsafe fn conditional_log_0( __args: &[VaArg], ) -> i32 { if (verbose != 0) { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut result: i32 = ap.arg::(); return result; } diff --git a/tests/unit/out/unsafe/va_arg_copy.rs b/tests/unit/out/unsafe/va_arg_copy.rs index d3a418112..5025b1629 100644 --- a/tests/unit/out/unsafe/va_arg_copy.rs +++ b/tests/unit/out/unsafe/va_arg_copy.rs @@ -7,9 +7,9 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn sum_with_copy_0(mut count: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - let mut aq: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + let mut aq: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); aq = ap.clone(); let mut sum1: i32 = 0; let mut i: i32 = 0; diff --git a/tests/unit/out/unsafe/va_arg_fn_ptr.rs b/tests/unit/out/unsafe/va_arg_fn_ptr.rs index 67ae7edc1..789263519 100644 --- a/tests/unit/out/unsafe/va_arg_fn_ptr.rs +++ b/tests/unit/out/unsafe/va_arg_fn_ptr.rs @@ -16,8 +16,8 @@ pub unsafe fn add_2(mut a: i32, mut b: i32) -> i32 { return ((a) + (b)); } pub unsafe fn apply_unary_3(mut x: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut fn_: Option i32> = std::mem::transmute::< *mut ::libc::c_void, Option i32>, @@ -26,8 +26,8 @@ pub unsafe fn apply_unary_3(mut x: i32, __args: &[VaArg]) -> i32 { return result; } pub unsafe fn apply_binary_4(mut a: i32, mut b: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut fn_: Option i32> = std::mem::transmute::< *mut ::libc::c_void, Option i32>, diff --git a/tests/unit/out/unsafe/va_arg_forward.rs b/tests/unit/out/unsafe/va_arg_forward.rs index 571764b8f..bccf0d8fc 100644 --- a/tests/unit/out/unsafe/va_arg_forward.rs +++ b/tests/unit/out/unsafe/va_arg_forward.rs @@ -6,7 +6,7 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub unsafe fn inner_0(mut count: i32, mut ap: VaList) -> i32 { +pub unsafe fn inner_0(mut count: i32, mut ap: *const VaArg) -> i32 { let mut total: i32 = 0; let mut i: i32 = 0; 'loop_: while ((((i) < (count)) as i32) != 0) { @@ -16,8 +16,8 @@ pub unsafe fn inner_0(mut count: i32, mut ap: VaList) -> i32 { return total; } pub unsafe fn outer_1(mut count: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut result: i32 = (unsafe { inner_0(count, ap) }); return result; } diff --git a/tests/unit/out/unsafe/va_arg_mixed_int_ptr.rs b/tests/unit/out/unsafe/va_arg_mixed_int_ptr.rs index 97e92cffa..6508cd480 100644 --- a/tests/unit/out/unsafe/va_arg_mixed_int_ptr.rs +++ b/tests/unit/out/unsafe/va_arg_mixed_int_ptr.rs @@ -7,8 +7,8 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn mixed_args_0(mut count: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut total: i32 = 0; let mut i: i32 = 0; 'loop_: while ((((i) < (count)) as i32) != 0) { diff --git a/tests/unit/out/unsafe/va_arg_mixed_types.rs b/tests/unit/out/unsafe/va_arg_mixed_types.rs index 0afe111dd..94cbe3392 100644 --- a/tests/unit/out/unsafe/va_arg_mixed_types.rs +++ b/tests/unit/out/unsafe/va_arg_mixed_types.rs @@ -13,8 +13,8 @@ pub struct pair { pub b: i32, } pub unsafe fn sum_mixed_0(mut count: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut total: i32 = 0; let mut i: i32 = 0; 'loop_: while ((((i) < (count)) as i32) != 0) { diff --git a/tests/unit/out/unsafe/va_arg_non_primitive_ptrs.rs b/tests/unit/out/unsafe/va_arg_non_primitive_ptrs.rs index 4989e5e38..7259516c6 100644 --- a/tests/unit/out/unsafe/va_arg_non_primitive_ptrs.rs +++ b/tests/unit/out/unsafe/va_arg_non_primitive_ptrs.rs @@ -18,8 +18,8 @@ pub const opt_OPT_FILE: opt = 1; pub const opt_OPT_NODE: opt = 2; pub const opt_OPT_NODE_OUT: opt = 3; pub unsafe fn dispatch_0(mut option: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut result: i32 = 0; 'switch: { match { option } { diff --git a/tests/unit/out/unsafe/va_arg_null_int_ptr.rs b/tests/unit/out/unsafe/va_arg_null_int_ptr.rs index 9c8db2eff..38eebb0e5 100644 --- a/tests/unit/out/unsafe/va_arg_null_int_ptr.rs +++ b/tests/unit/out/unsafe/va_arg_null_int_ptr.rs @@ -7,8 +7,8 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn first_nonnull_0(mut count: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut result: i32 = -1_i32; let mut i: i32 = 0; 'loop_: while ((((i) < (count)) as i32) != 0) { diff --git a/tests/unit/out/unsafe/va_arg_printf.rs b/tests/unit/out/unsafe/va_arg_printf.rs index 052e4ae90..a1a758f53 100644 --- a/tests/unit/out/unsafe/va_arg_printf.rs +++ b/tests/unit/out/unsafe/va_arg_printf.rs @@ -6,19 +6,19 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; -pub unsafe fn logf_impl_0(mut fmt: *const libc::c_char, mut ap: VaList) -> i32 { +pub unsafe fn logf_impl_0(mut fmt: *const libc::c_char, mut ap: *const VaArg) -> i32 { &(fmt); return ((ap.arg::()) + (ap.arg::())); } pub unsafe fn logf_1(mut fmt: *const libc::c_char, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut result: i32 = (unsafe { logf_impl_0(fmt, ap) }); return result; } pub unsafe fn lenf_2(mut fmt: *const libc::c_char, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut s: *const libc::c_char = ap.arg::<*const libc::c_char>(); let mut result: i32 = (libc::strlen(s) as i32); return result; diff --git a/tests/unit/out/unsafe/va_arg_promotion.rs b/tests/unit/out/unsafe/va_arg_promotion.rs index 99ae774f7..d9c00b5d4 100644 --- a/tests/unit/out/unsafe/va_arg_promotion.rs +++ b/tests/unit/out/unsafe/va_arg_promotion.rs @@ -7,8 +7,8 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn test_promotions_0(mut count: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut a: i32 = ap.arg::(); let mut b: i32 = ap.arg::(); let mut c: f64 = ap.arg::(); diff --git a/tests/unit/out/unsafe/va_arg_snprintf.rs b/tests/unit/out/unsafe/va_arg_snprintf.rs index 26b78730a..ba6e5071b 100644 --- a/tests/unit/out/unsafe/va_arg_snprintf.rs +++ b/tests/unit/out/unsafe/va_arg_snprintf.rs @@ -12,8 +12,8 @@ pub unsafe fn extract_first_0( mut fmt: *const libc::c_char, __args: &[VaArg], ) -> i32 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut n: i32 = ap.arg::(); (*buf.offset((0) as isize)) = (n as libc::c_char); return n; diff --git a/tests/unit/out/unsafe/va_arg_struct_ctx.rs b/tests/unit/out/unsafe/va_arg_struct_ctx.rs index 064ebbdc8..6fddfea1a 100644 --- a/tests/unit/out/unsafe/va_arg_struct_ctx.rs +++ b/tests/unit/out/unsafe/va_arg_struct_ctx.rs @@ -14,8 +14,8 @@ pub struct context { } pub unsafe fn set_error_0(mut ctx: *mut context, mut fmt: *const libc::c_char, __args: &[VaArg]) { if ((*ctx).verbose != 0) { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); (*ctx).last_error = ap.arg::(); } } @@ -31,8 +31,8 @@ impl Default for value { } } pub unsafe fn pick_1(mut use_long: i32, __args: &[VaArg]) -> i64 { - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); let mut v: value = ap.arg::(); if (use_long != 0) { return v.l; diff --git a/tests/unit/out/unsafe/va_arg_two_passes.rs b/tests/unit/out/unsafe/va_arg_two_passes.rs index 0b9152996..93c040ab7 100644 --- a/tests/unit/out/unsafe/va_arg_two_passes.rs +++ b/tests/unit/out/unsafe/va_arg_two_passes.rs @@ -7,10 +7,10 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn sum_then_product_0(mut first: i32, __args: &[VaArg]) -> i32 { - let mut ap: VaList = VaList::default(); + let mut ap: *const VaArg = ::std::ptr::null(); let mut sum: i32 = first; let mut product: i32 = first; - ap = VaList::new(__args); + ap = __args.as_ptr(); let mut val: i32 = 0_i32; 'loop_: while (((({ val = ap.arg::(); @@ -20,7 +20,7 @@ pub unsafe fn sum_then_product_0(mut first: i32, __args: &[VaArg]) -> i32 { { sum += val; } - ap = VaList::new(__args); + ap = __args.as_ptr(); 'loop_: while (((({ val = ap.arg::(); val diff --git a/tests/unit/out/unsafe/va_arg_void_ptr.rs b/tests/unit/out/unsafe/va_arg_void_ptr.rs index ec5e1a1a6..45f9a38a8 100644 --- a/tests/unit/out/unsafe/va_arg_void_ptr.rs +++ b/tests/unit/out/unsafe/va_arg_void_ptr.rs @@ -17,8 +17,8 @@ pub const field_FIELD_SLOT: field = 0; pub const field_FIELD_LEVEL: field = 1; pub unsafe fn registry_update_0(mut r: *mut registry, mut field: field, __args: &[VaArg]) -> i32 { let mut result: i32 = 0; - let mut ap: VaList = VaList::default(); - ap = VaList::new(__args); + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); 'switch: { match { (field as u32) } { __v if __v == ((field_FIELD_SLOT as i32) as u32) => { diff --git a/tests/unit/out/unsafe/va_list_field.rs b/tests/unit/out/unsafe/va_list_field.rs new file mode 100644 index 000000000..c01ce8831 --- /dev/null +++ b/tests/unit/out/unsafe/va_list_field.rs @@ -0,0 +1,70 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn sum_0(mut n: i32, __args: &[VaArg]) -> i32 { + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); + let mut guard: Guard_1 = Guard_1::new({ &mut ap }); + let _dtor_guard = ScopedDestructorUnsafe::new(&raw mut guard, Guard_1::destructor); + let mut total: i32 = 0; + let mut i: i32 = 0; + 'loop_: while ((i) < (n)) { + total += (*guard.ap).arg::(); + i.prefix_inc(); + } + return total; +} +#[repr(C)] +#[derive(Clone, VaArg, FnPtrArg, DestructorUnsafe, Default)] +pub struct Guard_1 { + pub ap: *mut *const VaArg, + pub active: bool, +} +impl Guard_1 { + pub unsafe fn new(val: *mut *const VaArg) -> Self { + let mut this = Self { + ap: val, + active: true, + }; + this + } + pub unsafe fn destructor(&mut self) { + if self.active {} + } +} +pub unsafe fn sum_ptr_2(mut n: i32, __args: &[VaArg]) -> i32 { + let mut ap: *const VaArg = ::std::ptr::null(); + ap = __args.as_ptr(); + let mut cursor: Cursor_3 = Cursor_3 { + ap: (&mut ap as *mut *const VaArg), + }; + let mut total: i32 = 0; + let mut i: i32 = 0; + 'loop_: while ((i) < (n)) { + total += (*cursor.ap).arg::(); + i.prefix_inc(); + } + return total; +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Cursor_3 { + pub ap: *mut *const VaArg, +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + assert!(((unsafe { sum_0(3, &[(1).into(), (2).into(), (3).into(),]) }) == (6))); + assert!(((unsafe { sum_ptr_2(2, &[(4).into(), (5).into(),]) }) == (9))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/va_list_field.cpp b/tests/unit/va_list_field.cpp new file mode 100644 index 000000000..0677ca1bd --- /dev/null +++ b/tests/unit/va_list_field.cpp @@ -0,0 +1,44 @@ +#include +#include + +static int sum(int n, ...) { + struct Guard { + va_list ≈ + bool active; + Guard(va_list &val) : ap(val), active(true) {} + ~Guard() { + if (active) { + va_end(ap); + } + } + }; + va_list ap; + va_start(ap, n); + Guard guard(ap); + int total = 0; + for (int i = 0; i < n; ++i) { + total += va_arg(guard.ap, int); + } + return total; +} + +static int sum_ptr(int n, ...) { + struct Cursor { + va_list *ap; + }; + va_list ap; + va_start(ap, n); + Cursor cursor{&ap}; + int total = 0; + for (int i = 0; i < n; ++i) { + total += va_arg(*cursor.ap, int); + } + va_end(ap); + return total; +} + +int main() { + assert(sum(3, 1, 2, 3) == 6); + assert(sum_ptr(2, 4, 5) == 9); + return 0; +}