-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathfield.c
More file actions
3844 lines (3561 loc) · 95.6 KB
/
field.c
File metadata and controls
3844 lines (3561 loc) · 95.6 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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2001-2024 Free Software Foundation, Inc.
Written by Keisuke Nishida, Roger While, Simon Sobisch, Ron Norman,
Edward Hart
This file is part of GnuCOBOL.
The GnuCOBOL compiler is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
GnuCOBOL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GnuCOBOL. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <limits.h>
#include "cobc.h"
#include "tree.h"
#include "../libcob/coblocal.h"
/* sanity checks */
#if COB_MAX_FIELD_SIZE >= INT_MAX
#error COB_MAX_FIELD_SIZE is too big, must be less than INT_MAX
#endif
#if COB_MAX_FIELD_SIZE_LINKAGE >= INT_MAX
#error COB_MAX_FIELD_SIZE_LINKAGE is too big, must be less than INT_MAX
#endif
#if COB_MAX_UNBOUNDED_SIZE >= INT_MAX
#error COB_MAX_UNBOUNDED_SIZE is too big, must be less than INT_MAX
#endif
/* Function prototypes */
static unsigned int validate_field_1 (struct cb_field *f);
static unsigned int validate_multi_value (const struct cb_field * const f);
/* Global variables */
cb_tree cb_depend_check = NULL;
size_t cb_needs_01 = 0;
/* Local variables */
static struct cb_field *last_real_field = NULL;
static int occur_align_size = 0;
static const unsigned char pic_digits[] = { 2, 4, 7, 9, 12, 14, 16, 18 };
#define CB_MAX_OPS 32
static int op_pos = 1, op_val_pos;
static char op_type [CB_MAX_OPS+1];
static char op_prec [CB_MAX_OPS+1];
static cob_s64_t op_val [CB_MAX_OPS+1];
static int op_scale[CB_MAX_OPS+1];
/* Is constant expression list in value really an expression? */
static int
cb_is_expr (cb_tree ch)
{
cb_tree t, l;
int num;
if (op_pos >= 0) {
for (num = 0; num < CB_MAX_OPS; num++) {
op_type [num] = ' ';
op_prec [num] = 0;
op_val [num] = 0;
}
}
op_pos = op_val_pos = -1;
num = 0;
for (l = ch; l; l = CB_CHAIN (l)) {
t = CB_VALUE (l);
if (t && CB_LITERAL_P (t)) {
if (++num > 1)
return 1;
}
}
return 0;
}
static void
cb_eval_op ( void )
{
cob_s64_t lval, rval, xval;
int lscale, rscale, xscale;
if (op_pos >= 0
&& op_val_pos > 0) {
lval = op_val [op_val_pos-1];
lscale = op_scale [op_val_pos-1];
rval = op_val [op_val_pos];
rscale = op_scale [op_val_pos];
op_val_pos--;
switch (op_type [op_pos]) {
case '+':
case '-':
while (lscale > rscale) {
rval = rval * 10;
rscale++;
}
while (lscale < rscale) {
lval = lval * 10;
lscale++;
}
xscale = lscale;
if (op_type [op_pos] == '+')
xval = lval + rval;
else
xval = lval - rval;
break;
case '*':
xscale = lscale + rscale;
xval = lval * rval;
break;
case '/':
while (rscale > 0) {
lval = lval * 10;
rscale--;
}
if (rval == 0) {
xscale = 0;
xval = 0;
cb_error (_("constant expression has Divide by ZERO"));
} else {
xscale = lscale;
xval = lval / rval;
}
break;
case '^':
while (rscale > 0) { /* Only whole number exponents */
rval = rval / 10;
rscale--;
}
if (rval == 0 || lval == 1) {
xval = 1;
xscale = 0;
} else {
xval = lval;
xscale = lscale;
while(--rval > 0) {
xscale = xscale + lscale;
xval = xval * lval;
}
}
break;
case '&':
xscale = 0;
xval = (lval && rval);
break;
case '|':
xscale = 0;
xval = (lval || rval);
break;
case '>':
xscale = 0;
xval = (lval > rval);
break;
case '<':
xscale = 0;
xval = (lval < rval);
break;
case '=':
xscale = 0;
xval = (lval == rval);
break;
case ']':
xscale = 0;
xval = (lval >= rval);
break;
case '[':
xscale = 0;
xval = (lval <= rval);
break;
case '~':
xscale = 0;
xval = (lval != rval);
break;
case '(':
cb_error (_("missing right parenthesis"));
op_pos--;
return;
default:
op_pos--;
return;
}
op_pos--;
while (xscale > 0
&& (xval % 10) == 0) {
xscale--;
xval = xval / 10;
}
op_scale [op_val_pos] = xscale;
op_val [op_val_pos] = xval;
}
}
static void
cb_push_op ( char op, int prec )
{
while (op_pos >= 0
&& op_val_pos > 0
&& prec > 0
&& op_type [op_pos] != '('
&& prec <= op_prec [op_pos]) {
cb_eval_op ();
}
if (op_pos >= CB_MAX_OPS) {
cb_error (_("expression stack overflow at %d entries for operation '%c'"), op_pos, op);
return;
}
op_pos++;
op_type [op_pos] = op;
op_prec [op_pos] = (char) prec;
}
/* Evaluate expression and store as new Numeric Literal */
static cb_tree
cb_evaluate_expr (cb_tree ch, int normal_prec)
{
cb_tree t, l;
cob_s64_t xval;
int unop = 1, xscale, k;
char result[48];
struct cb_literal *lp;
for (l = ch; l; l = CB_CHAIN (l)) {
t = CB_VALUE (l);
if (t && CB_LITERAL_P (t)) {
lp = CB_LITERAL (t);
if (CB_NUMERIC_LITERAL_P (t)) {
xval = atoll((const char *)lp->data);
xscale = lp->scale;
if (unop) {
if (lp->sign < 0) /* Unary op, change sign */
xval = -xval;
} else {
if (lp->sign < 0) { /* Treat 'sign' as binary op */
cb_push_op ('-', 4);
} else if (lp->sign > 0) {
cb_push_op ('+', 4);
}
}
while (xscale > 0
&& (xval % 10) == 0) { /* Remove decimal zeros */
xscale--;
xval = xval / 10;
}
if (op_val_pos >= CB_MAX_OPS) {
cb_error (_("expression stack overflow at %d entries"), op_val_pos);
return cb_error_node;
}
op_val_pos++;
op_val [op_val_pos] = xval;
op_scale [op_val_pos] = xscale;
unop = 0;
} else {
switch (lp->data[0]) {
case '(':
cb_push_op ('(', 0);
unop = 1;
break;
case ')':
unop = 0;
for (k=op_pos; k > 0 && op_type[k] != '('; k--);
if (op_type [k] != '(')
cb_error (_("missing left parenthesis"));
while (op_pos >= 0
&& op_val_pos > 0) {
if (op_type [op_pos] == '(') {
break;
}
cb_eval_op ();
}
if (op_pos >= 0
&& op_type [op_pos] == '(')
op_pos--;
break;
case '+':
cb_push_op ('+', 4);
unop = 1;
break;
case '-':
cb_push_op ('-', 4);
unop = 1;
break;
case '*':
cb_push_op ('*', normal_prec ? 6 : 4);
unop = 1;
break;
case '/':
cb_push_op ('/', normal_prec ? 6 : 4);
unop = 1;
break;
case '&':
cb_push_op ('&', normal_prec ? 8 : 4);
unop = 1;
break;
case '|':
cb_push_op ('|', normal_prec ? 8 : 4);
unop = 1;
break;
case '^':
cb_push_op ('^', normal_prec ? 7 : 4);
unop = 1;
break;
default:
cb_error (_("invalid operator '%s' in expression"),lp->data);
break;
}
}
}
}
while (op_pos >= 0
&& op_val_pos > 0) {
if (op_type [op_pos] == '(') {
cb_error (_("missing right parenthesis"));
op_pos--;
continue;
}
cb_eval_op ();
}
if (op_pos >= 0) {
if (op_type[op_pos] == '(') {
cb_error (_("missing right parenthesis"));
} else {
cb_error (_("'%c' operator misplaced"), op_type [op_pos]);
}
}
xval = op_val [0];
xscale = op_scale [0];
while (xscale > 0) { /* Reduce to 'fixed point numeric' */
xscale--;
xval = xval / 10;
}
while (xscale < 0) { /* Reduce to 'fixed point numeric' */
xscale++;
xval = xval * 10;
}
sprintf (result, CB_FMT_LLD, xval);
return cb_build_numeric_literal (0, result, xscale);
}
int
cb_get_level (cb_tree x)
{
#if 1 /* level always contains a valid tree with valid numeric values only
--> all validation is done in scanner.l */
return atoi (CB_NAME (x));
#else
const unsigned char *p;
const char *name;
int level;
if (CB_INVALID_TREE (x)) {
return 0;
}
name = CB_NAME (x);
level = 0;
/* Get level */
for (p = (const unsigned char *)name; *p; p++) {
if (!isdigit ((int)(*p))) {
goto level_error;
}
level = level * 10 + (COB_D2I(*p));
if (level > 88) {
goto level_error;
}
}
/* Check level */
switch (level) {
case 66:
case 77:
case 78:
case 88:
break;
default:
if (level < 1 || level > 49) {
goto level_error;
}
break;
}
return level;
level_error:
cb_error_x (x, _("invalid level number '%s'"), name);
return 0;
#endif
}
cb_tree
cb_build_field_tree (const int level, cb_tree name, struct cb_field *last_field,
enum cb_storage storage, struct cb_file *fn,
const int expl_level)
{
struct cb_reference *r;
struct cb_field *f;
struct cb_field *p;
cb_tree l;
cb_tree x;
int lv;
if (!expl_level) {
/* note: the level number is always a valid tree here, but the
name may be a defined constant which leads to an error node */
if (name == cb_error_node) {
return cb_error_node;
}
/* Check the level number */
lv = level;
#if 0 /*level is always valid --> 01 thru 49, 77, 66, 78, 88 */
if (!lv) {
return cb_error_node;
}
#endif
} else {
lv = expl_level;
}
/* Build the field */
r = CB_REFERENCE (name);
f = CB_FIELD (cb_build_field (name));
f->storage = storage;
last_real_field = last_field;
if (lv == 78) {
f->level = 01;
f->flag_item_78 = 1;
f->flag_constant = 0;
return CB_TREE (f);
} else {
f->level = lv;
}
/* copy EXTERNAL / GLOBAL attribute from file to record */
if (f->level == 01 && storage == CB_STORAGE_FILE && fn) {
if (fn->flag_external) {
f->flag_external = 1;
current_program->flag_has_external = 1;
} else if (fn->flag_global) {
f->flag_is_global = 1;
}
}
if (last_field) {
if (last_field->level == 77 && f->level != 01
&& f->level != 77 && f->level != 66 && f->level != 88) {
cb_error_x (name, _("level number must begin with 01 or 77"));
return cb_error_node;
}
}
/* Checks for redefinition */
if (get_warn_opt_value (cb_warn_redefinition)
&& r->word->count > 1 && !r->flag_filler_ref) {
if (f->level == 01 || f->level == 77) {
redefinition_warning (name, NULL);
} else {
for (l = r->word->items; l; l = CB_CHAIN (l)) {
x = CB_VALUE (l);
if (!CB_FIELD_P (x)
|| CB_FIELD (x)->level == 01
|| CB_FIELD (x)->level == 77
|| ( last_field
&& f->level == last_field->level
&& CB_FIELD (x)->parent == last_field->parent)) {
redefinition_warning (name, x);
break;
}
}
}
}
if (last_field && last_field->level == 88) {
last_field = last_field->parent;
}
/* Link the field into the tree */
if (f->level == 01 || f->level == 77) {
/* Top level */
cb_needs_01 = 0;
if (last_field) {
cb_field_founder (last_field)->sister = f;
}
} else if (!last_field || cb_needs_01) {
/* Invalid top level */
cb_error_x (name, _("level number must begin with 01 or 77"));
return cb_error_node;
} else if (f->level == 66) {
/* Level 66 */
f->parent = cb_field_founder (last_field);
for (p = f->parent->children; p && p->sister; p = p->sister) ;
if (p) {
p->sister = f;
}
} else if (f->level == 88) {
/* Level 88 */
f->parent = last_field;
if (last_real_field && last_real_field->level == 88) {
/* Level 88 sister */
last_real_field->sister = f;
} else {
/* First Level 88 on this item */
last_field->validation = f;
last_field = f;
}
} else if (f->level > last_field->level) {
/* Lower level */
last_field->children = f;
f->parent = last_field;
} else if (f->level == last_field->level) {
/* Same level; note:
last_field is a group if coming from "goto" */
same_level:
last_field->sister = f;
f->parent = last_field->parent;
} else {
/* Upper level */
for (p = last_field->parent; p /* <- silence warnings */; p = p->parent) {
if (p->level == f->level) {
last_field = p;
goto same_level;
}
if (cb_relax_level_hierarchy && p->level < f->level) {
break;
}
}
/* always generate dummy filler field to prevent
parsing of follow-on fields to fail the same way */
if (p) /* <- silence warnings */ {
cb_tree dummy_fill = cb_build_filler ();
struct cb_field *field_fill = CB_FIELD (cb_build_field (dummy_fill));
field_fill->level = f->level;
field_fill->flag_filler = 1;
field_fill->storage = storage;
field_fill->children = p->children;
field_fill->parent = p;
for (p = p->children; p; p = p->sister) {
p->parent = field_fill;
}
field_fill->parent->children = field_fill;
field_fill->sister = f;
f->parent = field_fill->parent;
/* last_field = field_fill; */
}
if (cb_relax_level_hierarchy) {
cb_warning_x (COBC_WARN_FILLER, name,
_("no previous data item of level %02d"),
f->level);
} else {
cb_error_x (name,
_("no previous data item of level %02d"),
f->level);
}
}
/* Inherit parents properties */
if (f->parent) {
struct cb_field *parent = f->parent;
f->usage = parent->usage;
f->indexes = parent->indexes;
f->flag_sign_leading = parent->flag_sign_leading;
f->flag_sign_separate = parent->flag_sign_separate;
f->flag_is_global = parent->flag_is_global;
if (f->level <= 66) {
f->flag_volatile = parent->flag_volatile;
}
if (f->storage == CB_STORAGE_SCREEN) {
f->screen_foreg = parent->screen_foreg;
f->screen_backg = parent->screen_backg;
f->screen_prompt = parent->screen_prompt;
f->screen_control = parent->screen_control;
f->screen_color = parent->screen_color;
}
}
return CB_TREE (f);
}
cb_tree
cb_build_full_field_reference (struct cb_field* field)
{
cb_tree ret = NULL;
cb_tree ref = NULL;
for (; field; field = field->parent) {
if (!field->flag_filler) {
cb_tree rchain = cb_build_reference (field->name);
if (ref) {
CB_REFERENCE (ref)->chain = rchain;
} else {
ret = rchain;
}
ref = rchain;
}
}
return ret;
}
struct cb_field *
cb_resolve_redefines (struct cb_field *field, cb_tree redefines)
{
struct cb_field *f;
struct cb_reference *r;
const char *name;
cb_tree x;
cb_tree candidate = NULL;
cb_tree items;
r = CB_REFERENCE (redefines);
name = CB_NAME (redefines);
x = CB_TREE (field);
/* Check qualification */
if (r->chain) {
cb_error_x (x, _("'%s' cannot be qualified here"), name);
return NULL;
}
/* Check subscripts */
if (r->subs) {
cb_error_x (x, _("'%s' cannot be subscripted here"), name);
return NULL;
}
/* Get last defined name */
/* note: chaining over these are much faster than chaining over the complete
parent using strcasecmp */
for (items = r->word->items; items; items = CB_CHAIN (items)) {
const cb_tree value = CB_VALUE (items);
if (value != x && CB_FIELD_P (value)) {
candidate = value;
/* we want to get the last, so no "break" here */
}
}
if (!candidate) {
if (field->parent) {
cb_error_x (x, _("'%s' is not defined in '%s'"),
name, field->parent->name);
} else {
undefined_error (redefines);
}
return NULL;
}
f = CB_FIELD_PTR (candidate);
/* Check if candidate is in the current group (if any) */
if (field->parent && field->parent != f->parent) {
cb_error_x (x, _ ("'%s' is not defined in '%s'"),
name, field->parent->name);
return NULL;
}
/* Check level number */
if (f->level != field->level) {
cb_error_x (x, _("level number of REDEFINES entries must be identical"));
return NULL;
}
if (!cb_indirect_redefines && f->redefines) {
cb_error_x (x, _("'%s' is not the original definition"), f->name);
return NULL;
}
/* Return the original definition */
while (f->redefines) {
f = f->redefines;
}
return f;
}
static void copy_into_field_recursive (struct cb_field *, struct cb_field *, const int);
static void
copy_duplicated_field_into_field (struct cb_field *field, struct cb_field *target,
const int level, const int outer_indexes, const enum cb_storage storage)
{
cb_tree x;
if (!field->flag_filler && field->name) {
x = cb_build_field_tree (0, cb_build_reference (field->name),
target, storage, NULL, level);
} else {
x = cb_build_field_tree (0, cb_build_filler (),
target, storage, NULL, level);
}
if (x == cb_error_node) {
return;
}
copy_into_field_recursive (field, CB_FIELD (x), outer_indexes);
}
static void
copy_validation (struct cb_field *source, struct cb_field *target)
{
struct cb_field *val, *last_val;
#if 0 /* in case we want to allow combining condition-names of typedef and field */
for (last_val = target->validation; last_val; last_val = last_val->sister) {
/* get to the last validation entry*/
if (!last_val->sister) {
break;
}
}
#else
if (target->validation) {
(void) cb_syntax_check_x (CB_TREE (target->validation), _("duplicate %s"), "level 88");
}
#endif
for (val = source->validation; val; val = val->sister) {
/* create content-name and link into the reference list */
cb_tree x = cb_build_field_tree (88, cb_build_reference (val->name),
target, target->storage, target->file, 0);
last_val = CB_FIELD (x);
/* directly assign the typef's value + false (no need for copy) */
last_val->values = val->values;
last_val->false_88 = val->false_88;
}
}
static void
copy_children (struct cb_field *child, struct cb_field *target,
const int level, const int outer_indexes, const enum cb_storage storage)
{
int level_child;
if (child->level > level) {
level_child = child->level;
} else {
level_child = level + 1;
/* ensure that we don't set the "virtual level number" to one of
the "special" level numbers */
if (level_child == 66 || level_child == 78 || level_child == 88) {
level_child++;
} else if (level_child == 77) {
level_child = 79;
}
}
copy_duplicated_field_into_field (child, target, level_child,
outer_indexes, storage);
}
#define field_attribute_copy(attribute) \
if (source->attribute) target->attribute = source->attribute
#define field_attribute_override(attribute) \
target->attribute = source->attribute
static void
copy_into_field_recursive (struct cb_field *source, struct cb_field *target,
const int outer_indexes)
{
field_attribute_override (usage);
field_attribute_override (occurs_min);
field_attribute_override (occurs_max);
field_attribute_override (flag_occurs);
if (CB_VALID_TREE (source->depending)) {
#if 0 /* TODO: check if DEPENDING field is part of the original TYPEDEF,
if yes then full-qualify the reference */
struct cb_field *dep_field = CB_FIELD_PTR (source->depending);
struct cb_field *field;
target->depending = cb_build_reference (CB_NAME(source->depending));
dep_field = dep_field->parent;
if (dep_field) {
for (field = target->parent; field; field = field->parent) {
if (dep_field == field) {
cb_tree rchain = cb_build_full_field_reference (field);
CB_REFERENCE (target->depending)->chain = rchain;
break;
}
}
}
#else
target->depending = cb_build_reference (CB_NAME (source->depending));
#endif
CB_ADD_TO_CHAIN (target->depending, current_program->reference_list);
}
field_attribute_override (nkeys);
if (source->keys) {
int i;
/* create reference chain all the way up
as later fields may have same name */
const cb_tree rchain = cb_build_full_field_reference (target);
target->keys = cobc_parse_malloc (sizeof (struct cb_key) * target->nkeys);
for (i = 0; i < target->nkeys; i++) {
const struct cb_reference* r = CB_REFERENCE (source->keys[i].key);
const cb_tree ref = cb_build_reference (r->word->name);
CB_REFERENCE (ref)->chain = rchain;
target->keys[i].key = ref;
CB_ADD_TO_CHAIN (ref, current_program->reference_list);
field_attribute_override (keys[i].dir);
}
}
if (source->index_list) {
cb_tree x;
target->index_list = NULL;
for (x = source->index_list; x; x = CB_CHAIN (x)) {
cb_tree ind_ref = cb_build_reference (CB_FIELD_PTR (CB_VALUE (x))->name);
cb_tree entry = cb_build_index (ind_ref, cb_int1, 1U, target);
CB_FIELD_PTR (entry)->index_type = CB_STATIC_INT_INDEX;
if (!target->index_list) {
target->index_list = CB_LIST_INIT (entry);
} else {
target->index_list = cb_list_add (target->index_list, entry);
}
}
}
field_attribute_override (values);
field_attribute_override (flag_blank_zero);
field_attribute_override (flag_justified);
field_attribute_override (flag_sign_clause);
field_attribute_override (flag_sign_leading);
field_attribute_override (flag_sign_separate);
field_attribute_override (flag_synchronized);
field_attribute_override (flag_sync_right);
field_attribute_override (flag_sync_left);
field_attribute_override (flag_any_length);
field_attribute_override (flag_any_numeric);
field_attribute_override (flag_invalid);
field_attribute_override (flag_item_based);
field_attribute_override (flag_is_pointer);
/* Note: attributes must be handled both here and in copy_into_field */
/* TODO: add copying of align clause and other boolean/bit stuff once added */
if (CB_VALID_TREE (source->redefines)) {
cb_tree ref = cb_build_reference (source->redefines->name);
target->redefines = cb_resolve_redefines (target, ref);
}
/* copy all level 88 */
if (source->validation) {
copy_validation (source, target);
}
if (source->children) {
copy_children (source->children, target, target->level, outer_indexes, target->storage);
} else if (source->pic){
/* take over internal PICTURE representation as-is, no use in re-building
that from scratch and handle calculated ->pic special */
target->pic = cobc_parse_malloc (sizeof (struct cb_picture));
memcpy (target->pic, source->pic, sizeof (struct cb_picture));
}
if (source->sister) {
/* for children: all sister entries need to be copied */
copy_duplicated_field_into_field (source->sister,
target, target->level, outer_indexes, target->storage);
}
/* special case: normally incremented during parse */
target->indexes = source->indexes + outer_indexes;
cb_validate_field (target);
}
/* note: same message in parser.y */
static void
duplicate_clause_message (cb_tree x, const char *clause)
{
(void) cb_syntax_check_x (x, _("duplicate %s clause"), clause);
}
void
copy_into_field (struct cb_field *source, struct cb_field *target)
{
#if 0
cb_tree external_definition = target->external_definition;
#endif
/* note: EXTERNAL is always applied from the typedef (if level 1/77),
but may be specified on the field;
note: MF has different syntax rules and _only_ allows it on the field */
if (target->level == 1 || target->level == 77) {
field_attribute_copy (flag_external);
if (target->flag_external
&& !target->ename) {
#if 1 /* CHECKME: Which one to use? Possibly depending on AS clause? */
target->ename = source->ename;
#else
target->ename = cb_to_cname (target->name);
#endif
}
}
target->usage = source->usage;
target->common.category = source->common.category;
/* Note: The attributes GLOBAL and SELECT WHEN are never included;
SAME AS does not include EXTERNAL, but the TYPEDEF */
if (source->values) {
if (target->values) {
duplicate_clause_message (target->values, "VALUE");
} else {
target->values = source->values;
}
}
field_attribute_copy (flag_blank_zero);
field_attribute_copy (flag_justified);
field_attribute_copy (flag_sign_clause);
field_attribute_copy (flag_sign_leading);
field_attribute_copy (flag_sign_separate);
if (source->flag_synchronized
&& !target->flag_synchronized) {
target->flag_synchronized = source->flag_synchronized;
target->flag_sync_right = source->flag_sync_right;
target->flag_sync_left = source->flag_sync_left;
}
field_attribute_override (flag_any_length);
field_attribute_override (flag_any_numeric);
field_attribute_override (flag_invalid);
field_attribute_copy (flag_item_based);
field_attribute_override (flag_is_pointer);
/* Note: attributes must be handled both here and in copy_into_field_recursive */
/* copy all level 88 */
if (source->validation) {
copy_validation (source, target);
}
if (unlikely (!target->like_modifier)) {
if (source->children) {
copy_children (source->children, target, target->level, target->indexes, target->storage);
} else if (source->pic) {
/* take over internal PICTURE representation as-is, no use in re-building
that from scratch and in handling calculated ->pic special */
target->pic = cobc_parse_malloc (sizeof (struct cb_picture));
memcpy (target->pic, source->pic, sizeof (struct cb_picture));
}
} else {
struct cb_picture *new_pic = NULL;
int modifier = cb_get_int (target->like_modifier);
if (modifier) {
switch (target->usage) {
case CB_USAGE_COMP_X:
case CB_USAGE_COMP_N:
if (target->pic->category == CB_CATEGORY_ALPHANUMERIC) {
char pic[8];
unsigned char newsize;
if (target->pic->size > 8) {
newsize = 36;
} else {
newsize = pic_digits[target->pic->size - 1];
}
newsize += (unsigned char)modifier;
if (newsize > 36) {
newsize = 36;
}
sprintf (pic, "9(%u)", newsize);
new_pic = cb_build_picture (pic);
break;
}
case CB_USAGE_BINARY:
case CB_USAGE_PACKED:
case CB_USAGE_COMP_5:
case CB_USAGE_COMP_6:
if (target->pic->orig[0] == '9') {
char pic[38];
/* only a prototype here,
TODO: add handling for S and friends... */
if (modifier > 0) {
sprintf (pic, "9(%d)", modifier);
strcat (pic, target->pic->orig);
new_pic = cb_build_picture (pic);
} else {
CB_PENDING_X (CB_TREE (target), "LIKE ... negative-integer");
}
} else {
cb_error_x (CB_TREE (target), _ ("%s clause not compatible with PIC %s"),
"LIKE", target->pic->orig);
target->flag_invalid = 1;
}
break;
case CB_USAGE_DISPLAY:
case CB_USAGE_NATIONAL:
break;
default:
cb_error_x (CB_TREE (target), _("%s clause not compatible with USAGE %s"),
"LIKE", cb_get_usage_string (target->usage));
target->flag_invalid = 1;