Skip to content

Commit 341b6eb

Browse files
fix layout output
1 parent 647d93e commit 341b6eb

25 files changed

+242
-51
lines changed

asttoc/src/cppmm_ast_add_c.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -1028,8 +1028,7 @@ NodeExprPtr constructor_body(TypeRegistry& type_registry, TranslationUnit& c_tu,
10281028
case BindType::OpaquePtr:
10291029
return opaqueptr_constructor_body(type_registry, c_tu, cpp_record,
10301030
c_record, cpp_method);
1031-
case BindType::OpaqueBytes:
1032-
case BindType::ValueType:
1031+
default:
10331032
return opaquebytes_constructor_body(type_registry, c_tu, cpp_record,
10341033
c_record, cpp_method);
10351034
}
@@ -1160,8 +1159,7 @@ NodeExprPtr destructor_body(TypeRegistry& type_registry, TranslationUnit& c_tu,
11601159
case BindType::OpaquePtr:
11611160
return opaqueptr_destructor_body(type_registry, c_tu, cpp_record,
11621161
c_record, cpp_method);
1163-
case BindType::OpaqueBytes:
1164-
case BindType::ValueType:
1162+
default:
11651163
return method_body(type_registry, c_tu, cpp_record, c_record, c_return,
11661164
cpp_method);
11671165
}
@@ -1260,10 +1258,10 @@ void record_memory(TypeRegistry& type_registry, TranslationUnit& c_tu,
12601258
compute_function_names(type_registry, c_record, memory_operator);
12611259

12621260
auto c_function = NodeFunction::n(
1263-
names.long_name, PLACEHOLDER_ID, std::vector<std::string>(), "",
1264-
std::move(return_), std::vector<Param>(), names.nice_name,
1265-
"returns the size of this type in bytes", std::vector<NodeTypePtr>(),
1266-
std::vector<Exception>());
1261+
names.long_name, PLACEHOLDER_ID, std::vector<std::string>(),
1262+
memory_operator, std::move(return_), std::vector<Param>(),
1263+
names.nice_name, "returns the size of this type in bytes",
1264+
std::vector<NodeTypePtr>(), std::vector<Exception>());
12671265
c_function->body = body;
12681266

