@@ -1200,8 +1200,7 @@ std::string find_function_short_name(const NodeFunction& cpp_function) {
1200
1200
// ------------------------------------------------------------------------------
1201
1201
void general_function (TypeRegistry& type_registry, TranslationUnit& c_tu,
1202
1202
const NodeFunction& cpp_function,
1203
- const NodeRecord * cpp_record,
1204
- const NodeRecord * c_record);
1203
+ const NodeRecord* cpp_record, const NodeRecord* c_record);
1205
1204
1206
1205
// ------------------------------------------------------------------------------
1207
1206
// FunctionNames
@@ -1213,15 +1212,12 @@ struct FunctionNames {
1213
1212
1214
1213
// ------------------------------------------------------------------------------
1215
1214
FunctionNames compute_function_names (TypeRegistry& type_registry,
1216
- const NodeRecord& cpp_record,
1217
- const NodeRecord& c_record,
1218
- const NodeFunction& cpp_function) {
1219
- auto short_name = find_function_short_name (cpp_function);
1220
-
1215
+ const NodeRecord& record,
1216
+ const std::string& short_name) {
1221
1217
// Build the new method name. We need to generate unique function names
1222
1218
// that share a common suffix but with different prefixes
1223
1219
// The full prefix we get by stripping the "_t" from the struct name
1224
- std::string function_prefix = pystring::slice (c_record .name , 0 , -2 );
1220
+ std::string function_prefix = pystring::slice (record .name , 0 , -2 );
1225
1221
std::string function_suffix = compute_c_name (short_name);
1226
1222
std::string function_name = function_prefix + " _" + function_suffix;
1227
1223
function_name = type_registry.make_symbol_unique (function_name);
@@ -1230,11 +1226,63 @@ FunctionNames compute_function_names(TypeRegistry& type_registry,
1230
1226
// nice prefix
1231
1227
function_suffix = function_name.substr (function_prefix.size () + 1 );
1232
1228
std::string function_nice_name =
1233
- pystring::slice (c_record .nice_name , 0 , -2 ) + " _" + function_suffix;
1229
+ pystring::slice (record .nice_name , 0 , -2 ) + " _" + function_suffix;
1234
1230
1235
1231
return FunctionNames{function_name, function_nice_name};
1236
1232
}
1237
1233
1234
+ // ------------------------------------------------------------------------------
1235
+ FunctionNames compute_function_names (TypeRegistry& type_registry,
1236
+ const NodeRecord& cpp_record,
1237
+ const NodeRecord& c_record,
1238
+ const NodeFunction& cpp_function) {
1239
+ auto short_name = find_function_short_name (cpp_function);
1240
+
1241
+ return compute_function_names (type_registry, c_record, short_name);
1242
+ }
1243
+
1244
+ // ------------------------------------------------------------------------------
1245
+ void record_memory (TypeRegistry& type_registry, TranslationUnit& c_tu,
1246
+ const NodeRecord& cpp_record, const NodeRecord& c_record,
1247
+ const char * memory_operator) {
1248
+
1249
+ auto sizeof_ = NodeFunctionCallExpr::n (
1250
+ memory_operator,
1251
+ std::vector<NodeExprPtr>{NodeVarRefExpr::n (cpp_record.name )},
1252
+ std::vector<NodeTypePtr>{});
1253
+
1254
+ auto body = NodeBlockExpr::n (
1255
+ std::vector<NodeExprPtr>({NodeReturnExpr::n (sizeof_)}));
1256
+
1257
+ auto return_ = NodeBuiltinType::n (" " , 0 , " unsigned int" , false );
1258
+
1259
+ auto names =
1260
+ compute_function_names (type_registry, c_record, memory_operator);
1261
+
1262
+ 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>());
1267
+ c_function->body = body;
1268
+
1269
+ c_tu.decls .push_back (NodePtr (c_function));
1270
+ }
1271
+
1272
+ // ------------------------------------------------------------------------------
1273
+ void record_sizeof (TypeRegistry& type_registry, TranslationUnit& c_tu,
1274
+ const NodeRecord& cpp_record, const NodeRecord& c_record) {
1275
+
1276
+ record_memory (type_registry, c_tu, cpp_record, c_record, " sizeof" );
1277
+ }
1278
+
1279
+ // ------------------------------------------------------------------------------
1280
+ void record_alignof (TypeRegistry& type_registry, TranslationUnit& c_tu,
1281
+ const NodeRecord& cpp_record, const NodeRecord& c_record) {
1282
+
1283
+ record_memory (type_registry, c_tu, cpp_record, c_record, " alignof" );
1284
+ }
1285
+
1238
1286
// ------------------------------------------------------------------------------
1239
1287
void record_method (TypeRegistry& type_registry, TranslationUnit& c_tu,
1240
1288
const NodeRecord& cpp_record, const NodeRecord& c_record,
@@ -1791,8 +1839,8 @@ void enum_entry(NodeId& new_id, TypeRegistry& type_registry,
1791
1839
// ------------------------------------------------------------------------------
1792
1840
void general_function (TypeRegistry& type_registry, TranslationUnit& c_tu,
1793
1841
const NodeFunction& cpp_function,
1794
- const NodeRecord * cpp_record = nullptr ,
1795
- const NodeRecord * c_record = nullptr ) {
1842
+ const NodeRecord* cpp_record = nullptr ,
1843
+ const NodeRecord* c_record = nullptr ) {
1796
1844
1797
1845
// Skip ignored methods
1798
1846
if (!should_wrap_function (cpp_function)) {
@@ -1853,26 +1901,21 @@ void general_function(TypeRegistry& type_registry, TranslationUnit& c_tu,
1853
1901
std::string function_name;
1854
1902
std::string function_nice_name;
1855
1903
1856
- if ( c_record == nullptr )
1857
- {
1904
+ if (c_record == nullptr ) {
1858
1905
function_name = compute_c_name (
1859
- compute_qualified_name ( type_registry, cpp_function.namespaces ,
1860
- find_function_short_name (cpp_function))
1861
- );
1906
+ compute_qualified_name (type_registry, cpp_function.namespaces ,
1907
+ find_function_short_name (cpp_function)));
1862
1908
1863
- function_nice_name =
1864
- compute_c_name (compute_qualified_nice_name (
1865
- type_registry, cpp_function.namespaces ,
1866
- find_function_short_name (cpp_function)));
1909
+ function_nice_name = compute_c_name (compute_qualified_nice_name (
1910
+ type_registry, cpp_function.namespaces ,
1911
+ find_function_short_name (cpp_function)));
1867
1912
} else {
1868
- auto names =
1869
- compute_function_names (type_registry, *cpp_record, *c_record,
1870
- cpp_function);
1913
+ auto names = compute_function_names (type_registry, *cpp_record,
1914
+ *c_record, cpp_function);
1871
1915
function_name = names.long_name ;
1872
1916
function_nice_name = names.nice_name ;
1873
1917
}
1874
1918
1875
-
1876
1919
// Build the new method name
1877
1920
if (function_name == function_nice_name) {
1878
1921
function_name = function_nice_name =
@@ -2083,6 +2126,10 @@ void record_detail(TypeRegistry& type_registry, TranslationUnit& c_tu,
2083
2126
// Record
2084
2127
record_fields (type_registry, c_tu, cpp_record, c_record);
2085
2128
2129
+ // 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);
2132
+
2086
2133
// Methods
2087
2134
NodePtr copy_constructor;
2088
2135
record_methods (type_registry, c_tu, cpp_record, c_record, copy_constructor);
0 commit comments