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
30 #define UNARY_OP(r, v, OP) \
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) \
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
; \
52 r.val.ui
= v1.val.ui OP v2.val.ui
;
54 #define BIN_OP_LONG(r, v1, v2, OP) \
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
; \
63 r.val.ul
= v1.val.ul OP v2.val.ul
;
65 #define BIN_OP_LONGLONG(r, v1, v2, OP) \
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
; \
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
) \
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
); \
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
);
107 static marg_t
**macro_args
; /* Macro parameters array while parsing */
108 static int nmacro_args
;
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
134 %token
<ulong
> tULONG
135 %token
<slong
> tSLONG
136 %token
<ull
> tULONGLONG
137 %token
<sll
> tSLONGLONG
138 %token
<cptr
> tRCINCLUDEPATH
147 %left
'<' tLTE
'>' tGTE
148 %left tLSHIFT tRSHIFT
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
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
)
182 include_ifdepth
= get_if_depth
();
184 else if
(include_state
!= 1)
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
();
204 push_if
(boolean
(&$2) ? if_true
: if_false
);
211 pperror
("#elif cannot follow #else");
213 internal_error
(__FILE__
, __LINE__
, "Invalid if_state (%d) in #elif directive", s
);
217 if_state_t s
= pop_if
();
221 push_if
(if_elsefalse
);
227 push_if
(if_elsetrue
);
234 pperror
("#else clause already defined");
236 internal_error
(__FILE__
, __LINE__
, "Invalid if_state (%d) in #else directive", s
);
241 if
(include_ifdepth
== get_if_depth
() && include_state
== 1)
246 else if
(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);
280 | tRCINCLUDE tDQSTRING
{
286 opt_text: /* Empty */ { $$
= NULL
; }
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
); }
314 : /* Empty */ { $$
= NULL
; }
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);
332 pperror
("Stringification identifier must be an argument parameter");
333 $$
= new_mtext
(NULL
, mat
, exp_stringize
);
336 int mat
= marg_index
($1);
338 $$
= new_mtext
(NULL
, mat
, exp_subst
);
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 **************************************************************************
383 **************************************************************************
386 static void cast_to_sint
(cval_t
*v
)
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
;
400 static void cast_to_uint
(cval_t
*v
)
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
;
414 static void cast_to_slong
(cval_t
*v
)
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
;
428 static void cast_to_ulong
(cval_t
*v
)
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
;
442 static void cast_to_sll
(cval_t
*v
)
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
;
456 static void cast_to_ull
(cval_t
*v
)
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
;
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
);
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
;
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
)
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;
521 static marg_t
*new_marg
(char *str
, def_arg_t type
)
523 marg_t
*ma
= (marg_t
*)xmalloc
(sizeof
(marg_t
));
529 static marg_t
*add_new_marg
(char *str
, def_arg_t type
)
531 marg_t
*ma
= new_marg
(str
, type
);
533 macro_args
= (marg_t
**)xrealloc
(macro_args
, nmacro_args
* sizeof
(macro_args
[0]));
534 macro_args
[nmacro_args
-1] = ma
;
538 static int marg_index
(char *id
)
541 for
(t
= 0; t
< nmacro_args
; t
++)
543 if
(!strcmp
(id
, macro_args
[t
]->arg
))
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
));
553 mt
->subst.argidx
= idx
;
555 mt
->subst.text
= str
;
560 static mtext_t
*combine_mtext
(mtext_t
*tail
, mtext_t
*mtp
)
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
);
577 if
(tail
->type
== exp_concat
&& mtp
->type
== exp_concat
)
583 if
(tail
->type
== exp_concat
&& mtp
->type
== exp_text
)
585 int len
= strlen
(mtp
->subst.text
);
588 /* FIXME: should delete space from head of string */
589 if
(isspace
(mtp
->subst.text
[len
-1] & 0xff))
590 mtp
->subst.text
[--len
] = '\0';
597 free
(mtp
->subst.text
);
603 if
(tail
->type
== exp_text
&& mtp
->type
== exp_concat
)
605 int len
= strlen
(tail
->subst.text
);
608 if
(isspace
(tail
->subst.text
[len
-1] & 0xff))
609 tail
->subst.text
[--len
] = '\0';
616 mtp
->prev
= tail
->prev
;
617 mtp
->next
= tail
->next
;
619 tail
->prev
->next
= mtp
;
620 free
(tail
->subst.text
);
632 static char *merge_text
(char *s1
, char *s2
)
636 s1
= xrealloc
(s1
, l1
+l2
+1);
637 memcpy
(s1
+l1
, s2
, l2
+1);