let_callback: optional call variable list
[m6w6/ext-psi] / src / parser_proc_grammar.y
index f4c1bc0a3d7045abc53995affff4e851ea0202cd..570af95fd3f11ad362e234061be2449f522a9d1b 100644 (file)
@@ -8,7 +8,9 @@
 
 #include "plist.h"
 #include "parser.h"
+
 #define YYDEBUG 1
+#define PSI_PARSER_PROC_DEBUG 1
 
 static int psi_parser_proc_lex(YYSTYPE *u, struct psi_parser *P, struct psi_plist *tokens, size_t *index);
 static void psi_parser_proc_error(struct psi_parser *P, struct psi_plist *tokens, size_t *index, const char *msg);
@@ -37,23 +39,29 @@ static inline void psi_parser_proc_add_enum(struct psi_parser *P, struct psi_dec
        }
        P->enums = psi_plist_add(P->enums, &e);
 }
-static inline void psi_parser_proc_add_from_typedef(struct psi_parser *P, struct psi_decl_arg *def)
+static inline void psi_parser_proc_deanon_typedef(struct psi_decl_arg *def)
 {
-       if (def->type->real.def) {
-               switch (def->type->type) {
-               case PSI_T_STRUCT:
-                       psi_parser_proc_add_struct(P, def->type->real.strct);
-                       break;
-               case PSI_T_UNION:
-                       psi_parser_proc_add_union(P, def->type->real.unn);
-                       break;
-               case PSI_T_ENUM:
-                       psi_parser_proc_add_enum(P, def->type->real.enm);
-                       break;
-               default:
-                       break;
+       switch (def->type->type) {
+       case PSI_T_STRUCT:
+               if (!psi_decl_type_is_anon(def->type->name, "struct")) {
+                       return;
+               }
+               break;
+       case PSI_T_UNION:
+               if (!psi_decl_type_is_anon(def->type->name, "union")) {
+                       return;
                }
+               break;
+       case PSI_T_ENUM:
+               if (!psi_decl_type_is_anon(def->type->name, "enum")) {
+                       return;
+               }
+               break;
+       default:
+               return;
        }
+       free(def->type->name);
+       def->type->name = strdup(def->var->name);
 }
 static inline void psi_parser_proc_add_typedef(struct psi_parser *P, struct psi_decl_arg *def)
 {
@@ -61,8 +69,8 @@ static inline void psi_parser_proc_add_typedef(struct psi_parser *P, struct psi_
        if (!P->types) {
                P->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
        }
+       psi_parser_proc_deanon_typedef(def);
        P->types = psi_plist_add(P->types, &def);
-       psi_parser_proc_add_from_typedef(P, def);
 }
 static inline void psi_parser_proc_add_const(struct psi_parser *P, struct psi_const *cnst) {
        assert(cnst);
@@ -74,6 +82,12 @@ static inline void psi_parser_proc_add_const(struct psi_parser *P, struct psi_co
 }
 static inline void psi_parser_proc_add_decl(struct psi_parser *P, struct psi_decl *decl) {
        assert(decl);
+       
+       if (psi_decl_is_blacklisted(decl->func->var->name)) {
+               psi_decl_free(&decl);
+               return;
+       }
+
        if (!P->decls) {
                P->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
        }
@@ -94,6 +108,11 @@ static inline void psi_parser_proc_add_impl(struct psi_parser *P, struct psi_imp
 #include "plist.h"
 #include "types/layout.h"
 
+#define yytokentype psi_token_type
+
+#define PSI_T_CAST                     PSI_T_EQUALS
+#define PSI_T_POINTER          PSI_T_ASTERISK
+
 struct psi_parser;
 
 }
@@ -135,14 +154,6 @@ struct psi_parser;
 %token <struct psi_token *> CALLABLE
 %token <struct psi_token *> VOID
 %token <struct psi_token *> ZVAL
-%token <struct psi_token *> INT8
-%token <struct psi_token *> UINT8
-%token <struct psi_token *> INT16
-%token <struct psi_token *> UINT16
-%token <struct psi_token *> INT32
-%token <struct psi_token *> UINT32
-%token <struct psi_token *> INT64
-%token <struct psi_token *> UINT64
 %token <struct psi_token *> NULL
 %token <struct psi_token *> TRUE
 %token <struct psi_token *> FALSE
@@ -152,6 +163,8 @@ struct psi_parser;
 %token <struct psi_token *> NUMBER
 %token <struct psi_token *> QUOTED_STRING
 %token <struct psi_token *> QUOTED_CHAR
+%token <struct psi_token *> SIZEOF
+%token <struct psi_token *> VOLATILE
 
 %token <struct psi_token *> EOF 0              "end of file"
 %token <struct psi_token *> EOL                        "end of line"
@@ -192,7 +205,8 @@ struct psi_parser;
 %token <struct psi_token *> IIF                        "?"
 
 %token <struct psi_token *> PRAGMA
-%token <struct psi_token *> ONCE
+%token <struct psi_token *> PRAGMA_ONCE
+%token <struct psi_token *> LINE
 %token <struct psi_token *> ERROR
 %token <struct psi_token *> WARNING
 %token <struct psi_token *> IF
@@ -222,6 +236,7 @@ struct psi_parser;
 %token <struct psi_token *> TEMP
 %token <struct psi_token *> FREE
 %token <struct psi_token *> RETURN
+%token <struct psi_token *> AS
 %token <struct psi_token *> PRE_ASSERT
 %token <struct psi_token *> POST_ASSERT
 %token <struct psi_token *> BOOLVAL
@@ -246,6 +261,26 @@ struct psi_parser;
 %token <struct psi_token *> NO_WHITESPACE
 %token <struct psi_token *> CPP_HEADER
 %token <struct psi_token *> CPP_ATTRIBUTE
+%token <struct psi_token *> CPP_EXTENSION
+%token <struct psi_token *> CPP_PASTE
+%token <struct psi_token *> CPP_INLINE
+%token <struct psi_token *> CPP_RESTRICT
+%token <struct psi_token *> CPP_ASM
+
+/* virtual tokens */
+%token <struct psi_token *> BSLASH
+%token <struct psi_token *> LONG_DOUBLE
+%token <struct psi_token *> INT8
+%token <struct psi_token *> UINT8
+%token <struct psi_token *> INT16
+%token <struct psi_token *> UINT16
+%token <struct psi_token *> INT32
+%token <struct psi_token *> UINT32
+%token <struct psi_token *> INT64
+%token <struct psi_token *> UINT64
+%token <struct psi_token *> INT128
+%token <struct psi_token *> UINT128
+
 
 %precedence IIF COLON
 %precedence OR
@@ -265,8 +300,8 @@ struct psi_parser;
 %type          <struct psi_token *>                            lib optional_name enum_name struct_name union_name
 %destructor    {psi_token_free(&$$);}                          lib optional_name enum_name struct_name union_name
 
-%type          <struct psi_token *>                            cpp_message_token cpp_include_token cpp_header_token cpp_no_arg_token cpp_name_arg_token cpp_exp_arg_token
-%destructor    {}                                                                      cpp_message_token cpp_include_token cpp_header_token cpp_no_arg_token cpp_name_arg_token cpp_exp_arg_token
+%type          <struct psi_token *>                            cpp_message_token cpp_include_token cpp_header_token cpp_no_arg_token cpp_name_arg_token cpp_exp_arg_token cpp_special_name_token
+%destructor    {}                                                                      cpp_message_token cpp_include_token cpp_header_token cpp_no_arg_token cpp_name_arg_token cpp_exp_arg_token cpp_special_name_token
 
 %type          <struct psi_token *>                            name_token any_noeol_token binary_op_token unary_op_token
 %destructor    {}                                                                      name_token any_noeol_token binary_op_token unary_op_token
@@ -299,15 +334,12 @@ struct psi_parser;
 %type          <struct psi_token *>                            decl_real_type decl_int_type decl_type_simple
 %destructor    {psi_token_free(&$$);}                          decl_real_type decl_int_type decl_type_simple
 
-%type          <struct psi_token *>                            decl_stdint_type
-%destructor    {}                                                                      decl_stdint_type
-
-%type          <struct psi_decl_type *>                        decl_type const_decl_type decl_type_complex
-%destructor    {psi_decl_type_free(&$$);}                      decl_type const_decl_type decl_type_complex
-%type          <struct psi_decl *>                                     decl_stmt decl
-%destructor    {psi_decl_free(&$$);}                           decl_stmt decl
-%type          <struct psi_decl_arg *>                         decl_typedef decl_fn decl_func decl_functor decl_arg struct_arg typedef
-%destructor    {psi_decl_arg_free(&$$);}                       decl_typedef decl_fn decl_func decl_functor decl_arg struct_arg typedef
+%type          <struct psi_decl_type *>                        decl_type qualified_decl_type decl_type_complex
+%destructor    {psi_decl_type_free(&$$);}                      decl_type qualified_decl_type decl_type_complex
+%type          <struct psi_decl *>                                     decl_stmt decl decl_body decl_func_body decl_functor_body
+%destructor    {psi_decl_free(&$$);}                           decl_stmt decl decl_body decl_func_body decl_functor_body
+%type          <struct psi_decl_arg *>                         decl_typedef decl_func decl_functor decl_arg decl_anon_arg typedef typedef_decl typedef_anon typedef_anon_decl
+%destructor    {psi_decl_arg_free(&$$);}                       decl_typedef decl_func decl_functor decl_arg decl_anon_arg typedef typedef_decl typedef_anon typedef_anon_decl  
 %type          <struct psi_decl_var *>                         decl_var
 %destructor    {psi_decl_var_free(&$$);}                       decl_var
 %type          <struct psi_decl_struct *>                      decl_struct
@@ -318,14 +350,17 @@ struct psi_parser;
 %destructor    {psi_decl_enum_free(&$$);}                      decl_enum
 %type          <struct psi_decl_enum_item *>           decl_enum_item
 %destructor    {psi_decl_enum_item_free(&$$);}         decl_enum_item
-%type          <struct psi_plist *>                            decl_args decl_struct_args struct_args_block struct_args decl_enum_items decl_vars
-%destructor    {psi_plist_free($$);}                           decl_args decl_struct_args struct_args_block struct_args decl_enum_items decl_vars
+%type          <struct psi_plist *>                            decl_args decl_arg_list decl_struct_args struct_args_block struct_args struct_arg_var_list decl_enum_items decl_vars decl_vars_with_layout call_decl_vars
+%destructor    {psi_plist_free($$);}                           decl_args decl_arg_list decl_struct_args struct_args_block struct_args struct_arg_var_list decl_enum_items decl_vars decl_vars_with_layout call_decl_vars
 
 %type          <struct psi_layout>                                     align_and_size
 %destructor    {}                                                                      align_and_size
 %type          <struct psi_layout *>                           decl_layout
 %destructor    {psi_layout_free(&$$);}                         decl_layout
 
+%type          <struct psi_number *>                           sizeof sizeof_body sizeof_body_notypes
+%destructor    {psi_number_free(&$$);}                         sizeof sizeof_body sizeof_body_notypes
+
 %type          <struct psi_impl *>                                     impl
 %destructor    {psi_impl_free(&$$);}                           impl
 %type          <struct psi_impl_func *>                        impl_func
@@ -357,6 +392,8 @@ struct psi_parser;
 %destructor    {psi_assert_stmt_free(&$$);}            assert_stmt
 %type          <struct psi_return_stmt *>                      return_stmt
 %destructor    {psi_return_stmt_free(&$$);}            return_stmt
+%type          <struct psi_return_exp *>                       return_exp
+%destructor    {psi_return_exp_free(&$$);}                     return_exp
 %type          <struct psi_free_stmt *>                        free_stmt
 %destructor    {psi_free_stmt_free(&$$);}                      free_stmt
 %type          <struct psi_free_exp *>                         free_exp
@@ -374,13 +411,11 @@ struct psi_parser;
 %type          <struct psi_number *>                           number
 %destructor    {psi_number_free(&$$);}                         number
 
-%type          <size_t>                                                        indirection pointers array_size
-%destructor    {}                                                                      indirection pointers array_size
+%type          <size_t>                                                        indirection pointers asterisks array_size
+%destructor    {}                                                                      indirection pointers asterisks array_size
 %type          <bool>                                                          reference
 %destructor    {}                                                                      reference
 
-//%destructor {}                                                                       file blocks block
-
 %%
 
 /* rules */
@@ -388,8 +423,9 @@ struct psi_parser;
 
 binary_op_token: PIPE | CARET | AMPERSAND | LSHIFT | RSHIFT | PLUS | MINUS | ASTERISK | SLASH | MODULO | RCHEVR | LCHEVR | CMP_GE | CMP_LE | OR | AND | CMP_EQ | CMP_NE ; 
 unary_op_token: TILDE | NOT | PLUS | MINUS ;
-name_token: NAME | TEMP | FREE | SET | LET | CALLOC | CALLBACK | ZVAL | LIB | STRING | COUNT | ERROR | WARNING | ONCE | PRAGMA | BOOL ;
-any_noeol_token: BOOL | CHAR | SHORT | INT | SIGNED | UNSIGNED | LONG | FLOAT | DOUBLE | STRING | MIXED | ARRAY | OBJECT | CALLABLE | VOID | ZVAL | INT8 | UINT8 | INT16 | UINT16 | INT32 | UINT32 | INT64 | UINT64 | NULL | TRUE | FALSE | NAME | NSNAME | DOLLAR_NAME | NUMBER | QUOTED_STRING | QUOTED_CHAR | EOF | EOS | LPAREN | RPAREN | COMMA | COLON | LBRACE | RBRACE | LBRACKET | RBRACKET | EQUALS | HASH | PIPE | CARET | AMPERSAND | LSHIFT | RSHIFT | PLUS | MINUS | ASTERISK | SLASH | MODULO | LCHEVR | RCHEVR | CMP_GE | CMP_LE | OR | AND | CMP_EQ | CMP_NE | TILDE | NOT | PERIOD | BACKSLASH | ELLIPSIS | ERROR | WARNING | IIF | IF | IFDEF | IFNDEF | ELSE | ELIF | ENDIF | DEFINE | DEFINED | UNDEF | INCLUDE | TYPEDEF | STRUCT | UNION | ENUM | CONST | LIB | STATIC | CALLBACK | FUNCTION | LET | SET | TEMP | FREE | RETURN | PRE_ASSERT | POST_ASSERT | BOOLVAL | INTVAL | STRVAL | PATHVAL | STRLEN | FLOATVAL | ARRVAL | OBJVAL | COUNT | CALLOC | TO_BOOL | TO_INT | TO_STRING | TO_FLOAT | TO_ARRAY | TO_OBJECT | COMMENT | CPP_HEADER;
+name_token: NAME | FUNCTION | TEMP | FREE | SET | LET | CALLOC | CALLBACK | LIB | BOOL | STRING | ERROR | WARNING | LINE | PRAGMA_ONCE | PRAGMA | AS | let_func_token | set_func_token;
+any_noeol_token: BOOL | CHAR | SHORT | INT | SIGNED | UNSIGNED | LONG | FLOAT | DOUBLE | STRING | MIXED | ARRAY | OBJECT | CALLABLE | VOID | ZVAL | NULL | TRUE | FALSE | NAME | NSNAME | DOLLAR_NAME | NUMBER | QUOTED_STRING | QUOTED_CHAR | EOF | EOS | LPAREN | RPAREN | COMMA | COLON | LBRACE | RBRACE | LBRACKET | RBRACKET | EQUALS | HASH | PIPE | CARET | AMPERSAND | LSHIFT | RSHIFT | PLUS | MINUS | ASTERISK | SLASH | MODULO | LCHEVR | RCHEVR | CMP_GE | CMP_LE | OR | AND | CMP_EQ | CMP_NE | TILDE | NOT | PERIOD | BACKSLASH | ELLIPSIS | ERROR | WARNING | LINE | PRAGMA | PRAGMA_ONCE | IIF | IF | IFDEF | IFNDEF | ELSE | ELIF | ENDIF | DEFINE | DEFINED | UNDEF | INCLUDE | TYPEDEF | STRUCT | UNION | ENUM | CONST | LIB | STATIC | CALLBACK | FUNCTION | LET | SET | TEMP | FREE | RETURN | PRE_ASSERT | POST_ASSERT | BOOLVAL | INTVAL | STRVAL | PATHVAL | STRLEN | FLOATVAL | ARRVAL | OBJVAL | COUNT | CALLOC | TO_BOOL | TO_INT | TO_STRING | TO_FLOAT | TO_ARRAY | TO_OBJECT | COMMENT | CPP_HEADER | CPP_PASTE | CPP_INLINE | CPP_RESTRICT | CPP_EXTENSION | CPP_ASM | SIZEOF | VOLATILE | AS;
+any_nobrace_token: BOOL | CHAR | SHORT | INT | SIGNED | UNSIGNED | LONG | FLOAT | DOUBLE | STRING | MIXED | ARRAY | OBJECT | CALLABLE | VOID | ZVAL | NULL | TRUE | FALSE | NAME | NSNAME | DOLLAR_NAME | NUMBER | QUOTED_STRING | QUOTED_CHAR | EOF | EOS | LPAREN | RPAREN | COMMA | COLON | LBRACKET | RBRACKET | EQUALS | HASH | PIPE | CARET | AMPERSAND | LSHIFT | RSHIFT | PLUS | MINUS | ASTERISK | SLASH | MODULO | LCHEVR | RCHEVR | CMP_GE | CMP_LE | OR | AND | CMP_EQ | CMP_NE | TILDE | NOT | PERIOD | BACKSLASH | ELLIPSIS | ERROR | WARNING | LINE | PRAGMA | PRAGMA_ONCE | IIF | IF | IFDEF | IFNDEF | ELSE | ELIF | ENDIF | DEFINE | DEFINED | UNDEF | INCLUDE | TYPEDEF | STRUCT | UNION | ENUM | CONST | LIB | STATIC | CALLBACK | FUNCTION | LET | SET | TEMP | FREE | RETURN | PRE_ASSERT | POST_ASSERT | BOOLVAL | INTVAL | STRVAL | PATHVAL | STRLEN | FLOATVAL | ARRVAL | OBJVAL | COUNT | CALLOC | TO_BOOL | TO_INT | TO_STRING | TO_FLOAT | TO_ARRAY | TO_OBJECT | COMMENT | CPP_HEADER | CPP_PASTE | CPP_INLINE | CPP_RESTRICT | CPP_EXTENSION | CPP_ASM | SIZEOF | VOLATILE | AS;
 
 
 file:
@@ -413,9 +449,9 @@ block:
 |      lib {
        if (P->file.ln) {
                P->error(PSI_DATA(P), $lib, PSI_WARNING,
-                               "Extra 'lib %s' statement has no effect", $lib->text);
+                               "Extra 'lib \"%s\"' statement has no effect", $lib->text);
        } else {
-               P->file.ln = strndup($lib->text + 1, $lib->size - 2);
+               P->file.ln = strndup($lib->text, $lib->size);
        }
 }
 |      constant {
@@ -424,6 +460,8 @@ block:
 |      decl_stmt {
        psi_parser_proc_add_decl(P, $decl_stmt);
 }
+|      ignored_decl
+|      decl_ext_var_stmt
 |      decl_typedef[def] {
        psi_parser_proc_add_typedef(P, $def);
 }
@@ -448,7 +486,10 @@ lib:
 ;
 
 cpp:
-       HASH cpp_exp[exp] EOL {
+       HASH EOL {
+       $cpp = NULL;
+}
+|      HASH cpp_exp[exp] EOL {
        $cpp = $exp;
 }
 ;
@@ -485,12 +526,7 @@ cpp_exp[exp]:
        $exp = psi_cpp_exp_init($cpp_no_arg_token->type, NULL);
        $exp->token = psi_token_copy($cpp_no_arg_token);
 }
-|      cpp_name_arg_token name_token {
-       $name_token->type = PSI_T_NAME;
-       $exp = psi_cpp_exp_init($cpp_name_arg_token->type, psi_token_copy($name_token));
-       $exp->token = psi_token_copy($cpp_name_arg_token);
-}
-|      cpp_name_arg_token NULL[name_token] {
+|      cpp_name_arg_token cpp_special_name_token[name_token] {
        $name_token->type = PSI_T_NAME;
        $exp = psi_cpp_exp_init($cpp_name_arg_token->type, psi_token_copy($name_token));
        $exp->token = psi_token_copy($cpp_name_arg_token);
@@ -503,16 +539,21 @@ cpp_exp[exp]:
        $exp = psi_cpp_exp_init($cpp_exp_arg_token->type, $cpp_macro_exp);
        $exp->token = psi_token_copy($cpp_exp_arg_token);
 }
-|      PRAGMA ONCE {
-       $exp = psi_cpp_exp_init($ONCE->type, NULL);
-       $exp->token = psi_token_copy($ONCE);
+|      PRAGMA_ONCE {
+       $exp = psi_cpp_exp_init($PRAGMA_ONCE->type, NULL);
+       $exp->token = psi_token_copy($PRAGMA_ONCE);
 }
-|      PRAGMA cpp_macro_decl_tokens[tokens] {
+|      cpp_ignored_token cpp_macro_decl_tokens[tokens] {
        psi_plist_free($tokens);
        $exp = NULL;
 }
 ;
 
+cpp_ignored_token:
+       LINE
+|      PRAGMA
+;
+
 cpp_message_token: 
        ERROR
 |      WARNING
@@ -545,18 +586,23 @@ cpp_exp_arg_token:
 |      ELIF
 ;
 
+cpp_special_name_token:
+       name_token
+|      NULL
+|      TRUE
+|      FALSE
+|      CPP_RESTRICT
+|      CPP_EXTENSION
+|      CPP_INLINE
+;
+
 cpp_macro_decl[macro]:
        name_token NO_WHITESPACE LPAREN cpp_macro_sig RPAREN cpp_macro_decl_tokens {
        $name_token->type = PSI_T_NAME;
        $macro = psi_cpp_macro_decl_init($cpp_macro_sig, $cpp_macro_decl_tokens, NULL);
        $macro->token = psi_token_copy($name_token);
 }
-|      name_token cpp_macro_decl_tokens {
-       $name_token->type = PSI_T_NAME;
-       $macro = psi_cpp_macro_decl_init(NULL, $cpp_macro_decl_tokens, NULL);
-       $macro->token = psi_token_copy($name_token);
-}
-|      NULL[name_token] cpp_macro_decl_tokens {
+|      cpp_special_name_token[name_token] cpp_macro_decl_tokens {
        $name_token->type = PSI_T_NAME;
        $macro = psi_cpp_macro_decl_init(NULL, $cpp_macro_decl_tokens, NULL);
        $macro->token = psi_token_copy($name_token);
@@ -567,7 +613,13 @@ cpp_macro_sig[sig]:
        %empty {
        $sig = psi_plist_init(NULL);
 }
+|      ELLIPSIS {
+       $sig = psi_plist_init(NULL); /* FIXME */
+}
 |      cpp_macro_sig_args
+|      cpp_macro_sig_args[args] COMMA ELLIPSIS {
+       $sig = $args;
+}
 ;
 
 cpp_macro_sig_args[args]:
@@ -649,6 +701,10 @@ cpp_macro_exp[exp]:
        $exp->token = psi_token_copy($QUOTED_CHAR);
        $exp->data.n->token = psi_token_copy($QUOTED_CHAR);
 }
+|      sizeof {
+       $exp = psi_num_exp_init_num($sizeof);
+       $exp->token = psi_token_copy($sizeof->token);
+}
 |      name_token {
        $name_token->type = PSI_T_NAME;
        $exp = psi_num_exp_init_num(psi_number_init(PSI_T_DEFINE, $name_token->text, 0));
@@ -672,7 +728,7 @@ cpp_macro_call_args[args]:
 
 cpp_macro_call_arg_list[args]:
        cpp_macro_exp {
-       $args = psi_plist_add(psi_plist_init((void (*)(void *)) psi_num_exp_free), 
+       $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_num_exp_free), 
                &$cpp_macro_exp);
 }
 |      cpp_macro_call_arg_list[args_] COMMA cpp_macro_exp {
@@ -704,7 +760,38 @@ impl_def_val[val]:
        %empty {
        $val = NULL;
 }
-|      impl_def_val_token[token] {
+|      num_exp[num] %dprec 1 {
+       if (psi_num_exp_validate(PSI_DATA(P), $num, NULL, NULL, NULL, NULL, NULL)) {
+               impl_val res = {0};
+               token_t type = psi_num_exp_exec($num, &res, NULL, &P->preproc->defs);
+               
+               if (type == PSI_T_FLOAT || type == PSI_T_DOUBLE) {
+                       $val = psi_impl_def_val_init(type, NULL);
+               } else {
+                       $val = psi_impl_def_val_init(PSI_T_INT, NULL);
+               }
+               
+               switch (type) {
+               case PSI_T_UINT8:       $val->ival.zend.lval = res.u8;  break;
+               case PSI_T_UINT16:      $val->ival.zend.lval = res.u16; break;
+               case PSI_T_UINT32:      $val->ival.zend.lval = res.u32; break;
+               case PSI_T_UINT64:      $val->ival.zend.lval = res.u64; break; /* FIXME */
+               case PSI_T_INT8:        $val->ival.zend.lval = res.i8;  break;
+               case PSI_T_INT16:       $val->ival.zend.lval = res.i16; break;
+               case PSI_T_INT32:       $val->ival.zend.lval = res.i32; break;
+               case PSI_T_INT64:       $val->ival.zend.lval = res.i64; break;
+               case PSI_T_FLOAT:       $val->ival.dval = res.fval;             break;
+               case PSI_T_DOUBLE:      $val->ival.dval = res.dval;             break;
+               default:
+                       assert(0);
+               
+               }
+       } else {
+               $val = NULL;
+       }
+       psi_num_exp_free(&$num);
+}
+|      impl_def_val_token[token] %dprec 2 {
        $val = psi_impl_def_val_init($token->type, $token->text);
        $val->token = psi_token_copy($token);
 }
@@ -712,7 +799,6 @@ impl_def_val[val]:
 
 impl_def_val_token:
        NULL
-|      NUMBER
 |      TRUE
 |      FALSE
 |      QUOTED_STRING
@@ -722,11 +808,41 @@ decl_typedef[def]:
        TYPEDEF typedef[def_] EOS {
        $def = $def_;
 }
+|      TYPEDEF VOID name_token EOS {
+       $def = psi_decl_arg_init(
+               psi_decl_type_init(PSI_T_VOID, $VOID->text),
+               psi_decl_var_init($name_token->text, 0, 0)
+       );
+       $def->token = psi_token_copy($VOID);
+       $def->type->token = psi_token_copy($VOID);
+       $def->var->token = psi_token_copy($name_token);
+}
+|      CPP_EXTENSION TYPEDEF typedef[def_] EOS {
+       $def = $def_;
+}
 ;
 
 typedef[def]:
+       typedef_decl {
+       $def = $typedef_decl;
+}
+|      CPP_EXTENSION typedef_decl {
+       $def = $typedef_decl;
+}
+;
+
+typedef_anon[def]:
+       typedef_anon_decl {
+       $def = $typedef_anon_decl;
+}
+|      CPP_EXTENSION typedef_anon_decl {
+       $def = $typedef_anon_decl;
+}
+;
+
+typedef_decl[def]:
        decl_arg
-|      decl {
+|      decl_func_body[decl] {
        $def = psi_decl_arg_init(
                psi_decl_type_init(PSI_T_FUNCTION, $decl->func->var->name),
                psi_decl_var_copy($decl->func->var)
@@ -743,6 +859,7 @@ typedef[def]:
        $def->var->token = psi_token_copy($name_token);
        $def->type->token = psi_token_copy($enum->token);
        $def->type->real.enm = $enum;
+       psi_parser_proc_add_enum(P, $enum);
 }
 |      struct_name[struct] align_and_size[as] struct_args_block[args] decl_var[var] {
        $def = psi_decl_arg_init(psi_decl_type_init(PSI_T_STRUCT, $struct->text), $var);
@@ -751,6 +868,7 @@ typedef[def]:
        $def->type->real.strct->token = psi_token_copy($struct);
        $def->type->real.strct->align = $as.pos;
        $def->type->real.strct->size = $as.len;
+       psi_parser_proc_add_struct(P, $def->type->real.strct);
 }
 |      union_name[union] align_and_size[as] struct_args_block[args] decl_var[var] {
        $def = psi_decl_arg_init(psi_decl_type_init(PSI_T_UNION, $union->text), $var);
@@ -759,18 +877,53 @@ typedef[def]:
        $def->type->real.unn->token = psi_token_copy($union);
        $def->type->real.unn->align = $as.pos;
        $def->type->real.unn->size = $as.len;
+       psi_parser_proc_add_union(P, $def->type->real.unn);
+}
+;
+
+typedef_anon_decl[def]:
+       typedef_decl %dprec 2
+|      qualified_decl_type[type] %dprec 1 {
+       $def = psi_decl_arg_init($type, psi_decl_var_init(NULL, 0, 0));
+       $def->var->token = psi_token_copy($type->token);
+}
+|      decl_enum[enum] {
+       $def = psi_decl_arg_init(
+               psi_decl_type_init(PSI_T_ENUM, $enum->name),
+               psi_decl_var_init(NULL, 0, 0)
+       );
+       $def->var->token = psi_token_copy($enum->token);
+       $def->type->token = psi_token_copy($enum->token);
+       $def->type->real.enm = $enum;
+       psi_parser_proc_add_enum(P, $enum);
+}
+|      struct_name[struct] align_and_size[as] struct_args_block[args] {
+       $def = psi_decl_arg_init(psi_decl_type_init(PSI_T_STRUCT, $struct->text), psi_decl_var_init(NULL, 0, 0));
+       $def->type->token = $struct;
+       $def->type->real.strct = psi_decl_struct_init($struct->text, $args);
+       $def->type->real.strct->token = psi_token_copy($struct);
+       $def->type->real.strct->align = $as.pos;
+       $def->type->real.strct->size = $as.len;
+       psi_parser_proc_add_struct(P, $def->type->real.strct);
 }
-|      const_decl_type[type] decl_stdint_type[stdint] {
-       $stdint->type = PSI_T_NAME;
-       $def = psi_decl_arg_init($type, psi_decl_var_init($stdint->text, 0, 0));
-       $def->var->token = psi_token_copy($stdint);
+|      union_name[union] align_and_size[as] struct_args_block[args] {
+       $def = psi_decl_arg_init(psi_decl_type_init(PSI_T_UNION, $union->text), psi_decl_var_init(NULL, 0, 0));
+       $def->type->token = $union;
+       $def->type->real.unn = psi_decl_union_init($union->text, $args);
+       $def->type->real.unn->token = psi_token_copy($union);
+       $def->type->real.unn->align = $as.pos;
+       $def->type->real.unn->size = $as.len;
+       psi_parser_proc_add_union(P, $def->type->real.unn);
 }
 ;
 
-const_decl_type[type]:
+qualified_decl_type[type]:
        CONST decl_type[type_] {
        $type = $type_;
 }
+|      VOLATILE decl_type[type_] {
+       $type = $type_;
+}
 |      decl_type
 ;
 
@@ -803,9 +956,6 @@ decl_type_complex[type]:
 decl_type_simple[type]:
        decl_int_type
 |      decl_real_type
-|      decl_stdint_type[type_] {
-       $type = psi_token_copy($type_);
-}
 |      NAME[type_] {
        $type = psi_token_copy($type_);
 }
@@ -823,17 +973,6 @@ decl_real_type[type]:
 }
 ;
 
-decl_stdint_type[type]:
-       INT8
-|      UINT8
-|      INT16
-|      UINT16
-|      INT32
-|      UINT32
-|      INT64
-|      UINT64
-;
-
 int_signed[i]:
        SIGNED
 |      UNSIGNED
@@ -881,6 +1020,9 @@ int_signed_types[type]:
        %empty {
        $type = NULL;
 }
+|      NAME {
+       $type = psi_token_copy($NAME);
+}
 |      CHAR {
        $type = psi_token_copy($CHAR);
 }
@@ -915,6 +1057,7 @@ signed_long_types[type]:
 }
 |      INT
 |      LONG
+|      LONG INT
 ;
 
 int_width_types[type]:
@@ -935,36 +1078,117 @@ int_width_types[type]:
 ;
 
 decl_stmt:
-       decl EOS {
+       decl decl_asm EOS {
        $decl_stmt = $decl;
 }
+|      CPP_EXTENSION decl decl_asm EOS {
+       $decl_stmt = $decl;
+}
+;
+
+decl_asm:
+       %empty
+|      CPP_ASM LPAREN ignored_quoted_strings RPAREN
+;
+
+ignored_quoted_strings:
+       QUOTED_STRING
+|      ignored_quoted_strings QUOTED_STRING
+;
+
+decl_ext_var_stmt:
+       decl_ext_var EOS
+;
+
+decl_ext_var: 
+       NAME decl_arg decl_ext_var_list {
+       psi_decl_arg_free(&$decl_arg);
+}
+;
+
+decl_ext_var_list:
+       %empty
+|      COMMA decl_vars {
+       psi_plist_free($decl_vars);
+}
+;
+
+decl_vars[vars]:
+       decl_var[var] {
+       $vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_var_free), &$var);
+}
+|      decl_vars[vars_] COMMA decl_var[var] {
+       $vars = psi_plist_add($vars_, &$var);
+}
+;
+
+ignored_decl:
+       STATIC decl_body ignored_decl_body {
+       psi_decl_free(&$decl_body);
+}
+|      CPP_INLINE decl_body ignored_decl_body {
+       psi_decl_free(&$decl_body);
+}
+|      STATIC CPP_INLINE decl_body ignored_decl_body {
+       psi_decl_free(&$decl_body);
+}
+;
+
+ignored_decl_body:
+       LBRACE ignored_decl_body_stmts RBRACE
+;
+
+ignored_decl_body_stmts:
+       ignored_decl_body_stmt
+|      ignored_decl_body_stmts ignored_decl_body_stmt
+;
+
+ignored_decl_body_stmt:
+       any_nobrace_token
+|      ignored_decl_body
 ;
 
 decl:
-       decl_fn[func] LPAREN decl_args[args] RPAREN array_size[as] {
-       $decl = psi_decl_init(psi_decl_abi_init("default"), $func, $args);
+       decl_body
+|      NAME[abi] decl_body {
+       $decl = $decl_body;
+       $decl->abi = psi_decl_abi_init($abi->text);
+}
+;
+
+decl_body:
+       decl_func_body
+|      decl_functor_body
+;
+
+decl_func_body[decl]:
+       decl_func[func] LPAREN decl_args[args] RPAREN array_size[as] {
+       $decl = psi_decl_init($func, $args);
        if ($as) {
                $decl->func->var->pointer_level += 1;
                $decl->func->var->array_size = $as;
        }
 }
-|      decl_fn[func] LPAREN decl_args[args] COMMA ELLIPSIS RPAREN array_size[as] {
-       $decl = psi_decl_init(psi_decl_abi_init("default"), $func, $args);
+|      decl_func[func] LPAREN decl_args[args] COMMA ELLIPSIS RPAREN array_size[as] {
+       $decl = psi_decl_init($func, $args);
        $decl->varargs = 1;
        if ($as) {
                $decl->func->var->pointer_level += 1;
                $decl->func->var->array_size = $as;
        }
 }
-|      NAME[abi] decl_fn[func] LPAREN decl_args[args] RPAREN array_size[as] {
-       $decl = psi_decl_init(psi_decl_abi_init($abi->text), $func, $args);
+;
+
+decl_functor_body[decl]:
+       decl_functor[func] LPAREN decl_args[args] RPAREN array_size[as] {
+       $decl = psi_decl_init($func, $args);
        if ($as) {
                $decl->func->var->pointer_level += 1;
                $decl->func->var->array_size = $as;
        }
 }
-|      NAME[abi] decl_fn[func] LPAREN decl_args[args] COMMA ELLIPSIS RPAREN array_size[as] {
-       $decl = psi_decl_init(psi_decl_abi_init($abi->text), $func, $args);
+|      decl_functor[func] LPAREN decl_args[args] COMMA ELLIPSIS RPAREN array_size[as] {
+       $decl = psi_decl_init($func, $args);
        $decl->varargs = 1;
        if ($as) {
                $decl->func->var->pointer_level += 1;
@@ -973,19 +1197,16 @@ decl:
 }
 ;
 
-decl_fn:
-       decl_func
-|      decl_functor
-;
-
 decl_functor[arg]:
-       const_decl_type[type] indirection[i] LPAREN indirection name_token[NAME] RPAREN {
+       qualified_decl_type[type] indirection[i] LPAREN indirection[unused1] name_token[NAME] RPAREN {
+       (void) $unused1;
        $NAME->type = PSI_T_NAME;
        $arg = psi_decl_arg_init($type, psi_decl_var_init($NAME->text, $i, 0));
        $arg->var->token = psi_token_copy($NAME);
        $arg->token = psi_token_copy($NAME);
 }
-|      CONST VOID pointers LPAREN indirection name_token[NAME] RPAREN {
+|      CONST VOID pointers LPAREN indirection[unused1] name_token[NAME] RPAREN {
+       (void) $unused1;
        $NAME->type = PSI_T_NAME;
        $arg = psi_decl_arg_init(
                psi_decl_type_init($VOID->type, $VOID->text),
@@ -995,7 +1216,8 @@ decl_functor[arg]:
        $arg->var->token = psi_token_copy($NAME);
        $arg->token = psi_token_copy($NAME);
 }
-|      VOID pointers LPAREN indirection name_token[NAME] RPAREN {
+|      VOID pointers LPAREN indirection[unused1] name_token[NAME] RPAREN {
+       (void) $unused1;
        $NAME->type = PSI_T_NAME;
        $arg = psi_decl_arg_init(
                psi_decl_type_init($VOID->type, $VOID->text),
@@ -1005,7 +1227,8 @@ decl_functor[arg]:
        $arg->var->token = psi_token_copy($NAME);
        $arg->token = psi_token_copy($NAME);
 }
-|      VOID LPAREN indirection name_token[NAME] RPAREN {
+|      VOID LPAREN indirection[unused1] name_token[NAME] RPAREN {
+       (void) $unused1;
        $NAME->type = PSI_T_NAME;
        $arg = psi_decl_arg_init(
                psi_decl_type_init($VOID->type, $VOID->text),
@@ -1037,16 +1260,61 @@ decl_args[args]:
 |      VOID {
        $args = NULL;
 }
-|      decl_arg[arg] {
+|      decl_arg_list[args_] {
+       $args = $args_;
+}
+;
+
+decl_arg_list[args]:
+       decl_anon_arg[arg] {
        $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free), &$arg);
 }
-|      decl_args[args_] COMMA decl_arg[arg] {
+|      decl_arg_list[args_] COMMA decl_anon_arg[arg] {
        $args = psi_plist_add($args_, &$arg);
 }
 ;
 
+decl_anon_arg[arg]:
+/* FIXME decl_functor_body_anon */
+       decl_arg %dprec 2 {
+       $arg = $decl_arg;
+}
+|      qualified_decl_type[type] indirection %dprec 1 {
+       $arg = psi_decl_arg_init(
+               $type, 
+               psi_decl_var_init(NULL, $indirection, 0)
+       );
+}
+|      CONST VOID pointers {
+       $arg = psi_decl_arg_init(
+               psi_decl_type_init($VOID->type, $VOID->text),
+               psi_decl_var_init(NULL, $pointers, 0)
+       );
+       $arg->type->token = psi_token_copy($VOID);
+       $arg->var->token = psi_token_copy($VOID);
+       $arg->token = psi_token_copy($VOID);
+}
+|      VOID pointers {
+       $arg = psi_decl_arg_init(
+               psi_decl_type_init($VOID->type, $VOID->text),
+               psi_decl_var_init(NULL, $pointers, 0)
+       );
+       $arg->type->token = psi_token_copy($VOID);
+       $arg->var->token = psi_token_copy($VOID);
+       $arg->token = psi_token_copy($VOID);
+}
+;
+
 decl_arg[arg]:
-       const_decl_type[type] decl_var[var] {
+       decl_functor_body[decl] {
+       $arg = psi_decl_arg_init(
+               psi_decl_type_init(PSI_T_FUNCTION, $decl->func->var->name),
+               psi_decl_var_copy($decl->func->var)
+       );
+       $arg->type->token = psi_token_copy($decl->func->token);
+       $arg->type->real.func = $decl;
+}
+|      qualified_decl_type[type] decl_var[var] {
        $arg = psi_decl_arg_init($type, $var);
 }
 |      CONST VOID pointers name_token[NAME] {
@@ -1118,24 +1386,64 @@ struct_args_block[args]:
 ;
 
 struct_args[args]:
-       struct_arg[arg] {
+       typedef_anon[arg] decl_layout[layout] struct_arg_var_list[vars] EOS {
+       $arg->layout = $layout;
        $args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free), &$arg);
+       if ($vars) {
+               size_t i = 0;
+               struct psi_decl_arg *arg;
+               
+               while (psi_plist_get($vars, i++, &arg)) {
+                       arg->type = psi_decl_type_copy($arg->type);
+                       $args = psi_plist_add($args, &arg);
+               }
+               free($vars);
+       }
 }
-|      struct_args[args_] struct_arg[arg] {
+|      struct_args[args_] typedef_anon[arg] decl_layout[layout] struct_arg_var_list[vars] EOS {
+       $arg->layout = $layout;
        $args = psi_plist_add($args_, &$arg);
+       if ($vars) {
+               size_t i = 0;
+               struct psi_decl_arg *arg;
+               
+               while (psi_plist_get($vars, i++, &arg)) {
+                       arg->type = psi_decl_type_copy($arg->type);
+                       $args = psi_plist_add($args, &arg);
+               }
+               free($vars);
+       }
 }
 ;
 
-struct_arg[arg]:
-       typedef[arg_] decl_layout[layout] EOS {
-       $arg = $arg_;
-       $arg->layout = $layout;
-       psi_parser_proc_add_from_typedef(P, $arg);
+struct_arg_var_list[vars]:
+       %empty {
+       $vars = NULL;
+}
+|      COMMA decl_vars_with_layout[vars_] {
+       $vars = $vars_;
+}
+;
+
+decl_vars_with_layout[vars]:
+       decl_var[var] decl_layout[layout] {
+       {
+               struct psi_decl_arg *arg = psi_decl_arg_init(NULL, $var);
+               arg->layout = $layout;
+               $vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free), &arg);
+       }
+}
+|      decl_vars_with_layout[vars_] COMMA decl_var[var] decl_layout[layout] {
+       {
+               struct psi_decl_arg *arg = psi_decl_arg_init(NULL, $var);
+               arg->layout = $layout;
+               $vars = psi_plist_add($vars_, &arg);
+       }
 }
 ;
 
 decl_enum[enum]:
-       enum_name LBRACE decl_enum_items[list] RBRACE {
+       enum_name LBRACE decl_enum_items[list] optional_comma RBRACE {
        $enum = psi_decl_enum_init($enum_name->text, $list);
        $enum->token = $enum_name;
 }
@@ -1168,7 +1476,7 @@ num_exp[exp]:
        $exp = psi_num_exp_init_num($number);
        $exp->token = psi_token_copy($number->token);
 }
-|      LPAREN const_decl_type[type] RPAREN num_exp[exp_] %prec UNARY {
+|      LPAREN qualified_decl_type[type] RPAREN num_exp[exp_] %prec UNARY {
        $exp = psi_num_exp_init_cast($type, $exp_);
        $exp->token = psi_token_copy($type->token);
 }
@@ -1203,10 +1511,57 @@ number[num]:
        $num = psi_number_init($token->type, $token->text, 0);
        $num->token = psi_token_copy($token);
 }
+|      NULL[token] {
+       $num = psi_number_init($token->type, $token->text, 0);
+       $num->token = psi_token_copy($token);
+}
 |      decl_var {
        $num = psi_number_init(PSI_T_NAME, $decl_var, 0);
        $num->token = psi_token_copy($decl_var->token);
 }
+|      sizeof {
+       $num = $sizeof;
+}
+;
+
+sizeof:
+       SIZEOF LPAREN sizeof_body[sizeof_] RPAREN {
+       $sizeof = $sizeof_;
+       $sizeof->token = psi_token_copy($SIZEOF);
+}
+|      SIZEOF sizeof_body_notypes[sizeof_] {
+       $sizeof = $sizeof_;
+       $sizeof->token = psi_token_copy($SIZEOF);
+}
+;
+
+sizeof_body[sizeof]:
+       sizeof_body_notypes[sizeof_] {
+       $sizeof = $sizeof_;
+}
+|      decl_type indirection {
+       if ($indirection) {
+               int8_t sizeof_void_p = sizeof(void *);
+               $sizeof = psi_number_init(PSI_T_INT8, &sizeof_void_p, 0);
+               psi_decl_type_free(&$decl_type);
+       } else {
+               $sizeof = psi_number_init(PSI_T_SIZEOF, $decl_type, 0);
+       }
+}
+;
+
+sizeof_body_notypes[sizeof]:
+       AMPERSAND NAME {
+       int8_t sizeof_void_p = sizeof(void *);
+       $sizeof = psi_number_init(PSI_T_INT8, &sizeof_void_p, 0);
+}
+|      QUOTED_CHAR {
+       int8_t sizeof_a = sizeof('a');
+       $sizeof = psi_number_init(PSI_T_INT8, &sizeof_a, 0);
+}
+|      QUOTED_STRING {
+       $sizeof = psi_number_init(PSI_T_INT64, &$QUOTED_STRING->size, 0);
+}
 ;
 
 enum_name[name]:
@@ -1258,12 +1613,23 @@ optional_name[name]:
 }
 ;
 
+optional_comma:
+       %empty
+|      COMMA
+;
+
 decl_layout[l]:
        %empty {
        $l = NULL;
 }
+|      COLON NUMBER[width] {
+       $l = psi_layout_init(0, 0, psi_layout_init(0, atol($width->text), NULL));
+}
 |      COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN {
-       $l = psi_layout_init(atol($align->text), atol($size->text));
+       $l = psi_layout_init(atol($align->text), atol($size->text), NULL);
+}
+|      COLON NUMBER[width] COLON COLON LPAREN NUMBER[align] COMMA NUMBER[size] RPAREN {
+       $l = psi_layout_init(atol($align->text), atol($size->text), psi_layout_init(0, atol($width->text), NULL));
 }
 ;
 
@@ -1282,8 +1648,19 @@ array_size[as]:
        %empty {
        $as = 0;
 }
-|      LBRACKET NUMBER RBRACKET {
-       $as = atol($NUMBER->text);
+|      LBRACKET RBRACKET {
+       $as = 0;
+}
+|      LBRACKET CPP_RESTRICT RBRACKET {
+       $as = 0;
+}
+|      LBRACKET num_exp RBRACKET {
+       if (psi_num_exp_validate(PSI_DATA(P), $num_exp, NULL, NULL, NULL, NULL, NULL)) {
+               $as = psi_long_num_exp($num_exp, NULL, &P->preproc->defs);
+       } else {
+               $as = 0;
+       }
+       psi_num_exp_free(&$num_exp);
 }
 ;
 
@@ -1297,14 +1674,26 @@ indirection[i]:
 ;
 
 pointers[p]:
-       ASTERISK {
-       $p = 1;
+       asterisks
+|      asterisks[a] CPP_RESTRICT {
+       $p = $a;
+}
+;
+
+asterisks[a]:
+       asterisk {
+       $a = 1;
 }
-|      pointers[p_] ASTERISK {
-       $p = $p_ + 1;
+|      asterisks[a_] asterisk {
+       $a = $a_ + 1;
 }
 ;
 
+asterisk:
+       ASTERISK
+|      ASTERISK CONST
+;
+
 /*
  *
  * impl
@@ -1437,21 +1826,25 @@ let_exp[exp]:
 ;
 
 let_exp_byref[exp]:
-       NULL {
+       NULL %dprec 2 {
        $exp = psi_let_exp_init(PSI_LET_NULL, NULL);
 }
+|      num_exp[num] %dprec 1 {
+       $exp = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, $num);
+}
 |      let_calloc[calloc] {
        $exp = psi_let_exp_init(PSI_LET_CALLOC, $calloc);
 }
+|      STATIC let_calloc[calloc] {
+       $exp = psi_let_exp_init(PSI_LET_CALLOC, $calloc);
+       $calloc->static_memory = 1;
+}
 |      let_callback[callback] {
        $exp = psi_let_exp_init(PSI_LET_CALLBACK, $callback);
 }
 |      let_func[func] {
        $exp = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, $func);
 }
-|      num_exp[num] {
-       $exp = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, $num);
-}
 ;
 
 let_exp_assign[exp]: 
@@ -1475,7 +1868,12 @@ let_calloc[calloc]:
 
 let_callback[callback]:
        CALLBACK callback_rval[func] LPAREN impl_var[var] LPAREN callback_arg_list[args] RPAREN RPAREN {
-       $callback = psi_let_callback_init(psi_let_func_init($func->type, $func->text, $var), $args);
+       $callback = psi_let_callback_init(psi_let_func_init($func->type, $func->text, $var), $args, NULL);
+       $callback->func->token = psi_token_copy($func);
+       $callback->token = psi_token_copy($CALLBACK);
+}
+|      CALLBACK LPAREN call_decl_vars[cb_args] RPAREN AS callback_rval[func] LPAREN impl_var[var] LPAREN callback_arg_list[args] RPAREN RPAREN {
+       $callback = psi_let_callback_init(psi_let_func_init($func->type, $func->text, $var), $args, $cb_args);
        $callback->func->token = psi_token_copy($func);
        $callback->token = psi_token_copy($CALLBACK);
 }
@@ -1544,12 +1942,35 @@ callback_args[args]:
 ;
 
 return_stmt[return]:
-       RETURN set_func[func] EOS {
-       $return = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, $func));
+       RETURN return_exp EOS {
+       $return = psi_return_stmt_init($return_exp);
        $return->token = psi_token_copy($RETURN);
 }
 ;
 
+return_exp:
+       decl_var[func] LPAREN call_decl_vars[args] RPAREN AS set_func {
+       $return_exp = psi_return_exp_init($func, $args, psi_set_exp_init(PSI_SET_FUNC, $set_func));
+       $return_exp->token = psi_token_copy($func->token);
+}
+|      set_func {
+       $return_exp = psi_return_exp_init(NULL, NULL, psi_set_exp_init(PSI_SET_FUNC, $set_func));
+       $return_exp->token = psi_token_copy($set_func->token);
+}
+;
+
+call_decl_vars[args]:
+       %empty {
+       $args = NULL;
+}
+|      VOID {
+       $args = NULL;
+}
+|      decl_vars[vars] {
+       $args = $vars;
+}
+;
+
 set_stmt[set]:
        SET set_exp[exp] EOS {
        $set = psi_set_stmt_init($exp);
@@ -1648,15 +2069,6 @@ free_exp[exp]:
 }
 ;
 
-decl_vars[vars]:
-       decl_var[var] {
-       $vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_var_free), &$var);
-}
-|      decl_vars[vars_] COMMA decl_var[var] {
-       $vars = psi_plist_add($vars_, &$var);
-}
-;
-
 reference:
        %empty {
        $reference = false;