Debian Codegraph @

gcc/cobol/symbols.h

raw
1 /*
2 * Copyright (c) 2021-2025 Symas Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of the Symas Corporation nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifdef _SYMBOLS_H_
31 #pragma message __FILE__ " included twice"
32 #else
33 #define _SYMBOLS_H_
34
35 #include <cassert>
36 #include <climits>
37 #include <cstdint>
38 #include <cstdlib>
39 #include <cstring>
40
41 #include <algorithm>
42 #include <list>
43 #include <map>
44 #include <set>
45 #include <stack>
46 #include <string>
47 #include <vector>
48
49 #define PICTURE_MAX 64
50
51 extern const char *numed_message;
52
53 enum cbl_dialect_t {
54 dialect_gcc_e = 0x00,
55 dialect_ibm_e = 0x01,
56 dialect_mf_e = 0x02,
57 dialect_gnu_e = 0x04,
58 };
59
60 // Dialects may be combined.
61 extern unsigned int cbl_dialects;
62 void cobol_dialect_set( cbl_dialect_t dialect );
63
64 // GCC dialect means no other dialects
65 static inline bool dialect_gcc() {
66 return dialect_gcc_e == cbl_dialects;
67 }
68 static inline bool dialect_ibm() {
69 return dialect_ibm_e == (cbl_dialects & dialect_ibm_e);
70 }
71 static inline bool dialect_mf() {
72 return dialect_mf_e == (cbl_dialects & dialect_mf_e );
73 }
74 static inline bool dialect_gnu() {
75 return dialect_gnu_e == (cbl_dialects & dialect_gnu_e );
76 }
77
78 enum cbl_gcobol_feature_t {
79 feature_gcc_e = 0x00,
80 feature_internal_ebcdic_e = 0x01,
81 feature_embiggen_e = 0x02, // widen numeric that redefine POINTER
82 };
83
84 extern size_t cbl_gcobol_features;
85 bool cobol_gcobol_feature_set( cbl_gcobol_feature_t gcobol_feature, bool on = true );
86
87 static inline bool gcobol_feature_internal_ebcdic() {
88 return feature_internal_ebcdic_e ==
89 (cbl_gcobol_features & feature_internal_ebcdic_e);
90 }
91 static inline bool gcobol_feature_embiggen() {
92 return feature_embiggen_e ==
93 (cbl_gcobol_features & feature_embiggen_e);
94 }
95
96 enum cbl_division_t {
97 identification_div_e,
98 environment_div_e,
99 data_div_e,
100 procedure_div_e,
101 };
102
103 void mode_syntax_only( cbl_division_t division );
104 bool mode_syntax_only();
105
106 static inline bool
107 is_numeric( cbl_field_type_t type ) {
108 switch ( type ) {
109 case FldInvalid:
110 case FldGroup:
111 case FldAlphanumeric:
112 case FldNumericEdited:
113 case FldAlphaEdited:
114 case FldLiteralA:
115 case FldClass:
116 case FldConditional:
117 case FldForward:
118 case FldSwitch:
119 case FldDisplay:
120 case FldPointer: // not numeric because not computable, only settable
121 case FldBlob:
122 return false;
123 // These types are computable or, in the case of FldIndex, may be
124 // arbitrarily set and incremented.
125 case FldNumericDisplay:
126 case FldNumericBinary:
127 case FldFloat:
128 case FldPacked:
129 case FldNumericBin5:
130 case FldLiteralN:
131 case FldIndex:
132 return true;
133 }
134 cbl_internal_error( "%s:%d: invalid %<symbol_type_t%> %d", __func__, __LINE__, type );
135 return false;
136 }
137
138 struct os_locale_t {
139 char assumed[16];
140 const char *codeset;
141 };
142
143 const char * cbl_field_attr_str( cbl_field_attr_t attr );
144
145 cbl_field_attr_t literal_attr( const char prefix[] );
146
147 static inline bool
148 is_working_storage(uint32_t attr) {
149 return 0 == (attr & (linkage_e | local_e));
150 }
151
152 int cbl_figconst_tok( const char *value );
153 enum cbl_figconst_t cbl_figconst_of( const char *value );
154 const char * cbl_figconst_str( cbl_figconst_t fig );
155
156 const char * consistent_encoding_check( const YYLTYPE& loc, const char input[] );
157
158 class cbl_domain_elem_t {
159 uint32_t length;
160 const char *value;
161 public:
162 bool is_numeric, all;
163
164 cbl_domain_elem_t()
165 : length(0), value(NULL), is_numeric(false), all(false)
166 {}
167 cbl_domain_elem_t( const YYLTYPE& loc,
168 bool all,
169 uint32_t length,
170 const char *value,
171 bool is_numeric = false )
172 : length(length), value(value), is_numeric(is_numeric), all(all)
173 {
174 if( value && ! is_numeric ) {
175 auto s = consistent_encoding_check(loc, value);
176 if( s ) this->value = s;
177 }
178 }
179 const char *name() const { return value; }
180 uint32_t size() const { return is_numeric ? strlen(value) : length; }
181 };
182
183 struct cbl_domain_t {
184 cbl_domain_elem_t first, last;
185 cbl_domain_t() : first(), last(first)
186 {}
187 cbl_domain_t( const YYLTYPE& loc,
188 bool all,
189 uint32_t length,
190 const char * value,
191 bool is_numeric = false )
192 : first(loc, all, length, value, is_numeric), last(first)
193 {}
194 cbl_domain_t( const cbl_domain_elem_t& a, const cbl_domain_elem_t& z )
195 : first(a)
196 , last(z)
197 {
198 assert(a.is_numeric == z.is_numeric);
199 }
200 };
201
202 typedef const char * (*time_now_f)( void );
203
204 const char * date2_is_now(void);
205 const char * day2_is_now(void);
206 const char * date4_is_now(void);
207 const char * day4_is_now(void);
208 const char * time_is_now(void);
209
210 struct cbl_upsi_mask_t {
211 bool on_off;
212 uint32_t value;
213 cbl_upsi_mask_t( bool on_off, uint32_t value ) : on_off(on_off), value(value) {}
214 };
215
216 char symbol_decimal_point_set( char ch );
217 char symbol_decimal_point();
218 bool decimal_is_comma();
219
220 enum symbol_type_t {
221 SymFilename,
222 SymField,
223 SymLabel, // section, paragraph, or label
224 SymSpecial,
225 SymAlphabet,
226 SymFile,
227 SymDataSection,
228 };
229
230 // The ISO specification says alphanumeric literals have a maximum length of
231 // 8,191 characters. It seems to be silent on the length of alphanumeric data
232 // items. Our implementation requires a maximum length, so we chose to make it
233 // the same.
234 #define MAXIMUM_ALPHA_LENGTH 8192
235
236 struct cbl_field_data_t {
237 uint32_t memsize; // nonzero if larger subsequent redefining field
238 uint32_t capacity, // allocated space
239 digits; // magnitude: total digits (or characters)
240 int32_t rdigits; // digits to the right
241 const char *initial, *picture;
242
243 enum etc_type_t { val88_e, upsi_e, value_e } etc_type;
244 const char *
245 etc_type_str() const {
246 switch(etc_type) {
247 case val88_e: return "val88_e";
248 case upsi_e: return "upsi_e";
249 case value_e: return "value_e";
250 }
251 return "???";
252 }
253
254 union etc_t {
255 // "Domain" is an array representing the VALUE of CLASS or 88 type.
256 struct val88_t {
257 cbl_domain_t *false_value;
258 cbl_domain_t *domain;
259 val88_t() : false_value(NULL), domain(NULL) {}
260 } val88;
261 struct cbl_upsi_mask_t *upsi_mask;
262 tree value;
263
264 explicit etc_t( tree v = build_zero_cst (float128_type_node)) : value(v) {}
265 } etc;
266
267 cbl_field_data_t()
268 : memsize(0)
269 , capacity(0)
270 , digits(0)
271 , rdigits(0)
272 , initial(0)
273 , picture(0)
274 , etc_type(value_e)
275 , etc()
276 {}
277
278 cbl_field_data_t( uint32_t memsize, uint32_t capacity )
279 : memsize(memsize)
280 , capacity(capacity)
281 , digits(0)
282 , rdigits(0)
283 , initial(0)
284 , picture(0)
285 , etc_type(value_e)
286 , etc()
287 {}
288
289 cbl_field_data_t( uint32_t memsize, uint32_t capacity,
290 uint32_t digits, uint32_t rdigits,
291 const char *initial,
292 const char *picture = NULL )
293 : memsize(memsize)
294 , capacity(capacity)
295 , digits(digits)
296 , rdigits(rdigits)
297 , initial(initial)
298 , picture(picture)
299 , etc_type(value_e)
300 , etc()
301 {}
302
303 cbl_field_data_t( const cbl_field_data_t& that ) {
304 copy_self(that);
305 }
306 cbl_field_data_t& operator=( const cbl_field_data_t& that ) {
307 return copy_self(that);
308 }
309
310 cbl_domain_t * false_value_of() const { return etc.val88.false_value; }
311 cbl_domain_t * false_value_as( cbl_domain_t * domain ) {
312 etc_type = val88_e;
313 return etc.val88.false_value = domain;
314 }
315 cbl_domain_t * domain_of() const {
316 assert(etc_type == val88_e);
317 return etc.val88.domain;
318 }
319 cbl_domain_t * domain_as(cbl_domain_t * domain) {
320 etc_type = val88_e;
321 return etc.val88.domain = domain;
322 }
323 cbl_upsi_mask_t * upsi_mask_of() const {
324 assert(etc_type == upsi_e);
325 return etc.upsi_mask;
326 }
327 cbl_upsi_mask_t * operator=( cbl_upsi_mask_t * mask) {
328 etc_type = upsi_e;
329 return etc.upsi_mask = mask;
330 }
331 tree value_of() const {
332 if( etc_type != value_e ) {
333 dbgmsg("%s:%d: type is %s", __func__, __LINE__, etc_type_str());
334 }
335 return etc.value;
336 }
337 tree& operator=( tree v) {
338 etc_type = value_e;
339 return etc.value = v;
340 }
341 tree& operator=(int i) {
342 etc_type = value_e;
343 return etc.value = build_int_cst_type(integer_type_node, i);
344 }
345
346 void set_real_from_capacity( REAL_VALUE_TYPE *r ) const {
347 real_from_integer (r, VOIDmode, capacity, SIGNED);
348 }
349
350 time_now_f time_func;
351
352 uint32_t upsi_mask_derive() const {
353 assert(initial);
354 assert('0' <= initial[0] && initial[0] < '8');
355 const uint32_t bitn = initial[0] - '0';
356 return (1 << bitn);
357 }
358
359 int32_t precision() const { return std::max(int32_t(0), rdigits); }
360 int32_t ldigits() const { return std::max(int(digits), int(digits - rdigits)); }
361
362 cbl_field_data_t& valify() {
363 assert(initial);
364 std::string input(initial);
365 if( decimal_is_comma() ) {
366 std::replace(input.begin(), input.end(), ',', '.');
367 }
368
369 double d;
370 int n;
371 int erc = sscanf(input.c_str(), "%lf%n", &d, &n);
372
373 if( erc < 0 || size_t(n) != input.size() ) {
374 dbgmsg("%s: error: could not interpret '%s' of '%s' as a number",
375 __func__, initial + n, initial);
376 }
377
378 REAL_VALUE_TYPE r;
379 real_from_string (&r, input.c_str());
380 r = real_value_truncate (TYPE_MODE (float128_type_node), r);
381 etc.value = build_real (float128_type_node, r);
382 return *this;
383 }
384 cbl_field_data_t& valify( const char *input ) {
385 assert(input);
386 initial = input;
387 capacity = strlen(initial);
388 return valify();
389 }
390
391 protected:
392 cbl_field_data_t& copy_self( const cbl_field_data_t& that ) {
393 memsize = that.memsize;
394 capacity = that.capacity;
395 digits = that.digits;
396 rdigits = that.rdigits;
397 initial = that.initial;
398 picture = that.picture;
399 etc_type = that.etc_type;
400
401 switch(etc_type) {
402 case value_e:
403 etc.value = that.etc.value;
404 break;
405 case val88_e:
406 etc.val88 = that.etc.val88;
407 break;
408 case upsi_e:
409 etc.upsi_mask = that.etc.upsi_mask;
410 break;
411 }
412 return *this;
413 }
414 };
415
416 static inline uint32_t
417 capacity_cast( size_t size ) {
418 uint32_t len = static_cast<uint32_t>(size);
419 assert(len == size);
420 return len;
421 }
422
423 struct cbl_occurs_bounds_t {
424 // lower = upper = 0 for a non-table
425 // lower = upper = occurs for a fixed table
426 // lower and upper are the (inclusive) bounds for DEPENDING ON in a
427 // variable size table. lower can be zero.
428 size_t lower, upper;
429
430 cbl_occurs_bounds_t()
431 : lower(0), upper(0) {}
432 explicit cbl_occurs_bounds_t(size_t lower, size_t upper=0)
433 : lower(lower), upper(upper) {}
434
435 size_t ntimes() const {
436 return upper;
437 }
438 bool fixed_size() const { return lower == upper; }
439 };
440
441 struct cbl_field_t; // A necessary forward reference
442
443 struct cbl_field_list_t {
444 size_t nfield;
445 size_t *fields;
446 cbl_field_list_t() : nfield(0), fields(NULL) {}
447 };
448
449 struct cbl_occurs_key_t {
450 bool ascending;
451 cbl_field_list_t field_list;
452 };
453
454 struct cbl_occurs_t {
455 cbl_occurs_bounds_t bounds;
456 size_t depending_on;
457 size_t nkey;
458 cbl_occurs_key_t *keys;
459 cbl_field_list_t indexes;
460
461 cbl_occurs_t() : depending_on(0), nkey(0), keys(NULL) {}
462
463 size_t ntimes() const { return bounds.ntimes(); }
464
465 void key_alloc( bool ascending );
466 void key_field_add( cbl_field_t *field );
467 void index_add( const cbl_field_t *field );
468 cbl_occurs_key_t * key_of( cbl_field_t *field );
469 bool subscript_ok( const cbl_field_t *subscript ) const;
470
471 protected:
472 void field_add( cbl_field_list_t& fields, const cbl_field_t *field );
473 };
474
475 /*
476 * Support for CALL and Linkage Section.
477 */
478 enum cbl_ffi_arg_attr_t { none_of_e, address_of_e, length_of_e };
479
480 enum cbl_ffi_crv_t {
481 by_default_e,
482 by_reference_e = 'R',
483 by_content_e = 'C',
484 by_value_e = 'E'
485 };
486
487 static inline const char *
488 cbl_ffi_crv_str( cbl_ffi_crv_t crv ) {
489 switch (crv) {
490 case by_default_e: return "<default>";
491 case by_reference_e: return "REFERENCE";
492 case by_content_e: return "CONTENT";
493 case by_value_e: return "VALUE";
494 }
495 return "???";
496 }
497
498 typedef std::pair<size_t, size_t> cbl_bytespan_t;
499 struct cbl_subtable_t {
500 size_t offset, isym;
501 };
502
503 bool is_elementary( enum cbl_field_type_t type );
504
505 /* In cbl_field_t:
506 * 'offset' is overloaded for FldAlphanumeric/temporary/intermediate variables
507 * For such variables, offset is a copy of the initial capacity. This is in
508 * support of the FUNCTION TRIM function, which both needs to be able to
509 * reduce the capacity of the target variable, and then to reset it back to
510 * the original value
511 */
512
513 struct cbl_field_t {
514 size_t offset;
515 enum cbl_field_type_t type, usage;
516 uint64_t attr;
517 static_assert(sizeof(attr) == sizeof(cbl_field_attr_t), "wrong attr size");
518 size_t parent; // symbols[] index of our parent
519 size_t our_index; // symbols[] index of this field, set in symbol_add()
520 uint32_t level;
521 struct cbl_occurs_t occurs;
522 int line; // Where it appears in the file.
523 cbl_name_t name; // Appears in the GIMPLE dump.
524 size_t file; // nonzero if field is 01 record for a file
525 struct linkage_t {
526 bool optional;
527 cbl_ffi_crv_t crv; // Using by C/R/V in Linkage
528 linkage_t() : optional(false), crv(by_default_e) {}
529 } linkage;
530 struct cbl_field_data_t data;
531 tree var_decl_node; // Reference to the pointer to the cblc_field_t structure
532 tree data_decl_node; // Reference to the run-time data of the COBOL variable
533 // // For linkage_e variables, data_decl_node is a pointer
534 // // to the data, rather than the actual data
535
536 void set_linkage( cbl_ffi_crv_t crv, bool optional ) {
537 linkage.optional = optional;
538 linkage.crv = crv;
539 assert(crv != by_content_e);
540 }
541
542 inline bool is_typedef() const {
543 return has_attr(typedef_e);
544 }
545 inline bool is_strongdef() const {
546 return has_attr(strongdef_e);
547 }
548
549 bool is_valid() const {
550 return data.capacity > 0
551 || level == 88
552 || level == 66
553 || type == FldClass
554 || type == FldIndex
555 || type == FldLiteralA
556 || type == FldLiteralN;
557 }
558
559 bool is_zero() const {
560 return real_zerop(data.value_of());
561 }
562
563 bool rename_level_ok() const {
564 switch( level ) {
565 case 0:
566 case 1:
567 case 66:
568 case 77:
569 case 88:
570 return false;
571 }
572 return true;
573 }
574
575 bool reasonable_capacity() const {
576 return data.capacity <= MAX_FIXED_POINT_DIGITS;
577 }
578
579 cbl_field_t& same_as( const cbl_field_t& that, bool is_typedef ) {
580 type = that.type;
581 attr |= (that.attr & external_e);
582 attr |= same_as_e;
583
584 data = that.data;
585
586 if( ! (is_typedef || that.type == FldClass) ) {
587 data.initial = NULL;
588 data = build_zero_cst (float128_type_node);
589 }
590 return *this;
591 }
592
593 void report_invalid_initial_value(const YYLTYPE& loc) const;
594
595 bool is_ascii() const;
596 bool is_integer() const { return is_numeric(type) && data.rdigits == 0; }
597
598 bool is_binary_integer() const {
599 return type == FldNumericBinary || type == FldNumericBin5;
600 }
601
602 HOST_WIDE_INT as_integer() const {
603 return real_to_integer( TREE_REAL_CST_PTR (data.value_of()) );
604 }
605
606 void embiggen( size_t eight=8 ) {
607 assert(gcobol_feature_embiggen() && is_numeric(type) && size() == 4);
608
609 type = FldNumericBin5;
610 attr |= embiggened_e;
611 data.capacity = eight;
612 data.digits = 0;
613 }
614
615 bool has_attr( cbl_field_attr_t attr ) const {
616 return cbl_field_attr_t(this->attr & attr) == attr;
617 }
618 uint64_t set_attr( cbl_field_attr_t attr );
619 uint64_t clear_attr( cbl_field_attr_t attr );
620 const char * attr_str( const std::vector<cbl_field_attr_t>& attrs ) const;
621
622 bool is_justifiable() const {
623 if( type == FldAlphanumeric ) return true;
624 if( type == FldInvalid ) return true;
625 return ! has_attr(rjust_e);
626 }
627
628 bool has_subordinate( const cbl_field_t *that ) const;
629
630 const char * internalize();
631 const char *value_str() const;
632
633 bool is_key_name() const { return has_attr(record_key_e); }
634
635 long scaled_capacity() const {
636 return data.digits?
637 long(data.digits) - data.rdigits
638 :
639 data.capacity;
640 }
641 uint32_t size() const; // table capacity or capacity
642
643 const char * pretty_name() const {
644 if( name[0] == '_' && data.initial ) return data.initial;
645 return name;
646 }
647 static const char * level_str(uint32_t level );
648 inline const char * level_str() const {
649 return level_str(level);
650 }
651 };
652
653 const cbl_field_t * cbl_figconst_field_of( const char *value );
654
655 // Necessary forward referencea
656 struct cbl_label_t;
657 struct cbl_refer_t;
658
659 struct cbl_span_t {
660 cbl_refer_t *from, *len;
661
662 explicit cbl_span_t( cbl_refer_t *from, cbl_refer_t *len = NULL )
663 : from(from), len(len) {};
664 bool is_active() const { return !( from == NULL && len == NULL ); }
665
666 cbl_field_t *from_field();
667 cbl_field_t *len_field();
668 };
669
670
671 struct cbl_refer_t {
672 YYLTYPE loc;
673 cbl_field_t *field;
674 cbl_label_t *prog_func;
675 bool all, addr_of;
676 std::vector<cbl_refer_t> subscripts; // indices
677 cbl_span_t refmod; // substring bounds
678
679 cbl_refer_t()
680 : loc(), field(NULL), prog_func(NULL)
681 , all(NULL), addr_of(false)
682 , refmod(NULL)
683 {}
684 // cppcheck-suppress noExplicitConstructor
685 cbl_refer_t( cbl_field_t *field, bool all = false )
686 : loc(), field(field), prog_func(NULL)
687 , all(all), addr_of(false)
688 , refmod(NULL)
689 {}
690 cbl_refer_t( const YYLTYPE& loc, cbl_field_t *field, bool all = false )
691 : loc(loc), field(field), prog_func(NULL)
692 , all(all), addr_of(false)
693 , refmod(NULL)
694 {}
695 cbl_refer_t( cbl_field_t *field, cbl_span_t& refmod )
696 : loc(), field(field), prog_func(NULL)
697 , all(false), addr_of(false)
698 , refmod(refmod)
699 {}
700 cbl_refer_t( cbl_field_t *field,
701 const std::vector<cbl_refer_t>& subscripts,
702 cbl_span_t refmod = cbl_span_t(NULL) )
703 : loc(), field(field), prog_func(NULL)
704 , all(false), addr_of(false)
705 , subscripts(subscripts)
706 , refmod(refmod)
707 {}
708 explicit cbl_refer_t( cbl_label_t *prog_func, bool addr_of = true )
709 : loc(), field(NULL), prog_func(prog_func)
710 , all(false), addr_of(addr_of)
711 , refmod(cbl_span_t(NULL))
712 {}
713
714 cbl_refer_t( const cbl_refer_t& that ) = default;
715
716 cbl_refer_t& operator=( const cbl_refer_t& that ) {
717 loc = that.loc;
718 field = that.field;
719 prog_func = that.prog_func;
720 all = that.all;
721 addr_of = that.addr_of;
722 subscripts = that.subscripts;
723 refmod = that.refmod;
724 return *this;
725 }
726
727
728 cbl_refer_t duplicate() const {
729 return cbl_refer_t( field, subscripts, refmod );
730 }
731
732 uint32_t nsubscript() const { return subscripts.size(); }
733
734 static cbl_refer_t *empty();
735
736 cbl_refer_t * name( const char name[] ) {
737 assert(name);
738 assert(strlen(name) < sizeof(field->name));
739 strcpy(field->name, name);
740 return this;
741 }
742
743 bool is_pointer() const { return addr_of || field->type == FldPointer; }
744 bool is_reference() const { return nsubscript() > 0 || refmod.is_active(); }
745 bool is_table_reference() const { return nsubscript() > 0; }
746 bool is_refmod_reference() const { return refmod.is_active(); }
747
748 size_t subscripts_set( const std::list<cbl_refer_t>& subs );
749 const char * str() const;
750 const char * deref_str() const;
751 const char * name() const;
752 cbl_field_t * cond() {
753 assert( ! is_reference() );
754 assert(field);
755 if( FldConditional != field->type ) {
756 dbgmsg("cbl_refer_t::cond: "
757 "logic error: %s is not a condition expression", field->name);
758 }
759 assert( FldConditional == field->type);
760 return field;
761 }
762 };
763
764 struct elem_key_t {
765 size_t program;
766 const char * name;
767 elem_key_t( size_t program, const cbl_name_t name )
768 : program(program)
769 , name(name)
770 {}
771 bool operator<( const elem_key_t& that ) const {
772 if( program == that.program ) {
773 return strcasecmp(name, that.name) < 0;
774 }
775 return program < that.program;
776 }
777 bool operator==( const elem_key_t& that ) const {
778 if( program == that.program ) {
779 return strcasecmp(name, that.name) == 0;
780 }
781 return false;
782 }
783 };
784
785 struct field_key_t {
786 size_t program;
787 const char * name;
788 field_key_t( size_t program, const cbl_field_t *field )
789 : program(program)
790 , name(field->name)
791 {}
792 field_key_t( size_t program, const cbl_name_t name )
793 : program(program)
794 , name(name)
795 {}
796 bool operator<( const field_key_t& that ) const {
797 if( program == that.program ) {
798 return strcasecmp(name, that.name) < 0;
799 }
800 return program < that.program;
801 }
802 bool operator==( const field_key_t& that ) const {
803 if( program == that.program ) {
804 return strcasecmp(name, that.name) == 0;
805 }
806 return false;
807 }
808 };
809
810 bool valid_move( const cbl_field_t *tgt, const cbl_field_t *src );
811
812 #define record_area_name_stem "_ra_"
813
814 static inline bool
815 is_record_area( const cbl_field_t *field ) {
816 static const char stem[] = record_area_name_stem;
817 return 0 == memcmp(field->name, stem, sizeof(stem)-1);
818 }
819
820 bool is_register_field( const cbl_field_t *field );
821
822 static inline bool
823 is_constant( const cbl_field_t *field ) {
824 return field->has_attr(constant_e);
825 }
826
827 const char *
828 is_numeric_constant( const char name[] );
829
830 cbl_field_t *
831 symbol_field_index_set( cbl_field_t *field );
832
833 bool
834 symbol_field_type_update( cbl_field_t *field,
835 cbl_field_type_t type, bool is_usage );
836
837 struct sort_key_t;
838 struct sort_key_t;
839
840 struct cbl_key_t {
841 bool ascending;
842 std::vector<const cbl_field_t*> fields;
843
844 cbl_key_t() : ascending(true) {}
845 explicit cbl_key_t( sort_key_t src );
846 explicit cbl_key_t( const cbl_occurs_key_t& that );
847 cbl_key_t( size_t nfield, cbl_field_t **fields, bool ascending = true )
848 : ascending(ascending)
849 , fields(fields, fields + nfield)
850 {}
851 cbl_key_t& operator=( const sort_key_t& that );
852 };
853
854 enum cbl_label_type_t {
855 /*
856 * LblNone "matches" all types, because it exists for forward
857 * references. Labels are equal if the types match and the names
858 * match.
859 */
860 LblNone, // top-level programs have no parent
861 LblProgram,
862 LblFunction,
863 LblSection,
864 LblParagraph,
865 LblLoop,
866 LblEvaluate,
867 LblSearch,
868 LblSort,
869 LblString,
870 LblArith,
871 LblCompute,
872 };
873
874 struct cbl_proc_addresses_t {
875 // This structure is used by 4; it very likely will never be
876 // referenced elsewhere
877 tree go_to; // gg_append_statement(go_to) generates "goto label"
878 tree label; // gg_append_statement(label) generates "label:"
879 tree addr; // addr can be used as the right-hand-side of a "pointer = addr"
880 tree decl; // This is the decl used to create the other three
881 };
882
883 struct cbl_proc_t {
884 struct cbl_label_t *label;
885 struct cbl_proc_addresses_t top;
886 struct cbl_proc_addresses_t exit;
887 struct cbl_proc_addresses_t bottom;
888 tree alter_location; // The altered value if this paragraph is the target of an ALTER
889 };
890
891 struct cbl_label_addresses_t {
892 // This structure is used by parser_label_label() and parser_label_goto()
893 // It reuses the cbl_label_t *proc pointer; the meaning is clear from context
894 tree go_to; // gg_append_statement(go_to) generates "goto label"
895 tree label; // gg_append_statement(label) generates "label:"
896 };
897
898 struct cbl_refer_t;
899
900 static inline const char *
901 logop_str( enum logop_t logop ) {
902 switch ( logop ) {
903 case not_op: return "not";
904 case and_op: return "and";
905 case or_op: return "or";
906 case xor_op: return "xor";
907 case xnor_op: return "xnor";
908 case true_op: return "true";
909 case false_op: return "false";
910 }
911 return "???";
912 }
913
914 static inline const char *
915 relop_str( enum relop_t relop ) {
916 switch ( relop ) {
917 case lt_op:
918 return "<";
919 case le_op:
920 return "<=";
921 case eq_op:
922 return "==";
923 case ne_op:
924 return "<>";
925 case ge_op:
926 return ">=";
927 case gt_op:
928 return ">";
929 }
930 return "???";
931 }
932
933 static inline const char *
934 setop_str( enum setop_t setop ) {
935 switch ( setop ) {
936 case is_op:
937 return "is_op";
938 }
939 return "???";
940 }
941
942 struct cbl_substitute_t {
943 enum subst_fl_t { subst_all_e, subst_first_e = 'F', subst_last_e = 'L'};
944 bool anycase;
945 subst_fl_t first_last;
946 cbl_refer_t orig, replacement;
947
948 cbl_substitute_t()
949 : anycase(false)
950 , first_last(subst_all_e)
951 {}
952 cbl_substitute_t( bool anycase, char first_last,
953 cbl_refer_t *orig, cbl_refer_t *replacement )
954 : anycase(anycase)
955 , first_last(subst_fl_t(first_last))
956 , orig( orig? *orig : cbl_refer_t() )
957 , replacement( replacement? *replacement : cbl_refer_t() )
958 {}
959 };
960
961 static inline const char *
962 field_name( const cbl_field_t *f ) { return f? f->name : "(void)"; }
963
964 static inline const char *
965 field_name(const cbl_refer_t *r) { return r? field_name(r->field) : "(Nil)"; }
966
967 char * field_str( const cbl_field_t *field );
968
969 struct cbl_string_src_t {
970 cbl_refer_t delimited_by; // identifier-2: BY SIZE indicated by NULL field
971 size_t ninput;
972 cbl_refer_t *inputs; // identifier-1
973
974 cbl_string_src_t( const cbl_refer_t& delimited_by,
975 size_t ninput, cbl_refer_t *inputs )
976 : delimited_by(delimited_by)
977 , ninput(ninput)
978 , inputs(inputs)
979 {}
980 };
981
982 struct cbl_num_result_t {
983 enum cbl_round_t rounded;
984 struct cbl_refer_t refer;
985
986 static const cbl_refer_t&
987 refer_of( const cbl_num_result_t& res ) {
988 return res.refer;
989 }
990 };
991
992 void parser_symbol_add( struct cbl_field_t *new_var );
993 void parser_local_add( struct cbl_field_t *new_var );
994
995 struct cbl_ffi_arg_t {
996 bool optional;
997 cbl_ffi_crv_t crv;
998 cbl_ffi_arg_attr_t attr;
999 cbl_refer_t refer; // refer::field == NULL is OMITTED
1000
1001 cbl_ffi_arg_t();
1002 cbl_ffi_arg_t( cbl_refer_t* refer,
1003 cbl_ffi_arg_attr_t attr );
1004 cbl_ffi_arg_t( cbl_ffi_crv_t crv,
1005 cbl_refer_t* refer,
1006 cbl_ffi_arg_attr_t attr = none_of_e );
1007 cbl_field_t *field() { return refer.field; }
1008 void validate() const {
1009 if( refer.is_reference() ) {
1010 yyerror("%s is a reference", refer.field->name);
1011 }
1012 if( ! refer.field->has_attr(linkage_e) ) {
1013 yyerror("%s not found in LINKAGE SECTION", refer.field->name);
1014 }
1015 switch( refer.field->level ) {
1016 case 1: case 77:
1017 break;
1018 default:
1019 yyerror("%s must be LEVEL 01 or 77", refer.field->name);
1020 }
1021 // Update Linkage Section data item.
1022 refer.field->set_linkage(crv, optional);
1023 }
1024 protected:
1025 bool by_value() const {
1026 if( crv == by_reference_e ) return false;
1027 return refer.field != NULL;
1028 }
1029 };
1030
1031 // In support of serial/linear search:
1032 struct cbl_lsearch_addresses_t {
1033 // This structure is used by linear_search
1034 struct cbl_label_addresses_t at_exit; // The at_exit statements are at the top
1035 struct cbl_label_addresses_t top; // Start of the loop of WHENS
1036 struct cbl_label_addresses_t bottom; // The very bottom
1037 };
1038
1039 struct cbl_lsearch_t {
1040 cbl_lsearch_addresses_t addresses;
1041 cbl_label_addresses_t jump_over;
1042 tree limit;
1043 tree counter;
1044 struct cbl_field_t *index;
1045 struct cbl_field_t *varying;
1046 bool first_when;
1047 };
1048
1049 // This structure is used for binary searches:
1050
1051 struct cbl_bsearch_t {
1052 cbl_label_addresses_t too_small;
1053 cbl_label_addresses_t too_big;
1054 cbl_label_addresses_t top;
1055 cbl_label_addresses_t first_test;
1056 cbl_label_addresses_t bottom;
1057 tree left; // This is a long
1058 tree right; // This is a long
1059 tree middle; // This is our copy of the index, so we only need to write
1060 // it and never read it.
1061 tree compare_result; // This is an int, and avoids
1062 struct cbl_field_t *index;
1063 bool first_when;
1064 };
1065
1066 struct cbl_unstring_t {
1067 cbl_label_addresses_t over;
1068 cbl_label_addresses_t into;
1069 cbl_label_addresses_t bottom;
1070 };
1071
1072 // Used by RETURN instruction in SORT with output-procedure
1073 struct cbl_sortreturn_t {
1074 cbl_label_addresses_t at_end;
1075 cbl_label_addresses_t not_at_end;
1076 cbl_label_addresses_t bottom;
1077 };
1078
1079 struct cbl_call_exception_t {
1080 cbl_label_addresses_t over;
1081 cbl_label_addresses_t into;
1082 cbl_label_addresses_t bottom;
1083 };
1084
1085 struct cbl_arith_error_t {
1086 cbl_label_addresses_t over;
1087 cbl_label_addresses_t into;
1088 cbl_label_addresses_t bottom;
1089 };
1090
1091 struct cbl_compute_error_t {
1092 // This is an int. The value is a cbl_compute_error_code_t
1093 tree compute_error_code;
1094 };
1095
1096 struct cbl_label_t {
1097 enum cbl_label_type_t type;
1098 size_t parent;
1099 int line, used, lain;
1100 bool common, initial, recursive;
1101 size_t initial_section, returning;
1102 cbl_name_t name;
1103 const char *os_name, *mangled_name;
1104 union
1105 {
1106 // For performs, paragraphs, and sections:
1107 cbl_proc_t *proc;
1108
1109 // For parser_label_label and parser_label_goto
1110 cbl_label_addresses_t *goto_trees;
1111
1112 // For linear/serial search
1113 cbl_lsearch_t *lsearch;
1114
1115 // For binary search
1116 cbl_bsearch_t *bsearch;
1117
1118 // For UNSTRING search
1119 cbl_unstring_t *unstring;
1120
1121 // for CALL [NOT] ON EXCEPTION
1122 struct cbl_call_exception_t *call_exception;
1123
1124 // for arithmetic [NOT] ON SIZE_ERROR
1125 struct cbl_arith_error_t *arith_error;
1126
1127 // for parser_op/parser_assign error tracking
1128 struct cbl_compute_error_t *compute_error;
1129 } structs;
1130
1131 bool is_function() const { return type == LblFunction; }
1132
1133 const char *type_str() const {
1134 switch(type) {
1135 case LblNone: return "LblNone";
1136 case LblProgram: return "LblProgram";
1137 case LblFunction: return "LblFunction";
1138 case LblSection: return "LblSection";
1139 case LblParagraph: return "LblParagraph";
1140 case LblLoop: return "LblLoop";
1141 case LblEvaluate: return "LblEvaluate";
1142 case LblSearch: return "LblSearch";
1143 case LblSort: return "LblSort";
1144 case LblString: return "LblString";
1145 case LblArith: return "LblArith";
1146 case LblCompute: return "LblCompute";
1147 }
1148 gcc_unreachable();
1149 }
1150
1151 size_t explicit_parent() const;
1152 const char *str() const;
1153 };
1154
1155 struct parser_tgt_t;
1156
1157 class cbl_label_ref_t {
1158 bool qualified; // caller mentioned paragraph & section
1159 cbl_label_t *target;
1160 const cbl_label_t& context; // section called from
1161 int line; // point of reference
1162 parser_tgt_t *handle;
1163 public:
1164 cbl_label_ref_t( size_t program, const cbl_label_t& context, int line,
1165 const char name[], size_t isect = 0 );
1166
1167 cbl_label_t * target_of() { return target; }
1168
1169 parser_tgt_t * handle_of(parser_tgt_t *parser_tgt) {
1170 return this->handle = parser_tgt;
1171 }
1172 parser_tgt_t * handle_of() {
1173 return this->handle;
1174 }
1175 };
1176
1177 static inline bool
1178 label_lessthan( const cbl_label_t & a, const cbl_label_t & b ) {
1179 if ( a.type == LblNone || b.type == LblNone || a.type == b.type ) {
1180 return strcmp( a.name, b.name ) < 0;
1181 }
1182 return a.type < b.type;
1183 }
1184
1185 static inline bool
1186 operator<( const cbl_label_t & a, const cbl_label_t & b ) {
1187 return label_lessthan( a, b );
1188 }
1189
1190 struct label_cmp_lessthan {
1191 bool operator() ( const cbl_label_t * a, const cbl_label_t * b ) {
1192 return label_lessthan( *a, *b );
1193 }
1194 bool operator() ( const cbl_label_t& a, const cbl_label_t& b ) {
1195 return label_lessthan( a, b );
1196 }
1197 };
1198
1199 size_t field_index( const cbl_field_t *f );
1200
1201 cbl_field_t * new_temporary( enum cbl_field_type_t type, const char initial[] = NULL );
1202 cbl_field_t * new_temporary_like( cbl_field_t skel );
1203 cbl_field_t * new_temporary_clone( const cbl_field_t *orig);
1204 cbl_field_t * keep_temporary( cbl_field_type_t type );
1205
1206 cbl_field_t * new_literal( uint32_t len, const char initial[],
1207 enum cbl_field_attr_t attr = none_e );
1208
1209 void symbol_temporaries_free();
1210
1211 class temporaries_t {
1212 friend void symbol_temporaries_free();
1213 struct literal_an {
1214 bool is_quoted;
1215 std::string value;
1216 literal_an() : is_quoted(false), value("???") {}
1217 literal_an( const char value[], bool is_quoted )
1218 : is_quoted(is_quoted), value(value) {}
1219 literal_an( const literal_an& that )
1220 : is_quoted(that.is_quoted), value(that.value) {}
1221 literal_an& operator=( const literal_an& that ) {
1222 is_quoted = that.is_quoted;
1223 value = that.value;
1224 return *this;
1225 }
1226 bool operator<( const literal_an& that ) const {
1227 if( value == that.value ) { // alpha before numeric
1228 return (is_quoted? 0 : 1) < (that.is_quoted? 0 : 1);
1229 }
1230 return value < that.value;
1231 }
1232 };
1233
1234 std::map<literal_an, cbl_field_t *> literals;
1235 typedef std::set<cbl_field_t *> fieldset_t;
1236 typedef std::map<cbl_field_type_t, fieldset_t> fieldmap_t;
1237 fieldmap_t used, freed;
1238
1239 public:
1240 cbl_field_t * literal( const char value[], uint32_t len, cbl_field_attr_t attr = none_e );
1241 cbl_field_t * reuse( cbl_field_type_t type );
1242 cbl_field_t * acquire( cbl_field_type_t type, const cbl_name_t name = nullptr );
1243 cbl_field_t * add( cbl_field_t *field );
1244 bool keep( cbl_field_t *field ) { return 1 == used[field->type].erase(field); }
1245 void dump() const;
1246 ~temporaries_t();
1247 };
1248
1249
1250 static inline bool is_table( const cbl_field_t *field ) {
1251 return field && field->occurs.ntimes() > 0;
1252 }
1253
1254 static inline bool is_filler( const cbl_field_t *field ) {
1255 return field && 0 == strcasecmp("FILLER", field->name);
1256 }
1257
1258 /*
1259 * CALL
1260 */
1261
1262 /*
1263 * Intrinsics
1264 */
1265
1266 enum cbl_intrinsic_trim_t {
1267 trim_none_e,
1268 trim_leading_e = 1,
1269 trim_trailing_e = 2,
1270 };
1271
1272 enum cbl_ctype_t {
1273 c_unknown,
1274 c_bool,
1275 c_char,
1276 c_wchar,
1277 c_byte,
1278 c_ubyte,
1279 c_short,
1280 c_ushort,
1281 c_int,
1282 c_uint,
1283 c_long,
1284 c_ulong,
1285 c_longlong,
1286 c_ulonglong,
1287 c_size_t,
1288 c_ssize_t,
1289 c_int128,
1290 c_float,
1291 c_double,
1292 c_longdouble,
1293 c_char_p,
1294 c_wchar_p,
1295 c_void_p,
1296 c_nts, // this is a null-terminated-string char_p
1297 };
1298
1299 struct function_descr_arg_t {
1300 size_t isym;
1301 cbl_ffi_crv_t crv;
1302 bool optional;
1303
1304 function_descr_arg_t()
1305 : isym(0), crv(by_default_e), optional(false)
1306 {}
1307 function_descr_arg_t( size_t isym, cbl_ffi_crv_t crv, bool optional )
1308 : isym(isym), crv(crv), optional(optional)
1309 {}
1310 };
1311
1312 struct function_descr_t {
1313 int token;
1314 cbl_name_t name;
1315 char cname[48];
1316 char types[8];
1317 std::vector<function_descr_arg_t> linkage_fields;
1318 cbl_field_type_t ret_type;
1319
1320 static function_descr_t init( const char name[] ) {
1321 function_descr_t descr = {};
1322 if( -1 == snprintf( descr.name, sizeof(descr.name), "%s", name ) ) {
1323 dbgmsg("name truncated to '%s' (max " HOST_SIZE_T_PRINT_UNSIGNED
1324 " characters)", name, (fmt_size_t)sizeof(descr.name));
1325 }
1326 return descr; // truncation also reported elsewhere ?
1327 }
1328 static function_descr_t init( int isym );
1329
1330 static char
1331 parameter_type( const cbl_field_t& field ) {
1332 switch( field.type ) {
1333 case FldDisplay:
1334 case FldInvalid:
1335 case FldGroup:
1336 case FldLiteralA:
1337 case FldLiteralN:
1338 case FldClass:
1339 case FldConditional:
1340 case FldForward:
1341 case FldIndex:
1342 case FldSwitch:
1343 case FldBlob:
1344 return '?';
1345 case FldPointer:
1346 return 'O';
1347 case FldAlphanumeric:
1348 return field.has_attr(all_alpha_e)? 'A' : 'X';
1349 case FldPacked:
1350 case FldNumericDisplay:
1351 case FldNumericEdited:
1352 case FldAlphaEdited:
1353 case FldNumericBinary:
1354 case FldNumericBin5:
1355 return field.data.rdigits == 0? 'I' : 'N';
1356 case FldFloat:
1357 return 'N';
1358 }
1359 gcc_unreachable();
1360 }
1361
1362 bool operator<( const function_descr_t& that ) const {
1363 return strcasecmp(name, that.name) < 0;
1364 }
1365 bool operator==( const function_descr_t& that ) const {
1366 return strcasecmp(name, that.name) == 0;
1367 }
1368 bool operator==( const char *name ) const {
1369 return strcasecmp(this->name, name) == 0;
1370 }
1371 };
1372
1373 enum cbl_section_type_t {
1374 file_sect_e,
1375 working_sect_e,
1376 linkage_sect_e,
1377 local_sect_e,
1378 };
1379
1380 struct cbl_section_t {
1381 cbl_section_type_t type;
1382 int line;
1383 void * node;
1384
1385 const char * name() const {
1386 switch(type) {
1387 case file_sect_e: return "file_sect_e";
1388 case working_sect_e: return "working_sect_e";
1389 case linkage_sect_e: return "linkage_sect_e";
1390 case local_sect_e: return "local_sect_e";
1391 }
1392 gcc_unreachable();
1393 }
1394 uint32_t attr() const {
1395 switch(type) {
1396 case file_sect_e:
1397 case working_sect_e: return 0;
1398 case linkage_sect_e: return linkage_e;
1399 case local_sect_e: return local_e;
1400 }
1401 gcc_unreachable();
1402 }
1403 };
1404
1405 struct cbl_special_name_t {
1406 int token;
1407 enum special_name_t id;
1408 cbl_name_t name;
1409 size_t filename;
1410 char os_filename[16]; // short because always in /dev
1411 };
1412
1413 char * hex_decode( const char text[] );
1414
1415 struct cbl_alphabet_t {
1416 YYLTYPE loc;
1417 cbl_name_t name;
1418 cbl_encoding_t encoding;
1419 unsigned char low_index, high_index, last_index, alphabet[256];
1420
1421 cbl_alphabet_t()
1422 : loc { 1,1, 1,1 }
1423 , encoding(ASCII_e)
1424 , low_index(0)
1425 , high_index(255)
1426 , last_index(0)
1427 {
1428 memset(name, '\0', sizeof(name));
1429 memset(alphabet, 0xFF, sizeof(alphabet));
1430 }
1431
1432 cbl_alphabet_t(const YYLTYPE& loc, cbl_encoding_t enc)
1433 : loc(loc)
1434 , encoding(enc)
1435 , low_index(0)
1436 , high_index(255)
1437 , last_index(0)
1438 {
1439 memset(name, '\0', sizeof(name));
1440 memset(alphabet, 0xFF, sizeof(alphabet));
1441 }
1442
1443 cbl_alphabet_t( const YYLTYPE& loc, const cbl_name_t name,
1444 unsigned char low_index, unsigned char high_index,
1445 unsigned char alphabet[] )
1446 : loc(loc)
1447 , encoding(custom_encoding_e)
1448 , low_index(low_index), high_index(high_index)
1449 , last_index(high_index)
1450 {
1451 assert(strlen(name) < sizeof(this->name));
1452 strcpy(this->name, name);
1453 std::copy(alphabet, alphabet + sizeof(this->alphabet), this->alphabet);
1454 }
1455
1456 unsigned char low_value() const {
1457 return alphabet[low_index];
1458 }
1459 unsigned char high_value() const {
1460 return alphabet[high_index];
1461 }
1462
1463 void
1464 add_sequence( const YYLTYPE& loc, const unsigned char seq[] ) {
1465 if( low_index == 0 ) low_index = seq[0];
1466
1467 unsigned char last = last_index > 0? alphabet[last_index] + 1 : 0;
1468
1469 for( const unsigned char *p = seq; !end_of_string(p); p++ ) {
1470 assign(loc, *p, last++);
1471 }
1472 }
1473
1474 void
1475 add_interval( const YYLTYPE& loc, unsigned char low, unsigned char high ) {
1476 if( low_index == 0 ) low_index = low;
1477
1478 unsigned char last = alphabet[last_index];
1479
1480 for( unsigned char ch = low; ch < high; ch++ ) {
1481 assign(loc, ch, last++);
1482 }
1483 }
1484
1485 void also( const YYLTYPE& loc, size_t ch );
1486 bool assign( const YYLTYPE& loc, unsigned char ch, unsigned char value );
1487
1488 static const char *
1489 encoding_str( cbl_encoding_t encoding ) {
1490 switch(encoding) {
1491 case ASCII_e: return "ascii";
1492 case iso646_e: return "iso646";
1493 case EBCDIC_e: return "ebcdic";
1494 case custom_encoding_e: return "custom";
1495 }
1496 return "???";
1497 }
1498
1499 void dump() const {
1500 yywarn("%qs: %s, %<%c%> to %<%c%> (low 0x%x, high 0x%x)",
1501 name, encoding_str(encoding),
1502 low_index, last_index, low_index, high_index);
1503 if( encoding == custom_encoding_e ) {
1504 fprintf(stderr, "\t"
1505 " 0 1 2 3 4 5 6 7"
1506 " 8 9 A B C C E F");
1507 unsigned int row = 0;
1508 for( auto p = alphabet; p < alphabet + sizeof(alphabet); p++ ) {
1509 if( (p - alphabet) % 16 == 0 ) fprintf(stderr, "\n%4X\t", row++);
1510 fprintf(stderr, "%3u ", *p);
1511 }
1512 fprintf(stderr, "\n");
1513 }
1514 }
1515 static unsigned char nul_string[2];
1516
1517 protected:
1518 static inline bool end_of_string( const unsigned char *p ) {
1519 return p != nul_string && *p == '\0';
1520 }
1521 };
1522
1523 static inline const char *
1524 file_org_str( enum cbl_file_org_t org ) {
1525 switch ( org ) {
1526 case file_disorganized_e: return "DISORGANIZED";
1527 case file_sequential_e: return "SEQUENTIAL";
1528 case file_line_sequential_e: return "LINE_SEQUENTIAL";
1529 case file_indexed_e: return "INDEXED";
1530 case file_relative_e: return "RELATIVE";
1531 }
1532 return "???";
1533 }
1534
1535 enum file_entry_type_t { fd_e, sd_e };
1536
1537 static inline const char *
1538 file_access_str( cbl_file_access_t access ) {
1539 switch(access) {
1540 case file_inaccessible_e: return "INACCESSIBLE";
1541 case file_access_seq_e: return "SEQUENTIAL";
1542 case file_access_rnd_e: return "RANDOM";
1543 case file_access_dyn_e: return "DYNAMIC";
1544 }
1545 return "???";
1546 }
1547
1548 enum declarative_culprit_t {
1549 culpa_none_e,
1550 culpa_input_e = 0x01,
1551 culpa_output_e = 0x02,
1552 culpa_io_e = 0x03, // both input and output
1553 culpa_extend_e = 0x04,
1554 };
1555
1556 struct cbl_file_key_t {
1557 bool unique;
1558 cbl_name_t name;
1559 size_t leftmost; // START or READ named leftmost field in key
1560 size_t nfield;
1561 size_t *fields; // cppcheck-suppress unsafeClassCanLeak
1562
1563 cbl_file_key_t()
1564 : unique(true)
1565 , leftmost(0)
1566 , nfield(0)
1567 , fields(nullptr)
1568 {
1569 memset(name, '\0', sizeof(name));
1570 }
1571
1572 // Construct a key of length 1 having a single field.
1573 explicit cbl_file_key_t( size_t field, bool unique = true )
1574 : unique(unique)
1575 , leftmost(0)
1576 , nfield(1)
1577 , fields( new size_t[nfield] )
1578 {
1579 fields[0] = field;
1580 memset(name, '\0', sizeof(name));
1581 }
1582
1583 cbl_file_key_t( cbl_name_t name,
1584 const std::list<cbl_field_t *>& fields,
1585 bool is_unique );
1586
1587 // The copy constructor and assignment operator exist to quell reports from
1588 // cppcheck. When these objects are copied, the copy still points to the
1589 // original data.
1590 cbl_file_key_t( const cbl_file_key_t& that )
1591 : unique(that.unique)
1592 , leftmost(that.leftmost)
1593 , nfield(that.nfield)
1594 // cppcheck-suppress copyCtorPointerCopying
1595 , fields(that.fields)
1596 {
1597 strcpy(name, that.name);
1598 }
1599 ~cbl_file_key_t() {}
1600 cbl_file_key_t& operator=( const cbl_file_key_t& that ) {
1601 unique = that.unique;
1602 leftmost = that.leftmost;
1603 nfield = that.nfield;
1604 // cppcheck-suppress copyCtorPointerCopying
1605 fields = that.fields;
1606 strcpy(name, that.name);
1607 return *this;
1608 }
1609
1610 uint32_t size();
1611 void deforward( size_t ifile );
1612 char * str() const;
1613 bool operator==( const cbl_field_t *key_field ); // not const, may set leftmost
1614
1615 protected:
1616 static uint32_t key_field_size( uint32_t sum, size_t ifield );
1617 size_t offset() const;
1618 };
1619
1620 struct cbl_file_lock_t {
1621 bool multiple;
1622 enum lock_mode_t { unlocked_e, manual_e, record_e, automatic_e } mode;
1623 cbl_file_lock_t() : multiple(false), mode(unlocked_e) {}
1624 bool mode_set( int token );
1625 bool locked() const { return mode != unlocked_e; }
1626 };
1627
1628 struct cbl_file_t {
1629 enum cbl_file_org_t org;
1630 enum file_entry_type_t entry_type;
1631 uint32_t attr;
1632 size_t reserve, same_record_as;
1633 char padding;
1634 bool optional;
1635 // varying_size::explicitly is TRUE if if RECORD has VARYING or CONTAINS x TO y
1636 struct varying_t { bool explicitly; size_t min, max; } varying_size;
1637 cbl_file_lock_t lock;
1638 // "The RECORD DELIMITER clause is syntax checked, but has no effect
1639 // on the execution of the program."
1640 enum cbl_file_access_t access;
1641 size_t filename; //
1642 size_t default_record;
1643 size_t nkey; // 1st key is primary & unique
1644 cbl_file_key_t *keys; // indexes into symbol table for key field(s)
1645 size_t password; // index into symbol table for password (!)
1646 size_t user_status; // index into symbol table for file status
1647 size_t vsam_status; // index into symbol table for vsam status PIC X(6)
1648 size_t record_length; // DEPENDS ON
1649 int line;
1650 cbl_name_t name;
1651 cbl_sortreturn_t *addresses; // Used during parser_return_start, et al.
1652 tree var_decl_node; // GENERIC tag for the run-time FIELD structure
1653
1654 cbl_file_t()
1655 : org(file_disorganized_e)
1656 , entry_type(fd_e)
1657 , attr(0), reserve(0), same_record_as(0)
1658 , padding('\0')
1659 , optional(false)
1660 , varying_size{ false, 0, 0 }
1661 , access(file_access_seq_e)
1662 , filename(0)
1663 , default_record(0)
1664 , nkey(0)
1665 , keys(nullptr)
1666 , password(0), user_status(0), vsam_status(0), record_length(0)
1667 , line(0)
1668 , addresses(nullptr)
1669 , var_decl_node(nullptr)
1670 {
1671 memset(name, '\0', sizeof(name));
1672 }
1673
1674 bool varies() const { return varying_size.min != varying_size.max; }
1675 bool validate() const;
1676 void deforward();
1677 cbl_file_key_t * keys_update( cbl_file_key_t * keys ) {
1678 if( this->keys ) delete[] this->keys;
1679 return this->keys = keys;
1680 }
1681 char * keys_str() const;
1682 int key_one( cbl_field_t *field ) const {
1683 auto ekey = keys + nkey, p = ekey;
1684 if( (p = std::find(keys, ekey, field)) == ekey ) return 0;
1685 return (p - keys) + 1;
1686 }
1687 bool relative_sequential() const {
1688 return org == file_relative_e && access == file_access_seq_e;
1689 }
1690 bool indexed_sequential() const {
1691 return org == file_indexed_e && access == file_access_seq_e;
1692 }
1693 void consider_for_default( const cbl_field_t *record );
1694 protected:
1695 bool validate_forward( size_t isym ) const;
1696 bool validate_key( const cbl_file_key_t& key ) const;
1697 };
1698
1699 static inline bool
1700 is_sequential( const cbl_file_t *file ) {
1701 assert(file);
1702 switch(file->org) {
1703 case file_sequential_e:
1704 case file_line_sequential_e:
1705 return true;
1706 case file_disorganized_e:
1707 case file_indexed_e:
1708 case file_relative_e:
1709 break;
1710 }
1711 return false;
1712 }
1713
1714 struct symbol_elem_t {
1715 enum symbol_type_t type;
1716 size_t program;
1717 union symbol_elem_u {
1718 char *filename;
1719 cbl_field_t field;
1720 cbl_label_t label;
1721 cbl_special_name_t special;
1722 cbl_alphabet_t alphabet;
1723 cbl_file_t file;
1724 cbl_section_t section;
1725 symbol_elem_u() : field() {}
1726 } elem;
1727
1728 symbol_elem_t() : type(SymField), program(0) {}
1729 explicit symbol_elem_t( symbol_type_t type, size_t program = 0 )
1730 : type(type), program(program)
1731 {}
1732
1733 symbol_elem_t( const symbol_elem_t& that )
1734 : type(that.type), program(that.program)
1735 {
1736 copy_by_type(that);
1737 }
1738 symbol_elem_t& operator=( const symbol_elem_t& that ) {
1739 type = that.type;
1740 program = that.program;
1741 return copy_by_type(that);
1742 }
1743 explicit symbol_elem_t( size_t program, const cbl_field_t& field )
1744 : type(SymField), program(program)
1745 {
1746 elem.field = field;
1747 }
1748 explicit symbol_elem_t( size_t program, const cbl_label_t& label )
1749 : type(SymLabel), program(program)
1750 {
1751 elem.label = label;
1752 }
1753 explicit symbol_elem_t( size_t program, const cbl_special_name_t& special )
1754 : type(SymSpecial), program(program)
1755 {
1756 elem.special = special;
1757 }
1758 explicit symbol_elem_t( size_t program, const cbl_section_t& section )
1759 : type(SymDataSection), program(program)
1760 {
1761 elem.section = section;
1762 }
1763
1764 protected:
1765 symbol_elem_t& copy_by_type( const symbol_elem_t& that ) {
1766 switch(type) {
1767 case SymFilename:
1768 elem.filename = that.elem.filename;
1769 break;
1770 case SymField:
1771 elem.field = that.elem.field;
1772 break;
1773 case SymLabel:
1774 elem.label = that.elem.label;
1775 break;
1776 case SymSpecial:
1777 elem.special = that.elem.special;
1778 break;
1779 case SymAlphabet:
1780 elem.alphabet = that.elem.alphabet;
1781 break;
1782 case SymFile:
1783 elem.file = that.elem.file;
1784 break;
1785 case SymDataSection:
1786 elem.section = that.elem.section;
1787 break;
1788 }
1789 return *this;
1790 }
1791 };
1792
1793 # define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
1794
1795 static inline symbol_elem_t *
1796 symbol_elem_of( cbl_label_t *label ) {
1797 size_t n = offsetof(struct symbol_elem_t, elem.label);
1798 return
1799 // cppcheck-suppress cstyleCast
1800 reinterpret_cast<struct symbol_elem_t *>((char*)label - n);
1801 }
1802
1803 static inline const symbol_elem_t *
1804 symbol_elem_of( const cbl_label_t *label ) {
1805 size_t n = offsetof(symbol_elem_t, elem.label);
1806 return
1807 // cppcheck-suppress cstyleCast
1808 reinterpret_cast<const symbol_elem_t *>((const char*)label - n);
1809 }
1810
1811 static inline symbol_elem_t *
1812 symbol_elem_of( cbl_special_name_t *special ) {
1813 size_t n = offsetof(symbol_elem_t, elem.special);
1814 return
1815 // cppcheck-suppress cstyleCast
1816 reinterpret_cast<symbol_elem_t *>((char*)special - n);
1817 }
1818
1819 static inline symbol_elem_t *
1820 symbol_elem_of( cbl_alphabet_t *alphabet ) {
1821 size_t n = offsetof(symbol_elem_t, elem.alphabet);
1822 return
1823 // cppcheck-suppress cstyleCast
1824 reinterpret_cast<symbol_elem_t *>((char*)alphabet - n);
1825 }
1826
1827 static inline symbol_elem_t *
1828 symbol_elem_of( cbl_file_t *file ) {
1829 size_t n = offsetof(struct symbol_elem_t, elem.file);
1830 return
1831 // cppcheck-suppress cstyleCast
1832 reinterpret_cast<struct symbol_elem_t *>((char*)file - n);
1833 }
1834 static inline const symbol_elem_t *
1835 symbol_elem_of( const cbl_file_t *file ) {
1836 size_t n = offsetof(symbol_elem_t, elem.file);
1837 return
1838 // cppcheck-suppress cstyleCast
1839 reinterpret_cast<const symbol_elem_t *>((const char*)file - n);
1840 }
1841
1842 static inline symbol_elem_t *
1843 symbol_elem_of( cbl_field_t *field ) {
1844 size_t n = offsetof(struct symbol_elem_t, elem.field);
1845 return
1846 // cppcheck-suppress cstyleCast
1847 reinterpret_cast<struct symbol_elem_t *>((char*)field - n);
1848 }
1849 static inline const symbol_elem_t *
1850 symbol_elem_of( const cbl_field_t *field ) {
1851 size_t n = offsetof(symbol_elem_t, elem.field);
1852 return
1853 // cppcheck-suppress cstyleCast
1854 reinterpret_cast<const symbol_elem_t *>((const char*)field - n);
1855 }
1856
1857 symbol_elem_t * symbols_begin( size_t first = 0 );
1858 symbol_elem_t * symbols_end(void);
1859 cbl_field_t * symbol_redefines( const cbl_field_t *field );
1860
1861 void build_symbol_map();
1862 bool update_symbol_map( symbol_elem_t *e );
1863
1864 void update_symbol_map2( const symbol_elem_t *elem );
1865 void finalize_symbol_map2();
1866 void dump_symbol_map2();
1867
1868 symbol_elem_t * symbol_register( const char name[] );
1869
1870 std::pair<symbol_elem_t *, bool>
1871 symbol_find( size_t program, std::list<const char *> names );
1872 symbol_elem_t * symbol_find_of( size_t program,
1873 std::list<const char *> names, size_t group );
1874
1875 struct cbl_field_t *symbol_find_odo( const cbl_field_t * field );
1876 size_t dimensions( const cbl_field_t *field );
1877
1878 const symbol_elem_t * symbol_field_current_record();
1879 const symbol_elem_t * symbol_field_alias_begin();
1880 void symbol_field_alias_end();
1881
1882 typedef std::map< size_t, size_t > corresponding_fields_t;
1883
1884 corresponding_fields_t
1885 corresponding_arith_fields( cbl_field_t *lhs, cbl_field_t *rhs );
1886 corresponding_fields_t
1887 corresponding_move_fields( cbl_field_t *lhs, cbl_field_t *rhs );
1888
1889 typedef std::set<size_t> symbolset_t;
1890
1891 symbolset_t symbol_program_programs();
1892 symbolset_t symbol_program_callables( size_t program );
1893 const cbl_label_t * symbol_program_local( const char called[] );
1894
1895 bool redefine_field( cbl_field_t *field );
1896
1897 const cbl_field_t *
1898 symbol_unresolved_file_key( const cbl_file_t * file,
1899 const cbl_name_t key_field_name );
1900
1901 static inline struct cbl_section_t *
1902 cbl_section_of( struct symbol_elem_t *e ) {
1903 assert(e && e->type == SymDataSection);
1904 return &e->elem.section;
1905 }
1906
1907 static inline struct cbl_field_t *
1908 cbl_field_of( struct symbol_elem_t *e ) {
1909 assert(e && e->type == SymField);
1910 return &e->elem.field;
1911 }
1912 static inline const cbl_field_t *
1913 cbl_field_of( const symbol_elem_t *e ) {
1914 assert(e && e->type == SymField);
1915 return &e->elem.field;
1916 }
1917
1918 static inline struct cbl_label_t *
1919 cbl_label_of( struct symbol_elem_t *e ) {
1920 assert(e && e->type == SymLabel);
1921 return &e->elem.label;
1922 }
1923
1924 static inline const cbl_label_t *
1925 cbl_label_of( const symbol_elem_t *e ) {
1926 assert(e && e->type == SymLabel);
1927 return &e->elem.label;
1928 }
1929
1930 static inline struct cbl_special_name_t *
1931 cbl_special_name_of( struct symbol_elem_t *e ) {
1932 assert(e && e->type == SymSpecial);
1933 return &e->elem.special;
1934 }
1935
1936 static inline struct cbl_alphabet_t *
1937 cbl_alphabet_of( struct symbol_elem_t *e ) {
1938 assert(e && e->type == SymAlphabet);
1939 return &e->elem.alphabet;
1940 }
1941
1942 static inline struct cbl_file_t *
1943 cbl_file_of( struct symbol_elem_t *e ) {
1944 assert(e && e->type == SymFile);
1945 return &e->elem.file;
1946 }
1947
1948 static inline const cbl_file_t *
1949 cbl_file_of( const symbol_elem_t *e ) {
1950 assert(e && e->type == SymFile);
1951 return &e->elem.file;
1952 }
1953
1954 static inline bool
1955 is_program( const symbol_elem_t& e ) {
1956 return e.type == SymLabel &&
1957 (cbl_label_of(&e)->type == LblProgram ||
1958 cbl_label_of(&e)->type == LblFunction);
1959 }
1960
1961 static inline bool
1962 is_procedure( const symbol_elem_t& e ) {
1963 return e.type == SymLabel &&
1964 (cbl_label_of(&e)->type == LblParagraph ||
1965 cbl_label_of(&e)->type == LblSection);
1966 }
1967
1968 static inline bool
1969 is_figconst(const cbl_field_t *field ) {
1970 return (field->attr & FIGCONST_MASK) != 0;
1971 }
1972
1973 static inline bool
1974 is_figconst_low( const cbl_field_t *field ) {
1975 return (field->attr & FIGCONST_MASK) == low_value_e;
1976 }
1977
1978 static inline bool
1979 is_figconst_zero( const cbl_field_t *field ) {
1980 return (field->attr & FIGCONST_MASK) == zero_value_e;
1981 }
1982
1983 static inline bool
1984 is_figconst_space( const cbl_field_t *field ) {
1985 return (field->attr & FIGCONST_MASK) == space_value_e;
1986 }
1987
1988 static inline bool
1989 is_figconst_quote( const cbl_field_t *field ) {
1990 return (field->attr & FIGCONST_MASK) == quote_value_e;
1991 }
1992
1993 static inline bool
1994 is_figconst_high( const cbl_field_t *field ) {
1995 return (field->attr & FIGCONST_MASK) == high_value_e;
1996 }
1997
1998 static inline bool
1999 is_space_value( const cbl_field_t *field ) {
2000 return (strcmp(field->name, "SPACE") == 0)
2001 || (strcmp(field->name, "SPACES") == 0);
2002 }
2003
2004 static inline bool
2005 is_quoted( const cbl_field_t *field ) {
2006 return field->has_attr(quoted_e);
2007 }
2008
2009 /*
2010 * PERFORM support
2011 *
2012 * cbl_until_addresses_t has the goto/label pairs needed to implement the
2013 * PERFORM UNTIL/VARYING/TIMES possibilities
2014 */
2015
2016 #define MAXIMUM_UNTILS 64 // This was one VARYING and four AFTERs
2017
2018 struct cbl_until_addresses_t {
2019 // This structure is used by parser_perform_start() and parser_perform_until
2020 struct cbl_label_addresses_t top; // The very top of the loop
2021 struct cbl_label_addresses_t exit; // The implied continue at the bottom
2022 struct cbl_label_addresses_t test; // The test at the bottom of the body
2023 struct cbl_label_addresses_t testA; // Starting point of a TEST_AFTER loop
2024 struct cbl_label_addresses_t setup; // The actual entry point
2025 unsigned int number_of_conditionals;
2026 struct cbl_label_addresses_t condover[MAXIMUM_UNTILS]; // Jumping over the conditional
2027 struct cbl_label_addresses_t condinto[MAXIMUM_UNTILS]; // Jumping into the conditional
2028 struct cbl_label_addresses_t condback[MAXIMUM_UNTILS]; // Jumping back from the conditional
2029 int line_number_of_setup_code; // This is needed to thwart the too-helpful compiler
2030 };
2031
2032 size_t symbol_index(); // nth after first program symbol
2033 size_t symbol_index( const symbol_elem_t *e );
2034 struct symbol_elem_t * symbol_at( size_t index );
2035
2036 struct cbl_options_t {
2037 enum arith_t {
2038 native_e,
2039 standard_e,
2040 standard_binary_e,
2041 standard_decimal_e,
2042 } arith;
2043 enum float_endidanism_t {
2044 high_order_left_e,
2045 high_order_right_e,
2046 } binary_endidanism, decimal_endidanism;
2047 enum float_encoding_t {
2048 binary_encoding_e,
2049 decimal_encoding_e,
2050 } float_encoding;
2051
2052 cbl_round_t default_round, intermediate_round;
2053
2054 struct initialize_t {
2055 ssize_t working, local;
2056 initialize_t() : working(-1), local(-1) {}
2057 } initial_value;
2058
2059 cbl_options_t()
2060 : arith(cbl_options_t::native_e)
2061 , binary_endidanism(cbl_options_t::high_order_right_e)
2062 , decimal_endidanism(cbl_options_t::high_order_right_e)
2063 , float_encoding(cbl_options_t::binary_encoding_e)
2064 , default_round(nearest_away_from_zero_e)
2065 , intermediate_round(nearest_away_from_zero_e)
2066 {}
2067 cbl_field_t * initial_working() const {
2068 return initial_value.working < 0? nullptr :
2069 cbl_field_of(symbol_at(initial_value.working));
2070 }
2071 cbl_field_t * initial_local() const {
2072 return initial_value.local < 0? nullptr :
2073 cbl_field_of(symbol_at(initial_value.local));
2074 }
2075 };
2076 cbl_options_t current_options();
2077
2078 struct symbol_elem_t *
2079 symbol_field_forward_add( size_t program, size_t parent,
2080 const char name[], int line );
2081
2082 struct cbl_field_t * symbol_field_forward( size_t index );
2083
2084 struct cbl_prog_hier_t {
2085 struct program_label_t {
2086 size_t ordinal;
2087 cbl_label_t label;
2088 program_label_t() : ordinal(0), label() {}
2089 // because std::copy_if:
2090 // cppcheck-suppress noExplicitConstructor
2091 program_label_t( const symbol_elem_t& e ) {
2092 assert(is_program(e));
2093 ordinal = symbol_index(&e);
2094 label = e.elem.label;
2095 }
2096 };
2097 std::vector<program_label_t> labels;
2098
2099 cbl_prog_hier_t();
2100 };
2101
2102 /*
2103 * cbl_perform_tgt_t has from and to: the 1st and last labels to be performed.
2104 * When only one label is being performed (no "thru"), "to" is NULL.
2105 * In the case of an inline perform, "from" points to a label of type LblLoop.
2106 */
2107 struct cbl_perform_tgt_t {
2108 struct cbl_until_addresses_t addresses;
2109
2110 cbl_perform_tgt_t() : addresses(), ifrom(0), ito(0) {}
2111 explicit cbl_perform_tgt_t( cbl_label_t * from, cbl_label_t *to = NULL )
2112 : addresses(), ifrom( from? symbol_index(symbol_elem_of(from)) : 0 )
2113 , ito( to? symbol_index(symbol_elem_of(to)) : 0 )
2114 {}
2115
2116 cbl_label_t * from( cbl_label_t * label ) {
2117 ifrom = symbol_index(symbol_elem_of(label));
2118 return from();
2119 }
2120 cbl_label_t * finally( size_t program );
2121
2122 cbl_label_t * from() const {
2123 return ifrom? cbl_label_of(symbol_at(ifrom)) : NULL;
2124 }
2125 cbl_label_t * to() const {
2126 return ito? cbl_label_of(symbol_at(ito)) : NULL;
2127 }
2128
2129
2130 void dump() const {
2131 assert(ifrom);
2132 if( !ito ) {
2133 dbgmsg( "%s:%d: #%3" GCC_PRISZ "u %s", __PRETTY_FUNCTION__, __LINE__,
2134 (fmt_size_t)ifrom, from()->str() );
2135 } else {
2136 dbgmsg( "%s:%d: #%3" GCC_PRISZ "u %s THRU #%3" GCC_PRISZ "u %s",
2137 __PRETTY_FUNCTION__, __LINE__,
2138 (fmt_size_t)ifrom, from()->str(), (fmt_size_t)ito, to()->str() );
2139 }
2140 }
2141
2142 protected:
2143 size_t ifrom, ito;
2144 };
2145
2146 struct cbl_perform_vary_t {
2147 struct cbl_refer_t varying; // numeric
2148 struct cbl_refer_t from; // numeric
2149 struct cbl_refer_t by; // numeric
2150 struct cbl_field_t *until; // FldConditional
2151
2152 cbl_perform_vary_t() : until(nullptr) {}
2153 cbl_perform_vary_t( const cbl_refer_t& varying,
2154 const cbl_refer_t& from,
2155 const cbl_refer_t& by,
2156 cbl_field_t *until )
2157 : varying(varying)
2158 , from(from)
2159 , by(by)
2160 , until(until)
2161 {}
2162 };
2163
2164 bool is_global( const cbl_field_t * field );
2165
2166 static inline bool
2167 is_literal( const cbl_field_type_t type ) {
2168 return type == FldLiteralA
2169 || type == FldLiteralN;
2170 }
2171
2172 static inline bool
2173 is_literal( const cbl_field_t *field ) {
2174 return is_literal(field->type);
2175 }
2176
2177 static inline bool
2178 is_signable( const cbl_field_t *field ) {
2179 return field->attr & signable_e;
2180 }
2181
2182 static inline bool
2183 is_temporary( const cbl_field_t *field ) {
2184 return field->attr & intermediate_e;
2185 }
2186
2187 bool has_value( cbl_field_type_t type );
2188
2189
2190 static inline bool
2191 is_numeric( const cbl_field_t *field ) {
2192 assert( field );
2193 bool is_zero = zero_value_e == (field->attr & zero_value_e);
2194 return is_zero || is_numeric(field->type);
2195 }
2196
2197 /*
2198 * Public functions
2199 */
2200
2201 bool cobol_filename( const char *name );
2202 const char * cobol_filename();
2203
2204 int cobol_fileline_set( const char line[] );
2205
2206 char *cobol_name_mangler(const char *cobol_name);
2207
2208 bool is_elementary( enum cbl_field_type_t type );
2209 bool is_numeric_edited( const char picture[] );
2210
2211 const char * intrinsic_function_name( int token );
2212
2213 char date_time_fmt( const char input[] );
2214
2215 size_t current_program_index();
2216 const char * current_declarative_section_name();
2217
2218 struct cbl_nameloc_t {
2219 YYLTYPE loc;
2220 const char *name;
2221
2222 cbl_nameloc_t() : loc{ 1,1, 1,1 }, name(NULL) {}
2223 cbl_nameloc_t( const YYLTYPE& loc, const char *name )
2224 : loc(loc), name(name)
2225 {}
2226 };
2227
2228 /*
2229 * The lexer pushes qualified names unilaterally, regardless of the
2230 * state of the parser, because it runs ahead of the parser. The
2231 * parser adds to the queue conditionally, only if the lexer has not.
2232 * The parser consumes a queue element (a name list) whenever it looks
2233 * up a name, e.g. on the way to producing a scalar.
2234 */
2235 #include <queue>
2236 typedef std::list<const char *> cbl_namelist_t;
2237 typedef std::list<cbl_nameloc_t> cbl_namelocs_t;
2238 class name_queue_t : private std::queue<cbl_namelocs_t>
2239 {
2240 friend void tee_up_empty();
2241 cbl_namelocs_t recent;
2242
2243 void allocate() {
2244 std::queue<cbl_namelocs_t>::push( cbl_namelocs_t() );
2245 }
2246 public:
2247 static cbl_namelist_t
2248 namelist_of( const cbl_namelocs_t& namelocs ) {
2249 cbl_namelist_t names;
2250 std::transform( namelocs.begin(), namelocs.end(), std::back_inserter(names),
2251 []( const cbl_nameloc_t& nameloc ) {
2252 return nameloc.name;
2253 } );
2254 return names;
2255 }
2256 size_t push( const YYLTYPE& loc, const char name[] ) {
2257 assert( !empty() );
2258 back().push_front( cbl_nameloc_t(loc, name) );
2259 dump(__func__);
2260 return size();
2261 }
2262 void qualify( const YYLTYPE& loc, const char name[] ) {
2263 if( empty() ) {
2264 allocate();
2265 push(loc, name);
2266 } else {
2267 back().push_front( cbl_nameloc_t(loc, name) );
2268 }
2269 dump(__func__);
2270 }
2271 cbl_namelocs_t pop() {
2272 assert(!empty());
2273 recent = front();
2274 std::queue<cbl_namelocs_t>::pop();
2275 dump(__func__);
2276 return recent;
2277 }
2278 cbl_namelist_t pop_as_names() {
2279 return namelist_of(pop());
2280 }
2281
2282 void dump( const char tag[] ) const;
2283
2284 cbl_namelocs_t peek() const { dump(__func__); return empty()? recent : back(); }
2285
2286 bool empty() const { return std::queue<cbl_namelocs_t>::empty(); }
2287 size_t size() const { return std::queue<cbl_namelocs_t>::size(); }
2288
2289 };
2290
2291 const std::string& keyword_alias_add( const std::string& keyword,
2292 const std::string& alias );
2293 int binary_integer_usage_of( const char name[] );
2294
2295 void tee_up_empty();
2296 void tee_up_name( const YYLTYPE& loc, const char name[] );
2297 cbl_namelist_t teed_up_names();
2298
2299 size_t end_of_group( size_t igroup );
2300
2301 struct symbol_elem_t * symbol_typedef( size_t program, std::list<const char *> names );
2302 struct symbol_elem_t * symbol_typedef( size_t program, const char name[] );
2303 struct symbol_elem_t * symbol_field( size_t program,
2304 size_t parent, const char name[] );
2305 struct cbl_label_t * symbol_program( size_t parent, const char name[] );
2306 struct cbl_label_t * symbol_label( size_t program, cbl_label_type_t type,
2307 size_t section, const char name[],
2308 const char os_name[] = NULL );
2309 struct symbol_elem_t * symbol_function( size_t parent, const char name[] );
2310
2311 struct symbol_elem_t * symbol_literalA( size_t program, const char name[] );
2312
2313 struct cbl_special_name_t * symbol_special( special_name_t id );
2314 struct symbol_elem_t * symbol_special( size_t program, const char name[] );
2315 struct symbol_elem_t * symbol_alphabet( size_t program, const char name[] );
2316
2317 struct symbol_elem_t * symbol_file( size_t program, const char name[] );
2318 struct cbl_field_t * symbol_file_record( const cbl_file_t *file );
2319 cbl_file_t::varying_t symbol_file_record_sizes( struct cbl_file_t *file );
2320 struct cbl_section_t * symbol_section( size_t program,
2321 struct cbl_section_t *section );
2322
2323 size_t symbol_label_id( const cbl_label_t *label );
2324
2325 struct cbl_field_t * parent_of( const cbl_field_t *f );
2326 const cbl_field_t * occurs_in( const cbl_field_t *f );
2327
2328 cbl_field_t *rename_not_ok( const cbl_field_t *first, const cbl_field_t *last);
2329 bool immediately_follows( const cbl_field_t *first );
2330 bool is_variable_length( const cbl_field_t *field );
2331
2332 cbl_file_t * symbol_record_file( const cbl_field_t *f );
2333
2334 struct cbl_field_t * symbol_find_odo( const cbl_field_t * field );
2335
2336 uint64_t numeric_group_attrs( const cbl_field_t *field );
2337
2338 static inline struct cbl_field_t *
2339 field_at( size_t index ) {
2340 struct symbol_elem_t *e = symbol_at(index);
2341 assert(e && e->type == SymField);
2342
2343 return &e->elem.field;
2344 }
2345
2346 bool symbols_alphabet_set( size_t program, const char name[]);
2347
2348 size_t symbols_update( size_t first, bool parsed_ok = true );
2349
2350 void symbol_table_init(void);
2351 void symbol_table_check(void);
2352
2353 symbol_elem_t * symbol_typedef_add( size_t program,
2354 cbl_field_t *field );
2355 symbol_elem_t * symbol_field_add( size_t program,
2356 cbl_field_t *field );
2357 cbl_label_t * symbol_label_add( size_t program,
2358 cbl_label_t *label );
2359 cbl_label_t * symbol_program_add( size_t program, cbl_label_t *input );
2360 symbol_elem_t * symbol_special_add( size_t program,
2361 cbl_special_name_t *special );
2362 symbol_elem_t * symbol_alphabet_add( size_t program,
2363 const cbl_alphabet_t *alphabet );
2364 symbol_elem_t * symbol_file_add( size_t program,
2365 cbl_file_t *file );
2366 symbol_elem_t * symbol_section_add( size_t program,
2367 cbl_section_t *section );
2368
2369 void symbol_field_location( size_t ifield, const YYLTYPE& loc );
2370 YYLTYPE symbol_field_location( size_t ifield );
2371
2372 bool symbol_label_section_exists( size_t program );
2373
2374 size_t symbol_field_capacity( const cbl_field_t *field );
2375
2376 size_t file_status_register();
2377 size_t return_code_register();
2378 size_t very_true_register();
2379 size_t very_false_register();
2380 size_t ec_register();
2381
2382 static inline size_t upsi_register() {
2383 return symbol_index(symbol_field(0,0,"UPSI-0"));
2384 }
2385
2386 void wsclear( char ch);
2387 const char *wsclear();
2388
2389 enum cbl_call_convention_t {
2390 cbl_call_verbatim_e = 'V',
2391 cbl_call_cobol_e = 'N', // native
2392 };
2393
2394 int keyword_tok( const char * text, bool include_intrinsics = false );
2395 int redefined_token( const cbl_name_t name );
2396
2397 class current_tokens_t {
2398 class tokenset_t {
2399 // token_names is initialized from a generated header file.
2400 std::vector<const char *>token_names; // position indicates token value
2401 std::map <std::string, int> tokens; // aliases
2402 std::set<std::string> cobol_words; // Anything in COBOL-WORDS may appear only once.
2403 public:
2404 static std::string
2405 lowercase( const cbl_name_t name ) {
2406 cbl_name_t lname;
2407 std::transform(name, name + strlen(name) + 1, lname, ftolower);
2408 return lname;
2409 }
2410 static std::string
2411 uppercase( const cbl_name_t name ) {
2412 cbl_name_t uname;
2413 std::transform(name, name + strlen(name) + 1, uname, ftoupper);
2414 return uname;
2415 }
2416
2417 public:
2418 tokenset_t();
2419 int find( const cbl_name_t name, bool include_intrinsics );
2420
2421 bool equate( const YYLTYPE& loc, int token,
2422 const cbl_name_t name, const cbl_name_t verb = "EQUATE") {
2423 auto lname( lowercase(name) );
2424 auto cw = cobol_words.insert(lname);
2425 if( ! cw.second ) {
2426 error_msg(loc, "COBOL-WORDS %s: %s may appear but once", verb, name);
2427 return false;
2428 }
2429 auto p = tokens.find(lowercase(name));
2430 bool fOK = p == tokens.end();
2431 if( fOK ) { // name not already in use
2432 tokens[lname] = token;
2433 dbgmsg("%s:%d: %d has alias %s", __func__, __LINE__, token, name);
2434 } else {
2435 error_msg(loc, "%s: %s already defined as a token", verb, name);
2436 }
2437 return fOK;
2438 }
2439 bool undefine( const YYLTYPE& loc,
2440 const cbl_name_t name, const cbl_name_t verb = "UNDEFINE" ) {
2441 auto lname( lowercase(name) );
2442 auto cw = cobol_words.insert(lname);
2443 if( ! cw.second ) {
2444 error_msg(loc, "COBOL-WORDS %s: %s may appear but once", verb, name);
2445 return false;
2446 }
2447
2448 // Do not erase generic, multi-type tokens COMPUTATIONAL and BINARY_INTEGER.
2449 if( binary_integer_usage_of(name) ) {
2450 dbgmsg("%s:%d: generic %s remains valid as a token", __func__, __LINE__, name);
2451 return true;
2452 }
2453
2454 auto p = tokens.find(lname);
2455 bool fOK = p != tokens.end();
2456 if( fOK ) { // name in use
2457 tokens.erase(p);
2458 } else {
2459 error_msg(loc, "%s: %s not defined as a token", verb, name);
2460 }
2461 dbgmsg("%s:%d: %s removed as a valid token name", __func__, __LINE__, name);
2462 return fOK;
2463 }
2464
2465 bool substitute( const YYLTYPE& loc,
2466 const cbl_name_t extant, int token, const cbl_name_t name ) {
2467 return
2468 equate( loc, token, name, "SUBSTITUTE" )
2469 &&
2470 undefine( loc, extant, "SUBSTITUTE" );
2471 }
2472 bool reserve( const YYLTYPE& loc, const cbl_name_t name ) {
2473 auto lname( lowercase(name) );
2474 auto cw = cobol_words.insert(lname);
2475 if( ! cw.second ) {
2476 error_msg(loc, "COBOL-WORDS RESERVE: %s may appear but once", name);
2477 return false;
2478 }
2479 tokens[lname] = -42;
2480 return true;
2481 }
2482 int redefined_as( const cbl_name_t name ) {
2483 auto lname( lowercase(name) );
2484 if( cobol_words.find(lname) != cobol_words.end() ) {
2485 auto p = tokens.find(lname);
2486 if( p != tokens.end() ) {
2487 return p->second;
2488 }
2489 }
2490 return 0;
2491 }
2492 const char * name_of( int tok ) const {
2493 tok -= (255 + 3);
2494 gcc_assert(0 <= tok && size_t(tok) < token_names.size());
2495 return tok < 0? "???" : token_names[tok];
2496 }
2497 };
2498
2499 tokenset_t tokens;
2500 public:
2501 current_tokens_t() {}
2502 int find( const cbl_name_t name, bool include_intrinsics ) {
2503 return tokens.find(name, include_intrinsics);
2504 }
2505 bool equate( const YYLTYPE& loc, const cbl_name_t keyword, const cbl_name_t alias ) {
2506 int token;
2507 if( 0 == (token = binary_integer_usage_of(keyword)) ) {
2508 if( 0 == (token = keyword_tok(keyword)) ) {
2509 error_msg(loc, "EQUATE %s: not a valid token", keyword);
2510 return false;
2511 }
2512 }
2513 auto name = keyword_alias_add(tokens.uppercase(keyword),
2514 tokens.uppercase(alias));
2515 if( name != keyword ) {
2516 error_msg(loc, "EQUATE: %s is already an alias for %s", alias, name.c_str());
2517 return false;
2518 }
2519 return tokens.equate(loc, token, alias);
2520 }
2521 bool undefine( const YYLTYPE& loc, cbl_name_t keyword ) {
2522 return tokens.undefine(loc, keyword);
2523 }
2524 bool substitute( const YYLTYPE& loc, const cbl_name_t keyword, const cbl_name_t alias ) {
2525 int token;
2526 if( 0 == (token = binary_integer_usage_of(keyword)) ) {
2527 if( 0 == (token = keyword_tok(keyword)) ) {
2528 error_msg(loc, "SUBSTITUTE %s: not a valid token", keyword);
2529 return false;
2530 }
2531 }
2532 auto name = keyword_alias_add(tokens.uppercase(keyword),
2533 tokens.uppercase(alias));
2534 if( name != keyword ) {
2535 error_msg(loc, "SUBSTITUTE: %s is already an alias for %s", alias, name.c_str());
2536 return false;
2537 }
2538
2539 dbgmsg("%s:%d: %s (%d) will have alias %s", __func__, __LINE__, keyword, token, alias);
2540 return tokens.substitute(loc, keyword, token, alias);
2541 }
2542 bool reserve( const YYLTYPE& loc, const cbl_name_t name ) {
2543 return tokens.reserve(loc, name);
2544 }
2545 int redefined_as( const cbl_name_t name ) {
2546 return tokens.redefined_as(name);
2547 }
2548 const char * name_of( int tok ) const {
2549 return tokens.name_of(tok);
2550 }
2551 };
2552
2553 cbl_call_convention_t current_call_convention();
2554 current_tokens_t& cdf_current_tokens();
2555
2556 void
2557 current_call_convention( cbl_call_convention_t convention);
2558
2559 class procref_base_t {
2560 private:
2561 const char *section_name, *paragraph_name;
2562 public:
2563 procref_base_t() : section_name(nullptr) , paragraph_name(nullptr) {}
2564 procref_base_t( const char *section_name,
2565 const char *paragraph_name )
2566 : section_name(section_name)
2567 , paragraph_name(paragraph_name)
2568 {}
2569 procref_base_t( const procref_base_t& that )
2570 : section_name(that.section_name)
2571 , paragraph_name(that.paragraph_name)
2572 {}
2573
2574 bool operator<( const procref_base_t& that ) const;
2575 bool operator==( const procref_base_t& that ) const;
2576
2577 const char *section() const { return section_name? section_name : ""; }
2578 const char *paragraph() const { return paragraph_name? paragraph_name : ""; }
2579
2580 bool has_section() const { return section_name != NULL; }
2581 bool has_paragraph() const { return paragraph_name != NULL; }
2582 };
2583
2584 class procref_t : public procref_base_t {
2585 int line;
2586 size_t context; // section called from
2587 public:
2588 procref_t( const char *section, const char *paragraph, int line, size_t context )
2589 : procref_base_t(section, paragraph)
2590 , line(line)
2591 , context(context)
2592 {
2593 assert(line);
2594 assert(context == 0 || cbl_label_of(symbol_at(context))->type == LblSection);
2595 }
2596
2597 int line_number() const { return line; }
2598 };
2599
2600 void procedure_definition_add( size_t program, const cbl_label_t *procedure );
2601 void procedure_reference_add( const char *sect, const char *para,
2602 int line, size_t context );
2603 procref_t * ambiguous_reference( size_t program );
2604
2605 struct symbol_elem_t *
2606 symbol_field_alias( struct symbol_elem_t *e, const char name[] );
2607 struct symbol_elem_t *
2608 symbol_field_alias2( struct symbol_elem_t *e,
2609 struct symbol_elem_t *e2, const char name[] );
2610 struct symbol_elem_t *
2611 symbol_field_same_as( cbl_field_t *tgt, const cbl_field_t *src );
2612
2613 size_t symbol_file_same_record_area( std::list<cbl_file_t*>& files );
2614
2615 bool symbol_currency_add( const char symbol[], const char sign[] = NULL );
2616 const char * symbol_currency( char symbol = '\0' );
2617
2618 const char * symbol_type_str( enum symbol_type_t type );
2619 const char * cbl_field_type_str( enum cbl_field_type_t type );
2620 const char * cbl_logop_str( enum logop_t op );
2621
2622 static inline const char *
2623 refer_type_str( const cbl_refer_t *r ) {
2624 return r && r->field? cbl_field_type_str(r->field->type) : "(none)";
2625 }
2626
2627 enum cbl_field_type_t symbol_field_type( size_t program, const char name[] );
2628
2629 struct symbol_elem_t * symbol_parent( const symbol_elem_t *e );
2630
2631 int length_of_picture(const char *picture);
2632 int rdigits_of_picture(const char *picture);
2633 int digits_of_picture(const char *picture, bool for_rdigits);
2634 bool is_picture_scaled(const char *picture);
2635
2636 template <typename LOC>
2637 void gcc_location_set( const LOC& loc );
2638
2639 // This is slightly oddball. This is an entry point in the charutf8.cc module.
2640 // It's the only entry point in the module, and so it seemed to me wasteful to
2641 // create an entire .h module. So, I stuck it here.
2642 size_t count_characters(const char *in, size_t length);
2643
2644 void current_enabled_ecs( tree ena );
2645
2646 #endif