H
[official-gcc.git] / gcc / c-lex.c
blobec1386ae59c30864e40aa646eff816d48460a3b4
1 /* Lexical analyzer for C and Objective C.
2 Copyright (C) 1987, 88, 89, 92, 94-97, 1998 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"
23 #include <setjmp.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"
36 #ifdef MULTIBYTE_CHARS
37 #include "mbchar.h"
38 #include <locale.h>
39 #endif /* MULTIBYTE_CHARS */
41 #if USE_CPPLIB
42 #include "cpplib.h"
43 extern cpp_reader parse_in;
44 extern cpp_options parse_options;
45 #else
46 /* Stream for reading from the input file. */
47 FILE *finput;
48 #endif
50 extern void yyprint PROTO((FILE *, int, YYSTYPE));
52 /* The elements of `ridpointers' are identifier nodes
53 for the reserved type names and storage classes.
54 It is indexed by a RID_... value. */
55 tree ridpointers[(int) RID_MAX];
57 /* Cause the `yydebug' variable to be defined. */
58 #define YYDEBUG 1
60 #if USE_CPPLIB
61 extern unsigned char *yy_cur, *yy_lim;
63 extern int yy_get_token ();
65 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
66 #define UNGETC(c) ((c), yy_cur--)
67 #else
68 #define GETC() getc (finput)
69 #define UNGETC(c) ungetc (c, finput)
70 #endif
72 /* the declaration found for the last IDENTIFIER token read in.
73 yylex must look this up to detect typedefs, which get token type TYPENAME,
74 so it is left around in case the identifier is not a typedef but is
75 used in a context which makes it a reference to a variable. */
76 tree lastiddecl;
78 /* Nonzero enables objc features. */
80 int doing_objc_thang;
82 extern int yydebug;
84 /* File used for outputting assembler code. */
85 extern FILE *asm_out_file;
87 #ifndef WCHAR_TYPE_SIZE
88 #ifdef INT_TYPE_SIZE
89 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
90 #else
91 #define WCHAR_TYPE_SIZE BITS_PER_WORD
92 #endif
93 #endif
95 /* Number of bytes in a wide character. */
96 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
98 static int maxtoken; /* Current nominal length of token buffer. */
99 char *token_buffer; /* Pointer to token buffer.
100 Actual allocated length is maxtoken + 2.
101 This is not static because objc-parse.y uses it. */
103 static int indent_level = 0; /* Number of { minus number of }. */
105 /* Nonzero if end-of-file has been seen on input. */
106 static int end_of_file;
108 #if !USE_CPPLIB
109 /* Buffered-back input character; faster than using ungetc. */
110 static int nextchar = -1;
111 #endif
113 #ifdef HANDLE_GENERIC_PRAGMAS
114 static int handle_generic_pragma PROTO((int));
115 #endif /* HANDLE_GENERIC_PRAGMAS */
116 static int whitespace_cr PROTO((int));
117 static int skip_white_space PROTO((int));
118 static int skip_white_space_on_line PROTO((void));
119 static char *extend_token_buffer PROTO((char *));
120 static int readescape PROTO((int *));
122 /* Do not insert generated code into the source, instead, include it.
123 This allows us to build gcc automatically even for targets that
124 need to add or modify the reserved keyword lists. */
125 #include "c-gperf.h"
127 /* Return something to represent absolute declarators containing a *.
128 TARGET is the absolute declarator that the * contains.
129 TYPE_QUALS is a list of modifiers such as const or volatile
130 to apply to the pointer type, represented as identifiers.
132 We return an INDIRECT_REF whose "contents" are TARGET
133 and whose type is the modifier list. */
135 tree
136 make_pointer_declarator (type_quals, target)
137 tree type_quals, target;
139 return build1 (INDIRECT_REF, type_quals, target);
142 void
143 forget_protocol_qualifiers ()
145 int i, n = sizeof wordlist / sizeof (struct resword);
147 for (i = 0; i < n; i++)
148 if ((int) wordlist[i].rid >= (int) RID_IN
149 && (int) wordlist[i].rid <= (int) RID_ONEWAY)
150 wordlist[i].name = "";
153 void
154 remember_protocol_qualifiers ()
156 int i, n = sizeof wordlist / sizeof (struct resword);
158 for (i = 0; i < n; i++)
159 if (wordlist[i].rid == RID_IN)
160 wordlist[i].name = "in";
161 else if (wordlist[i].rid == RID_OUT)
162 wordlist[i].name = "out";
163 else if (wordlist[i].rid == RID_INOUT)
164 wordlist[i].name = "inout";
165 else if (wordlist[i].rid == RID_BYCOPY)
166 wordlist[i].name = "bycopy";
167 else if (wordlist[i].rid == RID_BYREF)
168 wordlist[i].name = "byref";
169 else if (wordlist[i].rid == RID_ONEWAY)
170 wordlist[i].name = "oneway";
173 char *
174 init_parse (filename)
175 char *filename;
177 #if !USE_CPPLIB
178 /* Open input file. */
179 if (filename == 0 || !strcmp (filename, "-"))
181 finput = stdin;
182 filename = "stdin";
184 else
185 finput = fopen (filename, "r");
186 if (finput == 0)
187 pfatal_with_name (filename);
189 #ifdef IO_BUFFER_SIZE
190 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE), _IOFBF, IO_BUFFER_SIZE);
191 #endif
192 #endif /* !USE_CPPLIB */
194 init_lex ();
196 #if USE_CPPLIB
197 yy_cur = "\n";
198 yy_lim = yy_cur+1;
200 parse_in.show_column = 1;
201 if (! cpp_start_read (&parse_in, filename))
202 abort ();
203 #endif
205 return filename;
208 void
209 finish_parse ()
211 #if USE_CPPLIB
212 cpp_finish (&parse_in);
213 #else
214 fclose (finput);
215 #endif
218 void
219 init_lex ()
221 /* Make identifier nodes long enough for the language-specific slots. */
222 set_identifier_size (sizeof (struct lang_identifier));
224 /* Start it at 0, because check_newline is called at the very beginning
225 and will increment it to 1. */
226 lineno = 0;
228 #ifdef MULTIBYTE_CHARS
229 /* Change to the native locale for multibyte conversions. */
230 setlocale (LC_CTYPE, "");
231 literal_codeset = getenv ("LANG");
232 #endif
234 maxtoken = 40;
235 token_buffer = (char *) xmalloc (maxtoken + 2);
237 ridpointers[(int) RID_INT] = get_identifier ("int");
238 ridpointers[(int) RID_CHAR] = get_identifier ("char");
239 ridpointers[(int) RID_VOID] = get_identifier ("void");
240 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
241 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
242 ridpointers[(int) RID_SHORT] = get_identifier ("short");
243 ridpointers[(int) RID_LONG] = get_identifier ("long");
244 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
245 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
246 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
247 ridpointers[(int) RID_CONST] = get_identifier ("const");
248 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
249 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
250 ridpointers[(int) RID_STATIC] = get_identifier ("static");
251 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
252 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
253 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
254 ridpointers[(int) RID_ITERATOR] = get_identifier ("iterator");
255 ridpointers[(int) RID_COMPLEX] = get_identifier ("complex");
256 ridpointers[(int) RID_ID] = get_identifier ("id");
257 ridpointers[(int) RID_IN] = get_identifier ("in");
258 ridpointers[(int) RID_OUT] = get_identifier ("out");
259 ridpointers[(int) RID_INOUT] = get_identifier ("inout");
260 ridpointers[(int) RID_BYCOPY] = get_identifier ("bycopy");
261 ridpointers[(int) RID_BYREF] = get_identifier ("byref");
262 ridpointers[(int) RID_ONEWAY] = get_identifier ("oneway");
263 forget_protocol_qualifiers();
265 /* Some options inhibit certain reserved words.
266 Clear those words out of the hash table so they won't be recognized. */
267 #define UNSET_RESERVED_WORD(STRING) \
268 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
269 if (s) s->name = ""; } while (0)
271 if (! doing_objc_thang)
272 UNSET_RESERVED_WORD ("id");
274 if (flag_traditional)
276 UNSET_RESERVED_WORD ("const");
277 UNSET_RESERVED_WORD ("volatile");
278 UNSET_RESERVED_WORD ("typeof");
279 UNSET_RESERVED_WORD ("signed");
280 UNSET_RESERVED_WORD ("inline");
281 UNSET_RESERVED_WORD ("iterator");
282 UNSET_RESERVED_WORD ("complex");
284 if (flag_no_asm)
286 UNSET_RESERVED_WORD ("asm");
287 UNSET_RESERVED_WORD ("typeof");
288 UNSET_RESERVED_WORD ("inline");
289 UNSET_RESERVED_WORD ("iterator");
290 UNSET_RESERVED_WORD ("complex");
294 void
295 reinit_parse_for_function ()
299 /* Function used when yydebug is set, to print a token in more detail. */
301 void
302 yyprint (file, yychar, yylval)
303 FILE *file;
304 int yychar;
305 YYSTYPE yylval;
307 tree t;
308 switch (yychar)
310 case IDENTIFIER:
311 case TYPENAME:
312 case OBJECTNAME:
313 t = yylval.ttype;
314 if (IDENTIFIER_POINTER (t))
315 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
316 break;
318 case CONSTANT:
319 t = yylval.ttype;
320 if (TREE_CODE (t) == INTEGER_CST)
321 fprintf (file,
322 #if HOST_BITS_PER_WIDE_INT == 64
323 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
324 " 0x%x%016x",
325 #else
326 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
327 " 0x%lx%016lx",
328 #else
329 " 0x%llx%016llx",
330 #endif
331 #endif
332 #else
333 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
334 " 0x%lx%08lx",
335 #else
336 " 0x%x%08x",
337 #endif
338 #endif
339 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
340 break;
344 /* Iff C is a carriage return, warn about it - if appropriate -
345 and return nonzero. */
346 static int
347 whitespace_cr (c)
348 int c;
350 static int newline_warning = 0;
352 if (c == '\r')
354 /* ANSI C says the effects of a carriage return in a source file
355 are undefined. */
356 if (pedantic && !newline_warning)
358 warning ("carriage return in source file");
359 warning ("(we only warn about the first carriage return)");
360 newline_warning = 1;
362 return 1;
364 return 0;
367 /* If C is not whitespace, return C.
368 Otherwise skip whitespace and return first nonwhite char read. */
370 static int
371 skip_white_space (c)
372 register int c;
374 for (;;)
376 switch (c)
378 /* We don't recognize comments here, because
379 cpp output can include / and * consecutively as operators.
380 Also, there's no need, since cpp removes all comments. */
382 case '\n':
383 c = check_newline ();
384 break;
386 case ' ':
387 case '\t':
388 case '\f':
389 case '\v':
390 case '\b':
391 c = GETC();
392 break;
394 case '\r':
395 whitespace_cr (c);
396 c = GETC();
397 break;
399 case '\\':
400 c = GETC();
401 if (c == '\n')
402 lineno++;
403 else
404 error ("stray '\\' in program");
405 c = GETC();
406 break;
408 default:
409 return (c);
414 /* Skips all of the white space at the current location in the input file.
415 Must use and reset nextchar if it has the next character. */
417 void
418 position_after_white_space ()
420 register int c;
422 #if !USE_CPPLIB
423 if (nextchar != -1)
424 c = nextchar, nextchar = -1;
425 else
426 #endif
427 c = GETC();
429 UNGETC (skip_white_space (c));
432 /* Like skip_white_space, but don't advance beyond the end of line.
433 Moreover, we don't get passed a character to start with. */
434 static int
435 skip_white_space_on_line ()
437 register int c;
439 while (1)
441 c = GETC();
442 switch (c)
444 case '\n':
445 default:
446 break;
448 case ' ':
449 case '\t':
450 case '\f':
451 case '\v':
452 case '\b':
453 continue;
455 case '\r':
456 whitespace_cr (c);
457 continue;
459 break;
461 return c;
464 /* Make the token buffer longer, preserving the data in it.
465 P should point to just beyond the last valid character in the old buffer.
466 The value we return is a pointer to the new buffer
467 at a place corresponding to P. */
469 static char *
470 extend_token_buffer (p)
471 char *p;
473 int offset = p - token_buffer;
475 maxtoken = maxtoken * 2 + 10;
476 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
478 return token_buffer + offset;
481 #if defined HANDLE_PRAGMA
482 /* Local versions of these macros, that can be passed as function pointers. */
483 static int
484 pragma_getc ()
486 return GETC();
489 static void
490 pragma_ungetc (arg)
491 int arg;
493 UNGETC (arg);
495 #endif
497 /* At the beginning of a line, increment the line number
498 and process any #-directive on this line.
499 If the line is a #-directive, read the entire line and return a newline.
500 Otherwise, return the line's first non-whitespace character. */
503 check_newline ()
505 register int c;
506 register int token;
508 lineno++;
510 /* Read first nonwhite char on the line. */
512 c = GETC();
513 while (c == ' ' || c == '\t')
514 c = GETC();
516 if (c != '#')
518 /* If not #, return it so caller will use it. */
519 return c;
522 /* Read first nonwhite char after the `#'. */
524 c = GETC();
525 while (c == ' ' || c == '\t')
526 c = GETC();
528 /* If a letter follows, then if the word here is `line', skip
529 it and ignore it; otherwise, ignore the line, with an error
530 if the word isn't `pragma', `ident', `define', or `undef'. */
532 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
534 if (c == 'p')
536 if (GETC() == 'r'
537 && GETC() == 'a'
538 && GETC() == 'g'
539 && GETC() == 'm'
540 && GETC() == 'a'
541 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'
542 || whitespace_cr (c) ))
544 while (c == ' ' || c == '\t' || whitespace_cr (c))
545 c = GETC ();
546 if (c == '\n')
547 return c;
549 #if defined HANDLE_PRAGMA || defined HANDLE_GENERIC_PRAGMAS
550 UNGETC (c);
551 token = yylex ();
552 if (token != IDENTIFIER)
553 goto skipline;
554 #endif /* HANDLE_PRAGMA || HANDLE_GENERIC_PRAGMAS */
556 #ifdef HANDLE_PRAGMA
557 /* We invoke HANDLE_PRAGMA before HANDLE_GENERIC_PRAGMAS (if
558 both are defined), in order to give the back end a chance to
559 override the interpretation of generic style pragmas. */
560 #if !USE_CPPLIB
561 if (nextchar >= 0)
563 c = nextchar, nextchar = -1;
564 UNGETC (c);
566 #endif /* !USE_CPPLIB */
568 if (TREE_CODE (yylval.ttype) != IDENTIFIER_NODE)
569 goto skipline;
571 if (HANDLE_PRAGMA (pragma_getc, pragma_ungetc,
572 IDENTIFIER_POINTER (yylval.ttype)))
573 return GETC ();
574 #endif /* HANDLE_PRAGMA */
576 #ifdef HANDLE_GENERIC_PRAGMAS
577 if (handle_generic_pragma (token))
578 return GETC ();
579 #endif /* HANDLE_GENERIC_PRAGMAS */
581 /* Issue a warning message if we have been asked to do so.
582 Ignoring unknown pragmas in system header file unless
583 an explcit -Wunknown-pragmas has been given. */
584 if (warn_unknown_pragmas > 1
585 || (warn_unknown_pragmas && ! in_system_header))
586 warning ("ignoring pragma: %s", token_buffer);
588 goto skipline;
592 else if (c == 'd')
594 if (GETC() == 'e'
595 && GETC() == 'f'
596 && GETC() == 'i'
597 && GETC() == 'n'
598 && GETC() == 'e'
599 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'))
601 if (c != '\n')
602 debug_define (lineno, GET_DIRECTIVE_LINE ());
603 goto skipline;
606 else if (c == 'u')
608 if (GETC() == 'n'
609 && GETC() == 'd'
610 && GETC() == 'e'
611 && GETC() == 'f'
612 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'))
614 if (c != '\n')
615 debug_undef (lineno, GET_DIRECTIVE_LINE ());
616 goto skipline;
619 else if (c == 'l')
621 if (GETC() == 'i'
622 && GETC() == 'n'
623 && GETC() == 'e'
624 && ((c = GETC()) == ' ' || c == '\t'))
625 goto linenum;
627 else if (c == 'i')
629 if (GETC() == 'd'
630 && GETC() == 'e'
631 && GETC() == 'n'
632 && GETC() == 't'
633 && ((c = GETC()) == ' ' || c == '\t'))
635 /* #ident. The pedantic warning is now in cccp.c. */
637 /* Here we have just seen `#ident '.
638 A string constant should follow. */
640 c = skip_white_space_on_line ();
642 /* If no argument, ignore the line. */
643 if (c == '\n')
644 return c;
646 UNGETC (c);
647 token = yylex ();
648 if (token != STRING
649 || TREE_CODE (yylval.ttype) != STRING_CST)
651 error ("invalid #ident");
652 goto skipline;
655 if (!flag_no_ident)
657 #ifdef ASM_OUTPUT_IDENT
658 ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (yylval.ttype));
659 #endif
662 /* Skip the rest of this line. */
663 goto skipline;
667 error ("undefined or invalid # directive");
668 goto skipline;
671 linenum:
672 /* Here we have either `#line' or `# <nonletter>'.
673 In either case, it should be a line number; a digit should follow. */
675 /* Can't use skip_white_space here, but must handle all whitespace
676 that is not '\n', lest we get a recursion for '\r' '\n' when
677 calling yylex. */
678 UNGETC (c);
679 c = skip_white_space_on_line ();
681 /* If the # is the only nonwhite char on the line,
682 just ignore it. Check the new newline. */
683 if (c == '\n')
684 return c;
686 /* Something follows the #; read a token. */
688 UNGETC (c);
689 token = yylex ();
691 if (token == CONSTANT
692 && TREE_CODE (yylval.ttype) == INTEGER_CST)
694 int old_lineno = lineno;
695 int used_up = 0;
696 /* subtract one, because it is the following line that
697 gets the specified number */
699 int l = TREE_INT_CST_LOW (yylval.ttype) - 1;
701 /* Is this the last nonwhite stuff on the line? */
702 c = skip_white_space_on_line ();
703 if (c == '\n')
705 /* No more: store the line number and check following line. */
706 lineno = l;
707 return c;
709 UNGETC (c);
711 /* More follows: it must be a string constant (filename). */
713 /* Read the string constant. */
714 token = yylex ();
716 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
718 error ("invalid #line");
719 goto skipline;
722 input_filename
723 = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
724 strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
725 lineno = l;
727 /* Each change of file name
728 reinitializes whether we are now in a system header. */
729 in_system_header = 0;
731 if (main_input_filename == 0)
732 main_input_filename = input_filename;
734 /* Is this the last nonwhite stuff on the line? */
735 c = skip_white_space_on_line ();
736 if (c == '\n')
738 /* Update the name in the top element of input_file_stack. */
739 if (input_file_stack)
740 input_file_stack->name = input_filename;
742 return c;
744 UNGETC (c);
746 token = yylex ();
747 used_up = 0;
749 /* `1' after file name means entering new file.
750 `2' after file name means just left a file. */
752 if (token == CONSTANT
753 && TREE_CODE (yylval.ttype) == INTEGER_CST)
755 if (TREE_INT_CST_LOW (yylval.ttype) == 1)
757 /* Pushing to a new file. */
758 struct file_stack *p
759 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
760 input_file_stack->line = old_lineno;
761 p->next = input_file_stack;
762 p->name = input_filename;
763 p->indent_level = indent_level;
764 input_file_stack = p;
765 input_file_stack_tick++;
766 debug_start_source_file (input_filename);
767 used_up = 1;
769 else if (TREE_INT_CST_LOW (yylval.ttype) == 2)
771 /* Popping out of a file. */
772 if (input_file_stack->next)
774 struct file_stack *p = input_file_stack;
775 if (indent_level != p->indent_level)
777 warning_with_file_and_line
778 (p->name, old_lineno,
779 "This file contains more `%c's than `%c's.",
780 indent_level > p->indent_level ? '{' : '}',
781 indent_level > p->indent_level ? '}' : '{');
783 input_file_stack = p->next;
784 free (p);
785 input_file_stack_tick++;
786 debug_end_source_file (input_file_stack->line);
788 else
789 error ("#-lines for entering and leaving files don't match");
791 used_up = 1;
795 /* Now that we've pushed or popped the input stack,
796 update the name in the top element. */
797 if (input_file_stack)
798 input_file_stack->name = input_filename;
800 /* If we have handled a `1' or a `2',
801 see if there is another number to read. */
802 if (used_up)
804 /* Is this the last nonwhite stuff on the line? */
805 c = skip_white_space_on_line ();
806 if (c == '\n')
807 return c;
808 UNGETC (c);
810 token = yylex ();
811 used_up = 0;
814 /* `3' after file name means this is a system header file. */
816 if (token == CONSTANT
817 && TREE_CODE (yylval.ttype) == INTEGER_CST
818 && TREE_INT_CST_LOW (yylval.ttype) == 3)
819 in_system_header = 1, used_up = 1;
821 if (used_up)
823 /* Is this the last nonwhite stuff on the line? */
824 c = skip_white_space_on_line ();
825 if (c == '\n')
826 return c;
827 UNGETC (c);
830 warning ("unrecognized text at end of #line");
832 else
833 error ("invalid #-line");
835 /* skip the rest of this line. */
836 skipline:
837 #if !USE_CPPLIB
838 if (c != '\n' && c != EOF && nextchar >= 0)
839 c = nextchar, nextchar = -1;
840 #endif
841 while (c != '\n' && c != EOF)
842 c = GETC();
843 return c;
846 #ifdef HANDLE_GENERIC_PRAGMAS
848 /* Handle a #pragma directive.
849 TOKEN is the token we read after `#pragma'. Processes the entire input
850 line and return non-zero iff the pragma has been successfully parsed. */
852 /* This function has to be in this file, in order to get at
853 the token types. */
855 static int
856 handle_generic_pragma (token)
857 register int token;
859 register int c;
861 for (;;)
863 switch (token)
865 case IDENTIFIER:
866 case TYPENAME:
867 case STRING:
868 case CONSTANT:
869 handle_pragma_token (token_buffer, yylval.ttype);
870 break;
871 default:
872 handle_pragma_token (token_buffer, NULL);
874 #if !USE_CPPLIB
875 if (nextchar >= 0)
876 c = nextchar, nextchar = -1;
877 else
878 #endif
879 c = GETC ();
881 while (c == ' ' || c == '\t')
882 c = GETC ();
883 UNGETC (c);
885 if (c == '\n' || c == EOF)
886 return handle_pragma_token (NULL, NULL);
888 token = yylex ();
892 #endif /* HANDLE_GENERIC_PRAGMAS */
894 #define ENDFILE -1 /* token that represents end-of-file */
896 /* Read an escape sequence, returning its equivalent as a character,
897 or store 1 in *ignore_ptr if it is backslash-newline. */
899 static int
900 readescape (ignore_ptr)
901 int *ignore_ptr;
903 register int c = GETC();
904 register int code;
905 register unsigned count;
906 unsigned firstdig = 0;
907 int nonnull;
909 switch (c)
911 case 'x':
912 if (warn_traditional)
913 warning ("the meaning of `\\x' varies with -traditional");
915 if (flag_traditional)
916 return c;
918 code = 0;
919 count = 0;
920 nonnull = 0;
921 while (1)
923 c = GETC();
924 if (!(c >= 'a' && c <= 'f')
925 && !(c >= 'A' && c <= 'F')
926 && !(c >= '0' && c <= '9'))
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)
947 error ("\\x used with no following hex digits");
948 else if (count == 0)
949 /* Digits are all 0's. Ok. */
951 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
952 || (count > 1
953 && (((unsigned)1 << (TYPE_PRECISION (integer_type_node) - (count - 1) * 4))
954 <= firstdig)))
955 pedwarn ("hex escape out of range");
956 return code;
958 case '0': case '1': case '2': case '3': case '4':
959 case '5': case '6': case '7':
960 code = 0;
961 count = 0;
962 while ((c <= '7') && (c >= '0') && (count++ < 3))
964 code = (code * 8) + (c - '0');
965 c = GETC();
967 UNGETC (c);
968 return code;
970 case '\\': case '\'': case '"':
971 return c;
973 case '\n':
974 lineno++;
975 *ignore_ptr = 1;
976 return 0;
978 case 'n':
979 return TARGET_NEWLINE;
981 case 't':
982 return TARGET_TAB;
984 case 'r':
985 return TARGET_CR;
987 case 'f':
988 return TARGET_FF;
990 case 'b':
991 return TARGET_BS;
993 case 'a':
994 if (warn_traditional)
995 warning ("the meaning of `\\a' varies with -traditional");
997 if (flag_traditional)
998 return c;
999 return TARGET_BELL;
1001 case 'v':
1002 #if 0 /* Vertical tab is present in common usage compilers. */
1003 if (flag_traditional)
1004 return c;
1005 #endif
1006 return TARGET_VT;
1008 case 'e':
1009 case 'E':
1010 if (pedantic)
1011 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
1012 return 033;
1014 case '?':
1015 return c;
1017 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
1018 case '(':
1019 case '{':
1020 case '[':
1021 /* `\%' is used to prevent SCCS from getting confused. */
1022 case '%':
1023 if (pedantic)
1024 pedwarn ("non-ANSI escape sequence `\\%c'", c);
1025 return c;
1027 if (c >= 040 && c < 0177)
1028 pedwarn ("unknown escape sequence `\\%c'", c);
1029 else
1030 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
1031 return c;
1034 void
1035 yyerror (string)
1036 char *string;
1038 char buf[200];
1040 strcpy (buf, string);
1042 /* We can't print string and character constants well
1043 because the token_buffer contains the result of processing escapes. */
1044 if (end_of_file)
1045 strcat (buf, " at end of input");
1046 else if (token_buffer[0] == 0)
1047 strcat (buf, " at null character");
1048 else if (token_buffer[0] == '"')
1049 strcat (buf, " before string constant");
1050 else if (token_buffer[0] == '\'')
1051 strcat (buf, " before character constant");
1052 else if (token_buffer[0] < 040 || (unsigned char) token_buffer[0] >= 0177)
1053 sprintf (buf + strlen (buf), " before character 0%o",
1054 (unsigned char) token_buffer[0]);
1055 else
1056 strcat (buf, " before `%s'");
1058 error (buf, 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 */
1083 yylex ()
1085 register int c;
1086 register char *p;
1087 register int value;
1088 int wide_flag = 0;
1089 int objc_flag = 0;
1091 #if !USE_CPPLIB
1092 if (nextchar >= 0)
1093 c = nextchar, nextchar = -1;
1094 else
1095 #endif
1096 c = GETC();
1098 /* Effectively do c = skip_white_space (c)
1099 but do it faster in the usual cases. */
1100 while (1)
1101 switch (c)
1103 case ' ':
1104 case '\t':
1105 case '\f':
1106 case '\v':
1107 case '\b':
1108 c = GETC();
1109 break;
1111 case '\r':
1112 /* Call skip_white_space so we can warn if appropriate. */
1114 case '\n':
1115 case '/':
1116 case '\\':
1117 c = skip_white_space (c);
1118 default:
1119 goto found_nonwhite;
1121 found_nonwhite:
1123 token_buffer[0] = c;
1124 token_buffer[1] = 0;
1126 /* yylloc.first_line = lineno; */
1128 switch (c)
1130 case EOF:
1131 end_of_file = 1;
1132 token_buffer[0] = 0;
1133 value = ENDFILE;
1134 break;
1136 case 'L':
1137 /* Capital L may start a wide-string or wide-character constant. */
1139 register int c = GETC();
1140 if (c == '\'')
1142 wide_flag = 1;
1143 goto char_constant;
1145 if (c == '"')
1147 wide_flag = 1;
1148 goto string_constant;
1150 UNGETC (c);
1152 goto letter;
1154 case '@':
1155 if (!doing_objc_thang)
1157 value = c;
1158 break;
1160 else
1162 /* '@' may start a constant string object. */
1163 register int c = GETC ();
1164 if (c == '"')
1166 objc_flag = 1;
1167 goto string_constant;
1169 UNGETC (c);
1170 /* Fall through to treat '@' as the start of an identifier. */
1173 case 'A': case 'B': case 'C': case 'D': case 'E':
1174 case 'F': case 'G': case 'H': case 'I': case 'J':
1175 case 'K': case 'M': case 'N': case 'O':
1176 case 'P': case 'Q': case 'R': case 'S': case 'T':
1177 case 'U': case 'V': case 'W': case 'X': case 'Y':
1178 case 'Z':
1179 case 'a': case 'b': case 'c': case 'd': case 'e':
1180 case 'f': case 'g': case 'h': case 'i': case 'j':
1181 case 'k': case 'l': case 'm': case 'n': case 'o':
1182 case 'p': case 'q': case 'r': case 's': case 't':
1183 case 'u': case 'v': case 'w': case 'x': case 'y':
1184 case 'z':
1185 case '_':
1186 case '$':
1187 letter:
1188 p = token_buffer;
1189 while (ISALNUM (c) || c == '_' || c == '$' || c == '@')
1191 /* Make sure this char really belongs in an identifier. */
1192 if (c == '@' && ! doing_objc_thang)
1193 break;
1194 if (c == '$')
1196 if (! dollars_in_ident)
1197 error ("`$' in identifier");
1198 else if (pedantic)
1199 pedwarn ("`$' in identifier");
1202 if (p >= token_buffer + maxtoken)
1203 p = extend_token_buffer (p);
1205 *p++ = c;
1206 c = GETC();
1209 *p = 0;
1210 #if USE_CPPLIB
1211 UNGETC (c);
1212 #else
1213 nextchar = c;
1214 #endif
1216 value = IDENTIFIER;
1217 yylval.itype = 0;
1219 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1222 register struct resword *ptr;
1224 if ((ptr = is_reserved_word (token_buffer, p - token_buffer)))
1226 if (ptr->rid)
1227 yylval.ttype = ridpointers[(int) ptr->rid];
1228 value = (int) ptr->token;
1230 /* Only return OBJECTNAME if it is a typedef. */
1231 if (doing_objc_thang && value == OBJECTNAME)
1233 lastiddecl = lookup_name(yylval.ttype);
1235 if (lastiddecl == NULL_TREE
1236 || TREE_CODE (lastiddecl) != TYPE_DECL)
1237 value = IDENTIFIER;
1240 /* Even if we decided to recognize asm, still perhaps warn. */
1241 if (pedantic
1242 && (value == ASM_KEYWORD || value == TYPEOF
1243 || ptr->rid == RID_INLINE)
1244 && token_buffer[0] != '_')
1245 pedwarn ("ANSI does not permit the keyword `%s'",
1246 token_buffer);
1250 /* If we did not find a keyword, look for an identifier
1251 (or a typename). */
1253 if (value == IDENTIFIER)
1255 if (token_buffer[0] == '@')
1256 error("invalid identifier `%s'", token_buffer);
1258 yylval.ttype = get_identifier (token_buffer);
1259 lastiddecl = lookup_name (yylval.ttype);
1261 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1262 value = TYPENAME;
1263 /* A user-invisible read-only initialized variable
1264 should be replaced by its value.
1265 We handle only strings since that's the only case used in C. */
1266 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1267 && DECL_IGNORED_P (lastiddecl)
1268 && TREE_READONLY (lastiddecl)
1269 && DECL_INITIAL (lastiddecl) != 0
1270 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1272 tree stringval = DECL_INITIAL (lastiddecl);
1274 /* Copy the string value so that we won't clobber anything
1275 if we put something in the TREE_CHAIN of this one. */
1276 yylval.ttype = build_string (TREE_STRING_LENGTH (stringval),
1277 TREE_STRING_POINTER (stringval));
1278 value = STRING;
1280 else if (doing_objc_thang)
1282 tree objc_interface_decl = is_class_name (yylval.ttype);
1284 if (objc_interface_decl)
1286 value = CLASSNAME;
1287 yylval.ttype = objc_interface_decl;
1292 break;
1294 case '0': case '1':
1296 int next_c;
1297 /* Check first for common special case: single-digit 0 or 1. */
1299 next_c = GETC ();
1300 UNGETC (next_c); /* Always undo this lookahead. */
1301 if (!ISALNUM (next_c) && next_c != '.')
1303 token_buffer[0] = (char)c, token_buffer[1] = '\0';
1304 yylval.ttype = (c == '0') ? integer_zero_node : integer_one_node;
1305 value = CONSTANT;
1306 break;
1308 /*FALLTHRU*/
1310 case '2': case '3': case '4':
1311 case '5': case '6': case '7': case '8': case '9':
1312 case '.':
1314 int base = 10;
1315 int count = 0;
1316 int largest_digit = 0;
1317 int numdigits = 0;
1318 /* for multi-precision arithmetic,
1319 we actually store only HOST_BITS_PER_CHAR bits in each part.
1320 The number of parts is chosen so as to be sufficient to hold
1321 the enough bits to fit into the two HOST_WIDE_INTs that contain
1322 the integer value (this is always at least as many bits as are
1323 in a target `long long' value, but may be wider). */
1324 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2 + 2)
1325 int parts[TOTAL_PARTS];
1326 int overflow = 0;
1328 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS} floatflag
1329 = NOT_FLOAT;
1331 for (count = 0; count < TOTAL_PARTS; count++)
1332 parts[count] = 0;
1334 p = token_buffer;
1335 *p++ = c;
1337 if (c == '0')
1339 *p++ = (c = GETC());
1340 if ((c == 'x') || (c == 'X'))
1342 base = 16;
1343 *p++ = (c = GETC());
1345 /* Leading 0 forces octal unless the 0 is the only digit. */
1346 else if (c >= '0' && c <= '9')
1348 base = 8;
1349 numdigits++;
1351 else
1352 numdigits++;
1355 /* Read all the digits-and-decimal-points. */
1357 while (c == '.'
1358 || (ISALNUM (c) && c != 'l' && c != 'L'
1359 && c != 'u' && c != 'U'
1360 && c != 'i' && c != 'I' && c != 'j' && c != 'J'
1361 && (floatflag == NOT_FLOAT || ((c != 'f') && (c != 'F')))))
1363 if (c == '.')
1365 if (base == 16)
1366 error ("floating constant may not be in radix 16");
1367 if (floatflag == TOO_MANY_POINTS)
1368 /* We have already emitted an error. Don't need another. */
1370 else if (floatflag == AFTER_POINT)
1372 error ("malformed floating constant");
1373 floatflag = TOO_MANY_POINTS;
1374 /* Avoid another error from atof by forcing all characters
1375 from here on to be ignored. */
1376 p[-1] = '\0';
1378 else
1379 floatflag = AFTER_POINT;
1381 base = 10;
1382 *p++ = c = GETC();
1383 /* Accept '.' as the start of a floating-point number
1384 only when it is followed by a digit.
1385 Otherwise, unread the following non-digit
1386 and use the '.' as a structural token. */
1387 if (p == token_buffer + 2 && !ISDIGIT (c))
1389 if (c == '.')
1391 c = GETC();
1392 if (c == '.')
1394 *p++ = c;
1395 *p = 0;
1396 return ELLIPSIS;
1398 error ("parse error at `..'");
1400 UNGETC (c);
1401 token_buffer[1] = 0;
1402 value = '.';
1403 goto done;
1406 else
1408 /* It is not a decimal point.
1409 It should be a digit (perhaps a hex digit). */
1411 if (ISDIGIT (c))
1413 c = c - '0';
1415 else if (base <= 10)
1417 if (c == 'e' || c == 'E')
1419 base = 10;
1420 floatflag = AFTER_POINT;
1421 break; /* start of exponent */
1423 error ("nondigits in number and not hexadecimal");
1424 c = 0;
1426 else if (c >= 'a')
1428 c = c - 'a' + 10;
1430 else
1432 c = c - 'A' + 10;
1434 if (c >= largest_digit)
1435 largest_digit = c;
1436 numdigits++;
1438 for (count = 0; count < TOTAL_PARTS; count++)
1440 parts[count] *= base;
1441 if (count)
1443 parts[count]
1444 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1445 parts[count-1]
1446 &= (1 << HOST_BITS_PER_CHAR) - 1;
1448 else
1449 parts[0] += c;
1452 /* If the extra highest-order part ever gets anything in it,
1453 the number is certainly too big. */
1454 if (parts[TOTAL_PARTS - 1] != 0)
1455 overflow = 1;
1457 if (p >= token_buffer + maxtoken - 3)
1458 p = extend_token_buffer (p);
1459 *p++ = (c = GETC());
1463 if (numdigits == 0)
1464 error ("numeric constant with no digits");
1466 if (largest_digit >= base)
1467 error ("numeric constant contains digits beyond the radix");
1469 /* Remove terminating char from the token buffer and delimit the string */
1470 *--p = 0;
1472 if (floatflag != NOT_FLOAT)
1474 tree type = double_type_node;
1475 int imag = 0;
1476 int conversion_errno = 0;
1477 REAL_VALUE_TYPE value;
1478 jmp_buf handler;
1480 /* Read explicit exponent if any, and put it in tokenbuf. */
1482 if ((c == 'e') || (c == 'E'))
1484 if (p >= token_buffer + maxtoken - 3)
1485 p = extend_token_buffer (p);
1486 *p++ = c;
1487 c = GETC();
1488 if ((c == '+') || (c == '-'))
1490 *p++ = c;
1491 c = GETC();
1493 if (! ISDIGIT (c))
1494 error ("floating constant exponent has no digits");
1495 while (ISDIGIT (c))
1497 if (p >= token_buffer + maxtoken - 3)
1498 p = extend_token_buffer (p);
1499 *p++ = c;
1500 c = GETC();
1504 *p = 0;
1506 /* Convert string to a double, checking for overflow. */
1507 if (setjmp (handler))
1509 error ("floating constant out of range");
1510 value = dconst0;
1512 else
1514 int fflag = 0, lflag = 0;
1515 /* Copy token_buffer now, while it has just the number
1516 and not the suffixes; once we add `f' or `i',
1517 REAL_VALUE_ATOF may not work any more. */
1518 char *copy = (char *) alloca (p - token_buffer + 1);
1519 bcopy (token_buffer, copy, p - token_buffer + 1);
1521 set_float_handler (handler);
1523 while (1)
1525 int lose = 0;
1527 /* Read the suffixes to choose a data type. */
1528 switch (c)
1530 case 'f': case 'F':
1531 if (fflag)
1532 error ("more than one `f' in numeric constant");
1533 fflag = 1;
1534 break;
1536 case 'l': case 'L':
1537 if (lflag)
1538 error ("more than one `l' in numeric constant");
1539 lflag = 1;
1540 break;
1542 case 'i': case 'I':
1543 if (imag)
1544 error ("more than one `i' or `j' in numeric constant");
1545 else if (pedantic)
1546 pedwarn ("ANSI C forbids imaginary numeric constants");
1547 imag = 1;
1548 break;
1550 default:
1551 lose = 1;
1554 if (lose)
1555 break;
1557 if (p >= token_buffer + maxtoken - 3)
1558 p = extend_token_buffer (p);
1559 *p++ = c;
1560 *p = 0;
1561 c = GETC();
1564 /* The second argument, machine_mode, of REAL_VALUE_ATOF
1565 tells the desired precision of the binary result
1566 of decimal-to-binary conversion. */
1568 if (fflag)
1570 if (lflag)
1571 error ("both `f' and `l' in floating constant");
1573 type = float_type_node;
1574 errno = 0;
1575 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1576 conversion_errno = errno;
1577 /* A diagnostic is required here by some ANSI C testsuites.
1578 This is not pedwarn, become some people don't want
1579 an error for this. */
1580 if (REAL_VALUE_ISINF (value) && pedantic)
1581 warning ("floating point number exceeds range of `float'");
1583 else if (lflag)
1585 type = long_double_type_node;
1586 errno = 0;
1587 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1588 conversion_errno = errno;
1589 if (REAL_VALUE_ISINF (value) && pedantic)
1590 warning ("floating point number exceeds range of `long double'");
1592 else
1594 errno = 0;
1595 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1596 conversion_errno = errno;
1597 if (REAL_VALUE_ISINF (value) && pedantic)
1598 warning ("floating point number exceeds range of `double'");
1601 set_float_handler (NULL_PTR);
1603 #ifdef ERANGE
1604 /* ERANGE is also reported for underflow,
1605 so test the value to distinguish overflow from that. */
1606 if (conversion_errno == ERANGE && !flag_traditional && pedantic
1607 && (REAL_VALUES_LESS (dconst1, value)
1608 || REAL_VALUES_LESS (value, dconstm1)))
1609 warning ("floating point number exceeds range of `double'");
1610 #endif
1612 /* If the result is not a number, assume it must have been
1613 due to some error message above, so silently convert
1614 it to a zero. */
1615 if (REAL_VALUE_ISNAN (value))
1616 value = dconst0;
1618 /* Create a node with determined type and value. */
1619 if (imag)
1620 yylval.ttype = build_complex (NULL_TREE,
1621 convert (type, integer_zero_node),
1622 build_real (type, value));
1623 else
1624 yylval.ttype = build_real (type, value);
1626 else
1628 tree traditional_type, ansi_type, type;
1629 HOST_WIDE_INT high, low;
1630 int spec_unsigned = 0;
1631 int spec_long = 0;
1632 int spec_long_long = 0;
1633 int spec_imag = 0;
1634 int bytes, warn, i;
1636 traditional_type = ansi_type = type = NULL_TREE;
1637 while (1)
1639 if (c == 'u' || c == 'U')
1641 if (spec_unsigned)
1642 error ("two `u's in integer constant");
1643 spec_unsigned = 1;
1645 else if (c == 'l' || c == 'L')
1647 if (spec_long)
1649 if (spec_long_long)
1650 error ("three `l's in integer constant");
1651 else if (pedantic && ! in_system_header && warn_long_long)
1652 pedwarn ("ANSI C forbids long long integer constants");
1653 spec_long_long = 1;
1655 spec_long = 1;
1657 else if (c == 'i' || c == 'j' || c == 'I' || c == 'J')
1659 if (spec_imag)
1660 error ("more than one `i' or `j' in numeric constant");
1661 else if (pedantic)
1662 pedwarn ("ANSI C forbids imaginary numeric constants");
1663 spec_imag = 1;
1665 else
1666 break;
1667 if (p >= token_buffer + maxtoken - 3)
1668 p = extend_token_buffer (p);
1669 *p++ = c;
1670 c = GETC();
1673 /* If the constant won't fit in an unsigned long long,
1674 then warn that the constant is out of range. */
1676 /* ??? This assumes that long long and long integer types are
1677 a multiple of 8 bits. This better than the original code
1678 though which assumed that long was exactly 32 bits and long
1679 long was exactly 64 bits. */
1681 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1683 warn = overflow;
1684 for (i = bytes; i < TOTAL_PARTS; i++)
1685 if (parts[i])
1686 warn = 1;
1687 if (warn)
1688 pedwarn ("integer constant out of range");
1690 /* This is simplified by the fact that our constant
1691 is always positive. */
1693 high = low = 0;
1695 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1697 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1698 / HOST_BITS_PER_CHAR)]
1699 << (i * HOST_BITS_PER_CHAR));
1700 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1703 yylval.ttype = build_int_2 (low, high);
1704 TREE_TYPE (yylval.ttype) = long_long_unsigned_type_node;
1706 /* If warn_traditional, calculate both the ANSI type and the
1707 traditional type, then see if they disagree.
1708 Otherwise, calculate only the type for the dialect in use. */
1709 if (warn_traditional || flag_traditional)
1711 /* Calculate the traditional type. */
1712 /* Traditionally, any constant is signed;
1713 but if unsigned is specified explicitly, obey that.
1714 Use the smallest size with the right number of bits,
1715 except for one special case with decimal constants. */
1716 if (! spec_long && base != 10
1717 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1718 traditional_type = (spec_unsigned ? unsigned_type_node
1719 : integer_type_node);
1720 /* A decimal constant must be long
1721 if it does not fit in type int.
1722 I think this is independent of whether
1723 the constant is signed. */
1724 else if (! spec_long && base == 10
1725 && int_fits_type_p (yylval.ttype, integer_type_node))
1726 traditional_type = (spec_unsigned ? unsigned_type_node
1727 : integer_type_node);
1728 else if (! spec_long_long)
1729 traditional_type = (spec_unsigned ? long_unsigned_type_node
1730 : long_integer_type_node);
1731 else
1732 traditional_type = (spec_unsigned
1733 ? long_long_unsigned_type_node
1734 : long_long_integer_type_node);
1736 if (warn_traditional || ! flag_traditional)
1738 /* Calculate the ANSI type. */
1739 if (! spec_long && ! spec_unsigned
1740 && int_fits_type_p (yylval.ttype, integer_type_node))
1741 ansi_type = integer_type_node;
1742 else if (! spec_long && (base != 10 || spec_unsigned)
1743 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1744 ansi_type = unsigned_type_node;
1745 else if (! spec_unsigned && !spec_long_long
1746 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1747 ansi_type = long_integer_type_node;
1748 else if (! spec_long_long
1749 && int_fits_type_p (yylval.ttype,
1750 long_unsigned_type_node))
1751 ansi_type = long_unsigned_type_node;
1752 else if (! spec_unsigned
1753 && int_fits_type_p (yylval.ttype,
1754 long_long_integer_type_node))
1755 ansi_type = long_long_integer_type_node;
1756 else
1757 ansi_type = long_long_unsigned_type_node;
1760 type = flag_traditional ? traditional_type : ansi_type;
1762 if (warn_traditional && traditional_type != ansi_type)
1764 if (TYPE_PRECISION (traditional_type)
1765 != TYPE_PRECISION (ansi_type))
1766 warning ("width of integer constant changes with -traditional");
1767 else if (TREE_UNSIGNED (traditional_type)
1768 != TREE_UNSIGNED (ansi_type))
1769 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
1770 else
1771 warning ("width of integer constant may change on other systems with -traditional");
1774 if (pedantic && !flag_traditional && !spec_long_long && !warn
1775 && (TYPE_PRECISION (long_integer_type_node)
1776 < TYPE_PRECISION (type)))
1777 pedwarn ("integer constant out of range");
1779 if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type))
1780 warning ("decimal constant is so large that it is unsigned");
1782 if (spec_imag)
1784 if (TYPE_PRECISION (type)
1785 <= TYPE_PRECISION (integer_type_node))
1786 yylval.ttype
1787 = build_complex (NULL_TREE, integer_zero_node,
1788 convert (integer_type_node,
1789 yylval.ttype));
1790 else
1791 error ("complex integer constant is too wide for `complex int'");
1793 else if (flag_traditional && !int_fits_type_p (yylval.ttype, type))
1794 /* The traditional constant 0x80000000 is signed
1795 but doesn't fit in the range of int.
1796 This will change it to -0x80000000, which does fit. */
1798 TREE_TYPE (yylval.ttype) = unsigned_type (type);
1799 yylval.ttype = convert (type, yylval.ttype);
1800 TREE_OVERFLOW (yylval.ttype)
1801 = TREE_CONSTANT_OVERFLOW (yylval.ttype) = 0;
1803 else
1804 TREE_TYPE (yylval.ttype) = type;
1807 UNGETC (c);
1808 *p = 0;
1810 if (ISALNUM (c) || c == '.' || c == '_' || c == '$'
1811 || (!flag_traditional && (c == '-' || c == '+')
1812 && (p[-1] == 'e' || p[-1] == 'E')))
1813 error ("missing white space after number `%s'", token_buffer);
1815 value = CONSTANT; break;
1818 case '\'':
1819 char_constant:
1821 register int result = 0;
1822 register int num_chars = 0;
1823 int chars_seen = 0;
1824 unsigned width = TYPE_PRECISION (char_type_node);
1825 int max_chars;
1826 #ifdef MULTIBYTE_CHARS
1827 int longest_char = local_mb_cur_max ();
1828 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
1829 #endif
1831 max_chars = TYPE_PRECISION (integer_type_node) / width;
1832 if (wide_flag)
1833 width = WCHAR_TYPE_SIZE;
1835 while (1)
1837 tryagain:
1838 c = GETC();
1840 if (c == '\'' || c == EOF)
1841 break;
1843 ++chars_seen;
1844 if (c == '\\')
1846 int ignore = 0;
1847 c = readescape (&ignore);
1848 if (ignore)
1849 goto tryagain;
1850 if (width < HOST_BITS_PER_INT
1851 && (unsigned) c >= ((unsigned)1 << width))
1852 pedwarn ("escape sequence out of range for character");
1853 #ifdef MAP_CHARACTER
1854 if (ISPRINT (c))
1855 c = MAP_CHARACTER (c);
1856 #endif
1858 else if (c == '\n')
1860 if (pedantic)
1861 pedwarn ("ANSI C forbids newline in character constant");
1862 lineno++;
1864 else
1866 #ifdef MULTIBYTE_CHARS
1867 wchar_t wc;
1868 int i;
1869 int char_len = -1;
1870 for (i = 1; i <= longest_char; ++i)
1872 if (i > maxtoken - 4)
1873 extend_token_buffer (token_buffer);
1875 token_buffer[i] = c;
1876 char_len = local_mbtowc (& wc,
1877 token_buffer + 1,
1879 if (char_len != -1)
1880 break;
1881 c = GETC ();
1883 if (char_len > 1)
1885 /* mbtowc sometimes needs an extra char before accepting */
1886 if (char_len < i)
1887 UNGETC (c);
1888 if (! wide_flag)
1890 /* Merge character into result; ignore excess chars. */
1891 for (i = 1; i <= char_len; ++i)
1893 if (i > max_chars)
1894 break;
1895 if (width < HOST_BITS_PER_INT)
1896 result = (result << width)
1897 | (token_buffer[i]
1898 & ((1 << width) - 1));
1899 else
1900 result = token_buffer[i];
1902 num_chars += char_len;
1903 goto tryagain;
1905 c = wc;
1907 else
1909 if (char_len == -1)
1910 warning ("Ignoring invalid multibyte character");
1911 if (wide_flag)
1912 c = wc;
1913 #ifdef MAP_CHARACTER
1914 else
1915 c = MAP_CHARACTER (c);
1916 #endif
1918 #else /* ! MULTIBYTE_CHARS */
1919 #ifdef MAP_CHARACTER
1920 c = MAP_CHARACTER (c);
1921 #endif
1922 #endif /* ! MULTIBYTE_CHARS */
1925 if (wide_flag)
1927 if (chars_seen == 1) /* only keep the first one */
1928 result = c;
1929 goto tryagain;
1932 /* Merge character into result; ignore excess chars. */
1933 num_chars += (width / TYPE_PRECISION (char_type_node));
1934 if (num_chars < max_chars + 1)
1936 if (width < HOST_BITS_PER_INT)
1937 result = (result << width) | (c & ((1 << width) - 1));
1938 else
1939 result = c;
1943 if (c != '\'')
1944 error ("malformatted character constant");
1945 else if (chars_seen == 0)
1946 error ("empty character constant");
1947 else if (num_chars > max_chars)
1949 num_chars = max_chars;
1950 error ("character constant too long");
1952 else if (chars_seen != 1 && ! flag_traditional && warn_multichar)
1953 warning ("multi-character character constant");
1955 /* If char type is signed, sign-extend the constant. */
1956 if (! wide_flag)
1958 int num_bits = num_chars * width;
1959 if (num_bits == 0)
1960 /* We already got an error; avoid invalid shift. */
1961 yylval.ttype = build_int_2 (0, 0);
1962 else if (TREE_UNSIGNED (char_type_node)
1963 || ((result >> (num_bits - 1)) & 1) == 0)
1964 yylval.ttype
1965 = build_int_2 (result & (~(unsigned HOST_WIDE_INT) 0
1966 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1968 else
1969 yylval.ttype
1970 = build_int_2 (result | ~(~(unsigned HOST_WIDE_INT) 0
1971 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1972 -1);
1973 TREE_TYPE (yylval.ttype) = integer_type_node;
1975 else
1977 yylval.ttype = build_int_2 (result, 0);
1978 TREE_TYPE (yylval.ttype) = wchar_type_node;
1981 value = CONSTANT;
1982 break;
1985 case '"':
1986 string_constant:
1988 unsigned width = wide_flag ? WCHAR_TYPE_SIZE
1989 : TYPE_PRECISION (char_type_node);
1990 #ifdef MULTIBYTE_CHARS
1991 int longest_char = local_mb_cur_max ();
1992 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
1993 #endif
1994 c = GETC ();
1995 p = token_buffer + 1;
1997 while (c != '"' && c >= 0)
1999 if (c == '\\')
2001 int ignore = 0;
2002 c = readescape (&ignore);
2003 if (ignore)
2004 goto skipnewline;
2005 if (width < HOST_BITS_PER_INT
2006 && (unsigned) c >= ((unsigned)1 << width))
2007 pedwarn ("escape sequence out of range for character");
2009 else if (c == '\n')
2011 if (pedantic)
2012 pedwarn ("ANSI C forbids newline in string constant");
2013 lineno++;
2015 else
2017 #ifdef MULTIBYTE_CHARS
2018 wchar_t wc;
2019 int i;
2020 int char_len = -1;
2021 for (i = 0; i < longest_char; ++i)
2023 if (p + i >= token_buffer + maxtoken)
2024 p = extend_token_buffer (p);
2025 p[i] = c;
2027 char_len = local_mbtowc (& wc, p, i + 1);
2028 if (char_len != -1)
2029 break;
2030 c = GETC ();
2032 if (char_len == -1)
2033 warning ("Ignoring invalid multibyte character");
2034 else
2036 /* mbtowc sometimes needs an extra char before accepting */
2037 if (char_len <= i)
2038 UNGETC (c);
2039 if (wide_flag)
2041 *(wchar_t *)p = wc;
2042 p += sizeof (wc);
2044 else
2045 p += (i + 1);
2046 c = GETC ();
2047 continue;
2049 #endif /* MULTIBYTE_CHARS */
2052 /* Add this single character into the buffer either as a wchar_t
2053 or as a single byte. */
2054 if (wide_flag)
2056 unsigned width = TYPE_PRECISION (char_type_node);
2057 unsigned bytemask = (1 << width) - 1;
2058 int byte;
2060 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2061 p = extend_token_buffer (p);
2063 for (byte = 0; byte < WCHAR_BYTES; ++byte)
2065 int value;
2066 if (byte >= (int) sizeof (c))
2067 value = 0;
2068 else
2069 value = (c >> (byte * width)) & bytemask;
2070 if (BYTES_BIG_ENDIAN)
2071 p[WCHAR_BYTES - byte - 1] = value;
2072 else
2073 p[byte] = value;
2075 p += WCHAR_BYTES;
2077 else
2079 if (p >= token_buffer + maxtoken)
2080 p = extend_token_buffer (p);
2081 *p++ = c;
2084 skipnewline:
2085 c = GETC ();
2088 /* Terminate the string value, either with a single byte zero
2089 or with a wide zero. */
2090 if (wide_flag)
2092 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2093 p = extend_token_buffer (p);
2094 bzero (p, WCHAR_BYTES);
2095 p += WCHAR_BYTES;
2097 else
2099 if (p >= token_buffer + maxtoken)
2100 p = extend_token_buffer (p);
2101 *p++ = 0;
2104 if (c < 0)
2105 error ("Unterminated string constant");
2107 /* We have read the entire constant.
2108 Construct a STRING_CST for the result. */
2110 if (wide_flag)
2112 yylval.ttype = build_string (p - (token_buffer + 1),
2113 token_buffer + 1);
2114 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
2115 value = STRING;
2117 else if (objc_flag)
2119 /* Return an Objective-C @"..." constant string object. */
2120 yylval.ttype = build_objc_string (p - (token_buffer + 1),
2121 token_buffer + 1);
2122 TREE_TYPE (yylval.ttype) = char_array_type_node;
2123 value = OBJC_STRING;
2125 else
2127 yylval.ttype = build_string (p - (token_buffer + 1),
2128 token_buffer + 1);
2129 TREE_TYPE (yylval.ttype) = char_array_type_node;
2130 value = STRING;
2133 break;
2136 case '+':
2137 case '-':
2138 case '&':
2139 case '|':
2140 case ':':
2141 case '<':
2142 case '>':
2143 case '*':
2144 case '/':
2145 case '%':
2146 case '^':
2147 case '!':
2148 case '=':
2150 register int c1;
2152 combine:
2154 switch (c)
2156 case '+':
2157 yylval.code = PLUS_EXPR; break;
2158 case '-':
2159 yylval.code = MINUS_EXPR; break;
2160 case '&':
2161 yylval.code = BIT_AND_EXPR; break;
2162 case '|':
2163 yylval.code = BIT_IOR_EXPR; break;
2164 case '*':
2165 yylval.code = MULT_EXPR; break;
2166 case '/':
2167 yylval.code = TRUNC_DIV_EXPR; break;
2168 case '%':
2169 yylval.code = TRUNC_MOD_EXPR; break;
2170 case '^':
2171 yylval.code = BIT_XOR_EXPR; break;
2172 case LSHIFT:
2173 yylval.code = LSHIFT_EXPR; break;
2174 case RSHIFT:
2175 yylval.code = RSHIFT_EXPR; break;
2176 case '<':
2177 yylval.code = LT_EXPR; break;
2178 case '>':
2179 yylval.code = GT_EXPR; break;
2182 token_buffer[1] = c1 = GETC();
2183 token_buffer[2] = 0;
2185 if (c1 == '=')
2187 switch (c)
2189 case '<':
2190 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
2191 case '>':
2192 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
2193 case '!':
2194 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
2195 case '=':
2196 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
2198 value = ASSIGN; goto done;
2200 else if (c == c1)
2201 switch (c)
2203 case '+':
2204 value = PLUSPLUS; goto done;
2205 case '-':
2206 value = MINUSMINUS; goto done;
2207 case '&':
2208 value = ANDAND; goto done;
2209 case '|':
2210 value = OROR; goto done;
2211 case '<':
2212 c = LSHIFT;
2213 goto combine;
2214 case '>':
2215 c = RSHIFT;
2216 goto combine;
2218 else
2219 switch (c)
2221 case '-':
2222 if (c1 == '>')
2223 { value = POINTSAT; goto done; }
2224 break;
2225 case ':':
2226 if (c1 == '>')
2227 { value = ']'; goto done; }
2228 break;
2229 case '<':
2230 if (c1 == '%')
2231 { value = '{'; indent_level++; goto done; }
2232 if (c1 == ':')
2233 { value = '['; goto done; }
2234 break;
2235 case '%':
2236 if (c1 == '>')
2237 { value = '}'; indent_level--; goto done; }
2238 break;
2240 UNGETC (c1);
2241 token_buffer[1] = 0;
2243 if ((c == '<') || (c == '>'))
2244 value = ARITHCOMPARE;
2245 else value = c;
2246 goto done;
2249 case 0:
2250 /* Don't make yyparse think this is eof. */
2251 value = 1;
2252 break;
2254 case '{':
2255 indent_level++;
2256 value = c;
2257 break;
2259 case '}':
2260 indent_level--;
2261 value = c;
2262 break;
2264 default:
2265 value = c;
2268 done:
2269 /* yylloc.last_line = lineno; */
2271 return value;
2274 /* Sets the value of the 'yydebug' variable to VALUE.
2275 This is a function so we don't have to have YYDEBUG defined
2276 in order to build the compiler. */
2278 void
2279 set_yydebug (value)
2280 int value;
2282 #if YYDEBUG != 0
2283 yydebug = value;
2284 #else
2285 warning ("YYDEBUG not defined.");
2286 #endif