1 /* Lexical analyzer for C and Objective C.
2 Copyright (C) 1987, 88, 89, 92, 94-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
36 #ifdef MULTIBYTE_CHARS
40 #ifndef GET_ENVIRONMENT
41 #define GET_ENVIRONMENT(ENV_VALUE,ENV_NAME) ((ENV_VALUE) = getenv (ENV_NAME))
43 #endif /* MULTIBYTE_CHARS */
47 extern cpp_reader parse_in;
48 extern cpp_options parse_options;
50 /* Stream for reading from the input file. */
54 extern void yyprint PROTO((FILE *, int, YYSTYPE));
56 /* The elements of `ridpointers' are identifier nodes
57 for the reserved type names and storage classes.
58 It is indexed by a RID_... value. */
59 tree ridpointers[(int) RID_MAX];
61 /* Cause the `yydebug' variable to be defined. */
65 extern unsigned char *yy_cur, *yy_lim;
67 extern int yy_get_token ();
69 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
70 #define UNGETC(c) ((c), yy_cur--)
72 #define GETC() getc (finput)
73 #define UNGETC(c) ungetc (c, finput)
76 /* the declaration found for the last IDENTIFIER token read in.
77 yylex must look this up to detect typedefs, which get token type TYPENAME,
78 so it is left around in case the identifier is not a typedef but is
79 used in a context which makes it a reference to a variable. */
82 /* Nonzero enables objc features. */
88 /* File used for outputting assembler code. */
89 extern FILE *asm_out_file;
91 #ifndef WCHAR_TYPE_SIZE
93 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
95 #define WCHAR_TYPE_SIZE BITS_PER_WORD
99 /* Number of bytes in a wide character. */
100 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
102 static int maxtoken; /* Current nominal length of token buffer. */
103 char *token_buffer; /* Pointer to token buffer.
104 Actual allocated length is maxtoken + 2.
105 This is not static because objc-parse.y uses it. */
107 static int indent_level = 0; /* Number of { minus number of }. */
109 /* Nonzero if end-of-file has been seen on input. */
110 static int end_of_file;
113 /* Buffered-back input character; faster than using ungetc. */
114 static int nextchar = -1;
117 #ifdef HANDLE_SYSV_PRAGMA
118 static int handle_sysv_pragma PROTO((int));
119 #endif /* HANDLE_SYSV_PRAGMA */
120 static int whitespace_cr PROTO((int));
121 static int skip_white_space PROTO((int));
122 static int skip_white_space_on_line PROTO((void));
123 static char *extend_token_buffer PROTO((char *));
124 static int readescape PROTO((int *));
126 /* Do not insert generated code into the source, instead, include it.
127 This allows us to build gcc automatically even for targets that
128 need to add or modify the reserved keyword lists. */
131 /* Return something to represent absolute declarators containing a *.
132 TARGET is the absolute declarator that the * contains.
133 TYPE_QUALS is a list of modifiers such as const or volatile
134 to apply to the pointer type, represented as identifiers.
136 We return an INDIRECT_REF whose "contents" are TARGET
137 and whose type is the modifier list. */
140 make_pointer_declarator (type_quals, target)
141 tree type_quals, target;
143 return build1 (INDIRECT_REF, type_quals, target);
147 forget_protocol_qualifiers ()
149 int i, n = sizeof wordlist / sizeof (struct resword);
151 for (i = 0; i < n; i++)
152 if ((int) wordlist[i].rid >= (int) RID_IN
153 && (int) wordlist[i].rid <= (int) RID_ONEWAY)
154 wordlist[i].name = "";
158 remember_protocol_qualifiers ()
160 int i, n = sizeof wordlist / sizeof (struct resword);
162 for (i = 0; i < n; i++)
163 if (wordlist[i].rid == RID_IN)
164 wordlist[i].name = "in";
165 else if (wordlist[i].rid == RID_OUT)
166 wordlist[i].name = "out";
167 else if (wordlist[i].rid == RID_INOUT)
168 wordlist[i].name = "inout";
169 else if (wordlist[i].rid == RID_BYCOPY)
170 wordlist[i].name = "bycopy";
171 else if (wordlist[i].rid == RID_ONEWAY)
172 wordlist[i].name = "oneway";
176 init_parse (filename)
180 /* Open input file. */
181 if (filename == 0 || !strcmp (filename, "-"))
187 finput = fopen (filename, "r");
189 pfatal_with_name (filename);
191 #ifdef IO_BUFFER_SIZE
192 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE), _IOFBF, IO_BUFFER_SIZE);
194 #endif /* !USE_CPPLIB */
202 parse_in.show_column = 1;
203 if (! cpp_start_read (&parse_in, filename))
214 cpp_finish (&parse_in);
223 /* Make identifier nodes long enough for the language-specific slots. */
224 set_identifier_size (sizeof (struct lang_identifier));
226 /* Start it at 0, because check_newline is called at the very beginning
227 and will increment it to 1. */
230 #ifdef MULTIBYTE_CHARS
231 /* Change to the native locale for multibyte conversions. */
232 setlocale (LC_CTYPE, "");
233 GET_ENVIRONMENT (literal_codeset, "LANG");
237 token_buffer = (char *) xmalloc (maxtoken + 2);
239 ridpointers[(int) RID_INT] = get_identifier ("int");
240 ridpointers[(int) RID_CHAR] = get_identifier ("char");
241 ridpointers[(int) RID_VOID] = get_identifier ("void");
242 ridpointers[(int) RID_FLOAT] = get_identifier ("float");
243 ridpointers[(int) RID_DOUBLE] = get_identifier ("double");
244 ridpointers[(int) RID_SHORT] = get_identifier ("short");
245 ridpointers[(int) RID_LONG] = get_identifier ("long");
246 ridpointers[(int) RID_UNSIGNED] = get_identifier ("unsigned");
247 ridpointers[(int) RID_SIGNED] = get_identifier ("signed");
248 ridpointers[(int) RID_INLINE] = get_identifier ("inline");
249 ridpointers[(int) RID_CONST] = get_identifier ("const");
250 ridpointers[(int) RID_VOLATILE] = get_identifier ("volatile");
251 ridpointers[(int) RID_AUTO] = get_identifier ("auto");
252 ridpointers[(int) RID_STATIC] = get_identifier ("static");
253 ridpointers[(int) RID_EXTERN] = get_identifier ("extern");
254 ridpointers[(int) RID_TYPEDEF] = get_identifier ("typedef");
255 ridpointers[(int) RID_REGISTER] = get_identifier ("register");
256 ridpointers[(int) RID_ITERATOR] = get_identifier ("iterator");
257 ridpointers[(int) RID_COMPLEX] = get_identifier ("complex");
258 ridpointers[(int) RID_ID] = get_identifier ("id");
259 ridpointers[(int) RID_IN] = get_identifier ("in");
260 ridpointers[(int) RID_OUT] = get_identifier ("out");
261 ridpointers[(int) RID_INOUT] = get_identifier ("inout");
262 ridpointers[(int) RID_BYCOPY] = get_identifier ("bycopy");
263 ridpointers[(int) RID_ONEWAY] = get_identifier ("oneway");
264 forget_protocol_qualifiers();
266 /* Some options inhibit certain reserved words.
267 Clear those words out of the hash table so they won't be recognized. */
268 #define UNSET_RESERVED_WORD(STRING) \
269 do { struct resword *s = is_reserved_word (STRING, sizeof (STRING) - 1); \
270 if (s) s->name = ""; } while (0)
272 if (! doing_objc_thang)
273 UNSET_RESERVED_WORD ("id");
275 if (flag_traditional)
277 UNSET_RESERVED_WORD ("const");
278 UNSET_RESERVED_WORD ("volatile");
279 UNSET_RESERVED_WORD ("typeof");
280 UNSET_RESERVED_WORD ("signed");
281 UNSET_RESERVED_WORD ("inline");
282 UNSET_RESERVED_WORD ("iterator");
283 UNSET_RESERVED_WORD ("complex");
287 UNSET_RESERVED_WORD ("asm");
288 UNSET_RESERVED_WORD ("typeof");
289 UNSET_RESERVED_WORD ("inline");
290 UNSET_RESERVED_WORD ("iterator");
291 UNSET_RESERVED_WORD ("complex");
296 reinit_parse_for_function ()
300 /* Function used when yydebug is set, to print a token in more detail. */
303 yyprint (file, yychar, yylval)
315 if (IDENTIFIER_POINTER (t))
316 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
321 if (TREE_CODE (t) == INTEGER_CST)
323 #if HOST_BITS_PER_WIDE_INT == 64
324 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
327 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
334 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
340 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
345 /* Iff C is a carriage return, warn about it - if appropriate -
346 and return nonzero. */
351 static int newline_warning = 0;
355 /* ANSI C says the effects of a carriage return in a source file
357 if (pedantic && !newline_warning)
359 warning ("carriage return in source file");
360 warning ("(we only warn about the first carriage return)");
368 /* If C is not whitespace, return C.
369 Otherwise skip whitespace and return first nonwhite char read. */
379 /* We don't recognize comments here, because
380 cpp output can include / and * consecutively as operators.
381 Also, there's no need, since cpp removes all comments. */
384 c = check_newline ();
405 error ("stray '\\' in program");
415 /* Skips all of the white space at the current location in the input file.
416 Must use and reset nextchar if it has the next character. */
419 position_after_white_space ()
425 c = nextchar, nextchar = -1;
430 UNGETC (skip_white_space (c));
433 /* Like skip_white_space, but don't advance beyond the end of line.
434 Moreover, we don't get passed a character to start with. */
436 skip_white_space_on_line ()
465 /* Make the token buffer longer, preserving the data in it.
466 P should point to just beyond the last valid character in the old buffer.
467 The value we return is a pointer to the new buffer
468 at a place corresponding to P. */
471 extend_token_buffer (p)
474 int offset = p - token_buffer;
476 maxtoken = maxtoken * 2 + 10;
477 token_buffer = (char *) xrealloc (token_buffer, maxtoken + 2);
479 return token_buffer + offset;
482 #if defined HANDLE_PRAGMA
483 /* Local versions of these macros, that can be passed as function pointers. */
498 /* At the beginning of a line, increment the line number
499 and process any #-directive on this line.
500 If the line is a #-directive, read the entire line and return a newline.
501 Otherwise, return the line's first non-whitespace character. */
511 /* Read first nonwhite char on the line. */
514 while (c == ' ' || c == '\t')
519 /* If not #, return it so caller will use it. */
523 /* Read first nonwhite char after the `#'. */
526 while (c == ' ' || c == '\t')
529 /* If a letter follows, then if the word here is `line', skip
530 it and ignore it; otherwise, ignore the line, with an error
531 if the word isn't `pragma', `ident', `define', or `undef'. */
533 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
542 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'
543 || whitespace_cr (c) ))
545 while (c == ' ' || c == '\t' || whitespace_cr (c))
550 #if defined HANDLE_PRAGMA || defined HANDLE_SYSV_PRAGMA
553 if (token != IDENTIFIER)
557 /* We invoke HANDLE_PRAGMA before HANDLE_SYSV_PRAGMA
558 (if both are defined), in order to give the back
559 end a chance to override the interpretation of
560 SYSV style pragmas. */
564 c = nextchar, nextchar = -1;
568 if (HANDLE_PRAGMA (pragma_getc, pragma_ungetc,
569 IDENTIFIER_POINTER (yylval.ttype)))
571 #endif /* HANDLE_PRAGMA */
573 #ifdef HANDLE_SYSV_PRAGMA
574 if (handle_sysv_pragma (token))
576 #endif /* !HANDLE_SYSV_PRAGMA */
577 #endif /* HANDLE_PRAGMA || HANDLE_SYSV_PRAGMA */
579 /* Issue a warning message if we have been asked to do so.
580 Ignoring unknown pragmas in system header file unless
581 an explcit -Wunknown-pragmas has been given. */
582 if (warn_unknown_pragmas > 1
583 || (warn_unknown_pragmas && ! in_system_header))
584 warning ("ignoring pragma: %s", token_buffer);
597 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'))
600 debug_define (lineno, GET_DIRECTIVE_LINE ());
610 && ((c = GETC()) == ' ' || c == '\t' || c == '\n'))
613 debug_undef (lineno, GET_DIRECTIVE_LINE ());
622 && ((c = GETC()) == ' ' || c == '\t'))
631 && ((c = GETC()) == ' ' || c == '\t'))
633 /* #ident. The pedantic warning is now in cccp.c. */
635 /* Here we have just seen `#ident '.
636 A string constant should follow. */
638 c = skip_white_space_on_line ();
640 /* If no argument, ignore the line. */
647 || TREE_CODE (yylval.ttype) != STRING_CST)
649 error ("invalid #ident");
655 #ifdef ASM_OUTPUT_IDENT
656 ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (yylval.ttype));
660 /* Skip the rest of this line. */
665 error ("undefined or invalid # directive");
670 /* Here we have either `#line' or `# <nonletter>'.
671 In either case, it should be a line number; a digit should follow. */
673 /* Can't use skip_white_space here, but must handle all whitespace
674 that is not '\n', lest we get a recursion for '\r' '\n' when
677 c = skip_white_space_on_line ();
679 /* If the # is the only nonwhite char on the line,
680 just ignore it. Check the new newline. */
684 /* Something follows the #; read a token. */
689 if (token == CONSTANT
690 && TREE_CODE (yylval.ttype) == INTEGER_CST)
692 int old_lineno = lineno;
694 /* subtract one, because it is the following line that
695 gets the specified number */
697 int l = TREE_INT_CST_LOW (yylval.ttype) - 1;
699 /* Is this the last nonwhite stuff on the line? */
700 c = skip_white_space_on_line ();
703 /* No more: store the line number and check following line. */
709 /* More follows: it must be a string constant (filename). */
711 /* Read the string constant. */
714 if (token != STRING || TREE_CODE (yylval.ttype) != STRING_CST)
716 error ("invalid #line");
721 = (char *) permalloc (TREE_STRING_LENGTH (yylval.ttype) + 1);
722 strcpy (input_filename, TREE_STRING_POINTER (yylval.ttype));
725 /* Each change of file name
726 reinitializes whether we are now in a system header. */
727 in_system_header = 0;
729 if (main_input_filename == 0)
730 main_input_filename = input_filename;
732 /* Is this the last nonwhite stuff on the line? */
733 c = skip_white_space_on_line ();
736 /* Update the name in the top element of input_file_stack. */
737 if (input_file_stack)
738 input_file_stack->name = input_filename;
747 /* `1' after file name means entering new file.
748 `2' after file name means just left a file. */
750 if (token == CONSTANT
751 && TREE_CODE (yylval.ttype) == INTEGER_CST)
753 if (TREE_INT_CST_LOW (yylval.ttype) == 1)
755 /* Pushing to a new file. */
757 = (struct file_stack *) xmalloc (sizeof (struct file_stack));
758 input_file_stack->line = old_lineno;
759 p->next = input_file_stack;
760 p->name = input_filename;
761 p->indent_level = indent_level;
762 input_file_stack = p;
763 input_file_stack_tick++;
764 debug_start_source_file (input_filename);
767 else if (TREE_INT_CST_LOW (yylval.ttype) == 2)
769 /* Popping out of a file. */
770 if (input_file_stack->next)
772 struct file_stack *p = input_file_stack;
773 if (indent_level != p->indent_level)
775 warning_with_file_and_line
776 (p->name, old_lineno,
777 "This file contains more `%c's than `%c's.",
778 indent_level > p->indent_level ? '{' : '}',
779 indent_level > p->indent_level ? '}' : '{');
781 input_file_stack = p->next;
783 input_file_stack_tick++;
784 debug_end_source_file (input_file_stack->line);
787 error ("#-lines for entering and leaving files don't match");
793 /* Now that we've pushed or popped the input stack,
794 update the name in the top element. */
795 if (input_file_stack)
796 input_file_stack->name = input_filename;
798 /* If we have handled a `1' or a `2',
799 see if there is another number to read. */
802 /* Is this the last nonwhite stuff on the line? */
803 c = skip_white_space_on_line ();
812 /* `3' after file name means this is a system header file. */
814 if (token == CONSTANT
815 && TREE_CODE (yylval.ttype) == INTEGER_CST
816 && TREE_INT_CST_LOW (yylval.ttype) == 3)
817 in_system_header = 1, used_up = 1;
821 /* Is this the last nonwhite stuff on the line? */
822 c = skip_white_space_on_line ();
828 warning ("unrecognized text at end of #line");
831 error ("invalid #-line");
833 /* skip the rest of this line. */
836 if (c != '\n' && c != EOF && nextchar >= 0)
837 c = nextchar, nextchar = -1;
839 while (c != '\n' && c != EOF)
844 #ifdef HANDLE_SYSV_PRAGMA
846 /* Handle a #pragma directive.
847 TOKEN is the token we read after `#pragma'. Processes the entire input
848 line and returns a character for the caller to reread: either \n or EOF. */
850 /* This function has to be in this file, in order to get at
854 handle_sysv_pragma (token)
867 handle_pragma_token (token_buffer, yylval.ttype);
870 handle_pragma_token (token_buffer, NULL);
874 c = nextchar, nextchar = -1;
879 while (c == ' ' || c == '\t')
883 if (c == '\n' || c == EOF)
884 return handle_pragma_token (NULL, NULL);
890 #endif /* HANDLE_SYSV_PRAGMA */
892 #define ENDFILE -1 /* token that represents end-of-file */
894 /* Read an escape sequence, returning its equivalent as a character,
895 or store 1 in *ignore_ptr if it is backslash-newline. */
898 readescape (ignore_ptr)
901 register int c = GETC();
903 register unsigned count;
904 unsigned firstdig = 0;
910 if (warn_traditional)
911 warning ("the meaning of `\\x' varies with -traditional");
913 if (flag_traditional)
922 if (!(c >= 'a' && c <= 'f')
923 && !(c >= 'A' && c <= 'F')
924 && !(c >= '0' && c <= '9'))
930 if (c >= 'a' && c <= 'f')
931 code += c - 'a' + 10;
932 if (c >= 'A' && c <= 'F')
933 code += c - 'A' + 10;
934 if (c >= '0' && c <= '9')
936 if (code != 0 || count != 0)
945 error ("\\x used with no following hex digits");
947 /* Digits are all 0's. Ok. */
949 else if ((count - 1) * 4 >= TYPE_PRECISION (integer_type_node)
951 && ((1 << (TYPE_PRECISION (integer_type_node) - (count - 1) * 4))
953 pedwarn ("hex escape out of range");
956 case '0': case '1': case '2': case '3': case '4':
957 case '5': case '6': case '7':
960 while ((c <= '7') && (c >= '0') && (count++ < 3))
962 code = (code * 8) + (c - '0');
968 case '\\': case '\'': case '"':
977 return TARGET_NEWLINE;
992 if (warn_traditional)
993 warning ("the meaning of `\\a' varies with -traditional");
995 if (flag_traditional)
1000 #if 0 /* Vertical tab is present in common usage compilers. */
1001 if (flag_traditional)
1009 pedwarn ("non-ANSI-standard escape sequence, `\\%c'", c);
1015 /* `\(', etc, are used at beginning of line to avoid confusing Emacs. */
1019 /* `\%' is used to prevent SCCS from getting confused. */
1022 pedwarn ("non-ANSI escape sequence `\\%c'", c);
1025 if (c >= 040 && c < 0177)
1026 pedwarn ("unknown escape sequence `\\%c'", c);
1028 pedwarn ("unknown escape sequence: `\\' followed by char code 0x%x", c);
1038 strcpy (buf, string);
1040 /* We can't print string and character constants well
1041 because the token_buffer contains the result of processing escapes. */
1043 strcat (buf, " at end of input");
1044 else if (token_buffer[0] == 0)
1045 strcat (buf, " at null character");
1046 else if (token_buffer[0] == '"')
1047 strcat (buf, " before string constant");
1048 else if (token_buffer[0] == '\'')
1049 strcat (buf, " before character constant");
1050 else if (token_buffer[0] < 040 || (unsigned char) token_buffer[0] >= 0177)
1051 sprintf (buf + strlen (buf), " before character 0%o",
1052 (unsigned char) token_buffer[0]);
1054 strcat (buf, " before `%s'");
1056 error (buf, token_buffer);
1066 char long_long_flag;
1069 struct try_type type_sequence[] =
1071 { &integer_type_node, 0, 0, 0},
1072 { &unsigned_type_node, 1, 0, 0},
1073 { &long_integer_type_node, 0, 1, 0},
1074 { &long_unsigned_type_node, 1, 1, 0},
1075 { &long_long_integer_type_node, 0, 1, 1},
1076 { &long_long_unsigned_type_node, 1, 1, 1}
1091 c = nextchar, nextchar = -1;
1096 /* Effectively do c = skip_white_space (c)
1097 but do it faster in the usual cases. */
1110 /* Call skip_white_space so we can warn if appropriate. */
1115 c = skip_white_space (c);
1117 goto found_nonwhite;
1121 token_buffer[0] = c;
1122 token_buffer[1] = 0;
1124 /* yylloc.first_line = lineno; */
1130 token_buffer[0] = 0;
1135 /* Capital L may start a wide-string or wide-character constant. */
1137 register int c = GETC();
1146 goto string_constant;
1153 if (!doing_objc_thang)
1160 /* '@' may start a constant string object. */
1161 register int c = GETC ();
1165 goto string_constant;
1168 /* Fall through to treat '@' as the start of an identifier. */
1171 case 'A': case 'B': case 'C': case 'D': case 'E':
1172 case 'F': case 'G': case 'H': case 'I': case 'J':
1173 case 'K': case 'M': case 'N': case 'O':
1174 case 'P': case 'Q': case 'R': case 'S': case 'T':
1175 case 'U': case 'V': case 'W': case 'X': case 'Y':
1177 case 'a': case 'b': case 'c': case 'd': case 'e':
1178 case 'f': case 'g': case 'h': case 'i': case 'j':
1179 case 'k': case 'l': case 'm': case 'n': case 'o':
1180 case 'p': case 'q': case 'r': case 's': case 't':
1181 case 'u': case 'v': case 'w': case 'x': case 'y':
1187 while (ISALNUM (c) || c == '_' || c == '$' || c == '@')
1189 /* Make sure this char really belongs in an identifier. */
1190 if (c == '@' && ! doing_objc_thang)
1194 if (! dollars_in_ident)
1195 error ("`$' in identifier");
1197 pedwarn ("`$' in identifier");
1200 if (p >= token_buffer + maxtoken)
1201 p = extend_token_buffer (p);
1217 /* Try to recognize a keyword. Uses minimum-perfect hash function */
1220 register struct resword *ptr;
1222 if ((ptr = is_reserved_word (token_buffer, p - token_buffer)))
1225 yylval.ttype = ridpointers[(int) ptr->rid];
1226 value = (int) ptr->token;
1228 /* Only return OBJECTNAME if it is a typedef. */
1229 if (doing_objc_thang && value == OBJECTNAME)
1231 lastiddecl = lookup_name(yylval.ttype);
1233 if (lastiddecl == NULL_TREE
1234 || TREE_CODE (lastiddecl) != TYPE_DECL)
1238 /* Even if we decided to recognize asm, still perhaps warn. */
1240 && (value == ASM_KEYWORD || value == TYPEOF
1241 || ptr->rid == RID_INLINE)
1242 && token_buffer[0] != '_')
1243 pedwarn ("ANSI does not permit the keyword `%s'",
1248 /* If we did not find a keyword, look for an identifier
1251 if (value == IDENTIFIER)
1253 if (token_buffer[0] == '@')
1254 error("invalid identifier `%s'", token_buffer);
1256 yylval.ttype = get_identifier (token_buffer);
1257 lastiddecl = lookup_name (yylval.ttype);
1259 if (lastiddecl != 0 && TREE_CODE (lastiddecl) == TYPE_DECL)
1261 /* A user-invisible read-only initialized variable
1262 should be replaced by its value.
1263 We handle only strings since that's the only case used in C. */
1264 else if (lastiddecl != 0 && TREE_CODE (lastiddecl) == VAR_DECL
1265 && DECL_IGNORED_P (lastiddecl)
1266 && TREE_READONLY (lastiddecl)
1267 && DECL_INITIAL (lastiddecl) != 0
1268 && TREE_CODE (DECL_INITIAL (lastiddecl)) == STRING_CST)
1270 tree stringval = DECL_INITIAL (lastiddecl);
1272 /* Copy the string value so that we won't clobber anything
1273 if we put something in the TREE_CHAIN of this one. */
1274 yylval.ttype = build_string (TREE_STRING_LENGTH (stringval),
1275 TREE_STRING_POINTER (stringval));
1278 else if (doing_objc_thang)
1280 tree objc_interface_decl = is_class_name (yylval.ttype);
1282 if (objc_interface_decl)
1285 yylval.ttype = objc_interface_decl;
1295 /* Check first for common special case: single-digit 0 or 1. */
1298 UNGETC (next_c); /* Always undo this lookahead. */
1299 if (!ISALNUM (next_c) && next_c != '.')
1301 token_buffer[0] = (char)c, token_buffer[1] = '\0';
1302 yylval.ttype = (c == '0') ? integer_zero_node : integer_one_node;
1308 case '2': case '3': case '4':
1309 case '5': case '6': case '7': case '8': case '9':
1314 int largest_digit = 0;
1316 /* for multi-precision arithmetic,
1317 we actually store only HOST_BITS_PER_CHAR bits in each part.
1318 The number of parts is chosen so as to be sufficient to hold
1319 the enough bits to fit into the two HOST_WIDE_INTs that contain
1320 the integer value (this is always at least as many bits as are
1321 in a target `long long' value, but may be wider). */
1322 #define TOTAL_PARTS ((HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR) * 2 + 2)
1323 int parts[TOTAL_PARTS];
1326 enum anon1 { NOT_FLOAT, AFTER_POINT, TOO_MANY_POINTS} floatflag
1329 for (count = 0; count < TOTAL_PARTS; count++)
1337 *p++ = (c = GETC());
1338 if ((c == 'x') || (c == 'X'))
1341 *p++ = (c = GETC());
1343 /* Leading 0 forces octal unless the 0 is the only digit. */
1344 else if (c >= '0' && c <= '9')
1353 /* Read all the digits-and-decimal-points. */
1356 || (ISALNUM (c) && c != 'l' && c != 'L'
1357 && c != 'u' && c != 'U'
1358 && c != 'i' && c != 'I' && c != 'j' && c != 'J'
1359 && (floatflag == NOT_FLOAT || ((c != 'f') && (c != 'F')))))
1364 error ("floating constant may not be in radix 16");
1365 if (floatflag == TOO_MANY_POINTS)
1366 /* We have already emitted an error. Don't need another. */
1368 else if (floatflag == AFTER_POINT)
1370 error ("malformed floating constant");
1371 floatflag = TOO_MANY_POINTS;
1372 /* Avoid another error from atof by forcing all characters
1373 from here on to be ignored. */
1377 floatflag = AFTER_POINT;
1381 /* Accept '.' as the start of a floating-point number
1382 only when it is followed by a digit.
1383 Otherwise, unread the following non-digit
1384 and use the '.' as a structural token. */
1385 if (p == token_buffer + 2 && !ISDIGIT (c))
1396 error ("parse error at `..'");
1399 token_buffer[1] = 0;
1406 /* It is not a decimal point.
1407 It should be a digit (perhaps a hex digit). */
1413 else if (base <= 10)
1415 if (c == 'e' || c == 'E')
1418 floatflag = AFTER_POINT;
1419 break; /* start of exponent */
1421 error ("nondigits in number and not hexadecimal");
1432 if (c >= largest_digit)
1436 for (count = 0; count < TOTAL_PARTS; count++)
1438 parts[count] *= base;
1442 += (parts[count-1] >> HOST_BITS_PER_CHAR);
1444 &= (1 << HOST_BITS_PER_CHAR) - 1;
1450 /* If the extra highest-order part ever gets anything in it,
1451 the number is certainly too big. */
1452 if (parts[TOTAL_PARTS - 1] != 0)
1455 if (p >= token_buffer + maxtoken - 3)
1456 p = extend_token_buffer (p);
1457 *p++ = (c = GETC());
1462 error ("numeric constant with no digits");
1464 if (largest_digit >= base)
1465 error ("numeric constant contains digits beyond the radix");
1467 /* Remove terminating char from the token buffer and delimit the string */
1470 if (floatflag != NOT_FLOAT)
1472 tree type = double_type_node;
1474 int conversion_errno = 0;
1475 REAL_VALUE_TYPE value;
1478 /* Read explicit exponent if any, and put it in tokenbuf. */
1480 if ((c == 'e') || (c == 'E'))
1482 if (p >= token_buffer + maxtoken - 3)
1483 p = extend_token_buffer (p);
1486 if ((c == '+') || (c == '-'))
1492 error ("floating constant exponent has no digits");
1495 if (p >= token_buffer + maxtoken - 3)
1496 p = extend_token_buffer (p);
1504 /* Convert string to a double, checking for overflow. */
1505 if (setjmp (handler))
1507 error ("floating constant out of range");
1512 int fflag = 0, lflag = 0;
1513 /* Copy token_buffer now, while it has just the number
1514 and not the suffixes; once we add `f' or `i',
1515 REAL_VALUE_ATOF may not work any more. */
1516 char *copy = (char *) alloca (p - token_buffer + 1);
1517 bcopy (token_buffer, copy, p - token_buffer + 1);
1519 set_float_handler (handler);
1525 /* Read the suffixes to choose a data type. */
1530 error ("more than one `f' in numeric constant");
1536 error ("more than one `l' in numeric constant");
1542 error ("more than one `i' or `j' in numeric constant");
1544 pedwarn ("ANSI C forbids imaginary numeric constants");
1555 if (p >= token_buffer + maxtoken - 3)
1556 p = extend_token_buffer (p);
1562 /* The second argument, machine_mode, of REAL_VALUE_ATOF
1563 tells the desired precision of the binary result
1564 of decimal-to-binary conversion. */
1569 error ("both `f' and `l' in floating constant");
1571 type = float_type_node;
1573 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1574 conversion_errno = errno;
1575 /* A diagnostic is required here by some ANSI C testsuites.
1576 This is not pedwarn, become some people don't want
1577 an error for this. */
1578 if (REAL_VALUE_ISINF (value) && pedantic)
1579 warning ("floating point number exceeds range of `float'");
1583 type = long_double_type_node;
1585 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1586 conversion_errno = errno;
1587 if (REAL_VALUE_ISINF (value) && pedantic)
1588 warning ("floating point number exceeds range of `long double'");
1593 value = REAL_VALUE_ATOF (copy, TYPE_MODE (type));
1594 conversion_errno = errno;
1595 if (REAL_VALUE_ISINF (value) && pedantic)
1596 warning ("floating point number exceeds range of `double'");
1599 set_float_handler (NULL_PTR);
1602 /* ERANGE is also reported for underflow,
1603 so test the value to distinguish overflow from that. */
1604 if (conversion_errno == ERANGE && !flag_traditional && pedantic
1605 && (REAL_VALUES_LESS (dconst1, value)
1606 || REAL_VALUES_LESS (value, dconstm1)))
1607 warning ("floating point number exceeds range of `double'");
1610 /* If the result is not a number, assume it must have been
1611 due to some error message above, so silently convert
1613 if (REAL_VALUE_ISNAN (value))
1616 /* Create a node with determined type and value. */
1618 yylval.ttype = build_complex (NULL_TREE,
1619 convert (type, integer_zero_node),
1620 build_real (type, value));
1622 yylval.ttype = build_real (type, value);
1626 tree traditional_type, ansi_type, type;
1627 HOST_WIDE_INT high, low;
1628 int spec_unsigned = 0;
1630 int spec_long_long = 0;
1634 traditional_type = ansi_type = type = NULL_TREE;
1637 if (c == 'u' || c == 'U')
1640 error ("two `u's in integer constant");
1643 else if (c == 'l' || c == 'L')
1648 error ("three `l's in integer constant");
1649 else if (pedantic && ! in_system_header && warn_long_long)
1650 pedwarn ("ANSI C forbids long long integer constants");
1655 else if (c == 'i' || c == 'j' || c == 'I' || c == 'J')
1658 error ("more than one `i' or `j' in numeric constant");
1660 pedwarn ("ANSI C forbids imaginary numeric constants");
1665 if (p >= token_buffer + maxtoken - 3)
1666 p = extend_token_buffer (p);
1671 /* If the constant won't fit in an unsigned long long,
1672 then warn that the constant is out of range. */
1674 /* ??? This assumes that long long and long integer types are
1675 a multiple of 8 bits. This better than the original code
1676 though which assumed that long was exactly 32 bits and long
1677 long was exactly 64 bits. */
1679 bytes = TYPE_PRECISION (long_long_integer_type_node) / 8;
1682 for (i = bytes; i < TOTAL_PARTS; i++)
1686 pedwarn ("integer constant out of range");
1688 /* This is simplified by the fact that our constant
1689 is always positive. */
1693 for (i = 0; i < HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR; i++)
1695 high |= ((HOST_WIDE_INT) parts[i + (HOST_BITS_PER_WIDE_INT
1696 / HOST_BITS_PER_CHAR)]
1697 << (i * HOST_BITS_PER_CHAR));
1698 low |= (HOST_WIDE_INT) parts[i] << (i * HOST_BITS_PER_CHAR);
1701 yylval.ttype = build_int_2 (low, high);
1702 TREE_TYPE (yylval.ttype) = long_long_unsigned_type_node;
1704 /* If warn_traditional, calculate both the ANSI type and the
1705 traditional type, then see if they disagree.
1706 Otherwise, calculate only the type for the dialect in use. */
1707 if (warn_traditional || flag_traditional)
1709 /* Calculate the traditional type. */
1710 /* Traditionally, any constant is signed;
1711 but if unsigned is specified explicitly, obey that.
1712 Use the smallest size with the right number of bits,
1713 except for one special case with decimal constants. */
1714 if (! spec_long && base != 10
1715 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1716 traditional_type = (spec_unsigned ? unsigned_type_node
1717 : integer_type_node);
1718 /* A decimal constant must be long
1719 if it does not fit in type int.
1720 I think this is independent of whether
1721 the constant is signed. */
1722 else if (! spec_long && base == 10
1723 && int_fits_type_p (yylval.ttype, integer_type_node))
1724 traditional_type = (spec_unsigned ? unsigned_type_node
1725 : integer_type_node);
1726 else if (! spec_long_long)
1727 traditional_type = (spec_unsigned ? long_unsigned_type_node
1728 : long_integer_type_node);
1730 traditional_type = (spec_unsigned
1731 ? long_long_unsigned_type_node
1732 : long_long_integer_type_node);
1734 if (warn_traditional || ! flag_traditional)
1736 /* Calculate the ANSI type. */
1737 if (! spec_long && ! spec_unsigned
1738 && int_fits_type_p (yylval.ttype, integer_type_node))
1739 ansi_type = integer_type_node;
1740 else if (! spec_long && (base != 10 || spec_unsigned)
1741 && int_fits_type_p (yylval.ttype, unsigned_type_node))
1742 ansi_type = unsigned_type_node;
1743 else if (! spec_unsigned && !spec_long_long
1744 && int_fits_type_p (yylval.ttype, long_integer_type_node))
1745 ansi_type = long_integer_type_node;
1746 else if (! spec_long_long
1747 && int_fits_type_p (yylval.ttype,
1748 long_unsigned_type_node))
1749 ansi_type = long_unsigned_type_node;
1750 else if (! spec_unsigned
1751 && int_fits_type_p (yylval.ttype,
1752 long_long_integer_type_node))
1753 ansi_type = long_long_integer_type_node;
1755 ansi_type = long_long_unsigned_type_node;
1758 type = flag_traditional ? traditional_type : ansi_type;
1760 if (warn_traditional && traditional_type != ansi_type)
1762 if (TYPE_PRECISION (traditional_type)
1763 != TYPE_PRECISION (ansi_type))
1764 warning ("width of integer constant changes with -traditional");
1765 else if (TREE_UNSIGNED (traditional_type)
1766 != TREE_UNSIGNED (ansi_type))
1767 warning ("integer constant is unsigned in ANSI C, signed with -traditional");
1769 warning ("width of integer constant may change on other systems with -traditional");
1772 if (pedantic && !flag_traditional && !spec_long_long && !warn
1773 && (TYPE_PRECISION (long_integer_type_node)
1774 < TYPE_PRECISION (type)))
1775 pedwarn ("integer constant out of range");
1777 if (base == 10 && ! spec_unsigned && TREE_UNSIGNED (type))
1778 warning ("decimal constant is so large that it is unsigned");
1782 if (TYPE_PRECISION (type)
1783 <= TYPE_PRECISION (integer_type_node))
1785 = build_complex (NULL_TREE, integer_zero_node,
1786 convert (integer_type_node,
1789 error ("complex integer constant is too wide for `complex int'");
1791 else if (flag_traditional && !int_fits_type_p (yylval.ttype, type))
1792 /* The traditional constant 0x80000000 is signed
1793 but doesn't fit in the range of int.
1794 This will change it to -0x80000000, which does fit. */
1796 TREE_TYPE (yylval.ttype) = unsigned_type (type);
1797 yylval.ttype = convert (type, yylval.ttype);
1798 TREE_OVERFLOW (yylval.ttype)
1799 = TREE_CONSTANT_OVERFLOW (yylval.ttype) = 0;
1802 TREE_TYPE (yylval.ttype) = type;
1808 if (ISALNUM (c) || c == '.' || c == '_' || c == '$'
1809 || (!flag_traditional && (c == '-' || c == '+')
1810 && (p[-1] == 'e' || p[-1] == 'E')))
1811 error ("missing white space after number `%s'", token_buffer);
1813 value = CONSTANT; break;
1819 register int result = 0;
1820 register int num_chars = 0;
1822 unsigned width = TYPE_PRECISION (char_type_node);
1824 #ifdef MULTIBYTE_CHARS
1825 int longest_char = local_mb_cur_max ();
1826 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
1829 max_chars = TYPE_PRECISION (integer_type_node) / width;
1831 width = WCHAR_TYPE_SIZE;
1838 if (c == '\'' || c == EOF)
1845 c = readescape (&ignore);
1848 if (width < HOST_BITS_PER_INT
1849 && (unsigned) c >= (1 << width))
1850 pedwarn ("escape sequence out of range for character");
1851 #ifdef MAP_CHARACTER
1853 c = MAP_CHARACTER (c);
1859 pedwarn ("ANSI C forbids newline in character constant");
1864 #ifdef MULTIBYTE_CHARS
1868 for (i = 1; i <= longest_char; ++i)
1870 if (i > maxtoken - 4)
1871 extend_token_buffer (token_buffer);
1873 token_buffer[i] = c;
1874 char_len = local_mbtowc (& wc,
1883 /* mbtowc sometimes needs an extra char before accepting */
1888 /* Merge character into result; ignore excess chars. */
1889 for (i = 1; i <= char_len; ++i)
1893 if (width < HOST_BITS_PER_INT)
1894 result = (result << width)
1896 & ((1 << width) - 1));
1898 result = token_buffer[i];
1900 num_chars += char_len;
1908 warning ("Ignoring invalid multibyte character");
1911 #ifdef MAP_CHARACTER
1913 c = MAP_CHARACTER (c);
1916 #else /* ! MULTIBYTE_CHARS */
1917 #ifdef MAP_CHARACTER
1918 c = MAP_CHARACTER (c);
1920 #endif /* ! MULTIBYTE_CHARS */
1925 if (chars_seen == 1) /* only keep the first one */
1930 /* Merge character into result; ignore excess chars. */
1931 num_chars += (width / TYPE_PRECISION (char_type_node));
1932 if (num_chars < max_chars + 1)
1934 if (width < HOST_BITS_PER_INT)
1935 result = (result << width) | (c & ((1 << width) - 1));
1942 error ("malformatted character constant");
1943 else if (chars_seen == 0)
1944 error ("empty character constant");
1945 else if (num_chars > max_chars)
1947 num_chars = max_chars;
1948 error ("character constant too long");
1950 else if (chars_seen != 1 && ! flag_traditional && warn_multichar)
1951 warning ("multi-character character constant");
1953 /* If char type is signed, sign-extend the constant. */
1956 int num_bits = num_chars * width;
1958 /* We already got an error; avoid invalid shift. */
1959 yylval.ttype = build_int_2 (0, 0);
1960 else if (TREE_UNSIGNED (char_type_node)
1961 || ((result >> (num_bits - 1)) & 1) == 0)
1963 = build_int_2 (result & (~(unsigned HOST_WIDE_INT) 0
1964 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1968 = build_int_2 (result | ~(~(unsigned HOST_WIDE_INT) 0
1969 >> (HOST_BITS_PER_WIDE_INT - num_bits)),
1971 TREE_TYPE (yylval.ttype) = integer_type_node;
1975 yylval.ttype = build_int_2 (result, 0);
1976 TREE_TYPE (yylval.ttype) = wchar_type_node;
1986 unsigned width = wide_flag ? WCHAR_TYPE_SIZE
1987 : TYPE_PRECISION (char_type_node);
1988 #ifdef MULTIBYTE_CHARS
1989 int longest_char = local_mb_cur_max ();
1990 (void) local_mbtowc (NULL_PTR, NULL_PTR, 0);
1993 p = token_buffer + 1;
1995 while (c != '"' && c >= 0)
2000 c = readescape (&ignore);
2003 if (width < HOST_BITS_PER_INT
2004 && (unsigned) c >= (1 << width))
2005 pedwarn ("escape sequence out of range for character");
2010 pedwarn ("ANSI C forbids newline in string constant");
2015 #ifdef MULTIBYTE_CHARS
2019 for (i = 0; i < longest_char; ++i)
2021 if (p + i >= token_buffer + maxtoken)
2022 p = extend_token_buffer (p);
2025 char_len = local_mbtowc (& wc, p, i + 1);
2031 warning ("Ignoring invalid multibyte character");
2034 /* mbtowc sometimes needs an extra char before accepting */
2047 #endif /* MULTIBYTE_CHARS */
2050 /* Add this single character into the buffer either as a wchar_t
2051 or as a single byte. */
2054 unsigned width = TYPE_PRECISION (char_type_node);
2055 unsigned bytemask = (1 << width) - 1;
2058 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2059 p = extend_token_buffer (p);
2061 for (byte = 0; byte < WCHAR_BYTES; ++byte)
2064 if (byte >= sizeof (c))
2067 value = (c >> (byte * width)) & bytemask;
2068 if (BYTES_BIG_ENDIAN)
2069 p[WCHAR_BYTES - byte - 1] = value;
2077 if (p >= token_buffer + maxtoken)
2078 p = extend_token_buffer (p);
2086 /* Terminate the string value, either with a single byte zero
2087 or with a wide zero. */
2090 if (p + WCHAR_BYTES > token_buffer + maxtoken)
2091 p = extend_token_buffer (p);
2092 bzero (p, WCHAR_BYTES);
2097 if (p >= token_buffer + maxtoken)
2098 p = extend_token_buffer (p);
2103 error ("Unterminated string constant");
2105 /* We have read the entire constant.
2106 Construct a STRING_CST for the result. */
2110 yylval.ttype = build_string (p - (token_buffer + 1),
2112 TREE_TYPE (yylval.ttype) = wchar_array_type_node;
2117 /* Return an Objective-C @"..." constant string object. */
2118 yylval.ttype = build_objc_string (p - (token_buffer + 1),
2120 TREE_TYPE (yylval.ttype) = char_array_type_node;
2121 value = OBJC_STRING;
2125 yylval.ttype = build_string (p - (token_buffer + 1),
2127 TREE_TYPE (yylval.ttype) = char_array_type_node;
2155 yylval.code = PLUS_EXPR; break;
2157 yylval.code = MINUS_EXPR; break;
2159 yylval.code = BIT_AND_EXPR; break;
2161 yylval.code = BIT_IOR_EXPR; break;
2163 yylval.code = MULT_EXPR; break;
2165 yylval.code = TRUNC_DIV_EXPR; break;
2167 yylval.code = TRUNC_MOD_EXPR; break;
2169 yylval.code = BIT_XOR_EXPR; break;
2171 yylval.code = LSHIFT_EXPR; break;
2173 yylval.code = RSHIFT_EXPR; break;
2175 yylval.code = LT_EXPR; break;
2177 yylval.code = GT_EXPR; break;
2180 token_buffer[1] = c1 = GETC();
2181 token_buffer[2] = 0;
2188 value = ARITHCOMPARE; yylval.code = LE_EXPR; goto done;
2190 value = ARITHCOMPARE; yylval.code = GE_EXPR; goto done;
2192 value = EQCOMPARE; yylval.code = NE_EXPR; goto done;
2194 value = EQCOMPARE; yylval.code = EQ_EXPR; goto done;
2196 value = ASSIGN; goto done;
2202 value = PLUSPLUS; goto done;
2204 value = MINUSMINUS; goto done;
2206 value = ANDAND; goto done;
2208 value = OROR; goto done;
2221 { value = POINTSAT; goto done; }
2225 { value = ']'; goto done; }
2229 { value = '{'; indent_level++; goto done; }
2231 { value = '['; goto done; }
2235 { value = '}'; indent_level--; goto done; }
2239 token_buffer[1] = 0;
2241 if ((c == '<') || (c == '>'))
2242 value = ARITHCOMPARE;
2248 /* Don't make yyparse think this is eof. */
2267 /* yylloc.last_line = lineno; */
2272 /* Sets the value of the 'yydebug' variable to VALUE.
2273 This is a function so we don't have to have YYDEBUG defined
2274 in order to build the compiler. */
2283 warning ("YYDEBUG not defined.");