zval passthru
[m6w6/ext-psi] / src / parser_proc.y
index 1cc466f16cb969b97c665479bae33d970f299516..6a582cba4fffc4157d12383f9b97eb2a9ffbe2c2 100644 (file)
@@ -29,7 +29,7 @@ void psi_error(int, const char *, int, const char *, ...);
 %nonassoc NAME.
 %left PLUS MINUS.
 %left SLASH ASTERISK.
-%fallback NAME TEMP FREE SET LET RETURN LIB STRING.
+%fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING.
 
 file ::= blocks.
 
@@ -41,7 +41,7 @@ block ::= EOS.
 
 block ::= LIB(T) QUOTED_STRING(libname) EOS. {
        if (P->psi.file.ln) {
-               P->error(T, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
+               P->error(P, T, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
        } else {
                P->psi.file.ln = strndup(libname->text + 1, libname->size - 2);
        }
@@ -60,6 +60,9 @@ block ::= decl_typedef(def). {
        if (def->type->strct) {
                P->structs = add_decl_struct(P->structs, def->type->strct);
        }
+       if (def->type->enm) {
+               P->enums = add_decl_enum(P->enums, def->type->enm);
+       }
 }
 block ::= constant(constant). {
        P->consts = add_constant(P->consts, constant);
@@ -67,14 +70,91 @@ block ::= constant(constant). {
 block ::= decl_struct(strct). {
        P->structs = add_decl_struct(P->structs, strct);
 }
+block ::= decl_union(u). {
+       P->unions = add_decl_union(P->unions, u);
+}
+block ::= decl_enum(e). {
+       P->enums = add_decl_enum(P->enums, e);
+}
 
-struct_name(n) ::= STRUCT NAME(N). {
+optional_name(n) ::= .{
+       n = NULL;
+}
+optional_name(n) ::= NAME(N). {
        n = N;
 }
 
+enum_name(n) ::= ENUM(E) optional_name(N). {
+       if (N) {
+               n = N;
+               free(E);
+       } else {
+               char digest[17];
+
+               PSI_TokenHash(E, digest);
+               n = PSI_TokenTranslit(PSI_TokenAppend(E, 1, digest), " ", "@");
+       }
+}
+
+%type decl_enum {decl_enum *}
+%destructor decl_enum {free_decl_enum($$);}
+decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
+       e = init_decl_enum(N->text, list);
+       e->token = N;
+}
+
+%type decl_enum_items {decl_enum_items*}
+%destructor decl_enum_items {free_decl_enum_items($$);}
+decl_enum_items(l) ::= decl_enum_item(i). {
+       l = init_decl_enum_items(i);
+}
+decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
+       l = add_decl_enum_item(l_, i);
+}
+
+%type decl_enum_item {decl_enum_item*}
+%destructor decl_enum_item {free_decl_enum_item($$);}
+decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
+       i = init_decl_enum_item(N->text, num);
+       i->token = N;
+}
+decl_enum_item(i) ::= NAME(N). {
+       i = init_decl_enum_item(N->text, NULL);
+       i->token = N;
+}
+
+union_name(n) ::= UNION(U) optional_name(N). {
+       if (N) {
+               n = N;
+               free(U);
+       } else {
+               char digest[17];
+
+               PSI_TokenHash(U, digest);
+               n = PSI_TokenTranslit(PSI_TokenAppend(U, 1, digest), " ", "@");
+       }
+}
+
+struct_name(n) ::= STRUCT(S) optional_name(N). {
+       if (N) {
+               n = N;
+               free(S);
+       } else {
+               char digest[17];
+
+               PSI_TokenHash(S, digest);
+               n = PSI_TokenTranslit(PSI_TokenAppend(S, 1, digest), " ", "@");
+       }
+}
+
+%type decl_struct_args_block {decl_args*}
+%destructor decl_struct_args_block {free_decl_args($$);}
+decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
+       args_ = args;
+}
 %type decl_struct_args {decl_args*}
 %destructor decl_struct_args {free_decl_args($$);}
-decl_struct_args(args_) ::= LBRACE struct_args(args) RBRACE. {
+decl_struct_args(args_) ::= decl_struct_args_block(args). {
        args_ = args;
 }
 decl_struct_args(args_) ::= EOS. {
@@ -84,19 +164,32 @@ decl_struct_args(args_) ::= EOS. {
 
 %type decl_struct {decl_struct*}
 %destructor decl_struct {free_decl_struct($$);}
-decl_struct(strct) ::= struct_name(N) struct_size(size_) decl_struct_args(args). {
+decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
        strct = init_decl_struct(N->text, args);
-       strct->size = size_;
+       strct->align = as.a;
+       strct->size = as.s;
        strct->token = N;
 }
 
-%type struct_size {size_t}
-struct_size(size) ::= . {
-       size = 0;
+%type align_and_size { struct {size_t a; size_t s; } }
+align_and_size(as) ::= . {
+       as.a = 0;
+       as.s = 0;
 }
-struct_size(size) ::= COLON COLON LPAREN NUMBER(SIZ) RPAREN. {
-       size = atol(SIZ->text);
-       free(SIZ);
+align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
+       as.a = atol(A->text);
+       as.s = atol(S->text);
+       free(A);
+       free(S);
+}
+
+%type decl_union {decl_union*}
+%destructor decl_union {free_decl_union($$);}
+decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
+       u = init_decl_union(N->text, args);
+       u->align = as.a;
+       u->size = as.s;
+       u->token = N;
 }
 
 %token_class const_type_token BOOL INT FLOAT STRING.
@@ -113,23 +206,92 @@ constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val)
        free(T);
 }
 
-%type decl_typedef {decl_typedef*}
-%destructor decl_typedef {free_decl_typedef($$);}
-decl_typedef(def) ::= TYPEDEF decl_typedef_body(def_) EOS. {
+%type decl_typedef {decl_arg*}
+%destructor decl_typedef {
+       free_decl_arg($$);
+       if ($$->type->strct) {
+               free_decl_struct($$->type->strct);
+       }
+       if ($$->type->enm) {
+               free_decl_enum($$->type->enm);
+       }
+       if ($$->type->func) {
+               free_decl($$->type->func);
+       }
+}
+decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
+       def = def_;
+       def->token = T;
+}
+%type decl_typedef_body_ex {decl_arg*}
+%destructor decl_typedef_body_ex {
+       free_decl_arg($$);
+       if ($$->type->strct) {
+               free_decl_struct($$->type->strct);
+       }
+       if ($$->type->enm) {
+               free_decl_enum($$->type->enm);
+       }
+       if ($$->type->unn) {
+               free_decl_union($$->type->unn);
+       }
+       if ($$->type->func) {
+               free_decl($$->type->func);
+       }
+}
+decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
+       def = init_decl_arg(init_decl_type(PSI_T_STRUCT, N->text), var);
+       def->type->token = PSI_TokenCopy(N);
+       def->type->strct = init_decl_struct(N->text, args);
+       def->type->strct->token = N;
+       def->type->strct->align = as.a;
+       def->type->strct->size = as.s;
+}
+decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
+       def = init_decl_arg(init_decl_type(PSI_T_UNION, N->text), var);
+       def->type->token = PSI_TokenCopy(N);
+       def->type->unn = init_decl_union(N->text, args);
+       def->type->unn->token = N;
+       def->type->unn->align = as.a;
+       def->type->unn->size = as.s;
+}
+decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
+       def = init_decl_arg(init_decl_type(PSI_T_ENUM, e->name), init_decl_var(ALIAS->text, 0, 0));
+       def->var->token = ALIAS;
+       def->type->token = PSI_TokenCopy(e->token);
+       def->type->enm = e;
+}
+%type decl_typedef_body {decl_arg*}
+%destructor decl_typedef_body {
+       free_decl_arg($$);
+       if ($$->type->strct) {
+               free_decl_struct($$->type->strct);
+       }
+       if ($$->type->enm) {
+               free_decl_enum($$->type->enm);
+       }
+       if ($$->type->unn) {
+               free_decl_union($$->type->unn);
+       }
+       if ($$->type->func) {
+               free_decl($$->type->func);
+       }
+}
+decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
        def = def_;
 }
