summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErick Ochoa <erick.ochoa@theobroma-systems.com>2020-06-02 11:04:53 +0200
committerErick Ochoa <erick.ochoa@theobroma-systems.com>2020-06-02 11:04:53 +0200
commit71e7fa841fc569af0d7fabf3303a26df84eefcc6 (patch)
tree1cdaed4f0ae3458a48a2c3020a92a225552c1925
parent96e3d6fcd58eafa59f78df0ec9af5173c03f8962 (diff)
Add type stringifier
-rw-r--r--gcc/type-stringifier.hpp203
1 files changed, 203 insertions, 0 deletions
diff --git a/gcc/type-stringifier.hpp b/gcc/type-stringifier.hpp
new file mode 100644
index 00000000000..33f5cb359cb
--- /dev/null
+++ b/gcc/type-stringifier.hpp
@@ -0,0 +1,203 @@
+#pragma once
+
+#include "type-walker.hpp"
+
+class TypeStringifier : public TypeWalker
+{
+private:
+ std::string _stringification;
+
+ static std::string get_type_identifier(const_tree t);
+
+ void _stringifiy_simple(const_tree t);
+ void _stringifiy_aggregate_pre(const_tree t);
+ void _stringifiy_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_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 stringifiy(const_tree t);
+ TypeStringifier() {};
+};
+
+std::string
+TypeStringifier::stringify(const_tree t)
+{
+ gcc_assert(t);
+ walk(t);
+}
+
+virtual void
+TypeStringifier::_walk_void_pre(const_tree t)
+{
+ _stringifiy_simple(t);
+}
+
+virtual void
+TypeStringifier::_walk_integer_pre(const_tree t)
+{
+ _stringifiy_simple(t);
+}
+
+virtual void
+TypeStringifier::_walk_real_pre(const_tree t)
+{
+ _stringifiy_simple(t);
+}
+
+virtual void
+TypeStringifier::_walk_fixed_point_pre(const_tree t)
+{
+ _stringifiy_simple(t);
+}
+
+virtual void
+TypeStringifier::_walk_complex_pre(const_tree t)
+{
+ _stringifiy_simple(t);
+}
+
+virtual void
+TypeStringifier::_walk_offset_pre(const_tree t)
+{
+ _stringifiy_simple(t);
+}
+
+virtual void
+TypeStringifier::_walk_boolean_pre(const_tree t)
+{
+ _stringifiy_simple(t);
+}
+
+void
+TypeStringifier::_stringifiy_simple(const_tree t)
+{
+ gcc_assert(t);
+ const enum tree_code code = TREE_CODE(t);
+ this->_stringification += std::string(get_tree_code_name(code));
+}
+
+virtual void
+TypeStringifier::_walk_pointer_post(const_tree t)
+{
+ this->_stringification += std::string("*");
+}
+
+virtual void
+TypeStringifier::_walk_array_post(const_tree t)
+{
+ this->_stringification += std::string("[]");
+}
+
+virtual void
+TypeStringifier::_walk_reference_post(const_tree t)
+{
+ this->_stringification += std::string("&");
+}
+
+virtual void
+TypeStringifier::_walk_union_pre(const_tree t)
+{
+ _stringify_aggregate_pre(t);
+}
+
+virtual void
+TypeStringifier::_walk_union_post(const_tree t)
+{
+ _stringify_aggregate_post(t);
+}
+
+virtual void
+TypeStringifier::_walk_record_pre(const_tree t)
+{
+ _stringify_aggregate_pre(t);
+}
+
+virtual 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_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("}");
+}
+
+static 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);
+}
+