]> git.draconx.ca Git - cdecl99.git/blob - src/parse-decl.c
Forbid arrays of functions.
[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  * Determine if a declarator declares an identifier (other than a function
29  * parameter).
30  */
31 static bool is_abstract(struct cdecl_declarator *d)
32 {
33         while (d->child)
34                 d = d->child;
35
36         return d->type != CDECL_DECL_IDENT;
37 }
38
39 /*
40  * Verify the declaration specifiers of a declaration.  If top is true, treat
41  * this as a top-level declaration.  Otherwise, treat this as a function
42  * parameter (which carries additional constraints).
43  */
44 static bool valid_declspecs(struct cdecl *decl, bool top)
45 {
46         struct cdecl_declspec *specs = decl->specifiers;
47         struct cdecl_declarator *d   = decl->declarators;
48         bool abstract = is_abstract(d);
49         unsigned num_storage = 0;
50         unsigned long typemap;
51
52         typemap = cdecl__build_typemap(specs);
53         if (typemap == -1)
54                 return -1;
55
56
57         for (struct cdecl_declspec *c = specs; c; c = c->next) {
58                 switch (cdecl_spec_kind(c)) {
59                 case CDECL_SPEC_TYPE:
60                         if (c->type == CDECL_TYPE_VOID &&
61                             (d->type == CDECL_DECL_IDENT
62                              || d->type == CDECL_DECL_ARRAY)) {
63                                 fprintf(stderr, "invalid declaration of type void\n");
64                                 return false;
65                         }
66                         continue;
67                 case CDECL_SPEC_STOR:
68                         if (top && abstract) {
69                                 fprintf(stderr, "type names cannot have storage-class specifiers\n");
70                                 return false;
71                         }
72
73                         if (!top && c->type != CDECL_STOR_REGISTER) {
74                                 fprintf(stderr, "function parameters may only have register storage\n");
75                                 return false;
76                         }
77
78                         if (++num_storage > 1) {
79                                 fprintf(stderr, "too many storage-class specifiers\n");
80                                 return false;
81                         }
82                         break;
83                 case CDECL_SPEC_QUAL:
84                         /*
85                          * Restrict qualifiers are only valid in the
86                          * pointer qualifier list, which isn't checked here.
87                          */
88                         if (c->type == CDECL_QUAL_RESTRICT) {
89                                 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
90                                 return false;
91                         }
92                         break;
93                 case CDECL_SPEC_FUNC:
94                         if (abstract) {
95                                 fprintf(stderr, "type names cannot have function specifiers\n");
96                                 return false;
97                         }
98
99                         if (!top || d->type != CDECL_DECL_FUNCTION) {
100                                 fprintf(stderr, "only function declarations may have function specifiers.\n");
101                                 return false;
102                         }
103                         break;
104                 default:
105                         assert(0);
106                 }
107         }
108
109         return true;
110 }
111
112 /*
113  * The C grammar leaves ambiguous some cases where parentheses represent a
114  * function declarator or just parentheses.  The language uses additional
115  * context (whether or not a typedef is in scope, etc.) to resolve these
116  * ambiguities, but we don't have access to that kind of information.
117  *
118  * The cdecl99 parser uses an unambiguous grammar which treats almost
119  * everything as a function, and thus considers things like 'int (x)' to
120  * be a function type with a single parameter of type 'x' (a typedef name),
121  * returning int.  This can result in very complicated types for simple
122  * declarations.  Ideally, cdecl99 should try and find the "simplest"
123  * explanation for a given declaration.
124  *
125  * Whether or not it achieves the simplest explanation, we apply a simple rule:
126  * if a declarator could be interpreted as something other than a function,
127  * do that.
128  *
129  *   - The function declarator has a null child declarator.
130  *   - The function declarator has exactly one parameter, and is not variadic.
131  *   - The function parameter has a type specifier, and it is a typedef name.
132  *   - The function parameter has no other declaration specifiers.
133  *   - The function parameter does not declare an identifier.
134  *
135  * Since cdecl99 supports things like [*] in any context (in C, such constructs
136  * are only valid in function parameter lists), we don't treat them specially
137  * here.
138  */
139
140 static struct cdecl_declarator *reduce_function(struct cdecl *param)
141 {
142         struct cdecl_declspec   *spec = param->specifiers;
143         struct cdecl_declarator *decl = param->declarators;
144         struct cdecl_declarator *last;
145
146         for (last = decl; last && last->type != CDECL_DECL_NULL;)
147                 last = last->child;
148
149         if (!last)
150                 return NULL;
151
152         last->type = CDECL_DECL_IDENT;
153         last->u.ident = spec->ident;
154         free(param);
155         free(spec);
156
157         return decl;
158 }
159
160 static bool function_is_reducible(struct cdecl_declarator *d)
161 {
162         if (d->type != CDECL_DECL_FUNCTION)
163                 return false;
164         if (d->child->type != CDECL_DECL_NULL)
165                 return false; /* e.g., int (*)(x) */
166
167         if (!d->u.function.parameters)
168                 return false; /* e.g., int f() */
169         if (d->u.function.parameters->next)
170                 return false; /* e.g., int (x, y) */
171         if (d->u.function.variadic)
172                 return false; /* e.g., int (x, ...) */
173
174         if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
175                 return false; /* e.g. int (int) */
176         if (d->u.function.parameters->specifiers->next)
177                 return false; /* e.g. int (size_t const) */
178
179         return true;
180 }
181
182 static int
183 simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
184 {
185         struct cdecl_declarator *new;
186
187         if (!function_is_reducible(d))
188                 return 0;
189
190         new = reduce_function(d->u.function.parameters);
191         if (!new)
192                 return 0; /* e.g. int (foo bar) */
193         *p = new;
194         free(d->child);
195         free(d);
196
197         return 1;
198 }
199
200 /*
201  * The parser's bias towards considering things as functions whenever possible
202  * makes nested parentheses tricky.  (x) is considered to be part of a function
203  * declarator until simplify_functions converts it.  The problem is that
204  * (((x))) is not valid as part of a function declarator, but it *is* valid
205  * as an identifier enclosed 3 times in parentheses.  This is complicated by
206  * the fact that things like (((int))) are not valid anywhere.
207  *
208  * To avoid ambiguities, the parser actually emits a "function" declarator for
209  * every pair of parentheses.  The ones that can't reasonably be functions
210  * consist of a single "parameter" with no declaration specifiers (note that
211  * every valid function parameter will have at least one type specifier).
212  *
213  * This pass is to remove these fake functions from the parse tree.  We take
214  * care to avoid turning invalid things like ((int)) into valid things like
215  * (int) by observing that the only valid function declarators that appear
216  * in these "fake" parentheses are those that have a non-null child declarator
217  * (for instance, int ((*)(int)) *or* those that will be eliminated by the
218  * simplify_functions pass.
219  */
220
221 static int
222 reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
223 {
224         struct cdecl *param;
225
226         if (d->type != CDECL_DECL_FUNCTION)
227                 return 0;
228
229         param = d->u.function.parameters;
230         if (param && param->specifiers == NULL) {
231                 struct cdecl_declarator *decl;
232
233                 assert(!param->next);
234
235                 decl = param->declarators;
236                 if (decl->type == CDECL_DECL_NULL) {
237                         free(decl);
238                         free(param);
239                         d->u.function.parameters = NULL;
240                         return 0;
241                 }
242
243                 if (d->child->type != CDECL_DECL_NULL) {
244                         fprintf(stderr, "invalid function parameter\n");
245                         return -1;
246                 }
247
248                 free(d->child);
249                 free(param);
250                 free(d);
251                 *p = decl;
252
253                 /*
254                  * We may have replaced d with another fake function which
255                  * also needs to be eliminated.
256                  */
257                 if (reduce_parentheses(p, decl) < 0)
258                         return -1;
259
260                 /*
261                  * If the remaining declarator is a function, make sure it's
262                  * valid by checking its reducibility.
263                  */
264                 decl = *p;
265                 if (decl->type == CDECL_DECL_FUNCTION
266                     && decl->child->type == CDECL_DECL_NULL
267                     && !function_is_reducible(decl)) {
268                         fprintf(stderr, "too many parentheses in function\n");
269                         return -1;
270                 }
271
272                 return 1;
273         }
274
275         return 0;
276 }
277
278 /*
279  * Function parameters and return types have a few restrictions that are
280  * really easy to check in comparison to the above absurdity.
281  */
282 static int
283 check_parameters(struct cdecl_declarator **p, struct cdecl_declarator *d)
284 {
285         struct cdecl_declspec *spec;
286         struct cdecl *param;
287         bool has_void = false;
288
289         if (d->type != CDECL_DECL_FUNCTION)
290                 return 0;
291
292         for (param = d->u.function.parameters; param; param = param->next) {
293                 if (!valid_declspecs(param, false))
294                         return -1;
295
296                 for (spec = param->specifiers; spec; spec = spec->next) {
297                         if (spec->type == CDECL_TYPE_VOID
298                             && param->declarators->type == CDECL_DECL_NULL)
299                                 has_void = true;
300                 }
301         }
302
303         if (has_void && d->u.function.parameters->next) {
304                 fprintf(stderr, "a void parameter must stand alone\n");
305                 return -1;
306         } else if (has_void && d->u.function.variadic) {
307                 fprintf(stderr, "variadic functions cannot have a void parameter\n");
308                 return -1;
309         }
310
311         return 0;
312 }
313
314 /*
315  * Functions cannot return arrays or functions.  Since the parse tree is
316  * "inside-out", we need to look for functions as the child declarator.
317  */
318 static int
319 check_rettypes(struct cdecl_declarator **p, struct cdecl_declarator *d)
320 {
321         if (!d->child || d->child->type != CDECL_DECL_FUNCTION)
322                 return 0;
323
324         switch (d->type) {
325         case CDECL_DECL_FUNCTION:
326                 fprintf(stderr, "functions cannot return functions\n");
327                 return -1;
328         case CDECL_DECL_ARRAY:
329                 fprintf(stderr, "functions cannot return arrays\n");
330                 return -1;
331         }
332
333         return 0;
334 }
335
336 static int
337 check_arrays(struct cdecl_declarator **p, struct cdecl_declarator *d)
338 {
339         if (!d->child || d->child->type != CDECL_DECL_ARRAY)
340                 return 0;
341
342         switch (d->type) {
343         case CDECL_DECL_FUNCTION:
344                 fprintf(stderr, "array members cannot be functions\n");
345                 return -1;
346         }
347
348         return 0;
349 }
350
351 /*
352  * Traverse the parse tree, calling a function on every declarator in a
353  * depth-first preorder traversal.  The function is given a pointer to the
354  * declarator as well as to the pointer which was used to reach that
355  * declarator: this can be used to rewrite entire subtrees.
356  */
357 static bool forall_declarators(struct cdecl *decl,
358         int f(struct cdecl_declarator **, struct cdecl_declarator *))
359 {
360         struct cdecl_declarator *d, **p;
361
362         for (p = &decl->declarators, d = *p; d; p = &d->child, d = *p) {
363                 switch (f(p, d)) {
364                 case 0:
365                         break;
366                 case 1:
367                         d = *p;
368                         break;
369                 case -1:
370                         return false;
371                 default:
372                         assert(0);
373                 }
374
375                 if (d->type == CDECL_DECL_FUNCTION) {
376                         struct cdecl *i;
377
378                         for (i = d->u.function.parameters; i; i = i->next) {
379                                 if (!forall_declarators(i, f))
380                                         return false;
381                         }
382                 }
383         }
384
385         return true;
386 }
387
388 struct cdecl *cdecl_parse_decl(const char *declstr)
389 {
390         YY_BUFFER_STATE state;
391         yyscan_t scanner;
392         struct cdecl *decl;
393         int rc;
394
395         rc = yylex_init(&scanner);
396         if (rc != 0)
397                 return NULL;
398
399         state = yy_scan_string(declstr, scanner);
400         rc = yyparse(scanner, &decl);
401         yy_delete_buffer(state, scanner);
402         yylex_destroy(scanner);
403
404         if (rc != 0)
405                 return NULL;
406
407         for (struct cdecl *i = decl; i; i = i->next) {
408                 if (!forall_declarators(i, reduce_parentheses))
409                         goto err;
410                 if (!forall_declarators(i, simplify_functions))
411                         goto err;
412                 if (!forall_declarators(i, check_parameters))
413                         goto err;
414                 if (!forall_declarators(i, check_rettypes))
415                         goto err;
416                 if (!forall_declarators(i, check_arrays))
417                         goto err;
418
419                 if (!valid_declspecs(i, true))
420                         goto err;
421
422                 if (is_abstract(i->declarators) && (i != decl || i->next)) {
423                         fprintf(stderr, "mixing type names and declarations is not allowed\n");
424                         goto err;
425                 }
426         }
427
428         return decl;
429 err:
430         cdecl_free(decl);
431         return NULL;
432 }