summaryrefslogtreecommitdiff
path: root/gcc/type-stringifier.hpp
blob: 98e0546b8d7e811c6b8d5ece2ec30c8ff09c85da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#pragma once

#include "type-walker.hpp"
#include <string>

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);
}