|
| 1 | +extern crate libcc2rs; |
| 2 | +use libcc2rs::*; |
| 3 | +use std::cell::RefCell; |
| 4 | +use std::collections::BTreeMap; |
| 5 | +use std::io::prelude::*; |
| 6 | +use std::io::{Read, Seek, Write}; |
| 7 | +use std::os::fd::AsFd; |
| 8 | +use std::rc::{Rc, Weak}; |
| 9 | +pub fn print_moved_from_0(mut label: Ptr<i8>, from: Ptr<Vec<i8>>, to: Ptr<Vec<i8>>) { |
| 10 | + let sub: Value<Vec<i8>> = Rc::new(RefCell::new(from.with(|__s: &Vec<i8>| { |
| 11 | + let mut __tmp1 = |
| 12 | + __s[0_usize..::std::cmp::min(0_usize + 0_usize, __s.len().saturating_sub(1))].to_vec(); |
| 13 | + __tmp1.push(0); |
| 14 | + __tmp1 |
| 15 | + }))); |
| 16 | + println!( |
| 17 | + "{}: {} {} {} {} {} [{}]", |
| 18 | + label, |
| 19 | + ((*from.upgrade().deref()).len() - 1), |
| 20 | + ((*sub.borrow()).len() - 1), |
| 21 | + ((elem!(from.decay(), 0).read()) as i32), |
| 22 | + ((*to.upgrade().deref()).len() - 1), |
| 23 | + ((elem!(sub.decay(), 0).read()) as i32), |
| 24 | + to.decay() |
| 25 | + ); |
| 26 | +} |
| 27 | +pub fn return_local_1() -> Vec<i8> { |
| 28 | + let mut word: Vec<i8> = { |
| 29 | + let mut __bytes = Ptr::<i8>::from_string_literal(b"returned local").to_c_bytes(); |
| 30 | + __bytes.push(0); |
| 31 | + __bytes |
| 32 | + }; |
| 33 | + return std::mem::take(&mut (word)); |
| 34 | +} |
| 35 | +pub fn return_parameter_2(mut word: Vec<i8>) -> Vec<i8> { |
| 36 | + return std::mem::take(&mut word); |
| 37 | +} |
| 38 | +pub fn return_reference_3(word: Ptr<Vec<i8>>) -> Vec<i8> { |
| 39 | + return word.with_mut(|__v| std::mem::replace(__v, vec![0])); |
| 40 | +} |
| 41 | +pub fn return_rvalue_reference_4(word: Ptr<Vec<i8>>) -> Vec<i8> { |
| 42 | + return word.with_mut(|__v| std::mem::replace(__v, vec![0])); |
| 43 | +} |
| 44 | +pub fn main() { |
| 45 | + __cpp2rust_init_globals(); |
| 46 | + std::process::exit(main_0()); |
| 47 | +} |
| 48 | +fn main_0() -> i32 { |
| 49 | + let empty: Value<Vec<i8>> = Rc::new(RefCell::new(vec![0])); |
| 50 | + let from_empty: Value<Vec<i8>> = Rc::new(RefCell::new( |
| 51 | + ({ std::mem::replace(&mut (*empty.borrow_mut()), vec![0]) }), |
| 52 | + )); |
| 53 | + ({ |
| 54 | + print_moved_from_0( |
| 55 | + Ptr::<i8>::from_string_literal(b"construct empty"), |
| 56 | + empty.as_pointer(), |
| 57 | + from_empty.as_pointer(), |
| 58 | + ) |
| 59 | + }); |
| 60 | + let word: Value<Vec<i8>> = Rc::new(RefCell::new({ |
| 61 | + let mut __bytes = Ptr::<i8>::from_string_literal(b"hello").to_c_bytes(); |
| 62 | + __bytes.push(0); |
| 63 | + __bytes |
| 64 | + })); |
| 65 | + let from_word: Value<Vec<i8>> = Rc::new(RefCell::new( |
| 66 | + ({ std::mem::replace(&mut (*word.borrow_mut()), vec![0]) }), |
| 67 | + )); |
| 68 | + ({ |
| 69 | + print_moved_from_0( |
| 70 | + Ptr::<i8>::from_string_literal(b"construct word"), |
| 71 | + word.as_pointer(), |
| 72 | + from_word.as_pointer(), |
| 73 | + ) |
| 74 | + }); |
| 75 | + let long_word: Value<Vec<i8>> = Rc::new(RefCell::new({ |
| 76 | + let mut __bytes = |
| 77 | + Ptr::<i8>::from_string_literal(b"a string that does not fit in place").to_c_bytes(); |
| 78 | + __bytes.push(0); |
| 79 | + __bytes |
| 80 | + })); |
| 81 | + let assigned: Value<Vec<i8>> = Rc::new(RefCell::new({ |
| 82 | + let mut __bytes = Ptr::<i8>::from_string_literal(b"x").to_c_bytes(); |
| 83 | + __bytes.push(0); |
| 84 | + __bytes |
| 85 | + })); |
| 86 | + assigned |
| 87 | + .as_pointer() |
| 88 | + .write(({ std::mem::replace(&mut (*long_word.borrow_mut()), vec![0]) })); |
| 89 | + ({ |
| 90 | + print_moved_from_0( |
| 91 | + Ptr::<i8>::from_string_literal(b"assign long"), |
| 92 | + long_word.as_pointer(), |
| 93 | + assigned.as_pointer(), |
| 94 | + ) |
| 95 | + }); |
| 96 | + let short_word: Value<Vec<i8>> = Rc::new(RefCell::new({ |
| 97 | + let mut __bytes = Ptr::<i8>::from_string_literal(b"ab").to_c_bytes(); |
| 98 | + __bytes.push(0); |
| 99 | + __bytes |
| 100 | + })); |
| 101 | + let assigned_short: Value<Vec<i8>> = Rc::new(RefCell::new(vec![0])); |
| 102 | + assigned_short |
| 103 | + .as_pointer() |
| 104 | + .write(({ std::mem::replace(&mut (*short_word.borrow_mut()), vec![0]) })); |
| 105 | + ({ |
| 106 | + print_moved_from_0( |
| 107 | + Ptr::<i8>::from_string_literal(b"assign short"), |
| 108 | + short_word.as_pointer(), |
| 109 | + assigned_short.as_pointer(), |
| 110 | + ) |
| 111 | + }); |
| 112 | + { |
| 113 | + word.as_pointer().with_mut(|__v: &mut Vec<i8>| { |
| 114 | + __v.pop(); |
| 115 | + Ptr::<i8>::from_string_literal(b"again") |
| 116 | + .with_slice(5_usize, |__s| __v.extend_from_slice(__s)); |
| 117 | + __v.push(0); |
| 118 | + }); |
| 119 | + word.as_pointer() |
| 120 | + }; |
| 121 | + println!("reuse: {} {}", ((*word.borrow()).len() - 1), word.decay()); |
| 122 | + let local: Value<Vec<i8>> = Rc::new(RefCell::new(({ return_local_1() }))); |
| 123 | + let parameter: Value<Vec<i8>> = Rc::new(RefCell::new( |
| 124 | + ({ |
| 125 | + return_parameter_2({ |
| 126 | + let mut __bytes = |
| 127 | + Ptr::<i8>::from_string_literal(b"returned parameter").to_c_bytes(); |
| 128 | + __bytes.push(0); |
| 129 | + __bytes |
| 130 | + }) |
| 131 | + }), |
| 132 | + )); |
| 133 | + println!("returns: {} {}", local.decay(), parameter.decay()); |
| 134 | + let source: Value<Vec<i8>> = Rc::new(RefCell::new({ |
| 135 | + let mut __bytes = Ptr::<i8>::from_string_literal(b"returned reference").to_c_bytes(); |
| 136 | + __bytes.push(0); |
| 137 | + __bytes |
| 138 | + })); |
| 139 | + let reference: Value<Vec<i8>> = |
| 140 | + Rc::new(RefCell::new(({ return_reference_3(source.as_pointer()) }))); |
| 141 | + ({ |
| 142 | + print_moved_from_0( |
| 143 | + Ptr::<i8>::from_string_literal(b"return reference"), |
| 144 | + source.as_pointer(), |
| 145 | + reference.as_pointer(), |
| 146 | + ) |
| 147 | + }); |
| 148 | + { |
| 149 | + source.as_pointer().with_mut(|__v: &mut Vec<i8>| { |
| 150 | + __v.pop(); |
| 151 | + Ptr::<i8>::from_string_literal(b"rvalue reference") |
| 152 | + .with_slice(16_usize, |__s| __v.extend_from_slice(__s)); |
| 153 | + __v.push(0); |
| 154 | + }); |
| 155 | + source.as_pointer() |
| 156 | + }; |
| 157 | + let rvalue_reference: Value<Vec<i8>> = Rc::new(RefCell::new( |
| 158 | + ({ return_rvalue_reference_4(source.as_pointer()) }), |
| 159 | + )); |
| 160 | + ({ |
| 161 | + print_moved_from_0( |
| 162 | + Ptr::<i8>::from_string_literal(b"return rvalue reference"), |
| 163 | + source.as_pointer(), |
| 164 | + rvalue_reference.as_pointer(), |
| 165 | + ) |
| 166 | + }); |
| 167 | + return 0; |
| 168 | +} |
| 169 | +pub fn __cpp2rust_init_globals() {} |
0 commit comments