#include <stdbool.h>
#include "scan.h"
-#include "cdecl.h"
#include "cdecl-internal.h"
#include "errmsg.h"
#define ALLOC_ITEM_DECLSPEC(ptr) ALLOC_ITEM(ptr, declspec, next)
#define ALLOC_ITEM_DECL(ptr) ALLOC_ITEM(ptr, decl, next)
-#define ALLOC_FUNCTION(ptr, parameters_, variadic_) do { \
+#define ALLOC_FUNCTION_(ptr, parameters_) do { \
ALLOC_ITEM_DECLARATOR(ptr); \
(ptr)->type = CDECL_DECL_FUNCTION; \
(ptr)->u.function.parameters = parameters_; \
+} while (0)
+
+#define ALLOC_FUNCTION(ptr, parameters_, variadic_) do { \
+ ALLOC_FUNCTION_(ptr, parameters_); \
(ptr)->u.function.variadic = variadic_; \
} while (0)
* name strings can be used directly in error messages and there is no
* need for any string processing.
*/
-#define yytnamerr(a, b) ( (a) ? yytnamerr_copy(a, b) \
- : strlen(b) )
-
-static size_t yytnamerr_copy(char *dst, const char *src)
-{
- return cdecl__strlcpy(dst, src, strlen(src)+1);
-}
+#define yytnamerr(a, b) ( (a) ? cdecl__strlcpy(a, b, -1) : strlen(b) )
%}
%code requires {
#include <inttypes.h>
#include <stdbool.h>
+#include "cdecl.h"
}
%code provides {
}
%union {
- uintmax_t uintval;
+ cdecl_uintmax uintval;
unsigned spectype;
bool boolval;
struct cdecl_declspec *declspec;
return b;
}
+/*
+ * Reverse the order of a "struct cdecl" list, and return the new first
+ * element of the list (i.e., the last element of the original list).
+ */
+static struct cdecl *reverse_decls(struct cdecl *decl)
+{
+ struct cdecl *prev, *next;
+
+ for (prev = NULL; decl; decl = next) {
+ next = decl->next;
+ decl->next = prev;
+ prev = decl;
+ }
+
+ return prev;
+}
+
/*
* Alter an abstract declarator (type name) to declare an identifier instead,
* used by the English parser rules to reduce "identifier as type" sequences.
*/
-static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
+static struct cdecl *
+insert_identifier(struct cdecl *decl, struct parse_item *ident)
{
struct cdecl_declarator *d, **p = &decl->declarators;
%destructor { free_decl($$); } <decl>
/* Magic tokens */
-%token T_LEX_ERROR
+%token T_LEX_ERROR "@@@"
%token <item> T_IDENT "identifier"
%token <uintval> T_UINT "integer constant"
semi: | T_SEMICOLON
declaration: declspecs declarators semi {
- $$ = $2;
+ $$ = reverse_decls($2);
$$->specifiers = $1;
};
$$->next = $1;
}
-declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
- $$ = $1;
- $$->next = $3;
+declarators: declarator_wrap | declarators T_COMMA declarator_wrap {
+ $$ = $3;
+ $$->next = $1;
}
declarator_wrap: declarator {
varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
-parameter_type_list: parameter varargs {
- ALLOC_FUNCTION($$, $1, $2);
-} | parameter T_COMMA parameter_type_list {
- $$ = $3;
- $1->next = $$->u.function.parameters;
- $$->u.function.parameters = $1;
+parameter_type_list: parameter {
+ ALLOC_FUNCTION_($$, $1);
+} | parameter_type_list T_COMMA parameter {
+ $$ = $1;
+ $3->next = $$->u.function.parameters;
+ $$->u.function.parameters = $3;
}
-parens: T_LPAREN parameter_type_list T_RPAREN {
+parens: T_LPAREN parameter_type_list varargs T_RPAREN {
+ $2->u.function.parameters = reverse_decls($2->u.function.parameters);
+ $2->u.function.variadic = $3;
$$ = $2;
} | T_LPAREN declarator_ish T_RPAREN {
struct cdecl *fake_params;
ALLOC_DECL(fake_params, NULL, $2);
- ALLOC_FUNCTION($$, fake_params, false);
+ ALLOC_FUNCTION_($$, fake_params);
}
pointer: T_ASTERISK qualifiers direct_declarator {
english_function: T_FUNCTION T_RETURNING {
ALLOC_FUNCTION($$, NULL, false);
-} | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
+} | T_FUNCTION T_LPAREN english_parameter_list varargs T_RPAREN T_RETURNING {
+ $3->u.function.parameters = reverse_decls($3->u.function.parameters);
+ $3->u.function.variadic = $4;
$$ = $3;
}
-english_parameter_list: english_parameter varargs {
- ALLOC_FUNCTION($$, $1, $2);
-} | english_parameter T_COMMA english_parameter_list {
- $$ = $3;
- $1->next = $$->u.function.parameters;
- $$->u.function.parameters = $1;
+english_parameter_list: english_parameter {
+ ALLOC_FUNCTION_($$, $1);
+} | english_parameter_list T_COMMA english_parameter {
+ $$ = $1;
+ $3->next = $$->u.function.parameters;
+ $$->u.function.parameters = $3;
}
typedef_name_qual: T_IDENT qualifiers {