#pragma once #include "type-walker.hpp" #include class TypeStringifier : public TypeWalker { private: std::string _stringification; static std::string get_type_identifier(const_tree t); static std::string get_field_identifier(const_tree t); void _stringify_simple(const_tree t); void _stringify_aggregate_pre(const_tree t); void _stringify_aggregate_post(const_tree t); void _stringify_fm_pre(const_tree t); void _stringify_fm_post(const_tree t); virtual void _walk_void_pre(const_tree t); virtual void _walk_integer_pre(const_tree t); virtual void _walk_real_pre(const_tree t); virtual void _walk_fixed_point_pre(const_tree t); virtual void _walk_complex_pre(const_tree t); virtual void _walk_boolean_pre(const_tree t); virtual void _walk_offset_pre(const_tree t); virtual void _walk_pointer_post(const_tree t); virtual void _walk_reference_post(const_tree t); virtual void _walk_array_post(const_tree t); virtual void _walk_record_pre(const_tree t); virtual void _walk_record_post(const_tree t); virtual void _walk_union_pre(const_tree t); virtual void _walk_union_post(const_tree t); virtual void _walk_field_post(const_tree t); virtual void _walk_return_pre(const_tree t); virtual void _walk_return_post(const_tree t); virtual void _walk_args_pre(const_tree t); virtual void _walk_args_post(const_tree t); virtual void _walk_arg_post(const_tree t); virtual void _walk_method_pre(const_tree t); virtual void _walk_method_post(const_tree t); virtual void _walk_function_pre(const_tree t); virtual void _walk_function_post(const_tree t); public: std::string stringify(const_tree t); TypeStringifier() {}; }; std::string TypeStringifier::stringify(const_tree t) { _stringification.clear(); gcc_assert(t); walk(t); return _stringification; } void TypeStringifier::_walk_void_pre(const_tree t) { _stringify_simple(t); } void TypeStringifier::_walk_integer_pre(const_tree t) { _stringify_simple(t); } void TypeStringifier::_walk_real_pre(const_tree t) { _stringify_simple(t); } void TypeStringifier::_walk_fixed_point_pre(const_tree t) { _stringify_simple(t); } void TypeStringifier::_walk_complex_pre(const_tree t) { _stringify_simple(t); } void TypeStringifier::_walk_offset_pre(const_tree t) { _stringify_simple(t); } void TypeStringifier::_walk_boolean_pre(const_tree t) { _stringify_simple(t); } void TypeStringifier::_stringify_simple(const_tree t) { gcc_assert(t); const enum tree_code code = TREE_CODE(t); this->_stringification += std::string(get_tree_code_name(code)); } void TypeStringifier::_walk_pointer_post(const_tree t) { this->_stringification += std::string("*"); } void TypeStringifier::_walk_array_post(const_tree t) { this->_stringification += std::string("[]"); } void TypeStringifier::_walk_reference_post(const_tree t) { this->_stringification += std::string("&"); } void TypeStringifier::_walk_union_pre(const_tree t) { this->_stringification += std::string(" union "); _stringify_aggregate_pre(t); } void TypeStringifier::_walk_union_post(const_tree t) { _stringify_aggregate_post(t); } void TypeStringifier::_walk_record_pre(const_tree t) { this->_stringification += std::string(" record "); _stringify_aggregate_pre(t); } void TypeStringifier::_walk_record_post(const_tree t) { _stringify_aggregate_post(t); } void TypeStringifier::_stringify_aggregate_pre(const_tree t) { this->_stringification += TypeStringifier::get_type_identifier(t) + std::string(" {"); } void TypeStringifier::_stringify_aggregate_post(const_tree t) { this->_stringification += std::string("}"); } void TypeStringifier::_walk_field_post(const_tree t) { this->_stringification += std::string(" ") + TypeStringifier::get_field_identifier(t) + std::string(";"); } void TypeStringifier::_walk_method_pre(const_tree t) { _stringify_fm_pre(t); } void TypeStringifier::_walk_method_post(const_tree t) { _stringify_fm_post(t); } void TypeStringifier::_walk_function_pre(const_tree t) { _stringify_fm_pre(t); } void TypeStringifier::_walk_function_post(const_tree t) { _stringify_fm_post(t); } void TypeStringifier::_stringify_fm_pre(const_tree t) { this->_stringification += std::string("function { "); } void TypeStringifier::_stringify_fm_post(const_tree t) { this->_stringification += std::string("}"); } void TypeStringifier::_walk_return_pre(const_tree t) { this->_stringification += std::string("("); } void TypeStringifier::_walk_return_post(const_tree t) { this->_stringification += std::string(")"); } void TypeStringifier::_walk_args_pre(const_tree t) { this->_stringification += std::string("("); } void TypeStringifier::_walk_args_post(const_tree t) { this->_stringification += std::string(")"); } void TypeStringifier::_walk_arg_post(const_tree t) { this->_stringification += std::string(", "); } std::string TypeStringifier::get_type_identifier(const_tree t) { tree name = TYPE_NAME(t); const bool no_name = NULL_TREE == name; if (no_name) return std::string(""); const enum tree_code name_code = TREE_CODE(name); const bool is_name_type_decl = TYPE_DECL == name_code; name = is_name_type_decl ? DECL_NAME(name) : name; const char* identifier_ptr = IDENTIFIER_POINTER(name); gcc_assert(identifier_ptr); return std::string(identifier_ptr); } std::string TypeStringifier::get_field_identifier(const_tree t) { assert_is_type(t, FIELD_DECL); const_tree decl_name = DECL_NAME(t); if (!decl_name) return std::string(""); const char* identifier = IDENTIFIER_POINTER(decl_name); return std::string(identifier); }