*** empty log message ***
[libfirm] / testprograms / inheritance_example.c
1 /* Copyright (C) 2000 by Universitaet Karlsruhe
2 ** All rights reserved.
3 **
4 ** Author: Goetz Lindenmaier
5 **
6 ** testprogram.
7 */
8
9 #include <stdio.h>
10
11 # include "irdump.h"
12 # include "firm.h"
13
14 /**
15 ***  This file constructs type information for the following pseudo-program:
16 ***
17 ***  interface I {
18 ***    void m1 (void);
19 ***  }
20 ***
21 ***  class C implements I {
22 ***    void m1 (void) {return};
23 ***    void m2 (int)  {return 0};
24 ***  }
25 ***
26 ***  class D {
27 ***    int a;
28 ***  }
29 ***
30 ***  class E extends C, D {
31 ***    void m2 (int) {return 1};
32 ***    int a;
33 ***  }
34 ***
35 **/
36
37 int main(int argc, char **argv)
38 {
39   ident *ii, *ci, *di, *ei, *m1i, *m2i, *inti, *ai; /* suffix i names identifiers */
40   type_class  *it, *ct, *dt, *et;                   /*        t names types       */
41   type_method *m1t, *m2t;
42   type_primitive *intt;
43   entity *c_m1e, *c_m2e, *e_m2e, *d_ae, *e_ae;        /*        e names entities    */
44
45   ir_node *x;
46
47   printf("\nCreating type information...\n");
48
49   /** init library */
50   init_firm ();
51
52   /** make idents for all used identifiers in the program. */
53   ii  = id_from_str("i",  strlen("i"));
54   ci  = id_from_str("c",  strlen("c"));
55   di  = id_from_str("d",  strlen("d"));
56   ei  = id_from_str("e",  strlen("e"));
57   m1i = id_from_str("m1", strlen("m1"));
58   m2i = id_from_str("m2", strlen("m2"));
59   inti= id_from_str("int",strlen("int"));
60   ai  = id_from_str("a",  strlen("a"));
61
62   /** make the type information needed */
63   /* Language defined types */
64   intt = new_type_primitive(inti, mode_I);
65   /* Program defined types */
66   it = new_type_class(ii);
67   ct = new_type_class(ci);
68   dt = new_type_class(di);
69   et = new_type_class(ei);
70   m1t = new_type_method(m1i, 0, 0);  /* 0 parameters, 0 results */
71   m2t = new_type_method(m2i, 1, 0);  /* 1 parameter, 0 results */
72
73   /** add structure to type graph **/
74   /* parameters of methods */
75   set_method_param_type(m2t, 0, (type *)intt);
76   /* inheritance */
77   add_class_subtype(it, ct);
78   add_class_subtype(ct, et);
79   add_class_subtype(dt, et);
80   add_class_supertype(ct, it);
81   add_class_supertype(et, ct);
82   add_class_supertype(et, dt);
83
84   /** make entities **/
85   c_m1e = new_entity((type *)ct, m1i, (type *)m1t);
86   c_m2e = new_entity((type *)ct, m2i, (type *)m2t);
87   e_m2e = new_entity((type *)et, m2i, (type *)m2t);
88   d_ae = new_entity((type *)dt, ai, (type *)intt);
89   e_ae = new_entity((type *)et, ai, (type *)intt);
90
91   printf("Done building the graph.  Dumping it.\n");
92   dump_all_types();
93
94   printf("use xvcg to view this graph:\n");
95   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
96
97   return (0);
98 }