+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_) ::= decl_struct_args_block(args). {
+ args_ = args;
+}
+decl_struct_args(args_) ::= EOS. {
+ args_ = init_decl_args(NULL);
+}
+
+
+%type decl_struct {decl_struct*}
+%destructor decl_struct {free_decl_struct($$);}
+decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
+ strct = init_decl_struct(N->text, args);
+ strct->align = as.a;
+ strct->size = as.s;
+ strct->token = N;
+}
+
+%type align_and_size { struct {size_t a; size_t s; } }
+align_and_size(as) ::= . {
+ as.a = 0;
+ as.s = 0;
+}
+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.
+%type const_type {const_type*}
+%destructor const_type {free_const_type($$);}
+const_type(type_) ::= const_type_token(T). {
+ type_ = init_const_type(T->type, T->text);
+ free(T);
+}
+%type constant {constant*}
+%destructor constant {free_constant($$);}
+constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
+ constant = init_constant(type, T->text, val);
+ free(T);
+}
+
+%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_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);
+}
+decl_typedef_body(def) ::= decl_arg(arg). {
+ def = arg;