type.c 13.8 KB
Newer Older
Christian Schäfer's avatar
Christian Schäfer committed
1
2
3
4
5
6
7
8
9
10
/* Copyright (C) 1998 - 2000 by Universitaet Karlsruhe
** All rights reserved.
**
** Authors: Martin Trapp, Christian Schaefer &
**          Goetz Lindenmaier
**
** type.c: datastructures to hold type information.
*/

# include "type.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
11
12
# include "irprog.h"  /* So that constructors can add the type to global
			 data structure. */
13
# include "array.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
14
# include "ident_t.h"
Christian Schäfer's avatar
Christian Schäfer committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

unsigned long type_visited = 0;

void
init (void)
{
}

/*******************************************************************/
/** TYPE_CLASS                                                    **/
/*******************************************************************/

type_class *
new_type_class (ident *name)//, int members)
{
  type_class *res;

  res = (type_class *) xmalloc (sizeof (type_class));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
33
  add_irp_type((type *) res);   /* Remember the new type global. */
Christian Schäfer's avatar
Christian Schäfer committed
34
35
36
  res->kind = k_type_class;
  res->name = name;

37
38
39
  res->members = NEW_ARR_F (entity *, 1);
  res->subtypes = NEW_ARR_F (type_class *, 1);
  res->supertypes = NEW_ARR_F (type_class *, 1);
Christian Schäfer's avatar
Christian Schäfer committed
40
41
42
43
44
45
46

  res->visit = 0;

  return res;
}

/* manipulate fields of type_class */
Götz Lindenmaier's avatar
Götz Lindenmaier committed
47
const char *
Christian Schäfer's avatar
Christian Schäfer committed
48
49
get_class_name  (type_class *class) {
  assert(class);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
50
  return id_to_str(class->name);
Christian Schäfer's avatar
Christian Schäfer committed
51
52
}

53
/* field: ident */
Christian Schäfer's avatar
Christian Schäfer committed
54
55
56
57
58
59
60
61
62
63
64
ident *
get_class_ident (type_class *class) {
  assert(class);
  return class->name;
}

/*
void   set_class_name  (type_class *class, char *name);
void   set_class_ident (type_class *class, ident* ident);
*/

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* field: member */
void
add_class_member (type_class *class, entity *member)
{
  ARR_APP1 (entity *, class->members, member);
}

entity *
get_class_member (type_class *class, int pos)
{
  assert (class);
  return class->members[pos+1];
}

void
set_class_member (type_class *class, entity *member, int pos)
{
  class->members[pos+1] = member;
}

85
86
87
88
89
90
91
92
93
94
int
get_class_n_member (type_class *class)
{
  int res;

  assert(class);
  res = (ARR_LEN (class->members))-1;
  return res;
}

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* field: subtype */
void
add_class_subtype (type_class *class,  type_class *subtype)
{
  ARR_APP1 (type_class *, class->subtypes, subtype);
}

type_class *
get_class_subtype (type_class *class, int pos)
{
  assert (class);
  return class->subtypes[pos+1];
}

void
set_class_subtype (type_class *class, type_class *subtype, int pos)
{
  class->subtypes[pos+1] = subtype;
}

115
116
117
118
int
get_class_n_subtype (type_class *class)
{
  assert(class);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
119
  return (ARR_LEN (class->subtypes))-1;
120
121
}

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* field: supertype */
void
add_class_supertype (type_class *class, type_class *supertype)
{
  ARR_APP1 (type_class *, class->supertypes, supertype);
}

type_class *
get_class_supertype (type_class *class, int pos)
{
  assert (class);
  return class->supertypes[pos+1];
}

void
set_class_supertype (type_class *class, type_class *supertype, int pos)
{
  class->supertypes[pos+1] = supertype;
}
Christian Schäfer's avatar
Christian Schäfer committed
141

142
143
144
145
int
get_class_n_supertype (type_class *class)
{
  assert(class);
Götz Lindenmaier's avatar
Götz Lindenmaier committed
146
  return (ARR_LEN (class->supertypes))-1;
147
148
}

Christian Schäfer's avatar
Christian Schäfer committed
149
150
151
152
153
154
155
156
157
158
/*******************************************************************/
/** TYPE_STRCT                                                   **/
/*******************************************************************/

