%code {
#include <assert.h>
#include <stdarg.h>
+#include <fnmatch.h>
+
+#include "php_psi.h"
#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);
}
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)
-{
- 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;
- }
- }
-}
static inline void psi_parser_proc_add_typedef(struct psi_parser *P, struct psi_decl_arg *def)
{
assert(def);
P->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
}
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);
}
static inline void psi_parser_proc_add_decl(struct psi_parser *P, struct psi_decl *decl) {
+ char *blacklisted;
+ size_t i = 0;
+
assert(decl);
+
+ while (psi_plist_get(PSI_G(blacklist).decls, i++, &blacklisted)) {
+ if (!fnmatch(blacklisted, decl->func->var->name, 0)) {
+ psi_decl_free(&decl);
+ return;
+ }
+ }
+
if (!P->decls) {
P->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
}
%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"
%token <struct psi_token *> PERIOD "."
%token <struct psi_token *> BACKSLASH "\\"
%token <struct psi_token *> ELLIPSIS "..."
+%token <struct psi_token *> IIF "?"
+%token <struct psi_token *> PRAGMA
+%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
%token <struct psi_token *> WHITESPACE
%token <struct psi_token *> NO_WHITESPACE
%token <struct psi_token *> CPP_HEADER
-
-//%destructor {psi_token_free((struct psi_token **) &$$);} <>
-
-%precedence AND OR
+%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
+
+%precedence IIF COLON
+%precedence OR
+%precedence AND
%precedence PIPE
%precedence CARET
%precedence AMPERSAND
%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
%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
%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_struct_args struct_args_block struct_args struct_arg_var_list decl_enum_items decl_vars decl_vars_with_layout
+%destructor {psi_plist_free($$);} decl_args decl_struct_args struct_args_block struct_args struct_arg_var_list decl_enum_items decl_vars decl_vars_with_layout
%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
%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 */
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 | 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 | 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 | 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 | 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 | 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;
+any_nobrace_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 | 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;
file:
| 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 {
| 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);
}
;
cpp:
- HASH cpp_exp[exp] EOL {
+ HASH EOL {
+ $cpp = NULL;
+}
+| HASH cpp_exp[exp] EOL {
$cpp = $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);
$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($PRAGMA_ONCE->type, NULL);
+ $exp->token = psi_token_copy($PRAGMA_ONCE);
+}
+| cpp_ignored_token cpp_macro_decl_tokens[tokens] {
+ psi_plist_free($tokens);
+ $exp = NULL;
+}
+;
+
+cpp_ignored_token:
+ LINE
+| PRAGMA
;
cpp_message_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);
%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]:
$exp = psi_num_exp_init_binary($binary_op_token->type, $lhs, $rhs);
$exp->token = psi_token_copy($binary_op_token);
}
+| cpp_macro_exp[cond] IIF cpp_macro_exp[truthy] COLON cpp_macro_exp[falsy] {
+ $exp = psi_num_exp_init_ternary($IIF->type, $cond, $truthy, $falsy);
+ $exp->token = psi_token_copy($IIF);
+}
+
| DEFINED name_token {
{
uint8_t exists;
$name_token->type = PSI_T_NAME;
exists = psi_cpp_defined(P->preproc, $name_token);
- $exp = psi_num_exp_init_num(psi_number_init(PSI_T_UINT8, &exists));
+ $exp = psi_num_exp_init_num(psi_number_init(PSI_T_UINT8, &exists, 0));
$exp->token = psi_token_copy($name_token);
}
}
$name_token->type = PSI_T_NAME;
exists = psi_cpp_defined(P->preproc, $name_token);
- $exp = psi_num_exp_init_num(psi_number_init(PSI_T_UINT8, &exists));
+ $exp = psi_num_exp_init_num(psi_number_init(PSI_T_UINT8, &exists, 0));
$exp->token = psi_token_copy($name_token);
}
}
| NUMBER {
- $exp = psi_num_exp_init_num(psi_number_init($NUMBER->type, $NUMBER->text));
+ $exp = psi_num_exp_init_num(psi_number_init($NUMBER->type, $NUMBER->text, $NUMBER->flags));
$exp->token = psi_token_copy($NUMBER);
$exp->data.n->token = psi_token_copy($NUMBER);
}
| QUOTED_CHAR {
- $exp = psi_num_exp_init_num(psi_number_init($QUOTED_CHAR->type, $QUOTED_CHAR->text));
+ $exp = psi_num_exp_init_num(psi_number_init($QUOTED_CHAR->type, $QUOTED_CHAR->text, 0));
$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));
+ $exp = psi_num_exp_init_num(psi_number_init(PSI_T_DEFINE, $name_token->text, 0));
$exp->token = psi_token_copy($name_token);
$exp->data.n->token = psi_token_copy($name_token);
}
| name_token LPAREN cpp_macro_call_args RPAREN {
$name_token->type = PSI_T_NAME;
$exp = psi_num_exp_init_num(psi_number_init(PSI_T_FUNCTION,
- psi_cpp_macro_call_init($name_token->text, $cpp_macro_call_args)));
+ psi_cpp_macro_call_init($name_token->text, $cpp_macro_call_args), 0));
$exp->token = psi_token_copy($name_token);
}
;
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 {
%empty {
$val = NULL;
}
+| num_exp[num] {
+ 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] {
$val = psi_impl_def_val_init($token->type, $token->text);
$val->token = psi_token_copy($token);
impl_def_val_token:
NULL
-| NUMBER
| TRUE
| FALSE
| QUOTED_STRING
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)
$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);
$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);
$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);
+}
+| qualified_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);
+}
+;
+
+typedef_anon_decl[def]:
+ typedef_decl
+| qualified_decl_type[type] {
+ $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);
+}
+| 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
;
}
| INT
| LONG
+| LONG INT
;
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;
}
;
-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),
$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),
$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),
| VOID {
$args = NULL;
}
-| decl_arg[arg] {
+| 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_args[args_] COMMA decl_anon_arg[arg] {
$args = psi_plist_add($args_, &$arg);
}
;
+decl_anon_arg[arg]:
+/* FIXME decl_functor_body_anon */
+ decl_arg {
+ $arg = $decl_arg;
+}
+| qualified_decl_type[type] indirection {
+ $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] {
;
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);
+ }
}
;
$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);
}
$exp = psi_num_exp_init_unary($op->type, $exp_);
$exp->token = psi_token_copy($op);
}
+| num_exp[cond] IIF num_exp[truthy] COLON num_exp[falsy] {
+ $exp = psi_num_exp_init_ternary($IIF->type, $cond, $truthy, $falsy);
+ $exp->token = psi_token_copy($IIF);
+}
;
number[num]:
NUMBER[token] {
- $num = psi_number_init($token->type, $token->text);
+ $num = psi_number_init($token->type, $token->text, $token->flags);
$num->token = psi_token_copy($token);
}
| NSNAME[token] {
- $num = psi_number_init($token->type, $token->text);
+ $num = psi_number_init($token->type, $token->text, 0);
$num->token = psi_token_copy($token);
}
| QUOTED_CHAR[token] {
- $num = psi_number_init($token->type, $token->text);
+ $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);
+ $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);
+ } 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]:
%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));
}
;
%empty {
$as = 0;
}
-| LBRACKET NUMBER RBRACKET {
- $as = atol($NUMBER->text);
+| LBRACKET 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;
+ }
}
;
;
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
| 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);
}
}
;
-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;