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
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
32 #include "wine/port.h"
41 #include "wpp_private.h"
44 #define UNARY_OP(r, v, OP) \
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) \
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 v2.val.ui; \
63 else if(!cv_signed(v1) && cv_signed(v2)) \
64 r.val.ui = v1.val.ui OP v2.val.si; \
66 r.val.ui = v1.val.ui OP v2.val.ui;
68 #define BIN_OP_LONG(r, v1, v2, OP) \
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 v2.val.ul; \
74 else if(!cv_signed(v1) && cv_signed(v2)) \
75 r.val.ul = v1.val.ul OP v2.val.sl; \
77 r.val.ul = v1.val.ul OP v2.val.ul;
79 #define BIN_OP_LONGLONG(r, v1, v2, OP) \
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 v2.val.ull; \
85 else if(!cv_signed(v1) && cv_signed(v2)) \
86 r.val.ull = v1.val.ull OP v2.val.sll; \
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) \
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); \
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 marg_t *new_marg(char *str, def_arg_t type);
112 static marg_t *add_new_marg(char *str, def_arg_t type);
113 static int marg_index(char *id);
114 static mtext_t *new_mtext(char *str, int idx, def_exp_t type);
115 static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp);
116 static char *merge_text(char *s1, char *s2);
121 static marg_t **macro_args; /* Macro parameters array while parsing */
122 static int nmacro_args;
141 %token tIF tIFDEF tIFNDEF tELSE tELIF tENDIF tDEFINED tNL
142 %token tINCLUDE tLINE tGCCLINE tERROR tWARNING tPRAGMA tPPIDENT
143 %token tUNDEF tMACROEND tCONCAT tELIPSIS tSTRINGIZE
144 %token <cptr> tIDENT tLITERAL tMACRO tDEFINE
145 %token <cptr> tDQSTRING tSQSTRING tIQSTRING
148 %token <ulong> tULONG
149 %token <slong> tSLONG
150 %token <ull> tULONGLONG
151 %token <sll> tSLONGLONG
152 %token <cptr> tRCINCLUDEPATH
161 %left '<' tLTE '>' tGTE
162 %left tLSHIFT tRSHIFT
168 %type <marg> emargs margs
169 %type <mtext> opt_mtexts mtexts mtext
170 %type <sint> allmargs
171 %type <cptr> opt_text text
174 **************************************************************************
175 * The parser starts here
176 **************************************************************************
181 pp_file : /* Empty */
182 | pp_file preprocessor
186 : tINCLUDE tDQSTRING tNL { pp_do_include($2, 1); }
187 | tINCLUDE tIQSTRING tNL { pp_do_include($2, 0); }
188 | tIF pp_expr tNL { pp_next_if_state(boolean(&$2)); }
189 | tIFDEF tIDENT tNL { pp_next_if_state(pplookup($2) != NULL); free($2); }
190 | tIFNDEF tIDENT tNL {
191 int t = pplookup($2) == NULL;
192 if(pp_incl_state.state == 0 && t && !pp_incl_state.seen_junk)
194 pp_incl_state.state = 1;
195 pp_incl_state.ppp = $2;
196 pp_incl_state.ifdepth = pp_get_if_depth();
198 else if(pp_incl_state.state != 1)
200 pp_incl_state.state = -1;
207 fprintf(stderr, "tIFNDEF: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n",
208 pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ppp, pp_incl_state.ifdepth);
210 | tELIF pp_expr tNL {
211 pp_if_state_t s = pp_pop_if();
219 pp_push_if(boolean(&$2) ? if_true : if_false);
222 pp_push_if(if_ignore);
226 ppy_error("#elif cannot follow #else");
228 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d) in #elif directive", s);
232 pp_if_state_t s = pp_pop_if();
236 pp_push_if(if_elsefalse);
242 pp_push_if(if_elsetrue);
245 pp_push_if(if_ignore);
249 ppy_error("#else clause already defined");
251 pp_internal_error(__FILE__, __LINE__, "Invalid pp_if_state (%d) in #else directive", s);
256 if(pp_incl_state.ifdepth == pp_get_if_depth() && pp_incl_state.state == 1)
258 pp_incl_state.state = 2;
259 pp_incl_state.seen_junk = 0;
261 else if(pp_incl_state.state != 1)
263 pp_incl_state.state = -1;
266 fprintf(stderr, "tENDIF: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n",
267 pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ppp, pp_incl_state.ifdepth);
269 | tUNDEF tIDENT tNL { pp_del_define($2); free($2); }
270 | tDEFINE opt_text tNL { pp_add_define($1, $2); }
271 | tMACRO res_arg allmargs tMACROEND opt_mtexts tNL {
272 pp_add_macro($1, macro_args, nmacro_args, $5);
274 | tLINE tSINT tDQSTRING tNL { fprintf(ppy_out, "# %d %s\n", $2 , $3); free($3); }
275 | tGCCLINE tSINT tDQSTRING tNL { fprintf(ppy_out, "# %d %s\n", $2 , $3); free($3); }
276 | tGCCLINE tSINT tDQSTRING tSINT tNL
277 { fprintf(ppy_out, "# %d %s %d\n", $2, $3, $4); free($3); }
278 | tGCCLINE tSINT tDQSTRING tSINT tSINT tNL
279 { fprintf(ppy_out, "# %d %s %d %d\n", $2 ,$3, $4, $5); free($3); }
280 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tNL
281 { fprintf(ppy_out, "# %d %s %d %d %d\n", $2 ,$3 ,$4 ,$5, $6); free($3); }
282 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tSINT tNL
283 { fprintf(ppy_out, "# %d %s %d %d %d %d\n", $2 ,$3 ,$4 ,$5, $6, $7); free($3); }
284 | tGCCLINE tNL /* The null-token */
285 | tERROR opt_text tNL { ppy_error("#error directive: '%s'", $2); free($2); }
286 | tWARNING opt_text tNL { ppy_warning("#warning directive: '%s'", $2); free($2); }
287 | tPRAGMA opt_text tNL { fprintf(ppy_out, "#pragma %s\n", $2 ? $2 : ""); free($2); }
288 | tPPIDENT opt_text tNL { if(pp_status.pedantic) ppy_warning("#ident ignored (arg: '%s')", $2); free($2); }
289 | tRCINCLUDE tRCINCLUDEPATH {
290 int nl=strlen($2) +3;
291 char *fn=pp_xmalloc(nl);
292 sprintf(fn,"\"%s\"",$2);
296 | tRCINCLUDE tDQSTRING {
302 opt_text: /* Empty */ { $$ = NULL; }
306 text : tLITERAL { $$ = $1; }
307 | tDQSTRING { $$ = $1; }
308 | tSQSTRING { $$ = $1; }
309 | text tLITERAL { $$ = merge_text($1, $2); }
310 | text tDQSTRING { $$ = merge_text($1, $2); }
311 | text tSQSTRING { $$ = merge_text($1, $2); }
314 res_arg : /* Empty */ { macro_args = NULL; nmacro_args = 0; }
317 allmargs: /* Empty */ { $$ = 0; macro_args = NULL; nmacro_args = 0; }
318 | emargs { $$ = nmacro_args; }
321 emargs : margs { $$ = $1; }
322 | margs ',' tELIPSIS { $$ = add_new_marg(NULL, arg_list); nmacro_args *= -1; }
325 margs : margs ',' tIDENT { $$ = add_new_marg($3, arg_single); }
326 | tIDENT { $$ = add_new_marg($1, arg_single); }
330 : /* Empty */ { $$ = NULL; }
332 for($$ = $1; $$ && $$->prev; $$ = $$->prev)
337 mtexts : mtext { $$ = $1; }
338 | mtexts mtext { $$ = combine_mtext($1, $2); }
341 mtext : tLITERAL { $$ = new_mtext($1, 0, exp_text); }
342 | tDQSTRING { $$ = new_mtext($1, 0, exp_text); }
343 | tSQSTRING { $$ = new_mtext($1, 0, exp_text); }
344 | tCONCAT { $$ = new_mtext(NULL, 0, exp_concat); }
345 | tSTRINGIZE tIDENT {
346 int mat = marg_index($2);
348 ppy_error("Stringification identifier must be an argument parameter");
349 $$ = new_mtext(NULL, mat, exp_stringize);
352 int mat = marg_index($1);
354 $$ = new_mtext(NULL, mat, exp_subst);
356 $$ = new_mtext($1, 0, exp_text);
360 pp_expr : tSINT { $$.type = cv_sint; $$.val.si = $1; }
361 | tUINT { $$.type = cv_uint; $$.val.ui = $1; }
362 | tSLONG { $$.type = cv_slong; $$.val.sl = $1; }
363 | tULONG { $$.type = cv_ulong; $$.val.ul = $1; }
364 | tSLONGLONG { $$.type = cv_sll; $$.val.sll = $1; }
365 | tULONGLONG { $$.type = cv_ull; $$.val.ull = $1; }
366 | tDEFINED tIDENT { $$.type = cv_sint; $$.val.si = pplookup($2) != NULL; }
367 | tDEFINED '(' tIDENT ')' { $$.type = cv_sint; $$.val.si = pplookup($3) != NULL; }
368 | tIDENT { $$.type = cv_sint; $$.val.si = 0; }
369 | pp_expr tLOGOR pp_expr { $$.type = cv_sint; $$.val.si = boolean(&$1) || boolean(&$3); }
370 | pp_expr tLOGAND pp_expr { $$.type = cv_sint; $$.val.si = boolean(&$1) && boolean(&$3); }
371 | pp_expr tEQ pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, ==) }
372 | pp_expr tNE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, !=) }
373 | pp_expr '<' 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 tLTE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <=) }
376 | pp_expr tGTE pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >=) }
377 | pp_expr '+' 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 tLSHIFT pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, <<) }
385 | pp_expr tRSHIFT pp_expr { promote_equal_size(&$1, &$3); BIN_OP($$, $1, $3, >>) }
386 | '+' pp_expr { $$ = $2; }
387 | '-' pp_expr { UNARY_OP($$, $2, -) }
388 | '~' pp_expr { UNARY_OP($$, $2, ~) }
389 | '!' pp_expr { $$.type = cv_sint; $$.val.si = !boolean(&$2); }
390 | '(' pp_expr ')' { $$ = $2; }
391 | pp_expr '?' pp_expr ':' pp_expr { $$ = boolean(&$1) ? $3 : $5; }
397 **************************************************************************
399 **************************************************************************
402 static void cast_to_sint(cval_t *v)
408 case cv_slong: v->val.si = v->val.sl; break;
409 case cv_ulong: v->val.si = v->val.ul; break;
410 case cv_sll: v->val.si = v->val.sll; break;
411 case cv_ull: v->val.si = v->val.ull; break;
416 static void cast_to_uint(cval_t *v)
422 case cv_slong: v->val.ui = v->val.sl; break;
423 case cv_ulong: v->val.ui = v->val.ul; break;
424 case cv_sll: v->val.ui = v->val.sll; break;
425 case cv_ull: v->val.ui = v->val.ull; break;
430 static void cast_to_slong(cval_t *v)
434 case cv_sint: v->val.sl = v->val.si; break;
435 case cv_uint: v->val.sl = v->val.ui; break;
436 case cv_slong: break;
437 case cv_ulong: break;
438 case cv_sll: v->val.sl = v->val.sll; break;
439 case cv_ull: v->val.sl = v->val.ull; break;
444 static void cast_to_ulong(cval_t *v)
448 case cv_sint: v->val.ul = v->val.si; break;
449 case cv_uint: v->val.ul = v->val.ui; break;
450 case cv_slong: break;
451 case cv_ulong: break;
452 case cv_sll: v->val.ul = v->val.sll; break;
453 case cv_ull: v->val.ul = v->val.ull; break;
458 static void cast_to_sll(cval_t *v)
462 case cv_sint: v->val.sll = v->val.si; break;
463 case cv_uint: v->val.sll = v->val.ui; break;
464 case cv_slong: v->val.sll = v->val.sl; break;
465 case cv_ulong: v->val.sll = v->val.ul; break;
472 static void cast_to_ull(cval_t *v)
476 case cv_sint: v->val.ull = v->val.si; break;
477 case cv_uint: v->val.ull = v->val.ui; break;
478 case cv_slong: v->val.ull = v->val.sl; break;
479 case cv_ulong: v->val.ull = v->val.ul; break;
487 static void promote_equal_size(cval_t *v1, cval_t *v2)
489 #define cv_sizeof(v) ((int)(v->type & SIZE_MASK))
490 int s1 = cv_sizeof(v1);
491 int s2 = cv_sizeof(v2);
500 case cv_sint: cast_to_sint(v2); break;
501 case cv_uint: cast_to_uint(v2); break;
502 case cv_slong: cast_to_slong(v2); break;
503 case cv_ulong: cast_to_ulong(v2); break;
504 case cv_sll: cast_to_sll(v2); break;
505 case cv_ull: cast_to_ull(v2); break;
512 case cv_sint: cast_to_sint(v1); break;
513 case cv_uint: cast_to_uint(v1); break;
514 case cv_slong: cast_to_slong(v1); break;
515 case cv_ulong: cast_to_ulong(v1); break;
516 case cv_sll: cast_to_sll(v1); break;
517 case cv_ull: cast_to_ull(v1); break;
523 static int boolean(cval_t *v)
527 case cv_sint: return v->val.si != (int)0;
528 case cv_uint: return v->val.ui != (unsigned int)0;
529 case cv_slong: return v->val.sl != (long)0;
530 case cv_ulong: return v->val.ul != (unsigned long)0;
531 case cv_sll: return v->val.sll != (wrc_sll_t)0;
532 case cv_ull: return v->val.ull != (wrc_ull_t)0;
537 static marg_t *new_marg(char *str, def_arg_t type)
539 marg_t *ma = pp_xmalloc(sizeof(marg_t));
546 static marg_t *add_new_marg(char *str, def_arg_t type)
548 marg_t *ma = new_marg(str, type);
550 macro_args = pp_xrealloc(macro_args, nmacro_args * sizeof(macro_args[0]));
551 macro_args[nmacro_args-1] = ma;
555 static int marg_index(char *id)
558 for(t = 0; t < nmacro_args; t++)
560 if(!strcmp(id, macro_args[t]->arg))
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 mt->subst.argidx = idx;
572 mt->subst.text = str;
574 mt->next = mt->prev = NULL;
578 static mtext_t *combine_mtext(mtext_t *tail, mtext_t *mtp)
586 if(tail->type == exp_text && mtp->type == exp_text)
588 tail->subst.text = pp_xrealloc(tail->subst.text, strlen(tail->subst.text)+strlen(mtp->subst.text)+1);
589 strcat(tail->subst.text, mtp->subst.text);
590 free(mtp->subst.text);
595 if(tail->type == exp_concat && mtp->type == exp_concat)
601 if(tail->type == exp_concat && mtp->type == exp_text)
603 int len = strlen(mtp->subst.text);
606 /* FIXME: should delete space from head of string */
607 if(isspace(mtp->subst.text[len-1] & 0xff))
608 mtp->subst.text[--len] = '\0';
615 free(mtp->subst.text);
621 if(tail->type == exp_text && mtp->type == exp_concat)
623 int len = strlen(tail->subst.text);
626 if(isspace(tail->subst.text[len-1] & 0xff))
627 tail->subst.text[--len] = '\0';
634 mtp->prev = tail->prev;
635 mtp->next = tail->next;
637 tail->prev->next = mtp;
638 free(tail->subst.text);
650 static char *merge_text(char *s1, char *s2)
654 s1 = pp_xrealloc(s1, l1+l2+1);
655 memcpy(s1+l1, s2, l2+1);