1 |
/* |
2 |
* Ascend Instance Tree Killing |
3 |
* by Tom Epperly & Ben Allan |
4 |
* 9/3/89 |
5 |
* Version: $Revision: 1.12 $ |
6 |
* Version control file: $RCSfile: destroyinst.c,v $ |
7 |
* Date last modified: $Date: 1997/07/18 12:28:50 $ |
8 |
* Last modified by: $Author: mthomas $ |
9 |
* |
10 |
* This file is part of the Ascend Language Interpreter. |
11 |
* |
12 |
* Copyright (C) 1996 Ben Allan |
13 |
* based on instance.c |
14 |
* Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly |
15 |
* |
16 |
* The Ascend Language Interpreter is free software; you can redistribute |
17 |
* it and/or modify it under the terms of the GNU General Public License as |
18 |
* published by the Free Software Foundation; either version 2 of the |
19 |
* License, or (at your option) any later version. |
20 |
* |
21 |
* The Ascend Language Interpreter is distributed in hope that it will be |
22 |
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
24 |
* General Public License for more details. |
25 |
* |
26 |
* You should have received a copy of the GNU General Public License |
27 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28 |
*/ |
29 |
#include <stdarg.h> |
30 |
#include <ascend/general/platform.h> |
31 |
#include <ascend/general/panic.h> |
32 |
#include <ascend/general/ascMalloc.h> |
33 |
#include <ascend/general/pool.h> |
34 |
#include <ascend/general/list.h> |
35 |
#include <ascend/general/dstring.h> |
36 |
|
37 |
#include "symtab.h" |
38 |
#include "functype.h" |
39 |
#include "expr_types.h" |
40 |
#include "instance_name.h" |
41 |
#include "instance_io.h" |
42 |
#include "child.h" |
43 |
#include "type_desc.h" |
44 |
#include "instmacro.h" |
45 |
#include "instquery.h" |
46 |
#include "instance_types.h" |
47 |
#include "setinstval.h" |
48 |
#include "linkinst.h" |
49 |
#include "destroyinst.h" |
50 |
#include "arrayinst.h" |
51 |
#include "parentchild.h" |
52 |
#include "atomvalue.h" |
53 |
#include "atomsize.h" |
54 |
#include "check.h" |
55 |
#include "dump.h" |
56 |
#include "find.h" |
57 |
#include "type_desc.h" |
58 |
#include "prototype.h" |
59 |
#include "pending.h" |
60 |
#include "rel_blackbox.h" |
61 |
#include "vlist.h" |
62 |
#include "relation.h" |
63 |
#include "logical_relation.h" |
64 |
#include "logrelation.h" |
65 |
#include "relation_util.h" |
66 |
#include "logrel_util.h" |
67 |
#include "rel_common.h" |
68 |
#include "case.h" |
69 |
#include "when_util.h" |
70 |
#include "universal.h" |
71 |
#include "instance_types.h" |
72 |
#include "cmpfunc.h" |
73 |
#include "slvreq.h" |
74 |
|
75 |
|
76 |
static void DeleteIPtr(struct Instance *i){ |
77 |
assert(i&&InterfacePtrDelete); |
78 |
AssertMemory(i); |
79 |
switch(i->t) { |
80 |
case SIM_INST: |
81 |
slvreq_sim_destroy_hooks(i); |
82 |
(*InterfacePtrDelete)(i,SIM_INST(i)->interface_ptr); |
83 |
SIM_INST(i)->interface_ptr = NULL; |
84 |
return; |
85 |
case MODEL_INST: |
86 |
(*InterfacePtrDelete)(i,MOD_INST(i)->interface_ptr); |
87 |
MOD_INST(i)->interface_ptr = NULL; |
88 |
break; |
89 |
case REAL_ATOM_INST: |
90 |
(*InterfacePtrDelete)(i,RA_INST(i)->interface_ptr); |
91 |
RA_INST(i)->interface_ptr = NULL; |
92 |
break; |
93 |
case BOOLEAN_ATOM_INST: |
94 |
(*InterfacePtrDelete)(i,BA_INST(i)->interface_ptr); |
95 |
BA_INST(i)->interface_ptr = NULL; |
96 |
break; |
97 |
case INTEGER_ATOM_INST: |
98 |
(*InterfacePtrDelete)(i,IA_INST(i)->interface_ptr); |
99 |
IA_INST(i)->interface_ptr = NULL; |
100 |
break; |
101 |
case SET_ATOM_INST: |
102 |
(*InterfacePtrDelete)(i,SA_INST(i)->interface_ptr); |
103 |
SA_INST(i)->interface_ptr = NULL; |
104 |
break; |
105 |
case SYMBOL_ATOM_INST: |
106 |
(*InterfacePtrDelete)(i,SYMA_INST(i)->interface_ptr); |
107 |
SYMA_INST(i)->interface_ptr = NULL; |
108 |
break; |
109 |
case REL_INST: |
110 |
(*InterfacePtrDelete)(i,RELN_INST(i)->interface_ptr); |
111 |
RELN_INST(i)->interface_ptr = NULL; |
112 |
break; |
113 |
case LREL_INST: |
114 |
(*InterfacePtrDelete)(i,LRELN_INST(i)->interface_ptr); |
115 |
LRELN_INST(i)->interface_ptr = NULL; |
116 |
break; |
117 |
case WHEN_INST: |
118 |
(*InterfacePtrDelete)(i,W_INST(i)->interface_ptr); |
119 |
W_INST(i)->interface_ptr = NULL; |
120 |
break; |
121 |
/* constants */ |
122 |
case INTEGER_CONSTANT_INST: |
123 |
(*InterfacePtrDelete)(i,IC_INST(i)->interface_ptr); |
124 |
IC_INST(i)->interface_ptr = NULL; |
125 |
break; |
126 |
case BOOLEAN_CONSTANT_INST: |
127 |
(*InterfacePtrDelete)(i,BC_INST(i)->interface_ptr); |
128 |
BC_INST(i)->interface_ptr = NULL; |
129 |
break; |
130 |
case SYMBOL_CONSTANT_INST: |
131 |
(*InterfacePtrDelete)(i,SYMC_INST(i)->interface_ptr); |
132 |
SYMC_INST(i)->interface_ptr = NULL; |
133 |
break; |
134 |
case DUMMY_INST: |
135 |
(*InterfacePtrDelete)(i,D_INST(i)->interface_ptr); |
136 |
D_INST(i)->interface_ptr = NULL; |
137 |
break; |
138 |
case REAL_CONSTANT_INST: /* fall through */ |
139 |
/* arrays */ |
140 |
case ARRAY_INT_INST: /* fall through */ |
141 |
case ARRAY_ENUM_INST: /* fall through */ |
142 |
/* fundies */ |
143 |
case REAL_INST: /* fall through */ |
144 |
case INTEGER_INST: /* fall through */ |
145 |
case BOOLEAN_INST: /* fall through */ |
146 |
case SET_INST: /* fall through */ |
147 |
case SYMBOL_INST: /* fall through */ |
148 |
break; |
149 |
default: |
150 |
ASC_PANIC("Unknown instance type passed to DeleteIPtr.\n"); |
151 |
} |
152 |
} |
153 |
|
154 |
/** |
155 |
This never returns anything but 1 for DUMMY_INSTs. |
156 |
@return true value if inst should be deleted; otherwise, return 0. |
157 |
*/ |
158 |
static int RemoveParentReferences( |
159 |
struct Instance *inst, struct Instance *parent |
160 |
){ |
161 |
register unsigned long c,pos,length; |
162 |
AssertMemory(inst); |
163 |
if(parent!=NULL){ |
164 |
AssertMemory(parent); |
165 |
/* destroy link from inst to parent */ |
166 |
pos = SearchForParent(inst,parent); |
167 |
if(pos != 0 || inst->t == DUMMY_INST){ |
168 |
/* Because the dummy always 'adds' a parent, it must always delete it to |
169 |
keep the ref_count happy. Dummy knows of no parents, but knows exactly |
170 |
how many it doesn't have. */ |
171 |
DeleteParent(inst,pos); |
172 |
} |
173 |
/* destroy link(s) from parent to inst */ |
174 |
while(0 != (pos = ChildIndex(parent,inst))){ |
175 |
StoreChildPtr(parent,pos,NULL); |
176 |
} |
177 |
return (NumberParents(inst) == 0); |
178 |
}else{ |
179 |
length = NumberParents(inst); |
180 |
if(inst->t == DUMMY_INST){ |
181 |
FPRINTF(ASCERR, |
182 |
"The global dummy instance cannot be destroyed w/out parental consent\n" |
183 |
); |
184 |
FPRINTF(ASCERR, "You should not be seeing this message.\n"); |
185 |
return 0; |
186 |
} |
187 |
for(c=1;c<=length;c++) { |
188 |
parent = InstanceParent(inst,c); |
189 |
while(0 != (pos = ChildIndex(parent,inst))){ |
190 |
StoreChildPtr(parent,pos,NULL); |
191 |
} |
192 |
} |
193 |
return 1; |
194 |
} |
195 |
} |
196 |
|
197 |
static void RemoveFromClique(struct Instance *inst){ |
198 |
register struct Instance *i,*hold; |
199 |
AssertMemory(inst); |
200 |
if ((hold=i=NextCliqueMember(inst))==inst) |
201 |
return; |
202 |
while (NextCliqueMember(i)!=inst) |
203 |
i = NextCliqueMember(i); |
204 |
switch(i->t) { |
205 |
case MODEL_INST: |
206 |
MOD_INST(i)->alike_ptr = hold; |
207 |
break; |
208 |
case REAL_CONSTANT_INST: |
209 |
RC_INST(i)->alike_ptr = hold; |
210 |
break; |
211 |
case BOOLEAN_CONSTANT_INST: |
212 |
BC_INST(i)->alike_ptr = hold; |
213 |
break; |
214 |
case INTEGER_CONSTANT_INST: |
215 |
IC_INST(i)->alike_ptr = hold; |
216 |
break; |
217 |
case SYMBOL_CONSTANT_INST: |
218 |
SYMC_INST(i)->alike_ptr = hold; |
219 |
break; |
220 |
case REAL_ATOM_INST: |
221 |
RA_INST(i)->alike_ptr = hold; |
222 |
break; |
223 |
case BOOLEAN_ATOM_INST: |
224 |
BA_INST(i)->alike_ptr = hold; |
225 |
break; |
226 |
case INTEGER_ATOM_INST: |
227 |
IA_INST(i)->alike_ptr = hold; |
228 |
break; |
229 |
case SET_ATOM_INST: |
230 |
SA_INST(i)->alike_ptr = hold; |
231 |
break; |
232 |
case SYMBOL_ATOM_INST: |
233 |
SYMA_INST(i)->alike_ptr = hold; |
234 |
break; |
235 |
default: |
236 |
Asc_Panic(2, "RemoveFromClique", |
237 |
"Invalid instance in clique found in RemoveFromClique.\n"); |
238 |
} |
239 |
} |
240 |
|
241 |
static void DeleteArrayChild(struct ArrayChild *acp, struct Instance *parent){ |
242 |
if ((acp!=NULL)&&(acp->inst!=NULL)) { |
243 |
AssertContainedMemory(acp,sizeof(struct ArrayChild)); |
244 |
DestroyInstance(acp->inst,parent); |
245 |
} |
246 |
} |
247 |
|
248 |
/** |
249 |
Take an ATOM and tell all the relations that know about it to |
250 |
forget it, then destroy the list that identifies those relations. |
251 |
After this, a real ATOM can be safely deleted if there are no models |
252 |
refering to it. |
253 |
*/ |
254 |
static void RemoveRelationLinks(struct Instance *i){ |
255 |
struct gl_list_t *list = RA_INST(i)->relations; |
256 |
if(NULL==list)return; |
257 |
register unsigned long c,length; |
258 |
assert(list!=NULL); |
259 |
length = gl_length(list); |
260 |
for(c=1;c<=length;c++){ |
261 |
//CONSOLE_DEBUG("Var %p: referenced in relation %p",i, INST(gl_fetch(list,c))); |
262 |
ChangeRelationPointers(INST(gl_fetch(list,c)),i,INST(NULL)); |
263 |
} |
264 |
gl_destroy(list); |
265 |
RA_INST(i)->relations = NULL; |
266 |
} |
267 |
|
268 |
|
269 |
/** |
270 |
Take a BOOLEAN ATOM, REL or LOGREL and tell all the logrelations that |
271 |
know about them to forget them, then destroy the list that identifies |
272 |
those logrelations. |
273 |
After this, the ATOM, REL or LOGREL can be deleted if there are no |
274 |
models or whens refering to it. |
275 |
*/ |
276 |
static |
277 |
void RemoveLogRelLinks(struct Instance *i, struct gl_list_t *list) |
278 |
{ |
279 |
register unsigned long c,length; |
280 |
assert(list!=NULL); |
281 |
length = gl_length(list); |
282 |
for(c=1;c<=length;c++) { |
283 |
ChangeLogRelPointers(INST(gl_fetch(list,c)),i,INST(NULL)); |
284 |
} |
285 |
gl_destroy(list); |
286 |
} |
287 |
|
288 |
|
289 |
static |
290 |
void RemoveWhenLinks(struct Instance *i, struct gl_list_t *list) |
291 |
{ |
292 |
register unsigned long c,length; |
293 |
assert(list!=NULL); |
294 |
length = gl_length(list); |
295 |
for(c=1;c<=length;c++) { |
296 |
ChangeWhenPointers(INST(gl_fetch(list,c)),i,INST(NULL)); |
297 |
} |
298 |
gl_destroy(list); |
299 |
} |
300 |
|
301 |
static void DestroyAtomChildren( |
302 |
register struct Instance **i, register unsigned long int nc |
303 |
){ |
304 |
while(nc-- > 0){ |
305 |
AssertMemory(i); |
306 |
AssertMemory(*i); |
307 |
if ((*i)->t==SET_INST) { |
308 |
if (S_INST(*i)->list!=NULL) { |
309 |
DestroySet(S_INST(*i)->list); |
310 |
} |
311 |
} |
312 |
i++; /* position pointer for next child */ |
313 |
} |
314 |
} |
315 |
|
316 |
/* |
317 |
should only be called when there areno more references |
318 |
to the object. |
319 |
*/ |
320 |
static void DestroyInstanceParts(struct Instance *i){ |
321 |
register unsigned long c,length; |
322 |
register struct gl_list_t *l; |
323 |
struct Instance *child; |
324 |
AssertMemory(i); |
325 |
switch(i->t) { |
326 |
case SIM_INST: |
327 |
child = InstanceChild(i,1); /* one child only */ |
328 |
DestroyInstance(child,i); |
329 |
SIM_INST(i)->name = NULL; /* main symbol table owns the string */ |
330 |
SIM_INST(i)->extvars = NULL; |
331 |
i->t = ERROR_INST; |
332 |
DeleteTypeDesc(SIM_INST(i)->desc); |
333 |
SIM_INST(i)->desc = NULL; |
334 |
ascfree((char *)i); |
335 |
return; |
336 |
case MODEL_INST: |
337 |
gl_destroy(MOD_INST(i)->parents); |
338 |
MOD_INST(i)->parents = NULL; |
339 |
MOD_INST(i)->alike_ptr = NULL; |
340 |
if (MOD_INST(i)->whens!=NULL) { |
341 |
RemoveWhenLinks(i,MOD_INST(i)->whens); |
342 |
MOD_INST(i)->whens=NULL; |
343 |
} |
344 |
DestroyBList(MOD_INST(i)->executed); |
345 |
MOD_INST(i)->executed = NULL; |
346 |
/* destroy reference to children */ |
347 |
length = NumberChildren(i); |
348 |
for(c=1;c<=length;c++) { |
349 |
child = InstanceChild(i,c); |
350 |
DestroyInstance(child,i); |
351 |
} |
352 |
i->t = ERROR_INST; |
353 |
DeleteTypeDesc(MOD_INST(i)->desc); |
354 |
MOD_INST(i)->desc = NULL; |
355 |
gl_destroy(MOD_INST(i)->link_table); |
356 |
ascfree((char *)i); |
357 |
return; |
358 |
case REAL_CONSTANT_INST: |
359 |
/* continue delete the atom */ |
360 |
gl_destroy(RC_INST(i)->parents); |
361 |
RC_INST(i)->parents = NULL; |
362 |
DeleteTypeDesc(RC_INST(i)->desc); |
363 |
RC_INST(i)->desc = NULL; |
364 |
RC_INST(i)->alike_ptr = NULL; |
365 |
/* children are automatically deleted by the following */ |
366 |
i->t = ERROR_INST; |
367 |
ascfree((char *)i); |
368 |
return; |
369 |
case BOOLEAN_CONSTANT_INST: |
370 |
gl_destroy(BC_INST(i)->parents); |
371 |
BC_INST(i)->parents = NULL; |
372 |
DeleteTypeDesc(BC_INST(i)->desc); |
373 |
BC_INST(i)->desc = NULL; |
374 |
BC_INST(i)->alike_ptr = NULL; |
375 |
if (BC_INST(i)->whens!=NULL) { |
376 |
RemoveWhenLinks(i,BC_INST(i)->whens); |
377 |
BC_INST(i)->whens=NULL; |
378 |
} |
379 |
i->t = ERROR_INST; |
380 |
ascfree((char *)i); |
381 |
return; |
382 |
case INTEGER_CONSTANT_INST: |
383 |
gl_destroy(IC_INST(i)->parents); |
384 |
IC_INST(i)->parents = NULL; |
385 |
DeleteTypeDesc(IC_INST(i)->desc); |
386 |
IC_INST(i)->desc = NULL; |
387 |
IC_INST(i)->alike_ptr = NULL; |
388 |
if (IC_INST(i)->whens!=NULL) { |
389 |
RemoveWhenLinks(i,IC_INST(i)->whens); |
390 |
IC_INST(i)->whens=NULL; |
391 |
} |
392 |
i->t = ERROR_INST; |
393 |
ascfree((char *)i); |
394 |
return; |
395 |
case SYMBOL_CONSTANT_INST: |
396 |
gl_destroy(SYMC_INST(i)->parents); |
397 |
SYMC_INST(i)->parents = NULL; |
398 |
DeleteTypeDesc(SYMC_INST(i)->desc); |
399 |
SYMC_INST(i)->desc = NULL; |
400 |
SYMC_INST(i)->alike_ptr = NULL; |
401 |
SYMC_INST(i)->value = NULL; |
402 |
if (SYMC_INST(i)->whens!=NULL) { |
403 |
RemoveWhenLinks(i,SYMC_INST(i)->whens); |
404 |
SYMC_INST(i)->whens=NULL; |
405 |
} |
406 |
i->t = ERROR_INST; |
407 |
ascfree((char *)i); |
408 |
return; |
409 |
case REAL_ATOM_INST: |
410 |
//CONSOLE_DEBUG("REMOVE PARTS OF VAR %p =========",i); |
411 |
/* deallocate dynamic memory used by children */ |
412 |
DestroyAtomChildren(RA_CHILD(i,0),ChildListLen(GetChildList(RA_INST(i)->desc))); |
413 |
/* continue delete the atom */ |
414 |
gl_destroy(RA_INST(i)->parents); |
415 |
RA_INST(i)->parents = NULL; |
416 |
DeleteTypeDesc(RA_INST(i)->desc); |
417 |
RA_INST(i)->desc = NULL; |
418 |
RA_INST(i)->alike_ptr = NULL; |
419 |
RemoveRelationLinks(i); |
420 |
/* children are automatically deleted by the following ---- EH??? how does that work? -JP */ |
421 |
i->t = ERROR_INST; |
422 |
ascfree((char *)i); |
423 |
return; |
424 |
case BOOLEAN_ATOM_INST: |
425 |
/* deallocate dynamic memory used by children */ |
426 |
DestroyAtomChildren(BA_CHILD(i,0), |
427 |
ChildListLen(GetChildList(BA_INST(i)->desc))); |
428 |
/* continue delete the atom */ |
429 |
gl_destroy(BA_INST(i)->parents); |
430 |
BA_INST(i)->parents = NULL; |
431 |
DeleteTypeDesc(BA_INST(i)->desc); |
432 |
BA_INST(i)->desc = NULL; |
433 |
BA_INST(i)->alike_ptr = NULL; |
434 |
if (BA_INST(i)->logrelations!=NULL) |
435 |
RemoveLogRelLinks(i,BA_INST(i)->logrelations); |
436 |
if (BA_INST(i)->whens!=NULL) { |
437 |
RemoveWhenLinks(i,BA_INST(i)->whens); |
438 |
BA_INST(i)->whens=NULL; |
439 |
} |
440 |
i->t = ERROR_INST; |
441 |
/* children are automatically deleted by the following */ |
442 |
ascfree((char *)i); |
443 |
return; |
444 |
case INTEGER_ATOM_INST: |
445 |
/* deallocate dynamic memory used by children */ |
446 |
DestroyAtomChildren(IA_CHILD(i,0), |
447 |
ChildListLen(GetChildList(IA_INST(i)->desc))); |
448 |
/* continue delete the atom */ |
449 |
gl_destroy(IA_INST(i)->parents); |
450 |
IA_INST(i)->parents = NULL; |
451 |
DeleteTypeDesc(IA_INST(i)->desc); |
452 |
IA_INST(i)->desc = NULL; |
453 |
IA_INST(i)->alike_ptr = NULL; |
454 |
if (IA_INST(i)->whens!=NULL) { |
455 |
RemoveWhenLinks(i,IA_INST(i)->whens); |
456 |
IA_INST(i)->whens=NULL; |
457 |
} |
458 |
i->t = ERROR_INST; |
459 |
/* children are automatically deleted by the following */ |
460 |
ascfree((char *)i); |
461 |
return; |
462 |
case SET_ATOM_INST: |
463 |
/* deallocate dynamic memory used by children */ |
464 |
DestroyAtomChildren(SA_CHILD(i,0), |
465 |
ChildListLen(GetChildList(SA_INST(i)->desc))); |
466 |
/* continue delete the atom */ |
467 |
gl_destroy(SA_INST(i)->parents); |
468 |
SA_INST(i)->parents = NULL; |
469 |
DeleteTypeDesc(SA_INST(i)->desc); |
470 |
SA_INST(i)->desc = NULL; |
471 |
SA_INST(i)->alike_ptr = NULL; |
472 |
if (SA_INST(i)->list != NULL) |
473 |
DestroySet(SA_INST(i)->list); |
474 |
i->t = ERROR_INST; |
475 |
ascfree((char *)i); |
476 |
return; |
477 |
case SYMBOL_ATOM_INST: |
478 |
/* deallocate dynamic memory used by children */ |
479 |
DestroyAtomChildren(SYMA_CHILD(i,0), |
480 |
ChildListLen(GetChildList(SYMA_INST(i)->desc))); |
481 |
/* continue to delete the atom */ |
482 |
gl_destroy(SYMA_INST(i)->parents); |
483 |
SYMA_INST(i)->parents = NULL; |
484 |
DeleteTypeDesc(SYMA_INST(i)->desc); |
485 |
SYMA_INST(i)->desc = NULL; |
486 |
SYMA_INST(i)->alike_ptr = NULL; |
487 |
if (SYMA_INST(i)->whens!=NULL) { |
488 |
RemoveWhenLinks(i,SYMA_INST(i)->whens); |
489 |
SYMA_INST(i)->whens = NULL; |
490 |
} |
491 |
SYMA_INST(i)->value = NULL; |
492 |
i->t = ERROR_INST; |
493 |
ascfree((char *)i); |
494 |
return; |
495 |
case REL_INST: |
496 |
//CONSOLE_DEBUG("REMOVE PARTS OF REL %p ===================",i); |
497 |
/* deallocate dynamic memory used by children */ |
498 |
DestroyAtomChildren(REL_CHILD(i,0), |
499 |
ChildListLen(GetChildList(RELN_INST(i)->desc))); |
500 |
/* continue deleting the relation */ |
501 |
DeleteTypeDesc(RELN_INST(i)->desc); |
502 |
RELN_INST(i)->desc = NULL; |
503 |
RELN_INST(i)->parent[0] = NULL; |
504 |
RELN_INST(i)->parent[1] = NULL; |
505 |
if (RELN_INST(i)->logrels!=NULL) { |
506 |
RemoveLogRelLinks(i,RELN_INST(i)->logrels); |
507 |
RELN_INST(i)->logrels=NULL; |
508 |
} |
509 |
if (RELN_INST(i)->whens!=NULL) { |
510 |
RemoveWhenLinks(i,RELN_INST(i)->whens); |
511 |
RELN_INST(i)->whens=NULL; |
512 |
} |
513 |
/* delete references of reals to this expression */ |
514 |
if(RELN_INST(i)->ptr != NULL){ |
515 |
//CONSOLE_DEBUG("Destroying links to relation %p",i); |
516 |
DestroyRelation(RELN_INST(i)->ptr,i); |
517 |
RELN_INST(i)->ptr = NULL; |
518 |
} |
519 |
/* after relation has been destroyed */ |
520 |
if(RELN_INST(i)->ptr != NULL){ |
521 |
ERROR_REPORTER_HERE(ASC_PROG_ERR,"Rel ptr not null where expected"); |
522 |
} |
523 |
i->t = ERROR_INST; |
524 |
ascfree((char *)i); |
525 |
return; |
526 |
case LREL_INST: |
527 |
DestroyAtomChildren(LREL_CHILD(i,0), |
528 |
ChildListLen(GetChildList(LRELN_INST(i)->desc))); |
529 |
DeleteTypeDesc(LRELN_INST(i)->desc); |
530 |
LRELN_INST(i)->desc = NULL; |
531 |
LRELN_INST(i)->parent[0] = NULL; |
532 |
LRELN_INST(i)->parent[1] = NULL; |
533 |
if (LRELN_INST(i)->logrels!=NULL) { |
534 |
RemoveLogRelLinks(i,LRELN_INST(i)->logrels); |
535 |
LRELN_INST(i)->logrels=NULL; |
536 |
} |
537 |
if (LRELN_INST(i)->whens!=NULL) { |
538 |
RemoveWhenLinks(i,LRELN_INST(i)->whens); |
539 |
LRELN_INST(i)->whens=NULL; |
540 |
} |
541 |
if (LRELN_INST(i)->ptr != NULL){ |
542 |
DestroyLogRelation(LRELN_INST(i)->ptr,i); |
543 |
LRELN_INST(i)->ptr = NULL; |
544 |
} |
545 |
LRELN_INST(i)->ptr = NULL; |
546 |
i->t = ERROR_INST; |
547 |
ascfree((char *)i); |
548 |
return; |
549 |
case WHEN_INST: |
550 |
DeleteTypeDesc(W_INST(i)->desc); |
551 |
W_INST(i)->desc = NULL; |
552 |
W_INST(i)->parent[0] = NULL; |
553 |
W_INST(i)->parent[1] = NULL; |
554 |
if (W_INST(i)->whens!=NULL) { |
555 |
RemoveWhenLinks(i,W_INST(i)->whens); |
556 |
W_INST(i)->whens=NULL; |
557 |
} |
558 |
if (W_INST(i)->bvar!=NULL) { |
559 |
DestroyWhenVarList(W_INST(i)->bvar,i); |
560 |
W_INST(i)->bvar = NULL; |
561 |
} |
562 |
if (W_INST(i)->cases!=NULL) { |
563 |
DestroyWhenCaseList(W_INST(i)->cases,i); |
564 |
W_INST(i)->cases = NULL; |
565 |
} |
566 |
i->t = ERROR_INST; |
567 |
ascfree((char *)i); |
568 |
return; |
569 |
case ARRAY_INT_INST: |
570 |
case ARRAY_ENUM_INST: |
571 |
DeleteTypeDesc(ARY_INST(i)->desc); |
572 |
gl_destroy(ARY_INST(i)->parents); |
573 |
ARY_INST(i)->parents = NULL; |
574 |
l = ARY_INST(i)->children; |
575 |
if (l!=NULL){ |
576 |
length = gl_length(l); |
577 |
for (c=1; c <= length; c++) { |
578 |
DeleteArrayChild((struct ArrayChild *)gl_fetch(l,c),i); |
579 |
FREEPOOLAC(gl_fetch(l,c)); |
580 |
} |
581 |
gl_destroy(l); |
582 |
} |
583 |
ARY_INST(i)->children = NULL; |
584 |
i->t = ERROR_INST; |
585 |
ascfree((char *)i); |
586 |
return; |
587 |
case REAL_INST: |
588 |
i->t = ERROR_INST; |
589 |
return; |
590 |
case INTEGER_INST: |
591 |
i->t = ERROR_INST; |
592 |
return; |
593 |
case BOOLEAN_INST: |
594 |
i->t = ERROR_INST; |
595 |
return; |
596 |
case SET_INST: |
597 |
if (S_INST(i)->list!=NULL) |
598 |
DestroySet(S_INST(i)->list); |
599 |
i->t = ERROR_INST; |
600 |
return; |
601 |
case SYMBOL_INST: |
602 |
i->t = ERROR_INST; |
603 |
return; |
604 |
case DUMMY_INST: |
605 |
/* no parts */ |
606 |
DeleteTypeDesc(D_INST(i)->desc); |
607 |
i->t = ERROR_INST; |
608 |
ascfree((char *)i); |
609 |
return; |
610 |
default: |
611 |
Asc_Panic(2, "DeleteInstance", |
612 |
"Unknown type of instance passed to DeleteInstance.\n"); |
613 |
} |
614 |
} |
615 |
|
616 |
void DestroyInstance(struct Instance *inst, struct Instance *parent){ |
617 |
struct TypeDescription *desc; |
618 |
int delete; |
619 |
if(inst==NULL) return; |
620 |
|
621 |
if(InterfacePtrDelete!=NULL){ |
622 |
DeleteIPtr(inst); |
623 |
} |
624 |
delete = RemoveParentReferences(inst,parent); |
625 |
if(delete){ |
626 |
if (inst->t != DUMMY_INST){ |
627 |
desc = InstanceTypeDesc(inst); |
628 |
if(GetUniversalFlag(desc)){ /* universal is being deleted */ |
629 |
RemoveUniversalInstance(GetUniversalTable(),inst); |
630 |
} |
631 |
if(IsCompoundInstance(inst) && |
632 |
InstanceKind(inst) != SIM_INST && |
633 |
((struct PendInstance *)(inst))->p != NULL |
634 |
){ |
635 |
// remove instance from the pending list, if such exists |
636 |
RemoveInstance(inst); |
637 |
} |
638 |
/* remove PENDING or maybe not pending instance in destroy process. */ |
639 |
RemoveFromClique(inst); |
640 |
DestroyInstanceParts(inst); |
641 |
}else{ |
642 |
if(D_INST(inst)->ref_count<2){ |
643 |
desc = InstanceTypeDesc(inst); |
644 |
if(GetUniversalFlag(desc)){ /* universal is being deleted */ |
645 |
RemoveUniversalInstance(GetUniversalTable(),inst); |
646 |
} |
647 |
/* dummy is never in cliques or pending */ |
648 |
DestroyInstanceParts(inst); |
649 |
} |
650 |
} |
651 |
} |
652 |
} |
653 |
|