/[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 709 - (show annotations) (download) (as text)
Wed Jun 28 16:28:57 2006 UTC (13 years, 5 months ago) by johnpye
File MIME type: text/x-csrc
File size: 34343 byte(s)
Monster commit!
Lots of recommenting and reorganising of external relations-related stuff.
Replaced a lot of ascmalloc and asccalloc calls with the new ASC_NEW* macros.
Fixed (?) the problem Art is having with icons in PyGTK.
Turned on -Wall in SConstruct and fixed up a stack of warnings.
Removed the redundant exit(2) from after Asc_Panic calls and added __attribute__((noreturn)).
Set doxygen to create callgraphs to level 2, updated doxyfile to version 1.4.7.
Fixed up building of extfntest.c.
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.h"
40 #include "bit.h"
41 #include "symtab.h"
42 #include "fractions.h"
43 #include "dimen.h"
44 #include "functype.h"
45 #include "expr_types.h"
46 #include "child.h"
47 #include "childinfo.h"
48 #include "type_desc.h"
49 #include "instance_enum.h"
50 #include "instance_name.h"
51 #include "instance_io.h"
52 #include "instmacro.h"
53 #include "instquery.h"
54 #include "atomvalue.h"
55 #include "atomsize.h"
56 #include "check.h"
57 #include "dump.h"
58 #include "prototype.h"
59 #include "pending.h"
60 #include "find.h"
61 #include "relation_type.h"
62 #include "logical_relation.h"
63 #include "extfunc.h"
64 #include "relation.h"
65 #include "logrelation.h"
66 #include "relation_util.h"
67 #include "logrel_util.h"
68 #include "rel_common.h"
69 #include "case.h"
70 #include "when_util.h"
71 #include "universal.h"
72 #include "instance_types.h"
73 #include "cmpfunc.h"
74 #include "childio.h"
75 #include "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, __FUNCTION__, "Invalid instance");
139
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, __FUNCTION__, "Invalid instance");
191
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, __FUNCTION__, "Invalid instance");
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, __FUNCTION__, "Invalid instance");
349
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, __FUNCTION__, "p is a fundamental or constant (invalid)");
454 child = 0;
455 clist = 0;
456 num_children = 0;
457 default:
458 Asc_Panic(2, __FUNCTION__, "Invalid instance passed as parent");
459 child = 0;
460 clist = 0;
461 num_children = 0;
462 }
463 SetInstanceNameType(result,StrName);
464 while(num_children--) {
465 if (*(child++) == c) {
466 SetInstanceNameStrPtr(result,ChildStrPtr(clist,count));
467 return result;
468 }
469 count++;
470 }
471 SetInstanceNameStrPtr(result,AddSymbol(""));
472 return result;
473 }
474
475 void AddParent(struct Instance *i, struct Instance *p)
476 {
477 assert((i!=NULL) && (p!=NULL));
478 AssertMemory(i);
479 AssertMemory(p);
480 switch(i->t) {
481 case MODEL_INST:
482 gl_insert_sorted(MOD_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
483 break;
484 case REAL_CONSTANT_INST:
485 gl_insert_sorted(RC_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
486 break;
487 case BOOLEAN_CONSTANT_INST:
488 gl_insert_sorted(BC_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
489 break;
490 case INTEGER_CONSTANT_INST:
491 gl_insert_sorted(IC_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
492 break;
493 case SYMBOL_CONSTANT_INST:
494 gl_insert_sorted(SYMC_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
495 break;
496 case REAL_ATOM_INST:
497 gl_insert_sorted(RA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
498 break;
499 case BOOLEAN_ATOM_INST:
500 gl_insert_sorted(BA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
501 break;
502 case INTEGER_ATOM_INST:
503 gl_insert_sorted(IA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
504 break;
505 case SET_ATOM_INST:
506 gl_insert_sorted(SA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
507 break;
508 case SYMBOL_ATOM_INST:
509 gl_insert_sorted(SYMA_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
510 break;
511 case ARRAY_INT_INST:
512 case ARRAY_ENUM_INST:
513 gl_insert_sorted(ARY_INST(i)->parents,(char *)p,(CmpFunc)CmpParents);
514 break;
515 /* relations can only have one or two parents */
516 case REL_INST:
517 if (RELN_INST(i)->parent[0]==NULL)
518 RELN_INST(i)->parent[0] = p;
519 else if (RELN_INST(i)->parent[1]==NULL){
520 if (p > RELN_INST(i)->parent[0])
521 RELN_INST(i)->parent[1] = p;
522 else{
523 RELN_INST(i)->parent[1] = RELN_INST(i)->parent[0];
524 RELN_INST(i)->parent[0] = p;
525 }
526 }
527 else{
528 Asc_Panic(2, NULL,
529 "Relations can only have two parents.\n");/*NOTREACHED*/
530 }
531 break;
532 case LREL_INST:
533 if (LRELN_INST(i)->parent[0]==NULL)
534 LRELN_INST(i)->parent[0] = p;
535 else if (LRELN_INST(i)->parent[1]==NULL){
536 if (p > LRELN_INST(i)->parent[0])
537 LRELN_INST(i)->parent[1] = p;
538 else{
539 LRELN_INST(i)->parent[1] = LRELN_INST(i)->parent[0];
540 LRELN_INST(i)->parent[0] = p;
541 }
542 }
543 else{
544 Asc_Panic(2, NULL, "Logical relations can only have two parents.\n");
545 }
546 break;
547 /* WHENs can only have one or two parents */
548 case WHEN_INST:
549 if (W_INST(i)->parent[0]==NULL)
550 W_INST(i)->parent[0] = p;
551 else if (W_INST(i)->parent[1]==NULL){
552 if (p > W_INST(i)->parent[0])
553 W_INST(i)->parent[1] = p;
554 else{
555 W_INST(i)->parent[1] = W_INST(i)->parent[0];
556 W_INST(i)->parent[0] = p;
557 }
558 }
559 else{
560 Asc_Panic(2, NULL, "WHENs can only have two parents.\n");/*NOTREACHED*/
561 }
562 break;
563 /* fundamental atom instances can only have one parent */
564 case REAL_INST:
565 case INTEGER_INST:
566 case BOOLEAN_INST:
567 case SET_INST:
568 case SYMBOL_INST:
569 Asc_Panic(2, NULL,
570 "It is illegal to call AddParent"
571 " on a relation or funamental instance.\n");
572 case DUMMY_INST:
573 D_INST(i)->ref_count++;
574 break;
575 case SIM_INST:
576 default:
577 Asc_Panic(2, NULL,
578 "Illegal argument passed as passed as i to AddParent.\n");
579 }
580 }
581
582 unsigned long NumberChildren(CONST struct Instance *i)
583 {
584 assert(i!=NULL);
585 AssertMemory(i);
586 if (IsChildlessInstance(i)) {
587 return 0; /*no children of constants,when,funds*/
588 }
589 switch(i->t) {
590 case SIM_INST: /* only and always, 1 child */
591 return 1;
592 case MODEL_INST:
593 return ChildListLen(GetChildList(MOD_INST(i)->desc));
594 case REAL_ATOM_INST:
595 return ChildListLen(GetChildList(RA_INST(i)->desc));
596 case BOOLEAN_ATOM_INST:
597 return ChildListLen(GetChildList(BA_INST(i)->desc));
598 case INTEGER_ATOM_INST:
599 return ChildListLen(GetChildList(IA_INST(i)->desc));
600 case SET_ATOM_INST:
601 return ChildListLen(GetChildList(SA_INST(i)->desc));
602 case SYMBOL_ATOM_INST:
603 return ChildListLen(GetChildList(SYMA_INST(i)->desc));
604 case REL_INST:
605 return ChildListLen(GetChildList(RELN_INST(i)->desc));
606 case LREL_INST:
607 return ChildListLen(GetChildList(LRELN_INST(i)->desc));
608 case ARRAY_INT_INST:
609 case ARRAY_ENUM_INST:
610 if (ARY_INST(i)->children!=NULL)
611 return gl_length(ARY_INST(i)->children);
612 break;
613 case DUMMY_INST:
614 return 0;
615 default:
616 Asc_Panic(2, NULL, "Invalid argument to NumberChildren.\n");
617 /*NOTREACHED*/
618 }
619 return 0;
620 }
621
622 #define OLDICHILD 0
623 #if OLDICHILD
624 struct Instance *InstanceChild(CONST struct Instance *i,
625 unsigned long int n)
626 {
627 register struct ArrayChild *ptr;
628 register struct Instance **child;
629 assert((i!=NULL) && (n>0) && (n<=NumberChildren(i)));
630 AssertMemory(i);
631 switch(i->t) {
632 case ARRAY_INT_INST:
633 case ARRAY_ENUM_INST:
634 if (ARY_INST(i)->children!=NULL){
635 ptr = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
636 AssertContainedMemory(ptr,sizeof(struct ArrayChild));
637 return ptr->inst;
638 }
639 else return NULL;
640 case SIM_INST:
641 child = SIM_CHILD(i,n-1);
642 AssertContainedIn(i,child);
643 return *child;
644 case MODEL_INST:
645 child = MOD_CHILD(i,n-1);
646 AssertContainedIn(i,child);
647 return *child;
648 case REAL_ATOM_INST:
649 child = RA_CHILD(i,n-1);
650 AssertContainedIn(i,child);
651 return *child;
652 case BOOLEAN_ATOM_INST:
653 child = BA_CHILD(i,n-1);
654 AssertContainedIn(i,child);
655 return *child;
656 case INTEGER_ATOM_INST:
657 child = IA_CHILD(i,n-1);
658 AssertContainedIn(i,child);
659 return *child;
660 case SET_ATOM_INST:
661 child = SA_CHILD(i,n-1);
662 AssertContainedIn(i,child);
663 return *child;
664 case SYMBOL_ATOM_INST:
665 child = SYMA_CHILD(i,n-1);
666 AssertContainedIn(i,child);
667 return *child;
668 case REL_INST:
669 child = REL_CHILD(i,n-1);
670 AssertContainedIn(i,child);
671 return *child;
672 case LREL_INST:
673 child = LREL_CHILD(i,n-1);
674 AssertContainedIn(i,child);
675 return *child;
676 /* WHEN instances don't have any children */
677 case WHEN_INST:
678 Asc_Panic(2, "InstanceChild",
679 "InstanceChild shouldn't be called on WHEN instances\n");
680 /* fundamental atoms don't have any children */
681 case REAL_INST:
682 case INTEGER_INST:
683 case BOOLEAN_INST:
684 case SET_INST:
685 case SYMBOL_INST:
686 case REAL_CONSTANT_INST:
687 case INTEGER_CONSTANT_INST:
688 case BOOLEAN_CONSTANT_INST:
689 case SYMBOL_CONSTANT_INST:
690 case DUMMY_INST:
691 Asc_Panic(2, NULL,
692 "InstanceChild shouldn't be called"
693 " on fundamental atoms, constants.\n");
694 default:
695 Asc_Panic(2, NULL, "Invalid argument to InstanceChild.\n");
696 }
697 return NULL;
698 }
699 #else /* OLDICHILD */
700 /*
701 * we want to handle models, arrays, reals and rels very fast
702 * the rest we will be slow about.
703 */
704 struct Instance *InstanceChild(CONST struct Instance *i,
705 unsigned long int n)
706 {
707 #ifndef NDEBUG
708 register struct ArrayChild *ptr;
709 register struct Instance **child;
710 assert((i!=NULL) && (n>0) && (n<=NumberChildren(i)));
711 AssertMemory(i);
712 if (i->t & (IERRINST | ICHILDLESS)) {
713 Asc_Panic(2, NULL,
714 "InstanceChild called with corrupt/childless instance.\n");
715 }
716 #endif
717 if (i->t & (IERRINST | ICHILDLESS)) return NULL;
718 if (IsCompoundInstance(i)) { /* ary, model, sim */
719 if (IsArrayInstance(i)) {
720 if (ARY_INST(i)->children!=NULL){
721 #ifdef NDEBUG
722 return ((struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n))->inst;
723 #else
724 ptr = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
725 AssertContainedMemory(ptr,sizeof(struct ArrayChild));
726 return ptr->inst;
727 #endif
728 } else {
729 return NULL;
730 }
731 } else { /* model, sim */
732 if ( i->t == MODEL_INST) {
733 #ifdef NDEBUG
734 return *MOD_CHILD(i,n-1);
735 #else
736 child = MOD_CHILD(i,n-1);
737 AssertContainedIn(i,child);
738 return *child;
739 #endif
740 } else { /* sim */
741 #ifdef NDEBUG
742 return *SIM_CHILD(i,n-1);
743 #else
744 child = SIM_CHILD(i,n-1);
745 AssertContainedIn(i,child);
746 return *child;
747 #endif
748 }
749 }
750 #ifndef NDEBUG
751 Asc_Panic(2, NULL, "InstanceChild found unexpected compound instance.\n");
752 #endif
753 } else { /* not compound */
754 if (i->t == REAL_ATOM_INST) {
755 #ifdef NDEBUG
756 return *RA_CHILD(i,n-1);
757 #else
758 child = RA_CHILD(i,n-1);
759 AssertContainedIn(i,child);
760 return *child;
761 #endif
762 }
763 if (i->t == REL_INST) {
764 #ifdef NDEBUG
765 return *REL_CHILD(i,n-1);
766 #else
767 child = REL_CHILD(i,n-1);
768 AssertContainedIn(i,child);
769 return *child;
770 #endif
771 }
772 if (i->t == LREL_INST) {
773 #ifdef NDEBUG
774 return *LREL_CHILD(i,n-1);
775 #else
776 child = LREL_CHILD(i,n-1);
777 AssertContainedIn(i,child);
778 return *child;
779 #endif
780 }
781 if (i->t == INTEGER_ATOM_INST) {
782 #ifdef NDEBUG
783 return *IA_CHILD(i,n-1);
784 #else
785 child = IA_CHILD(i,n-1);
786 AssertContainedIn(i,child);
787 return *child;
788 #endif
789 }
790 if (i->t == BOOLEAN_ATOM_INST) {
791 #ifdef NDEBUG
792 return *BA_CHILD(i,n-1);
793 #else
794 child = BA_CHILD(i,n-1);
795 AssertContainedIn(i,child);
796 return *child;
797 #endif
798 }
799 if (i->t == SET_ATOM_INST) {
800 #ifdef NDEBUG
801 return *SA_CHILD(i,n-1);
802 #else
803 child = SA_CHILD(i,n-1);
804 AssertContainedIn(i,child);
805 return *child;
806 #endif
807 }
808 if (i->t == SYMBOL_ATOM_INST) {
809 #ifdef NDEBUG
810 return *SYMA_CHILD(i,n-1);
811 #else
812 child = SYMA_CHILD(i,n-1);
813 AssertContainedIn(i,child);
814 return *child;
815 #endif
816 }
817 }
818 /* constants, whens, fundamental atoms don't have any children */
819 /* NOTREACHED */
820 return NULL;
821 }
822 #endif /* OLDICHILD */
823
824 struct InstanceName ChildName(CONST struct Instance *i, unsigned long int n)
825 {
826 register struct ArrayChild *arychild;
827 struct InstanceName result;
828 assert((i!=NULL)&&(n>0)&&(n<=NumberChildren(i)));
829 AssertMemory(i);
830 switch(i->t) {
831 case SIM_INST:
832 SetInstanceNameType(result,StrName);
833 SetInstanceNameStrPtr(result,SIM_INST(i)->name);
834 break;
835 case MODEL_INST:
836 SetInstanceNameType(result,StrName);
837 SetInstanceNameStrPtr(result,
838 ChildStrPtr(GetChildList(MOD_INST(i)->desc),n));
839 break;
840 case REAL_ATOM_INST:
841 SetInstanceNameType(result,StrName);
842 SetInstanceNameStrPtr(result,
843 ChildStrPtr(GetChildList(RA_INST(i)->desc),n));
844 break;
845 case BOOLEAN_ATOM_INST:
846 SetInstanceNameType(result,StrName);
847 SetInstanceNameStrPtr(result,
848 ChildStrPtr(GetChildList(BA_INST(i)->desc),n));
849 break;
850 case INTEGER_ATOM_INST:
851 SetInstanceNameType(result,StrName);
852 SetInstanceNameStrPtr(result,
853 ChildStrPtr(GetChildList(IA_INST(i)->desc),n));
854 break;
855 case SET_ATOM_INST:
856 SetInstanceNameType(result,StrName);
857 SetInstanceNameStrPtr(result,
858 ChildStrPtr(GetChildList(SA_INST(i)->desc),n));
859 break;
860 case SYMBOL_ATOM_INST:
861 SetInstanceNameType(result,StrName);
862 SetInstanceNameStrPtr(result,
863 ChildStrPtr(GetChildList(SYMA_INST(i)->desc),n));
864 break;
865 case REL_INST:
866 SetInstanceNameType(result,StrName);
867 SetInstanceNameStrPtr(result,
868 ChildStrPtr(GetChildList(RELN_INST(i)->desc),n));
869 break;
870 case LREL_INST:
871 SetInstanceNameType(result,StrName);
872 SetInstanceNameStrPtr(result,
873 ChildStrPtr(GetChildList(LRELN_INST(i)->desc),n));
874 break;
875 case ARRAY_INT_INST:
876 SetInstanceNameType(result,IntArrayIndex);
877 arychild = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
878 SetInstanceNameIntIndex(result,arychild->name.index);
879 break;
880 case ARRAY_ENUM_INST:
881 SetInstanceNameType(result,StrArrayIndex);
882 arychild = (struct ArrayChild *)gl_fetch(ARY_INST(i)->children,n);
883 SetInstanceNameStrIndex(result,arychild->name.str);
884 break;
885 /* WHEN instances don't have any children */
886 case WHEN_INST:
887 Asc_Panic(2, NULL, "ChildName shouldn't be called on WHEN instances\n");
888 /* fundamental, constant instances don't have children */
889 case REAL_INST:
890 case INTEGER_INST:
891 case BOOLEAN_INST:
892 case SET_INST:
893 case SYMBOL_INST:
894 case REAL_CONSTANT_INST:
895 case INTEGER_CONSTANT_INST:
896 case BOOLEAN_CONSTANT_INST:
897 case SYMBOL_CONSTANT_INST:
898 case DUMMY_INST:
899 Asc_Panic(2, NULL,
900 "ChildName should not be called"
901 " on fundamentaa/const instances.\n");
902 exit(2); /*NOTREACHED*/
903 default:
904 Asc_Panic(2, NULL, "Invalid argument passed to ChildName.\n");
905 exit(2); /*NOTREACHED*/
906 }
907 return result;
908 }
909
910 CONST struct TypeDescription *ChildRefines(CONST struct Instance *i,
911 unsigned long n)
912 {
913 struct TypeDescription *d;
914 ChildListPtr cl;
915 if (i==NULL) return NULL;
916 if (n==0||n>NumberChildren(i)) return NULL;
917 d = InstanceTypeDesc(i);
918 cl = GetChildList(d);
919 return ChildBaseTypePtr(cl,n);
920 }
921
922 CONST struct Statement *ChildDeclaration(CONST struct Instance *i,
923 unsigned long n)
924 {
925 struct TypeDescription *d;
926 ChildListPtr cl;
927 if (i==NULL) return NULL;
928 if (n==0||n>NumberChildren(i)) return NULL;
929 d = InstanceTypeDesc(i);
930 cl = GetChildList(d);
931 return ChildStatement(cl,n);
932 }
933
934 unsigned long ChildSearch(CONST struct Instance *i,
935 CONST struct InstanceName *name)
936 {
937 struct ArrayChild arychild;
938 assert((i!=NULL)&&(name!=NULL));
939 AssertMemory(i);
940 switch(i->t) {
941 case SIM_INST:
942 if (InstanceNameType(*name)==StrName)
943 if (SIM_INST(i)->name == InstanceNameStr(*name))
944 return 1;
945 break;
946 case MODEL_INST:
947 if (InstanceNameType(*name)==StrName)
948 return ChildPos(GetChildList(MOD_INST(i)->desc),InstanceNameStr(*name));
949 break;
950 case REAL_ATOM_INST:
951 if (InstanceNameType(*name)==StrName)
952 return ChildPos(GetChildList(RA_INST(i)->desc),InstanceNameStr(*name));
953 break;
954 case BOOLEAN_ATOM_INST:
955 if (InstanceNameType(*name)==StrName)
956 return ChildPos(GetChildList(BA_INST(i)->desc),InstanceNameStr(*name));
957 break;
958 case INTEGER_ATOM_INST:
959 if (InstanceNameType(*name)==StrName)
960 return ChildPos(GetChildList(IA_INST(i)->desc),InstanceNameStr(*name));
961 break;
962 case SET_ATOM_INST:
963 if (InstanceNameType(*name)==StrName)
964 return ChildPos(GetChildList(SA_INST(i)->desc),InstanceNameStr(*name));
965 break;
966 case SYMBOL_ATOM_INST:
967 if (InstanceNameType(*name)==StrName)
968 return ChildPos(GetChildList(SYMA_INST(i)->desc),InstanceNameStr(*name));
969 case REL_INST:
970 if (InstanceNameType(*name)==StrName)
971 return ChildPos(GetChildList(RELN_INST(i)->desc),InstanceNameStr(*name));
972 break;
973 case LREL_INST:
974 if (InstanceNameType(*name)==StrName)
975 return ChildPos(GetChildList(LRELN_INST(i)->desc),
976 InstanceNameStr(*name));
977 break;
978 case ARRAY_INT_INST:
979 if (ARY_INST(i)->children!=NULL){
980 if (InstanceNameType(*name)==IntArrayIndex) {
981 arychild.inst = NULL;
982 arychild.name.index = InstanceIntIndex(*name);
983 return gl_search(ARY_INST(i)->children,(char *)&arychild,
984 (CmpFunc)CmpIntIndex);
985 }
986 }
987 break;
988 case ARRAY_ENUM_INST:
989 if (ARY_INST(i)->children!=NULL){
990 if (InstanceNameType(*name)==StrArrayIndex) {
991 arychild.inst = NULL;
992 arychild.name.str = InstanceStrIndex(*name);
993 return gl_search(ARY_INST(i)->children,(char *)&arychild,
994 (CmpFunc)CmpStrIndex);
995 }
996 }
997 break;
998 /* WHEN instances don't have any children */
999 case WHEN_INST:
1000 FPRINTF(ASCERR,
1001 "ChildSearch shouldn't be called on WHEN instances\n");
1002 break;
1003 case REAL_CONSTANT_INST:
1004 case INTEGER_CONSTANT_INST:
1005 case BOOLEAN_CONSTANT_INST:
1006 case SYMBOL_CONSTANT_INST:
1007 case REAL_INST:
1008 case INTEGER_INST:
1009 case BOOLEAN_INST:
1010 case SET_INST:
1011 case SYMBOL_INST:
1012 case DUMMY_INST:
1013 break;
1014 default:
1015 Asc_Panic(2, NULL, "Invalid argument passed to ChildSearch.\n");
1016 }
1017 return 0;
1018 }
1019
1020 unsigned long ChildIndex(CONST struct Instance *i,
1021 CONST struct Instance *child)
1022 {
1023 register unsigned long count=1, num_children=0;
1024 register struct Instance **childlist = NULL;
1025 register ChildListPtr clist;
1026 register struct gl_list_t *arylist;
1027 register struct ArrayChild *arychild;
1028
1029 assert((i!=NULL) && (child!=NULL));
1030 AssertMemory(i);
1031 if (IsChildlessInstance(i)) return 0; /* no kids of fundies, whens, consts */
1032 AssertMemory(child);
1033 switch(i->t) {
1034 case SIM_INST:
1035 childlist = SIM_CHILD(i,0);
1036 if (*childlist==child) return 1;
1037 return 0;
1038 case MODEL_INST:
1039 childlist = MOD_CHILD(i,0);
1040 num_children = ChildListLen(clist = GetChildList(MOD_INST(i)->desc));
1041 break;
1042 case REAL_ATOM_INST:
1043 childlist = RA_CHILD(i,0);
1044 num_children = ChildListLen(clist = GetChildList(RA_INST(i)->desc));
1045 break;
1046 case ARRAY_INT_INST:
1047 case ARRAY_ENUM_INST:
1048 if (ARY_INST(i)->children!=NULL){
1049 for(count=gl_length(arylist = ARY_INST(i)->children);count>0;count--) {
1050 arychild = (struct ArrayChild *)gl_fetch(arylist,count);
1051 if (arychild->inst == child) return count;
1052 }
1053 }
1054 break;
1055 case BOOLEAN_ATOM_INST:
1056 childlist = BA_CHILD(i,0);
1057 num_children = ChildListLen(clist = GetChildList(BA_INST(i)->desc));
1058 break;
1059 case INTEGER_ATOM_INST:
1060 childlist = IA_CHILD(i,0);
1061 num_children = ChildListLen(clist = GetChildList(IA_INST(i)->desc));
1062 break;
1063 case SET_ATOM_INST:
1064 childlist = SA_CHILD(i,0);
1065 num_children = ChildListLen(clist = GetChildList(SA_INST(i)->desc));
1066 break;
1067 case SYMBOL_ATOM_INST:
1068 childlist = SYMA_CHILD(i,0);
1069 num_children = ChildListLen(clist = GetChildList(SYMA_INST(i)->desc));
1070 break;
1071 case REL_INST:
1072 childlist = REL_CHILD(i,0);
1073 num_children = ChildListLen(clist = GetChildList(RELN_INST(i)->desc));
1074 break;
1075 case LREL_INST:
1076 childlist = LREL_CHILD(i,0);
1077 num_children = ChildListLen(clist = GetChildList(LRELN_INST(i)->desc));
1078 break;
1079 default:
1080 Asc_Panic(2, NULL, "Invalid argument to ChildIndex.\n");
1081 childlist = 0;
1082 }
1083 while(num_children--) {
1084 if (*(childlist++) == child) return count;
1085 count++;
1086 }
1087 return 0;
1088 }
1089
1090 struct Instance *ChildByChar(CONST struct Instance *inst,symchar *name)
1091 {
1092 unsigned long ndx;
1093 struct InstanceName childname;
1094 assert(name!=NULL);
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