summaryrefslogtreecommitdiff
path: root/gcc/compare-types.c
blob: 9a615c2dd2ad2a6bc9e9a337722ef6677ada55c1 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "backend.h"
#include "tree.h"
#include "gimple-expr.h"
#include "predict.h"
#include "alloc-pool.h"
#include "tree-pass.h"
#include "cgraph.h"
#include "diagnostic.h"
#include "fold-const.h"
#include "gimple-fold.h"
#include "symbol-summary.h"
#include "tree-vrp.h"
#include "ipa-prop.h"
#include "tree-pretty-print.h"
#include "tree-inline.h"
#include "ipa-fnsummary.h"
#include "ipa-utils.h"
#include "tree-ssa-ccp.h"
#include "stringpool.h"
#include "attribs.h"
#include "tree-ssa-alias.h"
#include "tree-ssanames.h"
#include "gimple.h"
#include "cfg.h"
#include "gimple-iterator.h"
#include "gimple-ssa.h"

#include "compare-types.h"
#include "types-inlines.h"
#include "name-types.h"

static bool
is_incomplete_type(const_tree a)
{
  gcc_assert(a);
  const_tree type_size = TYPE_SIZE(a);
  return NULL_TREE == type_size;
}

unsigned
eq_main_variant(const_tree a, const_tree b)
{
  gcc_assert(a && b);
  const_tree main_variant_a = TYPE_MAIN_VARIANT(a);
  const_tree main_variant_b = TYPE_MAIN_VARIANT(b);
  gcc_assert(main_variant_a && main_variant_b);
  const bool are_equal = main_variant_a == main_variant_b;
  return are_equal;
}

unsigned
eq_canonical_internal(const_tree a, const_tree b)
{
  gcc_assert(a && b);
  const_tree canonical_a = TYPE_CANONICAL(a);
  const_tree canonical_b = TYPE_CANONICAL(b);
  const bool are_equal = canonical_a == canonical_b;
  return are_equal;
}

static bool
eq_record_or_union_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);

  tree field_a = TYPE_FIELDS(a);
  tree field_b = TYPE_FIELDS(b);
  while (field_a || field_b)
  {
    const bool different_lengths = (bool)field_a != (bool)field_b;
    if (different_lengths) return false;

    const_tree field_type_a = TREE_TYPE(field_a);
    const_tree field_type_b = TREE_TYPE(field_b);
    gcc_assert(field_type_a && field_type_b);
    const bool same_field_types = eq_types(field_type_a, field_type_b, force_structural);
    if (!same_field_types) return false;

    field_a = DECL_CHAIN(field_a);
    field_b = DECL_CHAIN(field_b);
  }

  return true;
}

static bool
eq_record_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  assert_is_type(a, RECORD_TYPE);
  assert_is_type(b, RECORD_TYPE);
  return eq_record_or_union_types(a, b, force_structural);
}

static bool
eq_union_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  assert_is_type(a, UNION_TYPE);
  assert_is_type(b, UNION_TYPE);
  return eq_record_or_union_types(a, b, force_structural);
}

static bool
eq_wrapper_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  const_tree inner_type_a = TREE_TYPE(a);
  const_tree inner_type_b = TREE_TYPE(b);
  gcc_assert(inner_type_a && inner_type_b);
  return eq_types(inner_type_a, inner_type_b, force_structural);
}

static bool
eq_pointer_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  assert_is_type(a, POINTER_TYPE);
  assert_is_type(b, POINTER_TYPE);
  return eq_wrapper_types(a, b, force_structural);
}

static bool
eq_reference_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  assert_is_type(a, REFERENCE_TYPE);
  assert_is_type(b, REFERENCE_TYPE);
  return eq_wrapper_types(a, b, force_structural);
}

static bool
eq_array_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  assert_is_type(a, ARRAY_TYPE);
  assert_is_type(b, ARRAY_TYPE);
  return eq_wrapper_types(a, b, force_structural);
}

static bool
eq_function_or_method_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  const_tree return_type_a = TREE_TYPE(a);
  const_tree return_type_b = TREE_TYPE(b);
  const bool is_return_equal = eq_types(return_type_a, return_type_b, force_structural);
  if (!is_return_equal) return false;

  tree parm_a = TYPE_ARG_TYPES(a);
  tree parm_b = TYPE_ARG_TYPES(b);

  while (parm_a || parm_b)
  {
    const bool different_lengths = (bool)parm_a != (bool)parm_b;
    if (different_lengths) return false;

    const_tree parm_type_a = TREE_VALUE(parm_a);
    const_tree parm_type_b = TREE_VALUE(parm_b);
    gcc_assert(parm_type_a && parm_type_b);
    const bool same_field_types = eq_types(parm_type_a, parm_type_b, force_structural);
    if (!same_field_types) return false;

    parm_a = TREE_CHAIN(parm_a);
    parm_b = TREE_CHAIN(parm_b);
  }

  return true;
}

