Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / decspecs.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 /* $Id: decspecs.c,v 1.11 1994/06/27 07:59:12 ceriel Exp $ */
6 /*      D E C L A R A T I O N   S P E C I F I E R   C H E C K I N G     */
7
8 #include        "assert.h"
9 #include        "Lpars.h"
10 #include        "decspecs.h"
11 #include        "arith.h"
12 #include        "type.h"
13 #include        "level.h"
14 #include        "def.h"
15
16 extern char options[];
17 extern int level;
18 extern char *symbol2str();
19 extern struct type *qualifier_type();
20
21 struct decspecs null_decspecs;
22
23 do_decspecs(ds)
24         register struct decspecs *ds;
25 {
26         /*      The provisional decspecs ds as obtained from the program
27                 is turned into a legal consistent decspecs.
28         */
29         register struct type *tp = ds->ds_type;
30         
31         ASSERT(level != L_FORMAL1);
32         
33         if (    level == L_GLOBAL &&
34                 (ds->ds_sc == AUTO || ds->ds_sc == REGISTER)
35         )       {
36                 error("no global %s variable allowed",
37                         symbol2str(ds->ds_sc));
38                 ds->ds_sc = GLOBAL;
39         }
40
41         if (level == L_FORMAL2) {
42                 if (ds->ds_sc_given &&
43                     ds->ds_sc != REGISTER){
44                         error("%s formal illegal", symbol2str(ds->ds_sc));
45                         ds->ds_sc = FORMAL;
46                 }
47         }
48
49         /*      Since type qualifiers may be associated with types by means
50                 of typedefs, we have to perform same basic tests down here.
51         */
52         if (tp != (struct type *)0) {
53                 if ((ds->ds_typequal & TQ_VOLATILE) && (tp->tp_typequal & TQ_VOLATILE))
54                         error("indirect repeated type qualifier");
55                 if ((ds->ds_typequal & TQ_CONST) && (tp->tp_typequal & TQ_CONST))
56                         error("indirect repeated type qualifier");
57                 ds->ds_typequal |= tp->tp_typequal;
58         }
59
60         /*      The tests concerning types require a full knowledge of the
61                 type and will have to be postponed to declare_idf.
62         */
63
64         /* some adjustments as described in 3.5.2. */
65         if (tp == 0) {
66                 ds->ds_notypegiven = 1;
67                 tp = int_type;
68         }
69         if (ds->ds_size) {
70                 register int ds_isshort = (ds->ds_size == SHORT);
71
72                 if (ds->ds_typedef) goto SIZE_ERROR;            /* yes */
73                 if (tp == int_type) {
74                         if (ds_isshort) tp = short_type;
75                         else tp = long_type;
76                 } else if (tp == double_type && !ds_isshort ) {
77                         tp = lngdbl_type;
78                 } else {
79         SIZE_ERROR:
80                         error("%s with illegal type",symbol2str(ds->ds_size));
81                 }
82                 ds->ds_notypegiven = 0;
83         }
84         if (ds->ds_unsigned) {
85                 register int ds_isunsigned = (ds->ds_unsigned == UNSIGNED);
86
87                 if (ds->ds_typedef) goto SIGN_ERROR;            /* yes */
88                 /*
89                  * All integral types are signed by default (char too),
90                  * so the case that ds->ds_unsigned == SIGNED can be ignored.
91                  */
92                 if (tp == schar_type) {
93                         if (ds_isunsigned) tp = uchar_type;
94                 } else if (tp == short_type) {
95                         if (ds_isunsigned) tp = ushort_type;
96                 } else if (tp == int_type) {
97                         if (ds_isunsigned) tp = uint_type;
98                 } else if (tp == long_type) {
99                         if (ds_isunsigned) tp = ulong_type;
100                 } else {
101         SIGN_ERROR:
102                         error("%s with illegal type"
103                                 , symbol2str(ds->ds_unsigned));
104                 }
105                 ds->ds_notypegiven = 0;
106         }
107         ds->ds_type = qualifier_type(tp, ds->ds_typequal);
108 }
109
110 /*      Make tp into a qualified type. This is not as trivial as it
111         may seem. If tp is a fundamental type the qualified type is
112         either existent or will be generated.
113         In case of a complex type the top of the type list will be
114         replaced by a qualified version.
115 */
116 struct type *
117 qualifier_type(tp, typequal)
118         register struct type *tp;
119         int typequal;
120 {
121         register struct type *dtp = tp;
122         register int fund = tp->tp_fund;
123
124         while (dtp && dtp->tp_typequal != typequal)
125                 dtp = dtp->next;
126
127         if (!dtp) {
128                 dtp = create_type(fund);
129                 dtp->tp_unsigned = tp->tp_unsigned;
130                 dtp->tp_align = tp->tp_align;
131                 dtp->tp_typequal = typequal;
132                 dtp->tp_size = tp->tp_size;
133 #if 0
134 /* The tp_function field does not exist now. See the comment in the
135    function_of() routine.
136 */
137                 dtp->tp_function = tp->tp_function;
138 #endif
139                 switch (fund) {
140                 case ARRAY:
141                         if (typequal) {
142                             tp->tp_up = qualifier_type(tp->tp_up, typequal);
143                             dtp->tp_typequal = typequal = 0;
144                         }
145                         goto nottagged;
146                 case FIELD:
147                         dtp->tp_field = tp->tp_field;
148                         /* fallthrough */
149                 case POINTER:
150                 case FUNCTION:                  /* dont't assign tp_proto */
151                 nottagged:
152                         dtp->tp_up = tp->tp_up;
153                         break;
154                 case STRUCT:
155                 case UNION:
156                 case ENUM:
157                         dtp->tp_idf = tp->tp_idf;
158                         dtp->tp_sdef = tp->tp_sdef;
159                         break;
160                 default:
161                         break;
162                 }
163                 dtp->next = tp->next; /* don't know head or tail */
164                 tp->next = dtp;
165         }
166         return(dtp);
167 }
168