Debian Codegraph @

cc/cxxcom/cgram.y

raw
1 /* $Id: cgram.y,v 1.11 2016/10/11 13:48:24 ragge Exp $ */
2
3 /*
4 * Copyright (c) 2003 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 *
35 * Redistributions of source code and documentation must retain the above
36 * copyright notice, this list of conditions and the following disclaimer.
37 * Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed or owned by Caldera
43 * International, Inc.
44 * Neither the name of Caldera International, Inc. nor the names of other
45 * contributors may be used to endorse or promote products derived from
46 * this software without specific prior written permission.
47 *
48 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
49 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
53 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
58 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * Comments for this grammar file. Ragge 021123
64 *
65 * ANSI support required rewrite of the function header and declaration
66 * rules almost totally.
67 *
68 * The lex/yacc shared keywords are now split from the keywords used
69 * in the rest of the compiler, to simplify use of other frontends.
70 */
71
72 /*
73 * At last count, there were 5 shift/reduce and no reduce/reduce conflicts
74 * Four are accounted for;
75 * One is "dangling else"
76 * Two is in attribute parsing
77 * One is in ({ }) parsing
78 */
79
80 /*
81 * Token used in C lex/yacc communications.
82 */
83 %token C_STRING /* a string constant */
84 %token C_ICON /* an integer constant */
85 %token C_FCON /* a floating point constant */
86 %token C_NAME /* an identifier */
87 %token C_TYPENAME /* a typedef'd name */
88 %token C_ANDAND /* && */
89 %token C_OROR /* || */
90 %token C_GOTO /* unconditional goto */
91 %token C_RETURN /* return from function */
92 %token C_TYPE /* a type */
93 %token C_CLASS /* a storage class */
94 %token C_ASOP /* assignment ops */
95 %token C_RELOP /* <=, <, >=, > */
96 %token C_EQUOP /* ==, != */
97 %token C_DIVOP /* /, % */
98 %token C_SHIFTOP /* <<, >> */
99 %token C_INCOP /* ++, -- */
100 %token C_UNOP /* !, ~ */
101 %token C_STROP /* ., -> */
102 %token C_STRUCT
103 %token C_IF
104 %token C_ELSE
105 %token C_SWITCH
106 %token C_BREAK
107 %token C_CONTINUE
108 %token C_WHILE
109 %token C_DO
110 %token C_FOR
111 %token C_DEFAULT
112 %token C_CASE
113 %token C_SIZEOF
114 %token C_ALIGNOF
115 %token C_ENUM
116 %token C_ELLIPSIS
117 %token C_QUALIFIER
118 %token C_FUNSPEC
119 %token C_ASM
120 %token NOMATCH
121 %token C_TYPEOF /* COMPAT_GCC */
122 %token C_ATTRIBUTE /* COMPAT_GCC */
123 %token PCC_OFFSETOF
124 %token GCC_DESIG
125 %token CXX_NAMESPACE
126 %token CXX_DUALCC
127 %token CXX_TEMPLATE
128 %token CXX_USING
129 %token CXX_TYPENAME
130 %token CXX_CASTS
131 %token CXX_THROW
132 %token CXX_MORENM
133 %token CXX_NEW
134 %token CXX_DELETE
135 %token CXX_CLASS
136
137 /*
138 * Precedence
139 */
140 %left ','
141 %right '=' C_ASOP
142 %right '?' ':'
143 %left C_OROR
144 %left C_ANDAND
145 %left '|'
146 %left '^'
147 %left '&'
148 %left C_EQUOP
149 %left C_RELOP
150 %left C_SHIFTOP
151 %left '+' '-'
152 %left '*' C_DIVOP
153 %right C_UNOP
154 %right C_INCOP C_SIZEOF
155 %left '[' '(' C_STROP
156 %{
157 # include "pass1.h"
158 # include <stdarg.h>
159 # include <string.h>
160 # include <stdlib.h>
161
162 int fun_inline; /* Reading an inline function */
163 int oldstyle; /* Current function being defined */
164 static struct symtab *xnf;
165 extern int enummer, tvaloff, inattr;
166 extern struct rstack *rpole;
167 static int widestr, alwinl;
168 NODE *cftnod;
169 static int attrwarn = 1;
170
171 #define NORETYP SNOCREAT /* no return type, save in unused field in symtab */
172
173 NODE *bdty(int op, ...);
174 static void fend(struct symtab *);
175 static struct symtab *fundef(NODE *tp, NODE *p);
176 static void olddecl(NODE *p, NODE *a);
177 static struct symtab *init_declarator(NODE *tn, NODE *p, int assign, NODE *a);
178 static void resetbc(int mask);
179 static void swend(void);
180 static void addcase(NODE *p);
181 #ifdef GCC_COMPAT
182 static void gcccase(NODE *p, NODE *);
183 #endif
184 static struct attr *gcc_attr_wrapper(NODE *p);
185 static void adddef(void);
186 static void savebc(void);
187 static void swstart(int, TWORD);
188 static void genswitch(int, TWORD, struct swents **, int);
189 static char *mkpstr(char *str);
190 static struct symtab *clbrace(NODE *);
191 static NODE *cmop(NODE *l, NODE *r);
192 static NODE *xcmop(NODE *out, NODE *in, NODE *str);
193 static void mkxasm(char *str, NODE *p);
194 static NODE *xasmop(char *str, NODE *p);
195 static int maxstlen(char *str);
196 static char *stradd(char *old, char *new);
197 static NODE *biop(int op, NODE *l, NODE *r);
198 static void flend(void);
199 static char * simname(char *s);
200 static NODE *tyof(NODE *); /* COMPAT_GCC */
201 static NODE *voidcon(void); /* COMPAT_GCC */
202 static NODE *funargs(NODE *p);
203 static void oldargs(NODE *p);
204 static void uawarn(NODE *p, char *s);
205 static int con_e(NODE *p);
206 static void dainit(NODE *d, NODE *a);
207 static NODE *tymfix(NODE *p);
208 static NODE *namekill(NODE *p, int clr);
209 static NODE *aryfix(NODE *p);
210
211 #define TYMFIX(inp) { \
212 NODE *pp = inp; \
213 inp = tymerge(pp->n_left, pp->n_right); \
214 nfree(pp->n_left); nfree(pp); }
215 /*
216 * State for saving current switch state (when nested switches).
217 */
218 struct savbc {
219 struct savbc *next;
220 int brklab;
221 int contlab;
222 int flostat;
223 int swx;
224 } *savbc, *savctx;
225
226 %}
227
228 %union {
229 int intval;
230 NODE *nodep;
231 struct symtab *symp;
232 struct rstack *rp;
233 char *strp;
234 }
235
236 /* define types */
237 %start edf
238
239 %type <intval> ifelprefix ifprefix whprefix forprefix doprefix switchpart
240 xbegin
241 %type <nodep> e .e term enum_dcl struct_dcl cast_type declarator
242 elist type_sq cf_spec merge_attribs
243 parameter_declaration abstract_declarator initializer
244 parameter_type_list parameter_list addrlbl
245 declaration_specifiers designation
246 specifier_qualifier_list merge_specifiers
247 identifier_list arg_param_list type_qualifier_list
248 designator_list designator xasm oplist oper cnstr
249 typeof attribute attribute_specifier /* COMPAT_GCC */
250 attribute_list attr_spec_list attr_var /* COMPAT_GCC */
251 new_ma new_type_sq new_ds nmrec
252 %type <strp> string C_STRING GCC_DESIG nsname CXX_MORENM
253 %type <rp> str_head
254 %type <symp> xnfdeclarator clbrace enum_head funtype
255
256 %type <intval> C_STRUCT C_RELOP C_DIVOP C_SHIFTOP
257 C_ANDAND C_OROR C_STROP C_INCOP C_UNOP C_ASOP C_EQUOP
258
259 %type <nodep> C_TYPE C_QUALIFIER C_ICON C_FCON C_CLASS
260 %type <strp> C_NAME C_TYPENAME
261 %%
262
263 edf: ext_def_list
264 | { ftnend(); }
265 ;
266
267 ext_def_list: ext_def_list external_def
268 | external_def
269 ;
270
271 external_def: funtype kr_args compoundstmt { fend($1); }
272 | declaration { blevel = 0; symclear(0); }
273 | namespace
274 | extlink
275 | blockdcl
276 | ';'
277 | error { blevel = 0; }
278 ;
279
280 funtype: /* no type given */ declarator {
281 $$ = fundef(mkty(INT, 0, 0), $1);
282 cftnsp->sflags |= NORETYP;
283 }
284 | declaration_specifiers declarator { $$ = fundef($1,$2); }
285 ;
286
287 kr_args: /* empty */
288 | arg_dcl_list
289 ;
290
291 blockdcl: simple_decl
292 | asmstatement
293 | ns_alias
294 | using_x
295 ;
296
297 using_x: CXX_USING tnopt ccopt nested_name_sp ';' { werror("using"); }
298 | CXX_USING CXX_NAMESPACE ccopt nested_name_sp ';' { werror("using2"); }
299 ;
300
301 tnopt: CXX_TYPENAME { }
302 | { }
303 ;
304
305 ns_alias: CXX_NAMESPACE C_NAME '=' qual_ns_sp ';' { werror("ns_alias");}
306 ;
307
308 qual_ns_sp: ccopt nested_name_sp
309 ;
310
311 nested_name_sp: nmtnm
312 | nmtnm CXX_DUALCC nested_name_sp
313 | nmtnm CXX_DUALCC CXX_TEMPLATE nested_name_sp
314 ;
315
316 nmtnm: C_NAME
317 | C_TYPENAME
318 ;
319
320 ccopt: CXX_DUALCC
321 | { }
322 ;
323
324 simple_decl: ')' NOMATCH { uerror("simple-declaration"); }
325 ;
326
327 namespace: CXX_NAMESPACE nsname attr_var nsbeg ns_body '}' { POPSYM(); }
328 ;
329
330 nsname: C_NAME
331 | { $$ = NULL; }
332 ;
333
334 ns_body: ext_def_list
335 | { }
336 ;
337
338 nsbeg: '{' { dclns($<nodep>0, $<strp>-1); }
339 ;
340
341 extlink: C_CLASS C_STRING eb '{' ext_def_list '}' { elnk = LINK_DEF; }
342 | C_CLASS C_STRING eb '{' '}' { elnk = LINK_DEF; }
343 | C_CLASS C_STRING eb declaration { elnk = LINK_DEF; }
344 ;
345
346 eb: {
347 NODE *p = $<nodep>-1;
348 char *s = $<strp>0;
349 if (p->n_type != EXTERN)
350 uerror("'extern' expected");
351 if (strcmp(s, "\"C\"") != 0 && strcmp(s, "\"c\""))
352 uerror("unknown linkage %s", s);
353 nfree(p);
354 elnk = LINK_C;
355 }
356 ;
357 /*
358 * Returns a node pointer or NULL, if no types at all given.
359 * Type trees are checked for correctness and merged into one
360 * type node in typenode().
361 */
362 declaration_specifiers:
363 merge_attribs { $$ = typenode($1); }
364 ;
365
366 merge_attribs: type_sq { $$ = $1; }
367 | type_sq merge_attribs { $$ = cmop($2, $1); }
368 | cf_spec { $$ = $1; }
369 | cf_spec merge_attribs { $$ = cmop($2, $1); }
370 ;
371
372 type_sq: C_TYPE { $$ = $1; }
373 | C_TYPENAME {
374 struct symtab *sp = lookup($1, 0);
375 if (sp->stype == ENUMTY) {
376 sp->stype = strmemb(sp->sap)->stype;
377 }
378 $$ = mkty(sp->stype, sp->sdf, sp->sap);
379 $$->n_sp = sp;
380 }
381 | struct_dcl { $$ = $1; }
382 | enum_dcl { $$ = $1; }
383 | C_QUALIFIER { $$ = $1; }
384 | attribute_specifier { $$ = biop(ATTRIB, $1, 0); }
385 | typeof { $$ = $1; }
386 ;
387
388 cf_spec: C_CLASS { $$ = $1; }
389 | C_FUNSPEC { fun_inline = 1; /* XXX - hack */
390 $$ = block(QUALIFIER, NIL, NIL, 0, 0, 0); }
391 ;
392
393 typeof: C_TYPEOF '(' e ')' { $$ = tyof(eve($3)); }
394 | C_TYPEOF '(' cast_type ')' { TYMFIX($3); $$ = tyof($3); }
395 ;
396
397 new_ma: new_type_sq
398 | new_type_sq new_ma { $$ = cmop($2, $1); }
399 | cf_spec { $$ = $1; }
400 | cf_spec new_ma { $$ = cmop($2, $1); }
401 ;
402
403 new_ds: new_ma { $$ = typenode($1); }
404 ;
405
406 new_type_sq: C_TYPE { $$ = $1; }
407 | C_TYPENAME {
408 struct symtab *sp = lookup($1, 0);
409 if (sp->stype == ENUMTY) {
410 sp->stype = strmemb(sp->sap)->stype;
411 }
412 $$ = mkty(sp->stype, sp->sdf, sp->sap);
413 $$->n_sp = sp;
414 }
415 | struct_dcl { $$ = $1; }
416 | enum_dcl { $$ = $1; }
417 | C_QUALIFIER { $$ = $1; }
418 ;
419
420 attribute_specifier :
421 C_ATTRIBUTE '(' '(' attribute_list ')' ')' { $$ = $4; }
422 /*COMPAT_GCC*/ ;
423
424 attribute_list: attribute
425 | attribute ',' attribute_list { $$ = cmop($3, $1); }
426 ;
427
428 attribute: {
429 #ifdef GCC_COMPAT
430 $$ = voidcon();
431 #endif
432 }
433 | C_NAME { $$ = bdty(NAME, $1); }
434 | C_NAME '(' elist ')' {
435 $$ = bdty($3 == NIL ? UCALL : CALL, bdty(NAME, $1), $3);
436 }
437 ;
438
439 /*
440 * Adds a pointer list to front of the declarators.
441 */
442 declarator: '*' declarator { $$ = bdty(UMUL, $2); }
443 | '*' type_qualifier_list declarator {
444 $$ = $2;
445 $$->n_left = $3;
446 }
447 | nmrec C_NAME { $$ = biop(NMLIST, $1, bdty(NAME, $2)); }
448 | C_NAME { $$ = bdty(NAME, $1); }
449 | '(' attr_spec_list declarator ')' {
450 $$ = $3;
451 $$->n_ap = attr_add($$->n_ap, gcc_attr_wrapper($2));
452 }
453 | '(' declarator ')' { $$ = $2; }
454 | declarator '[' e ']' { $$ = biop(LB, $1, $3); }
455 | declarator '[' C_CLASS e ']' {
456 if ($3->n_type != STATIC)
457 uerror("bad class keyword");
458 tfree($3); /* XXX - handle */
459 $$ = biop(LB, $1, $4);
460 }
461 | declarator '[' ']' { $$ = biop(LB, $1, bcon(NOOFFSET)); }
462 | declarator '[' '*' ']' { $$ = biop(LB, $1, bcon(NOOFFSET)); }
463 | declarator '(' parameter_type_list ')' {
464 $$ = bdty(CALL, $1, $3);
465 }
466 | declarator '(' identifier_list ')' {
467 $$ = bdty(CALL, $1, $3);
468 oldstyle = 1;
469 }
470 | declarator '(' ')' { $$ = bdty(UCALL, $1); }
471 ;
472
473 type_qualifier_list:
474 C_QUALIFIER { $$ = $1; $$->n_op = UMUL; }
475 | type_qualifier_list C_QUALIFIER {
476 $$ = $1;
477 $$->n_qual |= $2->n_qual;
478 nfree($2);
479 }
480 | attribute_specifier {
481 $$ = block(UMUL, NIL, NIL, 0, 0, gcc_attr_wrapper($1));
482 }
483 | type_qualifier_list attribute_specifier {
484 $1->n_ap = attr_add($1->n_ap, gcc_attr_wrapper($2));
485 }
486 ;
487
488 identifier_list: C_NAME { $$ = bdty(NAME, $1); oldargs($$); }
489 | identifier_list ',' C_NAME {
490 $$ = cmop($1, bdty(NAME, $3));
491 oldargs($$->n_right);
492 }
493 ;
494
495 /*
496 * Returns as parameter_list, but can add an additional ELLIPSIS node.
497 */
498 parameter_type_list:
499 parameter_list { $$ = $1; }
500 | parameter_list ',' C_ELLIPSIS {
501 $$ = cmop($1, biop(ELLIPSIS, NIL, NIL));
502 }
503 ;
504
505 /*
506 * Returns a linked lists of nodes of op CM with parameters on
507 * its right and additional CM nodes of its left pointer.
508 * No CM nodes if only one parameter.
509 */
510 parameter_list: parameter_declaration { $$ = $1; }
511 | parameter_declaration '&' { $$ = $1; }
512 | parameter_list ',' parameter_declaration {
513 $$ = cmop($1, $3);
514 }
515 ;
516
517 /*
518 * Returns a node pointer to the declaration.
519 */
520 parameter_declaration:
521 declaration_specifiers declarator attr_var {
522 if (glval($1) != SNULL && glval($1) != REGISTER)
523 uerror("illegal parameter class");
524 $$ = block(TYMERGE, $1, $2, INT, 0, gcc_attr_wrapper($3));
525 }
526 | declaration_specifiers abstract_declarator {
527 $$ = block(TYMERGE, $1, $2, INT, 0, 0);
528 }
529 | declaration_specifiers {
530 $$ = block(TYMERGE, $1, bdty(NAME, NULL), INT, 0, 0);
531 }
532 ;
533
534 abstract_declarator:
535 '*' { $$ = bdty(UMUL, bdty(NAME, NULL)); }
536 | '*' type_qualifier_list {
537 $$ = $2;
538 $$->n_left = bdty(NAME, NULL);
539 }
540 | '*' abstract_declarator { $$ = bdty(UMUL, $2); }
541 | '*' type_qualifier_list abstract_declarator {
542 $$ = $2;
543 $$->n_left = $3;
544 }
545 | '(' abstract_declarator ')' { $$ = $2; }
546 | '[' ']' attr_var {
547 $$ = block(LB, bdty(NAME, NULL), bcon(NOOFFSET),
548 INT, 0, gcc_attr_wrapper($3));
549 }
550 | '[' e ']' attr_var {
551 $$ = block(LB, bdty(NAME, NULL), $2,
552 INT, 0, gcc_attr_wrapper($4));
553 }
554 | abstract_declarator '[' ']' attr_var {
555 $$ = block(LB, $1, bcon(NOOFFSET),
556 INT, 0, gcc_attr_wrapper($4));
557 }
558 | abstract_declarator '[' e ']' attr_var {
559 $$ = block(LB, $1, $3, INT, 0, gcc_attr_wrapper($5));
560 }
561 | '(' ')' { $$ = bdty(UCALL, bdty(NAME, NULL)); }
562 | '(' ib2 parameter_type_list ')' {
563 $$ = bdty(CALL, bdty(NAME, NULL), $3);
564 }
565 | abstract_declarator '(' ')' {
566 $$ = bdty(UCALL, $1);
567 }
568 | abstract_declarator '(' ib2 parameter_type_list ')' {
569 $$ = bdty(CALL, $1, $4);
570 }
571 ;
572
573 ib2: { }
574 ;
575 /*
576 * K&R arg declaration, between ) and {
577 */
578 arg_dcl_list: arg_declaration
579 | arg_dcl_list arg_declaration
580 ;
581
582
583 arg_declaration: declaration_specifiers arg_param_list ';' {
584 nfree($1);
585 }
586 ;
587
588 arg_param_list: declarator attr_var {
589 olddecl(block(TYMERGE, ccopy($<nodep>0), $1,
590 INT, 0, 0), $2);
591 }
592 | arg_param_list ',' declarator attr_var {
593 olddecl(block(TYMERGE, ccopy($<nodep>0), $3,
594 INT, 0, 0), $4);
595 }
596 ;
597
598 /*
599 * Declarations in beginning of blocks.
600 */
601 block_item_list: block_item
602 | block_item_list block_item
603 ;
604
605 block_item: declaration
606 | statement
607 ;
608
609 /*
610 * Here starts the old YACC code.
611 */
612
613 /*
614 * Variables are declared in init_declarator.
615 */
616 declaration: declaration_specifiers ';' { tfree($1); fun_inline = 0; }
617 | declaration_specifiers init_declarator_list ';' {
618 tfree($1);
619 fun_inline = 0;
620 }
621 ;
622
623 /*
624 * Normal declaration of variables. curtype contains the current type node.
625 * Returns nothing, variables are declared in init_declarator.
626 */
627 init_declarator_list:
628 init_declarator { symclear(blevel); }
629 | init_declarator_list ',' attr_var { $<nodep>$ = $<nodep>0; } init_declarator {
630 uawarn($3, "init_declarator");
631 symclear(blevel);
632 }
633 ;
634
635 enum_dcl: enum_head '{' moe_list optcomma '}' { $$ = enumdcl($1); }
636 | C_ENUM C_NAME { $$ = enumref($2); }
637 ;
638
639 enum_head: C_ENUM { $$ = enumhd(NULL); }
640 | C_ENUM C_NAME { $$ = enumhd($2); }
641 ;
642
643 moe_list: moe
644 | moe_list ',' moe
645 ;
646
647 moe: C_NAME { moedef($1); }
648 | C_TYPENAME { moedef($1); }
649 | C_NAME '=' e { enummer = con_e($3); moedef($1); }
650 | C_TYPENAME '=' e { enummer = con_e($3); moedef($1); }
651 ;
652
653 struct_dcl: str_head '{' struct_dcl_list '}' {
654 NODE *p;
655
656 $$ = dclstruct($1);
657 if (pragma_allpacked) {
658 p = bdty(CALL, bdty(NAME, "packed"),
659 bcon(pragma_allpacked));
660 $$->n_ap = attr_add($$->n_ap,gcc_attr_wrapper(p)); }
661 }
662 | C_STRUCT attr_var C_NAME {
663 $$ = rstruct($3,$1);
664 uawarn($2, "struct_dcl");
665 }
666 | C_STRUCT attr_var nmrec C_NAME {
667 $$ = cxxrstruct($1,$2,$3,$4);
668 uawarn($2, "struct_dcl");
669 }
670 /*COMPAT_GCC*/ | str_head '{' '}' { $$ = dclstruct($1); }
671 ;
672
673 attr_var: {
674 NODE *q, *p;
675
676 p = pragma_aligned ? bdty(CALL, bdty(NAME, "aligned"),
677 bcon(pragma_aligned)) : NIL;
678 if (pragma_packed) {
679 q = bdty(NAME, "packed");
680 p = (p == NIL ? q : cmop(p, q));
681 }
682 pragma_aligned = pragma_packed = 0;
683 $$ = p;
684 }
685 /*COMPAT_GCC*/ | attr_spec_list
686 ;
687
688 attr_spec_list: attribute_specifier
689 | attr_spec_list attribute_specifier { $$ = cmop($1, $2); }
690 ;
691
692 str_head: C_STRUCT attr_var { $$ = bstruct(NULL, $1, $2); }
693 | C_STRUCT attr_var C_NAME { $$ = bstruct($3, $1, $2); }
694 ;
695
696 struct_dcl_list: struct_declaration
697 | struct_dcl_list struct_declaration
698 ;
699
700 struct_declaration:
701 declaration_specifiers struct_declarator_list optsemi {
702 tfree($1);
703 }
704 | C_NAME ':' { /* cxxaccess($1); */ }
705 ;
706
707 optsemi: ';' { }
708 | optsemi ';' { werror("extra ; in struct"); }
709 ;
710
711 specifier_qualifier_list:
712 merge_specifiers { $$ = typenode($1); }
713 ;
714
715 merge_specifiers: type_sq merge_specifiers { $$ = cmop($2, $1); }
716 | type_sq { $$ = $1; }
717 ;
718
719 struct_declarator_list:
720 struct_declarator { symclear(blevel); }
721 | struct_declarator_list ',' { $<nodep>$=$<nodep>0; }
722 struct_declarator { symclear(blevel); }
723 ;
724
725 struct_declarator: declarator attr_var {
726 NODE *p;
727
728 $1 = aryfix($1);
729 p = tymerge($<nodep>0, tymfix($1));
730 if ($2)
731 p->n_ap = attr_add(p->n_ap, gcc_attr_wrapper($2));
732 soumemb(p, (char *)$1->n_sp, glval($<nodep>0));
733 tfree(p);
734 }
735 | ':' e {
736 int ie = con_e($2);
737 if (fldchk(ie))
738 ie = 1;
739 falloc(NULL, ie, $<nodep>0);
740 }
741 | declarator ':' e {
742 int ie = con_e($3);
743 if (fldchk(ie))
744 ie = 1;
745 if ($1->n_op == NAME) {
746 /* XXX - tymfix() may alter $1 */
747 tymerge($<nodep>0, tymfix($1));
748 soumemb($1, (char *)$1->n_sp, FIELD | ie);
749 nfree($1);
750 } else
751 uerror("illegal declarator");
752 }
753 | declarator ':' e attr_spec_list {
754 int ie = con_e($3);
755 if (fldchk(ie))
756 ie = 1;
757 if ($1->n_op == NAME) {
758 /* XXX - tymfix() may alter $1 */
759 tymerge($<nodep>0, tymfix($1));
760 if ($4)
761 $1->n_ap = attr_add($1->n_ap,
762 gcc_attr_wrapper($4));
763 soumemb($1, (char *)$1->n_sp, FIELD | ie);
764 nfree($1);
765 } else
766 uerror("illegal declarator");
767 }
768 | /* unnamed member */ {
769 NODE *p = $<nodep>0;
770 char *c = permalloc(10);
771
772 if (p->n_type != STRTY && p->n_type != UNIONTY)
773 uerror("bad unnamed member type");
774 snprintf(c, 10, "*%dFAKE", getlab());
775 soumemb(p, c, 0);
776 }
777 ;
778
779 /* always preceeded by attributes */
780 xnfdeclarator: declarator attr_var {
781 $$ = xnf = init_declarator($<nodep>0, $1, 1, $2);
782 }
783 | declarator C_ASM '(' string ')' {
784 pragma_renamed = newstring($4, strlen($4));
785 $$ = xnf = init_declarator($<nodep>0, $1, 1, NULL);
786 }
787 ;
788
789 /*
790 * Handles declarations and assignments.
791 * Returns nothing.
792 */
793 init_declarator: declarator attr_var { init_declarator($<nodep>0, $1, 0, $2);}
794 | declarator C_ASM '(' string ')' attr_var {
795 #ifdef GCC_COMPAT
796 pragma_renamed = newstring($4, strlen($4));
797 init_declarator($<nodep>0, $1, 0, $6);
798 #else
799 werror("gcc extension");
800 init_declarator($<nodep>0, $1, 0, $6);
801 #endif
802 }
803 | xnfdeclarator '=' e {
804 if ($1->sclass == STATIC || $1->sclass == EXTDEF)
805 statinit++;
806 simpleinit($1, eve($3));
807 if ($1->sclass == STATIC || $1->sclass == EXTDEF)
808 statinit--;
809 xnf = NULL;
810 }
811 | xnfdeclarator '=' begbr init_list optcomma '}' {
812 endinit(0);
813 xnf = NULL;
814 }
815 /*COMPAT_GCC*/ | xnfdeclarator '=' begbr '}' { endinit(0); xnf = NULL; }
816 | xnfdeclarator '=' addrlbl { simpleinit($1, $3); xnf = NULL; }
817 ;
818
819 begbr: '{' { beginit($<symp>-1); }
820 ;
821
822 initializer: e %prec ',' { $$ = eve($1); }
823 | addrlbl { $$ = $1; }
824 | ibrace init_list optcomma '}' { $$ = NULL; }
825 | ibrace '}' { asginit(bcon(0)); $$ = NULL; }
826 ;
827
828 init_list: designation initializer { dainit($1, $2); }
829 | init_list ',' designation initializer { dainit($3, $4); }
830 ;
831
832 designation: designator_list '=' { desinit($1); $$ = NIL; }
833 | GCC_DESIG { desinit(bdty(NAME, $1)); $$ = NIL; }
834 | '[' e C_ELLIPSIS e ']' '=' { $$ = biop(CM, $2, $4); }
835 | { $$ = NIL; }
836 ;
837
838 designator_list: designator { $$ = $1; }
839 | designator_list designator { $$ = $2; $$->n_left = $1; }
840 ;
841
842 designator: '[' e ']' {
843 int ie = con_e($2);
844 if (ie < 0) {
845 uerror("designator must be non-negative");
846 ie = 0;
847 }
848 $$ = biop(LB, NIL, bcon(ie));
849 }
850 | C_STROP C_TYPENAME {
851 if ($1 != DOT)
852 uerror("invalid designator");
853 $$ = bdty(NAME, $2);
854 }
855 | C_STROP C_NAME {
856 if ($1 != DOT)
857 uerror("invalid designator");
858 $$ = bdty(NAME, $2);
859 }
860 ;
861
862 optcomma : /* VOID */
863 | ','
864 ;
865
866 ibrace: '{' { ilbrace(); }
867 ;
868
869 /* STATEMENTS */
870
871 compoundstmt: begin block_item_list '}' { flend(); }
872 | begin '}' { flend(); }
873 ;
874
875 begin: '{' {
876 struct savbc *bc = tmpalloc(sizeof(struct savbc));
877 if (blevel == 1) {
878 #ifdef STABS
879 if (gflag)
880 stabs_line(lineno);
881 #endif
882 dclargs();
883 }
884 #ifdef STABS
885 if (gflag && blevel > 1)
886 stabs_lbrac(blevel+1);
887 #endif
888 ++blevel;
889 oldstyle = 0;
890 bc->contlab = autooff;
891 bc->next = savctx;
892 savctx = bc;
893 if (!isinlining && sspflag && blevel == 2)
894 sspstart();
895 }
896 ;
897
898 statement: e ';' { /* fwalk($1, eprint, 0); */ ecomp(eve($1)); symclear(blevel); }
899 | compoundstmt
900 | ifprefix statement { plabel($1); reached = 1; }
901 | ifelprefix statement {
902 if ($1 != NOLAB) {
903 plabel( $1);
904 reached = 1;
905 }
906 }
907 | whprefix statement {
908 branch(contlab);
909 plabel( brklab );
910 if( (flostat&FBRK) || !(flostat&FLOOP))
911 reached = 1;
912 else
913 reached = 0;
914 resetbc(0);
915 }
916 | doprefix statement C_WHILE '(' e ')' ';' {
917 plabel(contlab);
918 if (flostat & FCONT)
919 reached = 1;
920 if (reached)
921 cbranch(buildtree(NE, eve($5), bcon(0)),
922 bcon($1));
923 else
924 tfree(eve($5));
925 plabel( brklab);
926 reached = 1;
927 resetbc(0);
928 }
929 | forprefix .e ')' statement
930 { plabel( contlab );
931 if( flostat&FCONT ) reached = 1;
932 if( $2 ) ecomp( $2 );
933 branch($1);
934 plabel( brklab );
935 if( (flostat&FBRK) || !(flostat&FLOOP) ) reached = 1;
936 else reached = 0;
937 resetbc(0);
938 symclear(blevel); /* if declaration inside for() */
939 }
940 | switchpart statement
941 { if( reached ) branch( brklab );
942 plabel( $1 );
943 swend();
944 plabel( brklab);
945 if( (flostat&FBRK) || !(flostat&FDEF) ) reached = 1;
946 resetbc(FCONT);
947 }
948 | C_BREAK ';' {
949 if (brklab == NOLAB)
950 uerror("illegal break");
951 else if (reached)
952 branch(brklab);
953 flostat |= FBRK;
954 reached = 0;
955 }
956 | C_CONTINUE ';' {
957 if (contlab == NOLAB)
958 uerror("illegal continue");
959 else
960 branch(contlab);
961 flostat |= FCONT;
962 goto rch;
963 }
964 | C_RETURN ';' {
965 branch(retlab);
966 if (cftnsp->stype != VOID &&
967 (cftnsp->sflags & NORETYP) == 0 &&
968 cftnsp->stype != VOID+FTN)
969 uerror("return value required");
970 rch:
971 if (!reached)
972 warner(Wunreachable_code);
973 reached = 0;
974 }
975 | C_RETURN e ';' {
976 NODE *p, *q;
977
978 p = nametree(cftnsp);
979 p->n_type = DECREF(p->n_type);
980 q = eve($2);
981 #ifndef NO_COMPLEX
982 if (ANYCX(q) || ANYCX(p))
983 q = cxret(q, p);
984 #endif
985 p = buildtree(RETURN, p, q);
986 if (p->n_type == VOID) {
987 ecomp(p->n_right);
988 } else {
989 if (cftnod == NIL)
990 cftnod = tempnode(0, p->n_type,
991 p->n_df, p->n_ap);
992 ecomp(buildtree(ASSIGN,
993 ccopy(cftnod), p->n_right));
994 }
995 tfree(p->n_left);
996 nfree(p);
997 branch(retlab);
998 reached = 0;
999 }
1000 | C_GOTO C_NAME ';' { gotolabel($2); goto rch; }
1001 | C_GOTO '*' e ';' { ecomp(biop(GOTO, eve($3), NIL)); }
1002 | asmstatement ';'
1003 | ';'
1004 | error ';'
1005 | error '}'
1006 | label statement
1007 ;
1008
1009 asmstatement: C_ASM mvol '(' string ')' { send_passt(IP_ASM, mkpstr($4)); }
1010 | C_ASM mvol '(' string xasm ')' { mkxasm($4, $5); }
1011 ;
1012
1013 mvol: /* empty */
1014 | C_QUALIFIER { nfree($1); }
1015 ;
1016
1017 xasm: ':' oplist { $$ = xcmop($2, NIL, NIL); }
1018 | ':' oplist ':' oplist { $$ = xcmop($2, $4, NIL); }
1019 | ':' oplist ':' oplist ':' cnstr { $$ = xcmop($2, $4, $6); }
1020 ;
1021
1022 oplist: /* nothing */ { $$ = NIL; }
1023 | oper { $$ = $1; }
1024 ;
1025
1026 oper: string '(' e ')' { $$ = xasmop($1, eve($3)); }
1027 | oper ',' string '(' e ')' {
1028 $$ = cmop($1, xasmop($3, eve($5)));
1029 }
1030 ;
1031
1032 cnstr: string { $$ = xasmop($1, bcon(0)); }
1033 | cnstr ',' string { $$ = cmop($1, xasmop($3, bcon(0))); }
1034 ;
1035
1036 label: C_NAME ':' attr_var { deflabel($1, $3); reached = 1; }
1037 | C_TYPENAME ':' attr_var { deflabel($1, $3); reached = 1; }
1038 | C_CASE e ':' { addcase(eve($2)); reached = 1; }
1039 /* COMPAT_GCC */| C_CASE e C_ELLIPSIS e ':' {
1040 #ifdef GCC_COMPAT
1041 gcccase(eve($2), eve($4)); reached = 1;
1042 #endif
1043 }
1044 | C_DEFAULT ':' { reached = 1; adddef(); flostat |= FDEF; }
1045 ;
1046
1047 doprefix: C_DO {
1048 savebc();
1049 brklab = getlab();
1050 contlab = getlab();
1051 plabel( $$ = getlab());
1052 reached = 1;
1053 }
1054 ;
1055 ifprefix: C_IF '(' e ')' {
1056 cbranch(buildtree(NOT, eve($3), NIL), bcon($$ = getlab()));
1057 reached = 1;
1058 }
1059 ;
1060 ifelprefix: ifprefix statement C_ELSE {
1061 if (reached)
1062 branch($$ = getlab());
1063 else
1064 $$ = NOLAB;
1065 plabel( $1);
1066 reached = 1;
1067 }
1068 ;
1069
1070 whprefix: C_WHILE '(' e ')' {
1071 savebc();
1072 $3 = eve($3);
1073 if ($3->n_op == ICON && glval($3) != 0)
1074 flostat = FLOOP;
1075 plabel( contlab = getlab());
1076 reached = 1;
1077 brklab = getlab();
1078 if (flostat == FLOOP)
1079 tfree($3);
1080 else
1081 cbranch(buildtree(NOT, $3, NIL), bcon(brklab));
1082 }
1083 ;
1084 forprefix: C_FOR '(' .e ';' .e ';' {
1085 if ($3)
1086 ecomp($3);
1087 savebc();
1088 contlab = getlab();
1089 brklab = getlab();
1090 plabel( $$ = getlab());
1091 reached = 1;
1092 if ($5)
1093 cbranch(buildtree(NOT, $5, NIL), bcon(brklab));
1094 else
1095 flostat |= FLOOP;
1096 }
1097 | C_FOR '(' { ++blevel; } declaration .e ';' {
1098 blevel--;
1099 savebc();
1100 contlab = getlab();
1101 brklab = getlab();
1102 plabel( $$ = getlab());
1103 reached = 1;
1104 if ($5)
1105 cbranch(buildtree(NOT, $5, NIL), bcon(brklab));
1106 else
1107 flostat |= FLOOP;
1108 }
1109 ;
1110
1111 switchpart: C_SWITCH '(' e ')' {
1112 NODE *p;
1113 int num;
1114 TWORD t;
1115
1116 savebc();
1117 brklab = getlab();
1118 $3 = eve($3);
1119 if (!ISINTEGER($3->n_type)) {
1120 uerror("switch expression must have integer "
1121 "type");
1122 t = INT;
1123 } else {
1124 $3 = intprom($3);
1125 t = $3->n_type;
1126 }
1127 p = tempnode(0, t, 0, 0);
1128 num = regno(p);
1129 ecomp(buildtree(ASSIGN, p, $3));
1130 branch( $$ = getlab());
1131 swstart(num, t);
1132 reached = 0;
1133 }
1134 ;
1135 /* EXPRESSIONS */
1136 .e: e { $$ = eve($1); }
1137 | { $$=0; }
1138 ;
1139
1140 elist: { $$ = NIL; }
1141 | e %prec ','
1142 | elist ',' e { $$ = biop(CM, $1, $3); }
1143 | elist ',' cast_type { /* hack for stdarg */
1144 TYMFIX($3);
1145 $3->n_op = TYPE;
1146 $$ = biop(CM, $1, $3);
1147 }
1148 ;
1149
1150 /*
1151 * Precedence order of operators.
1152 */
1153 e: e ',' e { $$ = biop(COMOP, $1, $3); }
1154 | e '=' e { $$ = biop(ASSIGN, $1, $3); }
1155 | e C_ASOP e { $$ = biop($2, $1, $3); }
1156 | e '?' e ':' e {
1157 $$=biop(QUEST, $1, biop(COLON, $3, $5));
1158 }
1159 | e '?' ':' e {
1160 NODE *p = tempnode(0, $1->n_type, $1->n_df, $1->n_ap);
1161 $$ = biop(COLON, ccopy(p), $4);
1162 $$=biop(QUEST, biop(ASSIGN, p, $1), $$);
1163 }
1164 | e C_OROR e { $$ = biop($2, $1, $3); }
1165 | e C_ANDAND e { $$ = biop($2, $1, $3); }
1166 | e '|' e { $$ = biop(OR, $1, $3); }
1167 | e '^' e { $$ = biop(ER, $1, $3); }
1168 | e '&' e { $$ = biop(AND, $1, $3); }
1169 | e C_EQUOP e { $$ = biop($2, $1, $3); }
1170 | e C_RELOP e { $$ = biop($2, $1, $3); }
1171 | e C_SHIFTOP e { $$ = biop($2, $1, $3); }
1172 | e '+' e { $$ = biop(PLUS, $1, $3); }
1173 | e '-' e { $$ = biop(MINUS, $1, $3); }
1174 | e C_DIVOP e { $$ = biop($2, $1, $3); }
1175 | e '*' e { $$ = biop(MUL, $1, $3); }
1176 | e '=' addrlbl { $$ = biop(ASSIGN, $1, $3); }
1177 | term
1178 ;
1179
1180 xbegin: begin {
1181 $$ = getlab(); getlab(); getlab();
1182 branch($$); plabel(($$)+1); }
1183 ;
1184
1185 addrlbl: C_ANDAND C_NAME {
1186 #ifdef GCC_COMPAT
1187 struct symtab *s = lookup($2, SLBLNAME);
1188 if (s->soffset == 0)
1189 s->soffset = -getlab();
1190 $$ = buildtree(ADDROF, nametree(s), NIL);
1191 #else
1192 uerror("gcc extension");
1193 #endif
1194 }
1195 ;
1196
1197 term: term C_INCOP { $$ = biop($2, $1, bcon(1)); }
1198 | '*' term { $$ = biop(UMUL, $2, NIL); }
1199 | '&' term { $$ = biop(ADDROF, $2, NIL); }
1200 | '-' term { $$ = biop(UMINUS, $2, NIL ); }
1201 | '+' term { $$ = biop(PLUS, $2, bcon(0)); }
1202 | CXX_CASTS C_RELOP cast_type C_RELOP '(' e ')' {
1203 tfree($6);
1204 tfree($3);
1205 $$ = bcon(0);
1206 werror("CXX_CASTS unhandled");
1207 }
1208 | C_UNOP term { $$ = biop($1, $2, NIL); }
1209 | C_INCOP term {
1210 $$ = biop($1 == INCR ? PLUSEQ : MINUSEQ, $2, bcon(1));
1211 }
1212 | C_SIZEOF xa term { $$ = biop(SZOF, $3, bcon(0)); inattr = $<intval>2; }
1213 | '(' cast_type ')' term %prec C_INCOP {
1214 TYMFIX($2);
1215 $$ = biop(CAST, $2, $4);
1216 }
1217 | C_SIZEOF xa '(' cast_type ')' %prec C_SIZEOF {
1218 $$ = biop(SZOF, $4, bcon(1));
1219 inattr = $<intval>2;
1220 }
1221 | C_ALIGNOF xa '(' cast_type ')' {
1222 int al;
1223 TYMFIX($4);
1224 al = talign($4->n_type, $4->n_ap);
1225 $$ = bcon(al/SZCHAR);
1226 inattr = $<intval>2;
1227 tfree($4);
1228 }
1229 | '(' cast_type ')' clbrace init_list optcomma '}' {
1230 endinit(0);
1231 $$ = bdty(NAME, $4);
1232 $$->n_op = CLOP;
1233 }
1234 | '(' cast_type ')' clbrace '}' {
1235 endinit(0);
1236 $$ = bdty(NAME, $4);
1237 $$->n_op = CLOP;
1238 }
1239 | term '[' e ']' {
1240 if ($1->n_op == NEWKW) {
1241 $1->n_left = biop(LB, $1->n_left, $3);
1242 } else
1243 $$ = biop(LB, $1, $3);
1244 }
1245 | C_NAME '(' elist ')' {
1246 #if 0
1247 if ($3) tfree($3);
1248 $$ = bcon(0);
1249 #else
1250 $$ = biop($3 ? CALL : UCALL, bdty(NAME, $1), $3);
1251 #endif
1252 }
1253 | term '(' elist ')' { $$ = biop($3 ? CALL : UCALL, $1, $3); }
1254 | term C_STROP C_NAME { $$ = biop($2, $1, bdty(NAME, $3)); }
1255 | term C_STROP C_TYPENAME { $$ = biop($2, $1, bdty(NAME, $3));}
1256 | C_NAME %prec C_SIZEOF /* below ( */{ $$ = bdty(NAME, $1); }
1257 | nmrec C_NAME %prec C_SIZEOF {
1258 $$ = biop(NMLIST, $1, bdty(NAME, $2));
1259 }
1260 | PCC_OFFSETOF '(' cast_type ',' term ')' {
1261 TYMFIX($3);
1262 $3->n_type = INCREF($3->n_type);
1263 $3 = biop(CAST, $3, bcon(0));
1264 if ($5->n_op == NAME) {
1265 $$ = biop(STREF, $3, $5);
1266 } else {
1267 NODE *p = $5;
1268 while (p->n_left->n_op != NAME)
1269 p = p->n_left;
1270 p->n_left = biop(STREF, $3, p->n_left);
1271 $$ = $5;
1272 }
1273 $$ = biop(ADDROF, $$, NIL);
1274 $3 = block(NAME, NIL, NIL, ENUNSIGN(INTPTR), 0, 0);
1275 $$ = biop(CAST, $3, $$);
1276 }
1277 | C_ICON { $$ = $1; }
1278 | C_FCON { $$ = $1; }
1279 | string { $$ = bdty(STRING, $1, widestr); }
1280 | '(' e ')' { $$=$2; }
1281 | '(' xbegin block_item_list e ';' '}' ')' {
1282 /* XXX - check recursive ({ }) statements */
1283 branch(($2)+2);
1284 plabel($2);
1285 $$ = buildtree(COMOP,
1286 biop(GOTO, bcon(($2)+1), NIL), eve($4));
1287 flend();
1288 }
1289 | '(' xbegin block_item_list '}' ')' {
1290 /* XXX - check recursive ({ }) statements */
1291 branch(($2)+2);
1292 plabel($2);
1293 $$ = buildtree(COMOP,
1294 biop(GOTO, bcon(($2)+1), NIL), voidcon());
1295 flend();
1296 }
1297 | CXX_NEW new_ds { $$ = biop(NEWKW, $2, bcon(0)); }
1298 | CXX_NEW '(' cast_type ')' { $$ = 0; uerror("new cast"); }
1299 | CXX_DELETE term %prec '-' {
1300 $$ = biop(DELETE, $2, bcon(NM_DEL));
1301 }
1302 | CXX_DELETE '[' ']' term %prec '-' {
1303 $$ = biop(DELETE, $4, bcon(NM_DLA));
1304 }
1305 ;
1306
1307 nmrec: CXX_MORENM { $$ = bdty(NAME, $1); }
1308 | CXX_MORENM nmrec { $$ = biop(NMLIST, $2, bdty(NAME, $1)); }
1309 ;
1310
1311 xa: { $<intval>$ = inattr; inattr = 0; }
1312 ;
1313
1314 clbrace: '{' { NODE *q = $<nodep>-1; TYMFIX(q); $$ = clbrace(q); }
1315 ;
1316
1317 string: C_STRING { widestr = 0; $$ = stradd("", $1); }
1318 | string C_STRING { $$ = stradd($1, $2); }
1319 ;
1320
1321 cast_type: specifier_qualifier_list {
1322 $$ = biop(TYMERGE, $1, bdty(NAME, NULL));
1323 }
1324 | specifier_qualifier_list abstract_declarator {
1325 $$ = biop(TYMERGE, $1, aryfix($2));
1326 }
1327 ;
1328
1329 %%
1330
1331 NODE *
1332 mkty(TWORD t, union dimfun *d, struct attr *sue)
1333 {
1334 return block(TYPE, NIL, NIL, t, d, sue);
1335 }
1336
1337 NODE *
1338 bdty(int op, ...)
1339 {
1340 va_list ap;
1341 int val;
1342 register NODE *q;
1343
1344 va_start(ap, op);
1345 q = biop(op, NIL, NIL);
1346
1347 switch (op) {
1348 case UMUL:
1349 case UCALL:
1350 q->n_left = va_arg(ap, NODE *);
1351 q->n_rval = 0;
1352 break;
1353
1354 case CALL:
1355 q->n_left = va_arg(ap, NODE *);
1356 q->n_right = va_arg(ap, NODE *);
1357 break;
1358
1359 case LB:
1360 q->n_left = va_arg(ap, NODE *);
1361 if ((val = va_arg(ap, int)) <= 0) {
1362 uerror("array size must be positive");
1363 val = 1;
1364 }
1365 q->n_right = bcon(val);
1366 break;
1367
1368 case NAME:
1369 q->n_sp = va_arg(ap, struct symtab *); /* XXX survive tymerge */
1370 break;
1371
1372 case STRING:
1373 q->n_name = va_arg(ap, char *);
1374 val = va_arg(ap, int);
1375 slval(q, val);
1376 break;
1377
1378 default:
1379 cerror("bad bdty");
1380 }
1381 va_end(ap);
1382
1383 return q;
1384 }
1385
1386 static void
1387 flend(void)
1388 {
1389 if (!isinlining && sspflag && blevel == 2)
1390 sspend();
1391 #ifdef STABS
1392 if (gflag && blevel > 2)
1393 stabs_rbrac(blevel);
1394 #endif
1395 --blevel;
1396 if( blevel == 1 )
1397 blevel = 0;
1398 symclear(blevel); /* Clean ut the symbol table */
1399 if (autooff > maxautooff)
1400 maxautooff = autooff;
1401 autooff = savctx->contlab;
1402 savctx = savctx->next;
1403 }
1404
1405 static void
1406 savebc(void)
1407 {
1408 struct savbc *bc = tmpalloc(sizeof(struct savbc));
1409
1410 bc->brklab = brklab;
1411 bc->contlab = contlab;
1412 bc->flostat = flostat;
1413 bc->next = savbc;
1414 savbc = bc;
1415 flostat = 0;
1416 }
1417
1418 static void
1419 resetbc(int mask)
1420 {
1421 flostat = savbc->flostat | (flostat&mask);
1422 contlab = savbc->contlab;
1423 brklab = savbc->brklab;
1424 savbc = savbc->next;
1425 }
1426
1427 struct swdef {
1428 struct swdef *next; /* Next in list */
1429 int deflbl; /* Label for "default" */
1430 struct swents *ents; /* Linked sorted list of case entries */
1431 int nents; /* # of entries in list */
1432 int num; /* Node value will end up in */
1433 TWORD type; /* Type of switch expression */
1434 } *swpole;
1435
1436 /*
1437 * add case to switch
1438 */
1439 static void
1440 addcase(NODE *p)
1441 {
1442 struct swents **put, *w, *sw = tmpalloc(sizeof(struct swents));
1443 CONSZ val;
1444
1445 p = optim(rmpconv(p)); /* change enum to ints */
1446 if (p->n_op != ICON || p->n_sp != NULL) {
1447 uerror( "non-constant case expression");
1448 return;
1449 }
1450 if (swpole == NULL) {
1451 uerror("case not in switch");
1452 return;
1453 }
1454
1455 if (DEUNSIGN(swpole->type) != DEUNSIGN(p->n_type)) {
1456 val = glval(p);
1457 p = makety(p, swpole->type, 0, 0, 0);
1458 if (p->n_op != ICON)
1459 cerror("could not cast case value to type of switch "
1460 "expression");
1461 if (glval(p) != val)
1462 werror("case expression truncated");
1463 }
1464 sw->sval = glval(p);
1465 tfree(p);
1466 put = &swpole->ents;
1467 if (ISUNSIGNED(swpole->type)) {
1468 for (w = swpole->ents;
1469 w != NULL && (U_CONSZ)w->sval < (U_CONSZ)sw->sval;
1470 w = w->next)
1471 put = &w->next;
1472 } else {
1473 for (w = swpole->ents; w != NULL && w->sval < sw->sval;
1474 w = w->next)
1475 put = &w->next;
1476 }
1477 if (w != NULL && w->sval == sw->sval) {
1478 uerror("duplicate case in switch");
1479 return;
1480 }
1481 plabel(sw->slab = getlab());
1482 *put = sw;
1483 sw->next = w;
1484 swpole->nents++;
1485 }
1486
1487 #ifdef GCC_COMPAT
1488 void
1489 gcccase(NODE *ln, NODE *hn)
1490 {
1491 CONSZ i, l, h;
1492
1493 l = icons(optim(ln));
1494 h = icons(optim(hn));
1495
1496 if (h < l)
1497 i = l, l = h, h = i;
1498
1499 for (i = l; i <= h; i++)
1500 addcase(xbcon(i, NULL, hn->n_type));
1501 }
1502 #endif
1503
1504 /*
1505 * add default case to switch
1506 */
1507 static void
1508 adddef(void)
1509 {
1510 if (swpole == NULL)
1511 uerror("default not inside switch");
1512 else if (swpole->deflbl != 0)
1513 uerror("duplicate default in switch");
1514 else
1515 plabel( swpole->deflbl = getlab());
1516 }
1517
1518 static void
1519 swstart(int num, TWORD type)
1520 {
1521 struct swdef *sw = tmpalloc(sizeof(struct swdef));
1522
1523 sw->deflbl = sw->nents = 0;
1524 sw->ents = NULL;
1525 sw->next = swpole;
1526 sw->num = num;
1527 sw->type = type;
1528 swpole = sw;
1529 }
1530
1531 /*
1532 * end a switch block
1533 */
1534 static void
1535 swend(void)
1536 {
1537 struct swents *sw, **swp;
1538 int i;
1539
1540 sw = tmpalloc(sizeof(struct swents));
1541 swp = tmpalloc(sizeof(struct swents *) * (swpole->nents+1));
1542
1543 sw->slab = swpole->deflbl;
1544 swp[0] = sw;
1545
1546 for (i = 1; i <= swpole->nents; i++) {
1547 swp[i] = swpole->ents;
1548 swpole->ents = swpole->ents->next;
1549 }
1550 genswitch(swpole->num, swpole->type, swp, swpole->nents);
1551
1552 swpole = swpole->next;
1553 }
1554
1555 /*
1556 * num: tempnode the value of the switch expression is in
1557 * type: type of the switch expression
1558 *
1559 * p points to an array of structures, each consisting
1560 * of a constant value and a label.
1561 * The first is >=0 if there is a default label;
1562 * its value is the label number
1563 * The entries p[1] to p[n] are the nontrivial cases
1564 * n is the number of case statements (length of list)
1565 */
1566 static void
1567 genswitch(int num, TWORD type, struct swents **p, int n)
1568 {
1569 NODE *r, *q;
1570 int i;
1571
1572 if (mygenswitch(num, type, p, n))
1573 return;
1574
1575 /* simple switch code */
1576 for (i = 1; i <= n; ++i) {
1577 /* already in 1 */
1578 r = tempnode(num, type, 0, 0);
1579 q = xbcon(p[i]->sval, NULL, type);
1580 r = buildtree(NE, r, clocal(q));
1581 cbranch(buildtree(NOT, r, NIL), bcon(p[i]->slab));
1582 }
1583 if (p[0]->slab > 0)
1584 branch(p[0]->slab);
1585 }
1586
1587 /*
1588 * Declare a variable or prototype.
1589 */
1590 static struct symtab *
1591 init_declarator(NODE *tn, NODE *p, int assign, NODE *a)
1592 {
1593 int class = (int)glval(tn);
1594 struct symtab *sp;
1595
1596 p = aryfix(p);
1597 if (p->n_op == NMLIST) {
1598 tymerge(tn, p->n_right);
1599 } else
1600 p = tymerge(tn, p);
1601 if (a) {
1602 struct attr *ap = gcc_attr_wrapper(a);
1603 p->n_ap = attr_add(p->n_ap, ap);
1604 }
1605
1606 sp = cxxdeclvar(p);
1607
1608 if (fun_inline && ISFTN(p->n_type))
1609 sp->sflags |= SINLINE;
1610
1611 if (!ISFTN(p->n_type)) {
1612 if (assign) {
1613 defid(p, class);
1614 sp = p->n_sp;
1615 sp->sflags |= SASG;
1616 if (sp->sflags & SDYNARRAY)
1617 uerror("can't initialize dynamic arrays");
1618 lcommdel(sp);
1619 } else
1620 nidcl(p, class);
1621 } else {
1622 extern NODE *parlink;
1623 if (assign)
1624 uerror("cannot initialise function");
1625 defid(p, uclass(class));
1626 sp = p->n_sp;
1627 if (parlink) {
1628 /* dynamic sized arrays in prototypes */
1629 tfree(parlink); /* Free delayed tree */
1630 parlink = NIL;
1631 }
1632 }
1633 tfree(p);
1634 return sp;
1635 }
1636
1637 /*
1638 * Declare old-stype function arguments.
1639 */
1640 static void
1641 oldargs(NODE *p)
1642 {
1643 blevel++;
1644 p->n_op = TYPE;
1645 p->n_type = FARG;
1646 p->n_sp = lookup((char *)p->n_sp, 0);/* XXX */
1647 defid(p, PARAM);
1648 blevel--;
1649 }
1650
1651 /*
1652 * Set NAME nodes to a null name and index of LB nodes to NOOFFSET
1653 * unless clr is one, in that case preserve variable name.
1654 */
1655 static NODE *
1656 namekill(NODE *p, int clr)
1657 {
1658 NODE *q;
1659 int o = p->n_op;
1660
1661 switch (coptype(o)) {
1662 case LTYPE:
1663 if (o == NAME) {
1664 if (clr)
1665 p->n_sp = NULL;
1666 else
1667 p->n_sp = lookup((char *)p->n_sp, 0);/* XXX */
1668 }
1669 break;
1670
1671 case UTYPE:
1672 p->n_left = namekill(p->n_left, clr);
1673 break;
1674
1675 case BITYPE:
1676 p->n_left = namekill(p->n_left, clr);
1677 if (o == LB) {
1678 if (clr) {
1679 tfree(p->n_right);
1680 p->n_right = bcon(NOOFFSET);
1681 } else
1682 p->n_right = eve(p->n_right);
1683 } else if (o == CALL)
1684 p->n_right = namekill(p->n_right, 1);
1685 else
1686 p->n_right = namekill(p->n_right, clr);
1687 if (o == TYMERGE) {
1688 q = tymerge(p->n_left, p->n_right);
1689 q->n_ap = attr_add(q->n_ap, p->n_ap);
1690 tfree(p->n_left);
1691 nfree(p);
1692 p = q;
1693 }
1694 break;
1695 }
1696 return p;
1697 }
1698
1699 /*
1700 * Declare function arguments.
1701 */
1702 static NODE *
1703 funargs(NODE *p)
1704 {
1705 extern NODE *arrstk[10];
1706
1707 if (p->n_op == ELLIPSIS)
1708 return p;
1709
1710 p = namekill(p, 0);
1711 if (ISFTN(p->n_type))
1712 p->n_type = INCREF(p->n_type);
1713 if (ISARY(p->n_type)) {
1714 p->n_type += (PTR-ARY);
1715 if (p->n_df->ddim == -1)
1716 tfree(arrstk[0]), arrstk[0] = NIL;
1717 p->n_df++;
1718 }
1719 if (p->n_type == VOID && p->n_sp->sname == NULL)
1720 return p; /* sanitycheck later */
1721 else if (p->n_sp->sname == NULL)
1722 ; /* uerror("argument missing"); */
1723 else
1724 defid(p, PARAM);
1725 return p;
1726 }
1727
1728 static NODE *
1729 listfw(NODE *p, NODE * (*f)(NODE *))
1730 {
1731 if (p->n_op == CM) {
1732 p->n_left = listfw(p->n_left, f);
1733 p->n_right = (*f)(p->n_right);
1734 } else
1735 p = (*f)(p);
1736 return p;
1737 }
1738
1739
1740 /*
1741 * Declare a function.
1742 */
1743 static struct symtab *
1744 fundef(NODE *tp, NODE *p)
1745 {
1746 extern int prolab;
1747 struct symtab *s, *nsthis;
1748 NODE *q, *typ;
1749 int class = (int)glval(tp), oclass, ctval;
1750 char *c;
1751
1752 /*
1753 * We discard all names except for those needed for
1754 * parameter declaration. While doing that, also change
1755 * non-constant array sizes to unknown.
1756 */
1757 ctval = tvaloff;
1758 for (q = p; coptype(q->n_op) != LTYPE &&
1759 q->n_left->n_op != NAME &&
1760 q->n_left->n_op != NMLIST; q = q->n_left) {
1761 if (q->n_op == CALL)
1762 q->n_right = namekill(q->n_right, 1);
1763 }
1764 if (q->n_op != CALL && q->n_op != UCALL) {
1765 uerror("invalid function definition");
1766 p = bdty(UCALL, p);
1767 } else if (q->n_op == CALL) {
1768 blevel = 1;
1769 argoff = ARGINIT;
1770 if (oldstyle == 0)
1771 q->n_right = listfw(q->n_right, funargs);
1772 ftnarg(q);
1773 blevel = 0;
1774 }
1775
1776 if (p->n_op == CALL && p->n_left->n_op == NMLIST) {
1777 NODE *r = p->n_left;
1778 p->n_left = r->n_right;
1779 r->n_right = typ = tymerge(tp, p);
1780 p = r;
1781 } else
1782 typ = tymerge(tp, p);
1783
1784 #ifdef GCC_COMPAT
1785 /* gcc seems to discard __builtin_ when declaring functions */
1786 if (strncmp("__builtin_", (char *)typ->n_sp, 10) == 0)
1787 typ->n_sp = (struct symtab *)((char *)typ->n_sp + 10);
1788 #endif
1789
1790 s = cxxftnfind(p, SNORMAL);
1791 nsthis = nscur;
1792 nscur = s->sdown; /* XXX fun in fun? */
1793
1794 oclass = s->sclass;
1795 if (class == STATIC && oclass == EXTERN)
1796 werror("%s was first declared extern, then static", s->sname);
1797
1798 if (fun_inline) {
1799 /* special syntax for inline functions */
1800 if (! strcmp(s->sname,"main"))
1801 uerror("cannot inline main()");
1802
1803 s->sflags |= SINLINE;
1804 inline_start(s);
1805 if (class == EXTERN)
1806 class = EXTDEF;
1807 } else if (class == EXTERN)
1808 class = SNULL; /* same result */
1809
1810 cftnsp = s;
1811 #if 0
1812 defid(p, class);
1813 #endif
1814
1815 #ifdef GCC_COMPAT
1816 if (attr_find(p->n_ap, GCC_ATYP_ALW_INL)) {
1817 /* Temporary turn on temps to make always_inline work */
1818 alwinl = 1;
1819 if (xtemps == 0) alwinl |= 2;
1820 xtemps = 1;
1821 }
1822 #endif
1823 prolab = getlab();
1824 if ((c = cftnsp->soname) == NULL)
1825 c = addname(exname(cftnsp->sname));
1826 send_passt(IP_PROLOG, -1, c, cftnsp->stype,
1827 cftnsp->sclass == EXTDEF, prolab, ctval);
1828 blevel++;
1829 #ifdef STABS
1830 if (gflag)
1831 stabs_func(s);
1832 #endif
1833 tfree(tp);
1834 tfree(p);
1835 return nsthis;
1836 }
1837
1838 static void
1839 fend(struct symtab *ns)
1840 {
1841 if (blevel)
1842 cerror("function level error");
1843 ftnend();
1844 fun_inline = 0;
1845 if (alwinl & 2) xtemps = 0;
1846 alwinl = 0;
1847 cftnsp = NULL;
1848 nscur = ns;
1849 }
1850
1851 NODE *
1852 structref(NODE *p, int f, char *name)
1853 {
1854 NODE *r;
1855
1856 if (f == DOT)
1857 p = buildtree(ADDROF, p, NIL);
1858 r = biop(NAME, NIL, NIL);
1859 r->n_name = name;
1860 r = buildtree(STREF, p, r);
1861 return r;
1862 }
1863
1864 static void
1865 olddecl(NODE *p, NODE *a)
1866 {
1867 struct symtab *s;
1868
1869 p = namekill(p, 0);
1870 s = p->n_sp;
1871 if (s->slevel != 1 || s->stype == UNDEF)
1872 uerror("parameter '%s' not defined", s->sname);
1873 else if (s->stype != FARG)
1874 uerror("parameter '%s' redefined", s->sname);
1875
1876 s->stype = p->n_type;
1877 s->sdf = p->n_df;
1878 s->sap = p->n_ap;
1879 if (ISARY(s->stype)) {
1880 s->stype += (PTR-ARY);
1881 s->sdf++;
1882 } else if (s->stype == FLOAT)
1883 s->stype = DOUBLE;
1884 if (a)
1885 attr_add(s->sap, gcc_attr_wrapper(a));
1886 nfree(p);
1887 }
1888
1889 void
1890 branch(int lbl)
1891 {
1892 int r = reached++;
1893 ecomp(biop(GOTO, bcon(lbl), NIL));
1894 reached = r;
1895 }
1896
1897 /*
1898 * Create a printable string based on an encoded string.
1899 */
1900 static char *
1901 mkpstr(char *str)
1902 {
1903 char *s, *os;
1904 int v, l = (int)strlen(str)+3; /* \t + \n + \0 */
1905
1906 os = s = inlalloc(l);
1907 *s++ = '\t';
1908 for (; *str; ) {
1909 if (*str++ == '\\')
1910 v = esccon(&str);
1911 else
1912 v = str[-1];
1913 *s++ = v;
1914 }
1915 *s++ = '\n';
1916 *s = 0;
1917 return os;
1918 }
1919
1920 /*
1921 * Estimate the max length a string will have in its internal
1922 * representation based on number of \ characters.
1923 */
1924 static int
1925 maxstlen(char *str)
1926 {
1927 int i;
1928
1929 for (i = 0; *str; str++, i++)
1930 if (*str == '\\' || *str < 32 || *str > 0176)
1931 i += 3;
1932 return i;
1933 }
1934
1935 static char *
1936 voct(char *d, unsigned int v)
1937 {
1938 v &= (1 << SZCHAR) - 1;
1939 *d++ = '\\';
1940 *d++ = v/64 + '0'; v &= 077;
1941 *d++ = v/8 + '0'; v &= 7;
1942 *d++ = v + '0';
1943 return d;
1944 }
1945
1946
1947 /*
1948 * Convert a string to internal format. The resulting string may be no
1949 * more than len characters long.
1950 */
1951 static void
1952 fixstr(char *d, char *s, int len)
1953 {
1954 unsigned int v;
1955
1956 while (*s) {
1957 if (len <= 0)
1958 cerror("fixstr");
1959 if (*s == '\\') {
1960 s++;
1961 v = esccon(&s);
1962 d = voct(d, v);
1963 len -= 4;
1964 } else if (*s < ' ' || *s > 0176) {
1965 d = voct(d, *s++);
1966 len -= 4;
1967 } else
1968 *d++ = *s++, len--;
1969 }
1970 *d = 0;
1971 }
1972
1973 /*
1974 * Add "raw" string new to cleaned string old.
1975 */
1976 static char *
1977 stradd(char *old, char *new)
1978 {
1979 char *rv;
1980 int len;
1981
1982 if (*new == 'L' && new[1] == '\"')
1983 widestr = 1, new++;
1984 if (*new == '\"') {
1985 new++; /* remove first " */
1986 new[strlen(new) - 1] = 0;/* remove last " */
1987 }
1988 len = (int)strlen(old) + maxstlen(new) + 1;
1989 rv = tmpalloc(len);
1990 strlcpy(rv, old, len);
1991 fixstr(rv + strlen(old), new, maxstlen(new) + 1);
1992 return rv;
1993 }
1994
1995 /*
1996 * Fake a symtab entry for compound literals.
1997 */
1998 static struct symtab *
1999 clbrace(NODE *p)
2000 {
2001 struct symtab *sp;
2002
2003 sp = getsymtab(simname("cl"), STEMP);
2004 sp->stype = p->n_type;
2005 sp->squal = p->n_qual;
2006 sp->sdf = p->n_df;
2007 sp->sap = p->n_ap;
2008 tfree(p);
2009 if (blevel == 0 && xnf != NULL) {
2010 sp->sclass = STATIC;
2011 sp->slevel = 2;
2012 sp->soffset = getlab();
2013 } else {
2014 sp->sclass = blevel ? AUTO : STATIC;
2015 if (!ISARY(sp->stype) || sp->sdf->ddim != NOOFFSET) {
2016 sp->soffset = NOOFFSET;
2017 oalloc(sp, &autooff);
2018 }
2019 }
2020 beginit(sp);
2021 return sp;
2022 }
2023
2024 char *
2025 simname(char *s)
2026 {
2027 int len = (int)strlen(s) + 10 + 1;
2028 char *w = tmpalloc(len);
2029
2030 snprintf(w, len, "%s%d", s, getlab());
2031 return w;
2032 }
2033
2034 NODE *
2035 biop(int op, NODE *l, NODE *r)
2036 {
2037 return block(op, l, r, INT, 0, 0);
2038 }
2039
2040 static NODE *
2041 cmop(NODE *l, NODE *r)
2042 {
2043 return biop(CM, l, r);
2044 }
2045
2046 static NODE *
2047 voidcon(void)
2048 {
2049 return block(ICON, NIL, NIL, STRTY, 0, 0);
2050 }
2051
2052 /* Support for extended assembler a' la' gcc style follows below */
2053
2054 static NODE *
2055 xmrg(NODE *out, NODE *in)
2056 {
2057 NODE *p = in;
2058
2059 if (p->n_op == XARG) {
2060 in = cmop(out, p);
2061 } else {
2062 while (p->n_left->n_op == CM)
2063 p = p->n_left;
2064 p->n_left = cmop(out, p->n_left);
2065 }
2066 return in;
2067 }
2068
2069 /*
2070 * Put together in and out node lists in one list, and balance it with
2071 * the constraints on the right side of a CM node.
2072 */
2073 static NODE *
2074 xcmop(NODE *out, NODE *in, NODE *str)
2075 {
2076 NODE *p, *q;
2077
2078 if (out) {
2079 /* D out-list sanity check */
2080 for (p = out; p->n_op == CM; p = p->n_left) {
2081 q = p->n_right;
2082 if (q->n_name[0] != '=' && q->n_name[0] != '+')
2083 uerror("output missing =");
2084 }
2085 if (p->n_name[0] != '=' && p->n_name[0] != '+')
2086 uerror("output missing =");
2087 if (in == NIL)
2088 p = out;
2089 else
2090 p = xmrg(out, in);
2091 } else if (in) {
2092 p = in;
2093 } else
2094 p = voidcon();
2095
2096 if (str == NIL)
2097 str = voidcon();
2098 return cmop(p, str);
2099 }
2100
2101 /*
2102 * Generate a XARG node based on a string and an expression.
2103 */
2104 static NODE *
2105 xasmop(char *str, NODE *p)
2106 {
2107
2108 p = biop(XARG, p, NIL);
2109 p->n_name = isinlining ? newstring(str, strlen(str)) : str;
2110 return p;
2111 }
2112
2113 /*
2114 * Generate a XASM node based on a string and an expression.
2115 */
2116 static void
2117 mkxasm(char *str, NODE *p)
2118 {
2119 NODE *q;
2120
2121 q = biop(XASM, p->n_left, p->n_right);
2122 q->n_name = isinlining ? newstring(str, strlen(str)) : str;
2123 nfree(p);
2124 ecomp(q);
2125 }
2126
2127 static struct attr *
2128 gcc_attr_wrapper(NODE *p)
2129 {
2130 #ifdef GCC_COMPAT
2131 return gcc_attr_parse(p);
2132 #else
2133 uerror("gcc attribute used");
2134 return NULL;
2135 #endif
2136 }
2137
2138
2139 #ifdef GCC_COMPAT
2140 static NODE *
2141 tyof(NODE *p)
2142 {
2143 static struct symtab spp;
2144 NODE *q = block(TYPE, NIL, NIL, p->n_type, p->n_df, p->n_ap);
2145 q->n_qual = p->n_qual;
2146 q->n_sp = &spp; /* for typenode */
2147 tfree(p);
2148 return q;
2149 }
2150 #else
2151 static NODE *
2152 tyof(NODE *p)
2153 {
2154 uerror("typeof gcc extension");
2155 return bcon(0);
2156 }
2157 #endif
2158
2159 /*
2160 * Rewrite ++/-- to (t=p, p++, t) ops on types that do not act act as usual.
2161 */
2162 static NODE *
2163 rewincop(NODE *p1, NODE *p2, int op)
2164 {
2165 NODE *t, *r;
2166
2167 t = cstknode(p1->n_type, 0, 0);
2168 r = buildtree(ASSIGN, ccopy(t), ccopy(p1));
2169 r = buildtree(COMOP, r, buildtree(op, p1, eve(p2)));
2170 return buildtree(COMOP, r, t);
2171 }
2172
2173 /*
2174 * Traverse an unhandled expression tree bottom-up and call buildtree()
2175 * or equivalent as needed.
2176 */
2177 NODE *
2178 eve(NODE *p)
2179 {
2180 struct symtab *sp;
2181 NODE *r, *p1, *p2;
2182 int x;
2183
2184 p1 = p->n_left;
2185 p2 = p->n_right;
2186 switch (p->n_op) {
2187 case NMLIST:
2188 case NAME:
2189 sp = cxxlookup(p, SNORMAL|SNOCREAT);
2190 if (sp->sflags & SINLINE)
2191 inline_ref(sp);
2192 r = nametree(sp);
2193 if (sp->sflags & SDYNARRAY)
2194 r = buildtree(UMUL, r, NIL);
2195 #ifdef GCC_COMPAT
2196 if (attr_find(sp->sap, GCC_ATYP_DEPRECATED))
2197 warner(Wdeprecated_declarations, sp->sname);
2198 #endif
2199 break;
2200
2201 case DOT:
2202 case STREF:
2203 r = cxxstructref(eve(p1), p->n_op, (char *)p2->n_sp);
2204 nfree(p2);
2205 break;
2206
2207 case CAST:
2208 p1 = buildtree(CAST, p1, eve(p2));
2209 nfree(p1->n_left);
2210 r = p1->n_right;
2211 nfree(p1);
2212 break;
2213
2214
2215 case SZOF:
2216 x = xinline; xinline = 0; /* XXX hack */
2217 if (glval(p2) == 0)
2218 p1 = eve(p1);
2219 else
2220 TYMFIX(p1);
2221 nfree(p2);
2222 r = doszof(p1);
2223 xinline = x;
2224 break;
2225
2226 case LB:
2227 p1 = eve(p->n_left);
2228 r = buildtree(UMUL, buildtree(PLUS, p1, eve(p2)), NIL);
2229 break;
2230
2231 case COMPL:
2232 #ifndef NO_COMPLEX
2233 p1 = eve(p1);
2234 if (ANYCX(p1))
2235 r = cxconj(p1);
2236 else
2237 r = buildtree(COMPL, p1, NIL);
2238 break;
2239 #endif
2240 case UMINUS:
2241 case NOT:
2242 case UMUL:
2243 r = buildtree(p->n_op, eve(p->n_left), NIL);
2244 break;
2245
2246 case ADDROF:
2247 r = eve(p1);
2248 if (ISFTN(p->n_type)/* || ISARY(p->n_type) */){
2249 #ifdef notdef
2250 werror( "& before array or function: ignored" );
2251 #endif
2252 } else
2253 r = buildtree(ADDROF, r, NIL);
2254 break;
2255
2256 case CALL:
2257 case UCALL:
2258 if (p1->n_op == NAME || p1->n_op == NMLIST) {
2259 sp = cxxlookup(p1, SNORMAL);
2260 #ifndef NO_C_BUILTINS
2261 if (sp->sflags & SBUILTIN) {
2262 nfree(p1);
2263 r = builtin_check(sp, p2);
2264 break;
2265 }
2266 #endif
2267 if (sp->stype == UNDEF) {
2268 p1->n_type = FTN|INT;
2269 p1->n_sp = sp;
2270 p1->n_ap = NULL;
2271 defid(p1, EXTERN);
2272 }
2273 nfree(p1);
2274 #ifdef GCC_COMPAT
2275 if (attr_find(sp->sap, GCC_ATYP_DEPRECATED))
2276 warner(Wdeprecated_declarations, sp->sname);
2277 #endif
2278 p2 = p->n_op == CALL ? eve(p2) : NIL;
2279 r = doacall(sp, nametree(sp), p2, 0);
2280 } else if (p1->n_op == DOT || p1->n_op == STREF) {
2281 /*
2282 * function as member of a struct.
2283 * - check args for correct overloaded function
2284 * - add hidden arg0 as pointer to this struct
2285 */
2286
2287 p2 = p->n_op == CALL ? eve(p2) : NIL;
2288 p1->n_left = eve(p1->n_left); /* eval rest */
2289 r = cxxmatchftn(p1, p2);
2290 if (p1->n_op == DOT)
2291 p1->n_left = buildtree(ADDROF, p1->n_left, NIL);
2292 nfree(p1->n_right);
2293 p1 = nfree(p1);
2294 p2 = cxxaddhidden(p2, p1);
2295 r = doacall(NULL, r, p2, 1);
2296 } else {
2297 p2 = p->n_op == CALL ? eve(p2) : NIL;
2298 r = doacall(NULL, eve(p1), p2, 0);
2299 }
2300 break;
2301
2302 #ifndef NO_COMPLEX
2303 case XREAL:
2304 case XIMAG:
2305 p1 = eve(p1);
2306 r = cxelem(p->n_op, p1);
2307 break;
2308 #endif
2309
2310 case MUL:
2311 case DIV:
2312 case PLUS:
2313 case MINUS:
2314 case ASSIGN:
2315 case EQ:
2316 case NE:
2317 #ifndef NO_COMPLEX
2318 p1 = eve(p1);
2319 p2 = eve(p2);
2320 if (ANYCX(p1) || ANYCX(p2)) {
2321 r = cxop(p->n_op, p1, p2);
2322 } else if (ISITY(p1->n_type) || ISITY(p2->n_type)) {
2323 r = imop(p->n_op, p1, p2);
2324 } else
2325 r = buildtree(p->n_op, p1, p2);
2326 break;
2327 #endif
2328 case MOD:
2329 case CM:
2330 case GT:
2331 case GE:
2332 case LT:
2333 case LE:
2334 case RS:
2335 case LS:
2336 case RSEQ:
2337 case LSEQ:
2338 case AND:
2339 case OR:
2340 case ER:
2341 case OROR:
2342 case ANDAND:
2343 case EREQ:
2344 case OREQ:
2345 case ANDEQ:
2346 case QUEST:
2347 case COLON:
2348 p1 = eve(p1);
2349 eve2: r = buildtree(p->n_op, p1, eve(p2));
2350 break;
2351
2352 case INCR:
2353 case DECR:
2354 p1 = eve(p1);
2355 if (p1->n_type >= FLOAT && p1->n_type <= LDOUBLE) {
2356 /* ++/-- on floats isn't ((d+=1)-1) */
2357 /* rewrite to (t=d,d++,t) */
2358 /* XXX - side effects */
2359 r = rewincop(p1, p2, p->n_op);
2360 break;
2361 }
2362 if (p1->n_type != BOOL)
2363 goto eve2;
2364 /* Hey, fun. ++ will always be 1, and -- will toggle result */
2365 if (p->n_op == INCR) {
2366 /* (t=d,d=1,t) */
2367 r = rewincop(p1, p2, ASSIGN);
2368 } else {
2369 /* (t=d,d^=1,t) */
2370 r = rewincop(p1, p2, EREQ);
2371 }
2372 break;
2373
2374 case MODEQ:
2375 case MINUSEQ:
2376 case PLUSEQ:
2377 case MULEQ:
2378 case DIVEQ:
2379 p1 = eve(p1);
2380 p2 = eve(p2);
2381 #ifndef NO_COMPLEX
2382 if (ANYCX(p1) || ANYCX(p2)) {
2383 r = cxop(UNASG p->n_op, ccopy(p1), p2);
2384 r = cxop(ASSIGN, p1, r);
2385 break;
2386 } else if (ISITY(p1->n_type) || ISITY(p2->n_type)) {
2387 r = imop(UNASG p->n_op, ccopy(p1), p2);
2388 r = cxop(ASSIGN, p1, r);
2389 break;
2390 }
2391 /* FALLTHROUGH */
2392 #endif
2393 if (p1->n_type == BOOL) {
2394 r = buildtree(UNASG p->n_op, ccopy(p1), p2);
2395 r = buildtree(ASSIGN, p1, r);
2396 } else {
2397 r = buildtree(p->n_op, p1, p2);
2398 }
2399 break;
2400
2401 case STRING:
2402 r = strend(glval(p), p->n_name);
2403 break;
2404
2405 case COMOP:
2406 if (p1->n_op == GOTO) {
2407 /* inside ({ }), eve already called */
2408 r = buildtree(p->n_op, p1, p2);
2409 } else {
2410 p1 = eve(p1);
2411 r = buildtree(p->n_op, p1, eve(p2));
2412 }
2413 break;
2414
2415 case TYPE:
2416 case ICON:
2417 case FCON:
2418 case TEMP:
2419 return p;
2420
2421 case CLOP:
2422 r = nametree(p->n_sp);
2423 break;
2424
2425 case DELETE:
2426 p1 = eve(p1);
2427 r = cxx_delete(p1, glval(p2));
2428 nfree(p2);
2429 break;
2430
2431 case NEWKW:
2432 r = cxx_new(p1);
2433 nfree(p2);
2434 break;
2435
2436 default:
2437 #ifdef PCC_DEBUG
2438 fwalk(p, eprint, 0);
2439 #endif
2440 cerror("eve");
2441 r = NIL;
2442 }
2443 nfree(p);
2444 return r;
2445 }
2446
2447 int
2448 con_e(NODE *p)
2449 {
2450 #ifdef WORD_ADDRESSED
2451 return (int)icons(optim(eve(p)));
2452 #else
2453 return (int)icons(optim(rmpconv(eve(p))));
2454 #endif
2455 }
2456
2457 void
2458 uawarn(NODE *p, char *s)
2459 {
2460 if (p == 0)
2461 return;
2462 if (attrwarn)
2463 werror("unhandled %s attribute", s);
2464 tfree(p);
2465 }
2466
2467 static void
2468 dainit(NODE *d, NODE *a)
2469 {
2470 if (d == NULL) {
2471 asginit(a);
2472 } else if (d->n_op == CM) {
2473 int is = con_e(d->n_left);
2474 int ie = con_e(d->n_right);
2475 int i;
2476
2477 nfree(d);
2478 if (ie < is)
2479 uerror("negative initializer range");
2480 desinit(biop(LB, NIL, bcon(is)));
2481 for (i = is; i < ie; i++)
2482 asginit(ccopy(a));
2483 asginit(a);
2484 } else {
2485 cerror("dainit");
2486 }
2487 }
2488
2489 /*
2490 * Traverse down and tymerge() where appropriate.
2491 */
2492 static NODE *
2493 tymfix(NODE *p)
2494 {
2495 NODE *q;
2496 int o = coptype(p->n_op);
2497
2498 switch (o) {
2499 case LTYPE:
2500 break;
2501 case UTYPE:
2502 p->n_left = tymfix(p->n_left);
2503 break;
2504 case BITYPE:
2505 p->n_left = tymfix(p->n_left);
2506 p->n_right = tymfix(p->n_right);
2507 if (p->n_op == TYMERGE) {
2508 q = tymerge(p->n_left, p->n_right);
2509 q->n_ap = attr_add(q->n_ap, p->n_ap);
2510 tfree(p->n_left);
2511 nfree(p);
2512 p = q;
2513 }
2514 break;
2515 }
2516 return p;
2517 }
2518
2519 static NODE *
2520 aryfix(NODE *p)
2521 {
2522 NODE *q;
2523
2524 for (q = p; q->n_op != NAME; q = q->n_left) {
2525 if (q->n_op == LB) {
2526 q->n_right = optim(rmpconv(eve(q->n_right)));
2527 if ((blevel == 0 || rpole != NULL) &&
2528 !nncon(q->n_right))
2529 uerror("array size not constant");
2530 /*
2531 * Checks according to 6.7.5.2 clause 1:
2532 * "...the expression shall have an integer type."
2533 * "If the expression is a constant expression,
2534 * it shall have a value greater than zero."
2535 */
2536 if (!ISINTEGER(q->n_right->n_type))
2537 werror("array size is not an integer");
2538 else if (q->n_right->n_op == ICON &&
2539 glval(q->n_right) < 0 &&
2540 glval(q->n_right) != NOOFFSET) {
2541 uerror("array size cannot be negative");
2542 slval(q->n_right, 1);
2543 }
2544 } else if (q->n_op == CALL)
2545 q->n_right = namekill(q->n_right, 1);
2546 }
2547 return p;
2548 }