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
29 #include "wpp_private.h"
32 #define UNARY_OP(r, v, OP) \
35 case cv_sint
: r.val.si
= OP v.val.si
; break
; \
36 case cv_uint
: r.val.ui
= OP v.val.ui
; break
; \
37 case cv_slong
: r.val.sl
= OP v.val.sl
; break
; \
38 case cv_ulong
: r.val.ul
= OP v.val.ul
; break
; \
39 case cv_sll
: r.val.sll
= OP v.val.sll
; break
; \
40 case cv_ull
: r.val.ull
= OP v.val.ull
; break
; \
43 #define cv_signed(v) ((v.type & FLAG_SIGNED) != 0)
45 #define BIN_OP_INT(r, v1, v2, OP) \
47 if
(cv_signed
(v1
) && cv_signed
(v2
)) \
48 r.val.si
= v1.val.si OP v2.val.si
; \
49 else if
(cv_signed
(v1
) && !cv_signed
(v2
)) \
50 r.val.si
= v1.val.si OP
(signed) v2.val.ui
; \
51 else if
(!cv_signed
(v1
) && cv_signed
(v2
)) \
52 r.val.si
= (signed) v1.val.ui OP v2.val.si
; \
54 r.val.ui
= v1.val.ui OP v2.val.ui
;
56 #define BIN_OP_LONG(r, v1, v2, OP) \
58 if
(cv_signed
(v1
) && cv_signed
(v2
)) \
59 r.val.sl
= v1.val.sl OP v2.val.sl
; \
60 else if
(cv_signed
(v1
) && !cv_signed
(v2
)) \
61 r.val.sl
= v1.val.sl OP
(signed long) v2.val.ul
; \
62 else if
(!cv_signed
(v1
) && cv_signed
(v2
)) \
63 r.val.sl
= (signed long) v1.val.ul OP v2.val.sl
; \
65 r.val.ul
= v1.val.ul OP v2.val.ul
;
67 #define BIN_OP_LONGLONG(r, v1, v2, OP) \
69 if
(cv_signed
(v1
) && cv_signed
(v2
)) \
70 r.val.sll
= v1.val.sll OP v2.val.sll
; \
71 else if
(cv_signed
(v1
) && !cv_signed
(v2
)) \
72 r.val.sll
= v1.val.sll OP
(__int64
) v2.val.ull
; \
73 else if
(!cv_signed
(v1
) && cv_signed
(v2
)) \
74 r.val.sll
= (__int64
) v1.val.ull OP v2.val.sll
; \
76 r.val.ull
= v1.val.ull OP v2.val.ull
;
78 #define BIN_OP(r, v1, v2, OP) \
79 switch
(v1.type
& SIZE_MASK
) \
81 case SIZE_INT
: BIN_OP_INT
(r
, v1
, v2
, OP
); break
; \
82 case SIZE_LONG
: BIN_OP_LONG
(r
, v1
, v2
, OP
); break
; \
83 case SIZE_LONGLONG
: BIN_OP_LONGLONG
(r
, v1
, v2
, OP
); break
; \
84 default
: pp_internal_error
(__FILE__
, __LINE__
, "Invalid type indicator (0x%04x)", v1.type
); \
91 static int boolean
(cval_t
*v
);
92 static void promote_equal_size
(cval_t
*v1
, cval_t
*v2
);
93 static void cast_to_sint
(cval_t
*v
);
94 static void cast_to_uint
(cval_t
*v
);
95 static void cast_to_slong
(cval_t
*v
);
96 static void cast_to_ulong
(cval_t
*v
);
97 static void cast_to_sll
(cval_t
*v
);
98 static void cast_to_ull
(cval_t
*v
);
99 static marg_t
*new_marg
(char *str
, def_arg_t type
);
100 static marg_t
*add_new_marg
(char *str
, def_arg_t type
);
101 static int marg_index
(char *id
);
102 static mtext_t
*new_mtext
(char *str
, int idx
, def_exp_t type
);
103 static mtext_t
*combine_mtext
(mtext_t
*tail
, mtext_t
*mtp
);
104 static char *merge_text
(char *s1
, char *s2
);
109 static marg_t
**macro_args
; /* Macro parameters array while parsing */
110 static int nmacro_args
;
120 unsigned __int64 ull
;
129 %token tIF tIFDEF tIFNDEF tELSE tELIF tENDIF tDEFINED tNL
130 %token tINCLUDE tLINE tGCCLINE tERROR tWARNING tPRAGMA tPPIDENT
131 %token tUNDEF tMACROEND tCONCAT tELIPSIS tSTRINGIZE
132 %token
<cptr
> tIDENT tLITERAL tMACRO tDEFINE
133 %token
<cptr
> tDQSTRING tSQSTRING tIQSTRING
136 %token
<ulong
> tULONG
137 %token
<slong
> tSLONG
138 %token
<ull
> tULONGLONG
139 %token
<sll
> tSLONGLONG
140 %token
<cptr
> tRCINCLUDEPATH
149 %left
'<' tLTE
'>' tGTE
150 %left tLSHIFT tRSHIFT
156 %type
<marg
> emargs margs
157 %type
<mtext
> opt_mtexts mtexts mtext
158 %type
<sint
> allmargs
159 %type
<cptr
> opt_text text
162 **************************************************************************
163 * The parser starts here
164 **************************************************************************
169 pp_file
: /* Empty */
170 | pp_file preprocessor
174 : tINCLUDE tDQSTRING tNL
{ pp_do_include
($2, 1); }
175 | tINCLUDE tIQSTRING tNL
{ pp_do_include
($2, 0); }
176 | tIF pp_expr tNL
{ pp_next_if_state
(boolean
(&$2)); }
177 | tIFDEF tIDENT tNL
{ pp_next_if_state
(pplookup
($2) != NULL
); free
($2); }
178 | tIFNDEF tIDENT tNL
{
179 int t
= pplookup
($2) == NULL
;
180 if
(pp_incl_state.state
== 0 && t
&& !pp_incl_state.seen_junk
)
182 pp_incl_state.state
= 1;
183 pp_incl_state.ppp
= $2;
184 pp_incl_state.ifdepth
= pp_get_if_depth
();
186 else if
(pp_incl_state.state
!= 1)
188 pp_incl_state.state
= -1;
195 | tELIF pp_expr tNL
{
196 pp_if_state_t s
= pp_pop_if
();
204 pp_push_if
(boolean
(&$2) ? if_true
: if_false
);
207 pp_push_if
(if_ignore
);
211 ppy_error
("#elif cannot follow #else");
216 pp_internal_error
(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #elif directive", s
);
220 pp_if_state_t s
= pp_pop_if
();
224 pp_push_if
(if_elsefalse
);
230 pp_push_if
(if_elsetrue
);
233 pp_push_if
(if_ignore
);
237 ppy_error
("#else clause already defined");
242 pp_internal_error
(__FILE__
, __LINE__
, "Invalid pp_if_state (%d) in #else directive", s
);
246 if
(pp_pop_if
() != if_error
)
248 if
(pp_incl_state.ifdepth
== pp_get_if_depth
() && pp_incl_state.state
== 1)
250 pp_incl_state.state
= 2;
251 pp_incl_state.seen_junk
= 0;
253 else if
(pp_incl_state.state
!= 1)
255 pp_incl_state.state
= -1;
259 | tUNDEF tIDENT tNL
{ pp_del_define
($2); free
($2); }
260 | tDEFINE opt_text tNL
{ pp_add_define
($1, $2); free
($1); free
($2); }
261 | tMACRO res_arg allmargs tMACROEND opt_mtexts tNL
{
262 pp_add_macro
($1, macro_args
, nmacro_args
, $5);
264 | tLINE tSINT tDQSTRING tNL
{ if
($3) pp_writestring
("# %d %s\n", $2 , $3); free
($3); }
265 | tGCCLINE tSINT tDQSTRING tNL
{ if
($3) pp_writestring
("# %d %s\n", $2 , $3); free
($3); }
266 | tGCCLINE tSINT tDQSTRING tSINT tNL
267 { if
($3) pp_writestring
("# %d %s %d\n", $2, $3, $4); free
($3); }
268 | tGCCLINE tSINT tDQSTRING tSINT tSINT tNL
269 { if
($3) pp_writestring
("# %d %s %d %d\n", $2 ,$3, $4, $5); free
($3); }
270 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tNL
271 { if
($3) pp_writestring
("# %d %s %d %d %d\n", $2 ,$3 ,$4 ,$5, $6); free
($3); }
272 | tGCCLINE tSINT tDQSTRING tSINT tSINT tSINT tSINT tNL
273 { if
($3) pp_writestring
("# %d %s %d %d %d %d\n", $2 ,$3 ,$4 ,$5, $6, $7); free
($3); }
274 | tGCCLINE tNL
/* The null-token */
275 | tERROR opt_text tNL
{ ppy_error
("#error directive: '%s'", $2); free
($2); }
276 | tWARNING opt_text tNL
{ ppy_warning
("#warning directive: '%s'", $2); free
($2); }
277 | tPRAGMA opt_text tNL
{ pp_writestring
("#pragma %s\n", $2 ?
$2 : ""); free
($2); }
278 | tPPIDENT opt_text tNL
{ if
(pp_status.pedantic
) ppy_warning
("#ident ignored (arg: '%s')", $2); free
($2); }
279 | tRCINCLUDE tRCINCLUDEPATH
{
282 int nl
=strlen
($2) +3;
283 char *fn
=pp_xmalloc
(nl
);
286 sprintf
(fn
,"\"%s\"",$2);
292 | tRCINCLUDE tDQSTRING
{
298 opt_text: /* Empty */ { $$
= NULL
; }
302 text
: tLITERAL
{ $$
= $1; }
303 | tDQSTRING
{ $$
= $1; }
304 | tSQSTRING
{ $$
= $1; }
305 | text tLITERAL
{ $$
= merge_text
($1, $2); }
306 | text tDQSTRING
{ $$
= merge_text
($1, $2); }
307 | text tSQSTRING
{ $$
= merge_text
($1, $2); }
310 res_arg
: /* Empty */ { macro_args
= NULL
; nmacro_args
= 0; }
313 allmargs: /* Empty */ { $$
= 0; macro_args
= NULL
; nmacro_args
= 0; }
314 | emargs
{ $$
= nmacro_args
; }
317 emargs
: margs
{ $$
= $1; }
318 | margs
',' tELIPSIS
{ $$
= add_new_marg
(NULL
, arg_list
); nmacro_args
*= -1; }
321 margs
: margs
',' tIDENT
{ $$
= add_new_marg
($3, arg_single
); }
322 | tIDENT
{ $$
= add_new_marg
($1, arg_single
); }
326 : /* Empty */ { $$
= NULL
; }
328 for
($$
= $1; $$
&& $$
->prev
; $$
= $$
->prev
)
333 mtexts
: mtext
{ $$
= $1; }
334 | mtexts mtext
{ $$
= combine_mtext
($1, $2); }
337 mtext
: tLITERAL
{ $$
= new_mtext
($1, 0, exp_text
); }
338 | tDQSTRING
{ $$
= new_mtext
($1, 0, exp_text
); }
339 | tSQSTRING
{ $$
= new_mtext
($1, 0, exp_text
); }
340 | tCONCAT
{ $$
= new_mtext
(NULL
, 0, exp_concat
); }
341 | tSTRINGIZE tIDENT
{
342 int mat
= marg_index
($2);
344 ppy_error
("Stringification identifier must be an argument parameter");
346 $$
= new_mtext
(NULL
, mat
, exp_stringize
);
349 int mat
= marg_index
($1);
351 $$
= new_mtext
(NULL
, mat
, exp_subst
);
353 $$
= new_mtext
($1, 0, exp_text
);
357 pp_expr
: tSINT
{ $$.type
= cv_sint
; $$.val.si
= $1; }
358 | tUINT
{ $$.type
= cv_uint
; $$.val.ui
= $1; }
359 | tSLONG
{ $$.type
= cv_slong
; $$.val.sl
= $1; }
360 | tULONG
{ $$.type
= cv_ulong
; $$.val.ul
= $1; }
361 | tSLONGLONG
{ $$.type
= cv_sll
; $$.val.sll
= $1; }
362 | tULONGLONG
{ $$.type
= cv_ull
; $$.val.ull
= $1; }
363 | tDEFINED tIDENT
{ $$.type
= cv_sint
; $$.val.si
= pplookup
($2) != NULL
; }
364 | tDEFINED
'(' tIDENT
')' { $$.type
= cv_sint
; $$.val.si
= pplookup
($3) != NULL
; }
365 | tIDENT
{ $$.type
= cv_sint
; $$.val.si
= 0; }
366 | pp_expr tLOGOR pp_expr
{ $$.type
= cv_sint
; $$.val.si
= boolean
(&$1) || boolean
(&$3); }
367 | pp_expr tLOGAND pp_expr
{ $$.type
= cv_sint
; $$.val.si
= boolean
(&$1) && boolean
(&$3); }
368 | pp_expr tEQ pp_expr
{ promote_equal_size
(&$1, &$3); BIN_OP
($$
, $1, $3, ==); }
369 | pp_expr tNE pp_expr
{ promote_equal_size
(&$1, &$3); BIN_OP
($$
, $1, $3, !=); }
370 | pp_expr
'<' pp_expr
{ promote_equal_size
(&$1, &$3); BIN_OP
($$
, $1, $3, <); }
371 | pp_expr
'>' pp_expr
{ promote_equal_size
(&$1, &$3); BIN_OP
($$
, $1, $3, >); }
372 | pp_expr tLTE pp_expr
{ promote_equal_size
(&$1, &$3); BIN_OP
($$
, $1, $3, <=); }
373 | pp_expr tGTE 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
'^' 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 tLSHIFT pp_expr
{ promote_equal_size
(&$1, &$3); BIN_OP
($$
, $1, $3, <<); }
382 | pp_expr tRSHIFT pp_expr
{ promote_equal_size
(&$1, &$3); BIN_OP
($$
, $1, $3, >>); }
383 |
'+' pp_expr
{ $$
= $2; }
384 |
'-' pp_expr
{ UNARY_OP
($$
, $2, -); }
385 |
'~' pp_expr
{ UNARY_OP
($$
, $2, ~
); }
386 |
'!' pp_expr
{ $$.type
= cv_sint
; $$.val.si
= !boolean
(&$2); }
387 |
'(' pp_expr
')' { $$
= $2; }
388 | pp_expr
'?' pp_expr
':' pp_expr
{ $$
= boolean
(&$1) ?
$3 : $5; }
394 **************************************************************************
396 **************************************************************************
399 static void cast_to_sint
(cval_t
*v
)
405 case cv_slong
: v
->val.si
= v
->val.sl
; break
;
406 case cv_ulong
: v
->val.si
= v
->val.ul
; break
;
407 case cv_sll
: v
->val.si
= v
->val.sll
; break
;
408 case cv_ull
: v
->val.si
= v
->val.ull
; break
;
413 static void cast_to_uint
(cval_t
*v
)
419 case cv_slong
: v
->val.ui
= v
->val.sl
; break
;
420 case cv_ulong
: v
->val.ui
= v
->val.ul
; break
;
421 case cv_sll
: v
->val.ui
= v
->val.sll
; break
;
422 case cv_ull
: v
->val.ui
= v
->val.ull
; break
;
427 static void cast_to_slong
(cval_t
*v
)
431 case cv_sint
: v
->val.sl
= v
->val.si
; break
;
432 case cv_uint
: v
->val.sl
= v
->val.ui
; break
;
433 case cv_slong
: break
;
434 case cv_ulong
: break
;
435 case cv_sll
: v
->val.sl
= v
->val.sll
; break
;
436 case cv_ull
: v
->val.sl
= v
->val.ull
; break
;
441 static void cast_to_ulong
(cval_t
*v
)
445 case cv_sint
: v
->val.ul
= v
->val.si
; break
;
446 case cv_uint
: v
->val.ul
= v
->val.ui
; break
;
447 case cv_slong
: break
;
448 case cv_ulong
: break
;
449 case cv_sll
: v
->val.ul
= v
->val.sll
; break
;
450 case cv_ull
: v
->val.ul
= v
->val.ull
; break
;
455 static void cast_to_sll
(cval_t
*v
)
459 case cv_sint
: v
->val.sll
= v
->val.si
; break
;
460 case cv_uint
: v
->val.sll
= v
->val.ui
; break
;
461 case cv_slong
: v
->val.sll
= v
->val.sl
; break
;
462 case cv_ulong
: v
->val.sll
= v
->val.ul
; break
;
469 static void cast_to_ull
(cval_t
*v
)
473 case cv_sint
: v
->val.ull
= v
->val.si
; break
;
474 case cv_uint
: v
->val.ull
= v
->val.ui
; break
;
475 case cv_slong
: v
->val.ull
= v
->val.sl
; break
;
476 case cv_ulong
: v
->val.ull
= v
->val.ul
; break
;
484 static void promote_equal_size
(cval_t
*v1
, cval_t
*v2
)
486 #define cv_sizeof(v) ((int)(v->type & SIZE_MASK))
487 int s1
= cv_sizeof
(v1
);
488 int s2
= cv_sizeof
(v2
);
497 case cv_sint
: cast_to_sint
(v2
); break
;
498 case cv_uint
: cast_to_uint
(v2
); break
;
499 case cv_slong
: cast_to_slong
(v2
); break
;
500 case cv_ulong
: cast_to_ulong
(v2
); break
;
501 case cv_sll
: cast_to_sll
(v2
); break
;
502 case cv_ull
: cast_to_ull
(v2
); break
;
509 case cv_sint
: cast_to_sint
(v1
); break
;
510 case cv_uint
: cast_to_uint
(v1
); break
;
511 case cv_slong
: cast_to_slong
(v1
); break
;
512 case cv_ulong
: cast_to_ulong
(v1
); break
;
513 case cv_sll
: cast_to_sll
(v1
); break
;
514 case cv_ull
: cast_to_ull
(v1
); break
;
520 static int boolean
(cval_t
*v
)
524 case cv_sint
: return v
->val.si
!= 0;
525 case cv_uint
: return v
->val.ui
!= 0;
526 case cv_slong
: return v
->val.sl
!= 0;
527 case cv_ulong
: return v
->val.ul
!= 0;
528 case cv_sll
: return v
->val.sll
!= 0;
529 case cv_ull
: return v
->val.ull
!= 0;
534 static marg_t
*new_marg
(char *str
, def_arg_t type
)
536 marg_t
*ma
= pp_xmalloc
(sizeof
(marg_t
));
545 static marg_t
*add_new_marg
(char *str
, def_arg_t type
)
547 marg_t
**new_macro_args
;
551 new_macro_args
= pp_xrealloc
(macro_args
, (nmacro_args
+1) * sizeof
(macro_args
[0]));
554 macro_args
= new_macro_args
;
555 ma
= new_marg
(str
, type
);
558 macro_args
[nmacro_args
] = ma
;
563 static int marg_index
(char *id
)
568 for
(t
= 0; t
< nmacro_args
; t
++)
570 if
(!strcmp
(id
, macro_args
[t
]->arg
))
573 return t
< nmacro_args ? t
: -1;
576 static mtext_t
*new_mtext
(char *str
, int idx
, def_exp_t type
)
578 mtext_t
*mt
= pp_xmalloc
(sizeof
(mtext_t
));
582 mt
->subst.argidx
= idx
;
584 mt
->subst.text
= str
;
586 mt
->next
= mt
->prev
= NULL
;
590 static mtext_t
*combine_mtext
(mtext_t
*tail
, mtext_t
*mtp
)
598 if
(tail
->type
== exp_text
&& mtp
->type
== exp_text
)
601 new_text
= pp_xrealloc
(tail
->subst.text
, strlen
(tail
->subst.text
)+strlen
(mtp
->subst.text
)+1);
604 tail
->subst.text
= new_text
;
605 strcat
(tail
->subst.text
, mtp
->subst.text
);
606 free
(mtp
->subst.text
);
611 if
(tail
->type
== exp_concat
&& mtp
->type
== exp_concat
)
617 if
(tail
->type
== exp_concat
&& mtp
->type
== exp_text
)
619 int len
= strlen
(mtp
->subst.text
);
622 /* FIXME: should delete space from head of string */
623 if
(isspace
(mtp
->subst.text
[len
-1] & 0xff))
624 mtp
->subst.text
[--len
] = '\0';
631 free
(mtp
->subst.text
);
637 if
(tail
->type
== exp_text
&& mtp
->type
== exp_concat
)
639 int len
= strlen
(tail
->subst.text
);
642 if
(isspace
(tail
->subst.text
[len
-1] & 0xff))
643 tail
->subst.text
[--len
] = '\0';
650 mtp
->prev
= tail
->prev
;
651 mtp
->next
= tail
->next
;
653 tail
->prev
->next
= mtp
;
654 free
(tail
->subst.text
);
666 static char *merge_text
(char *s1
, char *s2
)
677 snew
= pp_xrealloc
(s1
, l1
+l2
+1);
684 memcpy
(s1
+l1
, s2
, l2
+1);