Fixed another regression in PlaySound.
[wine.git] / tools / wrc / ppy.y
blob099a12ba734540313e62a8b21cff74348bda9a1c
1 /*
2 * Wrc preprocessor syntax analysis
4 * Copyright 1999-2000 Bertho A. Stultiens (BS)
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * History:
22 * 24-Apr-2000 BS Restructured the lot to fit the new scanner
23 * and reintegrate into the wine-tree.
24 * 01-Jan-2000 BS FIXME: win16 preprocessor calculates with
25 * 16 bit ints and overflows...?
26 * 26-Dec-1999 BS Started this file
31 #include "config.h"
32 #include "wine/port.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <assert.h>
38 #include <ctype.h>
39 #include <string.h>
41 #include "utils.h"
42 #include "newstruc.h"
43 #include "wrc.h"
44 #include "preproc.h"
47 #define UNARY_OP(r, v, OP) \
48 switch(v.type) \
49 { \
50 case cv_sint: r.val.si = OP v.val.si; break; \
51 case cv_uint: r.val.ui = OP v.val.ui; break; \
52 case cv_slong: r.val.sl = OP v.val.sl; break; \
53 case cv_ulong: r.val.ul = OP v.val.ul; break; \
54 case cv_sll: r.val.sll = OP v.val.sll; break; \
55 case cv_ull: r.val.ull = OP v.val.ull; break; \
58 #define cv_signed(v) ((v.type & FLAG_SIGNED) != 0)
60 #define BIN_OP_INT(r, v1, v2, OP) \
61 r.type = v1.type; \
62 if(cv_signed(v1) && cv_signed(v2)) \
63 r.val.si = v1.val.si OP v2.val.si; \
64 else if(cv_signed(v1) && !cv_signed(v2)) \
65 r.val.si = v1.val.si OP v2.val.ui; \
66 else if(!cv_signed(v1) && cv_signed(v2)) \
67 r.val.ui = v1.val.ui OP v2.val.si; \
68 else \
69 r.val.ui = v1.val.ui OP v2.val.ui;
71 #define BIN_OP_LONG(r, v1, v2, OP) \
72 r.type = v1.type; \
73 if(cv_signed(v1) && cv_signed(v2)) \
74 r.val.sl = v1.val.sl OP v2.val.sl; \
75 else if(cv_signed(v1) && !cv_signed(v2)) \
76 r.val.sl = v1.val.sl OP v2.val.ul; \
77 else if(!cv_signed(v1) && cv_signed(v2)) \
78 r.val.ul = v1.val.ul OP v2.val.sl; \
79 else \
80 r.val.ul = v1.val.ul OP v2.val.ul;
82 #define BIN_OP_LONGLONG(r, v1, v2, OP) \
83 r.type = v1.type; \
84 if(cv_signed(v1) && cv_signed(v2)) \
85 r.val.sll = v1.val.sll OP v2.val.sll; \
86 else if(cv_signed(v1) && !cv_signed(v2)) \
87 r.val.sll = v1.val.sll OP v2.val.ull; \
88 else if(!cv_signed(v1) && cv_signed(v2)) \
89 r.val.ull = v1.val.ull OP v2.val.sll; \
90 else \
91 r.val.ull = v1.val.ull OP v2.val.ull;
93 #define BIN_OP(r, v1, v2, OP) \
94 switch(v1.type & SIZE_MASK) \
95 { \
96 case SIZE_INT: BIN_OP_INT(r, v1, v2, OP); break; \
97 case SIZE_LONG: BIN_OP_LONG(r, v1, v2, OP); break; \
98 case SIZE_LONGLONG: BIN_OP_LONGLONG(r, v1, v2, OP); break; \
99 default: internal_error(__FILE__, __LINE__, "Invalid type indicator (0x%04x)", v1.type); \
104 * Prototypes
106 static int boolean(cval_t *v);
107 static void promote_equal_size(cval_t *v1, cval_t *v2);
108 static void cast_to_sint(cval_t *v);
109 static void cast_to_uint(cval_t *v);
110 static void cast_to_slong(cval_t *v);
111 static void cast_to_ulong(cval_t *v);
112 static void cast_to_sll(cval_t *v);
113 static void cast_to_ull(cval_t *v);
114 static marg_t *new_marg(char *str, def_arg_t type);
115 static marg_t *add_new_marg(char *str, def_arg_t type);
116 static int marg_index(char *id);
117 static mtext_t *new_mtext(char *str, int idx, def_exp_t type);
118 static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp);
119 static char *merge_text(char *s1, char *s2);
122 * Local variables
124 static marg_t **macro_args; /* Macro parameters array while parsing */
125 static int nmacro_args;
129 %union{
130 int sint;
131 unsigned int uint;
132 long slong;
133 unsigned long ulong;
134 wrc_sll_t sll;
135 wrc_ull_t ull;
136 int *iptr;
137 char *cptr;
138 cval_t cval;
139 marg_t *marg;
140 mtext_t *mtext;
143 %token tRCINCLUDE
144 %token tIF tIFDEF tIFNDEF tELSE tELIF tENDIF tDEFINED tNL
145 %token tINCLUDE tLINE tGCCLINE tERROR tWARNING tPRAGMA tPPIDENT
146 %token tUNDEF tMACROEND tCONCAT tELIPSIS tSTRINGIZE
147 %token <cptr> tIDENT tLITERAL tMACRO tDEFINE
148 %token <cptr> tDQSTRING tSQSTRING tIQSTRING
149 %token <uint> tUINT
150 %token <sint> tSINT
151 %token <ulong> tULONG
152 %token <slong> tSLONG
153 %token <ull> tULONGLONG
154 %token <sll> tSLONGLONG
155 %token <cptr> tRCINCLUDEPATH
157 %right '?' ':'
158 %left tLOGOR
159 %left tLOGAND
160 %left '|'
161 %left '^'
162 %left '&'
163 %left tEQ tNE
164 %left '<' tLTE '>' tGTE
165 %left tLSHIFT tRSHIFT
166 %left '+' '-'
167 %left '*' '/'
168 %right '~' '!'
170 %type <cval> pp_expr
171 %type <marg> emargs margs
172 %type <mtext> opt_mtexts mtexts mtext
173 %type <sint> allmargs
174 %type <cptr> opt_text text
177 **************************************************************************
178 * The parser starts here
179 **************************************************************************
184 pp_file : /* Empty */
185 | pp_file preprocessor
188 preprocessor
189 : tINCLUDE tDQSTRING tNL { do_include($2, 1); }
190 | tINCLUDE tIQSTRING tNL { do_include($2, 0); }
191 | tIF pp_expr tNL { next_if_state(boolean(&$2)); }
192 | tIFDEF tIDENT tNL { next_if_state(pplookup($2) != NULL); free($2); }
193 | tIFNDEF tIDENT tNL {
194 int t = pplookup($2) == NULL;
195 if(include_state == 0 && t && !seen_junk)
197 include_state = 1;
198 include_ppp = $2;
199 include_ifdepth = get_if_depth();
201 else if(include_state != 1)
203 include_state = -1;
204 free($2);
206 else
207 free($2);
208 next_if_state(t);
209 if(debuglevel & DEBUGLEVEL_PPMSG)
210 fprintf(stderr, "tIFNDEF: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n", input_name, line_number, include_state, include_ppp, include_ifdepth);
212 | tELIF pp_expr tNL {
213 if_state_t s = pop_if();
214 switch(s)
216 case if_true:
217 case if_elif:
218 push_if(if_elif);
219 break;
220 case if_false:
221 push_if(boolean(&$2) ? if_true : if_false);
222 break;
223 case if_ignore:
224 push_if(if_ignore);
225 break;
226 case if_elsetrue:
227 case if_elsefalse:
228 pperror("#elif cannot follow #else");
229 default:
230 internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #elif directive", s);
233 | tELSE tNL {
234 if_state_t s = pop_if();
235 switch(s)
237 case if_true:
238 push_if(if_elsefalse);
239 break;
240 case if_elif:
241 push_if(if_elif);
242 break;
243 case if_false:
244 push_if(if_elsetrue);
245 break;
246 case if_ignore:
247 push_if(if_ignore);
248 break;
249 case if_elsetrue:
250 case if_elsefalse:
251 pperror("#else clause already defined");
252 default:
253 internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #else directive", s);
256 | tENDIF tNL {
257 pop_if();
258 if(include_ifdepth == get_if_depth() && include_state == 1)
260 include_state = 2;
261 seen_junk = 0;
263 else if(include_state != 1)
265 include_state = -1;
267 if(debuglevel & DEBUGLEVEL_PPMSG)
268 fprintf(stderr, "tENDIF: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n", input_name, line_number, include_state, include_ppp, include_ifdepth);
270 | tUNDEF tIDENT tNL { del_define($2); free($2); }
271 | tDEFINE opt_text tNL { add_define($1, $2); }
272 | tMACRO res_arg allmargs tMACROEND opt_mtexts tNL {
273 add_macro($1, macro_args, nmacro_args, $5);
275 | tLINE tSINT tDQSTRING tNL { fprintf(ppout, "# %d %s\n", $2 , $3); free($3); }
276 | tGCCLINE tSINT tDQSTRING tNL { fprintf(ppout, "# %d %s\n", $2 , $3); free($3); }
277 | tGCCLINE tSINT tDQSTRING tSINT tNL
278 { fprintf(ppout, "# %d %s %d\n", $2, $3, $4); free($3); }
279 | tGCCLINE tSINT tDQSTRING tSINT tSINT tNL
280 { fprintf(ppout, "# %d %s %d %d\n", $2 ,$3, $4, $5); free($3); }
281 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tNL
282 { fprintf(ppout, "# %d %s %d %d %d\n", $2 ,$3 ,$4 ,$5, $6); free($3); }
283 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tSINT tNL
284 { fprintf(ppout, "# %d %s %d %d %d %d\n", $2 ,$3 ,$4 ,$5, $6, $7); free($3); }
285 | tGCCLINE tNL /* The null-token */
286 | tERROR opt_text tNL { pperror("#error directive: '%s'", $2); if($2) free($2); }
287 | tWARNING opt_text tNL { ppwarning("#warning directive: '%s'", $2); if($2) free($2); }
288 | tPRAGMA opt_text tNL { if(pedantic) ppwarning("#pragma ignored (arg: '%s')", $2); if($2) free($2); }
289 | tPPIDENT opt_text tNL { if(pedantic) ppwarning("#ident ignored (arg: '%s')", $2); if($2) free($2); }
290 | tRCINCLUDE tRCINCLUDEPATH {
291 int nl=strlen($2) +3;
292 char *fn=xmalloc(nl);
293 snprintf(fn,nl,"\"%s\"",$2);
294 free($2);
295 do_include(fn,1);
297 | tRCINCLUDE tDQSTRING {
298 do_include($2,1);
300 /*| tNL*/
303 opt_text: /* Empty */ { $$ = NULL; }
304 | text { $$ = $1; }
307 text : tLITERAL { $$ = $1; }
308 | tDQSTRING { $$ = $1; }
309 | tSQSTRING { $$ = $1; }
310 | text tLITERAL { $$ = merge_text($1, $2); }
311 | text tDQSTRING { $$ = merge_text($1, $2); }
312 | text tSQSTRING { $$ = merge_text($1, $2); }
315 res_arg : /* Empty */ { macro_args = NULL; nmacro_args = 0; }
318 allmargs: /* Empty */ { $$ = 0; macro_args = NULL; nmacro_args = 0; }
319 | emargs { $$ = nmacro_args; }
322 emargs : margs { $$ = $1; }
323 | margs ',' tELIPSIS { $$ = add_new_marg(NULL, arg_list); nmacro_args *= -1; }
326 margs : margs ',' tIDENT { $$ = add_new_marg($3, arg_single); }
327 | tIDENT { $$ = add_new_marg($1, arg_single); }
330 opt_mtexts
331 : /* Empty */ { $$ = NULL; }
332 | mtexts {
333 for($$ = $1; $$ && $$->prev; $$ = $$->prev)
338 mtexts : mtext { $$ = $1; }
339 | mtexts mtext { $$ = combine_mtext($1, $2); }
342 mtext : tLITERAL { $$ = new_mtext($1, 0, exp_text); }
343 | tDQSTRING { $$ = new_mtext($1, 0, exp_text); }
344 | tSQSTRING { $$ = new_mtext($1, 0, exp_text); }
345 | tCONCAT { $$ = new_mtext(NULL, 0, exp_concat); }
346 | tSTRINGIZE tIDENT {
347 int mat = marg_index($2);
348 if(mat < 0)
349 pperror("Stringification identifier must be an argument parameter");
350 $$ = new_mtext(NULL, mat, exp_stringize);
352 | tIDENT {
353 int mat = marg_index($1);
354 if(mat >= 0)
355 $$ = new_mtext(NULL, mat, exp_subst);
356 else
357 $$ = new_mtext($1, 0, exp_text);
361 pp_expr : tSINT { $$.type = cv_sint; $$.val.si = $1; }
362 | tUINT { $$.type = cv_uint; $$.val.ui = $1; }
363 | tSLONG { $$.type = cv_slong; $$.val.sl = $1; }
364 | tULONG { $$.type = cv_ulong; $$.val.ul = $1; }
365 | tSLONGLONG { $$.type = cv_sll; $$.val.sl = $1; }
366 | tULONGLONG { $$.type = cv_ull; $$.val.ul = $1; }
367 | tDEFINED tIDENT { $$.type = cv_sint; $$.val.si = pplookup($2) != NULL; }
368 | tDEFINED '(' tIDENT ')' { $$.type = cv_sint; $$.val.si = pplookup($3) != NULL; }
369 | tIDENT { $$.type = cv_sint; $$.val.si = 0; }
370 | pp_expr tLOGOR pp_expr { $$.type = cv_sint; $$.val.si = boolean(&$1) || boolean(&$3); }
371 | pp_expr tLOGAND pp_expr { $$.type = cv_sint; $$.val.si = boolean(&$1) && boolean(&$3); }
372 | pp_expr tEQ pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, ==) }
373 | pp_expr tNE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, !=) }
374 | pp_expr '<' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <) }
375 | pp_expr '>' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >) }
376 | pp_expr tLTE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <=) }
377 | pp_expr tGTE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >=) }
378 | pp_expr '+' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, +) }
379 | pp_expr '-' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, -) }
380 | pp_expr '^' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, ^) }
381 | pp_expr '&' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, &) }
382 | pp_expr '|' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, |) }
383 | pp_expr '*' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, *) }
384 | pp_expr '/' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, /) }
385 | pp_expr tLSHIFT pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <<) }
386 | pp_expr tRSHIFT pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >>) }
387 | '+' pp_expr { $$ = $2; }
388 | '-' pp_expr { UNARY_OP($$, $2, -) }
389 | '~' pp_expr { UNARY_OP($$, $2, ~) }
390 | '!' pp_expr { $$.type = cv_sint; $$.val.si = !boolean(&$2); }
391 | '(' pp_expr ')' { $$ = $2; }
392 | pp_expr '?' pp_expr ':' pp_expr { $$ = boolean(&$1) ? $3 : $5; }
398 **************************************************************************
399 * Support functions
400 **************************************************************************
403 static void cast_to_sint(cval_t *v)
405 switch(v->type)
407 case cv_sint: break;
408 case cv_uint: break;
409 case cv_slong: v->val.si = v->val.sl; break;
410 case cv_ulong: v->val.si = v->val.ul; break;
411 case cv_sll: v->val.si = v->val.sll; break;
412 case cv_ull: v->val.si = v->val.ull; break;
414 v->type = cv_sint;
417 static void cast_to_uint(cval_t *v)
419 switch(v->type)
421 case cv_sint: break;
422 case cv_uint: break;
423 case cv_slong: v->val.ui = v->val.sl; break;
424 case cv_ulong: v->val.ui = v->val.ul; break;
425 case cv_sll: v->val.ui = v->val.sll; break;
426 case cv_ull: v->val.ui = v->val.ull; break;
428 v->type = cv_uint;
431 static void cast_to_slong(cval_t *v)
433 switch(v->type)
435 case cv_sint: v->val.sl = v->val.si; break;
436 case cv_uint: v->val.sl = v->val.ui; break;
437 case cv_slong: break;
438 case cv_ulong: break;
439 case cv_sll: v->val.sl = v->val.sll; break;
440 case cv_ull: v->val.sl = v->val.ull; break;
442 v->type = cv_slong;
445 static void cast_to_ulong(cval_t *v)
447 switch(v->type)
449 case cv_sint: v->val.ul = v->val.si; break;
450 case cv_uint: v->val.ul = v->val.ui; break;
451 case cv_slong: break;
452 case cv_ulong: break;
453 case cv_sll: v->val.ul = v->val.sll; break;
454 case cv_ull: v->val.ul = v->val.ull; break;
456 v->type = cv_ulong;
459 static void cast_to_sll(cval_t *v)
461 switch(v->type)
463 case cv_sint: v->val.sll = v->val.si; break;
464 case cv_uint: v->val.sll = v->val.ui; break;
465 case cv_slong: v->val.sll = v->val.sl; break;
466 case cv_ulong: v->val.sll = v->val.ul; break;
467 case cv_sll: break;
468 case cv_ull: break;
470 v->type = cv_sll;
473 static void cast_to_ull(cval_t *v)
475 switch(v->type)
477 case cv_sint: v->val.ull = v->val.si; break;
478 case cv_uint: v->val.ull = v->val.ui; break;
479 case cv_slong: v->val.ull = v->val.sl; break;
480 case cv_ulong: v->val.ull = v->val.ul; break;
481 case cv_sll: break;
482 case cv_ull: break;
484 v->type = cv_ull;
488 static void promote_equal_size(cval_t *v1, cval_t *v2)
490 #define cv_sizeof(v) ((int)(v->type & SIZE_MASK))
491 int s1 = cv_sizeof(v1);
492 int s2 = cv_sizeof(v2);
493 #undef cv_sizeof
495 if(s1 == s2)
496 return;
497 else if(s1 > s2)
499 switch(v1->type)
501 case cv_sint: cast_to_sint(v2); break;
502 case cv_uint: cast_to_uint(v2); break;
503 case cv_slong: cast_to_slong(v2); break;
504 case cv_ulong: cast_to_ulong(v2); break;
505 case cv_sll: cast_to_sll(v2); break;
506 case cv_ull: cast_to_ull(v2); break;
509 else
511 switch(v2->type)
513 case cv_sint: cast_to_sint(v1); break;
514 case cv_uint: cast_to_uint(v1); break;
515 case cv_slong: cast_to_slong(v1); break;
516 case cv_ulong: cast_to_ulong(v1); break;
517 case cv_sll: cast_to_sll(v1); break;
518 case cv_ull: cast_to_ull(v1); break;
524 static int boolean(cval_t *v)
526 switch(v->type)
528 case cv_sint: return v->val.si != (int)0;
529 case cv_uint: return v->val.ui != (unsigned int)0;
530 case cv_slong: return v->val.sl != (long)0;
531 case cv_ulong: return v->val.ul != (unsigned long)0;
532 case cv_sll: return v->val.sll != (wrc_sll_t)0;
533 case cv_ull: return v->val.ull != (wrc_ull_t)0;
535 return 0;
538 static marg_t *new_marg(char *str, def_arg_t type)
540 marg_t *ma = (marg_t *)xmalloc(sizeof(marg_t));
541 ma->arg = str;
542 ma->type = type;
543 return ma;
546 static marg_t *add_new_marg(char *str, def_arg_t type)
548 marg_t *ma = new_marg(str, type);
549 nmacro_args++;
550 macro_args = (marg_t **)xrealloc(macro_args, nmacro_args * sizeof(macro_args[0]));
551 macro_args[nmacro_args-1] = ma;
552 return ma;
555 static int marg_index(char *id)
557 int t;
558 for(t = 0; t < nmacro_args; t++)
560 if(!strcmp(id, macro_args[t]->arg))
561 break;
563 return t < nmacro_args ? t : -1;
566 static mtext_t *new_mtext(char *str, int idx, def_exp_t type)
568 mtext_t *mt = (mtext_t *)xmalloc(sizeof(mtext_t));
569 if(str == NULL)
570 mt->subst.argidx = idx;
571 else
572 mt->subst.text = str;
573 mt->type = type;
574 return mt;
577 static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
579 if(!tail)
580 return mtp;
582 if(!mtp)
583 return tail;
585 if(tail->type == exp_text && mtp->type == exp_text)
587 tail->subst.text = xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
588 strcat(tail->subst.text, mtp->subst.text);
589 free(mtp->subst.text);
590 free(mtp);
591 return tail;
594 if(tail->type == exp_concat && mtp->type == exp_concat)
596 free(mtp);
597 return tail;
600 if(tail->type == exp_concat && mtp->type == exp_text)
602 int len = strlen(mtp->subst.text);
603 while(len)
605 /* FIXME: should delete space from head of string */
606 if(isspace(mtp->subst.text[len-1] & 0xff))
607 mtp->subst.text[--len] = '\0';
608 else
609 break;
612 if(!len)
614 free(mtp->subst.text);
615 free(mtp);
616 return tail;
620 if(tail->type == exp_text && mtp->type == exp_concat)
622 int len = strlen(tail->subst.text);
623 while(len)
625 if(isspace(tail->subst.text[len-1] & 0xff))
626 tail->subst.text[--len] = '\0';
627 else
628 break;
631 if(!len)
633 mtp->prev = tail->prev;
634 mtp->next = tail->next;
635 if(tail->prev)
636 tail->prev->next = mtp;
637 free(tail->subst.text);
638 free(tail);
639 return mtp;
643 tail->next = mtp;
644 mtp->prev = tail;
646 return mtp;
649 static char *merge_text(char *s1, char *s2)
651 int l1 = strlen(s1);
652 int l2 = strlen(s2);
653 s1 = xrealloc(s1, l1+l2+1);
654 memcpy(s1+l1, s2, l2+1);
655 free(s2);
656 return s1;