1 /* Lexical analyzer for C and Objective C.
2 Copyright (C) 1987, 88, 89, 92, 94, 1995 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)
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
36 #ifdef MULTIBYTE_CHARS
45 /* The elements of `ridpointers' are identifier nodes
46 for the reserved type names and storage classes.
47 It is indexed by a RID_... value. */
48 tree ridpointers[(int) RID_MAX];
50 /* Cause the `yydebug' variable to be defined. */
53 /* the declaration found for the last IDENTIFIER token read in.
54 yylex must look this up to detect typedefs, which get token type TYPENAME,
55 so it is left around in case the identifier is not a typedef but is
56 used in a context which makes it a reference to a variable. */
59 /* Nonzero enables objc features. */
63 extern tree is_class_name ();
67 /* File used for outputting assembler code. */
68 extern FILE *asm_out_file;
70 #ifndef WCHAR_TYPE_SIZE
72 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
74 #define WCHAR_TYPE_SIZE BITS_PER_WORD
78 /* Number of bytes in a wide character. */
79 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
81 static int maxtoken; /* Current nominal length of token buffer. */
82 char *token_buffer; /* Pointer to token buffer.
83 Actual allocated length is maxtoken + 2.
84 This is not static because objc-parse.y uses it. */
86 /* Nonzero if end-of-file has been seen on input. */
87 static int end_of_file;
89 /* Buffered-back input character; faster than using ungetc. */
90 static int nextchar = -1;
94 /* Do not insert generated code into the source, instead, include it.
95 This allows us to build gcc automatically even for targets that
96 need to add or modify the reserved keyword lists. */
99 /* Return something to represent absolute declarators containing a *.
100 TARGET is the absolute declarator that the * contains.
101 TYPE_QUALS is a list of modifiers such as const or volatile
102 to apply to the pointer type, represented as identifiers.
104 We return an INDIRECT_REF whose "contents" are TARGET
105 and whose type is the modifier list. */
108 make_pointer_declarator (type_quals, target)
109 tree type_quals, target;
111 return build1 (INDIRECT_REF, type_quals, target);
115 forget_protocol_qualifiers ()
117 int i, n = sizeof wordlist / sizeof (struct resword);
119 for (i = 0; i < n; i++)
120 if ((int) wordlist[i].rid >= (int) RID_IN
121 && (int) wordlist[i].rid <= (int) RID_ONEWAY)
122 wordlist[i].name = "";
126 remember_protocol_qualifiers ()
128 int i, n = sizeof wordlist / sizeof (struct resword);
130 for (i = 0; i < n; i++)
131 if (wordlist[i].rid == RID_IN)
132 wordlist[i].name = "in";
133 else if (wordlist[i].rid == RID_OUT)
134 wordlist[i].name = "out";
135 else if (wordlist[i].rid == RID_INOUT)
136 wordlist[i].name = "inout";
137 else if (wordlist[i].rid == RID_BYCOPY)
138 wordlist[i].name = "bycopy";
139 else if (wordlist[i].rid == RID_ONEWAY)
140 wordlist[i].name = "oneway";
146 /* Make identifier nodes long enough for the language-specific slots. */
147 set_identifier_size (sizeof (struct lang_identifier));
149 /* Start it at 0, because check_newline is called at the very beginning
150 and will increment it to 1. */
153 #ifdef MULTIBYTE_CHARS
154 /* Change to the native locale for multibyte conversions. */
155 setlocale (LC_CTYPE, "");
159 token_buffer = (char *) xmalloc (maxtoken + 2);
161 ridpointers[(int) RID_INT] = get_identifier ("int");
162 ridpointers[(int) RID_CHAR] = get_identifier ("char");
163 ridpointers[(int) RID_VOID] = get_identifier ("void");
164 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
165 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
166 ridpointers[(int) RID_SHORT] = get_identifier ("short");
167 ridpointers[(int) RID_LONG] = get_identifier ("long");
168 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
169 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
170 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
171 ridpointers[(int) RID_CONST] = get_identifier ("const");
172 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
173 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
174 ridpointers[(int) RID_STATIC] = get_identifier ("static");
175 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
176 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
177 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
178 ridpointers[(int) RID_ITERATOR] = get_identifier ("iterator");
179 ridpointers[(int) RID_COMPLEX] = get_identifier ("complex");
180 ridpointers[(int) RID_ID] = get_identifier ("id");
181 ridpointers[(int) RID_IN] = get_identifier ("in");
182 ridpointers[(int) RID_OUT] = get_identifier ("out");
183 ridpointers[(int) RID_INOUT] = get_identifier ("inout");
184 ridpointers[(int) RID_BYCOPY] = get_identifier ("bycopy");
185 ridpointers[(int) RID_ONEWAY] = get_identifier ("oneway");
186 forget_protocol_qualifiers();
188 /* Some options inhibit certain reserved words.
189 Clear those words out of the hash table so they won't be recognized. */
190 #define UNSET_RESERVED_WORD(STRING) \
191 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
192 if (s) s->name = ""; } while (0)
194 if (! doing_objc_thang)
195 UNSET_RESERVED_WORD ("id");
197 if (flag_traditional)
199 UNSET_RESERVED_WORD ("const");
200 UNSET_RESERVED_WORD ("volatile");
201 UNSET_RESERVED_WORD ("typeof");
202 UNSET_RESERVED_WORD ("signed");
203 UNSET_RESERVED_WORD ("inline");
204 UNSET_RESERVED_WORD ("iterator");
205 UNSET_RESERVED_WORD ("complex");
209 UNSET_RESERVED_WORD ("asm");
210 UNSET_RESERVED_WORD ("typeof");
211 UNSET_RESERVED_WORD ("inline");
212 UNSET_RESERVED_WORD ("iterator");
213 UNSET_RESERVED_WORD ("complex");
218 reinit_parse_for_function ()
222 /* Function used when yydebug is set, to print a token in more detail. */
225 yyprint (file, yychar, yylval)
237 if (IDENTIFIER_POINTER (t))
238 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
243 if (TREE_CODE (t) == INTEGER_CST)
245 #if HOST_BITS_PER_WIDE_INT == 64
246 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
252 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
258 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
264 /* If C is not whitespace, return C.
265 Otherwise skip whitespace and return first nonwhite char read. */
271 static int newline_warning = 0;
277 /* We don't recognize comments here, because
278 cpp output can include / and * consecutively as operators.
279 Also, there's no need, since cpp removes all comments. */
282 c = check_newline ();
294 /* ANSI C says the effects of a carriage return in a source file
296 if (pedantic && !newline_warning)
298 warning ("carriage return in source file");
299 warning ("(we only warn about the first carriage return)");
310 error ("stray '\\' in program");
320 /* Skips all of the white space at the current location in the input file.
321 Must use and reset nextchar if it has the next character. */
324 position_after_white_space ()
329 c = nextchar, nextchar = -1;
333 ungetc (skip_white_space (c), finput);
336 /* Make the token buffer longer, preserving the data in it.
337 P should point to just beyond the last valid character in the old buffer.
338 The value we return is a pointer to the new buffer
339 at a place corresponding to P. */
342 extend_token_buffer (p)
345 int offset = p - token_buffer;
347 maxtoken = maxtoken * 2 + 10;
348 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
350 return token_buffer + offset;
353 /* At the beginning of a line, increment the line number
354 and process any #-directive on this line.
355 If the line is a #-directive, read the entire line and return a newline.
356 Otherwise, return the line's first non-whitespace character. */
366 /* Read first nonwhite char on the line. */
369 while (c == ' ' || c == '\t')
374 /* If not #, return it so caller will use it. */
378 /* Read first nonwhite char after the `#'. */
381 while (c == ' ' || c == '\t')
384 /* If a letter follows, then if the word here is `line', skip
385 it and ignore it; otherwise, ignore the line, with an error
386 if the word isn't `pragma', `ident', `define', or `undef'. */
388 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
392 if (getc (finput) == 'r'
393 && getc (finput) == 'a'
394 && getc (finput) == 'g'
395 && getc (finput) == 'm'
396 && getc (finput) == 'a'
397 && ((c = getc (finput)) == ' ' || c == '\t' || c == '\n'))
399 #ifdef HANDLE_SYSV_PRAGMA
400 return handle_sysv_pragma (finput, c);
401 #else /* !HANDLE_SYSV_PRAGMA */
403 HANDLE_PRAGMA (finput);
404 #endif /* HANDLE_PRAGMA */
406 #endif /* !HANDLE_SYSV_PRAGMA */
412 if (getc (finput) == 'e'
413 && getc (finput) == 'f'
414 && getc (finput) == 'i'
415 && getc (finput) == 'n'
416 && getc (finput) == 'e'
417 && ((c = getc (finput)) == ' ' || c == '\t' || c == '\n'))
419 #ifdef DWARF_DEBUGGING_INFO
420 if ((debug_info_level == DINFO_LEVEL_VERBOSE)
421 && (write_symbols == DWARF_DEBUG))
422 dwarfout_define (lineno, get_directive_line (finput));
423 #endif /* DWARF_DEBUGGING_INFO */
429 if (getc (finput) == 'n'
430 && getc (finput) == 'd'
431 && getc (finput) == 'e'
432 && getc (finput) == 'f'
433 && ((c = getc (finput)) == ' ' || c == '\t' || c == '\n'))
435 #ifdef DWARF_DEBUGGING_INFO
436 if ((debug_info_level == DINFO_LEVEL_VERBOSE)
437 && (write_symbols == DWARF_DEBUG))
438 dwarfout_undef (lineno, get_directive_line (finput));
439 #endif /* DWARF_DEBUGGING_INFO */
445 if (getc (finput) == 'i'
446 && getc (finput) == 'n'
447 && getc (finput) == 'e'
448 && ((c = getc (finput)) == ' ' || c == '\t'))
453 if (getc (finput) == 'd'
454 && getc (finput) == 'e'
455 && getc (finput) == 'n'
456 && getc (finput) == 't'
457 && ((c = getc (finput)) == ' ' || c == '\t'))
459 /* #ident. The pedantic warning is now in cccp.c. */
461 /* Here we have just seen `#ident '.
462 A string constant should follow. */
464 while (c == ' ' || c == '\t')
467 /* If no argument, ignore the line. */
474 || TREE_CODE (yylval.ttype) != STRING_CST)
476 error ("invalid #ident");
482 #ifdef ASM_OUTPUT_IDENT
483 ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (yylval.ttype));
487 /* Skip the rest of this line. */
492 error ("undefined or invalid # directive");
497 /* Here we have either `#line' or `# <nonletter>'.
498 In either case, it should be a line number; a digit should follow. */
500 while (c == ' ' || c == '\t')
503 /* If the # is the only nonwhite char on the line,
504 just ignore it. Check the new newline. */
508 /* Something follows the #; read a token. */
513 if (token == CONSTANT
514 && TREE_CODE (yylval.ttype) == INTEGER_CST)
516 int old_lineno = lineno;
518 /* subtract one, because it is the following line that
519 gets the specified number */
521 int l = TREE_INT_CST_LOW (yylval.ttype) - 1;
523 /* Is this the last nonwhite stuff on the line? */
525 while (c == ' ' || c == '\t')
529 /* No more: store the line number and check following line. */
535 /* More follows: it must be a string constant (filename). */
537 /* Read the string constant. */
540 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
542 error ("invalid #line");
547 = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
548 strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
551 /* Each change of file name
552 reinitializes whether we are now in a system header. */
553 in_system_header = 0;
555 if (main_input_filename == 0)
556 main_input_filename = input_filename;
558 /* Is this the last nonwhite stuff on the line? */
560 while (c == ' ' || c == '\t')
564 /* Update the name in the top element of input_file_stack. */
565 if (input_file_stack)
566 input_file_stack->name = input_filename;
575 /* `1' after file name means entering new file.
576 `2' after file name means just left a file. */
578 if (token == CONSTANT
579 && TREE_CODE (yylval.ttype) == INTEGER_CST)
581 if (TREE_INT_CST_LOW (yylval.ttype) == 1)
583 /* Pushing to a new file. */
585 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
586 input_file_stack->line = old_lineno;
587 p->next = input_file_stack;
588 p->name = input_filename;
589 input_file_stack = p;
590 input_file_stack_tick++;
591 #ifdef DWARF_DEBUGGING_INFO
592 if (debug_info_level == DINFO_LEVEL_VERBOSE
593 && write_symbols == DWARF_DEBUG)
594 dwarfout_start_new_source_file (input_filename);
595 #endif /* DWARF_DEBUGGING_INFO */
599 else if (TREE_INT_CST_LOW (yylval.ttype) == 2)
601 /* Popping out of a file. */
602 if (input_file_stack->next)
604 struct file_stack *p = input_file_stack;
605 input_file_stack = p->next;
607 input_file_stack_tick++;
608 #ifdef DWARF_DEBUGGING_INFO
609 if (debug_info_level == DINFO_LEVEL_VERBOSE
610 && write_symbols == DWARF_DEBUG)
611 dwarfout_resume_previous_source_file (input_file_stack->line);
612 #endif /* DWARF_DEBUGGING_INFO */
615 error ("#-lines for entering and leaving files don't match");
621 /* Now that we've pushed or popped the input stack,
622 update the name in the top element. */
623 if (input_file_stack)
624 input_file_stack->name = input_filename;
626 /* If we have handled a `1' or a `2',
627 see if there is another number to read. */
630 /* Is this the last nonwhite stuff on the line? */
632 while (c == ' ' || c == '\t')
642 /* `3' after file name means this is a system header file. */
644 if (token == CONSTANT
645 && TREE_CODE (yylval.ttype) == INTEGER_CST
646 && TREE_INT_CST_LOW (yylval.ttype) == 3)
647 in_system_header = 1, used_up = 1;
651 /* Is this the last nonwhite stuff on the line? */
653 while (c == ' ' || c == '\t')
660 warning ("unrecognized text at end of #line");
663 error ("invalid #-line");
665 /* skip the rest of this line. */
669 while ((c = getc (finput)) != EOF && c != '\n');
673 #ifdef HANDLE_SYSV_PRAGMA
675 /* Handle a #pragma directive. INPUT is the current input stream,
676 and C is a character to reread. Processes the entire input line
677 and returns a character for the caller to reread: either \n or EOF. */
679 /* This function has to be in this file, in order to get at
683 handle_sysv_pragma (input, c)
689 while (c == ' ' || c == '\t')
691 if (c == '\n' || c == EOF)
693 handle_pragma_token (0, 0);
703 handle_pragma_token (token_buffer, yylval.ttype);
706 handle_pragma_token (token_buffer, 0);
709 c = nextchar, nextchar = -1;
715 #endif /* HANDLE_SYSV_PRAGMA */
717 #define ENDFILE -1 /* token that represents end-of-file */
719 /* Read an escape sequence, returning its equivalent as a character,
720 or store 1 in *ignore_ptr if it is backslash-newline. */
723 readescape (ignore_ptr)
726 register int c = getc (finput);
728 register unsigned count;
729 unsigned firstdig = 0;
735 if (warn_traditional)
736 warning ("the meaning of `\\x' varies with -traditional");
738 if (flag_traditional)
747 if (!(c >= 'a' && c <= 'f')
748 && !(c >= 'A' && c <= 'F')
749 && !(c >= '0' && c <= '9'))
755 if (c >= 'a' && c <= 'f')
756 code += c - 'a' + 10;
757 if (c >= 'A' && c <= 'F')
758 code += c - 'A' + 10;
759 if (c >= '0' && c <= '9')
761 if (code != 0 || count != 0)
770 error ("\\x used with no following hex digits");
772 /* Digits are all 0's. Ok. */
774 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
776 && ((1 << (TYPE_PRECISION (integer_type_node) - (count - 1) * 4))
778 pedwarn ("hex escape out of range");
781 case '0': case '1': case '2': case '3': case '4':
782 case '5': case '6': case '7':
785 while ((c <= '7') && (c >= '0') && (count++ < 3))
787 code = (code * 8) + (c - '0');
793 case '\\': case '\'': case '"':
802 return TARGET_NEWLINE;
817 if (warn_traditional)
818 warning ("the meaning of `\\a' varies with -traditional");
820 if (flag_traditional)
825 #if 0 /* Vertical tab is present in common usage compilers. */
826 if (flag_traditional)
834 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
840 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
844 /* `\%' is used to prevent SCCS from getting confused. */
847 pedwarn ("non-ANSI escape sequence `\\%c'", c);
850 if (c >= 040 && c < 0177)
851 pedwarn ("unknown escape sequence `\\%c'", c);
853 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
863 strcpy (buf, string);
865 /* We can't print string and character constants well
866 because the token_buffer contains the result of processing escapes. */
868 strcat (buf, " at end of input");
869 else if (token_buffer[0] == 0)
870 strcat (buf, " at null character");
871 else if (token_buffer[0] == '"')
872 strcat (buf, " before string constant");
873 else if (token_buffer[0] == '\'')
874 strcat (buf, " before character constant");
875 else if (token_buffer[0] < 040 || (unsigned char) token_buffer[0] >= 0177)
876 sprintf (buf + strlen (buf), " before character 0%o",
877 (unsigned char) token_buffer[0]);
879 strcat (buf, " before `%s'");
881 error (buf, token_buffer);
894 struct try_type type_sequence[] =
896 { &integer_type_node, 0, 0, 0},
897 { &unsigned_type_node, 1, 0, 0},
898 { &long_integer_type_node, 0, 1, 0},
899 { &long_unsigned_type_node, 1, 1, 0},
900 { &long_long_integer_type_node, 0, 1, 1},
901 { &long_long_unsigned_type_node, 1, 1, 1}
915 c = nextchar, nextchar = -1;
919 /* Effectively do c = skip_white_space (c)
920 but do it faster in the usual cases. */
933 /* Call skip_white_space so we can warn if appropriate. */
938 c = skip_white_space (c);
947 /* yylloc.first_line = lineno; */
958 if (dollars_in_ident)
963 /* Capital L may start a wide-string or wide-character constant. */
965 register int c = getc (finput);
974 goto string_constant;
981 if (!doing_objc_thang)
988 /* '@' may start a constant string object. */
989 register int c = getc(finput);
993 goto string_constant;
996 /* Fall through to treat '@' as the start of an indentifier. */
999 case 'A': case 'B': case 'C': case 'D': case 'E':
1000 case 'F': case 'G': case 'H': case 'I': case 'J':
1001 case 'K': case 'M': case 'N': case 'O':
1002 case 'P': case 'Q': case 'R': case 'S': case 'T':
1003 case 'U': case 'V': case 'W': case 'X': case 'Y':
1005 case 'a': case 'b': case 'c': case 'd': case 'e':
1006 case 'f': case 'g': case 'h': case 'i': case 'j':
1007 case 'k': case 'l': case 'm': case 'n': case 'o':
1008 case 'p': case 'q': case 'r': case 's': case 't':
1009 case 'u': case 'v': case 'w': case 'x': case 'y':
1014 while (isalnum (c) || c == '_' || c == '$' || c == '@')
1016 /* Make sure this char really belongs in an identifier. */
1017 if (c == '@' && ! doing_objc_thang)
1019 if (c == '$' && ! dollars_in_ident)
1022 if (p >= token_buffer + maxtoken)
1023 p = extend_token_buffer (p);
1035 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1038 register struct resword *ptr;
1040 if (ptr = is_reserved_word (token_buffer, p - token_buffer))
1043 yylval.ttype = ridpointers[(int) ptr->rid];
1044 value = (int) ptr->token;
1046 /* Only return OBJECTNAME if it is a typedef. */
1047 if (doing_objc_thang && value == OBJECTNAME)
1049 lastiddecl = lookup_name(yylval.ttype);
1051 if (lastiddecl == NULL_TREE
1052 || TREE_CODE (lastiddecl) != TYPE_DECL)
1056 /* Even if we decided to recognize asm, still perhaps warn. */
1058 && (value == ASM_KEYWORD || value == TYPEOF
1059 || ptr->rid == RID_INLINE)
1060 && token_buffer[0] != '_')
1061 pedwarn ("ANSI does not permit the keyword `%s'",
1066 /* If we did not find a keyword, look for an identifier
1069 if (value == IDENTIFIER)
1071 if (token_buffer[0] == '@')
1072 error("invalid identifier `%s'", token_buffer);
1074 yylval.ttype = get_identifier (token_buffer);
1075 lastiddecl = lookup_name (yylval.ttype);
1077 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1079 /* A user-invisible read-only initialized variable
1080 should be replaced by its value.
1081 We handle only strings since that's the only case used in C. */
1082 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1083 && DECL_IGNORED_P (lastiddecl)
1084 && TREE_READONLY (lastiddecl)
1085 && DECL_INITIAL (lastiddecl) != 0
1086 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1088 tree stringval = DECL_INITIAL (lastiddecl);
1090 /* Copy the string value so that we won't clobber anything
1091 if we put something in the TREE_CHAIN of this one. */
1092 yylval.ttype = build_string (TREE_STRING_LENGTH (stringval),
1093 TREE_STRING_POINTER (stringval));
1096 else if (doing_objc_thang)
1098 tree objc_interface_decl = is_class_name (yylval.ttype);
1100 if (objc_interface_decl)
1103 yylval.ttype = objc_interface_decl;
1110 case '0': case '1': case '2': case '3': case '4':
1111 case '5': case '6': case '7': case '8': case '9':
1116 int largest_digit = 0;
1118 /* for multi-precision arithmetic,
1119 we actually store only HOST_BITS_PER_CHAR bits in each part.
1120 The number of parts is chosen so as to be sufficient to hold
1121 the enough bits to fit into the two HOST_WIDE_INTs that contain
1122 the integer value (this is always at least as many bits as are
1123 in a target `long long' value, but may be wider). */
1124 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2 + 2)
1125 int parts[TOTAL_PARTS];
1128 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS} floatflag
1131 for (count = 0; count < TOTAL_PARTS; count++)
1139 *p++ = (c = getc (finput));
1140 if ((c == 'x') || (c == 'X'))
1143 *p++ = (c = getc (finput));
1145 /* Leading 0 forces octal unless the 0 is the only digit. */
1146 else if (c >= '0' && c <= '9')
1155 /* Read all the digits-and-decimal-points. */
1158 || (isalnum (c) && c != 'l' && c != 'L'
1159 && c != 'u' && c != 'U'
1160 && c != 'i' && c != 'I' && c != 'j' && c != 'J'
1161 && (floatflag == NOT_FLOAT || ((c != 'f') && (c != 'F')))))
1166 error ("floating constant may not be in radix 16");
1167 if (floatflag == TOO_MANY_POINTS)
1168 /* We have already emitted an error. Don't need another. */
1170 else if (floatflag == AFTER_POINT)
1172 error ("malformed floating constant");
1173 floatflag = TOO_MANY_POINTS;
1174 /* Avoid another error from atof by forcing all characters
1175 from here on to be ignored. */
1179 floatflag = AFTER_POINT;
1182 *p++ = c = getc (finput);
1183 /* Accept '.' as the start of a floating-point number
1184 only when it is followed by a digit.
1185 Otherwise, unread the following non-digit
1186 and use the '.' as a structural token. */
1187 if (p == token_buffer + 2 && !isdigit (c))
1198 error ("parse error at `..'");
1201 token_buffer[1] = 0;
1208 /* It is not a decimal point.
1209 It should be a digit (perhaps a hex digit). */
1215 else if (base <= 10)
1217 if (c == 'e' || c == 'E')
1220 floatflag = AFTER_POINT;
1221 break; /* start of exponent */
1223 error ("nondigits in number and not hexadecimal");
1234 if (c >= largest_digit)
1238 for (count = 0; count < TOTAL_PARTS; count++)
1240 parts[count] *= base;
1244 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1246 &= (1 << HOST_BITS_PER_CHAR) - 1;
1252 /* If the extra highest-order part ever gets anything in it,
1253 the number is certainly too big. */
1254 if (parts[TOTAL_PARTS - 1] != 0)
1257 if (p >= token_buffer + maxtoken - 3)
1258 p = extend_token_buffer (p);
1259 *p++ = (c = getc (finput));
1264 error ("numeric constant with no digits");
1266 if (largest_digit >= base)
1267 error ("numeric constant contains digits beyond the radix");
1269 /* Remove terminating char from the token buffer and delimit the string */
1272 if (floatflag != NOT_FLOAT)
1274 tree type = long_double_type_node;
1275 int garbage_chars = 0, exceeds_double = 0;
1277 REAL_VALUE_TYPE value;
1280 /* Read explicit exponent if any, and put it in tokenbuf. */
1282 if ((c == 'e') || (c == 'E'))
1284 if (p >= token_buffer + maxtoken - 3)
1285 p = extend_token_buffer (p);
1288 if ((c == '+') || (c == '-'))
1294 error ("floating constant exponent has no digits");
1297 if (p >= token_buffer + maxtoken - 3)
1298 p = extend_token_buffer (p);
1307 /* Convert string to a double, checking for overflow. */
1308 if (setjmp (handler))
1310 error ("floating constant out of range");
1315 int fflag = 0, lflag = 0;
1316 /* Copy token_buffer now, while it has just the number
1317 and not the suffixes; once we add `f' or `i',
1318 REAL_VALUE_ATOF may not work any more. */
1319 char *copy = (char *) alloca (p - token_buffer + 1);
1320 bcopy (token_buffer, copy, p - token_buffer + 1);
1322 set_float_handler (handler);
1328 /* Read the suffixes to choose a data type. */
1333 error ("more than one `f' in numeric constant");
1339 error ("more than one `l' in numeric constant");
1345 error ("more than one `i' or `j' in numeric constant");
1347 pedwarn ("ANSI C forbids imaginary numeric constants");
1358 if (p >= token_buffer + maxtoken - 3)
1359 p = extend_token_buffer (p);
1365 /* The second argument, machine_mode, of REAL_VALUE_ATOF
1366 tells the desired precision of the binary result
1367 of decimal-to-binary conversion. */
1372 error ("both `f' and `l' in floating constant");
1374 type = float_type_node;
1375 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1376 /* A diagnostic is required here by some ANSI C testsuites.
1377 This is not pedwarn, become some people don't want
1378 an error for this. */
1379 if (REAL_VALUE_ISINF (value) && pedantic)
1380 warning ("floating point number exceeds range of `float'");
1384 type = long_double_type_node;
1385 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1386 if (REAL_VALUE_ISINF (value) && pedantic)
1387 warning ("floating point number exceeds range of `long double'");
1391 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1392 if (REAL_VALUE_ISINF (value) && pedantic)
1393 warning ("floating point number exceeds range of `double'");
1396 set_float_handler (NULL_PTR);
1399 if (errno == ERANGE && !flag_traditional && pedantic)
1401 /* ERANGE is also reported for underflow,
1402 so test the value to distinguish overflow from that. */
1403 if (REAL_VALUES_LESS (dconst1, value)
1404 || REAL_VALUES_LESS (value, dconstm1))
1406 warning ("floating point number exceeds range of `double'");
1412 while (isalnum (c) || c == '.' || c == '_'
1413 || (!flag_traditional && (c == '+' || c == '-')
1414 && (p[-1] == 'e' || p[-1] == 'E')))
1416 if (p >= token_buffer + maxtoken - 3)
1417 p = extend_token_buffer (p);
1422 if (garbage_chars > 0)
1423 error ("garbage at end of number");
1425 /* If the result is not a number, assume it must have been
1426 due to some error message above, so silently convert
1428 if (REAL_VALUE_ISNAN (value))
1431 /* Create a node with determined type and value. */
1433 yylval.ttype = build_complex (convert (type, integer_zero_node),
1434 build_real (type, value));
1436 yylval.ttype = build_real (type, value);
1443 tree traditional_type, ansi_type, type;
1444 HOST_WIDE_INT high, low;
1445 int spec_unsigned = 0;
1447 int spec_long_long = 0;
1453 if (c == 'u' || c == 'U')
1456 error ("two `u's in integer constant");
1459 else if (c == 'l' || c == 'L')
1464 error ("three `l's in integer constant");
1466 pedwarn ("ANSI C forbids long long integer constants");
1471 else if (c == 'i' || c == 'j' || c == 'I' || c == 'J')
1474 error ("more than one `i' or `j' in numeric constant");
1476 pedwarn ("ANSI C forbids imaginary numeric constants");
1481 if (isalnum (c) || c == '.' || c == '_'
1482 || (!flag_traditional && (c == '+' || c == '-')
1483 && (p[-1] == 'e' || p[-1] == 'E')))
1485 error ("garbage at end of number");
1486 while (isalnum (c) || c == '.' || c == '_'
1487 || (!flag_traditional && (c == '+' || c == '-')
1488 && (p[-1] == 'e' || p[-1] == 'E')))
1490 if (p >= token_buffer + maxtoken - 3)
1491 p = extend_token_buffer (p);
1498 if (p >= token_buffer + maxtoken - 3)
1499 p = extend_token_buffer (p);
1506 /* If the constant is not long long and it won't fit in an
1507 unsigned long, or if the constant is long long and won't fit
1508 in an unsigned long long, then warn that the constant is out
1511 /* ??? This assumes that long long and long integer types are
1512 a multiple of 8 bits. This better than the original code
1513 though which assumed that long was exactly 32 bits and long
1514 long was exactly 64 bits. */
1517 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1519 bytes = TYPE_PRECISION (long_integer_type_node) / 8;
1522 for (i = bytes; i < TOTAL_PARTS; i++)
1526 pedwarn ("integer constant out of range");
1528 /* This is simplified by the fact that our constant
1529 is always positive. */
1533 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1535 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1536 / HOST_BITS_PER_CHAR)]
1537 << (i * HOST_BITS_PER_CHAR));
1538 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1541 yylval.ttype = build_int_2 (low, high);
1542 TREE_TYPE (yylval.ttype) = long_long_unsigned_type_node;
1544 /* If warn_traditional, calculate both the ANSI type and the
1545 traditional type, then see if they disagree.
1546 Otherwise, calculate only the type for the dialect in use. */
1547 if (warn_traditional || flag_traditional)
1549 /* Calculate the traditional type. */
1550 /* Traditionally, any constant is signed;
1551 but if unsigned is specified explicitly, obey that.
1552 Use the smallest size with the right number of bits,
1553 except for one special case with decimal constants. */
1554 if (! spec_long && base != 10
1555 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1556 traditional_type = (spec_unsigned ? unsigned_type_node
1557 : integer_type_node);
1558 /* A decimal constant must be long
1559 if it does not fit in type int.
1560 I think this is independent of whether
1561 the constant is signed. */
1562 else if (! spec_long && base == 10
1563 && int_fits_type_p (yylval.ttype, integer_type_node))
1564 traditional_type = (spec_unsigned ? unsigned_type_node
1565 : integer_type_node);
1566 else if (! spec_long_long)
1567 traditional_type = (spec_unsigned ? long_unsigned_type_node
1568 : long_integer_type_node);
1570 traditional_type = (spec_unsigned
1571 ? long_long_unsigned_type_node
1572 : long_long_integer_type_node);
1574 if (warn_traditional || ! flag_traditional)
1576 /* Calculate the ANSI type. */
1577 if (! spec_long && ! spec_unsigned
1578 && int_fits_type_p (yylval.ttype, integer_type_node))
1579 ansi_type = integer_type_node;
1580 else if (! spec_long && (base != 10 || spec_unsigned)
1581 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1582 ansi_type = unsigned_type_node;
1583 else if (! spec_unsigned && !spec_long_long
1584 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1585 ansi_type = long_integer_type_node;
1586 else if (! spec_long_long)
1587 ansi_type = long_unsigned_type_node;
1588 else if (! spec_unsigned
1589 /* Verify value does not overflow into sign bit. */
1590 && TREE_INT_CST_HIGH (yylval.ttype) >= 0
1591 && int_fits_type_p (yylval.ttype,
1592 long_long_integer_type_node))
1593 ansi_type = long_long_integer_type_node;
1595 ansi_type = long_long_unsigned_type_node;
1598 type = flag_traditional ? traditional_type : ansi_type;
1600 if (warn_traditional && traditional_type != ansi_type)
1602 if (TYPE_PRECISION (traditional_type)
1603 != TYPE_PRECISION (ansi_type))
1604 warning ("width of integer constant changes with -traditional");
1605 else if (TREE_UNSIGNED (traditional_type)
1606 != TREE_UNSIGNED (ansi_type))
1607 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
1609 warning ("width of integer constant may change on other systems with -traditional");
1612 if (!flag_traditional && !int_fits_type_p (yylval.ttype, type)
1614 pedwarn ("integer constant out of range");
1616 if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type))
1617 warning ("decimal constant is so large that it is unsigned");
1621 if (TYPE_PRECISION (type)
1622 <= TYPE_PRECISION (integer_type_node))
1624 = build_complex (integer_zero_node,
1625 convert (integer_type_node, yylval.ttype));
1627 error ("complex integer constant is too wide for `complex int'");
1629 else if (flag_traditional && !int_fits_type_p (yylval.ttype, type))
1630 /* The traditional constant 0x80000000 is signed
1631 but doesn't fit in the range of int.
1632 This will change it to -0x80000000, which does fit. */
1634 TREE_TYPE (yylval.ttype) = unsigned_type (type);
1635 yylval.ttype = convert (type, yylval.ttype);
1636 TREE_OVERFLOW (yylval.ttype)
1637 = TREE_CONSTANT_OVERFLOW (yylval.ttype) = 0;
1640 TREE_TYPE (yylval.ttype) = type;
1645 value = CONSTANT; break;
1651 register int result = 0;
1652 register int num_chars = 0;
1653 unsigned width = TYPE_PRECISION (char_type_node);
1658 width = WCHAR_TYPE_SIZE;
1659 #ifdef MULTIBYTE_CHARS
1660 max_chars = MB_CUR_MAX;
1666 max_chars = TYPE_PRECISION (integer_type_node) / width;
1674 if (c == '\'' || c == EOF)
1680 c = readescape (&ignore);
1683 if (width < HOST_BITS_PER_INT
1684 && (unsigned) c >= (1 << width))
1685 pedwarn ("escape sequence out of range for character");
1686 #ifdef MAP_CHARACTER
1688 c = MAP_CHARACTER (c);
1694 pedwarn ("ANSI C forbids newline in character constant");
1697 #ifdef MAP_CHARACTER
1699 c = MAP_CHARACTER (c);
1703 if (num_chars > maxtoken - 4)
1704 extend_token_buffer (token_buffer);
1706 token_buffer[num_chars] = c;
1708 /* Merge character into result; ignore excess chars. */
1709 if (num_chars < max_chars + 1)
1711 if (width < HOST_BITS_PER_INT)
1712 result = (result << width) | (c & ((1 << width) - 1));
1718 token_buffer[num_chars + 1] = '\'';
1719 token_buffer[num_chars + 2] = 0;
1722 error ("malformatted character constant");
1723 else if (num_chars == 0)
1724 error ("empty character constant");
1725 else if (num_chars > max_chars)
1727 num_chars = max_chars;
1728 error ("character constant too long");
1730 else if (num_chars != 1 && ! flag_traditional)
1731 warning ("multi-character character constant");
1733 /* If char type is signed, sign-extend the constant. */
1736 int num_bits = num_chars * width;
1738 /* We already got an error; avoid invalid shift. */
1739 yylval.ttype = build_int_2 (0, 0);
1740 else if (TREE_UNSIGNED (char_type_node)
1741 || ((result >> (num_bits - 1)) & 1) == 0)
1743 = build_int_2 (result & ((unsigned HOST_WIDE_INT) ~0
1744 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1748 = build_int_2 (result | ~((unsigned HOST_WIDE_INT) ~0
1749 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1751 TREE_TYPE (yylval.ttype) = integer_type_node;
1755 #ifdef MULTIBYTE_CHARS
1756 /* Set the initial shift state and convert the next sequence. */
1758 /* In all locales L'\0' is zero and mbtowc will return zero,
1761 || (num_chars == 1 && token_buffer[1] != '\0'))
1764 (void) mbtowc (NULL_PTR, NULL_PTR, 0);
1765 if (mbtowc (& wc, token_buffer + 1, num_chars) == num_chars)
1768 warning ("Ignoring invalid multibyte character");
1771 yylval.ttype = build_int_2 (result, 0);
1772 TREE_TYPE (yylval.ttype) = wchar_type_node;
1783 p = token_buffer + 1;
1785 while (c != '"' && c >= 0)
1790 c = readescape (&ignore);
1794 && TYPE_PRECISION (char_type_node) < HOST_BITS_PER_INT
1795 && c >= (1 << TYPE_PRECISION (char_type_node)))
1796 pedwarn ("escape sequence out of range for character");
1801 pedwarn ("ANSI C forbids newline in string constant");
1805 if (p == token_buffer + maxtoken)
1806 p = extend_token_buffer (p);
1815 error ("Unterminated string constant");
1817 /* We have read the entire constant.
1818 Construct a STRING_CST for the result. */
1822 /* If this is a L"..." wide-string, convert the multibyte string
1823 to a wide character string. */
1824 char *widep = (char *) alloca ((p - token_buffer) * WCHAR_BYTES);
1827 #ifdef MULTIBYTE_CHARS
1828 len = mbstowcs ((wchar_t *) widep, token_buffer + 1, p - token_buffer);
1829 if (len < 0 || len >= (p - token_buffer))
1831 warning ("Ignoring invalid multibyte string");
1834 bzero (widep + (len * WCHAR_BYTES), WCHAR_BYTES);
1837 union { long l; char c[sizeof (long)]; } u;
1841 /* Determine whether host is little or big endian. */
1843 big_endian = u.c[sizeof (long) - 1];
1844 wp = widep + (big_endian ? WCHAR_BYTES - 1 : 0);
1846 bzero (widep, (p - token_buffer) * WCHAR_BYTES);
1847 for (cp = token_buffer + 1; cp < p; cp++)
1848 *wp = *cp, wp += WCHAR_BYTES;
1849 len = p - token_buffer - 1;
1852 yylval.ttype = build_string ((len + 1) * WCHAR_BYTES, widep);
1853 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
1858 extern tree build_objc_string();
1859 /* Return an Objective-C @"..." constant string object. */
1860 yylval.ttype = build_objc_string (p - token_buffer,
1862 TREE_TYPE (yylval.ttype) = char_array_type_node;
1863 value = OBJC_STRING;
1867 yylval.ttype = build_string (p - token_buffer, token_buffer + 1);
1868 TREE_TYPE (yylval.ttype) = char_array_type_node;
1899 yylval.code = PLUS_EXPR; break;
1901 yylval.code = MINUS_EXPR; break;
1903 yylval.code = BIT_AND_EXPR; break;
1905 yylval.code = BIT_IOR_EXPR; break;
1907 yylval.code = MULT_EXPR; break;
1909 yylval.code = TRUNC_DIV_EXPR; break;
1911 yylval.code = TRUNC_MOD_EXPR; break;
1913 yylval.code = BIT_XOR_EXPR; break;
1915 yylval.code = LSHIFT_EXPR; break;
1917 yylval.code = RSHIFT_EXPR; break;
1919 yylval.code = LT_EXPR; break;
1921 yylval.code = GT_EXPR; break;
1924 token_buffer[1] = c1 = getc (finput);
1925 token_buffer[2] = 0;
1932 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
1934 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
1936 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
1938 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
1940 value = ASSIGN; goto done;
1946 value = PLUSPLUS; goto done;
1948 value = MINUSMINUS; goto done;
1950 value = ANDAND; goto done;
1952 value = OROR; goto done;
1965 { value = POINTSAT; goto done; }
1969 { value = ']'; goto done; }
1973 { value = '{'; goto done; }
1975 { value = '['; goto done; }
1979 { value = '}'; goto done; }
1982 ungetc (c1, finput);
1983 token_buffer[1] = 0;
1985 if ((c == '<') || (c == '>'))
1986 value = ARITHCOMPARE;
1992 /* Don't make yyparse think this is eof. */
2001 /* yylloc.last_line = lineno; */
2006 /* Sets the value of the 'yydebug' variable to VALUE.
2007 This is a function so we don't have to have YYDEBUG defined
2008 in order to build the compiler. */
2017 warning ("YYDEBUG not defined.");