2000-08-08 Alexandre Petit-Bianco <apbianco@cygnus.com>
[official-gcc.git] / gcc / c-lex.c
blobc1b85c712a3598daf8166358866ed84a7addad56
1 /* Lexical analyzer for C and Objective C.
2 Copyright (C) 1987, 1988, 1989, 1992, 1994, 1995, 1996, 1997
3 1998, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "input.h"
28 #include "output.h"
29 #include "c-lex.h"
30 #include "c-tree.h"
31 #include "flags.h"
32 #include "c-parse.h"
33 #include "c-pragma.h"
34 #include "toplev.h"
35 #include "intl.h"
36 #include "ggc.h"
37 #include "tm_p.h"
39 /* MULTIBYTE_CHARS support only works for native compilers.
40 ??? Ideally what we want is to model widechar support after
41 the current floating point support. */
42 #ifdef CROSS_COMPILE
43 #undef MULTIBYTE_CHARS
44 #endif
46 #ifdef MULTIBYTE_CHARS
47 #include "mbchar.h"
48 #include <locale.h>
49 #endif /* MULTIBYTE_CHARS */
50 #ifndef GET_ENVIRONMENT
51 #define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ((ENV_VALUE) = getenv (ENV_NAME))
52 #endif
54 #if USE_CPPLIB
55 #include "cpplib.h"
56 extern cpp_reader parse_in;
57 extern cpp_options parse_options;
58 #else
59 /* Stream for reading from the input file. */
60 FILE *finput;
61 #endif
63 extern void yyprint PARAMS ((FILE *, int, YYSTYPE));
65 /* Cause the `yydebug' variable to be defined. */
66 #define YYDEBUG 1
68 #if USE_CPPLIB
69 extern unsigned char *yy_cur, *yy_lim;
70 extern enum cpp_token cpp_token;
72 extern int yy_get_token ();
74 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
75 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
77 #else /* ! USE_CPPLIB */
79 #define GETC() getch ()
80 #define UNGETC(c) put_back (c)
82 struct putback_buffer {
83 unsigned char *buffer;
84 int buffer_size;
85 int index;
88 static struct putback_buffer putback = {NULL, 0, -1};
90 static inline int getch PARAMS ((void));
92 static inline int
93 getch ()
95 if (putback.index != -1)
97 int ch = putback.buffer[putback.index];
98 --putback.index;
99 return ch;
101 return getc (finput);
104 static inline void put_back PARAMS ((int));
106 static inline void
107 put_back (ch)
108 int ch;
110 if (ch != EOF)
112 if (putback.index == putback.buffer_size - 1)
114 putback.buffer_size += 16;
115 putback.buffer = xrealloc (putback.buffer, putback.buffer_size);
117 putback.buffer[++putback.index] = ch;
120 #endif /* ! USE_CPPLIB */
122 int linemode;
124 extern int yydebug;
126 /* File used for outputting assembler code. */
127 extern FILE *asm_out_file;
129 #undef WCHAR_TYPE_SIZE
130 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
132 /* Number of bytes in a wide character. */
133 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
135 static int maxtoken; /* Current nominal length of token buffer. */
136 char *token_buffer; /* Pointer to token buffer.
137 Actual allocated length is maxtoken + 2.
138 This is not static because objc-parse.y uses it. */
140 static int indent_level; /* Number of { minus number of }. */
142 /* Nonzero tells yylex to ignore \ in string constants. */
143 static int ignore_escape_flag;
145 /* Nonzero if end-of-file has been seen on input. */
146 static int end_of_file;
148 #ifdef HANDLE_GENERIC_PRAGMAS
149 static int handle_generic_pragma PARAMS ((int));
150 #endif /* HANDLE_GENERIC_PRAGMAS */
151 static int whitespace_cr PARAMS ((int));
152 static int skip_white_space PARAMS ((int));
153 static char *extend_token_buffer PARAMS ((const char *));
154 static int readescape PARAMS ((int *));
155 static void parse_float PARAMS ((PTR));
156 static void extend_token_buffer_to PARAMS ((int));
157 static int read_line_number PARAMS ((int *));
159 /* Do not insert generated code into the source, instead, include it.
160 This allows us to build gcc automatically even for targets that
161 need to add or modify the reserved keyword lists. */
162 #include "c-gperf.h"
164 /* Return something to represent absolute declarators containing a *.
165 TARGET is the absolute declarator that the * contains.
166 TYPE_QUALS is a list of modifiers such as const or volatile
167 to apply to the pointer type, represented as identifiers.
169 We return an INDIRECT_REF whose "contents" are TARGET
170 and whose type is the modifier list. */
172 tree
173 make_pointer_declarator (type_quals, target)
174 tree type_quals, target;
176 return build1 (INDIRECT_REF, type_quals, target);
179 void
180 forget_protocol_qualifiers ()
182 int i, n = sizeof wordlist / sizeof (struct resword);
184 for (i = 0; i < n; i++)
185 if ((int) wordlist[i].rid >= (int) RID_IN
186 && (int) wordlist[i].rid <= (int) RID_ONEWAY)
187 wordlist[i].name = "";
190 void
191 remember_protocol_qualifiers ()
193 int i, n = sizeof wordlist / sizeof (struct resword);
195 for (i = 0; i < n; i++)
196 if (wordlist[i].rid == RID_IN)
197 wordlist[i].name = "in";
198 else if (wordlist[i].rid == RID_OUT)
199 wordlist[i].name = "out";
200 else if (wordlist[i].rid == RID_INOUT)
201 wordlist[i].name = "inout";
202 else if (wordlist[i].rid == RID_BYCOPY)
203 wordlist[i].name = "bycopy";
204 else if (wordlist[i].rid == RID_BYREF)
205 wordlist[i].name = "byref";
206 else if (wordlist[i].rid == RID_ONEWAY)
207 wordlist[i].name = "oneway";
210 const char *
211 init_parse (filename)
212 const char *filename;
214 #if !USE_CPPLIB
215 /* Open input file. */
216 if (filename == 0 || !strcmp (filename, "-"))
218 finput = stdin;
219 filename = "stdin";
221 else
222 finput = fopen (filename, "r");
223 if (finput == 0)
224 pfatal_with_name (filename);
226 #ifdef IO_BUFFER_SIZE
227 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE), _IOFBF, IO_BUFFER_SIZE);
228 #endif
229 #else /* !USE_CPPLIB */
230 parse_in.show_column = 1;
231 if (! cpp_start_read (&parse_in, filename))
232 abort ();
234 if (filename == 0 || !strcmp (filename, "-"))
235 filename = "stdin";
237 /* cpp_start_read always puts at least one line directive into the
238 token buffer. We must arrange to read it out here. */
239 yy_cur = parse_in.token_buffer;
240 yy_lim = CPP_PWRITTEN (&parse_in);
241 cpp_token = CPP_DIRECTIVE;
242 #endif
244 add_c_tree_codes ();
246 init_lex ();
247 init_pragma ();
249 return filename;
252 void
253 finish_parse ()
255 #if USE_CPPLIB
256 cpp_finish (&parse_in);
257 errorcount += parse_in.errors;
258 #else
259 fclose (finput);
260 #endif
263 void
264 init_lex ()
266 /* Make identifier nodes long enough for the language-specific slots. */
267 set_identifier_size (sizeof (struct lang_identifier));
269 /* Start it at 0, because check_newline is called at the very beginning
270 and will increment it to 1. */
271 lineno = 0;
273 #ifdef MULTIBYTE_CHARS
274 /* Change to the native locale for multibyte conversions. */
275 setlocale (LC_CTYPE, "");
276 GET_ENVIRONMENT (literal_codeset, "LANG");
277 #endif
279 maxtoken = 40;
280 token_buffer = (char *) xmalloc (maxtoken + 2);
282 ridpointers = (tree *) xcalloc ((int) RID_MAX, sizeof (tree));
283 ridpointers[(int) RID_INT] = get_identifier ("int");
284 ridpointers[(int) RID_CHAR] = get_identifier ("char");
285 ridpointers[(int) RID_VOID] = get_identifier ("void");
286 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
287 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
288 ridpointers[(int) RID_SHORT] = get_identifier ("short");
289 ridpointers[(int) RID_LONG] = get_identifier ("long");
290 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
291 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
292 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
293 ridpointers[(int) RID_CONST] = get_identifier ("const");
294 ridpointers[(int) RID_RESTRICT] = get_identifier ("restrict");
295 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
296 ridpointers[(int) RID_BOUNDED] = get_identifier ("__bounded");
297 ridpointers[(int) RID_UNBOUNDED] = get_identifier ("__unbounded");
298 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
299 ridpointers[(int) RID_STATIC] = get_identifier ("static");
300 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
301 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
302 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
303 ridpointers[(int) RID_ITERATOR] = get_identifier ("iterator");
304 ridpointers[(int) RID_COMPLEX] = get_identifier ("complex");
305 ridpointers[(int) RID_ID] = get_identifier ("id");
306 ridpointers[(int) RID_IN] = get_identifier ("in");
307 ridpointers[(int) RID_OUT] = get_identifier ("out");
308 ridpointers[(int) RID_INOUT] = get_identifier ("inout");
309 ridpointers[(int) RID_BYCOPY] = get_identifier ("bycopy");
310 ridpointers[(int) RID_BYREF] = get_identifier ("byref");
311 ridpointers[(int) RID_ONEWAY] = get_identifier ("oneway");
312 forget_protocol_qualifiers();
314 /* Some options inhibit certain reserved words.
315 Clear those words out of the hash table so they won't be recognized. */
316 #define UNSET_RESERVED_WORD(STRING) \
317 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
318 if (s) s->name = ""; } while (0)
320 if (! doing_objc_thang)
321 UNSET_RESERVED_WORD ("id");
323 if (flag_traditional)
325 UNSET_RESERVED_WORD ("const");
326 UNSET_RESERVED_WORD ("restrict");
327 UNSET_RESERVED_WORD ("volatile");
328 UNSET_RESERVED_WORD ("typeof");
329 UNSET_RESERVED_WORD ("signed");
330 UNSET_RESERVED_WORD ("inline");
331 UNSET_RESERVED_WORD ("iterator");
332 UNSET_RESERVED_WORD ("complex");
334 else if (!flag_isoc99)
335 UNSET_RESERVED_WORD ("restrict");
337 if (flag_no_asm)
339 UNSET_RESERVED_WORD ("asm");
340 UNSET_RESERVED_WORD ("typeof");
341 if (! flag_isoc99)
342 UNSET_RESERVED_WORD ("inline");
343 UNSET_RESERVED_WORD ("iterator");
344 UNSET_RESERVED_WORD ("complex");
348 void
349 reinit_parse_for_function ()
353 /* Function used when yydebug is set, to print a token in more detail. */
355 void
356 yyprint (file, yychar, yylval)
357 FILE *file;
358 int yychar;
359 YYSTYPE yylval;
361 tree t;
362 switch (yychar)
364 case IDENTIFIER:
365 case TYPENAME:
366 case OBJECTNAME:
367 t = yylval.ttype;
368 if (IDENTIFIER_POINTER (t))
369 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
370 break;
372 case CONSTANT:
373 t = yylval.ttype;
374 if (TREE_CODE (t) == INTEGER_CST)
375 fprintf (file,
376 #if HOST_BITS_PER_WIDE_INT == 64
377 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
378 " 0x%x%016x",
379 #else
380 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
381 " 0x%lx%016lx",
382 #else
383 " 0x%llx%016llx",
384 #endif
385 #endif
386 #else
387 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
388 " 0x%lx%08lx",
389 #else
390 " 0x%x%08x",
391 #endif
392 #endif
393 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
394 break;
398 /* Iff C is a carriage return, warn about it - if appropriate -
399 and return nonzero. */
401 static int
402 whitespace_cr (c)
403 int c;
405 static int newline_warning = 0;
407 if (c == '\r')
409 /* ANSI C says the effects of a carriage return in a source file
410 are undefined. */
411 if (pedantic && !newline_warning)
413 warning ("carriage return in source file");
414 warning ("(we only warn about the first carriage return)");
415 newline_warning = 1;
417 return 1;
419 return 0;
422 /* If C is not whitespace, return C.
423 Otherwise skip whitespace and return first nonwhite char read. */
425 static int
426 skip_white_space (c)
427 register int c;
429 for (;;)
431 switch (c)
433 /* We don't recognize comments here, because
434 cpp output can include / and * consecutively as operators.
435 Also, there's no need, since cpp removes all comments. */
437 case '\n':
438 if (linemode)
440 UNGETC (c);
441 return EOF;
443 c = check_newline ();
444 break;
446 case ' ':
447 case '\t':
448 case '\f':
449 case '\v':
450 case '\b':
451 #if USE_CPPLIB
452 /* While processing a # directive we don't get CPP_HSPACE
453 tokens, so we also need to handle whitespace the normal way. */
454 if (cpp_token == CPP_HSPACE)
455 c = yy_get_token ();
456 else
457 #endif
458 c = GETC();
459 break;
461 case '\r':
462 whitespace_cr (c);
463 c = GETC();
464 break;
466 case '\\':
467 c = GETC();
468 if (c == '\n')
469 lineno++;
470 else
471 error ("stray '\\' in program");
472 c = GETC();
473 break;
475 default:
476 return (c);
481 /* Skips all of the white space at the current location in the input file. */
483 void
484 position_after_white_space ()
486 register int c;
488 c = GETC();
490 UNGETC (skip_white_space (c));
493 /* Make the token buffer longer, preserving the data in it.
494 P should point to just beyond the last valid character in the old buffer.
495 The value we return is a pointer to the new buffer
496 at a place corresponding to P. */
498 static void
499 extend_token_buffer_to (size)
500 int size;
503 maxtoken = maxtoken * 2 + 10;
504 while (maxtoken < size);
505 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
508 static char *
509 extend_token_buffer (p)
510 const char *p;
512 int offset = p - token_buffer;
513 extend_token_buffer_to (offset);
514 return token_buffer + offset;
517 #if defined HANDLE_PRAGMA
518 /* Local versions of these macros, that can be passed as function pointers. */
519 static int
520 pragma_getc ()
522 return GETC ();
525 static void
526 pragma_ungetc (arg)
527 int arg;
529 UNGETC (arg);
531 #endif
533 static int
534 read_line_number (num)
535 int *num;
537 register int token = yylex ();
539 if (token == CONSTANT
540 && TREE_CODE (yylval.ttype) == INTEGER_CST)
542 *num = TREE_INT_CST_LOW (yylval.ttype);
543 return 1;
545 else
547 if (token != END_OF_LINE)
548 error ("invalid #-line");
549 return 0;
553 /* At the beginning of a line, increment the line number
554 and process any #-directive on this line.
555 If the line is a #-directive, read the entire line and return a newline.
556 Otherwise, return the line's first non-whitespace character.
558 Note that in the case of USE_CPPLIB, we get the whole line as one
559 CPP_DIRECTIVE token. */
562 check_newline ()
564 register int c;
565 register int token;
566 int saw_line;
567 enum { act_none, act_push, act_pop } action;
568 int old_lineno, action_number, l;
570 restart:
571 /* Read first nonwhite char on the line. */
573 #ifdef USE_CPPLIB
574 c = GETC ();
575 /* In some cases where we're leaving an include file, we can get multiple
576 CPP_HSPACE tokens in a row, so we need to loop. */
577 while (cpp_token == CPP_HSPACE)
578 c = yy_get_token ();
579 #else
581 c = GETC ();
582 while (c == ' ' || c == '\t');
583 #endif
585 lineno++;
587 if (c != '#')
589 /* Sequences of multiple newlines are very common; optimize them. */
590 if (c == '\n')
591 goto restart;
593 /* If not #, return it so caller will use it. */
594 return c;
597 /* Don't read beyond this line. */
598 saw_line = 0;
599 linemode = 1;
601 #if USE_CPPLIB
602 if (cpp_token == CPP_VSPACE)
604 /* Format is "<space> <line number> <filename> <newline>".
605 Only the line number is interesting, and even that
606 we can get more efficiently than scanning the line. */
607 yy_cur = yy_lim - 1;
608 lineno = parse_in.lineno - 1;
609 goto skipline;
611 #endif
613 token = yylex ();
615 if (token == IDENTIFIER)
617 /* If a letter follows, then if the word here is `line', skip
618 it and ignore it; otherwise, ignore the line, with an error
619 if the word isn't `pragma'. */
621 const char *name = IDENTIFIER_POINTER (yylval.ttype);
623 if (!strcmp (name, "pragma"))
625 token = yylex ();
626 if (token != IDENTIFIER
627 || TREE_CODE (yylval.ttype) != IDENTIFIER_NODE)
628 goto skipline;
630 #ifdef HANDLE_PRAGMA
631 /* We invoke HANDLE_PRAGMA before HANDLE_GENERIC_PRAGMAS
632 (if both are defined), in order to give the back
633 end a chance to override the interpretation of
634 SYSV style pragmas. */
635 if (HANDLE_PRAGMA (pragma_getc, pragma_ungetc,
636 IDENTIFIER_POINTER (yylval.ttype)))
637 goto skipline;
638 #endif /* HANDLE_PRAGMA */
640 #ifdef HANDLE_GENERIC_PRAGMAS
641 if (handle_generic_pragma (token))
642 goto skipline;
643 #endif /* HANDLE_GENERIC_PRAGMAS */
645 /* Issue a warning message if we have been asked to do so.
646 Ignoring unknown pragmas in system header file unless
647 an explcit -Wunknown-pragmas has been given. */
648 if (warn_unknown_pragmas > 1
649 || (warn_unknown_pragmas && ! in_system_header))
650 warning ("ignoring pragma: %s", token_buffer);
652 goto skipline;
654 else if (!strcmp (name, "define"))
656 debug_define (lineno, GET_DIRECTIVE_LINE ());
657 goto skipline;
659 else if (!strcmp (name, "undef"))
661 debug_undef (lineno, GET_DIRECTIVE_LINE ());
662 goto skipline;
664 else if (!strcmp (name, "line"))
666 saw_line = 1;
667 token = yylex ();
668 goto linenum;
670 else if (!strcmp (name, "ident"))
672 /* #ident. The pedantic warning is now in cpp. */
674 /* Here we have just seen `#ident '.
675 A string constant should follow. */
677 token = yylex ();
678 if (token == END_OF_LINE)
679 goto skipline;
680 if (token != STRING
681 || TREE_CODE (yylval.ttype) != STRING_CST)
683 error ("invalid #ident");
684 goto skipline;
687 if (! flag_no_ident)
689 #ifdef ASM_OUTPUT_IDENT
690 ASM_OUTPUT_IDENT (asm_out_file,
691 TREE_STRING_POINTER (yylval.ttype));
692 #endif
695 /* Skip the rest of this line. */
696 goto skipline;
699 error ("undefined or invalid # directive `%s'", name);
700 goto skipline;
703 /* If the # is the only nonwhite char on the line,
704 just ignore it. Check the new newline. */
705 if (token == END_OF_LINE)
706 goto skipline;
708 linenum:
709 /* Here we have either `#line' or `# <nonletter>'.
710 In either case, it should be a line number; a digit should follow. */
712 if (token != CONSTANT
713 || TREE_CODE (yylval.ttype) != INTEGER_CST)
715 error ("invalid #-line");
716 goto skipline;
719 /* subtract one, because it is the following line that
720 gets the specified number */
722 l = TREE_INT_CST_LOW (yylval.ttype) - 1;
724 /* More follows: it must be a string constant (filename).
725 It would be neat to use cpplib to quickly process the string, but
726 (1) we don't have a handy tokenization of the string, and
727 (2) I don't know how well that would work in the presense
728 of filenames that contain wide characters. */
730 if (saw_line)
732 /* Don't treat \ as special if we are processing #line 1 "...".
733 If you want it to be treated specially, use # 1 "...". */
734 ignore_escape_flag = 1;
737 /* Read the string constant. */
738 token = yylex ();
740 ignore_escape_flag = 0;
742 if (token == END_OF_LINE)
744 /* No more: store the line number and check following line. */
745 lineno = l;
746 goto skipline;
749 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
751 error ("invalid #line");
752 goto skipline;
755 input_filename = TREE_STRING_POINTER (yylval.ttype);
757 if (main_input_filename == 0)
758 main_input_filename = input_filename;
760 old_lineno = lineno;
761 action = act_none;
762 action_number = 0;
763 lineno = l;
765 /* Each change of file name
766 reinitializes whether we are now in a system header. */
767 in_system_header = 0;
769 if (!read_line_number (&action_number))
771 /* Update the name in the top element of input_file_stack. */
772 if (input_file_stack)
773 input_file_stack->name = input_filename;
776 /* `1' after file name means entering new file.
777 `2' after file name means just left a file. */
779 if (action_number == 1)
781 action = act_push;
782 read_line_number (&action_number);
784 else if (action_number == 2)
786 action = act_pop;
787 read_line_number (&action_number);
789 if (action_number == 3)
791 /* `3' after file name means this is a system header file. */
792 in_system_header = 1;
793 read_line_number (&action_number);
796 /* Do the actions implied by the preceding numbers. */
798 if (action == act_push)
800 /* Pushing to a new file. */
801 struct file_stack *p
802 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
803 input_file_stack->line = old_lineno;
804 p->next = input_file_stack;
805 p->name = input_filename;
806 p->indent_level = indent_level;
807 input_file_stack = p;
808 input_file_stack_tick++;
809 debug_start_source_file (input_filename);
811 else if (action == act_pop)
813 /* Popping out of a file. */
814 if (input_file_stack->next)
816 struct file_stack *p = input_file_stack;
817 if (indent_level != p->indent_level)
819 warning_with_file_and_line
820 (p->name, old_lineno,
821 "This file contains more `%c's than `%c's.",
822 indent_level > p->indent_level ? '{' : '}',
823 indent_level > p->indent_level ? '}' : '{');
825 input_file_stack = p->next;
826 free (p);
827 input_file_stack_tick++;
828 debug_end_source_file (input_file_stack->line);
830 else
831 error ("#-lines for entering and leaving files don't match");
834 /* Now that we've pushed or popped the input stack,
835 update the name in the top element. */
836 if (input_file_stack)
837 input_file_stack->name = input_filename;
839 /* skip the rest of this line. */
840 skipline:
841 linemode = 0;
842 end_of_file = 0;
845 c = GETC();
846 while (c != '\n' && c != EOF);
847 return c;
850 #ifdef HANDLE_GENERIC_PRAGMAS
852 /* Handle a #pragma directive.
853 TOKEN is the token we read after `#pragma'. Processes the entire input
854 line and return non-zero iff the pragma has been successfully parsed. */
856 /* This function has to be in this file, in order to get at
857 the token types. */
859 static int
860 handle_generic_pragma (token)
861 register int token;
863 for (;;)
865 switch (token)
867 case IDENTIFIER:
868 case TYPENAME:
869 case STRING:
870 case CONSTANT:
871 handle_pragma_token (token_buffer, yylval.ttype);
872 break;
874 case END_OF_LINE:
875 return handle_pragma_token (NULL_PTR, NULL_TREE);
877 default:
878 handle_pragma_token (token_buffer, NULL);
881 token = yylex ();
885 #endif /* HANDLE_GENERIC_PRAGMAS */
887 #define ENDFILE -1 /* token that represents end-of-file */
889 /* Read an escape sequence, returning its equivalent as a character,
890 or store 1 in *ignore_ptr if it is backslash-newline. */
892 static int
893 readescape (ignore_ptr)
894 int *ignore_ptr;
896 register int c = GETC();
897 register int code;
898 register unsigned count;
899 unsigned firstdig = 0;
900 int nonnull;
902 switch (c)
904 case 'x':
905 if (warn_traditional && !in_system_header)
906 warning ("the meaning of `\\x' varies with -traditional");
908 if (flag_traditional)
909 return c;
911 code = 0;
912 count = 0;
913 nonnull = 0;
914 while (1)
916 c = GETC();
917 if (! ISXDIGIT (c))
919 UNGETC (c);
920 break;
922 code *= 16;
923 if (c >= 'a' && c <= 'f')
924 code += c - 'a' + 10;
925 if (c >= 'A' && c <= 'F')
926 code += c - 'A' + 10;
927 if (c >= '0' && c <= '9')
928 code += c - '0';
929 if (code != 0 || count != 0)
931 if (count == 0)
932 firstdig = code;
933 count++;
935 nonnull = 1;
937 if (! nonnull)
939 warning ("\\x used with no following hex digits");
940 return 'x';
942 else if (count == 0)
943 /* Digits are all 0's. Ok. */
945 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
946 || (count > 1
947 && (((unsigned)1
948 << (TYPE_PRECISION (integer_type_node)
949 - (count - 1) * 4))
950 <= firstdig)))
951 pedwarn ("hex escape out of range");
952 return code;
954 case '0': case '1': case '2': case '3': case '4':
955 case '5': case '6': case '7':
956 code = 0;
957 count = 0;
958 while ((c <= '7') && (c >= '0') && (count++ < 3))
960 code = (code * 8) + (c - '0');
961 c = GETC();
963 UNGETC (c);
964 return code;
966 case '\\': case '\'': case '"':
967 return c;
969 case '\n':
970 lineno++;
971 *ignore_ptr = 1;
972 return 0;
974 case 'n':
975 return TARGET_NEWLINE;
977 case 't':
978 return TARGET_TAB;
980 case 'r':
981 return TARGET_CR;
983 case 'f':
984 return TARGET_FF;
986 case 'b':
987 return TARGET_BS;
989 case 'a':
990 if (warn_traditional && !in_system_header)
991 warning ("the meaning of `\\a' varies with -traditional");
993 if (flag_traditional)
994 return c;
995 return TARGET_BELL;
997 case 'v':
998 #if 0 /* Vertical tab is present in common usage compilers. */
999 if (flag_traditional)
1000 return c;
1001 #endif
1002 return TARGET_VT;
1004 case 'e':
1005 case 'E':
1006 if (pedantic)
1007 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
1008 return TARGET_ESC;
1010 case '?':
1011 return c;
1013 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
1014 case '(':
1015 case '{':
1016 case '[':
1017 /* `\%' is used to prevent SCCS from getting confused. */
1018 case '%':
1019 if (pedantic)
1020 pedwarn ("unknown escape sequence `\\%c'", c);
1021 return c;
1023 if (ISGRAPH (c))
1024 pedwarn ("unknown escape sequence `\\%c'", c);
1025 else
1026 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
1027 return c;
1030 void
1031 yyerror (msgid)
1032 const char *msgid;
1034 const char *string = _(msgid);
1036 /* We can't print string and character constants well
1037 because the token_buffer contains the result of processing escapes. */
1038 if (end_of_file)
1039 error ("%s at end of input", string);
1040 else if (token_buffer[0] == 0)
1041 error ("%s at null character", string);
1042 else if (token_buffer[0] == '"')
1043 error ("%s before string constant", string);
1044 else if (token_buffer[0] == '\'')
1045 error ("%s before character constant", string);
1046 else if (!ISGRAPH(token_buffer[0]))
1047 error ("%s before character 0%o", string, (unsigned char) token_buffer[0]);
1048 else
1049 error ("%s before `%s'", string, token_buffer);
1052 #if 0
1054 struct try_type
1056 tree *node_var;
1057 char unsigned_flag;
1058 char long_flag;
1059 char long_long_flag;
1062 struct try_type type_sequence[] =
1064 { &integer_type_node, 0, 0, 0},
1065 { &unsigned_type_node, 1, 0, 0},
1066 { &long_integer_type_node, 0, 1, 0},
1067 { &long_unsigned_type_node, 1, 1, 0},
1068 { &long_long_integer_type_node, 0, 1, 1},
1069 { &long_long_unsigned_type_node, 1, 1, 1}
1071 #endif /* 0 */
1073 struct pf_args
1075 /* Input */
1076 int base;
1077 char * p;
1078 /* I/O */
1079 int c;
1080 /* Output */
1081 int imag;
1082 tree type;
1083 int conversion_errno;
1084 REAL_VALUE_TYPE value;
1087 static void
1088 parse_float (data)
1089 PTR data;
1091 struct pf_args * args = (struct pf_args *) data;
1092 int fflag = 0, lflag = 0;
1093 /* Copy token_buffer now, while it has just the number
1094 and not the suffixes; once we add `f' or `i',
1095 REAL_VALUE_ATOF may not work any more. */
1096 char *copy = (char *) alloca (args->p - token_buffer + 1);
1097 bcopy (token_buffer, copy, args->p - token_buffer + 1);
1098 args->imag = 0;
1099 args->conversion_errno = 0;
1100 args->type = double_type_node;
1102 while (1)
1104 int lose = 0;
1106 /* Read the suffixes to choose a data type. */
1107 switch (args->c)
1109 case 'f': case 'F':
1110 if (fflag)
1111 error ("more than one `f' in numeric constant");
1112 else if (warn_traditional && !in_system_header)
1113 warning ("traditional C rejects the `%c' suffix", args->c);
1114 fflag = 1;
1115 break;
1117 case 'l': case 'L':
1118 if (lflag)
1119 error ("more than one `l' in numeric constant");
1120 else if (warn_traditional && !in_system_header)
1121 warning ("traditional C rejects the `%c' suffix", args->c);
1122 lflag = 1;
1123 break;
1125 case 'i': case 'I':
1126 if (args->imag)
1127 error ("more than one `i' or `j' in numeric constant");
1128 else if (pedantic)
1129 pedwarn ("ISO C forbids imaginary numeric constants");
1130 args->imag = 1;
1131 break;
1133 default:
1134 lose = 1;
1137 if (lose)
1138 break;
1140 if (args->p >= token_buffer + maxtoken - 3)
1141 args->p = extend_token_buffer (args->p);
1142 *(args->p++) = args->c;
1143 *(args->p) = 0;
1144 args->c = GETC();
1147 /* The second argument, machine_mode, of REAL_VALUE_ATOF
1148 tells the desired precision of the binary result
1149 of decimal-to-binary conversion. */
1151 if (fflag)
1153 if (lflag)
1154 error ("both `f' and `l' in floating constant");
1156 args->type = float_type_node;
1157 errno = 0;
1158 if (args->base == 16)
1159 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1160 else
1161 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1162 args->conversion_errno = errno;
1163 /* A diagnostic is required here by some ANSI C testsuites.
1164 This is not pedwarn, because some people don't want
1165 an error for this. */
1166 if (REAL_VALUE_ISINF (args->value) && pedantic)
1167 warning ("floating point number exceeds range of `float'");
1169 else if (lflag)
1171 args->type = long_double_type_node;
1172 errno = 0;
1173 if (args->base == 16)
1174 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1175 else
1176 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1177 args->conversion_errno = errno;
1178 if (REAL_VALUE_ISINF (args->value) && pedantic)
1179 warning ("floating point number exceeds range of `long double'");
1181 else
1183 errno = 0;
1184 if (flag_single_precision_constant)
1185 args->type = float_type_node;
1186 if (args->base == 16)
1187 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1188 else
1189 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1190 args->conversion_errno = errno;
1191 if (REAL_VALUE_ISINF (args->value) && pedantic)
1192 warning ("floating point number exceeds range of `double'");
1196 /* Get the next character, staying within the current token if possible.
1197 If we're lexing a token, we don't want to look beyond the end of the
1198 token cpplib has prepared for us; otherwise, we end up reading in the
1199 next token, which screws up feed_input. So just return a null
1200 character. */
1202 static inline int token_getch PARAMS ((void));
1204 static inline int
1205 token_getch ()
1207 #if USE_CPPLIB
1208 if (yy_cur == yy_lim)
1209 return '\0';
1210 #endif
1211 return GETC ();
1214 static inline void token_put_back PARAMS ((int));
1216 static inline void
1217 token_put_back (ch)
1218 int ch;
1220 #if USE_CPPLIB
1221 if (ch == '\0')
1222 return;
1223 #endif
1224 UNGETC (ch);
1227 /* Read a single token from the input stream, and assign it lexical
1228 semantics. */
1231 yylex ()
1233 register int c;
1234 register char *p;
1235 register int value;
1236 int wide_flag = 0;
1237 int objc_flag = 0;
1239 c = GETC();
1241 /* Effectively do c = skip_white_space (c)
1242 but do it faster in the usual cases. */
1243 while (1)
1244 switch (c)
1246 case ' ':
1247 case '\t':
1248 case '\f':
1249 case '\v':
1250 case '\b':
1251 #if USE_CPPLIB
1252 if (cpp_token == CPP_HSPACE)
1253 c = yy_get_token ();
1254 else
1255 #endif
1256 c = GETC();
1257 break;
1259 case '\r':
1260 /* Call skip_white_space so we can warn if appropriate. */
1262 case '\n':
1263 case '/':
1264 case '\\':
1265 c = skip_white_space (c);
1266 default:
1267 goto found_nonwhite;
1269 found_nonwhite:
1271 token_buffer[0] = c;
1272 token_buffer[1] = 0;
1274 /* yylloc.first_line = lineno; */
1276 switch (c)
1278 case EOF:
1279 end_of_file = 1;
1280 token_buffer[0] = 0;
1281 if (linemode)
1282 value = END_OF_LINE;
1283 else
1284 value = ENDFILE;
1285 break;
1287 case 'L':
1288 #if USE_CPPLIB
1289 if (cpp_token == CPP_NAME)
1290 goto letter;
1291 #endif
1292 /* Capital L may start a wide-string or wide-character constant. */
1294 register int c = token_getch();
1295 if (c == '\'')
1297 wide_flag = 1;
1298 goto char_constant;
1300 if (c == '"')
1302 wide_flag = 1;
1303 goto string_constant;
1305 token_put_back (c);
1307 goto letter;
1309 case '@':
1310 if (!doing_objc_thang)
1312 value = c;
1313 break;
1315 else
1317 /* '@' may start a constant string object. */
1318 register int c = token_getch ();
1319 if (c == '"')
1321 objc_flag = 1;
1322 goto string_constant;
1324 token_put_back (c);
1325 /* Fall through to treat '@' as the start of an identifier. */
1328 case 'A': case 'B': case 'C': case 'D': case 'E':
1329 case 'F': case 'G': case 'H': case 'I': case 'J':
1330 case 'K': case 'M': case 'N': case 'O':
1331 case 'P': case 'Q': case 'R': case 'S': case 'T':
1332 case 'U': case 'V': case 'W': case 'X': case 'Y':
1333 case 'Z':
1334 case 'a': case 'b': case 'c': case 'd': case 'e':
1335 case 'f': case 'g': case 'h': case 'i': case 'j':
1336 case 'k': case 'l': case 'm': case 'n': case 'o':
1337 case 'p': case 'q': case 'r': case 's': case 't':
1338 case 'u': case 'v': case 'w': case 'x': case 'y':
1339 case 'z':
1340 case '_':
1341 case '$':
1342 letter:
1343 #if USE_CPPLIB
1344 if (cpp_token == CPP_NAME)
1346 /* Note that one character has already been read from
1347 yy_cur into token_buffer. Also, cpplib complains about
1348 $ in identifiers, so we don't have to. */
1350 int len = yy_lim - yy_cur + 1;
1351 if (len >= maxtoken)
1352 extend_token_buffer_to (len + 1);
1353 memcpy (token_buffer + 1, yy_cur, len);
1354 p = token_buffer + len;
1355 yy_cur = yy_lim;
1357 else
1358 #endif
1360 p = token_buffer;
1361 while (ISALNUM (c) || c == '_' || c == '$' || c == '@')
1363 /* Make sure this char really belongs in an identifier. */
1364 if (c == '$')
1366 if (! dollars_in_ident)
1367 error ("`$' in identifier");
1368 else if (pedantic)
1369 pedwarn ("`$' in identifier");
1372 if (p >= token_buffer + maxtoken)
1373 p = extend_token_buffer (p);
1375 *p++ = c;
1376 c = token_getch();
1379 *p = 0;
1380 token_put_back (c);
1383 value = IDENTIFIER;
1384 yylval.itype = 0;
1386 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1389 register struct resword *ptr;
1391 if ((ptr = is_reserved_word (token_buffer, p - token_buffer)))
1393 if (ptr->rid)
1394 yylval.ttype = ridpointers[(int) ptr->rid];
1395 value = (int) ptr->token;
1397 /* Only return OBJECTNAME if it is a typedef. */
1398 if (doing_objc_thang && value == OBJECTNAME)
1400 tree decl = lookup_name(yylval.ttype);
1402 if (decl == NULL_TREE
1403 || TREE_CODE (decl) != TYPE_DECL)
1404 value = IDENTIFIER;
1407 /* Even if we decided to recognize asm, still perhaps warn. */
1408 if (pedantic
1409 && (value == ASM_KEYWORD || value == TYPEOF
1410 || (ptr->rid == RID_INLINE && ! flag_isoc99))
1411 && token_buffer[0] != '_')
1412 pedwarn ("ANSI does not permit the keyword `%s'",
1413 token_buffer);
1417 /* If we did not find a keyword, look for an identifier
1418 (or a typename). */
1420 if (value == IDENTIFIER)
1422 tree decl;
1424 if (token_buffer[0] == '@')
1425 error("invalid identifier `%s'", token_buffer);
1427 yylval.ttype = get_identifier (token_buffer);
1428 decl = lookup_name (yylval.ttype);
1430 if (decl != 0 && TREE_CODE (decl) == TYPE_DECL)
1431 value = TYPENAME;
1432 /* A user-invisible read-only initialized variable
1433 should be replaced by its value.
1434 We handle only strings since that's the only case used in C. */
1435 else if (decl != 0 && TREE_CODE (decl) == VAR_DECL
1436 && DECL_IGNORED_P (decl)
1437 && TREE_READONLY (decl)
1438 && DECL_INITIAL (decl) != 0
1439 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
1441 tree stringval = DECL_INITIAL (decl);
1443 /* Copy the string value so that we won't clobber anything
1444 if we put something in the TREE_CHAIN of this one. */
1445 yylval.ttype = build_string (TREE_STRING_LENGTH (stringval),
1446 TREE_STRING_POINTER (stringval));
1447 value = STRING;
1449 else if (doing_objc_thang)
1451 tree objc_interface_decl = is_class_name (yylval.ttype);
1453 if (objc_interface_decl)
1455 value = CLASSNAME;
1456 yylval.ttype = objc_interface_decl;
1461 break;
1463 case '.':
1464 #if USE_CPPLIB
1465 if (yy_cur < yy_lim)
1466 #endif
1468 /* It's hard to preserve tokenization on '.' because
1469 it could be a symbol by itself, or it could be the
1470 start of a floating point number and cpp won't tell us. */
1471 register int c1 = token_getch ();
1472 token_buffer[1] = c1;
1473 if (c1 == '.')
1475 c1 = token_getch ();
1476 if (c1 == '.')
1478 token_buffer[2] = c1;
1479 token_buffer[3] = 0;
1480 value = ELLIPSIS;
1481 goto done;
1483 error ("parse error at `..'");
1485 if (ISDIGIT (c1))
1487 token_put_back (c1);
1488 goto number;
1490 token_put_back (c1);
1492 value = '.';
1493 token_buffer[1] = 0;
1494 break;
1496 case '0': case '1':
1497 /* Optimize for most frequent case. */
1499 register int cond;
1501 #if USE_CPPLIB
1502 cond = (yy_cur == yy_lim);
1503 #else
1504 register int c1 = token_getch ();
1505 token_put_back (c1);
1506 cond = (! ISALNUM (c1) && c1 != '.');
1507 #endif
1508 if (cond)
1510 yylval.ttype = (c == '0') ? integer_zero_node : integer_one_node;
1511 value = CONSTANT;
1512 break;
1514 /*FALLTHRU*/
1516 case '2': case '3': case '4':
1517 case '5': case '6': case '7': case '8': case '9':
1518 number:
1520 int base = 10;
1521 int count = 0;
1522 int largest_digit = 0;
1523 int numdigits = 0;
1524 int overflow = 0;
1526 /* We actually store only HOST_BITS_PER_CHAR bits in each part.
1527 The code below which fills the parts array assumes that a host
1528 int is at least twice as wide as a host char, and that
1529 HOST_BITS_PER_WIDE_INT is an even multiple of HOST_BITS_PER_CHAR.
1530 Two HOST_WIDE_INTs is the largest int literal we can store.
1531 In order to detect overflow below, the number of parts (TOTAL_PARTS)
1532 must be exactly the number of parts needed to hold the bits
1533 of two HOST_WIDE_INTs. */
1534 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2)
1535 unsigned int parts[TOTAL_PARTS];
1537 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS, AFTER_EXPON}
1538 floatflag = NOT_FLOAT;
1540 for (count = 0; count < TOTAL_PARTS; count++)
1541 parts[count] = 0;
1543 p = token_buffer;
1544 *p++ = c;
1546 if (c == '0')
1548 *p++ = (c = token_getch());
1549 if ((c == 'x') || (c == 'X'))
1551 base = 16;
1552 *p++ = (c = token_getch());
1554 /* Leading 0 forces octal unless the 0 is the only digit. */
1555 else if (c >= '0' && c <= '9')
1557 base = 8;
1558 numdigits++;
1560 else
1561 numdigits++;
1564 /* Read all the digits-and-decimal-points. */
1566 while (c == '.'
1567 || (ISALNUM (c) && c != 'l' && c != 'L'
1568 && c != 'u' && c != 'U'
1569 && c != 'i' && c != 'I' && c != 'j' && c != 'J'
1570 && (floatflag == NOT_FLOAT
1571 || ((base != 16) && (c != 'f') && (c != 'F'))
1572 || base == 16)))
1574 if (c == '.')
1576 if (base == 16 && pedantic && !flag_isoc99)
1577 pedwarn ("floating constant may not be in radix 16");
1578 if (floatflag == TOO_MANY_POINTS)
1579 /* We have already emitted an error. Don't need another. */
1581 else if (floatflag == AFTER_POINT || floatflag == AFTER_EXPON)
1583 error ("malformed floating constant");
1584 floatflag = TOO_MANY_POINTS;
1585 /* Avoid another error from atof by forcing all characters
1586 from here on to be ignored. */
1587 p[-1] = '\0';
1589 else
1590 floatflag = AFTER_POINT;
1592 if (base == 8)
1593 base = 10;
1594 *p++ = c = token_getch();
1595 /* Accept '.' as the start of a floating-point number
1596 only when it is followed by a digit. */
1597 if (p == token_buffer + 2 && !ISDIGIT (c))
1598 abort ();
1600 else
1602 /* It is not a decimal point.
1603 It should be a digit (perhaps a hex digit). */
1605 if (ISDIGIT (c))
1607 c = c - '0';
1609 else if (base <= 10)
1611 if (c == 'e' || c == 'E')
1613 base = 10;
1614 floatflag = AFTER_EXPON;
1615 break; /* start of exponent */
1617 error ("nondigits in number and not hexadecimal");
1618 c = 0;
1620 else if (base == 16 && (c == 'p' || c == 'P'))
1622 floatflag = AFTER_EXPON;
1623 break; /* start of exponent */
1625 else if (c >= 'a' && c <= 'f')
1627 c = c - 'a' + 10;
1629 else
1631 c = c - 'A' + 10;
1633 if (c >= largest_digit)
1634 largest_digit = c;
1635 numdigits++;
1637 for (count = 0; count < TOTAL_PARTS; count++)
1639 parts[count] *= base;
1640 if (count)
1642 parts[count]
1643 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1644 parts[count-1]
1645 &= (1 << HOST_BITS_PER_CHAR) - 1;
1647 else
1648 parts[0] += c;
1651 /* If the highest-order part overflows (gets larger than
1652 a host char will hold) then the whole number has
1653 overflowed. Record this and truncate the highest-order
1654 part. */
1655 if (parts[TOTAL_PARTS - 1] >> HOST_BITS_PER_CHAR)
1657 overflow = 1;
1658 parts[TOTAL_PARTS - 1] &= (1 << HOST_BITS_PER_CHAR) - 1;
1661 if (p >= token_buffer + maxtoken - 3)
1662 p = extend_token_buffer (p);
1663 *p++ = (c = token_getch());
1667 /* This can happen on input like `int i = 0x;' */
1668 if (numdigits == 0)
1669 error ("numeric constant with no digits");
1671 if (largest_digit >= base)
1672 error ("numeric constant contains digits beyond the radix");
1674 /* Remove terminating char from the token buffer and delimit the
1675 string. */
1676 *--p = 0;
1678 if (floatflag != NOT_FLOAT)
1680 tree type;
1681 int imag, conversion_errno;
1682 REAL_VALUE_TYPE value;
1683 struct pf_args args;
1685 /* Read explicit exponent if any, and put it in tokenbuf. */
1687 if ((base == 10 && ((c == 'e') || (c == 'E')))
1688 || (base == 16 && (c == 'p' || c == 'P')))
1690 if (p >= token_buffer + maxtoken - 3)
1691 p = extend_token_buffer (p);
1692 *p++ = c;
1693 c = token_getch();
1694 if ((c == '+') || (c == '-'))
1696 *p++ = c;
1697 c = token_getch();
1699 /* Exponent is decimal, even if string is a hex float. */
1700 if (! ISDIGIT (c))
1701 error ("floating constant exponent has no digits");
1702 while (ISDIGIT (c))
1704 if (p >= token_buffer + maxtoken - 3)
1705 p = extend_token_buffer (p);
1706 *p++ = c;
1707 c = token_getch ();
1710 if (base == 16 && floatflag != AFTER_EXPON)
1711 error ("hexadecimal floating constant has no exponent");
1713 *p = 0;
1715 /* Setup input for parse_float() */
1716 args.base = base;
1717 args.p = p;
1718 args.c = c;
1720 /* Convert string to a double, checking for overflow. */
1721 if (do_float_handler (parse_float, (PTR) &args))
1723 /* Receive output from parse_float() */
1724 value = args.value;
1726 else
1728 /* We got an exception from parse_float() */
1729 error ("floating constant out of range");
1730 value = dconst0;
1733 /* Receive output from parse_float() */
1734 c = args.c;
1735 imag = args.imag;
1736 type = args.type;
1737 conversion_errno = args.conversion_errno;
1739 #ifdef ERANGE
1740 /* ERANGE is also reported for underflow,
1741 so test the value to distinguish overflow from that. */
1742 if (conversion_errno == ERANGE && !flag_traditional && pedantic
1743 && (REAL_VALUES_LESS (dconst1, value)
1744 || REAL_VALUES_LESS (value, dconstm1)))
1745 warning ("floating point number exceeds range of `double'");
1746 #endif
1748 /* If the result is not a number, assume it must have been
1749 due to some error message above, so silently convert
1750 it to a zero. */
1751 if (REAL_VALUE_ISNAN (value))
1752 value = dconst0;
1754 /* Create a node with determined type and value. */
1755 if (imag)
1756 yylval.ttype = build_complex (NULL_TREE,
1757 convert (type, integer_zero_node),
1758 build_real (type, value));
1759 else
1760 yylval.ttype = build_real (type, value);
1762 else
1764 tree traditional_type, ansi_type, type;
1765 HOST_WIDE_INT high, low;
1766 int spec_unsigned = 0;
1767 int spec_long = 0;
1768 int spec_long_long = 0;
1769 int suffix_lu = 0;
1770 int spec_imag = 0;
1771 int warn = 0, i;
1773 traditional_type = ansi_type = type = NULL_TREE;
1774 while (1)
1776 if (c == 'u' || c == 'U')
1778 if (spec_unsigned)
1779 error ("two `u's in integer constant");
1780 else if (warn_traditional && !in_system_header)
1781 warning ("traditional C rejects the `%c' suffix", c);
1782 spec_unsigned = 1;
1783 if (spec_long)
1784 suffix_lu = 1;
1786 else if (c == 'l' || c == 'L')
1788 if (spec_long)
1790 if (spec_long_long)
1791 error ("three `l's in integer constant");
1792 else if (suffix_lu)
1793 error ("`LUL' is not a valid integer suffix");
1794 else if (c != spec_long)
1795 error ("`Ll' and `lL' are not valid integer suffixes");
1796 else if (pedantic && ! flag_isoc99
1797 && ! in_system_header && warn_long_long)
1798 pedwarn ("ISO C89 forbids long long integer constants");
1799 spec_long_long = 1;
1801 spec_long = c;
1803 else if (c == 'i' || c == 'j' || c == 'I' || c == 'J')
1805 if (spec_imag)
1806 error ("more than one `i' or `j' in numeric constant");
1807 else if (pedantic)
1808 pedwarn ("ISO C forbids imaginary numeric constants");
1809 spec_imag = 1;
1811 else
1812 break;
1813 if (p >= token_buffer + maxtoken - 3)
1814 p = extend_token_buffer (p);
1815 *p++ = c;
1816 c = token_getch();
1819 /* If the literal overflowed, pedwarn about it now. */
1820 if (overflow)
1822 warn = 1;
1823 pedwarn ("integer constant is too large for this configuration of the compiler - truncated to %d bits", HOST_BITS_PER_WIDE_INT * 2);
1826 /* This is simplified by the fact that our constant
1827 is always positive. */
1829 high = low = 0;
1831 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1833 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1834 / HOST_BITS_PER_CHAR)]
1835 << (i * HOST_BITS_PER_CHAR));
1836 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1839 yylval.ttype = build_int_2 (low, high);
1840 TREE_TYPE (yylval.ttype) = long_long_unsigned_type_node;
1842 /* If warn_traditional, calculate both the ANSI type and the
1843 traditional type, then see if they disagree.
1844 Otherwise, calculate only the type for the dialect in use. */
1845 if (warn_traditional || flag_traditional)
1847 /* Calculate the traditional type. */
1848 /* Traditionally, any constant is signed;
1849 but if unsigned is specified explicitly, obey that.
1850 Use the smallest size with the right number of bits,
1851 except for one special case with decimal constants. */
1852 if (! spec_long && base != 10
1853 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1854 traditional_type = (spec_unsigned ? unsigned_type_node
1855 : integer_type_node);
1856 /* A decimal constant must be long
1857 if it does not fit in type int.
1858 I think this is independent of whether
1859 the constant is signed. */
1860 else if (! spec_long && base == 10
1861 && int_fits_type_p (yylval.ttype, integer_type_node))
1862 traditional_type = (spec_unsigned ? unsigned_type_node
1863 : integer_type_node);
1864 else if (! spec_long_long)
1865 traditional_type = (spec_unsigned ? long_unsigned_type_node
1866 : long_integer_type_node);
1867 else if (int_fits_type_p (yylval.ttype,
1868 spec_unsigned
1869 ? long_long_unsigned_type_node
1870 : long_long_integer_type_node))
1871 traditional_type = (spec_unsigned
1872 ? long_long_unsigned_type_node
1873 : long_long_integer_type_node);
1874 else
1875 traditional_type = (spec_unsigned
1876 ? widest_unsigned_literal_type_node
1877 : widest_integer_literal_type_node);
1879 if (warn_traditional || ! flag_traditional)
1881 /* Calculate the ANSI type. */
1882 if (! spec_long && ! spec_unsigned
1883 && int_fits_type_p (yylval.ttype, integer_type_node))
1884 ansi_type = integer_type_node;
1885 else if (! spec_long && (base != 10 || spec_unsigned)
1886 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1887 ansi_type = unsigned_type_node;
1888 else if (! spec_unsigned && !spec_long_long
1889 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1890 ansi_type = long_integer_type_node;
1891 else if (! spec_long_long
1892 && int_fits_type_p (yylval.ttype,
1893 long_unsigned_type_node))
1894 ansi_type = long_unsigned_type_node;
1895 else if (! spec_unsigned
1896 && int_fits_type_p (yylval.ttype,
1897 long_long_integer_type_node))
1898 ansi_type = long_long_integer_type_node;
1899 else if (int_fits_type_p (yylval.ttype,
1900 long_long_unsigned_type_node))
1901 ansi_type = long_long_unsigned_type_node;
1902 else if (! spec_unsigned
1903 && int_fits_type_p (yylval.ttype,
1904 widest_integer_literal_type_node))
1905 ansi_type = widest_integer_literal_type_node;
1906 else
1907 ansi_type = widest_unsigned_literal_type_node;
1910 type = flag_traditional ? traditional_type : ansi_type;
1912 /* We assume that constants specified in a non-decimal
1913 base are bit patterns, and that the programmer really
1914 meant what they wrote. */
1915 if (warn_traditional && !in_system_header && base == 10
1916 && traditional_type != ansi_type)
1918 if (TYPE_PRECISION (traditional_type)
1919 != TYPE_PRECISION (ansi_type))
1920 warning ("width of integer constant changes with -traditional");
1921 else if (TREE_UNSIGNED (traditional_type)
1922 != TREE_UNSIGNED (ansi_type))
1923 warning ("integer constant is unsigned in ISO C, signed with -traditional");
1924 else
1925 warning ("width of integer constant may change on other systems with -traditional");
1928 if (pedantic && !flag_traditional && !spec_long_long && !warn
1929 && (TYPE_PRECISION (long_integer_type_node)
1930 < TYPE_PRECISION (type)))
1932 warn = 1;
1933 pedwarn ("integer constant larger than the maximum value of an unsigned long int");
1936 if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type))
1937 warning ("decimal constant is so large that it is unsigned");
1939 if (spec_imag)
1941 if (TYPE_PRECISION (type)
1942 <= TYPE_PRECISION (integer_type_node))
1943 yylval.ttype
1944 = build_complex (NULL_TREE, integer_zero_node,
1945 convert (integer_type_node,
1946 yylval.ttype));
1947 else
1948 error ("complex integer constant is too wide for `complex int'");
1950 else if (flag_traditional && !int_fits_type_p (yylval.ttype, type))
1951 /* The traditional constant 0x80000000 is signed
1952 but doesn't fit in the range of int.
1953 This will change it to -0x80000000, which does fit. */
1955 TREE_TYPE (yylval.ttype) = unsigned_type (type);
1956 yylval.ttype = convert (type, yylval.ttype);
1957 TREE_OVERFLOW (yylval.ttype)
1958 = TREE_CONSTANT_OVERFLOW (yylval.ttype) = 0;
1960 else
1961 TREE_TYPE (yylval.ttype) = type;
1964 /* If it's still an integer (not a complex), and it doesn't
1965 fit in the type we choose for it, then pedwarn. */
1967 if (! warn
1968 && TREE_CODE (TREE_TYPE (yylval.ttype)) == INTEGER_TYPE
1969 && ! int_fits_type_p (yylval.ttype, TREE_TYPE (yylval.ttype)))
1970 pedwarn ("integer constant is larger than the maximum value for its type");
1973 token_put_back (c);
1974 *p = 0;
1976 if (ISALNUM (c) || c == '.' || c == '_' || c == '$'
1977 || (!flag_traditional && (c == '-' || c == '+')
1978 && (p[-1] == 'e' || p[-1] == 'E')))
1979 error ("missing white space after number `%s'", token_buffer);
1981 value = CONSTANT; break;
1984 case '\'':
1985 char_constant:
1987 register int result = 0;
1988 register int num_chars = 0;
1989 int chars_seen = 0;
1990 unsigned width = TYPE_PRECISION (char_type_node);
1991 int max_chars;
1992 #ifdef MULTIBYTE_CHARS
1993 int longest_char = local_mb_cur_max ();
1994 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
1995 #endif
1997 max_chars = TYPE_PRECISION (integer_type_node) / width;
1998 if (wide_flag)
1999 width = WCHAR_TYPE_SIZE;
2001 while (1)
2003 tryagain:
2004 c = token_getch();
2006 if (c == '\'' || c == EOF)
2007 break;
2009 ++chars_seen;
2010 if (c == '\\')
2012 int ignore = 0;
2013 c = readescape (&ignore);
2014 if (ignore)
2015 goto tryagain;
2016 if (width < HOST_BITS_PER_INT
2017 && (unsigned) c >= ((unsigned)1 << width))
2018 pedwarn ("escape sequence out of range for character");
2019 #ifdef MAP_CHARACTER
2020 if (ISPRINT (c))
2021 c = MAP_CHARACTER (c);
2022 #endif
2024 else if (c == '\n')
2026 if (pedantic)
2027 pedwarn ("ISO C forbids newline in character constant");
2028 lineno++;
2030 else
2032 #ifdef MULTIBYTE_CHARS
2033 wchar_t wc;
2034 int i;
2035 int char_len = -1;
2036 for (i = 1; i <= longest_char; ++i)
2038 if (i > maxtoken - 4)
2039 extend_token_buffer (token_buffer);
2041 token_buffer[i] = c;
2042 char_len = local_mbtowc (& wc,
2043 token_buffer + 1,
2045 if (char_len != -1)
2046 break;
2047 c = token_getch ();
2049 if (char_len > 1)
2051 /* mbtowc sometimes needs an extra char before accepting */
2052 if (char_len < i)
2053 token_put_back (c);
2054 if (! wide_flag)
2056 /* Merge character into result; ignore excess chars. */
2057 for (i = 1; i <= char_len; ++i)
2059 if (i > max_chars)
2060 break;
2061 if (width < HOST_BITS_PER_INT)
2062 result = (result << width)
2063 | (token_buffer[i]
2064 & ((1 << width) - 1));
2065 else
2066 result = token_buffer[i];
2068 num_chars += char_len;
2069 goto tryagain;
2071 c = wc;
2073 else
2075 if (char_len == -1)
2077 warning ("Ignoring invalid multibyte character");
2078 /* Replace all but the first byte. */
2079 for (--i; i > 1; --i)
2080 token_put_back (token_buffer[i]);
2081 wc = token_buffer[1];
2083 #ifdef MAP_CHARACTER
2084 c = MAP_CHARACTER (wc);
2085 #else
2086 c = wc;
2087 #endif
2089 #else /* ! MULTIBYTE_CHARS */
2090 #ifdef MAP_CHARACTER
2091 c = MAP_CHARACTER (c);
2092 #endif
2093 #endif /* ! MULTIBYTE_CHARS */
2096 if (wide_flag)
2098 if (chars_seen == 1) /* only keep the first one */
2099 result = c;
2100 goto tryagain;
2103 /* Merge character into result; ignore excess chars. */
2104 num_chars += (width / TYPE_PRECISION (char_type_node));
2105 if (num_chars < max_chars + 1)
2107 if (width < HOST_BITS_PER_INT)
2108 result = (result << width) | (c & ((1 << width) - 1));
2109 else
2110 result = c;
2114 if (c != '\'')
2115 error ("malformed character constant");
2116 else if (chars_seen == 0)
2117 error ("empty character constant");
2118 else if (num_chars > max_chars)
2120 num_chars = max_chars;
2121 error ("character constant too long");
2123 else if (chars_seen != 1 && ! flag_traditional && warn_multichar)
2124 warning ("multi-character character constant");
2126 /* If char type is signed, sign-extend the constant. */
2127 if (! wide_flag)
2129 int num_bits = num_chars * width;
2130 if (num_bits == 0)
2131 /* We already got an error; avoid invalid shift. */
2132 yylval.ttype = build_int_2 (0, 0);
2133 else if (TREE_UNSIGNED (char_type_node)
2134 || ((result >> (num_bits - 1)) & 1) == 0)
2135 yylval.ttype
2136 = build_int_2 (result & (~(unsigned HOST_WIDE_INT) 0
2137 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
2139 else
2140 yylval.ttype
2141 = build_int_2 (result | ~(~(unsigned HOST_WIDE_INT) 0
2142 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
2143 -1);
2144 TREE_TYPE (yylval.ttype) = integer_type_node;
2146 else
2148 yylval.ttype = build_int_2 (result, 0);
2149 TREE_TYPE (yylval.ttype) = wchar_type_node;
2152 value = CONSTANT;
2153 break;
2156 case '"':
2157 string_constant:
2159 unsigned width = wide_flag ? WCHAR_TYPE_SIZE
2160 : TYPE_PRECISION (char_type_node);
2161 #ifdef MULTIBYTE_CHARS
2162 int longest_char = local_mb_cur_max ();
2163 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
2164 #endif
2165 c = token_getch ();
2166 p = token_buffer + 1;
2168 while (c != '"' && c != EOF)
2170 /* ignore_escape_flag is set for reading the filename in #line. */
2171 if (!ignore_escape_flag && c == '\\')
2173 int ignore = 0;
2174 c = readescape (&ignore);
2175 if (ignore)
2176 goto skipnewline;
2177 if (width < HOST_BITS_PER_INT
2178 && (unsigned) c >= ((unsigned)1 << width))
2179 pedwarn ("escape sequence out of range for character");
2181 else if (c == '\n')
2183 if (pedantic)
2184 pedwarn ("ISO C forbids newline in string constant");
2185 lineno++;
2187 else
2189 #ifdef MULTIBYTE_CHARS
2190 wchar_t wc;
2191 int i;
2192 int char_len = -1;
2193 for (i = 0; i < longest_char; ++i)
2195 if (p + i >= token_buffer + maxtoken)
2196 p = extend_token_buffer (p);
2197 p[i] = c;
2199 char_len = local_mbtowc (& wc, p, i + 1);
2200 if (char_len != -1)
2201 break;
2202 c = token_getch ();
2204 if (char_len == -1)
2206 warning ("Ignoring invalid multibyte character");
2207 /* Replace all except the first byte. */
2208 token_put_back (c);
2209 for (--i; i > 0; --i)
2210 token_put_back (p[i]);
2211 char_len = 1;
2213 /* mbtowc sometimes needs an extra char before accepting */
2214 if (char_len <= i)
2215 token_put_back (c);
2216 if (! wide_flag)
2218 p += (i + 1);
2219 c = token_getch ();
2220 continue;
2222 c = wc;
2223 #endif /* MULTIBYTE_CHARS */
2226 /* Add this single character into the buffer either as a wchar_t
2227 or as a single byte. */
2228 if (wide_flag)
2230 unsigned width = TYPE_PRECISION (char_type_node);
2231 unsigned bytemask = (1 << width) - 1;
2232 int byte;
2234 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2235 p = extend_token_buffer (p);
2237 for (byte = 0; byte < WCHAR_BYTES; ++byte)
2239 int value;
2240 if (byte >= (int) sizeof (c))
2241 value = 0;
2242 else
2243 value = (c >> (byte * width)) & bytemask;
2244 if (BYTES_BIG_ENDIAN)
2245 p[WCHAR_BYTES - byte - 1] = value;
2246 else
2247 p[byte] = value;
2249 p += WCHAR_BYTES;
2251 else
2253 if (p >= token_buffer + maxtoken)
2254 p = extend_token_buffer (p);
2255 *p++ = c;
2258 skipnewline:
2259 c = token_getch ();
2262 /* Terminate the string value, either with a single byte zero
2263 or with a wide zero. */
2264 if (wide_flag)
2266 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2267 p = extend_token_buffer (p);
2268 bzero (p, WCHAR_BYTES);
2269 p += WCHAR_BYTES;
2271 else
2273 if (p >= token_buffer + maxtoken)
2274 p = extend_token_buffer (p);
2275 *p++ = 0;
2278 if (c == EOF)
2279 error ("Unterminated string constant");
2281 /* We have read the entire constant.
2282 Construct a STRING_CST for the result. */
2284 yylval.ttype = build_string (p - (token_buffer + 1), token_buffer + 1);
2285 if (wide_flag)
2287 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
2288 value = STRING;
2290 else if (objc_flag)
2292 TREE_TYPE (yylval.ttype) = char_array_type_node;
2293 value = OBJC_STRING;
2295 else
2297 TREE_TYPE (yylval.ttype) = char_array_type_node;
2298 value = STRING;
2301 break;
2304 case '+':
2305 case '-':
2306 case '&':
2307 case '|':
2308 case ':':
2309 case '<':
2310 case '>':
2311 case '*':
2312 case '/':
2313 case '%':
2314 case '^':
2315 case '!':
2316 case '=':
2318 register int c1;
2320 combine:
2322 switch (c)
2324 case '+':
2325 yylval.code = PLUS_EXPR; break;
2326 case '-':
2327 yylval.code = MINUS_EXPR; break;
2328 case '&':
2329 yylval.code = BIT_AND_EXPR; break;
2330 case '|':
2331 yylval.code = BIT_IOR_EXPR; break;
2332 case '*':
2333 yylval.code = MULT_EXPR; break;
2334 case '/':
2335 yylval.code = TRUNC_DIV_EXPR; break;
2336 case '%':
2337 yylval.code = TRUNC_MOD_EXPR; break;
2338 case '^':
2339 yylval.code = BIT_XOR_EXPR; break;
2340 case LSHIFT:
2341 yylval.code = LSHIFT_EXPR; break;
2342 case RSHIFT:
2343 yylval.code = RSHIFT_EXPR; break;
2344 case '<':
2345 yylval.code = LT_EXPR; break;
2346 case '>':
2347 yylval.code = GT_EXPR; break;
2350 token_buffer[1] = c1 = token_getch();
2351 token_buffer[2] = 0;
2353 if (c1 == '=')
2355 switch (c)
2357 case '<':
2358 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
2359 case '>':
2360 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
2361 case '!':
2362 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
2363 case '=':
2364 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
2366 value = ASSIGN; goto done;
2368 else if (c == c1)
2369 switch (c)
2371 case '+':
2372 value = PLUSPLUS; goto done;
2373 case '-':
2374 value = MINUSMINUS; goto done;
2375 case '&':
2376 value = ANDAND; goto done;
2377 case '|':
2378 value = OROR; goto done;
2379 case '<':
2380 c = LSHIFT;
2381 goto combine;
2382 case '>':
2383 c = RSHIFT;
2384 goto combine;
2386 else
2387 switch (c)
2389 case '-':
2390 if (c1 == '>')
2391 { value = POINTSAT; goto done; }
2392 break;
2394 /* digraphs */
2395 case ':':
2396 if (c1 == '>' && flag_digraphs)
2397 { value = ']'; goto done; }
2398 break;
2399 case '<':
2400 if (flag_digraphs)
2402 if (c1 == '%')
2403 { value = '{'; indent_level++; goto done; }
2404 if (c1 == ':')
2405 { value = '['; goto done; }
2407 break;
2408 case '%':
2409 if (c1 == '>' && flag_digraphs)
2410 { value = '}'; indent_level--; goto done; }
2411 break;
2414 token_put_back (c1);
2415 token_buffer[1] = 0;
2417 if ((c == '<') || (c == '>'))
2418 value = ARITHCOMPARE;
2419 else value = c;
2420 break;
2423 case 0:
2424 /* Don't make yyparse think this is eof. */
2425 value = 1;
2426 break;
2428 case '{':
2429 indent_level++;
2430 value = c;
2431 break;
2433 case '}':
2434 indent_level--;
2435 value = c;
2436 break;
2438 default:
2439 value = c;
2442 done:
2443 /* yylloc.last_line = lineno; */
2445 return value;
2448 /* Sets the value of the 'yydebug' variable to VALUE.
2449 This is a function so we don't have to have YYDEBUG defined
2450 in order to build the compiler. */
2452 void
2453 set_yydebug (value)
2454 int value;
2456 #if YYDEBUG != 0
2457 yydebug = value;
2458 #else
2459 warning ("YYDEBUG not defined.");
2460 #endif