Debian Codegraph @

cc/cxxcom/scan.l

raw
1 %{
2 /* $Id: scan.l,v 1.11 2020/02/02 09:52:32 ragge Exp $ */
3
4 /*
5 * Copyright (c) 2002 Anders Magnusson. 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 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 %}
30
31
32 D [0-9]
33 L [a-zA-Z_]
34 H [a-fA-F0-9]
35 E [Ee][+-]?{D}+
36 P [Pp][+-]?{D}+
37 FS (f|F|l|L)?i?
38 IS (u|U|l|L)*
39 UL ({L}|\\u{H}{H}{H}{H}|\\U{H}{H}{H}{H}{H}{H}{H}{H})
40
41 %{
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <string.h>
45 #include <stdarg.h>
46 #include <ctype.h>
47
48 #include "pass1.h"
49 #include "cgram.h"
50
51 #define bdebug flexbdebug
52
53 static NODE *cvtdig(int radix);
54 static NODE *charcon(void);
55 static NODE *wcharcon(void);
56 static void control(int);
57 static void pragma(void);
58 int notype, parbal, inattr, parlvl, nodinit, inoso;
59 static int dotfile;
60 static int resw(TWORD, int);
61 static int namechk(void);
62
63 #define CPP_IDENT 2
64 #define CPP_LINE 3
65 #define CPP_HASH 4
66
67 #ifdef STABS
68 #define STABS_LINE(x) if (gflag && cftnsp) stabs_line(x)
69 #else
70 #define STABS_LINE(x)
71 #endif
72 #if defined(FLEX_SCANNER) && YY_FLEX_SUBMINOR_VERSION == 31
73 /* Hack to avoid unnecessary warnings */
74 FILE *yyget_in (void);
75 FILE *yyget_out (void);
76 int yyget_leng (void);
77 char *yyget_text (void);
78 void yyset_in (FILE *);
79 void yyset_out (FILE *);
80 int yyget_debug (void);
81 void yyset_debug (int);
82 int yylex_destroy (void);
83 extern int yyget_lineno (void);
84 extern void yyset_lineno (int);
85 #endif
86
87 %}
88
89 %%
90
91 "__func__" {
92 if (cftnsp == NULL)
93 uerror("__func__ outside function");
94 yylval.strp = cftnsp->sname; /* XXX - not C99 */
95 return(C_STRING);
96 }
97 "asm" { return(C_ASM); }
98 "auto" { return resw(AUTO, C_CLASS); }
99 "_Bool" { return resw(BOOL, C_TYPE); }
100 "break" { return(C_BREAK); }
101 "case" { return(C_CASE); }
102 "char" { return resw(CHAR, C_TYPE); }
103 "class" { yylval.intval = CLNAME; notype=1; return(C_STRUCT); }
104 "_Complex" { return resw(COMPLEX, C_TYPE); }
105 "const" { return resw(CON, C_QUALIFIER); }
106 "const_cast" { yylval.intval = CONST_CAST; return(CXX_CASTS); }
107 "continue" { return(C_CONTINUE); }
108 "default" { return(C_DEFAULT); }
109 "delete" { return(CXX_DELETE); }
110 "do" { return(C_DO); }
111 "double" { return resw(DOUBLE, C_TYPE); }
112 "dynamic_cast" { yylval.intval = DYN_CAST; return(CXX_CASTS); }
113 "else" { return(C_ELSE); }
114 "enum" { notype=1; return(C_ENUM); }
115 "extern" { return resw(EXTERN, C_CLASS); }
116 "float" { return resw(FLOAT, C_TYPE); }
117 "for" { return(C_FOR); }
118 "goto" { notype=1; return(C_GOTO); }
119 "if" { return(C_IF); }
120 "_Imaginary" { return resw(IMAG, C_TYPE); }
121 "inline" { return(C_FUNSPEC); }
122 "int" { return resw(INT, C_TYPE); }
123 "long" { return resw(LONG, C_TYPE); }
124 "namespace" { return(CXX_NAMESPACE); }
125 "new" { notype = 0; return(CXX_NEW); }
126 "register" { return resw(REGISTER, C_CLASS); }
127 "reinterpret_cast" { yylval.intval = REINT_CAST; return(CXX_CASTS); }
128 "restrict" { ; /* just ignore */ }
129 "return" { return(C_RETURN); }
130 "short" { return resw(SHORT, C_TYPE); }
131 "signed" { return resw(SIGNED, C_TYPE); }
132 "sizeof" { return(C_SIZEOF); }
133 "static" { return resw(STATIC, C_CLASS); }
134 "static_cast" { yylval.intval = STATIC_CAST; return(CXX_CASTS); }
135 "struct" { yylval.intval = STNAME; notype=1; return(C_STRUCT); }
136 "switch" { return(C_SWITCH); }
137 "template" { return(CXX_TEMPLATE); }
138 "typedef" { return resw(TYPEDEF, C_CLASS); }
139 "typename" { return(CXX_TYPENAME); }
140 "union" { yylval.intval = UNAME; notype=1; return(C_STRUCT); }
141 "unsigned" { return resw(UNSIGNED, C_TYPE); }
142 "using" { return(CXX_USING); }
143 "void" { return resw(VOID, C_TYPE); }
144 "volatile" { return resw(VOL, C_QUALIFIER); }
145 "while" { return(C_WHILE); }
146
147 {UL}({UL}|{D})* { return namechk(); }
148 0[xX]{H}+{IS}? { yylval.nodep = cvtdig(16); return(C_ICON); }
149 0{D}+{IS}? { yylval.nodep = cvtdig(8); return(C_ICON); }
150 {D}+{IS}? { yylval.nodep = cvtdig(10); return(C_ICON); }
151 L'(\\.|[^\\'])*' { yylval.nodep = wcharcon(); return(C_ICON); }
152 '(\\.|[^\\'])*' { yylval.nodep = charcon(); return(C_ICON); }
153
154 {D}+{E}{FS}? { yylval.nodep = floatcon(yytext); return(C_FCON); }
155 {D}*"."{D}+({E})?{FS}? { yylval.nodep = floatcon(yytext); return(C_FCON); }
156 {D}+"."{D}*({E})?{FS}? { yylval.nodep = floatcon(yytext); return(C_FCON); }
157 0[xX]{H}*"."{H}+{P}{FS}? { yylval.nodep = fhexcon(yytext); return(C_FCON); }
158 0[xX]{H}+"."{P}{FS}? { yylval.nodep = fhexcon(yytext); return(C_FCON); }
159 0[xX]{H}+{P}{FS}? { yylval.nodep = fhexcon(yytext); return(C_FCON); }
160
161 L?\"(\\.|[^\\"])*\" { yylval.strp = yytext; return C_STRING; }
162
163 "..." { return(C_ELLIPSIS); }
164 ">>=" { yylval.intval = RSEQ; return(C_ASOP); }
165 "<<=" { yylval.intval = LSEQ; return(C_ASOP); }
166 "+=" { yylval.intval = PLUSEQ; return(C_ASOP); }
167 "-=" { yylval.intval = MINUSEQ; return(C_ASOP); }
168 "*=" { yylval.intval = MULEQ; return(C_ASOP); }
169 "/=" { yylval.intval = DIVEQ; return(C_ASOP); }
170 "%=" { yylval.intval = MODEQ; return(C_ASOP); }
171 "&=" { yylval.intval = ANDEQ; return(C_ASOP); }
172 "^=" { yylval.intval = EREQ; return(C_ASOP); }
173 "|=" { yylval.intval = OREQ; return(C_ASOP); }
174 ">>" { yylval.intval = RS; return(C_SHIFTOP); }
175 "<<" { yylval.intval = LS; return(C_SHIFTOP); }
176 "++" { yylval.intval = INCR; return(C_INCOP); }
177 "--" { yylval.intval = DECR; return(C_INCOP); }
178 "->" { yylval.intval = STREF; return(C_STROP); }
179 "&&" { yylval.intval = ANDAND; return(C_ANDAND); }
180 "||" { yylval.intval = OROR; return(C_OROR); }
181 "<=" { yylval.intval = LE; return(C_RELOP); }
182 ">=" { yylval.intval = GE; return(C_RELOP); }
183 "==" { yylval.intval = EQ; return(C_EQUOP); }
184 "!=" { yylval.intval = NE; return(C_EQUOP); }
185 "::" { return(CXX_DUALCC); }
186 ";" { notype = 0; return(';'); }
187 ("{"|"<%") { notype = 0; return('{'); }
188 ("}"|"%>") { if (rpole) notype = 1; return('}'); }
189 "," { if (parbal && !inoso) notype = 0; return(','); }
190 ":" { if (doing_init) nodinit--; return(':'); }
191 "=" { return('='); }
192 "(" { parbal++; notype = 0; return('('); }
193 ")" { parbal--;
194 inoso = 0;
195 if (parbal==0) { notype = 0; }
196 if (inattr && parlvl == parbal)
197 inattr = 0;
198 return(')'); }
199 ("["|"<:") { return('['); }
200 ("]"|":>") { return(']'); }
201 "." { yylval.intval = DOT; return(C_STROP); }
202 "&" { return('&'); }
203 "!" { yylval.intval = NOT; return(C_UNOP); }
204 "~" { yylval.intval = COMPL; return(C_UNOP); }
205 "-" { return('-'); }
206 "+" { return('+'); }
207 "*" { if (parbal && notype == 0) notype = 1; return('*'); }
208 "/" { yylval.intval = DIV; return(C_DIVOP); }
209 "%" { yylval.intval = MOD; return(C_DIVOP); }
210 "<" { yylval.intval = LT; return(C_RELOP); }
211 ">" { yylval.intval = GT; return(C_RELOP); }
212 "^" { return('^'); }
213 "|" { return('|'); }
214 "?" { if (doing_init) nodinit++; return('?'); }
215 ^#pragma[ \t].* { pragma(); }
216 ^#ident[ \t].* { control(CPP_IDENT); }
217 ^#line[ \t].* { control(CPP_LINE); }
218 ^#.* { control(CPP_HASH); }
219
220 [ \t\v\f] { }
221 "\n" { ++lineno; STABS_LINE(lineno); }
222 . { /* ignore bad characters */ }
223
224 %%
225
226 int issyshdr;
227 char *ftitle = "<stdin>";
228
229 static int
230 namechk(void)
231 {
232 struct symtab *s;
233 int i;
234
235 yylval.strp = addname(yytext);
236
237 while ((i = input()) == ' ' || i == '\t')
238 ;
239 if (i == ':') {
240 if ((i = input()) == ':')
241 return CXX_MORENM;
242 unput(i);
243 if (doing_init && nodinit == 0)
244 return(GCC_DESIG);
245 i = ':';
246 }
247 unput(i);
248
249 #ifdef GCC_COMPAT
250 if ((i = gcc_keyword(yylval.strp, &yylval.nodep)) > 0)
251 return i;
252 #endif
253
254 if (notype)
255 return(C_NAME);
256 s = lookup(yylval.strp, SNOCREAT);
257 return s && s->sclass == TYPEDEF ? notype=1, C_TYPENAME : C_NAME;
258 }
259
260 int
261 yywrap(void)
262 {
263 if (0) unput(0); /* quiet gcc */
264 return(1);
265 }
266
267 int
268 resw(TWORD t, int rv)
269 {
270 if (inattr) {
271 yylval.strp = addname(yytext);
272 return C_NAME;
273 }
274
275 switch (rv) {
276 case C_CLASS:
277 yylval.nodep = block(CLASS, NIL, NIL, t, 0, 0);
278 return rv;
279
280 case C_QUALIFIER:
281 yylval.nodep = block(QUALIFIER, NIL, NIL, 0, 0, 0);
282 yylval.nodep->n_qual = t;
283 return rv;
284
285 case C_TYPE:
286 yylval.nodep = mkty(t, 0, 0);
287 notype=1;
288 return(rv);
289
290 default:
291 cerror("resw");
292 }
293 return 0;
294 }
295
296 #ifndef SOFTFLOAT
297
298 static long double
299 typround(long double dc, char *e, TWORD *tw)
300 {
301 int im = 0;
302
303 *tw = DOUBLE;
304 for (; *e; e++) {
305 switch (*e) {
306 case 'f':
307 case 'F':
308 *tw = FLOAT;
309 dc = (float)dc;
310 break;
311 case 'l':
312 case 'L':
313 *tw = LDOUBLE;
314 break;
315 case 'i':
316 case 'I':
317 im = 1;
318 break;
319 }
320 }
321 if (*tw == DOUBLE)
322 dc = (double)dc;
323 #ifndef NO_COMPLEX
324 if (im)
325 *tw += (FIMAG-FLOAT);
326 #endif
327 return dc;
328 }
329
330 /*
331 * XXX floatcon() and fhexcon() should be in support libraries for
332 * the target floating point.
333 */
334 static NODE *
335 f2(char *str)
336 {
337 TWORD tw;
338 NODE *p;
339 long double dc;
340 char *eptr;
341
342 #ifdef HAVE_STRTOLD
343 dc = strtold(str, &eptr); /* XXX - avoid strtod() */
344 #else
345 dc = strtod(str, &eptr); /* XXX - avoid strtod() */
346 #endif
347 dc = typround(dc, eptr, &tw);
348 p = block(FCON, NIL, NIL, tw, 0, 0);
349 p->n_dcon = fltallo();
350 FCAST(p->n_dcon)->fp = dc;
351 return p;
352 }
353
354 NODE *
355 floatcon(char *s)
356 {
357 return f2(s);
358 }
359
360 static int
361 h2n(int ch)
362 {
363 if (ch >= '0' && ch <= '9')
364 return ch - '0';
365 if (ch >= 'a' && ch <= 'f')
366 return ch - 'a' + 10;
367 return ch - 'A' + 10;
368
369 }
370
371 NODE *
372 fhexcon(char *c)
373 {
374 TWORD tw;
375 char *ep;
376 long double d;
377 int i, ed;
378 NODE *p;
379
380 d = 0.0;
381 ed = 0;
382 c+= 2; /* skip 0x */
383 #define FSET(n) { d *= 2; if (i & n) d += 1.0; }
384 for (; *c != '.' && *c != 'p' && *c != 'P'; c++) {
385 i = h2n(*c);
386 FSET(8); FSET(4); FSET(2); FSET(1);
387 }
388 if (*c != '.' && *c != 'p' && *c != 'P')
389 cerror("fhexcon");
390 if (*c == '.') {
391 c++;
392 for (; *c != 'p' && *c != 'P'; c++) {
393 i = h2n(*c);
394 FSET(8); FSET(4); FSET(2); FSET(1);
395 ed -= 4;
396 }
397 }
398 if (*c != 'P' && *c != 'p')
399 cerror("fhexcon2");
400 c++;
401 ed += (int)strtol(c, &ep, 10);
402
403 /* avoid looping in vain. Idea from Fred J. Tydeman */
404 if (ed > 32769) ed = 32769;
405 if (ed < -32769) ed = -32769;
406
407 while (ed > 0)
408 d *= 2, ed--;
409 while (ed < 0)
410 d /= 2, ed++;
411 d = typround(d, ep, &tw);
412 p = block(FCON, NIL, NIL, tw, 0, 0);
413 p->n_dcon = fltallo();
414 FCAST(p->n_dcon)->fp = d;
415 return p;
416 }
417 #endif
418
419 unsigned int
420 esccon(char **sptr)
421 {
422 char *wr = *sptr;
423 char *owr;
424 char c;
425 unsigned int val;
426 int wsz = 4, esccon_warn = 1;
427
428 switch (*wr++) {
429 case 'a': val = '\a'; break;
430 case 'b': val = '\b'; break;
431 case 'f': val = '\f'; break;
432 case 'n': val = '\n'; break;
433 case 'r': val = '\r'; break;
434 case 't': val = '\t'; break;
435 case 'v': val = '\v'; break;
436 case '\"': val = '\"'; break;
437 case 'x': val = (int)strtoul(wr, &wr, 16); break;
438 /* ISO/IEC 9099:1999 (E) 6.4.3 */
439 case 'U'|(char)0x80:
440 esccon_warn = 0;
441 /* FALLTHROUGH */
442 case 'U':
443 wsz = 8;
444 /* FALLTHROUGH */
445 case 'u':
446 owr = wr;
447 while (wr < (owr + wsz))
448 if (*wr == '\0')
449 break;
450 else
451 ++wr;
452 if (wr != (owr + wsz)) {
453 /* incomplete */
454 val = (int)strtoul(owr, &wr, 16);
455 } else {
456 c = owr[wsz];
457 owr[wsz] = '\0'; /* prevent it from reading too much */
458 val = (int)strtoul(owr, &wr, 16);
459 owr[wsz] = c;
460 }
461 if (wr != (owr + wsz))
462 werror("incomplete universal character name");
463 if (wsz == 4)
464 val &= 0xFFFF;
465 if (esccon_warn && ((val >= 0xD800 && val <= 0xDFFF) ||
466 (val < 0xA0 && val != 0x24 && val != 0x40 && val != 0x60)))
467 werror("invalid universal character name %04X", val);
468 break;
469 case '0': case '1': case '2': case '3': case '4':
470 case '5': case '6': case '7':
471 val = wr[-1] - '0';
472 if (*wr >= '0' && *wr <= '7') {
473 val = (val << 3) + (*wr++ - '0');
474 if (*wr >= '0' && *wr <= '7')
475 val = (val << 3) + (*wr++ - '0');
476 }
477 break;
478 default: val = wr[-1];
479 }
480 *sptr = wr;
481 return val;
482 }
483
484 NODE *
485 cvtdig(int radix)
486 {
487 NODE *p;
488 TWORD otype, ntype;
489 unsigned long long v;
490 char *ch = yytext;
491 int n, numl, numu;
492
493 if (radix == 16)
494 ch += 2; /* Skip 0x */
495
496 v = 0;
497 while ((*ch >= '0' && *ch <= '9') || (*ch >= 'a' && *ch <= 'f') ||
498 (*ch >= 'A' && *ch <= 'F')) {
499 v *= radix;
500 n = *ch;
501 n = (n <= '9' ? n - '0' : (n > 'F' ? n - 'a' : n - 'A') + 10);
502 ch++;
503 v += n;
504 }
505 /* Parse trailing chars */
506 ntype = INT;
507 numl = numu = 0;
508 for (n = 0; n < 3; n++) {
509 if (*ch == 0)
510 break;
511 if ((*ch == 'l' || *ch == 'L') && numl < 2)
512 ntype+=2, numl++;
513 else if ((*ch == 'u' || *ch == 'U') && numu < 1)
514 ntype = ENUNSIGN(ntype), numu++;
515 else
516 break;
517 ch++;
518 }
519 if (*ch)
520 uerror("constant has too many '%c'", *ch);
521
522 otype = ntype;
523 switch (ntype) {
524 case INT:
525 case LONG:
526 case LONGLONG:
527 if (radix == 10) {
528 if (otype == LONGLONG)
529 break;
530 if (v > MAX_LONG) {
531 ntype = LONGLONG;
532 if (otype == LONG)
533 break;
534 } else if (v > MAX_INT)
535 ntype = LONG;
536 } else {
537 if (v > MAX_LONGLONG) {
538 ntype = ULONGLONG;
539 if (otype == LONGLONG)
540 break;
541 } else if (v > MAX_ULONG) {
542 ntype = LONGLONG;
543 } else if (v > MAX_LONG) {
544 ntype = ULONG;
545 if (otype == LONG)
546 break;
547 } else if (v > MAX_UNSIGNED) {
548 ntype = LONG;
549 } else if (v > MAX_INT)
550 ntype = UNSIGNED;
551 }
552 break;
553 case UNSIGNED:
554 case ULONG:
555 if (v > MAX_ULONG) {
556 ntype = ULONGLONG;
557 if (otype == ULONG)
558 break;
559 } else if (v > MAX_UNSIGNED)
560 ntype = ULONG;
561 break;
562 }
563
564 ntype = ctype(ntype);
565 p = xbcon(v, NULL, ntype);
566 ASGLVAL(p->n_slval, v);
567
568 return p;
569 }
570
571 /*
572 * Convert a character constant to an integer.
573 */
574 NODE *
575 charcon(void)
576 {
577 int lastcon = 0;
578 int val, i = 0;
579 char *pp = yytext;
580
581 pp++; /* skip ' */
582 while (*pp != '\'') {
583 if (*pp++ == '\\') {
584 val = esccon(&pp);
585 } else
586 val = pp[-1];
587 makecc(val, i);
588 i++;
589 }
590
591 if (i == 0)
592 uerror("empty character constant");
593 else if (i > (SZINT/SZCHAR) || (i>1))
594 werror("too many characters in character constant");
595 return bcon(lastcon);
596 }
597
598 NODE *
599 wcharcon(void)
600 {
601 unsigned int lastcon = 0;
602 unsigned int val, i = 0;
603 char *pp = yytext;
604
605 pp++; /* skip L */
606 pp++; /* skip ' */
607 while (*pp != '\'') {
608 if (*pp++ == '\\') {
609 val = esccon(&pp);
610 } else
611 val = pp[-1];
612 #if WCHAR_SIZE == 2
613 lastcon = (lastcon << 16) | (val & 0xFFFF);
614 #else
615 lastcon = val;
616 #endif
617 i++;
618 }
619
620 if (i == 0)
621 uerror("empty wide-character constant");
622 else if (i > 1)
623 werror("too many characters in wide-character constant");
624 return xbcon(lastcon, NULL, ctype(UNSIGNED));
625 }
626
627 #ifndef MYDOTFILE
628 /*
629 * Get basename and print it as '.file "basename.c"'
630 */
631 static void
632 printdotfile(char *file)
633 {
634 char *p;
635
636 if ((p = strrchr(file, '/')) == NULL)
637 p = file;
638 else
639 p++;
640 printf(PRTPREF "\t.file \"%s\"\n", p);
641 }
642 #endif
643
644 void
645 control(int t)
646 {
647 char *wr = yytext;
648 char *eptr;
649 int val;
650
651 wr++; /* Skip initial '#' */
652 switch (t) {
653 case CPP_IDENT:
654 return; /* Just skip these for now. */
655
656 case CPP_LINE:
657 wr += 4;
658 /* FALLTHROUGH */
659 case CPP_HASH:
660 val = (int)strtol(wr, &eptr, 10);
661 if (wr == eptr) /* Illegal string */
662 goto bad;
663 wr = eptr;
664 lineno = val - 1;
665 while (*wr && *wr != '\"')
666 wr++;
667 if (*wr == 0)
668 return;
669 if (*wr++ != '\"')
670 goto bad;
671 eptr = wr;
672 while (*wr && *wr != '\"')
673 wr++;
674 if (*wr != '\"')
675 goto bad;
676 *wr = 0;
677 ftitle = addstring(eptr);
678 #ifdef STABS
679 if (gflag)
680 stabs_file(ftitle);
681 #endif
682 if (dotfile == 0) {
683 dotfile++;
684 printdotfile(ftitle);
685 }
686 }
687 return;
688 bad:
689 werror("%s: illegal control", yytext);
690 }
691
692 int pragma_allpacked;
693 int pragma_packed, pragma_aligned;
694 char *pragma_renamed;
695
696 static int
697 pragmas_weak(char *str)
698 {
699 struct symtab *sp;
700 char *s1, *s2;
701
702 if ((s1 = pragtok(NULL)) == NULL)
703 return 1;
704 if ((s2 = pragtok(NULL)) == NULL) {
705 sp = lookup(addname(s1), SNORMAL);
706 #ifdef GCC_COMPAT
707 sp->sap = attr_add(sp->sap, gcc_attr_parse(bdty(NAME, "weak")));
708 #else
709 sp->sap = 0;
710 #endif
711 } else if (*s2 == '=') {
712 if ((s2 = pragtok(NULL)) == NULL)
713 return 1;
714 sp = lookup(addname(s2), SNORMAL);
715 #ifdef GCC_COMPAT
716 sp->sap = attr_add(sp->sap, gcc_attr_parse(bdty(CALL,
717 bdty(NAME, "aliasweak"), bdty(STRING, s1, 0))));
718 #else
719 sp->sap = NULL;
720 #endif
721 } else
722 return 1;
723 return 0;
724 }
725
726 char *pragstore;
727
728 /* trivial tokenizer for pragmas */
729 #define ps pragstore
730 char *
731 pragtok(char *sin)
732 {
733 static char ss[2];
734 char *rv;
735
736 if (sin)
737 ps = sin;
738
739 for (; isspace((int)*ps); ps++)
740 ;
741 if (*ps == 0)
742 return NULL;
743 for (rv = ps; isalpha((int)*ps) || isdigit((int)*ps) || *ps == '_'; ps++)
744 ;
745 ss[0] = *ps;
746 if (rv == ps) {
747 rv = ss, ps++;
748 } else {
749 *ps = 0;
750 rv = tmpstrdup(rv);
751 *ps = ss[0];
752 }
753 return rv;
754 }
755
756 /* return 1 on error */
757 int
758 eat(int ch)
759 {
760 char *s = pragtok(0);
761 return (s == 0 || *s != ch);
762 }
763
764 static int
765 pragmas_alpack(char *t)
766 {
767 char *s;
768 int ap;
769
770 ap = (s = pragtok(0)) ? atoi(s) : 1;
771 if (strcmp(t, "packed") == 0)
772 pragma_packed = ap;
773 else
774 pragma_aligned = ap;
775 return 0;
776 }
777
778
779 /*
780 * Packing control.
781 * still missing push/pop.
782 */
783 static int
784 pragmas_pack(char *t)
785 {
786 char *s;
787
788 if (eat('('))
789 return 1;
790 s = pragtok(0);
791 if (*s == ')')
792 return pragma_allpacked = 0;
793
794 if (*s < '0' || *s > '9') /* no number */
795 return 1;
796 pragma_allpacked = atoi(s);
797 return eat(')');
798 }
799
800 static int
801 pragmas_renamed(char *t)
802 {
803 char *f = pragtok(0);
804
805 if (f == 0)
806 return 1;
807 pragma_renamed = newstring(f, strlen(f));
808 return 0;
809 }
810
811 static int
812 pragmas_stdc(char *t)
813 {
814 return 0; /* Just ignore */
815 }
816
817 struct pragmas {
818 char *name;
819 int (*fun)(char *);
820 } pragmas[] = {
821 { "pack", pragmas_pack },
822 { "packed", pragmas_alpack },
823 { "aligned", pragmas_alpack },
824 { "rename", pragmas_renamed },
825 #ifdef GCC_COMPAT
826 { "GCC", pragmas_gcc },
827 #endif
828 { "STDC", pragmas_stdc },
829 { "weak", pragmas_weak },
830 { "ident", NULL },
831 { 0 },
832 };
833
834 /*
835 * got a full pragma line. Split it up here.
836 */
837 static void
838 pragma(void)
839 {
840 struct pragmas *p;
841 char *t, *pt;
842
843 if ((t = pragtok(&yytext[7])) != NULL) {
844 pt = ps;
845 for (p = pragmas; p->name; p++) {
846 if (strcmp(t, p->name) == 0) {
847 if (p->fun && (*p->fun)(t))
848 uerror("bad argument to #pragma");
849 return;
850 }
851 }
852 ps = pt;
853 if (mypragma(t))
854 return;
855 }
856 warner(Wunknown_pragmas, t, ps);
857 }
858
859 void
860 cunput(char c)
861 {
862 unput(c);
863 }