Skip to content

Commit 647d93e

Browse files
Merge pull request #86 from luke-titley/sizeof-alignof
This wraps the sizeof and alignof operators.
2 parents eb7609e + 5b9f170 commit 647d93e

File tree

1 file changed

+71
-24
lines changed

1 file changed

+71
-24
lines changed

asttoc/src/cppmm_ast_add_c.cpp

+71-24
Original file line numberDiff line numberDiff line change
@@ -1200,8 +1200,7 @@ std::string find_function_short_name(const NodeFunction& cpp_function) {
12001200
//------------------------------------------------------------------------------
12011201
void general_function(TypeRegistry& type_registry, TranslationUnit& c_tu,
12021202
const NodeFunction& cpp_function,
1203-
const NodeRecord * cpp_record,
1204-
const NodeRecord * c_record);
1203+
const NodeRecord* cpp_record, const NodeRecord* c_record);
12051204

12061205
//------------------------------------------------------------------------------
12071206
// FunctionNames
@@ -1213,15 +1212,12 @@ struct FunctionNames {
12131212

12141213
//------------------------------------------------------------------------------
12151214
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) {
12211217
// Build the new method name. We need to generate unique function names
12221218
// that share a common suffix but with different prefixes
12231219
// 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);
12251221
std::string function_suffix = compute_c_name(short_name);
12261222
std::string function_name = function_prefix + "_" + function_suffix;
12271223
function_name = type_registry.make_symbol_unique(function_name);
@@ -1230,11 +1226,63 @@ FunctionNames compute_function_names(TypeRegistry& type_registry,
12301226
// nice prefix
12311227
function_suffix = function_name.substr(function_prefix.size() + 1);
12321228
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;
12341230

12351231
return FunctionNames{function_name, function_nice_name};
12361232
}
12371233

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+
12381286
//------------------------------------------------------------------------------
12391287
void record_method(TypeRegistry& type_registry, TranslationUnit& c_tu,
12401288
const NodeRecord& cpp_record, const NodeRecord& c_record,
@@ -1791,8 +1839,8 @@ void enum_entry(NodeId& new_id, TypeRegistry& type_registry,
17911839
//------------------------------------------------------------------------------
17921840
void general_function(TypeRegistry& type_registry, TranslationUnit& c_tu,
17931841
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) {
17961844

17971845
// Skip ignored methods
17981846
if (!should_wrap_function(cpp_function)) {
@@ -1853,26 +1901,21 @@ void general_function(TypeRegistry& type_registry, TranslationUnit& c_tu,
18531901
std::string function_name;
18541902
std::string function_nice_name;
18551903

1856-
if( c_record == nullptr )
1857-
{
1904+
if (c_record == nullptr) {
18581905
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)));
18621908

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)));
18671912
} 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);
18711915
function_name = names.long_name;
18721916
function_nice_name = names.nice_name;
18731917
}
18741918

1875-
18761919
// Build the new method name
18771920
if (function_name == function_nice_name) {
18781921
function_name = function_nice_name =
@@ -2083,6 +2126,10 @@ void record_detail(TypeRegistry& type_registry, TranslationUnit& c_tu,
20832126
// Record
20842127
record_fields(type_registry, c_tu, cpp_record, c_record);
20852128

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+
20862133
// Methods
20872134
NodePtr copy_constructor;
20882135
record_methods(type_registry, c_tu, cpp_record, c_record, copy_constructor);

0 commit comments

Comments
 (0)