OSDN Git Service

* c-lex.c (c_lex): Handle CPP_OTHER differently.
[pf3gnuchains/gcc-fork.git] / gcc / c-lex.c
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.
4
5 This file is part of GCC.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26
27 #include "real.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "expr.h"
31 #include "input.h"
32 #include "output.h"
33 #include "c-tree.h"
34 #include "c-common.h"
35 #include "flags.h"
36 #include "timevar.h"
37 #include "cpplib.h"
38 #include "c-pragma.h"
39 #include "toplev.h"
40 #include "intl.h"
41 #include "tm_p.h"
42 #include "splay-tree.h"
43 #include "debug.h"
44
45 #ifdef MULTIBYTE_CHARS
46 #include "mbchar.h"
47 #include <locale.h>
48 #endif /* MULTIBYTE_CHARS */
49
50 /* The current line map.  */
51 static const struct line_map *map;
52
53 /* The line used to refresh the lineno global variable after each token.  */
54 static unsigned int src_lineno;
55
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;
59
60 /* File used for outputting assembler code.  */
61 extern FILE *asm_out_file;
62
63 #undef WCHAR_TYPE_SIZE
64 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
65
66 /* Number of bytes in a wide character.  */
67 #define WCHAR_BYTES (WCHAR_TYPE_SIZE / BITS_PER_UNIT)
68
69 int pending_lang_change; /* If we need to switch languages - C++ only */
70 int c_header_level;      /* depth in C headers - C++ only */
71
72 /* Nonzero tells yylex to ignore \ in string constants.  */
73 static int ignore_escape_flag;
74
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,
82                                          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,
88                                          const cpp_string *));
89 static void cb_def_pragma       PARAMS ((cpp_reader *, unsigned int));
90 static void cb_define           PARAMS ((cpp_reader *, unsigned int,
91                                          cpp_hashnode *));
92 static void cb_undef            PARAMS ((cpp_reader *, unsigned int,
93                                          cpp_hashnode *));
94 \f
95 void
96 init_c_lex ()
97 {
98   struct cpp_callbacks *cb;
99   struct c_fileinfo *toplevel;
100
101   /* Set up filename timing.  Must happen before cpp_read_main_file.  */
102   file_info_tree = splay_tree_new ((splay_tree_compare_fn)strcmp,
103                                    0,
104                                    (splay_tree_delete_value_fn)free);
105   toplevel = get_fileinfo ("<top level>");
106   if (flag_detailed_statistics)
107     {
108       header_time = 0;
109       body_time = get_run_time ();
110       toplevel->time = body_time;
111     }
112   
113   cb = cpp_get_callbacks (parse_in);
114
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;
120
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))
125     {
126       cb->define = cb_define;
127       cb->undef = cb_undef;
128     }
129 }
130
131 struct c_fileinfo *
132 get_fileinfo (name)
133      const char *name;
134 {
135   splay_tree_node n;
136   struct c_fileinfo *fi;
137
138   n = splay_tree_lookup (file_info_tree, (splay_tree_key) name);
139   if (n)
140     return (struct c_fileinfo *) n->value;
141
142   fi = (struct c_fileinfo *) xmalloc (sizeof (struct c_fileinfo));
143   fi->time = 0;
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);
148   return fi;
149 }
150
151 static void
152 update_header_times (name)
153      const char *name;
154 {
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)
158     {
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;
164     }
165 }
166
167 static int
168 dump_one_header (n, dummy)
169      splay_tree_node n;
170      void *dummy ATTRIBUTE_UNUSED;
171 {
172   print_time ((const char *) n->key,
173               ((struct c_fileinfo *) n->value)->time);
174   return 0;
175 }
176
177 void
178 dump_time_statistics ()
179 {
180   struct c_fileinfo *file = get_fileinfo (input_filename);
181   int this_time = get_run_time ();
182   file->time += this_time - body_time;
183
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");
190
191   splay_tree_foreach (file_info_tree, dump_one_header, 0);
192 }
193
194 static void
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;
199 {
200 #ifdef ASM_OUTPUT_IDENT
201   if (! flag_no_ident)
202     {
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));
206     }
207 #endif
208 }
209
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.  */
212 static void
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;
217 {
218   src_lineno = SOURCE_LINE (map, token->line);
219 }
220
221 void
222 fe_file_change (new_map)
223      const struct line_map *new_map;
224 {
225   unsigned int to_line = SOURCE_LINE (new_map, new_map->to_line);
226
227   if (new_map->reason == LC_ENTER)
228     {
229       /* Don't stack the main buffer on the input stack;
230          we already did in compile_file.  */
231       if (map == NULL)
232         main_input_filename = new_map->to_file;
233       else
234         {
235           int included_at = SOURCE_LINE (new_map - 1, new_map->from_line - 1);
236
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
241           if (c_header_level)
242             ++c_header_level;
243           else if (new_map->sysp == 2)
244             {
245               c_header_level = 1;
246               ++pending_lang_change;
247             }
248 #endif
249         }
250     }
251   else if (new_map->reason == LC_LEAVE)
252     {
253 #ifndef NO_IMPLICIT_EXTERN_C
254       if (c_header_level && --c_header_level == 0)
255         {
256           if (new_map->sysp == 2)
257             warning ("badly nested C headers from preprocessor");
258           --pending_lang_change;
259         }
260 #endif
261       pop_srcloc ();
262       
263       (*debug_hooks->end_source_file) (to_line);
264     }
265
266   update_header_times (new_map->to_file);
267   in_system_header = new_map->sysp != 0;
268   input_filename = new_map->to_file;
269   lineno = to_line;
270   map = new_map;
271
272   /* Hook for C++.  */
273   extract_interface_info ();
274 }
275
276 static void
277 cb_def_pragma (pfile, line)
278      cpp_reader *pfile;
279      unsigned int line;
280 {
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)
285     {
286       const unsigned char *space, *name;
287       const cpp_token *s;
288
289       space = name = (const unsigned char *) "";
290       s = cpp_get_token (pfile);
291       if (s->type != CPP_EOF)
292         {
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);
297         }
298
299       lineno = SOURCE_LINE (map, line);
300       warning ("ignoring #pragma %s %s", space, name);
301     }
302 }
303
304 /* #define callback for DWARF and DWARF2 debug info.  */
305 static void
306 cb_define (pfile, line, node)
307      cpp_reader *pfile;
308      unsigned int line;
309      cpp_hashnode *node;
310 {
311   (*debug_hooks->define) (SOURCE_LINE (map, line),
312                           (const char *) cpp_macro_definition (pfile, node));
313 }
314
315 /* #undef callback for DWARF and DWARF2 debug info.  */
316 static void
317 cb_undef (pfile, line, node)
318      cpp_reader *pfile ATTRIBUTE_UNUSED;
319      unsigned int line;
320      cpp_hashnode *node;
321 {
322   (*debug_hooks->undef) (SOURCE_LINE (map, line),
323                          (const char *) NODE_NAME (node));
324 }
325 \f
326 int
327 c_lex (value)
328      tree *value;
329 {
330   const cpp_token *tok;
331
332   retry:
333   timevar_push (TV_CPP);
334   do
335     tok = cpp_get_token (parse_in);
336   while (tok->type == CPP_PADDING);
337   timevar_pop (TV_CPP);
338
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.  */
342   lineno = src_lineno;
343
344   *value = NULL_TREE;
345   switch (tok->type)
346     {
347     case CPP_OTHER:
348       error ("stray token \"%s\" in program",
349              cpp_token_as_text (parse_in, tok));
350       goto retry;
351       
352     case CPP_NAME:
353       *value = HT_IDENT_TO_GCC_IDENT (HT_NODE (tok->val.node));
354       break;
355
356     case CPP_NUMBER:
357       {
358         unsigned int flags = cpp_classify_number (parse_in, tok);
359
360         switch (flags & CPP_N_CATEGORY)
361           {
362           case CPP_N_INVALID:
363             /* cpplib has issued an error.  */
364             *value = error_mark_node;
365             break;
366
367           case CPP_N_INTEGER:
368             *value = interpret_integer (tok, flags);
369             break;
370
371           case CPP_N_FLOATING:
372             *value = interpret_float (tok, flags);
373             break;
374
375           default:
376             abort ();
377           }
378       }
379       break;
380
381     case CPP_CHAR:
382     case CPP_WCHAR:
383       *value = lex_charconst (tok);
384       break;
385
386     case CPP_STRING:
387     case CPP_WSTRING:
388       *value = lex_string (tok->val.str.text, tok->val.str.len,
389                            tok->type == CPP_WSTRING);
390       break;
391
392       /* These tokens should not be visible outside cpplib.  */
393     case CPP_HEADER_NAME:
394     case CPP_COMMENT:
395     case CPP_MACRO_ARG:
396       abort ();
397
398     default: break;
399     }
400
401   return tok->type;
402 }
403
404 /* Returns the narrowest C-visible unsigned type, starting with the
405    minimum specified by FLAGS, that can fit VALUE, or itk_none if
406    there isn't one.  */
407 static enum integer_type_kind
408 narrowest_unsigned_type (value, flags)
409      tree value;
410      unsigned int flags;
411 {
412   enum integer_type_kind itk;
413
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;
418   else
419     itk = itk_unsigned_long_long;
420
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;
424
425   for (; itk < itk_none; itk += 2 /* skip unsigned types */)
426     if (int_fits_type_p (value, integer_types[itk]))
427       return itk;
428
429   return itk_none;
430 }
431
432 /* Ditto, but narrowest signed type.  */
433 static enum integer_type_kind
434 narrowest_signed_type (value, flags)
435      tree value;
436      unsigned int flags;
437 {
438   enum integer_type_kind itk;
439
440   if ((flags & CPP_N_WIDTH) == CPP_N_SMALL)
441     itk = itk_int;
442   else if ((flags & CPP_N_WIDTH) == CPP_N_MEDIUM)
443     itk = itk_long;
444   else
445     itk = itk_long_long;
446
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;
450
451   for (; itk < itk_none; itk += 2 /* skip signed types */)
452     if (int_fits_type_p (value, integer_types[itk]))
453       return itk;
454
455   return itk_none;
456 }
457
458 /* Interpret TOKEN, an integer with FLAGS as classified by cpplib.  */
459 static tree
460 interpret_integer (token, flags)
461      const cpp_token *token;
462      unsigned int flags;
463 {
464   tree value, type;
465   enum integer_type_kind itk;
466   cpp_num integer;
467   cpp_options *options = cpp_get_options (parse_in);
468
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);
472
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);
476   else
477     {
478       /* The type of a potentially-signed integer constant varies
479          depending on the base it's in, the standard in use, and the
480          length suffixes.  */
481       enum integer_type_kind itk_u = narrowest_unsigned_type (value, flags);
482       enum integer_type_kind itk_s = narrowest_signed_type (value, flags);
483
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);
490       else
491         {
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.  */
497
498           itk = itk_s;
499           if (itk_s > itk_u && itk_s > itk_long)
500             {
501               if (!flag_isoc99)
502                 {
503                   if (itk_u < itk_unsigned_long)
504                     itk_u = itk_unsigned_long;
505                   itk = itk_u;
506                   warning ("this decimal constant is unsigned only in ISO C90");
507                 }
508               else if (warn_traditional)
509                 warning ("this decimal constant would be unsigned in ISO C90");
510             }
511         }
512     }
513
514   if (itk == itk_none)
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);
519   else
520     type = integer_types[itk];
521
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");
527
528   TREE_TYPE (value) = type;
529
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);
533
534   return value;
535 }
536
537 /* Interpret TOKEN, a floating point number with FLAGS as classified
538    by cpplib.  */
539 static tree
540 interpret_float (token, flags)
541      const cpp_token *token;
542      unsigned int flags;
543 {
544   tree type;
545   tree value;
546   REAL_VALUE_TYPE real;
547   char *copy;
548   size_t copylen;
549   const char *typename;
550
551   /* FIXME: make %T work in error/warning, then we don't need typename.  */
552   if ((flags & CPP_N_WIDTH) == CPP_N_LARGE)
553     {
554       type = long_double_type_node;
555       typename = "long double";
556     }
557   else if ((flags & CPP_N_WIDTH) == CPP_N_SMALL
558            || flag_single_precision_constant)
559     {
560       type = float_type_node;
561       typename = "float";
562     }
563   else
564     {
565       type = double_type_node;
566       typename = "double";
567     }
568
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.  */
575     copylen--;
576   if (flags & CPP_N_IMAGINARY)
577     /* I or J suffix.  */
578     copylen--;
579
580   copy = alloca (copylen + 1);
581   memcpy (copy, token->val.str.text, copylen);
582   copy[copylen] = '\0';
583
584   real_from_string (&real, copy);
585   real_convert (&real, TYPE_MODE (type), &real);
586
587   /* A diagnostic is required for "soft" overflow by some ISO C
588      testsuites.  This is not pedwarn, because some people don't want
589      an error for this.
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);
594
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);
599
600   return value;
601 }
602
603 static tree
604 lex_string (str, len, wide)
605      const unsigned char *str;
606      unsigned int len;
607      int wide;
608 {
609   tree value;
610   char *buf = alloca ((len + 1) * (wide ? WCHAR_BYTES : 1));
611   char *q = buf;
612   const unsigned char *p = str, *limit = str + len;
613   cppchar_t c;
614
615 #ifdef MULTIBYTE_CHARS
616   /* Reset multibyte conversion state.  */
617   (void) local_mbtowc (NULL, NULL, 0);
618 #endif
619
620   while (p < limit)
621     {
622 #ifdef MULTIBYTE_CHARS
623       wchar_t wc;
624       int char_len;
625
626       char_len = local_mbtowc (&wc, (const char *) p, limit - p);
627       if (char_len == -1)
628         {
629           warning ("ignoring invalid multibyte character");
630           char_len = 1;
631           c = *p++;
632         }
633       else
634         {
635           p += char_len;
636           c = wc;
637         }
638 #else
639       c = *p++;
640 #endif
641
642       if (c == '\\' && !ignore_escape_flag)
643         c = cpp_parse_escape (parse_in, &p, limit, wide);
644         
645       /* Add this single character into the buffer either as a wchar_t,
646          a multibyte sequence, or as a single byte.  */
647       if (wide)
648         {
649           unsigned charwidth = TYPE_PRECISION (char_type_node);
650           unsigned bytemask = (1 << charwidth) - 1;
651           int byte;
652
653           for (byte = 0; byte < WCHAR_BYTES; ++byte)
654             {
655               int n;
656               if (byte >= (int) sizeof (c))
657                 n = 0;
658               else
659                 n = (c >> (byte * charwidth)) & bytemask;
660               if (BYTES_BIG_ENDIAN)
661                 q[WCHAR_BYTES - byte - 1] = n;
662               else
663                 q[byte] = n;
664             }
665           q += WCHAR_BYTES;
666         }
667 #ifdef MULTIBYTE_CHARS
668       else if (char_len > 1)
669         {
670           /* We're dealing with a multibyte character.  */
671           for ( ; char_len >0; --char_len)
672             {
673               *q++ = *(p - char_len);
674             }
675         }
676 #endif
677       else
678         {
679           *q++ = c;
680         }
681     }
682
683   /* Terminate the string value, either with a single byte zero
684      or with a wide zero.  */
685
686   if (wide)
687     {
688       memset (q, 0, WCHAR_BYTES);
689       q += WCHAR_BYTES;
690     }
691   else
692     {
693       *q++ = '\0';
694     }
695
696   value = build_string (q - buf, buf);
697
698   if (wide)
699     TREE_TYPE (value) = wchar_array_type_node;
700   else
701     TREE_TYPE (value) = char_array_type_node;
702   return value;
703 }
704
705 /* Converts a (possibly wide) character constant token into a tree.  */
706 static tree
707 lex_charconst (token)
708      const cpp_token *token;
709 {
710   cppchar_t result;
711   tree type, value;
712   unsigned int chars_seen;
713   int unsignedp;
714
715   result = cpp_interpret_charconst (parse_in, token,
716                                     &chars_seen, &unsignedp);
717
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);
722   else
723     value = build_int_2 ((cppchar_signed_t) result, -1);
724
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;
731   else
732     type = char_type_node;
733
734   TREE_TYPE (value) = type;
735   return value;
736 }