Fixed behavior of GetTextExtentExPointW. It must always return the
[wine/testsucceed.git] / tools / wrc / ppy.y
blobf6c8c380a02e6c353c8fc67ae6a6d91700527472
1 /*
2 * Wrc preprocessor syntax analysis
4 * Copyright 1999-2000 Bertho A. Stultiens (BS)
6 * 24-Apr-2000 BS Restructured the lot to fit the new scanner
7 * and reintegrate into the wine-tree.
8 * 01-Jan-2000 BS FIXME: win16 preprocessor calculates with
9 * 16 bit ints and overflows...?
10 * 26-Dec-1999 BS Started this file
15 #include "config.h"
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <assert.h>
21 #include <ctype.h>
22 #include <string.h>
24 #include "utils.h"
25 #include "newstruc.h"
26 #include "wrc.h"
27 #include "preproc.h"
30 #define UNARY_OP(r, v, OP) \
31 switch(v.type) \
32 { \
33 case cv_sint: r.val.si = OP v.val.si; break; \
34 case cv_uint: r.val.ui = OP v.val.ui; break; \
35 case cv_slong: r.val.sl = OP v.val.sl; break; \
36 case cv_ulong: r.val.ul = OP v.val.ul; break; \
37 case cv_sll: r.val.sll = OP v.val.sll; break; \
38 case cv_ull: r.val.ull = OP v.val.ull; break; \
41 #define cv_signed(v) ((v.type & FLAG_SIGNED) != 0)
43 #define BIN_OP_INT(r, v1, v2, OP) \
44 r.type = v1.type; \
45 if(cv_signed(v1) && cv_signed(v2)) \
46 r.val.si = v1.val.si OP v2.val.si; \
47 else if(cv_signed(v1) && !cv_signed(v2)) \
48 r.val.si = v1.val.si OP v2.val.ui; \
49 else if(!cv_signed(v1) && cv_signed(v2)) \
50 r.val.ui = v1.val.ui OP v2.val.si; \
51 else \
52 r.val.ui = v1.val.ui OP v2.val.ui;
54 #define BIN_OP_LONG(r, v1, v2, OP) \
55 r.type = v1.type; \
56 if(cv_signed(v1) && cv_signed(v2)) \
57 r.val.sl = v1.val.sl OP v2.val.sl; \
58 else if(cv_signed(v1) && !cv_signed(v2)) \
59 r.val.sl = v1.val.sl OP v2.val.ul; \
60 else if(!cv_signed(v1) && cv_signed(v2)) \
61 r.val.ul = v1.val.ul OP v2.val.sl; \
62 else \
63 r.val.ul = v1.val.ul OP v2.val.ul;
65 #define BIN_OP_LONGLONG(r, v1, v2, OP) \
66 r.type = v1.type; \
67 if(cv_signed(v1) && cv_signed(v2)) \
68 r.val.sll = v1.val.sll OP v2.val.sll; \
69 else if(cv_signed(v1) && !cv_signed(v2)) \
70 r.val.sll = v1.val.sll OP v2.val.ull; \
71 else if(!cv_signed(v1) && cv_signed(v2)) \
72 r.val.ull = v1.val.ull OP v2.val.sll; \
73 else \
74 r.val.ull = v1.val.ull OP v2.val.ull;
76 #define BIN_OP(r, v1, v2, OP) \
77 switch(v1.type & SIZE_MASK) \
78 { \
79 case SIZE_INT: BIN_OP_INT(r, v1, v2, OP); break; \
80 case SIZE_LONG: BIN_OP_LONG(r, v1, v2, OP); break; \
81 case SIZE_LONGLONG: BIN_OP_LONGLONG(r, v1, v2, OP); break; \
82 default: internal_error(__FILE__, __LINE__, "Invalid type indicator (0x%04x)", v1.type); \
87 * Prototypes
89 static int boolean(cval_t *v);
90 static void promote_equal_size(cval_t *v1, cval_t *v2);
91 static void cast_to_sint(cval_t *v);
92 static void cast_to_uint(cval_t *v);
93 static void cast_to_slong(cval_t *v);
94 static void cast_to_ulong(cval_t *v);
95 static void cast_to_sll(cval_t *v);
96 static void cast_to_ull(cval_t *v);
97 static marg_t *new_marg(char *str, def_arg_t type);
98 static marg_t *add_new_marg(char *str, def_arg_t type);
99 static int marg_index(char *id);
100 static mtext_t *new_mtext(char *str, int idx, def_exp_t type);
101 static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp);
102 static char *merge_text(char *s1, char *s2);
105 * Local variables
107 static marg_t **macro_args; /* Macro parameters array while parsing */
108 static int nmacro_args;
112 %union{
113 int sint;
114 unsigned int uint;
115 long slong;
116 unsigned long ulong;
117 wrc_sll_t sll;
118 wrc_ull_t ull;
119 int *iptr;
120 char *cptr;
121 cval_t cval;
122 marg_t *marg;
123 mtext_t *mtext;
126 %token tRCINCLUDE
127 %token tIF tIFDEF tIFNDEF tELSE tELIF tENDIF tDEFINED tNL
128 %token tINCLUDE tLINE tGCCLINE tERROR tWARNING tPRAGMA tPPIDENT
129 %token tUNDEF tMACROEND tCONCAT tELIPSIS tSTRINGIZE
130 %token <cptr> tIDENT tLITERAL tMACRO tDEFINE
131 %token <cptr> tDQSTRING tSQSTRING tIQSTRING
132 %token <uint> tUINT
133 %token <sint> tSINT
134 %token <ulong> tULONG
135 %token <slong> tSLONG
136 %token <ull> tULONGLONG
137 %token <sll> tSLONGLONG
138 %token <cptr> tRCINCLUDEPATH
140 %right '?' ':'
141 %left tLOGOR
142 %left tLOGAND
143 %left '|'
144 %left '^'
145 %left '&'
146 %left tEQ tNE
147 %left '<' tLTE '>' tGTE
148 %left tLSHIFT tRSHIFT
149 %left '+' '-'
150 %left '*' '/'
151 %right '~' '!'
153 %type <cval> pp_expr
154 %type <marg> emargs margs
155 %type <mtext> opt_mtexts mtexts mtext
156 %type <sint> allmargs
157 %type <cptr> opt_text text
160 **************************************************************************
161 * The parser starts here
162 **************************************************************************
167 pp_file : /* Empty */
168 | pp_file preprocessor
171 preprocessor
172 : tINCLUDE tDQSTRING tNL { do_include($2, 1); }
173 | tINCLUDE tIQSTRING tNL { do_include($2, 0); }
174 | tIF pp_expr tNL { next_if_state(boolean(&$2)); }
175 | tIFDEF tIDENT tNL { next_if_state(pplookup($2) != NULL); free($2); }
176 | tIFNDEF tIDENT tNL {
177 int t = pplookup($2) == NULL;
178 if(include_state == 0 && t && !seen_junk)
180 include_state = 1;
181 include_ppp = $2;
182 include_ifdepth = get_if_depth();
184 else if(include_state != 1)
186 include_state = -1;
187 free($2);
189 else
190 free($2);
191 next_if_state(t);
192 if(debuglevel & DEBUGLEVEL_PPMSG)
193 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);
195 | tELIF pp_expr tNL {
196 if_state_t s = pop_if();
197 switch(s)
199 case if_true:
200 case if_elif:
201 push_if(if_elif);
202 break;
203 case if_false:
204 push_if(boolean(&$2) ? if_true : if_false);
205 break;
206 case if_ignore:
207 push_if(if_ignore);
208 break;
209 case if_elsetrue:
210 case if_elsefalse:
211 pperror("#elif cannot follow #else");
212 default:
213 internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #elif directive", s);
216 | tELSE tNL {
217 if_state_t s = pop_if();
218 switch(s)
220 case if_true:
221 push_if(if_elsefalse);
222 break;
223 case if_elif:
224 push_if(if_elif);
225 break;
226 case if_false:
227 push_if(if_elsetrue);
228 break;
229 case if_ignore:
230 push_if(if_ignore);
231 break;
232 case if_elsetrue:
233 case if_elsefalse:
234 pperror("#else clause already defined");
235 default:
236 internal_error(__FILE__, __LINE__, "Invalid if_state (%d) in #else directive", s);
239 | tENDIF tNL {
240 pop_if();
241 if(include_ifdepth == get_if_depth() && include_state == 1)
243 include_state = 2;
244 seen_junk = 0;
246 else if(include_state != 1)
248 include_state = -1;
250 if(debuglevel & DEBUGLEVEL_PPMSG)
251 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);
253 | tUNDEF tIDENT tNL { del_define($2); free($2); }
254 | tDEFINE opt_text tNL { add_define($1, $2); }
255 | tMACRO res_arg allmargs tMACROEND opt_mtexts tNL {
256 add_macro($1, macro_args, nmacro_args, $5);
258 | tLINE tSINT tDQSTRING tNL { fprintf(ppout, "# %d %s\n", $2 , $3); free($3); }
259 | tGCCLINE tSINT tDQSTRING tNL { fprintf(ppout, "# %d %s\n", $2 , $3); free($3); }
260 | tGCCLINE tSINT tDQSTRING tSINT tNL
261 { fprintf(ppout, "# %d %s %d\n", $2, $3, $4); free($3); }
262 | tGCCLINE tSINT tDQSTRING tSINT tSINT tNL
263 { fprintf(ppout, "# %d %s %d %d\n", $2 ,$3, $4, $5); free($3); }
264 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tNL
265 { fprintf(ppout, "# %d %s %d %d %d\n", $2 ,$3 ,$4 ,$5, $6); free($3); }
266 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tSINT tNL
267 { fprintf(ppout, "# %d %s %d %d %d %d\n", $2 ,$3 ,$4 ,$5, $6, $7); free($3); }
268 | tGCCLINE tNL /* The null-token */
269 | tERROR opt_text tNL { pperror("#error directive: '%s'", $2); if($2) free($2); }
270 | tWARNING opt_text tNL { ppwarning("#warning directive: '%s'", $2); if($2) free($2); }
271 | tPRAGMA opt_text tNL { if(pedantic) ppwarning("#pragma ignored (arg: '%s')", $2); if($2) free($2); }
272 | tPPIDENT opt_text tNL { if(pedantic) ppwarning("#ident ignored (arg: '%s')", $2); if($2) free($2); }
273 | tRCINCLUDE tRCINCLUDEPATH {
274 int nl=strlen($2) +3;
275 char *fn=xmalloc(nl);
276 snprintf(fn,nl,"\"%s\"",$2);
277 free($2);
278 do_include(fn,1);
280 | tRCINCLUDE tDQSTRING {
281 do_include($2,1);
283 /*| tNL*/
286 opt_text: /* Empty */ { $$ = NULL; }
287 | text { $$ = $1; }
290 text : tLITERAL { $$ = $1; }
291 | tDQSTRING { $$ = $1; }
292 | tSQSTRING { $$ = $1; }
293 | text tLITERAL { $$ = merge_text($1, $2); }
294 | text tDQSTRING { $$ = merge_text($1, $2); }
295 | text tSQSTRING { $$ = merge_text($1, $2); }
298 res_arg : /* Empty */ { macro_args = NULL; nmacro_args = 0; }
301 allmargs: /* Empty */ { $$ = 0; macro_args = NULL; nmacro_args = 0; }
302 | emargs { $$ = nmacro_args; }
305 emargs : margs { $$ = $1; }
306 | margs ',' tELIPSIS { $$ = add_new_marg(NULL, arg_list); nmacro_args *= -1; }
309 margs : margs ',' tIDENT { $$ = add_new_marg($3, arg_single); }
310 | tIDENT { $$ = add_new_marg($1, arg_single); }
313 opt_mtexts
314 : /* Empty */ { $$ = NULL; }
315 | mtexts {
316 for($$ = $1; $$ && $$->prev; $$ = $$->prev)
321 mtexts : mtext { $$ = $1; }
322 | mtexts mtext { $$ = combine_mtext($1, $2); }
325 mtext : tLITERAL { $$ = new_mtext($1, 0, exp_text); }
326 | tDQSTRING { $$ = new_mtext($1, 0, exp_text); }
327 | tSQSTRING { $$ = new_mtext($1, 0, exp_text); }
328 | tCONCAT { $$ = new_mtext(NULL, 0, exp_concat); }
329 | tSTRINGIZE tIDENT {
330 int mat = marg_index($2);
331 if(mat < 0)
332 pperror("Stringification identifier must be an argument parameter");
333 $$ = new_mtext(NULL, mat, exp_stringize);
335 | tIDENT {
336 int mat = marg_index($1);
337 if(mat >= 0)
338 $$ = new_mtext(NULL, mat, exp_subst);
339 else
340 $$ = new_mtext($1, 0, exp_text);
344 pp_expr : tSINT { $$.type = cv_sint; $$.val.si = $1; }
345 | tUINT { $$.type = cv_uint; $$.val.ui = $1; }
346 | tSLONG { $$.type = cv_slong; $$.val.sl = $1; }
347 | tULONG { $$.type = cv_ulong; $$.val.ul = $1; }
348 | tSLONGLONG { $$.type = cv_sll; $$.val.sl = $1; }
349 | tULONGLONG { $$.type = cv_ull; $$.val.ul = $1; }
350 | tDEFINED tIDENT { $$.type = cv_sint; $$.val.si = pplookup($2) != NULL; }
351 | tDEFINED '(' tIDENT ')' { $$.type = cv_sint; $$.val.si = pplookup($3) != NULL; }
352 | tIDENT { $$.type = cv_sint; $$.val.si = 0; }
353 | pp_expr tLOGOR pp_expr { $$.type = cv_sint; $$.val.si = boolean(&$1) || boolean(&$3); }
354 | pp_expr tLOGAND pp_expr { $$.type = cv_sint; $$.val.si = boolean(&$1) && boolean(&$3); }
355 | pp_expr tEQ pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, ==) }
356 | pp_expr tNE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, !=) }
357 | pp_expr '<' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <) }
358 | pp_expr '>' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >) }
359 | pp_expr tLTE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <=) }
360 | pp_expr tGTE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >=) }
361 | pp_expr '+' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, +) }
362 | pp_expr '-' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, -) }
363 | pp_expr '^' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, ^) }
364 | pp_expr '&' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, &) }
365 | pp_expr '|' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, |) }
366 | pp_expr '*' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, *) }
367 | pp_expr '/' pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, /) }
368 | pp_expr tLSHIFT pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <<) }
369 | pp_expr tRSHIFT pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >>) }
370 | '+' pp_expr { $$ = $2; }
371 | '-' pp_expr { UNARY_OP($$, $2, -) }
372 | '~' pp_expr { UNARY_OP($$, $2, ~) }
373 | '!' pp_expr { $$.type = cv_sint; $$.val.si = !boolean(&$2); }
374 | '(' pp_expr ')' { $$ = $2; }
375 | pp_expr '?' pp_expr ':' pp_expr { $$ = boolean(&$1) ? $3 : $5; }
381 **************************************************************************
382 * Support functions
383 **************************************************************************
386 static void cast_to_sint(cval_t *v)
388 switch(v->type)
390 case cv_sint: break;
391 case cv_uint: break;
392 case cv_slong: v->val.si = v->val.sl; break;
393 case cv_ulong: v->val.si = v->val.ul; break;
394 case cv_sll: v->val.si = v->val.sll; break;
395 case cv_ull: v->val.si = v->val.ull; break;
397 v->type = cv_sint;
400 static void cast_to_uint(cval_t *v)
402 switch(v->type)
404 case cv_sint: break;
405 case cv_uint: break;
406 case cv_slong: v->val.ui = v->val.sl; break;
407 case cv_ulong: v->val.ui = v->val.ul; break;
408 case cv_sll: v->val.ui = v->val.sll; break;
409 case cv_ull: v->val.ui = v->val.ull; break;
411 v->type = cv_uint;
414 static void cast_to_slong(cval_t *v)
416 switch(v->type)
418 case cv_sint: v->val.sl = v->val.si; break;
419 case cv_uint: v->val.sl = v->val.ui; break;
420 case cv_slong: break;
421 case cv_ulong: break;
422 case cv_sll: v->val.sl = v->val.sll; break;
423 case cv_ull: v->val.sl = v->val.ull; break;
425 v->type = cv_slong;
428 static void cast_to_ulong(cval_t *v)
430 switch(v->type)
432 case cv_sint: v->val.ul = v->val.si; break;
433 case cv_uint: v->val.ul = v->val.ui; break;
434 case cv_slong: break;
435 case cv_ulong: break;
436 case cv_sll: v->val.ul = v->val.sll; break;
437 case cv_ull: v->val.ul = v->val.ull; break;
439 v->type = cv_ulong;
442 static void cast_to_sll(cval_t *v)
444 switch(v->type)
446 case cv_sint: v->val.sll = v->val.si; break;
447 case cv_uint: v->val.sll = v->val.ui; break;
448 case cv_slong: v->val.sll = v->val.sl; break;
449 case cv_ulong: v->val.sll = v->val.ul; break;
450 case cv_sll: break;
451 case cv_ull: break;
453 v->type = cv_sll;
456 static void cast_to_ull(cval_t *v)
458 switch(v->type)
460 case cv_sint: v->val.ull = v->val.si; break;
461 case cv_uint: v->val.ull = v->val.ui; break;
462 case cv_slong: v->val.ull = v->val.sl; break;
463 case cv_ulong: v->val.ull = v->val.ul; break;
464 case cv_sll: break;
465 case cv_ull: break;
467 v->type = cv_ull;
471 static void promote_equal_size(cval_t *v1, cval_t *v2)
473 #define cv_sizeof(v) ((int)(v->type & SIZE_MASK))
474 int s1 = cv_sizeof(v1);
475 int s2 = cv_sizeof(v2);
476 #undef cv_sizeof
478 if(s1 == s2)
479 return;
480 else if(s1 > s2)
482 switch(v1->type)
484 case cv_sint: cast_to_sint(v2); break;
485 case cv_uint: cast_to_uint(v2); break;
486 case cv_slong: cast_to_slong(v2); break;
487 case cv_ulong: cast_to_ulong(v2); break;
488 case cv_sll: cast_to_sll(v2); break;
489 case cv_ull: cast_to_ull(v2); break;
492 else
494 switch(v2->type)
496 case cv_sint: cast_to_sint(v1); break;
497 case cv_uint: cast_to_uint(v1); break;
498 case cv_slong: cast_to_slong(v1); break;
499 case cv_ulong: cast_to_ulong(v1); break;
500 case cv_sll: cast_to_sll(v1); break;
501 case cv_ull: cast_to_ull(v1); break;
507 static int boolean(cval_t *v)
509 switch(v->type)
511 case cv_sint: return v->val.si != (int)0;
512 case cv_uint: return v->val.ui != (unsigned int)0;
513 case cv_slong: return v->val.sl != (long)0;
514 case cv_ulong: return v->val.ul != (unsigned long)0;
515 case cv_sll: return v->val.sll != (wrc_sll_t)0;
516 case cv_ull: return v->val.ull != (wrc_ull_t)0;
518 return 0;
521 static marg_t *new_marg(char *str, def_arg_t type)
523 marg_t *ma = (marg_t *)xmalloc(sizeof(marg_t));
524 ma->arg = str;
525 ma->type = type;
526 return ma;
529 static marg_t *add_new_marg(char *str, def_arg_t type)
531 marg_t *ma = new_marg(str, type);
532 nmacro_args++;
533 macro_args = (marg_t **)xrealloc(macro_args, nmacro_args * sizeof(macro_args[0]));
534 macro_args[nmacro_args-1] = ma;
535 return ma;
538 static int marg_index(char *id)
540 int t;
541 for(t = 0; t < nmacro_args; t++)
543 if(!strcmp(id, macro_args[t]->arg))
544 break;
546 return t < nmacro_args ? t : -1;
549 static mtext_t *new_mtext(char *str, int idx, def_exp_t type)
551 mtext_t *mt = (mtext_t *)xmalloc(sizeof(mtext_t));
552 if(str == NULL)
553 mt->subst.argidx = idx;
554 else
555 mt->subst.text = str;
556 mt->type = type;
557 return mt;
560 static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
562 if(!tail)
563 return mtp;
565 if(!mtp)
566 return tail;
568 if(tail->type == exp_text && mtp->type == exp_text)
570 tail->subst.text = xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
571 strcat(tail->subst.text, mtp->subst.text);
572 free(mtp->subst.text);
573 free(mtp);
574 return tail;
577 if(tail->type == exp_concat && mtp->type == exp_concat)
579 free(mtp);
580 return tail;
583 if(tail->type == exp_concat && mtp->type == exp_text)
585 int len = strlen(mtp->subst.text);
586 while(len)
588 /* FIXME: should delete space from head of string */
589 if(isspace(mtp->subst.text[len-1] & 0xff))
590 mtp->subst.text[--len] = '\0';
591 else
592 break;
595 if(!len)
597 free(mtp->subst.text);
598 free(mtp);
599 return tail;
603 if(tail->type == exp_text && mtp->type == exp_concat)
605 int len = strlen(tail->subst.text);
606 while(len)
608 if(isspace(tail->subst.text[len-1] & 0xff))
609 tail->subst.text[--len] = '\0';
610 else
611 break;
614 if(!len)
616 mtp->prev = tail->prev;
617 mtp->next = tail->next;
618 if(tail->prev)
619 tail->prev->next = mtp;
620 free(tail->subst.text);
621 free(tail);
622 return mtp;
626 tail->next = mtp;
627 mtp->prev = tail;
629 return mtp;
632 static char *merge_text(char *s1, char *s2)
634 int l1 = strlen(s1);
635 int l2 = strlen(s2);
636 s1 = xrealloc(s1, l1+l2+1);
637 memcpy(s1+l1, s2, l2+1);
638 free(s2);
639 return s1;