~jdpipe/ascend/trunk-old

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
 *  Ascend Instance Tree Killing
 *  by Tom Epperly & Ben Allan
 *  9/3/89
 *  Version: $Revision: 1.12 $
 *  Version control file: $RCSfile: destroyinst.c,v $
 *  Date last modified: $Date: 1997/07/18 12:28:50 $
 *  Last modified by: $Author: mthomas $
 *
 *  This file is part of the Ascend Language Interpreter.
 *
 *  Copyright (C) 1996 Ben Allan
 *  based on instance.c
 *  Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly
 *
 *  The Ascend Language Interpreter is free software; you can redistribute
 *  it and/or modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  The Ascend Language Interpreter is distributed in hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdarg.h>
#include <ascend/general/platform.h>
#include <ascend/general/panic.h>
#include <ascend/general/ascMalloc.h>
#include <ascend/general/pool.h>
#include <ascend/general/list.h>
#include <ascend/general/dstring.h>

#include "symtab.h"
#include "functype.h"
#include "expr_types.h"
#include "instance_name.h"
#include "instance_io.h"
#include "child.h"
#include "type_desc.h"
#include "instmacro.h"
#include "instquery.h"
#include "instance_types.h"
#include "setinstval.h"
#include "linkinst.h"
#include "destroyinst.h"
#include "arrayinst.h"
#include "parentchild.h"
#include "atomvalue.h"
#include "atomsize.h"
#include "check.h"
#include "dump.h"
#include "find.h"
#include "type_desc.h"
#include "prototype.h"
#include "pending.h"
#include "rel_blackbox.h"
#include "vlist.h"
#include "relation.h"
#include "logical_relation.h"
#include "logrelation.h"
#include "relation_util.h"
#include "logrel_util.h"
#include "rel_common.h"
#include "case.h"
#include "when_util.h"
#include "universal.h"
#include "instance_types.h"
#include "cmpfunc.h"
#include "slvreq.h"


static void DeleteIPtr(struct Instance *i){
  assert(i&&InterfacePtrDelete);
  AssertMemory(i);
  switch(i->t) {
  case SIM_INST:
	slvreq_sim_destroy_hooks(i);
    (*InterfacePtrDelete)(i,SIM_INST(i)->interface_ptr);
    SIM_INST(i)->interface_ptr = NULL;
    return;
  case MODEL_INST:
    (*InterfacePtrDelete)(i,MOD_INST(i)->interface_ptr);
    MOD_INST(i)->interface_ptr = NULL;
    break;
  case REAL_ATOM_INST:
    (*InterfacePtrDelete)(i,RA_INST(i)->interface_ptr);
    RA_INST(i)->interface_ptr = NULL;
    break;
  case BOOLEAN_ATOM_INST:
    (*InterfacePtrDelete)(i,BA_INST(i)->interface_ptr);
    BA_INST(i)->interface_ptr = NULL;
    break;
  case INTEGER_ATOM_INST:
    (*InterfacePtrDelete)(i,IA_INST(i)->interface_ptr);
    IA_INST(i)->interface_ptr = NULL;
    break;
  case SET_ATOM_INST:
    (*InterfacePtrDelete)(i,SA_INST(i)->interface_ptr);
    SA_INST(i)->interface_ptr = NULL;
    break;
  case SYMBOL_ATOM_INST:
    (*InterfacePtrDelete)(i,SYMA_INST(i)->interface_ptr);
    SYMA_INST(i)->interface_ptr = NULL;
    break;
  case REL_INST:
    (*InterfacePtrDelete)(i,RELN_INST(i)->interface_ptr);
    RELN_INST(i)->interface_ptr = NULL;
    break;
  case LREL_INST:
    (*InterfacePtrDelete)(i,LRELN_INST(i)->interface_ptr);
    LRELN_INST(i)->interface_ptr = NULL;
    break;
  case WHEN_INST:
    (*InterfacePtrDelete)(i,W_INST(i)->interface_ptr);
    W_INST(i)->interface_ptr = NULL;
    break;
  /* constants */
  case INTEGER_CONSTANT_INST:
    (*InterfacePtrDelete)(i,IC_INST(i)->interface_ptr);
    IC_INST(i)->interface_ptr = NULL;
    break;
  case BOOLEAN_CONSTANT_INST:
    (*InterfacePtrDelete)(i,BC_INST(i)->interface_ptr);
    BC_INST(i)->interface_ptr = NULL;
    break;
  case SYMBOL_CONSTANT_INST:
    (*InterfacePtrDelete)(i,SYMC_INST(i)->interface_ptr);
    SYMC_INST(i)->interface_ptr = NULL;
    break;
  case DUMMY_INST:
    (*InterfacePtrDelete)(i,D_INST(i)->interface_ptr);
    D_INST(i)->interface_ptr = NULL;
    break;
  case REAL_CONSTANT_INST:	/* fall through */
  /* arrays */
  case ARRAY_INT_INST:         /* fall through */
  case ARRAY_ENUM_INST:        /* fall through */
  /* fundies */
  case REAL_INST:		/* fall through */
  case INTEGER_INST:		/* fall through */
  case BOOLEAN_INST:		/* fall through */
  case SET_INST:		/* fall through */
  case SYMBOL_INST:		/* fall through */
    break;
  default:
    ASC_PANIC("Unknown instance type passed to DeleteIPtr.\n");
  }
}

