Skip to content

Commit

Permalink
Merge pull request #220 from chaoticgd/cppout
Browse files Browse the repository at this point in the history
Fix syntax of structs defined in global variables, neaten up symbol database code
  • Loading branch information
chaoticgd authored Aug 8, 2024
2 parents b03557f + 9b95088 commit f698159
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## v2.1

- Variable symbols are no longer incorrectly deduplicated as if they were types.
- Structs defined inside global variable definitions are now printed with the correct C++ syntax.
- Fixed an issue where null addresses would be handled incorrectly.
- stdump: Added `--sort-by-address` option.

Expand Down
36 changes: 20 additions & 16 deletions src/ccc/print_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ bool CppPrinter::data_type(const DataType& symbol, const SymbolDatabase& databas
if(node.descriptor == ast::STRUCT_OR_UNION && node.size_bits > 0) {
m_digits_for_offset = (s32) ceilf(log2(node.size_bits / 8.f) / 4.f);
}
ast_node(node, name, 0, 0, database, !symbol.not_defined_in_any_translation_unit);
ast_node(node, name, 0, 0, database, SymbolDescriptor::DATA_TYPE, !symbol.not_defined_in_any_translation_unit);
fprintf(out, ";\n");

m_last_wants_spacing = wants_spacing;
Expand Down Expand Up @@ -201,7 +201,7 @@ void CppPrinter::function(const Function& symbol, const SymbolDatabase& database
print_cpp_storage_class(out, symbol.storage_class);
if(symbol.type()) {
VariableName dummy;
ast_node(*symbol.type(), dummy, 0, 0, database);
ast_node(*symbol.type(), dummy, 0, 0, database, SymbolDescriptor::FUNCTION);
fprintf(out, " ");
}
print_cpp_variable_name(out, name, BRACKETS_IF_POINTER);
Expand Down Expand Up @@ -247,7 +247,7 @@ void CppPrinter::function(const Function& symbol, const SymbolDatabase& database
VariableName local_name;
local_name.identifier = &variable->name();
if(variable->type()) {
ast_node(*variable->type(), local_name, 0, 1, database);
ast_node(*variable->type(), local_name, 0, 1, database, SymbolDescriptor::LOCAL_VARIABLE);
} else {
print_cpp_variable_name(out, local_name, NO_VAR_PRINT_FLAGS);
}
Expand Down Expand Up @@ -333,7 +333,7 @@ void CppPrinter::global_variable(
VariableName name;
name.identifier = &symbol.name();
if(symbol.type()) {
ast_node(*symbol.type(), name, 0, 0, database);
ast_node(*symbol.type(), name, 0, 0, database, SymbolDescriptor::GLOBAL_VARIABLE);
} else {
print_cpp_variable_name(out, name, NO_VAR_PRINT_FLAGS);
}
Expand All @@ -352,6 +352,7 @@ void CppPrinter::ast_node(
s32 base_offset,
s32 indentation_level,
const SymbolDatabase& database,
SymbolDescriptor symbol_descriptor,
bool print_body)
{
VariableName this_name{&node.name};
Expand All @@ -378,13 +379,13 @@ void CppPrinter::ast_node(
const ast::Array& array = node.as<ast::Array>();
CCC_ASSERT(array.element_type.get());
name.array_indices.emplace_back(array.element_count);
ast_node(*array.element_type.get(), name, base_offset, indentation_level, database);
ast_node(*array.element_type.get(), name, base_offset, indentation_level, database, symbol_descriptor);
break;
}
case ast::BITFIELD: {
const ast::BitField& bit_field = node.as<ast::BitField>();
CCC_ASSERT(bit_field.underlying_type.get());
ast_node(*bit_field.underlying_type.get(), name, base_offset, indentation_level, database);
ast_node(*bit_field.underlying_type.get(), name, base_offset, indentation_level, database, symbol_descriptor);
fprintf(out, " : %d", bit_field.size_bits);
break;
}
Expand Down Expand Up @@ -453,7 +454,7 @@ void CppPrinter::ast_node(
if(!function.is_constructor_or_destructor) {
if(function.return_type.has_value()) {
VariableName dummy;
ast_node(*function.return_type->get(), dummy, 0, indentation_level, database);
ast_node(*function.return_type->get(), dummy, 0, indentation_level, database, symbol_descriptor);
fprintf(out, " ");
}
}
Expand Down Expand Up @@ -485,7 +486,7 @@ void CppPrinter::ast_node(
}
for(size_t i = start; i < parameters->size(); i++) {
VariableName dummy;
ast_node(*(*parameters)[i].get(), dummy, 0, indentation_level, database);
ast_node(*(*parameters)[i].get(), dummy, 0, indentation_level, database, symbol_descriptor);
if(i != parameters->size() - 1) {
fprintf(out, ", ");
}
Expand All @@ -505,7 +506,7 @@ void CppPrinter::ast_node(
} else {
name.pointer_chars.emplace_back('&');
}
ast_node(*pointer_or_reference.value_type.get(), name, base_offset, indentation_level, database);
ast_node(*pointer_or_reference.value_type.get(), name, base_offset, indentation_level, database, symbol_descriptor);
print_cpp_variable_name(out, name, INSERT_SPACE_TO_LEFT);
break;
}
Expand All @@ -514,9 +515,9 @@ void CppPrinter::ast_node(
// but for now lets not think about that.
const ast::PointerToDataMember& member_pointer = node.as<ast::PointerToDataMember>();
VariableName dummy;
ast_node(*member_pointer.member_type.get(), dummy, 0, indentation_level, database);
ast_node(*member_pointer.member_type.get(), dummy, 0, indentation_level, database, symbol_descriptor);
fprintf(out, " ");
ast_node(*member_pointer.class_type.get(), dummy, 0, indentation_level, database);
ast_node(*member_pointer.class_type.get(), dummy, 0, indentation_level, database, symbol_descriptor);
fprintf(out, "::");
print_cpp_variable_name(out, name, NO_VAR_PRINT_FLAGS);
break;
Expand All @@ -529,7 +530,10 @@ void CppPrinter::ast_node(
} else {
fprintf(out, "union");
}
bool name_on_top = (indentation_level == 0) && (struct_or_union.storage_class != STORAGE_CLASS_TYPEDEF);
bool name_on_top =
indentation_level == 0 &&
struct_or_union.storage_class != STORAGE_CLASS_TYPEDEF &&
symbol_descriptor == SymbolDescriptor::DATA_TYPE;
if(name_on_top) {
print_cpp_variable_name(out, name, INSERT_SPACE_TO_LEFT);
}
Expand All @@ -548,7 +552,7 @@ void CppPrinter::ast_node(
fprintf(out, "virtual ");
}
VariableName dummy;
ast_node(base_class, dummy, 0, indentation_level + 1, database);
ast_node(base_class, dummy, 0, indentation_level + 1, database, symbol_descriptor);
if(i != struct_or_union.base_classes.size() - 1) {
fprintf(out, ", ");
}
Expand All @@ -573,7 +577,7 @@ void CppPrinter::ast_node(
indent(out, indentation_level + 1);
offset(*field.get(), base_offset);
VariableName dummy;
ast_node(*field.get(), dummy, base_offset + field->offset_bytes, indentation_level + 1, database);
ast_node(*field.get(), dummy, base_offset + field->offset_bytes, indentation_level + 1, database, symbol_descriptor);
fprintf(out, ";\n");
}

Expand All @@ -594,7 +598,7 @@ void CppPrinter::ast_node(
}
indent(out, indentation_level + 1);
VariableName dummy;
ast_node(*member_function, dummy, 0, indentation_level + 1, database);
ast_node(*member_function, dummy, 0, indentation_level + 1, database, symbol_descriptor);
fprintf(out, ";\n");
}
}
Expand Down Expand Up @@ -650,7 +654,7 @@ void CppPrinter::function_parameters(std::span<const ParameterVariable*> paramet
VariableName variable_name;
variable_name.identifier = &parameter_variable.name();
if(parameter_variable.type()) {
ast_node(*parameter_variable.type(), variable_name, 0, 0, database);
ast_node(*parameter_variable.type(), variable_name, 0, 0, database, SymbolDescriptor::PARAMETER_VARIABLE);
} else {
print_cpp_variable_name(out, variable_name, NO_VAR_PRINT_FLAGS);
}
Expand Down
1 change: 1 addition & 0 deletions src/ccc/print_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class CppPrinter {
s32 base_offset,
s32 indentation_level,
const SymbolDatabase& database,
SymbolDescriptor symbol_descriptor,
bool print_body = true);
void function_parameters(std::span<const ParameterVariable*> parameters, const SymbolDatabase& database);
void refined_data(const RefinedData& data, s32 indentation_level);
Expand Down
20 changes: 4 additions & 16 deletions src/ccc/symbol_database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,7 @@ Result<SymbolType*> SymbolList<SymbolType>::create_symbol(
symbol.m_address = address;
}

// Call SymbolType::on_create if it exists.
if constexpr(requires { symbol.on_create(); }) {
symbol.on_create();
}
symbol.on_create();

CCC_ASSERT(symbol.source().valid());

Expand Down Expand Up @@ -379,10 +376,7 @@ bool SymbolList<SymbolType>::mark_symbol_for_destruction(SymbolHandle<SymbolType

symbol->mark_for_destruction();

// Call SymbolType::on_destroy if it exists.
if constexpr(requires { symbol->on_destroy(database); }) {
symbol->on_destroy(database);
}
symbol->on_destroy(database);

return true;
}
Expand All @@ -397,10 +391,7 @@ void SymbolList<SymbolType>::mark_symbols_from_source_for_destruction(SymbolSour

symbol.mark_for_destruction();

// Call SymbolType::on_destroy if it exists.
if constexpr(requires { symbol.on_destroy(database); }) {
symbol.on_destroy(database);
}
symbol.on_destroy(database);
}
}

Expand All @@ -414,10 +405,7 @@ void SymbolList<SymbolType>::mark_symbols_from_module_for_destruction(ModuleHand

symbol.mark_for_destruction();

// Call SymbolType::on_destroy if it exists.
if constexpr(requires { symbol.on_destroy(database); }) {
symbol.on_destroy(database);
}
symbol.on_destroy(database);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/ccc/symbol_database.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ class Symbol {
bool is_marked_for_destruction() { return m_marked_for_destruction; }

protected:
void on_create() {}
void on_destroy(SymbolDatabase* database) {}

u32 m_handle = (u32) -1;
SymbolSourceHandle m_source;
Address m_address;
Expand Down

0 comments on commit f698159

Please sign in to comment.