]> gcc.gnu.org Git - gcc.git/blame - gcc/c-lex.c
Entire file is new except for defining MD_STARTFILE_PREFIX.
[gcc.git] / gcc / c-lex.c
CommitLineData
d45cf215 1/* Lexical analyzer for C and Objective C.
e8bbfc4e
RK
2 Copyright (C) 1987, 1988, 1989, 1992 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21#include <stdio.h>
22#include <errno.h>
23#include <setjmp.h>
24
25#include "config.h"
26#include "rtl.h"
27#include "tree.h"
28#include "input.h"
29#include "c-lex.h"
30#include "c-tree.h"
31#include "flags.h"
32#include "c-parse.h"
33
34#ifdef MULTIBYTE_CHARS
35#include <stdlib.h>
36#include <locale.h>
37#endif
38
39#ifndef errno
40extern int errno;
41#endif
42
43/* The elements of `ridpointers' are identifier nodes
44 for the reserved type names and storage classes.
45 It is indexed by a RID_... value. */
46tree ridpointers[(int) RID_MAX];
47
48/* Cause the `yydebug' variable to be defined. */
49#define YYDEBUG 1
50
51/* the declaration found for the last IDENTIFIER token read in.
52 yylex must look this up to detect typedefs, which get token type TYPENAME,
53 so it is left around in case the identifier is not a typedef but is
54 used in a context which makes it a reference to a variable. */
55tree lastiddecl;
56
57/* Nonzero enables objc features. */
58
59int doing_objc_thang;
60
61extern tree lookup_interface ();
62
63extern int yydebug;
64
65/* File used for outputting assembler code. */
66extern FILE *asm_out_file;
67
68#ifndef WCHAR_TYPE_SIZE
69#ifdef INT_TYPE_SIZE
70#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
71#else
72#define WCHAR_TYPE_SIZE BITS_PER_WORD
73#endif
74#endif
75
76/* Number of bytes in a wide character. */
77#define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
78
79static int maxtoken; /* Current nominal length of token buffer. */
80char *token_buffer; /* Pointer to token buffer.
81 Actual allocated length is maxtoken + 2.
82 This is not static because objc-parse.y uses it. */
83
84/* Nonzero if end-of-file has been seen on input. */
85static int end_of_file;
86
87/* Buffered-back input character; faster than using ungetc. */
88static int nextchar = -1;
89
90int check_newline ();
91
92/* Nonzero tells yylex to ignore \ in string constants. */
93static int ignore_escape_flag = 0;
94\f
95/* C code produced by gperf version 2.5 (GNU C++ version) */
96/* Command-line: gperf -p -j1 -i 1 -g -o -t -N is_reserved_word -k1,3,$ c-parse.gperf */
97struct resword { char *name; short token; enum rid rid; };
98
99#define TOTAL_KEYWORDS 53
100#define MIN_WORD_LENGTH 2
101#define MAX_WORD_LENGTH 13
102#define MIN_HASH_VALUE 7
103#define MAX_HASH_VALUE 102
104/* maximum key range = 96, duplicates = 0 */
105
106#ifdef __GNUC__
107__inline
108#endif
109static unsigned int
110hash (str, len)
111 register char *str;
112 register int unsigned len;
113{
114 static unsigned char asso_values[] =
115 {
116 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
117 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
118 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
119 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
120 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
121 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
122 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
123 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
124 103, 103, 103, 103, 103, 103, 103, 103, 103, 103,
125 103, 103, 103, 103, 103, 1, 103, 2, 1, 24,
126 1, 5, 19, 39, 16, 13, 103, 1, 25, 1,
127 34, 34, 24, 103, 13, 12, 1, 45, 24, 7,
128 103, 103, 2, 103, 103, 103, 103, 103,
129 };
130 register int hval = len;
131
132 switch (hval)
133 {
134 default:
135 case 3:
136 hval += asso_values[str[2]];
137 case 2:
138 case 1:
139 hval += asso_values[str[0]];
140 }
141 return hval + asso_values[str[len - 1]];
142}
143
144#ifdef __GNUC__
145__inline
146#endif
147struct resword *
148is_reserved_word (str, len)
149 register char *str;
150 register unsigned int len;
151{
152 static struct resword wordlist[] =
153 {
154 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
d45cf215 155 {"asm", ASM_KEYWORD, NORID},
e8bbfc4e 156 {"",},
d45cf215 157 {"__asm", ASM_KEYWORD, NORID},
e8bbfc4e 158 {"",},
d45cf215 159 {"__asm__", ASM_KEYWORD, NORID},
e8bbfc4e
RK
160 {"break", BREAK, NORID},
161 {"__typeof__", TYPEOF, NORID},
162 {"",},
163 {"__alignof__", ALIGNOF, NORID},
164 {"",},
165 {"__attribute__", ATTRIBUTE, NORID},
166 {"int", TYPESPEC, RID_INT},
167 {"__attribute", ATTRIBUTE, NORID},
168 {"__extension__", EXTENSION, NORID},
169 {"",},
170 {"__signed", TYPESPEC, RID_SIGNED},
171 {"",},
172 {"__signed__", TYPESPEC, RID_SIGNED},
173 {"__inline__", SCSPEC, RID_INLINE},
174 {"else", ELSE, NORID},
175 {"__inline", SCSPEC, RID_INLINE},
176 {"default", DEFAULT, NORID},
177 {"__typeof", TYPEOF, NORID},
178 {"while", WHILE, NORID},
179 {"__alignof", ALIGNOF, NORID},
180 {"struct", STRUCT, NORID},
181 {"__const", TYPE_QUAL, RID_CONST},
182 {"if", IF, NORID},
183 {"__const__", TYPE_QUAL, RID_CONST},
184 {"__label__", LABEL, NORID},
185 {"do", DO, NORID},
186 {"__volatile__", TYPE_QUAL, RID_VOLATILE},
187 {"sizeof", SIZEOF, NORID},
188 {"__volatile", TYPE_QUAL, RID_VOLATILE},
189 {"auto", SCSPEC, RID_AUTO},
190 {"void", TYPESPEC, RID_VOID},
191 {"char", TYPESPEC, RID_CHAR},
192 {"static", SCSPEC, RID_STATIC},
193 {"case", CASE, NORID},
194 {"extern", SCSPEC, RID_EXTERN},
195 {"switch", SWITCH, NORID},
196 {"for", FOR, NORID},
197 {"inline", SCSPEC, RID_INLINE},
198 {"typeof", TYPEOF, NORID},
199 {"typedef", SCSPEC, RID_TYPEDEF},
200 {"short", TYPESPEC, RID_SHORT},
201 {"",},
202 {"return", RETURN, NORID},
203 {"enum", ENUM, NORID},
204 {"",},
205 {"double", TYPESPEC, RID_DOUBLE},
206 {"signed", TYPESPEC, RID_SIGNED},
207 {"float", TYPESPEC, RID_FLOAT},
208 {"",}, {"",},
209 {"volatile", TYPE_QUAL, RID_VOLATILE},
210 {"",},
211 {"const", TYPE_QUAL, RID_CONST},
212 {"",},
213 {"unsigned", TYPESPEC, RID_UNSIGNED},
214 {"",}, {"",}, {"",}, {"",},
215 {"continue", CONTINUE, NORID},
216 {"",},
217 {"register", SCSPEC, RID_REGISTER},
218 {"",}, {"",}, {"",}, {"",},
219 {"goto", GOTO, NORID},
220 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
221 {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",}, {"",},
222
223 {"union", UNION, NORID},
224 {"",}, {"",}, {"",}, {"",},
225 {"long", TYPESPEC, RID_LONG},
226 };
227
228 if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
229 {
230 register int key = hash (str, len);
231
232 if (key <= MAX_HASH_VALUE && key >= 0)
233 {
234 register char *s = wordlist[key].name;
235
236 if (*s == *str && !strcmp (str + 1, s + 1))
237 return &wordlist[key];
238 }
239 }
240 return 0;
241}
242\f
243/* Return something to represent absolute declarators containing a *.
244 TARGET is the absolute declarator that the * contains.
245 TYPE_QUALS is a list of modifiers such as const or volatile
246 to apply to the pointer type, represented as identifiers.
247
248 We return an INDIRECT_REF whose "contents" are TARGET
249 and whose type is the modifier list. */
250
251tree
252make_pointer_declarator (type_quals, target)
253 tree type_quals, target;
254{
255 return build1 (INDIRECT_REF, type_quals, target);
256}
257\f
258void
259init_lex ()
260{
261 /* Make identifier nodes long enough for the language-specific slots. */
262 set_identifier_size (sizeof (struct lang_identifier));
263
264 /* Start it at 0, because check_newline is called at the very beginning
265 and will increment it to 1. */
266 lineno = 0;
267
268#ifdef MULTIBYTE_CHARS
269 /* Change to the native locale for multibyte conversions. */
270 setlocale (LC_CTYPE, "");
271#endif
272
273 maxtoken = 40;
274 token_buffer = (char *) xmalloc (maxtoken + 2);
275
276 ridpointers[(int) RID_INT] = get_identifier ("int");
277 ridpointers[(int) RID_CHAR] = get_identifier ("char");
278 ridpointers[(int) RID_VOID] = get_identifier ("void");
279 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
280 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
281 ridpointers[(int) RID_SHORT] = get_identifier ("short");
282 ridpointers[(int) RID_LONG] = get_identifier ("long");
283 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
284 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
285 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
286 ridpointers[(int) RID_CONST] = get_identifier ("const");
287 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
288 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
289 ridpointers[(int) RID_STATIC] = get_identifier ("static");
290 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
291 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
292 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
293
294 /* Some options inhibit certain reserved words.
295 Clear those words out of the hash table so they won't be recognized. */
296#define UNSET_RESERVED_WORD(STRING) \
297 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
298 if (s) s->name = ""; } while (0)
299
300 if (flag_traditional)
301 {
302 UNSET_RESERVED_WORD ("const");
303 UNSET_RESERVED_WORD ("volatile");
304 UNSET_RESERVED_WORD ("typeof");
305 UNSET_RESERVED_WORD ("signed");
306 UNSET_RESERVED_WORD ("inline");
307 }
308 if (flag_no_asm)
309 {
310 UNSET_RESERVED_WORD ("asm");
311 UNSET_RESERVED_WORD ("typeof");
312 UNSET_RESERVED_WORD ("inline");
313 }
314}
315
316void
317reinit_parse_for_function ()
318{
319}
320\f
321/* Function used when yydebug is set, to print a token in more detail. */
322
323void
324yyprint (file, yychar, yylval)
325 FILE *file;
326 int yychar;
327 YYSTYPE yylval;
328{
329 tree t;
330 switch (yychar)
331 {
332 case IDENTIFIER:
333 case TYPENAME:
334 t = yylval.ttype;
335 if (IDENTIFIER_POINTER (t))
336 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
337 break;
338
339 case CONSTANT:
340 t = yylval.ttype;
341 if (TREE_CODE (t) == INTEGER_CST)
8d9bfdc5
RK
342 fprintf (file,
343#if HOST_BITS_PER_WIDE_INT == 64
344#if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
345 " 0x%lx%016lx",
346#else
347 " 0x%x%016x",
348#endif
349#else
350#if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
351 " 0x%lx%08lx",
352#else
353 " 0x%x%08x",
354#endif
355#endif
356 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
e8bbfc4e
RK
357 break;
358 }
359}
360
361\f
362/* If C is not whitespace, return C.
363 Otherwise skip whitespace and return first nonwhite char read. */
364
365static int
366skip_white_space (c)
367 register int c;
368{
0dcd8cee 369 static int newline_warning = 0;
e8bbfc4e
RK
370
371 for (;;)
372 {
373 switch (c)
374 {
0dcd8cee
RS
375 /* We don't recognize comments here, because
376 cpp output can include / and * consecutively as operators.
377 Also, there's no need, since cpp removes all comments. */
e8bbfc4e
RK
378
379 case '\n':
380 c = check_newline ();
381 break;
382
383 case ' ':
384 case '\t':
385 case '\f':
e8bbfc4e
RK
386 case '\v':
387 case '\b':
388 c = getc (finput);
389 break;
390
0dcd8cee
RS
391 case '\r':
392 /* ANSI C says the effects of a carriage return in a source file
393 are undefined. */
394 if (pedantic && !newline_warning)
395 {
396 warning ("carriage return in source file");
397 warning ("(we only warn about the first carriage return)");
398 newline_warning = 1;
399 }
400 c = getc (finput);
401 break;
402
e8bbfc4e
RK
403 case '\\':
404 c = getc (finput);
405 if (c == '\n')
406 lineno++;
407 else
408 error ("stray '\\' in program");
409 c = getc (finput);
410 break;
411
412 default:
413 return (c);
414 }
415 }
416}
417
418/* Skips all of the white space at the current location in the input file.
419 Must use and reset nextchar if it has the next character. */
420
421void
422position_after_white_space ()
423{
424 register int c;
425
426 if (nextchar != -1)
427 c = nextchar, nextchar = -1;
428 else
429 c = getc (finput);
430
431 ungetc (skip_white_space (c), finput);
432}
433
434/* Make the token buffer longer, preserving the data in it.
435 P should point to just beyond the last valid character in the old buffer.
436 The value we return is a pointer to the new buffer
437 at a place corresponding to P. */
438
439static char *
440extend_token_buffer (p)
441 char *p;
442{
443 int offset = p - token_buffer;
444
445 maxtoken = maxtoken * 2 + 10;
446 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
447
448 return token_buffer + offset;
449}
450\f
451/* At the beginning of a line, increment the line number
452 and process any #-directive on this line.
453 If the line is a #-directive, read the entire line and return a newline.
454 Otherwise, return the line's first non-whitespace character. */
455
456int
457check_newline ()
458{
459 register int c;
460 register int token;
461
462 lineno++;
463
464 /* Read first nonwhite char on the line. */
465
466 c = getc (finput);
467 while (c == ' ' || c == '\t')
468 c = getc (finput);
469
470 if (c != '#')
471 {
472 /* If not #, return it so caller will use it. */
473 return c;
474 }
475
476 /* Read first nonwhite char after the `#'. */
477
478 c = getc (finput);
479 while (c == ' ' || c == '\t')
480 c = getc (finput);
481
482 /* If a letter follows, then if the word here is `line', skip
483 it and ignore it; otherwise, ignore the line, with an error
484 if the word isn't `pragma', `ident', `define', or `undef'. */
485
486 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
487 {
488 if (c == 'p')
489 {
490 if (getc (finput) == 'r'
491 && getc (finput) == 'a'
492 && getc (finput) == 'g'
493 && getc (finput) == 'm'
494 && getc (finput) == 'a'
495 && ((c = getc (finput)) == ' ' || c == '\t' || c == '\n'))
496 {
ca5b800a 497#ifdef HANDLE_SYSV_PRAGMA
a3100298 498 return handle_sysv_pragma (finput, c);
ca5b800a 499#endif /* HANDLE_SYSV_PRAGMA */
e8bbfc4e
RK
500#ifdef HANDLE_PRAGMA
501 HANDLE_PRAGMA (finput);
502#endif /* HANDLE_PRAGMA */
503 goto skipline;
504 }
505 }
506
507 else if (c == 'd')
508 {
509 if (getc (finput) == 'e'
510 && getc (finput) == 'f'
511 && getc (finput) == 'i'
512 && getc (finput) == 'n'
513 && getc (finput) == 'e'
514 && ((c = getc (finput)) == ' ' || c == '\t' || c == '\n'))
515 {
516#ifdef DWARF_DEBUGGING_INFO
517 if ((debug_info_level == DINFO_LEVEL_VERBOSE)
518 && (write_symbols == DWARF_DEBUG))
519 dwarfout_define (lineno, get_directive_line (finput));
520#endif /* DWARF_DEBUGGING_INFO */
521 goto skipline;
522 }
523 }
524 else if (c == 'u')
525 {
526 if (getc (finput) == 'n'
527 && getc (finput) == 'd'
528 && getc (finput) == 'e'
529 && getc (finput) == 'f'
530 && ((c = getc (finput)) == ' ' || c == '\t' || c == '\n'))
531 {
532#ifdef DWARF_DEBUGGING_INFO
533 if ((debug_info_level == DINFO_LEVEL_VERBOSE)
534 && (write_symbols == DWARF_DEBUG))
535 dwarfout_undef (lineno, get_directive_line (finput));
536#endif /* DWARF_DEBUGGING_INFO */
537 goto skipline;
538 }
539 }
540 else if (c == 'l')
541 {
542 if (getc (finput) == 'i'
543 && getc (finput) == 'n'
544 && getc (finput) == 'e'
545 && ((c = getc (finput)) == ' ' || c == '\t'))
546 goto linenum;
547 }
548 else if (c == 'i')
549 {
550 if (getc (finput) == 'd'
551 && getc (finput) == 'e'
552 && getc (finput) == 'n'
553 && getc (finput) == 't'
554 && ((c = getc (finput)) == ' ' || c == '\t'))
555 {
556 /* #ident. The pedantic warning is now in cccp.c. */
557
558 /* Here we have just seen `#ident '.
559 A string constant should follow. */
560
561 while (c == ' ' || c == '\t')
562 c = getc (finput);
563
564 /* If no argument, ignore the line. */
565 if (c == '\n')
566 return c;
567
568 ungetc (c, finput);
569 token = yylex ();
570 if (token != STRING
571 || TREE_CODE (yylval.ttype) != STRING_CST)
572 {
573 error ("invalid #ident");
574 goto skipline;
575 }
576
577 if (!flag_no_ident)
578 {
579#ifdef ASM_OUTPUT_IDENT
580 ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (yylval.ttype));
581#endif
582 }
583
584 /* Skip the rest of this line. */
585 goto skipline;
586 }
587 }
588
589 error ("undefined or invalid # directive");
590 goto skipline;
591 }
592
593linenum:
594 /* Here we have either `#line' or `# <nonletter>'.
595 In either case, it should be a line number; a digit should follow. */
596
597 while (c == ' ' || c == '\t')
598 c = getc (finput);
599
600 /* If the # is the only nonwhite char on the line,
601 just ignore it. Check the new newline. */
602 if (c == '\n')
603 return c;
604
605 /* Something follows the #; read a token. */
606
607 ungetc (c, finput);
608 token = yylex ();
609
610 if (token == CONSTANT
611 && TREE_CODE (yylval.ttype) == INTEGER_CST)
612 {
613 int old_lineno = lineno;
614 int used_up = 0;
615 /* subtract one, because it is the following line that
616 gets the specified number */
617
618 int l = TREE_INT_CST_LOW (yylval.ttype) - 1;
619
620 /* Is this the last nonwhite stuff on the line? */
621 c = getc (finput);
622 while (c == ' ' || c == '\t')
623 c = getc (finput);
624 if (c == '\n')
625 {
626 /* No more: store the line number and check following line. */
627 lineno = l;
628 return c;
629 }
630 ungetc (c, finput);
631
632 /* More follows: it must be a string constant (filename). */
633
634 /* Read the string constant, but don't treat \ as special. */
635 ignore_escape_flag = 1;
636 token = yylex ();
637 ignore_escape_flag = 0;
638
639 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
640 {
641 error ("invalid #line");
642 goto skipline;
643 }
644
645 input_filename
646 = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
647 strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
648 lineno = l;
649
650 /* Each change of file name
651 reinitializes whether we are now in a system header. */
652 in_system_header = 0;
653
654 if (main_input_filename == 0)
655 main_input_filename = input_filename;
656
657 /* Is this the last nonwhite stuff on the line? */
658 c = getc (finput);
659 while (c == ' ' || c == '\t')
660 c = getc (finput);
661 if (c == '\n')
662 return c;
663 ungetc (c, finput);
664
665 token = yylex ();
666 used_up = 0;
667
668 /* `1' after file name means entering new file.
669 `2' after file name means just left a file. */
670
671 if (token == CONSTANT
672 && TREE_CODE (yylval.ttype) == INTEGER_CST)
673 {
674 if (TREE_INT_CST_LOW (yylval.ttype) == 1)
675 {
676 /* Pushing to a new file. */
677 struct file_stack *p
678 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
679 input_file_stack->line = old_lineno;
680 p->next = input_file_stack;
681 p->name = input_filename;
682 input_file_stack = p;
683 input_file_stack_tick++;
684#ifdef DWARF_DEBUGGING_INFO
685 if (debug_info_level == DINFO_LEVEL_VERBOSE
686 && write_symbols == DWARF_DEBUG)
687 dwarfout_start_new_source_file (input_filename);
688#endif /* DWARF_DEBUGGING_INFO */
689
690 used_up = 1;
691 }
692 else if (TREE_INT_CST_LOW (yylval.ttype) == 2)
693 {
694 /* Popping out of a file. */
695 if (input_file_stack->next)
696 {
697 struct file_stack *p = input_file_stack;
698 input_file_stack = p->next;
699 free (p);
700 input_file_stack_tick++;
701#ifdef DWARF_DEBUGGING_INFO
702 if (debug_info_level == DINFO_LEVEL_VERBOSE
703 && write_symbols == DWARF_DEBUG)
704 dwarfout_resume_previous_source_file (input_file_stack->line);
705#endif /* DWARF_DEBUGGING_INFO */
706 }
707 else
708 error ("#-lines for entering and leaving files don't match");
709
710 used_up = 1;
711 }
712 }
713
714 /* If we have handled a `1' or a `2',
715 see if there is another number to read. */
716 if (used_up)
717 {
718 /* Is this the last nonwhite stuff on the line? */
719 c = getc (finput);
720 while (c == ' ' || c == '\t')
721 c = getc (finput);
722 if (c == '\n')
723 return c;
724 ungetc (c, finput);
725
726 token = yylex ();
727 used_up = 0;
728 }
729
730 /* `3' after file name means this is a system header file. */
731
732 if (token == CONSTANT
733 && TREE_CODE (yylval.ttype) == INTEGER_CST
734 && TREE_INT_CST_LOW (yylval.ttype) == 3)
735 in_system_header = 1;
736 }
737 else
738 error ("invalid #-line");
739
740 /* skip the rest of this line. */
741 skipline:
742 if (c == '\n')
743 return c;
744 while ((c = getc (finput)) != EOF && c != '\n');
745 return c;
746}
747\f
ca5b800a
RS
748#ifdef HANDLE_SYSV_PRAGMA
749
750/* Handle a #pragma directive. INPUT is the current input stream,
a3100298
TW
751 and C is a character to reread. Processes the entire input line
752 and returns a character for the caller to reread: either \n or EOF. */
ca5b800a
RS
753
754/* This function has to be in this file, in order to get at
755 the token types. */
756
757int
758handle_sysv_pragma (input, c)
759 FILE *input;
760 int c;
761{
a3100298 762 for (;;)
ca5b800a 763 {
a3100298
TW
764 while (c == ' ' || c == '\t')
765 c = getc (input);
766 if (c == '\n' || c == EOF)
767 {
768 handle_pragma_token (0, 0);
769 return c;
770 }
771 ungetc (c, input);
772 switch (yylex ())
773 {
774 case IDENTIFIER:
775 case TYPENAME:
776 case STRING:
777 case CONSTANT:
778 handle_pragma_token (token_buffer, yylval.ttype);
779 break;
780 default:
781 handle_pragma_token (token_buffer, 0);
782 }
783 if (nextchar >= 0)
784 c = nextchar, nextchar = -1;
785 else
786 c = getc (input);
ca5b800a 787 }
ca5b800a
RS
788}
789
790#endif /* HANDLE_SYSV_PRAGMA */
791\f
e8bbfc4e
RK
792#define isalnum(char) ((char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9'))
793#define isdigit(char) (char >= '0' && char <= '9')
794#define ENDFILE -1 /* token that represents end-of-file */
795
796/* Read an escape sequence, returning its equivalent as a character,
797 or -1 if it is backslash-newline. */
798
799static int
800readescape ()
801{
802 register int c = getc (finput);
803 register int code;
804 register unsigned count;
805 unsigned firstdig;
806
807 switch (c)
808 {
809 case 'x':
810 if (warn_traditional)
811 warning ("the meaning of `\\x' varies with -traditional");
812
813 if (flag_traditional)
814 return c;
815
816 code = 0;
817 count = 0;
818 while (1)
819 {
820 c = getc (finput);
821 if (!(c >= 'a' && c <= 'f')
822 && !(c >= 'A' && c <= 'F')
823 && !(c >= '0' && c <= '9'))
824 {
825 ungetc (c, finput);
826 break;
827 }
828 code *= 16;
829 if (c >= 'a' && c <= 'f')
830 code += c - 'a' + 10;
831 if (c >= 'A' && c <= 'F')
832 code += c - 'A' + 10;
833 if (c >= '0' && c <= '9')
834 code += c - '0';
835 if (count == 0)
836 firstdig = code;
837 count++;
838 }
839 if (count == 0)
840 error ("\\x used with no following hex digits");
841 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
842 || (count > 1
843 && ((1 << (TYPE_PRECISION (integer_type_node) - (count - 1) * 4))
844 <= firstdig)))
845 pedwarn ("hex escape out of range");
846 return code;
847
848 case '0': case '1': case '2': case '3': case '4':
849 case '5': case '6': case '7':
850 code = 0;
851 count = 0;
852 while ((c <= '7') && (c >= '0') && (count++ < 3))
853 {
854 code = (code * 8) + (c - '0');
855 c = getc (finput);
856 }
857 ungetc (c, finput);
858 return code;
859
860 case '\\': case '\'': case '"':
861 return c;
862
863 case '\n':
864 lineno++;
865 return -1;
866
867 case 'n':
868 return TARGET_NEWLINE;
869
870 case 't':
871 return TARGET_TAB;
872
873 case 'r':
874 return TARGET_CR;
875
876 case 'f':
877 return TARGET_FF;
878
879 case 'b':
880 return TARGET_BS;
881
882 case 'a':
883 if (warn_traditional)
884 warning ("the meaning of `\\a' varies with -traditional");
885
886 if (flag_traditional)
887 return c;
888 return TARGET_BELL;
889
890 case 'v':
891#if 0 /* Vertical tab is present in common usage compilers. */
892 if (flag_traditional)
893 return c;
894#endif
895 return TARGET_VT;
896
897 case 'E':
898 pedwarn ("non-ANSI-standard escape sequence, `\\E'");
899 return 033;
900
901 case '?':
902 return c;
903
904 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
905 case '(':
906 case '{':
907 case '[':
908 if (pedantic)
909 pedwarn ("non-ANSI escape sequence `\\%c'", c);
910 return c;
911 }
912 if (c >= 040 && c <= 0177)
913 pedwarn ("unknown escape sequence `\\%c'", c);
914 else
915 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
916 return c;
917}
918\f
919void
920yyerror (string)
921 char *string;
922{
923 char buf[200];
924
925 strcpy (buf, string);
926
927 /* We can't print string and character constants well
928 because the token_buffer contains the result of processing escapes. */
929 if (end_of_file)
930 strcat (buf, " at end of input");
931 else if (token_buffer[0] == 0)
932 strcat (buf, " at null character");
933 else if (token_buffer[0] == '"')
934 strcat (buf, " before string constant");
935 else if (token_buffer[0] == '\'')
936 strcat (buf, " before character constant");
937 else if (token_buffer[0] < 040 || (unsigned char) token_buffer[0] >= 0177)
938 sprintf (buf + strlen (buf), " before character 0%o",
939 (unsigned char) token_buffer[0]);
940 else
941 strcat (buf, " before `%s'");
942
943 error (buf, token_buffer);
944}
945
946#if 0
947
948struct try_type
949{
950 tree *node_var;
951 char unsigned_flag;
952 char long_flag;
953 char long_long_flag;
954};
955
956struct try_type type_sequence[] =
957{
958 { &integer_type_node, 0, 0, 0},
959 { &unsigned_type_node, 1, 0, 0},
960 { &long_integer_type_node, 0, 1, 0},
961 { &long_unsigned_type_node, 1, 1, 0},
962 { &long_long_integer_type_node, 0, 1, 1},
963 { &long_long_unsigned_type_node, 1, 1, 1}
964};
965#endif /* 0 */
966\f
967int
968yylex ()
969{
970 register int c;
971 register char *p;
972 register int value;
973 int wide_flag = 0;
974
975 if (nextchar >= 0)
976 c = nextchar, nextchar = -1;
977 else
978 c = getc (finput);
979
980 /* Effectively do c = skip_white_space (c)
981 but do it faster in the usual cases. */
982 while (1)
983 switch (c)
984 {
e8bbfc4e
RK
985 case ' ':
986 case '\t':
987 case '\f':
988 case '\v':
989 case '\b':
990 c = getc (finput);
991 break;
992
80f5b9d8
RS
993 case '\r':
994 /* Call skip_white_space so we can warn if appropriate. */
995
e8bbfc4e
RK
996 case '\n':
997 case '/':
998 case '\\':
999 c = skip_white_space (c);
1000 default:
1001 goto found_nonwhite;
1002 }
1003 found_nonwhite:
1004
1005 token_buffer[0] = c;
1006 token_buffer[1] = 0;
1007
1008/* yylloc.first_line = lineno; */
1009
1010 switch (c)
1011 {
1012 case EOF:
1013 end_of_file = 1;
1014 token_buffer[0] = 0;
1015 value = ENDFILE;
1016 break;
1017
1018 case '$':
1019 if (dollars_in_ident)
1020 goto letter;
1021 return '$';
1022
1023 case 'L':
1024 /* Capital L may start a wide-string or wide-character constant. */
1025 {
1026 register int c = getc (finput);
1027 if (c == '\'')
1028 {
1029 wide_flag = 1;
1030 goto char_constant;
1031 }
1032 if (c == '"')
1033 {
1034 wide_flag = 1;
1035 goto string_constant;
1036 }
1037 ungetc (c, finput);
1038 }
1039 goto letter;
1040
1041 case '@':
1042 if (!doing_objc_thang)
1043 {
1044 value = c;
1045 break;
1046 }
1047 p = token_buffer;
1048 *p++ = '@';
1049 c = getc (finput);
1050 while (isalnum (c) || c == '_')
1051 {
1052 if (p >= token_buffer + maxtoken)
1053 p = extend_token_buffer (p);
1054
1055 *p++ = c;
1056 c = getc (finput);
1057 }
1058
1059 *p = 0;
1060 nextchar = c;
1061 value = recognize_objc_keyword (token_buffer + 1);
1062 if (value != 0)
1063 break;
1064 error ("invalid Objective C keyword `%s'", token_buffer);
1065 /* Cause a syntax error--1 is not a valid token type. */
1066 value = 1;
1067 break;
1068
1069 case 'A': case 'B': case 'C': case 'D': case 'E':
1070 case 'F': case 'G': case 'H': case 'I': case 'J':
1071 case 'K': case 'M': case 'N': case 'O':
1072 case 'P': case 'Q': case 'R': case 'S': case 'T':
1073 case 'U': case 'V': case 'W': case 'X': case 'Y':
1074 case 'Z':
1075 case 'a': case 'b': case 'c': case 'd': case 'e':
1076 case 'f': case 'g': case 'h': case 'i': case 'j':
1077 case 'k': case 'l': case 'm': case 'n': case 'o':
1078 case 'p': case 'q': case 'r': case 's': case 't':
1079 case 'u': case 'v': case 'w': case 'x': case 'y':
1080 case 'z':
1081 case '_':
1082 letter:
1083 p = token_buffer;
1084 while (isalnum (c) || c == '_' || c == '$' || c == '@')
1085 {
1086 if (p >= token_buffer + maxtoken)
1087 p = extend_token_buffer (p);
1088 if (c == '$' && ! dollars_in_ident)
1089 break;
1090
1091 *p++ = c;
1092 c = getc (finput);
1093 }
1094
1095 *p = 0;
1096 nextchar = c;
1097
1098 value = IDENTIFIER;
1099 yylval.itype = 0;
1100
1101 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1102
1103 {
1104 register struct resword *ptr;
1105
1106 if (ptr = is_reserved_word (token_buffer, p - token_buffer))
1107 {
1108 if (ptr->rid)
1109 yylval.ttype = ridpointers[(int) ptr->rid];
1110 value = (int) ptr->token;
1111
1112 /* Even if we decided to recognize asm, still perhaps warn. */
1113 if (pedantic
d45cf215 1114 && (value == ASM_KEYWORD || value == TYPEOF
e8bbfc4e
RK
1115 || ptr->rid == RID_INLINE)
1116 && token_buffer[0] != '_')
1117 pedwarn ("ANSI does not permit the keyword `%s'",
1118 token_buffer);
1119 }
1120 }
1121
1122 /* If we did not find a keyword, look for an identifier
1123 (or a typename). */
1124
1125 if (value == IDENTIFIER)
1126 {
1127 yylval.ttype = get_identifier (token_buffer);
1128 lastiddecl = lookup_name (yylval.ttype);
1129
1130 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1131 value = TYPENAME;
6dbf678a
RS
1132 /* A user-invisible read-only initialized variable
1133 should be replaced by its value.
1134 We handle only strings since that's the only case used in C. */
1135 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1136 && DECL_IGNORED_P (lastiddecl)
1137 && TREE_READONLY (lastiddecl)
1138 && DECL_INITIAL (lastiddecl) != 0
1139 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1140 {
1141 yylval.ttype = DECL_INITIAL (lastiddecl);
1142 value = STRING;
1143 }
e8bbfc4e
RK
1144 else if (doing_objc_thang)
1145 {
1146 tree objc_interface_decl = lookup_interface (yylval.ttype);
1147
1148 if (objc_interface_decl)
1149 {
1150 value = CLASSNAME;
1151 yylval.ttype = objc_interface_decl;
1152 }
1153 }
1154 }
1155
1156 break;
1157
1158 case '0': case '1': case '2': case '3': case '4':
1159 case '5': case '6': case '7': case '8': case '9':
1160 case '.':
1161 {
1162 int base = 10;
1163 int count = 0;
1164 int largest_digit = 0;
1165 int numdigits = 0;
1166 /* for multi-precision arithmetic,
4ca827d0
RS
1167 we actually store only HOST_BITS_PER_CHAR bits in each part.
1168 The number of parts is chosen so as to be sufficient to hold
9b0995aa
RK
1169 the enough bits to fit into the two HOST_WIDE_INTs that contain
1170 the integer value (this is always at least as many bits as are
1171 in a target `long long' value, but may be wider). */
1172#define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2 + 2)
4ca827d0 1173 int parts[TOTAL_PARTS];
e8bbfc4e
RK
1174 int overflow = 0;
1175
1176 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS} floatflag
1177 = NOT_FLOAT;
1178
4ca827d0
RS
1179 for (count = 0; count < TOTAL_PARTS; count++)
1180 parts[count] = 0;
e8bbfc4e
RK
1181
1182 p = token_buffer;
1183 *p++ = c;
1184
1185 if (c == '0')
1186 {
1187 *p++ = (c = getc (finput));
1188 if ((c == 'x') || (c == 'X'))
1189 {
1190 base = 16;
1191 *p++ = (c = getc (finput));
1192 }
1193 /* Leading 0 forces octal unless the 0 is the only digit. */
1194 else if (c >= '0' && c <= '9')
1195 {
1196 base = 8;
1197 numdigits++;
1198 }
1199 else
1200 numdigits++;
1201 }
1202
1203 /* Read all the digits-and-decimal-points. */
1204
1205 while (c == '.'
1206 || (isalnum (c) && (c != 'l') && (c != 'L')
1207 && (c != 'u') && (c != 'U')
1208 && (floatflag == NOT_FLOAT || ((c != 'f') && (c != 'F')))))
1209 {
1210 if (c == '.')
1211 {
1212 if (base == 16)
1213 error ("floating constant may not be in radix 16");
1214 if (floatflag == AFTER_POINT)
1215 {
1216 error ("malformed floating constant");
1217 floatflag = TOO_MANY_POINTS;
1218 }
1219 else
1220 floatflag = AFTER_POINT;
1221
1222 base = 10;
1223 *p++ = c = getc (finput);
1224 /* Accept '.' as the start of a floating-point number
1225 only when it is followed by a digit.
1226 Otherwise, unread the following non-digit
1227 and use the '.' as a structural token. */
1228 if (p == token_buffer + 2 && !isdigit (c))
1229 {
1230 if (c == '.')
1231 {
1232 c = getc (finput);
1233 if (c == '.')
1234 {
1235 *p++ = c;
1236 *p = 0;
1237 return ELLIPSIS;
1238 }
1239 error ("parse error at `..'");
1240 }
1241 ungetc (c, finput);
1242 token_buffer[1] = 0;
1243 value = '.';
1244 goto done;
1245 }
1246 }
1247 else
1248 {
1249 /* It is not a decimal point.
1250 It should be a digit (perhaps a hex digit). */
1251
1252 if (isdigit (c))
1253 {
1254 c = c - '0';
1255 }
1256 else if (base <= 10)
1257 {
1258 if ((c&~040) == 'E')
1259 {
1260 base = 10;
1261 floatflag = AFTER_POINT;
1262 break; /* start of exponent */
1263 }
1264 error ("nondigits in number and not hexadecimal");
1265 c = 0;
1266 }
1267 else if (c >= 'a')
1268 {
1269 c = c - 'a' + 10;
1270 }
1271 else
1272 {
1273 c = c - 'A' + 10;
1274 }
1275 if (c >= largest_digit)
1276 largest_digit = c;
1277 numdigits++;
1278
4ca827d0 1279 for (count = 0; count < TOTAL_PARTS; count++)
e8bbfc4e 1280 {
4ca827d0 1281 parts[count] *= base;
e8bbfc4e
RK
1282 if (count)
1283 {
4ca827d0
RS
1284 parts[count]
1285 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1286 parts[count-1]
1287 &= (1 << HOST_BITS_PER_CHAR) - 1;
e8bbfc4e 1288 }
4ca827d0
RS
1289 else
1290 parts[0] += c;
e8bbfc4e
RK
1291 }
1292
4ca827d0
RS
1293 /* If the extra highest-order part ever gets anything in it,
1294 the number is certainly too big. */
1295 if (parts[TOTAL_PARTS - 1] != 0)
1296 overflow = 1;
e8bbfc4e
RK
1297
1298 if (p >= token_buffer + maxtoken - 3)
1299 p = extend_token_buffer (p);
1300 *p++ = (c = getc (finput));
1301 }
1302 }
1303
1304 if (numdigits == 0)
1305 error ("numeric constant with no digits");
1306
1307 if (largest_digit >= base)
1308 error ("numeric constant contains digits beyond the radix");
1309
1310 /* Remove terminating char from the token buffer and delimit the string */
1311 *--p = 0;
1312
1313 if (floatflag != NOT_FLOAT)
1314 {
1315 tree type = double_type_node;
1316 char f_seen = 0;
1317 char l_seen = 0;
1318 REAL_VALUE_TYPE value;
1319 jmp_buf handler;
1320
1321 /* Read explicit exponent if any, and put it in tokenbuf. */
1322
1323 if ((c == 'e') || (c == 'E'))
1324 {
1325 if (p >= token_buffer + maxtoken - 3)
1326 p = extend_token_buffer (p);
1327 *p++ = c;
1328 c = getc (finput);
1329 if ((c == '+') || (c == '-'))
1330 {
1331 *p++ = c;
1332 c = getc (finput);
1333 }
1334 if (! isdigit (c))
1335 error ("floating constant exponent has no digits");
1336 while (isdigit (c))
1337 {
1338 if (p >= token_buffer + maxtoken - 3)
1339 p = extend_token_buffer (p);
1340 *p++ = c;
1341 c = getc (finput);
1342 }
1343 }
1344
1345 *p = 0;
1346 errno = 0;
1347
1348 /* Convert string to a double, checking for overflow. */
1349 if (setjmp (handler))
1350 {
1351 error ("floating constant out of range");
1352 value = dconst0;
1353 }
1354 else
1355 {
1356 set_float_handler (handler);
1357 value = REAL_VALUE_ATOF (token_buffer);
8d9bfdc5 1358 set_float_handler (NULL_PTR);
e8bbfc4e
RK
1359 }
1360#ifdef ERANGE
e53c841d 1361 if (errno == ERANGE && !flag_traditional && pedantic)
e8bbfc4e
RK
1362 {
1363 char *p1 = token_buffer;
1364 /* Check for "0.0" and variants;
7b6327ae 1365 SunOS 4 spuriously returns ERANGE for them. */
e8bbfc4e
RK
1366 while (*p1 == '0') p1++;
1367 if (*p1 == '.')
1368 {
1369 p1++;
1370 while (*p1 == '0') p1++;
1371 }
1372 if (*p1 == 'e' || *p1 == 'E')
1373 {
1374 /* with significand==0, ignore the exponent */
1375 p1++;
1376 while (*p1 != 0) p1++;
1377 }
1378 /* ERANGE is also reported for underflow,
1379 so test the value to distinguish overflow from that. */
1380 if (*p1 != 0 && (value > 1.0 || value < -1.0))
7e8b9a1d 1381 pedwarn ("floating point number exceeds range of `double'");
e8bbfc4e
RK
1382 }
1383#endif
1384
1385 /* Read the suffixes to choose a data type. */
1386 while (1)
1387 {
1388 if (c == 'f' || c == 'F')
1389 {
1390 if (f_seen)
1391 error ("two `f's in floating constant");
e53c841d
RS
1392 else
1393 {
1394 f_seen = 1;
1395 type = float_type_node;
ef66cdfe 1396 value = real_value_truncate (TYPE_MODE (type), value);
e53c841d
RS
1397 if (REAL_VALUE_ISINF (value) && pedantic)
1398 pedwarn ("floating point number exceeds range of `float'");
1399 }
e8bbfc4e
RK
1400 }
1401 else if (c == 'l' || c == 'L')
1402 {
1403 if (l_seen)
1404 error ("two `l's in floating constant");
1405 l_seen = 1;
1406 type = long_double_type_node;
1407 }
1408 else
1409 {
1410 if (isalnum (c))
1411 {
1412 error ("garbage at end of number");
1413 while (isalnum (c))
1414 {
1415 if (p >= token_buffer + maxtoken - 3)
1416 p = extend_token_buffer (p);
1417 *p++ = c;
1418 c = getc (finput);
1419 }
1420 }
1421 break;
1422 }
1423 if (p >= token_buffer + maxtoken - 3)
1424 p = extend_token_buffer (p);
1425 *p++ = c;
1426 c = getc (finput);
1427 }
1428
1429 /* Create a node with determined type and value. */
1430 yylval.ttype = build_real (type, value);
1431
1432 ungetc (c, finput);
1433 *p = 0;
1434 }
1435 else
1436 {
1437 tree traditional_type, ansi_type, type;
8d9bfdc5 1438 HOST_WIDE_INT high, low;
e8bbfc4e
RK
1439 int spec_unsigned = 0;
1440 int spec_long = 0;
1441 int spec_long_long = 0;
1442 int bytes, warn, i;
1443
1444 while (1)
1445 {
1446 if (c == 'u' || c == 'U')
1447 {
1448 if (spec_unsigned)
1449 error ("two `u's in integer constant");
1450 spec_unsigned = 1;
1451 }
1452 else if (c == 'l' || c == 'L')
1453 {
1454 if (spec_long)
1455 {
1456 if (spec_long_long)
1457 error ("three `l's in integer constant");
1458 else if (pedantic)
1459 pedwarn ("ANSI C forbids long long integer constants");
1460 spec_long_long = 1;
1461 }
1462 spec_long = 1;
1463 }
1464 else
1465 {
1466 if (isalnum (c))
1467 {
1468 error ("garbage at end of number");
1469 while (isalnum (c))
1470 {
1471 if (p >= token_buffer + maxtoken - 3)
1472 p = extend_token_buffer (p);
1473 *p++ = c;
1474 c = getc (finput);
1475 }
1476 }
1477 break;
1478 }
1479 if (p >= token_buffer + maxtoken - 3)
1480 p = extend_token_buffer (p);
1481 *p++ = c;
1482 c = getc (finput);
1483 }
1484
1485 ungetc (c, finput);
1486
1487 /* If the constant is not long long and it won't fit in an
1488 unsigned long, or if the constant is long long and won't fit
1489 in an unsigned long long, then warn that the constant is out
1490 of range. */
1491
1492 /* ??? This assumes that long long and long integer types are
1493 a multiple of 8 bits. This better than the original code
1494 though which assumed that long was exactly 32 bits and long
1495 long was exactly 64 bits. */
1496
1497 if (spec_long_long)
1498 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1499 else
1500 bytes = TYPE_PRECISION (long_integer_type_node) / 8;
1501
4ca827d0
RS
1502 warn = overflow;
1503 for (i = bytes; i < TOTAL_PARTS; i++)
1504 if (parts[i])
1505 warn = 1;
1506 if (warn)
1507 pedwarn ("integer constant out of range");
e8bbfc4e
RK
1508
1509 /* This is simplified by the fact that our constant
1510 is always positive. */
8d9bfdc5
RK
1511
1512 high = low = 0;
1513
1514 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1515 {
1516 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1517 / HOST_BITS_PER_CHAR)]
1518 << (i * HOST_BITS_PER_CHAR));
1519 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1520 }
1521
1522 yylval.ttype = build_int_2 (low, high);
e8bbfc4e 1523
e8bbfc4e
RK
1524 /* If warn_traditional, calculate both the ANSI type and the
1525 traditional type, then see if they disagree.
1526 Otherwise, calculate only the type for the dialect in use. */
1527 if (warn_traditional || flag_traditional)
1528 {
1529 /* Calculate the traditional type. */
1530 /* Traditionally, any constant is signed;
1531 but if unsigned is specified explicitly, obey that.
1532 Use the smallest size with the right number of bits,
1533 except for one special case with decimal constants. */
1534 if (! spec_long && base != 10
1535 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1536 traditional_type = (spec_unsigned ? unsigned_type_node
1537 : integer_type_node);
1538 /* A decimal constant must be long
1539 if it does not fit in type int.
1540 I think this is independent of whether
1541 the constant is signed. */
1542 else if (! spec_long && base == 10
1543 && int_fits_type_p (yylval.ttype, integer_type_node))
1544 traditional_type = (spec_unsigned ? unsigned_type_node
1545 : integer_type_node);
4ca827d0 1546 else if (! spec_long_long)
e8bbfc4e
RK
1547 traditional_type = (spec_unsigned ? long_unsigned_type_node
1548 : long_integer_type_node);
1549 else
1550 traditional_type = (spec_unsigned
1551 ? long_long_unsigned_type_node
1552 : long_long_integer_type_node);
1553 }
1554 if (warn_traditional || ! flag_traditional)
1555 {
1556 /* Calculate the ANSI type. */
1557 if (! spec_long && ! spec_unsigned
1558 && int_fits_type_p (yylval.ttype, integer_type_node))
1559 ansi_type = integer_type_node;
1560 else if (! spec_long && (base != 10 || spec_unsigned)
1561 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1562 ansi_type = unsigned_type_node;
1563 else if (! spec_unsigned && !spec_long_long
1564 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1565 ansi_type = long_integer_type_node;
4ca827d0 1566 else if (! spec_long_long)
e8bbfc4e
RK
1567 ansi_type = long_unsigned_type_node;
1568 else if (! spec_unsigned
1569 && int_fits_type_p (yylval.ttype,
1570 long_long_integer_type_node))
1571 ansi_type = long_long_integer_type_node;
1572 else
1573 ansi_type = long_long_unsigned_type_node;
1574 }
1575
1576 type = flag_traditional ? traditional_type : ansi_type;
1577
1578 if (warn_traditional && traditional_type != ansi_type)
1579 {
1580 if (TYPE_PRECISION (traditional_type)
1581 != TYPE_PRECISION (ansi_type))
1582 warning ("width of integer constant changes with -traditional");
1583 else if (TREE_UNSIGNED (traditional_type)
1584 != TREE_UNSIGNED (ansi_type))
1585 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
a3ee5899
JW
1586 else
1587 warning ("width of integer constant may change on other systems with -traditional");
e8bbfc4e 1588 }
e8bbfc4e 1589
4ca827d0
RS
1590 if (!flag_traditional && !int_fits_type_p (yylval.ttype, type)
1591 && !warn)
88d92ca5
RS
1592 pedwarn ("integer constant out of range");
1593
e8bbfc4e
RK
1594 TREE_TYPE (yylval.ttype) = type;
1595 *p = 0;
1596 }
1597
1598 value = CONSTANT; break;
1599 }
1600
1601 case '\'':
1602 char_constant:
1603 {
1604 register int result = 0;
1605 register num_chars = 0;
1606 unsigned width = TYPE_PRECISION (char_type_node);
1607 int max_chars;
1608
1609 if (wide_flag)
1610 {
1611 width = WCHAR_TYPE_SIZE;
1612#ifdef MULTIBYTE_CHARS
1613 max_chars = MB_CUR_MAX;
1614#else
1615 max_chars = 1;
1616#endif
1617 }
1618 else
1619 max_chars = TYPE_PRECISION (integer_type_node) / width;
1620
1621 while (1)
1622 {
1623 tryagain:
1624
1625 c = getc (finput);
1626
1627 if (c == '\'' || c == EOF)
1628 break;
1629
1630 if (c == '\\')
1631 {
1632 c = readescape ();
1633 if (c < 0)
1634 goto tryagain;
1635 if (width < HOST_BITS_PER_INT
1636 && (unsigned) c >= (1 << width))
1637 pedwarn ("escape sequence out of range for character");
1638 }
1639 else if (c == '\n')
1640 {
1641 if (pedantic)
1642 pedwarn ("ANSI C forbids newline in character constant");
1643 lineno++;
1644 }
1645
1646 num_chars++;
1647 if (num_chars > maxtoken - 4)
1648 extend_token_buffer (token_buffer);
1649
1650 token_buffer[num_chars] = c;
1651
1652 /* Merge character into result; ignore excess chars. */
1653 if (num_chars < max_chars + 1)
1654 {
1655 if (width < HOST_BITS_PER_INT)
1656 result = (result << width) | (c & ((1 << width) - 1));
1657 else
1658 result = c;
1659 }
1660 }
1661
1662 token_buffer[num_chars + 1] = '\'';
1663 token_buffer[num_chars + 2] = 0;
1664
1665 if (c != '\'')
1666 error ("malformatted character constant");
1667 else if (num_chars == 0)
1668 error ("empty character constant");
1669 else if (num_chars > max_chars)
1670 {
1671 num_chars = max_chars;
1672 error ("character constant too long");
1673 }
1674 else if (num_chars != 1 && ! flag_traditional)
1675 warning ("multi-character character constant");
1676
1677 /* If char type is signed, sign-extend the constant. */
1678 if (! wide_flag)
1679 {
1680 int num_bits = num_chars * width;
1681 if (TREE_UNSIGNED (char_type_node)
1682 || ((result >> (num_bits - 1)) & 1) == 0)
1683 yylval.ttype
8d9bfdc5
RK
1684 = build_int_2 (result & ((unsigned HOST_WIDE_INT) ~0
1685 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
e8bbfc4e
RK
1686 0);
1687 else
1688 yylval.ttype
8d9bfdc5
RK
1689 = build_int_2 (result | ~((unsigned HOST_WIDE_INT) ~0
1690 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
e8bbfc4e
RK
1691 -1);
1692 }
1693 else
1694 {
1695#ifdef MULTIBYTE_CHARS
1696 /* Set the initial shift state and convert the next sequence. */
1697 result = 0;
1698 /* In all locales L'\0' is zero and mbtowc will return zero,
1699 so don't use it. */
1700 if (num_chars > 1
1701 || (num_chars == 1 && token_buffer[1] != '\0'))
1702 {
1703 wchar_t wc;
8d9bfdc5 1704 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
e8bbfc4e
RK
1705 if (mbtowc (& wc, token_buffer + 1, num_chars) == num_chars)
1706 result = wc;
1707 else
1708 warning ("Ignoring invalid multibyte character");
1709 }
1710#endif
1711 yylval.ttype = build_int_2 (result, 0);
1712 }
1713
1714 TREE_TYPE (yylval.ttype) = integer_type_node;
1715 value = CONSTANT;
1716 break;
1717 }
1718
1719 case '"':
1720 string_constant:
1721 {
1722 c = getc (finput);
1723 p = token_buffer + 1;
1724
1725 while (c != '"' && c >= 0)
1726 {
1727 /* ignore_escape_flag is set for reading the filename in #line. */
1728 if (!ignore_escape_flag && c == '\\')
1729 {
1730 c = readescape ();
1731 if (c < 0)
1732 goto skipnewline;
1733 if (!wide_flag
1734 && TYPE_PRECISION (char_type_node) < HOST_BITS_PER_INT
1735 && c >= (1 << TYPE_PRECISION (char_type_node)))
1736 pedwarn ("escape sequence out of range for character");
1737 }
1738 else if (c == '\n')
1739 {
1740 if (pedantic)
1741 pedwarn ("ANSI C forbids newline in string constant");
1742 lineno++;
1743 }
1744
1745 if (p == token_buffer + maxtoken)
1746 p = extend_token_buffer (p);
1747 *p++ = c;
1748
1749 skipnewline:
1750 c = getc (finput);
1751 }
1752 *p = 0;
1753
1754 /* We have read the entire constant.
1755 Construct a STRING_CST for the result. */
1756
1757 if (wide_flag)
1758 {
1759 /* If this is a L"..." wide-string, convert the multibyte string
1760 to a wide character string. */
1761 char *widep = (char *) alloca ((p - token_buffer) * WCHAR_BYTES);
1762 int len;
1763
1764#ifdef MULTIBYTE_CHARS
1765 len = mbstowcs ((wchar_t *) widep, token_buffer + 1, p - token_buffer);
1766 if ((unsigned) len >= (p - token_buffer))
1767 {
1768 warning ("Ignoring invalid multibyte string");
1769 len = 0;
1770 }
1771 bzero (widep + (len * WCHAR_BYTES), WCHAR_BYTES);
1772#else
1773 {
1774 union { long l; char c[sizeof (long)]; } u;
1775 int big_endian;
1776 char *wp, *cp;
1777
1778 /* Determine whether host is little or big endian. */
1779 u.l = 1;
1780 big_endian = u.c[sizeof (long) - 1];
1781 wp = widep + (big_endian ? WCHAR_BYTES - 1 : 0);
1782
1783 bzero (widep, (p - token_buffer) * WCHAR_BYTES);
1784 for (cp = token_buffer + 1; cp < p; cp++)
1785 *wp = *cp, wp += WCHAR_BYTES;
1786 len = p - token_buffer - 1;
1787 }
1788#endif
1789 yylval.ttype = build_string ((len + 1) * WCHAR_BYTES, widep);
1790 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
1791 }
1792 else
1793 {
1794 yylval.ttype = build_string (p - token_buffer, token_buffer + 1);
1795 TREE_TYPE (yylval.ttype) = char_array_type_node;
1796 }
1797
1798 *p++ = '"';
1799 *p = 0;
1800
1801 value = STRING; break;
1802 }
1803
1804 case '+':
1805 case '-':
1806 case '&':
1807 case '|':
1808 case '<':
1809 case '>':
1810 case '*':
1811 case '/':
1812 case '%':
1813 case '^':
1814 case '!':
1815 case '=':
1816 {
1817 register int c1;
1818
1819 combine:
1820
1821 switch (c)
1822 {
1823 case '+':
1824 yylval.code = PLUS_EXPR; break;
1825 case '-':
1826 yylval.code = MINUS_EXPR; break;
1827 case '&':
1828 yylval.code = BIT_AND_EXPR; break;
1829 case '|':
1830 yylval.code = BIT_IOR_EXPR; break;
1831 case '*':
1832 yylval.code = MULT_EXPR; break;
1833 case '/':
1834 yylval.code = TRUNC_DIV_EXPR; break;
1835 case '%':
1836 yylval.code = TRUNC_MOD_EXPR; break;
1837 case '^':
1838 yylval.code = BIT_XOR_EXPR; break;
1839 case LSHIFT:
1840 yylval.code = LSHIFT_EXPR; break;
1841 case RSHIFT:
1842 yylval.code = RSHIFT_EXPR; break;
1843 case '<':
1844 yylval.code = LT_EXPR; break;
1845 case '>':
1846 yylval.code = GT_EXPR; break;
1847 }
1848
1849 token_buffer[1] = c1 = getc (finput);
1850 token_buffer[2] = 0;
1851
1852 if (c1 == '=')
1853 {
1854 switch (c)
1855 {
1856 case '<':
1857 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
1858 case '>':
1859 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
1860 case '!':
1861 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
1862 case '=':
1863 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
1864 }
1865 value = ASSIGN; goto done;
1866 }
1867 else if (c == c1)
1868 switch (c)
1869 {
1870 case '+':
1871 value = PLUSPLUS; goto done;
1872 case '-':
1873 value = MINUSMINUS; goto done;
1874 case '&':
1875 value = ANDAND; goto done;
1876 case '|':
1877 value = OROR; goto done;
1878 case '<':
1879 c = LSHIFT;
1880 goto combine;
1881 case '>':
1882 c = RSHIFT;
1883 goto combine;
1884 }
1885 else if ((c == '-') && (c1 == '>'))
1886 { value = POINTSAT; goto done; }
1887 ungetc (c1, finput);
1888 token_buffer[1] = 0;
1889
1890 if ((c == '<') || (c == '>'))
1891 value = ARITHCOMPARE;
1892 else value = c;
1893 goto done;
1894 }
1895
1896 case 0:
1897 /* Don't make yyparse think this is eof. */
1898 value = 1;
1899 break;
1900
1901 default:
1902 value = c;
1903 }
1904
1905done:
1906/* yylloc.last_line = lineno; */
1907
1908 return value;
1909}
1910
d45cf215 1911/* Sets the value of the 'yydebug' variable to VALUE.
e8bbfc4e
RK
1912 This is a function so we don't have to have YYDEBUG defined
1913 in order to build the compiler. */
1914
1915void
1916set_yydebug (value)
1917 int value;
1918{
1919#if YYDEBUG != 0
1920 yydebug = value;
1921#else
1922 warning ("YYDEBUG not defined.");
1923#endif
1924}
This page took 0.236751 seconds and 5 git commands to generate.