/**
	This never returns anything but 1 for DUMMY_INSTs.
	@return true value if inst should be deleted; otherwise, return 0.
*/
static int RemoveParentReferences(
	struct Instance *inst, struct Instance *parent
){
  register unsigned long c,pos,length;
  AssertMemory(inst);
  if(parent!=NULL){
    AssertMemory(parent);
    /* destroy link from inst to parent */
    pos = SearchForParent(inst,parent);
    if(pos != 0 || inst->t == DUMMY_INST){
      /* Because the dummy always 'adds' a parent, it must always delete it to 
      keep the ref_count happy. Dummy knows of no parents, but knows exactly 
      how many it doesn't have. */
      DeleteParent(inst,pos);
    }
    /* destroy link(s) from parent to inst */
    while(0 != (pos = ChildIndex(parent,inst))){
      StoreChildPtr(parent,pos,NULL);
    }
    return (NumberParents(inst) == 0);
  }else{
    length = NumberParents(inst);
    if(inst->t == DUMMY_INST){
      FPRINTF(ASCERR,
        "The global dummy instance cannot be destroyed w/out parental consent\n"
      );
      FPRINTF(ASCERR, "You should not be seeing this message.\n");
      return 0;
    }
    for(c=1;c<=length;c++) {
      parent = InstanceParent(inst,c);
      while(0 != (pos = ChildIndex(parent,inst))){
        StoreChildPtr(parent,pos,NULL);
      }
    }
    return  1;
  }
}

static void RemoveFromClique(struct Instance *inst){
  register struct Instance *i,*hold;
  AssertMemory(inst);
  if ((hold=i=NextCliqueMember(inst))==inst)
    return;
  while (NextCliqueMember(i)!=inst)
    i = NextCliqueMember(i);
  switch(i->t) {
  case MODEL_INST:
    MOD_INST(i)->alike_ptr = hold;
    break;
  case REAL_CONSTANT_INST:
    RC_INST(i)->alike_ptr = hold;
    break;
  case BOOLEAN_CONSTANT_INST:
    BC_INST(i)->alike_ptr = hold;
    break;
  case INTEGER_CONSTANT_INST:
    IC_INST(i)->alike_ptr = hold;
    break;
  case SYMBOL_CONSTANT_INST:
    SYMC_INST(i)->alike_ptr = hold;
    break;
  case REAL_ATOM_INST:
    RA_INST(i)->alike_ptr = hold;
    break;
  case BOOLEAN_ATOM_INST:
    BA_INST(i)->alike_ptr = hold;
    break;
  case INTEGER_ATOM_INST:
    IA_INST(i)->alike_ptr = hold;
    break;
  case SET_ATOM_INST:
    SA_INST(i)->alike_ptr = hold;
    break;
  case SYMBOL_ATOM_INST:
    SYMA_INST(i)->alike_ptr = hold;
    break;
  default:
    Asc_Panic(2, "RemoveFromClique",
              "Invalid instance in clique found in RemoveFromClique.\n");
  }
}

