diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 80fd5acf39..6c2d9961c7 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -1201,8 +1201,9 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) { id = GetDeclId(var->getCanonicalDecl(), var->getFormalLinkage() != clang::Linkage::External); } else if (auto *tag = clang::dyn_cast(decl); - tag && tag->getDeclContext()->isFunctionOrMethod()) { - id = GetUniqueNumber(GetID(tag)); + tag && (tag->getDeclContext()->isFunctionOrMethod() || + tag->isInAnonymousNamespace())) { + id = GetUniqueNumber(GetID(tag->getCanonicalDecl())); } if (id) { name += '_'; diff --git a/cpp2rust/converter/printer.cpp b/cpp2rust/converter/printer.cpp index 384aef9b32..bd186d8afd 100644 --- a/cpp2rust/converter/printer.cpp +++ b/cpp2rust/converter/printer.cpp @@ -168,7 +168,9 @@ std::string ToString(clang::ASTContext &ctx, clang::QualType qual_type, if (auto *tag = qual_type->getAsTagDecl(); tag && tag->getIdentifier() && - tag->getDeclContext()->isFunctionOrMethod()) { + (tag->getDeclContext()->isFunctionOrMethod() || + (tag->isInAnonymousNamespace() && + !clang::isa(tag)))) { return GetNamedDeclAsString(tag); } diff --git a/tests/std/basic/basic.link/CMakeLists.txt b/tests/std/basic/basic.link/CMakeLists.txt new file mode 100644 index 0000000000..f1512e2e0a --- /dev/null +++ b/tests/std/basic/basic.link/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.20) +project(basic_link LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +add_executable(app a.cpp b.cpp) diff --git a/tests/std/basic/basic.link/a.cpp b/tests/std/basic/basic.link/a.cpp new file mode 100644 index 0000000000..f2b2f22542 --- /dev/null +++ b/tests/std/basic/basic.link/a.cpp @@ -0,0 +1,304 @@ +#include + +// N4950 [basic.link]/3.1: the name of a function that is explicitly declared +// static has internal linkage. +namespace p3_1_static_functions_in_two_files { +static int pick() { return 1; } + +int pick_in_b(); + +void test() { + assert(pick() == 1); + assert(pick_in_b() == 2); +} +} // namespace p3_1_static_functions_in_two_files + +// N4950 [basic.link]/3.2: the name of a non-template variable of non-volatile +// const-qualified type has internal linkage. +namespace p3_2_const_variables_in_two_files { +const int limit = 10; + +int limit_in_b(); +const int *limit_address_in_b(); + +void test() { + assert(limit == 10); + assert(limit_in_b() == 20); + assert(&limit != limit_address_in_b()); +} +} // namespace p3_2_const_variables_in_two_files + +// N4950 [basic.link]/3.2.2: unless it is explicitly declared extern. +namespace p3_2_2_extern_const_variable { +extern const int threshold; + +const int *threshold_address_in_b(); + +void test() { + assert(threshold == 30); + assert(&threshold == threshold_address_in_b()); +} +} // namespace p3_2_2_extern_const_variable + +// N4950 [basic.link]/3.2.4: or it was previously declared and the prior +// declaration did not have internal linkage. +namespace p3_2_4_const_variable_previously_declared_extern { +extern const int ceiling; + +const int *ceiling_address_in_b(); + +void test() { + assert(ceiling == 40); + assert(&ceiling == ceiling_address_in_b()); +} +} // namespace p3_2_4_const_variable_previously_declared_extern + +// N4950 [basic.link]/3.3: the name of a data member of an anonymous union has +// internal linkage. +namespace p3_3_anonymous_union_members_in_two_files { +static union { + int whole; + float fraction; +}; + +void set_whole_in_b(int value); +int whole_in_b(); + +void test() { + whole = 5; + set_whole_in_b(6); + assert(whole == 5); + assert(whole_in_b() == 6); +} +} // namespace p3_3_anonymous_union_members_in_two_files + +// N4950 [basic.link]/4: an unnamed namespace or a namespace declared directly +// or indirectly within an unnamed namespace has internal linkage, and so do +// the names of the entities that belong to it. +namespace p4_unnamed_namespaces_in_two_files { +namespace { +int counter = 1; + +int bump() { return ++counter; } + +struct Record { + int id = 100; +}; + +namespace Nested { +int depth = 3; +} +} // namespace + +int bump_in_b(); +int record_id_in_b(); +int depth_in_b(); + +void test() { + assert(bump() == 2); + assert(bump_in_b() == 51); + assert(counter == 2); + + Record record; + assert(record.id == 100); + assert(record_id_in_b() == 200); + + assert(Nested::depth == 3); + assert(depth_in_b() == 4); +} +} // namespace p4_unnamed_namespaces_in_two_files + +// N4950 [basic.link]/4.1: otherwise the name of a variable that belongs to a +// namespace scope has external linkage. +namespace p4_1_variable_defined_in_other_file { +extern int shared_total; + +void add_in_b(int amount); + +void test() { + assert(shared_total == 5); + shared_total = 7; + add_in_b(3); + assert(shared_total == 10); +} +} // namespace p4_1_variable_defined_in_other_file + +// N4950 [basic.link]/4.9: the name of an entity that belongs to a namespace +// with external linkage has external linkage. +namespace p4_9_members_of_named_namespace { +namespace Config { +extern int level; + +int raise(int by); +} // namespace Config + +void test() { + assert(Config::level == 1); + assert(Config::raise(2) == 3); + Config::level = 10; + assert(Config::raise(1) == 11); +} +} // namespace p4_9_members_of_named_namespace + +// N4950 [basic.link]/5: a member function or static data member has the same +// linkage as the name of the class of which it is a member. +namespace p5_members_of_classes_in_unnamed_namespaces { +namespace { +struct Tally { + static int total; + + int add(int amount) { + total += amount; + return total; + } +}; +int Tally::total = 10; +} // namespace + +int add_in_b(int amount); + +void test() { + Tally tally; + assert(tally.add(1) == 11); + assert(add_in_b(1) == 102); + assert(Tally::total == 11); +} +} // namespace p5_members_of_classes_in_unnamed_namespaces + +namespace p5_members_of_class_with_external_linkage { +struct Ledger { + static int balance; + + static int deposit(int amount); +}; + +void test() { + assert(Ledger::balance == 50); + Ledger::balance = 60; + assert(Ledger::deposit(5) == 65); + assert(Ledger::balance == 65); +} +} // namespace p5_members_of_class_with_external_linkage + +// N4950 [basic.link]/6: a block-scope extern declaration redeclares the +// entity of the enclosing namespace, with that entity's linkage. +namespace p6_block_scope_extern_names_entity_in_other_file { +int read_remote() { + extern int remote; + return remote; +} + +void test() { + extern void set_remote_in_b(int value); + assert(read_remote() == 70); + set_remote_in_b(71); + assert(read_remote() == 71); +} +} // namespace p6_block_scope_extern_names_entity_in_other_file + +namespace p6_block_scope_extern_names_hidden_static { +static int hidden = 80; + +int hidden_in_b(); + +int read_hidden_through_extern() { + int hidden = 1; + { + extern int hidden; + return hidden; + } +} + +void test() { + assert(read_hidden_through_extern() == 80); + hidden = 81; + assert(read_hidden_through_extern() == 81); + assert(hidden_in_b() == 90); +} +} // namespace p6_block_scope_extern_names_hidden_static + +// N4950 [basic.link]/7: a name declared at block scope has no linkage. +namespace p7_block_scope_static_variables { +int next_even() { + static int count = 0; + count += 2; + return count; +} + +int next_odd() { + static int count = 1; + count += 2; + return count; +} + +int next_in_b(); + +void test() { + assert(next_even() == 2); + assert(next_odd() == 3); + assert(next_in_b() == 1000); + assert(next_even() == 4); + assert(next_odd() == 5); + assert(next_in_b() == 2000); +} +} // namespace p7_block_scope_static_variables + +namespace p7_local_classes { +int first_key() { + struct Entry { + int key = 1; + }; + Entry entry; + return entry.key; +} + +int second_key_plus_value() { + struct Entry { + int key = 2; + int value = 30; + }; + Entry entry; + return entry.key + entry.value; +} + +int key_in_b(); + +void test() { + assert(first_key() == 1); + assert(second_key_plus_value() == 32); + assert(key_in_b() == 400); +} +} // namespace p7_local_classes + +// N4950 [basic.link]/11: declarations for an array object can specify array +// types that differ by the presence or absence of a major array bound. +namespace p11_array_declared_without_bound { +extern int table[]; + +int table_sum_in_b(); + +void test() { + assert(table[1] == 2); + table[2] = 30; + assert(table_sum_in_b() == 33); +} +} // namespace p11_array_declared_without_bound + +int main() { + p3_1_static_functions_in_two_files::test(); + p3_2_const_variables_in_two_files::test(); + p3_2_2_extern_const_variable::test(); + p3_2_4_const_variable_previously_declared_extern::test(); + p3_3_anonymous_union_members_in_two_files::test(); + p4_unnamed_namespaces_in_two_files::test(); + p4_1_variable_defined_in_other_file::test(); + p4_9_members_of_named_namespace::test(); + p5_members_of_classes_in_unnamed_namespaces::test(); + p5_members_of_class_with_external_linkage::test(); + p6_block_scope_extern_names_entity_in_other_file::test(); + p6_block_scope_extern_names_hidden_static::test(); + p7_block_scope_static_variables::test(); + p7_local_classes::test(); + p11_array_declared_without_bound::test(); + return 0; +} diff --git a/tests/std/basic/basic.link/b.cpp b/tests/std/basic/basic.link/b.cpp new file mode 100644 index 0000000000..2c7ce2b6c2 --- /dev/null +++ b/tests/std/basic/basic.link/b.cpp @@ -0,0 +1,146 @@ +namespace p3_1_static_functions_in_two_files { +static int pick() { return 2; } + +int pick_in_b() { return pick(); } +} // namespace p3_1_static_functions_in_two_files + +namespace p3_2_const_variables_in_two_files { +const int limit = 20; + +int limit_in_b() { return limit; } +const int *limit_address_in_b() { return &limit; } +} // namespace p3_2_const_variables_in_two_files + +namespace p3_2_2_extern_const_variable { +extern const int threshold = 30; + +const int *threshold_address_in_b() { return &threshold; } +} // namespace p3_2_2_extern_const_variable + +namespace p3_2_4_const_variable_previously_declared_extern { +extern const int ceiling; +const int ceiling = 40; + +const int *ceiling_address_in_b() { return &ceiling; } +} // namespace p3_2_4_const_variable_previously_declared_extern + +namespace p3_3_anonymous_union_members_in_two_files { +static union { + int whole; + float fraction; +}; + +void set_whole_in_b(int value) { whole = value; } +int whole_in_b() { return whole; } +} // namespace p3_3_anonymous_union_members_in_two_files + +namespace p4_unnamed_namespaces_in_two_files { +namespace { +int counter = 50; + +int bump() { return ++counter; } + +struct Record { + long id = 200; + int extra = 1; +}; + +namespace Nested { +int depth = 4; +} +} // namespace + +int bump_in_b() { return bump(); } +int record_id_in_b() { + Record record; + return static_cast(record.id); +} +int depth_in_b() { return Nested::depth; } +} // namespace p4_unnamed_namespaces_in_two_files + +namespace p4_1_variable_defined_in_other_file { +int shared_total = 5; + +void add_in_b(int amount) { shared_total += amount; } +} // namespace p4_1_variable_defined_in_other_file + +namespace p4_9_members_of_named_namespace { +namespace Config { +int level = 1; + +int raise(int by) { + level += by; + return level; +} +} // namespace Config +} // namespace p4_9_members_of_named_namespace + +namespace p5_members_of_classes_in_unnamed_namespaces { +namespace { +struct Tally { + static int total; + + int add(int amount) { + total += amount * 2; + return total; + } +}; +int Tally::total = 100; +} // namespace + +int add_in_b(int amount) { + Tally tally; + return tally.add(amount); +} +} // namespace p5_members_of_classes_in_unnamed_namespaces + +namespace p5_members_of_class_with_external_linkage { +struct Ledger { + static int balance; + + static int deposit(int amount); +}; + +int Ledger::balance = 50; + +int Ledger::deposit(int amount) { + balance += amount; + return balance; +} +} // namespace p5_members_of_class_with_external_linkage + +namespace p6_block_scope_extern_names_entity_in_other_file { +int remote = 70; + +void set_remote_in_b(int value) { remote = value; } +} // namespace p6_block_scope_extern_names_entity_in_other_file + +namespace p6_block_scope_extern_names_hidden_static { +static int hidden = 90; + +int hidden_in_b() { return hidden; } +} // namespace p6_block_scope_extern_names_hidden_static + +namespace p7_block_scope_static_variables { +int next_in_b() { + static int count = 0; + count += 1000; + return count; +} +} // namespace p7_block_scope_static_variables + +namespace p7_local_classes { +int key_in_b() { + struct Entry { + long key = 400; + }; + Entry entry; + return static_cast(entry.key); +} +} // namespace p7_local_classes + +namespace p11_array_declared_without_bound { +int table[3] = {1, 2, 3}; + +int table_sum_in_b() { return table[0] + table[1] + table[2]; } +} // namespace p11_array_declared_without_bound diff --git a/tests/std/basic/basic.link/out/refcount/basic.link.rs b/tests/std/basic/basic.link/out/refcount/basic.link.rs new file mode 100644 index 0000000000..5aeb51e2df --- /dev/null +++ b/tests/std/basic/basic.link/out/refcount/basic.link.rs @@ -0,0 +1,455 @@ +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 pick_0() -> i32 { + return 1; +} +pub fn test_1() { + assert!((({ pick_0() }) == 1)); + assert!((({ pick_in_b_2() }) == 2)); +} +thread_local!( + pub static limit_3: Value = Rc::new(RefCell::new(10)); +); +pub fn test_4() { + assert!((limit_3.with(|rc| *rc.borrow()) == 10)); + assert!((({ limit_in_b_5() }) == 20)); + assert!(((limit_3.with(|v| v.as_pointer())) != ({ limit_address_in_b_6() }))); +} +pub fn test_8() { + assert!((threshold_7.with(|rc| *rc.borrow()) == 30)); + assert!(((threshold_7.with(|v| v.as_pointer())) == ({ threshold_address_in_b_9() }))); +} +pub fn test_11() { + assert!((ceiling_10.with(|rc| *rc.borrow()) == 40)); + assert!(((ceiling_10.with(|v| v.as_pointer())) == ({ ceiling_address_in_b_12() }))); +} +#[derive(VaArg, FnPtrArg, ByteRepr, Clone)] +#[byte_size(4)] +pub struct anon_13 { + #[offset(0)] + #[byte_size(4)] + __bytes: Box<[u8]>, +} +impl anon_13 { + pub fn whole(this: Ptr) -> Ptr { + this.reinterpret_cast() + } + pub fn fraction(this: Ptr) -> Ptr { + this.reinterpret_cast() + } +} +impl Default for anon_13 { + fn default() -> Self { + anon_13 { + __bytes: Box::from([0u8; 4]), + } + } +} +thread_local!( + pub static anon_13: Value = Rc::new(RefCell::new(::default())); +); +pub fn test_14() { + anon_13::whole(anon_13.with(|v| v.as_pointer())).write(5); + ({ set_whole_in_b_15(6) }); + assert!(((anon_13::whole(anon_13.with(|v| v.as_pointer())).read()) == 5)); + assert!((({ whole_in_b_16() }) == 6)); +} +thread_local!( + pub static counter_17: Value = Rc::new(RefCell::new(1)); +); +pub fn bump_18() -> i32 { + return (*counter_17.with(Value::clone).borrow_mut()).prefix_inc(); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(4)] +pub struct Record_19 { + #[offset(0)] + pub id: i32, +} +impl Default for Record_19 { + fn default() -> Self { + { Record_19 { id: 100 } } + } +} +thread_local!( + pub static depth_20: Value = Rc::new(RefCell::new(3)); +); +pub fn test_21() { + assert!((({ bump_18() }) == 2)); + assert!((({ bump_in_b_22() }) == 51)); + assert!((counter_17.with(|rc| *rc.borrow()) == 2)); + let mut record: Record_19 = ::default(); + assert!((record.id == 100)); + assert!((({ record_id_in_b_23() }) == 200)); + assert!((depth_20.with(|rc| *rc.borrow()) == 3)); + assert!((({ depth_in_b_24() }) == 4)); +} +pub fn test_26() { + assert!((shared_total_25.with(|rc| *rc.borrow()) == 5)); + shared_total_25.with(|rc| *rc.borrow_mut() = 7); + ({ add_in_b_27(3) }); + assert!((shared_total_25.with(|rc| *rc.borrow()) == 10)); +} +pub fn test_29() { + assert!((level_28.with(|rc| *rc.borrow()) == 1)); + assert!((({ raise_30(2,) }) == 3)); + level_28.with(|rc| *rc.borrow_mut() = 10); + assert!((({ raise_30(1,) }) == 11)); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct Tally_31 {} +thread_local!( + pub static total_32: Value = Rc::new(RefCell::new(10)); +); +pub fn test_33() { + let tally: Value = Rc::new(RefCell::new(::default())); + assert!((({ Tally_31Impl::add(&tally.as_pointer(), 1,) }) == 11)); + assert!((({ add_in_b_34(1,) }) == 102)); + assert!((total_32.with(|rc| *rc.borrow()) == 11)); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct p5_members_of_class_with_external_linkage_Ledger {} +pub fn test_36() { + assert!((balance_35.with(|rc| *rc.borrow()) == 50)); + balance_35.with(|rc| *rc.borrow_mut() = 60); + assert!((({ p5_members_of_class_with_external_linkage_Ledger::deposit(5,) }) == 65)); + assert!((balance_35.with(|rc| *rc.borrow()) == 65)); +} +pub fn read_remote_37() -> i32 { + return remote_38.with(|rc| *rc.borrow()); +} +pub fn test_39() { + assert!((({ read_remote_37() }) == 70)); + ({ set_remote_in_b_40(71) }); + assert!((({ read_remote_37() }) == 71)); +} +thread_local!( + pub static hidden_41: Value = Rc::new(RefCell::new(80)); +); +pub fn read_hidden_through_extern_42() -> i32 { + let mut hidden: i32 = 1; + { + return hidden_41.with(|rc| *rc.borrow()); + } + panic!("ub: non-void function does not return a value") +} +pub fn test_43() { + assert!((({ read_hidden_through_extern_42() }) == 80)); + hidden_41.with(|rc| *rc.borrow_mut() = 81); + assert!((({ read_hidden_through_extern_42() }) == 81)); + assert!((({ hidden_in_b_44() }) == 90)); +} +pub fn next_even_45() -> i32 { + count_46.with(|rc| *rc.borrow_mut() += 2); + return count_46.with(|rc| *rc.borrow()); +} +pub fn next_odd_47() -> i32 { + count_48.with(|rc| *rc.borrow_mut() += 2); + return count_48.with(|rc| *rc.borrow()); +} +pub fn test_49() { + assert!((({ next_even_45() }) == 2)); + assert!((({ next_odd_47() }) == 3)); + assert!((({ next_in_b_50() }) == 1000)); + assert!((({ next_even_45() }) == 4)); + assert!((({ next_odd_47() }) == 5)); + assert!((({ next_in_b_50() }) == 2000)); +} +thread_local!( + static count_46: Value = Rc::new(RefCell::new(0)); +); +thread_local!( + static count_48: Value = Rc::new(RefCell::new(1)); +); +pub fn first_key_51() -> i32 { + let mut entry: Entry_52 = ::default(); + return entry.key; +} +pub fn second_key_plus_value_53() -> i32 { + let mut entry: Entry_54 = ::default(); + return (entry.key + entry.value); +} +pub fn test_55() { + assert!((({ first_key_51() }) == 1)); + assert!((({ second_key_plus_value_53() }) == 32)); + assert!((({ key_in_b_56() }) == 400)); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(4)] +pub struct Entry_52 { + #[offset(0)] + pub key: i32, +} +impl Default for Entry_52 { + fn default() -> Self { + { Entry_52 { key: 1 } } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(8)] +pub struct Entry_54 { + #[offset(0)] + pub key: i32, + #[offset(4)] + pub value: i32, +} +impl Default for Entry_54 { + fn default() -> Self { + { Entry_54 { key: 2, value: 30 } } + } +} +pub fn test_58() { + assert!( + (({ + let __idx0 = (1) as usize; + table_57.with(|rc| rc.borrow()[__idx0]) + }) == 2) + ); + (*table_57.with(Value::clone).borrow_mut())[(2) as usize] = 30; + assert!((({ table_sum_in_b_59() }) == 33)); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_1() }); + ({ test_4() }); + ({ test_8() }); + ({ test_11() }); + ({ test_14() }); + ({ test_21() }); + ({ test_26() }); + ({ test_29() }); + ({ test_33() }); + ({ test_36() }); + ({ test_39() }); + ({ test_43() }); + ({ test_49() }); + ({ test_55() }); + ({ test_58() }); + return 0; +} +pub fn pick_60() -> i32 { + return 2; +} +pub fn pick_in_b_2() -> i32 { + return ({ pick_60() }); +} +thread_local!( + pub static limit_61: Value = Rc::new(RefCell::new(20)); +); +pub fn limit_in_b_5() -> i32 { + return limit_61.with(|rc| *rc.borrow()); +} +pub fn limit_address_in_b_6() -> Ptr { + return (limit_61.with(|v| v.as_pointer())); +} +thread_local!( + pub static threshold_7: Value = Rc::new(RefCell::new(30)); +); +pub fn threshold_address_in_b_9() -> Ptr { + return (threshold_7.with(|v| v.as_pointer())); +} +thread_local!( + pub static ceiling_10: Value = Rc::new(RefCell::new(40)); +); +pub fn ceiling_address_in_b_12() -> Ptr { + return (ceiling_10.with(|v| v.as_pointer())); +} +#[derive(VaArg, FnPtrArg, ByteRepr, Clone)] +#[byte_size(4)] +pub struct anon_62 { + #[offset(0)] + #[byte_size(4)] + __bytes: Box<[u8]>, +} +impl anon_62 { + pub fn whole(this: Ptr) -> Ptr { + this.reinterpret_cast() + } + pub fn fraction(this: Ptr) -> Ptr { + this.reinterpret_cast() + } +} +impl Default for anon_62 { + fn default() -> Self { + anon_62 { + __bytes: Box::from([0u8; 4]), + } + } +} +thread_local!( + pub static anon_62: Value = Rc::new(RefCell::new(::default())); +); +pub fn set_whole_in_b_15(mut value: i32) { + anon_62::whole(anon_62.with(|v| v.as_pointer())).write(value); +} +pub fn whole_in_b_16() -> i32 { + return (anon_62::whole(anon_62.with(|v| v.as_pointer())).read()); +} +thread_local!( + pub static counter_63: Value = Rc::new(RefCell::new(50)); +); +pub fn bump_64() -> i32 { + return (*counter_63.with(Value::clone).borrow_mut()).prefix_inc(); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(16)] +pub struct Record_65 { + #[offset(0)] + pub id: i64, + #[offset(8)] + pub extra: i32, +} +impl Default for Record_65 { + fn default() -> Self { + { + Record_65 { + id: 200_i64, + extra: 1, + } + } + } +} +thread_local!( + pub static depth_66: Value = Rc::new(RefCell::new(4)); +); +pub fn bump_in_b_22() -> i32 { + return ({ bump_64() }); +} +pub fn record_id_in_b_23() -> i32 { + let mut record: Record_65 = ::default(); + return (record.id as i32); +} +pub fn depth_in_b_24() -> i32 { + return depth_66.with(|rc| *rc.borrow()); +} +thread_local!( + pub static shared_total_25: Value = Rc::new(RefCell::new(5)); +); +pub fn add_in_b_27(mut amount: i32) { + shared_total_25.with(|rc| *rc.borrow_mut() += amount); +} +thread_local!( + pub static level_28: Value = Rc::new(RefCell::new(1)); +); +pub fn raise_30(mut by: i32) -> i32 { + level_28.with(|rc| *rc.borrow_mut() += by); + return level_28.with(|rc| *rc.borrow()); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)] +#[byte_size(1)] +pub struct Tally_67 {} +thread_local!( + pub static total_68: Value = Rc::new(RefCell::new(100)); +); +pub fn add_in_b_34(mut amount: i32) -> i32 { + let tally: Value = Rc::new(RefCell::new(::default())); + return ({ Tally_67Impl::add(&tally.as_pointer(), amount) }); +} +thread_local!( + pub static balance_35: Value = Rc::new(RefCell::new(50)); +); +impl p5_members_of_class_with_external_linkage_Ledger { + pub fn deposit(mut amount: i32) -> i32 { + balance_35.with(|rc| *rc.borrow_mut() += amount); + return balance_35.with(|rc| *rc.borrow()); + } +} +thread_local!( + pub static remote_38: Value = Rc::new(RefCell::new(70)); +); +pub fn set_remote_in_b_40(mut value: i32) { + remote_38.with(|rc| *rc.borrow_mut() = value); +} +thread_local!( + pub static hidden_69: Value = Rc::new(RefCell::new(90)); +); +pub fn hidden_in_b_44() -> i32 { + return hidden_69.with(|rc| *rc.borrow()); +} +pub fn next_in_b_50() -> i32 { + count_70.with(|rc| *rc.borrow_mut() += 1000); + return count_70.with(|rc| *rc.borrow()); +} +thread_local!( + static count_70: Value = Rc::new(RefCell::new(0)); +); +pub fn key_in_b_56() -> i32 { + let mut entry: Entry_71 = ::default(); + return (entry.key as i32); +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg)] +#[byte_size(8)] +pub struct Entry_71 { + #[offset(0)] + pub key: i64, +} +impl Default for Entry_71 { + fn default() -> Self { + { Entry_71 { key: 400_i64 } } + } +} +thread_local!( + pub static table_57: Value> = Rc::new(RefCell::new(Box::new([1, 2, 3]))); +); +pub fn table_sum_in_b_59() -> i32 { + return ((({ + let __idx0 = (0) as usize; + table_57.with(|rc| rc.borrow()[__idx0]) + }) + ({ + let __idx0 = (1) as usize; + table_57.with(|rc| rc.borrow()[__idx0]) + })) + ({ + let __idx0 = (2) as usize; + table_57.with(|rc| rc.borrow()[__idx0]) + })); +} +pub trait Tally_31Impl { + fn add(&self, amount: i32) -> i32; +} +impl Tally_31Impl for Ptr { + fn add(&self, mut amount: i32) -> i32 { + total_32.with(|rc| *rc.borrow_mut() += amount); + return total_32.with(|rc| *rc.borrow()); + } +} +pub trait Tally_67Impl { + fn add(&self, amount: i32) -> i32; +} +impl Tally_67Impl for Ptr { + fn add(&self, mut amount: i32) -> i32 { + total_68.with(|rc| *rc.borrow_mut() += (amount * 2)); + return total_68.with(|rc| *rc.borrow()); + } +} +pub fn __cpp2rust_init_globals() { + let _ = limit_3.with(|_| ()); + let _ = anon_13.with(|_| ()); + let _ = counter_17.with(|_| ()); + let _ = depth_20.with(|_| ()); + let _ = total_32.with(|_| ()); + let _ = hidden_41.with(|_| ()); + let _ = limit_61.with(|_| ()); + let _ = threshold_7.with(|_| ()); + let _ = ceiling_10.with(|_| ()); + let _ = anon_62.with(|_| ()); + let _ = counter_63.with(|_| ()); + let _ = depth_66.with(|_| ()); + let _ = shared_total_25.with(|_| ()); + let _ = level_28.with(|_| ()); + let _ = total_68.with(|_| ()); + let _ = balance_35.with(|_| ()); + let _ = remote_38.with(|_| ()); + let _ = hidden_69.with(|_| ()); + let _ = table_57.with(|_| ()); +} diff --git a/tests/std/basic/basic.link/out/unsafe/basic.link.rs b/tests/std/basic/basic.link/out/unsafe/basic.link.rs new file mode 100644 index 0000000000..46c81ed457 --- /dev/null +++ b/tests/std/basic/basic.link/out/unsafe/basic.link.rs @@ -0,0 +1,380 @@ +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 pick_0() -> i32 { + return 1; +} +pub unsafe fn test_1() { + assert!(((unsafe { pick_0() }) == (1))); + assert!(((unsafe { pick_in_b_2() }) == (2))); +} +pub static mut limit_3: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 10 }); +pub unsafe fn test_4() { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut limit_3)) == (10))); + assert!(((unsafe { limit_in_b_5() }) == (20))); + assert!( + ((&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut limit_3)) as *const i32) + != (unsafe { limit_address_in_b_6() })) + ); +} +pub unsafe fn test_8() { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut threshold_7)) == (30))); + assert!( + ((&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut threshold_7)) as *const i32) + == (unsafe { threshold_address_in_b_9() })) + ); +} +pub unsafe fn test_11() { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut ceiling_10)) == (40))); + assert!( + ((&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut ceiling_10)) as *const i32) + == (unsafe { ceiling_address_in_b_12() })) + ); +} +#[repr(C)] +#[derive(Copy, Clone, FnPtrArg, VaArg)] +pub union anon_13 { + pub whole: i32, + pub fraction: f32, +} +impl Default for anon_13 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +pub static mut anon_13: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { ::default() }); +pub unsafe fn test_14() { + (*std::cell::LazyCell::force_mut(&mut *&raw mut anon_13)).whole = 5; + (unsafe { set_whole_in_b_15(6) }); + assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut anon_13)).whole) == (5))); + assert!(((unsafe { whole_in_b_16() }) == (6))); +} +pub static mut counter_17: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 1 }); +pub unsafe fn bump_18() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut counter_17)).prefix_inc(); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct Record_19 { + pub id: i32, +} +impl Default for Record_19 { + fn default() -> Self { + unsafe { Record_19 { id: 100 } } + } +} +pub static mut depth_20: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 3 }); +pub unsafe fn test_21() { + assert!(((unsafe { bump_18() }) == (2))); + assert!(((unsafe { bump_in_b_22() }) == (51))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut counter_17)) == (2))); + let mut record: Record_19 = ::default(); + assert!(((record.id) == (100))); + assert!(((unsafe { record_id_in_b_23() }) == (200))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut depth_20)) == (3))); + assert!(((unsafe { depth_in_b_24() }) == (4))); +} +pub unsafe fn test_26() { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut shared_total_25)) == (5))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut shared_total_25)) = 7; + (unsafe { add_in_b_27(3) }); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut shared_total_25)) == (10))); +} +pub unsafe fn test_29() { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut level_28)) == (1))); + assert!(((unsafe { raise_30(2,) }) == (3))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut level_28)) = 10; + assert!(((unsafe { raise_30(1,) }) == (11))); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Tally_31 {} +impl Tally_31 { + pub unsafe fn add(&mut self, mut amount: i32) -> i32 { + (*std::cell::LazyCell::force_mut(&mut *&raw mut total_32)) += amount; + return (*std::cell::LazyCell::force_mut(&mut *&raw mut total_32)); + } +} +pub static mut total_32: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 10 }); +pub unsafe fn test_33() { + let mut tally: Tally_31 = ::default(); + assert!(((unsafe { Tally_31::add(&mut tally, 1,) }) == (11))); + assert!(((unsafe { add_in_b_34(1,) }) == (102))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut total_32)) == (11))); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct p5_members_of_class_with_external_linkage_Ledger {} +pub unsafe fn test_36() { + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut balance_35)) == (50))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut balance_35)) = 60; + assert!(((unsafe { p5_members_of_class_with_external_linkage_Ledger::deposit(5,) }) == (65))); + assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut balance_35)) == (65))); +} +pub unsafe fn read_remote_37() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut remote_38)); +} +pub unsafe fn test_39() { + assert!(((unsafe { read_remote_37() }) == (70))); + (unsafe { set_remote_in_b_40(71) }); + assert!(((unsafe { read_remote_37() }) == (71))); +} +pub static mut hidden_41: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 80 }); +pub unsafe fn read_hidden_through_extern_42() -> i32 { + let mut hidden: i32 = 1; + { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut hidden_41)); + } + panic!("ub: non-void function does not return a value") +} +pub unsafe fn test_43() { + assert!(((unsafe { read_hidden_through_extern_42() }) == (80))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut hidden_41)) = 81; + assert!(((unsafe { read_hidden_through_extern_42() }) == (81))); + assert!(((unsafe { hidden_in_b_44() }) == (90))); +} +pub unsafe fn next_even_45() -> i32 { + (*std::cell::LazyCell::force_mut(&mut *&raw mut count_46)) += 2; + return (*std::cell::LazyCell::force_mut(&mut *&raw mut count_46)); +} +pub unsafe fn next_odd_47() -> i32 { + (*std::cell::LazyCell::force_mut(&mut *&raw mut count_48)) += 2; + return (*std::cell::LazyCell::force_mut(&mut *&raw mut count_48)); +} +pub unsafe fn test_49() { + assert!(((unsafe { next_even_45() }) == (2))); + assert!(((unsafe { next_odd_47() }) == (3))); + assert!(((unsafe { next_in_b_50() }) == (1000))); + assert!(((unsafe { next_even_45() }) == (4))); + assert!(((unsafe { next_odd_47() }) == (5))); + assert!(((unsafe { next_in_b_50() }) == (2000))); +} +static mut count_46: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +static mut count_48: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 1 }); +pub unsafe fn first_key_51() -> i32 { + let mut entry: Entry_52 = ::default(); + return entry.key; +} +pub unsafe fn second_key_plus_value_53() -> i32 { + let mut entry: Entry_54 = ::default(); + return ((entry.key) + (entry.value)); +} +pub unsafe fn test_55() { + assert!(((unsafe { first_key_51() }) == (1))); + assert!(((unsafe { second_key_plus_value_53() }) == (32))); + assert!(((unsafe { key_in_b_56() }) == (400))); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct Entry_52 { + pub key: i32, +} +impl Default for Entry_52 { + fn default() -> Self { + unsafe { Entry_52 { key: 1 } } + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct Entry_54 { + pub key: i32, + pub value: i32, +} +impl Default for Entry_54 { + fn default() -> Self { + unsafe { Entry_54 { key: 2, value: 30 } } + } +} +pub unsafe fn test_58() { + assert!((((*std::cell::LazyCell::force_mut(&mut *&raw mut table_57))[(1) as usize]) == (2))); + (*std::cell::LazyCell::force_mut(&mut *&raw mut table_57))[(2) as usize] = 30; + assert!(((unsafe { table_sum_in_b_59() }) == (33))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_1() }); + (unsafe { test_4() }); + (unsafe { test_8() }); + (unsafe { test_11() }); + (unsafe { test_14() }); + (unsafe { test_21() }); + (unsafe { test_26() }); + (unsafe { test_29() }); + (unsafe { test_33() }); + (unsafe { test_36() }); + (unsafe { test_39() }); + (unsafe { test_43() }); + (unsafe { test_49() }); + (unsafe { test_55() }); + (unsafe { test_58() }); + return 0; +} +pub unsafe fn pick_60() -> i32 { + return 2; +} +pub unsafe fn pick_in_b_2() -> i32 { + return (unsafe { pick_60() }); +} +pub static mut limit_61: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 20 }); +pub unsafe fn limit_in_b_5() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut limit_61)); +} +pub unsafe fn limit_address_in_b_6() -> *const i32 { + return (&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut limit_61)) as *const i32); +} +pub static mut threshold_7: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 30 }); +pub unsafe fn threshold_address_in_b_9() -> *const i32 { + return (&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut threshold_7)) + as *const i32); +} +pub static mut ceiling_10: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 40 }); +pub unsafe fn ceiling_address_in_b_12() -> *const i32 { + return (&raw const (*std::cell::LazyCell::force_mut(&mut *&raw mut ceiling_10)) as *const i32); +} +#[repr(C)] +#[derive(Copy, Clone, FnPtrArg, VaArg)] +pub union anon_62 { + pub whole: i32, + pub fraction: f32, +} +impl Default for anon_62 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} +pub static mut anon_62: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { ::default() }); +pub unsafe fn set_whole_in_b_15(mut value: i32) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut anon_62)).whole = value; +} +pub unsafe fn whole_in_b_16() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut anon_62)).whole; +} +pub static mut counter_63: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 50 }); +pub unsafe fn bump_64() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut counter_63)).prefix_inc(); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct Record_65 { + pub id: i64, + pub extra: i32, +} +impl Default for Record_65 { + fn default() -> Self { + unsafe { + Record_65 { + id: 200_i64, + extra: 1, + } + } + } +} +pub static mut depth_66: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 4 }); +pub unsafe fn bump_in_b_22() -> i32 { + return (unsafe { bump_64() }); +} +pub unsafe fn record_id_in_b_23() -> i32 { + let mut record: Record_65 = ::default(); + return (record.id as i32); +} +pub unsafe fn depth_in_b_24() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut depth_66)); +} +pub static mut shared_total_25: std::cell::LazyCell = + std::cell::LazyCell::new(|| unsafe { 5 }); +pub unsafe fn add_in_b_27(mut amount: i32) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut shared_total_25)) += amount; +} +pub static mut level_28: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 1 }); +pub unsafe fn raise_30(mut by: i32) -> i32 { + (*std::cell::LazyCell::force_mut(&mut *&raw mut level_28)) += by; + return (*std::cell::LazyCell::force_mut(&mut *&raw mut level_28)); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, Default)] +pub struct Tally_67 {} +impl Tally_67 { + pub unsafe fn add(&mut self, mut amount: i32) -> i32 { + (*std::cell::LazyCell::force_mut(&mut *&raw mut total_68)) += ((amount) * (2)); + return (*std::cell::LazyCell::force_mut(&mut *&raw mut total_68)); + } +} +pub static mut total_68: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 100 }); +pub unsafe fn add_in_b_34(mut amount: i32) -> i32 { + let mut tally: Tally_67 = ::default(); + return (unsafe { Tally_67::add(&mut tally, amount) }); +} +pub static mut balance_35: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 50 }); +impl p5_members_of_class_with_external_linkage_Ledger { + pub unsafe fn deposit(mut amount: i32) -> i32 { + (*std::cell::LazyCell::force_mut(&mut *&raw mut balance_35)) += amount; + return (*std::cell::LazyCell::force_mut(&mut *&raw mut balance_35)); + } +} +pub static mut remote_38: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 70 }); +pub unsafe fn set_remote_in_b_40(mut value: i32) { + (*std::cell::LazyCell::force_mut(&mut *&raw mut remote_38)) = value; +} +pub static mut hidden_69: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 90 }); +pub unsafe fn hidden_in_b_44() -> i32 { + return (*std::cell::LazyCell::force_mut(&mut *&raw mut hidden_69)); +} +pub unsafe fn next_in_b_50() -> i32 { + (*std::cell::LazyCell::force_mut(&mut *&raw mut count_70)) += 1000; + return (*std::cell::LazyCell::force_mut(&mut *&raw mut count_70)); +} +static mut count_70: std::cell::LazyCell = std::cell::LazyCell::new(|| unsafe { 0 }); +pub unsafe fn key_in_b_56() -> i32 { + let mut entry: Entry_71 = ::default(); + return (entry.key as i32); +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg)] +pub struct Entry_71 { + pub key: i64, +} +impl Default for Entry_71 { + fn default() -> Self { + unsafe { Entry_71 { key: 400_i64 } } + } +} +pub static mut table_57: std::cell::LazyCell<[i32; 3]> = + std::cell::LazyCell::new(|| unsafe { [1, 2, 3] }); +pub unsafe fn table_sum_in_b_59() -> i32 { + return ((((*std::cell::LazyCell::force_mut(&mut *&raw mut table_57))[(0) as usize]) + + ((*std::cell::LazyCell::force_mut(&mut *&raw mut table_57))[(1) as usize])) + + ((*std::cell::LazyCell::force_mut(&mut *&raw mut table_57))[(2) as usize])); +} +pub unsafe fn __cpp2rust_init_globals() { + std::cell::LazyCell::force(&*&raw const limit_3); + std::cell::LazyCell::force(&*&raw const anon_13); + std::cell::LazyCell::force(&*&raw const counter_17); + std::cell::LazyCell::force(&*&raw const depth_20); + std::cell::LazyCell::force(&*&raw const total_32); + std::cell::LazyCell::force(&*&raw const hidden_41); + std::cell::LazyCell::force(&*&raw const limit_61); + std::cell::LazyCell::force(&*&raw const threshold_7); + std::cell::LazyCell::force(&*&raw const ceiling_10); + std::cell::LazyCell::force(&*&raw const anon_62); + std::cell::LazyCell::force(&*&raw const counter_63); + std::cell::LazyCell::force(&*&raw const depth_66); + std::cell::LazyCell::force(&*&raw const shared_total_25); + std::cell::LazyCell::force(&*&raw const level_28); + std::cell::LazyCell::force(&*&raw const total_68); + std::cell::LazyCell::force(&*&raw const balance_35); + std::cell::LazyCell::force(&*&raw const remote_38); + std::cell::LazyCell::force(&*&raw const hidden_69); + std::cell::LazyCell::force(&*&raw const table_57); +}