/[ascend]/trunk/base/generic/compiler/parentchild.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/parentchild.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 114 - (show annotations) (download) (as text)
Fri Dec 16 00:20:44 2005 UTC (14 years, 9 months ago) by jds
File MIME type: text/x-csrc
File size: 34898 byte(s)
Fixed various bugs & compiler warnings:
- numerous AssertAllocateMemory & AssertMemory() bugs
- converted strdup() calls to ascstrdup() (and ascfree())
- several minor (mostly VisualC) compiler warnings (old style declarations, assignment in conditional, ...)
1 /*
2 * Ascend Instance Tree Link Implementation
3 * by Tom Epperly & Ben Allan
4 * 9/3/89
5 * Version: $Revision: 1.15 $
6 * Version control file: $RCSfile: parentchild.c,v $
7 * Date last modified: $Date: 1998/06/11 15:28:33 $
8 * Last modified by: $Author: ballan $
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 the program; if not, write to the Free Software Foundation,
28 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
29 * COPYING.
30 *
31 */
32 #include <stdarg.h>
33 #include "utilities/ascConfig.h"
34 #include "utilities/ascPanic.h"
35 #include "utilities/ascMalloc.h"
36 #include "general/pool.h"
37 #include "general/list.h"
38 #include "general/dstring.h"
39 #include "compiler/compiler.h"
40 #include "compiler/bit.h"
41 #include "compiler/symtab.h"
42 #include "compiler/fractions.h"
43 #include "compiler/dimen.h"
44 #include "compiler/functype.h"
45 #include "compiler/types.h"
46 #include "compiler/child.h"
47 #include "compiler/childinfo.h"
48 #include "compiler/type_desc.h"
49 #include "compiler/instance_enum.h"
50 #include "compiler/instance_name.h"
51 #include "compiler/instance_io.h"
52 #include "compiler/instmacro.h"
53 #include "compiler/instquery.h"
54 #include "compiler/atomvalue.h"
55 #include "compiler/atomsize.h"
56 #include "compiler/check.h"
57 #include "compiler/dump.h"
58 #include "compiler/prototype.h"
59 #include "compiler/pending.h"
60 #include "compiler/find.h"
61 #include "compiler/relation_type.h"
62 #include "compiler/logical_relation.h"
63 #include "compiler/extfunc.h"
64 #include "compiler/relation.h"
65 #include "compiler/logrelation.h"
66 #include "compiler/relation_util.h"
67 #include "compiler/logrel_util.h"
68 #include "compiler/rel_common.h"
69 #include "compiler/case.h"
70 #include "compiler/when_util.h"
71 #include "compiler/universal.h"
72 #include "compiler/instance_types.h"
73 #include "compiler/cmpfunc.h"
74 #include "compiler/childio.h"
75 #include "compiler/parentchild.h"
76
77 #ifndef lint
78 static CONST char ParentChildModuleID[] = "$Id: parentchild.c,v 1.15 1998/06/11 15:28:33 ballan Exp $";
79 #endif
80
81 unsigned long NumberParents(CONST struct Instance *i)
82 {
83 AssertMemory(i);
84 assert(i != NULL);
85 switch(i->t) {
86 /* relations have only zero, one or two parents */
87 case REL_INST:
88 if (RELN_INST(i)->parent[1]) return 2;
89 if (RELN_INST(i)->parent[0]) return 1;
90 else return 0;
91 case LREL_INST:
92 if (LRELN_INST(i)->parent[1]) return 2;
93 if (LRELN_INST(i)->parent[0]) return 1;
94 else return 0;
95 case WHEN_INST:
96 if (W_INST(i)->parent[1]) return 2;
97 if (W_INST(i)->parent[0]) return 1;
98 else return 0;
99 case SIM_INST:
100 return 0;
101 /* fundamental instances have only zero or one parent */
102 case REAL_INST:
103 return R_INST(i)->parent_offset ? 1 : 0;
104 case INTEGER_INST:
105 return I_INST(i)->parent_offset ? 1 : 0;
106 case BOOLEAN_INST:
107 return B_INST(i)->parent_offset ? 1 : 0;
108 case SET_INST:
109 return S_INST(i)->parent_offset ? 1 : 0;
110 case SYMBOL_INST:
111 return SYM_INST(i)->parent_offset ? 1 : 0;
112 case MODEL_INST:
113 return gl_length(MOD_INST(i)->parents);
114 case REAL_CONSTANT_INST:
115 return gl_length(RC_INST(i)->parents);
116 case BOOLEAN_CONSTANT_INST:
117 return gl_length(BC_INST(i)->parents);
118 case INTEGER_CONSTANT_INST:
119 return gl_length(IC_INST(i)->parents);
120 case SYMBOL_CONSTANT_INST:
121 return gl_length(SYMC_INST(i)->parents);
122 case REAL_ATOM_INST:
123 return gl_length(RA_INST(i)->parents);
124 case BOOLEAN_ATOM_INST:
125 return gl_length(BA_INST(i)->parents);
126 case INTEGER_ATOM_INST:
127 return gl_length(IA_INST(i)->parents);
128 case SET_ATOM_INST:
129 return gl_length(SA_INST(i)->parents);
130 case SYMBOL_ATOM_INST:
131 return gl_length(SYMA_INST(i)->parents);
132 case ARRAY_INT_INST:
133 case ARRAY_ENUM_INST:
134 return gl_length(ARY_INST(i)->parents);
135 case DUMMY_INST:
136 return 0;
137 default:
138 Asc_Panic(2, NULL, "Invalid instance passed to NumberParents.\n");
139 exit(2);/* Needed to keep gcc from whining */
140 }
141 }
142
143 struct Instance *InstanceParent(CONST struct Instance *i, unsigned long int n)
144 {
145 AssertMemory(i);
146 assert((i!=NULL) && (n>0) && (n<=NumberParents(i)));
147 switch(i->t) {
148 case MODEL_INST:
149 return INST(gl_fetch(MOD_INST(i)->parents,n));
150 case REAL_CONSTANT_INST:
151 return INST(gl_fetch(RC_INST(i)->parents,n));
152 case BOOLEAN_CONSTANT_INST:
153 return INST(gl_fetch(BC_INST(i)->parents,n));
154 case INTEGER_CONSTANT_INST:
155 return INST(gl_fetch(IC_INST(i)->parents,n));
156 case SYMBOL_CONSTANT_INST:
157 return INST(gl_fetch(SYMC_INST(i)->parents,n));
158 case REAL_ATOM_INST:
159 return INST(gl_fetch(RA_INST(i)->parents,n));
160 case BOOLEAN_ATOM_INST:
161 return INST(gl_fetch(BA_INST(i)->parents,n));
162 case INTEGER_ATOM_INST:
163 return INST(gl_fetch(IA_INST(i)->parents,n));
164 case SET_ATOM_INST:
165 return INST(gl_fetch(SA_INST(i)->parents,n));
166 case SYMBOL_ATOM_INST:
167 return INST(gl_fetch(SYMA_INST(i)->parents,n));
168 case REL_INST:
169 return RELN_INST(i)->parent[n - 1];
170 case LREL_INST:
171 return LRELN_INST(i)->parent[n - 1];
172 case WHEN_INST:
173 return W_INST(i)->parent[n - 1];
174 case ARRAY_INT_INST:
175 case ARRAY_ENUM_INST:
176 return INST(gl_fetch(ARY_INST(i)->parents,n));
177 case REAL_INST:
178 return R_PARENT(i);
179 case INTEGER_INST:
180 return I_PARENT(i);
181 case BOOLEAN_INST:
182 return B_PARENT(i);
183 case SET_INST:
184 return S_PARENT(i);
185 case SYMBOL_INST:
186 return SYM_PARENT(i);
187 case SIM_INST:
188 case DUMMY_INST:
189 default:
190 Asc_Panic(2, NULL, "Invalid instance passed to InstanceParent.\n");
191 exit(2);/* Needed to keep gcc from whining */
192 }
193 }
194
195 #define ParentSearch(list,parent)\
196 gl_search(list,(char *)parent,(CmpFunc)CmpParents)
197
198 void DeleteParent(struct Instance *i, unsigned long int pos)
199 {
200 assert((i != NULL)
201 && (i->t==DUMMY_INST || ( pos>0 && pos<=NumberParents(i) )));
202 AssertMemory(i);
203 switch(i->t) {
204 case MODEL_INST:
205 gl_delete(MOD_INST(i)->parents,pos,0);
206 break;
207 case REAL_CONSTANT_INST:
208 gl_delete(RC_INST(i)->parents,pos,0);
209 break;
210 case BOOLEAN_CONSTANT_INST:
211 gl_delete(BC_INST(i)->parents,pos,0);
212 break;
213 case INTEGER_CONSTANT_INST:
214 gl_delete(IC_INST(i)->parents,pos,0);
215 break;
216 case SYMBOL_CONSTANT_INST:
217 gl_delete(SYMC_INST(i)->parents,pos,0);
218 break;
219 case REAL_ATOM_INST:
220 gl_delete(RA_INST(i)->parents,pos,0);
221 break;
222 case BOOLEAN_ATOM_INST:
223 gl_delete(BA_INST(i)->parents,pos,0);
224 break;
225 case INTEGER_ATOM_INST:
226 gl_delete(IA_INST(i)->parents,pos,0);
227 break;
228 case SET_ATOM_INST:
229 gl_delete(SA_INST(i)->parents,pos,0);
230 break;
231 case SYMBOL_ATOM_INST:
232 gl_delete(SYMA_INST(i)->parents,pos,0);
233 break;
234 case ARRAY_INT_INST:
235 case ARRAY_ENUM_INST:
236 gl_delete(ARY_INST(i)->parents,pos,0);
237 break;
238 case REL_INST:
239 if (pos == 1){
240 RELN_INST(i)->parent[0] = RELN_INST(i)->parent[1];
241 RELN_INST(i)->parent[1] = NULL;
242 }
243 else{
244 assert(pos==2);
245 RELN_INST(i)->parent[1] = NULL;
246 }
247 break;
248 case LREL_INST:
249 if (pos == 1){
250 LRELN_INST(i)->parent[0] = LRELN_INST(i)->parent[1];
251 LRELN_INST(i)->parent[1] = NULL;
252 }
253 else{
254 assert(pos==2);
255 LRELN_INST(i)->parent[1] = NULL;
256 }
257 break;
258 case WHEN_INST:
259 if (pos == 1){
260 W_INST(i)->parent[0] = W_INST(i)->parent[1];
261 W_INST(i)->parent[1] = NULL;
262 }
263 else{
264 assert(pos==2);
265 W_INST(i)->parent[1] = NULL;
266 }
267 break;
268 case REAL_INST:
269 assert(pos==1);
270 R_INST(i)->parent_offset = NULL;
271 break;
272 case INTEGER_INST:
273 assert(pos==1);
274 I_INST(i)->parent_offset = NULL;
275 break;
276 case BOOLEAN_INST:
277 assert(pos==1);
278 B_INST(i)->parent_offset = NULL;
279 break;
280 case SET_INST:
281 assert(pos==1);
282 S_INST(i)->parent_offset = NULL;
283 break;
284 case SYMBOL_INST:
285 assert(pos==1);
286 SYM_INST(i)->parent_offset = NULL;
287 case DUMMY_INST:
288 D_INST(i)->ref_count--;
289 break;
290 default:
291 Asc_Panic(2, NULL, "Invalid instance passed to DeleteParent.\n");
292 }
293 }
294
295 unsigned long SearchForParent(CONST struct Instance *i,
296 CONST struct Instance *p)
297 {
298 AssertMemory(i);
299 switch(i->t) {
300 case MODEL_INST:
301 return ParentSearch(MOD_INST(i)->parents,p);
302 case REAL_CONSTANT_INST:
303 return ParentSearch(RC_INST(i)->parents,p);
304 case BOOLEAN_CONSTANT_INST:
305 return ParentSearch(BC_INST(i)->parents,p);
306 case INTEGER_CONSTANT_INST:
307 return ParentSearch(IC_INST(i)->parents,p);
308 case SYMBOL_CONSTANT_INST:
309 return ParentSearch(SYMC_INST(i)->parents,p);
310 case REAL_ATOM_INST:
311 return ParentSearch(RA_INST(i)->parents,p);
312 case BOOLEAN_ATOM_INST:
313 return ParentSearch(BA_INST(i)->parents,p);
314 case INTEGER_ATOM_INST:
315 return ParentSearch(IA_INST(i)->parents,p);
316 case SET_ATOM_INST:
317 return ParentSearch(SA_INST(i)->parents,p);
318 case SYMBOL_ATOM_INST:
319 return ParentSearch(SYMA_INST(i)->parents,p);
320 case REL_INST:
321 if (RELN_INST(i)->parent[0]==p) return 1;
322 if (RELN_INST(i)->parent[1]==p) return 2;
323 return 0;
324 case LREL_INST:
325 if (LRELN_INST(i)->parent[0]==p) return 1;
326 if (LRELN_INST(i)->parent[1]==p) return 2;
327 return 0;
328 case WHEN_INST:
329 if (W_INST(i)->parent[0]==p) return 1;
330 if (W_INST(i)->parent[1]==p) return 2;
331 return 0;
332 case ARRAY_INT_INST:
333 case ARRAY_ENUM_INST:
334 return ParentSearch(ARY_INST(i)->parents,p);
335 case REAL_INST:
336 return (R_PARENT(i)==p) ? 1 : 0;
337 case INTEGER_INST:
338 return (I_PARENT(i)==p) ? 1 : 0;
339 case BOOLEAN_INST:
340 return (B_PARENT(i)==p) ? 1 : 0;
341 case SET_INST:
342 return (S_PARENT(i)==p) ? 1 : 0;
343 case SYMBOL_INST:
344 return (SYM_PARENT(i)==p) ? 1 : 0;
345 case DUMMY_INST:
346 return 0;
347 default:
348 Asc_Panic(2, NULL, "Invalid instance passed to SearchForParent.\n");
349 exit(2);/* Needed to keep gcc from whining */
350 }
351 }
352
353 /*********************************************************************\
354 This function has a funny name. It means, what does my parent call
355 me. Instances dont know their names and have to ask their parents.
356 \*********************************************************************/
357
358 struct InstanceName ParentsName(CONST struct Instance *p,
359 CONST struct Instance *c)
360 {
361 register unsigned long count=1, num_children;
362 register struct Instance **child;
363 register ChildListPtr clist;
364 struct InstanceName result;
365 register struct gl_list_t *arylist;
366 register struct ArrayChild *arychild;
367 AssertMemory(p);
368 AssertMemory(c);
369 assert((p != NULL)&&(c != NULL));
370 switch(p->t) {
371 case SIM_INST: /* sims hold the name of their only child */
372 SetInstanceNameType(result,StrName);
373 SetInstanceNameStrPtr(result,SIM_INST(p)->name);
374 return result;
375 case MODEL_INST:
376 child = MOD_CHILD(p,0);
377 num_children = ChildListLen(clist = GetChildList(MOD_INST(p)->desc));
378 break;
379 case REAL_ATOM_INST:
380 child = RA_CHILD(p,0);
381 num_children = ChildListLen(clist = GetChildList(RA_INST(p)->desc));
382 break;
383 case BOOLEAN_ATOM_INST:
384 child = BA_CHILD(p,0);
385 num_children = ChildListLen(clist = GetChildList(BA_INST(p)->desc));
386 break;
387 case INTEGER_ATOM_INST:
388 child = IA_CHILD(p,0);
389 num_children = ChildListLen(clist = GetChildList(IA_INST(p)->desc));
390 break;
391 case SET_ATOM_INST:
392 child = SA_CHILD(p,0);
393 num_children = ChildListLen(clist = GetChildList(SA_INST(p)->desc));
394 break;
395 case SYMBOL_ATOM_INST:
396 child = SYMA_CHILD(p,0);
397 num_children = ChildListLen(clist = GetChildList(SYMA_INST(p)->desc));
398 break;
399 case REL_INST:
400 child = REL_CHILD(p,0);
401 num_children = ChildListLen(clist = GetChildList(RELN_INST(p)->desc));
402 break;
403 case LREL_INST:
404 child = LREL_CHILD(p,0);
405 num_children = ChildListLen(clist = GetChildList(LRELN_INST(p)->desc));
406 break;
407 case WHEN_INST: /* WHEN instances have no children */
408 Asc_Panic(2, NULL, "ParentsName cannot be called on a WHEN instance.\n");
409 case ARRAY_INT_INST:
410 if (ARY_INST(p)->children!=NULL){
411 SetInstanceNameType(result,IntArrayIndex);
412 for (count=gl_length(arylist = ARY_INST(p)->children);count>0;count--) {
413 arychild = (struct ArrayChild *)gl_fetch(arylist,count);
414 AssertContainedMemory(arychild,sizeof(struct ArrayChild));
415 if (arychild->inst == c) {
416 SetInstanceNameIntIndex(result,arychild->name.index);
417 return result;
418 }
419 }
420 }
421 else{
422 SetInstanceNameType(result,StrName);
423 SetInstanceNameStrPtr(result,AddSymbol(""));
424 }
425 return result;
426 case ARRAY_ENUM_INST:
427 if (ARY_INST(p)->children!=NULL){
428 SetInstanceNameType(result,StrArrayIndex);
429 for (count=gl_length(arylist = ARY_INST(p)->children);count>0;count--) {
430 arychild = (struct ArrayChild *)gl_fetch(arylist,count);
431 AssertContainedMemory(arychild,sizeof(struct ArrayChild));
432 if (arychild->inst == c) {
433 SetInstanceNameStrIndex(result,arychild->name.str);
434 return result;
435 }
436 }
437 }
438 else{
439 SetInstanceNameType(result,StrName);
440 SetInstanceNameStrPtr(result,AddSymbol(""));
441 }
442 return result;
443 case REAL_CONSTANT_INST:
444 case INTEGER_CONSTANT_INST:
445 case BOOLEAN_CONSTANT_INST:
446 case SYMBOL_CONSTANT_INST:
447 case REAL_INST:
448 case INTEGER_INST:
449 case BOOLEAN_INST:
450 case SET_INST:
451 case SYMBOL_INST:
452 case DUMMY_INST:
453 Asc_Panic(2, NULL,
454 "ParentsName cannot be called on a fundamental or constant.\n");
455 child = 0;
456 clist = 0;
457 num_children = 0;
458 default:
459 Asc_Panic(2, NULL, "Invalid instance passed as parent to ParentsName.\n");
460 child = 0;
461 clist = 0;
462 num_children = 0;
463 }
464 SetInstanceNameType(result,StrName);
465 while(num_children--) {
466 if (*(child++) == c) {
467 SetInstanceNameStrPtr(result,ChildStrPtr(clist,count));
468 return result;
469 }
470 count++;
471 }
472 SetInstanceNameStrPtr(result,AddSymbol(""));
473 return result;
474 }
475
476 void AddParent(struct Instance *i, struct Instance *p)
477 {
478 assert((i!=NULL) && (p!=NULL));
479 AssertMemory(i);
480 AssertMemory(p);
481 switch(i->t) {
482 case MODEL_INST:
483 gl_insert_sorted(MOD_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
484 break;
485 case REAL_CONSTANT_INST:
486 gl_insert_sorted(RC_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
487 break;
488 case BOOLEAN_CONSTANT_INST:
489 gl_insert_sorted(BC_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
490 break;
491 case INTEGER_CONSTANT_INST:
492 gl_insert_sorted(IC_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
493 break;
494 case SYMBOL_CONSTANT_INST:
495 gl_insert_sorted(SYMC_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
496 break;
497 case REAL_ATOM_INST:
498 gl_insert_sorted(RA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
499 break;
500 case BOOLEAN_ATOM_INST:
501 gl_insert_sorted(BA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
502 break;
503 case INTEGER_ATOM_INST:
504 gl_insert_sorted(IA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
505 break;
506 case SET_ATOM_INST:
507 gl_insert_sorted(SA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
508 break;
509 case SYMBOL_ATOM_INST:
510 gl_insert_sorted(SYMA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
511 break;
512 case ARRAY_INT_INST:
513 case ARRAY_ENUM_INST:
514 gl_insert_sorted(ARY_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
515 break;
516 /* relations can only have one or two parents */
517 case REL_INST:
518 if (RELN_INST(i)->parent[0]==NULL)
519 RELN_INST(i)->parent[0] = p;
520 else if (RELN_INST(i)->parent[1]==NULL){
521 if (p > RELN_INST(i)->parent[0])
522 RELN_INST(i)->parent[1] = p;
523 else{
524 RELN_INST(i)->parent[1] = RELN_INST(i)->parent[0];
525 RELN_INST(i)->parent[0] = p;
526 }
527 }
528 else{
529 Asc_Panic(2, NULL,
530 "Relations can only have two parents.\n");/*NOTREACHED*/
531 }
532 break;
533 case LREL_INST:
534 if (LRELN_INST(i)->parent[0]==NULL)
535 LRELN_INST(i)->parent[0] = p;
536 else if (LRELN_INST(i)->parent[1]==NULL){
537 if (p > LRELN_INST(i)->parent[0])
538 LRELN_INST(i)->parent[1] = p;
539 else{
540 LRELN_INST(i)->parent[1] = LRELN_INST(i)->parent[0];
541 LRELN_INST(i)->parent[0] = p;
542 }
543 }
544 else{
545 Asc_Panic(2, NULL, "Logical relations can only have two parents.\n");
546 }
547 break;
548 /* WHENs can only have one or two parents */
549 case WHEN_INST:
550 if (W_INST(i)->parent[0]==NULL)
551 W_INST(i)->parent[0] = p;
552 else if (W_INST(i)->parent[1]==NULL){
553 if (p > W_INST(i)->parent[0])
554 W_INST(i)->parent[1] = p;
555 else{
556 W_INST(i)->parent[1] = W_INST(i)->parent[0];
557 W_INST(i)->parent[0] = p;
558 }
559 }
560 else{
561 Asc_Panic(2, NULL, "WHENs can only have two parents.\n");/*NOTREACHED*/
562 }
563 break;
564 /* fundamental atom instances can only have one parent */
565 case REAL_INST:
566 case INTEGER_INST:
567 case BOOLEAN_INST:
568 case SET_INST:
569 case SYMBOL_INST:
570 Asc_Panic(2, NULL,
571 "It is illegal to call AddParent"
572 " on a relation or funamental instance.\n");
573 case DUMMY_INST:
574 D_INST(i)->ref_count++;
575 break;
576 case SIM_INST:
577 default:
578 Asc_Panic(2, NULL,
579 "Illegal argument passed as passed as i to AddParent.\n");
580 }
581 }
582
583 unsigned long NumberChildren(CONST struct Instance *i)
584 {
585 assert(i!=NULL);
586 AssertMemory(i);
587 if (IsChildlessInstance(i)) {
588 return 0; /*no children of constants,when,funds*/
589 }
590 switch(i->t) {
591 case SIM_INST: /* only and always, 1 child */
592 return 1;
593 case MODEL_INST:
594 return ChildListLen(GetChildList(MOD_INST(i)->desc));
595 case REAL_ATOM_INST:
596 return ChildListLen(GetChildList(RA_INST(i)->desc));
597 case BOOLEAN_ATOM_INST:
598 return ChildListLen(GetChildList(BA_INST(i)->desc));
599 case INTEGER_ATOM_INST:
600 return ChildListLen(GetChildList(IA_INST(i)->desc));
601 case SET_ATOM_INST:
602 return ChildListLen(GetChildList(SA_INST(i)->desc));
603 case SYMBOL_ATOM_INST:
604 return ChildListLen(GetChildList(SYMA_INST(i)->desc));
605 case REL_INST:
606 return ChildListLen(GetChildList(RELN_INST(i)->desc));
607 case LREL_INST:
608 return ChildListLen(GetChildList(LRELN_INST(i)->desc));
609 case ARRAY_INT_INST:
610 case ARRAY_ENUM_INST:
611 if (ARY_INST(i)->children!=NULL)
612 return gl_length(ARY_INST(i)->children);
613 break;
614 case DUMMY_INST:
615 return 0;
616 default:
617 Asc_Panic(2, NULL, "Invalid argument to NumberChildren.\n");
618 /*NOTREACHED*/
619 }
620 return 0;
621 }
622
623 #define OLDICHILD 0
624 #if OLDICHILD
625 struct Instance *InstanceChild(CONST struct Instance *i,
626 unsigned long int n)
627 {
628 register struct ArrayChild *ptr;
629 register struct Instance **child;
630 assert((i!=NULL) && (n>0) && (n<=NumberChildren(i)));
631 AssertMemory(i);
632 switch(i->t) {
633 case ARRAY_INT_INST:
634 case ARRAY_ENUM_INST:
635 if (ARY_INST(i)->children!=NULL){
636 ptr = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
637 AssertContainedMemory(ptr,sizeof(struct ArrayChild));
638 return ptr->inst;
639 }
640 else return NULL;
641 case SIM_INST:
642 child = SIM_CHILD(i,n-1);
643 AssertContainedIn(i,child);
644 return *child;
645 case MODEL_INST:
646 child = MOD_CHILD(i,n-1);
647 AssertContainedIn(i,child);
648 return *child;
649 case REAL_ATOM_INST:
650 child = RA_CHILD(i,n-1);
651 AssertContainedIn(i,child);
652 return *child;
653 case BOOLEAN_ATOM_INST:
654 child = BA_CHILD(i,n-1);
655 AssertContainedIn(i,child);
656 return *child;
657 case INTEGER_ATOM_INST:
658 child = IA_CHILD(i,n-1);
659 AssertContainedIn(i,child);
660 return *child;
661 case SET_ATOM_INST:
662 child = SA_CHILD(i,n-1);
663 AssertContainedIn(i,child);
664 return *child;
665 case SYMBOL_ATOM_INST:
666 child = SYMA_CHILD(i,n-1);
667 AssertContainedIn(i,child);
668 return *child;
669 case REL_INST:
670 child = REL_CHILD(i,n-1);
671 AssertContainedIn(i,child);
672 return *child;
673 case LREL_INST:
674 child = LREL_CHILD(i,n-1);
675 AssertContainedIn(i,child);
676 return *child;
677 /* WHEN instances don't have any children */
678 case WHEN_INST:
679 Asc_Panic(2, "InstanceChild",
680 "InstanceChild shouldn't be called on WHEN instances\n");
681 /* fundamental atoms don't have any children */
682 case REAL_INST:
683 case INTEGER_INST:
684 case BOOLEAN_INST:
685 case SET_INST:
686 case SYMBOL_INST:
687 case REAL_CONSTANT_INST:
688 case INTEGER_CONSTANT_INST:
689 case BOOLEAN_CONSTANT_INST:
690 case SYMBOL_CONSTANT_INST:
691 case DUMMY_INST:
692 Asc_Panic(2, NULL,
693 "InstanceChild shouldn't be called"
694 " on fundamental atoms, constants.\n");
695 default:
696 Asc_Panic(2, NULL, "Invalid argument to InstanceChild.\n");
697 }
698 return NULL;
699 }
700 #else /* OLDICHILD */
701 /*
702 * we want to handle models, arrays, reals and rels very fast
703 * the rest we will be slow about.
704 */
705 struct Instance *InstanceChild(CONST struct Instance *i,
706 unsigned long int n)
707 {
708 #ifndef NDEBUG
709 register struct ArrayChild *ptr;
710 register struct Instance **child;
711 assert((i!=NULL) && (n>0) && (n<=NumberChildren(i)));
712 AssertMemory(i);
713 if (i->t & (IERRINST | ICHILDLESS)) {
714 Asc_Panic(2, NULL,
715 "InstanceChild called with corrupt/childless instance.\n");
716 }
717 #endif
718 if (i->t & (IERRINST | ICHILDLESS)) return NULL;
719 if (IsCompoundInstance(i)) { /* ary, model, sim */
720 if (IsArrayInstance(i)) {
721 if (ARY_INST(i)->children!=NULL){
722 #ifdef NDEBUG
723 return ((struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n))->inst;
724 #else
725 ptr = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
726 AssertContainedMemory(ptr,sizeof(struct ArrayChild));
727 return ptr->inst;
728 #endif
729 } else {
730 return NULL;
731 }
732 } else { /* model, sim */
733 if ( i->t == MODEL_INST) {
734 #ifdef NDEBUG
735 return *MOD_CHILD(i,n-1);
736 #else
737 child = MOD_CHILD(i,n-1);
738 AssertContainedIn(i,child);
739 return *child;
740 #endif
741 } else { /* sim */
742 #ifdef NDEBUG
743 return *SIM_CHILD(i,n-1);
744 #else
745 child = SIM_CHILD(i,n-1);
746 AssertContainedIn(i,child);
747 return *child;
748 #endif
749 }
750 }
751 #ifndef NDEBUG
752 Asc_Panic(2, NULL, "InstanceChild found unexpected compound instance.\n");
753 #endif
754 } else { /* not compound */
755 if (i->t == REAL_ATOM_INST) {
756 #ifdef NDEBUG
757 return *RA_CHILD(i,n-1);
758 #else
759 child = RA_CHILD(i,n-1);
760 AssertContainedIn(i,child);
761 return *child;
762 #endif
763 }
764 if (i->t == REL_INST) {
765 #ifdef NDEBUG
766 return *REL_CHILD(i,n-1);
767 #else
768 child = REL_CHILD(i,n-1);
769 AssertContainedIn(i,child);
770 return *child;
771 #endif
772 }
773 if (i->t == LREL_INST) {
774 #ifdef NDEBUG
775 return *LREL_CHILD(i,n-1);
776 #else
777 child = LREL_CHILD(i,n-1);
778 AssertContainedIn(i,child);
779 return *child;
780 #endif
781 }
782 if (i->t == INTEGER_ATOM_INST) {
783 #ifdef NDEBUG
784 return *IA_CHILD(i,n-1);
785 #else
786 child = IA_CHILD(i,n-1);
787 AssertContainedIn(i,child);
788 return *child;
789 #endif
790 }
791 if (i->t == BOOLEAN_ATOM_INST) {
792 #ifdef NDEBUG
793 return *BA_CHILD(i,n-1);
794 #else
795 child = BA_CHILD(i,n-1);
796 AssertContainedIn(i,child);
797 return *child;
798 #endif
799 }
800 if (i->t == SET_ATOM_INST) {
801 #ifdef NDEBUG
802 return *SA_CHILD(i,n-1);
803 #else
804 child = SA_CHILD(i,n-1);
805 AssertContainedIn(i,child);
806 return *child;
807 #endif
808 }
809 if (i->t == SYMBOL_ATOM_INST) {
810 #ifdef NDEBUG
811 return *SYMA_CHILD(i,n-1);
812 #else
813 child = SYMA_CHILD(i,n-1);
814 AssertContainedIn(i,child);
815 return *child;
816 #endif
817 }
818 }
819 /* constants, whens, fundamental atoms don't have any children */
820 /* NOTREACHED */
821 return NULL;
822 }
823 #endif /* OLDICHILD */
824
825 struct InstanceName ChildName(CONST struct Instance *i, unsigned long int n)
826 {
827 register struct ArrayChild *arychild;
828 struct InstanceName result;
829 assert((i!=NULL)&&(n>0)&&(n<=NumberChildren(i)));
830 AssertMemory(i);
831 switch(i->t) {
832 case SIM_INST:
833 SetInstanceNameType(result,StrName);
834 SetInstanceNameStrPtr(result,SIM_INST(i)->name);
835 break;
836 case MODEL_INST:
837 SetInstanceNameType(result,StrName);
838 SetInstanceNameStrPtr(result,
839 ChildStrPtr(GetChildList(MOD_INST(i)->desc),n));
840 break;
841 case REAL_ATOM_INST:
842 SetInstanceNameType(result,StrName);
843 SetInstanceNameStrPtr(result,
844 ChildStrPtr(GetChildList(RA_INST(i)->desc),n));
845 break;
846 case BOOLEAN_ATOM_INST:
847 SetInstanceNameType(result,StrName);
848 SetInstanceNameStrPtr(result,
849 ChildStrPtr(GetChildList(BA_INST(i)->desc),n));
850 break;
851 case INTEGER_ATOM_INST:
852 SetInstanceNameType(result,StrName);
853 SetInstanceNameStrPtr(result,
854 ChildStrPtr(GetChildList(IA_INST(i)->desc),n));
855 break;
856 case SET_ATOM_INST:
857 SetInstanceNameType(result,StrName);
858 SetInstanceNameStrPtr(result,
859 ChildStrPtr(GetChildList(SA_INST(i)->desc),n));
860 break;
861 case SYMBOL_ATOM_INST:
862 SetInstanceNameType(result,StrName);
863 SetInstanceNameStrPtr(result,
864 ChildStrPtr(GetChildList(SYMA_INST(i)->desc),n));
865 break;
866 case REL_INST:
867 SetInstanceNameType(result,StrName);
868 SetInstanceNameStrPtr(result,
869 ChildStrPtr(GetChildList(RELN_INST(i)->desc),n));
870 break;
871 case LREL_INST:
872 SetInstanceNameType(result,StrName);
873 SetInstanceNameStrPtr(result,
874 ChildStrPtr(GetChildList(LRELN_INST(i)->desc),n));
875 break;
876 case ARRAY_INT_INST:
877 SetInstanceNameType(result,IntArrayIndex);
878 arychild = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
879 SetInstanceNameIntIndex(result,arychild->name.index);
880 break;
881 case ARRAY_ENUM_INST:
882 SetInstanceNameType(result,StrArrayIndex);
883 arychild = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
884 SetInstanceNameStrIndex(result,arychild->name.str);
885 break;
886 /* WHEN instances don't have any children */
887 case WHEN_INST:
888 Asc_Panic(2, NULL, "ChildName shouldn't be called on WHEN instances\n");
889 /* fundamental, constant instances don't have children */
890 case REAL_INST:
891 case INTEGER_INST:
892 case BOOLEAN_INST:
893 case SET_INST:
894 case SYMBOL_INST:
895 case REAL_CONSTANT_INST:
896 case INTEGER_CONSTANT_INST:
897 case BOOLEAN_CONSTANT_INST:
898 case SYMBOL_CONSTANT_INST:
899 case DUMMY_INST:
900 Asc_Panic(2, NULL,
901 "ChildName should not be called"
902 " on fundamentaa/const instances.\n");
903 exit(2); /*NOTREACHED*/
904 default:
905 Asc_Panic(2, NULL, "Invalid argument passed to ChildName.\n");
906 exit(2); /*NOTREACHED*/
907 }
908 return result;
909 }
910
911 CONST struct TypeDescription *ChildRefines(CONST struct Instance *i,
912 unsigned long n)
913 {
914 struct TypeDescription *d;
915 ChildListPtr cl;
916 if (i==NULL) return NULL;
917 if (n==0||n>NumberChildren(i)) return NULL;
918 d = InstanceTypeDesc(i);
919 cl = GetChildList(d);
920 return ChildBaseTypePtr(cl,n);
921 }
922
923 CONST struct Statement *ChildDeclaration(CONST struct Instance *i,
924 unsigned long n)
925 {
926 struct TypeDescription *d;
927 ChildListPtr cl;
928 if (i==NULL) return NULL;
929 if (n==0||n>NumberChildren(i)) return NULL;
930 d = InstanceTypeDesc(i);
931 cl = GetChildList(d);
932 return ChildStatement(cl,n);
933 }
934
935 unsigned long ChildSearch(CONST struct Instance *i,
936 CONST struct InstanceName *name)
937 {
938 struct ArrayChild arychild;
939 assert((i!=NULL)&&(name!=NULL));
940 AssertMemory(i);
941 switch(i->t) {
942 case SIM_INST:
943 if (InstanceNameType(*name)==StrName)
944 if (SIM_INST(i)->name == InstanceNameStr(*name))
945 return 1;
946 break;
947 case MODEL_INST:
948 if (InstanceNameType(*name)==StrName)
949 return ChildPos(GetChildList(MOD_INST(i)->desc),InstanceNameStr(*name));
950 break;
951 case REAL_ATOM_INST:
952 if (InstanceNameType(*name)==StrName)
953 return ChildPos(GetChildList(RA_INST(i)->desc),InstanceNameStr(*name));
954 break;
955 case BOOLEAN_ATOM_INST:
956 if (InstanceNameType(*name)==StrName)
957 return ChildPos(GetChildList(BA_INST(i)->desc),InstanceNameStr(*name));
958 break;
959 case INTEGER_ATOM_INST:
960 if (InstanceNameType(*name)==StrName)
961 return ChildPos(GetChildList(IA_INST(i)->desc),InstanceNameStr(*name));
962 break;
963 case SET_ATOM_INST:
964 if (InstanceNameType(*name)==StrName)
965 return ChildPos(GetChildList(SA_INST(i)->desc),InstanceNameStr(*name));
966 break;
967 case SYMBOL_ATOM_INST:
968 if (InstanceNameType(*name)==StrName)
969 return ChildPos(GetChildList(SYMA_INST(i)->desc),InstanceNameStr(*name));
970 case REL_INST:
971 if (InstanceNameType(*name)==StrName)
972 return ChildPos(GetChildList(RELN_INST(i)->desc),InstanceNameStr(*name));
973 break;
974 case LREL_INST:
975 if (InstanceNameType(*name)==StrName)
976 return ChildPos(GetChildList(LRELN_INST(i)->desc),
977 InstanceNameStr(*name));
978 break;
979 case ARRAY_INT_INST:
980 if (ARY_INST(i)->children!=NULL){
981 if (InstanceNameType(*name)==IntArrayIndex) {
982 arychild.inst = NULL;
983 arychild.name.index = InstanceIntIndex(*name);
984 return gl_search(ARY_INST(i)->children,(char *)&arychild,
985 (CmpFunc)CmpIntIndex);
986 }
987 }
988 break;
989 case ARRAY_ENUM_INST:
990 if (ARY_INST(i)->children!=NULL){
991 if (InstanceNameType(*name)==StrArrayIndex) {
992 arychild.inst = NULL;
993 arychild.name.str = InstanceStrIndex(*name);
994 return gl_search(ARY_INST(i)->children,(char *)&arychild,
995 (CmpFunc)CmpStrIndex);
996 }
997 }
998 break;
999 /* WHEN instances don't have any children */
1000 case WHEN_INST:
1001 FPRINTF(ASCERR,
1002 "ChildSearch shouldn't be called on WHEN instances\n");
1003 break;
1004 case REAL_CONSTANT_INST:
1005 case INTEGER_CONSTANT_INST:
1006 case BOOLEAN_CONSTANT_INST:
1007 case SYMBOL_CONSTANT_INST:
1008 case REAL_INST:
1009 case INTEGER_INST:
1010 case BOOLEAN_INST:
1011 case SET_INST:
1012 case SYMBOL_INST:
1013 case DUMMY_INST:
1014 break;
1015 default:
1016 Asc_Panic(2, NULL, "Invalid argument passed to ChildSearch.\n");
1017 }
1018 return 0;
1019 }
1020
1021 unsigned long ChildIndex(CONST struct Instance *i,
1022 CONST struct Instance *child)
1023 {
1024 register unsigned long count=1, num_children=0;
1025 register struct Instance **childlist = NULL;
1026 register ChildListPtr clist;
1027 register struct gl_list_t *arylist;
1028 register struct ArrayChild *arychild;
1029
1030 assert((i!=NULL) && (child!=NULL));
1031 AssertMemory(i);
1032 if (IsChildlessInstance(i)) return 0; /* no kids of fundies, whens, consts */
1033 AssertMemory(child);
1034 switch(i->t) {
1035 case SIM_INST:
1036 childlist = SIM_CHILD(i,0);
1037 if (*childlist==child) return 1;
1038 return 0;
1039 case MODEL_INST:
1040 childlist = MOD_CHILD(i,0);
1041 num_children = ChildListLen(clist = GetChildList(MOD_INST(i)->desc));
1042 break;
1043 case REAL_ATOM_INST:
1044 childlist = RA_CHILD(i,0);
1045 num_children = ChildListLen(clist = GetChildList(RA_INST(i)->desc));
1046 break;
1047 case ARRAY_INT_INST:
1048 case ARRAY_ENUM_INST:
1049 if (ARY_INST(i)->children!=NULL){
1050 for(count=gl_length(arylist = ARY_INST(i)->children);count>0;count--) {
1051 arychild = (struct ArrayChild *)gl_fetch(arylist,count);
1052 if (arychild->inst == child) return count;
1053 }
1054 }
1055 break;
1056 case BOOLEAN_ATOM_INST:
1057 childlist = BA_CHILD(i,0);
1058 num_children = ChildListLen(clist = GetChildList(BA_INST(i)->desc));
1059 break;
1060 case INTEGER_ATOM_INST:
1061 childlist = IA_CHILD(i,0);
1062 num_children = ChildListLen(clist = GetChildList(IA_INST(i)->desc));
1063 break;
1064 case SET_ATOM_INST:
1065 childlist = SA_CHILD(i,0);
1066 num_children = ChildListLen(clist = GetChildList(SA_INST(i)->desc));
1067 break;
1068 case SYMBOL_ATOM_INST:
1069 childlist = SYMA_CHILD(i,0);
1070 num_children = ChildListLen(clist = GetChildList(SYMA_INST(i)->desc));
1071 break;
1072 case REL_INST:
1073 childlist = REL_CHILD(i,0);
1074 num_children = ChildListLen(clist = GetChildList(RELN_INST(i)->desc));
1075 break;
1076 case LREL_INST:
1077 childlist = LREL_CHILD(i,0);
1078 num_children = ChildListLen(clist = GetChildList(LRELN_INST(i)->desc));
1079 break;
1080 default:
1081 Asc_Panic(2, NULL, "Invalid argument to ChildIndex.\n");
1082 childlist = 0;
1083 }
1084 while(num_children--) {
1085 if (*(childlist++) == child) return count;
1086 count++;
1087 }
1088 return 0;
1089 }
1090
1091 struct Instance *ChildByChar(CONST struct Instance *inst,symchar *name)
1092 {
1093 unsigned long ndx;
1094 struct InstanceName childname;
1095 assert(AscFindSymbol(name)!=NULL);
1096
1097 if (inst==NULL || name == NULL) {
1098 return NULL;
1099 }
1100 childname.t = StrName;
1101 childname.u.name = name;
1102 ndx = ChildSearch(inst,&childname);
1103 if( ndx ) {
1104 return InstanceChild(inst,ndx);
1105 } else {
1106 WriteChildMissing(ASCERR,"ChildByChar",name);
1107 return NULL;
1108 }
1109 }
1110
1111 void StoreChildPtr(struct Instance *i, unsigned long int n,
1112 struct Instance *child)
1113 {
1114 register struct Instance **childptr;
1115 register struct ArrayChild *ptr;
1116 assert((i!=NULL)&&(n>0)&&(n<=NumberChildren(i)));
1117 AssertMemory(i);
1118 switch(i->t) {
1119 case SIM_INST:
1120 childptr = SIM_CHILD(i,0); /* only one child at pos 0 */
1121 AssertContainedIn(i,childptr);
1122 *childptr = child;
1123 break;
1124 case MODEL_INST:
1125 childptr = MOD_CHILD(i,n-1);
1126 AssertContainedIn(i,childptr);
1127 *childptr = child;
1128 break;
1129 case REAL_ATOM_INST:
1130 childptr = RA_CHILD(i,n-1);
1131 AssertContainedIn(i,childptr);
1132 *childptr = child;
1133 break;
1134 case BOOLEAN_ATOM_INST:
1135 childptr = BA_CHILD(i,n-1);
1136 AssertContainedIn(i,childptr);
1137 *childptr = child;
1138 break;
1139 case INTEGER_ATOM_INST:
1140 childptr = IA_CHILD(i,n-1);
1141 AssertContainedIn(i,childptr);
1142 *childptr = child;
1143 break;
1144 case SET_ATOM_INST:
1145 childptr = SA_CHILD(i,n-1);
1146 AssertContainedIn(i,childptr);
1147 *childptr = child;
1148 break;
1149 case SYMBOL_ATOM_INST:
1150 childptr = SYMA_CHILD(i,n-1);
1151 AssertContainedIn(i,childptr);
1152 *childptr = child;
1153 break;
1154 case REL_INST:
1155 childptr = REL_CHILD(i,n-1);
1156 AssertContainedIn(i,childptr);
1157 *childptr = child;
1158 break;
1159 case LREL_INST:
1160 childptr = LREL_CHILD(i,n-1);
1161 AssertContainedIn(i,childptr);
1162 *childptr = child;
1163 break;
1164 case ARRAY_INT_INST:
1165 case ARRAY_ENUM_INST:
1166 ptr = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
1167 AssertContainedMemory(ptr,sizeof(struct ArrayChild));
1168 ptr->inst = child;
1169 break;
1170 /* WHEN instances don't have any children */
1171 case WHEN_INST:
1172 Asc_Panic(2, "StoreChildPtr",
1173 "StoreChildPtr shouldn't be called on WHEN instances\n");
1174 case REAL_CONSTANT_INST:
1175 case INTEGER_CONSTANT_INST:
1176 case BOOLEAN_CONSTANT_INST:
1177 case SYMBOL_CONSTANT_INST:
1178 case REAL_INST:
1179 case INTEGER_INST:
1180 case BOOLEAN_INST:
1181 case SET_INST:
1182 case SYMBOL_INST:
1183 case DUMMY_INST:
1184 Asc_Panic(2, NULL,
1185 "StoreChildPtr shouldn't be called"
1186 " on fundamental/constant atoms.\n");
1187 default:
1188 Asc_Panic(2, NULL, "Invalid argument to StoreChildPtr.\n");
1189 }
1190 }
1191

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22