DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / structures / type.c
1 /********************************************************************
3 type.c - function for handling types in pascal
5 Niksa Orlic, 2004-04-28
7 ********************************************************************/
9 #include "../util/strings.h"
10 #include "../util/error.h"
11 //#include "../util/message.h"
12 #include "type_list.h"
13 #include "string_list.h"
14 #include "type.h"
15 #include "identifier.h"
16 #include "name_table.h"
17 #include "../classgen/bytecode.h"
18 #include "block.h"
19 #include "../util/memory.h"
21 #include <string.h>
22 #include <stdlib.h>
24 extern int linenum;
26 /*
27 Create an empty type
28 */
29 type* type_create()
30 {
31 type *new_type;
32 new_type = (type*) mem_alloc(sizeof(type));
34 if (new_type == NULL)
35 die(1);
37 new_type->type_class = void_type;
39 return new_type;
40 }
43 /*
44 Deletes a type
45 */
46 void type_destroy(type *item)
47 {
48 if (item == NULL)
49 return;
51 switch(item->type_class)
52 {
53 case array_type:
54 {
55 type_list_destroy(item->dimensions_list);
56 type_destroy(item->element_type);
57 break;
58 }
60 case record_type:
61 {
62 type_list_destroy(item->elements_type_list);
63 string_list_destroy(item->elements_name_list);
64 break;
65 }
66 }
68 mem_free(item);
69 }
72 /*
73 Create a copy of a type
74 */
75 type* type_duplicate(type *item)
76 {
77 type *new_item;
78 new_item = (type*) mem_alloc(sizeof(type));
80 if (new_item == NULL)
81 die(1);
83 memcpy(new_item, item, sizeof(type));
85 if (new_item->type_class == array_type)
86 {
87 new_item->dimensions_list = type_list_duplicate(item->dimensions_list);
88 new_item->element_type = type_duplicate(item->element_type);
89 }
91 if (new_item->type_class == record_type)
92 {
93 new_item->elements_type_list = type_list_duplicate(item->elements_type_list);
94 new_item->elements_name_list = string_list_duplicate(item->elements_name_list);
95 new_item->unique_record_ID = item->unique_record_ID;
96 }
98 return new_item;
99 }
102 /*
103 Return a nonzero value if both types are equal.
104 */
105 int type_equal(type *type1, type *type2)
107 if (type1 == type2)
108 return 1;
110 if ((type1 == NULL) || (type2 == NULL))
111 return 0;
113 if (type1->type_class != type2->type_class)
114 return 0;
116 switch (type1->type_class)
118 case array_type:
120 if (!type_equal(type1->element_type, type2->element_type))
121 return 0;
123 if (type_list_different_parameter(type1->dimensions_list,
124 type2->dimensions_list) != 0)
125 return 0;
127 break;
130 case record_type:
132 return (type1->unique_record_ID == type2->unique_record_ID);
134 break;
137 case interval_type:
139 if (type1->interval_base_type != type2->interval_base_type)
140 return 0;
142 if (type1->first_element != type2->first_element)
143 return 0;
145 if (type1->last_element != type2->last_element)
146 return 0;
148 break;
152 return 1;
155 /*
156 Return 0 if types are not equal; 1 if the types
157 are exactly the same, 2 if type1 is real and type2 int
158 or 3 if type2 is real and type1 int
160 Also, char can be casted into string
161 */
162 int type_equal_cast(type *type1, type* type2)
164 if (type_equal(type1, type2))
165 return 1;
167 if ((type1->type_class == real_type)
168 && (type2->type_class == integer_type))
169 return 2;
171 if ((type1->type_class == integer_type)
172 && (type2->type_class == real_type))
173 return 3;
175 if ((type1->type_class == string_type)
176 && (type2->type_class == char_type))
177 return 2;
179 if ((type1->type_class == char_type)
180 && (type2->type_class == string_type))
181 return 3;
183 return 0;
187 /*
188 Returns a name for any basic type, string 'unknown type' otherwise
189 */
190 string *type_get_name(type *basic_type)
192 switch(basic_type->type_class)
194 case integer_type:
195 return string_from_cstr("integer");
197 case real_type:
198 return string_from_cstr("real");
200 case boolean_type:
201 return string_from_cstr("boolean");
203 case char_type:
204 return string_from_cstr("char");
206 case image_type:
207 return string_from_cstr("image");
209 case command_type:
210 return string_from_cstr("command");
212 case stream_type:
213 return string_from_cstr("stream");
215 case string_type:
216 return string_from_cstr("string");
218 case array_type:
219 return string_from_cstr("array");
221 case record_type:
222 return string_from_cstr("record");
224 case record_store_type:
225 return string_from_cstr("recordStore");
227 case http_type:
228 return string_from_cstr("http");
230 case alert_type:
231 return string_from_cstr("alert type");
233 case interval_type:
234 return string_from_cstr("interval");
236 default:
237 return string_from_cstr("unknown type");
240 return NULL;
244 /*
245 Check if any name from element_names already exists in
246 item->elements_name_list
247 */
248 void type_record_check_duplicate_names(type *item, string_list *element_names)
250 string_list *item_list;
252 while (element_names != NULL)
254 if (element_names->data != NULL)
256 item_list = item->elements_name_list;
258 while (item_list != NULL)
260 if (item_list->data != NULL)
262 if (STRING_COMPARE(string_get_cstr(item_list->data),
263 string_get_cstr(element_names->data)) == 0)
265 add_error_message(418, string_get_cstr(item_list->data), "");
269 item_list = item_list->next;
273 element_names = element_names->next;
278 /*
279 Add elements into a record type
280 */
281 void type_add_record(type *item, string_list *element_names, type *element_type)
283 /* check for duplicate names in the names */
284 type_record_check_duplicate_names(item, element_names);
286 while (element_names != NULL)
288 if (element_names->data != NULL)
290 string_list_append(item->elements_name_list, element_names->data);
291 type_list_append(item->elements_type_list, element_type);
294 element_names = element_names->next;
299 /*
300 Returns the type of an element of a given record type identified
301 by a given name. In case of an error, NULL is returned.
302 */
303 type* type_find_record_element(type *record, char *cstr)
305 string_list *names;
306 type_list *types;
308 if (record->type_class != record_type)
309 return NULL;
311 names = record->elements_name_list;
312 types = record->elements_type_list;
314 while (names != NULL)
316 if (names->data != NULL)
318 if(STRING_COMPARE(cstr, string_get_cstr(names->data)) == 0)
320 return type_duplicate(types->data);
324 names = names->next;
325 types = types->next;
328 return NULL;