static void DeleteArrayChild(struct ArrayChild *acp, struct Instance *parent){
  if ((acp!=NULL)&&(acp->inst!=NULL)) {
    AssertContainedMemory(acp,sizeof(struct ArrayChild));
    DestroyInstance(acp->inst,parent);
  }
}

/**
	Take an ATOM and tell all the relations that know about it to
	forget it, then destroy the list that identifies those relations.
	After this, a real ATOM can be safely deleted if there are no models
	refering to it.
*/
static void RemoveRelationLinks(struct Instance *i){
  struct gl_list_t *list = RA_INST(i)->relations;
  if(NULL==list)return;
  register unsigned long c,length;
  assert(list!=NULL);
  length = gl_length(list);
  for(c=1;c<=length;c++){
	//CONSOLE_DEBUG("Var %p: referenced in relation %p",i, INST(gl_fetch(list,c)));
    ChangeRelationPointers(INST(gl_fetch(list,c)),i,INST(NULL));
  }
  gl_destroy(list);
  RA_INST(i)->relations = NULL;
}


/**
	Take a BOOLEAN ATOM, REL or LOGREL and tell all the logrelations that
	know about them to forget them, then destroy the list that identifies
	those logrelations.
	After this, the ATOM, REL or LOGREL  can be deleted if there are no
	models or whens refering to it.
*/
static
void RemoveLogRelLinks(struct Instance *i, struct gl_list_t *list)
{
  register unsigned long c,length;
  assert(list!=NULL);
  length = gl_length(list);
  for(c=1;c<=length;c++) {
    ChangeLogRelPointers(INST(gl_fetch(list,c)),i,INST(NULL));
  }
  gl_destroy(list);
}


static
void RemoveWhenLinks(struct Instance *i, struct gl_list_t *list)
{
  register unsigned long c,length;
  assert(list!=NULL);
  length = gl_length(list);
  for(c=1;c<=length;c++) {
    ChangeWhenPointers(INST(gl_fetch(list,c)),i,INST(NULL));
  }
  gl_destroy(list);
}

static void DestroyAtomChildren(
	register struct Instance **i, register unsigned long int nc
){
  while(nc-- > 0){
    AssertMemory(i);
    AssertMemory(*i);
    if ((*i)->t==SET_INST) {
      if (S_INST(*i)->list!=NULL) {
        DestroySet(S_INST(*i)->list);
      }
    }
    i++;			/* position pointer for next child */
  }
}

