mf: Subscribe standard quality manager to clock state change events.
[wine/zf.git] / libs / wpp / ppy.y
blob5b7083b3813bf7911d8ae98668d04456373ad0cb
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "wpp_private.h"
44 #define UNARY_OP(r, v, OP) \
45 switch(v.type) \
46 { \
47 case cv_sint: r.val.si = OP v.val.si; break; \
48 case cv_uint: r.val.ui = OP v.val.ui; break; \
49 case cv_slong: r.val.sl = OP v.val.sl; break; \
50 case cv_ulong: r.val.ul = OP v.val.ul; break; \
51 case cv_sll: r.val.sll = OP v.val.sll; break; \
52 case cv_ull: r.val.ull = OP v.val.ull; break; \
55 #define cv_signed(v) ((v.type & FLAG_SIGNED) != 0)
57 #define BIN_OP_INT(r, v1, v2, OP) \
58 r.type = v1.type; \
59 if(cv_signed(v1) && cv_signed(v2)) \
60 r.val.si = v1.val.si OP v2.val.si; \
61 else if(cv_signed(v1) && !cv_signed(v2)) \
62 r.val.si = v1.val.si OP (signed) v2.val.ui; \
63 else if(!cv_signed(v1) && cv_signed(v2)) \
64 r.val.si = (signed) v1.val.ui OP v2.val.si; \
65 else \
66 r.val.ui = v1.val.ui OP v2.val.ui;
68 #define BIN_OP_LONG(r, v1, v2, OP) \
69 r.type = v1.type; \
70 if(cv_signed(v1) && cv_signed(v2)) \
71 r.val.sl = v1.val.sl OP v2.val.sl; \
72 else if(cv_signed(v1) && !cv_signed(v2)) \
73 r.val.sl = v1.val.sl OP (signed long) v2.val.ul; \
74 else if(!cv_signed(v1) && cv_signed(v2)) \
75 r.val.sl = (signed long) v1.val.ul OP v2.val.sl; \
76 else \
77 r.val.ul = v1.val.ul OP v2.val.ul;
79 #define BIN_OP_LONGLONG(r, v1, v2, OP) \
80 r.type = v1.type; \
81 if(cv_signed(v1) && cv_signed(v2)) \
82 r.val.sll = v1.val.sll OP v2.val.sll; \
83 else if(cv_signed(v1) && !cv_signed(v2)) \
84 r.val.sll = v1.val.sll OP (__int64) v2.val.ull; \
85 else if(!cv_signed(v1) && cv_signed(v2)) \
86 r.val.sll = (__int64) v1.val.ull OP v2.val.sll; \
87 else \
88 r.val.ull = v1.val.ull OP v2.val.ull;
90 #define BIN_OP(r, v1, v2, OP) \
91 switch(v1.type & SIZE_MASK) \
92 { \
93 case SIZE_INT: BIN_OP_INT(r, v1, v2, OP); break; \
94 case SIZE_LONG: BIN_OP_LONG(r, v1, v2, OP); break; \
95 case SIZE_LONGLONG: BIN_OP_LONGLONG(r, v1, v2, OP); break; \
96 default: pp_internal_error(__FILE__, __LINE__, "Invalid type indicator (0x%04x)", v1.type); \
101 * Prototypes
103 static int boolean(cval_t *v);
104 static void promote_equal_size(cval_t *v1, cval_t *v2);
105 static void cast_to_sint(cval_t *v);
106 static void cast_to_uint(cval_t *v);
107 static void cast_to_slong(cval_t *v);
108 static void cast_to_ulong(cval_t *v);
109 static void cast_to_sll(cval_t *v);
110 static void cast_to_ull(cval_t *v);
111 static char *add_new_marg(char *str);
112 static int marg_index(char *id);
113 static mtext_t *new_mtext(char *str, int idx, def_exp_t type);
114 static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp);
115 static char *merge_text(char *s1, char *s2);
118 * Local variables
120 static char **macro_args; /* Macro parameters array while parsing */
121 static int nmacro_args;
125 %union{
126 int sint;
127 unsigned int uint;
128 long slong;
129 unsigned long ulong;
130 __int64 sll;
131 unsigned __int64 ull;
132 int *iptr;
133 char *cptr;
134 cval_t cval;
135 char *marg;
136 mtext_t *mtext;
139 %token tRCINCLUDE
140 %token tIF tIFDEF tIFNDEF tELSE tELIF tENDIF tDEFINED tNL
141 %token tINCLUDE tLINE tGCCLINE tERROR tWARNING tPRAGMA tPPIDENT
142 %token tUNDEF tMACROEND tCONCAT tELLIPSIS tSTRINGIZE
143 %token <cptr> tIDENT tLITERAL tMACRO tDEFINE
144 %token <cptr> tDQSTRING tSQSTRING tIQSTRING
145 %token <uint> tUINT
146 %token <sint> tSINT
147 %token <ulong> tULONG
148 %token <slong> tSLONG
149 %token <ull> tULONGLONG
150 %token <sll> tSLONGLONG
151 %token <cptr> tRCINCLUDEPATH
153 %right '?' ':'
154 %left tLOGOR
155 %left tLOGAND
156 %left '|'
157 %left '^'
158 %left '&'
159 %left tEQ tNE
160 %left '<' tLTE '>' tGTE
161 %left tLSHIFT tRSHIFT
162 %left '+' '-'
163 %left '*' '/'
164 %right '~' '!'
166 %type <cval> pp_expr
167 %type <marg> emargs margs
168 %type <mtext> opt_mtexts mtexts mtext
169 %type <sint> allmargs
170 %type <cptr> opt_text text
173 **************************************************************************
174 * The parser starts here
175 **************************************************************************
180 pp_file : /* Empty */
181 | pp_file preprocessor
184 preprocessor
185 : tINCLUDE tDQSTRING tNL { pp_do_include($2, 1); }
186 | tINCLUDE tIQSTRING tNL { pp_do_include($2, 0); }
187 | tIF pp_expr tNL { pp_next_if_state(boolean(&$2)); }
188 | tIFDEF tIDENT tNL { pp_next_if_state(pplookup($2) != NULL); free($2); }
189 | tIFNDEF tIDENT tNL {
190 int t = pplookup($2) == NULL;
191 if(pp_incl_state.state == 0 && t && !pp_incl_state.seen_junk)
193 pp_incl_state.state = 1;
194 pp_incl_state.ppp = $2;
195 pp_incl_state.ifdepth = pp_get_if_depth();
197 else if(pp_incl_state.state != 1)
199 pp_incl_state.state = -1;
200 free($2);
202 else
203 free($2);
204 pp_next_if_state(t);
205 if(pp_status.debug)
206 fprintf(stderr, "tIFNDEF: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n",
207 pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ppp, pp_incl_state.ifdepth);
209 | tELIF pp_expr tNL {
210 pp_if_state_t s = pp_pop_if();
211 switch(s)
213 case if_true:
214 case if_elif:
215 pp_push_if(if_elif);
216 break;
217 case if_false:
218 pp_push_if(boolean(&$2) ? if_true : if_false);
219 break;
220 case if_ignore:
221 pp_push_if(if_ignore);
222 break;
223 case if_elsetrue:
224 case if_elsefalse:
225 ppy_error("#elif cannot follow #else");
226 break;
227 case if_error:
228 break;
229 default:
230 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d) in #elif directive", s);
233 | tELSE tNL {
234 pp_if_state_t s = pp_pop_if();
235 switch(s)
237 case if_true:
238 pp_push_if(if_elsefalse);
239 break;
240 case if_elif:
241 pp_push_if(if_elif);
242 break;
243 case if_false:
244 pp_push_if(if_elsetrue);
245 break;
246 case if_ignore:
247 pp_push_if(if_ignore);
248 break;
249 case if_elsetrue:
250 case if_elsefalse:
251 ppy_error("#else clause already defined");
252 break;
253 case if_error:
254 break;
255 default:
256 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d) in #else directive", s);
259 | tENDIF tNL {
260 if(pp_pop_if() != if_error)
262 if(pp_incl_state.ifdepth == pp_get_if_depth() && pp_incl_state.state == 1)
264 pp_incl_state.state = 2;
265 pp_incl_state.seen_junk = 0;
267 else if(pp_incl_state.state != 1)
269 pp_incl_state.state = -1;
271 if(pp_status.debug)
272 fprintf(stderr, "tENDIF: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n",
273 pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ppp, pp_incl_state.ifdepth);
276 | tUNDEF tIDENT tNL { pp_del_define($2); free($2); }
277 | tDEFINE opt_text tNL { pp_add_define($1, $2); free($1); free($2); }
278 | tMACRO res_arg allmargs tMACROEND opt_mtexts tNL {
279 pp_add_macro($1, macro_args, nmacro_args, $5);
281 | tLINE tSINT tDQSTRING tNL { if($3) pp_writestring("# %d %s\n", $2 , $3); free($3); }
282 | tGCCLINE tSINT tDQSTRING tNL { if($3) pp_writestring("# %d %s\n", $2 , $3); free($3); }
283 | tGCCLINE tSINT tDQSTRING tSINT tNL
284 { if($3) pp_writestring("# %d %s %d\n", $2, $3, $4); free($3); }
285 | tGCCLINE tSINT tDQSTRING tSINT tSINT tNL
286 { if($3) pp_writestring("# %d %s %d %d\n", $2 ,$3, $4, $5); free($3); }
287 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tNL
288 { if($3) pp_writestring("# %d %s %d %d %d\n", $2 ,$3 ,$4 ,$5, $6); free($3); }
289 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tSINT tNL
290 { if($3) pp_writestring("# %d %s %d %d %d %d\n", $2 ,$3 ,$4 ,$5, $6, $7); free($3); }
291 | tGCCLINE tNL /* The null-token */
292 | tERROR opt_text tNL { ppy_error("#error directive: '%s'", $2); free($2); }
293 | tWARNING opt_text tNL { ppy_warning("#warning directive: '%s'", $2); free($2); }
294 | tPRAGMA opt_text tNL { pp_writestring("#pragma %s\n", $2 ? $2 : ""); free($2); }
295 | tPPIDENT opt_text tNL { if(pp_status.pedantic) ppy_warning("#ident ignored (arg: '%s')", $2); free($2); }
296 | tRCINCLUDE tRCINCLUDEPATH {
297 int nl=strlen($2) +3;
298 char *fn=pp_xmalloc(nl);
299 sprintf(fn,"\"%s\"",$2);
300 pp_do_include(fn,1);
301 free($2);
303 | tRCINCLUDE tDQSTRING {
304 pp_do_include($2,1);
306 /*| tNL*/
309 opt_text: /* Empty */ { $$ = NULL; }
310 | text { $$ = $1; }
313 text : tLITERAL { $$ = $1; }
314 | tDQSTRING { $$ = $1; }
315 | tSQSTRING { $$ = $1; }
316 | text tLITERAL { $$ = merge_text($1, $2); }
317 | text tDQSTRING { $$ = merge_text($1, $2); }
318 | text tSQSTRING { $$ = merge_text($1, $2); }
321 res_arg : /* Empty */ { macro_args = NULL; nmacro_args = 0; }
324 allmargs: /* Empty */ { $$ = 0; macro_args = NULL; nmacro_args = 0; }
325 | emargs { $$ = nmacro_args; }
328 emargs : margs { $$ = $1; }
329 | margs ',' tELLIPSIS { nmacro_args *= -1; }
332 margs : margs ',' tIDENT { $$ = add_new_marg($3); }
333 | tIDENT { $$ = add_new_marg($1); }
336 opt_mtexts
337 : /* Empty */ { $$ = NULL; }
338 | mtexts {
339 for($$ = $1; $$ && $$->prev; $$ = $$->prev)
344 mtexts : mtext { $$ = $1; }
345 | mtexts mtext { $$ = combine_mtext($1, $2); }
348 mtext : tLITERAL { $$ = new_mtext($1, 0, exp_text); }
349 | tDQSTRING { $$ = new_mtext($1, 0, exp_text); }
350 | tSQSTRING { $$ = new_mtext($1, 0, exp_text); }
351 | tCONCAT { $$ = new_mtext(NULL, 0, exp_concat); }
352 | tSTRINGIZE tIDENT {
353 int mat = marg_index($2);
354 if(mat < 0)
355 ppy_error("Stringification identifier must be an argument parameter");
356 else
357 $$ = new_mtext(NULL, mat, exp_stringize);
359 | tIDENT {
360 int mat = marg_index($1);
361 if(mat >= 0)
362 $$ = new_mtext(NULL, mat, exp_subst);
363 else if($1)
364 $$ = new_mtext($1, 0, exp_text);
368 pp_expr : tSINT { $$.type = cv_sint; $$.val.si = $1; }
369 | tUINT { $$.type = cv_uint; $$.val.ui = $1; }
370 | tSLONG { $$.type = cv_slong; $$.val.sl = $1; }
371 | tULONG { $$.type = cv_ulong; $$.val.ul = $1; }
372 | tSLONGLONG { $$.type = cv_sll; $$.val.sll = $1; }
373 | tULONGLONG { $$.type = cv_ull; $$.val.ull = $1; }
374 | tDEFINED tIDENT { $$.type = cv_sint; $$.val.si = pplookup($2) != NULL; }
375 | tDEFINED '(' tIDENT ')' { $$.type = cv_sint; $$.val.si = pplookup($3) != NULL; }
376 | tIDENT { $$.type = cv_sint; $$.val.si = 0; }
377 | pp_expr tLOGOR pp_expr { $$.type = cv_sint; $$.val.si = boolean(&$1) || boolean(&$3); }
378 | pp_expr tLOGAND pp_expr { $$.type = cv_sint; $$.val.si = boolean(&$1) && boolean(&$3); }
379 | pp_expr tEQ pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, ==); }
380 | pp_expr tNE 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 tLTE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <=); }
384 | pp_expr tGTE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >=); }
385 | pp_expr '+' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, +); }
386 | pp_expr '-' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, -); }
387 | pp_expr '^' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, ^); }
388 | pp_expr '&' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, &); }
389 | pp_expr '|' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, |); }
390 | pp_expr '*' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, *); }
391 | pp_expr '/' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, /); }
392 | pp_expr tLSHIFT pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <<); }
393 | pp_expr tRSHIFT pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >>); }
394 | '+' pp_expr { $$ = $2; }
395 | '-' pp_expr { UNARY_OP($$, $2, -); }
396 | '~' pp_expr { UNARY_OP($$, $2, ~); }
397 | '!' pp_expr { $$.type = cv_sint; $$.val.si = !boolean(&$2); }
398 | '(' pp_expr ')' { $$ = $2; }
399 | pp_expr '?' pp_expr ':' pp_expr { $$ = boolean(&$1) ? $3 : $5; }
405 **************************************************************************
406 * Support functions
407 **************************************************************************
410 static void cast_to_sint(cval_t *v)
412 switch(v->type)
414 case cv_sint: break;
415 case cv_uint: break;
416 case cv_slong: v->val.si = v->val.sl; break;
417 case cv_ulong: v->val.si = v->val.ul; break;
418 case cv_sll: v->val.si = v->val.sll; break;
419 case cv_ull: v->val.si = v->val.ull; break;
421 v->type = cv_sint;
424 static void cast_to_uint(cval_t *v)
426 switch(v->type)
428 case cv_sint: break;
429 case cv_uint: break;
430 case cv_slong: v->val.ui = v->val.sl; break;
431 case cv_ulong: v->val.ui = v->val.ul; break;
432 case cv_sll: v->val.ui = v->val.sll; break;
433 case cv_ull: v->val.ui = v->val.ull; break;
435 v->type = cv_uint;
438 static void cast_to_slong(cval_t *v)
440 switch(v->type)
442 case cv_sint: v->val.sl = v->val.si; break;
443 case cv_uint: v->val.sl = v->val.ui; break;
444 case cv_slong: break;
445 case cv_ulong: break;
446 case cv_sll: v->val.sl = v->val.sll; break;
447 case cv_ull: v->val.sl = v->val.ull; break;
449 v->type = cv_slong;
452 static void cast_to_ulong(cval_t *v)
454 switch(v->type)
456 case cv_sint: v->val.ul = v->val.si; break;
457 case cv_uint: v->val.ul = v->val.ui; break;
458 case cv_slong: break;
459 case cv_ulong: break;
460 case cv_sll: v->val.ul = v->val.sll; break;
461 case cv_ull: v->val.ul = v->val.ull; break;
463 v->type = cv_ulong;
466 static void cast_to_sll(cval_t *v)
468 switch(v->type)
470 case cv_sint: v->val.sll = v->val.si; break;
471 case cv_uint: v->val.sll = v->val.ui; break;
472 case cv_slong: v->val.sll = v->val.sl; break;
473 case cv_ulong: v->val.sll = v->val.ul; break;
474 case cv_sll: break;
475 case cv_ull: break;
477 v->type = cv_sll;
480 static void cast_to_ull(cval_t *v)
482 switch(v->type)
484 case cv_sint: v->val.ull = v->val.si; break;
485 case cv_uint: v->val.ull = v->val.ui; break;
486 case cv_slong: v->val.ull = v->val.sl; break;
487 case cv_ulong: v->val.ull = v->val.ul; break;
488 case cv_sll: break;
489 case cv_ull: break;
491 v->type = cv_ull;
495 static void promote_equal_size(cval_t *v1, cval_t *v2)
497 #define cv_sizeof(v) ((int)(v->type & SIZE_MASK))
498 int s1 = cv_sizeof(v1);
499 int s2 = cv_sizeof(v2);
500 #undef cv_sizeof
502 if(s1 == s2)
503 return;
504 else if(s1 > s2)
506 switch(v1->type)
508 case cv_sint: cast_to_sint(v2); break;
509 case cv_uint: cast_to_uint(v2); break;
510 case cv_slong: cast_to_slong(v2); break;
511 case cv_ulong: cast_to_ulong(v2); break;
512 case cv_sll: cast_to_sll(v2); break;
513 case cv_ull: cast_to_ull(v2); break;
516 else
518 switch(v2->type)
520 case cv_sint: cast_to_sint(v1); break;
521 case cv_uint: cast_to_uint(v1); break;
522 case cv_slong: cast_to_slong(v1); break;
523 case cv_ulong: cast_to_ulong(v1); break;
524 case cv_sll: cast_to_sll(v1); break;
525 case cv_ull: cast_to_ull(v1); break;
531 static int boolean(cval_t *v)
533 switch(v->type)
535 case cv_sint: return v->val.si != 0;
536 case cv_uint: return v->val.ui != 0;
537 case cv_slong: return v->val.sl != 0;
538 case cv_ulong: return v->val.ul != 0;
539 case cv_sll: return v->val.sll != 0;
540 case cv_ull: return v->val.ull != 0;
542 return 0;
545 static char *add_new_marg(char *str)
547 char *ma;
548 macro_args = pp_xrealloc(macro_args, (nmacro_args+1) * sizeof(macro_args[0]));
549 macro_args[nmacro_args++] = ma = pp_xstrdup(str);
550 return ma;
553 static int marg_index(char *id)
555 int t;
556 if(!id)
557 return -1;
558 for(t = 0; t < nmacro_args; t++)
560 if(!strcmp(id, macro_args[t]))
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 = pp_xmalloc(sizeof(mtext_t));
570 if(str == NULL)
571 mt->subst.argidx = idx;
572 else
573 mt->subst.text = str;
574 mt->type = type;
575 mt->next = mt->prev = NULL;
576 return mt;
579 static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
581 if(!tail)
582 return mtp;
584 if(!mtp)
585 return tail;
587 if(tail->type == exp_text && mtp->type == exp_text)
589 tail->subst.text = pp_xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
590 strcat(tail->subst.text, mtp->subst.text);
591 free(mtp->subst.text);
592 free(mtp);
593 return tail;
596 if(tail->type == exp_concat && mtp->type == exp_concat)
598 free(mtp);
599 return tail;
602 if(tail->type == exp_concat && mtp->type == exp_text)
604 int len = strlen(mtp->subst.text);
605 while(len)
607 /* FIXME: should delete space from head of string */
608 if(isspace(mtp->subst.text[len-1] & 0xff))
609 mtp->subst.text[--len] = '\0';
610 else
611 break;
614 if(!len)
616 free(mtp->subst.text);
617 free(mtp);
618 return tail;
622 if(tail->type == exp_text && mtp->type == exp_concat)
624 int len = strlen(tail->subst.text);
625 while(len)
627 if(isspace(tail->subst.text[len-1] & 0xff))
628 tail->subst.text[--len] = '\0';
629 else
630 break;
633 if(!len)
635 mtp->prev = tail->prev;
636 mtp->next = tail->next;
637 if(tail->prev)
638 tail->prev->next = mtp;
639 free(tail->subst.text);
640 free(tail);
641 return mtp;
645 tail->next = mtp;
646 mtp->prev = tail;
648 return mtp;
651 static char *merge_text(char *s1, char *s2)
653 int l1 = strlen(s1);
654 int l2 = strlen(s2);
655 s1 = pp_xrealloc(s1, l1+l2+1);
656 memcpy(s1+l1, s2, l2+1);
657 free(s2);
658 return s1;