1 /* Mainly the interface between cpplib and the C front ends.
2 Copyright (C) 1987, 1988, 1989, 1992, 1994, 1995, 1996, 1997
3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
42 #include "splay-tree.h"
45 #ifdef MULTIBYTE_CHARS
48 #endif /* MULTIBYTE_CHARS */
50 /* The current line map. */
51 static const struct line_map *map;
53 /* The line used to refresh the lineno global variable after each token. */
54 static unsigned int src_lineno;
56 /* We may keep statistics about how long which files took to compile. */
57 static int header_time, body_time;
58 static splay_tree file_info_tree;
60 /* File used for outputting assembler code. */
61 extern FILE *asm_out_file;
63 #undef WCHAR_TYPE_SIZE
64 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
66 /* Number of bytes in a wide character. */
67 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
69 int pending_lang_change; /* If we need to switch languages - C++ only */
70 int c_header_level; /* depth in C headers - C++ only */
72 /* Nonzero tells yylex to ignore \ in string constants. */
73 static int ignore_escape_flag;
75 static tree interpret_integer PARAMS ((const cpp_token *, unsigned int));
76 static tree interpret_float PARAMS ((const cpp_token *, unsigned int));
77 static enum integer_type_kind
78 narrowest_unsigned_type PARAMS ((tree, unsigned int));
79 static enum integer_type_kind
80 narrowest_signed_type PARAMS ((tree, unsigned int));
81 static tree lex_string PARAMS ((const unsigned char *, unsigned int,
83 static tree lex_charconst PARAMS ((const cpp_token *));
84 static void update_header_times PARAMS ((const char *));
85 static int dump_one_header PARAMS ((splay_tree_node, void *));
86 static void cb_line_change PARAMS ((cpp_reader *, const cpp_token *, int));
87 static void cb_ident PARAMS ((cpp_reader *, unsigned int,
89 static void cb_def_pragma PARAMS ((cpp_reader *, unsigned int));
90 static void cb_define PARAMS ((cpp_reader *, unsigned int,
92 static void cb_undef PARAMS ((cpp_reader *, unsigned int,
98 struct cpp_callbacks *cb;
99 struct c_fileinfo *toplevel;
101 /* Set up filename timing. Must happen before cpp_read_main_file. */
102 file_info_tree = splay_tree_new ((splay_tree_compare_fn)strcmp,
104 (splay_tree_delete_value_fn)free);
105 toplevel = get_fileinfo ("<top level>");
106 if (flag_detailed_statistics)
109 body_time = get_run_time ();
110 toplevel->time = body_time;
113 cb = cpp_get_callbacks (parse_in);
115 cb->line_change = cb_line_change;
116 cb->ident = cb_ident;
117 cb->def_pragma = cb_def_pragma;
118 cb->valid_pch = c_common_valid_pch;
119 cb->read_pch = c_common_read_pch;
121 /* Set the debug callbacks if we can use them. */
122 if (debug_info_level == DINFO_LEVEL_VERBOSE
123 && (write_symbols == DWARF_DEBUG || write_symbols == DWARF2_DEBUG
124 || write_symbols == VMS_AND_DWARF2_DEBUG))
126 cb->define = cb_define;
127 cb->undef = cb_undef;
136 struct c_fileinfo *fi;
138 n = splay_tree_lookup (file_info_tree, (splay_tree_key) name);
140 return (struct c_fileinfo *) n->value;
142 fi = (struct c_fileinfo *) xmalloc (sizeof (struct c_fileinfo));
144 fi->interface_only = 0;
145 fi->interface_unknown = 1;
146 splay_tree_insert (file_info_tree, (splay_tree_key) name,
147 (splay_tree_value) fi);
152 update_header_times (name)
155 /* Changing files again. This means currently collected time
156 is charged against header time, and body time starts back at 0. */
157 if (flag_detailed_statistics)
159 int this_time = get_run_time ();
160 struct c_fileinfo *file = get_fileinfo (name);
161 header_time += this_time - body_time;
162 file->time += this_time - body_time;
163 body_time = this_time;
168 dump_one_header (n, dummy)
170 void *dummy ATTRIBUTE_UNUSED;
172 print_time ((const char *) n->key,
173 ((struct c_fileinfo *) n->value)->time);
178 dump_time_statistics ()
180 struct c_fileinfo *file = get_fileinfo (input_filename);
181 int this_time = get_run_time ();
182 file->time += this_time - body_time;
184 fprintf (stderr, "\n******\n");
185 print_time ("header files (total)", header_time);
186 print_time ("main file (total)", this_time - body_time);
187 fprintf (stderr, "ratio = %g : 1\n",
188 (double)header_time / (double)(this_time - body_time));
189 fprintf (stderr, "\n******\n");
191 splay_tree_foreach (file_info_tree, dump_one_header, 0);
195 cb_ident (pfile, line, str)
196 cpp_reader *pfile ATTRIBUTE_UNUSED;
197 unsigned int line ATTRIBUTE_UNUSED;
198 const cpp_string *str ATTRIBUTE_UNUSED;
200 #ifdef ASM_OUTPUT_IDENT
203 /* Convert escapes in the string. */
204 tree value ATTRIBUTE_UNUSED = lex_string (str->text, str->len, 0);
205 ASM_OUTPUT_IDENT (asm_out_file, TREE_STRING_POINTER (value));
210 /* Called at the start of every non-empty line. TOKEN is the first
211 lexed token on the line. Used for diagnostic line numbers. */
213 cb_line_change (pfile, token, parsing_args)
214 cpp_reader *pfile ATTRIBUTE_UNUSED;
215 const cpp_token *token;
216 int parsing_args ATTRIBUTE_UNUSED;
218 src_lineno = SOURCE_LINE (map, token->line);
222 fe_file_change (new_map)
223 const struct line_map *new_map;
225 unsigned int to_line = SOURCE_LINE (new_map, new_map->to_line);
227 if (new_map->reason == LC_ENTER)
229 /* Don't stack the main buffer on the input stack;
230 we already did in compile_file. */
232 main_input_filename = new_map->to_file;
235 int included_at = SOURCE_LINE (new_map - 1, new_map->from_line - 1);
237 lineno = included_at;
238 push_srcloc (new_map->to_file, 1);
239 (*debug_hooks->start_source_file) (included_at, new_map->to_file);
240 #ifndef NO_IMPLICIT_EXTERN_C
243 else if (new_map->sysp == 2)
246 ++pending_lang_change;
251 else if (new_map->reason == LC_LEAVE)
253 #ifndef NO_IMPLICIT_EXTERN_C
254 if (c_header_level && --c_header_level == 0)
256 if (new_map->sysp == 2)
257 warning ("badly nested C headers from preprocessor");
258 --pending_lang_change;
263 (*debug_hooks->end_source_file) (to_line);
266 update_header_times (new_map->to_file);
267 in_system_header = new_map->sysp != 0;
268 input_filename = new_map->to_file;
273 extract_interface_info ();
277 cb_def_pragma (pfile, line)
281 /* Issue a warning message if we have been asked to do so. Ignore
282 unknown pragmas in system headers unless an explicit
283 -Wunknown-pragmas has been given. */
284 if (warn_unknown_pragmas > in_system_header)
286 const unsigned char *space, *name;
289 space = name = (const unsigned char *) "";
290 s = cpp_get_token (pfile);
291 if (s->type != CPP_EOF)
293 space = cpp_token_as_text (pfile, s);
294 s = cpp_get_token (pfile);
295 if (s->type == CPP_NAME)
296 name = cpp_token_as_text (pfile, s);
299 lineno = SOURCE_LINE (map, line);
300 warning ("ignoring #pragma %s %s", space, name);
304 /* #define callback for DWARF and DWARF2 debug info. */
306 cb_define (pfile, line, node)
311 (*debug_hooks->define) (SOURCE_LINE (map, line),
312 (const char *) cpp_macro_definition (pfile, node));
315 /* #undef callback for DWARF and DWARF2 debug info. */
317 cb_undef (pfile, line, node)
318 cpp_reader *pfile ATTRIBUTE_UNUSED;
322 (*debug_hooks->undef) (SOURCE_LINE (map, line),
323 (const char *) NODE_NAME (node));
330 const cpp_token *tok;
333 timevar_push (TV_CPP);
335 tok = cpp_get_token (parse_in);
336 while (tok->type == CPP_PADDING);
337 timevar_pop (TV_CPP);
339 /* The C++ front end does horrible things with the current line
340 number. To ensure an accurate line number, we must reset it
341 every time we return a token. */
348 error ("stray token \"%s\" in program",
349 cpp_token_as_text (parse_in, tok));
353 *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node));
358 unsigned int flags = cpp_classify_number (parse_in, tok);
360 switch (flags & CPP_N_CATEGORY)
363 /* cpplib has issued an error. */
364 *value = error_mark_node;
368 *value = interpret_integer (tok, flags);
372 *value = interpret_float (tok, flags);
383 *value = lex_charconst (tok);
388 *value = lex_string (tok->val.str.text, tok->val.str.len,
389 tok->type == CPP_WSTRING);
392 /* These tokens should not be visible outside cpplib. */
393 case CPP_HEADER_NAME:
404 /* Returns the narrowest C-visible unsigned type, starting with the
405 minimum specified by FLAGS, that can fit VALUE, or itk_none if
407 static enum integer_type_kind
408 narrowest_unsigned_type (value, flags)
412 enum integer_type_kind itk;
414 if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
415 itk = itk_unsigned_int;
416 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
417 itk = itk_unsigned_long;
419 itk = itk_unsigned_long_long;
421 /* int_fits_type_p must think the type of its first argument is
422 wider than its second argument, or it won't do the proper check. */
423 TREE_TYPE (value) = widest_unsigned_literal_type_node;
425 for (; itk < itk_none; itk += 2 /* skip unsigned types */)
426 if (int_fits_type_p (value, integer_types[itk]))
432 /* Ditto, but narrowest signed type. */
433 static enum integer_type_kind
434 narrowest_signed_type (value, flags)
438 enum integer_type_kind itk;
440 if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
442 else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
447 /* int_fits_type_p must think the type of its first argument is
448 wider than its second argument, or it won't do the proper check. */
449 TREE_TYPE (value) = widest_unsigned_literal_type_node;
451 for (; itk < itk_none; itk += 2 /* skip signed types */)
452 if (int_fits_type_p (value, integer_types[itk]))
458 /* Interpret TOKEN, an integer with FLAGS as classified by cpplib. */
460 interpret_integer (token, flags)
461 const cpp_token *token;
465 enum integer_type_kind itk;
467 cpp_options *options = cpp_get_options (parse_in);
469 integer = cpp_interpret_integer (parse_in, token, flags);
470 integer = cpp_num_sign_extend (integer, options->precision);
471 value = build_int_2_wide (integer.low, integer.high);
473 /* The type of a constant with a U suffix is straightforward. */
474 if (flags & CPP_N_UNSIGNED)
475 itk = narrowest_unsigned_type (value, flags);
478 /* The type of a potentially-signed integer constant varies
479 depending on the base it's in, the standard in use, and the
481 enum integer_type_kind itk_u = narrowest_unsigned_type (value, flags);
482 enum integer_type_kind itk_s = narrowest_signed_type (value, flags);
484 /* In both C89 and C99, octal and hex constants may be signed or
485 unsigned, whichever fits tighter. We do not warn about this
486 choice differing from the traditional choice, as the constant
487 is probably a bit pattern and either way will work. */
488 if ((flags & CPP_N_RADIX) != CPP_N_DECIMAL)
489 itk = MIN (itk_u, itk_s);
492 /* In C99, decimal constants are always signed.
493 In C89, decimal constants that don't fit in long have
494 undefined behavior; we try to make them unsigned long.
495 In GCC's extended C89, that last is true of decimal
496 constants that don't fit in long long, too. */
499 if (itk_s > itk_u && itk_s > itk_long)
503 if (itk_u < itk_unsigned_long)
504 itk_u = itk_unsigned_long;
506 warning ("this decimal constant is unsigned only in ISO C90");
508 else if (warn_traditional)
509 warning ("this decimal constant would be unsigned in ISO C90");
515 /* cpplib has already issued a warning for overflow. */
516 type = ((flags & CPP_N_UNSIGNED)
517 ? widest_unsigned_literal_type_node
518 : widest_integer_literal_type_node);
520 type = integer_types[itk];
522 if (itk > itk_unsigned_long
523 && (flags & CPP_N_WIDTH) != CPP_N_LARGE
524 && ! in_system_header && ! flag_isoc99)
525 pedwarn ("integer constant is too large for \"%s\" type",
526 (flags & CPP_N_UNSIGNED) ? "unsigned long" : "long");
528 TREE_TYPE (value) = type;
530 /* Convert imaginary to a complex type. */
531 if (flags & CPP_N_IMAGINARY)
532 value = build_complex (NULL_TREE, convert (type, integer_zero_node), value);
537 /* Interpret TOKEN, a floating point number with FLAGS as classified
540 interpret_float (token, flags)
541 const cpp_token *token;
546 REAL_VALUE_TYPE real;
549 const char *typename;
551 /* FIXME: make %T work in error/warning, then we don't need typename. */
552 if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
554 type = long_double_type_node;
555 typename = "long double";
557 else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL
558 || flag_single_precision_constant)
560 type = float_type_node;
565 type = double_type_node;
569 /* Copy the constant to a nul-terminated buffer. If the constant
570 has any suffixes, cut them off; REAL_VALUE_ATOF/ REAL_VALUE_HTOF
571 can't handle them. */
572 copylen = token->val.str.len;
573 if ((flags & CPP_N_WIDTH) != CPP_N_MEDIUM)
574 /* Must be an F or L suffix. */
576 if (flags & CPP_N_IMAGINARY)
580 copy = alloca (copylen + 1);
581 memcpy (copy, token->val.str.text, copylen);
582 copy[copylen] = '\0';
584 real_from_string (&real, copy);
585 real_convert (&real, TYPE_MODE (type), &real);
587 /* A diagnostic is required for "soft" overflow by some ISO C
588 testsuites. This is not pedwarn, because some people don't want
590 ??? That's a dubious reason... is this a mandatory diagnostic or
591 isn't it? -- zw, 2001-08-21. */
592 if (REAL_VALUE_ISINF (real) && pedantic)
593 warning ("floating constant exceeds range of \"%s\"", typename);
595 /* Create a node with determined type and value. */
596 value = build_real (type, real);
597 if (flags & CPP_N_IMAGINARY)
598 value = build_complex (NULL_TREE, convert (type, integer_zero_node), value);
604 lex_string (str, len, wide)
605 const unsigned char *str;
610 char *buf = alloca ((len + 1) * (wide ? WCHAR_BYTES : 1));
612 const unsigned char *p = str, *limit = str + len;
615 #ifdef MULTIBYTE_CHARS
616 /* Reset multibyte conversion state. */
617 (void) local_mbtowc (NULL, NULL, 0);
622 #ifdef MULTIBYTE_CHARS
626 char_len = local_mbtowc (&wc, (const char *) p, limit - p);
629 warning ("ignoring invalid multibyte character");
642 if (c == '\\' && !ignore_escape_flag)
643 c = cpp_parse_escape (parse_in, &p, limit, wide);
645 /* Add this single character into the buffer either as a wchar_t,
646 a multibyte sequence, or as a single byte. */
649 unsigned charwidth = TYPE_PRECISION (char_type_node);
650 unsigned bytemask = (1 << charwidth) - 1;
653 for (byte = 0; byte < WCHAR_BYTES; ++byte)
656 if (byte >= (int) sizeof (c))
659 n = (c >> (byte * charwidth)) & bytemask;
660 if (BYTES_BIG_ENDIAN)
661 q[WCHAR_BYTES - byte - 1] = n;
667 #ifdef MULTIBYTE_CHARS
668 else if (char_len > 1)
670 /* We're dealing with a multibyte character. */
671 for ( ; char_len >0; --char_len)
673 *q++ = *(p - char_len);
683 /* Terminate the string value, either with a single byte zero
684 or with a wide zero. */
688 memset (q, 0, WCHAR_BYTES);
696 value = build_string (q - buf, buf);
699 TREE_TYPE (value) = wchar_array_type_node;
701 TREE_TYPE (value) = char_array_type_node;
705 /* Converts a (possibly wide) character constant token into a tree. */
707 lex_charconst (token)
708 const cpp_token *token;
712 unsigned int chars_seen;
715 result = cpp_interpret_charconst (parse_in, token,
716 &chars_seen, &unsignedp);
718 /* Cast to cppchar_signed_t to get correct sign-extension of RESULT
719 before possibly widening to HOST_WIDE_INT for build_int_2. */
720 if (unsignedp || (cppchar_signed_t) result >= 0)
721 value = build_int_2 (result, 0);
723 value = build_int_2 ((cppchar_signed_t) result, -1);
725 if (token->type == CPP_WCHAR)
726 type = wchar_type_node;
727 /* In C, a character constant has type 'int'.
728 In C++ 'char', but multi-char charconsts have type 'int'. */
729 else if ((c_language == clk_c) || chars_seen > 1)
730 type = integer_type_node;
732 type = char_type_node;
734 TREE_TYPE (value) = type;