-%type decl_typedef_body {decl_typedef*}
-%destructor decl_typedef_body {free_decl_typedef($$);}
-decl_typedef_body(def) ::= decl_type(type) NAME(ALIAS). {
-       def = init_decl_typedef(ALIAS->text, type);
-       def->token = ALIAS;
+%type decl_typedef_body_fn_args {decl_args *}
+%destructor decl_typedef_body_fn_args {free_decl_args($$);}
+decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
+       args = args_;
 }
-
-/* support opaque types */
-decl_typedef_body(def) ::= VOID(V) indirection(i) NAME(ALIAS). {
-       def = init_decl_typedef(ALIAS->text, init_decl_type(i?PSI_T_POINTER:V->type, V->text));
-       def->token = ALIAS;
-       def->type->token = V;
+decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
+       def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
+       def->type->token = PSI_TokenCopy(func_->token);
+       def->type->func = init_decl(init_decl_abi("default"), func_, args);
+}
+decl_typedef_body(def) ::= decl_arg(arg). {
+       def = arg;
 }
 
 %type decl {decl*}
@@ -150,8 +312,32 @@ decl_func(func) ::= VOID(T) NAME(N). {
                init_decl_var(N->text, 0, 0)
        );
        func->type->token = T;
-       //free(T);
-       free(N);
+       func->var->token = N;
+       func->token = N;
+}
+decl_typedef_body(def) ::= VOID(T) indirection(i) LPAREN ASTERISK NAME(N) RPAREN decl_typedef_body_fn_args(args). {
+       decl_arg *func_ = init_decl_arg(
+               init_decl_type(T->type, T->text),
+               init_decl_var(N->text, i, 0)
+       );
+       func_->type->token = T;
+       func_->var->token = N;
+       func_->token = N;
+       def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
+       def->type->token = PSI_TokenCopy(func_->token);
+       def->type->func = init_decl(init_decl_abi("default"), func_, args);
+}
+decl_typedef_body(def) ::= CONST VOID(T) pointers(i) LPAREN ASTERISK NAME(N) RPAREN decl_typedef_body_fn_args(args). {
+       decl_arg *func_ = init_decl_arg(
+               init_decl_type(T->type, T->text),
+               init_decl_var(N->text, i, 0)
+       );
+       func_->type->token = T;
+       func_->var->token = N;
+       func_->token = N;
+       def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
+       def->type->token = PSI_TokenCopy(func_->token);
+       def->type->func = init_decl(init_decl_abi("default"), func_, args);
 }
 
 %type decl_abi {decl_abi*}
