From b6a04e85779e14399f03e91939b8cb47eb330ef7 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 5 Feb 2016 09:39:53 +0100 Subject: [PATCH] callable and func_ptr typedef fixes --- src/context.c | 3 +- src/libffi.c | 1 + src/parser.re | 75 ++--------------------------------------------- src/parser_proc.h | 5 ++-- src/parser_proc.y | 49 ++++++++++++++++++++++++++++--- 5 files changed, 54 insertions(+), 79 deletions(-) diff --git a/src/context.c b/src/context.c index 218da24..ed26f82 100644 --- a/src/context.c +++ b/src/context.c @@ -184,7 +184,7 @@ void PSI_ContextBuild(PSI_Context *C, const char *paths) { int i, n; char *sep = NULL, *cpy = strdup(paths), *ptr = cpy; - struct dirent **entries = NULL; + struct dirent **entries; do { sep = strchr(ptr, ':'); @@ -193,6 +193,7 @@ void PSI_ContextBuild(PSI_Context *C, const char *paths) *sep = 0; } + entries = NULL; n = php_scandir(ptr, &entries, psi_select_dirent, alphasort); if (n > 0) { diff --git a/src/libffi.c b/src/libffi.c index 301995e..be9edfd 100644 --- a/src/libffi.c +++ b/src/libffi.c @@ -101,6 +101,7 @@ static inline ffi_type *psi_ffi_token_type(token_t t) { return &ffi_type_longdouble; #endif case PSI_T_POINTER: + case PSI_T_FUNCTION: return &ffi_type_pointer; } } diff --git a/src/parser.re b/src/parser.re index e7787fc..7e445fd 100644 --- a/src/parser.re +++ b/src/parser.re @@ -127,77 +127,6 @@ void PSI_ParserFree(PSI_Parser **P) # error BSIZE must be greater than YYMAXFILL #endif -#define PSI_T(n) \ -(n) == PSI_T_NAME ? "NAME" : \ -(n) == PSI_T_PLUS ? "PLUS" : \ -(n) == PSI_T_MINUS ? "MINUS" : \ -(n) == PSI_T_SLASH ? "SLASH" : \ -(n) == PSI_T_ASTERISK ? "ASTERISK" : \ -(n) == PSI_T_TEMP ? "TEMP" : \ -(n) == PSI_T_FREE ? "FREE" : \ -(n) == PSI_T_SET ? "SET" : \ -(n) == PSI_T_LET ? "LET" : \ -(n) == PSI_T_RETURN ? "RETURN" : \ -(n) == PSI_T_LIB ? "LIB" : \ -(n) == PSI_T_INT ? "INT" : \ -(n) == PSI_T_UNSIGNED ? "UNSIGNED" : \ -(n) == PSI_T_EOF ? "EOF" : \ -(n) == PSI_T_QUOTED_STRING ? "QUOTED_STRING" : \ -(n) == PSI_T_EOS ? "EOS" : \ -(n) == PSI_T_STRUCT ? "STRUCT" : \ -(n) == PSI_T_LBRACE ? "LBRACE" : \ -(n) == PSI_T_RBRACE ? "RBRACE" : \ -(n) == PSI_T_COLON ? "COLON" : \ -(n) == PSI_T_LPAREN ? "LPAREN" : \ -(n) == PSI_T_NUMBER ? "NUMBER" : \ -(n) == PSI_T_RPAREN ? "RPAREN" : \ -(n) == PSI_T_BOOL ? "BOOL" : \ -(n) == PSI_T_FLOAT ? "FLOAT" : \ -(n) == PSI_T_STRING ? "STRING" : \ -(n) == PSI_T_CONST ? "CONST" : \ -(n) == PSI_T_NSNAME ? "NSNAME" : \ -(n) == PSI_T_EQUALS ? "EQUALS" : \ -(n) == PSI_T_TYPEDEF ? "TYPEDEF" : \ -(n) == PSI_T_VOID ? "VOID" : \ -(n) == PSI_T_LBRACKET ? "LBRACKET" : \ -(n) == PSI_T_RBRACKET ? "RBRACKET" : \ -(n) == PSI_T_COMMA ? "COMMA" : \ -(n) == PSI_T_ELLIPSIS ? "ELLIPSIS" : \ -(n) == PSI_T_DOUBLE ? "DOUBLE" : \ -(n) == PSI_T_INT8 ? "INT8" : \ -(n) == PSI_T_UINT8 ? "UINT8" : \ -(n) == PSI_T_INT16 ? "INT16" : \ -(n) == PSI_T_UINT16 ? "UINT16" : \ -(n) == PSI_T_INT32 ? "INT32" : \ -(n) == PSI_T_UINT32 ? "UINT32" : \ -(n) == PSI_T_INT64 ? "INT64" : \ -(n) == PSI_T_UINT64 ? "UINT64" : \ -(n) == PSI_T_FUNCTION ? "FUNCTION" : \ -(n) == PSI_T_NULL ? "NULL" : \ -(n) == PSI_T_TRUE ? "TRUE" : \ -(n) == PSI_T_FALSE ? "FALSE" : \ -(n) == PSI_T_DOLLAR ? "DOLLAR" : \ -(n) == PSI_T_CALLOC ? "CALLOC" : \ -(n) == PSI_T_OBJVAL ? "OBJVAL" : \ -(n) == PSI_T_ARRVAL ? "ARRVAL" : \ -(n) == PSI_T_PATHVAL ? "PATHVAL" : \ -(n) == PSI_T_STRLEN ? "STRLEN" : \ -(n) == PSI_T_STRVAL ? "STRVAL" : \ -(n) == PSI_T_FLOATVAL ? "FLOATVAL" : \ -(n) == PSI_T_INTVAL ? "INTVAL" : \ -(n) == PSI_T_BOOLVAL ? "BOOLVAL" : \ -(n) == PSI_T_TO_OBJECT ? "TO_OBJECT" : \ -(n) == PSI_T_TO_ARRAY ? "TO_ARRAY" : \ -(n) == PSI_T_TO_STRING ? "TO_STRING" : \ -(n) == PSI_T_TO_INT ? "TO_INT" : \ -(n) == PSI_T_TO_FLOAT ? "TO_FLOAT" : \ -(n) == PSI_T_TO_BOOL ? "TO_BOOL" : \ -(n) == PSI_T_MIXED ? "MIXED" : \ -(n) == PSI_T_ARRAY ? "ARRAY" : \ -(n) == PSI_T_OBJECT ? "OBJECT" : \ -(n) == PSI_T_AMPERSAND ? "AMPERSAND" : \ - - #define RETURN(t) do { \ P->num = t; \ if (P->flags & PSI_PARSER_DEBUG) { \ @@ -235,6 +164,7 @@ token_t PSI_ParserScan(PSI_Parser *P) W = [a-zA-Z0-9_]; NAME = [a-zA-Z_]W*; NSNAME = (NAME)? ("\\" NAME)+; + DOLLAR_NAME = '$' NAME; QUOTED_STRING = "\"" ([^\"])+ "\""; NUMBER = [+-]? [0-9]* "."? [0-9]+ ([eE] [+-]? [0-9]+)?; @@ -250,7 +180,6 @@ token_t PSI_ParserScan(PSI_Parser *P) "[" {RETURN(PSI_T_LBRACKET);} "]" {RETURN(PSI_T_RBRACKET);} "=" {RETURN(PSI_T_EQUALS);} - "$" {RETURN(PSI_T_DOLLAR);} "*" {RETURN(PSI_T_ASTERISK);} "&" {RETURN(PSI_T_AMPERSAND);} "+" {RETURN(PSI_T_PLUS);} @@ -263,6 +192,7 @@ token_t PSI_ParserScan(PSI_Parser *P) 'FALSE' {RETURN(PSI_T_FALSE);} 'NULL' {RETURN(PSI_T_NULL);} 'MIXED' {RETURN(PSI_T_MIXED);} + 'CALLABLE' {RETURN(PSI_T_CALLABLE);} 'VOID' {RETURN(PSI_T_VOID);} 'BOOL' {RETURN(PSI_T_BOOL);} 'CHAR' {RETURN(PSI_T_CHAR);} @@ -314,6 +244,7 @@ token_t PSI_ParserScan(PSI_Parser *P) NUMBER {RETURN(PSI_T_NUMBER);} NAME {RETURN(PSI_T_NAME);} NSNAME {RETURN(PSI_T_NSNAME);} + DOLLAR_NAME {RETURN(PSI_T_DOLLAR_NAME);} QUOTED_STRING {RETURN(PSI_T_QUOTED_STRING);} [^] {break;} */ diff --git a/src/parser_proc.h b/src/parser_proc.h index 3980125..8471c14 100644 --- a/src/parser_proc.h +++ b/src/parser_proc.h @@ -53,7 +53,7 @@ #define PSI_T_NULL 53 #define PSI_T_TRUE 54 #define PSI_T_FALSE 55 -#define PSI_T_DOLLAR 56 +#define PSI_T_DOLLAR_NAME 56 #define PSI_T_OBJVAL 57 #define PSI_T_ARRVAL 58 #define PSI_T_PATHVAL 59 @@ -71,4 +71,5 @@ #define PSI_T_MIXED 71 #define PSI_T_ARRAY 72 #define PSI_T_OBJECT 73 -#define PSI_T_AMPERSAND 74 +#define PSI_T_CALLABLE 74 +#define PSI_T_AMPERSAND 75 diff --git a/src/parser_proc.y b/src/parser_proc.y index c28ff59..b4bf960 100644 --- a/src/parser_proc.y +++ b/src/parser_proc.y @@ -280,7 +280,12 @@ decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). { decl_typedef_body(def) ::= decl_typedef_body_ex(def_). { def = def_; } -decl_typedef_body(def) ::= decl_func(func_) LPAREN decl_args(args) RPAREN. { +%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_; +} +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); @@ -310,6 +315,30 @@ decl_func(func) ::= VOID(T) NAME(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*} %destructor decl_abi {free_decl_abi($$);} @@ -344,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( @@ -560,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; } @@ -589,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); } @@ -765,7 +806,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). { -- 2.30.2