* class.c (check_bitfield_decl): New function, split out from
[official-gcc.git] / gcc / c-lex.c
blobab71f787228aca4d6065b1ab30ccec0ed4bf9606
1 /* Lexical analyzer for C and Objective C.
2 Copyright (C) 1987, 88, 89, 92, 94-98, 1999 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"
22 #include "system.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "input.h"
27 #include "output.h"
28 #include "c-lex.h"
29 #include "c-tree.h"
30 #include "flags.h"
31 #include "c-parse.h"
32 #include "c-pragma.h"
33 #include "toplev.h"
34 #include "intl.h"
35 #include "ggc.h"
37 /* MULTIBYTE_CHARS support only works for native compilers.
38 ??? Ideally what we want is to model widechar support after
39 the current floating point support. */
40 #ifdef CROSS_COMPILE
41 #undef MULTIBYTE_CHARS
42 #endif
44 #ifdef MULTIBYTE_CHARS
45 #include "mbchar.h"
46 #include <locale.h>
47 #endif /* MULTIBYTE_CHARS */
48 #ifndef GET_ENVIRONMENT
49 #define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ((ENV_VALUE) = getenv (ENV_NAME))
50 #endif
52 #if USE_CPPLIB
53 #include "cpplib.h"
54 extern cpp_reader parse_in;
55 extern cpp_options parse_options;
56 #else
57 /* Stream for reading from the input file. */
58 FILE *finput;
59 #endif
61 extern void yyprint PROTO((FILE *, int, YYSTYPE));
63 /* The elements of `ridpointers' are identifier nodes
64 for the reserved type names and storage classes.
65 It is indexed by a RID_... value. */
66 tree ridpointers[(int) RID_MAX];
68 /* Cause the `yydebug' variable to be defined. */
69 #define YYDEBUG 1
71 #if USE_CPPLIB
72 extern unsigned char *yy_cur, *yy_lim;
73 extern enum cpp_token cpp_token;
75 extern int yy_get_token ();
77 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
78 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
80 #else /* ! USE_CPPLIB */
82 #define GETC() getch ()
83 #define UNGETC(c) put_back (c)
85 struct putback_buffer {
86 char *buffer;
87 int buffer_size;
88 int index;
91 static struct putback_buffer putback = {NULL, 0, -1};
93 static inline int getch PROTO ((void));
95 static inline int
96 getch ()
98 if (putback.index != -1)
100 int ch = putback.buffer[putback.index];
101 --putback.index;
102 return ch;
104 return getc (finput);
107 static inline void put_back PROTO ((int));
109 static inline void
110 put_back (ch)
111 int ch;
113 if (ch != EOF)
115 if (putback.index == putback.buffer_size - 1)
117 putback.buffer_size += 16;
118 putback.buffer = xrealloc (putback.buffer, putback.buffer_size);
120 putback.buffer[++putback.index] = ch;
123 #endif /* ! USE_CPPLIB */
125 int linemode;
127 /* the declaration found for the last IDENTIFIER token read in.
128 yylex must look this up to detect typedefs, which get token type TYPENAME,
129 so it is left around in case the identifier is not a typedef but is
130 used in a context which makes it a reference to a variable. */
131 tree lastiddecl;
133 /* Nonzero enables objc features. */
135 int doing_objc_thang;
137 extern int yydebug;
139 /* File used for outputting assembler code. */
140 extern FILE *asm_out_file;
142 #undef WCHAR_TYPE_SIZE
143 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
145 /* Number of bytes in a wide character. */
146 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
148 static int maxtoken; /* Current nominal length of token buffer. */
149 char *token_buffer; /* Pointer to token buffer.
150 Actual allocated length is maxtoken + 2.
151 This is not static because objc-parse.y uses it. */
153 static int indent_level; /* Number of { minus number of }. */
155 /* Nonzero tells yylex to ignore \ in string constants. */
156 static int ignore_escape_flag;
158 /* Nonzero if end-of-file has been seen on input. */
159 static int end_of_file;
161 #ifdef HANDLE_GENERIC_PRAGMAS
162 static int handle_generic_pragma PROTO((int));
163 #endif /* HANDLE_GENERIC_PRAGMAS */
164 static int whitespace_cr PROTO((int));
165 static int skip_white_space PROTO((int));
166 static char *extend_token_buffer PROTO((const char *));
167 static int readescape PROTO((int *));
168 static void parse_float PROTO((PTR));
169 static void extend_token_buffer_to PROTO((int));
170 static int read_line_number PROTO((int *));
172 /* Do not insert generated code into the source, instead, include it.
173 This allows us to build gcc automatically even for targets that
174 need to add or modify the reserved keyword lists. */
175 #include "c-gperf.h"
177 /* Return something to represent absolute declarators containing a *.
178 TARGET is the absolute declarator that the * contains.
179 TYPE_QUALS is a list of modifiers such as const or volatile
180 to apply to the pointer type, represented as identifiers.
182 We return an INDIRECT_REF whose "contents" are TARGET
183 and whose type is the modifier list. */
185 tree
186 make_pointer_declarator (type_quals, target)
187 tree type_quals, target;
189 return build1 (INDIRECT_REF, type_quals, target);
192 void
193 forget_protocol_qualifiers ()
195 int i, n = sizeof wordlist / sizeof (struct resword);
197 for (i = 0; i < n; i++)
198 if ((int) wordlist[i].rid >= (int) RID_IN
199 && (int) wordlist[i].rid <= (int) RID_ONEWAY)
200 wordlist[i].name = "";
203 void
204 remember_protocol_qualifiers ()
206 int i, n = sizeof wordlist / sizeof (struct resword);
208 for (i = 0; i < n; i++)
209 if (wordlist[i].rid == RID_IN)
210 wordlist[i].name = "in";
211 else if (wordlist[i].rid == RID_OUT)
212 wordlist[i].name = "out";
213 else if (wordlist[i].rid == RID_INOUT)
214 wordlist[i].name = "inout";
215 else if (wordlist[i].rid == RID_BYCOPY)
216 wordlist[i].name = "bycopy";
217 else if (wordlist[i].rid == RID_BYREF)
218 wordlist[i].name = "byref";
219 else if (wordlist[i].rid == RID_ONEWAY)
220 wordlist[i].name = "oneway";
223 char *
224 init_parse (filename)
225 char *filename;
227 #if !USE_CPPLIB
228 /* Open input file. */
229 if (filename == 0 || !strcmp (filename, "-"))
231 finput = stdin;
232 filename = "stdin";
234 else
235 finput = fopen (filename, "r");
236 if (finput == 0)
237 pfatal_with_name (filename);
239 #ifdef IO_BUFFER_SIZE
240 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE), _IOFBF, IO_BUFFER_SIZE);
241 #endif
242 #else /* !USE_CPPLIB */
243 parse_in.show_column = 1;
244 if (! cpp_start_read (&parse_in, filename))
245 abort ();
247 if (filename == 0 || !strcmp (filename, "-"))
248 filename = "stdin";
250 /* cpp_start_read always puts at least one line directive into the
251 token buffer. We must arrange to read it out here. */
252 yy_cur = parse_in.token_buffer;
253 yy_lim = CPP_PWRITTEN (&parse_in);
254 cpp_token = CPP_DIRECTIVE;
255 #endif
257 init_lex ();
258 init_pragma ();
260 return filename;
263 void
264 finish_parse ()
266 #if USE_CPPLIB
267 cpp_finish (&parse_in);
268 errorcount += parse_in.errors;
269 #else
270 fclose (finput);
271 #endif
274 void
275 init_lex ()
277 /* Make identifier nodes long enough for the language-specific slots. */
278 set_identifier_size (sizeof (struct lang_identifier));
280 /* Start it at 0, because check_newline is called at the very beginning
281 and will increment it to 1. */
282 lineno = 0;
284 #ifdef MULTIBYTE_CHARS
285 /* Change to the native locale for multibyte conversions. */
286 setlocale (LC_CTYPE, "");
287 GET_ENVIRONMENT (literal_codeset, "LANG");
288 #endif
290 maxtoken = 40;
291 token_buffer = (char *) xmalloc (maxtoken + 2);
293 ridpointers[(int) RID_INT] = get_identifier ("int");
294 ridpointers[(int) RID_CHAR] = get_identifier ("char");
295 ridpointers[(int) RID_VOID] = get_identifier ("void");
296 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
297 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
298 ridpointers[(int) RID_SHORT] = get_identifier ("short");
299 ridpointers[(int) RID_LONG] = get_identifier ("long");
300 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
301 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
302 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
303 ridpointers[(int) RID_CONST] = get_identifier ("const");
304 ridpointers[(int) RID_RESTRICT] = get_identifier ("restrict");
305 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
306 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
307 ridpointers[(int) RID_STATIC] = get_identifier ("static");
308 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
309 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
310 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
311 ridpointers[(int) RID_ITERATOR] = get_identifier ("iterator");
312 ridpointers[(int) RID_COMPLEX] = get_identifier ("complex");
313 ridpointers[(int) RID_ID] = get_identifier ("id");
314 ridpointers[(int) RID_IN] = get_identifier ("in");
315 ridpointers[(int) RID_OUT] = get_identifier ("out");
316 ridpointers[(int) RID_INOUT] = get_identifier ("inout");
317 ridpointers[(int) RID_BYCOPY] = get_identifier ("bycopy");
318 ridpointers[(int) RID_BYREF] = get_identifier ("byref");
319 ridpointers[(int) RID_ONEWAY] = get_identifier ("oneway");
320 forget_protocol_qualifiers();
322 /* Some options inhibit certain reserved words.
323 Clear those words out of the hash table so they won't be recognized. */
324 #define UNSET_RESERVED_WORD(STRING) \
325 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
326 if (s) s->name = ""; } while (0)
328 if (! doing_objc_thang)
329 UNSET_RESERVED_WORD ("id");
331 if (flag_traditional)
333 UNSET_RESERVED_WORD ("const");
334 UNSET_RESERVED_WORD ("restrict");
335 UNSET_RESERVED_WORD ("volatile");
336 UNSET_RESERVED_WORD ("typeof");
337 UNSET_RESERVED_WORD ("signed");
338 UNSET_RESERVED_WORD ("inline");
339 UNSET_RESERVED_WORD ("iterator");
340 UNSET_RESERVED_WORD ("complex");
342 else if (!flag_isoc9x)
343 UNSET_RESERVED_WORD ("restrict");
345 if (flag_no_asm)
347 UNSET_RESERVED_WORD ("asm");
348 UNSET_RESERVED_WORD ("typeof");
349 UNSET_RESERVED_WORD ("inline");
350 UNSET_RESERVED_WORD ("iterator");
351 UNSET_RESERVED_WORD ("complex");
355 void
356 reinit_parse_for_function ()
360 /* Function used when yydebug is set, to print a token in more detail. */
362 void
363 yyprint (file, yychar, yylval)
364 FILE *file;
365 int yychar;
366 YYSTYPE yylval;
368 tree t;
369 switch (yychar)
371 case IDENTIFIER:
372 case TYPENAME:
373 case OBJECTNAME:
374 t = yylval.ttype;
375 if (IDENTIFIER_POINTER (t))
376 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
377 break;
379 case CONSTANT:
380 t = yylval.ttype;
381 if (TREE_CODE (t) == INTEGER_CST)
382 fprintf (file,
383 #if HOST_BITS_PER_WIDE_INT == 64
384 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
385 " 0x%x%016x",
386 #else
387 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
388 " 0x%lx%016lx",
389 #else
390 " 0x%llx%016llx",
391 #endif
392 #endif
393 #else
394 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
395 " 0x%lx%08lx",
396 #else
397 " 0x%x%08x",
398 #endif
399 #endif
400 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
401 break;
405 /* Iff C is a carriage return, warn about it - if appropriate -
406 and return nonzero. */
407 static int
408 whitespace_cr (c)
409 int c;
411 static int newline_warning = 0;
413 if (c == '\r')
415 /* ANSI C says the effects of a carriage return in a source file
416 are undefined. */
417 if (pedantic && !newline_warning)
419 warning ("carriage return in source file");
420 warning ("(we only warn about the first carriage return)");
421 newline_warning = 1;
423 return 1;
425 return 0;
428 /* If C is not whitespace, return C.
429 Otherwise skip whitespace and return first nonwhite char read. */
431 static int
432 skip_white_space (c)
433 register int c;
435 for (;;)
437 switch (c)
439 /* We don't recognize comments here, because
440 cpp output can include / and * consecutively as operators.
441 Also, there's no need, since cpp removes all comments. */
443 case '\n':
444 if (linemode)
446 UNGETC (c);
447 return EOF;
449 c = check_newline ();
450 break;
452 case ' ':
453 case '\t':
454 case '\f':
455 case '\v':
456 case '\b':
457 #if USE_CPPLIB
458 /* While processing a # directive we don't get CPP_HSPACE
459 tokens, so we also need to handle whitespace the normal way. */
460 if (cpp_token == CPP_HSPACE)
461 c = yy_get_token ();
462 else
463 #endif
464 c = GETC();
465 break;
467 case '\r':
468 whitespace_cr (c);
469 c = GETC();
470 break;
472 case '\\':
473 c = GETC();
474 if (c == '\n')
475 lineno++;
476 else
477 error ("stray '\\' in program");
478 c = GETC();
479 break;
481 default:
482 return (c);
487 /* Skips all of the white space at the current location in the input file. */
489 void
490 position_after_white_space ()
492 register int c;
494 c = GETC();
496 UNGETC (skip_white_space (c));
499 /* Make the token buffer longer, preserving the data in it.
500 P should point to just beyond the last valid character in the old buffer.
501 The value we return is a pointer to the new buffer
502 at a place corresponding to P. */
504 static void
505 extend_token_buffer_to (size)
506 int size;
509 maxtoken = maxtoken * 2 + 10;
510 while (maxtoken < size);
511 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
514 static char *
515 extend_token_buffer (p)
516 const char *p;
518 int offset = p - token_buffer;
519 extend_token_buffer_to (offset);
520 return token_buffer + offset;
523 #if defined HANDLE_PRAGMA
524 /* Local versions of these macros, that can be passed as function pointers. */
525 static int
526 pragma_getc ()
528 return GETC();
531 static void
532 pragma_ungetc (arg)
533 int arg;
535 UNGETC (arg);
537 #endif
539 static int
540 read_line_number (num)
541 int *num;
543 register int token = yylex ();
545 if (token == CONSTANT
546 && TREE_CODE (yylval.ttype) == INTEGER_CST)
548 *num = TREE_INT_CST_LOW (yylval.ttype);
549 return 1;
551 else
553 if (token != END_OF_LINE)
554 error ("invalid #-line");
555 return 0;
559 /* At the beginning of a line, increment the line number
560 and process any #-directive on this line.
561 If the line is a #-directive, read the entire line and return a newline.
562 Otherwise, return the line's first non-whitespace character.
564 Note that in the case of USE_CPPLIB, we get the whole line as one
565 CPP_DIRECTIVE token. */
568 check_newline ()
570 register int c;
571 register int token;
572 int saw_line;
573 enum { act_none, act_push, act_pop } action;
574 int old_lineno, action_number, l;
576 restart:
577 /* Read first nonwhite char on the line. */
579 #ifdef USE_CPPLIB
580 c = GETC ();
581 /* In some cases where we're leaving an include file, we can get multiple
582 CPP_HSPACE tokens in a row, so we need to loop. */
583 while (cpp_token == CPP_HSPACE)
584 c = yy_get_token ();
585 #else
587 c = GETC ();
588 while (c == ' ' || c == '\t');
589 #endif
591 lineno++;
593 if (c != '#')
595 /* Sequences of multiple newlines are very common; optimize them. */
596 if (c == '\n')
597 goto restart;
599 /* If not #, return it so caller will use it. */
600 return c;
603 /* Don't read beyond this line. */
604 saw_line = 0;
605 linemode = 1;
607 #if USE_CPPLIB
608 if (cpp_token == CPP_VSPACE)
610 /* Format is "<space> <line number> <filename> <newline>".
611 Only the line number is interesting, and even that
612 we can get more efficiently than scanning the line. */
613 yy_cur = yy_lim - 1;
614 lineno = parse_in.lineno - 1;
615 goto skipline;
617 #endif
619 token = yylex ();
621 if (token == IDENTIFIER)
623 /* If a letter follows, then if the word here is `line', skip
624 it and ignore it; otherwise, ignore the line, with an error
625 if the word isn't `pragma'. */
627 const char *name = IDENTIFIER_POINTER (yylval.ttype);
629 if (!strcmp (name, "pragma"))
631 token = yylex ();
632 if (token != IDENTIFIER
633 || TREE_CODE (yylval.ttype) != IDENTIFIER_NODE)
634 goto skipline;
636 #ifdef HANDLE_PRAGMA
637 /* We invoke HANDLE_PRAGMA before HANDLE_GENERIC_PRAGMAS
638 (if both are defined), in order to give the back
639 end a chance to override the interpretation of
640 SYSV style pragmas. */
641 if (HANDLE_PRAGMA (getch, put_back,
642 IDENTIFIER_POINTER (yylval.ttype)))
643 goto skipline;
644 #endif /* HANDLE_PRAGMA */
646 #ifdef HANDLE_GENERIC_PRAGMAS
647 if (handle_generic_pragma (token))
648 goto skipline;
649 #endif /* HANDLE_GENERIC_PRAGMAS */
651 /* Issue a warning message if we have been asked to do so.
652 Ignoring unknown pragmas in system header file unless
653 an explcit -Wunknown-pragmas has been given. */
654 if (warn_unknown_pragmas > 1
655 || (warn_unknown_pragmas && ! in_system_header))
656 warning ("ignoring pragma: %s", token_buffer);
658 goto skipline;
660 else if (!strcmp (name, "define"))
662 debug_define (lineno, GET_DIRECTIVE_LINE ());
663 goto skipline;
665 else if (!strcmp (name, "undef"))
667 debug_undef (lineno, GET_DIRECTIVE_LINE ());
668 goto skipline;
670 else if (!strcmp (name, "line"))
672 saw_line = 1;
673 token = yylex ();
674 goto linenum;
676 else if (!strcmp (name, "ident"))
678 /* #ident. The pedantic warning is now in cccp.c. */
680 /* Here we have just seen `#ident '.
681 A string constant should follow. */
683 token = yylex ();
684 if (token == END_OF_LINE)
685 goto skipline;
686 if (token != STRING
687 || TREE_CODE (yylval.ttype) != STRING_CST)
689 error ("invalid #ident");
690 goto skipline;
693 if (! flag_no_ident)
695 #ifdef ASM_OUTPUT_IDENT
696 ASM_OUTPUT_IDENT (asm_out_file,
697 TREE_STRING_POINTER (yylval.ttype));
698 #endif
701 /* Skip the rest of this line. */
702 goto skipline;
705 error ("undefined or invalid # directive `%s'", name);
706 goto skipline;
709 /* If the # is the only nonwhite char on the line,
710 just ignore it. Check the new newline. */
711 if (token == END_OF_LINE)
712 goto skipline;
714 linenum:
715 /* Here we have either `#line' or `# <nonletter>'.
716 In either case, it should be a line number; a digit should follow. */
718 if (token != CONSTANT
719 || TREE_CODE (yylval.ttype) != INTEGER_CST)
721 error ("invalid #-line");
722 goto skipline;
725 /* subtract one, because it is the following line that
726 gets the specified number */
728 l = TREE_INT_CST_LOW (yylval.ttype) - 1;
730 /* More follows: it must be a string constant (filename).
731 It would be neat to use cpplib to quickly process the string, but
732 (1) we don't have a handy tokenization of the string, and
733 (2) I don't know how well that would work in the presense
734 of filenames that contain wide characters. */
736 if (saw_line)
738 /* Don't treat \ as special if we are processing #line 1 "...".
739 If you want it to be treated specially, use # 1 "...". */
740 ignore_escape_flag = 1;
743 /* Read the string constant. */
744 token = yylex ();
746 ignore_escape_flag = 0;
748 if (token == END_OF_LINE)
750 /* No more: store the line number and check following line. */
751 lineno = l;
752 goto skipline;
755 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
757 error ("invalid #line");
758 goto skipline;
761 if (! ggc_p && !TREE_PERMANENT (yylval.ttype))
763 input_filename
764 = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
765 strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
767 else
768 input_filename = TREE_STRING_POINTER (yylval.ttype);
770 if (main_input_filename == 0)
771 main_input_filename = input_filename;
773 old_lineno = lineno;
774 action = act_none;
775 action_number = 0;
776 lineno = l;
778 /* Each change of file name
779 reinitializes whether we are now in a system header. */
780 in_system_header = 0;
782 if (!read_line_number (&action_number))
784 /* Update the name in the top element of input_file_stack. */
785 if (input_file_stack)
786 input_file_stack->name = input_filename;
789 /* `1' after file name means entering new file.
790 `2' after file name means just left a file. */
792 if (action_number == 1)
794 action = act_push;
795 read_line_number (&action_number);
797 else if (action_number == 2)
799 action = act_pop;
800 read_line_number (&action_number);
802 if (action_number == 3)
804 /* `3' after file name means this is a system header file. */
805 in_system_header = 1;
806 read_line_number (&action_number);
809 /* Do the actions implied by the preceding numbers. */
811 if (action == act_push)
813 /* Pushing to a new file. */
814 struct file_stack *p
815 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
816 input_file_stack->line = old_lineno;
817 p->next = input_file_stack;
818 p->name = input_filename;
819 p->indent_level = indent_level;
820 input_file_stack = p;
821 input_file_stack_tick++;
822 debug_start_source_file (input_filename);
824 else if (action == act_pop)
826 /* Popping out of a file. */
827 if (input_file_stack->next)
829 struct file_stack *p = input_file_stack;
830 if (indent_level != p->indent_level)
832 warning_with_file_and_line
833 (p->name, old_lineno,
834 "This file contains more `%c's than `%c's.",
835 indent_level > p->indent_level ? '{' : '}',
836 indent_level > p->indent_level ? '}' : '{');
838 input_file_stack = p->next;
839 free (p);
840 input_file_stack_tick++;
841 debug_end_source_file (input_file_stack->line);
843 else
844 error ("#-lines for entering and leaving files don't match");
847 /* Now that we've pushed or popped the input stack,
848 update the name in the top element. */
849 if (input_file_stack)
850 input_file_stack->name = input_filename;
852 /* skip the rest of this line. */
853 skipline:
854 linemode = 0;
855 end_of_file = 0;
858 c = GETC();
859 while (c != '\n' && c != EOF);
860 return c;
863 #ifdef HANDLE_GENERIC_PRAGMAS
865 /* Handle a #pragma directive.
866 TOKEN is the token we read after `#pragma'. Processes the entire input
867 line and return non-zero iff the pragma has been successfully parsed. */
869 /* This function has to be in this file, in order to get at
870 the token types. */
872 static int
873 handle_generic_pragma (token)
874 register int token;
876 for (;;)
878 switch (token)
880 case IDENTIFIER:
881 case TYPENAME:
882 case STRING:
883 case CONSTANT:
884 handle_pragma_token (token_buffer, yylval.ttype);
885 break;
887 case END_OF_LINE:
888 return handle_pragma_token (NULL_PTR, NULL_TREE);
890 default:
891 handle_pragma_token (token_buffer, NULL);
894 token = yylex ();
898 #endif /* HANDLE_GENERIC_PRAGMAS */
900 #define ENDFILE -1 /* token that represents end-of-file */
902 /* Read an escape sequence, returning its equivalent as a character,
903 or store 1 in *ignore_ptr if it is backslash-newline. */
905 static int
906 readescape (ignore_ptr)
907 int *ignore_ptr;
909 register int c = GETC();
910 register int code;
911 register unsigned count;
912 unsigned firstdig = 0;
913 int nonnull;
915 switch (c)
917 case 'x':
918 if (warn_traditional)
919 warning ("the meaning of `\\x' varies with -traditional");
921 if (flag_traditional)
922 return c;
924 code = 0;
925 count = 0;
926 nonnull = 0;
927 while (1)
929 c = GETC();
930 if (! ISXDIGIT (c))
932 UNGETC (c);
933 break;
935 code *= 16;
936 if (c >= 'a' && c <= 'f')
937 code += c - 'a' + 10;
938 if (c >= 'A' && c <= 'F')
939 code += c - 'A' + 10;
940 if (c >= '0' && c <= '9')
941 code += c - '0';
942 if (code != 0 || count != 0)
944 if (count == 0)
945 firstdig = code;
946 count++;
948 nonnull = 1;
950 if (! nonnull)
951 error ("\\x used with no following hex digits");
952 else if (count == 0)
953 /* Digits are all 0's. Ok. */
955 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
956 || (count > 1
957 && (((unsigned)1
958 << (TYPE_PRECISION (integer_type_node)
959 - (count - 1) * 4))
960 <= firstdig)))
961 pedwarn ("hex escape out of range");
962 return code;
964 case '0': case '1': case '2': case '3': case '4':
965 case '5': case '6': case '7':
966 code = 0;
967 count = 0;
968 while ((c <= '7') && (c >= '0') && (count++ < 3))
970 code = (code * 8) + (c - '0');
971 c = GETC();
973 UNGETC (c);
974 return code;
976 case '\\': case '\'': case '"':
977 return c;
979 case '\n':
980 lineno++;
981 *ignore_ptr = 1;
982 return 0;
984 case 'n':
985 return TARGET_NEWLINE;
987 case 't':
988 return TARGET_TAB;
990 case 'r':
991 return TARGET_CR;
993 case 'f':
994 return TARGET_FF;
996 case 'b':
997 return TARGET_BS;
999 case 'a':
1000 if (warn_traditional)
1001 warning ("the meaning of `\\a' varies with -traditional");
1003 if (flag_traditional)
1004 return c;
1005 return TARGET_BELL;
1007 case 'v':
1008 #if 0 /* Vertical tab is present in common usage compilers. */
1009 if (flag_traditional)
1010 return c;
1011 #endif
1012 return TARGET_VT;
1014 case 'e':
1015 case 'E':
1016 if (pedantic)
1017 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
1018 return TARGET_ESC;
1020 case '?':
1021 return c;
1023 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
1024 case '(':
1025 case '{':
1026 case '[':
1027 /* `\%' is used to prevent SCCS from getting confused. */
1028 case '%':
1029 if (pedantic)
1030 pedwarn ("unknown escape sequence `\\%c'", c);
1031 return c;
1033 if (ISGRAPH (c))
1034 pedwarn ("unknown escape sequence `\\%c'", c);
1035 else
1036 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
1037 return c;
1040 void
1041 yyerror (msgid)
1042 const char *msgid;
1044 const char *string = _(msgid);
1046 /* We can't print string and character constants well
1047 because the token_buffer contains the result of processing escapes. */
1048 if (end_of_file)
1049 error ("%s at end of input", string);
1050 else if (token_buffer[0] == 0)
1051 error ("%s at null character", string);
1052 else if (token_buffer[0] == '"')
1053 error ("%s before string constant", string);
1054 else if (token_buffer[0] == '\'')
1055 error ("%s before character constant", string);
1056 else if (!ISGRAPH(token_buffer[0]))
1057 error ("%s before character 0%o", string, (unsigned char) token_buffer[0]);
1058 else
1059 error ("%s before `%s'", string, token_buffer);
1062 #if 0
1064 struct try_type
1066 tree *node_var;
1067 char unsigned_flag;
1068 char long_flag;
1069 char long_long_flag;
1072 struct try_type type_sequence[] =
1074 { &integer_type_node, 0, 0, 0},
1075 { &unsigned_type_node, 1, 0, 0},
1076 { &long_integer_type_node, 0, 1, 0},
1077 { &long_unsigned_type_node, 1, 1, 0},
1078 { &long_long_integer_type_node, 0, 1, 1},
1079 { &long_long_unsigned_type_node, 1, 1, 1}
1081 #endif /* 0 */
1083 struct pf_args
1085 /* Input */
1086 int base;
1087 char * p;
1088 /* I/O */
1089 int c;
1090 /* Output */
1091 int imag;
1092 tree type;
1093 int conversion_errno;
1094 REAL_VALUE_TYPE value;
1097 static void
1098 parse_float (data)
1099 PTR data;
1101 struct pf_args * args = (struct pf_args *) data;
1102 int fflag = 0, lflag = 0;
1103 /* Copy token_buffer now, while it has just the number
1104 and not the suffixes; once we add `f' or `i',
1105 REAL_VALUE_ATOF may not work any more. */
1106 char *copy = (char *) alloca (args->p - token_buffer + 1);
1107 bcopy (token_buffer, copy, args->p - token_buffer + 1);
1108 args->imag = 0;
1109 args->conversion_errno = 0;
1110 args->type = double_type_node;
1112 while (1)
1114 int lose = 0;
1116 /* Read the suffixes to choose a data type. */
1117 switch (args->c)
1119 case 'f': case 'F':
1120 if (fflag)
1121 error ("more than one `f' in numeric constant");
1122 fflag = 1;
1123 break;
1125 case 'l': case 'L':
1126 if (lflag)
1127 error ("more than one `l' in numeric constant");
1128 lflag = 1;
1129 break;
1131 case 'i': case 'I':
1132 if (args->imag)
1133 error ("more than one `i' or `j' in numeric constant");
1134 else if (pedantic)
1135 pedwarn ("ANSI C forbids imaginary numeric constants");
1136 args->imag = 1;
1137 break;
1139 default:
1140 lose = 1;
1143 if (lose)
1144 break;
1146 if (args->p >= token_buffer + maxtoken - 3)
1147 args->p = extend_token_buffer (args->p);
1148 *(args->p++) = args->c;
1149 *(args->p) = 0;
1150 args->c = GETC();
1153 /* The second argument, machine_mode, of REAL_VALUE_ATOF
1154 tells the desired precision of the binary result
1155 of decimal-to-binary conversion. */
1157 if (fflag)
1159 if (lflag)
1160 error ("both `f' and `l' in floating constant");
1162 args->type = float_type_node;
1163 errno = 0;
1164 if (args->base == 16)
1165 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1166 else
1167 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1168 args->conversion_errno = errno;
1169 /* A diagnostic is required here by some ANSI C testsuites.
1170 This is not pedwarn, because some people don't want
1171 an error for this. */
1172 if (REAL_VALUE_ISINF (args->value) && pedantic)
1173 warning ("floating point number exceeds range of `float'");
1175 else if (lflag)
1177 args->type = long_double_type_node;
1178 errno = 0;
1179 if (args->base == 16)
1180 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1181 else
1182 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1183 args->conversion_errno = errno;
1184 if (REAL_VALUE_ISINF (args->value) && pedantic)
1185 warning ("floating point number exceeds range of `long double'");
1187 else
1189 errno = 0;
1190 if (args->base == 16)
1191 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1192 else
1193 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1194 args->conversion_errno = errno;
1195 if (REAL_VALUE_ISINF (args->value) && pedantic)
1196 warning ("floating point number exceeds range of `double'");
1200 /* Get the next character, staying within the current token if possible.
1201 If we're lexing a token, we don't want to look beyond the end of the
1202 token cpplib has prepared for us; otherwise, we end up reading in the
1203 next token, which screws up feed_input. So just return a null
1204 character. */
1206 static inline int token_getch PROTO ((void));
1208 static inline int
1209 token_getch ()
1211 #if USE_CPPLIB
1212 if (yy_cur == yy_lim)
1213 return '\0';
1214 #endif
1215 return GETC ();
1218 static inline void token_put_back PROTO ((int));
1220 static inline void
1221 token_put_back (ch)
1222 int ch;
1224 #if USE_CPPLIB
1225 if (ch == '\0')
1226 return;
1227 #endif
1228 UNGETC (ch);
1231 /* Read a single token from the input stream, and assign it lexical
1232 semantics. */
1235 yylex ()
1237 register int c;
1238 register char *p;
1239 register int value;
1240 int wide_flag = 0;
1241 int objc_flag = 0;
1243 c = GETC();
1245 /* Effectively do c = skip_white_space (c)
1246 but do it faster in the usual cases. */
1247 while (1)
1248 switch (c)
1250 case ' ':
1251 case '\t':
1252 case '\f':
1253 case '\v':
1254 case '\b':
1255 #if USE_CPPLIB
1256 if (cpp_token == CPP_HSPACE)
1257 c = yy_get_token ();
1258 else
1259 #endif
1260 c = GETC();
1261 break;
1263 case '\r':
1264 /* Call skip_white_space so we can warn if appropriate. */
1266 case '\n':
1267 case '/':
1268 case '\\':
1269 c = skip_white_space (c);
1270 default:
1271 goto found_nonwhite;
1273 found_nonwhite:
1275 token_buffer[0] = c;
1276 token_buffer[1] = 0;
1278 /* yylloc.first_line = lineno; */
1280 switch (c)
1282 case EOF:
1283 end_of_file = 1;
1284 token_buffer[0] = 0;
1285 if (linemode)
1286 value = END_OF_LINE;
1287 else
1288 value = ENDFILE;
1289 break;
1291 case 'L':
1292 #if USE_CPPLIB
1293 if (cpp_token == CPP_NAME)
1294 goto letter;
1295 #endif
1296 /* Capital L may start a wide-string or wide-character constant. */
1298 register int c = token_getch();
1299 if (c == '\'')
1301 wide_flag = 1;
1302 goto char_constant;
1304 if (c == '"')
1306 wide_flag = 1;
1307 goto string_constant;
1309 token_put_back (c);
1311 goto letter;
1313 case '@':
1314 if (!doing_objc_thang)
1316 value = c;
1317 break;
1319 else
1321 /* '@' may start a constant string object. */
1322 register int c = token_getch ();
1323 if (c == '"')
1325 objc_flag = 1;
1326 goto string_constant;
1328 token_put_back (c);
1329 /* Fall through to treat '@' as the start of an identifier. */
1332 case 'A': case 'B': case 'C': case 'D': case 'E':
1333 case 'F': case 'G': case 'H': case 'I': case 'J':
1334 case 'K': case 'M': case 'N': case 'O':
1335 case 'P': case 'Q': case 'R': case 'S': case 'T':
1336 case 'U': case 'V': case 'W': case 'X': case 'Y':
1337 case 'Z':
1338 case 'a': case 'b': case 'c': case 'd': case 'e':
1339 case 'f': case 'g': case 'h': case 'i': case 'j':
1340 case 'k': case 'l': case 'm': case 'n': case 'o':
1341 case 'p': case 'q': case 'r': case 's': case 't':
1342 case 'u': case 'v': case 'w': case 'x': case 'y':
1343 case 'z':
1344 case '_':
1345 case '$':
1346 letter:
1347 #if USE_CPPLIB
1348 if (cpp_token == CPP_NAME)
1350 /* Note that one character has already been read from
1351 yy_cur into token_buffer. Also, cpplib complains about
1352 $ in identifiers, so we don't have to. */
1354 int len = yy_lim - yy_cur + 1;
1355 if (len >= maxtoken)
1356 extend_token_buffer_to (len + 1);
1357 memcpy (token_buffer + 1, yy_cur, len);
1358 p = token_buffer + len;
1359 yy_cur = yy_lim;
1361 else
1362 #endif
1364 p = token_buffer;
1365 while (ISALNUM (c) || c == '_' || c == '$' || c == '@')
1367 /* Make sure this char really belongs in an identifier. */
1368 if (c == '$')
1370 if (! dollars_in_ident)
1371 error ("`$' in identifier");
1372 else if (pedantic)
1373 pedwarn ("`$' in identifier");
1376 if (p >= token_buffer + maxtoken)
1377 p = extend_token_buffer (p);
1379 *p++ = c;
1380 c = token_getch();
1383 *p = 0;
1384 token_put_back (c);
1387 value = IDENTIFIER;
1388 yylval.itype = 0;
1390 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1393 register struct resword *ptr;
1395 if ((ptr = is_reserved_word (token_buffer, p - token_buffer)))
1397 if (ptr->rid)
1398 yylval.ttype = ridpointers[(int) ptr->rid];
1399 value = (int) ptr->token;
1401 /* Only return OBJECTNAME if it is a typedef. */
1402 if (doing_objc_thang && value == OBJECTNAME)
1404 lastiddecl = lookup_name(yylval.ttype);
1406 if (lastiddecl == NULL_TREE
1407 || TREE_CODE (lastiddecl) != TYPE_DECL)
1408 value = IDENTIFIER;
1411 /* Even if we decided to recognize asm, still perhaps warn. */
1412 if (pedantic
1413 && (value == ASM_KEYWORD || value == TYPEOF
1414 || ptr->rid == RID_INLINE)
1415 && token_buffer[0] != '_')
1416 pedwarn ("ANSI does not permit the keyword `%s'",
1417 token_buffer);
1421 /* If we did not find a keyword, look for an identifier
1422 (or a typename). */
1424 if (value == IDENTIFIER)
1426 if (token_buffer[0] == '@')
1427 error("invalid identifier `%s'", token_buffer);
1429 yylval.ttype = get_identifier (token_buffer);
1430 lastiddecl = lookup_name (yylval.ttype);
1432 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1433 value = TYPENAME;
1434 /* A user-invisible read-only initialized variable
1435 should be replaced by its value.
1436 We handle only strings since that's the only case used in C. */
1437 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1438 && DECL_IGNORED_P (lastiddecl)
1439 && TREE_READONLY (lastiddecl)
1440 && DECL_INITIAL (lastiddecl) != 0
1441 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1443 tree stringval = DECL_INITIAL (lastiddecl);
1445 /* Copy the string value so that we won't clobber anything
1446 if we put something in the TREE_CHAIN of this one. */
1447 yylval.ttype = build_string (TREE_STRING_LENGTH (stringval),
1448 TREE_STRING_POINTER (stringval));
1449 value = STRING;
1451 else if (doing_objc_thang)
1453 tree objc_interface_decl = is_class_name (yylval.ttype);
1455 if (objc_interface_decl)
1457 value = CLASSNAME;
1458 yylval.ttype = objc_interface_decl;
1463 break;
1465 case '.':
1466 #if USE_CPPLIB
1467 if (yy_cur < yy_lim)
1468 #endif
1470 /* It's hard to preserve tokenization on '.' because
1471 it could be a symbol by itself, or it could be the
1472 start of a floating point number and cpp won't tell us. */
1473 register int c1 = token_getch ();
1474 token_buffer[1] = c1;
1475 if (c1 == '.')
1477 c1 = token_getch ();
1478 if (c1 == '.')
1480 token_buffer[2] = c1;
1481 token_buffer[3] = 0;
1482 value = ELLIPSIS;
1483 goto done;
1485 error ("parse error at `..'");
1487 if (ISDIGIT (c1))
1489 token_put_back (c1);
1490 goto number;
1492 token_put_back (c1);
1494 value = '.';
1495 token_buffer[1] = 0;
1496 break;
1498 case '0': case '1':
1499 /* Optimize for most frequent case. */
1501 register int cond;
1503 #if USE_CPPLIB
1504 cond = (yy_cur == yy_lim);
1505 #else
1506 register int c1 = token_getch ();
1507 token_put_back (c1);
1508 cond = (! ISALNUM (c1) && c1 != '.');
1509 #endif
1510 if (cond)
1512 yylval.ttype = (c == '0') ? integer_zero_node : integer_one_node;
1513 value = CONSTANT;
1514 break;
1516 /*FALLTHRU*/
1518 case '2': case '3': case '4':
1519 case '5': case '6': case '7': case '8': case '9':
1520 number:
1522 int base = 10;
1523 int count = 0;
1524 int largest_digit = 0;
1525 int numdigits = 0;
1526 int overflow = 0;
1528 /* We actually store only HOST_BITS_PER_CHAR bits in each part.
1529 The code below which fills the parts array assumes that a host
1530 int is at least twice as wide as a host char, and that
1531 HOST_BITS_PER_WIDE_INT is an even multiple of HOST_BITS_PER_CHAR.
1532 Two HOST_WIDE_INTs is the largest int literal we can store.
1533 In order to detect overflow below, the number of parts (TOTAL_PARTS)
1534 must be exactly the number of parts needed to hold the bits
1535 of two HOST_WIDE_INTs. */
1536 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2)
1537 unsigned int parts[TOTAL_PARTS];
1539 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS, AFTER_EXPON}
1540 floatflag = NOT_FLOAT;
1542 for (count = 0; count < TOTAL_PARTS; count++)
1543 parts[count] = 0;
1545 p = token_buffer;
1546 *p++ = c;
1548 if (c == '0')
1550 *p++ = (c = token_getch());
1551 if ((c == 'x') || (c == 'X'))
1553 base = 16;
1554 *p++ = (c = token_getch());
1556 /* Leading 0 forces octal unless the 0 is the only digit. */
1557 else if (c >= '0' && c <= '9')
1559 base = 8;
1560 numdigits++;
1562 else
1563 numdigits++;
1566 /* Read all the digits-and-decimal-points. */
1568 while (c == '.'
1569 || (ISALNUM (c) && c != 'l' && c != 'L'
1570 && c != 'u' && c != 'U'
1571 && c != 'i' && c != 'I' && c != 'j' && c != 'J'
1572 && (floatflag == NOT_FLOAT
1573 || ((base != 16) && (c != 'f') && (c != 'F'))
1574 || base == 16)))
1576 if (c == '.')
1578 if (base == 16 && pedantic)
1579 pedwarn ("floating constant may not be in radix 16");
1580 if (floatflag == TOO_MANY_POINTS)
1581 /* We have already emitted an error. Don't need another. */
1583 else if (floatflag == AFTER_POINT || floatflag == AFTER_EXPON)
1585 error ("malformed floating constant");
1586 floatflag = TOO_MANY_POINTS;
1587 /* Avoid another error from atof by forcing all characters
1588 from here on to be ignored. */
1589 p[-1] = '\0';
1591 else
1592 floatflag = AFTER_POINT;
1594 if (base == 8)
1595 base = 10;
1596 *p++ = c = token_getch();
1597 /* Accept '.' as the start of a floating-point number
1598 only when it is followed by a digit. */
1599 if (p == token_buffer + 2 && !ISDIGIT (c))
1600 abort ();
1602 else
1604 /* It is not a decimal point.
1605 It should be a digit (perhaps a hex digit). */
1607 if (ISDIGIT (c))
1609 c = c - '0';
1611 else if (base <= 10)
1613 if (c == 'e' || c == 'E')
1615 base = 10;
1616 floatflag = AFTER_EXPON;
1617 break; /* start of exponent */
1619 error ("nondigits in number and not hexadecimal");
1620 c = 0;
1622 else if (base == 16 && (c == 'p' || c == 'P'))
1624 floatflag = AFTER_EXPON;
1625 break; /* start of exponent */
1627 else if (c >= 'a')
1629 c = c - 'a' + 10;
1631 else
1633 c = c - 'A' + 10;
1635 if (c >= largest_digit)
1636 largest_digit = c;
1637 numdigits++;
1639 for (count = 0; count < TOTAL_PARTS; count++)
1641 parts[count] *= base;
1642 if (count)
1644 parts[count]
1645 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1646 parts[count-1]
1647 &= (1 << HOST_BITS_PER_CHAR) - 1;
1649 else
1650 parts[0] += c;
1653 /* If the highest-order part overflows (gets larger than
1654 a host char will hold) then the whole number has
1655 overflowed. Record this and truncate the highest-order
1656 part. */
1657 if (parts[TOTAL_PARTS - 1] >> HOST_BITS_PER_CHAR)
1659 overflow = 1;
1660 parts[TOTAL_PARTS - 1] &= (1 << HOST_BITS_PER_CHAR) - 1;
1663 if (p >= token_buffer + maxtoken - 3)
1664 p = extend_token_buffer (p);
1665 *p++ = (c = token_getch());
1669 /* This can happen on input like `int i = 0x;' */
1670 if (numdigits == 0)
1671 error ("numeric constant with no digits");
1673 if (largest_digit >= base)
1674 error ("numeric constant contains digits beyond the radix");
1676 /* Remove terminating char from the token buffer and delimit the
1677 string. */
1678 *--p = 0;
1680 if (floatflag != NOT_FLOAT)
1682 tree type;
1683 int imag, conversion_errno;
1684 REAL_VALUE_TYPE value;
1685 struct pf_args args;
1687 /* Read explicit exponent if any, and put it in tokenbuf. */
1689 if ((base == 10 && ((c == 'e') || (c == 'E')))
1690 || (base == 16 && (c == 'p' || c == 'P')))
1692 if (p >= token_buffer + maxtoken - 3)
1693 p = extend_token_buffer (p);
1694 *p++ = c;
1695 c = token_getch();
1696 if ((c == '+') || (c == '-'))
1698 *p++ = c;
1699 c = token_getch();
1701 /* Exponent is decimal, even if string is a hex float. */
1702 if (! ISDIGIT (c))
1703 error ("floating constant exponent has no digits");
1704 while (ISDIGIT (c))
1706 if (p >= token_buffer + maxtoken - 3)
1707 p = extend_token_buffer (p);
1708 *p++ = c;
1709 c = token_getch ();
1712 if (base == 16 && floatflag != AFTER_EXPON)
1713 error ("hexadecimal floating constant has no exponent");
1715 *p = 0;
1717 /* Setup input for parse_float() */
1718 args.base = base;
1719 args.p = p;
1720 args.c = c;
1722 /* Convert string to a double, checking for overflow. */
1723 if (do_float_handler (parse_float, (PTR) &args))
1725 /* Receive output from parse_float() */
1726 value = args.value;
1728 else
1730 /* We got an exception from parse_float() */
1731 error ("floating constant out of range");
1732 value = dconst0;
1735 /* Receive output from parse_float() */
1736 c = args.c;
1737 imag = args.imag;
1738 type = args.type;
1739 conversion_errno = args.conversion_errno;
1741 #ifdef ERANGE
1742 /* ERANGE is also reported for underflow,
1743 so test the value to distinguish overflow from that. */
1744 if (conversion_errno == ERANGE && !flag_traditional && pedantic
1745 && (REAL_VALUES_LESS (dconst1, value)
1746 || REAL_VALUES_LESS (value, dconstm1)))
1747 warning ("floating point number exceeds range of `double'");
1748 #endif
1750 /* If the result is not a number, assume it must have been
1751 due to some error message above, so silently convert
1752 it to a zero. */
1753 if (REAL_VALUE_ISNAN (value))
1754 value = dconst0;
1756 /* Create a node with determined type and value. */
1757 if (imag)
1758 yylval.ttype = build_complex (NULL_TREE,
1759 convert (type, integer_zero_node),
1760 build_real (type, value));
1761 else
1762 yylval.ttype = build_real (type, value);
1764 else
1766 tree traditional_type, ansi_type, type;
1767 HOST_WIDE_INT high, low;
1768 int spec_unsigned = 0;
1769 int spec_long = 0;
1770 int spec_long_long = 0;
1771 int spec_imag = 0;
1772 int warn = 0, i;
1774 traditional_type = ansi_type = type = NULL_TREE;
1775 while (1)
1777 if (c == 'u' || c == 'U')
1779 if (spec_unsigned)
1780 error ("two `u's in integer constant");
1781 spec_unsigned = 1;
1783 else if (c == 'l' || c == 'L')
1785 if (spec_long)
1787 if (spec_long_long)
1788 error ("three `l's in integer constant");
1789 else if (pedantic && ! in_system_header && warn_long_long)
1790 pedwarn ("ANSI C forbids long long integer constants");
1791 spec_long_long = 1;
1793 spec_long = 1;
1795 else if (c == 'i' || c == 'j' || c == 'I' || c == 'J')
1797 if (spec_imag)
1798 error ("more than one `i' or `j' in numeric constant");
1799 else if (pedantic)
1800 pedwarn ("ANSI C forbids imaginary numeric constants");
1801 spec_imag = 1;
1803 else
1804 break;
1805 if (p >= token_buffer + maxtoken - 3)
1806 p = extend_token_buffer (p);
1807 *p++ = c;
1808 c = token_getch();
1811 /* If the literal overflowed, pedwarn about it now. */
1812 if (overflow)
1814 warn = 1;
1815 pedwarn ("integer constant is too large for this configuration of the compiler - truncated to %d bits", HOST_BITS_PER_WIDE_INT * 2);
1818 /* This is simplified by the fact that our constant
1819 is always positive. */
1821 high = low = 0;
1823 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1825 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1826 / HOST_BITS_PER_CHAR)]
1827 << (i * HOST_BITS_PER_CHAR));
1828 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1831 yylval.ttype = build_int_2 (low, high);
1832 TREE_TYPE (yylval.ttype) = long_long_unsigned_type_node;
1834 /* If warn_traditional, calculate both the ANSI type and the
1835 traditional type, then see if they disagree.
1836 Otherwise, calculate only the type for the dialect in use. */
1837 if (warn_traditional || flag_traditional)
1839 /* Calculate the traditional type. */
1840 /* Traditionally, any constant is signed;
1841 but if unsigned is specified explicitly, obey that.
1842 Use the smallest size with the right number of bits,
1843 except for one special case with decimal constants. */
1844 if (! spec_long && base != 10
1845 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1846 traditional_type = (spec_unsigned ? unsigned_type_node
1847 : integer_type_node);
1848 /* A decimal constant must be long
1849 if it does not fit in type int.
1850 I think this is independent of whether
1851 the constant is signed. */
1852 else if (! spec_long && base == 10
1853 && int_fits_type_p (yylval.ttype, integer_type_node))
1854 traditional_type = (spec_unsigned ? unsigned_type_node
1855 : integer_type_node);
1856 else if (! spec_long_long)
1857 traditional_type = (spec_unsigned ? long_unsigned_type_node
1858 : long_integer_type_node);
1859 else if (int_fits_type_p (yylval.ttype,
1860 spec_unsigned
1861 ? long_long_unsigned_type_node
1862 : long_long_integer_type_node))
1863 traditional_type = (spec_unsigned
1864 ? long_long_unsigned_type_node
1865 : long_long_integer_type_node);
1866 else
1867 traditional_type = (spec_unsigned
1868 ? widest_unsigned_literal_type_node
1869 : widest_integer_literal_type_node);
1871 if (warn_traditional || ! flag_traditional)
1873 /* Calculate the ANSI type. */
1874 if (! spec_long && ! spec_unsigned
1875 && int_fits_type_p (yylval.ttype, integer_type_node))
1876 ansi_type = integer_type_node;
1877 else if (! spec_long && (base != 10 || spec_unsigned)
1878 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1879 ansi_type = unsigned_type_node;
1880 else if (! spec_unsigned && !spec_long_long
1881 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1882 ansi_type = long_integer_type_node;
1883 else if (! spec_long_long
1884 && int_fits_type_p (yylval.ttype,
1885 long_unsigned_type_node))
1886 ansi_type = long_unsigned_type_node;
1887 else if (! spec_unsigned
1888 && int_fits_type_p (yylval.ttype,
1889 long_long_integer_type_node))
1890 ansi_type = long_long_integer_type_node;
1891 else if (int_fits_type_p (yylval.ttype,
1892 long_long_unsigned_type_node))
1893 ansi_type = long_long_unsigned_type_node;
1894 else if (! spec_unsigned
1895 && int_fits_type_p (yylval.ttype,
1896 widest_integer_literal_type_node))
1897 ansi_type = widest_integer_literal_type_node;
1898 else
1899 ansi_type = widest_unsigned_literal_type_node;
1902 type = flag_traditional ? traditional_type : ansi_type;
1904 if (warn_traditional && traditional_type != ansi_type)
1906 if (TYPE_PRECISION (traditional_type)
1907 != TYPE_PRECISION (ansi_type))
1908 warning ("width of integer constant changes with -traditional");
1909 else if (TREE_UNSIGNED (traditional_type)
1910 != TREE_UNSIGNED (ansi_type))
1911 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
1912 else
1913 warning ("width of integer constant may change on other systems with -traditional");
1916 if (pedantic && !flag_traditional && !spec_long_long && !warn
1917 && (TYPE_PRECISION (long_integer_type_node)
1918 < TYPE_PRECISION (type)))
1920 warn = 1;
1921 pedwarn ("integer constant larger than the maximum value of an unsigned long int");
1924 if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type))
1925 warning ("decimal constant is so large that it is unsigned");
1927 if (spec_imag)
1929 if (TYPE_PRECISION (type)
1930 <= TYPE_PRECISION (integer_type_node))
1931 yylval.ttype
1932 = build_complex (NULL_TREE, integer_zero_node,
1933 convert (integer_type_node,
1934 yylval.ttype));
1935 else
1936 error ("complex integer constant is too wide for `complex int'");
1938 else if (flag_traditional && !int_fits_type_p (yylval.ttype, type))
1939 /* The traditional constant 0x80000000 is signed
1940 but doesn't fit in the range of int.
1941 This will change it to -0x80000000, which does fit. */
1943 TREE_TYPE (yylval.ttype) = unsigned_type (type);
1944 yylval.ttype = convert (type, yylval.ttype);
1945 TREE_OVERFLOW (yylval.ttype)
1946 = TREE_CONSTANT_OVERFLOW (yylval.ttype) = 0;
1948 else
1949 TREE_TYPE (yylval.ttype) = type;
1952 /* If it's still an integer (not a complex), and it doesn't
1953 fit in the type we choose for it, then pedwarn. */
1955 if (! warn
1956 && TREE_CODE (TREE_TYPE (yylval.ttype)) == INTEGER_TYPE
1957 && ! int_fits_type_p (yylval.ttype, TREE_TYPE (yylval.ttype)))
1958 pedwarn ("integer constant is larger than the maximum value for its type");
1961 token_put_back (c);
1962 *p = 0;
1964 if (ISALNUM (c) || c == '.' || c == '_' || c == '$'
1965 || (!flag_traditional && (c == '-' || c == '+')
1966 && (p[-1] == 'e' || p[-1] == 'E')))
1967 error ("missing white space after number `%s'", token_buffer);
1969 value = CONSTANT; break;
1972 case '\'':
1973 char_constant:
1975 register int result = 0;
1976 register int num_chars = 0;
1977 int chars_seen = 0;
1978 unsigned width = TYPE_PRECISION (char_type_node);
1979 int max_chars;
1980 #ifdef MULTIBYTE_CHARS
1981 int longest_char = local_mb_cur_max ();
1982 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
1983 #endif
1985 max_chars = TYPE_PRECISION (integer_type_node) / width;
1986 if (wide_flag)
1987 width = WCHAR_TYPE_SIZE;
1989 while (1)
1991 tryagain:
1992 c = token_getch();
1994 if (c == '\'' || c == EOF)
1995 break;
1997 ++chars_seen;
1998 if (c == '\\')
2000 int ignore = 0;
2001 c = readescape (&ignore);
2002 if (ignore)
2003 goto tryagain;
2004 if (width < HOST_BITS_PER_INT
2005 && (unsigned) c >= ((unsigned)1 << width))
2006 pedwarn ("escape sequence out of range for character");
2007 #ifdef MAP_CHARACTER
2008 if (ISPRINT (c))
2009 c = MAP_CHARACTER (c);
2010 #endif
2012 else if (c == '\n')
2014 if (pedantic)
2015 pedwarn ("ANSI C forbids newline in character constant");
2016 lineno++;
2018 else
2020 #ifdef MULTIBYTE_CHARS
2021 wchar_t wc;
2022 int i;
2023 int char_len = -1;
2024 for (i = 1; i <= longest_char; ++i)
2026 if (i > maxtoken - 4)
2027 extend_token_buffer (token_buffer);
2029 token_buffer[i] = c;
2030 char_len = local_mbtowc (& wc,
2031 token_buffer + 1,
2033 if (char_len != -1)
2034 break;
2035 c = token_getch ();
2037 if (char_len > 1)
2039 /* mbtowc sometimes needs an extra char before accepting */
2040 if (char_len < i)
2041 token_put_back (c);
2042 if (! wide_flag)
2044 /* Merge character into result; ignore excess chars. */
2045 for (i = 1; i <= char_len; ++i)
2047 if (i > max_chars)
2048 break;
2049 if (width < HOST_BITS_PER_INT)
2050 result = (result << width)
2051 | (token_buffer[i]
2052 & ((1 << width) - 1));
2053 else
2054 result = token_buffer[i];
2056 num_chars += char_len;
2057 goto tryagain;
2059 c = wc;
2061 else
2063 if (char_len == -1)
2065 warning ("Ignoring invalid multibyte character");
2066 /* Replace all but the first byte. */
2067 for (--i; i > 1; --i)
2068 token_put_back (token_buffer[i]);
2069 wc = token_buffer[1];
2071 #ifdef MAP_CHARACTER
2072 c = MAP_CHARACTER (wc);
2073 #else
2074 c = wc;
2075 #endif
2077 #else /* ! MULTIBYTE_CHARS */
2078 #ifdef MAP_CHARACTER
2079 c = MAP_CHARACTER (c);
2080 #endif
2081 #endif /* ! MULTIBYTE_CHARS */
2084 if (wide_flag)
2086 if (chars_seen == 1) /* only keep the first one */
2087 result = c;
2088 goto tryagain;
2091 /* Merge character into result; ignore excess chars. */
2092 num_chars += (width / TYPE_PRECISION (char_type_node));
2093 if (num_chars < max_chars + 1)
2095 if (width < HOST_BITS_PER_INT)
2096 result = (result << width) | (c & ((1 << width) - 1));
2097 else
2098 result = c;
2102 if (c != '\'')
2103 error ("malformed character constant");
2104 else if (chars_seen == 0)
2105 error ("empty character constant");
2106 else if (num_chars > max_chars)
2108 num_chars = max_chars;
2109 error ("character constant too long");
2111 else if (chars_seen != 1 && ! flag_traditional && warn_multichar)
2112 warning ("multi-character character constant");
2114 /* If char type is signed, sign-extend the constant. */
2115 if (! wide_flag)
2117 int num_bits = num_chars * width;
2118 if (num_bits == 0)
2119 /* We already got an error; avoid invalid shift. */
2120 yylval.ttype = build_int_2 (0, 0);
2121 else if (TREE_UNSIGNED (char_type_node)
2122 || ((result >> (num_bits - 1)) & 1) == 0)
2123 yylval.ttype
2124 = build_int_2 (result & (~(unsigned HOST_WIDE_INT) 0
2125 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
2127 else
2128 yylval.ttype
2129 = build_int_2 (result | ~(~(unsigned HOST_WIDE_INT) 0
2130 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
2131 -1);
2132 TREE_TYPE (yylval.ttype) = integer_type_node;
2134 else
2136 yylval.ttype = build_int_2 (result, 0);
2137 TREE_TYPE (yylval.ttype) = wchar_type_node;
2140 value = CONSTANT;
2141 break;
2144 case '"':
2145 string_constant:
2147 unsigned width = wide_flag ? WCHAR_TYPE_SIZE
2148 : TYPE_PRECISION (char_type_node);
2149 #ifdef MULTIBYTE_CHARS
2150 int longest_char = local_mb_cur_max ();
2151 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
2152 #endif
2153 c = token_getch ();
2154 p = token_buffer + 1;
2156 while (c != '"' && c != EOF)
2158 /* ignore_escape_flag is set for reading the filename in #line. */
2159 if (!ignore_escape_flag && c == '\\')
2161 int ignore = 0;
2162 c = readescape (&ignore);
2163 if (ignore)
2164 goto skipnewline;
2165 if (width < HOST_BITS_PER_INT
2166 && (unsigned) c >= ((unsigned)1 << width))
2167 pedwarn ("escape sequence out of range for character");
2169 else if (c == '\n')
2171 if (pedantic)
2172 pedwarn ("ANSI C forbids newline in string constant");
2173 lineno++;
2175 else
2177 #ifdef MULTIBYTE_CHARS
2178 wchar_t wc;
2179 int i;
2180 int char_len = -1;
2181 for (i = 0; i < longest_char; ++i)
2183 if (p + i >= token_buffer + maxtoken)
2184 p = extend_token_buffer (p);
2185 p[i] = c;
2187 char_len = local_mbtowc (& wc, p, i + 1);
2188 if (char_len != -1)
2189 break;
2190 c = token_getch ();
2192 if (char_len == -1)
2194 warning ("Ignoring invalid multibyte character");
2195 /* Replace all except the first byte. */
2196 token_put_back (c);
2197 for (--i; i > 0; --i)
2198 token_put_back (p[i]);
2199 char_len = 1;
2201 /* mbtowc sometimes needs an extra char before accepting */
2202 if (char_len <= i)
2203 token_put_back (c);
2204 if (! wide_flag)
2206 p += (i + 1);
2207 c = token_getch ();
2208 continue;
2210 c = wc;
2211 #endif /* MULTIBYTE_CHARS */
2214 /* Add this single character into the buffer either as a wchar_t
2215 or as a single byte. */
2216 if (wide_flag)
2218 unsigned width = TYPE_PRECISION (char_type_node);
2219 unsigned bytemask = (1 << width) - 1;
2220 int byte;
2222 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2223 p = extend_token_buffer (p);
2225 for (byte = 0; byte < WCHAR_BYTES; ++byte)
2227 int value;
2228 if (byte >= (int) sizeof (c))
2229 value = 0;
2230 else
2231 value = (c >> (byte * width)) & bytemask;
2232 if (BYTES_BIG_ENDIAN)
2233 p[WCHAR_BYTES - byte - 1] = value;
2234 else
2235 p[byte] = value;
2237 p += WCHAR_BYTES;
2239 else
2241 if (p >= token_buffer + maxtoken)
2242 p = extend_token_buffer (p);
2243 *p++ = c;
2246 skipnewline:
2247 c = token_getch ();
2250 /* Terminate the string value, either with a single byte zero
2251 or with a wide zero. */
2252 if (wide_flag)
2254 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2255 p = extend_token_buffer (p);
2256 bzero (p, WCHAR_BYTES);
2257 p += WCHAR_BYTES;
2259 else
2261 if (p >= token_buffer + maxtoken)
2262 p = extend_token_buffer (p);
2263 *p++ = 0;
2266 if (c == EOF)
2267 error ("Unterminated string constant");
2269 /* We have read the entire constant.
2270 Construct a STRING_CST for the result. */
2272 if (wide_flag)
2274 yylval.ttype = build_string (p - (token_buffer + 1),
2275 token_buffer + 1);
2276 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
2277 value = STRING;
2279 else if (objc_flag)
2281 /* Return an Objective-C @"..." constant string object. */
2282 yylval.ttype = build_objc_string (p - (token_buffer + 1),
2283 token_buffer + 1);
2284 TREE_TYPE (yylval.ttype) = char_array_type_node;
2285 value = OBJC_STRING;
2287 else
2289 yylval.ttype = build_string (p - (token_buffer + 1),
2290 token_buffer + 1);
2291 TREE_TYPE (yylval.ttype) = char_array_type_node;
2292 value = STRING;
2295 break;
2298 case '+':
2299 case '-':
2300 case '&':
2301 case '|':
2302 case ':':
2303 case '<':
2304 case '>':
2305 case '*':
2306 case '/':
2307 case '%':
2308 case '^':
2309 case '!':
2310 case '=':
2312 register int c1;
2314 combine:
2316 switch (c)
2318 case '+':
2319 yylval.code = PLUS_EXPR; break;
2320 case '-':
2321 yylval.code = MINUS_EXPR; break;
2322 case '&':
2323 yylval.code = BIT_AND_EXPR; break;
2324 case '|':
2325 yylval.code = BIT_IOR_EXPR; break;
2326 case '*':
2327 yylval.code = MULT_EXPR; break;
2328 case '/':
2329 yylval.code = TRUNC_DIV_EXPR; break;
2330 case '%':
2331 yylval.code = TRUNC_MOD_EXPR; break;
2332 case '^':
2333 yylval.code = BIT_XOR_EXPR; break;
2334 case LSHIFT:
2335 yylval.code = LSHIFT_EXPR; break;
2336 case RSHIFT:
2337 yylval.code = RSHIFT_EXPR; break;
2338 case '<':
2339 yylval.code = LT_EXPR; break;
2340 case '>':
2341 yylval.code = GT_EXPR; break;
2344 token_buffer[1] = c1 = token_getch();
2345 token_buffer[2] = 0;
2347 if (c1 == '=')
2349 switch (c)
2351 case '<':
2352 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
2353 case '>':
2354 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
2355 case '!':
2356 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
2357 case '=':
2358 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
2360 value = ASSIGN; goto done;
2362 else if (c == c1)
2363 switch (c)
2365 case '+':
2366 value = PLUSPLUS; goto done;
2367 case '-':
2368 value = MINUSMINUS; goto done;
2369 case '&':
2370 value = ANDAND; goto done;
2371 case '|':
2372 value = OROR; goto done;
2373 case '<':
2374 c = LSHIFT;
2375 goto combine;
2376 case '>':
2377 c = RSHIFT;
2378 goto combine;
2380 else
2381 switch (c)
2383 case '-':
2384 if (c1 == '>')
2385 { value = POINTSAT; goto done; }
2386 break;
2388 /* digraphs */
2389 case ':':
2390 if (c1 == '>')
2391 { value = ']'; goto done; }
2392 break;
2393 case '<':
2394 if (c1 == '%')
2395 { value = '{'; indent_level++; goto done; }
2396 if (c1 == ':')
2397 { value = '['; goto done; }
2398 break;
2399 case '%':
2400 if (c1 == '>')
2401 { value = '}'; indent_level--; goto done; }
2402 break;
2405 token_put_back (c1);
2406 token_buffer[1] = 0;
2408 if ((c == '<') || (c == '>'))
2409 value = ARITHCOMPARE;
2410 else value = c;
2411 break;
2414 case 0:
2415 /* Don't make yyparse think this is eof. */
2416 value = 1;
2417 break;
2419 case '{':
2420 indent_level++;
2421 value = c;
2422 break;
2424 case '}':
2425 indent_level--;
2426 value = c;
2427 break;
2429 default:
2430 value = c;
2433 done:
2434 /* yylloc.last_line = lineno; */
2436 return value;
2439 /* Sets the value of the 'yydebug' variable to VALUE.
2440 This is a function so we don't have to have YYDEBUG defined
2441 in order to build the compiler. */
2443 void
2444 set_yydebug (value)
2445 int value;
2447 #if YYDEBUG != 0
2448 yydebug = value;
2449 #else
2450 warning ("YYDEBUG not defined.");
2451 #endif