type_strct *
new_type_strct (ident *name)//, int members)
{
  type_strct *res;

  res = (type_strct *) xmalloc (sizeof (type_strct));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
159
  add_irp_type((type *) res);   /* Remember the new type global. */
Christian Schäfer's avatar
Christian Schäfer committed
160
161
162
  res->kind = k_type_strct;
  res->name = name;

Götz Lindenmaier's avatar
Götz Lindenmaier committed
163
  res->members = NEW_ARR_F (entity *, 1);
Christian Schäfer's avatar
Christian Schäfer committed
164
165
166
167
168
169
  res->visit = 0;

  return res;
}

/* manipulate fields of type_strct */
Götz Lindenmaier's avatar
Götz Lindenmaier committed
170
171

const char *
Christian Schäfer's avatar
Christian Schäfer committed
172
173
174
175
get_strct_name  (type_strct *strct) {
  assert(strct);
  return ID_TO_STR(strct->name);
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
176

Christian Schäfer's avatar
Christian Schäfer committed
177
178
179
180
181
182
183

ident *
get_strct_ident (type_strct *strct) {
  assert(strct);
  return strct->name;
}

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
int
get_strct_n_member (type_strct *strct)
{
  int res;

  assert(strct);
  res = (ARR_LEN (strct->members))-1;
  return res;
}

void
add_strct_member (type_strct *strct, entity *member)
{
  ARR_APP1 (type_strct *, strct->members, member);
}

entity *
get_strct_member (type_strct *strct, int pos)
{
  assert (strct);
  return strct->members[pos+1];
}

void
set_strct_member (type_strct *strct, int pos, entity *member)
{
  strct->members[pos+1] = member;
}

Christian Schäfer's avatar
Christian Schäfer committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
void   set_strct_name  (type_strct *strct, char *name);
void   set_strct_ident (type_strct *strct, ident* ident);
*/


/*******************************************************************/
/** TYPE_METHOD                                                   **/
/*******************************************************************/

/* create a new type_method */
type_method *
new_type_method (ident *name, int arity, int n_res)
{
  type_method *res;

  res = (type_method *) xmalloc (sizeof (type_method));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
230
  add_irp_type((type *) res);   /* Remember the new type global. */
Christian Schäfer's avatar
Christian Schäfer committed
231
232
233
234
235
236
237
238
239
240
241
242
243
  res->kind = k_type_method;
  res->name = name;   // do I need the name, or is the name in entity sufficient?
  res->arity = arity;
  res->param_type = (type **) xmalloc (sizeof (type *) * arity);
  res->n_res  = n_res;
  res->res_type = (type **) xmalloc (sizeof (type *) * n_res);

  res->visit = 0;

  return res;
}

/* manipulate fields of type_method */
Götz Lindenmaier's avatar
Götz Lindenmaier committed
244
const char *
Christian Schäfer's avatar
Christian Schäfer committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
get_method_name  (type_method *method) {
  assert(method);
  return ID_TO_STR(method->name);
}

ident *
get_method_ident (type_method *method) {
  assert(method);
  return method->name;
}

/*
void   set_method_name  (type_method *method, char *name);
void   set_method_ident (type_method *method, ident* ident);
*/

Götz Lindenmaier's avatar
Götz Lindenmaier committed
261
262
263
264
265
266

inline int
get_method_n_params (type_method *method) {
  return method->arity;
}

Christian Schäfer's avatar
Christian Schäfer committed
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
inline int
get_method_arity (type_method *method) {
  return method->arity;
}

/*
inline void
set_method_arity (type_method *method, int arity) {
  method->arity = arity;
  / change array size, somehow copy.  *
}
*/

inline type *
get_method_param_type(type_method *method, int pos) {
  return method->param_type[pos];
}

inline void
set_method_param_type(type_method *method, int pos, type* type) {
  method->param_type[pos] = type;
}


inline int
get_method_n_res (type_method *method) {
  return method->n_res;
}

/*
inline void
set_method_n_res (type_method *method, int n_res) {
  method->n_res = n_res;
}
*/

inline type *
get_method_res_type(type_method *method, int pos) {
  return method->res_type[pos];
}

inline void
set_method_res_type(type_method *method, int pos, type* type) {
  method->res_type[pos] = type;
}


/*******************************************************************/
/** TYPE_UNION                                                    **/
/*******************************************************************/

/* create a new type_union -- set unioned types by hand. */
type_union *
new_type_union (ident *name, int n_types)
{
  type_union *res;

  res = (type_union *) xmalloc (sizeof (type_union));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
325
  add_irp_type((type *) res);   /* Remember the new type global. */
Christian Schäfer's avatar
Christian Schäfer committed
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
  res->kind = k_type_union;
  res->name = name;   // do I need a name?
  res->n_types = n_types;
  /*
  res->unioned_type = (int *) xmalloc (sizeof (int) * n_types);
  */

  res->visit = 0;

  return res;
}

/* manipulate fields of type_union */
/*
char *
get_union_name  (type_union *uni) {
  assert(uni);
  return ID_TO_STR(uni->name);
}
*/

ident *
get_union_ident (type_union *uni) {
  assert(uni);
  return uni->name;
}

/*
void   set_union_name  (type_union *union, char *name);
void   set_union_ident (type_union *union, ident* ident);
*/
/*
int    get_union_n_types (type_union *union);
void   set_union_n_types (type_union *union, int n);
type  *get_union_unioned_type (type_union *union, int pos);
void   set_union_unioned_type (type_union *union, int pos, type *type);
*/

/*******************************************************************/
/** TYPE_ARRAY                                                    **/
/*******************************************************************/

/* create a new type_array */
inline type_array *
new_type_array (ident *name, int n_dimensions)
{
  type_array *res;

  res = (type_array *) xmalloc (sizeof (type_array));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
375
  add_irp_type((type *) res);   /* Remember the new type global. */
Christian Schäfer's avatar
Christian Schäfer committed
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
  res->kind = k_type_array;
  res->name = name;
  res->n_dimensions = n_dimensions;
  res->lower_bound = (int *) xmalloc (sizeof (int) * n_dimensions);
  res->upper_bound = (int *) xmalloc (sizeof (int) * n_dimensions);

  res->visit = 0;

  return res;
}

/* manipulate fields of type_array */
/*
char *
get_array_name  (type_array *array) {
  assert(array);
  return ID_TO_STR(array->name);
}
*/

ident *
get_array_ident (type_array *array) {
  assert(array);
  return array->name;
}

/*
void   set_array_name  (type_array *array, char *name);
void   set_array_ident (type_array *array, ident* ident);
*/

inline void
set_array_dimensions (type_array* array, int n) {
  array->n_dimensions = n;
}

inline int
get_array_dimensions (type_array* array) {
  return array->n_dimensions;
}

inline void
set_array_bounds (type_array* array, int dimension, int lower_bound,
		  int upper_bound) {
  array->lower_bound[dimension-1] = lower_bound;
  array->upper_bound[dimension-1] = upper_bound;
}

inline void
set_array_lower_bound (type_array* array, int dimension, int lower_bound) {
  array->lower_bound[dimension-1] = lower_bound;
}

inline void
set_array_upper_bound (type_array* array, int dimension, int upper_bound) {
  array->upper_bound[dimension-1] = upper_bound;
}

inline int
get_array_lower_bound (type_array* array, int dimension) {
  return array->lower_bound[dimension-1];
}

inline int
get_array_upper_bound (type_array* array, int dimension) {
  return array->upper_bound[dimension-1];
}

inline void set_array_element_type (type_array *array, type *type) {
  array->element_type = type;
}

inline type *
get_array_element_type (type_array *array) {
  return array->element_type;
}


/*******************************************************************/
/** TYPE_ENUMERATION                                              **/
/*******************************************************************/

/* create a new type enumeration -- set the enumerators independently */
type_enumeration *
new_type_enumeration (ident *name /* , int n_enums */)
{
  type_enumeration *res;

  res = (type_enumeration *) xmalloc (sizeof (type_enumeration));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
465
  add_irp_type((type *) res);   /* Remember the new type global. */
Christian Schäfer's avatar
Christian Schäfer committed
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  res->kind = k_type_enumeration;
  res->name = name;
  /*
  res->n_enums = n_enums;
  res->enum = (int *) xmalloc (sizeof (int) * n_enums);
  */

  res->visit = 0;

  return res;
}

/* manipulate fields of type_enumeration */
/*
char *
get_enumeration_name  (type_enumeration *enumeration) {
  assert(enumeration);
  return ID_TO_STR(enumeration->name);
}
*/

ident *
get_enumeration_ident (type_enumeration *enumeration) {
  assert(enumeration);
  return enumeration->name;
}

/*
void   set_enumeration_name  (type_enumeration *enumeration, char *name);
void   set_enumeration_ident (type_enumeration *enumeration, ident* ident);
*/
/*
void     set_enumeration_n_enums (type_enumeration *enumeration, int n);
int     *get_enumeration_n_enums (type_enumeration *enumeration);
void     set_enumeration_enum    (type_enumeration *enumeration, int pos,
                                 ir_node const);
ir_node *get_enumeration_enum    (type_enumeration *enumeration, int pos);
*/


/*******************************************************************/
/** TYPE_POINTER                                                  **/
/*******************************************************************/

/* create a new type pointer */
type_pointer *
new_type_pointer (ident *name, type *points_to)
{
  type_pointer *res;

  res = (type_pointer *) xmalloc (sizeof (type_pointer));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
517
  add_irp_type((type *) res);   /* Remember the new type global. */
Christian Schäfer's avatar
Christian Schäfer committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
  res->kind = k_type_pointer;
  res->name = name;
  res->points_to = points_to;

  res->visit = 0;

  return res;
}

/* manipulate fields of type_pointer */
/*
char *
get_pointer_name  (type_pointer *pointer) {
  assert(pointer);
  return ID_TO_STR(pointer->name);
}
*/

ident *
get_pointer_ident (type_pointer *pointer) {
  assert(pointer);
  return pointer->name;
}

/*
void   set_pointer_name  (type_pointer *pointer, char *name);
void   set_pointer_ident (type_pointer *pointer, ident* ident);
*/

inline void
set_pointer_points_to_type (type_pointer *pointer, type* type) {
  pointer->points_to = type;
}

inline type *
get_pointer_points_to_type (type_pointer *pointer) {
  return pointer->points_to;
}


/*******************************************************************/
/** TYPE_PRIMITIVE                                                **/
/*******************************************************************/

/* create a new type_primitive */
inline type_primitive *
new_type_primitive (ident *name, ir_mode *mode)
{
  type_primitive *res;

  res = (type_primitive *) xmalloc (sizeof (type_primitive));
Götz Lindenmaier's avatar
Götz Lindenmaier committed
569
  add_irp_type((type *) res);   /* Remember the new type global. */
Christian Schäfer's avatar
Christian Schäfer committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
  res->kind = k_type_primitive;
  res->name = name;
  res->mode = mode;

  res->visit = 0;

  return res;
}

/* manipulate fields of type_primitive */
/*

char  *
get_primitive_name  (type_primitive *primitive) {
  assert(primitive);
  return ID_TO_STR(primitive->name);
}
*/

ident *
get_primitive_ident (type_primitive *primitive) {
  assert(primitive);
  return primitive->name;
}
/*
void   set_primitive_name  (type_primitive *primitive, char *name);
void   set_primitive_ident (type_primitive *primitive, ident* ident);
*/

inline ir_mode *
get_primitive_mode (type_primitive *primitive) {
  return primitive->mode;
}

inline void
set_primitive_mode (type_primitive *primitive, ir_mode *mode) {
  primitive->mode = mode;
}




/*******************************************************************/
/**  To manage all different types the same                       **/
/*******************************************************************/


int
is_type(void *thing) {
  firm_kind kind;

  kind = get_kind(thing);
  if (   (kind == k_type_class)
      || (kind == k_type_strct)
      || (kind == k_type_method)
      || (kind == k_type_union)
      || (kind == k_type_array)
      || (kind == k_type_enumeration)
      || (kind == k_type_pointer)
      || (kind == k_type_primitive))
    return 1;
  else
    return 0;
}