static bool
eq_function_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  assert_is_type(a, FUNCTION_TYPE);
  assert_is_type(b, FUNCTION_TYPE);
  return eq_function_or_method_types(a, b, force_structural);
}

static bool
eq_method_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  assert_is_type(a, METHOD_TYPE);
  assert_is_type(b, METHOD_TYPE);

  const_tree base_type_a = TYPE_METHOD_BASETYPE(a);
  const_tree base_type_b = TYPE_METHOD_BASETYPE(b);
  const bool eq_base_types = eq_types(base_type_a, base_type_b, force_structural);
  return eq_base_types && eq_function_or_method_types(a, b, force_structural);
}

static bool eq_structural(const_tree a, const_tree b, const bool force_structural = false);

unsigned
eq_pointer(const_tree a, const_tree b)
{
  return a == b;
}

unsigned
eq_identifier(const_tree a, const_tree b)
{
  // TODO: Note, we have had the following miscomparison:
  // comparing 0,0: {} == 0,64:void_type*
  // since at least one of these two types is incomplete
  // and both lack an identifier...
  std::string name_a = get_type_identifier(a);
  std::string name_b = get_type_identifier(b);
  const int length_a = name_a.length();
  const int length_b = name_b.length();
  const bool is_sound_input = length_a > 0 || length_b > 0;
  const bool retval = is_sound_input && name_a.compare(name_b) == 0;
  return retval;
}

static bool
eq_structural(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  const enum tree_code code_a = TREE_CODE(a);
  const enum tree_code code_b = TREE_CODE(b);
  const bool equal_code = code_a == code_b;
  if (!equal_code) return false;

  const enum tree_code code = code_a;
  switch (code)
  {
    //FIXME: Just because two codes are the same
    //doesn't mean they are the same type
    //E.g. short != int
    //     float != double
    //     enum a != enum b
    case VOID_TYPE: 
    case INTEGER_TYPE: 
    case REAL_TYPE: 
    case FIXED_POINT_TYPE:
    case COMPLEX_TYPE:
    case ENUMERAL_TYPE:
    case BOOLEAN_TYPE:
    case OFFSET_TYPE:
      return true;
    break;
    default:
    break;
  }

  switch (code)
  {
    case RECORD_TYPE:
      return eq_record_types(a, b, force_structural);
    break;
    case POINTER_TYPE:
      return eq_pointer_types(a, b, force_structural);
    break;
    case REFERENCE_TYPE:
      return eq_reference_types(a, b, force_structural);
    break;
    case ARRAY_TYPE:
      return eq_array_types(a, b, force_structural);
    break;
    case UNION_TYPE:
      return eq_union_types(a, b, force_structural);
    break;
    case FUNCTION_TYPE:
      return eq_function_types(a, b, force_structural);
    break;
    case METHOD_TYPE:
      return eq_method_types(a, b, force_structural);
    break;
    case QUAL_UNION_TYPE:
    case LANG_TYPE:
    default:
      // Unimplemented
      gcc_unreachable();
    break;
  }

  gcc_unreachable();
  return false;
}

unsigned
eq_canonical(const_tree a, const_tree b)
{
  gcc_assert(a && b);
  const bool struct_eq_a = TYPE_STRUCTURAL_EQUALITY_P(a);
  const bool struct_eq_b = TYPE_STRUCTURAL_EQUALITY_P(b);
  const bool use_structural_equality = struct_eq_a || struct_eq_b;
  const bool are_equal = use_structural_equality ? eq_structural(a, b) : eq_canonical_internal(a, b);
  return are_equal;
}


unsigned
eq_type_compare(const_tree a, const_tree b)
{
  return eq_types(a, b);
}

unsigned
eq_type_structural(const_tree a, const_tree b)
{
  return eq_types(a, b, true);
}

unsigned
eq_types(const_tree a, const_tree b, const bool force_structural)
{
  gcc_assert(a && b);
  // This should be before comparing incomplete types.
  // This is not enabled by default, and it is used for
  // testing structural equality limitations.
  if (force_structural) return eq_structural(a, b, force_structural);

  // eq_structural  (a-incomplete, a-complete) = false
  // eq_main_variant(a-incomplete, a-complete) = false
  // eq_canonical   (a-incomplete, a-complete) = false
  // Fallback to eq_identifier only here.
  // This should be the only static call to eq_identifier!

  const bool is_a_incomplete = is_incomplete_type(a);
  const bool is_b_incomplete = is_incomplete_type(b);
  const bool compare_incomplete_types = is_a_incomplete || is_b_incomplete;
  if (compare_incomplete_types) return eq_identifier(a, b);


  // Main variant is unreliable when we are comparing builtin types?
  // comparing 0,64: {}* == 0,64:__gcov_info {0,32:integer_type;8,72: {}*;16,48:integer_type;24,88:integer_type*;32,544:void_type(0,64:integer_type*, 0,32:integer_type, 0,0:void_type)*[];96,128:integer_type;104,168:__gcov_fn_info {0,64: {}*;8,40:integer_type;12,44:integer_type;16,48:integer_type;24,280:__gcov_ctr_info {0,32:integer_type;8,72:integer_type*;}[];}**;}*
  if (eq_main_variant(a, b) && eq_structural(a, b)) return true;  

  if (!eq_canonical(a, b)) return false;

  // optimistic...
  // maybe we should have a MAYBE?
  return eq_structural(a, b);
}

