-
Notifications
You must be signed in to change notification settings - Fork 0
/
yacc.y
2003 lines (1763 loc) · 71 KB
/
yacc.y
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
/*
FILE NAME: yacc.y
Copyright (C) 1997-2016 Vladimir Makarov.
Written by Vladimir Makarov <[email protected]>
This file is part of the tool SPRUT.
This 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 2, or (at your option)
any later version.
This software 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 GNU CC; see the file COPYING. If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
TITLE: Parser of SPRUT (internal representation description
translator) described as YACC file
DESCRIPTION: This file makes lexical, syntactic analysis
(with possible syntactic error recovery) of one or more
files and builds up internal representation of parsed internal
representation description.
SPECIAL CONSIDERATION:
Defining macro `NDEBUG' (e.g. by option `-D' in C compiler
command line) during the file compilation disables to fix
some internal errors of parser work and errors of its usage.
*/
%{
#ifdef HAVE_CONFIG_H
#include "config.h"
#else /* In this case we are oriented to ANSI C */
#endif /* #ifdef HAVE_CONFIG_H */
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "vlobject.h"
#include "position.h"
#include "errors.h"
#include "ird.h"
#include "common.h"
#include "tab.h"
#include "yacc.h"
#include <assert.h>
/* Attributes of all YACC symbols (token and nonterminals) will be
of given type. */
#define YYSTYPE IR_node_t
/* This macro value is used for correct calculation of current position in
file when TAB character is processed. */
#define TAB_STOP 8
/* The following variables and forward definitions of functions are
used in actions of YACC grammar. */
/* The following variable value is begin position of the last lexema returned
by the function `yylex'. */
static position_t current_lexema_position;
/* The following variable value is flag of that the last list of
immediate super types is list of basic immediate super types
(i.e. this list does not start with comma -- see syntax). */
static int current_basic_super_types_flag;
static int file_has_been_already_processed (const char *new_file_name);
static void start_scanner_file (const char *new_file_name,
position_t error_position);
static void finish_scanner_file (void);
static void add_lexema_to_input_file (int lexema_code);
static void syntax_error (const char *s);
static void append_additional_code (IR_node_t additional_code);
static void append_type_list (IR_node_t type_list);
static void append_double_declaration (IR_node_t double_declaration);
static IR_node_t merge_and_set_field_declaration_part_and_class_double_fields
(IR_node_t class_fields, IR_node_t skeleton_fields, IR_node_t other_fields);
static void process_node_type_list
(IR_node_t last_node_type, int abstract_flag,
IR_node_t first_super_type_list_element,
IR_node_t last_super_type_list_element, int basic_super_types_flag,
IR_node_t field_list);
static const char *full_extended_file_name (const char *extend_identifier);
static void finish_parser_file (void);
%}
/* To process included files (construction `%extend ...') the parser
could call itself recursively. But according to POSIX the utility
YACC does not give such feature. Therefore the parser does not make
switching process of other source files. The scanner executes all
this work (see commentaries for scanner). Such implementation
requires enrichment of syntax which reflects that basic description
part (with lexema `EXTENSION') may be after construction `%extend
....' and robust error recovery within a description part.
To disable influence syntactic errors on fragments of file started
with unique lexema (see rules for nonterminals
`predefined_types_declaration' and `double_nodes_declaration')
`yyerrok' is used.
The implementation of processing construction `%extend ...' by
parser require robust error recovery within a description part, i.e.
syntactic errors fixed in basic description part may not affect fixing
syntactic errors in original description part. Such robust error
recovery is to be implemented also by using `yyerrok' (see rule for
nonterminal basic_description_part).
Also when function yyerror fixed too many syntactic errors it may
skip up to end of file of current compilation unit. But it should
be remembered that all `error' constructions in rules must have
token `END_OF_BASIC_FILE' as look ahead token set (this is achieved
by additional error rules). Otherwise the parser will move in
cycles.
See commentaries below for abstract data `syntax_errors'
about used method of syntactic error reporting. */
/* The attributes of the following tokens are not defined and not used. */
%token PERCENTS COMMA COLON DOUBLE_COLON SEMICOLON
EXTENSION END_OF_BASIC_FILE
DOUBLE EXTEND LOCAL IMPORT EXPORT TYPE ROOT ABSTRACT CLASS SKELETON
OTHER
/* The attributes of the following tokens are defined and used. */
%token IDENTIFIER CODE_INSERTION EXPRESSION ADDITIONAL_C_CODE
%start description
%%
/* The attribute of the following nonterminal is not defined and not used. */
description : {
IR_node_t description_part;
/* Create new description part. */
description_part = IR_create_node (IR_NM_description_part);
IR_set_position (description_part, current_position);
IR_set_description_part_level (description_part, 0);
IR_set_first_basic_description_part (description_part,
NULL);
IR_set_next_basic_description_part (description_part,
NULL);
IR_set_last_code_insertion (description_part, NULL);
IR_set_last_double_declaration (description_part, NULL);
IR_set_last_type (description_part, NULL);
IR_set_last_additional_code (description_part, NULL);
if (current_description_part != NULL)
{
IR_node_t last_basic_description_part;
IR_node_t current_part;
last_basic_description_part = NULL;
for (current_part
= IR_first_basic_description_part
(current_description_part);
current_part != NULL;
current_part
= IR_next_basic_description_part (current_part))
last_basic_description_part = current_part;
if (last_basic_description_part == NULL)
IR_set_first_basic_description_part
(current_description_part, description_part);
else
IR_set_next_basic_description_part
(last_basic_description_part, description_part);
}
current_description_part = description_part;
}
declaration_part PERCENTS node_type_definition_list
ADDITIONAL_C_CODE
{append_additional_code ($5);}
;
/* A symbol with attribute equal to node representing identifier
in constructions `%extend ...' must before this nonterminal.
The attribute of this nonterminal is not defined and not used. */
basic_description_part :
| {
yyerrok;
}
EXTENSION
{
const char *file_name;
file_name
= full_extended_file_name (IR_identifier_itself
($0));
start_parser_file (file_name, IR_position ($0));
$$ = current_description_part;
}
description END_OF_BASIC_FILE
{
yyerrok;
finish_parser_file ();
IR_set_description_part_level
($3,
IR_description_part_level
(current_description_part) + 1);
current_description_part = $3;
}
;
/* A symbol with attribute equal to identifier in the first
construction `%extend ...' (or NULL) in source file must before this
nonterminal. The attribute of the following nonterminal is not
defined and not used. */
declaration_part
:
| declaration_part double_nodes_declaration
| declaration_part EXTEND extend_identifier_list
| declaration_part predefined_types_declaration
| declaration_part code_insertion
{
if (IR_last_code_insertion (current_description_part) != NULL)
{
IR_set_next_code_insertion
($2, IR_next_code_insertion (IR_last_code_insertion
(current_description_part)));
IR_set_next_code_insertion
(IR_last_code_insertion (current_description_part), $2);
}
else
IR_set_next_code_insertion ($2, $2);
IR_set_last_code_insertion (current_description_part, $2);
}
| declaration_part error
{syntax_error ("syntax error in declaration");}
;
/* The attribute of this nonterminal is not defined and not used. */
extend_identifier_list : extend_identifier
| extend_identifier_list extend_identifier
;
/* The attribute of this nonterminal is not defined and not used. */
extend_identifier
: IDENTIFIER
{
/* The attribute of this action (`$$') is used in rule with
nonterminal `basic_description_part' in the left hand
side. The value of the attribute is node representing
identifier in construction `%extend ...' in current
source file. */
const char *file_name;
$$ = $1;
file_name = full_extended_file_name (IR_identifier_itself ($1));
if (!file_has_been_already_processed (file_name))
/* It is first construction `%extend ...' with given
identifier -- start parsing new description part. */
add_lexema_to_input_file (EXTENSION);
else if (v_flag)
warning
(IR_position ($1),
"warning -- repeated extension of description `%s' is ignored",
IR_identifier_itself ($1));
}
basic_description_part
;
/* Attribute of this nonterminal is node representing last of
predefined type of cyclic list of predefined type in current
description part. */
predefined_types_declaration
: TYPE {yyerrok;}
| predefined_types_declaration IDENTIFIER
{
$$ = IR_create_node (IR_NM_predefined_type);
IR_set_position ($$, IR_position ($2));
IR_set_type_identifier ($$, $2);
IR_set_next_type ($$, $$);
/* Append cyclic list of one node to the list of current
description part. */
append_type_list ($$);
}
| predefined_types_declaration error
{syntax_error ("syntax error in predefined type declaration");}
;
/* Attribute of this nonterminal is node representing last of
identifier in double node type declaration of cyclic list of such
identifiers in current description part. */
double_nodes_declaration
: DOUBLE {yyerrok;}
| double_nodes_declaration any_node_type_name
{
$$ = IR_create_node (IR_NM_double_declaration);
IR_set_position ($$, IR_position ($2));
IR_set_double_declaration_identifier ($$, $2);
/* Append node to the list of current description part. */
append_double_declaration ($$);
}
| double_nodes_declaration error
{
syntax_error ("syntax error in double node type declaration");
}
;
/* Attribute of this nonterminal is node representing corresponding
local or export/import code. */
code_insertion : LOCAL CODE_INSERTION
{
$$ = IR_create_node (IR_NM_local_code);
IR_set_position ($$, IR_position ($2));
IR_set_code_itself ($$, $2);
}
| IMPORT CODE_INSERTION
{
$$ = IR_create_node (IR_NM_import_code);
IR_set_position ($$, IR_position ($2));
IR_set_code_itself ($$, $2);
}
| EXPORT CODE_INSERTION
{
$$ = IR_create_node (IR_NM_export_code);
IR_set_position ($$, IR_position ($2));
IR_set_code_itself ($$, $2);
}
;
/* The attribute of the following nonterminal is not defined and not
used. */
node_type_definition_list
: node_type_definition
| error
{
syntax_error ("syntax error in node type definition");
}
| node_type_definition_list SEMICOLON node_type_definition
;
/* The attribute of the following nonterminal is not defined and not
used. */
node_type_definition
:
| abstract_node_flag node_type_identifier_list
optional_immediate_super_type_list class_field_definition_part
skeleton_field_definition_part other_field_definition_part
{
IR_node_t last_field;
IR_node_t last_super_type_list_element;
last_field
= merge_and_set_field_declaration_part_and_class_double_fields
($4, $5, $6);
last_super_type_list_element = $3;
if ($3 != NULL)
{
/* Make uncyclic list. */
$3 = IR_next_super_type_list_element ($3);
IR_set_next_super_type_list_element (last_super_type_list_element,
NULL);
}
process_node_type_list
($2, $1 != NULL, $3, last_super_type_list_element,
current_basic_super_types_flag, last_field);
}
;
/* Any non-nil value of attribute of this nonterminal means that
keyword abstract is present. */
abstract_node_flag : {$$ = NULL;}
| ABSTRACT {$$ = root_identifier;}
;
/* Attribute of this nonterminal is node representing last of node
type of cyclic list of node types in given node type identifier
list. */
node_type_identifier_list : node_type_identifier
{
IR_set_next_type ($1, $1);
$$ = $1;
}
| node_type_identifier_list COMMA
node_type_identifier
{
assert ($1 != NULL);
IR_set_next_type ($3, IR_next_type ($1));
IR_set_next_type ($1, $3);
$$ = $3;
}
;
/* Attribute of this nonterminal is node representing corresponding
node type. */
node_type_identifier : IDENTIFIER
{
$$ = IR_create_node (IR_NM_node_type);
IR_set_position ($$, IR_position ($1));
IR_set_type_identifier ($$, $1);
IR_set_double_node_flag ($$, FALSE);
IR_set_last_field ($$, NULL);
}
;
/* Attribute of this nonterminal is node representing last immediate
super type list element of cyclic list of them. There is
additional attribute `current_basic_super_types_flag' of the
following nonterminal. */
optional_immediate_super_type_list
:
{
current_basic_super_types_flag = FALSE;
$$ = NULL;
}
| immediate_super_type_list
{
$$ = $1;
}
;
/* Attribute of this nonterminal is node representing last immediate
super type list element of cyclic list of them. There is
additional attribute `current_basic_super_types_flag' of the
following nonterminal. */
immediate_super_type_list
: DOUBLE_COLON any_node_type_name
{
current_basic_super_types_flag = TRUE;
$$ = IR_create_node (IR_NM_super_type_list_element);
IR_set_position ($$, IR_position ($2));
IR_set_immediate_super_type_identifier ($$, $2);
/* Create cyclic list. */
IR_set_next_super_type_list_element ($$, $$);
}
| DOUBLE_COLON COMMA any_node_type_name
{
current_basic_super_types_flag = FALSE;
$$ = IR_create_node (IR_NM_super_type_list_element);
IR_set_position ($$, IR_position ($3));
IR_set_immediate_super_type_identifier ($$, $3);
/* Create cyclic list. */
IR_set_next_super_type_list_element ($$, $$);
}
| immediate_super_type_list COMMA any_node_type_name
{
assert ($1 != NULL);
$$ = IR_create_node (IR_NM_super_type_list_element);
IR_set_position ($$, IR_position ($3));
IR_set_immediate_super_type_identifier ($$, $3);
/* Add new element to the cyclic list. */
IR_set_next_super_type_list_element
($$, IR_next_super_type_list_element ($1));
IR_set_next_super_type_list_element ($1, $$);
}
;
/* Attribute of this nonterminal is node representing given identifier. */
any_node_type_name : ROOT
{
/* Copy is needed because any identifier node
representing identifier occurrence (see
commentaries for node `identifier'). */
$$ = IR_copy_node (root_identifier);
IR_set_position ($$, current_lexema_position);
}
| IDENTIFIER {$$ = $1;}
;
/* Attribute of this nonterminal is node representing last field, action
or constraint of cyclic list of class fields (or NULL) in given field
definition list. */
class_field_definition_part : {$$ = NULL;}
| CLASS field_definition_list {$$ = $2;}
;
/* Attribute of this nonterminal is node representing last field, action
or constraint of cyclic list of skeleton fields (or NULL) in given field
definition list. */
skeleton_field_definition_part : {$$ = NULL;}
| SKELETON field_definition_list {$$ = $2;}
;
/* Attribute of this nonterminal is node representing last field, action
or constraint of cyclic list of other fields (or NULL) in given field
definition list. */
other_field_definition_part : {$$ = NULL;}
| OTHER field_definition_list {$$ = $2;}
;
/* Attribute of this nonterminal is node representing last field, action
or constraint of cyclic list of them in given field definition list. */
field_definition_list : {$$ = NULL;}
| field_definition_list field_definition
{
if ($1 != NULL)
{
$$ = IR_next_field ($2);
IR_set_next_field ($2, IR_next_field ($1));
IR_set_next_field ($1, $$);
}
$$ = $2;
}
| field_definition_list constraint_or_action
{
if ($1 != NULL)
{
IR_set_next_field ($2, IR_next_field ($1));
IR_set_next_field ($1, $2);
}
else
IR_set_next_field ($2, $2);
$$ = $2;
}
| field_definition_list error
{
syntax_error ("syntax error in field definition");
$$ = $1;
}
;
/* Attribute of this nonterminal is node representing constraint or action. */
constraint_or_action : constraint {$$ = $1;}
| action {$$ = $1;}
;
/* Attribute of this nonterminal is node representing last field of cyclic
list of them in given field definition. */
field_definition : field_identifier_list COLON optional_double
any_node_type_name
{
$$ = $1;
assert ($1 != NULL);
/* Set up `double_field_flag' for all fields
of the list. */
do
{
IR_set_double_field_flag ($1, $3 != NULL);
IR_set_field_type_identifier ($1, $4);
$1 = IR_next_field ($1);
}
while ($1 != $$);
}
;
/* Any non-nill value of attribute of this nonterminal means that keyword
double is present. */
optional_double : {$$ = NULL;}
| DOUBLE {$$ = root_identifier;}
;
/* Attribute of this nonterminal is node representing constraint. */
constraint : EXPRESSION
{
$$ = IR_create_node (IR_NM_constraint);
IR_set_position ($$, IR_position ($1));
IR_set_constraint_code ($$, $1);
}
;
/* Attribute of this nonterminal is node representing action. */
action : CODE_INSERTION
{
$$ = IR_create_node (IR_NM_action);
IR_set_position ($$, IR_position ($1));
IR_set_action_code ($$, $1);
}
;
/* Attribute of this nonterminal is node representing last field of cyclic
list of fields in given field identifier list. */
field_identifier_list : field_identifier
{
IR_set_next_field ($1, $1);
$$ = $1;
}
| field_identifier_list COMMA field_identifier
{
assert ($1 != NULL);
IR_set_next_field ($3, IR_next_field ($1));
IR_set_next_field ($1, $3);
$$ = $3;
}
;
/* Attribute of this nonterminal is node representing field. */
field_identifier : IDENTIFIER
{
$$ = IR_create_node (IR_NM_field);
IR_set_position ($$, IR_position ($1));
IR_set_field_identifier ($$, $1);
}
;
/* Two following rules are necessary to guarantee that token
`END_OF_BASIC_FILE' is not skipped in error rules above and results
in their reducing. Remember that any addition of error rule to the
grammar may require addition of such rules here! */
/* There are also rules for given nonterminal upper. This rule covers
also errors in `predefined_types_declaration' and
`double_nodes_declaration'. */
declaration_part : declaration_part END_OF_BASIC_FILE
{
#ifndef YYRECOVERING
#define YYRECOVERING() (!!yyerrflag)
#endif
yychar = END_OF_BASIC_FILE;
if (!YYRECOVERING ())
yyerror ("error in description part");
else
syntax_error ("error in description part");
}
;
/* There are also rules for given nonterminal upper. */
field_definition_list : field_definition_list END_OF_BASIC_FILE
{
yychar = END_OF_BASIC_FILE;
#ifndef YYRECOVERING
#define YYRECOVERING() (!!yyerrflag)
#endif
if (!YYRECOVERING ())
yyerror ("error in description part");
else
syntax_error ("error in description part");
}
;
%%
/* This page contains functions used only in actions of YACC grammar. */
/* The following function appends given additional code to the cyclic
list of all additional codes of current description part (it can be
the only one in given description part). */
static void
append_additional_code (IR_node_t additional_code)
{
if (IR_last_additional_code (current_description_part) != NULL)
{
IR_set_next_additional_code
(additional_code,
IR_next_additional_code (IR_last_additional_code
(current_description_part)));
IR_set_next_additional_code
(IR_last_additional_code (current_description_part), additional_code);
}
else
IR_set_next_additional_code (additional_code, additional_code);
IR_set_last_additional_code (current_description_part, additional_code);
}
/* The following function appends cyclic list of node types or
predefined types to the cyclic list of node types and predefined
types of current description part. */
static void
append_type_list (IR_node_t last_type)
{
IR_node_t first_appended_type;
if (IR_last_type (current_description_part) != NULL)
{
first_appended_type = IR_next_type (last_type);
IR_set_next_type
(last_type, IR_next_type (IR_last_type (current_description_part)));
IR_set_next_type (IR_last_type (current_description_part),
first_appended_type);
}
IR_set_last_type (current_description_part, last_type);
}
/* The function appends given double declaration to the cyclic list of
all double declarations of current description part. */
static void
append_double_declaration (IR_node_t double_declaration)
{
if (IR_last_double_declaration (current_description_part) != NULL)
{
IR_set_next_double_declaration
(double_declaration,
IR_next_double_declaration (IR_last_double_declaration
(current_description_part)));
IR_set_next_double_declaration (IR_last_double_declaration
(current_description_part),
double_declaration);
}
else
IR_set_next_double_declaration (double_declaration, double_declaration);
IR_set_last_double_declaration (current_description_part,
double_declaration);
}
/* The following function sets up field `declaration_part' for all
nodes representing fields in given cyclic list. The function also
checks up that class fields are not double fields (see commentaries
for `double_field_flag'). If the condition is violated then error
is fixed and corresponding `double_field_flag' is set up to FALSE. */
static void
set_field_declaration_part_and_class_double_fields
(IR_node_t last_field, declaration_part_t declaration_part)
{
IR_node_t current_field;
current_field = last_field;
if (current_field != NULL)
do
{
IR_set_declaration_part (current_field, declaration_part);
if (IR_NODE_MODE (current_field) == IR_NM_field
&& IR_double_field_flag (current_field)
&& declaration_part == DP_CLASS)
{
error (FALSE, IR_position (current_field),
"class field `%s' is double",
IR_identifier_itself (IR_field_identifier (current_field)));
IR_set_double_field_flag (current_field, FALSE);
}
current_field = IR_next_field (current_field);
}
while (current_field != last_field);
}
/* The following function calls function
`set_field_declaration_part_and_class_double_fields' for each list
of three lists (of class fields, skeleton fields, and other
fields). After that the function merges these lists. The function
returns node (may be NULL) representing last field of the result
list. */
static IR_node_t
merge_and_set_field_declaration_part_and_class_double_fields
(IR_node_t class_fields, IR_node_t skeleton_fields, IR_node_t other_fields)
{
IR_node_t first_field;
IR_node_t last_field;
set_field_declaration_part_and_class_double_fields (other_fields, DP_OTHER);
set_field_declaration_part_and_class_double_fields (class_fields, DP_CLASS);
set_field_declaration_part_and_class_double_fields (skeleton_fields,
DP_SKELETON);
last_field = class_fields;
if (last_field == NULL)
last_field = skeleton_fields;
else if (skeleton_fields != NULL)
{
first_field = IR_next_field (skeleton_fields);
IR_set_next_field (skeleton_fields, IR_next_field (last_field));
IR_set_next_field (last_field, first_field);
}
if (last_field == NULL)
last_field = other_fields;
else if (other_fields != NULL)
{
first_field = IR_next_field (other_fields);
IR_set_next_field (other_fields, IR_next_field (last_field));
IR_set_next_field (last_field, first_field);
}
return last_field;
}
/* The following function makes copy of given cyclic field (and action
and constraints) list. The function returns node (may be NULL)
representing last field (or action or constraints) of the result
(new) list. */
static IR_node_t
copy_field_list (IR_node_t field_list)
{
IR_node_t current_field;
IR_node_t current_new_field;
IR_node_t new_field;
IR_node_t result;
if (field_list == NULL)
return NULL;
current_new_field = result = IR_copy_node (field_list);
IR_set_next_field (result, result);
for (current_field = field_list;;)
{
current_field = IR_next_field (current_field);
if (current_field == field_list)
break;
new_field = IR_copy_node (current_field);
IR_set_next_field (new_field, IR_next_field (current_new_field));
IR_set_next_field (current_new_field, new_field);
current_new_field = new_field;
}
return result;
}
/* The following function sets up fields `abstract_flag',
`first_super_type_list_element', `last_super_type_list_element',
and `last_field' for all nodes representing node types in given
cyclic list. If the node type list contains more one node type the
copy of fields (and action and constraints) is made for each non
last node type of the list. After that function `append_type_list'
is called for given node type list. */
static void
process_node_type_list
(IR_node_t last_node_type, int abstract_flag,
IR_node_t first_super_type_list_element,
IR_node_t last_super_type_list_element, int basic_super_types_flag,
IR_node_t last_field)
{
IR_node_t current_node_type;
current_node_type = last_node_type;
if (current_node_type != NULL)
do
{
assert (IR_NODE_MODE (current_node_type) == IR_NM_node_type);
IR_set_abstract_flag (current_node_type, abstract_flag);
IR_set_first_super_type_list_element (current_node_type,
first_super_type_list_element);
IR_set_last_super_type_list_element (current_node_type,
last_super_type_list_element);
IR_set_basic_super_types_flag (current_node_type,
basic_super_types_flag);
IR_set_last_field (current_node_type,
(current_node_type == last_node_type
? last_field : copy_field_list (last_field)));
current_node_type = IR_next_type (current_node_type);
}
while (current_node_type != last_node_type);
append_type_list (last_node_type);
}
/* This page contains abstract data `input_stream_stack'. This
abstract data opens and closes included files saves and restores
scanner states corresponding to processed files. */
/* The following structure describes current input stream and scanner
state and is used for saving and restoring input stream and scanner
state to process included files. */
struct input_stream_state
{
/* The following member is defined only for `current_input_stream_state'. */
FILE *input_file;
/* File name of given input stream. */
const char *input_file_name;
/* Current position in file corresponding to given input stream.
The following member is defined only for structures in the input
stream stack. */
long input_file_position;
/* The following member contains parameter value of the function
`add_lexema_to_input_file' after immediately call of this function or
-1 otherwise. */
int uninput_lexema_code;
/* The following member is TRUE if the first `%%' in given input stream
was already processed or FALSE otherwise. */
int it_is_first_percents;
/* The following member is TRUE if the ADDITIONAL_CODE lexema was already
returned by the scanner or FALSE otherwise. Remember that lexema
ADDITIONAL_CODE is always returned by the scanner
even if second `%%' in the input stream is absent. */
int additional_c_code_was_returned;
};
/* The following structure contains all current input stream and
scanner state. If the member `input_file' value is NULL
then input stream is not defined. */
static struct input_stream_state current_input_stream_state;
/* All input stream stack is implemented by variable length object.
See package `vl-object'. */
static vlo_t input_stream_stack;
/* The following function creates empty input stream stack and
initiates current input stream and scanner state as undefined. The
function must be called only once before any work with the input
stream stack. */
static void
initiate_input_stream_stack (void)
{
VLO_CREATE (input_stream_stack, 0);
current_input_stream_state.input_file = NULL;
current_input_stream_state.uninput_lexema_code = (-1);
current_input_stream_state.it_is_first_percents = TRUE;
current_input_stream_state.additional_c_code_was_returned = FALSE;
}
/* The following function deletes the input stream stack and closes
current input file if current input stream state is defined. Only
call of function `initiate_input_stream_stack' is possible
immediately after this function call. */
static void
finish_input_stream_stack (void)
{
VLO_DELETE (input_stream_stack);
if (current_input_stream_state.input_file != NULL)
fclose (current_input_stream_state.input_file);
}
/* The following function returns height of input stream stack,
i.e. current file inclusion level. */
static int
input_stream_stack_height (void)
{
return VLO_LENGTH (input_stream_stack) / sizeof (struct input_stream_state);
}
/* The following value is used if previous_char does not contain an
input char */
#define EMPTY_PREVIOUS_CHAR (-2000)
/* The following variable is used for storing '\r'. */
static int previous_char;
/* The following function saves current input stream and scanner state
(if it is defined) in the stack, closes file corresponding to
current input stream, opens file corresponding to new input stream,
and sets up new current input stream and scanner state. The
function also checks up absence of loop of file inclusions. The
function reports errors if the loop exists or new file is not
opened. */
static void
push_current_input_stream (const char *new_file_name,
position_t error_position)
{
int i;
if (current_input_stream_state.input_file != NULL)
{
current_input_stream_state.input_file_position
= ftell (current_input_stream_state.input_file);
fclose (current_input_stream_state.input_file);
current_input_stream_state.input_file = NULL;
VLO_ADD_MEMORY (input_stream_stack, ¤t_input_stream_state,
sizeof (struct input_stream_state));
for (i = 0; i < input_stream_stack_height (); i++)
if (strcmp
(((struct input_stream_state *) VLO_BEGIN (input_stream_stack))
[i].input_file_name, new_file_name) == 0)