becopyopt: Remove the unnecessary attribute name from struct copy_opt_t.
[libfirm] / ir / tr / tpop.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Opcode of types.
9  * @author  Goetz Lindenmaier, Michael Beck
10  */
11 #include "config.h"
12
13 #include "xmalloc.h"
14 #include "tpop_t.h"
15 #include "type_t.h"
16 #include "ident.h"
17
18 const tp_op *type_class;         const tp_op *get_tpop_class      (void) { return type_class;       }
19 const tp_op *type_struct;        const tp_op *get_tpop_struct     (void) { return type_struct;      }
20 const tp_op *type_method;        const tp_op *get_tpop_method     (void) { return type_method;      }
21 const tp_op *type_union;         const tp_op *get_tpop_union      (void) { return type_union;       }
22 const tp_op *type_array;         const tp_op *get_tpop_array      (void) { return type_array;       }
23 const tp_op *type_enumeration;   const tp_op *get_tpop_enumeration(void) { return type_enumeration; }
24 const tp_op *type_pointer;       const tp_op *get_tpop_pointer    (void) { return type_pointer;     }
25 const tp_op *type_primitive;     const tp_op *get_tpop_primitive  (void) { return type_primitive;   }
26 const tp_op *tpop_code;          const tp_op *get_tpop_code_type  (void) { return tpop_code;        }
27 const tp_op *tpop_none;          const tp_op *get_tpop_none       (void) { return tpop_none;        }
28 const tp_op *tpop_unknown;       const tp_op *get_tpop_unknown    (void) { return tpop_unknown;     }
29
30 const tp_op *
31 new_tpop(tp_opcode code, ident *name, unsigned flags, size_t attr_size, const tp_op_ops *ops)
32 {
33         tp_op *res = XMALLOC(tp_op);
34         res->code          = code;
35         res->name          = name;
36         res->flags         = flags;
37         res->attr_size     = attr_size;
38
39         if (ops)
40                 res->ops = *ops;
41         else
42                 memset(&res->ops, 0, sizeof(res->ops));
43
44         return res;
45 }
46
47 void free_tpop(const tp_op *tpop)
48 {
49         xfree((void *)tpop);
50 }
51
52 static const tp_op_ops
53         /** tpop operations for class types */
54         class_ops = {
55                 free_class_attrs,
56                 free_class_entities,
57                 NULL,
58                 set_class_mode,
59                 set_class_size,
60                 get_class_n_members,
61                 get_class_member,
62                 get_class_member_index
63         },
64         /** tpop operations for struct types */
65         struct_ops = {
66                 free_struct_attrs,
67                 free_struct_entities,
68                 NULL,
69                 set_struct_mode,
70                 set_struct_size,
71                 get_struct_n_members,
72                 get_struct_member,
73                 get_struct_member_index
74         },
75         /** tpop operations for method types */
76         method_ops = {
77                 free_method_attrs,
78                 free_method_entities,
79                 NULL,
80                 NULL,
81                 NULL,
82                 NULL,
83                 NULL,
84                 NULL
85                 },
86         /** tpop operations for union types */
87         union_ops = {
88                 free_union_attrs,
89                 free_union_entities,
90                 NULL,
91                 NULL,
92                 set_union_size,
93                 get_union_n_members,
94                 get_union_member,
95                 get_union_member_index
96         },
97         /** tpop operations for array types */
98         array_ops = {
99                 free_array_attrs,
100                 free_array_entities,
101                 free_array_automatic_entities,
102                 NULL,
103                 set_array_size,
104                 NULL,
105                 NULL,
106                 NULL
107         },
108         /** tpop operations for enumeration types */
109         enum_ops = {
110                 free_enumeration_attrs,
111                 free_enumeration_entities,
112                 NULL,
113                 set_enumeration_mode,
114                 NULL,
115                 NULL,
116                 NULL,
117                 NULL
118         },
119         /** tpop operations for pointer types */
120         pointer_ops = {
121                 free_pointer_attrs,
122                 free_pointer_entities,
123                 NULL,
124                 set_pointer_mode,
125                 NULL,
126                 NULL,
127                 NULL,
128                 NULL
129         },
130         /** tpop operations for pseudo types */
131         pseudo_ops = {
132                 NULL,
133                 NULL,
134                 NULL,
135                 NULL,
136                 set_default_size,
137                 NULL,
138                 NULL,
139                 NULL
140         },
141         /** tpop operations for primitive types */
142         null_ops = {
143                 NULL,
144                 NULL,
145                 NULL,
146                 NULL,
147                 set_default_size,
148                 NULL,
149                 NULL,
150                 NULL
151         };
152
153 void init_tpop(void)
154 {
155 #define ID(s) new_id_from_chars(s, sizeof(s) - 1)
156         type_class       = new_tpop(tpo_class      , ID("class"),       TP_OP_FLAG_COMPOUND, sizeof(cls_attr), &class_ops);
157         type_struct      = new_tpop(tpo_struct     , ID("struct"),      TP_OP_FLAG_COMPOUND, sizeof(stc_attr), &struct_ops);
158         type_method      = new_tpop(tpo_method     , ID("method"),      0,                   sizeof(mtd_attr), &method_ops);
159         type_union       = new_tpop(tpo_union      , ID("union"),       TP_OP_FLAG_COMPOUND, sizeof(uni_attr), &union_ops);
160         type_array       = new_tpop(tpo_array      , ID("array"),       0,                   sizeof(arr_attr), &array_ops);
161         type_enumeration = new_tpop(tpo_enumeration, ID("enumeration"), 0,                   sizeof(enm_attr), &enum_ops);
162         type_pointer     = new_tpop(tpo_pointer    , ID("pointer"),     0,                   sizeof(ptr_attr), &pointer_ops);
163         type_primitive   = new_tpop(tpo_primitive  , ID("primitive"),   0,                   sizeof(pri_attr), &null_ops);
164         tpop_code        = new_tpop(tpo_code       , ID("code"),        0,                   0,                &null_ops);
165         tpop_none        = new_tpop(tpo_none       , ID("None"),        0,                   0,                &pseudo_ops);
166         tpop_unknown     = new_tpop(tpo_unknown    , ID("Unknown"),     0,                   0,                &pseudo_ops);
167 #undef ID
168 }
169
170 void finish_tpop(void)
171 {
172         free_tpop(type_class      ); type_class       = NULL;
173         free_tpop(type_struct     ); type_struct      = NULL;
174         free_tpop(type_method     ); type_method      = NULL;
175         free_tpop(type_union      ); type_union       = NULL;
176         free_tpop(type_array      ); type_array       = NULL;
177         free_tpop(type_enumeration); type_enumeration = NULL;
178         free_tpop(type_pointer    ); type_pointer     = NULL;
179         free_tpop(type_primitive  ); type_primitive   = NULL;
180         free_tpop(tpop_code       ); tpop_code        = NULL;
181         free_tpop(tpop_none       ); tpop_none        = NULL;
182         free_tpop(tpop_unknown    ); tpop_unknown     = NULL;
183 }
184
185 const char *get_tpop_name(const tp_op *op)
186 {
187         return get_id_str(op->name);
188 }
189
190 tp_opcode (get_tpop_code)(const tp_op *op)
191 {
192         return _get_tpop_code(op);
193 }