]> gcc.gnu.org Git - gcc.git/blob - gcc/c-lex.c
toplev.c (compile_file): Call init_parse using new interface.
[gcc.git] / gcc / c-lex.c
1 /* Lexical analyzer for C and Objective C.
2 Copyright (C) 1987, 88, 89, 92, 94-97, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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. */
20
21 #include "config.h"
22 #include "system.h"
23 #include <setjmp.h>
24
25 #include "rtl.h"
26 #include "tree.h"
27 #include "input.h"
28 #include "c-lex.h"
29 #include "c-tree.h"
30 #include "flags.h"
31 #include "c-parse.h"
32 #include "c-pragma.h"
33
34 /* MULTIBYTE_CHARS support only works for native compilers.
35 ??? Ideally what we want is to model widechar support after
36 the current floating point support. */
37 #ifdef CROSS_COMPILE
38 #undef MULTIBYTE_CHARS
39 #endif
40
41 #ifdef MULTIBYTE_CHARS
42 #include <locale.h>
43 #endif
44
45 #if USE_CPPLIB
46 #include "cpplib.h"
47 cpp_reader parse_in;
48 cpp_options parse_options;
49 static enum cpp_token cpp_token;
50 #else
51 /* Stream for reading from the input file. */
52 FILE *finput;
53 #endif
54
55 /* The elements of `ridpointers' are identifier nodes
56 for the reserved type names and storage classes.
57 It is indexed by a RID_... value. */
58 tree ridpointers[(int) RID_MAX];
59
60 /* Cause the `yydebug' variable to be defined. */
61 #define YYDEBUG 1
62
63 #if USE_CPPLIB
64 static unsigned char *yy_cur, *yy_lim;
65
66 int
67 yy_get_token ()
68 {
69 for (;;)
70 {
71 parse_in.limit = parse_in.token_buffer;
72 cpp_token = cpp_get_token (&parse_in);
73 if (cpp_token == CPP_EOF)
74 return -1;
75 yy_lim = CPP_PWRITTEN (&parse_in);
76 yy_cur = parse_in.token_buffer;
77 if (yy_cur < yy_lim)
78 return *yy_cur++;
79 }
80 }
81
82 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
83 #define UNGETC(c) ((c), yy_cur--)
84 #else
85 #define GETC() getc (finput)
86 #define UNGETC(c) ungetc (c, finput)
87 #endif
88
89 /* the declaration found for the last IDENTIFIER token read in.
90 yylex must look this up to detect typedefs, which get token type TYPENAME,
91 so it is left around in case the identifier is not a typedef but is
92 used in a context which makes it a reference to a variable. */
93 tree lastiddecl;
94
95 /* Nonzero enables objc features. */
96
97 int doing_objc_thang;
98
99 extern int yydebug;
100
101 /* File used for outputting assembler code. */
102 extern FILE *asm_out_file;
103
104 #ifndef WCHAR_TYPE_SIZE
105 #ifdef INT_TYPE_SIZE
106 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
107 #else
108 #define WCHAR_TYPE_SIZE BITS_PER_WORD
109 #endif
110 #endif
111
112 /* Number of bytes in a wide character. */
113 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
114
115 static int maxtoken; /* Current nominal length of token buffer. */
116 char *token_buffer; /* Pointer to token buffer.
117 Actual allocated length is maxtoken + 2.
118 This is not static because objc-parse.y uses it. */
119
120 static int indent_level = 0; /* Number of { minus number of }. */
121
122 /* Nonzero if end-of-file has been seen on input. */
123 static int end_of_file;
124
125 #if !USE_CPPLIB
126 /* Buffered-back input character; faster than using ungetc. */
127 static int nextchar = -1;
128 #endif
129
130 #ifdef HANDLE_SYSV_PRAGMA
131 static int handle_sysv_pragma PROTO((int));
132 #endif /* HANDLE_SYSV_PRAGMA */
133 static int whitespace_cr PROTO((int));
134 static int skip_white_space PROTO((int));
135 static char *extend_token_buffer PROTO((char *));
136 static int readescape PROTO((int *));
137 int check_newline ();
138 \f
139 /* Do not insert generated code into the source, instead, include it.
140 This allows us to build gcc automatically even for targets that
141 need to add or modify the reserved keyword lists. */
142 #include "c-gperf.h"
143 \f
144 /* Return something to represent absolute declarators containing a *.
145 TARGET is the absolute declarator that the * contains.
146 TYPE_QUALS is a list of modifiers such as const or volatile
147 to apply to the pointer type, represented as identifiers.
148
149 We return an INDIRECT_REF whose "contents" are TARGET
150 and whose type is the modifier list. */
151
152 tree
153 make_pointer_declarator (type_quals, target)
154 tree type_quals, target;
155 {
156 return build1 (INDIRECT_REF, type_quals, target);
157 }
158 \f
159 void
160 forget_protocol_qualifiers ()
161 {
162 int i, n = sizeof wordlist / sizeof (struct resword);
163
164 for (i = 0; i < n; i++)
165 if ((int) wordlist[i].rid >= (int) RID_IN
166 && (int) wordlist[i].rid <= (int) RID_ONEWAY)
167 wordlist[i].name = "";
168 }
169
170 void
171 remember_protocol_qualifiers ()
172 {
173 int i, n = sizeof wordlist / sizeof (struct resword);
174
175 for (i = 0; i < n; i++)
176 if (wordlist[i].rid == RID_IN)
177 wordlist[i].name = "in";
178 else if (wordlist[i].rid == RID_OUT)
179 wordlist[i].name = "out";
180 else if (wordlist[i].rid == RID_INOUT)
181 wordlist[i].name = "inout";
182 else if (wordlist[i].rid == RID_BYCOPY)
183 wordlist[i].name = "bycopy";
184 else if (wordlist[i].rid == RID_ONEWAY)
185 wordlist[i].name = "oneway";
186 }
187 \f
188 char *
189 init_parse (filename)
190 char *filename;
191 {
192 #if !USE_CPPLIB
193 /* Open input file. */
194 if (filename == 0 || !strcmp (filename, "-"))
195 {
196 finput = stdin;
197 filename = "stdin";
198 }
199 else
200 finput = fopen (filename, "r");
201 if (finput == 0)
202 pfatal_with_name (filename);
203
204 #ifdef IO_BUFFER_SIZE
205 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE), _IOFBF, IO_BUFFER_SIZE);
206 #endif
207 #endif /* !USE_CPPLIB */
208
209 init_lex ();
210
211 #if USE_CPPLIB
212 yy_cur = "\n";
213 yy_lim = yy_cur+1;
214
215 cpp_reader_init (&parse_in);
216 parse_in.data = &parse_options;
217 cpp_options_init (&parse_options);
218 cpp_handle_options (&parse_in, 0, NULL); /* FIXME */
219 parse_in.show_column = 1;
220 if (! cpp_start_read (&parse_in, filename))
221 abort ();
222 #endif
223
224 return filename;
225 }
226
227 void
228 finish_parse ()
229 {
230 #if USE_CPPLIB
231 cpp_finish (&parse_in);
232 #else
233 fclose (finput);
234 #endif
235 }
236
237 void
238 init_lex ()
239 {
240 /* Make identifier nodes long enough for the language-specific slots. */
241 set_identifier_size (sizeof (struct lang_identifier));
242
243 /* Start it at 0, because check_newline is called at the very beginning
244 and will increment it to 1. */
245 lineno = 0;
246
247 #ifdef MULTIBYTE_CHARS
248 /* Change to the native locale for multibyte conversions. */
249 setlocale (LC_CTYPE, "");
250 #endif
251
252 maxtoken = 40;
253 token_buffer = (char *) xmalloc (maxtoken + 2);
254
255 ridpointers[(int) RID_INT] = get_identifier ("int");
256 ridpointers[(int) RID_CHAR] = get_identifier ("char");
257 ridpointers[(int) RID_VOID] = get_identifier ("void");
258 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
259 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
260 ridpointers[(int) RID_SHORT] = get_identifier ("short");
261 ridpointers[(int) RID_LONG] = get_identifier ("long");
262 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
263 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
264 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
265 ridpointers[(int) RID_CONST] = get_identifier ("const");
266 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
267 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
268 ridpointers[(int) RID_STATIC] = get_identifier ("static");
269 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
270 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
271 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
272 ridpointers[(int) RID_ITERATOR] = get_identifier ("iterator");
273 ridpointers[(int) RID_COMPLEX] = get_identifier ("complex");
274 ridpointers[(int) RID_ID] = get_identifier ("id");
275 ridpointers[(int) RID_IN] = get_identifier ("in");
276 ridpointers[(int) RID_OUT] = get_identifier ("out");
277 ridpointers[(int) RID_INOUT] = get_identifier ("inout");
278 ridpointers[(int) RID_BYCOPY] = get_identifier ("bycopy");
279 ridpointers[(int) RID_ONEWAY] = get_identifier ("oneway");
280 forget_protocol_qualifiers();
281
282 /* Some options inhibit certain reserved words.
283 Clear those words out of the hash table so they won't be recognized. */
284 #define UNSET_RESERVED_WORD(STRING) \
285 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
286 if (s) s->name = ""; } while (0)
287
288 if (! doing_objc_thang)
289 UNSET_RESERVED_WORD ("id");
290
291 if (flag_traditional)
292 {
293 UNSET_RESERVED_WORD ("const");
294 UNSET_RESERVED_WORD ("volatile");
295 UNSET_RESERVED_WORD ("typeof");
296 UNSET_RESERVED_WORD ("signed");
297 UNSET_RESERVED_WORD ("inline");
298 UNSET_RESERVED_WORD ("iterator");
299 UNSET_RESERVED_WORD ("complex");
300 }
301 if (flag_no_asm)
302 {
303 UNSET_RESERVED_WORD ("asm");
304 UNSET_RESERVED_WORD ("typeof");
305 UNSET_RESERVED_WORD ("inline");
306 UNSET_RESERVED_WORD ("iterator");
307 UNSET_RESERVED_WORD ("complex");
308 }
309 }
310
311 void
312 reinit_parse_for_function ()
313 {
314 }
315 \f
316 /* Function used when yydebug is set, to print a token in more detail. */
317
318 void
319 yyprint (file, yychar, yylval)
320 FILE *file;
321 int yychar;
322 YYSTYPE yylval;
323 {
324 tree t;
325 switch (yychar)
326 {
327 case IDENTIFIER:
328 case TYPENAME:
329 case OBJECTNAME:
330 t = yylval.ttype;
331 if (IDENTIFIER_POINTER (t))
332 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
333 break;
334
335 case CONSTANT:
336 t = yylval.ttype;
337 if (TREE_CODE (t) == INTEGER_CST)
338 fprintf (file,
339 #if HOST_BITS_PER_WIDE_INT == 64
340 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
341 " 0x%x%016x",
342 #else
343 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
344 " 0x%lx%016lx",
345 #else
346 " 0x%llx%016llx",
347 #endif
348 #endif
349 #else
350 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
351 " 0x%lx%08lx",
352 #else
353 " 0x%x%08x",
354 #endif
355 #endif
356 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
357 break;
358 }
359 }
360 \f
361 /* Iff C is a carriage return, warn about it - if appropriate -
362 and return nonzero. */
363 static int
364 whitespace_cr (c)
365 int c;
366 {
367 static int newline_warning = 0;
368
369 if (c == '\r')
370 {
371 /* ANSI C says the effects of a carriage return in a source file
372 are undefined. */
373 if (pedantic && !newline_warning)
374 {
375 warning ("carriage return in source file");
376 warning ("(we only warn about the first carriage return)");
377 newline_warning = 1;
378 }
379 return 1;
380 }
381 return 0;
382 }
383
384 /* If C is not whitespace, return C.
385 Otherwise skip whitespace and return first nonwhite char read. */
386
387 static int
388 skip_white_space (c)
389 register int c;
390 {
391 for (;;)
392 {
393 switch (c)
394 {
395 /* We don't recognize comments here, because
396 cpp output can include / and * consecutively as operators.
397 Also, there's no need, since cpp removes all comments. */
398
399 case '\n':
400 c = check_newline ();
401 break;
402
403 case ' ':
404 case '\t':
405 case '\f':
406 case '\v':
407 case '\b':
408 c = GETC();
409 break;
410
411 case '\r':
412 whitespace_cr (c);
413 c = GETC();
414 break;
415
416 case '\\':
417 c = GETC();
418 if (c == '\n')
419 lineno++;
420 else
421 error ("stray '\\' in program");
422 c = GETC();
423 break;
424
425 default:
426 return (c);
427 }
428 }
429 }
430
431 /* Skips all of the white space at the current location in the input file.
432 Must use and reset nextchar if it has the next character. */
433
434 void
435 position_after_white_space ()
436 {
437 register int c;
438
439 #if !USE_CPPLIB
440 if (nextchar != -1)
441 c = nextchar, nextchar = -1;
442 else
443 #endif
444 c = GETC();
445
446 UNGETC (skip_white_space (c));
447 }
448
449 /* Like skip_white_space, but don't advance beyond the end of line.
450 Moreover, we don't get passed a character to start with. */
451 static int
452 skip_white_space_on_line ()
453 {
454 register int c;
455
456 while (1)
457 {
458 c = GETC();
459 switch (c)
460 {
461 case '\n':
462 default:
463 break;
464
465 case ' ':
466 case '\t':
467 case '\f':
468 case '\v':
469 case '\b':
470 continue;
471
472 case '\r':
473 whitespace_cr (c);
474 continue;
475 }
476 break;
477 }
478 return c;
479 }
480
481 /* Make the token buffer longer, preserving the data in it.
482 P should point to just beyond the last valid character in the old buffer.
483 The value we return is a pointer to the new buffer
484 at a place corresponding to P. */
485
486 static char *
487 extend_token_buffer (p)
488 char *p;
489 {
490 int offset = p - token_buffer;
491
492 maxtoken = maxtoken * 2 + 10;
493 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
494
495 return token_buffer + offset;
496 }
497 \f
498 #if !USE_CPPLIB
499 #define GET_DIRECTIVE_LINE() get_directive_line (finput)
500 #else /* USE_CPPLIB */
501 /* Read the rest of a #-directive from input stream FINPUT.
502 In normal use, the directive name and the white space after it
503 have already been read, so they won't be included in the result.
504 We allow for the fact that the directive line may contain
505 a newline embedded within a character or string literal which forms
506 a part of the directive.
507
508 The value is a string in a reusable buffer. It remains valid
509 only until the next time this function is called. */
510
511 static char *
512 GET_DIRECTIVE_LINE ()
513 {
514 static char *directive_buffer = NULL;
515 static unsigned buffer_length = 0;
516 register char *p;
517 register char *buffer_limit;
518 register int looking_for = 0;
519 register int char_escaped = 0;
520
521 if (buffer_length == 0)
522 {
523 directive_buffer = (char *)xmalloc (128);
524 buffer_length = 128;
525 }
526
527 buffer_limit = &directive_buffer[buffer_length];
528
529 for (p = directive_buffer; ; )
530 {
531 int c;
532
533 /* Make buffer bigger if it is full. */
534 if (p >= buffer_limit)
535 {
536 register unsigned bytes_used = (p - directive_buffer);
537
538 buffer_length *= 2;
539 directive_buffer
540 = (char *)xrealloc (directive_buffer, buffer_length);
541 p = &directive_buffer[bytes_used];
542 buffer_limit = &directive_buffer[buffer_length];
543 }
544
545 c = GETC ();
546
547 /* Discard initial whitespace. */
548 if ((c == ' ' || c == '\t') && p == directive_buffer)
549 continue;
550
551 /* Detect the end of the directive. */
552 if (c == '\n' && looking_for == 0)
553 {
554 UNGETC (c);
555 c = '\0';
556 }
557
558 *p++ = c;
559
560 if (c == 0)
561 return directive_buffer;
562
563 /* Handle string and character constant syntax. */
564 if (looking_for)
565 {
566 if (looking_for == c && !char_escaped)
567 looking_for = 0; /* Found terminator... stop looking. */
568 }
569 else
570 if (c == '\'' || c == '"')
571 looking_for = c; /* Don't stop buffering until we see another
572 another one of these (or an EOF). */
573
574 /* Handle backslash. */
575 char_escaped = (c == '\\' && ! char_escaped);
576 }
577 }
578 #endif /* USE_CPPLIB */
579 \f
580 /* At the beginning of a line, increment the line number
581 and process any #-directive on this line.
582 If the line is a #-directive, read the entire line and return a newline.
583 Otherwise, return the line's first non-whitespace character. */
584
585 int
586 check_newline ()
587 {
588 register int c;
589 register int token;
590
591 lineno++;
592
593 /* Read first nonwhite char on the line. */
594
595 c = GETC();
596 while (c == ' ' || c == '\t')
597 c = GETC();
598
599 if (c != '#')
600 {
601 /* If not #, return it so caller will use it. */
602 return c;
603 }
604
605 /* Read first nonwhite char after the `#'. */
606
607 c = GETC();
608 while (c == ' ' || c == '\t')
609 c = GETC();
610
611 /* If a letter follows, then if the word here is `line', skip
612 it and ignore it; otherwise, ignore the line, with an error
613 if the word isn't `pragma', `ident', `define', or `undef'. */
614
615 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
616 {
617 if (c == 'p')
618 {
619 if (GETC() == 'r'
620 && GETC() == 'a'
621 && GETC() == 'g'
622 && GETC() == 'm'
623 && GETC() == 'a'
624 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'
625 || whitespace_cr (c) ))
626 {
627 while (c == ' ' || c == '\t' || whitespace_cr (c))
628 c = GETC ();
629 if (c == '\n')
630 return c;
631 #ifdef HANDLE_SYSV_PRAGMA
632 UNGETC (c);
633 token = yylex ();
634 if (token != IDENTIFIER)
635 goto skipline;
636 return handle_sysv_pragma (token);
637 #else /* !HANDLE_SYSV_PRAGMA */
638 #ifdef HANDLE_PRAGMA
639 #if !USE_CPPLIB
640 UNGETC (c);
641 token = yylex ();
642 if (token != IDENTIFIER)
643 goto skipline;
644 if (HANDLE_PRAGMA (finput, yylval.ttype))
645 {
646 c = GETC ();
647 return c;
648 }
649 #else
650 ??? do not know what to do ???;
651 #endif /* !USE_CPPLIB */
652 #endif /* HANDLE_PRAGMA */
653 #endif /* !HANDLE_SYSV_PRAGMA */
654 goto skipline;
655 }
656 }
657
658 else if (c == 'd')
659 {
660 if (GETC() == 'e'
661 && GETC() == 'f'
662 && GETC() == 'i'
663 && GETC() == 'n'
664 && GETC() == 'e'
665 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'))
666 {
667 if (c != '\n')
668 debug_define (lineno, GET_DIRECTIVE_LINE ());
669 goto skipline;
670 }
671 }
672 else if (c == 'u')
673 {
674 if (GETC() == 'n'
675 && GETC() == 'd'
676 && GETC() == 'e'
677 && GETC() == 'f'
678 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'))
679 {
680 if (c != '\n')
681 debug_undef (lineno, GET_DIRECTIVE_LINE ());
682 goto skipline;
683 }
684 }
685 else if (c == 'l')
686 {
687 if (GETC() == 'i'
688 && GETC() == 'n'
689 && GETC() == 'e'
690 && ((c = GETC()) == ' ' || c == '\t'))
691 goto linenum;
692 }
693 else if (c == 'i')
694 {
695 if (GETC() == 'd'
696 && GETC() == 'e'
697 && GETC() == 'n'
698 && GETC() == 't'
699 && ((c = GETC()) == ' ' || c == '\t'))
700 {
701 /* #ident. The pedantic warning is now in cccp.c. */
702
703 /* Here we have just seen `#ident '.
704 A string constant should follow. */
705
706 c = skip_white_space_on_line ();
707
708 /* If no argument, ignore the line. */
709 if (c == '\n')
710 return c;
711
712 UNGETC (c);
713 token = yylex ();
714 if (token != STRING
715 || TREE_CODE (yylval.ttype) != STRING_CST)
716 {
717 error ("invalid #ident");
718 goto skipline;
719 }
720
721 if (!flag_no_ident)
722 {
723 #ifdef ASM_OUTPUT_IDENT
724 ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (yylval.ttype));
725 #endif
726 }
727
728 /* Skip the rest of this line. */
729 goto skipline;
730 }
731 }
732
733 error ("undefined or invalid # directive");
734 goto skipline;
735 }
736
737 linenum:
738 /* Here we have either `#line' or `# <nonletter>'.
739 In either case, it should be a line number; a digit should follow. */
740
741 /* Can't use skip_white_space here, but must handle all whitespace
742 that is not '\n', lest we get a recursion for '\r' '\n' when
743 calling yylex. */
744 UNGETC (c);
745 c = skip_white_space_on_line ();
746
747 /* If the # is the only nonwhite char on the line,
748 just ignore it. Check the new newline. */
749 if (c == '\n')
750 return c;
751
752 /* Something follows the #; read a token. */
753
754 UNGETC (c);
755 token = yylex ();
756
757 if (token == CONSTANT
758 && TREE_CODE (yylval.ttype) == INTEGER_CST)
759 {
760 int old_lineno = lineno;
761 int used_up = 0;
762 /* subtract one, because it is the following line that
763 gets the specified number */
764
765 int l = TREE_INT_CST_LOW (yylval.ttype) - 1;
766
767 /* Is this the last nonwhite stuff on the line? */
768 c = skip_white_space_on_line ();
769 if (c == '\n')
770 {
771 /* No more: store the line number and check following line. */
772 lineno = l;
773 return c;
774 }
775 UNGETC (c);
776
777 /* More follows: it must be a string constant (filename). */
778
779 /* Read the string constant. */
780 token = yylex ();
781
782 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
783 {
784 error ("invalid #line");
785 goto skipline;
786 }
787
788 input_filename
789 = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
790 strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
791 lineno = l;
792
793 /* Each change of file name
794 reinitializes whether we are now in a system header. */
795 in_system_header = 0;
796
797 if (main_input_filename == 0)
798 main_input_filename = input_filename;
799
800 /* Is this the last nonwhite stuff on the line? */
801 c = skip_white_space_on_line ();
802 if (c == '\n')
803 {
804 /* Update the name in the top element of input_file_stack. */
805 if (input_file_stack)
806 input_file_stack->name = input_filename;
807
808 return c;
809 }
810 UNGETC (c);
811
812 token = yylex ();
813 used_up = 0;
814
815 /* `1' after file name means entering new file.
816 `2' after file name means just left a file. */
817
818 if (token == CONSTANT
819 && TREE_CODE (yylval.ttype) == INTEGER_CST)
820 {
821 if (TREE_INT_CST_LOW (yylval.ttype) == 1)
822 {
823 /* Pushing to a new file. */
824 struct file_stack *p
825 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
826 input_file_stack->line = old_lineno;
827 p->next = input_file_stack;
828 p->name = input_filename;
829 p->indent_level = indent_level;
830 input_file_stack = p;
831 input_file_stack_tick++;
832 debug_start_source_file (input_filename);
833 used_up = 1;
834 }
835 else if (TREE_INT_CST_LOW (yylval.ttype) == 2)
836 {
837 /* Popping out of a file. */
838 if (input_file_stack->next)
839 {
840 struct file_stack *p = input_file_stack;
841 if (indent_level != p->indent_level)
842 {
843 warning_with_file_and_line
844 (p->name, old_lineno,
845 "This file contains more `%c's than `%c's.",
846 indent_level > p->indent_level ? '{' : '}',
847 indent_level > p->indent_level ? '}' : '{');
848 }
849 input_file_stack = p->next;
850 free (p);
851 input_file_stack_tick++;
852 debug_end_source_file (input_file_stack->line);
853 }
854 else
855 error ("#-lines for entering and leaving files don't match");
856
857 used_up = 1;
858 }
859 }
860
861 /* Now that we've pushed or popped the input stack,
862 update the name in the top element. */
863 if (input_file_stack)
864 input_file_stack->name = input_filename;
865
866 /* If we have handled a `1' or a `2',
867 see if there is another number to read. */
868 if (used_up)
869 {
870 /* Is this the last nonwhite stuff on the line? */
871 c = skip_white_space_on_line ();
872 if (c == '\n')
873 return c;
874 UNGETC (c);
875
876 token = yylex ();
877 used_up = 0;
878 }
879
880 /* `3' after file name means this is a system header file. */
881
882 if (token == CONSTANT
883 && TREE_CODE (yylval.ttype) == INTEGER_CST
884 && TREE_INT_CST_LOW (yylval.ttype) == 3)
885 in_system_header = 1, used_up = 1;
886
887 if (used_up)
888 {
889 /* Is this the last nonwhite stuff on the line? */
890 c = skip_white_space_on_line ();
891 if (c == '\n')
892 return c;
893 UNGETC (c);
894 }
895
896 warning ("unrecognized text at end of #line");
897 }
898 else
899 error ("invalid #-line");
900
901 /* skip the rest of this line. */
902 skipline:
903 #if !USE_CPPLIB
904 if (c != '\n' && c != EOF && nextchar >= 0)
905 c = nextchar, nextchar = -1;
906 #endif
907 while (c != '\n' && c != EOF)
908 c = GETC();
909 return c;
910 }
911 \f
912 #ifdef HANDLE_SYSV_PRAGMA
913
914 /* Handle a #pragma directive.
915 TOKEN is the token we read after `#pragma'. Processes the entire input
916 line and returns a character for the caller to reread: either \n or EOF. */
917
918 /* This function has to be in this file, in order to get at
919 the token types. */
920
921 static int
922 handle_sysv_pragma (token)
923 register int token;
924 {
925 register int c;
926
927 for (;;)
928 {
929 switch (token)
930 {
931 case IDENTIFIER:
932 case TYPENAME:
933 case STRING:
934 case CONSTANT:
935 handle_pragma_token (token_buffer, yylval.ttype);
936 break;
937 default:
938 handle_pragma_token (token_buffer, 0);
939 }
940 #if !USE_CPPLIB
941 if (nextchar >= 0)
942 c = nextchar, nextchar = -1;
943 else
944 #endif
945 c = GETC ();
946
947 while (c == ' ' || c == '\t')
948 c = GETC ();
949 if (c == '\n' || c == EOF)
950 {
951 handle_pragma_token (0, 0);
952 return c;
953 }
954 UNGETC (c);
955 token = yylex ();
956 }
957 }
958
959 #endif /* HANDLE_SYSV_PRAGMA */
960 \f
961 #define ENDFILE -1 /* token that represents end-of-file */
962
963 /* Read an escape sequence, returning its equivalent as a character,
964 or store 1 in *ignore_ptr if it is backslash-newline. */
965
966 static int
967 readescape (ignore_ptr)
968 int *ignore_ptr;
969 {
970 register int c = GETC();
971 register int code;
972 register unsigned count;
973 unsigned firstdig = 0;
974 int nonnull;
975
976 switch (c)
977 {
978 case 'x':
979 if (warn_traditional)
980 warning ("the meaning of `\\x' varies with -traditional");
981
982 if (flag_traditional)
983 return c;
984
985 code = 0;
986 count = 0;
987 nonnull = 0;
988 while (1)
989 {
990 c = GETC();
991 if (!(c >= 'a' && c <= 'f')
992 && !(c >= 'A' && c <= 'F')
993 && !(c >= '0' && c <= '9'))
994 {
995 UNGETC (c);
996 break;
997 }
998 code *= 16;
999 if (c >= 'a' && c <= 'f')
1000 code += c - 'a' + 10;
1001 if (c >= 'A' && c <= 'F')
1002 code += c - 'A' + 10;
1003 if (c >= '0' && c <= '9')
1004 code += c - '0';
1005 if (code != 0 || count != 0)
1006 {
1007 if (count == 0)
1008 firstdig = code;
1009 count++;
1010 }
1011 nonnull = 1;
1012 }
1013 if (! nonnull)
1014 error ("\\x used with no following hex digits");
1015 else if (count == 0)
1016 /* Digits are all 0's. Ok. */
1017 ;
1018 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
1019 || (count > 1
1020 && ((1 << (TYPE_PRECISION (integer_type_node) - (count - 1) * 4))
1021 <= firstdig)))
1022 pedwarn ("hex escape out of range");
1023 return code;
1024
1025 case '0': case '1': case '2': case '3': case '4':
1026 case '5': case '6': case '7':
1027 code = 0;
1028 count = 0;
1029 while ((c <= '7') && (c >= '0') && (count++ < 3))
1030 {
1031 code = (code * 8) + (c - '0');
1032 c = GETC();
1033 }
1034 UNGETC (c);
1035 return code;
1036
1037 case '\\': case '\'': case '"':
1038 return c;
1039
1040 case '\n':
1041 lineno++;
1042 *ignore_ptr = 1;
1043 return 0;
1044
1045 case 'n':
1046 return TARGET_NEWLINE;
1047
1048 case 't':
1049 return TARGET_TAB;
1050
1051 case 'r':
1052 return TARGET_CR;
1053
1054 case 'f':
1055 return TARGET_FF;
1056
1057 case 'b':
1058 return TARGET_BS;
1059
1060 case 'a':
1061 if (warn_traditional)
1062 warning ("the meaning of `\\a' varies with -traditional");
1063
1064 if (flag_traditional)
1065 return c;
1066 return TARGET_BELL;
1067
1068 case 'v':
1069 #if 0 /* Vertical tab is present in common usage compilers. */
1070 if (flag_traditional)
1071 return c;
1072 #endif
1073 return TARGET_VT;
1074
1075 case 'e':
1076 case 'E':
1077 if (pedantic)
1078 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
1079 return 033;
1080
1081 case '?':
1082 return c;
1083
1084 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
1085 case '(':
1086 case '{':
1087 case '[':
1088 /* `\%' is used to prevent SCCS from getting confused. */
1089 case '%':
1090 if (pedantic)
1091 pedwarn ("non-ANSI escape sequence `\\%c'", c);
1092 return c;
1093 }
1094 if (c >= 040 && c < 0177)
1095 pedwarn ("unknown escape sequence `\\%c'", c);
1096 else
1097 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
1098 return c;
1099 }
1100 \f
1101 void
1102 yyerror (string)
1103 char *string;
1104 {
1105 char buf[200];
1106
1107 strcpy (buf, string);
1108
1109 /* We can't print string and character constants well
1110 because the token_buffer contains the result of processing escapes. */
1111 if (end_of_file)
1112 strcat (buf, " at end of input");
1113 else if (token_buffer[0] == 0)
1114 strcat (buf, " at null character");
1115 else if (token_buffer[0] == '"')
1116 strcat (buf, " before string constant");
1117 else if (token_buffer[0] == '\'')
1118 strcat (buf, " before character constant");
1119 else if (token_buffer[0] < 040 || (unsigned char) token_buffer[0] >= 0177)
1120 sprintf (buf + strlen (buf), " before character 0%o",
1121 (unsigned char) token_buffer[0]);
1122 else
1123 strcat (buf, " before `%s'");
1124
1125 error (buf, token_buffer);
1126 }
1127
1128 #if 0
1129
1130 struct try_type
1131 {
1132 tree *node_var;
1133 char unsigned_flag;
1134 char long_flag;
1135 char long_long_flag;
1136 };
1137
1138 struct try_type type_sequence[] =
1139 {
1140 { &integer_type_node, 0, 0, 0},
1141 { &unsigned_type_node, 1, 0, 0},
1142 { &long_integer_type_node, 0, 1, 0},
1143 { &long_unsigned_type_node, 1, 1, 0},
1144 { &long_long_integer_type_node, 0, 1, 1},
1145 { &long_long_unsigned_type_node, 1, 1, 1}
1146 };
1147 #endif /* 0 */
1148 \f
1149 int
1150 yylex ()
1151 {
1152 register int c;
1153 register char *p;
1154 register int value;
1155 int wide_flag = 0;
1156 int objc_flag = 0;
1157
1158 #if !USE_CPPLIB
1159 if (nextchar >= 0)
1160 c = nextchar, nextchar = -1;
1161 else
1162 #endif
1163 c = GETC();
1164
1165 /* Effectively do c = skip_white_space (c)
1166 but do it faster in the usual cases. */
1167 while (1)
1168 switch (c)
1169 {
1170 case ' ':
1171 case '\t':
1172 case '\f':
1173 case '\v':
1174 case '\b':
1175 c = GETC();
1176 break;
1177
1178 case '\r':
1179 /* Call skip_white_space so we can warn if appropriate. */
1180
1181 case '\n':
1182 case '/':
1183 case '\\':
1184 c = skip_white_space (c);
1185 default:
1186 goto found_nonwhite;
1187 }
1188 found_nonwhite:
1189
1190 token_buffer[0] = c;
1191 token_buffer[1] = 0;
1192
1193 /* yylloc.first_line = lineno; */
1194
1195 switch (c)
1196 {
1197 case EOF:
1198 end_of_file = 1;
1199 token_buffer[0] = 0;
1200 value = ENDFILE;
1201 break;
1202
1203 case 'L':
1204 /* Capital L may start a wide-string or wide-character constant. */
1205 {
1206 register int c = GETC();
1207 if (c == '\'')
1208 {
1209 wide_flag = 1;
1210 goto char_constant;
1211 }
1212 if (c == '"')
1213 {
1214 wide_flag = 1;
1215 goto string_constant;
1216 }
1217 UNGETC (c);
1218 }
1219 goto letter;
1220
1221 case '@':
1222 if (!doing_objc_thang)
1223 {
1224 value = c;
1225 break;
1226 }
1227 else
1228 {
1229 /* '@' may start a constant string object. */
1230 register int c = GETC ();
1231 if (c == '"')
1232 {
1233 objc_flag = 1;
1234 goto string_constant;
1235 }
1236 UNGETC (c);
1237 /* Fall through to treat '@' as the start of an identifier. */
1238 }
1239
1240 case 'A': case 'B': case 'C': case 'D': case 'E':
1241 case 'F': case 'G': case 'H': case 'I': case 'J':
1242 case 'K': case 'M': case 'N': case 'O':
1243 case 'P': case 'Q': case 'R': case 'S': case 'T':
1244 case 'U': case 'V': case 'W': case 'X': case 'Y':
1245 case 'Z':
1246 case 'a': case 'b': case 'c': case 'd': case 'e':
1247 case 'f': case 'g': case 'h': case 'i': case 'j':
1248 case 'k': case 'l': case 'm': case 'n': case 'o':
1249 case 'p': case 'q': case 'r': case 's': case 't':
1250 case 'u': case 'v': case 'w': case 'x': case 'y':
1251 case 'z':
1252 case '_':
1253 case '$':
1254 letter:
1255 p = token_buffer;
1256 while (isalnum (c) || c == '_' || c == '$' || c == '@')
1257 {
1258 /* Make sure this char really belongs in an identifier. */
1259 if (c == '@' && ! doing_objc_thang)
1260 break;
1261 if (c == '$')
1262 {
1263 if (! dollars_in_ident)
1264 error ("`$' in identifier");
1265 else if (pedantic)
1266 pedwarn ("`$' in identifier");
1267 }
1268
1269 if (p >= token_buffer + maxtoken)
1270 p = extend_token_buffer (p);
1271
1272 *p++ = c;
1273 c = GETC();
1274 }
1275
1276 *p = 0;
1277 #if USE_CPPLIB
1278 UNGETC (c);
1279 #else
1280 nextchar = c;
1281 #endif
1282
1283 value = IDENTIFIER;
1284 yylval.itype = 0;
1285
1286 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1287
1288 {
1289 register struct resword *ptr;
1290
1291 if ((ptr = is_reserved_word (token_buffer, p - token_buffer)))
1292 {
1293 if (ptr->rid)
1294 yylval.ttype = ridpointers[(int) ptr->rid];
1295 value = (int) ptr->token;
1296
1297 /* Only return OBJECTNAME if it is a typedef. */
1298 if (doing_objc_thang && value == OBJECTNAME)
1299 {
1300 lastiddecl = lookup_name(yylval.ttype);
1301
1302 if (lastiddecl == NULL_TREE
1303 || TREE_CODE (lastiddecl) != TYPE_DECL)
1304 value = IDENTIFIER;
1305 }
1306
1307 /* Even if we decided to recognize asm, still perhaps warn. */
1308 if (pedantic
1309 && (value == ASM_KEYWORD || value == TYPEOF
1310 || ptr->rid == RID_INLINE)
1311 && token_buffer[0] != '_')
1312 pedwarn ("ANSI does not permit the keyword `%s'",
1313 token_buffer);
1314 }
1315 }
1316
1317 /* If we did not find a keyword, look for an identifier
1318 (or a typename). */
1319
1320 if (value == IDENTIFIER)
1321 {
1322 if (token_buffer[0] == '@')
1323 error("invalid identifier `%s'", token_buffer);
1324
1325 yylval.ttype = get_identifier (token_buffer);
1326 lastiddecl = lookup_name (yylval.ttype);
1327
1328 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1329 value = TYPENAME;
1330 /* A user-invisible read-only initialized variable
1331 should be replaced by its value.
1332 We handle only strings since that's the only case used in C. */
1333 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1334 && DECL_IGNORED_P (lastiddecl)
1335 && TREE_READONLY (lastiddecl)
1336 && DECL_INITIAL (lastiddecl) != 0
1337 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1338 {
1339 tree stringval = DECL_INITIAL (lastiddecl);
1340
1341 /* Copy the string value so that we won't clobber anything
1342 if we put something in the TREE_CHAIN of this one. */
1343 yylval.ttype = build_string (TREE_STRING_LENGTH (stringval),
1344 TREE_STRING_POINTER (stringval));
1345 value = STRING;
1346 }
1347 else if (doing_objc_thang)
1348 {
1349 tree objc_interface_decl = is_class_name (yylval.ttype);
1350
1351 if (objc_interface_decl)
1352 {
1353 value = CLASSNAME;
1354 yylval.ttype = objc_interface_decl;
1355 }
1356 }
1357 }
1358
1359 break;
1360
1361 case '0': case '1':
1362 {
1363 int next_c;
1364 /* Check first for common special case: single-digit 0 or 1. */
1365
1366 next_c = GETC ();
1367 UNGETC (next_c); /* Always undo this lookahead. */
1368 if (!isalnum (next_c) && next_c != '.')
1369 {
1370 token_buffer[0] = (char)c, token_buffer[1] = '\0';
1371 yylval.ttype = (c == '0') ? integer_zero_node : integer_one_node;
1372 value = CONSTANT;
1373 break;
1374 }
1375 /*FALLTHRU*/
1376 }
1377 case '2': case '3': case '4':
1378 case '5': case '6': case '7': case '8': case '9':
1379 case '.':
1380 {
1381 int base = 10;
1382 int count = 0;
1383 int largest_digit = 0;
1384 int numdigits = 0;
1385 /* for multi-precision arithmetic,
1386 we actually store only HOST_BITS_PER_CHAR bits in each part.
1387 The number of parts is chosen so as to be sufficient to hold
1388 the enough bits to fit into the two HOST_WIDE_INTs that contain
1389 the integer value (this is always at least as many bits as are
1390 in a target `long long' value, but may be wider). */
1391 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2 + 2)
1392 int parts[TOTAL_PARTS];
1393 int overflow = 0;
1394
1395 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS} floatflag
1396 = NOT_FLOAT;
1397
1398 for (count = 0; count < TOTAL_PARTS; count++)
1399 parts[count] = 0;
1400
1401 p = token_buffer;
1402 *p++ = c;
1403
1404 if (c == '0')
1405 {
1406 *p++ = (c = GETC());
1407 if ((c == 'x') || (c == 'X'))
1408 {
1409 base = 16;
1410 *p++ = (c = GETC());
1411 }
1412 /* Leading 0 forces octal unless the 0 is the only digit. */
1413 else if (c >= '0' && c <= '9')
1414 {
1415 base = 8;
1416 numdigits++;
1417 }
1418 else
1419 numdigits++;
1420 }
1421
1422 /* Read all the digits-and-decimal-points. */
1423
1424 while (c == '.'
1425 || (isalnum (c) && c != 'l' && c != 'L'
1426 && c != 'u' && c != 'U'
1427 && c != 'i' && c != 'I' && c != 'j' && c != 'J'
1428 && (floatflag == NOT_FLOAT || ((c != 'f') && (c != 'F')))))
1429 {
1430 if (c == '.')
1431 {
1432 if (base == 16)
1433 error ("floating constant may not be in radix 16");
1434 if (floatflag == TOO_MANY_POINTS)
1435 /* We have already emitted an error. Don't need another. */
1436 ;
1437 else if (floatflag == AFTER_POINT)
1438 {
1439 error ("malformed floating constant");
1440 floatflag = TOO_MANY_POINTS;
1441 /* Avoid another error from atof by forcing all characters
1442 from here on to be ignored. */
1443 p[-1] = '\0';
1444 }
1445 else
1446 floatflag = AFTER_POINT;
1447
1448 base = 10;
1449 *p++ = c = GETC();
1450 /* Accept '.' as the start of a floating-point number
1451 only when it is followed by a digit.
1452 Otherwise, unread the following non-digit
1453 and use the '.' as a structural token. */
1454 if (p == token_buffer + 2 && !isdigit (c))
1455 {
1456 if (c == '.')
1457 {
1458 c = GETC();
1459 if (c == '.')
1460 {
1461 *p++ = c;
1462 *p = 0;
1463 return ELLIPSIS;
1464 }
1465 error ("parse error at `..'");
1466 }
1467 UNGETC (c);
1468 token_buffer[1] = 0;
1469 value = '.';
1470 goto done;
1471 }
1472 }
1473 else
1474 {
1475 /* It is not a decimal point.
1476 It should be a digit (perhaps a hex digit). */
1477
1478 if (isdigit (c))
1479 {
1480 c = c - '0';
1481 }
1482 else if (base <= 10)
1483 {
1484 if (c == 'e' || c == 'E')
1485 {
1486 base = 10;
1487 floatflag = AFTER_POINT;
1488 break; /* start of exponent */
1489 }
1490 error ("nondigits in number and not hexadecimal");
1491 c = 0;
1492 }
1493 else if (c >= 'a')
1494 {
1495 c = c - 'a' + 10;
1496 }
1497 else
1498 {
1499 c = c - 'A' + 10;
1500 }
1501 if (c >= largest_digit)
1502 largest_digit = c;
1503 numdigits++;
1504
1505 for (count = 0; count < TOTAL_PARTS; count++)
1506 {
1507 parts[count] *= base;
1508 if (count)
1509 {
1510 parts[count]
1511 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1512 parts[count-1]
1513 &= (1 << HOST_BITS_PER_CHAR) - 1;
1514 }
1515 else
1516 parts[0] += c;
1517 }
1518
1519 /* If the extra highest-order part ever gets anything in it,
1520 the number is certainly too big. */
1521 if (parts[TOTAL_PARTS - 1] != 0)
1522 overflow = 1;
1523
1524 if (p >= token_buffer + maxtoken - 3)
1525 p = extend_token_buffer (p);
1526 *p++ = (c = GETC());
1527 }
1528 }
1529
1530 if (numdigits == 0)
1531 error ("numeric constant with no digits");
1532
1533 if (largest_digit >= base)
1534 error ("numeric constant contains digits beyond the radix");
1535
1536 /* Remove terminating char from the token buffer and delimit the string */
1537 *--p = 0;
1538
1539 if (floatflag != NOT_FLOAT)
1540 {
1541 tree type = double_type_node;
1542 int imag = 0;
1543 int conversion_errno = 0;
1544 REAL_VALUE_TYPE value;
1545 jmp_buf handler;
1546
1547 /* Read explicit exponent if any, and put it in tokenbuf. */
1548
1549 if ((c == 'e') || (c == 'E'))
1550 {
1551 if (p >= token_buffer + maxtoken - 3)
1552 p = extend_token_buffer (p);
1553 *p++ = c;
1554 c = GETC();
1555 if ((c == '+') || (c == '-'))
1556 {
1557 *p++ = c;
1558 c = GETC();
1559 }
1560 if (! isdigit (c))
1561 error ("floating constant exponent has no digits");
1562 while (isdigit (c))
1563 {
1564 if (p >= token_buffer + maxtoken - 3)
1565 p = extend_token_buffer (p);
1566 *p++ = c;
1567 c = GETC();
1568 }
1569 }
1570
1571 *p = 0;
1572
1573 /* Convert string to a double, checking for overflow. */
1574 if (setjmp (handler))
1575 {
1576 error ("floating constant out of range");
1577 value = dconst0;
1578 }
1579 else
1580 {
1581 int fflag = 0, lflag = 0;
1582 /* Copy token_buffer now, while it has just the number
1583 and not the suffixes; once we add `f' or `i',
1584 REAL_VALUE_ATOF may not work any more. */
1585 char *copy = (char *) alloca (p - token_buffer + 1);
1586 bcopy (token_buffer, copy, p - token_buffer + 1);
1587
1588 set_float_handler (handler);
1589
1590 while (1)
1591 {
1592 int lose = 0;
1593
1594 /* Read the suffixes to choose a data type. */
1595 switch (c)
1596 {
1597 case 'f': case 'F':
1598 if (fflag)
1599 error ("more than one `f' in numeric constant");
1600 fflag = 1;
1601 break;
1602
1603 case 'l': case 'L':
1604 if (lflag)
1605 error ("more than one `l' in numeric constant");
1606 lflag = 1;
1607 break;
1608
1609 case 'i': case 'I':
1610 if (imag)
1611 error ("more than one `i' or `j' in numeric constant");
1612 else if (pedantic)
1613 pedwarn ("ANSI C forbids imaginary numeric constants");
1614 imag = 1;
1615 break;
1616
1617 default:
1618 lose = 1;
1619 }
1620
1621 if (lose)
1622 break;
1623
1624 if (p >= token_buffer + maxtoken - 3)
1625 p = extend_token_buffer (p);
1626 *p++ = c;
1627 *p = 0;
1628 c = GETC();
1629 }
1630
1631 /* The second argument, machine_mode, of REAL_VALUE_ATOF
1632 tells the desired precision of the binary result
1633 of decimal-to-binary conversion. */
1634
1635 if (fflag)
1636 {
1637 if (lflag)
1638 error ("both `f' and `l' in floating constant");
1639
1640 type = float_type_node;
1641 errno = 0;
1642 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1643 conversion_errno = errno;
1644 /* A diagnostic is required here by some ANSI C testsuites.
1645 This is not pedwarn, become some people don't want
1646 an error for this. */
1647 if (REAL_VALUE_ISINF (value) && pedantic)
1648 warning ("floating point number exceeds range of `float'");
1649 }
1650 else if (lflag)
1651 {
1652 type = long_double_type_node;
1653 errno = 0;
1654 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1655 conversion_errno = errno;
1656 if (REAL_VALUE_ISINF (value) && pedantic)
1657 warning ("floating point number exceeds range of `long double'");
1658 }
1659 else
1660 {
1661 errno = 0;
1662 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1663 conversion_errno = errno;
1664 if (REAL_VALUE_ISINF (value) && pedantic)
1665 warning ("floating point number exceeds range of `double'");
1666 }
1667
1668 set_float_handler (NULL_PTR);
1669 }
1670 #ifdef ERANGE
1671 /* ERANGE is also reported for underflow,
1672 so test the value to distinguish overflow from that. */
1673 if (conversion_errno == ERANGE && !flag_traditional && pedantic
1674 && (REAL_VALUES_LESS (dconst1, value)
1675 || REAL_VALUES_LESS (value, dconstm1)))
1676 warning ("floating point number exceeds range of `double'");
1677 #endif
1678
1679 /* If the result is not a number, assume it must have been
1680 due to some error message above, so silently convert
1681 it to a zero. */
1682 if (REAL_VALUE_ISNAN (value))
1683 value = dconst0;
1684
1685 /* Create a node with determined type and value. */
1686 if (imag)
1687 yylval.ttype = build_complex (NULL_TREE,
1688 convert (type, integer_zero_node),
1689 build_real (type, value));
1690 else
1691 yylval.ttype = build_real (type, value);
1692 }
1693 else
1694 {
1695 tree traditional_type, ansi_type, type;
1696 HOST_WIDE_INT high, low;
1697 int spec_unsigned = 0;
1698 int spec_long = 0;
1699 int spec_long_long = 0;
1700 int spec_imag = 0;
1701 int bytes, warn, i;
1702
1703 traditional_type = ansi_type = type = NULL_TREE;
1704 while (1)
1705 {
1706 if (c == 'u' || c == 'U')
1707 {
1708 if (spec_unsigned)
1709 error ("two `u's in integer constant");
1710 spec_unsigned = 1;
1711 }
1712 else if (c == 'l' || c == 'L')
1713 {
1714 if (spec_long)
1715 {
1716 if (spec_long_long)
1717 error ("three `l's in integer constant");
1718 else if (pedantic)
1719 pedwarn ("ANSI C forbids long long integer constants");
1720 spec_long_long = 1;
1721 }
1722 spec_long = 1;
1723 }
1724 else if (c == 'i' || c == 'j' || c == 'I' || c == 'J')
1725 {
1726 if (spec_imag)
1727 error ("more than one `i' or `j' in numeric constant");
1728 else if (pedantic)
1729 pedwarn ("ANSI C forbids imaginary numeric constants");
1730 spec_imag = 1;
1731 }
1732 else
1733 break;
1734 if (p >= token_buffer + maxtoken - 3)
1735 p = extend_token_buffer (p);
1736 *p++ = c;
1737 c = GETC();
1738 }
1739
1740 /* If the constant won't fit in an unsigned long long,
1741 then warn that the constant is out of range. */
1742
1743 /* ??? This assumes that long long and long integer types are
1744 a multiple of 8 bits. This better than the original code
1745 though which assumed that long was exactly 32 bits and long
1746 long was exactly 64 bits. */
1747
1748 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1749
1750 warn = overflow;
1751 for (i = bytes; i < TOTAL_PARTS; i++)
1752 if (parts[i])
1753 warn = 1;
1754 if (warn)
1755 pedwarn ("integer constant out of range");
1756
1757 /* This is simplified by the fact that our constant
1758 is always positive. */
1759
1760 high = low = 0;
1761
1762 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1763 {
1764 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1765 / HOST_BITS_PER_CHAR)]
1766 << (i * HOST_BITS_PER_CHAR));
1767 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1768 }
1769
1770 yylval.ttype = build_int_2 (low, high);
1771 TREE_TYPE (yylval.ttype) = long_long_unsigned_type_node;
1772
1773 /* If warn_traditional, calculate both the ANSI type and the
1774 traditional type, then see if they disagree.
1775 Otherwise, calculate only the type for the dialect in use. */
1776 if (warn_traditional || flag_traditional)
1777 {
1778 /* Calculate the traditional type. */
1779 /* Traditionally, any constant is signed;
1780 but if unsigned is specified explicitly, obey that.
1781 Use the smallest size with the right number of bits,
1782 except for one special case with decimal constants. */
1783 if (! spec_long && base != 10
1784 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1785 traditional_type = (spec_unsigned ? unsigned_type_node
1786 : integer_type_node);
1787 /* A decimal constant must be long
1788 if it does not fit in type int.
1789 I think this is independent of whether
1790 the constant is signed. */
1791 else if (! spec_long && base == 10
1792 && int_fits_type_p (yylval.ttype, integer_type_node))
1793 traditional_type = (spec_unsigned ? unsigned_type_node
1794 : integer_type_node);
1795 else if (! spec_long_long)
1796 traditional_type = (spec_unsigned ? long_unsigned_type_node
1797 : long_integer_type_node);
1798 else
1799 traditional_type = (spec_unsigned
1800 ? long_long_unsigned_type_node
1801 : long_long_integer_type_node);
1802 }
1803 if (warn_traditional || ! flag_traditional)
1804 {
1805 /* Calculate the ANSI type. */
1806 if (! spec_long && ! spec_unsigned
1807 && int_fits_type_p (yylval.ttype, integer_type_node))
1808 ansi_type = integer_type_node;
1809 else if (! spec_long && (base != 10 || spec_unsigned)
1810 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1811 ansi_type = unsigned_type_node;
1812 else if (! spec_unsigned && !spec_long_long
1813 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1814 ansi_type = long_integer_type_node;
1815 else if (! spec_long_long
1816 && int_fits_type_p (yylval.ttype,
1817 long_unsigned_type_node))
1818 ansi_type = long_unsigned_type_node;
1819 else if (! spec_unsigned
1820 && int_fits_type_p (yylval.ttype,
1821 long_long_integer_type_node))
1822 ansi_type = long_long_integer_type_node;
1823 else
1824 ansi_type = long_long_unsigned_type_node;
1825 }
1826
1827 type = flag_traditional ? traditional_type : ansi_type;
1828
1829 if (warn_traditional && traditional_type != ansi_type)
1830 {
1831 if (TYPE_PRECISION (traditional_type)
1832 != TYPE_PRECISION (ansi_type))
1833 warning ("width of integer constant changes with -traditional");
1834 else if (TREE_UNSIGNED (traditional_type)
1835 != TREE_UNSIGNED (ansi_type))
1836 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
1837 else
1838 warning ("width of integer constant may change on other systems with -traditional");
1839 }
1840
1841 if (pedantic && !flag_traditional && !spec_long_long && !warn
1842 && (TYPE_PRECISION (long_integer_type_node)
1843 < TYPE_PRECISION (type)))
1844 pedwarn ("integer constant out of range");
1845
1846 if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type))
1847 warning ("decimal constant is so large that it is unsigned");
1848
1849 if (spec_imag)
1850 {
1851 if (TYPE_PRECISION (type)
1852 <= TYPE_PRECISION (integer_type_node))
1853 yylval.ttype
1854 = build_complex (NULL_TREE, integer_zero_node,
1855 convert (integer_type_node,
1856 yylval.ttype));
1857 else
1858 error ("complex integer constant is too wide for `complex int'");
1859 }
1860 else if (flag_traditional && !int_fits_type_p (yylval.ttype, type))
1861 /* The traditional constant 0x80000000 is signed
1862 but doesn't fit in the range of int.
1863 This will change it to -0x80000000, which does fit. */
1864 {
1865 TREE_TYPE (yylval.ttype) = unsigned_type (type);
1866 yylval.ttype = convert (type, yylval.ttype);
1867 TREE_OVERFLOW (yylval.ttype)
1868 = TREE_CONSTANT_OVERFLOW (yylval.ttype) = 0;
1869 }
1870 else
1871 TREE_TYPE (yylval.ttype) = type;
1872 }
1873
1874 UNGETC (c);
1875 *p = 0;
1876
1877 if (isalnum (c) || c == '.' || c == '_' || c == '$'
1878 || (!flag_traditional && (c == '-' || c == '+')
1879 && (p[-1] == 'e' || p[-1] == 'E')))
1880 error ("missing white space after number `%s'", token_buffer);
1881
1882 value = CONSTANT; break;
1883 }
1884
1885 case '\'':
1886 char_constant:
1887 {
1888 register int result = 0;
1889 register int num_chars = 0;
1890 unsigned width = TYPE_PRECISION (char_type_node);
1891 int max_chars;
1892
1893 if (wide_flag)
1894 {
1895 width = WCHAR_TYPE_SIZE;
1896 #ifdef MULTIBYTE_CHARS
1897 max_chars = MB_CUR_MAX;
1898 #else
1899 max_chars = 1;
1900 #endif
1901 }
1902 else
1903 max_chars = TYPE_PRECISION (integer_type_node) / width;
1904
1905 while (1)
1906 {
1907 tryagain:
1908
1909 c = GETC();
1910
1911 if (c == '\'' || c == EOF)
1912 break;
1913
1914 if (c == '\\')
1915 {
1916 int ignore = 0;
1917 c = readescape (&ignore);
1918 if (ignore)
1919 goto tryagain;
1920 if (width < HOST_BITS_PER_INT
1921 && (unsigned) c >= (1 << width))
1922 pedwarn ("escape sequence out of range for character");
1923 #ifdef MAP_CHARACTER
1924 if (isprint (c))
1925 c = MAP_CHARACTER (c);
1926 #endif
1927 }
1928 else if (c == '\n')
1929 {
1930 if (pedantic)
1931 pedwarn ("ANSI C forbids newline in character constant");
1932 lineno++;
1933 }
1934 #ifdef MAP_CHARACTER
1935 else
1936 c = MAP_CHARACTER (c);
1937 #endif
1938
1939 num_chars++;
1940 if (num_chars > maxtoken - 4)
1941 extend_token_buffer (token_buffer);
1942
1943 token_buffer[num_chars] = c;
1944
1945 /* Merge character into result; ignore excess chars. */
1946 if (num_chars < max_chars + 1)
1947 {
1948 if (width < HOST_BITS_PER_INT)
1949 result = (result << width) | (c & ((1 << width) - 1));
1950 else
1951 result = c;
1952 }
1953 }
1954
1955 token_buffer[num_chars + 1] = '\'';
1956 token_buffer[num_chars + 2] = 0;
1957
1958 if (c != '\'')
1959 error ("malformatted character constant");
1960 else if (num_chars == 0)
1961 error ("empty character constant");
1962 else if (num_chars > max_chars)
1963 {
1964 num_chars = max_chars;
1965 error ("character constant too long");
1966 }
1967 else if (num_chars != 1 && ! flag_traditional)
1968 warning ("multi-character character constant");
1969
1970 /* If char type is signed, sign-extend the constant. */
1971 if (! wide_flag)
1972 {
1973 int num_bits = num_chars * width;
1974 if (num_bits == 0)
1975 /* We already got an error; avoid invalid shift. */
1976 yylval.ttype = build_int_2 (0, 0);
1977 else if (TREE_UNSIGNED (char_type_node)
1978 || ((result >> (num_bits - 1)) & 1) == 0)
1979 yylval.ttype
1980 = build_int_2 (result & (~(unsigned HOST_WIDE_INT) 0
1981 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1982 0);
1983 else
1984 yylval.ttype
1985 = build_int_2 (result | ~(~(unsigned HOST_WIDE_INT) 0
1986 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1987 -1);
1988 TREE_TYPE (yylval.ttype) = integer_type_node;
1989 }
1990 else
1991 {
1992 #ifdef MULTIBYTE_CHARS
1993 /* Set the initial shift state and convert the next sequence. */
1994 result = 0;
1995 /* In all locales L'\0' is zero and mbtowc will return zero,
1996 so don't use it. */
1997 if (num_chars > 1
1998 || (num_chars == 1 && token_buffer[1] != '\0'))
1999 {
2000 wchar_t wc;
2001 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
2002 if (mbtowc (& wc, token_buffer + 1, num_chars) == num_chars)
2003 result = wc;
2004 else
2005 warning ("Ignoring invalid multibyte character");
2006 }
2007 #endif
2008 yylval.ttype = build_int_2 (result, 0);
2009 TREE_TYPE (yylval.ttype) = wchar_type_node;
2010 }
2011
2012 value = CONSTANT;
2013 break;
2014 }
2015
2016 case '"':
2017 string_constant:
2018 {
2019 c = GETC();
2020 p = token_buffer + 1;
2021
2022 while (c != '"' && c >= 0)
2023 {
2024 if (c == '\\')
2025 {
2026 int ignore = 0;
2027 c = readescape (&ignore);
2028 if (ignore)
2029 goto skipnewline;
2030 if (!wide_flag
2031 && TYPE_PRECISION (char_type_node) < HOST_BITS_PER_INT
2032 && c >= (1 << TYPE_PRECISION (char_type_node)))
2033 pedwarn ("escape sequence out of range for character");
2034 }
2035 else if (c == '\n')
2036 {
2037 if (pedantic)
2038 pedwarn ("ANSI C forbids newline in string constant");
2039 lineno++;
2040 }
2041
2042 if (p == token_buffer + maxtoken)
2043 p = extend_token_buffer (p);
2044 *p++ = c;
2045
2046 skipnewline:
2047 c = GETC();
2048 }
2049 *p = 0;
2050
2051 if (c < 0)
2052 error ("Unterminated string constant");
2053
2054 /* We have read the entire constant.
2055 Construct a STRING_CST for the result. */
2056
2057 if (wide_flag)
2058 {
2059 /* If this is a L"..." wide-string, convert the multibyte string
2060 to a wide character string. */
2061 char *widep = (char *) alloca ((p - token_buffer) * WCHAR_BYTES);
2062 int len;
2063
2064 #ifdef MULTIBYTE_CHARS
2065 len = mbstowcs ((wchar_t *) widep, token_buffer + 1, p - token_buffer);
2066 if (len < 0 || len >= (p - token_buffer))
2067 {
2068 warning ("Ignoring invalid multibyte string");
2069 len = 0;
2070 }
2071 bzero (widep + (len * WCHAR_BYTES), WCHAR_BYTES);
2072 #else
2073 {
2074 char *wp, *cp;
2075
2076 wp = widep + (BYTES_BIG_ENDIAN ? WCHAR_BYTES - 1 : 0);
2077 bzero (widep, (p - token_buffer) * WCHAR_BYTES);
2078 for (cp = token_buffer + 1; cp < p; cp++)
2079 *wp = *cp, wp += WCHAR_BYTES;
2080 len = p - token_buffer - 1;
2081 }
2082 #endif
2083 yylval.ttype = build_string ((len + 1) * WCHAR_BYTES, widep);
2084 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
2085 value = STRING;
2086 }
2087 else if (objc_flag)
2088 {
2089 extern tree build_objc_string();
2090 /* Return an Objective-C @"..." constant string object. */
2091 yylval.ttype = build_objc_string (p - token_buffer,
2092 token_buffer + 1);
2093 TREE_TYPE (yylval.ttype) = char_array_type_node;
2094 value = OBJC_STRING;
2095 }
2096 else
2097 {
2098 yylval.ttype = build_string (p - token_buffer, token_buffer + 1);
2099 TREE_TYPE (yylval.ttype) = char_array_type_node;
2100 value = STRING;
2101 }
2102
2103 *p++ = '"';
2104 *p = 0;
2105
2106 break;
2107 }
2108
2109 case '+':
2110 case '-':
2111 case '&':
2112 case '|':
2113 case ':':
2114 case '<':
2115 case '>':
2116 case '*':
2117 case '/':
2118 case '%':
2119 case '^':
2120 case '!':
2121 case '=':
2122 {
2123 register int c1;
2124
2125 combine:
2126
2127 switch (c)
2128 {
2129 case '+':
2130 yylval.code = PLUS_EXPR; break;
2131 case '-':
2132 yylval.code = MINUS_EXPR; break;
2133 case '&':
2134 yylval.code = BIT_AND_EXPR; break;
2135 case '|':
2136 yylval.code = BIT_IOR_EXPR; break;
2137 case '*':
2138 yylval.code = MULT_EXPR; break;
2139 case '/':
2140 yylval.code = TRUNC_DIV_EXPR; break;
2141 case '%':
2142 yylval.code = TRUNC_MOD_EXPR; break;
2143 case '^':
2144 yylval.code = BIT_XOR_EXPR; break;
2145 case LSHIFT:
2146 yylval.code = LSHIFT_EXPR; break;
2147 case RSHIFT:
2148 yylval.code = RSHIFT_EXPR; break;
2149 case '<':
2150 yylval.code = LT_EXPR; break;
2151 case '>':
2152 yylval.code = GT_EXPR; break;
2153 }
2154
2155 token_buffer[1] = c1 = GETC();
2156 token_buffer[2] = 0;
2157
2158 if (c1 == '=')
2159 {
2160 switch (c)
2161 {
2162 case '<':
2163 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
2164 case '>':
2165 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
2166 case '!':
2167 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
2168 case '=':
2169 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
2170 }
2171 value = ASSIGN; goto done;
2172 }
2173 else if (c == c1)
2174 switch (c)
2175 {
2176 case '+':
2177 value = PLUSPLUS; goto done;
2178 case '-':
2179 value = MINUSMINUS; goto done;
2180 case '&':
2181 value = ANDAND; goto done;
2182 case '|':
2183 value = OROR; goto done;
2184 case '<':
2185 c = LSHIFT;
2186 goto combine;
2187 case '>':
2188 c = RSHIFT;
2189 goto combine;
2190 }
2191 else
2192 switch (c)
2193 {
2194 case '-':
2195 if (c1 == '>')
2196 { value = POINTSAT; goto done; }
2197 break;
2198 case ':':
2199 if (c1 == '>')
2200 { value = ']'; goto done; }
2201 break;
2202 case '<':
2203 if (c1 == '%')
2204 { value = '{'; indent_level++; goto done; }
2205 if (c1 == ':')
2206 { value = '['; goto done; }
2207 break;
2208 case '%':
2209 if (c1 == '>')
2210 { value = '}'; indent_level--; goto done; }
2211 break;
2212 }
2213 UNGETC (c1);
2214 token_buffer[1] = 0;
2215
2216 if ((c == '<') || (c == '>'))
2217 value = ARITHCOMPARE;
2218 else value = c;
2219 goto done;
2220 }
2221
2222 case 0:
2223 /* Don't make yyparse think this is eof. */
2224 value = 1;
2225 break;
2226
2227 case '{':
2228 indent_level++;
2229 value = c;
2230 break;
2231
2232 case '}':
2233 indent_level--;
2234 value = c;
2235 break;
2236
2237 default:
2238 value = c;
2239 }
2240
2241 done:
2242 /* yylloc.last_line = lineno; */
2243
2244 return value;
2245 }
2246
2247 /* Sets the value of the 'yydebug' variable to VALUE.
2248 This is a function so we don't have to have YYDEBUG defined
2249 in order to build the compiler. */
2250
2251 void
2252 set_yydebug (value)
2253 int value;
2254 {
2255 #if YYDEBUG != 0
2256 yydebug = value;
2257 #else
2258 warning ("YYDEBUG not defined.");
2259 #endif
2260 }
This page took 0.169449 seconds and 6 git commands to generate.