12691267
c_tu.decls.push_back(NodePtr(c_function));
@@ -2127,8 +2125,10 @@ void record_detail(TypeRegistry& type_registry, TranslationUnit& c_tu,
21272125
record_fields(type_registry, c_tu, cpp_record, c_record);
21282126

21292127
// Sizeof and Alignoff
2130-
record_sizeof(type_registry, c_tu, cpp_record, c_record);
2131-
record_alignof(type_registry, c_tu, cpp_record, c_record);
2128+
if (bind_type(cpp_record) == BindType::OpaqueBytes) {
2129+
record_sizeof(type_registry, c_tu, cpp_record, c_record);
2130+
record_alignof(type_registry, c_tu, cpp_record, c_record);
2131+
}
21322132

21332133
// Methods
21342134
NodePtr copy_constructor;

asttoc/src/cppmm_ast_write_c.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -531,13 +531,15 @@ void write_function_bdy(fmt::ostream& out, const NodePtr& node, Access access) {
531531

532532
// FIXME AL: taking a shortcut here. We need to express this in terms
533533
// of expression nodes, but let's get it working first
534-
if (!function.private_) {
534+
if (!function.private_ && function.short_name != "sizeof" &&
535+
function.short_name != "alignof") {
535536
out.print(" try {{\n");
536537
}
537538

538539
write_expression(out, 2, function.body);
539540

540-
if (!function.private_) {
541+
if (!function.private_ && function.short_name != "sizeof" &&
542+
function.short_name != "alignof") {
541543
for (const auto& e : function.exceptions) {
542544
out.print(" }} catch ({}& e) {{\n"
543545
" TLG_EXCEPTION_STRING = e.what();\n"

asttoc/src/cppmm_ast_write_rustsys.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,16 @@ void write_function(fmt::ostream& out, const NodeFunction* node_function) {
179179
out.print("pub fn {}({})", node_function->name,
180180
pystring::join(", ", params));
181181

182-
std::string ret = convert_type(node_function->return_type.get());
183-
if (ret != "void") {
184-
out.print(" -> Exception;\n\n");
182+
if (node_function->short_name == "sizeof" ||
183+
node_function->short_name == "alignof") {
184+
out.print(" -> usize;\n\n");
185185
} else {
186-
out.print(";\n\n");
186+
std::string ret = convert_type(node_function->return_type.get());
187+
if (ret != "void") {
188+
out.print(" -> Exception;\n\n");
189+
} else {
190+
out.print(";\n\n");
191+
}
187192
}
188193
}
189194

test/dtor/bind/c-dtor.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct Struct {
1010
using BoundType = ::dtor::Struct;
1111
Struct();
1212
~Struct();
13-
} CPPMM_OPAQUEBYTES;
13+
} CPPMM_OPAQUEPTR;
1414

1515
} // namespace dtor
1616

test/dtor/ref/ast/c-dtor.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"align": 64,
3535
"alias": "Struct",
3636
"attributes": [
37-
"cppmm|opaquebytes"
37+
"cppmm|opaqueptr"
3838
],
3939
"comment": "",
4040
"fields": [

test/dtor/ref/dtor-c/std_string.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,31 @@
44

55
#include <stdexcept>
66

7+
unsigned int std____cxx11__basic_string_char__sizeof()
8+
{
9+
return sizeof(std::__cxx11::basic_string<char>);
10+
}
11+
unsigned int std____cxx11__basic_string_char__alignof()
12+
{
13+
return alignof(std::__cxx11::basic_string<char>);
14+
}
715
unsigned int std____cxx11__basic_string_char__ctor(
8-
std___cxx11_string_t * * this_)
16+
std___cxx11_string_t * this_)
917
{
1018
try {
11-
to_c(this_, new std::__cxx11::basic_string<char>());
19+
new (this_) std::__cxx11::basic_string<char>();
1220
return 0;
1321
} catch (std::exception& e) {
1422
TLG_EXCEPTION_STRING = e.what();
1523
return -1;
1624
}
1725
}
1826
unsigned int std____cxx11__basic_string_char__copy(
19-
std___cxx11_string_t * * this_
27+
std___cxx11_string_t * this_
2028
, std___cxx11_string_t const * rhs)
2129
{
2230
try {
23-
to_c(this_, new std::__cxx11::basic_string<char>(to_cpp_ref(rhs)));
31+
new (this_) std::__cxx11::basic_string<char>(to_cpp_ref(rhs));
2432
return 0;
2533
} catch (std::exception& e) {
2634
TLG_EXCEPTION_STRING = e.what();
@@ -31,7 +39,7 @@ unsigned int std____cxx11__basic_string_char__dtor(
3139
std___cxx11_string_t * this_)
3240
{
3341
try {
34-
delete to_cpp(this_);
42+
(to_cpp(this_)) -> std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string();
3543
return 0;
3644
} catch (std::exception& e) {
3745
TLG_EXCEPTION_STRING = e.what();

test/dtor/ref/dtor-c/std_string.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,29 @@ extern "C" {
55
#endif
66

77
typedef struct std____cxx11__basic_string_char__t_s {
8-
char _unused;
8+
char data[32];
99
} __attribute__((aligned(8))) std____cxx11__basic_string_char__t;
1010
typedef std____cxx11__basic_string_char__t std___cxx11_string_t;
1111

1212

1313

14+
/** returns the size of this type in bytes */
15+
unsigned int std____cxx11__basic_string_char__sizeof();
16+
#define std___cxx11_string_sizeof std____cxx11__basic_string_char__sizeof
17+
18+
19+
/** returns the size of this type in bytes */
20+
unsigned int std____cxx11__basic_string_char__alignof();
21+
#define std___cxx11_string_alignof std____cxx11__basic_string_char__alignof
22+
23+
1424
unsigned int std____cxx11__basic_string_char__ctor(
15-
std___cxx11_string_t * * this_);
25+
std___cxx11_string_t * this_);
1626
#define std___cxx11_string_ctor std____cxx11__basic_string_char__ctor
1727

1828

1929
unsigned int std____cxx11__basic_string_char__copy(
20-
std___cxx11_string_t * * this_
30+
std___cxx11_string_t * this_
2131
, std___cxx11_string_t const * rhs);
2232
#define std___cxx11_string_copy std____cxx11__basic_string_char__copy
2333

test/dtor/ref/dtor-c/std_string_private.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include <string>
99

1010

11+
/** returns the size of this type in bytes */
12+
13+
/** returns the size of this type in bytes */
14+
1115

1216

1317

@@ -66,7 +70,7 @@ inline void to_c(
6670
}
6771

6872
inline void to_c_copy(
69-
std___cxx11_string_t * * lhs
73+
std___cxx11_string_t * lhs
7074
, std::__cxx11::basic_string<char> const & rhs)
7175
{
7276
std____cxx11__basic_string_char__copy(lhs, reinterpret_cast<std___cxx11_string_t const * >(&(rhs)));

test/dtor/ref/dtor-sys/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub use c_dtor::dtor__Struct_dtor as dtor_Struct_dtor;
4949
pub mod std_string;
5050
pub use std_string::std____cxx11__basic_string_char__t as std___cxx11_string_t;
5151

52+
pub use std_string::std____cxx11__basic_string_char__sizeof as std___cxx11_string_sizeof;
53+
pub use std_string::std____cxx11__basic_string_char__alignof as std___cxx11_string_alignof;
5254
pub use std_string::std____cxx11__basic_string_char__ctor as std___cxx11_string_ctor;
5355
pub use std_string::std____cxx11__basic_string_char__copy as std___cxx11_string_copy;
5456
pub use std_string::std____cxx11__basic_string_char__dtor as std___cxx11_string_dtor;

test/dtor/ref/dtor-sys/src/std_string.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@
55
use crate::*;
66
use std::os::raw::*;
77

8-
#[repr(C)]
8+
#[repr(C, align(8))]
9+
#[derive(Clone)]
910
pub struct std____cxx11__basic_string_char__t {
10-
_unused: [u8; 0],
11+
_inner: [u8; 32]
1112
}
1213

14+
impl Default for std____cxx11__basic_string_char__t {
15+
fn default() -> Self {
16+
Self { _inner: [0u8; 32] }
17+
}
18+
}
19+
20+
1321

1422
extern "C" {
1523

16-
pub fn std____cxx11__basic_string_char__ctor(this_: *mut *mut std___cxx11_string_t) -> Exception;
24+
/// returns the size of this type in bytes
25+
pub fn std____cxx11__basic_string_char__sizeof() -> usize;
26+
27+
/// returns the size of this type in bytes
28+
pub fn std____cxx11__basic_string_char__alignof() -> usize;
29+
30+
pub fn std____cxx11__basic_string_char__ctor(this_: *mut std___cxx11_string_t) -> Exception;
1731

18-
pub fn std____cxx11__basic_string_char__copy(this_: *mut *mut std___cxx11_string_t, rhs: *const std___cxx11_string_t) -> Exception;
32+
pub fn std____cxx11__basic_string_char__copy(this_: *mut std___cxx11_string_t, rhs: *const std___cxx11_string_t) -> Exception;
1933

2034
pub fn std____cxx11__basic_string_char__dtor(this_: *mut std___cxx11_string_t) -> Exception;
2135

test/exceptions/ref/exceptions-c/c-ex.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
#include <stdexcept>
55

6+
unsigned int ex__Struct_sizeof()
7+
{
8+
return sizeof(ex::Struct);
9+
}
10+
unsigned int ex__Struct_alignof()
11+
{
12+
return alignof(ex::Struct);
13+
}
614
unsigned int ex__Struct_m1(
715
ex_Struct_t * this_)
816
{

test/exceptions/ref/exceptions-c/c-ex.h

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ typedef ex__Struct_t ex_Struct_t;
1111

1212

1313

14+
/** returns the size of this type in bytes */
15+
unsigned int ex__Struct_sizeof();
16+
#define ex_Struct_sizeof ex__Struct_sizeof
17+
18+
19+
/** returns the size of this type in bytes */
20+
unsigned int ex__Struct_alignof();
21+
#define ex_Struct_alignof ex__Struct_alignof
22+
23+
1424
unsigned int ex__Struct_m1(
1525
ex_Struct_t * this_);
1626
#define ex_Struct_m1 ex__Struct_m1

test/exceptions/ref/exceptions-c/c-ex_private.h

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include <ex.hpp>
99

1010

11+
/** returns the size of this type in bytes */
12+
13+
/** returns the size of this type in bytes */
14+
1115

1216

1317
inline ex::Struct const & to_cpp_ref(

test/exceptions/ref/exceptions-sys/src/c_ex.rs

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ impl Default for ex__Struct_t {
2121

2222
extern "C" {
2323

24+
/// returns the size of this type in bytes
25+
pub fn ex__Struct_sizeof() -> usize;
26+
27+
/// returns the size of this type in bytes
28+
pub fn ex__Struct_alignof() -> usize;
29+
2430
pub fn ex__Struct_m1(this_: *mut ex_Struct_t) -> Exception;
2531

2632
pub fn ex__Struct_m2(this_: *mut ex_Struct_t, return_: *mut c_float, a: c_float) -> Exception;

test/exceptions/ref/exceptions-sys/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ extern {
6464
pub mod c_ex;
6565
pub use c_ex::ex__Struct_t as ex_Struct_t;
6666

67+
pub use c_ex::ex__Struct_sizeof as ex_Struct_sizeof;
68+
pub use c_ex::ex__Struct_alignof as ex_Struct_alignof;
6769
pub use c_ex::ex__Struct_m1 as ex_Struct_m1;
6870
pub use c_ex::ex__Struct_m2 as ex_Struct_m2;
6971
pub use c_ex::ex_f1 as ex_f1;

test/std/bind/std_string.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ namespace cppmm_bind {
77

88
namespace std {
99

10-
#ifdef _GLIBCXX_USE_CXX11_ABI
10+
#if defined(_GLIBCXX_USE_CXX11_ABI)
1111
namespace std = ::std::__cxx11;
12+
#elif defined(_LIBCPP_VERSION)
13+
namespace std = ::std::_LIBCPP_ABI_NAMESPACE;
1214
#else
1315
namespace std = ::std;
1416
#endif

test/std/ref/std-c/std_set.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55

66
#include <stdexcept>
77

8+
unsigned int std__set_std__string__sizeof()
9+
{
10+
return sizeof(std::set<std::string>);
11+
}
12+
unsigned int std__set_std__string__alignof()
13+
{
14+
return alignof(std::set<std::string>);
15+
}
816
unsigned int std__set_std__string__ctor(
9-
std_set_string_t * * this_)
17+
std_set_string_t * this_)
1018
{
1119
try {
12-
to_c(this_, new std::set<std::string>());
20+
new (this_) std::set<std::string>();
1321
return 0;
1422
} catch (std::exception& e) {
1523
TLG_EXCEPTION_STRING = e.what();
@@ -20,7 +28,7 @@ unsigned int std__set_std__string__dtor(
2028
std_set_string_t * this_)
2129
{
2230
try {
23-
delete to_cpp(this_);
31+
(to_cpp(this_)) -> std::set<std::__cxx11::basic_string<char>, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::__cxx11::basic_string<char> > >::~set();
2432
return 0;
2533
} catch (std::exception& e) {
2634
TLG_EXCEPTION_STRING = e.what();

test/std/ref/std-c/std_set.h

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ typedef struct std___Rb_tree_node_base_t_s {
1414
typedef std___Rb_tree_node_base_t std__Rb_tree_node_base_t;
1515

1616
typedef struct std__set_std__string__t_s {
17-
char _unused;
17+
char data[48];
1818
} __attribute__((aligned(8))) std__set_std__string__t;
1919
typedef std__set_std__string__t std_set_string_t;
2020

@@ -34,8 +34,18 @@ typedef std___Rb_tree_const_iterator_std____cxx11__basic_string_char___t std_set
3434

3535

3636

37+
/** returns the size of this type in bytes */
38+
unsigned int std__set_std__string__sizeof();
39+
#define std_set_string_sizeof std__set_std__string__sizeof
40+
41+
42+
/** returns the size of this type in bytes */
43+
unsigned int std__set_std__string__alignof();
44+
#define std_set_string_alignof std__set_std__string__alignof
45+
46+
3747
unsigned int std__set_std__string__ctor(
38-
std_set_string_t * * this_);
48+
std_set_string_t * this_);
3949
#define std_set_string_ctor std__set_std__string__ctor
4050

4151

test/std/ref/std-c/std_set_private.h

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ inline void to_c_copy(
6868
memcpy(lhs, &(rhs), sizeof(*(lhs)));
6969
}
7070

71+
/** returns the size of this type in bytes */
72+
73+
/** returns the size of this type in bytes */
74+
7175

7276

7377

0 commit comments

Comments
 (0)