* rtl.h (rtunion_def): Constify member `rtstr'.
[official-gcc.git] / gcc / c-lex.c
blob6597ffc28959535866cf1c104cb8c899f9df7d8a
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 /* The elements of `ridpointers' are identifier nodes
66 for the reserved type names and storage classes.
67 It is indexed by a RID_... value. */
68 tree ridpointers[(int) RID_MAX];
70 /* Cause the `yydebug' variable to be defined. */
71 #define YYDEBUG 1
73 #if USE_CPPLIB
74 extern unsigned char *yy_cur, *yy_lim;
75 extern enum cpp_token cpp_token;
77 extern int yy_get_token ();
79 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
80 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
82 #else /* ! USE_CPPLIB */
84 #define GETC() getch ()
85 #define UNGETC(c) put_back (c)
87 struct putback_buffer {
88 unsigned char *buffer;
89 int buffer_size;
90 int index;
93 static struct putback_buffer putback = {NULL, 0, -1};
95 static inline int getch PARAMS ((void));
97 static inline int
98 getch ()
100 if (putback.index != -1)
102 int ch = putback.buffer[putback.index];
103 --putback.index;
104 return ch;
106 return getc (finput);
109 static inline void put_back PARAMS ((int));
111 static inline void
112 put_back (ch)
113 int ch;
115 if (ch != EOF)
117 if (putback.index == putback.buffer_size - 1)
119 putback.buffer_size += 16;
120 putback.buffer = xrealloc (putback.buffer, putback.buffer_size);
122 putback.buffer[++putback.index] = ch;
125 #endif /* ! USE_CPPLIB */
127 int linemode;
129 /* the declaration found for the last IDENTIFIER token read in.
130 yylex must look this up to detect typedefs, which get token type TYPENAME,
131 so it is left around in case the identifier is not a typedef but is
132 used in a context which makes it a reference to a variable. */
133 tree lastiddecl;
135 /* Nonzero enables objc features. */
137 int doing_objc_thang;
139 extern int yydebug;
141 /* File used for outputting assembler code. */
142 extern FILE *asm_out_file;
144 #undef WCHAR_TYPE_SIZE
145 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
147 /* Number of bytes in a wide character. */
148 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
150 static int maxtoken; /* Current nominal length of token buffer. */
151 char *token_buffer; /* Pointer to token buffer.
152 Actual allocated length is maxtoken + 2.
153 This is not static because objc-parse.y uses it. */
155 static int indent_level; /* Number of { minus number of }. */
157 /* Nonzero tells yylex to ignore \ in string constants. */
158 static int ignore_escape_flag;
160 /* Nonzero if end-of-file has been seen on input. */
161 static int end_of_file;
163 #ifdef HANDLE_GENERIC_PRAGMAS
164 static int handle_generic_pragma PARAMS ((int));
165 #endif /* HANDLE_GENERIC_PRAGMAS */
166 static int whitespace_cr PARAMS ((int));
167 static int skip_white_space PARAMS ((int));
168 static char *extend_token_buffer PARAMS ((const char *));
169 static int readescape PARAMS ((int *));
170 static void parse_float PARAMS ((PTR));
171 static void extend_token_buffer_to PARAMS ((int));
172 static int read_line_number PARAMS ((int *));
174 /* Do not insert generated code into the source, instead, include it.
175 This allows us to build gcc automatically even for targets that
176 need to add or modify the reserved keyword lists. */
177 #include "c-gperf.h"
179 /* Return something to represent absolute declarators containing a *.
180 TARGET is the absolute declarator that the * contains.
181 TYPE_QUALS is a list of modifiers such as const or volatile
182 to apply to the pointer type, represented as identifiers.
184 We return an INDIRECT_REF whose "contents" are TARGET
185 and whose type is the modifier list. */
187 tree
188 make_pointer_declarator (type_quals, target)
189 tree type_quals, target;
191 return build1 (INDIRECT_REF, type_quals, target);
194 void
195 forget_protocol_qualifiers ()
197 int i, n = sizeof wordlist / sizeof (struct resword);
199 for (i = 0; i < n; i++)
200 if ((int) wordlist[i].rid >= (int) RID_IN
201 && (int) wordlist[i].rid <= (int) RID_ONEWAY)
202 wordlist[i].name = "";
205 void
206 remember_protocol_qualifiers ()
208 int i, n = sizeof wordlist / sizeof (struct resword);
210 for (i = 0; i < n; i++)
211 if (wordlist[i].rid == RID_IN)
212 wordlist[i].name = "in";
213 else if (wordlist[i].rid == RID_OUT)
214 wordlist[i].name = "out";
215 else if (wordlist[i].rid == RID_INOUT)
216 wordlist[i].name = "inout";
217 else if (wordlist[i].rid == RID_BYCOPY)
218 wordlist[i].name = "bycopy";
219 else if (wordlist[i].rid == RID_BYREF)
220 wordlist[i].name = "byref";
221 else if (wordlist[i].rid == RID_ONEWAY)
222 wordlist[i].name = "oneway";
225 char *
226 init_parse (filename)
227 char *filename;
229 #if !USE_CPPLIB
230 /* Open input file. */
231 if (filename == 0 || !strcmp (filename, "-"))
233 finput = stdin;
234 filename = "stdin";
236 else
237 finput = fopen (filename, "r");
238 if (finput == 0)
239 pfatal_with_name (filename);
241 #ifdef IO_BUFFER_SIZE
242 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE), _IOFBF, IO_BUFFER_SIZE);
243 #endif
244 #else /* !USE_CPPLIB */
245 parse_in.show_column = 1;
246 if (! cpp_start_read (&parse_in, filename))
247 abort ();
249 if (filename == 0 || !strcmp (filename, "-"))
250 filename = "stdin";
252 /* cpp_start_read always puts at least one line directive into the
253 token buffer. We must arrange to read it out here. */
254 yy_cur = parse_in.token_buffer;
255 yy_lim = CPP_PWRITTEN (&parse_in);
256 cpp_token = CPP_DIRECTIVE;
257 #endif
259 init_lex ();
260 init_pragma ();
262 return filename;
265 void
266 finish_parse ()
268 #if USE_CPPLIB
269 cpp_finish (&parse_in);
270 errorcount += parse_in.errors;
271 #else
272 fclose (finput);
273 #endif
276 void
277 init_lex ()
279 /* Make identifier nodes long enough for the language-specific slots. */
280 set_identifier_size (sizeof (struct lang_identifier));
282 /* Start it at 0, because check_newline is called at the very beginning
283 and will increment it to 1. */
284 lineno = 0;
286 #ifdef MULTIBYTE_CHARS
287 /* Change to the native locale for multibyte conversions. */
288 setlocale (LC_CTYPE, "");
289 GET_ENVIRONMENT (literal_codeset, "LANG");
290 #endif
292 maxtoken = 40;
293 token_buffer = (char *) xmalloc (maxtoken + 2);
295 ridpointers[(int) RID_INT] = get_identifier ("int");
296 ridpointers[(int) RID_CHAR] = get_identifier ("char");
297 ridpointers[(int) RID_VOID] = get_identifier ("void");
298 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
299 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
300 ridpointers[(int) RID_SHORT] = get_identifier ("short");
301 ridpointers[(int) RID_LONG] = get_identifier ("long");
302 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
303 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
304 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
305 ridpointers[(int) RID_CONST] = get_identifier ("const");
306 ridpointers[(int) RID_RESTRICT] = get_identifier ("restrict");
307 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
308 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
309 ridpointers[(int) RID_STATIC] = get_identifier ("static");
310 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
311 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
312 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
313 ridpointers[(int) RID_ITERATOR] = get_identifier ("iterator");
314 ridpointers[(int) RID_COMPLEX] = get_identifier ("complex");
315 ridpointers[(int) RID_ID] = get_identifier ("id");
316 ridpointers[(int) RID_IN] = get_identifier ("in");
317 ridpointers[(int) RID_OUT] = get_identifier ("out");
318 ridpointers[(int) RID_INOUT] = get_identifier ("inout");
319 ridpointers[(int) RID_BYCOPY] = get_identifier ("bycopy");
320 ridpointers[(int) RID_BYREF] = get_identifier ("byref");
321 ridpointers[(int) RID_ONEWAY] = get_identifier ("oneway");
322 forget_protocol_qualifiers();
324 /* Some options inhibit certain reserved words.
325 Clear those words out of the hash table so they won't be recognized. */
326 #define UNSET_RESERVED_WORD(STRING) \
327 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
328 if (s) s->name = ""; } while (0)
330 if (! doing_objc_thang)
331 UNSET_RESERVED_WORD ("id");
333 if (flag_traditional)
335 UNSET_RESERVED_WORD ("const");
336 UNSET_RESERVED_WORD ("restrict");
337 UNSET_RESERVED_WORD ("volatile");
338 UNSET_RESERVED_WORD ("typeof");
339 UNSET_RESERVED_WORD ("signed");
340 UNSET_RESERVED_WORD ("inline");
341 UNSET_RESERVED_WORD ("iterator");
342 UNSET_RESERVED_WORD ("complex");
344 else if (!flag_isoc99)
345 UNSET_RESERVED_WORD ("restrict");
347 if (flag_no_asm)
349 UNSET_RESERVED_WORD ("asm");
350 UNSET_RESERVED_WORD ("typeof");
351 UNSET_RESERVED_WORD ("inline");
352 UNSET_RESERVED_WORD ("iterator");
353 UNSET_RESERVED_WORD ("complex");
357 void
358 reinit_parse_for_function ()
362 /* Function used when yydebug is set, to print a token in more detail. */
364 void
365 yyprint (file, yychar, yylval)
366 FILE *file;
367 int yychar;
368 YYSTYPE yylval;
370 tree t;
371 switch (yychar)
373 case IDENTIFIER:
374 case TYPENAME:
375 case OBJECTNAME:
376 t = yylval.ttype;
377 if (IDENTIFIER_POINTER (t))
378 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
379 break;
381 case CONSTANT:
382 t = yylval.ttype;
383 if (TREE_CODE (t) == INTEGER_CST)
384 fprintf (file,
385 #if HOST_BITS_PER_WIDE_INT == 64
386 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
387 " 0x%x%016x",
388 #else
389 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
390 " 0x%lx%016lx",
391 #else
392 " 0x%llx%016llx",
393 #endif
394 #endif
395 #else
396 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
397 " 0x%lx%08lx",
398 #else
399 " 0x%x%08x",
400 #endif
401 #endif
402 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
403 break;
407 /* Iff C is a carriage return, warn about it - if appropriate -
408 and return nonzero. */
410 static int
411 whitespace_cr (c)
412 int c;
414 static int newline_warning = 0;
416 if (c == '\r')
418 /* ANSI C says the effects of a carriage return in a source file
419 are undefined. */
420 if (pedantic && !newline_warning)
422 warning ("carriage return in source file");
423 warning ("(we only warn about the first carriage return)");
424 newline_warning = 1;
426 return 1;
428 return 0;
431 /* If C is not whitespace, return C.
432 Otherwise skip whitespace and return first nonwhite char read. */
434 static int
435 skip_white_space (c)
436 register int c;
438 for (;;)
440 switch (c)
442 /* We don't recognize comments here, because
443 cpp output can include / and * consecutively as operators.
444 Also, there's no need, since cpp removes all comments. */
446 case '\n':
447 if (linemode)
449 UNGETC (c);
450 return EOF;
452 c = check_newline ();
453 break;
455 case ' ':
456 case '\t':
457 case '\f':
458 case '\v':
459 case '\b':
460 #if USE_CPPLIB
461 /* While processing a # directive we don't get CPP_HSPACE
462 tokens, so we also need to handle whitespace the normal way. */
463 if (cpp_token == CPP_HSPACE)
464 c = yy_get_token ();
465 else
466 #endif
467 c = GETC();
468 break;
470 case '\r':
471 whitespace_cr (c);
472 c = GETC();
473 break;
475 case '\\':
476 c = GETC();
477 if (c == '\n')
478 lineno++;
479 else
480 error ("stray '\\' in program");
481 c = GETC();
482 break;
484 default:
485 return (c);
490 /* Skips all of the white space at the current location in the input file. */
492 void
493 position_after_white_space ()
495 register int c;
497 c = GETC();
499 UNGETC (skip_white_space (c));
502 /* Make the token buffer longer, preserving the data in it.
503 P should point to just beyond the last valid character in the old buffer.
504 The value we return is a pointer to the new buffer
505 at a place corresponding to P. */
507 static void
508 extend_token_buffer_to (size)
509 int size;
512 maxtoken = maxtoken * 2 + 10;
513 while (maxtoken < size);
514 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
517 static char *
518 extend_token_buffer (p)
519 const char *p;
521 int offset = p - token_buffer;
522 extend_token_buffer_to (offset);
523 return token_buffer + offset;
526 #if defined HANDLE_PRAGMA
527 /* Local versions of these macros, that can be passed as function pointers. */
528 static int
529 pragma_getc ()
531 return GETC ();
534 static void
535 pragma_ungetc (arg)
536 int arg;
538 UNGETC (arg);
540 #endif
542 static int
543 read_line_number (num)
544 int *num;
546 register int token = yylex ();
548 if (token == CONSTANT
549 && TREE_CODE (yylval.ttype) == INTEGER_CST)
551 *num = TREE_INT_CST_LOW (yylval.ttype);
552 return 1;
554 else
556 if (token != END_OF_LINE)
557 error ("invalid #-line");
558 return 0;
562 /* At the beginning of a line, increment the line number
563 and process any #-directive on this line.
564 If the line is a #-directive, read the entire line and return a newline.
565 Otherwise, return the line's first non-whitespace character.
567 Note that in the case of USE_CPPLIB, we get the whole line as one
568 CPP_DIRECTIVE token. */
571 check_newline ()
573 register int c;
574 register int token;
575 int saw_line;
576 enum { act_none, act_push, act_pop } action;
577 int old_lineno, action_number, l;
579 restart:
580 /* Read first nonwhite char on the line. */
582 #ifdef USE_CPPLIB
583 c = GETC ();
584 /* In some cases where we're leaving an include file, we can get multiple
585 CPP_HSPACE tokens in a row, so we need to loop. */
586 while (cpp_token == CPP_HSPACE)
587 c = yy_get_token ();
588 #else
590 c = GETC ();
591 while (c == ' ' || c == '\t');
592 #endif
594 lineno++;
596 if (c != '#')
598 /* Sequences of multiple newlines are very common; optimize them. */
599 if (c == '\n')
600 goto restart;
602 /* If not #, return it so caller will use it. */
603 return c;
606 /* Don't read beyond this line. */
607 saw_line = 0;
608 linemode = 1;
610 #if USE_CPPLIB
611 if (cpp_token == CPP_VSPACE)
613 /* Format is "<space> <line number> <filename> <newline>".
614 Only the line number is interesting, and even that
615 we can get more efficiently than scanning the line. */
616 yy_cur = yy_lim - 1;
617 lineno = parse_in.lineno - 1;
618 goto skipline;
620 #endif
622 token = yylex ();
624 if (token == IDENTIFIER)
626 /* If a letter follows, then if the word here is `line', skip
627 it and ignore it; otherwise, ignore the line, with an error
628 if the word isn't `pragma'. */
630 const char *name = IDENTIFIER_POINTER (yylval.ttype);
632 if (!strcmp (name, "pragma"))
634 token = yylex ();
635 if (token != IDENTIFIER
636 || TREE_CODE (yylval.ttype) != IDENTIFIER_NODE)
637 goto skipline;
639 #ifdef HANDLE_PRAGMA
640 /* We invoke HANDLE_PRAGMA before HANDLE_GENERIC_PRAGMAS
641 (if both are defined), in order to give the back
642 end a chance to override the interpretation of
643 SYSV style pragmas. */
644 if (HANDLE_PRAGMA (pragma_getc, pragma_ungetc,
645 IDENTIFIER_POINTER (yylval.ttype)))
646 goto skipline;
647 #endif /* HANDLE_PRAGMA */
649 #ifdef HANDLE_GENERIC_PRAGMAS
650 if (handle_generic_pragma (token))
651 goto skipline;
652 #endif /* HANDLE_GENERIC_PRAGMAS */
654 /* Issue a warning message if we have been asked to do so.
655 Ignoring unknown pragmas in system header file unless
656 an explcit -Wunknown-pragmas has been given. */
657 if (warn_unknown_pragmas > 1
658 || (warn_unknown_pragmas && ! in_system_header))
659 warning ("ignoring pragma: %s", token_buffer);
661 goto skipline;
663 else if (!strcmp (name, "define"))
665 debug_define (lineno, GET_DIRECTIVE_LINE ());
666 goto skipline;
668 else if (!strcmp (name, "undef"))
670 debug_undef (lineno, GET_DIRECTIVE_LINE ());
671 goto skipline;
673 else if (!strcmp (name, "line"))
675 saw_line = 1;
676 token = yylex ();
677 goto linenum;
679 else if (!strcmp (name, "ident"))
681 /* #ident. The pedantic warning is now in cccp.c. */
683 /* Here we have just seen `#ident '.
684 A string constant should follow. */
686 token = yylex ();
687 if (token == END_OF_LINE)
688 goto skipline;
689 if (token != STRING
690 || TREE_CODE (yylval.ttype) != STRING_CST)
692 error ("invalid #ident");
693 goto skipline;
696 if (! flag_no_ident)
698 #ifdef ASM_OUTPUT_IDENT
699 ASM_OUTPUT_IDENT (asm_out_file,
700 TREE_STRING_POINTER (yylval.ttype));
701 #endif
704 /* Skip the rest of this line. */
705 goto skipline;
708 error ("undefined or invalid # directive `%s'", name);
709 goto skipline;
712 /* If the # is the only nonwhite char on the line,
713 just ignore it. Check the new newline. */
714 if (token == END_OF_LINE)
715 goto skipline;
717 linenum:
718 /* Here we have either `#line' or `# <nonletter>'.
719 In either case, it should be a line number; a digit should follow. */
721 if (token != CONSTANT
722 || TREE_CODE (yylval.ttype) != INTEGER_CST)
724 error ("invalid #-line");
725 goto skipline;
728 /* subtract one, because it is the following line that
729 gets the specified number */
731 l = TREE_INT_CST_LOW (yylval.ttype) - 1;
733 /* More follows: it must be a string constant (filename).
734 It would be neat to use cpplib to quickly process the string, but
735 (1) we don't have a handy tokenization of the string, and
736 (2) I don't know how well that would work in the presense
737 of filenames that contain wide characters. */
739 if (saw_line)
741 /* Don't treat \ as special if we are processing #line 1 "...".
742 If you want it to be treated specially, use # 1 "...". */
743 ignore_escape_flag = 1;
746 /* Read the string constant. */
747 token = yylex ();
749 ignore_escape_flag = 0;
751 if (token == END_OF_LINE)
753 /* No more: store the line number and check following line. */
754 lineno = l;
755 goto skipline;
758 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
760 error ("invalid #line");
761 goto skipline;
764 input_filename = TREE_STRING_POINTER (yylval.ttype);
766 if (main_input_filename == 0)
767 main_input_filename = input_filename;
769 old_lineno = lineno;
770 action = act_none;
771 action_number = 0;
772 lineno = l;
774 /* Each change of file name
775 reinitializes whether we are now in a system header. */
776 in_system_header = 0;
778 if (!read_line_number (&action_number))
780 /* Update the name in the top element of input_file_stack. */
781 if (input_file_stack)
782 input_file_stack->name = input_filename;
785 /* `1' after file name means entering new file.
786 `2' after file name means just left a file. */
788 if (action_number == 1)
790 action = act_push;
791 read_line_number (&action_number);
793 else if (action_number == 2)
795 action = act_pop;
796 read_line_number (&action_number);
798 if (action_number == 3)
800 /* `3' after file name means this is a system header file. */
801 in_system_header = 1;
802 read_line_number (&action_number);
805 /* Do the actions implied by the preceding numbers. */
807 if (action == act_push)
809 /* Pushing to a new file. */
810 struct file_stack *p
811 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
812 input_file_stack->line = old_lineno;
813 p->next = input_file_stack;
814 p->name = input_filename;
815 p->indent_level = indent_level;
816 input_file_stack = p;
817 input_file_stack_tick++;
818 debug_start_source_file (input_filename);
820 else if (action == act_pop)
822 /* Popping out of a file. */
823 if (input_file_stack->next)
825 struct file_stack *p = input_file_stack;
826 if (indent_level != p->indent_level)
828 warning_with_file_and_line
829 (p->name, old_lineno,
830 "This file contains more `%c's than `%c's.",
831 indent_level > p->indent_level ? '{' : '}',
832 indent_level > p->indent_level ? '}' : '{');
834 input_file_stack = p->next;
835 free (p);
836 input_file_stack_tick++;
837 debug_end_source_file (input_file_stack->line);
839 else
840 error ("#-lines for entering and leaving files don't match");
843 /* Now that we've pushed or popped the input stack,
844 update the name in the top element. */
845 if (input_file_stack)
846 input_file_stack->name = input_filename;
848 /* skip the rest of this line. */
849 skipline:
850 linemode = 0;
851 end_of_file = 0;
854 c = GETC();
855 while (c != '\n' && c != EOF);
856 return c;
859 #ifdef HANDLE_GENERIC_PRAGMAS
861 /* Handle a #pragma directive.
862 TOKEN is the token we read after `#pragma'. Processes the entire input
863 line and return non-zero iff the pragma has been successfully parsed. */
865 /* This function has to be in this file, in order to get at
866 the token types. */
868 static int
869 handle_generic_pragma (token)
870 register int token;
872 for (;;)
874 switch (token)
876 case IDENTIFIER:
877 case TYPENAME:
878 case STRING:
879 case CONSTANT:
880 handle_pragma_token (token_buffer, yylval.ttype);
881 break;
883 case END_OF_LINE:
884 return handle_pragma_token (NULL_PTR, NULL_TREE);
886 default:
887 handle_pragma_token (token_buffer, NULL);
890 token = yylex ();
894 #endif /* HANDLE_GENERIC_PRAGMAS */
896 #define ENDFILE -1 /* token that represents end-of-file */
898 /* Read an escape sequence, returning its equivalent as a character,
899 or store 1 in *ignore_ptr if it is backslash-newline. */
901 static int
902 readescape (ignore_ptr)
903 int *ignore_ptr;
905 register int c = GETC();
906 register int code;
907 register unsigned count;
908 unsigned firstdig = 0;
909 int nonnull;
911 switch (c)
913 case 'x':
914 if (warn_traditional)
915 warning ("the meaning of `\\x' varies with -traditional");
917 if (flag_traditional)
918 return c;
920 code = 0;
921 count = 0;
922 nonnull = 0;
923 while (1)
925 c = GETC();
926 if (! ISXDIGIT (c))
928 UNGETC (c);
929 break;
931 code *= 16;
932 if (c >= 'a' && c <= 'f')
933 code += c - 'a' + 10;
934 if (c >= 'A' && c <= 'F')
935 code += c - 'A' + 10;
936 if (c >= '0' && c <= '9')
937 code += c - '0';
938 if (code != 0 || count != 0)
940 if (count == 0)
941 firstdig = code;
942 count++;
944 nonnull = 1;
946 if (! nonnull)
948 warning ("\\x used with no following hex digits");
949 return 'x';
951 else if (count == 0)
952 /* Digits are all 0's. Ok. */
954 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
955 || (count > 1
956 && (((unsigned)1
957 << (TYPE_PRECISION (integer_type_node)
958 - (count - 1) * 4))
959 <= firstdig)))
960 pedwarn ("hex escape out of range");
961 return code;
963 case '0': case '1': case '2': case '3': case '4':
964 case '5': case '6': case '7':
965 code = 0;
966 count = 0;
967 while ((c <= '7') && (c >= '0') && (count++ < 3))
969 code = (code * 8) + (c - '0');
970 c = GETC();
972 UNGETC (c);
973 return code;
975 case '\\': case '\'': case '"':
976 return c;
978 case '\n':
979 lineno++;
980 *ignore_ptr = 1;
981 return 0;
983 case 'n':
984 return TARGET_NEWLINE;
986 case 't':
987 return TARGET_TAB;
989 case 'r':
990 return TARGET_CR;
992 case 'f':
993 return TARGET_FF;
995 case 'b':
996 return TARGET_BS;
998 case 'a':
999 if (warn_traditional)
1000 warning ("the meaning of `\\a' varies with -traditional");
1002 if (flag_traditional)
1003 return c;
1004 return TARGET_BELL;
1006 case 'v':
1007 #if 0 /* Vertical tab is present in common usage compilers. */
1008 if (flag_traditional)
1009 return c;
1010 #endif
1011 return TARGET_VT;
1013 case 'e':
1014 case 'E':
1015 if (pedantic)
1016 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
1017 return TARGET_ESC;
1019 case '?':
1020 return c;
1022 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
1023 case '(':
1024 case '{':
1025 case '[':
1026 /* `\%' is used to prevent SCCS from getting confused. */
1027 case '%':
1028 if (pedantic)
1029 pedwarn ("unknown escape sequence `\\%c'", c);
1030 return c;
1032 if (ISGRAPH (c))
1033 pedwarn ("unknown escape sequence `\\%c'", c);
1034 else
1035 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
1036 return c;
1039 void
1040 yyerror (msgid)
1041 const char *msgid;
1043 const char *string = _(msgid);
1045 /* We can't print string and character constants well
1046 because the token_buffer contains the result of processing escapes. */
1047 if (end_of_file)
1048 error ("%s at end of input", string);
1049 else if (token_buffer[0] == 0)
1050 error ("%s at null character", string);
1051 else if (token_buffer[0] == '"')
1052 error ("%s before string constant", string);
1053 else if (token_buffer[0] == '\'')
1054 error ("%s before character constant", string);
1055 else if (!ISGRAPH(token_buffer[0]))
1056 error ("%s before character 0%o", string, (unsigned char) token_buffer[0]);
1057 else
1058 error ("%s before `%s'", string, token_buffer);
1061 #if 0
1063 struct try_type
1065 tree *node_var;
1066 char unsigned_flag;
1067 char long_flag;
1068 char long_long_flag;
1071 struct try_type type_sequence[] =
1073 { &integer_type_node, 0, 0, 0},
1074 { &unsigned_type_node, 1, 0, 0},
1075 { &long_integer_type_node, 0, 1, 0},
1076 { &long_unsigned_type_node, 1, 1, 0},
1077 { &long_long_integer_type_node, 0, 1, 1},
1078 { &long_long_unsigned_type_node, 1, 1, 1}
1080 #endif /* 0 */
1082 struct pf_args
1084 /* Input */
1085 int base;
1086 char * p;
1087 /* I/O */
1088 int c;
1089 /* Output */
1090 int imag;
1091 tree type;
1092 int conversion_errno;
1093 REAL_VALUE_TYPE value;
1096 static void
1097 parse_float (data)
1098 PTR data;
1100 struct pf_args * args = (struct pf_args *) data;
1101 int fflag = 0, lflag = 0;
1102 /* Copy token_buffer now, while it has just the number
1103 and not the suffixes; once we add `f' or `i',
1104 REAL_VALUE_ATOF may not work any more. */
1105 char *copy = (char *) alloca (args->p - token_buffer + 1);
1106 bcopy (token_buffer, copy, args->p - token_buffer + 1);
1107 args->imag = 0;
1108 args->conversion_errno = 0;
1109 args->type = double_type_node;
1111 while (1)
1113 int lose = 0;
1115 /* Read the suffixes to choose a data type. */
1116 switch (args->c)
1118 case 'f': case 'F':
1119 if (fflag)
1120 error ("more than one `f' in numeric constant");
1121 fflag = 1;
1122 break;
1124 case 'l': case 'L':
1125 if (lflag)
1126 error ("more than one `l' in numeric constant");
1127 lflag = 1;
1128 break;
1130 case 'i': case 'I':
1131 if (args->imag)
1132 error ("more than one `i' or `j' in numeric constant");
1133 else if (pedantic)
1134 pedwarn ("ANSI C forbids imaginary numeric constants");
1135 args->imag = 1;
1136 break;
1138 default:
1139 lose = 1;
1142 if (lose)
1143 break;
1145 if (args->p >= token_buffer + maxtoken - 3)
1146 args->p = extend_token_buffer (args->p);
1147 *(args->p++) = args->c;
1148 *(args->p) = 0;
1149 args->c = GETC();
1152 /* The second argument, machine_mode, of REAL_VALUE_ATOF
1153 tells the desired precision of the binary result
1154 of decimal-to-binary conversion. */
1156 if (fflag)
1158 if (lflag)
1159 error ("both `f' and `l' in floating constant");
1161 args->type = float_type_node;
1162 errno = 0;
1163 if (args->base == 16)
1164 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1165 else
1166 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1167 args->conversion_errno = errno;
1168 /* A diagnostic is required here by some ANSI C testsuites.
1169 This is not pedwarn, because some people don't want
1170 an error for this. */
1171 if (REAL_VALUE_ISINF (args->value) && pedantic)
1172 warning ("floating point number exceeds range of `float'");
1174 else if (lflag)
1176 args->type = long_double_type_node;
1177 errno = 0;
1178 if (args->base == 16)
1179 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1180 else
1181 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1182 args->conversion_errno = errno;
1183 if (REAL_VALUE_ISINF (args->value) && pedantic)
1184 warning ("floating point number exceeds range of `long double'");
1186 else
1188 errno = 0;
1189 if (args->base == 16)
1190 args->value = REAL_VALUE_HTOF (copy, TYPE_MODE (args->type));
1191 else
1192 args->value = REAL_VALUE_ATOF (copy, TYPE_MODE (args->type));
1193 args->conversion_errno = errno;
1194 if (REAL_VALUE_ISINF (args->value) && pedantic)
1195 warning ("floating point number exceeds range of `double'");
1199 /* Get the next character, staying within the current token if possible.
1200 If we're lexing a token, we don't want to look beyond the end of the
1201 token cpplib has prepared for us; otherwise, we end up reading in the
1202 next token, which screws up feed_input. So just return a null
1203 character. */
1205 static inline int token_getch PARAMS ((void));
1207 static inline int
1208 token_getch ()
1210 #if USE_CPPLIB
1211 if (yy_cur == yy_lim)
1212 return '\0';
1213 #endif
1214 return GETC ();
1217 static inline void token_put_back PARAMS ((int));
1219 static inline void
1220 token_put_back (ch)
1221 int ch;
1223 #if USE_CPPLIB
1224 if (ch == '\0')
1225 return;
1226 #endif
1227 UNGETC (ch);
1230 /* Read a single token from the input stream, and assign it lexical
1231 semantics. */
1234 yylex ()
1236 register int c;
1237 register char *p;
1238 register int value;
1239 int wide_flag = 0;
1240 int objc_flag = 0;
1242 c = GETC();
1244 /* Effectively do c = skip_white_space (c)
1245 but do it faster in the usual cases. */
1246 while (1)
1247 switch (c)
1249 case ' ':
1250 case '\t':
1251 case '\f':
1252 case '\v':
1253 case '\b':
1254 #if USE_CPPLIB
1255 if (cpp_token == CPP_HSPACE)
1256 c = yy_get_token ();
1257 else
1258 #endif
1259 c = GETC();
1260 break;
1262 case '\r':
1263 /* Call skip_white_space so we can warn if appropriate. */
1265 case '\n':
1266 case '/':
1267 case '\\':
1268 c = skip_white_space (c);
1269 default:
1270 goto found_nonwhite;
1272 found_nonwhite:
1274 token_buffer[0] = c;
1275 token_buffer[1] = 0;
1277 /* yylloc.first_line = lineno; */
1279 switch (c)
1281 case EOF:
1282 end_of_file = 1;
1283 token_buffer[0] = 0;
1284 if (linemode)
1285 value = END_OF_LINE;
1286 else
1287 value = ENDFILE;
1288 break;
1290 case 'L':
1291 #if USE_CPPLIB
1292 if (cpp_token == CPP_NAME)
1293 goto letter;
1294 #endif
1295 /* Capital L may start a wide-string or wide-character constant. */
1297 register int c = token_getch();
1298 if (c == '\'')
1300 wide_flag = 1;
1301 goto char_constant;
1303 if (c == '"')
1305 wide_flag = 1;
1306 goto string_constant;
1308 token_put_back (c);
1310 goto letter;
1312 case '@':
1313 if (!doing_objc_thang)
1315 value = c;
1316 break;
1318 else
1320 /* '@' may start a constant string object. */
1321 register int c = token_getch ();
1322 if (c == '"')
1324 objc_flag = 1;
1325 goto string_constant;
1327 token_put_back (c);
1328 /* Fall through to treat '@' as the start of an identifier. */
1331 case 'A': case 'B': case 'C': case 'D': case 'E':
1332 case 'F': case 'G': case 'H': case 'I': case 'J':
1333 case 'K': case 'M': case 'N': case 'O':
1334 case 'P': case 'Q': case 'R': case 'S': case 'T':
1335 case 'U': case 'V': case 'W': case 'X': case 'Y':
1336 case 'Z':
1337 case 'a': case 'b': case 'c': case 'd': case 'e':
1338 case 'f': case 'g': case 'h': case 'i': case 'j':
1339 case 'k': case 'l': case 'm': case 'n': case 'o':
1340 case 'p': case 'q': case 'r': case 's': case 't':
1341 case 'u': case 'v': case 'w': case 'x': case 'y':
1342 case 'z':
1343 case '_':
1344 case '$':
1345 letter:
1346 #if USE_CPPLIB
1347 if (cpp_token == CPP_NAME)
1349 /* Note that one character has already been read from
1350 yy_cur into token_buffer. Also, cpplib complains about
1351 $ in identifiers, so we don't have to. */
1353 int len = yy_lim - yy_cur + 1;
1354 if (len >= maxtoken)
1355 extend_token_buffer_to (len + 1);
1356 memcpy (token_buffer + 1, yy_cur, len);
1357 p = token_buffer + len;
1358 yy_cur = yy_lim;
1360 else
1361 #endif
1363 p = token_buffer;
1364 while (ISALNUM (c) || c == '_' || c == '$' || c == '@')
1366 /* Make sure this char really belongs in an identifier. */
1367 if (c == '$')
1369 if (! dollars_in_ident)
1370 error ("`$' in identifier");
1371 else if (pedantic)
1372 pedwarn ("`$' in identifier");
1375 if (p >= token_buffer + maxtoken)
1376 p = extend_token_buffer (p);
1378 *p++ = c;
1379 c = token_getch();
1382 *p = 0;
1383 token_put_back (c);
1386 value = IDENTIFIER;
1387 yylval.itype = 0;
1389 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1392 register struct resword *ptr;
1394 if ((ptr = is_reserved_word (token_buffer, p - token_buffer)))
1396 if (ptr->rid)
1397 yylval.ttype = ridpointers[(int) ptr->rid];
1398 value = (int) ptr->token;
1400 /* Only return OBJECTNAME if it is a typedef. */
1401 if (doing_objc_thang && value == OBJECTNAME)
1403 lastiddecl = lookup_name(yylval.ttype);
1405 if (lastiddecl == NULL_TREE
1406 || TREE_CODE (lastiddecl) != TYPE_DECL)
1407 value = IDENTIFIER;
1410 /* Even if we decided to recognize asm, still perhaps warn. */
1411 if (pedantic
1412 && (value == ASM_KEYWORD || value == TYPEOF
1413 || ptr->rid == RID_INLINE)
1414 && token_buffer[0] != '_')
1415 pedwarn ("ANSI does not permit the keyword `%s'",
1416 token_buffer);
1420 /* If we did not find a keyword, look for an identifier
1421 (or a typename). */
1423 if (value == IDENTIFIER)
1425 if (token_buffer[0] == '@')
1426 error("invalid identifier `%s'", token_buffer);
1428 yylval.ttype = get_identifier (token_buffer);
1429 lastiddecl = lookup_name (yylval.ttype);
1431 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1432 value = TYPENAME;
1433 /* A user-invisible read-only initialized variable
1434 should be replaced by its value.
1435 We handle only strings since that's the only case used in C. */
1436 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1437 && DECL_IGNORED_P (lastiddecl)
1438 && TREE_READONLY (lastiddecl)
1439 && DECL_INITIAL (lastiddecl) != 0
1440 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1442 tree stringval = DECL_INITIAL (lastiddecl);
1444 /* Copy the string value so that we won't clobber anything
1445 if we put something in the TREE_CHAIN of this one. */
1446 yylval.ttype = build_string (TREE_STRING_LENGTH (stringval),
1447 TREE_STRING_POINTER (stringval));
1448 value = STRING;
1450 else if (doing_objc_thang)
1452 tree objc_interface_decl = is_class_name (yylval.ttype);
1454 if (objc_interface_decl)
1456 value = CLASSNAME;
1457 yylval.ttype = objc_interface_decl;
1462 break;
1464 case '.':
1465 #if USE_CPPLIB
1466 if (yy_cur < yy_lim)
1467 #endif
1469 /* It's hard to preserve tokenization on '.' because
1470 it could be a symbol by itself, or it could be the
1471 start of a floating point number and cpp won't tell us. */
1472 register int c1 = token_getch ();
1473 token_buffer[1] = c1;
1474 if (c1 == '.')
1476 c1 = token_getch ();
1477 if (c1 == '.')
1479 token_buffer[2] = c1;
1480 token_buffer[3] = 0;
1481 value = ELLIPSIS;
1482 goto done;
1484 error ("parse error at `..'");
1486 if (ISDIGIT (c1))
1488 token_put_back (c1);
1489 goto number;
1491 token_put_back (c1);
1493 value = '.';
1494 token_buffer[1] = 0;
1495 break;
1497 case '0': case '1':
1498 /* Optimize for most frequent case. */
1500 register int cond;
1502 #if USE_CPPLIB
1503 cond = (yy_cur == yy_lim);
1504 #else
1505 register int c1 = token_getch ();
1506 token_put_back (c1);
1507 cond = (! ISALNUM (c1) && c1 != '.');
1508 #endif
1509 if (cond)
1511 yylval.ttype = (c == '0') ? integer_zero_node : integer_one_node;
1512 value = CONSTANT;
1513 break;
1515 /*FALLTHRU*/
1517 case '2': case '3': case '4':
1518 case '5': case '6': case '7': case '8': case '9':
1519 number:
1521 int base = 10;
1522 int count = 0;
1523 int largest_digit = 0;
1524 int numdigits = 0;
1525 int overflow = 0;
1527 /* We actually store only HOST_BITS_PER_CHAR bits in each part.
1528 The code below which fills the parts array assumes that a host
1529 int is at least twice as wide as a host char, and that
1530 HOST_BITS_PER_WIDE_INT is an even multiple of HOST_BITS_PER_CHAR.
1531 Two HOST_WIDE_INTs is the largest int literal we can store.
1532 In order to detect overflow below, the number of parts (TOTAL_PARTS)
1533 must be exactly the number of parts needed to hold the bits
1534 of two HOST_WIDE_INTs. */
1535 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2)
1536 unsigned int parts[TOTAL_PARTS];
1538 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS, AFTER_EXPON}
1539 floatflag = NOT_FLOAT;
1541 for (count = 0; count < TOTAL_PARTS; count++)
1542 parts[count] = 0;
1544 p = token_buffer;
1545 *p++ = c;
1547 if (c == '0')
1549 *p++ = (c = token_getch());
1550 if ((c == 'x') || (c == 'X'))
1552 base = 16;
1553 *p++ = (c = token_getch());
1555 /* Leading 0 forces octal unless the 0 is the only digit. */
1556 else if (c >= '0' && c <= '9')
1558 base = 8;
1559 numdigits++;
1561 else
1562 numdigits++;
1565 /* Read all the digits-and-decimal-points. */
1567 while (c == '.'
1568 || (ISALNUM (c) && c != 'l' && c != 'L'
1569 && c != 'u' && c != 'U'
1570 && c != 'i' && c != 'I' && c != 'j' && c != 'J'
1571 && (floatflag == NOT_FLOAT
1572 || ((base != 16) && (c != 'f') && (c != 'F'))
1573 || base == 16)))
1575 if (c == '.')
1577 if (base == 16 && pedantic)
1578 pedwarn ("floating constant may not be in radix 16");
1579 if (floatflag == TOO_MANY_POINTS)
1580 /* We have already emitted an error. Don't need another. */
1582 else if (floatflag == AFTER_POINT || floatflag == AFTER_EXPON)
1584 error ("malformed floating constant");
1585 floatflag = TOO_MANY_POINTS;
1586 /* Avoid another error from atof by forcing all characters
1587 from here on to be ignored. */
1588 p[-1] = '\0';
1590 else
1591 floatflag = AFTER_POINT;
1593 if (base == 8)
1594 base = 10;
1595 *p++ = c = token_getch();
1596 /* Accept '.' as the start of a floating-point number
1597 only when it is followed by a digit. */
1598 if (p == token_buffer + 2 && !ISDIGIT (c))
1599 abort ();
1601 else
1603 /* It is not a decimal point.
1604 It should be a digit (perhaps a hex digit). */
1606 if (ISDIGIT (c))
1608 c = c - '0';
1610 else if (base <= 10)
1612 if (c == 'e' || c == 'E')
1614 base = 10;
1615 floatflag = AFTER_EXPON;
1616 break; /* start of exponent */
1618 error ("nondigits in number and not hexadecimal");
1619 c = 0;
1621 else if (base == 16 && (c == 'p' || c == 'P'))
1623 floatflag = AFTER_EXPON;
1624 break; /* start of exponent */
1626 else if (c >= 'a')
1628 c = c - 'a' + 10;
1630 else
1632 c = c - 'A' + 10;
1634 if (c >= largest_digit)
1635 largest_digit = c;
1636 numdigits++;
1638 for (count = 0; count < TOTAL_PARTS; count++)
1640 parts[count] *= base;
1641 if (count)
1643 parts[count]
1644 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1645 parts[count-1]
1646 &= (1 << HOST_BITS_PER_CHAR) - 1;
1648 else
1649 parts[0] += c;
1652 /* If the highest-order part overflows (gets larger than
1653 a host char will hold) then the whole number has
1654 overflowed. Record this and truncate the highest-order
1655 part. */
1656 if (parts[TOTAL_PARTS - 1] >> HOST_BITS_PER_CHAR)
1658 overflow = 1;
1659 parts[TOTAL_PARTS - 1] &= (1 << HOST_BITS_PER_CHAR) - 1;
1662 if (p >= token_buffer + maxtoken - 3)
1663 p = extend_token_buffer (p);
1664 *p++ = (c = token_getch());
1668 /* This can happen on input like `int i = 0x;' */
1669 if (numdigits == 0)
1670 error ("numeric constant with no digits");
1672 if (largest_digit >= base)
1673 error ("numeric constant contains digits beyond the radix");
1675 /* Remove terminating char from the token buffer and delimit the
1676 string. */
1677 *--p = 0;
1679 if (floatflag != NOT_FLOAT)
1681 tree type;
1682 int imag, conversion_errno;
1683 REAL_VALUE_TYPE value;
1684 struct pf_args args;
1686 /* Read explicit exponent if any, and put it in tokenbuf. */
1688 if ((base == 10 && ((c == 'e') || (c == 'E')))
1689 || (base == 16 && (c == 'p' || c == 'P')))
1691 if (p >= token_buffer + maxtoken - 3)
1692 p = extend_token_buffer (p);
1693 *p++ = c;
1694 c = token_getch();
1695 if ((c == '+') || (c == '-'))
1697 *p++ = c;
1698 c = token_getch();
1700 /* Exponent is decimal, even if string is a hex float. */
1701 if (! ISDIGIT (c))
1702 error ("floating constant exponent has no digits");
1703 while (ISDIGIT (c))
1705 if (p >= token_buffer + maxtoken - 3)
1706 p = extend_token_buffer (p);
1707 *p++ = c;
1708 c = token_getch ();
1711 if (base == 16 && floatflag != AFTER_EXPON)
1712 error ("hexadecimal floating constant has no exponent");
1714 *p = 0;
1716 /* Setup input for parse_float() */
1717 args.base = base;
1718 args.p = p;
1719 args.c = c;
1721 /* Convert string to a double, checking for overflow. */
1722 if (do_float_handler (parse_float, (PTR) &args))
1724 /* Receive output from parse_float() */
1725 value = args.value;
1727 else
1729 /* We got an exception from parse_float() */
1730 error ("floating constant out of range");
1731 value = dconst0;
1734 /* Receive output from parse_float() */
1735 c = args.c;
1736 imag = args.imag;
1737 type = args.type;
1738 conversion_errno = args.conversion_errno;
1740 #ifdef ERANGE
1741 /* ERANGE is also reported for underflow,
1742 so test the value to distinguish overflow from that. */
1743 if (conversion_errno == ERANGE && !flag_traditional && pedantic
1744 && (REAL_VALUES_LESS (dconst1, value)
1745 || REAL_VALUES_LESS (value, dconstm1)))
1746 warning ("floating point number exceeds range of `double'");
1747 #endif
1749 /* If the result is not a number, assume it must have been
1750 due to some error message above, so silently convert
1751 it to a zero. */
1752 if (REAL_VALUE_ISNAN (value))
1753 value = dconst0;
1755 /* Create a node with determined type and value. */
1756 if (imag)
1757 yylval.ttype = build_complex (NULL_TREE,
1758 convert (type, integer_zero_node),
1759 build_real (type, value));
1760 else
1761 yylval.ttype = build_real (type, value);
1763 else
1765 tree traditional_type, ansi_type, type;
1766 HOST_WIDE_INT high, low;
1767 int spec_unsigned = 0;
1768 int spec_long = 0;
1769 int spec_long_long = 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 spec_unsigned = 1;
1782 else if (c == 'l' || c == 'L')
1784 if (spec_long)
1786 if (spec_long_long)
1787 error ("three `l's in integer constant");
1788 else if (pedantic && ! in_system_header && warn_long_long)
1789 pedwarn ("ANSI C forbids long long integer constants");
1790 spec_long_long = 1;
1792 spec_long = 1;
1794 else if (c == 'i' || c == 'j' || c == 'I' || c == 'J')
1796 if (spec_imag)
1797 error ("more than one `i' or `j' in numeric constant");
1798 else if (pedantic)
1799 pedwarn ("ANSI C forbids imaginary numeric constants");
1800 spec_imag = 1;
1802 else
1803 break;
1804 if (p >= token_buffer + maxtoken - 3)
1805 p = extend_token_buffer (p);
1806 *p++ = c;
1807 c = token_getch();
1810 /* If the literal overflowed, pedwarn about it now. */
1811 if (overflow)
1813 warn = 1;
1814 pedwarn ("integer constant is too large for this configuration of the compiler - truncated to %d bits", HOST_BITS_PER_WIDE_INT * 2);
1817 /* This is simplified by the fact that our constant
1818 is always positive. */
1820 high = low = 0;
1822 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1824 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1825 / HOST_BITS_PER_CHAR)]
1826 << (i * HOST_BITS_PER_CHAR));
1827 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1830 yylval.ttype = build_int_2 (low, high);
1831 TREE_TYPE (yylval.ttype) = long_long_unsigned_type_node;
1833 /* If warn_traditional, calculate both the ANSI type and the
1834 traditional type, then see if they disagree.
1835 Otherwise, calculate only the type for the dialect in use. */
1836 if (warn_traditional || flag_traditional)
1838 /* Calculate the traditional type. */
1839 /* Traditionally, any constant is signed;
1840 but if unsigned is specified explicitly, obey that.
1841 Use the smallest size with the right number of bits,
1842 except for one special case with decimal constants. */
1843 if (! spec_long && base != 10
1844 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1845 traditional_type = (spec_unsigned ? unsigned_type_node
1846 : integer_type_node);
1847 /* A decimal constant must be long
1848 if it does not fit in type int.
1849 I think this is independent of whether
1850 the constant is signed. */
1851 else if (! spec_long && base == 10
1852 && int_fits_type_p (yylval.ttype, integer_type_node))
1853 traditional_type = (spec_unsigned ? unsigned_type_node
1854 : integer_type_node);
1855 else if (! spec_long_long)
1856 traditional_type = (spec_unsigned ? long_unsigned_type_node
1857 : long_integer_type_node);
1858 else if (int_fits_type_p (yylval.ttype,
1859 spec_unsigned
1860 ? long_long_unsigned_type_node
1861 : long_long_integer_type_node))
1862 traditional_type = (spec_unsigned
1863 ? long_long_unsigned_type_node
1864 : long_long_integer_type_node);
1865 else
1866 traditional_type = (spec_unsigned
1867 ? widest_unsigned_literal_type_node
1868 : widest_integer_literal_type_node);
1870 if (warn_traditional || ! flag_traditional)
1872 /* Calculate the ANSI type. */
1873 if (! spec_long && ! spec_unsigned
1874 && int_fits_type_p (yylval.ttype, integer_type_node))
1875 ansi_type = integer_type_node;
1876 else if (! spec_long && (base != 10 || spec_unsigned)
1877 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1878 ansi_type = unsigned_type_node;
1879 else if (! spec_unsigned && !spec_long_long
1880 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1881 ansi_type = long_integer_type_node;
1882 else if (! spec_long_long
1883 && int_fits_type_p (yylval.ttype,
1884 long_unsigned_type_node))
1885 ansi_type = long_unsigned_type_node;
1886 else if (! spec_unsigned
1887 && int_fits_type_p (yylval.ttype,
1888 long_long_integer_type_node))
1889 ansi_type = long_long_integer_type_node;
1890 else if (int_fits_type_p (yylval.ttype,
1891 long_long_unsigned_type_node))
1892 ansi_type = long_long_unsigned_type_node;
1893 else if (! spec_unsigned
1894 && int_fits_type_p (yylval.ttype,
1895 widest_integer_literal_type_node))
1896 ansi_type = widest_integer_literal_type_node;
1897 else
1898 ansi_type = widest_unsigned_literal_type_node;
1901 type = flag_traditional ? traditional_type : ansi_type;
1903 /* We assume that constants specified in a non-decimal
1904 base are bit patterns, and that the programmer really
1905 meant what they wrote. */
1906 if (warn_traditional && base == 10
1907 && traditional_type != ansi_type)
1909 if (TYPE_PRECISION (traditional_type)
1910 != TYPE_PRECISION (ansi_type))
1911 warning ("width of integer constant changes with -traditional");
1912 else if (TREE_UNSIGNED (traditional_type)
1913 != TREE_UNSIGNED (ansi_type))
1914 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
1915 else
1916 warning ("width of integer constant may change on other systems with -traditional");
1919 if (pedantic && !flag_traditional && !spec_long_long && !warn
1920 && (TYPE_PRECISION (long_integer_type_node)
1921 < TYPE_PRECISION (type)))
1923 warn = 1;
1924 pedwarn ("integer constant larger than the maximum value of an unsigned long int");
1927 if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type))
1928 warning ("decimal constant is so large that it is unsigned");
1930 if (spec_imag)
1932 if (TYPE_PRECISION (type)
1933 <= TYPE_PRECISION (integer_type_node))
1934 yylval.ttype
1935 = build_complex (NULL_TREE, integer_zero_node,
1936 convert (integer_type_node,
1937 yylval.ttype));
1938 else
1939 error ("complex integer constant is too wide for `complex int'");
1941 else if (flag_traditional && !int_fits_type_p (yylval.ttype, type))
1942 /* The traditional constant 0x80000000 is signed
1943 but doesn't fit in the range of int.
1944 This will change it to -0x80000000, which does fit. */
1946 TREE_TYPE (yylval.ttype) = unsigned_type (type);
1947 yylval.ttype = convert (type, yylval.ttype);
1948 TREE_OVERFLOW (yylval.ttype)
1949 = TREE_CONSTANT_OVERFLOW (yylval.ttype) = 0;
1951 else
1952 TREE_TYPE (yylval.ttype) = type;
1955 /* If it's still an integer (not a complex), and it doesn't
1956 fit in the type we choose for it, then pedwarn. */
1958 if (! warn
1959 && TREE_CODE (TREE_TYPE (yylval.ttype)) == INTEGER_TYPE
1960 && ! int_fits_type_p (yylval.ttype, TREE_TYPE (yylval.ttype)))
1961 pedwarn ("integer constant is larger than the maximum value for its type");
1964 token_put_back (c);
1965 *p = 0;
1967 if (ISALNUM (c) || c == '.' || c == '_' || c == '$'
1968 || (!flag_traditional && (c == '-' || c == '+')
1969 && (p[-1] == 'e' || p[-1] == 'E')))
1970 error ("missing white space after number `%s'", token_buffer);
1972 value = CONSTANT; break;
1975 case '\'':
1976 char_constant:
1978 register int result = 0;
1979 register int num_chars = 0;
1980 int chars_seen = 0;
1981 unsigned width = TYPE_PRECISION (char_type_node);
1982 int max_chars;
1983 #ifdef MULTIBYTE_CHARS
1984 int longest_char = local_mb_cur_max ();
1985 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
1986 #endif
1988 max_chars = TYPE_PRECISION (integer_type_node) / width;
1989 if (wide_flag)
1990 width = WCHAR_TYPE_SIZE;
1992 while (1)
1994 tryagain:
1995 c = token_getch();
1997 if (c == '\'' || c == EOF)
1998 break;
2000 ++chars_seen;
2001 if (c == '\\')
2003 int ignore = 0;
2004 c = readescape (&ignore);
2005 if (ignore)
2006 goto tryagain;
2007 if (width < HOST_BITS_PER_INT
2008 && (unsigned) c >= ((unsigned)1 << width))
2009 pedwarn ("escape sequence out of range for character");
2010 #ifdef MAP_CHARACTER
2011 if (ISPRINT (c))
2012 c = MAP_CHARACTER (c);
2013 #endif
2015 else if (c == '\n')
2017 if (pedantic)
2018 pedwarn ("ANSI C forbids newline in character constant");
2019 lineno++;
2021 else
2023 #ifdef MULTIBYTE_CHARS
2024 wchar_t wc;
2025 int i;
2026 int char_len = -1;
2027 for (i = 1; i <= longest_char; ++i)
2029 if (i > maxtoken - 4)
2030 extend_token_buffer (token_buffer);
2032 token_buffer[i] = c;
2033 char_len = local_mbtowc (& wc,
2034 token_buffer + 1,
2036 if (char_len != -1)
2037 break;
2038 c = token_getch ();
2040 if (char_len > 1)
2042 /* mbtowc sometimes needs an extra char before accepting */
2043 if (char_len < i)
2044 token_put_back (c);
2045 if (! wide_flag)
2047 /* Merge character into result; ignore excess chars. */
2048 for (i = 1; i <= char_len; ++i)
2050 if (i > max_chars)
2051 break;
2052 if (width < HOST_BITS_PER_INT)
2053 result = (result << width)
2054 | (token_buffer[i]
2055 & ((1 << width) - 1));
2056 else
2057 result = token_buffer[i];
2059 num_chars += char_len;
2060 goto tryagain;
2062 c = wc;
2064 else
2066 if (char_len == -1)
2068 warning ("Ignoring invalid multibyte character");
2069 /* Replace all but the first byte. */
2070 for (--i; i > 1; --i)
2071 token_put_back (token_buffer[i]);
2072 wc = token_buffer[1];
2074 #ifdef MAP_CHARACTER
2075 c = MAP_CHARACTER (wc);
2076 #else
2077 c = wc;
2078 #endif
2080 #else /* ! MULTIBYTE_CHARS */
2081 #ifdef MAP_CHARACTER
2082 c = MAP_CHARACTER (c);
2083 #endif
2084 #endif /* ! MULTIBYTE_CHARS */
2087 if (wide_flag)
2089 if (chars_seen == 1) /* only keep the first one */
2090 result = c;
2091 goto tryagain;
2094 /* Merge character into result; ignore excess chars. */
2095 num_chars += (width / TYPE_PRECISION (char_type_node));
2096 if (num_chars < max_chars + 1)
2098 if (width < HOST_BITS_PER_INT)
2099 result = (result << width) | (c & ((1 << width) - 1));
2100 else
2101 result = c;
2105 if (c != '\'')
2106 error ("malformed character constant");
2107 else if (chars_seen == 0)
2108 error ("empty character constant");
2109 else if (num_chars > max_chars)
2111 num_chars = max_chars;
2112 error ("character constant too long");
2114 else if (chars_seen != 1 && ! flag_traditional && warn_multichar)
2115 warning ("multi-character character constant");
2117 /* If char type is signed, sign-extend the constant. */
2118 if (! wide_flag)
2120 int num_bits = num_chars * width;
2121 if (num_bits == 0)
2122 /* We already got an error; avoid invalid shift. */
2123 yylval.ttype = build_int_2 (0, 0);
2124 else if (TREE_UNSIGNED (char_type_node)
2125 || ((result >> (num_bits - 1)) & 1) == 0)
2126 yylval.ttype
2127 = build_int_2 (result & (~(unsigned HOST_WIDE_INT) 0
2128 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
2130 else
2131 yylval.ttype
2132 = build_int_2 (result | ~(~(unsigned HOST_WIDE_INT) 0
2133 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
2134 -1);
2135 TREE_TYPE (yylval.ttype) = integer_type_node;
2137 else
2139 yylval.ttype = build_int_2 (result, 0);
2140 TREE_TYPE (yylval.ttype) = wchar_type_node;
2143 value = CONSTANT;
2144 break;
2147 case '"':
2148 string_constant:
2150 unsigned width = wide_flag ? WCHAR_TYPE_SIZE
2151 : TYPE_PRECISION (char_type_node);
2152 #ifdef MULTIBYTE_CHARS
2153 int longest_char = local_mb_cur_max ();
2154 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
2155 #endif
2156 c = token_getch ();
2157 p = token_buffer + 1;
2159 while (c != '"' && c != EOF)
2161 /* ignore_escape_flag is set for reading the filename in #line. */
2162 if (!ignore_escape_flag && c == '\\')
2164 int ignore = 0;
2165 c = readescape (&ignore);
2166 if (ignore)
2167 goto skipnewline;
2168 if (width < HOST_BITS_PER_INT
2169 && (unsigned) c >= ((unsigned)1 << width))
2170 pedwarn ("escape sequence out of range for character");
2172 else if (c == '\n')
2174 if (pedantic)
2175 pedwarn ("ANSI C forbids newline in string constant");
2176 lineno++;
2178 else
2180 #ifdef MULTIBYTE_CHARS
2181 wchar_t wc;
2182 int i;
2183 int char_len = -1;
2184 for (i = 0; i < longest_char; ++i)
2186 if (p + i >= token_buffer + maxtoken)
2187 p = extend_token_buffer (p);
2188 p[i] = c;
2190 char_len = local_mbtowc (& wc, p, i + 1);
2191 if (char_len != -1)
2192 break;
2193 c = token_getch ();
2195 if (char_len == -1)
2197 warning ("Ignoring invalid multibyte character");
2198 /* Replace all except the first byte. */
2199 token_put_back (c);
2200 for (--i; i > 0; --i)
2201 token_put_back (p[i]);
2202 char_len = 1;
2204 /* mbtowc sometimes needs an extra char before accepting */
2205 if (char_len <= i)
2206 token_put_back (c);
2207 if (! wide_flag)
2209 p += (i + 1);
2210 c = token_getch ();
2211 continue;
2213 c = wc;
2214 #endif /* MULTIBYTE_CHARS */
2217 /* Add this single character into the buffer either as a wchar_t
2218 or as a single byte. */
2219 if (wide_flag)
2221 unsigned width = TYPE_PRECISION (char_type_node);
2222 unsigned bytemask = (1 << width) - 1;
2223 int byte;
2225 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2226 p = extend_token_buffer (p);
2228 for (byte = 0; byte < WCHAR_BYTES; ++byte)
2230 int value;
2231 if (byte >= (int) sizeof (c))
2232 value = 0;
2233 else
2234 value = (c >> (byte * width)) & bytemask;
2235 if (BYTES_BIG_ENDIAN)
2236 p[WCHAR_BYTES - byte - 1] = value;
2237 else
2238 p[byte] = value;
2240 p += WCHAR_BYTES;
2242 else
2244 if (p >= token_buffer + maxtoken)
2245 p = extend_token_buffer (p);
2246 *p++ = c;
2249 skipnewline:
2250 c = token_getch ();
2253 /* Terminate the string value, either with a single byte zero
2254 or with a wide zero. */
2255 if (wide_flag)
2257 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2258 p = extend_token_buffer (p);
2259 bzero (p, WCHAR_BYTES);
2260 p += WCHAR_BYTES;
2262 else
2264 if (p >= token_buffer + maxtoken)
2265 p = extend_token_buffer (p);
2266 *p++ = 0;
2269 if (c == EOF)
2270 error ("Unterminated string constant");
2272 /* We have read the entire constant.
2273 Construct a STRING_CST for the result. */
2275 if (wide_flag)
2277 yylval.ttype = build_string (p - (token_buffer + 1),
2278 token_buffer + 1);
2279 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
2280 value = STRING;
2282 else if (objc_flag)
2284 /* Return an Objective-C @"..." constant string object. */
2285 yylval.ttype = build_objc_string (p - (token_buffer + 1),
2286 token_buffer + 1);
2287 TREE_TYPE (yylval.ttype) = char_array_type_node;
2288 value = OBJC_STRING;
2290 else
2292 yylval.ttype = build_string (p - (token_buffer + 1),
2293 token_buffer + 1);
2294 TREE_TYPE (yylval.ttype) = char_array_type_node;
2295 value = STRING;
2298 break;
2301 case '+':
2302 case '-':
2303 case '&':
2304 case '|':
2305 case ':':
2306 case '<':
2307 case '>':
2308 case '*':
2309 case '/':
2310 case '%':
2311 case '^':
2312 case '!':
2313 case '=':
2315 register int c1;
2317 combine:
2319 switch (c)
2321 case '+':
2322 yylval.code = PLUS_EXPR; break;
2323 case '-':
2324 yylval.code = MINUS_EXPR; break;
2325 case '&':
2326 yylval.code = BIT_AND_EXPR; break;
2327 case '|':
2328 yylval.code = BIT_IOR_EXPR; break;
2329 case '*':
2330 yylval.code = MULT_EXPR; break;
2331 case '/':
2332 yylval.code = TRUNC_DIV_EXPR; break;
2333 case '%':
2334 yylval.code = TRUNC_MOD_EXPR; break;
2335 case '^':
2336 yylval.code = BIT_XOR_EXPR; break;
2337 case LSHIFT:
2338 yylval.code = LSHIFT_EXPR; break;
2339 case RSHIFT:
2340 yylval.code = RSHIFT_EXPR; break;
2341 case '<':
2342 yylval.code = LT_EXPR; break;
2343 case '>':
2344 yylval.code = GT_EXPR; break;
2347 token_buffer[1] = c1 = token_getch();
2348 token_buffer[2] = 0;
2350 if (c1 == '=')
2352 switch (c)
2354 case '<':
2355 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
2356 case '>':
2357 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
2358 case '!':
2359 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
2360 case '=':
2361 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
2363 value = ASSIGN; goto done;
2365 else if (c == c1)
2366 switch (c)
2368 case '+':
2369 value = PLUSPLUS; goto done;
2370 case '-':
2371 value = MINUSMINUS; goto done;
2372 case '&':
2373 value = ANDAND; goto done;
2374 case '|':
2375 value = OROR; goto done;
2376 case '<':
2377 c = LSHIFT;
2378 goto combine;
2379 case '>':
2380 c = RSHIFT;
2381 goto combine;
2383 else
2384 switch (c)
2386 case '-':
2387 if (c1 == '>')
2388 { value = POINTSAT; goto done; }
2389 break;
2391 /* digraphs */
2392 case ':':
2393 if (c1 == '>')
2394 { value = ']'; goto done; }
2395 break;
2396 case '<':
2397 if (c1 == '%')
2398 { value = '{'; indent_level++; goto done; }
2399 if (c1 == ':')
2400 { value = '['; goto done; }
2401 break;
2402 case '%':
2403 if (c1 == '>')
2404 { value = '}'; indent_level--; goto done; }
2405 break;
2408 token_put_back (c1);
2409 token_buffer[1] = 0;
2411 if ((c == '<') || (c == '>'))
2412 value = ARITHCOMPARE;
2413 else value = c;
2414 break;
2417 case 0:
2418 /* Don't make yyparse think this is eof. */
2419 value = 1;
2420 break;
2422 case '{':
2423 indent_level++;
2424 value = c;
2425 break;
2427 case '}':
2428 indent_level--;
2429 value = c;
2430 break;
2432 default:
2433 value = c;
2436 done:
2437 /* yylloc.last_line = lineno; */
2439 return value;
2442 /* Sets the value of the 'yydebug' variable to VALUE.
2443 This is a function so we don't have to have YYDEBUG defined
2444 in order to build the compiler. */
2446 void
2447 set_yydebug (value)
2448 int value;
2450 #if YYDEBUG != 0
2451 yydebug = value;
2452 #else
2453 warning ("YYDEBUG not defined.");
2454 #endif