-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathclasses_misc_python.c
More file actions
557 lines (484 loc) · 16.2 KB
/
classes_misc_python.c
File metadata and controls
557 lines (484 loc) · 16.2 KB
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
#define CLASSES_MISC_C
#include <stdlib.h>
#include "classes.h"
/*******************************************************************
** FedEx parser output module for generating C++ class definitions
** December 5, 1989
** release 2 17-Feb-1992
** release 3 March 1993
** release 4 December 1993
** K. C. Morris
**
** Development of FedEx was funded by the United States Government,
** and is not subject to copyright.
*******************************************************************
The conventions used in this binding follow the proposed specification
for the STEP Standard Data Access Interface as defined in document
N350 ( August 31, 1993 ) of ISO 10303 TC184/SC4/WG7.
*******************************************************************/
extern int multiple_inheritance;
/******************************************************************
** The following functions will be used ***
*** throughout the program exp2python ***/
/******************************************************************
** Procedure: CheckWord
** Description: if word is a reserved word, it is capitalized
** Parameters: word -- string to be checked
** Returns: altered word if word is reserved; otherwise the original word
** Side Effects: the word is altered in memory
** Status: started 12/1
******************************************************************/
const char *
CheckWord( const char * word ) {
#ifdef NOT_USING_SDAI_BINDING
/* obsolete with proposed c++ binding */
static char * reserved_words [] = {
"application_marker", "asm", "attributes", "auto",
"break", "case", "char", "class", "const", "continue",
"default", "delete", "do", "double",
"else", "enum", "extern",
"float", "for", "friend", "goto", "handle",
"if", "inline", "instance_id", "int", "long",
"new", "nullable", "opcode", "operator", "overload",
"private", "protected", "public", "register", "return",
"shared", "short", "sizeof", "static", "struct", "switch",
"this", "template", "type", "typedef", "type_name",
"union", "unsigned",
"val", "virtual", "void", "volatile"
};
int nwords = ( sizeof reserved_words / sizeof reserved_words[0] );
int cond,
i,
low = 0,
high = nwords - 1;
/* word is obviously not in list, if it is longer than any of the words in the list */
if( strlen( word ) > 18 ) {
return ( word );
}
while( low <= high ) {
i = ( low + high ) / 2;
if( ( cond = strcmp( word, reserved_words [i] ) ) < 0 ) {
high = i - 1;
} else if( cond > 0 ) {
low = i + 1;
} else { /* word is a reserved word, capitalize it */
fprintf( stderr, "Warning: reserved word %s capitalized\n", word );
*word = toupper( *word );
}
}
#endif
return ( word );
}
/******************************************************************
** Procedure: string functions
** Description: These functions take a character or a string and return
** a temporary copy of the string with the function applied to it.
** Parameters:
** Returns: temporary copy of characters
** Side Effects: character or string returned persists until the
** next invocation of the function
** Status: complete
******************************************************************/
char
ToLower( char c ) {
if( isupper( c ) ) {
return ( tolower( c ) );
} else {
return ( c );
}
}
char
ToUpper( char c ) {
if( islower( c ) ) {
return ( toupper( c ) );
} else {
return ( c );
}
}
const char *
StrToLower( const char * word ) {
static char newword [MAX_LEN+1];
int i = 0;
if( !word ) {
return 0;
}
while( word [i] != '\0' ) {
newword [i] = ToLower( word [i] );
++i;
}
newword [i] = '\0';
return ( newword ) ;
}
const char * StrToUpper( const char * word ) {
static char newword [MAX_LEN+1];
int i = 0;
char ToUpper( char c );
while( word [i] != '\0' ) {
newword [i] = ToUpper( word [i] );
++i;
}
newword [i] = '\0';
return ( newword );
}
const char * StrToConstant( const char * word ) {
static char newword [MAX_LEN+1];
int i = 0;
while( word [i] != '\0' ) {
if( word [i] == '/' || word [i] == '.' ) {
newword [i] = '_';
} else {
newword [i] = ToUpper( word [i] );
}
++i;
}
newword [i] = '\0';
return ( newword );
}
/** creates a file for python */
FILE * FILEcreate( const char * filename ) {
FILE * file;
if( ( file = fopen( filename, "w" ) ) == NULL ) {
fprintf( stderr, "Error in SCHEMAprint: unable to create file %s\n", filename );
return ( NULL );
}
fprintf( file, "# This file was generated by exp2python. You probably don't want to edit\n" );
fprintf( file, "# it since your modifications will be lost if exp2python is used to\n" );
fprintf( file, "# regenerate it.\n" );
return ( file );
}
/** closes a file opened with FILEcreate */
void FILEclose( FILE * file ) {
fclose( file );
}
/** indicates whether the attribute is an aggregate */
int isAggregate( Variable a ) {
return( TYPEinherits_from( VARget_type( a ), aggregate_ ) );
}
/** indicates whether the type is an aggregate type */
int isAggregateType( const Type t ) {
return( TYPEinherits_from( t, aggregate_ ) );
}
/** returns temporary copy of name suitable for use as a class name
*
* each call erases the name created by a previous call to this function
*/
const char * ClassName( const char * oldname ) {
int i = 0, j = 0;
static char newname [BUFSIZ+1];
if( !oldname ) {
return ( "" );
}
strcpy( newname, ENTITYCLASS_PREFIX ) ;
j = strlen( ENTITYCLASS_PREFIX ) ;
newname [j] = ToUpper( oldname [i] );
++i;
++j;
while( oldname [i] != '\0' ) {
newname [j] = ToLower( oldname [i] );
++i;
++j;
}
newname [j] = '\0';
return ( newname );
}
/** returns the name of the c++ class representing the entity */
const char * ENTITYget_classname( Entity ent ) {
const char * oldname = ENTITYget_name( ent );
return ( ClassName( oldname ) );
}
/** returns a new capitalized name, in internal static buffer */
const char * PrettyTmpName( const char * oldname ) {
int i = 0;
static char newname [BUFSIZ+1];
newname [0] = '\0';
while( ( oldname [i] != '\0' ) && ( i < BUFSIZ ) ) {
newname [i] = ToLower( oldname [i] );
if( oldname [i] == '_' ) { /* character is '_' */
++i;
newname [i] = ToUpper( oldname [i] );
}
if( oldname [i] != '\0' ) {
++i;
}
}
newname [0] = ToUpper( oldname [0] );
newname [i] = '\0';
return newname;
}
/** This function is out of date DAS */
const char * EnumName( const char * oldname ) {
int j = 0;
static char newname [MAX_LEN+1];
if( !oldname ) {
return ( "" );
}
strcpy( newname, ENUM_PREFIX ) ;
j = strlen( ENUM_PREFIX ) ;
newname [j] = ToUpper( oldname [0] );
strncpy( newname + j + 1, StrToLower( oldname + 1 ), MAX_LEN - j );
j = strlen( newname );
newname [j] = '\0';
return ( newname );
}
const char * SelectName( const char * oldname ) {
int j = 0;
static char newname [MAX_LEN+1];
if( !oldname ) {
return ( "" );
}
strcpy( newname, TYPE_PREFIX );
newname [0] = ToUpper( newname [0] );
j = strlen( TYPE_PREFIX );
newname [j] = ToUpper( oldname [0] );
strncpy( newname + j + 1, StrToLower( oldname + 1 ), MAX_LEN - j );
j = strlen( newname );
newname [j] = '\0';
return ( newname );
}
const char * FirstToUpper( const char * word ) {
static char newword [MAX_LEN+1];
strncpy( newword, word, MAX_LEN );
newword[0] = ToUpper( newword[0] );
return ( newword );
}
/** return fundamental type but as the string which corresponds to
* the appropriate type descriptor
* if report_reftypes is true, report REFERENCE_TYPE when appropriate
*/
const char * FundamentalType( const Type t, int report_reftypes ) {
if( report_reftypes && TYPEget_head( t ) ) {
return( "REFERENCE_TYPE" );
}
switch( TYPEget_body( t )->type ) {
case integer_:
return( "INTEGER" );
case real_:
return( "REAL" );
case string_:
return( "STRING" );
case binary_:
return( "BINARY" );
case boolean_:
return( "BOOLEAN" );
case logical_:
return( "LOGICAL" );
case number_:
return( "NUMBER" );
case generic_:
return( "GENERIC_TYPE" );
case aggregate_:
return( "AGGREGATE_" );
case array_:
return( "ARRAY_TYPE" );
case bag_:
return( "BAG_TYPE" );
case set_:
return( "'SET_TYPE not implemented'" );
case list_:
return( "'LIST TYPE Not implemented'" );
case entity_:
return( "INSTANCE" );
case enumeration_:
return( "ENUMERATION" );
case select_:
return ( "SELECT" );
default:
return( "UNKNOWN_TYPE" );
}
}
/** this actually gets you the name of the variable that will be generated to
* be a TypeDescriptor or subtype of TypeDescriptor to represent Type t in
* the dictionary.
*/
const char * TypeDescriptorName( Type t ) {
static char b [BUFSIZ+1];
Schema parent = t->superscope;
/* NOTE - I corrected a prev bug here in which the *current* schema was
** passed to this function. Now we take "parent" - the schema in which
** Type t was defined - which was actually used to create t's name. DAR */
if( !parent ) {
parent = TYPEget_body( t )->entity->superscope;
/* This works in certain cases that don't work otherwise (basically a
** kludge). For some reason types which are really entity choices of
** a select have no superscope value, but their super may be tracked
** by following through the entity they reference, as above. */
}
sprintf( b, "%s%s%s", SCHEMAget_name( parent ), TYPEprefix( t ),
TYPEget_name( t ) );
return b;
}
/** this gets you the name of the type of TypeDescriptor (or subtype) that a
* variable generated to represent Type t would be an instance of.
*/
const char * GetTypeDescriptorName( Type t ) {
switch( TYPEget_body( t )->type ) {
case aggregate_:
return "AggrTypeDescriptor";
case list_:
return "ListTypeDescriptor";
case set_:
return "SetTypeDescriptor";
case bag_:
return "BagTypeDescriptor";
case array_:
return "ArrayTypeDescriptor";
case select_:
return "SelectTypeDescriptor";
case boolean_:
case logical_:
case enumeration_:
return "EnumTypeDescriptor";
case entity_:
return "EntityDescriptor";
case integer_:
case real_:
case string_:
case binary_:
case number_:
case generic_:
return "TypeDescriptor";
default:
fprintf( stderr, "Error in %s, line %d: type %d not handled by switch statement.", __FILE__, __LINE__, TYPEget_body( t )->type );
abort();
}
}
int ENTITYhas_explicit_attributes( Entity e ) {
Linked_List l = ENTITYget_attributes( e );
int cnt = 0;
LISTdo( l, a, Variable )
if( VARget_initializer( a ) == EXPRESSION_NULL ) {
++cnt;
}
LISTod;
return cnt;
}
Entity ENTITYput_superclass( Entity entity ) {
#define ENTITYget_type(e) ((e)->u.entity->type)
Linked_List l = ENTITYget_supertypes( entity );
EntityTag tag;
if( ! LISTempty( l ) ) {
Entity super = 0;
if( multiple_inheritance ) {
Linked_List list = 0;
list = ENTITYget_supertypes( entity );
if( ! LISTempty( list ) ) {
/* assign superclass to be the first one on the list of parents */
super = ( Entity )LISTpeek_first( list );
}
} else {
Entity ignore = 0;
int super_cnt = 0;
/* find the first parent that has attributes (in the parent or any of its
ancestors). Make super point at that parent and print warnings for
all the rest of the parents. DAS */
LISTdo( l, e, Entity ) {
/* if there's no super class yet,
or if the entity super class [the variable] super is pointing at
doesn't have any attributes: make super point at the current parent.
As soon as the parent pointed to by super has attributes, stop
assigning super and print ignore messages for the remaining parents.
*/
if( ( ! super ) || ( ! ENTITYhas_explicit_attributes( super ) ) ) {
ignore = super;
super = e;
++ super_cnt;
} else {
ignore = e;
}
if( ignore ) {
printf( "WARNING: multiple inheritance not implemented.\n" );
printf( "\tin ENTITY %s\n\tSUPERTYPE %s IGNORED.\n\n",
ENTITYget_name( entity ), ENTITYget_name( e ) );
}
} LISTod
}
tag = ( EntityTag ) malloc( sizeof( struct EntityTag_ ) );
tag -> superclass = super;
TYPEput_clientData( ENTITYget_type( entity ), tag );
return super;
}
return 0;
}
Entity ENTITYget_superclass( Entity entity ) {
EntityTag tag;
tag = TYPEget_clientData( ENTITYget_type( entity ) );
return ( tag ? tag -> superclass : 0 );
}
void ENTITYget_first_attribs( Entity entity, Linked_List result ) {
Linked_List supers;
LISTdo( ENTITYget_attributes( entity ), attr, void * )
LISTadd_last( result, attr );
LISTod;
supers = ENTITYget_supertypes( entity );
if( supers ) {
ENTITYget_first_attribs( ( Entity )LISTget_first( supers ), result );
}
}
/* Attributes are divided into four categories:
** these are not exclusive as far as I can tell! I added defs below DAS
**
** . simple explicit
** . type shifters // not DERIVEd - redefines type in ancestor
** // VARget_initializer(v) returns null
**
** . simple derived // DERIVEd - is calculated - VARget_initializer(v)
** // returns non-zero, VARis_derived(v) is non-zero
**
** . overriding // includes type shifters and derived
**
** All of them are added to the dictionary.
** Only type shifters generate a new STEPattribute.
** Type shifters generate access functions and data members, for now.
** Overriding generate access functions and data members, for now. ???? DAS
** // type shifting attributes
** // ------------------------
** // before printing new STEPattribute
** // check to see if it\'s already printed in supertype
** // still add new access function
**
** // overriding attributes
** // ---------------------
** // go through derived attributes
** // if STEPattribute found with same name
** // tell it to be * for reading and writing
**/
Variable VARis_type_shifter( Variable a ) {
char * temp;
if( VARis_derived( a ) || VARget_inverse( a ) ) {
return 0;
}
temp = strdup( VARget_name( a )->symbol.name );
if( ! strncmp( StrToLower( temp ), "self\\", 5 ) ) {
/* a is a type shifter */
free( temp );
return a;
}
free( temp );
return 0;
}
Variable VARis_overrider( Entity e, Variable a ) {
Variable other;
char * tmp;
tmp = VARget_simple_name( a );
LISTdo( ENTITYget_supertypes( e ), s, Entity )
if( ( other = ENTITYget_named_attribute( s, tmp ) )
&& other != a ) {
return other;
}
LISTod;
return 0;
}
/**
* For a renamed type, returns the original (ancestor) type from which t
* descends. Return NULL if t is top level.
*/
Type TYPEget_ancestor( Type t ) {
Type i = t;
if( !TYPEget_head( i ) ) {
return NULL;
}
while( TYPEget_head( i ) ) {
i = TYPEget_head( i );
}
return i;
}