@@ -187,6 +373,18 @@ decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
        arg_ = init_decl_arg(type, var);
 }
+decl_typedef_body(def) ::= const_decl_type(type_) indirection(i) LPAREN ASTERISK NAME(N) RPAREN decl_typedef_body_fn_args(args). {
+       decl_arg *func_ = init_decl_arg(
+               type_,
+               init_decl_var(N->text, i, 0)
+       );
+       func_->var->token = N;
+       func_->token = N;
+       def = init_decl_arg(init_decl_type(PSI_T_FUNCTION, func_->var->name), copy_decl_var(func_->var));
+       def->type->token = PSI_TokenCopy(func_->token);
+       def->type->func = init_decl(init_decl_abi("default"), func_, args);
+}
+
 /* void pointers need a specific rule */
 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
        arg_ = init_decl_arg(
@@ -230,7 +428,27 @@ struct_args(args) ::= struct_args(args_) struct_arg(arg). {
        args = add_decl_arg(args_, arg);
 }
 %type struct_arg {decl_arg*}
-%destructor struct_arg {free_decl_arg($$);}
+%destructor struct_arg {
+       free_decl_arg($$);
+       if ($$->type->strct) {
+               free_decl_struct($$->type->strct);
+       }
+       if ($$->type->enm) {
+               free_decl_enum($$->type->enm);
+       }
+       if ($$->type->func) {
+               free_decl($$->type->func);
+       }
+}
+struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
+       arg_ = def;
+       if (def->type->strct) {
+               P->structs = add_decl_struct(P->structs, def->type->strct);
+       }
+       if (def->type->enm) {
+               P->enums = add_decl_enum(P->enums, def->type->enm);
+       }
+}
 struct_arg(arg) ::= decl_arg(arg_) struct_layout(layout_) EOS. {
        arg_->layout = layout_;
        arg = arg_;
@@ -332,6 +550,16 @@ decl_type(type_) ::= STRUCT(S) NAME(T). {
        type_->token = T;
        free(S);
 }
+decl_type(type_) ::= UNION(U) NAME(T). {
+       type_ = init_decl_type(U->type, T->text);
+       type_->token = T;
+       free(U);
+}
+decl_type(type_) ::= ENUM(E) NAME(T). {
+       type_ = init_decl_type(E->type, T->text);
+       type_->token = T;
+       free(E);
+}
 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
 %type decl_type {decl_type*}
 %destructor decl_type {free_decl_type($$);}
@@ -373,7 +601,7 @@ impl_def_val(def) ::= impl_def_val_token(T). {
 
 %type impl_var {impl_var*}
 %destructor impl_var {free_impl_var($$);}
-impl_var(var) ::= reference(r) DOLLAR NAME(T). {
+impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
        var = init_impl_var(T->text, r);
        var->token = T;
 }
@@ -402,7 +630,7 @@ impl_args(args) ::= LPAREN impl_arg_list(args_) COMMA impl_vararg(va) RPAREN. {
 
 %type impl_vararg {impl_arg*}
 %destructor impl_vararg {free_impl_arg($$);}
-impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR NAME(T). {
+impl_vararg(va) ::= impl_type(type) reference(r) ELLIPSIS DOLLAR_NAME(T). {
        va = init_impl_arg(type, init_impl_var(T->text, r), NULL);
        free(T);
 }
@@ -472,6 +700,35 @@ let_stmt(let) ::= TEMP decl_var(var) EQUALS decl_var(val) EOS. {
        let = init_let_stmt(var, init_let_val(PSI_LET_TMP, val));
 }
 
+%type let_calloc {let_calloc*}
+%destructor let_calloc {free_let_calloc($$);}
+let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
+       alloc = init_let_calloc(nmemb, size);
+}
+%token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
+%type let_func {let_func*}
+%destructor let_func {free_let_func($$);}
+let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
+       func = init_let_func(T->type, T->text, var);
+       free(T);
+}
+
+%type callback_arg_list {set_values *}
+%destructor callback_arg_list {free_set_values($$);}
+callback_arg_list ::= .
+callback_arg_list(args) ::= callback_args(args_). {
+       args = args_;
+}
+
+%type callback_args {set_values *}
+%destructor callback_args {free_set_values($$);}
+callback_args(args) ::= set_value(val). {
+       args = init_set_values(val);
+}
+callback_args(args) ::= callback_args(args_) COMMA set_value(val). {
+       args = add_set_value(args_, val);
+}
+
 %type let_val {let_val*}
 %destructor let_val {free_let_val($$);}
 let_val(val) ::= NULL. {
@@ -486,20 +743,11 @@ let_val(val) ::= CALLOC LPAREN let_calloc(alloc) RPAREN. {
 let_val(val) ::= let_func(func). {
        val = init_let_val(PSI_LET_FUNC, func);
 }
-
-%type let_calloc {let_calloc*}
-%destructor let_calloc {free_let_calloc($$);}
-let_calloc(alloc) ::= num_exp(nmemb) COMMA num_exp(size). {
-       alloc = init_let_calloc(nmemb, size);
+let_val(val) ::= CALLBACK let_func_token(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. {
+       val = init_let_val(PSI_LET_CALLBACK, init_let_callback(
+               init_let_func(F->type, F->text, var), args_));
+       free(F);
 }
-%token_class let_func_token OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL.
-%type let_func {let_func*}
-%destructor let_func {free_let_func($$);}
-let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
-       func = init_let_func(T->type, T->text, var);
-       free(T);
-}
-
 
 %type set_stmt {set_stmt*}
 %destructor set_stmt {free_set_stmt($$);}
@@ -541,7 +789,7 @@ set_vals(vals) ::= set_vals(vals_) COMMA set_value(val). {
        vals = add_inner_set_value(vals_, val);
 }
 
-%token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL VOID.
+%token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID.
 %type set_func {set_func*}
 %destructor set_func {free_set_func($$);}
 set_func(func) ::= set_func_token(T). {
@@ -578,7 +826,7 @@ free_call(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
        call->token = F;
 }
 
-%token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT.
+%token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE.
 %type impl_type {impl_type*}
 %destructor impl_type {free_impl_type($$);}
 impl_type(type_) ::= impl_type_token(T). {