]> git.draconx.ca Git - cdecl99.git/blob - src/parse-decl.c
Validate declspecs in function parameters.
[cdecl99.git] / src / parse-decl.c
1 /*
2  *  Parse and validate C declarations.
3  *  Copyright © 2011 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include <stdio.h>
19 #include <assert.h>
20 #include <stdbool.h>
21
22 #include "cdecl.h"
23 #include "typemap.h"
24 #include "parse.h"
25 #include "scan.h"
26
27 /*
28  * Verify the declaration specifiers of a declaration.  If top is true, treat
29  * this as a top-level declaration.  Otherwise, treat this as a function
30  * parameter (which carries additional constraints).
31  */
32 static bool valid_declspecs(struct cdecl *decl, bool top)
33 {
34         struct cdecl_declspec *specs = decl->specifiers;
35         struct cdecl_declarator *d   = decl->declarators;
36         unsigned num_storage = 0;
37         unsigned long typemap;
38
39         typemap = cdecl__build_typemap(specs);
40         if (typemap == -1)
41                 return -1;
42
43         for (struct cdecl_declspec *c = specs; c; c = c->next) {
44                 switch (cdecl_spec_kind(c)) {
45                 case CDECL_SPEC_TYPE:
46                         if (c->type == CDECL_TYPE_VOID &&
47                             (d->type == CDECL_DECL_IDENT
48                              || d->type == CDECL_DECL_ARRAY)) {
49                                 fprintf(stderr, "invalid declaration of type void\n");
50                                 return false;
51                         }
52                         continue;
53                 case CDECL_SPEC_STOR:
54                         if (!top && c->type != CDECL_STOR_REGISTER) {
55                                 fprintf(stderr, "function parameters may only have register storage\n");
56                                 return false;
57                         }
58
59                         if (++num_storage > 1) {
60                                 fprintf(stderr, "too many storage-class specifiers\n");
61                                 return false;
62                         }
63                         break;
64                 case CDECL_SPEC_QUAL:
65                         /*
66                          * Restrict qualifiers are only valid in the
67                          * pointer qualifier list, which isn't checked here.
68                          */
69                         if (c->type == CDECL_QUAL_RESTRICT) {
70                                 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
71                                 return false;
72                         }
73                         break;
74                 case CDECL_SPEC_FUNC:
75                         if (!top || d->type != CDECL_DECL_FUNCTION) {
76                                 fprintf(stderr, "only function declarations may have function specifiers.\n");
77                                 return false;
78                         }
79                         break;
80                 default:
81                         assert(0);
82                 }
83         }
84
85         return true;
86 }
87
88 /*
89  * The C grammar leaves ambiguous some cases where parentheses represent a
90  * function declarator or just parentheses.  The language uses additional
91  * context (whether or not a typedef is in scope, etc.) to resolve these
92  * ambiguities, but we don't have access to that kind of information.
93  *
94  * The cdecl99 parser uses an unambiguous grammar which treats almost
95  * everything as a function, and thus considers things like 'int (x)' to
96  * be a function type with a single parameter of type 'x' (a typedef name),
97  * returning int.  This can result in very complicated types for simple
98  * declarations.  Ideally, cdecl99 should try and find the "simplest"
99  * explanation for a given declaration.
100  *
101  * Whether or not it achieves the simplest explanation, we apply a simple rule:
102  * if a declarator could be interpreted as something other than a function,
103  * do that.
104  *
105  *   - The function declarator has a null child declarator.
106  *   - The function declarator has exactly one parameter, and is not variadic.
107  *   - The function parameter has a type specifier, and it is a typedef name.
108  *   - The function parameter has no other declaration specifiers.
109  *   - The function parameter does not declare an identifier.
110  *
111  * Since cdecl99 supports things like [*] in any context (in C, such constructs
112  * are only valid in function parameter lists), we don't treat them specially
113  * here.
114  */
115
116 static struct cdecl_declarator *reduce_function(struct cdecl *param)
117 {
118         struct cdecl_declspec   *spec = param->specifiers;
119         struct cdecl_declarator *decl = param->declarators;
120         struct cdecl_declarator *last;
121
122         for (last = decl; last && last->type != CDECL_DECL_NULL;)
123                 last = last->child;
124
125         if (!last)
126                 return NULL;
127
128         last->type = CDECL_DECL_IDENT;
129         last->u.ident = spec->ident;
130         free(param);
131         free(spec);
132
133         return decl;
134 }
135
136 static bool function_is_reducible(struct cdecl_declarator *d)
137 {
138         if (d->type != CDECL_DECL_FUNCTION)
139                 return false;
140         if (d->child->type != CDECL_DECL_NULL)
141                 return false; /* e.g., int (*)(x) */
142
143         if (!d->u.function.parameters)
144                 return false; /* e.g., int f() */
145         if (d->u.function.parameters->next)
146                 return false; /* e.g., int (x, y) */
147         if (d->u.function.variadic)
148                 return false; /* e.g., int (x, ...) */
149
150         if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
151                 return false; /* e.g. int (int) */
152         if (d->u.function.parameters->specifiers->next)
153                 return false; /* e.g. int (size_t const) */
154
155         return true;
156 }
157
158 static int
159 simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
160 {
161         struct cdecl_declarator *new;
162
163         if (!function_is_reducible(d))
164                 return 0;
165
166         new = reduce_function(d->u.function.parameters);
167         if (!new)
168                 return 0; /* e.g. int (foo bar) */
169         *p = new;
170         free(d->child);
171         free(d);
172
173         return 1;
174 }
175
176 /*
177  * The parser's bias towards considering things as functions whenever possible
178  * makes nested parentheses tricky.  (x) is considered to be part of a function
179  * declarator until simplify_functions converts it.  The problem is that
180  * (((x))) is not valid as part of a function declarator, but it *is* valid
181  * as an identifier enclosed 3 times in parentheses.  This is complicated by
182  * the fact that things like (((int))) are not valid anywhere.
183  *
184  * To avoid ambiguities, the parser actually emits a "function" declarator for
185  * every pair of parentheses.  The ones that can't reasonably be functions
186  * consist of a single "parameter" with no declaration specifiers (note that
187  * every valid function parameter will have at least one type specifier).
188  *
189  * This pass is to remove these fake functions from the parse tree.  We take
190  * care to avoid turning invalid things like ((int)) into valid things like
191  * (int) by observing that the only valid function declarators that appear
192  * in these "fake" parentheses are those that have a non-null child declarator
193  * (for instance, int ((*)(int)) *or* those that will be eliminated by the
194  * simplify_functions pass.
195  */
196
197 static int
198 reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
199 {
200         struct cdecl *param;
201
202         if (d->type != CDECL_DECL_FUNCTION)
203                 return 0;
204
205         param = d->u.function.parameters;
206         if (param && param->specifiers == NULL) {
207                 struct cdecl_declarator *decl;
208
209                 assert(!param->next);
210
211                 decl = param->declarators;
212                 if (decl->type == CDECL_DECL_NULL) {
213                         free(decl);
214                         free(param);
215                         d->u.function.parameters = NULL;
216                         return 0;
217                 }
218
219                 if (d->child->type != CDECL_DECL_NULL) {
220                         fprintf(stderr, "invalid function parameter\n");
221                         return -1;
222                 }
223
224                 free(d->child);
225                 free(param);
226                 free(d);
227                 *p = decl;
228
229                 /*
230                  * We may have replaced d with another fake function which
231                  * also needs to be eliminated.
232                  */
233                 if (reduce_parentheses(p, decl) < 0)
234                         return -1;
235
236                 /*
237                  * If the remaining declarator is a function, make sure it's
238                  * valid by checking its reducibility.
239                  */
240                 decl = *p;
241                 if (decl->type == CDECL_DECL_FUNCTION
242                     && decl->child->type == CDECL_DECL_NULL
243                     && !function_is_reducible(decl)) {
244                         fprintf(stderr, "too many parentheses in function\n");
245                         return -1;
246                 }
247
248                 return 1;
249         }
250
251         return 0;
252 }
253
254 static int
255 check_parameters(struct cdecl_declarator **p, struct cdecl_declarator *d)
256 {
257         struct cdecl_declspec *spec;
258         struct cdecl *param;
259         bool has_void = false;
260
261         if (d->type != CDECL_DECL_FUNCTION)
262                 return 0;
263
264         for (param = d->u.function.parameters; param; param = param->next) {
265                 if (!valid_declspecs(param, false))
266                         return -1;
267
268                 for (spec = param->specifiers; spec; spec = spec->next) {
269                         if (spec->type == CDECL_TYPE_VOID
270                             && param->declarators->type == CDECL_DECL_NULL)
271                                 has_void = true;
272                 }
273         }
274
275         if (has_void && d->u.function.parameters->next) {
276                 fprintf(stderr, "a void parameter must stand alone\n");
277                 return -1;
278         } else if (has_void && d->u.function.variadic) {
279                 fprintf(stderr, "variadic functions cannot have a void parameter\n");
280                 return -1;
281         }
282
283         return 0;
284 }
285
286 /*
287  * Traverse the parse tree, calling a function on every declarator in a
288  * depth-first preorder traversal.  The function is given a pointer to the
289  * declarator as well as to the pointer which was used to reach that
290  * declarator: this can be used to rewrite entire subtrees.
291  */
292 static bool forall_declarators(struct cdecl *decl,
293         int f(struct cdecl_declarator **, struct cdecl_declarator *))
294 {
295         struct cdecl_declarator *d, **p;
296
297         for (p = &decl->declarators, d = *p; d; p = &d->child, d = *p) {
298                 switch (f(p, d)) {
299                 case 0:
300                         break;
301                 case 1:
302                         d = *p;
303                         break;
304                 case -1:
305                         return false;
306                 default:
307                         assert(0);
308                 }
309
310                 if (d->type == CDECL_DECL_FUNCTION) {
311                         struct cdecl *i;
312
313                         for (i = d->u.function.parameters; i; i = i->next) {
314                                 if (!forall_declarators(i, f))
315                                         return false;
316                         }
317                 }
318         }
319
320         return true;
321 }
322
323 struct cdecl *cdecl_parse_decl(const char *declstr)
324 {
325         YY_BUFFER_STATE state;
326         struct cdecl *decl;
327         int rc;
328
329         state = yy_scan_string(declstr);
330         rc = yyparse(&decl);
331         yy_delete_buffer(state);
332
333         if (rc != 0)
334                 return NULL;
335
336         for (struct cdecl *i = decl; i; i = i->next) {
337                 if (!forall_declarators(i, reduce_parentheses))
338                         goto err;
339                 if (!forall_declarators(i, simplify_functions))
340                         goto err;
341                 if (!forall_declarators(i, check_parameters))
342                         goto err;
343
344                 if (!valid_declspecs(i, true))
345                         goto err;
346         }
347
348         return decl;
349 err:
350         cdecl_free(decl);
351         return NULL;
352 }