namespace test_type_equality {

static void
test_main_variant()
{
  tree void_a = make_node(VOID_TYPE);
  tree void_b = build_variant_type_copy(void_a);
  const bool expected = true;
  const bool observed = eq_types(void_a, void_b);
  const bool success = expected == observed;
  gcc_assert(success);
}

static void
test_pointer_types_eq()
{
  tree pointer_a = make_node(POINTER_TYPE);
  tree inner_type = make_node(INTEGER_TYPE);
  TREE_TYPE(pointer_a) = inner_type;
  tree pointer_b = build_variant_type_copy(pointer_a);
  const bool expected = true;
  const bool observed = eq_types(pointer_a, pointer_b);
  const bool success = expected == observed;
  gcc_assert(success);
}

static void
test_pointer_types_ne()
{
  tree pointer_a = make_node(POINTER_TYPE);
  tree inner_type_a = make_node(INTEGER_TYPE);
  TREE_TYPE(pointer_a) = inner_type_a;
  tree pointer_b = make_node(POINTER_TYPE);
  tree inner_type_b = make_node(VOID_TYPE);
  TREE_TYPE(pointer_b) = inner_type_b;
  const bool expected = false;
  const bool observed = eq_types(pointer_a, pointer_b, true);
  const bool success = expected == observed;
  gcc_assert(success);
}

static void
test_pointer_types_eq_structurally()
{
  tree pointer_a = make_node(POINTER_TYPE);
  tree inner_type_a = make_node(INTEGER_TYPE);
  TREE_TYPE(pointer_a) = inner_type_a;
  tree pointer_b = make_node(POINTER_TYPE);
  tree inner_type_b = make_node(INTEGER_TYPE);
  TREE_TYPE(pointer_b) = inner_type_b;
  const bool expected = true;
  const bool observed = eq_types(pointer_a, pointer_b, true);
  const bool success = expected == observed;
  gcc_assert(success);
}

static void
test_void_eq()
{
   tree void_a = make_node(VOID_TYPE);
   tree void_b = build_variant_type_copy(void_a);
   const bool expected = true;
   const bool observed = eq_types(void_a, void_b, true);
   const bool success = expected == observed;
   gcc_assert(success);
}	

static void
test_structural_equality_different_types()
{
  tree type_a = make_node(VOID_TYPE);
  tree type_b = make_node(RECORD_TYPE);
  const bool expected = false;
  const bool observed = eq_types(type_a, type_b, true);
  const bool success = expected == observed;
  gcc_assert(success);
}

static void
test_different_record_types()
{
  tree type_a = make_node(RECORD_TYPE);
  tree type_b = make_node(RECORD_TYPE);
  tree field_a = make_node(FIELD_DECL);
  tree field_b = make_node(FIELD_DECL);
  tree type_field_a = make_node(VOID_TYPE);
  tree type_field_b = make_node(RECORD_TYPE);
  TREE_TYPE(field_a) = type_field_a;
  TREE_TYPE(field_b) = type_field_b;
  TYPE_FIELDS(type_a) = field_a;
  TYPE_FIELDS(type_b) = field_b;
  const bool expected = false;
  const bool observed = eq_types(type_a, type_b, true);
  const bool success = expected == observed;
  gcc_assert(success);
}

static void
test_same_record_types()
{
  tree type_a = make_node(RECORD_TYPE);
  tree field_a = make_node(FIELD_DECL);
  tree type_field_a = make_node(VOID_TYPE);
  TREE_TYPE(field_a) = type_field_a;
  TYPE_FIELDS(type_a) = field_a;
  tree type_b = build_variant_type_copy(type_a);
  const bool expected = true;
  const bool observed = eq_types(type_a, type_b, true);
  const bool success = expected == observed;
  gcc_assert(success);
}

void
run_tests()
{
  test_main_variant();
  test_void_eq();
  test_structural_equality_different_types();
  test_different_record_types();
  test_same_record_types();
  test_pointer_types_eq_structurally();
  test_pointer_types_ne();
  test_pointer_types_eq();

}
} // test_type_equality