-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdecl.c
2224 lines (1796 loc) · 65.3 KB
/
decl.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 declarations and variables for C compiler.
Copyright (C) 1987 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing. Refer to the GNU CC General Public
License for full details.
Everyone is granted permission to copy, modify and redistribute
GNU CC, but only under the conditions described in the
GNU CC General Public License. A copy of this license is
supposed to have been given to you along with GNU CC so you
can know your rights and responsibilities. It should be in a
file named COPYING. Among other things, the copyright notice
and this notice must be preserved on all copies. */
/* Process declarations and symbol lookup for C front end.
Also constructs types; the standard scalar types at initialization,
and structure, union, array and enum types when they are declared. */
/* ??? not all decl nodes are given the most useful possible
line numbers. For example, the CONST_DECLs for enum values. */
#include "config.h"
#include "parse.h"
#include "tree.h"
#include "c-tree.h"
/* In grokdeclarator, distinguish syntactic contexts of declarators. */
enum decl_context
{ NORMAL, /* Ordinary declaration */
PARM, /* Declaration of parm before function body */
FIELD, /* Declaration inside struct or union */
TYPENAME}; /* Typename (inside cast or sizeof) */
#define NULL 0
#define min(X,Y) ((X) < (Y) ? (X) : (Y))
static tree grokparms (), grokdeclarator ();
static tree make_index_type ();
static void builtin_function ();
static tree lookup_labelname ();
static tree lookup_tag ();
static tree lookup_name_current_level ();
/* a node which has tree code ERROR_MARK, and whose type is itself.
All erroneous expressions are replaced with this node. All functions
that accept nodes as arguments should avoid generating error messages
if this node is one of the arguments, since it is undesirable to get
multiple error messages from one error in the input. */
tree error_mark_node;
/* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
tree short_integer_type_node;
tree integer_type_node;
tree long_integer_type_node;
tree short_unsigned_type_node;
tree unsigned_type_node;
tree long_unsigned_type_node;
tree unsigned_char_type_node;
tree char_type_node;
tree float_type_node;
tree double_type_node;
tree long_double_type_node;
/* a VOID_TYPE node. */
tree void_type_node;
/* A node for type `void *'. */
tree ptr_type_node;
/* A node for type `char *'. */
tree string_type_node;
/* Type `char[256]' or something like it.
Used when an array of char is needed and the size is irrelevant. */
tree char_array_type_node;
/* type `int ()' -- used for implicit declaration of functions. */
tree default_function_type;
/* function types `double (double)' and `double (double, double)', etc. */
tree double_ftype_double, double_ftype_double_double;
tree int_ftype_int, long_ftype_long;
/* Function type `void (void *, void *, int)' and similar ones */
tree void_ftype_ptr_ptr_int, int_ftype_ptr_ptr_int, void_ftype_ptr_int_int;
/* Two expressions that are constants with value zero.
The first is of type `int', the second of type `void *'. */
tree integer_zero_node;
tree null_pointer_node;
/* A node for the integer constant 1. */
tree integer_one_node;
/* An identifier whose name is <value>. This is used as the "name"
of the RESULT_DECLs for values of functions. */
tree value_identifier;
/* Enumeration type currently being built, or 0 if not doing that now. */
tree current_enum_type;
/* Default value for next enumerator of enumeration type currently being read,
or undefined at other times. */
int enum_next_value;
/* Parsing a function declarator leaves a list of parameter names here.
If the declarator is the beginning of a function definition,
the names are stored into the function declaration when it is created. */
static tree last_function_parm_names;
/* A list (chain of TREE_LIST nodes) of all LABEL_STMTs in the function
that have names. Here so we can clear out their names' definitions
at the end of the function. */
static tree named_labels;
/* A list of all GOTO_STMT nodes in the current function,
so we can fill in the LABEL_STMTs they go to once all are defined. */
static tree all_gotos;
/* Set to 0 at beginning of a function definition, set to 1 if
a return statement that specifies a return value is seen. */
int current_function_returns_value;
/* For each binding contour we allocate a binding_level structure
* which records the names defined in that contour.
* Contours include:
* 0) the global one
* 1) one for each function definition,
* where internal declarations of the parameters appear.
* 2) one for each compound statement,
* to record its declarations.
*
* The current meaning of a name can be found by searching the levels from
* the current one out to the global one.
*/
/* Note that the information in the `names' component of the global contour
is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
static struct binding_level
{
/* A chain of _DECL nodes for all variables, constants, functions,
and typedef types. These are in the reverse of the order supplied.
*/
tree names;
/* A list of structure, union and enum definitions,
* for looking up tag names.
* It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
* or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
* or ENUMERAL_TYPE node.
*/
tree tags;
/* For each level, a list of shadowed outer-level local definitions
to be restored when this level is popped.
Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
tree shadowed;
/* The binding level which this one is contained in (inherits from). */
struct binding_level *level_chain;
};
#define NULL_BINDING_LEVEL (struct binding_level *) NULL
/* The binding level currently in effect. */
static struct binding_level *current_binding_level;
/* A chain of binding_level structures awaiting reuse. */
static struct binding_level *free_binding_level;
/* The outermost binding level, for names of file scope.
This is created when the compiler is started and exists
through the entire run. */
static struct binding_level *global_binding_level;
/* Binding level structures are initialized by copying this one. */
static struct binding_level clear_binding_level =
{NULL, NULL, NULL, NULL};
/* Create a new `struct binding_level'. */
static
struct binding_level *
make_binding_level ()
{
/* NOSTRICT */
return (struct binding_level *) xmalloc (sizeof (struct binding_level));
}
/* Enter a new binding level. */
void
pushlevel ()
{
register struct binding_level *newlevel = NULL_BINDING_LEVEL;
/* If this is the top level of a function,
just make sure that ALL_GOTOS and NAMED_LABELS are 0.
They should have been set to 0 at the end of the previous function. */
if (current_binding_level == global_binding_level)
{
if (all_gotos || named_labels)
abort ();
}
/* Reuse or create a struct for this binding level. */
if (free_binding_level)
{
newlevel = free_binding_level;
free_binding_level = free_binding_level->level_chain;
}
else
{
newlevel = make_binding_level ();
}
/* Add this level to the front of the chain (stack) of levels that
are active. */
*newlevel = clear_binding_level;
newlevel->level_chain = current_binding_level;
current_binding_level = newlevel;
}
/* Exit a binding level. */
void
poplevel ()
{
register tree link;
/* Clear out the meanings of the local variables of this level. */
for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = 0;
/* Restore all name-meanings of the outer levels
that were shadowed by this level. */
for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
/* If the level being exited is the top level of a function,
match all goto statements with their labels. */
if (current_binding_level->level_chain == global_binding_level)
{
/* Search for labels for any unresolved gotos. */
for (link = all_gotos; link; link = TREE_CHAIN (link))
{
register tree stmt = TREE_VALUE (link);
register tree label = lookup_labelname (STMT_BODY (stmt));
if (label)
STMT_BODY (stmt) = label;
else
{
yylineerror (STMT_SOURCE_LINE (stmt),
"no label %s visible for goto",
IDENTIFIER_POINTER (STMT_BODY (stmt)));
STMT_BODY (stmt) = NULL_TREE;
}
}
/* Then clear out the definitions of all label names,
since their scopes end here. */
for (link = named_labels; link; link = TREE_CHAIN (link))
IDENTIFIER_LABEL_VALUE (DECL_NAME (STMT_BODY (TREE_VALUE (link)))) = 0;
named_labels = 0;
all_gotos = 0;
}
/* Pop the current level, and free the structure for reuse. */
{
register struct binding_level *level = current_binding_level;
current_binding_level = current_binding_level->level_chain;
level->level_chain = free_binding_level;
free_binding_level = level;
}
}
/* Push a definition of struct, union or enum tag "name".
"type" should be the type node.
Note that the definition may really be just a forward reference.
In that case, the TYPE_SIZE will be zero. */
void
pushtag (name, type)
tree name, type;
{
register tree t;
/* If the type has a name, check for duplicate definitions of the name. */
if (name)
{
for (t = current_binding_level->tags; t; t = TREE_CHAIN (t))
if (TREE_PURPOSE (t) == name)
{
yyerror ("redeclaration of struct, union or enum tag %s",
IDENTIFIER_POINTER (name));
return;
}
/* Record the identifier as the type's name if it has none. */
if (TYPE_NAME (type) == 0)
TYPE_NAME (type) = name;
}
current_binding_level->tags
= tree_cons (name, type, current_binding_level->tags);
}
/* Handle when a new declaration X has the same name as an old one T
in the same binding contour.
May alter the old decl to say what the new one says.
Returns the old decl T if the two are more or less compatible;
returns the new one X if they are thoroughly alien. */
static tree
duplicate_decls (x, t)
register tree x, t;
{
/* At top level in file, if the old definition is "tentative" and
this one is close enough, no error. */
if (! allowed_redeclaration (x, t,
current_binding_level == global_binding_level))
yylineerror (DECL_SOURCE_LINE (x), "redeclaration of %s",
IDENTIFIER_POINTER (DECL_NAME (x)));
/* Install latest semantics. */
if (TREE_CODE (t) == TREE_CODE (x))
{
bcopy ((char *) x + sizeof (struct tree_shared),
(char *) t + sizeof (struct tree_shared),
sizeof (struct tree_decl) - sizeof (struct tree_shared));
TREE_TYPE (t) = TREE_TYPE (x);
DECL_ARGUMENTS (t) = DECL_ARGUMENTS (x);
DECL_RESULT (t) = DECL_RESULT (x);
DECL_SOURCE_FILE (t) = DECL_SOURCE_FILE (x);
DECL_SOURCE_LINE (t) = DECL_SOURCE_LINE (x);
TREE_STATIC (t) = TREE_STATIC (x);
TREE_EXTERNAL (t) = TREE_EXTERNAL (x);
TREE_PUBLIC (t) = TREE_PUBLIC (x);
if (DECL_INITIAL (x))
DECL_INITIAL (t) = DECL_INITIAL (x);
return t;
}
return x;
}
/* Record a decl-node X as belonging to the current lexical scope.
Check for errors (such as an incompatible declaration for the same
name already seen in the same scope).
Returns either X or an old decl for the same name.
If an old decl is returned, it has been smashed
to agree with what X says. */
tree
pushdecl (x)
tree x;
{
register tree t;
register tree name = DECL_NAME (x);
if (name)
{
t = lookup_name_current_level (name);
if (t)
return duplicate_decls (x, t);
/* If declaring a type as a typedef, and the type has no known
typedef name, install this TYPE_DECL as its typedef name. */
if (TREE_CODE (x) == TYPE_DECL)
if (TYPE_NAME (TREE_TYPE (x)) == 0
|| TREE_CODE (TYPE_NAME (TREE_TYPE (x))) != TYPE_DECL)
TYPE_NAME (TREE_TYPE (x)) = x;
}
/* This name is new.
Install the new declaration and return it. */
if (current_binding_level == global_binding_level)
{
IDENTIFIER_GLOBAL_VALUE (name) = x;
}
else
{
/* If storing a local value, there may already be one (inherited).
If so, record it for restoration when this binding level ends. */
if (IDENTIFIER_LOCAL_VALUE (name))
current_binding_level->shadowed
= tree_cons (name, IDENTIFIER_LOCAL_VALUE (name),
current_binding_level->shadowed);
IDENTIFIER_LOCAL_VALUE (name) = x;
}
/* Put decls on list in reverse order.
We will reverse them later if necessary. */
current_binding_level->names = chainon (x, current_binding_level->names);
return x;
}
/* Record a C label name. X is a LABEL_STMT and its STMT_BODY is a LABEL_DECL.
That is where we find the name. */
void
pushlabel (x)
tree x;
{
register tree decl = STMT_BODY (x);
if (0 == DECL_NAME (decl))
return;
if (IDENTIFIER_LABEL_VALUE (DECL_NAME (decl)))
yyerror ("duplicate label %s",
IDENTIFIER_POINTER (DECL_NAME (decl)));
else
IDENTIFIER_LABEL_VALUE (DECL_NAME (decl)) = decl;
named_labels
= tree_cons (NULL_TREE, x, named_labels);
}
/* Record the goto statement X on the list of all gotos in the
current function. The list is used to find them all at
the end of the function so that their target labels can be found then. */
void
pushgoto (x)
tree x;
{
all_gotos = tree_cons (NULL_TREE, x, all_gotos);
}
/* Return the list of declarations of the current level.
They are pushed on the list in reverse order since that is easiest.
We reverse them to the correct order here. */
tree
getdecls ()
{
return
current_binding_level->names = nreverse (current_binding_level->names);
}
/* Return the list of type-tags (for structs, etc) of the current level. */
tree
gettags ()
{
return current_binding_level->tags;
}
/* Store the list of declarations of the current level.
This is done for the parameter declarations of a function being defined,
after they are modified in the light of any missing parameters. */
void
storedecls (decls)
tree decls;
{
current_binding_level->names = decls;
}
/* Given NAME, an IDENTIFIER_NODE,
return the structure (or union or enum) definition for that name.
Searches binding levels from BINDING_LEVEL up to the global level.
If THISLEVEL_ONLY is nonzero, searches only the specified context.
FORM says which kind of type the caller wants;
it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
If the wrong kind of type is found, an error is reported. */
static tree
lookup_tag (form, name, binding_level, thislevel_only)
enum tree_code form;
struct binding_level *binding_level;
tree name;
int thislevel_only;
{
register struct binding_level *level;
for (level = binding_level; level; level = level->level_chain)
{
register tree tail;
for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
{
if (TREE_PURPOSE (tail) == name)
{
if (TREE_CODE (TREE_VALUE (tail)) != form)
{
/* Definition isn't the kind we were looking for. */
yyerror ("%s defined as wrong kind of tag",
IDENTIFIER_POINTER (name));
}
return TREE_VALUE (tail);
}
}
if (thislevel_only)
return NULL_TREE;
}
return NULL_TREE;
}
/* Look up NAME in the current binding level and its superiors
in the namespace of variables, functions and typedefs.
Return a ..._DECL node of some kind representing its definition,
or return 0 if it is undefined. */
tree
lookup_name (name)
tree name;
{
if (current_binding_level != global_binding_level
&& IDENTIFIER_LOCAL_VALUE (name))
return IDENTIFIER_LOCAL_VALUE (name);
return IDENTIFIER_GLOBAL_VALUE (name);
}
/* Similar to `lookup_name' but look only at current binding level. */
static tree
lookup_name_current_level (name)
tree name;
{
register tree t;
if (current_binding_level == global_binding_level)
return IDENTIFIER_GLOBAL_VALUE (name);
if (IDENTIFIER_LOCAL_VALUE (name) == 0)
return 0;
for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
if (DECL_NAME (t) == name)
break;
return t;
}
/* Return the definition of NAME as a label (a LABEL-DECL node),
or 0 if it has no definition as a label. */
static
tree
lookup_labelname (name)
tree name;
{
return IDENTIFIER_LABEL_VALUE (name);
}
/* Create a DECL_... node of code CODE, name NAME and data type TYPE.
STATICP nonzero means this is declared `static' in the C sense;
EXTERNP means it is declared `extern' in the C sense.
The name's definition is *not* entered in the symbol table.
The source file and line number are left 0.
layout_decl is used to set up the decl's storage layout.
are initialized to 0 or null pointers. */
tree
build_decl (code, name, type, staticp, externp)
enum tree_code code;
tree name, type;
int staticp, externp;
{
register tree t;
t = make_node (code);
/* if (type == error_mark_node)
type = integer_type_node; */
/* That is not done, deliberately, so that having error_mark_node
as the type can suppress useless errors in the use of this variable. */
DECL_NAME (t) = name;
TREE_TYPE (t) = type;
DECL_ARGUMENTS (t) = NULL_TREE;
DECL_INITIAL (t) = NULL_TREE;
if (externp)
TREE_EXTERNAL (t) = 1;
if (staticp)
TREE_STATIC (t) = 1;
if (current_binding_level == global_binding_level)
{
if (!TREE_STATIC (t))
TREE_PUBLIC (t) = 1;
TREE_STATIC (t) = (code != FUNCTION_DECL);
}
if (TREE_EXTERNAL (t))
TREE_STATIC (t) = 0;
if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
layout_decl (t, 0);
else if (code == FUNCTION_DECL)
/* all functions considered external unless def in this file */
{
TREE_EXTERNAL (t) = 1;
DECL_MODE (t) = FUNCTION_MODE;
}
return t;
}
/* Create a LABEL_STMT statement node containing a LABEL_DECL named NAME.
NAME may be a null pointer.
FILE ane LINENUM say where in the source the label is.
CONTEXT is the LET_STMT node which is the context of this label name.
The label name definition is entered in the symbol table. */
tree
build_label (filename, line, name, context)
char *filename;
int line;
tree name;
tree context;
{
register tree t = make_node (LABEL_STMT);
register tree decl = build_decl (LABEL_DECL, name, NULL_TREE, 1, 0);
STMT_SOURCE_FILE (t) = filename;
STMT_SOURCE_LINE (t) = line;
STMT_BODY (t) = decl;
DECL_SOURCE_FILE (decl) = filename;
DECL_SOURCE_LINE (decl) = line;
DECL_MODE (decl) = VOIDmode;
DECL_CONTEXT (decl) = context;
pushlabel (t);
return t;
}
/* Store the data into a LET_STMT node.
Each C braced grouping with declarations is represented
by a LET_STMT node. The node is created when the open-brace is read,
but the contents to put in it are not known until the close-brace.
This function is called at the time of the close-brace
to install the proper contents.
BLOCK is the LET_STMT node itself.
DCLS is the chain of declarations within the grouping.
TAGS is the chain of struct, union and enum tags defined within it.
STMTS is the chain of statements making up the inside of the grouping. */
finish_block (block, dcls, tags, stmts)
tree block, dcls, tags, stmts;
{
register tree tem;
/* In each decl, record the block it belongs to. */
for (tem = dcls; tem; tem = TREE_CHAIN (tem))
DECL_CONTEXT (tem) = block;
STMT_VARS (block) = dcls;
STMT_BODY (block) = stmts;
STMT_TYPE_TAGS (block) = tags;
}
finish_tree ()
{
}
/* Create the predefined scalar types of C,
and some nodes representing standard constants (0, 1, (void *)0).
Initialize the global binding level.
Make definitions for built-in primitive functions. */
void
init_decl_processing ()
{
register tree endlink;
named_labels = NULL;
all_gotos = NULL;
current_binding_level = NULL_BINDING_LEVEL;
free_binding_level = NULL_BINDING_LEVEL;
pushlevel (); /* make the binding_level structure for global names */
global_binding_level = current_binding_level;
value_identifier = get_identifier ("<value>");
/* This must be the first type made and laid out,
so that it will get used as the type for expressions
for the sizes of types. */
integer_type_node = make_signed_type (BITS_PER_WORD);
pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_INT],
integer_type_node, 1, 0));
error_mark_node = make_node (ERROR_MARK);
TREE_TYPE (error_mark_node) = error_mark_node;
short_integer_type_node = make_signed_type (BITS_PER_UNIT * min (UNITS_PER_WORD / 2, 2));
pushdecl (build_decl (TYPE_DECL, get_identifier ("short int"),
short_integer_type_node, 1, 0));
long_integer_type_node = make_signed_type (BITS_PER_WORD);
pushdecl (build_decl (TYPE_DECL, get_identifier ("long int"),
long_integer_type_node, 1, 0));
short_unsigned_type_node = make_unsigned_type (BITS_PER_UNIT * min (UNITS_PER_WORD / 2, 2));
pushdecl (build_decl (TYPE_DECL, get_identifier ("short unsigned int"),
short_unsigned_type_node, 1, 0));
unsigned_type_node = make_unsigned_type (BITS_PER_WORD);
pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
unsigned_type_node, 1, 0));
long_unsigned_type_node = make_unsigned_type (BITS_PER_WORD);
pushdecl (build_decl (TYPE_DECL, get_identifier ("long unsigned int"),
long_unsigned_type_node, 1, 0));
char_type_node = make_signed_type (BITS_PER_UNIT);
pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_CHAR],
char_type_node, 1, 0));
unsigned_char_type_node = make_unsigned_type (BITS_PER_UNIT);
pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned char"),
unsigned_char_type_node, 1, 0));
float_type_node = make_node (REAL_TYPE);
TYPE_PRECISION (float_type_node) = BITS_PER_WORD;
pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_FLOAT],
float_type_node, 1, 0));
layout_type (float_type_node);
double_type_node = make_node (REAL_TYPE);
TYPE_PRECISION (double_type_node) = 2 * BITS_PER_WORD;
pushdecl (build_decl (TYPE_DECL, ridpointers[(int) RID_DOUBLE],
double_type_node, 1, 0));
layout_type (double_type_node);
long_double_type_node = make_node (REAL_TYPE);
TYPE_PRECISION (long_double_type_node) = 2 * BITS_PER_WORD;
pushdecl (build_decl (TYPE_DECL, get_identifier ("long double"),
long_double_type_node, 1, 0));
layout_type (long_double_type_node);
integer_zero_node = build_int_2 (0, 0);
TREE_TYPE (integer_zero_node) = integer_type_node;
integer_one_node = build_int_2 (1, 0);
TREE_TYPE (integer_one_node) = integer_type_node;
void_type_node = make_node (VOID_TYPE);
pushdecl (build_decl (TYPE_DECL,
ridpointers[(int) RID_VOID], void_type_node,
1, 0));
layout_type (void_type_node); /* Uses integer_zero_node */
null_pointer_node = build_int_2 (0, 0);
TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
layout_type (TREE_TYPE (null_pointer_node));
string_type_node = build_pointer_type (char_type_node);
layout_type (string_type_node);
/* make a type for arrays of 256 characters.
256 is picked randomly because we have a type for integers from 0 to 255.
With luck nothing will ever really depend on the length of this
array type. */
char_array_type_node
= build_array_type (char_type_node, unsigned_char_type_node);
layout_type (char_array_type_node);
default_function_type
= build_function_type (integer_type_node, NULL_TREE);
layout_type (default_function_type);
ptr_type_node = build_pointer_type (void_type_node);
endlink = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
double_ftype_double
= build_function_type (double_type_node,
tree_cons (NULL_TREE, double_type_node, endlink));
double_ftype_double_double
= build_function_type (double_type_node,
tree_cons (NULL_TREE, double_type_node,
tree_cons (NULL_TREE,
double_type_node, endlink)));
int_ftype_int
= build_function_type (integer_type_node,
tree_cons (NULL_TREE, integer_type_node, endlink));
long_ftype_long
= build_function_type (long_integer_type_node,
tree_cons (NULL_TREE,
long_integer_type_node, endlink));
void_ftype_ptr_ptr_int
= build_function_type (void_type_node,
tree_cons (NULL_TREE, ptr_type_node,
tree_cons (NULL_TREE, ptr_type_node,
tree_cons (NULL_TREE,
integer_type_node,
endlink))));
int_ftype_ptr_ptr_int
= build_function_type (integer_type_node,
tree_cons (NULL_TREE, ptr_type_node,
tree_cons (NULL_TREE, ptr_type_node,
tree_cons (NULL_TREE,
integer_type_node,
endlink))));
void_ftype_ptr_int_int
= build_function_type (void_type_node,
tree_cons (NULL_TREE, ptr_type_node,
tree_cons (NULL_TREE, integer_type_node,
tree_cons (NULL_TREE,
integer_type_node,
endlink))));
builtin_function ("_builtin_alloca",
build_function_type (ptr_type_node,
tree_cons (NULL_TREE,
integer_type_node,
endlink)),
BUILT_IN_ALLOCA);
builtin_function ("_builtin_abs", int_ftype_int, BUILT_IN_ABS);
builtin_function ("_builtin_fabs", double_ftype_double, BUILT_IN_FABS);
builtin_function ("_builtin_labs", long_ftype_long, BUILT_IN_LABS);
/* builtin_function ("_builtin_div", default_ftype, BUILT_IN_DIV);
builtin_function ("_builtin_ldiv", default_ftype, BUILT_IN_LDIV); */
builtin_function ("_builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR);
builtin_function ("_builtin_fceil", double_ftype_double, BUILT_IN_FCEIL);
builtin_function ("_builtin_fmod", double_ftype_double_double, BUILT_IN_FMOD);
builtin_function ("_builtin_frem", double_ftype_double_double, BUILT_IN_FREM);
builtin_function ("_builtin_memcpy", void_ftype_ptr_ptr_int, BUILT_IN_MEMCPY);
builtin_function ("_builtin_memcmp", int_ftype_ptr_ptr_int, BUILT_IN_MEMCMP);
builtin_function ("_builtin_memset", void_ftype_ptr_int_int, BUILT_IN_MEMSET);
builtin_function ("_builtin_fsqrt", double_ftype_double, BUILT_IN_FSQRT);
builtin_function ("_builtin_getexp", double_ftype_double, BUILT_IN_GETEXP);
builtin_function ("_builtin_getman", double_ftype_double, BUILT_IN_GETMAN);
}
/* Make a definition for a builtin function named NAME and whose data type
is TYPE. TYPE should be a function type with argument types.
FUNCTION_CODE tells later passes how to compile calls to this function.
See tree.h for its possible values. */
static void
builtin_function (name, type, function_code)
char *name;
tree type;
enum built_in_function function_code;
{
tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type, 0, 1);
make_function_rtl (decl);
pushdecl (decl);
DECL_SET_FUNCTION_CODE (decl, function_code);
}
/* Validate a structure, union or enum type: make sure it
is not just a forward reference.
If it is valid, return it. Otherwise, return error_mark_node.
Callers may want to use the returned value instead of the original type. */
tree
resolve_tags (type)
tree type;
{
register char *errmsg = NULL;
switch (TREE_CODE (type))
{
case RECORD_TYPE:
errmsg = "undefined struct tag %s";
break;
case UNION_TYPE:
errmsg = "undefined union tag %s";
break;
case ENUMERAL_TYPE:
errmsg = "undefined enum tag %s";
break;
default:
return type;
}
if (TYPE_SIZE (type) != 0)
return type;
yyerror (errmsg, IDENTIFIER_POINTER (TYPE_NAME (type)));
return error_mark_node;
}
/* Called when a declaration is seen that contains no names to declare.
If its type is a reference to a structure, union or enum inherited
from a containing scope, shadow that tag name for the current scope
with a forward reference.
If its type defines a new named structure or union
or defines an enum, it is valid but we need not do anything here.
Otherwise, it is an error. */
void
shadow_tag (declspecs)
tree declspecs;
{
register tree link;
for (link = declspecs; link; link = TREE_CHAIN (link))
{
register tree value = TREE_VALUE (link);
register enum tree_code code = TREE_CODE (value);
if ((code == RECORD_TYPE || code == UNION_TYPE || code == ENUMERAL_TYPE)
&& TYPE_SIZE (value) != 0)
{
register tree name = TYPE_NAME (value);
register tree t = lookup_tag (code, name, current_binding_level, 1);
if (t == 0)
{
t = make_node (code);
pushtag (name, t);
return;
}
if (name != 0 || code == ENUMERAL_TYPE)
return;
}
}
warning ("empty declaration");
}
/* Decode a "typename", such as "int **", returning a ..._TYPE node. */
tree
groktypename (typename)
tree typename;
{
return grokdeclarator (TREE_VALUE (typename),
TREE_PURPOSE (typename),
TYPENAME);
}
/* Decode a declarator in an ordinary declaration or data definition.
This is called as soon as the type information and variable name
have been parsed, before parsing the initializer if any.
Here we create the ..._DECL node, fill in its type,
and put it on the list of decls for the current context.
The ..._DECL node is returned as the value.
Exception: for arrays where the length is not specified,
the type is left null, to be filled in by `finish_decl'.
Function definitions do not come here; they go to start_function
instead. However, external and forward declarations of functions
do go through here. Structure field declarations are done by
grokfield and not through here. */