parser: fix uninitialized args
[m6w6/ext-psi] / src / parser_proc.y
1 %include {
2 #include "php_psi_stdinc.h"
3
4 #include <assert.h>
5 #include <stdarg.h>
6
7 #include "plist.h"
8 #include "parser.h"
9
10 /* rename lemon symbols, works better than DEF(%name) */
11 #define ParseAlloc psi_parser_proc_init_ex
12 #define Parse psi_parser_proc_parse
13 #define ParseTrace psi_parser_proc_trace
14 #define ParseFree psi_parser_proc_free_ex
15
16 /* fwd decls */
17 void *ParseAlloc(void *(*mallocProc)(size_t));
18 void ParseFree(void *p, void (*freeProc)(void*));
19
20 /* wrappers */
21 void *psi_parser_proc_init(void)
22 {
23 return ParseAlloc(malloc);
24 }
25
26 void psi_parser_proc_free(void **parser_proc)
27 {
28 if (*parser_proc) {
29 ParseFree(*parser_proc, free);
30 *parser_proc = NULL;
31 }
32 }
33
34 }
35 %token_prefix PSI_T_
36 %token_type {struct psi_token *}
37 %token_destructor {free($$);}
38 %default_destructor {(void)P;}
39 %extra_argument {struct psi_parser *P}
40 %syntax_error { ++P->errors; if (TOKEN && TOKEN->type != PSI_T_EOF) { psi_error(PSI_WARNING, TOKEN->file, TOKEN->line, "PSI syntax error: Unexpected token '%s' at pos %u", TOKEN->text, TOKEN->col); } else { psi_error(PSI_WARNING, P->file.fn, P->line, "PSI syntax error: Unexpected end of input"); } }
41 %token_class const_type_token BOOL INT FLOAT STRING.
42 %token_class decl_type_token FLOAT DOUBLE INT8 UINT8 INT16 UINT16 INT32 UINT32 INT64 UINT64 NAME.
43 %token_class impl_def_val_token NULL NUMBER TRUE FALSE QUOTED_STRING.
44 %token_class number_token NUMBER NSNAME.
45 %token_class num_exp_binary_op_token PIPE CARET AMPERSAND LSHIFT RSHIFT PLUS MINUS ASTERISK SLASH MODULO.
46 %token_class num_exp_unary_op_token TILDE NOT PLUS MINUS.
47 %token_class let_func_token ZVAL OBJVAL ARRVAL PATHVAL STRLEN STRVAL FLOATVAL INTVAL BOOLVAL COUNT.
48 %token_class set_func_token TO_OBJECT TO_ARRAY TO_STRING TO_INT TO_FLOAT TO_BOOL ZVAL VOID.
49 %token_class impl_type_token VOID MIXED BOOL INT FLOAT STRING ARRAY OBJECT CALLABLE.
50 %nonassoc NAME.
51 %right NOT TILDE.
52 %left PIPE.
53 %left CARET.
54 %left AMPERSAND.
55 %left LSHIFT RSHIFT.
56 %left PLUS MINUS.
57 %left ASTERISK SLASH MODULO.
58 %fallback NAME TEMP FREE SET LET RETURN CALLOC CALLBACK ZVAL LIB STRING COUNT.
59 %type decl_enum {struct psi_decl_enum *}
60 %destructor decl_enum {psi_decl_enum_free(&$$);}
61 %type decl_enum_items {struct psi_plist*}
62 %destructor decl_enum_items {psi_plist_free($$);}
63 %type decl_enum_item {struct psi_decl_enum_item*}
64 %destructor decl_enum_item {psi_decl_enum_item_free(&$$);}
65 %type decl_struct_args_block {struct psi_plist*}
66 %destructor decl_struct_args_block {psi_plist_free($$);}
67 %type decl_struct_args {struct psi_plist*}
68 %destructor decl_struct_args {psi_plist_free($$);}
69 %type decl_struct {struct psi_decl_struct*}
70 %destructor decl_struct {psi_decl_struct_free(&$$);}
71 %type align_and_size {struct psi_layout}
72 %type decl_union {struct psi_decl_union*}
73 %destructor decl_union {psi_decl_union_free(&$$);}
74 %type const_type {struct psi_const_type*}
75 %destructor const_type {psi_const_type_free(&$$);}
76 %type constant {struct psi_const*}
77 %destructor constant {psi_const_free(&$$);}
78 %type decl_typedef {struct psi_decl_arg*}
79 %destructor decl_typedef {psi_decl_arg_free(&$$);}
80 %type decl_typedef_body_ex {struct psi_decl_arg*}
81 %destructor decl_typedef_body_ex {psi_decl_arg_free(&$$);}
82 %type decl_typedef_body {struct psi_decl_arg*}
83 %destructor decl_typedef_body {psi_decl_arg_free(&$$);}
84 %type decl_typedef_body_fn_args {struct psi_plist*}
85 %destructor decl_typedef_body_fn_args {psi_plist_free($$);}
86 %type decl {struct psi_decl*}
87 %destructor decl {psi_decl_free(&$$);}
88 %type decl_func {struct psi_decl_arg*}
89 %destructor decl_func {psi_decl_arg_free(&$$);}
90 %type decl_abi {struct psi_decl_abi*}
91 %destructor decl_abi {psi_decl_abi_free(&$$);}
92 %type decl_var {struct psi_decl_var*}
93 %destructor decl_var {psi_decl_var_free(&$$);}
94 %type decl_vars {struct psi_plist*}
95 %destructor decl_vars {psi_plist_free($$);}
96 %type decl_arg {struct psi_decl_arg*}
97 %destructor decl_arg {psi_decl_arg_free(&$$);}
98 %type decl_args {struct psi_plist*}
99 %destructor decl_args {psi_plist_free($$);}
100 %type struct_args {struct psi_plist*}
101 %destructor struct_args {psi_plist_free($$);}
102 %type struct_arg {struct psi_decl_arg*}
103 %destructor struct_arg {psi_decl_arg_free(&$$);}
104 %type decl_layout {struct psi_layout*}
105 %destructor decl_layout {psi_layout_free(&$$);}
106 %type decl_type {struct psi_decl_type*}
107 %destructor decl_type {psi_decl_type_free(&$$);}
108 %type const_decl_type {struct psi_decl_type*}
109 %destructor const_decl_type {psi_decl_type_free(&$$);}
110 %type impl {struct psi_impl*}
111 %destructor impl {psi_impl_free(&$$);}
112 %type impl_func {struct psi_impl_func*}
113 %destructor impl_func {psi_impl_func_free(&$$);}
114 %type impl_def_val {struct psi_impl_def_val*}
115 %destructor impl_def_val {psi_impl_def_val_free(&$$);}
116 %type impl_var {struct psi_impl_var*}
117 %destructor impl_var {psi_impl_var_free(&$$);}
118 %type impl_arg {struct psi_impl_arg*}
119 %destructor impl_arg {psi_impl_arg_free(&$$);}
120 %type impl_args {struct psi_plist*}
121 %destructor impl_args {psi_plist_free($$);}
122 %type impl_vararg {struct psi_impl_arg*}
123 %destructor impl_vararg {psi_impl_arg_free(&$$);}
124 %type impl_stmts {struct psi_plist*}
125 %destructor impl_stmts {psi_plist_free($$);}
126 %type impl_stmt {struct psi_token**}
127 %destructor impl_stmt {psi_impl_stmt_free(&$$);}
128 %type number {struct psi_number*}
129 %destructor number {psi_number_free(&$$);}
130 %type num_exp {struct psi_num_exp*}
131 %destructor num_exp {psi_num_exp_free(&$$);}
132 %type let_stmt {struct psi_let_stmt*}
133 %destructor let_stmt {psi_let_stmt_free(&$$);}
134 %type let_calloc {struct psi_let_calloc*}
135 %destructor let_calloc {psi_let_calloc_free(&$$);}
136 %type let_func {struct psi_let_func*}
137 %destructor let_func {psi_let_func_free(&$$);}
138 %type callback_arg_list {struct psi_plist *}
139 %destructor callback_arg_list {psi_plist_free($$);}
140 %type callback_args {struct psi_plist *}
141 %destructor callback_args {psi_plist_free($$);}
142 %type let_callback {struct psi_let_callback*}
143 %destructor let_callback {psi_let_callback_free(&$$);}
144 %type let_exp {struct psi_let_exp*}
145 %destructor let_exp {psi_let_exp_free(&$$);}
146 %type let_exps {struct psi_plist*}
147 %destructor let_exps {psi_plist_free($$);}
148 %type set_stmt {struct psi_set_stmt*}
149 %destructor set_stmt {psi_set_stmt_free(&$$);}
150 %type set_exp {struct psi_set_exp*}
151 %destructor set_exp {psi_set_exp_free(&$$);}
152 %type set_exps {struct psi_plist*}
153 %destructor set_exps {psi_plist_free($$);}
154 %type set_func {struct psi_set_func*}
155 %destructor set_func {psi_set_func_free(&$$);}
156 %type return_stmt {struct psi_return_stmt*}
157 %destructor return_stmt {psi_return_stmt_free(&$$);}
158 %type free_stmt {struct psi_free_stmt*}
159 %destructor free_stmt {psi_free_stmt_free(&$$);}
160 %type free_exps {struct psi_plist*}
161 %destructor free_exps {psi_plist_free($$);}
162 %type free_exp {struct psi_free_exp*}
163 %destructor free_exp {psi_free_exp_free(&$$);}
164 %type impl_type {struct psi_impl_type*}
165 %destructor impl_type {psi_impl_type_free(&$$);}
166 %type reference {bool}
167 %type indirection {unsigned}
168 %type pointers {unsigned}
169 file ::= blocks.
170 blocks ::= block.
171 blocks ::= blocks block.
172 block ::= EOF.
173 block ::= EOS.
174 block ::= LIB(token) QUOTED_STRING(libname) EOS. {
175 if (P->file.ln) {
176 P->error(PSI_DATA(P), token, PSI_WARNING, "Extra 'lib %s' statement has no effect", libname->text);
177 } else {
178 P->file.ln = strndup(libname->text + 1, libname->size - 2);
179 }
180 free(libname);
181 free(token);
182 }
183 block ::= decl(decl). {
184 if (!P->decls) {
185 P->decls = psi_plist_init((psi_plist_dtor) psi_decl_free);
186 }
187 P->decls = psi_plist_add(P->decls, &decl);
188 }
189 block ::= impl(impl). {
190 if (!P->impls) {
191 P->impls = psi_plist_init((psi_plist_dtor) psi_impl_free);
192 }
193 P->impls = psi_plist_add(P->impls, &impl);
194 }
195 block ::= decl_typedef(def). {
196 if (!P->types) {
197 P->types = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
198 }
199 P->types = psi_plist_add(P->types, &def);
200 switch (def->type->type) {
201 case PSI_T_STRUCT:
202 if (def->type->real.strct) {
203 if (!P->structs) {
204 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
205 }
206 P->structs = psi_plist_add(P->structs, &def->type->real.strct);
207 }
208 break;
209 case PSI_T_UNION:
210 if (def->type->real.unn) {
211 if (!P->unions) {
212 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
213 }
214 P->unions = psi_plist_add(P->unions, &def->type->real.unn);
215 }
216 break;
217 case PSI_T_ENUM:
218 if (def->type->real.enm) {
219 if (!P->enums) {
220 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
221 }
222 P->enums = psi_plist_add(P->enums, &def->type->real.enm);
223 }
224 break;
225 }
226 }
227 block ::= constant(constant). {
228 if (!P->consts) {
229 P->consts = psi_plist_init((psi_plist_dtor) psi_const_free);
230 }
231 P->consts = psi_plist_add(P->consts, &constant);
232 }
233 block ::= decl_struct(strct). {
234 if (!P->structs) {
235 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
236 }
237 P->structs = psi_plist_add(P->structs, &strct);
238 }
239 block ::= decl_union(u). {
240 if (!P->unions) {
241 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
242 }
243 P->unions = psi_plist_add(P->unions, &u);
244 }
245 block ::= decl_enum(e). {
246 if (!P->enums) {
247 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
248 }
249 P->enums = psi_plist_add(P->enums, &e);
250 }
251 optional_name(n) ::= . {
252 n = NULL;
253 }
254 optional_name(n) ::= NAME(N). {
255 n = N;
256 }
257 align_and_size(as) ::= . {
258 as.pos = 0;
259 as.len = 0;
260 }
261 align_and_size(as) ::= COLON COLON LPAREN NUMBER(A) COMMA NUMBER(S) RPAREN. {
262 as.pos = atol(A->text);
263 as.len = atol(S->text);
264 free(A);
265 free(S);
266 }
267 enum_name(n) ::= ENUM(E) optional_name(N). {
268 if (N) {
269 n = N;
270 free(E);
271 } else {
272 char digest[17];
273 psi_token_hash(E, digest);
274 n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
275 }
276 }
277 struct_name(n) ::= STRUCT(S) optional_name(N). {
278 if (N) {
279 n = N;
280 free(S);
281 } else {
282 char digest[17];
283 psi_token_hash(S, digest);
284 n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
285 }
286 }
287 union_name(n) ::= UNION(U) optional_name(N). {
288 if (N) {
289 n = N;
290 free(U);
291 } else {
292 char digest[17];
293 psi_token_hash(U, digest);
294 n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
295 }
296 }
297 decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
298 e = psi_decl_enum_init(N->text, list);
299 e->token = N;
300 }
301 decl_enum_items(l) ::= decl_enum_item(i). {
302 l = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_enum_item_free),
303 &i);
304 }
305 decl_enum_items(l) ::= decl_enum_items(l_) COMMA decl_enum_item(i). {
306 l = psi_plist_add(l_, &i);
307 }
308 decl_enum_item(i) ::= NAME(N) EQUALS num_exp(num). {
309 i = psi_decl_enum_item_init(N->text, num);
310 i->token = N;
311 }
312 decl_enum_item(i) ::= NAME(N). {
313 i = psi_decl_enum_item_init(N->text, NULL);
314 i->token = N;
315 }
316 decl_struct_args_block(args_) ::= LBRACE struct_args(args) RBRACE. {
317 args_ = args;
318 }
319 decl_struct_args(args_) ::= decl_struct_args_block(args). {
320 args_ = args;
321 }
322 decl_struct_args(args_) ::= EOS. {
323 args_ = psi_plist_init((psi_plist_dtor) psi_decl_arg_free);
324 }
325 decl_struct(strct) ::= STRUCT NAME(N) align_and_size(as) decl_struct_args(args). {
326 strct = psi_decl_struct_init(N->text, args);
327 strct->align = as.pos;
328 strct->size = as.len;
329 strct->token = N;
330 }
331 decl_union(u) ::= UNION NAME(N) align_and_size(as) decl_struct_args(args). {
332 u = psi_decl_union_init(N->text, args);
333 u->align = as.pos;
334 u->size = as.len;
335 u->token = N;
336 }
337 const_type(type_) ::= const_type_token(T). {
338 type_ = psi_const_type_init(T->type, T->text);
339 free(T);
340 }
341 constant(constant) ::= CONST const_type(type) NSNAME(T) EQUALS impl_def_val(val) EOS. {
342 constant = psi_const_init(type, T->text, val);
343 constant->token = T;
344 }
345 decl_typedef(def) ::= TYPEDEF(T) decl_typedef_body(def_) EOS. {
346 def = def_;
347 def->token = T;
348 }
349 decl_typedef_body_ex(def) ::= struct_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
350 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_STRUCT, N->text), var);
351 def->type->token = psi_token_copy(N);
352 def->type->real.strct = psi_decl_struct_init(N->text, args);
353 def->type->real.strct->token = N;
354 def->type->real.strct->align = as.pos;
355 def->type->real.strct->size = as.len;
356 }
357 decl_typedef_body_ex(def) ::= union_name(N) align_and_size(as) decl_struct_args_block(args) decl_var(var). {
358 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_UNION, N->text), var);
359 def->type->token = psi_token_copy(N);
360 def->type->real.unn = psi_decl_union_init(N->text, args);
361 def->type->real.unn->token = N;
362 def->type->real.unn->align = as.pos;
363 def->type->real.unn->size = as.len;
364 }
365 decl_typedef_body_ex(def) ::= decl_enum(e) NAME(ALIAS). {
366 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_ENUM, e->name), psi_decl_var_init(ALIAS->text, 0, 0));
367 def->var->token = ALIAS;
368 def->type->token = psi_token_copy(e->token);
369 def->type->real.enm = e;
370 }
371 decl_typedef_body(def) ::= decl_typedef_body_ex(def_). {
372 def = def_;
373 }
374 decl_typedef_body_fn_args(args) ::= LPAREN decl_args(args_) RPAREN. {
375 args = args_;
376 }
377 decl_typedef_body(def) ::= decl_func(func_) decl_typedef_body_fn_args(args). {
378 def = psi_decl_arg_init(psi_decl_type_init(PSI_T_FUNCTION, func_->var->name), psi_decl_var_copy(func_->var));
379 def->type->token = psi_token_copy(func_->token);
380 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
381 }
382 decl_typedef_body(def) ::= decl_arg(arg). {
383 def = arg;
384 }
385 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) RPAREN EOS. {
386 decl = psi_decl_init(abi, func, args);
387 }
388 decl(decl) ::= decl_abi(abi) decl_func(func) LPAREN decl_args(args) COMMA ELLIPSIS RPAREN EOS. {
389 decl = psi_decl_init(abi, func, args);
390 decl->varargs = 1;
391 }
392 decl_func(func) ::= decl_arg(arg). {
393 func = arg;
394 }
395 decl_func(func) ::= VOID(T) NAME(N). {
396 func = psi_decl_arg_init(
397 psi_decl_type_init(T->type, T->text),
398 psi_decl_var_init(N->text, 0, 0)
399 );
400 func->type->token = T;
401 func->var->token = N;
402 func->token = N;
403 }
404 decl_typedef_body(def) ::= VOID(T) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
405 struct psi_decl_arg *func_ = psi_decl_arg_init(
406 psi_decl_type_init(T->type, T->text),
407 psi_decl_var_init(N->text, decl_i, 0)
408 );
409 func_->type->token = T;
410 func_->var->token = N;
411 func_->token = N;
412 def = psi_decl_arg_init(
413 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
414 psi_decl_var_copy(func_->var)
415 );
416 def->var->pointer_level = type_i;
417 def->type->token = psi_token_copy(func_->token);
418 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
419 }
420 decl_typedef_body(def) ::= CONST VOID(T) pointers(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
421 struct psi_decl_arg *func_ = psi_decl_arg_init(
422 psi_decl_type_init(T->type, T->text),
423 psi_decl_var_init(N->text, decl_i, 0)
424 );
425 func_->type->token = T;
426 func_->var->token = N;
427 func_->token = N;
428 def = psi_decl_arg_init(
429 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
430 psi_decl_var_copy(func_->var)
431 );
432 def->var->pointer_level = type_i;
433 def->type->token = psi_token_copy(func_->token);
434 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
435 }
436 decl_abi(abi) ::= NAME(T). {
437 abi = psi_decl_abi_init(T->text);
438 abi->token = T;
439 }
440 decl_var_array_size(as) ::= . {
441 as = NULL;
442 }
443 decl_var_array_size(as) ::= LBRACKET NUMBER(D) RBRACKET. {
444 as = D;
445 }
446 decl_var(var) ::= NAME(T) decl_var_array_size(as). {
447 var = psi_decl_var_init(T->text, 0, as?atol(as->text):0);
448 var->token = T;
449 if (as) {
450 free(as);
451 }
452 }
453 decl_var(var) ::= pointers(p) NAME(T) decl_var_array_size(as). {
454 var = psi_decl_var_init(T->text, p+!!as, as?atol(as->text):0);
455 var->token = T;
456 if (as) {
457 free(as);
458 }
459 }
460 decl_vars(vars) ::= decl_var(var). {
461 vars = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_var_free),
462 &var);
463 }
464 decl_vars(vars) ::= decl_vars(vars_) COMMA decl_var(var). {
465 vars = psi_plist_add(vars_, &var);
466 }
467 decl_arg(arg_) ::= const_decl_type(type) decl_var(var). {
468 arg_ = psi_decl_arg_init(type, var);
469 }
470 decl_typedef_body(def) ::= const_decl_type(type_) indirection(decl_i) LPAREN indirection(type_i) NAME(N) RPAREN decl_typedef_body_fn_args(args). {
471 struct psi_decl_arg *func_ = psi_decl_arg_init(
472 type_,
473 psi_decl_var_init(N->text, decl_i, 0)
474 );
475 func_->var->token = N;
476 func_->token = N;
477 def = psi_decl_arg_init(
478 psi_decl_type_init(PSI_T_FUNCTION, func_->var->name),
479 psi_decl_var_copy(func_->var)
480 );
481 def->var->pointer_level = type_i;
482 def->type->token = psi_token_copy(func_->token);
483 def->type->real.func = psi_decl_init(psi_decl_abi_init("default"), func_, args);
484 }
485 decl_arg(arg_) ::= VOID(T) pointers(p) NAME(N). {
486 arg_ = psi_decl_arg_init(
487 psi_decl_type_init(T->type, T->text),
488 psi_decl_var_init(N->text, p, 0)
489 );
490 arg_->type->token = T;
491 arg_->var->token = N;
492 arg_->token = N;
493 }
494 decl_arg(arg_) ::= CONST VOID(T) pointers(p) NAME(N). {
495 arg_ = psi_decl_arg_init(
496 psi_decl_type_init(T->type, T->text),
497 psi_decl_var_init(N->text, p, 0)
498 );
499 arg_->type->token = T;
500 arg_->var->token = N;
501 arg_->token = N;
502 }
503 decl_args(args) ::= . {
504 args = NULL;
505 }
506 decl_args(args) ::= VOID. {
507 args = NULL;
508 }
509 decl_args(args) ::= decl_arg(arg). {
510 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
511 &arg);
512 }
513 decl_args(args) ::= decl_args(args_) COMMA decl_arg(arg). {
514 args = psi_plist_add(args_, &arg);
515 }
516 struct_args(args) ::= struct_arg(arg). {
517 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_decl_arg_free),
518 &arg);
519 }
520 struct_args(args) ::= struct_args(args_) struct_arg(arg). {
521 args = psi_plist_add(args_, &arg);
522 }
523 struct_arg(arg_) ::= decl_typedef_body_ex(def) EOS. {
524 arg_ = def;
525 switch (def->type->type) {
526 case PSI_T_STRUCT:
527 if (def->type->real.strct) {
528 if (!P->structs) {
529 P->structs = psi_plist_init((psi_plist_dtor) psi_decl_struct_free);
530 }
531 P->structs = psi_plist_add(P->structs, &def->type->real.strct);
532 }
533 break;
534 case PSI_T_UNION:
535 if (def->type->real.unn) {
536 if (!P->unions) {
537 P->unions = psi_plist_init((psi_plist_dtor) psi_decl_union_free);
538 }
539 P->unions = psi_plist_add(P->unions, &def->type->real.unn);
540 }
541 break;
542 case PSI_T_ENUM:
543 if (def->type->real.enm) {
544 if (!P->enums) {
545 P->enums = psi_plist_init((psi_plist_dtor) psi_decl_enum_free);
546 }
547 P->enums = psi_plist_add(P->enums, &def->type->real.enm);
548 }
549 break;
550 }
551 }
552 struct_arg(arg) ::= decl_arg(arg_) decl_layout(layout_) EOS. {
553 arg_->layout = layout_;
554 arg = arg_;
555 }
556 decl_layout(layout) ::= . {
557 layout = NULL;
558 }
559 decl_layout(layout) ::= COLON COLON LPAREN NUMBER(POS) COMMA NUMBER(SIZ) RPAREN. {
560 layout = psi_layout_init(atol(POS->text), atol(SIZ->text));
561 free(POS);
562 free(SIZ);
563 }
564 decl_scalar_type(type_) ::= CHAR(C). {
565 type_ = C;
566 }
567 decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
568 if (s) {
569 type_ = psi_token_cat(2, S, s);
570 free(S);
571 free(s);
572 } else {
573 type_ = S;
574 }
575 }
576 decl_scalar_type_short(s) ::= . {
577 s = NULL;
578 }
579 decl_scalar_type_short(s) ::= INT(I). {
580 s = I;
581 }
582 decl_scalar_type(type_) ::= INT(I). {
583 type_ = I;
584 }
585 decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
586 if (l) {
587 type_ = psi_token_cat(2, L, l);
588 free(L);
589 free(l);
590 } else {
591 type_ = L;
592 }
593 }
594 decl_scalar_type_long(l) ::= . {
595 l = NULL;
596 }
597 decl_scalar_type_long(l) ::= DOUBLE(D). {
598 l = D;
599 }
600 decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
601 if (ll) {
602 l = psi_token_cat(2, L, ll);
603 free(L);
604 free(ll);
605 } else {
606 l = L;
607 }
608 }
609 decl_scalar_type_long_long(ll) ::= . {
610 ll = NULL;
611 }
612 decl_scalar_type_long_long(ll) ::= INT(I). {
613 ll = I;
614 }
615 decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
616 struct psi_token *T = psi_token_cat(2, U, N);
617 type_ = psi_decl_type_init(T->type, T->text);
618 type_->token = T;
619 free(U);
620 free(N);
621 }
622 decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
623 struct psi_token *T = psi_token_cat(2, S, N);
624 type_ = psi_decl_type_init(T->type, T->text);
625 type_->token = T;
626 free(S);
627 free(N);
628 }
629 decl_type(type_) ::= UNSIGNED(U). {
630 type_ = psi_decl_type_init(PSI_T_NAME, U->text);
631 type_->token = U;
632 }
633 decl_type(type_) ::= SIGNED(S). {
634 type_ = psi_decl_type_init(PSI_T_NAME, S->text);
635 type_->token = S;
636 }
637 decl_type(type_) ::= decl_scalar_type(N). {
638 type_ = psi_decl_type_init(N->type, N->text);
639 type_->token = N;
640 }
641 decl_type(type_) ::= STRUCT(S) NAME(T). {
642 type_ = psi_decl_type_init(S->type, T->text);
643 type_->token = T;
644 free(S);
645 }
646 decl_type(type_) ::= UNION(U) NAME(T). {
647 type_ = psi_decl_type_init(U->type, T->text);
648 type_->token = T;
649 free(U);
650 }
651 decl_type(type_) ::= ENUM(E) NAME(T). {
652 type_ = psi_decl_type_init(E->type, T->text);
653 type_->token = T;
654 free(E);
655 }
656 decl_type(type_) ::= decl_type_token(T). {
657 type_ = psi_decl_type_init(T->type, T->text);
658 type_->token = T;
659 }
660 const_decl_type(type) ::= decl_type(type_). {
661 type = type_;
662 }
663 const_decl_type(type) ::= CONST decl_type(type_). {
664 type = type_;
665 }
666 impl(impl) ::= impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
667 impl = psi_impl_init(func, stmts);
668 }
669 impl(impl) ::= STATIC impl_func(func) LBRACE impl_stmts(stmts) RBRACE. {
670 func->static_memory = 1;
671 impl = psi_impl_init(func, stmts);
672 }
673 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) LPAREN RPAREN COLON impl_type(type). {
674 func = psi_impl_func_init(NAME->text, NULL, type);
675 func->token = NAME;
676 func->return_reference = r;
677 }
678 impl_func(func) ::= FUNCTION reference(r) NSNAME(NAME) LPAREN impl_args(args) RPAREN COLON impl_type(type). {
679 func = psi_impl_func_init(NAME->text, args, type);
680 func->token = NAME;
681 func->return_reference = r;
682 }
683 impl_func(func) ::= FUNCTION reference(func_r) NSNAME(NAME) LPAREN impl_args(args) COMMA impl_type(va_type) reference(va_r) ELLIPSIS DOLLAR_NAME(T) RPAREN COLON impl_type(func_type). {
684 func = psi_impl_func_init(NAME->text, args, func_type);
685 func->token = NAME;
686 func->return_reference = func_r;
687 func->vararg = psi_impl_arg_init(va_type, psi_impl_var_init(T->text, va_r), NULL);
688 free(T);
689 }
690 impl_def_val(def) ::= impl_def_val_token(T). {
691 def = psi_impl_def_val_init(T->type, T->text);
692 def->token = T;
693 }
694 impl_var(var) ::= reference(r) DOLLAR_NAME(T). {
695 var = psi_impl_var_init(T->text, r);
696 var->token = T;
697 }
698 impl_type(type_) ::= impl_type_token(T). {
699 type_ = psi_impl_type_init(T->type, T->text);
700 free(T);
701 }
702 impl_arg(arg) ::= impl_type(type) impl_var(var). {
703 arg = psi_impl_arg_init(type, var, NULL);
704 }
705 impl_arg(arg) ::= impl_type(type) impl_var(var) EQUALS impl_def_val(def). {
706 arg = psi_impl_arg_init(type, var, def);
707 }
708 impl_args(args) ::= impl_arg(arg). {
709 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_arg_free),
710 &arg);
711 }
712 impl_args(args) ::= impl_args(args_) COMMA impl_arg(arg). {
713 args = psi_plist_add(args_, &arg);
714 }
715 impl_stmts(stmts) ::= impl_stmt(stmt). {
716 stmts = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_impl_stmt_free),
717 &stmt);
718 }
719 impl_stmts(stmts) ::= impl_stmts(stmts_) impl_stmt(stmt). {
720 stmts = psi_plist_add(stmts_, &stmt);
721 }
722 impl_stmt(i) ::= return_stmt(r). {
723 i = (struct psi_token**) r;
724 }
725 impl_stmt(i) ::= let_stmt(l). {
726 i = (struct psi_token**) l;
727 }
728 impl_stmt(i) ::= set_stmt(s). {
729 i = (struct psi_token**) s;
730 }
731 impl_stmt(i) ::= free_stmt(f). {
732 i = (struct psi_token**) f;
733 }
734 number(exp) ::= number_token(tok). {
735 exp = psi_number_init(tok->type, tok->text);
736 exp->token = tok;
737 }
738 number(exp) ::= decl_var(var). {
739 exp = psi_number_init(PSI_T_NAME, var);
740 exp->token = psi_token_copy(var->token);
741 }
742 num_exp(exp) ::= number(num). {
743 exp = psi_num_exp_init_num(num);
744 exp->token = psi_token_copy(num->token);
745 }
746 num_exp(exp) ::= LPAREN(L) num_exp(exp_) RPAREN. {
747 exp = psi_num_exp_init_unary(PSI_T_LPAREN, exp_);
748 exp->token = L;
749 }
750 num_exp(exp) ::= num_exp(lhs_) num_exp_binary_op_token(OP) num_exp(rhs_). {
751 exp = psi_num_exp_init_binary(OP->type, lhs_, rhs_);
752 exp->token = OP;
753 }
754 num_exp(exp) ::= num_exp_unary_op_token(OP) num_exp(exp_). {
755 exp = psi_num_exp_init_unary(OP->type, exp_);
756 exp->token = OP;
757 }
758 let_exp(val) ::= NULL. {
759 val = psi_let_exp_init(PSI_LET_NULL, NULL);
760 }
761 let_exp(val) ::= AMPERSAND NULL. {
762 val = psi_let_exp_init(PSI_LET_NULL, NULL);
763 val->is_reference = 1;
764 }
765 let_exp(val) ::= let_callback(cb). {
766 val = psi_let_exp_init(PSI_LET_CALLBACK, cb);
767 }
768 let_exp(val) ::= let_calloc(ca). {
769 val = psi_let_exp_init(PSI_LET_CALLOC, ca);
770 }
771 let_exp(val) ::= AMPERSAND let_calloc(ca). {
772 val = psi_let_exp_init(PSI_LET_CALLOC, ca);
773 val->is_reference = 1;
774 }
775 let_exp(val) ::= let_func(fn). {
776 val = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, fn);
777 }
778 let_exp(val) ::= AMPERSAND let_func(fn). {
779 val = psi_let_exp_init_ex(NULL, PSI_LET_FUNC, fn);
780 val->is_reference = 1;
781 }
782 let_exp(val) ::= num_exp(exp). {
783 val = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, exp);
784 }
785 let_exp(val) ::= AMPERSAND num_exp(exp). {
786 val = psi_let_exp_init_ex(NULL, PSI_LET_NUMEXP, exp);
787 val->is_reference = 1;
788 }
789 let_exp(exp) ::= decl_var(var_) EQUALS let_exp(val). {
790 exp = val;
791 exp->var = var_;
792 }
793 let_stmt(let) ::= LET(T) let_exp(val) EOS. {
794 let = psi_let_stmt_init(val);
795 let->token = T;
796 }
797 let_stmt(let) ::= TEMP(T) decl_var(var) EQUALS reference(r) decl_var(val_) EOS. {
798 let = psi_let_stmt_init(psi_let_exp_init_ex(var, PSI_LET_TMP, val_));
799 let->token = T;
800 let->exp->is_reference = r ? 1 : 0;
801 }
802 let_callback(cb) ::= CALLBACK(T) callback_rval(F) LPAREN impl_var(var) LPAREN callback_arg_list(args_) RPAREN RPAREN. {
803 cb = psi_let_callback_init(psi_let_func_init(F->type, F->text, var), args_);
804 cb->token = T;
805 free(F);
806 }
807 let_calloc(alloc) ::= CALLOC(T) LPAREN num_exp(nmemb) COMMA num_exp(size) RPAREN. {
808 alloc = psi_let_calloc_init(nmemb, size);
809 alloc->token = T;
810 }
811 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) RPAREN. {
812 func = psi_let_func_init(T->type, T->text, var);
813 func->token = T;
814 }
815 let_func(func) ::= let_func_token(T) LPAREN impl_var(var) COMMA let_exps(vals) RPAREN. {
816 func = psi_let_func_init(T->type, T->text, var);
817 func->token = T;
818 func->inner = vals;
819 }
820 let_exps(exps) ::= let_exp(exp). {
821 exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_let_exp_free),
822 &exp);
823 }
824 let_exps(exps) ::= let_exps(exps_) COMMA let_exp(exp). {
825 exps = psi_plist_add(exps_, &exp);
826 }
827 callback_arg_list(args) ::= . {
828 args = NULL;
829 }
830 callback_arg_list(args) ::= callback_args(args_). {
831 args = args_;
832 }
833 callback_args(args) ::= set_exp(val). {
834 args = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free),
835 &val);
836 }
837 callback_args(args) ::= callback_args(args_) COMMA set_exp(val). {
838 args = psi_plist_add(args_, &val);
839 }
840 callback_rval(rval) ::= let_func_token(F). {
841 rval = F;
842 }
843 callback_rval(rval) ::= VOID(V). {
844 rval = V;
845 }
846 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) RPAREN. {
847 func = psi_set_func_init(T->type, T->text, var);
848 func->token = T;
849 }
850 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) COMMA set_exps(vals) RPAREN. {
851 func = psi_set_func_init(T->type, T->text, var);
852 func->token = T;
853 func->inner = vals;
854 }
855 set_func(func) ::= set_func_token(T) LPAREN decl_var(var) COMMA ELLIPSIS RPAREN. {
856 func = psi_set_func_init(T->type, T->text, var);
857 func->token = T;
858 func->recursive = 1;
859 }
860 set_exp(val) ::= set_func(fn). {
861 val = psi_set_exp_init(PSI_SET_FUNC, fn);
862 }
863 set_exp(val) ::= num_exp(num). {
864 val = psi_set_exp_init(PSI_SET_NUMEXP, num);
865 }
866 set_exps(exps) ::= set_exp(exp). {
867 exps = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_set_exp_free),
868 &exp);
869 }
870 set_exps(exps) ::= set_exps(exps_) COMMA set_exp(exp). {
871 exps = psi_plist_add(exps_, &exp);
872 }
873 set_exp(exp) ::= impl_var(var_) EQUALS set_exp(val). {
874 exp = val;
875 exp->var = var_;
876 }
877 set_stmt(set) ::= SET(T) set_exp(exp) EOS. {
878 set = psi_set_stmt_init(exp);
879 set->token = T;
880 }
881 return_stmt(ret) ::= RETURN(T) set_func(func) EOS. {
882 ret = psi_return_stmt_init(psi_set_exp_init(PSI_SET_FUNC, func));
883 ret->token = T;
884 }
885 free_stmt(free) ::= FREE(T) free_exps(calls) EOS. {
886 free = psi_free_stmt_init(calls);
887 free->token = T;
888 }
889 free_exps(calls) ::= free_exp(call). {
890 calls = psi_plist_add(psi_plist_init((psi_plist_dtor) psi_free_exp_free),
891 &call);
892 }
893 free_exps(calls) ::= free_exps(calls_) COMMA free_exp(call). {
894 calls = psi_plist_add(calls_, &call);
895 }
896 free_exp(call) ::= NAME(F) LPAREN decl_vars(vars) RPAREN. {
897 call = psi_free_exp_init(F->text, vars);
898 call->token = F;
899 }
900 reference(r) ::= . {
901 r = false;
902 }
903 reference(r) ::= AMPERSAND. {
904 r = true;
905 }
906 indirection(i) ::= .{
907 i = 0;
908 }
909 indirection(i) ::= pointers(p). {
910 i = p;
911 }
912 pointers(p) ::= ASTERISK. {
913 p = 1;
914 }
915 pointers(p) ::= pointers(p_) ASTERISK. {
916 p = p_+1;
917 }