/*
	should only be called when there areno more references
	to the object.
*/
static void DestroyInstanceParts(struct Instance *i){
  register unsigned long c,length;
  register struct gl_list_t *l;
  struct Instance *child;
  AssertMemory(i);
  switch(i->t) {
  case SIM_INST:
    child = InstanceChild(i,1); /* one child only */
    DestroyInstance(child,i);
    SIM_INST(i)->name = NULL;	/* main symbol table owns the string */
    SIM_INST(i)->extvars = NULL;
    i->t = ERROR_INST;
    DeleteTypeDesc(SIM_INST(i)->desc);
    SIM_INST(i)->desc = NULL;
    ascfree((char *)i);
    return;
  case MODEL_INST:
    gl_destroy(MOD_INST(i)->parents);
    MOD_INST(i)->parents = NULL;
    MOD_INST(i)->alike_ptr = NULL;
    if (MOD_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,MOD_INST(i)->whens);
      MOD_INST(i)->whens=NULL;
    }
    DestroyBList(MOD_INST(i)->executed);
    MOD_INST(i)->executed = NULL;
    /* destroy reference to children */
    length = NumberChildren(i);
    for(c=1;c<=length;c++) {
      child = InstanceChild(i,c);
      DestroyInstance(child,i);
    }
    i->t = ERROR_INST;
    DeleteTypeDesc(MOD_INST(i)->desc);
    MOD_INST(i)->desc = NULL;
	gl_destroy(MOD_INST(i)->link_table);
    ascfree((char *)i);
    return;
  case REAL_CONSTANT_INST:
    /* continue delete the atom */
    gl_destroy(RC_INST(i)->parents);
    RC_INST(i)->parents = NULL;
    DeleteTypeDesc(RC_INST(i)->desc);
    RC_INST(i)->desc = NULL;
    RC_INST(i)->alike_ptr = NULL;
    /* children are automatically deleted by the following */
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case BOOLEAN_CONSTANT_INST:
    gl_destroy(BC_INST(i)->parents);
    BC_INST(i)->parents = NULL;
    DeleteTypeDesc(BC_INST(i)->desc);
    BC_INST(i)->desc = NULL;
    BC_INST(i)->alike_ptr = NULL;
    if (BC_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,BC_INST(i)->whens);
      BC_INST(i)->whens=NULL;
    }
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case INTEGER_CONSTANT_INST:
    gl_destroy(IC_INST(i)->parents);
    IC_INST(i)->parents = NULL;
    DeleteTypeDesc(IC_INST(i)->desc);
    IC_INST(i)->desc = NULL;
    IC_INST(i)->alike_ptr = NULL;
    if (IC_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,IC_INST(i)->whens);
      IC_INST(i)->whens=NULL;
    }
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case SYMBOL_CONSTANT_INST:
    gl_destroy(SYMC_INST(i)->parents);
    SYMC_INST(i)->parents = NULL;
    DeleteTypeDesc(SYMC_INST(i)->desc);
    SYMC_INST(i)->desc = NULL;
    SYMC_INST(i)->alike_ptr = NULL;
    SYMC_INST(i)->value = NULL;
    if (SYMC_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,SYMC_INST(i)->whens);
      SYMC_INST(i)->whens=NULL;
    }
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case REAL_ATOM_INST:
    //CONSOLE_DEBUG("REMOVE PARTS OF VAR %p =========",i);
    /* deallocate dynamic memory used by children */
    DestroyAtomChildren(RA_CHILD(i,0),ChildListLen(GetChildList(RA_INST(i)->desc)));
    /* continue delete the atom */
    gl_destroy(RA_INST(i)->parents);
    RA_INST(i)->parents = NULL;
    DeleteTypeDesc(RA_INST(i)->desc);
    RA_INST(i)->desc = NULL;
    RA_INST(i)->alike_ptr = NULL;
    RemoveRelationLinks(i);
    /* children are automatically deleted by the following  ----  EH??? how does that work? -JP */
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case BOOLEAN_ATOM_INST:
    /* deallocate dynamic memory used by children */
    DestroyAtomChildren(BA_CHILD(i,0),
			ChildListLen(GetChildList(BA_INST(i)->desc)));
    /* continue delete the atom */
    gl_destroy(BA_INST(i)->parents);
    BA_INST(i)->parents = NULL;
    DeleteTypeDesc(BA_INST(i)->desc);
    BA_INST(i)->desc = NULL;
    BA_INST(i)->alike_ptr = NULL;
    if (BA_INST(i)->logrelations!=NULL)
      RemoveLogRelLinks(i,BA_INST(i)->logrelations);
    if (BA_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,BA_INST(i)->whens);
      BA_INST(i)->whens=NULL;
    }
    i->t = ERROR_INST;
    /* children are automatically deleted by the following */
    ascfree((char *)i);
    return;
  case INTEGER_ATOM_INST:
    /* deallocate dynamic memory used by children */
    DestroyAtomChildren(IA_CHILD(i,0),
			ChildListLen(GetChildList(IA_INST(i)->desc)));
    /* continue delete the atom */
    gl_destroy(IA_INST(i)->parents);
    IA_INST(i)->parents = NULL;
    DeleteTypeDesc(IA_INST(i)->desc);
    IA_INST(i)->desc = NULL;
    IA_INST(i)->alike_ptr = NULL;
    if (IA_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,IA_INST(i)->whens);
      IA_INST(i)->whens=NULL;
    }
    i->t = ERROR_INST;
    /* children are automatically deleted by the following */
    ascfree((char *)i);
    return;
  case SET_ATOM_INST:
    /* deallocate dynamic memory used by children */
    DestroyAtomChildren(SA_CHILD(i,0),
			ChildListLen(GetChildList(SA_INST(i)->desc)));
    /* continue delete the atom */
    gl_destroy(SA_INST(i)->parents);
    SA_INST(i)->parents = NULL;
    DeleteTypeDesc(SA_INST(i)->desc);
    SA_INST(i)->desc = NULL;
    SA_INST(i)->alike_ptr = NULL;
    if (SA_INST(i)->list != NULL)
      DestroySet(SA_INST(i)->list);
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case SYMBOL_ATOM_INST:
    /* deallocate dynamic memory used by children */
    DestroyAtomChildren(SYMA_CHILD(i,0),
			ChildListLen(GetChildList(SYMA_INST(i)->desc)));
    /* continue to delete the atom */
    gl_destroy(SYMA_INST(i)->parents);
    SYMA_INST(i)->parents = NULL;
    DeleteTypeDesc(SYMA_INST(i)->desc);
    SYMA_INST(i)->desc = NULL;
    SYMA_INST(i)->alike_ptr = NULL;
    if (SYMA_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,SYMA_INST(i)->whens);
      SYMA_INST(i)->whens = NULL;
    }
    SYMA_INST(i)->value = NULL;
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case REL_INST:
    //CONSOLE_DEBUG("REMOVE PARTS OF REL %p ===================",i);
    /* deallocate dynamic memory used by children */
    DestroyAtomChildren(REL_CHILD(i,0),
    ChildListLen(GetChildList(RELN_INST(i)->desc)));
    /* continue deleting the relation */
    DeleteTypeDesc(RELN_INST(i)->desc);
    RELN_INST(i)->desc = NULL;
    RELN_INST(i)->parent[0] = NULL;
    RELN_INST(i)->parent[1] = NULL;
    if (RELN_INST(i)->logrels!=NULL) {
      RemoveLogRelLinks(i,RELN_INST(i)->logrels);
      RELN_INST(i)->logrels=NULL;
    }
    if (RELN_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,RELN_INST(i)->whens);
      RELN_INST(i)->whens=NULL;
    }
    /* delete references of reals to this expression */
    if(RELN_INST(i)->ptr != NULL){
      //CONSOLE_DEBUG("Destroying links to relation %p",i);
      DestroyRelation(RELN_INST(i)->ptr,i);
      RELN_INST(i)->ptr = NULL;
    }
    /* after relation has been destroyed */
    if(RELN_INST(i)->ptr != NULL){
      ERROR_REPORTER_HERE(ASC_PROG_ERR,"Rel ptr not null where expected");
    }
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case LREL_INST:
    DestroyAtomChildren(LREL_CHILD(i,0),
			ChildListLen(GetChildList(LRELN_INST(i)->desc)));
    DeleteTypeDesc(LRELN_INST(i)->desc);
    LRELN_INST(i)->desc = NULL;
    LRELN_INST(i)->parent[0] = NULL;
    LRELN_INST(i)->parent[1] = NULL;
    if (LRELN_INST(i)->logrels!=NULL) {
      RemoveLogRelLinks(i,LRELN_INST(i)->logrels);
      LRELN_INST(i)->logrels=NULL;
    }
    if (LRELN_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,LRELN_INST(i)->whens);
      LRELN_INST(i)->whens=NULL;
    }
    if (LRELN_INST(i)->ptr != NULL){
      DestroyLogRelation(LRELN_INST(i)->ptr,i);
      LRELN_INST(i)->ptr = NULL;
    }
    LRELN_INST(i)->ptr = NULL;
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case WHEN_INST:
    DeleteTypeDesc(W_INST(i)->desc);
    W_INST(i)->desc = NULL;
    W_INST(i)->parent[0] = NULL;
    W_INST(i)->parent[1] = NULL;
    if (W_INST(i)->whens!=NULL) {
      RemoveWhenLinks(i,W_INST(i)->whens);
      W_INST(i)->whens=NULL;
    }
    if (W_INST(i)->bvar!=NULL) {
      DestroyWhenVarList(W_INST(i)->bvar,i);
      W_INST(i)->bvar = NULL;
    }
    if (W_INST(i)->cases!=NULL) {
      DestroyWhenCaseList(W_INST(i)->cases,i);
      W_INST(i)->cases = NULL;
    }
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case ARRAY_INT_INST:
  case ARRAY_ENUM_INST:
    DeleteTypeDesc(ARY_INST(i)->desc);
    gl_destroy(ARY_INST(i)->parents);
    ARY_INST(i)->parents = NULL;
    l = ARY_INST(i)->children;
    if (l!=NULL){
      length = gl_length(l);
      for (c=1; c <= length; c++) {
        DeleteArrayChild((struct ArrayChild *)gl_fetch(l,c),i);
        FREEPOOLAC(gl_fetch(l,c));
      }
      gl_destroy(l);
    }
    ARY_INST(i)->children = NULL;
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  case REAL_INST:
    i->t = ERROR_INST;
    return;
  case INTEGER_INST:
    i->t = ERROR_INST;
    return;
  case BOOLEAN_INST:
    i->t = ERROR_INST;
    return;
  case SET_INST:
    if (S_INST(i)->list!=NULL)
      DestroySet(S_INST(i)->list);
    i->t = ERROR_INST;
    return;
  case SYMBOL_INST:
    i->t = ERROR_INST;
    return;
  case DUMMY_INST:
    /* no parts */
    DeleteTypeDesc(D_INST(i)->desc);
    i->t = ERROR_INST;
    ascfree((char *)i);
    return;
  default:
    Asc_Panic(2, "DeleteInstance",
              "Unknown type of instance passed to DeleteInstance.\n");
  }
}

