From: Michael Wallner Date: Thu, 28 Jan 2016 09:05:22 +0000 (+0100) Subject: full struct typedefs X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=d199099941c6546d29cb5900e2862b79a55e620f;p=m6w6%2Fext-psi full struct typedefs --- diff --git a/TODO b/TODO index 9ed0e5e..ee63abe 100644 --- a/TODO +++ b/TODO @@ -6,4 +6,3 @@ * pemalloc * unions * callbacks and function pointers -* full struct typedefs \ No newline at end of file diff --git a/src/context_validate.c b/src/context_validate.c index 66b2a77..6a89d6e 100644 --- a/src/context_validate.c +++ b/src/context_validate.c @@ -222,6 +222,7 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) { } } else { token_t t; + size_t size, align; if (darg->var->pointer_level && (!darg->var->array_size || darg->var->pointer_level == 1)) { t = PSI_T_POINTER; @@ -229,17 +230,21 @@ static inline int validate_decl_struct(PSI_Data *data, decl_struct *s) { t = real_decl_type(darg->type)->type; } + size = psi_t_size(t) * (darg->var->array_size ?: 1); + if (i) { decl_arg *last = s->args->args[i-1]; - darg->layout = init_decl_struct_layout( - psi_t_align(t, last->layout->pos + last->layout->len), - psi_t_size(t) * darg->var->array_size); + + align = psi_t_align(t, last->layout->pos + last->layout->len); } else { - darg->layout = init_decl_struct_layout(0, psi_t_size(t)); + align = 0; } + + darg->layout = init_decl_struct_layout(align, size); } if (s->size < darg->layout->pos + darg->layout->len) { s->size = darg->layout->pos + darg->layout->len; + /* FIXME: align struct */ } } diff --git a/src/parser.h b/src/parser.h index daf99dc..6710996 100644 --- a/src/parser.h +++ b/src/parser.h @@ -162,7 +162,7 @@ typedef struct decl_struct_layout { static inline decl_struct_layout *init_decl_struct_layout(size_t pos, size_t len) { decl_struct_layout *l = calloc(1, sizeof(*l)); - + ZEND_ASSERT(pos+len); l->pos = pos; l->len = len; return l; diff --git a/src/parser_proc.y b/src/parser_proc.y index 328bd10..2068105 100644 --- a/src/parser_proc.y +++ b/src/parser_proc.y @@ -106,9 +106,14 @@ struct_name(n) ::= STRUCT NAME(N). { n = N; } +%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. { @@ -154,11 +159,17 @@ decl_typedef(def) ::= TYPEDEF decl_typedef_body(def_) EOS. { } %type decl_typedef_body {decl_typedef*} %destructor decl_typedef_body {free_decl_typedef($$);} +decl_typedef_body(def) ::= struct_name(N) struct_size(size_) decl_struct_args_block(args) NAME(ALIAS). { + def = init_decl_typedef(ALIAS->text, init_decl_type(PSI_T_STRUCT, N->text)); + def->token = ALIAS; + def->type->strct = init_decl_struct(N->text, args); + def->type->strct->token = N; + def->type->strct->size = size_; +} decl_typedef_body(def) ::= decl_type(type) NAME(ALIAS). { def = init_decl_typedef(ALIAS->text, type); def->token = ALIAS; } - /* 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));