-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathtypeDescriptor.cc
More file actions
427 lines (388 loc) · 12.5 KB
/
typeDescriptor.cc
File metadata and controls
427 lines (388 loc) · 12.5 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
#include "clstepcore/typeDescriptor.h"
TypeDescriptor::TypeDescriptor( )
: _name( 0 ), altNames( 0 ), _fundamentalType( UNKNOWN_TYPE ),
_originatingSchema( 0 ), _referentType( 0 ), _description( 0 ), _where_rules( 0 ) {
}
TypeDescriptor::TypeDescriptor
( const char * nm, PrimitiveType ft, Schema * origSchema,
const char * d )
: _name( nm ), altNames( 0 ), _fundamentalType( ft ),
_originatingSchema( origSchema ), _referentType( 0 ), _description( d ),
_where_rules( 0 ) {
}
TypeDescriptor::~TypeDescriptor() {
if( _where_rules ) {
delete _where_rules;
}
}
/**
* Determines the current name of this. Normally, this is simply _name.
* If "schnm" is set to a value, however, then this function becomes a
* request for our name when referenced by schnm. (This will be diff from
* our original name if schnm USEs or REFERENCEs us and renames us (e.g.,
* "USE (xx as yy)").) In such a case, this function searches through our
* altNames list to see if schema schnm refers to us with a different name
* and returns the new name if found. (See header comments to function
* SchRename::rename().)
*/
const char * TypeDescriptor::Name( const char * schnm ) const {
if( schnm == NULL ) {
return _name;
}
if( altNames && altNames->rename( schnm, ( char * )_altname ) ) {
// If our altNames list has an alternate for schnm, copy it into
// _altname, and return it:
return _altname;
}
return _name;
}
const char * TypeDescriptor::BaseTypeName() const {
return BaseTypeDescriptor() ? BaseTypeDescriptor() -> Name() : 0;
}
const TypeDescriptor * TypeDescriptor::BaseTypeIsA( const TypeDescriptor * td ) const {
switch( NonRefType() ) {
case AGGREGATE_TYPE:
return AggrElemTypeDescriptor() -> IsA( td );
case ENTITY_TYPE:
case SELECT_TYPE:
default:
return IsA( td );
}
}
/**
* Check if our "current" name = other. CurrName may be different from
* _name if schNm tells us the current schema is a different one from the
* one in which we're defined. If so, we may have an alternate name for
* that schema (it may be listed in our altNames list). This would be the
* case if schNm USEs or REFERENCEs type and renames it in the process
* (e.g., "USE (X as Y)".
*/
bool TypeDescriptor::CurrName( const char * other, const char * schNm ) const {
if( !schNm || *schNm == '\0' ) {
// If there's no current schema, accept any possible name of this.
// (I.e., accept its actual name or any substitute):
return ( PossName( other ) );
}
if( altNames && altNames->rename( schNm, ( char * )_altname ) ) {
// If we have a different name when the current schema = schNm, then
// other better = the alt name.
return ( !StrCmpIns( _altname, other ) );
} else {
// If we have no designated alternate name when the current schema =
// schNm, other must = our _name.
return ( OurName( other ) );
}
}
/**
* return true if nm is either our name or one of the possible alternates.
*/
bool TypeDescriptor::PossName( const char * nm ) const {
return ( OurName( nm ) || AltName( nm ) );
}
bool TypeDescriptor::OurName( const char * nm ) const {
return !StrCmpIns( nm, _name );
}
bool TypeDescriptor::AltName( const char * nm ) const {
if( altNames ) {
return ( altNames->choice( nm ) );
}
return false;
}
/**
* Creates a SchRename consisting of schnm & newnm. Places it in alphabe-
* tical order in this's altNames list.
*/
void TypeDescriptor::addAltName( const char * schnm, const char * newnm ) {
SchRename * newpair = new SchRename( schnm, newnm ),
*node = ( SchRename * )altNames, *prev = NULL;
while( node && *node < *newpair ) {
prev = node;
node = node->next;
}
newpair->next = node; // node may = NULL
if( prev ) {
// Will be the case if new node should not be first (and above while
// loop was entered).
prev->next = newpair;
} else {
// put newpair first
altNames = newpair;
}
}
void TypeDescriptor::AttrTypeName( std::string & buf, const char * schnm ) const {
const char * sn = Name( schnm );
if( sn ) {
StrToLower( sn , buf );
} else {
buf = _description;
}
}
const char * TypeDescriptor::GenerateExpress( std::string & buf ) const {
char tmp[BUFSIZ+1];
buf = "TYPE ";
buf.append( StrToLower( Name(), tmp ) );
buf.append( " = " );
const char * desc = Description();
const char * ptr = desc;
while( *ptr != '\0' ) {
if( *ptr == ',' ) {
buf.append( ",\n " );
} else if( *ptr == '(' ) {
buf.append( "\n (" );
} else if( isupper( *ptr ) ) {
buf += ( char )tolower( *ptr );
} else {
buf += *ptr;
}
ptr++;
}
buf.append( ";\n" );
///////////////
// count is # of WHERE rules
if( _where_rules != 0 ) {
int all_comments = 1;
int count = _where_rules->Count();
for( int i = 0; i < count; i++ ) { // print out each UNIQUE rule
if( !( *( _where_rules ) )[i]->_label.size() ) {
all_comments = 0;
}
}
if( all_comments ) {
buf.append( " (* WHERE *)\n" );
} else {
buf.append( " WHERE\n" );
}
for( int i = 0; i < count; i++ ) { // print out each WHERE rule
if( !( *( _where_rules ) )[i]->_comment.empty() ) {
buf.append( " " );
buf.append( ( *( _where_rules ) )[i]->comment_() );
}
if( ( *( _where_rules ) )[i]->_label.size() ) {
buf.append( " " );
buf.append( ( *( _where_rules ) )[i]->label_() );
}
}
}
buf.append( "END_TYPE;\n" );
return const_cast<char *>( buf.c_str() );
}
/**
* This is a fully expanded description of the type.
* This returns a string like the _description member variable
* except it is more thorough of a description where possible
* e.g. if the description contains a TYPE name it will also
* be explained.
*/
const char * TypeDescriptor::TypeString( std::string & s ) const {
switch( Type() ) {
case REFERENCE_TYPE:
if( Name() ) {
s.append( "TYPE " );
s.append( Name() );
s.append( " = " );
}
if( Description() ) {
s.append( Description() );
}
if( ReferentType() ) {
s.append( " -- " );
std::string tmp;
s.append( ReferentType()->TypeString( tmp ) );
}
return const_cast<char *>( s.c_str() );
case INTEGER_TYPE:
s.clear();
if( _referentType != 0 ) {
s = "TYPE ";
s.append( Name() );
s.append( " = " );
}
s.append( "Integer" );
break;
case STRING_TYPE:
s.clear();
if( _referentType != 0 ) {
s = "TYPE ";
s.append( Name() );
s.append( " = " );
}
s.append( "String" );
break;
case REAL_TYPE:
s.clear();
if( _referentType != 0 ) {
s = "TYPE ";
s.append( Name() );
s.append( " = " );
}
s.append( "Real" );
break;
case ENUM_TYPE:
s = "Enumeration: ";
if( Name() ) {
s.append( "TYPE " );
s.append( Name() );
s.append( " = " );
}
if( Description() ) {
s.append( Description() );
}
break;
case BOOLEAN_TYPE:
s.clear();
if( _referentType != 0 ) {
s = "TYPE ";
s.append( Name() );
s.append( " = " );
}
s.append( "Boolean: F, T" );
break;
case LOGICAL_TYPE:
s.clear();
if( _referentType != 0 ) {
s = "TYPE ";
s.append( Name() );
s.append( " = " );
}
s.append( "Logical: F, T, U" );
break;
case NUMBER_TYPE:
s.clear();
if( _referentType != 0 ) {
s = "TYPE ";
s.append( Name() );
s.append( " = " );
}
s.append( "Number" );
break;
case BINARY_TYPE:
s.clear();
if( _referentType != 0 ) {
s = "TYPE ";
s.append( Name() );
s.append( " = " );
}
s.append( "Binary" );
break;
case ENTITY_TYPE:
s = "Entity: ";
if( Name() ) {
s.append( Name() );
}
break;
case AGGREGATE_TYPE:
case ARRAY_TYPE: // DAS
case BAG_TYPE: // DAS
case SET_TYPE: // DAS
case LIST_TYPE: // DAS
s = Description();
if( ReferentType() ) {
s.append( " -- " );
std::string tmp;
s.append( ReferentType()->TypeString( tmp ) );
}
break;
case SELECT_TYPE:
s.append( Description() );
break;
case GENERIC_TYPE:
case UNKNOWN_TYPE:
s = "Unknown";
break;
} // end switch
return const_cast<char *>( s.c_str() );
}
const TypeDescriptor * TypeDescriptor::IsA( const TypeDescriptor * other ) const {
if( this == other ) {
return other;
}
return 0;
}
const TypeDescriptor * TypeDescriptor::IsA( const char * other ) const {
if( !Name() ) {
return 0;
}
if( !StrCmpIns( _name, other ) ) { // this is the type
return this;
}
return ( ReferentType() ? ReferentType() -> IsA( other ) : 0 );
}
/**
* the first PrimitiveType that is not REFERENCE_TYPE (the first
* TypeDescriptor *_referentType that does not have REFERENCE_TYPE
* for it's fundamentalType variable). This would return the same
* as BaseType() for fundamental types. An aggregate type
* would return AGGREGATE_TYPE then you could find out the type of
* an element by calling AggrElemType(). Select types
* would work the same?
*/
PrimitiveType TypeDescriptor::NonRefType() const {
const TypeDescriptor * td = NonRefTypeDescriptor();
if( td ) {
return td->FundamentalType();
}
return UNKNOWN_TYPE;
}
const TypeDescriptor * TypeDescriptor::NonRefTypeDescriptor() const {
const TypeDescriptor * td = this;
while( td->ReferentType() ) {
if( td->Type() != REFERENCE_TYPE ) {
return td;
}
td = td->ReferentType();
}
return td;
}
/// This returns the PrimitiveType of the first non-aggregate element of an aggregate
int TypeDescriptor::IsAggrType() const {
switch( NonRefType() ) {
case AGGREGATE_TYPE:
case ARRAY_TYPE: // DAS
case BAG_TYPE: // DAS
case SET_TYPE: // DAS
case LIST_TYPE: // DAS
return 1;
default:
return 0;
}
}
PrimitiveType TypeDescriptor::AggrElemType() const {
const TypeDescriptor * aggrElemTD = AggrElemTypeDescriptor();
if( aggrElemTD ) {
return aggrElemTD->Type();
}
return UNKNOWN_TYPE;
}
const TypeDescriptor * TypeDescriptor::AggrElemTypeDescriptor() const {
const TypeDescriptor * aggrTD = NonRefTypeDescriptor();
const TypeDescriptor * aggrElemTD = aggrTD->ReferentType();
if( aggrElemTD ) {
aggrElemTD = aggrElemTD->NonRefTypeDescriptor();
}
return aggrElemTD;
}
/**
* This is the underlying type of this type. For instance:
* TYPE count = INTEGER;
* TYPE ref_count = count;
* TYPE count_set = SET OF ref_count;
* each of the above will generate a TypeDescriptor and for
* each one, PrimitiveType BaseType() will return INTEGER_TYPE
* TypeDescriptor *BaseTypeDescriptor() returns the TypeDescriptor
* for Integer
*/
PrimitiveType TypeDescriptor::BaseType() const {
const TypeDescriptor * td = BaseTypeDescriptor();
if( td ) {
return td->FundamentalType();
} else {
return ENTITY_TYPE;
}
}
const TypeDescriptor * TypeDescriptor::BaseTypeDescriptor() const {
const TypeDescriptor * td = this;
while( td -> ReferentType() ) {
td = td->ReferentType();
}
return td;
}