* Make-lang.in (DEMANGLER_INSTALL_NAME, DEMANGLER_CROSS_NAME): New
[official-gcc.git] / gcc / c-lex.c
blob22e7e5f5c5c678be6f95817c10954ded55ae5d47
1 /* Lexical analyzer for C and Objective C.
2 Copyright (C) 1987, 88, 89, 92, 94-96, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
23 #include <stdio.h>
24 #include <errno.h>
25 #include <setjmp.h>
26 #if HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #else
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 #endif
37 #include "rtl.h"
38 #include "tree.h"
39 #include "input.h"
40 #include "c-lex.h"
41 #include "c-tree.h"
42 #include "flags.h"
43 #include "c-parse.h"
44 #include "c-pragma.h"
46 #include <ctype.h>
48 /* MULTIBYTE_CHARS support only works for native compilers.
49 ??? Ideally what we want is to model widechar support after
50 the current floating point support. */
51 #ifdef CROSS_COMPILE
52 #undef MULTIBYTE_CHARS
53 #endif
55 #ifdef MULTIBYTE_CHARS
56 #include <stdlib.h>
57 #include <locale.h>
58 #endif
60 #ifndef errno
61 extern int errno;
62 #endif
64 #if USE_CPPLIB
65 #include "cpplib.h"
66 cpp_reader parse_in;
67 cpp_options parse_options;
68 static enum cpp_token cpp_token;
69 #endif
71 /* The elements of `ridpointers' are identifier nodes
72 for the reserved type names and storage classes.
73 It is indexed by a RID_... value. */
74 tree ridpointers[(int) RID_MAX];
76 /* Cause the `yydebug' variable to be defined. */
77 #define YYDEBUG 1
79 #if USE_CPPLIB
80 static unsigned char *yy_cur, *yy_lim;
82 int
83 yy_get_token ()
85 for (;;)
87 parse_in.limit = parse_in.token_buffer;
88 cpp_token = cpp_get_token (&parse_in);
89 if (cpp_token == CPP_EOF)
90 return -1;
91 yy_lim = CPP_PWRITTEN (&parse_in);
92 yy_cur = parse_in.token_buffer;
93 if (yy_cur < yy_lim)
94 return *yy_cur++;
98 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
99 #define UNGETC(c) ((c), yy_cur--)
100 #else
101 #define GETC() getc (finput)
102 #define UNGETC(c) ungetc (c, finput)
103 #endif
105 /* the declaration found for the last IDENTIFIER token read in.
106 yylex must look this up to detect typedefs, which get token type TYPENAME,
107 so it is left around in case the identifier is not a typedef but is
108 used in a context which makes it a reference to a variable. */
109 tree lastiddecl;
111 /* Nonzero enables objc features. */
113 int doing_objc_thang;
115 extern tree is_class_name ();
117 extern int yydebug;
119 /* File used for outputting assembler code. */
120 extern FILE *asm_out_file;
122 #ifndef WCHAR_TYPE_SIZE
123 #ifdef INT_TYPE_SIZE
124 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
125 #else
126 #define WCHAR_TYPE_SIZE BITS_PER_WORD
127 #endif
128 #endif
130 /* Number of bytes in a wide character. */
131 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
133 static int maxtoken; /* Current nominal length of token buffer. */
134 char *token_buffer; /* Pointer to token buffer.
135 Actual allocated length is maxtoken + 2.
136 This is not static because objc-parse.y uses it. */
138 static int indent_level = 0; /* Number of { minus number of }. */
140 /* Nonzero if end-of-file has been seen on input. */
141 static int end_of_file;
143 #if !USE_CPPLIB
144 /* Buffered-back input character; faster than using ungetc. */
145 static int nextchar = -1;
146 #endif
148 #ifdef HANDLE_SYSV_PRAGMA
149 static int handle_sysv_pragma PROTO((int));
150 #endif /* HANDLE_SYSV_PRAGMA */
151 static int skip_white_space PROTO((int));
152 static char *extend_token_buffer PROTO((char *));
153 static int readescape PROTO((int *));
154 int check_newline ();
156 /* Do not insert generated code into the source, instead, include it.
157 This allows us to build gcc automatically even for targets that
158 need to add or modify the reserved keyword lists. */
159 #include "c-gperf.h"
161 /* Return something to represent absolute declarators containing a *.
162 TARGET is the absolute declarator that the * contains.
163 TYPE_QUALS is a list of modifiers such as const or volatile
164 to apply to the pointer type, represented as identifiers.
166 We return an INDIRECT_REF whose "contents" are TARGET
167 and whose type is the modifier list. */
169 tree
170 make_pointer_declarator (type_quals, target)
171 tree type_quals, target;
173 return build1 (INDIRECT_REF, type_quals, target);
176 void
177 forget_protocol_qualifiers ()
179 int i, n = sizeof wordlist / sizeof (struct resword);
181 for (i = 0; i < n; i++)
182 if ((int) wordlist[i].rid >= (int) RID_IN
183 && (int) wordlist[i].rid <= (int) RID_ONEWAY)
184 wordlist[i].name = "";
187 void
188 remember_protocol_qualifiers ()
190 int i, n = sizeof wordlist / sizeof (struct resword);
192 for (i = 0; i < n; i++)
193 if (wordlist[i].rid == RID_IN)
194 wordlist[i].name = "in";
195 else if (wordlist[i].rid == RID_OUT)
196 wordlist[i].name = "out";
197 else if (wordlist[i].rid == RID_INOUT)
198 wordlist[i].name = "inout";
199 else if (wordlist[i].rid == RID_BYCOPY)
200 wordlist[i].name = "bycopy";
201 else if (wordlist[i].rid == RID_ONEWAY)
202 wordlist[i].name = "oneway";
205 #if USE_CPPLIB
206 void
207 init_parse (filename)
208 char *filename;
210 init_lex ();
211 yy_cur = "\n";
212 yy_lim = yy_cur+1;
214 cpp_reader_init (&parse_in);
215 parse_in.data = &parse_options;
216 cpp_options_init (&parse_options);
217 cpp_handle_options (&parse_in, 0, NULL); /* FIXME */
218 parse_in.show_column = 1;
219 if (! cpp_start_read (&parse_in, filename))
220 abort ();
223 void
224 finish_parse ()
226 cpp_finish (&parse_in);
228 #endif
230 void
231 init_lex ()
233 /* Make identifier nodes long enough for the language-specific slots. */
234 set_identifier_size (sizeof (struct lang_identifier));
236 /* Start it at 0, because check_newline is called at the very beginning
237 and will increment it to 1. */
238 lineno = 0;
240 #ifdef MULTIBYTE_CHARS
241 /* Change to the native locale for multibyte conversions. */
242 setlocale (LC_CTYPE, "");
243 #endif
245 maxtoken = 40;
246 token_buffer = (char *) xmalloc (maxtoken + 2);
248 ridpointers[(int) RID_INT] = get_identifier ("int");
249 ridpointers[(int) RID_CHAR] = get_identifier ("char");
250 ridpointers[(int) RID_VOID] = get_identifier ("void");
251 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
252 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
253 ridpointers[(int) RID_SHORT] = get_identifier ("short");
254 ridpointers[(int) RID_LONG] = get_identifier ("long");
255 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
256 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
257 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
258 ridpointers[(int) RID_CONST] = get_identifier ("const");
259 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
260 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
261 ridpointers[(int) RID_STATIC] = get_identifier ("static");
262 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
263 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
264 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
265 ridpointers[(int) RID_ITERATOR] = get_identifier ("iterator");
266 ridpointers[(int) RID_COMPLEX] = get_identifier ("complex");
267 ridpointers[(int) RID_ID] = get_identifier ("id");
268 ridpointers[(int) RID_IN] = get_identifier ("in");
269 ridpointers[(int) RID_OUT] = get_identifier ("out");
270 ridpointers[(int) RID_INOUT] = get_identifier ("inout");
271 ridpointers[(int) RID_BYCOPY] = get_identifier ("bycopy");
272 ridpointers[(int) RID_ONEWAY] = get_identifier ("oneway");
273 forget_protocol_qualifiers();
275 /* Some options inhibit certain reserved words.
276 Clear those words out of the hash table so they won't be recognized. */
277 #define UNSET_RESERVED_WORD(STRING) \
278 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
279 if (s) s->name = ""; } while (0)
281 if (! doing_objc_thang)
282 UNSET_RESERVED_WORD ("id");
284 if (flag_traditional)
286 UNSET_RESERVED_WORD ("const");
287 UNSET_RESERVED_WORD ("volatile");
288 UNSET_RESERVED_WORD ("typeof");
289 UNSET_RESERVED_WORD ("signed");
290 UNSET_RESERVED_WORD ("inline");
291 UNSET_RESERVED_WORD ("iterator");
292 UNSET_RESERVED_WORD ("complex");
294 if (flag_no_asm)
296 UNSET_RESERVED_WORD ("asm");
297 UNSET_RESERVED_WORD ("typeof");
298 UNSET_RESERVED_WORD ("inline");
299 UNSET_RESERVED_WORD ("iterator");
300 UNSET_RESERVED_WORD ("complex");
304 void
305 reinit_parse_for_function ()
309 /* Function used when yydebug is set, to print a token in more detail. */
311 void
312 yyprint (file, yychar, yylval)
313 FILE *file;
314 int yychar;
315 YYSTYPE yylval;
317 tree t;
318 switch (yychar)
320 case IDENTIFIER:
321 case TYPENAME:
322 case OBJECTNAME:
323 t = yylval.ttype;
324 if (IDENTIFIER_POINTER (t))
325 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
326 break;
328 case CONSTANT:
329 t = yylval.ttype;
330 if (TREE_CODE (t) == INTEGER_CST)
331 fprintf (file,
332 #if HOST_BITS_PER_WIDE_INT == 64
333 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
334 " 0x%x%016x",
335 #else
336 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
337 " 0x%lx%016lx",
338 #else
339 " 0x%llx%016llx",
340 #endif
341 #endif
342 #else
343 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
344 " 0x%lx%08lx",
345 #else
346 " 0x%x%08x",
347 #endif
348 #endif
349 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
350 break;
354 /* Iff C is a carriage return, warn about it - if appropriate -
355 and return nonzero. */
357 whitespace_cr (c)
358 int c;
360 static int newline_warning = 0;
362 if (c == '\r')
364 /* ANSI C says the effects of a carriage return in a source file
365 are undefined. */
366 if (pedantic && !newline_warning)
368 warning ("carriage return in source file");
369 warning ("(we only warn about the first carriage return)");
370 newline_warning = 1;
372 return 1;
374 return 0;
377 /* If C is not whitespace, return C.
378 Otherwise skip whitespace and return first nonwhite char read. */
380 static int
381 skip_white_space (c)
382 register int c;
384 for (;;)
386 switch (c)
388 /* We don't recognize comments here, because
389 cpp output can include / and * consecutively as operators.
390 Also, there's no need, since cpp removes all comments. */
392 case '\n':
393 c = check_newline ();
394 break;
396 case ' ':
397 case '\t':
398 case '\f':
399 case '\v':
400 case '\b':
401 c = GETC();
402 break;
404 case '\r':
405 whitespace_cr (c);
406 c = GETC();
407 break;
409 case '\\':
410 c = GETC();
411 if (c == '\n')
412 lineno++;
413 else
414 error ("stray '\\' in program");
415 c = GETC();
416 break;
418 default:
419 return (c);
424 /* Skips all of the white space at the current location in the input file.
425 Must use and reset nextchar if it has the next character. */
427 void
428 position_after_white_space ()
430 register int c;
432 #if !USE_CPPLIB
433 if (nextchar != -1)
434 c = nextchar, nextchar = -1;
435 else
436 #endif
437 c = GETC();
439 UNGETC (skip_white_space (c));
442 /* Like skip_white_space, but don't advance beyond the end of line.
443 Moreover, we don't get passed a character to start with. */
444 static int
445 skip_white_space_on_line ()
447 register int c;
449 while (1)
451 c = GETC();
452 switch (c)
454 case '\n':
455 default:
456 break;
458 case ' ':
459 case '\t':
460 case '\f':
461 case '\v':
462 case '\b':
463 continue;
465 case '\r':
466 whitespace_cr (c);
467 continue;
469 break;
471 return c;
474 /* Make the token buffer longer, preserving the data in it.
475 P should point to just beyond the last valid character in the old buffer.
476 The value we return is a pointer to the new buffer
477 at a place corresponding to P. */
479 static char *
480 extend_token_buffer (p)
481 char *p;
483 int offset = p - token_buffer;
485 maxtoken = maxtoken * 2 + 10;
486 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
488 return token_buffer + offset;
491 #if !USE_CPPLIB
492 #define GET_DIRECTIVE_LINE() get_directive_line (finput)
493 #else /* USE_CPPLIB */
494 /* Read the rest of a #-directive from input stream FINPUT.
495 In normal use, the directive name and the white space after it
496 have already been read, so they won't be included in the result.
497 We allow for the fact that the directive line may contain
498 a newline embedded within a character or string literal which forms
499 a part of the directive.
501 The value is a string in a reusable buffer. It remains valid
502 only until the next time this function is called. */
504 static char *
505 GET_DIRECTIVE_LINE ()
507 static char *directive_buffer = NULL;
508 static unsigned buffer_length = 0;
509 register char *p;
510 register char *buffer_limit;
511 register int looking_for = 0;
512 register int char_escaped = 0;
514 if (buffer_length == 0)
516 directive_buffer = (char *)xmalloc (128);
517 buffer_length = 128;
520 buffer_limit = &directive_buffer[buffer_length];
522 for (p = directive_buffer; ; )
524 int c;
526 /* Make buffer bigger if it is full. */
527 if (p >= buffer_limit)
529 register unsigned bytes_used = (p - directive_buffer);
531 buffer_length *= 2;
532 directive_buffer
533 = (char *)xrealloc (directive_buffer, buffer_length);
534 p = &directive_buffer[bytes_used];
535 buffer_limit = &directive_buffer[buffer_length];
538 c = GETC ();
540 /* Discard initial whitespace. */
541 if ((c == ' ' || c == '\t') && p == directive_buffer)
542 continue;
544 /* Detect the end of the directive. */
545 if (c == '\n' && looking_for == 0)
547 UNGETC (c);
548 c = '\0';
551 *p++ = c;
553 if (c == 0)
554 return directive_buffer;
556 /* Handle string and character constant syntax. */
557 if (looking_for)
559 if (looking_for == c && !char_escaped)
560 looking_for = 0; /* Found terminator... stop looking. */
562 else
563 if (c == '\'' || c == '"')
564 looking_for = c; /* Don't stop buffering until we see another
565 another one of these (or an EOF). */
567 /* Handle backslash. */
568 char_escaped = (c == '\\' && ! char_escaped);
571 #endif /* USE_CPPLIB */
573 /* At the beginning of a line, increment the line number
574 and process any #-directive on this line.
575 If the line is a #-directive, read the entire line and return a newline.
576 Otherwise, return the line's first non-whitespace character. */
579 check_newline ()
581 register int c;
582 register int token;
584 lineno++;
586 /* Read first nonwhite char on the line. */
588 c = GETC();
589 while (c == ' ' || c == '\t')
590 c = GETC();
592 if (c != '#')
594 /* If not #, return it so caller will use it. */
595 return c;
598 /* Read first nonwhite char after the `#'. */
600 c = GETC();
601 while (c == ' ' || c == '\t')
602 c = GETC();
604 /* If a letter follows, then if the word here is `line', skip
605 it and ignore it; otherwise, ignore the line, with an error
606 if the word isn't `pragma', `ident', `define', or `undef'. */
608 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
610 if (c == 'p')
612 if (GETC() == 'r'
613 && GETC() == 'a'
614 && GETC() == 'g'
615 && GETC() == 'm'
616 && GETC() == 'a'
617 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'
618 || whitespace_cr (c) ))
620 while (c == ' ' || c == '\t' || whitespace_cr (c))
621 c = GETC ();
622 if (c == '\n')
623 return c;
624 #ifdef HANDLE_SYSV_PRAGMA
625 UNGETC (c);
626 token = yylex ();
627 if (token != IDENTIFIER)
628 goto skipline;
629 return handle_sysv_pragma (token);
630 #else /* !HANDLE_SYSV_PRAGMA */
631 #ifdef HANDLE_PRAGMA
632 #if !USE_CPPLIB
633 UNGETC (c);
634 token = yylex ();
635 if (token != IDENTIFIER)
636 goto skipline;
637 if (HANDLE_PRAGMA (finput, yylval.ttype))
639 c = GETC ();
640 return c;
642 #else
643 ??? do not know what to do ???;
644 #endif /* !USE_CPPLIB */
645 #endif /* HANDLE_PRAGMA */
646 #endif /* !HANDLE_SYSV_PRAGMA */
647 goto skipline;
651 else if (c == 'd')
653 if (GETC() == 'e'
654 && GETC() == 'f'
655 && GETC() == 'i'
656 && GETC() == 'n'
657 && GETC() == 'e'
658 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'))
660 if (c != '\n')
661 debug_define (lineno, GET_DIRECTIVE_LINE ());
662 goto skipline;
665 else if (c == 'u')
667 if (GETC() == 'n'
668 && GETC() == 'd'
669 && GETC() == 'e'
670 && GETC() == 'f'
671 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'))
673 if (c != '\n')
674 debug_undef (lineno, GET_DIRECTIVE_LINE ());
675 goto skipline;
678 else if (c == 'l')
680 if (GETC() == 'i'
681 && GETC() == 'n'
682 && GETC() == 'e'
683 && ((c = GETC()) == ' ' || c == '\t'))
684 goto linenum;
686 else if (c == 'i')
688 if (GETC() == 'd'
689 && GETC() == 'e'
690 && GETC() == 'n'
691 && GETC() == 't'
692 && ((c = GETC()) == ' ' || c == '\t'))
694 /* #ident. The pedantic warning is now in cccp.c. */
696 /* Here we have just seen `#ident '.
697 A string constant should follow. */
699 c = skip_white_space_on_line ();
701 /* If no argument, ignore the line. */
702 if (c == '\n')
703 return c;
705 UNGETC (c);
706 token = yylex ();
707 if (token != STRING
708 || TREE_CODE (yylval.ttype) != STRING_CST)
710 error ("invalid #ident");
711 goto skipline;
714 if (!flag_no_ident)
716 #ifdef ASM_OUTPUT_IDENT
717 ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (yylval.ttype));
718 #endif
721 /* Skip the rest of this line. */
722 goto skipline;
726 error ("undefined or invalid # directive");
727 goto skipline;
730 linenum:
731 /* Here we have either `#line' or `# <nonletter>'.
732 In either case, it should be a line number; a digit should follow. */
734 /* Can't use skip_white_space here, but must handle all whitespace
735 that is not '\n', lest we get a recursion for '\r' '\n' when
736 calling yylex. */
737 UNGETC (c);
738 c = skip_white_space_on_line ();
740 /* If the # is the only nonwhite char on the line,
741 just ignore it. Check the new newline. */
742 if (c == '\n')
743 return c;
745 /* Something follows the #; read a token. */
747 UNGETC (c);
748 token = yylex ();
750 if (token == CONSTANT
751 && TREE_CODE (yylval.ttype) == INTEGER_CST)
753 int old_lineno = lineno;
754 int used_up = 0;
755 /* subtract one, because it is the following line that
756 gets the specified number */
758 int l = TREE_INT_CST_LOW (yylval.ttype) - 1;
760 /* Is this the last nonwhite stuff on the line? */
761 c = skip_white_space_on_line ();
762 if (c == '\n')
764 /* No more: store the line number and check following line. */
765 lineno = l;
766 return c;
768 UNGETC (c);
770 /* More follows: it must be a string constant (filename). */
772 /* Read the string constant. */
773 token = yylex ();
775 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
777 error ("invalid #line");
778 goto skipline;
781 input_filename
782 = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
783 strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
784 lineno = l;
786 /* Each change of file name
787 reinitializes whether we are now in a system header. */
788 in_system_header = 0;
790 if (main_input_filename == 0)
791 main_input_filename = input_filename;
793 /* Is this the last nonwhite stuff on the line? */
794 c = skip_white_space_on_line ();
795 if (c == '\n')
797 /* Update the name in the top element of input_file_stack. */
798 if (input_file_stack)
799 input_file_stack->name = input_filename;
801 return c;
803 UNGETC (c);
805 token = yylex ();
806 used_up = 0;
808 /* `1' after file name means entering new file.
809 `2' after file name means just left a file. */
811 if (token == CONSTANT
812 && TREE_CODE (yylval.ttype) == INTEGER_CST)
814 if (TREE_INT_CST_LOW (yylval.ttype) == 1)
816 /* Pushing to a new file. */
817 struct file_stack *p
818 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
819 input_file_stack->line = old_lineno;
820 p->next = input_file_stack;
821 p->name = input_filename;
822 p->indent_level = indent_level;
823 input_file_stack = p;
824 input_file_stack_tick++;
825 debug_start_source_file (input_filename);
826 used_up = 1;
828 else if (TREE_INT_CST_LOW (yylval.ttype) == 2)
830 /* Popping out of a file. */
831 if (input_file_stack->next)
833 struct file_stack *p = input_file_stack;
834 if (indent_level != p->indent_level)
836 warning_with_file_and_line
837 (p->name, old_lineno,
838 "This file contains more `%c's than `%c's.",
839 indent_level > p->indent_level ? '{' : '}',
840 indent_level > p->indent_level ? '}' : '{');
842 input_file_stack = p->next;
843 free (p);
844 input_file_stack_tick++;
845 debug_end_source_file (input_file_stack->line);
847 else
848 error ("#-lines for entering and leaving files don't match");
850 used_up = 1;
854 /* Now that we've pushed or popped the input stack,
855 update the name in the top element. */
856 if (input_file_stack)
857 input_file_stack->name = input_filename;
859 /* If we have handled a `1' or a `2',
860 see if there is another number to read. */
861 if (used_up)
863 /* Is this the last nonwhite stuff on the line? */
864 c = skip_white_space_on_line ();
865 if (c == '\n')
866 return c;
867 UNGETC (c);
869 token = yylex ();
870 used_up = 0;
873 /* `3' after file name means this is a system header file. */
875 if (token == CONSTANT
876 && TREE_CODE (yylval.ttype) == INTEGER_CST
877 && TREE_INT_CST_LOW (yylval.ttype) == 3)
878 in_system_header = 1, used_up = 1;
880 if (used_up)
882 /* Is this the last nonwhite stuff on the line? */
883 c = skip_white_space_on_line ();
884 if (c == '\n')
885 return c;
886 UNGETC (c);
889 warning ("unrecognized text at end of #line");
891 else
892 error ("invalid #-line");
894 /* skip the rest of this line. */
895 skipline:
896 #if !USE_CPPLIB
897 if (c != '\n' && c != EOF && nextchar >= 0)
898 c = nextchar, nextchar = -1;
899 #endif
900 while (c != '\n' && c != EOF)
901 c = GETC();
902 return c;
905 #ifdef HANDLE_SYSV_PRAGMA
907 /* Handle a #pragma directive.
908 TOKEN is the token we read after `#pragma'. Processes the entire input
909 line and returns a character for the caller to reread: either \n or EOF. */
911 /* This function has to be in this file, in order to get at
912 the token types. */
914 static int
915 handle_sysv_pragma (token)
916 register int token;
918 register int c;
920 for (;;)
922 switch (token)
924 case IDENTIFIER:
925 case TYPENAME:
926 case STRING:
927 case CONSTANT:
928 handle_pragma_token (token_buffer, yylval.ttype);
929 break;
930 default:
931 handle_pragma_token (token_buffer, 0);
933 #if !USE_CPPLIB
934 if (nextchar >= 0)
935 c = nextchar, nextchar = -1;
936 else
937 #endif
938 c = GETC ();
940 while (c == ' ' || c == '\t')
941 c = GETC ();
942 if (c == '\n' || c == EOF)
944 handle_pragma_token (0, 0);
945 return c;
947 UNGETC (c);
948 token = yylex ();
952 #endif /* HANDLE_SYSV_PRAGMA */
954 #define ENDFILE -1 /* token that represents end-of-file */
956 /* Read an escape sequence, returning its equivalent as a character,
957 or store 1 in *ignore_ptr if it is backslash-newline. */
959 static int
960 readescape (ignore_ptr)
961 int *ignore_ptr;
963 register int c = GETC();
964 register int code;
965 register unsigned count;
966 unsigned firstdig = 0;
967 int nonnull;
969 switch (c)
971 case 'x':
972 if (warn_traditional)
973 warning ("the meaning of `\\x' varies with -traditional");
975 if (flag_traditional)
976 return c;
978 code = 0;
979 count = 0;
980 nonnull = 0;
981 while (1)
983 c = GETC();
984 if (!(c >= 'a' && c <= 'f')
985 && !(c >= 'A' && c <= 'F')
986 && !(c >= '0' && c <= '9'))
988 UNGETC (c);
989 break;
991 code *= 16;
992 if (c >= 'a' && c <= 'f')
993 code += c - 'a' + 10;
994 if (c >= 'A' && c <= 'F')
995 code += c - 'A' + 10;
996 if (c >= '0' && c <= '9')
997 code += c - '0';
998 if (code != 0 || count != 0)
1000 if (count == 0)
1001 firstdig = code;
1002 count++;
1004 nonnull = 1;
1006 if (! nonnull)
1007 error ("\\x used with no following hex digits");
1008 else if (count == 0)
1009 /* Digits are all 0's. Ok. */
1011 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
1012 || (count > 1
1013 && ((1 << (TYPE_PRECISION (integer_type_node) - (count - 1) * 4))
1014 <= firstdig)))
1015 pedwarn ("hex escape out of range");
1016 return code;
1018 case '0': case '1': case '2': case '3': case '4':
1019 case '5': case '6': case '7':
1020 code = 0;
1021 count = 0;
1022 while ((c <= '7') && (c >= '0') && (count++ < 3))
1024 code = (code * 8) + (c - '0');
1025 c = GETC();
1027 UNGETC (c);
1028 return code;
1030 case '\\': case '\'': case '"':
1031 return c;
1033 case '\n':
1034 lineno++;
1035 *ignore_ptr = 1;
1036 return 0;
1038 case 'n':
1039 return TARGET_NEWLINE;
1041 case 't':
1042 return TARGET_TAB;
1044 case 'r':
1045 return TARGET_CR;
1047 case 'f':
1048 return TARGET_FF;
1050 case 'b':
1051 return TARGET_BS;
1053 case 'a':
1054 if (warn_traditional)
1055 warning ("the meaning of `\\a' varies with -traditional");
1057 if (flag_traditional)
1058 return c;
1059 return TARGET_BELL;
1061 case 'v':
1062 #if 0 /* Vertical tab is present in common usage compilers. */
1063 if (flag_traditional)
1064 return c;
1065 #endif
1066 return TARGET_VT;
1068 case 'e':
1069 case 'E':
1070 if (pedantic)
1071 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
1072 return 033;
1074 case '?':
1075 return c;
1077 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
1078 case '(':
1079 case '{':
1080 case '[':
1081 /* `\%' is used to prevent SCCS from getting confused. */
1082 case '%':
1083 if (pedantic)
1084 pedwarn ("non-ANSI escape sequence `\\%c'", c);
1085 return c;
1087 if (c >= 040 && c < 0177)
1088 pedwarn ("unknown escape sequence `\\%c'", c);
1089 else
1090 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
1091 return c;
1094 void
1095 yyerror (string)
1096 char *string;
1098 char buf[200];
1100 strcpy (buf, string);
1102 /* We can't print string and character constants well
1103 because the token_buffer contains the result of processing escapes. */
1104 if (end_of_file)
1105 strcat (buf, " at end of input");
1106 else if (token_buffer[0] == 0)
1107 strcat (buf, " at null character");
1108 else if (token_buffer[0] == '"')
1109 strcat (buf, " before string constant");
1110 else if (token_buffer[0] == '\'')
1111 strcat (buf, " before character constant");
1112 else if (token_buffer[0] < 040 || (unsigned char) token_buffer[0] >= 0177)
1113 sprintf (buf + strlen (buf), " before character 0%o",
1114 (unsigned char) token_buffer[0]);
1115 else
1116 strcat (buf, " before `%s'");
1118 error (buf, token_buffer);
1121 #if 0
1123 struct try_type
1125 tree *node_var;
1126 char unsigned_flag;
1127 char long_flag;
1128 char long_long_flag;
1131 struct try_type type_sequence[] =
1133 { &integer_type_node, 0, 0, 0},
1134 { &unsigned_type_node, 1, 0, 0},
1135 { &long_integer_type_node, 0, 1, 0},
1136 { &long_unsigned_type_node, 1, 1, 0},
1137 { &long_long_integer_type_node, 0, 1, 1},
1138 { &long_long_unsigned_type_node, 1, 1, 1}
1140 #endif /* 0 */
1143 yylex ()
1145 register int c;
1146 register char *p;
1147 register int value;
1148 int wide_flag = 0;
1149 int objc_flag = 0;
1151 #if !USE_CPPLIB
1152 if (nextchar >= 0)
1153 c = nextchar, nextchar = -1;
1154 else
1155 #endif
1156 c = GETC();
1158 /* Effectively do c = skip_white_space (c)
1159 but do it faster in the usual cases. */
1160 while (1)
1161 switch (c)
1163 case ' ':
1164 case '\t':
1165 case '\f':
1166 case '\v':
1167 case '\b':
1168 c = GETC();
1169 break;
1171 case '\r':
1172 /* Call skip_white_space so we can warn if appropriate. */
1174 case '\n':
1175 case '/':
1176 case '\\':
1177 c = skip_white_space (c);
1178 default:
1179 goto found_nonwhite;
1181 found_nonwhite:
1183 token_buffer[0] = c;
1184 token_buffer[1] = 0;
1186 /* yylloc.first_line = lineno; */
1188 switch (c)
1190 case EOF:
1191 end_of_file = 1;
1192 token_buffer[0] = 0;
1193 value = ENDFILE;
1194 break;
1196 case 'L':
1197 /* Capital L may start a wide-string or wide-character constant. */
1199 register int c = GETC();
1200 if (c == '\'')
1202 wide_flag = 1;
1203 goto char_constant;
1205 if (c == '"')
1207 wide_flag = 1;
1208 goto string_constant;
1210 UNGETC (c);
1212 goto letter;
1214 case '@':
1215 if (!doing_objc_thang)
1217 value = c;
1218 break;
1220 else
1222 /* '@' may start a constant string object. */
1223 register int c = GETC ();
1224 if (c == '"')
1226 objc_flag = 1;
1227 goto string_constant;
1229 UNGETC (c);
1230 /* Fall through to treat '@' as the start of an identifier. */
1233 case 'A': case 'B': case 'C': case 'D': case 'E':
1234 case 'F': case 'G': case 'H': case 'I': case 'J':
1235 case 'K': case 'M': case 'N': case 'O':
1236 case 'P': case 'Q': case 'R': case 'S': case 'T':
1237 case 'U': case 'V': case 'W': case 'X': case 'Y':
1238 case 'Z':
1239 case 'a': case 'b': case 'c': case 'd': case 'e':
1240 case 'f': case 'g': case 'h': case 'i': case 'j':
1241 case 'k': case 'l': case 'm': case 'n': case 'o':
1242 case 'p': case 'q': case 'r': case 's': case 't':
1243 case 'u': case 'v': case 'w': case 'x': case 'y':
1244 case 'z':
1245 case '_':
1246 case '$':
1247 letter:
1248 p = token_buffer;
1249 while (isalnum (c) || c == '_' || c == '$' || c == '@')
1251 /* Make sure this char really belongs in an identifier. */
1252 if (c == '@' && ! doing_objc_thang)
1253 break;
1254 if (c == '$')
1256 if (! dollars_in_ident)
1257 error ("`$' in identifier");
1258 else if (pedantic)
1259 pedwarn ("`$' in identifier");
1262 if (p >= token_buffer + maxtoken)
1263 p = extend_token_buffer (p);
1265 *p++ = c;
1266 c = GETC();
1269 *p = 0;
1270 #if USE_CPPLIB
1271 UNGETC (c);
1272 #else
1273 nextchar = c;
1274 #endif
1276 value = IDENTIFIER;
1277 yylval.itype = 0;
1279 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1282 register struct resword *ptr;
1284 if ((ptr = is_reserved_word (token_buffer, p - token_buffer)))
1286 if (ptr->rid)
1287 yylval.ttype = ridpointers[(int) ptr->rid];
1288 value = (int) ptr->token;
1290 /* Only return OBJECTNAME if it is a typedef. */
1291 if (doing_objc_thang && value == OBJECTNAME)
1293 lastiddecl = lookup_name(yylval.ttype);
1295 if (lastiddecl == NULL_TREE
1296 || TREE_CODE (lastiddecl) != TYPE_DECL)
1297 value = IDENTIFIER;
1300 /* Even if we decided to recognize asm, still perhaps warn. */
1301 if (pedantic
1302 && (value == ASM_KEYWORD || value == TYPEOF
1303 || ptr->rid == RID_INLINE)
1304 && token_buffer[0] != '_')
1305 pedwarn ("ANSI does not permit the keyword `%s'",
1306 token_buffer);
1310 /* If we did not find a keyword, look for an identifier
1311 (or a typename). */
1313 if (value == IDENTIFIER)
1315 if (token_buffer[0] == '@')
1316 error("invalid identifier `%s'", token_buffer);
1318 yylval.ttype = get_identifier (token_buffer);
1319 lastiddecl = lookup_name (yylval.ttype);
1321 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1322 value = TYPENAME;
1323 /* A user-invisible read-only initialized variable
1324 should be replaced by its value.
1325 We handle only strings since that's the only case used in C. */
1326 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1327 && DECL_IGNORED_P (lastiddecl)
1328 && TREE_READONLY (lastiddecl)
1329 && DECL_INITIAL (lastiddecl) != 0
1330 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1332 tree stringval = DECL_INITIAL (lastiddecl);
1334 /* Copy the string value so that we won't clobber anything
1335 if we put something in the TREE_CHAIN of this one. */
1336 yylval.ttype = build_string (TREE_STRING_LENGTH (stringval),
1337 TREE_STRING_POINTER (stringval));
1338 value = STRING;
1340 else if (doing_objc_thang)
1342 tree objc_interface_decl = is_class_name (yylval.ttype);
1344 if (objc_interface_decl)
1346 value = CLASSNAME;
1347 yylval.ttype = objc_interface_decl;
1352 break;
1354 case '0': case '1':
1356 int next_c;
1357 /* Check first for common special case: single-digit 0 or 1. */
1359 next_c = GETC ();
1360 UNGETC (next_c); /* Always undo this lookahead. */
1361 if (!isalnum (next_c) && next_c != '.')
1363 token_buffer[0] = (char)c, token_buffer[1] = '\0';
1364 yylval.ttype = (c == '0') ? integer_zero_node : integer_one_node;
1365 value = CONSTANT;
1366 break;
1368 /*FALLTHRU*/
1370 case '2': case '3': case '4':
1371 case '5': case '6': case '7': case '8': case '9':
1372 case '.':
1374 int base = 10;
1375 int count = 0;
1376 int largest_digit = 0;
1377 int numdigits = 0;
1378 /* for multi-precision arithmetic,
1379 we actually store only HOST_BITS_PER_CHAR bits in each part.
1380 The number of parts is chosen so as to be sufficient to hold
1381 the enough bits to fit into the two HOST_WIDE_INTs that contain
1382 the integer value (this is always at least as many bits as are
1383 in a target `long long' value, but may be wider). */
1384 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2 + 2)
1385 int parts[TOTAL_PARTS];
1386 int overflow = 0;
1388 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS} floatflag
1389 = NOT_FLOAT;
1391 for (count = 0; count < TOTAL_PARTS; count++)
1392 parts[count] = 0;
1394 p = token_buffer;
1395 *p++ = c;
1397 if (c == '0')
1399 *p++ = (c = GETC());
1400 if ((c == 'x') || (c == 'X'))
1402 base = 16;
1403 *p++ = (c = GETC());
1405 /* Leading 0 forces octal unless the 0 is the only digit. */
1406 else if (c >= '0' && c <= '9')
1408 base = 8;
1409 numdigits++;
1411 else
1412 numdigits++;
1415 /* Read all the digits-and-decimal-points. */
1417 while (c == '.'
1418 || (isalnum (c) && c != 'l' && c != 'L'
1419 && c != 'u' && c != 'U'
1420 && c != 'i' && c != 'I' && c != 'j' && c != 'J'
1421 && (floatflag == NOT_FLOAT || ((c != 'f') && (c != 'F')))))
1423 if (c == '.')
1425 if (base == 16)
1426 error ("floating constant may not be in radix 16");
1427 if (floatflag == TOO_MANY_POINTS)
1428 /* We have already emitted an error. Don't need another. */
1430 else if (floatflag == AFTER_POINT)
1432 error ("malformed floating constant");
1433 floatflag = TOO_MANY_POINTS;
1434 /* Avoid another error from atof by forcing all characters
1435 from here on to be ignored. */
1436 p[-1] = '\0';
1438 else
1439 floatflag = AFTER_POINT;
1441 base = 10;
1442 *p++ = c = GETC();
1443 /* Accept '.' as the start of a floating-point number
1444 only when it is followed by a digit.
1445 Otherwise, unread the following non-digit
1446 and use the '.' as a structural token. */
1447 if (p == token_buffer + 2 && !isdigit (c))
1449 if (c == '.')
1451 c = GETC();
1452 if (c == '.')
1454 *p++ = c;
1455 *p = 0;
1456 return ELLIPSIS;
1458 error ("parse error at `..'");
1460 UNGETC (c);
1461 token_buffer[1] = 0;
1462 value = '.';
1463 goto done;
1466 else
1468 /* It is not a decimal point.
1469 It should be a digit (perhaps a hex digit). */
1471 if (isdigit (c))
1473 c = c - '0';
1475 else if (base <= 10)
1477 if (c == 'e' || c == 'E')
1479 base = 10;
1480 floatflag = AFTER_POINT;
1481 break; /* start of exponent */
1483 error ("nondigits in number and not hexadecimal");
1484 c = 0;
1486 else if (c >= 'a')
1488 c = c - 'a' + 10;
1490 else
1492 c = c - 'A' + 10;
1494 if (c >= largest_digit)
1495 largest_digit = c;
1496 numdigits++;
1498 for (count = 0; count < TOTAL_PARTS; count++)
1500 parts[count] *= base;
1501 if (count)
1503 parts[count]
1504 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1505 parts[count-1]
1506 &= (1 << HOST_BITS_PER_CHAR) - 1;
1508 else
1509 parts[0] += c;
1512 /* If the extra highest-order part ever gets anything in it,
1513 the number is certainly too big. */
1514 if (parts[TOTAL_PARTS - 1] != 0)
1515 overflow = 1;
1517 if (p >= token_buffer + maxtoken - 3)
1518 p = extend_token_buffer (p);
1519 *p++ = (c = GETC());
1523 if (numdigits == 0)
1524 error ("numeric constant with no digits");
1526 if (largest_digit >= base)
1527 error ("numeric constant contains digits beyond the radix");
1529 /* Remove terminating char from the token buffer and delimit the string */
1530 *--p = 0;
1532 if (floatflag != NOT_FLOAT)
1534 tree type = double_type_node;
1535 int exceeds_double = 0;
1536 int imag = 0;
1537 REAL_VALUE_TYPE value;
1538 jmp_buf handler;
1540 /* Read explicit exponent if any, and put it in tokenbuf. */
1542 if ((c == 'e') || (c == 'E'))
1544 if (p >= token_buffer + maxtoken - 3)
1545 p = extend_token_buffer (p);
1546 *p++ = c;
1547 c = GETC();
1548 if ((c == '+') || (c == '-'))
1550 *p++ = c;
1551 c = GETC();
1553 if (! isdigit (c))
1554 error ("floating constant exponent has no digits");
1555 while (isdigit (c))
1557 if (p >= token_buffer + maxtoken - 3)
1558 p = extend_token_buffer (p);
1559 *p++ = c;
1560 c = GETC();
1564 *p = 0;
1565 errno = 0;
1567 /* Convert string to a double, checking for overflow. */
1568 if (setjmp (handler))
1570 error ("floating constant out of range");
1571 value = dconst0;
1573 else
1575 int fflag = 0, lflag = 0;
1576 /* Copy token_buffer now, while it has just the number
1577 and not the suffixes; once we add `f' or `i',
1578 REAL_VALUE_ATOF may not work any more. */
1579 char *copy = (char *) alloca (p - token_buffer + 1);
1580 bcopy (token_buffer, copy, p - token_buffer + 1);
1582 set_float_handler (handler);
1584 while (1)
1586 int lose = 0;
1588 /* Read the suffixes to choose a data type. */
1589 switch (c)
1591 case 'f': case 'F':
1592 if (fflag)
1593 error ("more than one `f' in numeric constant");
1594 fflag = 1;
1595 break;
1597 case 'l': case 'L':
1598 if (lflag)
1599 error ("more than one `l' in numeric constant");
1600 lflag = 1;
1601 break;
1603 case 'i': case 'I':
1604 if (imag)
1605 error ("more than one `i' or `j' in numeric constant");
1606 else if (pedantic)
1607 pedwarn ("ANSI C forbids imaginary numeric constants");
1608 imag = 1;
1609 break;
1611 default:
1612 lose = 1;
1615 if (lose)
1616 break;
1618 if (p >= token_buffer + maxtoken - 3)
1619 p = extend_token_buffer (p);
1620 *p++ = c;
1621 *p = 0;
1622 c = GETC();
1625 /* The second argument, machine_mode, of REAL_VALUE_ATOF
1626 tells the desired precision of the binary result
1627 of decimal-to-binary conversion. */
1629 if (fflag)
1631 if (lflag)
1632 error ("both `f' and `l' in floating constant");
1634 type = float_type_node;
1635 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1636 /* A diagnostic is required here by some ANSI C testsuites.
1637 This is not pedwarn, become some people don't want
1638 an error for this. */
1639 if (REAL_VALUE_ISINF (value) && pedantic)
1640 warning ("floating point number exceeds range of `float'");
1642 else if (lflag)
1644 type = long_double_type_node;
1645 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1646 if (REAL_VALUE_ISINF (value) && pedantic)
1647 warning ("floating point number exceeds range of `long double'");
1649 else
1651 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1652 if (REAL_VALUE_ISINF (value) && pedantic)
1653 warning ("floating point number exceeds range of `double'");
1656 set_float_handler (NULL_PTR);
1658 #ifdef ERANGE
1659 if (errno == ERANGE && !flag_traditional && pedantic)
1661 /* ERANGE is also reported for underflow,
1662 so test the value to distinguish overflow from that. */
1663 if (REAL_VALUES_LESS (dconst1, value)
1664 || REAL_VALUES_LESS (value, dconstm1))
1666 warning ("floating point number exceeds range of `double'");
1667 exceeds_double = 1;
1670 #endif
1672 /* If the result is not a number, assume it must have been
1673 due to some error message above, so silently convert
1674 it to a zero. */
1675 if (REAL_VALUE_ISNAN (value))
1676 value = dconst0;
1678 /* Create a node with determined type and value. */
1679 if (imag)
1680 yylval.ttype = build_complex (NULL_TREE,
1681 convert (type, integer_zero_node),
1682 build_real (type, value));
1683 else
1684 yylval.ttype = build_real (type, value);
1686 else
1688 tree traditional_type, ansi_type, type;
1689 HOST_WIDE_INT high, low;
1690 int spec_unsigned = 0;
1691 int spec_long = 0;
1692 int spec_long_long = 0;
1693 int spec_imag = 0;
1694 int bytes, warn, i;
1696 while (1)
1698 if (c == 'u' || c == 'U')
1700 if (spec_unsigned)
1701 error ("two `u's in integer constant");
1702 spec_unsigned = 1;
1704 else if (c == 'l' || c == 'L')
1706 if (spec_long)
1708 if (spec_long_long)
1709 error ("three `l's in integer constant");
1710 else if (pedantic)
1711 pedwarn ("ANSI C forbids long long integer constants");
1712 spec_long_long = 1;
1714 spec_long = 1;
1716 else if (c == 'i' || c == 'j' || c == 'I' || c == 'J')
1718 if (spec_imag)
1719 error ("more than one `i' or `j' in numeric constant");
1720 else if (pedantic)
1721 pedwarn ("ANSI C forbids imaginary numeric constants");
1722 spec_imag = 1;
1724 else
1725 break;
1726 if (p >= token_buffer + maxtoken - 3)
1727 p = extend_token_buffer (p);
1728 *p++ = c;
1729 c = GETC();
1732 /* If the constant won't fit in an unsigned long long,
1733 then warn that the constant is out of range. */
1735 /* ??? This assumes that long long and long integer types are
1736 a multiple of 8 bits. This better than the original code
1737 though which assumed that long was exactly 32 bits and long
1738 long was exactly 64 bits. */
1740 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1742 warn = overflow;
1743 for (i = bytes; i < TOTAL_PARTS; i++)
1744 if (parts[i])
1745 warn = 1;
1746 if (warn)
1747 pedwarn ("integer constant out of range");
1749 /* This is simplified by the fact that our constant
1750 is always positive. */
1752 high = low = 0;
1754 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1756 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1757 / HOST_BITS_PER_CHAR)]
1758 << (i * HOST_BITS_PER_CHAR));
1759 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1762 yylval.ttype = build_int_2 (low, high);
1763 TREE_TYPE (yylval.ttype) = long_long_unsigned_type_node;
1765 /* If warn_traditional, calculate both the ANSI type and the
1766 traditional type, then see if they disagree.
1767 Otherwise, calculate only the type for the dialect in use. */
1768 if (warn_traditional || flag_traditional)
1770 /* Calculate the traditional type. */
1771 /* Traditionally, any constant is signed;
1772 but if unsigned is specified explicitly, obey that.
1773 Use the smallest size with the right number of bits,
1774 except for one special case with decimal constants. */
1775 if (! spec_long && base != 10
1776 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1777 traditional_type = (spec_unsigned ? unsigned_type_node
1778 : integer_type_node);
1779 /* A decimal constant must be long
1780 if it does not fit in type int.
1781 I think this is independent of whether
1782 the constant is signed. */
1783 else if (! spec_long && base == 10
1784 && int_fits_type_p (yylval.ttype, integer_type_node))
1785 traditional_type = (spec_unsigned ? unsigned_type_node
1786 : integer_type_node);
1787 else if (! spec_long_long)
1788 traditional_type = (spec_unsigned ? long_unsigned_type_node
1789 : long_integer_type_node);
1790 else
1791 traditional_type = (spec_unsigned
1792 ? long_long_unsigned_type_node
1793 : long_long_integer_type_node);
1795 if (warn_traditional || ! flag_traditional)
1797 /* Calculate the ANSI type. */
1798 if (! spec_long && ! spec_unsigned
1799 && int_fits_type_p (yylval.ttype, integer_type_node))
1800 ansi_type = integer_type_node;
1801 else if (! spec_long && (base != 10 || spec_unsigned)
1802 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1803 ansi_type = unsigned_type_node;
1804 else if (! spec_unsigned && !spec_long_long
1805 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1806 ansi_type = long_integer_type_node;
1807 else if (! spec_long_long
1808 && int_fits_type_p (yylval.ttype,
1809 long_unsigned_type_node))
1810 ansi_type = long_unsigned_type_node;
1811 else if (! spec_unsigned
1812 && int_fits_type_p (yylval.ttype,
1813 long_long_integer_type_node))
1814 ansi_type = long_long_integer_type_node;
1815 else
1816 ansi_type = long_long_unsigned_type_node;
1819 type = flag_traditional ? traditional_type : ansi_type;
1821 if (warn_traditional && traditional_type != ansi_type)
1823 if (TYPE_PRECISION (traditional_type)
1824 != TYPE_PRECISION (ansi_type))
1825 warning ("width of integer constant changes with -traditional");
1826 else if (TREE_UNSIGNED (traditional_type)
1827 != TREE_UNSIGNED (ansi_type))
1828 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
1829 else
1830 warning ("width of integer constant may change on other systems with -traditional");
1833 if (pedantic && !flag_traditional && !spec_long_long && !warn
1834 && (TYPE_PRECISION (long_integer_type_node)
1835 < TYPE_PRECISION (type)))
1836 pedwarn ("integer constant out of range");
1838 if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type))
1839 warning ("decimal constant is so large that it is unsigned");
1841 if (spec_imag)
1843 if (TYPE_PRECISION (type)
1844 <= TYPE_PRECISION (integer_type_node))
1845 yylval.ttype
1846 = build_complex (NULL_TREE, integer_zero_node,
1847 convert (integer_type_node,
1848 yylval.ttype));
1849 else
1850 error ("complex integer constant is too wide for `complex int'");
1852 else if (flag_traditional && !int_fits_type_p (yylval.ttype, type))
1853 /* The traditional constant 0x80000000 is signed
1854 but doesn't fit in the range of int.
1855 This will change it to -0x80000000, which does fit. */
1857 TREE_TYPE (yylval.ttype) = unsigned_type (type);
1858 yylval.ttype = convert (type, yylval.ttype);
1859 TREE_OVERFLOW (yylval.ttype)
1860 = TREE_CONSTANT_OVERFLOW (yylval.ttype) = 0;
1862 else
1863 TREE_TYPE (yylval.ttype) = type;
1866 UNGETC (c);
1867 *p = 0;
1869 if (isalnum (c) || c == '.' || c == '_' || c == '$'
1870 || (!flag_traditional && (c == '-' || c == '+')
1871 && (p[-1] == 'e' || p[-1] == 'E')))
1872 error ("missing white space after number `%s'", token_buffer);
1874 value = CONSTANT; break;
1877 case '\'':
1878 char_constant:
1880 register int result = 0;
1881 register int num_chars = 0;
1882 unsigned width = TYPE_PRECISION (char_type_node);
1883 int max_chars;
1885 if (wide_flag)
1887 width = WCHAR_TYPE_SIZE;
1888 #ifdef MULTIBYTE_CHARS
1889 max_chars = MB_CUR_MAX;
1890 #else
1891 max_chars = 1;
1892 #endif
1894 else
1895 max_chars = TYPE_PRECISION (integer_type_node) / width;
1897 while (1)
1899 tryagain:
1901 c = GETC();
1903 if (c == '\'' || c == EOF)
1904 break;
1906 if (c == '\\')
1908 int ignore = 0;
1909 c = readescape (&ignore);
1910 if (ignore)
1911 goto tryagain;
1912 if (width < HOST_BITS_PER_INT
1913 && (unsigned) c >= (1 << width))
1914 pedwarn ("escape sequence out of range for character");
1915 #ifdef MAP_CHARACTER
1916 if (isprint (c))
1917 c = MAP_CHARACTER (c);
1918 #endif
1920 else if (c == '\n')
1922 if (pedantic)
1923 pedwarn ("ANSI C forbids newline in character constant");
1924 lineno++;
1926 #ifdef MAP_CHARACTER
1927 else
1928 c = MAP_CHARACTER (c);
1929 #endif
1931 num_chars++;
1932 if (num_chars > maxtoken - 4)
1933 extend_token_buffer (token_buffer);
1935 token_buffer[num_chars] = c;
1937 /* Merge character into result; ignore excess chars. */
1938 if (num_chars < max_chars + 1)
1940 if (width < HOST_BITS_PER_INT)
1941 result = (result << width) | (c & ((1 << width) - 1));
1942 else
1943 result = c;
1947 token_buffer[num_chars + 1] = '\'';
1948 token_buffer[num_chars + 2] = 0;
1950 if (c != '\'')
1951 error ("malformatted character constant");
1952 else if (num_chars == 0)
1953 error ("empty character constant");
1954 else if (num_chars > max_chars)
1956 num_chars = max_chars;
1957 error ("character constant too long");
1959 else if (num_chars != 1 && ! flag_traditional)
1960 warning ("multi-character character constant");
1962 /* If char type is signed, sign-extend the constant. */
1963 if (! wide_flag)
1965 int num_bits = num_chars * width;
1966 if (num_bits == 0)
1967 /* We already got an error; avoid invalid shift. */
1968 yylval.ttype = build_int_2 (0, 0);
1969 else if (TREE_UNSIGNED (char_type_node)
1970 || ((result >> (num_bits - 1)) & 1) == 0)
1971 yylval.ttype
1972 = build_int_2 (result & (~(unsigned HOST_WIDE_INT) 0
1973 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1975 else
1976 yylval.ttype
1977 = build_int_2 (result | ~(~(unsigned HOST_WIDE_INT) 0
1978 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1979 -1);
1980 TREE_TYPE (yylval.ttype) = integer_type_node;
1982 else
1984 #ifdef MULTIBYTE_CHARS
1985 /* Set the initial shift state and convert the next sequence. */
1986 result = 0;
1987 /* In all locales L'\0' is zero and mbtowc will return zero,
1988 so don't use it. */
1989 if (num_chars > 1
1990 || (num_chars == 1 && token_buffer[1] != '\0'))
1992 wchar_t wc;
1993 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
1994 if (mbtowc (& wc, token_buffer + 1, num_chars) == num_chars)
1995 result = wc;
1996 else
1997 warning ("Ignoring invalid multibyte character");
1999 #endif
2000 yylval.ttype = build_int_2 (result, 0);
2001 TREE_TYPE (yylval.ttype) = wchar_type_node;
2004 value = CONSTANT;
2005 break;
2008 case '"':
2009 string_constant:
2011 c = GETC();
2012 p = token_buffer + 1;
2014 while (c != '"' && c >= 0)
2016 if (c == '\\')
2018 int ignore = 0;
2019 c = readescape (&ignore);
2020 if (ignore)
2021 goto skipnewline;
2022 if (!wide_flag
2023 && TYPE_PRECISION (char_type_node) < HOST_BITS_PER_INT
2024 && c >= (1 << TYPE_PRECISION (char_type_node)))
2025 pedwarn ("escape sequence out of range for character");
2027 else if (c == '\n')
2029 if (pedantic)
2030 pedwarn ("ANSI C forbids newline in string constant");
2031 lineno++;
2034 if (p == token_buffer + maxtoken)
2035 p = extend_token_buffer (p);
2036 *p++ = c;
2038 skipnewline:
2039 c = GETC();
2041 *p = 0;
2043 if (c < 0)
2044 error ("Unterminated string constant");
2046 /* We have read the entire constant.
2047 Construct a STRING_CST for the result. */
2049 if (wide_flag)
2051 /* If this is a L"..." wide-string, convert the multibyte string
2052 to a wide character string. */
2053 char *widep = (char *) alloca ((p - token_buffer) * WCHAR_BYTES);
2054 int len;
2056 #ifdef MULTIBYTE_CHARS
2057 len = mbstowcs ((wchar_t *) widep, token_buffer + 1, p - token_buffer);
2058 if (len < 0 || len >= (p - token_buffer))
2060 warning ("Ignoring invalid multibyte string");
2061 len = 0;
2063 bzero (widep + (len * WCHAR_BYTES), WCHAR_BYTES);
2064 #else
2066 char *wp, *cp;
2068 wp = widep + (BYTES_BIG_ENDIAN ? WCHAR_BYTES - 1 : 0);
2069 bzero (widep, (p - token_buffer) * WCHAR_BYTES);
2070 for (cp = token_buffer + 1; cp < p; cp++)
2071 *wp = *cp, wp += WCHAR_BYTES;
2072 len = p - token_buffer - 1;
2074 #endif
2075 yylval.ttype = build_string ((len + 1) * WCHAR_BYTES, widep);
2076 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
2077 value = STRING;
2079 else if (objc_flag)
2081 extern tree build_objc_string();
2082 /* Return an Objective-C @"..." constant string object. */
2083 yylval.ttype = build_objc_string (p - token_buffer,
2084 token_buffer + 1);
2085 TREE_TYPE (yylval.ttype) = char_array_type_node;
2086 value = OBJC_STRING;
2088 else
2090 yylval.ttype = build_string (p - token_buffer, token_buffer + 1);
2091 TREE_TYPE (yylval.ttype) = char_array_type_node;
2092 value = STRING;
2095 *p++ = '"';
2096 *p = 0;
2098 break;
2101 case '+':
2102 case '-':
2103 case '&':
2104 case '|':
2105 case ':':
2106 case '<':
2107 case '>':
2108 case '*':
2109 case '/':
2110 case '%':
2111 case '^':
2112 case '!':
2113 case '=':
2115 register int c1;
2117 combine:
2119 switch (c)
2121 case '+':
2122 yylval.code = PLUS_EXPR; break;
2123 case '-':
2124 yylval.code = MINUS_EXPR; break;
2125 case '&':
2126 yylval.code = BIT_AND_EXPR; break;
2127 case '|':
2128 yylval.code = BIT_IOR_EXPR; break;
2129 case '*':
2130 yylval.code = MULT_EXPR; break;
2131 case '/':
2132 yylval.code = TRUNC_DIV_EXPR; break;
2133 case '%':
2134 yylval.code = TRUNC_MOD_EXPR; break;
2135 case '^':
2136 yylval.code = BIT_XOR_EXPR; break;
2137 case LSHIFT:
2138 yylval.code = LSHIFT_EXPR; break;
2139 case RSHIFT:
2140 yylval.code = RSHIFT_EXPR; break;
2141 case '<':
2142 yylval.code = LT_EXPR; break;
2143 case '>':
2144 yylval.code = GT_EXPR; break;
2147 token_buffer[1] = c1 = GETC();
2148 token_buffer[2] = 0;
2150 if (c1 == '=')
2152 switch (c)
2154 case '<':
2155 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
2156 case '>':
2157 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
2158 case '!':
2159 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
2160 case '=':
2161 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
2163 value = ASSIGN; goto done;
2165 else if (c == c1)
2166 switch (c)
2168 case '+':
2169 value = PLUSPLUS; goto done;
2170 case '-':
2171 value = MINUSMINUS; goto done;
2172 case '&':
2173 value = ANDAND; goto done;
2174 case '|':
2175 value = OROR; goto done;
2176 case '<':
2177 c = LSHIFT;
2178 goto combine;
2179 case '>':
2180 c = RSHIFT;
2181 goto combine;
2183 else
2184 switch (c)
2186 case '-':
2187 if (c1 == '>')
2188 { value = POINTSAT; goto done; }
2189 break;
2190 case ':':
2191 if (c1 == '>')
2192 { value = ']'; goto done; }
2193 break;
2194 case '<':
2195 if (c1 == '%')
2196 { value = '{'; indent_level++; goto done; }
2197 if (c1 == ':')
2198 { value = '['; goto done; }
2199 break;
2200 case '%':
2201 if (c1 == '>')
2202 { value = '}'; indent_level--; goto done; }
2203 break;
2205 UNGETC (c1);
2206 token_buffer[1] = 0;
2208 if ((c == '<') || (c == '>'))
2209 value = ARITHCOMPARE;
2210 else value = c;
2211 goto done;
2214 case 0:
2215 /* Don't make yyparse think this is eof. */
2216 value = 1;
2217 break;
2219 case '{':
2220 indent_level++;
2221 value = c;
2222 break;
2224 case '}':
2225 indent_level--;
2226 value = c;
2227 break;
2229 default:
2230 value = c;
2233 done:
2234 /* yylloc.last_line = lineno; */
2236 return value;
2239 /* Sets the value of the 'yydebug' variable to VALUE.
2240 This is a function so we don't have to have YYDEBUG defined
2241 in order to build the compiler. */
2243 void
2244 set_yydebug (value)
2245 int value;
2247 #if YYDEBUG != 0
2248 yydebug = value;
2249 #else
2250 warning ("YYDEBUG not defined.");
2251 #endif