/[ascend]/branches/pallav/ascend/compiler/findpath.c
ViewVC logotype

Contents of /branches/pallav/ascend/compiler/findpath.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2823 - (show annotations) (download) (as text)
Tue Feb 17 06:38:36 2015 UTC (4 years, 4 months ago) by jpye
File MIME type: text/x-csrc
File size: 9368 byte(s)
restored files from non-svn backup

1 /*
2 *
3 * This file is part of the Ascend Language Interpreter.
4 * See top of findpath.h
5 *
6 * The Ascend Language Interpreter is free software; you can redistribute
7 * it and/or modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * The Ascend Language Interpreter is distributed in hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include <stdarg.h>
21 #include <math.h>
22 #include <ascend/general/platform.h>
23 #include <ascend/general/ascMalloc.h>
24 #include <ascend/general/panic.h>
25 #include <ascend/general/pool.h>
26 #include <ascend/general/list.h>
27 #include <ascend/general/dstring.h>
28
29 #include "symtab.h"
30
31
32 #include "functype.h"
33 #include "expr_types.h"
34 #include "child.h"
35 #include "type_desc.h"
36 #include "instance_enum.h"
37 #include "instance_name.h"
38 #include "instance_io.h"
39 #include "mathinst.h"
40 #include "name.h"
41 #include "nameio.h"
42 #include "evaluate.h"
43 #include "atomvalue.h"
44 #include "instquery.h"
45 #include "arrayinst.h"
46 #include "parentchild.h"
47 #include "value_type.h"
48 #include "forvars.h"
49 #include "setinstval.h"
50 #include "find.h"
51 #include "findpath.h"
52 #include "safe.h"
53 #include "relation_type.h"
54 #include "relation_util.h"
55 #include "logical_relation.h"
56 #include "logrelation.h"
57 #include "logrel_util.h"
58
59
60 #define NAMELISTSIZE 20L
61 #define DEFTOLERANCE 1e-08
62
63 #ifndef lint
64 static CONST char FindRCSid[]="$Id: find.c,v 1.24 1998/03/26 20:39:44 ballan Exp $";
65 #endif /* lint */
66
67 typedef struct IAndName
68 {
69 CONST struct Instance *i;
70 struct Name *n;
71 } PAN;
72
73 /** store a name and the instance that carries it, with
74 reference to a context which is not defined here. Thus
75 n may be null if i == context.
76 */
77 static
78 PAN *CreatePAN(CONST struct Instance *i, struct Name *n)
79 {
80 PAN *result = (PAN *)ascmalloc(sizeof(PAN));
81 result->i = i;
82 result->n = n;
83 return result;
84 }
85
86 static
87 void DestroyPAN(PAN *p)
88 {
89 p->i = NULL;
90 p->n = NULL;
91 ascfree(p);
92 }
93
94 /* by usage in this file, all n in PAN are allocated for exclusive use. */
95 static
96 void DestroyPANAndName(PAN *p)
97 {
98 p->i = NULL;
99 DestroyName(p->n);
100 p->n = NULL;
101 ascfree(p);
102 }
103
104 static
105 void DestroyPANList(struct gl_list_t **gl)
106 {
107 gl_iterate(*gl,(IterateFunc)DestroyPANAndName);
108 gl_destroy(*gl);
109 *gl = NULL;
110 }
111
112 static struct gl_list_t *FindArrayChildrenPath(struct gl_list_t *list,
113 CONST struct set_t *sptr,
114 enum find_errors *errval)
115 {
116 struct gl_list_t *result;
117 CONST struct Instance *i,*child;
118 struct InstanceName rec;
119 struct TypeDescription *desc;
120 unsigned long c1,len1,c2,len2,pos;
121 PAN *p, *p2;
122 struct Name *n, *n2;
123 symchar *senum;
124 long sint;
125
126 switch(SetKind(sptr)){
127 case empty_set: return gl_create(0);
128 case string_set:
129 SetInstanceNameType(rec,StrArrayIndex);
130 len2 = Cardinality(sptr);
131 len1 = gl_length(list);
132 result = gl_create(len1*len2);
133 for (c1=1; c1<=len1; c1++){
134 p = (PAN *)gl_fetch(list,c1);
135 i = p->i;
136 if (InstanceKind(i)==ARRAY_ENUM_INST){
137 if (NextToExpand(i)!=1){
138 for (c2=1; c2<=len2; c2++){
139 senum = FetchStrMember(sptr,c2);
140 SetInstanceNameStrIndex(rec,senum);
141 if ((pos = ChildSearch(i,&rec))==0){
142 DestroyPANList(&result);
143 desc = InstanceTypeDesc(i);
144 if ( GetArrayBaseIsRelation(desc) || GetArrayBaseIsLogRel(desc)){
145 *errval = unmade_instance;
146 } else {
147 *errval = impossible_instance;
148 }
149 return NULL;
150 } else {
151 child = InstanceChild(i,pos);
152 if (child!=NULL){
153 n = CreateEnumElementName(senum);
154 n2 = CopyAppendNameNode(p->n, n);
155 DestroyName(n);
156 p2 = CreatePAN(child, n2);
157 gl_append_ptr(result,(VOIDPTR)p2);
158 } else {
159 DestroyPANList(&result);
160 *errval = unmade_instance;
161 return NULL;
162 }
163 }
164 }
165 } else {
166 DestroyPANList(&result);
167 *errval = unmade_instance;
168 return NULL;
169 }
170 } else {
171 DestroyPANList(&result);
172 *errval = impossible_instance;
173 return NULL;
174 }
175 }
176 return result;
177 case integer_set:
178 SetInstanceNameType(rec,IntArrayIndex);
179 len2 = Cardinality(sptr);
180 len1 = gl_length(list);
181 result = gl_create(len1*len2);
182 for (c1=1; c1<=len1; c1++){
183 p = (PAN *)gl_fetch(list,c1);
184 i = p->i;
185 if (InstanceKind(i)==ARRAY_INT_INST){
186 if (NextToExpand(i)!=1){
187 for (c2=1; c2<=len2; c2++){
188 sint = FetchIntMember(sptr,c2);
189 SetInstanceNameIntIndex(rec,sint);
190 if ((pos = ChildSearch(i,&rec))==0){
191 DestroyPANList(&result);
192 desc = InstanceTypeDesc(i);
193 if (GetArrayBaseIsRelation(desc) || GetArrayBaseIsLogRel(desc)) {
194 *errval = unmade_instance;
195 } else {
196 *errval = impossible_instance;
197 }
198 return NULL;
199 } else {
200 child = InstanceChild(i,pos);
201 if (child!=NULL){
202 n = CreateIntegerElementName(sint);
203 n2 = CopyAppendNameNode(p->n, n);
204 DestroyName(n);
205 p2 = CreatePAN(child, n2);
206 gl_append_ptr(result,(VOIDPTR)p2);
207 } else{
208 DestroyPANList(&result);
209 *errval = unmade_instance;
210 return NULL;
211 }
212 }
213 }
214 } else {
215 DestroyPANList(&result);
216 *errval = unmade_instance;
217 return NULL;
218 }
219 } else {
220 DestroyPANList(&result);
221 *errval = impossible_instance;
222 return NULL;
223 }
224 }
225 return result;
226 }
227 /*NOTREACHED*/
228 return NULL;
229 }
230
231 static
232 struct gl_list_t *FindNextNameElementPath(CONST struct Name *n,
233 struct gl_list_t *list,
234 enum find_errors *errval)
235 {
236 unsigned long pos,c,len;
237 struct InstanceName rec;
238 CONST struct Instance *current,*child;
239 struct value_t setvalue,oldvalue;
240 CONST struct Set *sptr;
241 struct gl_list_t *result;
242 PAN *p, *p2;
243 struct Name *n2;
244
245 *errval = correct_instance;
246 if (NameId(n)){
247 result = gl_create(NAMELISTSIZE);
248 SetInstanceNameType(rec,StrName);
249 SetInstanceNameStrPtr(rec,NameIdPtr(n));
250 len = gl_length(list);
251 for (c=1; c<=len; c++){
252 p = (PAN *)gl_fetch(list,c);
253 current = p->i;
254 pos = ChildSearch(current,&rec);
255 if (pos!=0){
256 child = InstanceChild(current,pos);
257 if (child!=NULL){
258 n2 = CopyAppendNameNode(p->n, n);
259 p2 = CreatePAN(child, n2);
260 gl_append_ptr(result,(VOIDPTR)p2);
261 } else{
262 *errval = unmade_instance;
263 DestroyPANList(&result);
264 return NULL;
265 }
266 } else{
267 *errval = unmade_instance;
268 /* it would seem this ought to be undefined_instance,
269 * but maybe refinement causes insanity. -- in which case
270 * it should be a caller policy to wait, rather than our
271 * job to anticipate policy and short circuit things here.
272 */
273 DestroyPANList(&result);
274 return NULL;
275 }
276 }
277 return result;
278 } else {
279 sptr = NameSetPtr(n);
280 setvalue = EvaluateSet(sptr,InstanceEvaluateName);
281 switch(ValueKind(setvalue)){
282 case integer_value:
283 case symbol_value:
284 case list_value:
285 oldvalue = setvalue;
286 if (ListMode) {
287 setvalue = CreateOrderedSetFromList(oldvalue);
288 } else {
289 setvalue = CreateSetFromList(oldvalue);
290 }
291 DestroyValue(&oldvalue);
292 /* intended to fall through to next case */
293 case set_value:
294 result = FindArrayChildrenPath(list,SetValue(setvalue),errval);
295 DestroyValue(&setvalue);
296 return result;
297 case error_value:
298 switch(ErrorValue(setvalue)){
299 case illegal_set_use:
300 *errval = impossible_instance;
301 break;
302 default:
303 *errval = undefined_instance;
304 break;
305 /* more needs to be added here */
306 }
307 DestroyValue(&setvalue);
308 return NULL;
309 default:
310 ASC_PANIC("Need to add to FindNextNameElementPath.\n");
311 exit(2);/* Needed to keep gcc from whining */
312 }
313 }
314 }
315
316
317 static
318 struct gl_list_t *RealFindInstancesPath(CONST struct Instance *i,
319 CONST struct Name *n,
320 enum find_errors *errval)
321 {
322 struct gl_list_t *result,*next;
323 PAN *p;
324
325 result = gl_create(NAMELISTSIZE);
326 p = CreatePAN(i,NULL); /* in context of i, i has no name. */
327 gl_append_ptr(result,(VOIDPTR)p);
328 while(n!=NULL){
329 next = FindNextNameElementPath(n,result,errval);
330 DestroyPANList(&result);
331 if (next!=NULL){
332 result = next;
333 n = NextName(n);
334 } else {
335 return NULL;
336 }
337 }
338 return result;
339 }
340
341 struct gl_list_t *FindInstancesPaths(CONST struct Instance *i,
342 CONST struct Name *n,
343 enum find_errors *errval)
344 {
345 struct gl_list_t *result;
346 unsigned long k, len;
347 PAN *p;
348 struct Name *n2;
349 *errval = impossible_instance;
350 if (i == NULL) return NULL;
351 AssertMemory(i);
352 assert(GetEvaluationContext()==NULL);
353 SetEvaluationContext(i);
354 *errval = correct_instance;
355 result = RealFindInstancesPath(i,n,errval);
356 SetEvaluationContext(NULL);
357 if (result != NULL)
358 {
359 /* convert list of pairs to list of names. */
360 len = gl_length(result);
361 for (k= 1 ; k <= len; k++) {
362 p = (PAN*)gl_fetch(result,k);
363 n2 = p->n;
364 gl_store(result,k,n2);
365 DestroyPAN(p);
366 }
367 }
368 return result;
369 }

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