Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / cemcom.ansi / declarator.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: declarator.c,v 1.8 1994/06/27 07:59:09 ceriel Exp $ */
6 /*      D E C L A R A T O R   M A N I P U L A T I O N           */
7
8 #include        "debug.h"
9 #include        "botch_free.h"
10 #include        <alloc.h>
11 #include        <flt_arith.h>
12 #include        "arith.h"
13 #include        "type.h"
14 #include        "proto.h"
15 #include        "Lpars.h"
16 #include        "declar.h"
17 #include        "def.h"
18 #include        "idf.h"
19 #include        "label.h"
20 #include        "expr.h"
21 #include        "sizes.h"
22 #include        "level.h"
23
24 extern char options[];
25 struct declarator null_declarator;
26
27 struct type *
28 declare_type(tp, dc)
29         struct type *tp;
30         struct declarator *dc;
31 {
32         /*      Applies the decl_unary list starting at dc->dc_decl_unary
33                 to the type tp and returns the result.
34                 Functions that are declared within a parameter type list
35                 are purely prototypes. Simply add the type list to the
36                 function node.
37         */
38         register struct decl_unary *du = dc->dc_decl_unary;
39
40         while (du)      {
41                 tp = construct_type(du->du_fund, tp, du->du_typequal,
42                                     du->du_count, du->du_proto);
43                 du = du->next;
44         }
45         return tp;
46 }
47
48 add_decl_unary(dc, fund, qual,  count, fm, pl)
49         register struct declarator *dc;
50         int qual;
51         arith count;
52         struct formal *fm;
53         struct proto *pl;
54 {
55         /*      A decl_unary describing a constructor with fundamental
56                 type fund and with size count is inserted in front of the
57                 declarator dc.
58         */
59         register struct decl_unary *new = new_decl_unary();
60
61         new->next = dc->dc_decl_unary;
62         new->du_fund = fund;
63         new->du_count = count;
64         new->du_typequal = qual;
65         new->du_proto = pl;
66         if (fm) {
67                 if (dc->dc_decl_unary)  {
68                         /* parameters only allowed at first decl_unary  */
69                         error("formal parameters list discarded");
70                 }
71                 else    {
72                         /* register the proto   */
73                         dc->dc_formal = fm;
74                 }
75         }
76
77         dc->dc_decl_unary = new;
78 }
79
80 remove_declarator(dc)
81         struct declarator *dc;
82 {
83         /*      The decl_unary list starting at dc->dc_decl_unary is
84                 removed.
85         */
86         register struct decl_unary *du = dc->dc_decl_unary;
87
88         while (du)      {
89                 struct decl_unary *old_du = du;
90
91                 du = du->next;
92                 free_decl_unary(old_du);
93         }
94 }
95
96 reject_params(dc)
97         register struct declarator *dc;
98 {
99         /*      The declarator is checked to have no parameters, if it
100                 is an old-style function.  If it is a new-style function,
101                 the identifiers are removed.  The function is not called in
102                 case of a function definition.
103         */
104         register struct decl_unary *du = dc->dc_decl_unary;
105         int     err_given = 0;
106
107         if (dc->dc_formal)      {
108                 error("non_empty formal parameter pack");
109                 free_formals(dc->dc_formal);
110                 dc->dc_formal = 0;
111                 err_given = 1;
112         }
113         while (du) {
114                 if (du->du_fund == FUNCTION) {
115                         if (du->du_proto) remove_proto_idfs(du->du_proto);
116                         else if (! err_given && ! options['o']) {
117                                 err_given = 1;
118                                 warning("old-fashioned function declaration");
119                         }
120                 }
121                 du = du->next;
122         }
123 }
124
125 check_array_subscript(expr)
126         register struct expr *expr;
127 {
128         arith size = expr->VL_VALUE;
129
130         if (size < 0)   {
131                 error("array size is negative");
132                 expr->VL_VALUE = (arith)1;
133         }
134         else
135         if (size == 0) {
136                 strict("array size is 0");
137         }
138         else
139         if (size & ~max_unsigned) {     /* absolutely ridiculous */
140                 expr_error(expr, "overflow in array size");
141                 expr->VL_VALUE = (arith)1;
142         }
143 }