Changed testprograms to work with new tarval interface
[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 # include <string.h>
11
12 # include "irvrfy.h"
13 # include "irdump.h"
14 # include "firm.h"
15
16 /**
17 ***  This file constructs type information for the following pseudo-program.
18 ***  The procedure code is not constructed.
19 ***
20 ***  interface I {
21 ***    void m1 (void);
22 ***  }
23 ***
24 ***  class C implements I {
25 ***    void m1 (void) {return};
26 ***    void m2 (int)  {return 0};
27 ***  }
28 ***
29 ***  class D {
30 ***    int b;
31 ***  }
32 ***
33 ***  class E extends C, D {
34 ***    void m2 (int) {return 1};
35 ***    int a;
36 ***  }
37 ***
38 **/
39
40 int main(int argc, char **argv)
41 {
42   ident *ii, *ci, *di, *ei, *m1i, *m2i, *inti, *ai, *bi; /* suffix i names identifiers */
43   type  *it, *ct, *dt, *et;                              /*        t names types       */
44   type  *m1t, *m2t;
45   type  *intt;
46   entity *i_m1e, *c_m1e, *c_m2e, *e_m2e, *d_be, *e_ae;   /*        e names entities    */
47
48   printf("\nCreating type information...\n");
49
50   /** init library */
51   init_firm ();
52
53   /** make idents for all used identifiers in the program. */
54   ii  = id_from_str("i",  strlen("i"));
55   ci  = id_from_str("c",  strlen("c"));
56   di  = id_from_str("d",  strlen("d"));
57   ei  = id_from_str("e",  strlen("e"));
58   m1i = id_from_str("m1", strlen("m1"));
59   m2i = id_from_str("m2", strlen("m2"));
60   inti= id_from_str("int",strlen("int"));
61   ai  = id_from_str("a",  strlen("a"));
62   bi  = id_from_str("b",  strlen("b"));
63
64   /** make the type information needed */
65   /* Language defined types */
66   intt = new_type_primitive(inti, mode_Iu);
67   /* Program defined types */
68   it = new_type_class(ii);           /* The fact that this is an interface is
69                                         of no interest.  It's just a class without
70                                         fields and implementations.  But the
71                                         implementation will never be needed. */
72   ct = new_type_class(ci);
73   dt = new_type_class(di);
74   et = new_type_class(ei);
75                                      /* Methods with the same type should use the same
76                                         method type information! */
77   m1t = new_type_method(m1i, 0, 0);  /* 0 parameters, 0 results */
78   m2t = new_type_method(m2i, 1, 0);  /* 1 parameter, 0 results */
79
80   /** add structure to type graph **/
81   /* parameters of methods */
82   set_method_param_type(m2t, 0, intt);
83   /* inheritance. The other direction is added automatically. */
84   add_class_subtype(it, ct);
85   add_class_subtype(ct, et);
86   add_class_subtype(dt, et);
87
88   /** make entities **/
89   i_m1e = new_entity(it, m1i, m1t);
90   c_m1e = new_entity(ct, m1i, m1t);
91   c_m2e = new_entity(ct, m2i, m2t);
92   e_m2e = new_entity(et, m2i, m2t);
93   d_be  = new_entity(dt, bi, intt);
94   e_ae  = new_entity(et, ai, intt);
95
96   /** Add overwirtes relation **/
97   /* How these edges are added depends on the source language. */
98   add_entity_overwrites (c_m1e, i_m1e);
99   add_entity_overwrites (e_m2e, c_m2e);
100
101
102   printf("Done building the graph.  Dumping it.\n");
103   dump_all_types();
104
105   printf("use xvcg to view this graph:\n");
106   printf("/ben/goetz/bin/xvcg GRAPHNAME\n\n");
107
108   return (0);
109 }