DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / structures / type_list.c
1 /********************************************************************
3 type_list.c - function for handling type lists
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 <stdlib.h>
23 /*
24 Create a new empty list
25 */
26 type_list *type_list_create()
27 {
28 type_list *new_list = (type_list*) mem_alloc(sizeof(type_list));
30 if (new_list == NULL)
31 die(1);
33 new_list->data = NULL;
34 new_list->next = NULL;
36 return new_list;
37 }
40 /*
41 Delete the list with all associated data
42 */
43 void type_list_destroy(type_list* item)
44 {
45 type_list *it, *next;
46 it = item;
48 while (it != NULL)
49 {
50 next = it->next;
52 if (it->data != NULL)
53 mem_free(it->data);
55 mem_free (it);
57 it = next;
58 }
60 }
62 /*
63 Creates a copy of the list, the data
64 values are also copied
65 */
66 type_list *type_list_duplicate(type_list *item)
67 {
68 type_list *new_list;
70 if (item == NULL)
71 return NULL;
73 new_list = type_list_create();
75 if ((item == NULL) ||(item->data == NULL))
76 return new_list;
78 do
79 {
80 type_list_append(new_list, item->data);
81 item = item->next;
82 } while (item != NULL);
84 return new_list;
85 }
88 /*
89 Add an element into the list, the data is
90 copied.
91 */
92 void type_list_append(type_list *item, struct type_struct *data)
93 {
94 type_list *new_element;
96 if (item->data == NULL)
97 item->data = type_duplicate(data);
98 else
99 {
100 new_element = (type_list*) mem_alloc(sizeof(type_list));
102 if (new_element == NULL)
103 die(1);
105 new_element->data = type_duplicate(data);
106 new_element->next = NULL;
108 /* move to the end of the list */
109 while (item->next != NULL)
110 item = item->next;
112 item->next = new_element;
117 /*
118 Returns the number of elements in the list
119 */
120 int type_list_length(type_list *item)
122 int counter = 0;
124 if (item->data == NULL)
125 return counter;
127 do
129 item = item->next;
130 counter ++;
131 } while (item != NULL);
133 return counter;
137 /*
138 Returns the index (starting with one) of the first
139 item in the list1 different than the parameetr in the list2,
140 0 if the lists are equal, or -1 if the list1 is shorter than
141 the list2 and all elements in the list1 correspond to the first
142 n elements in the list2.
143 */
144 int type_list_different_parameter(type_list *list1, type_list *list2)
146 int counter = 1;
148 while (list1 != NULL)
150 if ((list2 == NULL)
151 || (list1->data == NULL)
152 || (list2->data == NULL)
153 || (!type_equal(list1->data, list2->data)))
155 if ((list2 != NULL)
156 && (list1->data == NULL)
157 && (list2->data == NULL))
158 return 0;
160 return counter;
163 counter ++;
164 list1 = list1->next;
165 list2 = list2->next;
168 if (list2 != NULL)
169 return -1;
171 return 0;
174 /*
175 Same as the previous function, except that if the list1 element
176 can be casted into list2 element, it is OK !!!
177 */
178 int type_list_different_parameter_cast(type_list *list1, type_list *list2)
180 int counter = 1;
182 while (list1 != NULL)
184 if ((list2 == NULL)
185 || (list1->data == NULL)
186 || (list2->data == NULL)
187 || (type_equal_cast(list1->data, list2->data) == 0)
188 || (type_equal_cast(list1->data, list2->data) == 2))
190 if ((list2 != NULL)
191 && (list1->data == NULL)
192 && (list2->data == NULL))
193 return 0;
195 return counter;
198 counter ++;
199 list1 = list1->next;
200 list2 = list2->next;
203 if (list2 != NULL)
204 return -1;
206 return 0;
209 /*
210 Same as the above, but compares interval base types against the types;
211 this is used when checking array dimensions
212 */
213 int type_list_different_parameter_array(type_list *real_types, type_list *interval_types)
215 int counter = 1;
217 while (real_types != NULL)
219 if ((interval_types == NULL)
220 || (real_types->data == NULL)
221 || (interval_types->data == NULL)
222 || (real_types->data->type_class != interval_types->data->interval_base_type))
224 return counter;
227 counter ++;
228 real_types = real_types->next;
229 interval_types = interval_types->next;
232 if (interval_types != NULL)
233 return -1;
235 return 0;