Debian Codegraph @

cc/ccom/scan.l

raw
1 %{
2 /* $Id: scan.l,v 1.162 2020/02/02 09:52:31 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 B [0-1]
33 D [0-9]
34 L [a-zA-Z_]
35 H [a-fA-F0-9]
36 E [Ee][+-]?{D}+
37 P [Pp][+-]?{D}+
38 FS ((f|F|l|L)?i|i?(f|F|l|L))
39 IS (u|U|l|L|i)*
40 UL ({L}|[\x80-\xFF])
41 UC (L|u|U)
42 US (L|u|U|u8)
43
44 %{
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <ctype.h>
50
51 #include "pass1.h"
52 #include "cgram.h"
53 #include "unicode.h"
54
55 #define bdebug flexbdebug
56
57 static void cvtdig(int radix);
58 static void charcon(void);
59 static void wcharcon(void);
60 static void control(int);
61 static void pragma(void);
62 static int dotfile;
63 int notype, parbal, inattr, parlvl, nodinit, inoso;
64 int kwdecode(struct symtab *s);
65
66 #define CPP_IDENT 2
67 #define CPP_LINE 3
68 #define CPP_HASH 4
69
70 #ifdef STABS
71 #define STABS_LINE(x) if (gflag && cftnsp) stabs_line(x)
72 #else
73 #define STABS_LINE(x)
74 #endif
75 #if defined(FLEX_SCANNER) && YY_FLEX_SUBMINOR_VERSION == 31
76 /* Hack to avoid unnecessary warnings */
77 FILE *yyget_in (void);
78 FILE *yyget_out (void);
79 int yyget_leng (void);
80 char *yyget_text (void);
81 void yyset_in (FILE *);
82 void yyset_out (FILE *);
83 int yyget_debug (void);
84 void yyset_debug (int);
85 int yylex_destroy (void);
86 extern int yyget_lineno (void);
87 extern void yyset_lineno (int);
88 #endif
89
90 %}
91
92 %%
93
94 {UL}({UL}|{D})* { struct symtab *s;
95 int i = 0;
96
97 yylval.strp = addname(yytext);
98 if ((s = lookup(yylval.strp, SNOCREAT)) != NULL &&
99 s->sclass == KEYWORD)
100 return kwdecode(s);
101
102 #ifdef GCC_COMPAT
103 if (doing_init && nodinit == 0) {
104 /* check for name: for old gcc compat */
105 while ((i = input()) == ' ' || i == '\t')
106 ;
107 if (i == ':')
108 return(GCC_DESIG);
109 unput(i);
110 }
111 if ((i = gcc_keyword(yylval.strp)) > 0) {
112 if (i == PCC_OFFSETOF)
113 inoso = 1;
114 return i;
115 }
116 #endif
117 if (i == 0) {
118 if (notype)
119 return(C_NAME);
120 return s && s->sclass == TYPEDEF ?
121 notype=1, C_TYPENAME : C_NAME;
122 }
123 }
124
125 0[xX]{H}+{IS}? { cvtdig(16); return(C_ICON); }
126 0{D}+{IS}? { cvtdig(8); return(C_ICON); }
127 0[bB]{B}+{IS}? { cvtdig(2); return(C_ICON); }
128 {D}+{IS}? { cvtdig(10); return(C_ICON); }
129 {UC}'(\\.|[^\\'])*' { wcharcon(); return(C_ICON); }
130 '(\\.|[^\\'])*' { charcon(); return(C_ICON); }
131
132 {D}+{E}{FS}? { floatcon(yytext); return(C_FCON); }
133 {D}*"."{D}+({E})?{FS}? { floatcon(yytext); return(C_FCON); }
134 {D}+"."{D}*({E})?{FS}? { floatcon(yytext); return(C_FCON); }
135 0[xX]{H}*"."{H}+{P}{FS}? { floatcon(yytext); return(C_FCON); }
136 0[xX]{H}+"."{P}{FS}? { floatcon(yytext); return(C_FCON); }
137 0[xX]{H}+{P}{FS}? { floatcon(yytext); return(C_FCON); }
138
139 {US}?\"(\\.|[^\\"])*\" { yylval.strp = yytext; return C_STRING; }
140
141 "..." { return(C_ELLIPSIS); }
142 ">>=" { yylval.intval = RSEQ; return(C_ASOP); }
143 "<<=" { yylval.intval = LSEQ; return(C_ASOP); }
144 "+=" { yylval.intval = PLUSEQ; return(C_ASOP); }
145 "-=" { yylval.intval = MINUSEQ; return(C_ASOP); }
146 "*=" { yylval.intval = MULEQ; return(C_ASOP); }
147 "/=" { yylval.intval = DIVEQ; return(C_ASOP); }
148 "%=" { yylval.intval = MODEQ; return(C_ASOP); }
149 "&=" { yylval.intval = ANDEQ; return(C_ASOP); }
150 "^=" { yylval.intval = EREQ; return(C_ASOP); }
151 "|=" { yylval.intval = OREQ; return(C_ASOP); }
152 ">>" { yylval.intval = RS; return(C_SHIFTOP); }
153 "<<" { yylval.intval = LS; return(C_SHIFTOP); }
154 "++" { yylval.intval = INCR; return(C_INCOP); }
155 "--" { yylval.intval = DECR; return(C_INCOP); }
156 "->" { yylval.intval = STREF; return(C_STROP); }
157 "&&" { yylval.intval = ANDAND; return(C_ANDAND); }
158 "||" { yylval.intval = OROR; return(C_OROR); }
159 "<=" { yylval.intval = LE; return(C_RELOP); }
160 ">=" { yylval.intval = GE; return(C_RELOP); }
161 "==" { yylval.intval = EQ; return(C_EQUOP); }
162 "!=" { yylval.intval = NE; return(C_EQUOP); }
163 ";" { notype = 0; return(';'); }
164 ("{"|"<%") { notype = 0; return('{'); }
165 ("}"|"%>") { if (rpole) notype = 1; return('}'); }
166 "," { if (parbal && !inoso) notype = 0;
167 if (parbal == 0) notype = 1; return(','); }
168 ":" { if (doing_init) nodinit--; return(':'); }
169 "=" { return('='); }
170 "(" { parbal++; notype = 0; return('('); }
171 ")" { parbal--;
172 inoso = 0;
173 if (parbal==0) { notype = 0; }
174 if (inattr && parlvl == parbal)
175 inattr = 0;
176 return(')'); }
177 ("["|"<:") { return('['); }
178 ("]"|":>") { return(']'); }
179 "." { yylval.intval = DOT; return(C_STROP); }
180 "&" { return('&'); }
181 "!" { yylval.intval = NOT; return(C_UNOP); }
182 "~" { yylval.intval = COMPL; return(C_UNOP); }
183 "-" { return('-'); }
184 "+" { return('+'); }
185 "*" { if (parbal && notype == 0) notype = 1; return('*'); }
186 "/" { yylval.intval = DIV; return(C_DIVOP); }
187 "%" { yylval.intval = MOD; return(C_DIVOP); }
188 "<" { yylval.intval = LT; return(C_RELOP); }
189 ">" { yylval.intval = GT; return(C_RELOP); }
190 "^" { return('^'); }
191 "|" { return('|'); }
192 "?" { if (doing_init) nodinit++; return('?'); }
193 ^#pragma[ \t].* { pragma(); }
194 ^#ident[ \t].* { control(CPP_IDENT); }
195 ^#line[ \t].* { control(CPP_LINE); }
196 ^#.* { control(CPP_HASH); }
197
198 [ \t\v\f] { }
199 "\n" { ++lineno; STABS_LINE(lineno); }
200 . { /* ignore bad characters */ }
201
202 %%
203
204 int issyshdr;
205
206 int
207 yywrap(void)
208 {
209 if (0) unput(0); /* quiet gcc */
210 return(1);
211 }
212
213 #define DEFKW 500
214 #define KWFUNC 501
215 #define KWNOT 502
216
217 struct keywords {
218 char *name;
219 int cword, wclass;
220 } keywords[] = {
221 { "__func__", 0, KWFUNC },
222 { "_Alignas", C_ALIGNAS, DEFKW },
223 { "_Alignof", C_ALIGNOF, DEFKW },
224 { "_Atomic", C_ATOMIC, DEFKW },
225 { "asm", C_ASM, DEFKW },
226 { "auto", AUTO, C_CLASS },
227 { "_Bool", BOOL, C_TYPE },
228 { "break", C_BREAK, DEFKW },
229 { "case", C_CASE, DEFKW },
230 { "char", CHAR, C_TYPE },
231 { "continue", C_CONTINUE, DEFKW },
232 { "_Complex", COMPLEX, C_TYPE },
233 { "const", CON, C_QUALIFIER },
234 { "default", C_DEFAULT, DEFKW },
235 { "do", C_DO, DEFKW },
236 { "double", DOUBLE, C_TYPE },
237 { "else", C_ELSE, DEFKW },
238 { "enum", C_ENUM, KWNOT },
239 { "extern", EXTERN, C_CLASS },
240 { "float", FLOAT, C_TYPE },
241 { "for", C_FOR, DEFKW },
242 { "_Generic", C_GENERIC, DEFKW },
243 { "goto", C_GOTO, KWNOT },
244 { "if", C_IF, DEFKW },
245 { "_Imaginary", IMAG, C_TYPE },
246 { "inline", INLINE, C_FUNSPEC },
247 { "int", INT, C_TYPE },
248 { "long", LONG, C_TYPE },
249 { "_Noreturn", NORETURN, C_FUNSPEC },
250 { "register", REGISTER, C_CLASS },
251 { "restrict", 0, C_QUALIFIER },
252 { "return", C_RETURN, DEFKW },
253 { "short", SHORT, C_TYPE },
254 { "signed", SIGNED, C_TYPE },
255 { "sizeof", C_SIZEOF, DEFKW },
256 { "static", STATIC, C_CLASS },
257 { "_Static_assert", C_STATICASSERT, DEFKW },
258 { "struct", STNAME, C_STRUCT },
259 { "switch", C_SWITCH, DEFKW },
260 { "_Thread_local", THLOCAL, C_CLASS },
261 { "typedef", TYPEDEF, C_CLASS },
262 { "union", UNAME, C_STRUCT },
263 { "unsigned", UNSIGNED, C_TYPE },
264 { "void", VOID, C_TYPE },
265 { "volatile", VOL, C_QUALIFIER },
266 { "while", C_WHILE, DEFKW },
267 };
268
269
270 void
271 kwinit(void)
272 {
273 struct symtab *s;
274 int i, n = sizeof(keywords) / sizeof(keywords[0]);
275
276 for (i = 0; i < n; i++) {
277 s = lookup(addname(keywords[i].name), 0);
278 s->sclass = KEYWORD;
279 s->soffset = i;
280 }
281 }
282
283 int
284 kwdecode(struct symtab *s)
285 {
286 struct keywords *kw = &keywords[s->soffset];
287
288 if (inattr && kw->cword != C_SIZEOF)
289 return C_NAME;
290
291 switch (kw->wclass) {
292 case C_TYPE:
293 yylval.type = kw->cword;
294 notype=1;
295 break;
296
297 case C_CLASS:
298 if (kw->cword == THLOCAL)
299 uerror("_Thread_local not supported");
300 yylval.type = kw->cword;
301 break;
302
303 case C_QUALIFIER:
304 yylval.type = kw->cword;
305 break;
306
307 case C_FUNSPEC:
308 yylval.type = kw->cword;
309 break;
310
311 case DEFKW:
312 return kw->cword;
313
314 case KWFUNC:
315 if (cftnsp == NULL)
316 uerror("__func__ outside function");
317 yylval.strp = cftnsp->sname;
318 return(C_STRING);
319
320 case KWNOT:
321 notype = 1;
322 return kw->cword;
323
324 case C_STRUCT:
325 notype = 1;
326 yylval.intval = kw->cword;
327 break;
328
329 default:
330 cerror("keyword %s not found", kw->name);
331 }
332 return kw->wclass;
333 }
334
335 static TWORD
336 endtyp(char *s)
337 {
338 TWORD tw = DOUBLE;
339
340 for (; *s; s++)
341 ;
342 s--;
343 if (*s == 'i' || *s == 'I')
344 tw += (FIMAG-FLOAT), s--;
345 if (*s == 'f' || *s == 'F')
346 tw--, s--;
347 else if (*s == 'l' || *s == 'L')
348 tw++,s--;
349 if ((*s == 'i' || *s == 'I') && ISFTY(tw))
350 tw += (FIMAG-FLOAT), s--;
351 /* XXX complain */
352 return tw;
353 }
354
355 void
356 floatcon(char *s)
357 {
358 yylval.flt.t = ctype(endtyp(s));
359 strtosf(&yylval.flt.sf, s, yylval.flt.t);
360 }
361
362 void
363 cvtdig(int radix)
364 {
365 TWORD ntype;
366 unsigned long long v;
367 char *ch = yytext;
368 int n, numl, numu;
369
370 if (radix == 16 || radix == 2)
371 ch += 2; /* Skip 0x or 0b */
372
373 v = 0;
374 while ((*ch >= '0' && *ch <= '9') || (*ch >= 'a' && *ch <= 'f') ||
375 (*ch >= 'A' && *ch <= 'F')) {
376 v *= radix;
377 n = *ch;
378 n = (n <= '9' ? n - '0' : (n > 'F' ? n - 'a' : n - 'A') + 10);
379 ch++;
380 v += n;
381 }
382 /* Parse trailing chars */
383 ntype = INT;
384 numl = numu = 0;
385 for (n = 0; n < 3; n++) {
386 if (*ch == 0)
387 break;
388 if ((*ch == 'l' || *ch == 'L') && numl < 2)
389 ntype+=2, numl++;
390 else if ((*ch == 'u' || *ch == 'U') && numu < 1)
391 ntype = ENUNSIGN(ntype), numu++;
392 else if (*ch == 'i')
393 ntype = DOUBLE;
394 else
395 break;
396 ch++;
397 }
398 if (*ch)
399 uerror("constant has too many '%c'", *ch);
400
401 switch (ntype) {
402 case DOUBLE: /* special case */
403 floatcon(ch);
404 return;
405
406 case INT:
407 case LONG:
408 case LONGLONG:
409 if (radix == 10) {
410 if (ntype == LONGLONG)
411 break;
412 if (v > MAX_LONG)
413 ntype = LONGLONG;
414 else if (v > MAX_INT)
415 ntype = LONG;
416 } else {
417 if (v > MAX_LONGLONG) {
418 ntype = ULONGLONG;
419 } else if (v > MAX_ULONG) {
420 if (ntype < LONGLONG)
421 ntype = LONGLONG;
422 } else if (v > MAX_LONG) {
423 if (ntype < ULONG)
424 ntype = ULONG;
425 } else if (v > MAX_UNSIGNED) {
426 if (ntype < LONG)
427 ntype = LONG;
428 } else if (v > MAX_INT) {
429 if (ntype < UNSIGNED)
430 ntype = UNSIGNED;
431 }
432 }
433 break;
434 case UNSIGNED:
435 case ULONG:
436 if (v > MAX_ULONG) {
437 ntype = ULONGLONG;
438 } else if (v > MAX_UNSIGNED)
439 ntype = ULONG;
440 break;
441 }
442
443 yylval.li.val = v;
444 yylval.li.t = ctype(ntype);
445 }
446
447 /*
448 * return value of escaped character constant
449 */
450 unsigned int
451 esccon(char **sptr)
452 {
453 unsigned int val;
454 char *wr = *sptr;
455 char c;
456
457 wr++; /* skip \ */
458 switch (c = *wr++) {
459 case 'a': val = '\a'; break;
460 case 'b': val = '\b'; break;
461 #ifdef GCC_COMPAT
462 case 'e': val = '\033'; break;
463 #endif
464 case 'f': val = '\f'; break;
465 case 'n': val = '\n'; break;
466 case 'r': val = '\r'; break;
467 case 't': val = '\t'; break;
468 case 'v': val = '\v'; break;
469
470 case '\"': val = '\"'; break;
471 case '\'': val = '\''; break;
472 case '\?': val = '\?'; break;
473 case '\\': val = '\\'; break;
474
475 case 'x':
476 val = 0;
477 for (;;) {
478 c = *wr;
479 if (c >= '0' && c <= '9')
480 c = c - '0';
481 else if (c >= 'a' && c <= 'f')
482 c = c - 'a' + 10;
483 else if (c >= 'A' && c <= 'F')
484 c = c - 'A' + 10;
485 else
486 break;
487
488 val = (val << 4) + c;
489 wr++;
490 }
491 break;
492
493 case '0': case '1': case '2': case '3':
494 case '4': case '5': case '6': case '7':
495 val = (c - '0');
496 c = *wr;
497 if (c >= '0' && c <= '7') {
498 wr++;
499 val = (val << 3) + (c - '0');
500 c = *wr;
501 if (c >= '0' && c <= '7') {
502 wr++;
503 val = (val << 3) + (c - '0');
504 }
505 }
506 break;
507
508 default:
509 werror("unknown escape sequence \\%c", c);
510 val = c;
511 break;
512 }
513
514 *sptr = wr;
515 return val;
516 }
517
518 /*
519 * Convert (one) character constant to an int.
520 * Handle as unsigned except if only one character and char is signed.
521 */
522 void
523 charcon(void)
524 {
525 unsigned int val = 0, i = 0;
526 char *pp = yytext;
527
528 pp++; /* skip ' */
529 while (*pp != '\'') {
530 val <<= SZCHAR; /* XXX big endian? */
531 if (*pp == '\\')
532 val |= esccon(&pp);
533 else
534 val |= (*pp++ & ((1 << SZCHAR)-1));
535 i++;
536 }
537
538 if (i == 0)
539 uerror("empty character constant");
540 else if (i == 1 && xuchar == 0)
541 val = (signed char)val; /* XXX other sizes of char? */
542 else if (i > 1)
543 werror("too many characters in character constant");
544
545 yylval.li.val = val;
546 yylval.li.t = INT;
547 }
548
549 /*
550 * Convert a wide-character constant to an unsigned int
551 */
552 void
553 wcharcon(void)
554 {
555 unsigned int val = 0, i = 0;
556 char *pp = yytext;
557
558 pp++; /* skip L */
559 pp++; /* skip ' */
560 while (*pp != '\'') {
561 /*
562 * although u82cp() does handle escaped values, we deal
563 * with them directly since otherwise you can't process
564 * values which might be valid utf8 prefix
565 */
566 if (*pp == '\\')
567 val = esccon(&pp);
568 else
569 val = (unsigned)u82cp(&pp);
570
571 i++;
572 }
573
574 if (i == 0)
575 uerror("empty wide-character constant");
576 else if (i > 1)
577 werror("too many characters in wide-character constant");
578
579 yylval.li.val = val;
580 yylval.li.t = ctype(UNSIGNED);
581 }
582
583 #ifndef MYDOTFILE
584 /*
585 * Get basename and print it as '.file "basename.c"'
586 */
587 static void
588 printdotfile(char *file)
589 {
590 char *p;
591
592 if ((p = strrchr(file, '/')) == NULL)
593 p = file;
594 else
595 p++;
596 printf(PRTPREF "\t.file \"%s\"\n", p);
597 }
598 #endif
599
600 void
601 control(int t)
602 {
603 char *wr = yytext;
604 char *eptr;
605 int val;
606
607 wr++; /* Skip initial '#' */
608 switch (t) {
609 case CPP_IDENT:
610 return; /* Just skip these for now. */
611
612 case CPP_LINE:
613 wr += 4;
614 /* FALLTHROUGH */
615 case CPP_HASH:
616 val = (unsigned)strtol(wr, &eptr, 10);
617 if (wr == eptr) /* Illegal string */
618 goto bad;
619 wr = eptr;
620 lineno = val - 1;
621 while (*wr && *wr != '\"')
622 wr++;
623 if (*wr == 0)
624 return;
625 if (*wr++ != '\"')
626 goto bad;
627 eptr = wr;
628 while (*wr && *wr != '\"')
629 wr++;
630 if (*wr != '\"')
631 goto bad;
632 *wr++ = 0;
633 free(ftitle);
634 ftitle = xstrdup(eptr);
635 while (*wr == ' ')
636 wr++;
637 issyshdr = 0;
638 if (*wr == '3')
639 issyshdr = 1;
640 #ifdef DWARF
641 if (gflag)
642 dwarf_file(ftitle);
643 #endif
644 #ifdef STABS
645 if (gflag)
646 stabs_file(ftitle);
647 #endif
648 if (dotfile == 0) {
649 dotfile++;
650 printdotfile(ftitle);
651 }
652 }
653 return;
654 bad:
655 werror("%s: illegal control", yytext);
656 }
657
658 int pragma_allpacked;
659 int pragma_packed, pragma_aligned;
660
661 static int
662 pragmas_weak(char *str)
663 {
664 struct symtab *sp;
665 char *s1, *s2;
666
667 if ((s1 = pragtok(NULL)) == NULL)
668 return 1;
669 if ((s2 = pragtok(NULL)) == NULL) {
670 sp = lookup(addname(s1), SNORMAL);
671 #ifdef GCC_COMPAT
672 sp->sap = attr_add(sp->sap, gcc_attr_parse(bdty(NAME, "weak")));
673 #else
674 sp->sap = NULL;
675 #endif
676 } else if (*s2 == '=') {
677 if ((s2 = pragtok(NULL)) == NULL)
678 return 1;
679 sp = lookup(addname(s2), SNORMAL);
680 #ifdef GCC_COMPAT
681 sp->sap = attr_add(sp->sap, gcc_attr_parse(bdty(CALL,
682 bdty(NAME, "aliasweak"), bdty(STRING, s1, 0))));
683 #else
684 sp->sap = NULL;
685 #endif
686 } else
687 return 1;
688 return 0;
689 }
690
691 char *pragstore;
692
693 /* trivial tokenizer for pragmas */
694 #define ps pragstore
695 char *
696 pragtok(char *sin)
697 {
698 static char ss[2];
699 char *rv;
700
701 if (sin)
702 ps = sin;
703
704 for (; isspace((int)*ps); ps++)
705 ;
706 if (*ps == 0)
707 return NULL;
708 for (rv = ps; isalpha((int)*ps) || isdigit((int)*ps) || *ps == '_'; ps++)
709 ;
710 ss[0] = *ps;
711 if (rv == ps) {
712 rv = ss, ps++;
713 } else {
714 *ps = 0;
715 rv = tmpstrdup(rv);
716 *ps = ss[0];
717 }
718 return rv;
719 }
720
721 /* return 1 on error */
722 int
723 eat(int ch)
724 {
725 char *s = pragtok(0);
726 return (s == 0 || *s != ch);
727 }
728
729 static int
730 pragmas_alpack(char *t)
731 {
732 char *s;
733 int ap;
734
735 ap = (s = pragtok(0)) ? atoi(s) : 1;
736 if (strcmp(t, "packed") == 0)
737 pragma_packed = ap;
738 else
739 pragma_aligned = ap;
740 return 0;
741 }
742
743
744 /*
745 * Packing control.
746 */
747 static int
748 pragmas_pack(char *t)
749 {
750 #define PACKSTKSZ 10
751 static int packstk[PACKSTKSZ], packptr;
752 char *s;
753
754 if (eat('('))
755 return 1;
756 s = pragtok(0);
757 if (*s == ')')
758 return pragma_allpacked = 0;
759 if (strcmp(s, "push") == 0) {
760 if (packptr == PACKSTKSZ)
761 uerror("too many push");
762 packstk[packptr++] = pragma_allpacked;
763 s = pragtok(0);
764 if (*s == ')')
765 return 0;
766 if (*s != ',')
767 return 1;
768 s = pragtok(0);
769 } else if (strcmp(s, "pop") == 0) {
770 if (packptr == 0)
771 uerror("stack empty");
772 pragma_allpacked = packstk[--packptr];
773 return eat(')');
774 }
775
776 if (*s < '0' || *s > '9') /* no number */
777 return 1;
778 pragma_allpacked = atoi(s);
779 return eat(')');
780 }
781
782 static int
783 pragmas_unsupp(char *t)
784 {
785 werror("#pragma %s unsupported", t);
786 return 0; /* Just ignore */
787 }
788
789 static int
790 pragmas_stdc(char *t)
791 {
792 char *s = pragtok(0);
793
794 if (strcmp(s, "FP_CONTRACT") == 0) {
795 if (strcmp(s = pragtok(0), "ON") == 0) {
796 flostat |= FP_CONTR_CBR;
797 } else if (strcmp(s, "OFF") == 0) {
798 flostat &= ~FP_CONTR_CBR;
799 }
800 }
801 return 0; /* Just ignore */
802 }
803
804 struct pragmas {
805 char *name;
806 int (*fun)(char *);
807 } pragmas[] = {
808 { "pack", pragmas_pack },
809 { "packed", pragmas_alpack },
810 { "aligned", pragmas_alpack },
811 { "rename", pragmas_unsupp },
812 #ifdef GCC_COMPAT
813 { "GCC", pragmas_gcc },
814 #endif
815 { "STDC", pragmas_stdc },
816 { "weak", pragmas_weak },
817 { "ident", NULL },
818 { 0 },
819 };
820
821 /*
822 * got a full pragma line. Split it up here.
823 */
824 static void
825 pragma(void)
826 {
827 struct pragmas *p;
828 char *t, *pt;
829
830 if ((t = pragtok(&yytext[7])) != NULL) {
831 pt = ps;
832 for (p = pragmas; p->name; p++) {
833 if (strcmp(t, p->name) == 0) {
834 if (p->fun && (*p->fun)(t))
835 uerror("bad argument to #pragma");
836 return;
837 }
838 }
839 ps = pt;
840 if (mypragma(t))
841 return;
842 }
843 warner(Wunknown_pragmas, t, ps);
844 }
845
846 void
847 cunput(char c)
848 {
849 unput(c);
850 }