1 /*
2  * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19 
20 /**
21  * @file
22  * @brief   Opcode of types.
23  * @author  Goetz Lindenmaier, Michael Beck
24  */
25 #include "config.h"
26 
27 #include "xmalloc.h"
28 #include "tpop_t.h"
29 #include "type_t.h"
30 #include "ident.h"
31 
get_tpop_class(void)32 const tp_op *type_class;         const tp_op *get_tpop_class      (void) { return type_class;       }
get_tpop_struct(void)33 const tp_op *type_struct;        const tp_op *get_tpop_struct     (void) { return type_struct;      }
get_tpop_method(void)34 const tp_op *type_method;        const tp_op *get_tpop_method     (void) { return type_method;      }
get_tpop_union(void)35 const tp_op *type_union;         const tp_op *get_tpop_union      (void) { return type_union;       }
get_tpop_array(void)36 const tp_op *type_array;         const tp_op *get_tpop_array      (void) { return type_array;       }
get_tpop_enumeration(void)37 const tp_op *type_enumeration;   const tp_op *get_tpop_enumeration(void) { return type_enumeration; }
get_tpop_pointer(void)38 const tp_op *type_pointer;       const tp_op *get_tpop_pointer    (void) { return type_pointer;     }
get_tpop_primitive(void)39 const tp_op *type_primitive;     const tp_op *get_tpop_primitive  (void) { return type_primitive;   }
get_tpop_code_type(void)40 const tp_op *tpop_code;          const tp_op *get_tpop_code_type  (void) { return tpop_code;        }
get_tpop_none(void)41 const tp_op *tpop_none;          const tp_op *get_tpop_none       (void) { return tpop_none;        }
get_tpop_unknown(void)42 const tp_op *tpop_unknown;       const tp_op *get_tpop_unknown    (void) { return tpop_unknown;     }
43 
44 const tp_op *
new_tpop(tp_opcode code,ident * name,unsigned flags,size_t attr_size,const tp_op_ops * ops)45 new_tpop(tp_opcode code, ident *name, unsigned flags, size_t attr_size, const tp_op_ops *ops)
46 {
47 	tp_op *res = XMALLOC(tp_op);
48 	res->code          = code;
49 	res->name          = name;
50 	res->flags         = flags;
51 	res->attr_size     = attr_size;
52 
53 	if (ops)
54 		res->ops = *ops;
55 	else
56 		memset(&res->ops, 0, sizeof(res->ops));
57 
58 	return res;
59 }
60 
free_tpop(const tp_op * tpop)61 void free_tpop(const tp_op *tpop)
62 {
63 	xfree((void *)tpop);
64 }
65 
66 static const tp_op_ops
67 	/** tpop operations for class types */
68 	class_ops = {
69 		free_class_attrs,
70 		free_class_entities,
71 		NULL,
72 		set_class_mode,
73 		set_class_size,
74 		get_class_n_members,
75 		get_class_member,
76 		get_class_member_index
77 	},
78 	/** tpop operations for struct types */
79 	struct_ops = {
80 		free_struct_attrs,
81 		free_struct_entities,
82 		NULL,
83 		set_struct_mode,
84 		set_struct_size,
85 		get_struct_n_members,
86 		get_struct_member,
87 		get_struct_member_index
88 	},
89 	/** tpop operations for method types */
90 	method_ops = {
91 		free_method_attrs,
92 		free_method_entities,
93 		NULL,
94 		NULL,
95 		NULL,
96 		NULL,
97 		NULL,
98 		NULL
99 		},
100 	/** tpop operations for union types */
101 	union_ops = {
102 		free_union_attrs,
103 		free_union_entities,
104 		NULL,
105 		NULL,
106 		set_union_size,
107 		get_union_n_members,
108 		get_union_member,
109 		get_union_member_index
110 	},
111 	/** tpop operations for array types */
112 	array_ops = {
113 		free_array_attrs,
114 		free_array_entities,
115 		free_array_automatic_entities,
116 		NULL,
117 		set_array_size,
118 		NULL,
119 		NULL,
120 		NULL
121 	},
122 	/** tpop operations for enumeration types */
123 	enum_ops = {
124 		free_enumeration_attrs,
125 		free_enumeration_entities,
126 		NULL,
127 		set_enumeration_mode,
128 		NULL,
129 		NULL,
130 		NULL,
131 		NULL
132 	},
133 	/** tpop operations for pointer types */
134 	pointer_ops = {
135 		free_pointer_attrs,
136 		free_pointer_entities,
137 		NULL,
138 		set_pointer_mode,
139 		NULL,
140 		NULL,
141 		NULL,
142 		NULL
143 	},
144 	/** tpop operations for pseudo types */
145 	pseudo_ops = {
146 		NULL,
147 		NULL,
148 		NULL,
149 		NULL,
150 		set_default_size,
151 		NULL,
152 		NULL,
153 		NULL
154 	},
155 	/** tpop operations for primitive types */
156 	null_ops = {
157 		NULL,
158 		NULL,
159 		NULL,
160 		NULL,
161 		set_default_size,
162 		NULL,
163 		NULL,
164 		NULL
165 	};
166 
init_tpop(void)167 void init_tpop(void)
168 {
169 #define ID(s) new_id_from_chars(s, sizeof(s) - 1)
170 	type_class       = new_tpop(tpo_class      , ID("class"),       TP_OP_FLAG_COMPOUND, sizeof(cls_attr), &class_ops);
171 	type_struct      = new_tpop(tpo_struct     , ID("struct"),      TP_OP_FLAG_COMPOUND, sizeof(stc_attr), &struct_ops);
172 	type_method      = new_tpop(tpo_method     , ID("method"),      0,                   sizeof(mtd_attr), &method_ops);
173 	type_union       = new_tpop(tpo_union      , ID("union"),       TP_OP_FLAG_COMPOUND, sizeof(uni_attr), &union_ops);
174 	type_array       = new_tpop(tpo_array      , ID("array"),       0,                   sizeof(arr_attr), &array_ops);
175 	type_enumeration = new_tpop(tpo_enumeration, ID("enumeration"), 0,                   sizeof(enm_attr), &enum_ops);
176 	type_pointer     = new_tpop(tpo_pointer    , ID("pointer"),     0,                   sizeof(ptr_attr), &pointer_ops);
177 	type_primitive   = new_tpop(tpo_primitive  , ID("primitive"),   0,                   sizeof(pri_attr), &null_ops);
178 	tpop_code        = new_tpop(tpo_code       , ID("code"),        0,                   0,                &null_ops);
179 	tpop_none        = new_tpop(tpo_none       , ID("None"),        0,                   0,                &pseudo_ops);
180 	tpop_unknown     = new_tpop(tpo_unknown    , ID("Unknown"),     0,                   0,                &pseudo_ops);
181 #undef ID
182 }
183 
finish_tpop(void)184 void finish_tpop(void)
185 {
186 	free_tpop(type_class      ); type_class       = NULL;
187 	free_tpop(type_struct     ); type_struct      = NULL;
188 	free_tpop(type_method     ); type_method      = NULL;
189 	free_tpop(type_union      ); type_union       = NULL;
190 	free_tpop(type_array      ); type_array       = NULL;
191 	free_tpop(type_enumeration); type_enumeration = NULL;
192 	free_tpop(type_pointer    ); type_pointer     = NULL;
193 	free_tpop(type_primitive  ); type_primitive   = NULL;
194 	free_tpop(tpop_code       ); tpop_code        = NULL;
195 	free_tpop(tpop_none       ); tpop_none        = NULL;
196 	free_tpop(tpop_unknown    ); tpop_unknown     = NULL;
197 }
198 
get_tpop_name(const tp_op * op)199 const char *get_tpop_name(const tp_op *op)
200 {
201 	return get_id_str(op->name);
202 }
203 
tp_opcode(get_tpop_code)204 tp_opcode (get_tpop_code)(const tp_op *op)
205 {
206 	return _get_tpop_code(op);
207 }
208 
size_t(get_tpop_attr_size)209 size_t (get_tpop_attr_size)(const tp_op *op)
210 {
211 	return _get_tpop_attr_size(op);
212 }
213