]> 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;
1072 else if (doing_objc_thang)
1073 {
1074 tree objc_interface_decl = lookup_interface (yylval.ttype);
1075
1076 if (objc_interface_decl)
1077 {
1078 value = CLASSNAME;
1079 yylval.ttype = objc_interface_decl;
1080 }
1081 }
1082 }
1083
1084 break;
1085
1086 case '0': case '1': case '2': case '3': case '4':
1087 case '5': case '6': case '7': case '8': case '9':
1088 case '.':
1089 {
1090 int base = 10;
1091 int count = 0;
1092 int largest_digit = 0;
1093 int numdigits = 0;
1094 /* for multi-precision arithmetic,
1095 we store only 8 live bits in each short,
1096 giving us 64 bits of reliable precision */
1097 short shorts[8];
1098 int overflow = 0;
1099
1100 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS} floatflag
1101 = NOT_FLOAT;
1102
1103 for (count = 0; count < 8; count++)
1104 shorts[count] = 0;
1105
1106 p = token_buffer;
1107 *p++ = c;
1108
1109 if (c == '0')
1110 {
1111 *p++ = (c = getc (finput));
1112 if ((c == 'x') || (c == 'X'))
1113 {
1114 base = 16;
1115 *p++ = (c = getc (finput));
1116 }
1117 /* Leading 0 forces octal unless the 0 is the only digit. */
1118 else if (c >= '0' && c <= '9')
1119 {
1120 base = 8;
1121 numdigits++;
1122 }
1123 else
1124 numdigits++;
1125 }
1126
1127 /* Read all the digits-and-decimal-points. */
1128
1129 while (c == '.'
1130 || (isalnum (c) && (c != 'l') && (c != 'L')
1131 && (c != 'u') && (c != 'U')
1132 && (floatflag == NOT_FLOAT || ((c != 'f') && (c != 'F')))))
1133 {
1134 if (c == '.')
1135 {
1136 if (base == 16)
1137 error ("floating constant may not be in radix 16");
1138 if (floatflag == AFTER_POINT)
1139 {
1140 error ("malformed floating constant");
1141 floatflag = TOO_MANY_POINTS;
1142 }
1143 else
1144 floatflag = AFTER_POINT;
1145
1146 base = 10;
1147 *p++ = c = getc (finput);
1148 /* Accept '.' as the start of a floating-point number
1149 only when it is followed by a digit.
1150 Otherwise, unread the following non-digit
1151 and use the '.' as a structural token. */
1152 if (p == token_buffer + 2 && !isdigit (c))
1153 {
1154 if (c == '.')
1155 {
1156 c = getc (finput);
1157 if (c == '.')
1158 {
1159 *p++ = c;
1160 *p = 0;
1161 return ELLIPSIS;
1162 }
1163 error ("parse error at `..'");
1164 }
1165 ungetc (c, finput);
1166 token_buffer[1] = 0;
1167 value = '.';
1168 goto done;
1169 }
1170 }
1171 else
1172 {
1173 /* It is not a decimal point.
1174 It should be a digit (perhaps a hex digit). */
1175
1176 if (isdigit (c))
1177 {
1178 c = c - '0';
1179 }
1180 else if (base <= 10)
1181 {
1182 if ((c&~040) == 'E')
1183 {
1184 base = 10;
1185 floatflag = AFTER_POINT;
1186 break; /* start of exponent */
1187 }
1188 error ("nondigits in number and not hexadecimal");
1189 c = 0;
1190 }
1191 else if (c >= 'a')
1192 {
1193 c = c - 'a' + 10;
1194 }
1195 else
1196 {
1197 c = c - 'A' + 10;
1198 }
1199 if (c >= largest_digit)
1200 largest_digit = c;
1201 numdigits++;
1202
1203 for (count = 0; count < 8; count++)
1204 {
1205 shorts[count] *= base;
1206 if (count)
1207 {
1208 shorts[count] += (shorts[count-1] >> 8);
1209 shorts[count-1] &= (1<<8)-1;
1210 }
1211 else shorts[0] += c;
1212 }
1213
1214 if (shorts[7] >= 1<<8
1215 || shorts[7] < - (1 << 8))
1216 overflow = TRUE;
1217
1218 if (p >= token_buffer + maxtoken - 3)
1219 p = extend_token_buffer (p);
1220 *p++ = (c = getc (finput));
1221 }
1222 }
1223
1224 if (numdigits == 0)
1225 error ("numeric constant with no digits");
1226
1227 if (largest_digit >= base)
1228 error ("numeric constant contains digits beyond the radix");
1229
1230 /* Remove terminating char from the token buffer and delimit the string */
1231 *--p = 0;
1232
1233 if (floatflag != NOT_FLOAT)
1234 {
1235 tree type = double_type_node;
1236 char f_seen = 0;
1237 char l_seen = 0;
1238 REAL_VALUE_TYPE value;
1239 jmp_buf handler;
1240
1241 /* Read explicit exponent if any, and put it in tokenbuf. */
1242
1243 if ((c == 'e') || (c == 'E'))
1244 {
1245 if (p >= token_buffer + maxtoken - 3)
1246 p = extend_token_buffer (p);
1247 *p++ = c;
1248 c = getc (finput);
1249 if ((c == '+') || (c == '-'))
1250 {
1251 *p++ = c;
1252 c = getc (finput);
1253 }
1254 if (! isdigit (c))
1255 error ("floating constant exponent has no digits");
1256 while (isdigit (c))
1257 {
1258 if (p >= token_buffer + maxtoken - 3)
1259 p = extend_token_buffer (p);
1260 *p++ = c;
1261 c = getc (finput);
1262 }
1263 }
1264
1265 *p = 0;
1266 errno = 0;
1267
1268 /* Convert string to a double, checking for overflow. */
1269 if (setjmp (handler))
1270 {
1271 error ("floating constant out of range");
1272 value = dconst0;
1273 }
1274 else
1275 {
1276 set_float_handler (handler);
1277 value = REAL_VALUE_ATOF (token_buffer);
1278 set_float_handler (0);
1279 }
1280#ifdef ERANGE
1281 if (errno == ERANGE && !flag_traditional)
1282 {
1283 char *p1 = token_buffer;
1284 /* Check for "0.0" and variants;
1285 Sunos 4 spuriously returns ERANGE for them. */
1286 while (*p1 == '0') p1++;
1287 if (*p1 == '.')
1288 {
1289 p1++;
1290 while (*p1 == '0') p1++;
1291 }
1292 if (*p1 == 'e' || *p1 == 'E')
1293 {
1294 /* with significand==0, ignore the exponent */
1295 p1++;
1296 while (*p1 != 0) p1++;
1297 }
1298 /* ERANGE is also reported for underflow,
1299 so test the value to distinguish overflow from that. */
1300 if (*p1 != 0 && (value > 1.0 || value < -1.0))
1301 warning ("floating point number exceeds range of `double'");
1302 }
1303#endif
1304
1305 /* Read the suffixes to choose a data type. */
1306 while (1)
1307 {
1308 if (c == 'f' || c == 'F')
1309 {
1310 if (f_seen)
1311 error ("two `f's in floating constant");
1312 f_seen = 1;
1313 type = float_type_node;
1314 value = REAL_VALUE_TRUNCATE (TYPE_MODE (type), value);
1315 }
1316 else if (c == 'l' || c == 'L')
1317 {
1318 if (l_seen)
1319 error ("two `l's in floating constant");
1320 l_seen = 1;
1321 type = long_double_type_node;
1322 }
1323 else
1324 {
1325 if (isalnum (c))
1326 {
1327 error ("garbage at end of number");
1328 while (isalnum (c))
1329 {
1330 if (p >= token_buffer + maxtoken - 3)
1331 p = extend_token_buffer (p);
1332 *p++ = c;
1333 c = getc (finput);
1334 }
1335 }
1336 break;
1337 }
1338 if (p >= token_buffer + maxtoken - 3)
1339 p = extend_token_buffer (p);
1340 *p++ = c;
1341 c = getc (finput);
1342 }
1343
1344 /* Create a node with determined type and value. */
1345 yylval.ttype = build_real (type, value);
1346
1347 ungetc (c, finput);
1348 *p = 0;
1349 }
1350 else
1351 {
1352 tree traditional_type, ansi_type, type;
1353 int spec_unsigned = 0;
1354 int spec_long = 0;
1355 int spec_long_long = 0;
1356 int bytes, warn, i;
1357
1358 while (1)
1359 {
1360 if (c == 'u' || c == 'U')
1361 {
1362 if (spec_unsigned)
1363 error ("two `u's in integer constant");
1364 spec_unsigned = 1;
1365 }
1366 else if (c == 'l' || c == 'L')
1367 {
1368 if (spec_long)
1369 {
1370 if (spec_long_long)
1371 error ("three `l's in integer constant");
1372 else if (pedantic)
1373 pedwarn ("ANSI C forbids long long integer constants");
1374 spec_long_long = 1;
1375 }
1376 spec_long = 1;
1377 }
1378 else
1379 {
1380 if (isalnum (c))
1381 {
1382 error ("garbage at end of number");
1383 while (isalnum (c))
1384 {
1385 if (p >= token_buffer + maxtoken - 3)
1386 p = extend_token_buffer (p);
1387 *p++ = c;
1388 c = getc (finput);
1389 }
1390 }
1391 break;
1392 }
1393 if (p >= token_buffer + maxtoken - 3)
1394 p = extend_token_buffer (p);
1395 *p++ = c;
1396 c = getc (finput);
1397 }
1398
1399 ungetc (c, finput);
1400
1401 /* If the constant is not long long and it won't fit in an
1402 unsigned long, or if the constant is long long and won't fit
1403 in an unsigned long long, then warn that the constant is out
1404 of range. */
1405
1406 /* ??? This assumes that long long and long integer types are
1407 a multiple of 8 bits. This better than the original code
1408 though which assumed that long was exactly 32 bits and long
1409 long was exactly 64 bits. */
1410
1411 if (spec_long_long)
1412 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1413 else
1414 bytes = TYPE_PRECISION (long_integer_type_node) / 8;
1415
1416 if (bytes <= 8)
1417 {
1418 warn = overflow;
1419 for (i = bytes; i < 8; i++)
1420 if (shorts[i])
1421 {
1422 /* If LL was not used, then clear any excess precision.
1423 This is equivalent to the original code, but it is
1424 not clear why this is being done. Perhaps to prevent
1425 ANSI programs from creating long long constants
1426 by accident? */
1427 if (! spec_long_long)
1428 shorts[i] = 0;
1429 warn = 1;
1430 }
1431 if (warn)
1432 warning ("integer constant out of range");
1433 }
1434 else if (overflow)
1435 warning ("integer constant larger than compiler can handle");
1436
1437 /* If it overflowed our internal buffer, then make it unsigned.
1438 We can't distinguish based on the tree node because
1439 any integer constant fits any long long type. */
1440 if (overflow)
1441 spec_unsigned = 1;
1442
1443 /* This is simplified by the fact that our constant
1444 is always positive. */
1445 /* The casts in the following statement should not be
1446 needed, but they get around bugs in some C compilers. */
1447 yylval.ttype
1448 = (build_int_2
1449 ((((long)shorts[3]<<24) + ((long)shorts[2]<<16)
1450 + ((long)shorts[1]<<8) + (long)shorts[0]),
1451 (((long)shorts[7]<<24) + ((long)shorts[6]<<16)
1452 + ((long)shorts[5]<<8) + (long)shorts[4])));
1453
1454#if 0
1455 /* Find the first allowable type that the value fits in. */
1456 type = 0;
1457 for (i = 0; i < sizeof (type_sequence) / sizeof (type_sequence[0]);
1458 i++)
1459 if (!(spec_long && !type_sequence[i].long_flag)
1460 && !(spec_long_long && !type_sequence[i].long_long_flag)
1461 && !(spec_unsigned && !type_sequence[i].unsigned_flag)
1462 /* A decimal constant can't be unsigned int
1463 unless explicitly specified. */
1464 && !(base == 10 && !spec_unsigned
1465 && *type_sequence[i].node_var == unsigned_type_node))
1466 if (int_fits_type_p (yylval.ttype, *type_sequence[i].node_var))
1467 {
1468 type = *type_sequence[i].node_var;
1469 break;
1470 }
1471 if (flag_traditional && type == long_unsigned_type_node
1472 && !spec_unsigned)
1473 type = long_integer_type_node;
1474
1475 if (type == 0)
1476 {
1477 type = long_long_integer_type_node;
1478 warning ("integer constant out of range");
1479 }
1480
1481 /* Warn about some cases where the type of a given constant
1482 changes from traditional C to ANSI C. */
1483 if (warn_traditional)
1484 {
1485 tree other_type = 0;
1486
1487 /* This computation is the same as the previous one
1488 except that flag_traditional is used backwards. */
1489 for (i = 0; i < sizeof (type_sequence) / sizeof (type_sequence[0]);
1490 i++)
1491 if (!(spec_long && !type_sequence[i].long_flag)
1492 && !(spec_long_long && !type_sequence[i].long_long_flag)
1493 && !(spec_unsigned && !type_sequence[i].unsigned_flag)
1494 /* A decimal constant can't be unsigned int
1495 unless explicitly specified. */
1496 && !(base == 10 && !spec_unsigned
1497 && *type_sequence[i].node_var == unsigned_type_node))
1498 if (int_fits_type_p (yylval.ttype, *type_sequence[i].node_var))
1499 {
1500 other_type = *type_sequence[i].node_var;
1501 break;
1502 }
1503 if (!flag_traditional && type == long_unsigned_type_node
1504 && !spec_unsigned)
1505 type = long_integer_type_node;
1506
1507 if (other_type != 0 && other_type != type)
1508 {
1509 if (flag_traditional)
1510 warning ("type of integer constant would be different without -traditional");
1511 else
1512 warning ("type of integer constant would be different with -traditional");
1513 }
1514 }
1515
1516#else /* 1 */
1517 /* If warn_traditional, calculate both the ANSI type and the
1518 traditional type, then see if they disagree.
1519 Otherwise, calculate only the type for the dialect in use. */
1520 if (warn_traditional || flag_traditional)
1521 {
1522 /* Calculate the traditional type. */
1523 /* Traditionally, any constant is signed;
1524 but if unsigned is specified explicitly, obey that.
1525 Use the smallest size with the right number of bits,
1526 except for one special case with decimal constants. */
1527 if (! spec_long && base != 10
1528 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1529 traditional_type = (spec_unsigned ? unsigned_type_node
1530 : integer_type_node);
1531 /* A decimal constant must be long
1532 if it does not fit in type int.
1533 I think this is independent of whether
1534 the constant is signed. */
1535 else if (! spec_long && base == 10
1536 && int_fits_type_p (yylval.ttype, integer_type_node))
1537 traditional_type = (spec_unsigned ? unsigned_type_node
1538 : integer_type_node);
1539 else if (! spec_long_long
1540 && int_fits_type_p (yylval.ttype,
1541 long_unsigned_type_node))
1542 traditional_type = (spec_unsigned ? long_unsigned_type_node
1543 : long_integer_type_node);
1544 else
1545 traditional_type = (spec_unsigned
1546 ? long_long_unsigned_type_node
1547 : long_long_integer_type_node);
1548 }
1549 if (warn_traditional || ! flag_traditional)
1550 {
1551 /* Calculate the ANSI type. */
1552 if (! spec_long && ! spec_unsigned
1553 && int_fits_type_p (yylval.ttype, integer_type_node))
1554 ansi_type = integer_type_node;
1555 else if (! spec_long && (base != 10 || spec_unsigned)
1556 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1557 ansi_type = unsigned_type_node;
1558 else if (! spec_unsigned && !spec_long_long
1559 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1560 ansi_type = long_integer_type_node;
1561 else if (! spec_long_long
1562 && int_fits_type_p (yylval.ttype,
1563 long_unsigned_type_node))
1564 ansi_type = long_unsigned_type_node;
1565 else if (! spec_unsigned
1566 && int_fits_type_p (yylval.ttype,
1567 long_long_integer_type_node))
1568 ansi_type = long_long_integer_type_node;
1569 else
1570 ansi_type = long_long_unsigned_type_node;
1571 }
1572
1573 type = flag_traditional ? traditional_type : ansi_type;
1574
1575 if (warn_traditional && traditional_type != ansi_type)
1576 {
1577 if (TYPE_PRECISION (traditional_type)
1578 != TYPE_PRECISION (ansi_type))
1579 warning ("width of integer constant changes with -traditional");
1580 else if (TREE_UNSIGNED (traditional_type)
1581 != TREE_UNSIGNED (ansi_type))
1582 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
a3ee5899
JW
1583 else
1584 warning ("width of integer constant may change on other systems with -traditional");
e8bbfc4e
RK
1585 }
1586#endif
1587
1588 TREE_TYPE (yylval.ttype) = type;
1589 *p = 0;
1590 }
1591
1592 value = CONSTANT; break;
1593 }
1594
1595 case '\'':
1596 char_constant:
1597 {
1598 register int result = 0;
1599 register num_chars = 0;
1600 unsigned width = TYPE_PRECISION (char_type_node);
1601 int max_chars;
1602
1603 if (wide_flag)
1604 {
1605 width = WCHAR_TYPE_SIZE;
1606#ifdef MULTIBYTE_CHARS
1607 max_chars = MB_CUR_MAX;
1608#else
1609 max_chars = 1;
1610#endif
1611 }
1612 else
1613 max_chars = TYPE_PRECISION (integer_type_node) / width;
1614
1615 while (1)
1616 {
1617 tryagain:
1618
1619 c = getc (finput);
1620
1621 if (c == '\'' || c == EOF)
1622 break;
1623
1624 if (c == '\\')
1625 {
1626 c = readescape ();
1627 if (c < 0)
1628 goto tryagain;
1629 if (width < HOST_BITS_PER_INT
1630 && (unsigned) c >= (1 << width))
1631 pedwarn ("escape sequence out of range for character");
1632 }
1633 else if (c == '\n')
1634 {
1635 if (pedantic)
1636 pedwarn ("ANSI C forbids newline in character constant");
1637 lineno++;
1638 }
1639
1640 num_chars++;
1641 if (num_chars > maxtoken - 4)
1642 extend_token_buffer (token_buffer);
1643
1644 token_buffer[num_chars] = c;
1645
1646 /* Merge character into result; ignore excess chars. */
1647 if (num_chars < max_chars + 1)
1648 {
1649 if (width < HOST_BITS_PER_INT)
1650 result = (result << width) | (c & ((1 << width) - 1));
1651 else
1652 result = c;
1653 }
1654 }
1655
1656 token_buffer[num_chars + 1] = '\'';
1657 token_buffer[num_chars + 2] = 0;
1658
1659 if (c != '\'')
1660 error ("malformatted character constant");
1661 else if (num_chars == 0)
1662 error ("empty character constant");
1663 else if (num_chars > max_chars)
1664 {
1665 num_chars = max_chars;
1666 error ("character constant too long");
1667 }
1668 else if (num_chars != 1 && ! flag_traditional)
1669 warning ("multi-character character constant");
1670
1671 /* If char type is signed, sign-extend the constant. */
1672 if (! wide_flag)
1673 {
1674 int num_bits = num_chars * width;
1675 if (TREE_UNSIGNED (char_type_node)
1676 || ((result >> (num_bits - 1)) & 1) == 0)
1677 yylval.ttype
1678 = build_int_2 (result & ((unsigned) ~0
1679 >> (HOST_BITS_PER_INT - num_bits)),
1680 0);
1681 else
1682 yylval.ttype
1683 = build_int_2 (result | ~((unsigned) ~0
1684 >> (HOST_BITS_PER_INT - num_bits)),
1685 -1);
1686 }
1687 else
1688 {
1689#ifdef MULTIBYTE_CHARS
1690 /* Set the initial shift state and convert the next sequence. */
1691 result = 0;
1692 /* In all locales L'\0' is zero and mbtowc will return zero,
1693 so don't use it. */
1694 if (num_chars > 1
1695 || (num_chars == 1 && token_buffer[1] != '\0'))
1696 {
1697 wchar_t wc;
1698 (void) mbtowc (NULL, NULL, 0);
1699 if (mbtowc (& wc, token_buffer + 1, num_chars) == num_chars)
1700 result = wc;
1701 else
1702 warning ("Ignoring invalid multibyte character");
1703 }
1704#endif
1705 yylval.ttype = build_int_2 (result, 0);
1706 }
1707
1708 TREE_TYPE (yylval.ttype) = integer_type_node;
1709 value = CONSTANT;
1710 break;
1711 }
1712
1713 case '"':
1714 string_constant:
1715 {
1716 c = getc (finput);
1717 p = token_buffer + 1;
1718
1719 while (c != '"' && c >= 0)
1720 {
1721 /* ignore_escape_flag is set for reading the filename in #line. */
1722 if (!ignore_escape_flag && c == '\\')
1723 {
1724 c = readescape ();
1725 if (c < 0)
1726 goto skipnewline;
1727 if (!wide_flag
1728 && TYPE_PRECISION (char_type_node) < HOST_BITS_PER_INT
1729 && c >= (1 << TYPE_PRECISION (char_type_node)))
1730 pedwarn ("escape sequence out of range for character");
1731 }
1732 else if (c == '\n')
1733 {
1734 if (pedantic)
1735 pedwarn ("ANSI C forbids newline in string constant");
1736 lineno++;
1737 }
1738
1739 if (p == token_buffer + maxtoken)
1740 p = extend_token_buffer (p);
1741 *p++ = c;
1742
1743 skipnewline:
1744 c = getc (finput);
1745 }
1746 *p = 0;
1747
1748 /* We have read the entire constant.
1749 Construct a STRING_CST for the result. */
1750
1751 if (wide_flag)
1752 {
1753 /* If this is a L"..." wide-string, convert the multibyte string
1754 to a wide character string. */
1755 char *widep = (char *) alloca ((p - token_buffer) * WCHAR_BYTES);
1756 int len;
1757
1758#ifdef MULTIBYTE_CHARS
1759 len = mbstowcs ((wchar_t *) widep, token_buffer + 1, p - token_buffer);
1760 if ((unsigned) len >= (p - token_buffer))
1761 {
1762 warning ("Ignoring invalid multibyte string");
1763 len = 0;
1764 }
1765 bzero (widep + (len * WCHAR_BYTES), WCHAR_BYTES);
1766#else
1767 {
1768 union { long l; char c[sizeof (long)]; } u;
1769 int big_endian;
1770 char *wp, *cp;
1771
1772 /* Determine whether host is little or big endian. */
1773 u.l = 1;
1774 big_endian = u.c[sizeof (long) - 1];
1775 wp = widep + (big_endian ? WCHAR_BYTES - 1 : 0);
1776
1777 bzero (widep, (p - token_buffer) * WCHAR_BYTES);
1778 for (cp = token_buffer + 1; cp < p; cp++)
1779 *wp = *cp, wp += WCHAR_BYTES;
1780 len = p - token_buffer - 1;
1781 }
1782#endif
1783 yylval.ttype = build_string ((len + 1) * WCHAR_BYTES, widep);
1784 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
1785 }
1786 else
1787 {
1788 yylval.ttype = build_string (p - token_buffer, token_buffer + 1);
1789 TREE_TYPE (yylval.ttype) = char_array_type_node;
1790 }
1791
1792 *p++ = '"';
1793 *p = 0;
1794
1795 value = STRING; break;
1796 }
1797
1798 case '+':
1799 case '-':
1800 case '&':
1801 case '|':
1802 case '<':
1803 case '>':
1804 case '*':
1805 case '/':
1806 case '%':
1807 case '^':
1808 case '!':
1809 case '=':
1810 {
1811 register int c1;
1812
1813 combine:
1814
1815 switch (c)
1816 {
1817 case '+':
1818 yylval.code = PLUS_EXPR; break;
1819 case '-':
1820 yylval.code = MINUS_EXPR; break;
1821 case '&':
1822 yylval.code = BIT_AND_EXPR; break;
1823 case '|':
1824 yylval.code = BIT_IOR_EXPR; break;
1825 case '*':
1826 yylval.code = MULT_EXPR; break;
1827 case '/':
1828 yylval.code = TRUNC_DIV_EXPR; break;
1829 case '%':
1830 yylval.code = TRUNC_MOD_EXPR; break;
1831 case '^':
1832 yylval.code = BIT_XOR_EXPR; break;
1833 case LSHIFT:
1834 yylval.code = LSHIFT_EXPR; break;
1835 case RSHIFT:
1836 yylval.code = RSHIFT_EXPR; break;
1837 case '<':
1838 yylval.code = LT_EXPR; break;
1839 case '>':
1840 yylval.code = GT_EXPR; break;
1841 }
1842
1843 token_buffer[1] = c1 = getc (finput);
1844 token_buffer[2] = 0;
1845
1846 if (c1 == '=')
1847 {
1848 switch (c)
1849 {
1850 case '<':
1851 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
1852 case '>':
1853 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
1854 case '!':
1855 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
1856 case '=':
1857 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
1858 }
1859 value = ASSIGN; goto done;
1860 }
1861 else if (c == c1)
1862 switch (c)
1863 {
1864 case '+':
1865 value = PLUSPLUS; goto done;
1866 case '-':
1867 value = MINUSMINUS; goto done;
1868 case '&':
1869 value = ANDAND; goto done;
1870 case '|':
1871 value = OROR; goto done;
1872 case '<':
1873 c = LSHIFT;
1874 goto combine;
1875 case '>':
1876 c = RSHIFT;
1877 goto combine;
1878 }
1879 else if ((c == '-') && (c1 == '>'))
1880 { value = POINTSAT; goto done; }
1881 ungetc (c1, finput);
1882 token_buffer[1] = 0;
1883
1884 if ((c == '<') || (c == '>'))
1885 value = ARITHCOMPARE;
1886 else value = c;
1887 goto done;
1888 }
1889
1890 case 0:
1891 /* Don't make yyparse think this is eof. */
1892 value = 1;
1893 break;
1894
1895 default:
1896 value = c;
1897 }
1898
1899done:
1900/* yylloc.last_line = lineno; */
1901
1902 return value;
1903}
1904
d45cf215 1905/* Sets the value of the 'yydebug' variable to VALUE.
e8bbfc4e
RK
1906 This is a function so we don't have to have YYDEBUG defined
1907 in order to build the compiler. */
1908
1909void
1910set_yydebug (value)
1911 int value;
1912{
1913#if YYDEBUG != 0
1914 yydebug = value;
1915#else
1916 warning ("YYDEBUG not defined.");
1917#endif
1918}
This page took 0.217942 seconds and 5 git commands to generate.