forked from riscvarchive/riscv-gcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgengtype.c
5337 lines (4651 loc) · 147 KB
/
gengtype.c
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
/* Process source files and output type information.
Copyright (C) 2002-2020 Free Software Foundation, Inc.
This file is part of GCC.
GCC 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, or (at your option) any later
version.
GCC 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 GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#ifdef HOST_GENERATOR_FILE
#include "config.h"
#define GENERATOR_FILE 1
#else
#include "bconfig.h"
#endif
#include "system.h"
#include "errors.h" /* for fatal */
#include "getopt.h"
#include "version.h" /* for version_string & pkgversion_string. */
#include "xregex.h"
#include "obstack.h"
#include "gengtype.h"
#include "filenames.h"
/* Data types, macros, etc. used only in this file. */
/* The list of output files. */
outf_p output_files;
/* The output header file that is included into pretty much every
source file. */
outf_p header_file;
/* The name of the file containing the list of input files. */
static char *inputlist;
/* The plugin input files and their number; in that case only
a single file is produced. */
static input_file **plugin_files;
static size_t nb_plugin_files;
/* The generated plugin output file and name. */
static outf_p plugin_output;
static char *plugin_output_filename;
/* Our source directory and its length. */
const char *srcdir;
size_t srcdir_len;
/* Variables used for reading and writing the state. */
const char *read_state_filename;
const char *write_state_filename;
/* Variables to help debugging. */
int do_dump;
int do_debug;
/* Level for verbose messages. */
int verbosity_level;
/* We have a type count and use it to set the state_number of newly
allocated types to some unique negative number. */
static int type_count;
/* The backup directory should be in the same file system as the
generated files, otherwise the rename(2) system call would fail.
If NULL, no backup is made when overwriting a generated file. */
static const char* backup_dir; /* (-B) program option. */
static outf_p create_file (const char *, const char *);
static const char *get_file_basename (const input_file *);
static const char *get_file_realbasename (const input_file *);
static int get_prefix_langdir_index (const char *);
static const char *get_file_langdir (const input_file *);
static void dump_pair (int indent, pair_p p);
static void dump_type (int indent, type_p p);
static void dump_type_list (int indent, type_p p);
/* Nonzero iff an error has occurred. */
bool hit_error = false;
static void gen_rtx_next (void);
static void write_rtx_next (void);
static void open_base_files (void);
static void close_output_files (void);
/* Report an error at POS, printing MSG. */
void
error_at_line (const struct fileloc *pos, const char *msg, ...)
{
va_list ap;
gcc_assert (pos != NULL && pos->file != NULL);
va_start (ap, msg);
fprintf (stderr, "%s:%d: ", get_input_file_name (pos->file), pos->line);
vfprintf (stderr, msg, ap);
fputc ('\n', stderr);
hit_error = true;
va_end (ap);
}
/* Locate the ultimate base class of struct S. */
static const_type_p
get_ultimate_base_class (const_type_p s)
{
while (s->u.s.base_class)
s = s->u.s.base_class;
return s;
}
static type_p
get_ultimate_base_class (type_p s)
{
while (s->u.s.base_class)
s = s->u.s.base_class;
return s;
}
/* Input file handling. */
/* Table of all input files. */
const input_file **gt_files;
size_t num_gt_files;
/* A number of places use the name of this "gengtype.c" file for a
location for things that we can't rely on the source to define.
Make sure we can still use pointer comparison on filenames. */
input_file* this_file;
/* The "system.h" file is likewise specially useful. */
input_file* system_h_file;
/* Vector of per-language directories. */
const char **lang_dir_names;
size_t num_lang_dirs;
/* An array of output files suitable for definitions. There is one
BASE_FILES entry for each language. */
static outf_p *base_files;
/* Utility debugging function, printing the various type counts within
a list of types. Called through the DBGPRINT_COUNT_TYPE macro. */
void
dbgprint_count_type_at (const char *fil, int lin, const char *msg, type_p t)
{
int nb_types = 0, nb_scalar = 0, nb_string = 0;
int nb_struct = 0, nb_union = 0, nb_array = 0, nb_pointer = 0;
int nb_lang_struct = 0;
int nb_user_struct = 0, nb_undefined = 0;
type_p p = NULL;
for (p = t; p; p = p->next)
{
nb_types++;
switch (p->kind)
{
case TYPE_UNDEFINED:
nb_undefined++;
break;
case TYPE_SCALAR:
nb_scalar++;
break;
case TYPE_STRING:
nb_string++;
break;
case TYPE_STRUCT:
nb_struct++;
break;
case TYPE_USER_STRUCT:
nb_user_struct++;
break;
case TYPE_UNION:
nb_union++;
break;
case TYPE_POINTER:
nb_pointer++;
break;
case TYPE_ARRAY:
nb_array++;
break;
case TYPE_LANG_STRUCT:
nb_lang_struct++;
break;
case TYPE_NONE:
gcc_unreachable ();
}
}
fprintf (stderr, "\n" "%s:%d: %s: @@%%@@ %d types ::\n",
lbasename (fil), lin, msg, nb_types);
if (nb_scalar > 0 || nb_string > 0)
fprintf (stderr, "@@%%@@ %d scalars, %d strings\n", nb_scalar, nb_string);
if (nb_struct > 0 || nb_union > 0)
fprintf (stderr, "@@%%@@ %d structs, %d unions\n", nb_struct, nb_union);
if (nb_pointer > 0 || nb_array > 0)
fprintf (stderr, "@@%%@@ %d pointers, %d arrays\n", nb_pointer, nb_array);
if (nb_lang_struct > 0)
fprintf (stderr, "@@%%@@ %d lang_structs\n", nb_lang_struct);
if (nb_user_struct > 0)
fprintf (stderr, "@@%%@@ %d user_structs\n", nb_user_struct);
if (nb_undefined > 0)
fprintf (stderr, "@@%%@@ %d undefined types\n", nb_undefined);
fprintf (stderr, "\n");
}
/* Scan the input file, LIST, and determine how much space we need to
store strings in. Also, count the number of language directories
and files. The numbers returned are overestimates as they does not
consider repeated files. */
static size_t
measure_input_list (FILE *list)
{
size_t n = 0;
int c;
bool atbol = true;
num_lang_dirs = 0;
num_gt_files = plugin_files ? nb_plugin_files : 0;
while ((c = getc (list)) != EOF)
{
n++;
if (atbol)
{
if (c == '[')
num_lang_dirs++;
else
{
/* Add space for a lang_bitmap before the input file name. */
n += sizeof (lang_bitmap);
num_gt_files++;
}
atbol = false;
}
if (c == '\n')
atbol = true;
}
rewind (list);
return n;
}
/* Read one input line from LIST to HEREP (which is updated). A
pointer to the string is returned via LINEP. If it was a language
subdirectory in square brackets, strip off the square brackets and
return true. Otherwise, leave space before the string for a
lang_bitmap, and return false. At EOF, returns false, does not
touch *HEREP, and sets *LINEP to NULL. POS is used for
diagnostics. */
static bool
read_input_line (FILE *list, char **herep, char **linep, struct fileloc *pos)
{
char *here = *herep;
char *line;
int c = getc (list);
/* Read over whitespace. */
while (c == '\n' || c == ' ')
c = getc (list);
if (c == EOF)
{
*linep = 0;
return false;
}
else if (c == '[')
{
/* No space for a lang_bitmap is necessary. Discard the '['. */
c = getc (list);
line = here;
while (c != ']' && c != '\n' && c != EOF)
{
*here++ = c;
c = getc (list);
}
*here++ = '\0';
if (c == ']')
{
c = getc (list); /* eat what should be a newline */
if (c != '\n' && c != EOF)
error_at_line (pos, "junk on line after language tag [%s]", line);
}
else
error_at_line (pos, "missing close bracket for language tag [%s",
line);
*herep = here;
*linep = line;
return true;
}
else
{
/* Leave space for a lang_bitmap. */
memset (here, 0, sizeof (lang_bitmap));
here += sizeof (lang_bitmap);
line = here;
do
{
*here++ = c;
c = getc (list);
}
while (c != EOF && c != '\n');
*here++ = '\0';
*herep = here;
*linep = line;
return false;
}
}
/* Read the list of input files from LIST and compute all of the
relevant tables. There is one file per line of the list. At
first, all the files on the list are language-generic, but
eventually a line will appear which is the name of a language
subdirectory in square brackets, like this: [cp]. All subsequent
files are specific to that language, until another language
subdirectory tag appears. Files can appear more than once, if
they apply to more than one language. */
static void
read_input_list (const char *listname)
{
FILE *list = fopen (listname, "r");
if (!list)
fatal ("cannot open %s: %s", listname, xstrerror (errno));
else
{
struct fileloc epos;
size_t bufsz = measure_input_list (list);
char *buf = XNEWVEC (char, bufsz);
char *here = buf;
char *committed = buf;
char *limit = buf + bufsz;
char *line;
bool is_language;
size_t langno = 0;
size_t nfiles = 0;
lang_bitmap curlangs = (1 << num_lang_dirs) - 1;
epos.file = input_file_by_name (listname);
epos.line = 0;
lang_dir_names = XNEWVEC (const char *, num_lang_dirs);
gt_files = XNEWVEC (const input_file *, num_gt_files);
for (;;)
{
next_line:
epos.line++;
committed = here;
is_language = read_input_line (list, &here, &line, &epos);
gcc_assert (here <= limit);
if (line == 0)
break;
else if (is_language)
{
size_t i;
gcc_assert (langno <= num_lang_dirs);
for (i = 0; i < langno; i++)
if (strcmp (lang_dir_names[i], line) == 0)
{
error_at_line (&epos, "duplicate language tag [%s]",
line);
curlangs = 1 << i;
here = committed;
goto next_line;
}
curlangs = 1 << langno;
lang_dir_names[langno++] = line;
}
else
{
size_t i;
input_file *inpf = input_file_by_name (line);
gcc_assert (nfiles <= num_gt_files);
for (i = 0; i < nfiles; i++)
/* Since the input_file-s are uniquely hash-consed, we
can just compare pointers! */
if (gt_files[i] == inpf)
{
/* Throw away the string we just read, and add the
current language to the existing string's bitmap. */
lang_bitmap bmap = get_lang_bitmap (inpf);
if (bmap & curlangs)
error_at_line (&epos,
"file %s specified more than once "
"for language %s", line,
langno ==
0 ? "(all)" : lang_dir_names[langno -
1]);
bmap |= curlangs;
set_lang_bitmap (inpf, bmap);
here = committed;
goto next_line;
}
set_lang_bitmap (inpf, curlangs);
gt_files[nfiles++] = inpf;
}
}
/* Update the global counts now that we know accurately how many
things there are. (We do not bother resizing the arrays down.) */
num_lang_dirs = langno;
/* Add the plugin files if provided. */
if (plugin_files)
{
size_t i;
for (i = 0; i < nb_plugin_files; i++)
gt_files[nfiles++] = plugin_files[i];
}
num_gt_files = nfiles;
}
/* Sanity check: any file that resides in a language subdirectory
(e.g. 'cp') ought to belong to the corresponding language.
??? Still true if for instance ObjC++ is enabled and C++ isn't?
(Can you even do that? Should you be allowed to?) */
{
size_t f;
for (f = 0; f < num_gt_files; f++)
{
lang_bitmap bitmap = get_lang_bitmap (gt_files[f]);
const char *basename = get_file_basename (gt_files[f]);
const char *slashpos = strchr (basename, '/');
#ifdef HAVE_DOS_BASED_FILE_SYSTEM
const char *slashpos2 = strchr (basename, '\\');
if (!slashpos || (slashpos2 && slashpos2 < slashpos))
slashpos = slashpos2;
#endif
if (slashpos)
{
size_t l;
for (l = 0; l < num_lang_dirs; l++)
if ((size_t) (slashpos - basename) == strlen (lang_dir_names[l])
&& memcmp (basename, lang_dir_names[l],
strlen (lang_dir_names[l])) == 0)
{
if (!(bitmap & (1 << l)))
error ("%s is in language directory '%s' but is not "
"tagged for that language",
basename, lang_dir_names[l]);
break;
}
}
}
}
if (ferror (list))
fatal ("error reading %s: %s", listname, xstrerror (errno));
fclose (list);
}
/* The one and only TYPE_STRING. */
struct type string_type = {
TYPE_STRING, 0, 0, 0, GC_USED, {0}
};
/* The two and only TYPE_SCALARs. Their u.scalar_is_char flags are
set early in main. */
struct type scalar_nonchar = {
TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
};
struct type scalar_char = {
TYPE_SCALAR, 0, 0, 0, GC_USED, {0}
};
/* Lists of various things. */
pair_p typedefs = NULL;
type_p structures = NULL;
pair_p variables = NULL;
static type_p adjust_field_tree_exp (type_p t, options_p opt);
static type_p adjust_field_rtx_def (type_p t, options_p opt);
/* Define S as a typedef to T at POS. */
void
do_typedef (const char *s, type_p t, struct fileloc *pos)
{
pair_p p;
/* temporary kludge - gengtype doesn't handle conditionals or
macros. Ignore any attempt to typedef CUMULATIVE_ARGS, unless it
is coming from this file (main() sets them up with safe dummy
definitions). */
if (!strcmp (s, "CUMULATIVE_ARGS") && pos->file != this_file)
return;
for (p = typedefs; p != NULL; p = p->next)
if (strcmp (p->name, s) == 0)
{
if (p->type != t && strcmp (s, "result_type") != 0)
{
error_at_line (pos, "type `%s' previously defined", s);
error_at_line (&p->line, "previously defined here");
}
return;
}
p = XNEW (struct pair);
p->next = typedefs;
p->name = s;
p->type = t;
p->line = *pos;
p->opt = NULL;
typedefs = p;
}
/* Define S as a typename of a scalar. Cannot be used to define
typedefs of 'char'. Note: is also used for pointer-to-function
typedefs (which are therefore not treated as pointers). */
void
do_scalar_typedef (const char *s, struct fileloc *pos)
{
do_typedef (s, &scalar_nonchar, pos);
}
/* Similar to strtok_r. */
static char *
strtoken (char *str, const char *delim, char **next)
{
char *p;
if (str == NULL)
str = *next;
/* Skip the leading delimiters. */
str += strspn (str, delim);
if (*str == '\0')
/* This is an empty token. */
return NULL;
/* The current token. */
p = str;
/* Find the next delimiter. */
str += strcspn (str, delim);
if (*str == '\0')
/* This is the last token. */
*next = str;
else
{
/* Terminate the current token. */
*str = '\0';
/* Advance to the next token. */
*next = str + 1;
}
return p;
}
/* Define TYPE_NAME to be a user defined type at location POS. */
type_p
create_user_defined_type (const char *type_name, struct fileloc *pos)
{
type_p ty = find_structure (type_name, TYPE_USER_STRUCT);
/* We might have already seen an incomplete decl of the given type,
in which case we won't have yet seen a GTY((user)), and the type will
only have kind "TYPE_STRUCT". Mark it as a user struct. */
ty->kind = TYPE_USER_STRUCT;
ty->u.s.line = *pos;
ty->u.s.bitmap = get_lang_bitmap (pos->file);
do_typedef (type_name, ty, pos);
/* If TYPE_NAME specifies a template, create references to the types
in the template by pretending that each type is a field of TY.
This is needed to make sure that the types referenced by the
template are marked as used. */
char *str = xstrdup (type_name);
char *open_bracket = strchr (str, '<');
if (open_bracket)
{
/* We only accept simple template declarations (see
require_template_declaration), so we only need to parse a
comma-separated list of strings, implicitly assumed to
be type names, potentially with "*" characters. */
char *arg = open_bracket + 1;
/* Workaround -Wmaybe-uninitialized false positive during
profiledbootstrap by initializing it. */
char *next = NULL;
char *type_id = strtoken (arg, ",>", &next);
pair_p fields = 0;
while (type_id)
{
/* Create a new field for every type found inside the template
parameter list. */
/* Support a single trailing "*" character. */
const char *star = strchr (type_id, '*');
int is_ptr = (star != NULL);
size_t offset_to_star = star - type_id;
if (is_ptr)
offset_to_star = star - type_id;
if (strstr (type_id, "char*"))
{
type_id = strtoken (0, ",>", &next);
continue;
}
char *field_name = xstrdup (type_id);
type_p arg_type;
if (is_ptr)
{
/* Strip off the first '*' character (and any subsequent text). */
*(field_name + offset_to_star) = '\0';
arg_type = find_structure (field_name, TYPE_STRUCT);
arg_type = create_pointer (arg_type);
}
else
arg_type = resolve_typedef (field_name, pos);
fields = create_field_at (fields, arg_type, field_name, 0, pos);
type_id = strtoken (0, ",>", &next);
}
/* Associate the field list to TY. */
ty->u.s.fields = fields;
}
free (str);
return ty;
}
/* Given a typedef name S, return its associated type. Return NULL if
S is not a registered type name. */
static type_p
type_for_name (const char *s)
{
pair_p p;
/* Special-case support for types within a "gcc::" namespace. Rather
than fully-supporting namespaces, simply strip off the "gcc::" prefix
where present. This allows us to have GTY roots of this form:
extern GTY(()) gcc::some_type *some_ptr;
where the autogenerated functions will refer to simply "some_type",
where they can be resolved into their namespace. */
if (strncmp (s, "gcc::", 5) == 0)
s += 5;
for (p = typedefs; p != NULL; p = p->next)
if (strcmp (p->name, s) == 0)
return p->type;
return NULL;
}
/* Create an undefined type with name S and location POS. Return the
newly created type. */
static type_p
create_undefined_type (const char *s, struct fileloc *pos)
{
type_p ty = find_structure (s, TYPE_UNDEFINED);
ty->u.s.line = *pos;
ty->u.s.bitmap = get_lang_bitmap (pos->file);
do_typedef (s, ty, pos);
return ty;
}
/* Return the type previously defined for S. Use POS to report errors. */
type_p
resolve_typedef (const char *s, struct fileloc *pos)
{
bool is_template_instance = (strchr (s, '<') != NULL);
type_p p = type_for_name (s);
/* If we did not find a typedef registered, generate a TYPE_UNDEFINED
type for regular type identifiers. If the type identifier S is a
template instantiation, however, we treat it as a user defined
type.
FIXME, this is actually a limitation in gengtype. Supporting
template types and their instances would require keeping separate
track of the basic types definition and its instances. This
essentially forces all template classes in GC to be marked
GTY((user)). */
if (!p)
p = (is_template_instance)
? create_user_defined_type (s, pos)
: create_undefined_type (s, pos);
return p;
}
/* Add SUBCLASS to head of linked list of BASE's subclasses. */
void add_subclass (type_p base, type_p subclass)
{
gcc_assert (union_or_struct_p (base));
gcc_assert (union_or_struct_p (subclass));
subclass->u.s.next_sibling_class = base->u.s.first_subclass;
base->u.s.first_subclass = subclass;
}
/* Create and return a new structure with tag NAME at POS with fields
FIELDS and options O. The KIND of structure must be one of
TYPE_STRUCT, TYPE_UNION or TYPE_USER_STRUCT. */
type_p
new_structure (const char *name, enum typekind kind, struct fileloc *pos,
pair_p fields, options_p o, type_p base_class)
{
type_p si;
type_p s = NULL;
lang_bitmap bitmap = get_lang_bitmap (pos->file);
bool isunion = (kind == TYPE_UNION);
type_p *p = &structures;
gcc_assert (union_or_struct_p (kind));
for (si = structures; si != NULL; p = &si->next, si = *p)
if (strcmp (name, si->u.s.tag) == 0 && UNION_P (si) == isunion)
{
type_p ls = NULL;
if (si->kind == TYPE_LANG_STRUCT)
{
ls = si;
for (si = ls->u.s.lang_struct; si != NULL; si = si->next)
if (si->u.s.bitmap == bitmap)
s = si;
}
else if (si->u.s.line.file != NULL && si->u.s.bitmap != bitmap)
{
ls = si;
type_count++;
si = XCNEW (struct type);
memcpy (si, ls, sizeof (struct type));
ls->kind = TYPE_LANG_STRUCT;
ls->u.s.lang_struct = si;
ls->u.s.fields = NULL;
si->next = NULL;
si->state_number = -type_count;
si->pointer_to = NULL;
si->u.s.lang_struct = ls;
}
else
s = si;
if (ls != NULL && s == NULL)
{
type_count++;
s = XCNEW (struct type);
s->state_number = -type_count;
s->next = ls->u.s.lang_struct;
ls->u.s.lang_struct = s;
s->u.s.lang_struct = ls;
}
break;
}
if (s == NULL)
{
type_count++;
s = XCNEW (struct type);
s->state_number = -type_count;
*p = s;
}
if (s->u.s.lang_struct && (s->u.s.lang_struct->u.s.bitmap & bitmap))
{
error_at_line (pos, "duplicate definition of '%s %s'",
isunion ? "union" : "struct", s->u.s.tag);
error_at_line (&s->u.s.line, "previous definition here");
}
s->kind = kind;
s->u.s.tag = name;
s->u.s.line = *pos;
s->u.s.fields = fields;
s->u.s.opt = o;
s->u.s.bitmap = bitmap;
if (s->u.s.lang_struct)
s->u.s.lang_struct->u.s.bitmap |= bitmap;
s->u.s.base_class = base_class;
if (base_class)
add_subclass (base_class, s);
return s;
}
/* Return the previously-defined structure or union with tag NAME,
or a new empty structure or union if none was defined previously.
The KIND of structure must be one of TYPE_STRUCT, TYPE_UNION or
TYPE_USER_STRUCT. */
type_p
find_structure (const char *name, enum typekind kind)
{
type_p s;
bool isunion = (kind == TYPE_UNION);
type_p *p = &structures;
gcc_assert (kind == TYPE_UNDEFINED || union_or_struct_p (kind));
for (s = structures; s != NULL; p = &s->next, s = *p)
if (strcmp (name, s->u.s.tag) == 0 && UNION_P (s) == isunion)
return s;
type_count++;
s = XCNEW (struct type);
s->state_number = -type_count;
s->kind = kind;
s->u.s.tag = name;
*p = s;
return s;
}
/* Return a scalar type with name NAME. */
type_p
create_scalar_type (const char *name)
{
if (!strcmp (name, "char") || !strcmp (name, "unsigned char"))
return &scalar_char;
else
return &scalar_nonchar;
}
/* Return a pointer to T. */
type_p
create_pointer (type_p t)
{
if (!t->pointer_to)
{
type_p r = XCNEW (struct type);
type_count++;
r->state_number = -type_count;
r->kind = TYPE_POINTER;
r->u.p = t;
t->pointer_to = r;
}
return t->pointer_to;
}
/* Return an array of length LEN. */
type_p
create_array (type_p t, const char *len)
{
type_p v;
type_count++;
v = XCNEW (struct type);
v->kind = TYPE_ARRAY;
v->state_number = -type_count;
v->u.a.p = t;
v->u.a.len = len;
return v;
}
/* Return a string options structure with name NAME and info INFO.
NEXT is the next option in the chain. */
options_p
create_string_option (options_p next, const char *name, const char *info)
{
options_p o = XNEW (struct options);
o->kind = OPTION_STRING;
o->next = next;
o->name = name;
o->info.string = info;
return o;
}
/* Create a type options structure with name NAME and info INFO. NEXT
is the next option in the chain. */
options_p
create_type_option (options_p next, const char* name, type_p info)
{
options_p o = XNEW (struct options);
o->next = next;
o->name = name;
o->kind = OPTION_TYPE;
o->info.type = info;
return o;
}
/* Create a nested pointer options structure with name NAME and info
INFO. NEXT is the next option in the chain. */
options_p
create_nested_option (options_p next, const char* name,
struct nested_ptr_data* info)
{
options_p o;
o = XNEW (struct options);
o->next = next;
o->name = name;
o->kind = OPTION_NESTED;
o->info.nested = info;
return o;
}
/* Return an options structure for a "nested_ptr" option. */
options_p
create_nested_ptr_option (options_p next, type_p t,
const char *to, const char *from)
{
struct nested_ptr_data *d = XNEW (struct nested_ptr_data);
d->type = adjust_field_type (t, 0);
d->convert_to = to;
d->convert_from = from;
return create_nested_option (next, "nested_ptr", d);
}
/* Add a variable named S of type T with options O defined at POS,
to `variables'. */
void
note_variable (const char *s, type_p t, options_p o, struct fileloc *pos)
{
pair_p n;
n = XNEW (struct pair);
n->name = s;
n->type = t;
n->line = *pos;
n->opt = o;
n->next = variables;
variables = n;
}
/* Most-general structure field creator. */
static pair_p
create_field_all (pair_p next, type_p type, const char *name, options_p opt,
const input_file *inpf, int line)
{
pair_p field;
field = XNEW (struct pair);
field->next = next;
field->type = type;
field->name = name;
field->opt = opt;
field->line.file = inpf;
field->line.line = line;
return field;
}
/* Create a field that came from the source code we are scanning,
i.e. we have a 'struct fileloc', and possibly options; also,
adjust_field_type should be called. */
pair_p
create_field_at (pair_p next, type_p type, const char *name, options_p opt,
struct fileloc *pos)
{
return create_field_all (next, adjust_field_type (type, opt),
name, opt, pos->file, pos->line);
}
/* Create a fake field with the given type and name. NEXT is the next
field in the chain. */
#define create_field(next,type,name) \
create_field_all (next,type,name, 0, this_file, __LINE__)
/* Like create_field, but the field is only valid when condition COND
is true. */
static pair_p
create_optional_field_ (pair_p next, type_p type, const char *name,