void DestroyInstance(struct Instance *inst, struct Instance *parent){
  struct TypeDescription *desc;
  int delete;
  if(inst==NULL) return;

  if(InterfacePtrDelete!=NULL){
    DeleteIPtr(inst);
  }
  delete = RemoveParentReferences(inst,parent);
  if(delete){
    if (inst->t != DUMMY_INST){
      desc = InstanceTypeDesc(inst);
      if(GetUniversalFlag(desc)){ /* universal is being deleted */
        RemoveUniversalInstance(GetUniversalTable(),inst);
      }
      if(IsCompoundInstance(inst) &&
          InstanceKind(inst) != SIM_INST &&
          ((struct PendInstance *)(inst))->p != NULL
      ){
		// remove instance from the pending list, if such exists
        RemoveInstance(inst);
      }
      /* remove PENDING or maybe not pending instance in destroy process. */
      RemoveFromClique(inst);
      DestroyInstanceParts(inst);
    }else{
      if(D_INST(inst)->ref_count<2){
        desc = InstanceTypeDesc(inst);
        if(GetUniversalFlag(desc)){ /* universal is being deleted */
          RemoveUniversalInstance(GetUniversalTable(),inst);
        }
        /* dummy is never in cliques or pending */
        DestroyInstanceParts(inst);
      }
    }
  }
}