]> gcc.gnu.org Git - gcc.git/blame - gcc/c-lex.c
*** empty log message ***
[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)
342 fprintf (file, " 0x%8x%8x", TREE_INT_CST_HIGH (t),
343 TREE_INT_CST_LOW (t));
344 break;
345 }
346}
347
348\f
349/* If C is not whitespace, return C.
350 Otherwise skip whitespace and return first nonwhite char read. */
351
352static int
353skip_white_space (c)
354 register int c;
355{
0dcd8cee 356 static int newline_warning = 0;
e8bbfc4e
RK
357
358 for (;;)
359 {
360 switch (c)
361 {
0dcd8cee
RS
362 /* We don't recognize comments here, because
363 cpp output can include / and * consecutively as operators.
364 Also, there's no need, since cpp removes all comments. */
e8bbfc4e
RK
365
366 case '\n':
367 c = check_newline ();
368 break;
369
370 case ' ':
371 case '\t':
372 case '\f':
e8bbfc4e
RK
373 case '\v':
374 case '\b':
375 c = getc (finput);
376 break;
377
0dcd8cee
RS
378 case '\r':
379 /* ANSI C says the effects of a carriage return in a source file
380 are undefined. */
381 if (pedantic && !newline_warning)
382 {
383 warning ("carriage return in source file");
384 warning ("(we only warn about the first carriage return)");
385 newline_warning = 1;
386 }
387 c = getc (finput);
388 break;
389
e8bbfc4e
RK
390 case '\\':
391 c = getc (finput);
392 if (c == '\n')
393 lineno++;
394 else
395 error ("stray '\\' in program");
396 c = getc (finput);
397 break;
398
399 default:
400 return (c);
401 }
402 }
403}
404
405/* Skips all of the white space at the current location in the input file.
406 Must use and reset nextchar if it has the next character. */
407
408void
409position_after_white_space ()
410{
411 register int c;
412
413 if (nextchar != -1)
414 c = nextchar, nextchar = -1;
415 else
416 c = getc (finput);
417
418 ungetc (skip_white_space (c), finput);
419}
420
421/* Make the token buffer longer, preserving the data in it.
422 P should point to just beyond the last valid character in the old buffer.
423 The value we return is a pointer to the new buffer
424 at a place corresponding to P. */
425
426static char *
427extend_token_buffer (p)
428 char *p;
429{
430 int offset = p - token_buffer;
431
432 maxtoken = maxtoken * 2 + 10;
433 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
434
435 return token_buffer + offset;
436}
437\f
438/* At the beginning of a line, increment the line number
439 and process any #-directive on this line.
440 If the line is a #-directive, read the entire line and return a newline.
441 Otherwise, return the line's first non-whitespace character. */
442
443int
444check_newline ()
445{
446 register int c;
447 register int token;
448
449 lineno++;
450
451 /* Read first nonwhite char on the line. */
452
453 c = getc (finput);
454 while (c == ' ' || c == '\t')
455 c = getc (finput);
456
457 if (c != '#')
458 {
459 /* If not #, return it so caller will use it. */
460 return c;
461 }
462
463 /* Read first nonwhite char after the `#'. */
464
465 c = getc (finput);
466 while (c == ' ' || c == '\t')
467 c = getc (finput);
468
469 /* If a letter follows, then if the word here is `line', skip
470 it and ignore it; otherwise, ignore the line, with an error
471 if the word isn't `pragma', `ident', `define', or `undef'. */
472
473 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
474 {
475 if (c == 'p')
476 {
477 if (getc (finput) == 'r'
478 && getc (finput) == 'a'
479 && getc (finput) == 'g'
480 && getc (finput) == 'm'
481 && getc (finput) == 'a'
482 && ((c = getc (finput)) == ' ' || c == '\t' || c == '\n'))
483 {
484#ifdef HANDLE_PRAGMA
485 HANDLE_PRAGMA (finput);
486#endif /* HANDLE_PRAGMA */
487 goto skipline;
488 }
489 }
490
491 else if (c == 'd')
492 {
493 if (getc (finput) == 'e'
494 && getc (finput) == 'f'
495 && getc (finput) == 'i'
496 && getc (finput) == 'n'
497 && getc (finput) == 'e'
498 && ((c = getc (finput)) == ' ' || c == '\t' || c == '\n'))
499 {
500#ifdef DWARF_DEBUGGING_INFO
501 if ((debug_info_level == DINFO_LEVEL_VERBOSE)
502 && (write_symbols == DWARF_DEBUG))
503 dwarfout_define (lineno, get_directive_line (finput));
504#endif /* DWARF_DEBUGGING_INFO */
505 goto skipline;
506 }
507 }
508 else if (c == 'u')
509 {
510 if (getc (finput) == 'n'
511 && getc (finput) == 'd'
512 && getc (finput) == 'e'
513 && getc (finput) == 'f'
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_undef (lineno, get_directive_line (finput));
520#endif /* DWARF_DEBUGGING_INFO */
521 goto skipline;
522 }
523 }
524 else if (c == 'l')
525 {
526 if (getc (finput) == 'i'
527 && getc (finput) == 'n'
528 && getc (finput) == 'e'
529 && ((c = getc (finput)) == ' ' || c == '\t'))
530 goto linenum;
531 }
532 else if (c == 'i')
533 {
534 if (getc (finput) == 'd'
535 && getc (finput) == 'e'
536 && getc (finput) == 'n'
537 && getc (finput) == 't'
538 && ((c = getc (finput)) == ' ' || c == '\t'))
539 {
540 /* #ident. The pedantic warning is now in cccp.c. */
541
542 /* Here we have just seen `#ident '.
543 A string constant should follow. */
544
545 while (c == ' ' || c == '\t')
546 c = getc (finput);
547
548 /* If no argument, ignore the line. */
549 if (c == '\n')
550 return c;
551
552 ungetc (c, finput);
553 token = yylex ();
554 if (token != STRING
555 || TREE_CODE (yylval.ttype) != STRING_CST)
556 {
557 error ("invalid #ident");
558 goto skipline;
559 }
560
561 if (!flag_no_ident)
562 {
563#ifdef ASM_OUTPUT_IDENT
564 ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (yylval.ttype));
565#endif
566 }
567
568 /* Skip the rest of this line. */
569 goto skipline;
570 }
571 }
572
573 error ("undefined or invalid # directive");
574 goto skipline;
575 }
576
577linenum:
578 /* Here we have either `#line' or `# <nonletter>'.
579 In either case, it should be a line number; a digit should follow. */
580
581 while (c == ' ' || c == '\t')
582 c = getc (finput);
583
584 /* If the # is the only nonwhite char on the line,
585 just ignore it. Check the new newline. */
586 if (c == '\n')
587 return c;
588
589 /* Something follows the #; read a token. */
590
591 ungetc (c, finput);
592 token = yylex ();
593
594 if (token == CONSTANT
595 && TREE_CODE (yylval.ttype) == INTEGER_CST)
596 {
597 int old_lineno = lineno;
598 int used_up = 0;
599 /* subtract one, because it is the following line that
600 gets the specified number */
601
602 int l = TREE_INT_CST_LOW (yylval.ttype) - 1;
603
604 /* Is this the last nonwhite stuff on the line? */
605 c = getc (finput);
606 while (c == ' ' || c == '\t')
607 c = getc (finput);
608 if (c == '\n')
609 {
610 /* No more: store the line number and check following line. */
611 lineno = l;
612 return c;
613 }
614 ungetc (c, finput);
615
616 /* More follows: it must be a string constant (filename). */
617
618 /* Read the string constant, but don't treat \ as special. */
619 ignore_escape_flag = 1;
620 token = yylex ();
621 ignore_escape_flag = 0;
622
623 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
624 {
625 error ("invalid #line");
626 goto skipline;
627 }
628
629 input_filename
630 = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
631 strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
632 lineno = l;
633
634 /* Each change of file name
635 reinitializes whether we are now in a system header. */
636 in_system_header = 0;
637
638 if (main_input_filename == 0)
639 main_input_filename = input_filename;
640
641 /* Is this the last nonwhite stuff on the line? */
642 c = getc (finput);
643 while (c == ' ' || c == '\t')
644 c = getc (finput);
645 if (c == '\n')
646 return c;
647 ungetc (c, finput);
648
649 token = yylex ();
650 used_up = 0;
651
652 /* `1' after file name means entering new file.
653 `2' after file name means just left a file. */
654
655 if (token == CONSTANT
656 && TREE_CODE (yylval.ttype) == INTEGER_CST)
657 {
658 if (TREE_INT_CST_LOW (yylval.ttype) == 1)
659 {
660 /* Pushing to a new file. */
661 struct file_stack *p
662 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
663 input_file_stack->line = old_lineno;
664 p->next = input_file_stack;
665 p->name = input_filename;
666 input_file_stack = p;
667 input_file_stack_tick++;
668#ifdef DWARF_DEBUGGING_INFO
669 if (debug_info_level == DINFO_LEVEL_VERBOSE
670 && write_symbols == DWARF_DEBUG)
671 dwarfout_start_new_source_file (input_filename);
672#endif /* DWARF_DEBUGGING_INFO */
673
674 used_up = 1;
675 }
676 else if (TREE_INT_CST_LOW (yylval.ttype) == 2)
677 {
678 /* Popping out of a file. */
679 if (input_file_stack->next)
680 {
681 struct file_stack *p = input_file_stack;
682 input_file_stack = p->next;
683 free (p);
684 input_file_stack_tick++;
685#ifdef DWARF_DEBUGGING_INFO
686 if (debug_info_level == DINFO_LEVEL_VERBOSE
687 && write_symbols == DWARF_DEBUG)
688 dwarfout_resume_previous_source_file (input_file_stack->line);
689#endif /* DWARF_DEBUGGING_INFO */
690 }
691 else
692 error ("#-lines for entering and leaving files don't match");
693
694 used_up = 1;
695 }
696 }
697
698 /* If we have handled a `1' or a `2',
699 see if there is another number to read. */
700 if (used_up)
701 {
702 /* Is this the last nonwhite stuff on the line? */
703 c = getc (finput);
704 while (c == ' ' || c == '\t')
705 c = getc (finput);
706 if (c == '\n')
707 return c;
708 ungetc (c, finput);
709
710 token = yylex ();
711 used_up = 0;
712 }
713
714 /* `3' after file name means this is a system header file. */
715
716 if (token == CONSTANT
717 && TREE_CODE (yylval.ttype) == INTEGER_CST
718 && TREE_INT_CST_LOW (yylval.ttype) == 3)
719 in_system_header = 1;
720 }
721 else
722 error ("invalid #-line");
723
724 /* skip the rest of this line. */
725 skipline:
726 if (c == '\n')
727 return c;
728 while ((c = getc (finput)) != EOF && c != '\n');
729 return c;
730}
731\f
732#define isalnum(char) ((char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char >= '0' && char <= '9'))
733#define isdigit(char) (char >= '0' && char <= '9')
734#define ENDFILE -1 /* token that represents end-of-file */
735
736/* Read an escape sequence, returning its equivalent as a character,
737 or -1 if it is backslash-newline. */
738
739static int
740readescape ()
741{
742 register int c = getc (finput);
743 register int code;
744 register unsigned count;
745 unsigned firstdig;
746
747 switch (c)
748 {
749 case 'x':
750 if (warn_traditional)
751 warning ("the meaning of `\\x' varies with -traditional");
752
753 if (flag_traditional)
754 return c;
755
756 code = 0;
757 count = 0;
758 while (1)
759 {
760 c = getc (finput);
761 if (!(c >= 'a' && c <= 'f')
762 && !(c >= 'A' && c <= 'F')
763 && !(c >= '0' && c <= '9'))
764 {
765 ungetc (c, finput);
766 break;
767 }
768 code *= 16;
769 if (c >= 'a' && c <= 'f')
770 code += c - 'a' + 10;
771 if (c >= 'A' && c <= 'F')
772 code += c - 'A' + 10;
773 if (c >= '0' && c <= '9')
774 code += c - '0';
775 if (count == 0)
776 firstdig = code;
777 count++;
778 }
779 if (count == 0)
780 error ("\\x used with no following hex digits");
781 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
782 || (count > 1
783 && ((1 << (TYPE_PRECISION (integer_type_node) - (count - 1) * 4))
784 <= firstdig)))
785 pedwarn ("hex escape out of range");
786 return code;
787
788 case '0': case '1': case '2': case '3': case '4':
789 case '5': case '6': case '7':
790 code = 0;
791 count = 0;
792 while ((c <= '7') && (c >= '0') && (count++ < 3))
793 {
794 code = (code * 8) + (c - '0');
795 c = getc (finput);
796 }
797 ungetc (c, finput);
798 return code;
799
800 case '\\': case '\'': case '"':
801 return c;
802
803 case '\n':
804 lineno++;
805 return -1;
806
807 case 'n':
808 return TARGET_NEWLINE;
809
810 case 't':
811 return TARGET_TAB;
812
813 case 'r':
814 return TARGET_CR;
815
816 case 'f':
817 return TARGET_FF;
818
819 case 'b':
820 return TARGET_BS;
821
822 case 'a':
823 if (warn_traditional)
824 warning ("the meaning of `\\a' varies with -traditional");
825
826 if (flag_traditional)
827 return c;
828 return TARGET_BELL;
829
830 case 'v':
831#if 0 /* Vertical tab is present in common usage compilers. */
832 if (flag_traditional)
833 return c;
834#endif
835 return TARGET_VT;
836
837 case 'E':
838 pedwarn ("non-ANSI-standard escape sequence, `\\E'");
839 return 033;
840
841 case '?':
842 return c;
843
844 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
845 case '(':
846 case '{':
847 case '[':
848 if (pedantic)
849 pedwarn ("non-ANSI escape sequence `\\%c'", c);
850 return c;
851 }
852 if (c >= 040 && c <= 0177)
853 pedwarn ("unknown escape sequence `\\%c'", c);
854 else
855 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
856 return c;
857}
858\f
859void
860yyerror (string)
861 char *string;
862{
863 char buf[200];
864
865 strcpy (buf, string);
866
867 /* We can't print string and character constants well
868 because the token_buffer contains the result of processing escapes. */
869 if (end_of_file)
870 strcat (buf, " at end of input");
871 else if (token_buffer[0] == 0)
872 strcat (buf, " at null character");
873 else if (token_buffer[0] == '"')
874 strcat (buf, " before string constant");
875 else if (token_buffer[0] == '\'')
876 strcat (buf, " before character constant");
877 else if (token_buffer[0] < 040 || (unsigned char) token_buffer[0] >= 0177)
878 sprintf (buf + strlen (buf), " before character 0%o",
879 (unsigned char) token_buffer[0]);
880 else
881 strcat (buf, " before `%s'");
882
883 error (buf, token_buffer);
884}
885
886#if 0
887
888struct try_type
889{
890 tree *node_var;
891 char unsigned_flag;
892 char long_flag;
893 char long_long_flag;
894};
895
896struct try_type type_sequence[] =
897{
898 { &integer_type_node, 0, 0, 0},
899 { &unsigned_type_node, 1, 0, 0},
900 { &long_integer_type_node, 0, 1, 0},
901 { &long_unsigned_type_node, 1, 1, 0},
902 { &long_long_integer_type_node, 0, 1, 1},
903 { &long_long_unsigned_type_node, 1, 1, 1}
904};
905#endif /* 0 */
906\f
907int
908yylex ()
909{
910 register int c;
911 register char *p;
912 register int value;
913 int wide_flag = 0;
914
915 if (nextchar >= 0)
916 c = nextchar, nextchar = -1;
917 else
918 c = getc (finput);
919
920 /* Effectively do c = skip_white_space (c)
921 but do it faster in the usual cases. */
922 while (1)
923 switch (c)
924 {
e8bbfc4e
RK
925 case ' ':
926 case '\t':
927 case '\f':
928 case '\v':
929 case '\b':
930 c = getc (finput);
931 break;
932
80f5b9d8
RS
933 case '\r':
934 /* Call skip_white_space so we can warn if appropriate. */
935
e8bbfc4e
RK
936 case '\n':
937 case '/':
938 case '\\':
939 c = skip_white_space (c);
940 default:
941 goto found_nonwhite;
942 }
943 found_nonwhite:
944
945 token_buffer[0] = c;
946 token_buffer[1] = 0;
947
948/* yylloc.first_line = lineno; */
949
950 switch (c)
951 {
952 case EOF:
953 end_of_file = 1;
954 token_buffer[0] = 0;
955 value = ENDFILE;
956 break;
957
958 case '$':
959 if (dollars_in_ident)
960 goto letter;
961 return '$';
962
963 case 'L':
964 /* Capital L may start a wide-string or wide-character constant. */
965 {
966 register int c = getc (finput);
967 if (c == '\'')
968 {
969 wide_flag = 1;
970 goto char_constant;
971 }
972 if (c == '"')
973 {
974 wide_flag = 1;
975 goto string_constant;
976 }
977 ungetc (c, finput);
978 }
979 goto letter;
980
981 case '@':
982 if (!doing_objc_thang)
983 {
984 value = c;
985 break;
986 }
987 p = token_buffer;
988 *p++ = '@';
989 c = getc (finput);
990 while (isalnum (c) || c == '_')
991 {
992 if (p >= token_buffer + maxtoken)
993 p = extend_token_buffer (p);
994
995 *p++ = c;
996 c = getc (finput);
997 }
998
999 *p = 0;
1000 nextchar = c;
1001 value = recognize_objc_keyword (token_buffer + 1);
1002 if (value != 0)
1003 break;
1004 error ("invalid Objective C keyword `%s'", token_buffer);
1005 /* Cause a syntax error--1 is not a valid token type. */
1006 value = 1;
1007 break;
1008
1009 case 'A': case 'B': case 'C': case 'D': case 'E':
1010 case 'F': case 'G': case 'H': case 'I': case 'J':
1011 case 'K': case 'M': case 'N': case 'O':
1012 case 'P': case 'Q': case 'R': case 'S': case 'T':
1013 case 'U': case 'V': case 'W': case 'X': case 'Y':
1014 case 'Z':
1015 case 'a': case 'b': case 'c': case 'd': case 'e':
1016 case 'f': case 'g': case 'h': case 'i': case 'j':
1017 case 'k': case 'l': case 'm': case 'n': case 'o':
1018 case 'p': case 'q': case 'r': case 's': case 't':
1019 case 'u': case 'v': case 'w': case 'x': case 'y':
1020 case 'z':
1021 case '_':
1022 letter:
1023 p = token_buffer;
1024 while (isalnum (c) || c == '_' || c == '$' || c == '@')
1025 {
1026 if (p >= token_buffer + maxtoken)
1027 p = extend_token_buffer (p);
1028 if (c == '$' && ! dollars_in_ident)
1029 break;
1030
1031 *p++ = c;
1032 c = getc (finput);
1033 }
1034
1035 *p = 0;
1036 nextchar = c;
1037
1038 value = IDENTIFIER;
1039 yylval.itype = 0;
1040
1041 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1042
1043 {
1044 register struct resword *ptr;
1045
1046 if (ptr = is_reserved_word (token_buffer, p - token_buffer))
1047 {
1048 if (ptr->rid)
1049 yylval.ttype = ridpointers[(int) ptr->rid];
1050 value = (int) ptr->token;
1051
1052 /* Even if we decided to recognize asm, still perhaps warn. */
1053 if (pedantic
d45cf215 1054 && (value == ASM_KEYWORD || value == TYPEOF
e8bbfc4e
RK
1055 || ptr->rid == RID_INLINE)
1056 && token_buffer[0] != '_')
1057 pedwarn ("ANSI does not permit the keyword `%s'",
1058 token_buffer);
1059 }
1060 }
1061
1062 /* If we did not find a keyword, look for an identifier
1063 (or a typename). */
1064
1065 if (value == IDENTIFIER)
1066 {
1067 yylval.ttype = get_identifier (token_buffer);
1068 lastiddecl = lookup_name (yylval.ttype);
1069
1070 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1071 value = TYPENAME;
6dbf678a
RS
1072 /* A user-invisible read-only initialized variable
1073 should be replaced by its value.
1074 We handle only strings since that's the only case used in C. */
1075 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1076 && DECL_IGNORED_P (lastiddecl)
1077 && TREE_READONLY (lastiddecl)
1078 && DECL_INITIAL (lastiddecl) != 0
1079 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1080 {
1081 yylval.ttype = DECL_INITIAL (lastiddecl);
1082 value = STRING;
1083 }
e8bbfc4e
RK
1084 else if (doing_objc_thang)
1085 {
1086 tree objc_interface_decl = lookup_interface (yylval.ttype);
1087
1088 if (objc_interface_decl)
1089 {
1090 value = CLASSNAME;
1091 yylval.ttype = objc_interface_decl;
1092 }
1093 }
1094 }
1095
1096 break;
1097
1098 case '0': case '1': case '2': case '3': case '4':
1099 case '5': case '6': case '7': case '8': case '9':
1100 case '.':
1101 {
1102 int base = 10;
1103 int count = 0;
1104 int largest_digit = 0;
1105 int numdigits = 0;
1106 /* for multi-precision arithmetic,
1107 we store only 8 live bits in each short,
1108 giving us 64 bits of reliable precision */
1109 short shorts[8];
1110 int overflow = 0;
1111
1112 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS} floatflag
1113 = NOT_FLOAT;
1114
1115 for (count = 0; count < 8; count++)
1116 shorts[count] = 0;
1117
1118 p = token_buffer;
1119 *p++ = c;
1120
1121 if (c == '0')
1122 {
1123 *p++ = (c = getc (finput));
1124 if ((c == 'x') || (c == 'X'))
1125 {
1126 base = 16;
1127 *p++ = (c = getc (finput));
1128 }
1129 /* Leading 0 forces octal unless the 0 is the only digit. */
1130 else if (c >= '0' && c <= '9')
1131 {
1132 base = 8;
1133 numdigits++;
1134 }
1135 else
1136 numdigits++;
1137 }
1138
1139 /* Read all the digits-and-decimal-points. */
1140
1141 while (c == '.'
1142 || (isalnum (c) && (c != 'l') && (c != 'L')
1143 && (c != 'u') && (c != 'U')
1144 && (floatflag == NOT_FLOAT || ((c != 'f') && (c != 'F')))))
1145 {
1146 if (c == '.')
1147 {
1148 if (base == 16)
1149 error ("floating constant may not be in radix 16");
1150 if (floatflag == AFTER_POINT)
1151 {
1152 error ("malformed floating constant");
1153 floatflag = TOO_MANY_POINTS;
1154 }
1155 else
1156 floatflag = AFTER_POINT;
1157
1158 base = 10;
1159 *p++ = c = getc (finput);
1160 /* Accept '.' as the start of a floating-point number
1161 only when it is followed by a digit.
1162 Otherwise, unread the following non-digit
1163 and use the '.' as a structural token. */
1164 if (p == token_buffer + 2 && !isdigit (c))
1165 {
1166 if (c == '.')
1167 {
1168 c = getc (finput);
1169 if (c == '.')
1170 {
1171 *p++ = c;
1172 *p = 0;
1173 return ELLIPSIS;
1174 }
1175 error ("parse error at `..'");
1176 }
1177 ungetc (c, finput);
1178 token_buffer[1] = 0;
1179 value = '.';
1180 goto done;
1181 }
1182 }
1183 else
1184 {
1185 /* It is not a decimal point.
1186 It should be a digit (perhaps a hex digit). */
1187
1188 if (isdigit (c))
1189 {
1190 c = c - '0';
1191 }
1192 else if (base <= 10)
1193 {
1194 if ((c&~040) == 'E')
1195 {
1196 base = 10;
1197 floatflag = AFTER_POINT;
1198 break; /* start of exponent */
1199 }
1200 error ("nondigits in number and not hexadecimal");
1201 c = 0;
1202 }
1203 else if (c >= 'a')
1204 {
1205 c = c - 'a' + 10;
1206 }
1207 else
1208 {
1209 c = c - 'A' + 10;
1210 }
1211 if (c >= largest_digit)
1212 largest_digit = c;
1213 numdigits++;
1214
1215 for (count = 0; count < 8; count++)
1216 {
1217 shorts[count] *= base;
1218 if (count)
1219 {
1220 shorts[count] += (shorts[count-1] >> 8);
1221 shorts[count-1] &= (1<<8)-1;
1222 }
1223 else shorts[0] += c;
1224 }
1225
1226 if (shorts[7] >= 1<<8
1227 || shorts[7] < - (1 << 8))
1228 overflow = TRUE;
1229
1230 if (p >= token_buffer + maxtoken - 3)
1231 p = extend_token_buffer (p);
1232 *p++ = (c = getc (finput));
1233 }
1234 }
1235
1236 if (numdigits == 0)
1237 error ("numeric constant with no digits");
1238
1239 if (largest_digit >= base)
1240 error ("numeric constant contains digits beyond the radix");
1241
1242 /* Remove terminating char from the token buffer and delimit the string */
1243 *--p = 0;
1244
1245 if (floatflag != NOT_FLOAT)
1246 {
1247 tree type = double_type_node;
1248 char f_seen = 0;
1249 char l_seen = 0;
1250 REAL_VALUE_TYPE value;
1251 jmp_buf handler;
1252
1253 /* Read explicit exponent if any, and put it in tokenbuf. */
1254
1255 if ((c == 'e') || (c == 'E'))
1256 {
1257 if (p >= token_buffer + maxtoken - 3)
1258 p = extend_token_buffer (p);
1259 *p++ = c;
1260 c = getc (finput);
1261 if ((c == '+') || (c == '-'))
1262 {
1263 *p++ = c;
1264 c = getc (finput);
1265 }
1266 if (! isdigit (c))
1267 error ("floating constant exponent has no digits");
1268 while (isdigit (c))
1269 {
1270 if (p >= token_buffer + maxtoken - 3)
1271 p = extend_token_buffer (p);
1272 *p++ = c;
1273 c = getc (finput);
1274 }
1275 }
1276
1277 *p = 0;
1278 errno = 0;
1279
1280 /* Convert string to a double, checking for overflow. */
1281 if (setjmp (handler))
1282 {
1283 error ("floating constant out of range");
1284 value = dconst0;
1285 }
1286 else
1287 {
1288 set_float_handler (handler);
1289 value = REAL_VALUE_ATOF (token_buffer);
1290 set_float_handler (0);
1291 }
1292#ifdef ERANGE
e53c841d 1293 if (errno == ERANGE && !flag_traditional && pedantic)
e8bbfc4e
RK
1294 {
1295 char *p1 = token_buffer;
1296 /* Check for "0.0" and variants;
1297 Sunos 4 spuriously returns ERANGE for them. */
1298 while (*p1 == '0') p1++;
1299 if (*p1 == '.')
1300 {
1301 p1++;
1302 while (*p1 == '0') p1++;
1303 }
1304 if (*p1 == 'e' || *p1 == 'E')
1305 {
1306 /* with significand==0, ignore the exponent */
1307 p1++;
1308 while (*p1 != 0) p1++;
1309 }
1310 /* ERANGE is also reported for underflow,
1311 so test the value to distinguish overflow from that. */
1312 if (*p1 != 0 && (value > 1.0 || value < -1.0))
7e8b9a1d 1313 pedwarn ("floating point number exceeds range of `double'");
e8bbfc4e
RK
1314 }
1315#endif
1316
1317 /* Read the suffixes to choose a data type. */
1318 while (1)
1319 {
1320 if (c == 'f' || c == 'F')
1321 {
1322 if (f_seen)
1323 error ("two `f's in floating constant");
e53c841d
RS
1324 else
1325 {
1326 f_seen = 1;
1327 type = float_type_node;
ef66cdfe 1328 value = real_value_truncate (TYPE_MODE (type), value);
e53c841d
RS
1329 if (REAL_VALUE_ISINF (value) && pedantic)
1330 pedwarn ("floating point number exceeds range of `float'");
1331 }
e8bbfc4e
RK
1332 }
1333 else if (c == 'l' || c == 'L')
1334 {
1335 if (l_seen)
1336 error ("two `l's in floating constant");
1337 l_seen = 1;
1338 type = long_double_type_node;
1339 }
1340 else
1341 {
1342 if (isalnum (c))
1343 {
1344 error ("garbage at end of number");
1345 while (isalnum (c))
1346 {
1347 if (p >= token_buffer + maxtoken - 3)
1348 p = extend_token_buffer (p);
1349 *p++ = c;
1350 c = getc (finput);
1351 }
1352 }
1353 break;
1354 }
1355 if (p >= token_buffer + maxtoken - 3)
1356 p = extend_token_buffer (p);
1357 *p++ = c;
1358 c = getc (finput);
1359 }
1360
1361 /* Create a node with determined type and value. */
1362 yylval.ttype = build_real (type, value);
1363
1364 ungetc (c, finput);
1365 *p = 0;
1366 }
1367 else
1368 {
1369 tree traditional_type, ansi_type, type;
1370 int spec_unsigned = 0;
1371 int spec_long = 0;
1372 int spec_long_long = 0;
1373 int bytes, warn, i;
1374
1375 while (1)
1376 {
1377 if (c == 'u' || c == 'U')
1378 {
1379 if (spec_unsigned)
1380 error ("two `u's in integer constant");
1381 spec_unsigned = 1;
1382 }
1383 else if (c == 'l' || c == 'L')
1384 {
1385 if (spec_long)
1386 {
1387 if (spec_long_long)
1388 error ("three `l's in integer constant");
1389 else if (pedantic)
1390 pedwarn ("ANSI C forbids long long integer constants");
1391 spec_long_long = 1;
1392 }
1393 spec_long = 1;
1394 }
1395 else
1396 {
1397 if (isalnum (c))
1398 {
1399 error ("garbage at end of number");
1400 while (isalnum (c))
1401 {
1402 if (p >= token_buffer + maxtoken - 3)
1403 p = extend_token_buffer (p);
1404 *p++ = c;
1405 c = getc (finput);
1406 }
1407 }
1408 break;
1409 }
1410 if (p >= token_buffer + maxtoken - 3)
1411 p = extend_token_buffer (p);
1412 *p++ = c;
1413 c = getc (finput);
1414 }
1415
1416 ungetc (c, finput);
1417
1418 /* If the constant is not long long and it won't fit in an
1419 unsigned long, or if the constant is long long and won't fit
1420 in an unsigned long long, then warn that the constant is out
1421 of range. */
1422
1423 /* ??? This assumes that long long and long integer types are
1424 a multiple of 8 bits. This better than the original code
1425 though which assumed that long was exactly 32 bits and long
1426 long was exactly 64 bits. */
1427
1428 if (spec_long_long)
1429 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1430 else
1431 bytes = TYPE_PRECISION (long_integer_type_node) / 8;
1432
1433 if (bytes <= 8)
1434 {
1435 warn = overflow;
1436 for (i = bytes; i < 8; i++)
1437 if (shorts[i])
1438 {
1439 /* If LL was not used, then clear any excess precision.
1440 This is equivalent to the original code, but it is
1441 not clear why this is being done. Perhaps to prevent
1442 ANSI programs from creating long long constants
1443 by accident? */
1444 if (! spec_long_long)
1445 shorts[i] = 0;
1446 warn = 1;
1447 }
1448 if (warn)
88d92ca5 1449 pedwarn ("integer constant out of range");
e8bbfc4e
RK
1450 }
1451 else if (overflow)
88d92ca5 1452 pedwarn ("integer constant larger than compiler can handle");
e8bbfc4e
RK
1453
1454 /* If it overflowed our internal buffer, then make it unsigned.
1455 We can't distinguish based on the tree node because
1456 any integer constant fits any long long type. */
1457 if (overflow)
1458 spec_unsigned = 1;
1459
1460 /* This is simplified by the fact that our constant
1461 is always positive. */
1462 /* The casts in the following statement should not be
1463 needed, but they get around bugs in some C compilers. */
1464 yylval.ttype
1465 = (build_int_2
1466 ((((long)shorts[3]<<24) + ((long)shorts[2]<<16)
1467 + ((long)shorts[1]<<8) + (long)shorts[0]),
1468 (((long)shorts[7]<<24) + ((long)shorts[6]<<16)
1469 + ((long)shorts[5]<<8) + (long)shorts[4])));
1470
1471#if 0
1472 /* Find the first allowable type that the value fits in. */
1473 type = 0;
1474 for (i = 0; i < sizeof (type_sequence) / sizeof (type_sequence[0]);
1475 i++)
1476 if (!(spec_long && !type_sequence[i].long_flag)
1477 && !(spec_long_long && !type_sequence[i].long_long_flag)
1478 && !(spec_unsigned && !type_sequence[i].unsigned_flag)
1479 /* A decimal constant can't be unsigned int
1480 unless explicitly specified. */
1481 && !(base == 10 && !spec_unsigned
1482 && *type_sequence[i].node_var == unsigned_type_node))
1483 if (int_fits_type_p (yylval.ttype, *type_sequence[i].node_var))
1484 {
1485 type = *type_sequence[i].node_var;
1486 break;
1487 }
1488 if (flag_traditional && type == long_unsigned_type_node
1489 && !spec_unsigned)
1490 type = long_integer_type_node;
1491
1492 if (type == 0)
1493 {
1494 type = long_long_integer_type_node;
1495 warning ("integer constant out of range");
1496 }
1497
1498 /* Warn about some cases where the type of a given constant
1499 changes from traditional C to ANSI C. */
1500 if (warn_traditional)
1501 {
1502 tree other_type = 0;
1503
1504 /* This computation is the same as the previous one
1505 except that flag_traditional is used backwards. */
1506 for (i = 0; i < sizeof (type_sequence) / sizeof (type_sequence[0]);
1507 i++)
1508 if (!(spec_long && !type_sequence[i].long_flag)
1509 && !(spec_long_long && !type_sequence[i].long_long_flag)
1510 && !(spec_unsigned && !type_sequence[i].unsigned_flag)
1511 /* A decimal constant can't be unsigned int
1512 unless explicitly specified. */
1513 && !(base == 10 && !spec_unsigned
1514 && *type_sequence[i].node_var == unsigned_type_node))
1515 if (int_fits_type_p (yylval.ttype, *type_sequence[i].node_var))
1516 {
1517 other_type = *type_sequence[i].node_var;
1518 break;
1519 }
1520 if (!flag_traditional && type == long_unsigned_type_node
1521 && !spec_unsigned)
1522 type = long_integer_type_node;
1523
1524 if (other_type != 0 && other_type != type)
1525 {
1526 if (flag_traditional)
1527 warning ("type of integer constant would be different without -traditional");
1528 else
1529 warning ("type of integer constant would be different with -traditional");
1530 }
1531 }
1532
1533#else /* 1 */
1534 /* If warn_traditional, calculate both the ANSI type and the
1535 traditional type, then see if they disagree.
1536 Otherwise, calculate only the type for the dialect in use. */
1537 if (warn_traditional || flag_traditional)
1538 {
1539 /* Calculate the traditional type. */
1540 /* Traditionally, any constant is signed;
1541 but if unsigned is specified explicitly, obey that.
1542 Use the smallest size with the right number of bits,
1543 except for one special case with decimal constants. */
1544 if (! spec_long && base != 10
1545 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1546 traditional_type = (spec_unsigned ? unsigned_type_node
1547 : integer_type_node);
1548 /* A decimal constant must be long
1549 if it does not fit in type int.
1550 I think this is independent of whether
1551 the constant is signed. */
1552 else if (! spec_long && base == 10
1553 && int_fits_type_p (yylval.ttype, integer_type_node))
1554 traditional_type = (spec_unsigned ? unsigned_type_node
1555 : integer_type_node);
1556 else if (! spec_long_long
1557 && int_fits_type_p (yylval.ttype,
1558 long_unsigned_type_node))
1559 traditional_type = (spec_unsigned ? long_unsigned_type_node
1560 : long_integer_type_node);
1561 else
1562 traditional_type = (spec_unsigned
1563 ? long_long_unsigned_type_node
1564 : long_long_integer_type_node);
1565 }
1566 if (warn_traditional || ! flag_traditional)
1567 {
1568 /* Calculate the ANSI type. */
1569 if (! spec_long && ! spec_unsigned
1570 && int_fits_type_p (yylval.ttype, integer_type_node))
1571 ansi_type = integer_type_node;
1572 else if (! spec_long && (base != 10 || spec_unsigned)
1573 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1574 ansi_type = unsigned_type_node;
1575 else if (! spec_unsigned && !spec_long_long
1576 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1577 ansi_type = long_integer_type_node;
1578 else if (! spec_long_long
1579 && int_fits_type_p (yylval.ttype,
1580 long_unsigned_type_node))
1581 ansi_type = long_unsigned_type_node;
1582 else if (! spec_unsigned
1583 && int_fits_type_p (yylval.ttype,
1584 long_long_integer_type_node))
1585 ansi_type = long_long_integer_type_node;
1586 else
1587 ansi_type = long_long_unsigned_type_node;
1588 }
1589
1590 type = flag_traditional ? traditional_type : ansi_type;
1591
1592 if (warn_traditional && traditional_type != ansi_type)
1593 {
1594 if (TYPE_PRECISION (traditional_type)
1595 != TYPE_PRECISION (ansi_type))
1596 warning ("width of integer constant changes with -traditional");
1597 else if (TREE_UNSIGNED (traditional_type)
1598 != TREE_UNSIGNED (ansi_type))
1599 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
a3ee5899
JW
1600 else
1601 warning ("width of integer constant may change on other systems with -traditional");
e8bbfc4e
RK
1602 }
1603#endif
1604
473a05b9 1605 if (!flag_traditional && !int_fits_type_p (yylval.ttype, type))
88d92ca5
RS
1606 pedwarn ("integer constant out of range");
1607
e8bbfc4e
RK
1608 TREE_TYPE (yylval.ttype) = type;
1609 *p = 0;
1610 }
1611
1612 value = CONSTANT; break;
1613 }
1614
1615 case '\'':
1616 char_constant:
1617 {
1618 register int result = 0;
1619 register num_chars = 0;
1620 unsigned width = TYPE_PRECISION (char_type_node);
1621 int max_chars;
1622
1623 if (wide_flag)
1624 {
1625 width = WCHAR_TYPE_SIZE;
1626#ifdef MULTIBYTE_CHARS
1627 max_chars = MB_CUR_MAX;
1628#else
1629 max_chars = 1;
1630#endif
1631 }
1632 else
1633 max_chars = TYPE_PRECISION (integer_type_node) / width;
1634
1635 while (1)
1636 {
1637 tryagain:
1638
1639 c = getc (finput);
1640
1641 if (c == '\'' || c == EOF)
1642 break;
1643
1644 if (c == '\\')
1645 {
1646 c = readescape ();
1647 if (c < 0)
1648 goto tryagain;
1649 if (width < HOST_BITS_PER_INT
1650 && (unsigned) c >= (1 << width))
1651 pedwarn ("escape sequence out of range for character");
1652 }
1653 else if (c == '\n')
1654 {
1655 if (pedantic)
1656 pedwarn ("ANSI C forbids newline in character constant");
1657 lineno++;
1658 }
1659
1660 num_chars++;
1661 if (num_chars > maxtoken - 4)
1662 extend_token_buffer (token_buffer);
1663
1664 token_buffer[num_chars] = c;
1665
1666 /* Merge character into result; ignore excess chars. */
1667 if (num_chars < max_chars + 1)
1668 {
1669 if (width < HOST_BITS_PER_INT)
1670 result = (result << width) | (c & ((1 << width) - 1));
1671 else
1672 result = c;
1673 }
1674 }
1675
1676 token_buffer[num_chars + 1] = '\'';
1677 token_buffer[num_chars + 2] = 0;
1678
1679 if (c != '\'')
1680 error ("malformatted character constant");
1681 else if (num_chars == 0)
1682 error ("empty character constant");
1683 else if (num_chars > max_chars)
1684 {
1685 num_chars = max_chars;
1686 error ("character constant too long");
1687 }
1688 else if (num_chars != 1 && ! flag_traditional)
1689 warning ("multi-character character constant");
1690
1691 /* If char type is signed, sign-extend the constant. */
1692 if (! wide_flag)
1693 {
1694 int num_bits = num_chars * width;
1695 if (TREE_UNSIGNED (char_type_node)
1696 || ((result >> (num_bits - 1)) & 1) == 0)
1697 yylval.ttype
1698 = build_int_2 (result & ((unsigned) ~0
1699 >> (HOST_BITS_PER_INT - num_bits)),
1700 0);
1701 else
1702 yylval.ttype
1703 = build_int_2 (result | ~((unsigned) ~0
1704 >> (HOST_BITS_PER_INT - num_bits)),
1705 -1);
1706 }
1707 else
1708 {
1709#ifdef MULTIBYTE_CHARS
1710 /* Set the initial shift state and convert the next sequence. */
1711 result = 0;
1712 /* In all locales L'\0' is zero and mbtowc will return zero,
1713 so don't use it. */
1714 if (num_chars > 1
1715 || (num_chars == 1 && token_buffer[1] != '\0'))
1716 {
1717 wchar_t wc;
1718 (void) mbtowc (NULL, NULL, 0);
1719 if (mbtowc (& wc, token_buffer + 1, num_chars) == num_chars)
1720 result = wc;
1721 else
1722 warning ("Ignoring invalid multibyte character");
1723 }
1724#endif
1725 yylval.ttype = build_int_2 (result, 0);
1726 }
1727
1728 TREE_TYPE (yylval.ttype) = integer_type_node;
1729 value = CONSTANT;
1730 break;
1731 }
1732
1733 case '"':
1734 string_constant:
1735 {
1736 c = getc (finput);
1737 p = token_buffer + 1;
1738
1739 while (c != '"' && c >= 0)
1740 {
1741 /* ignore_escape_flag is set for reading the filename in #line. */
1742 if (!ignore_escape_flag && c == '\\')
1743 {
1744 c = readescape ();
1745 if (c < 0)
1746 goto skipnewline;
1747 if (!wide_flag
1748 && TYPE_PRECISION (char_type_node) < HOST_BITS_PER_INT
1749 && c >= (1 << TYPE_PRECISION (char_type_node)))
1750 pedwarn ("escape sequence out of range for character");
1751 }
1752 else if (c == '\n')
1753 {
1754 if (pedantic)
1755 pedwarn ("ANSI C forbids newline in string constant");
1756 lineno++;
1757 }
1758
1759 if (p == token_buffer + maxtoken)
1760 p = extend_token_buffer (p);
1761 *p++ = c;
1762
1763 skipnewline:
1764 c = getc (finput);
1765 }
1766 *p = 0;
1767
1768 /* We have read the entire constant.
1769 Construct a STRING_CST for the result. */
1770
1771 if (wide_flag)
1772 {
1773 /* If this is a L"..." wide-string, convert the multibyte string
1774 to a wide character string. */
1775 char *widep = (char *) alloca ((p - token_buffer) * WCHAR_BYTES);
1776 int len;
1777
1778#ifdef MULTIBYTE_CHARS
1779 len = mbstowcs ((wchar_t *) widep, token_buffer + 1, p - token_buffer);
1780 if ((unsigned) len >= (p - token_buffer))
1781 {
1782 warning ("Ignoring invalid multibyte string");
1783 len = 0;
1784 }
1785 bzero (widep + (len * WCHAR_BYTES), WCHAR_BYTES);
1786#else
1787 {
1788 union { long l; char c[sizeof (long)]; } u;
1789 int big_endian;
1790 char *wp, *cp;
1791
1792 /* Determine whether host is little or big endian. */
1793 u.l = 1;
1794 big_endian = u.c[sizeof (long) - 1];
1795 wp = widep + (big_endian ? WCHAR_BYTES - 1 : 0);
1796
1797 bzero (widep, (p - token_buffer) * WCHAR_BYTES);
1798 for (cp = token_buffer + 1; cp < p; cp++)
1799 *wp = *cp, wp += WCHAR_BYTES;
1800 len = p - token_buffer - 1;
1801 }
1802#endif
1803 yylval.ttype = build_string ((len + 1) * WCHAR_BYTES, widep);
1804 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
1805 }
1806 else
1807 {
1808 yylval.ttype = build_string (p - token_buffer, token_buffer + 1);
1809 TREE_TYPE (yylval.ttype) = char_array_type_node;
1810 }
1811
1812 *p++ = '"';
1813 *p = 0;
1814
1815 value = STRING; break;
1816 }
1817
1818 case '+':
1819 case '-':
1820 case '&':
1821 case '|':
1822 case '<':
1823 case '>':
1824 case '*':
1825 case '/':
1826 case '%':
1827 case '^':
1828 case '!':
1829 case '=':
1830 {
1831 register int c1;
1832
1833 combine:
1834
1835 switch (c)
1836 {
1837 case '+':
1838 yylval.code = PLUS_EXPR; break;
1839 case '-':
1840 yylval.code = MINUS_EXPR; break;
1841 case '&':
1842 yylval.code = BIT_AND_EXPR; break;
1843 case '|':
1844 yylval.code = BIT_IOR_EXPR; break;
1845 case '*':
1846 yylval.code = MULT_EXPR; break;
1847 case '/':
1848 yylval.code = TRUNC_DIV_EXPR; break;
1849 case '%':
1850 yylval.code = TRUNC_MOD_EXPR; break;
1851 case '^':
1852 yylval.code = BIT_XOR_EXPR; break;
1853 case LSHIFT:
1854 yylval.code = LSHIFT_EXPR; break;
1855 case RSHIFT:
1856 yylval.code = RSHIFT_EXPR; break;
1857 case '<':
1858 yylval.code = LT_EXPR; break;
1859 case '>':
1860 yylval.code = GT_EXPR; break;
1861 }
1862
1863 token_buffer[1] = c1 = getc (finput);
1864 token_buffer[2] = 0;
1865
1866 if (c1 == '=')
1867 {
1868 switch (c)
1869 {
1870 case '<':
1871 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
1872 case '>':
1873 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
1874 case '!':
1875 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
1876 case '=':
1877 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
1878 }
1879 value = ASSIGN; goto done;
1880 }
1881 else if (c == c1)
1882 switch (c)
1883 {
1884 case '+':
1885 value = PLUSPLUS; goto done;
1886 case '-':
1887 value = MINUSMINUS; goto done;
1888 case '&':
1889 value = ANDAND; goto done;
1890 case '|':
1891 value = OROR; goto done;
1892 case '<':
1893 c = LSHIFT;
1894 goto combine;
1895 case '>':
1896 c = RSHIFT;
1897 goto combine;
1898 }
1899 else if ((c == '-') && (c1 == '>'))
1900 { value = POINTSAT; goto done; }
1901 ungetc (c1, finput);
1902 token_buffer[1] = 0;
1903
1904 if ((c == '<') || (c == '>'))
1905 value = ARITHCOMPARE;
1906 else value = c;
1907 goto done;
1908 }
1909
1910 case 0:
1911 /* Don't make yyparse think this is eof. */
1912 value = 1;
1913 break;
1914
1915 default:
1916 value = c;
1917 }
1918
1919done:
1920/* yylloc.last_line = lineno; */
1921
1922 return value;
1923}
1924
d45cf215 1925/* Sets the value of the 'yydebug' variable to VALUE.
e8bbfc4e
RK
1926 This is a function so we don't have to have YYDEBUG defined
1927 in order to build the compiler. */
1928
1929void
1930set_yydebug (value)
1931 int value;
1932{
1933#if YYDEBUG != 0
1934 yydebug = value;
1935#else
1936 warning ("YYDEBUG not defined.");
1937#endif
1938}
This page took 0.223882 seconds and 5 git commands to generate.