]> 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 {
925 case '\r':
926 if (!flag_traditional) /* ANSI says no */
927 goto found_nonwhite;
928 case ' ':
929 case '\t':
930 case '\f':
931 case '\v':
932 case '\b':
933 c = getc (finput);
934 break;
935
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
1293 if (errno == ERANGE && !flag_traditional)
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");
1324 f_seen = 1;
1325 type = float_type_node;
1326 value = REAL_VALUE_TRUNCATE (TYPE_MODE (type), value);
1327 }
1328 else if (c == 'l' || c == 'L')
1329 {
1330 if (l_seen)
1331 error ("two `l's in floating constant");
1332 l_seen = 1;
1333 type = long_double_type_node;
1334 }
1335 else
1336 {
1337 if (isalnum (c))
1338 {
1339 error ("garbage at end of number");
1340 while (isalnum (c))
1341 {
1342 if (p >= token_buffer + maxtoken - 3)
1343 p = extend_token_buffer (p);
1344 *p++ = c;
1345 c = getc (finput);
1346 }
1347 }
1348 break;
1349 }
1350 if (p >= token_buffer + maxtoken - 3)
1351 p = extend_token_buffer (p);
1352 *p++ = c;
1353 c = getc (finput);
1354 }
1355
1356 /* Create a node with determined type and value. */
1357 yylval.ttype = build_real (type, value);
1358
1359 ungetc (c, finput);
1360 *p = 0;
1361 }
1362 else
1363 {
1364 tree traditional_type, ansi_type, type;
1365 int spec_unsigned = 0;
1366 int spec_long = 0;
1367 int spec_long_long = 0;
1368 int bytes, warn, i;
1369
1370 while (1)
1371 {
1372 if (c == 'u' || c == 'U')
1373 {
1374 if (spec_unsigned)
1375 error ("two `u's in integer constant");
1376 spec_unsigned = 1;
1377 }
1378 else if (c == 'l' || c == 'L')
1379 {
1380 if (spec_long)
1381 {
1382 if (spec_long_long)
1383 error ("three `l's in integer constant");
1384 else if (pedantic)
1385 pedwarn ("ANSI C forbids long long integer constants");
1386 spec_long_long = 1;
1387 }
1388 spec_long = 1;
1389 }
1390 else
1391 {
1392 if (isalnum (c))
1393 {
1394 error ("garbage at end of number");
1395 while (isalnum (c))
1396 {
1397 if (p >= token_buffer + maxtoken - 3)
1398 p = extend_token_buffer (p);
1399 *p++ = c;
1400 c = getc (finput);
1401 }
1402 }
1403 break;
1404 }
1405 if (p >= token_buffer + maxtoken - 3)
1406 p = extend_token_buffer (p);
1407 *p++ = c;
1408 c = getc (finput);
1409 }
1410
1411 ungetc (c, finput);
1412
1413 /* If the constant is not long long and it won't fit in an
1414 unsigned long, or if the constant is long long and won't fit
1415 in an unsigned long long, then warn that the constant is out
1416 of range. */
1417
1418 /* ??? This assumes that long long and long integer types are
1419 a multiple of 8 bits. This better than the original code
1420 though which assumed that long was exactly 32 bits and long
1421 long was exactly 64 bits. */
1422
1423 if (spec_long_long)
1424 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1425 else
1426 bytes = TYPE_PRECISION (long_integer_type_node) / 8;
1427
1428 if (bytes <= 8)
1429 {
1430 warn = overflow;
1431 for (i = bytes; i < 8; i++)
1432 if (shorts[i])
1433 {
1434 /* If LL was not used, then clear any excess precision.
1435 This is equivalent to the original code, but it is
1436 not clear why this is being done. Perhaps to prevent
1437 ANSI programs from creating long long constants
1438 by accident? */
1439 if (! spec_long_long)
1440 shorts[i] = 0;
1441 warn = 1;
1442 }
1443 if (warn)
1444 warning ("integer constant out of range");
1445 }
1446 else if (overflow)
1447 warning ("integer constant larger than compiler can handle");
1448
1449 /* If it overflowed our internal buffer, then make it unsigned.
1450 We can't distinguish based on the tree node because
1451 any integer constant fits any long long type. */
1452 if (overflow)
1453 spec_unsigned = 1;
1454
1455 /* This is simplified by the fact that our constant
1456 is always positive. */
1457 /* The casts in the following statement should not be
1458 needed, but they get around bugs in some C compilers. */
1459 yylval.ttype
1460 = (build_int_2
1461 ((((long)shorts[3]<<24) + ((long)shorts[2]<<16)
1462 + ((long)shorts[1]<<8) + (long)shorts[0]),
1463 (((long)shorts[7]<<24) + ((long)shorts[6]<<16)
1464 + ((long)shorts[5]<<8) + (long)shorts[4])));
1465
1466#if 0
1467 /* Find the first allowable type that the value fits in. */
1468 type = 0;
1469 for (i = 0; i < sizeof (type_sequence) / sizeof (type_sequence[0]);
1470 i++)
1471 if (!(spec_long && !type_sequence[i].long_flag)
1472 && !(spec_long_long && !type_sequence[i].long_long_flag)
1473 && !(spec_unsigned && !type_sequence[i].unsigned_flag)
1474 /* A decimal constant can't be unsigned int
1475 unless explicitly specified. */
1476 && !(base == 10 && !spec_unsigned
1477 && *type_sequence[i].node_var == unsigned_type_node))
1478 if (int_fits_type_p (yylval.ttype, *type_sequence[i].node_var))
1479 {
1480 type = *type_sequence[i].node_var;
1481 break;
1482 }
1483 if (flag_traditional && type == long_unsigned_type_node
1484 && !spec_unsigned)
1485 type = long_integer_type_node;
1486
1487 if (type == 0)
1488 {
1489 type = long_long_integer_type_node;
1490 warning ("integer constant out of range");
1491 }
1492
1493 /* Warn about some cases where the type of a given constant
1494 changes from traditional C to ANSI C. */
1495 if (warn_traditional)
1496 {
1497 tree other_type = 0;
1498
1499 /* This computation is the same as the previous one
1500 except that flag_traditional is used backwards. */
1501 for (i = 0; i < sizeof (type_sequence) / sizeof (type_sequence[0]);
1502 i++)
1503 if (!(spec_long && !type_sequence[i].long_flag)
1504 && !(spec_long_long && !type_sequence[i].long_long_flag)
1505 && !(spec_unsigned && !type_sequence[i].unsigned_flag)
1506 /* A decimal constant can't be unsigned int
1507 unless explicitly specified. */
1508 && !(base == 10 && !spec_unsigned
1509 && *type_sequence[i].node_var == unsigned_type_node))
1510 if (int_fits_type_p (yylval.ttype, *type_sequence[i].node_var))
1511 {
1512 other_type = *type_sequence[i].node_var;
1513 break;
1514 }
1515 if (!flag_traditional && type == long_unsigned_type_node
1516 && !spec_unsigned)
1517 type = long_integer_type_node;
1518
1519 if (other_type != 0 && other_type != type)
1520 {
1521 if (flag_traditional)
1522 warning ("type of integer constant would be different without -traditional");
1523 else
1524 warning ("type of integer constant would be different with -traditional");
1525 }
1526 }
1527
1528#else /* 1 */
1529 /* If warn_traditional, calculate both the ANSI type and the
1530 traditional type, then see if they disagree.
1531 Otherwise, calculate only the type for the dialect in use. */
1532 if (warn_traditional || flag_traditional)
1533 {
1534 /* Calculate the traditional type. */
1535 /* Traditionally, any constant is signed;
1536 but if unsigned is specified explicitly, obey that.
1537 Use the smallest size with the right number of bits,
1538 except for one special case with decimal constants. */
1539 if (! spec_long && base != 10
1540 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1541 traditional_type = (spec_unsigned ? unsigned_type_node
1542 : integer_type_node);
1543 /* A decimal constant must be long
1544 if it does not fit in type int.
1545 I think this is independent of whether
1546 the constant is signed. */
1547 else if (! spec_long && base == 10
1548 && int_fits_type_p (yylval.ttype, integer_type_node))
1549 traditional_type = (spec_unsigned ? unsigned_type_node
1550 : integer_type_node);
1551 else if (! spec_long_long
1552 && int_fits_type_p (yylval.ttype,
1553 long_unsigned_type_node))
1554 traditional_type = (spec_unsigned ? long_unsigned_type_node
1555 : long_integer_type_node);
1556 else
1557 traditional_type = (spec_unsigned
1558 ? long_long_unsigned_type_node
1559 : long_long_integer_type_node);
1560 }
1561 if (warn_traditional || ! flag_traditional)
1562 {
1563 /* Calculate the ANSI type. */
1564 if (! spec_long && ! spec_unsigned
1565 && int_fits_type_p (yylval.ttype, integer_type_node))
1566 ansi_type = integer_type_node;
1567 else if (! spec_long && (base != 10 || spec_unsigned)
1568 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1569 ansi_type = unsigned_type_node;
1570 else if (! spec_unsigned && !spec_long_long
1571 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1572 ansi_type = long_integer_type_node;
1573 else if (! spec_long_long
1574 && int_fits_type_p (yylval.ttype,
1575 long_unsigned_type_node))
1576 ansi_type = long_unsigned_type_node;
1577 else if (! spec_unsigned
1578 && int_fits_type_p (yylval.ttype,
1579 long_long_integer_type_node))
1580 ansi_type = long_long_integer_type_node;
1581 else
1582 ansi_type = long_long_unsigned_type_node;
1583 }
1584
1585 type = flag_traditional ? traditional_type : ansi_type;
1586
1587 if (warn_traditional && traditional_type != ansi_type)
1588 {
1589 if (TYPE_PRECISION (traditional_type)
1590 != TYPE_PRECISION (ansi_type))
1591 warning ("width of integer constant changes with -traditional");
1592 else if (TREE_UNSIGNED (traditional_type)
1593 != TREE_UNSIGNED (ansi_type))
1594 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
a3ee5899
JW
1595 else
1596 warning ("width of integer constant may change on other systems with -traditional");
e8bbfc4e
RK
1597 }
1598#endif
1599
1600 TREE_TYPE (yylval.ttype) = type;
1601 *p = 0;
1602 }
1603
1604 value = CONSTANT; break;
1605 }
1606
1607 case '\'':
1608 char_constant:
1609 {
1610 register int result = 0;
1611 register num_chars = 0;
1612 unsigned width = TYPE_PRECISION (char_type_node);
1613 int max_chars;
1614
1615 if (wide_flag)
1616 {
1617 width = WCHAR_TYPE_SIZE;
1618#ifdef MULTIBYTE_CHARS
1619 max_chars = MB_CUR_MAX;
1620#else
1621 max_chars = 1;
1622#endif
1623 }
1624 else
1625 max_chars = TYPE_PRECISION (integer_type_node) / width;
1626
1627 while (1)
1628 {
1629 tryagain:
1630
1631 c = getc (finput);
1632
1633 if (c == '\'' || c == EOF)
1634 break;
1635
1636 if (c == '\\')
1637 {
1638 c = readescape ();
1639 if (c < 0)
1640 goto tryagain;
1641 if (width < HOST_BITS_PER_INT
1642 && (unsigned) c >= (1 << width))
1643 pedwarn ("escape sequence out of range for character");
1644 }
1645 else if (c == '\n')
1646 {
1647 if (pedantic)
1648 pedwarn ("ANSI C forbids newline in character constant");
1649 lineno++;
1650 }
1651
1652 num_chars++;
1653 if (num_chars > maxtoken - 4)
1654 extend_token_buffer (token_buffer);
1655
1656 token_buffer[num_chars] = c;
1657
1658 /* Merge character into result; ignore excess chars. */
1659 if (num_chars < max_chars + 1)
1660 {
1661 if (width < HOST_BITS_PER_INT)
1662 result = (result << width) | (c & ((1 << width) - 1));
1663 else
1664 result = c;
1665 }
1666 }
1667
1668 token_buffer[num_chars + 1] = '\'';
1669 token_buffer[num_chars + 2] = 0;
1670
1671 if (c != '\'')
1672 error ("malformatted character constant");
1673 else if (num_chars == 0)
1674 error ("empty character constant");
1675 else if (num_chars > max_chars)
1676 {
1677 num_chars = max_chars;
1678 error ("character constant too long");
1679 }
1680 else if (num_chars != 1 && ! flag_traditional)
1681 warning ("multi-character character constant");
1682
1683 /* If char type is signed, sign-extend the constant. */
1684 if (! wide_flag)
1685 {
1686 int num_bits = num_chars * width;
1687 if (TREE_UNSIGNED (char_type_node)
1688 || ((result >> (num_bits - 1)) & 1) == 0)
1689 yylval.ttype
1690 = build_int_2 (result & ((unsigned) ~0
1691 >> (HOST_BITS_PER_INT - num_bits)),
1692 0);
1693 else
1694 yylval.ttype
1695 = build_int_2 (result | ~((unsigned) ~0
1696 >> (HOST_BITS_PER_INT - num_bits)),
1697 -1);
1698 }
1699 else
1700 {
1701#ifdef MULTIBYTE_CHARS
1702 /* Set the initial shift state and convert the next sequence. */
1703 result = 0;
1704 /* In all locales L'\0' is zero and mbtowc will return zero,
1705 so don't use it. */
1706 if (num_chars > 1
1707 || (num_chars == 1 && token_buffer[1] != '\0'))
1708 {
1709 wchar_t wc;
1710 (void) mbtowc (NULL, NULL, 0);
1711 if (mbtowc (& wc, token_buffer + 1, num_chars) == num_chars)
1712 result = wc;
1713 else
1714 warning ("Ignoring invalid multibyte character");
1715 }
1716#endif
1717 yylval.ttype = build_int_2 (result, 0);
1718 }
1719
1720 TREE_TYPE (yylval.ttype) = integer_type_node;
1721 value = CONSTANT;
1722 break;
1723 }
1724
1725 case '"':
1726 string_constant:
1727 {
1728 c = getc (finput);
1729 p = token_buffer + 1;
1730
1731 while (c != '"' && c >= 0)
1732 {
1733 /* ignore_escape_flag is set for reading the filename in #line. */
1734 if (!ignore_escape_flag && c == '\\')
1735 {
1736 c = readescape ();
1737 if (c < 0)
1738 goto skipnewline;
1739 if (!wide_flag
1740 && TYPE_PRECISION (char_type_node) < HOST_BITS_PER_INT
1741 && c >= (1 << TYPE_PRECISION (char_type_node)))
1742 pedwarn ("escape sequence out of range for character");
1743 }
1744 else if (c == '\n')
1745 {
1746 if (pedantic)
1747 pedwarn ("ANSI C forbids newline in string constant");
1748 lineno++;
1749 }
1750
1751 if (p == token_buffer + maxtoken)
1752 p = extend_token_buffer (p);
1753 *p++ = c;
1754
1755 skipnewline:
1756 c = getc (finput);
1757 }
1758 *p = 0;
1759
1760 /* We have read the entire constant.
1761 Construct a STRING_CST for the result. */
1762
1763 if (wide_flag)
1764 {
1765 /* If this is a L"..." wide-string, convert the multibyte string
1766 to a wide character string. */
1767 char *widep = (char *) alloca ((p - token_buffer) * WCHAR_BYTES);
1768 int len;
1769
1770#ifdef MULTIBYTE_CHARS
1771 len = mbstowcs ((wchar_t *) widep, token_buffer + 1, p - token_buffer);
1772 if ((unsigned) len >= (p - token_buffer))
1773 {
1774 warning ("Ignoring invalid multibyte string");
1775 len = 0;
1776 }
1777 bzero (widep + (len * WCHAR_BYTES), WCHAR_BYTES);
1778#else
1779 {
1780 union { long l; char c[sizeof (long)]; } u;
1781 int big_endian;
1782 char *wp, *cp;
1783
1784 /* Determine whether host is little or big endian. */
1785 u.l = 1;
1786 big_endian = u.c[sizeof (long) - 1];
1787 wp = widep + (big_endian ? WCHAR_BYTES - 1 : 0);
1788
1789 bzero (widep, (p - token_buffer) * WCHAR_BYTES);
1790 for (cp = token_buffer + 1; cp < p; cp++)
1791 *wp = *cp, wp += WCHAR_BYTES;
1792 len = p - token_buffer - 1;
1793 }
1794#endif
1795 yylval.ttype = build_string ((len + 1) * WCHAR_BYTES, widep);
1796 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
1797 }
1798 else
1799 {
1800 yylval.ttype = build_string (p - token_buffer, token_buffer + 1);
1801 TREE_TYPE (yylval.ttype) = char_array_type_node;
1802 }
1803
1804 *p++ = '"';
1805 *p = 0;
1806
1807 value = STRING; break;
1808 }
1809
1810 case '+':
1811 case '-':
1812 case '&':
1813 case '|':
1814 case '<':
1815 case '>':
1816 case '*':
1817 case '/':
1818 case '%':
1819 case '^':
1820 case '!':
1821 case '=':
1822 {
1823 register int c1;
1824
1825 combine:
1826
1827 switch (c)
1828 {
1829 case '+':
1830 yylval.code = PLUS_EXPR; break;
1831 case '-':
1832 yylval.code = MINUS_EXPR; break;
1833 case '&':
1834 yylval.code = BIT_AND_EXPR; break;
1835 case '|':
1836 yylval.code = BIT_IOR_EXPR; break;
1837 case '*':
1838 yylval.code = MULT_EXPR; break;
1839 case '/':
1840 yylval.code = TRUNC_DIV_EXPR; break;
1841 case '%':
1842 yylval.code = TRUNC_MOD_EXPR; break;
1843 case '^':
1844 yylval.code = BIT_XOR_EXPR; break;
1845 case LSHIFT:
1846 yylval.code = LSHIFT_EXPR; break;
1847 case RSHIFT:
1848 yylval.code = RSHIFT_EXPR; break;
1849 case '<':
1850 yylval.code = LT_EXPR; break;
1851 case '>':
1852 yylval.code = GT_EXPR; break;
1853 }
1854
1855 token_buffer[1] = c1 = getc (finput);
1856 token_buffer[2] = 0;
1857
1858 if (c1 == '=')
1859 {
1860 switch (c)
1861 {
1862 case '<':
1863 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
1864 case '>':
1865 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
1866 case '!':
1867 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
1868 case '=':
1869 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
1870 }
1871 value = ASSIGN; goto done;
1872 }
1873 else if (c == c1)
1874 switch (c)
1875 {
1876 case '+':
1877 value = PLUSPLUS; goto done;
1878 case '-':
1879 value = MINUSMINUS; goto done;
1880 case '&':
1881 value = ANDAND; goto done;
1882 case '|':
1883 value = OROR; goto done;
1884 case '<':
1885 c = LSHIFT;
1886 goto combine;
1887 case '>':
1888 c = RSHIFT;
1889 goto combine;
1890 }
1891 else if ((c == '-') && (c1 == '>'))
1892 { value = POINTSAT; goto done; }
1893 ungetc (c1, finput);
1894 token_buffer[1] = 0;
1895
1896 if ((c == '<') || (c == '>'))
1897 value = ARITHCOMPARE;
1898 else value = c;
1899 goto done;
1900 }
1901
1902 case 0:
1903 /* Don't make yyparse think this is eof. */
1904 value = 1;
1905 break;
1906
1907 default:
1908 value = c;
1909 }
1910
1911done:
1912/* yylloc.last_line = lineno; */
1913
1914 return value;
1915}
1916
d45cf215 1917/* Sets the value of the 'yydebug' variable to VALUE.
e8bbfc4e
RK
1918 This is a function so we don't have to have YYDEBUG defined
1919 in order to build the compiler. */
1920
1921void
1922set_yydebug (value)
1923 int value;
1924{
1925#if YYDEBUG != 0
1926 yydebug = value;
1927#else
1928 warning ("YYDEBUG not defined.");
1929#endif
1930}
This page took 0.208539 seconds and 5 git commands to generate.