OSDN Git Service

2008-08-21 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cp / lex.c
1 /* Separate lexical analyzer for GNU C++.
2    Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
4    Free Software Foundation, Inc.
5    Hacked by Michael Tiemann (tiemann@cygnus.com)
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23
24 /* This file is the lexical analyzer for GNU C++.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "input.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "cpplib.h"
34 #include "flags.h"
35 #include "c-pragma.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "tm_p.h"
39 #include "timevar.h"
40
41 static int interface_strcmp (const char *);
42 static void init_cp_pragma (void);
43
44 static tree parse_strconst_pragma (const char *, int);
45 static void handle_pragma_vtable (cpp_reader *);
46 static void handle_pragma_unit (cpp_reader *);
47 static void handle_pragma_interface (cpp_reader *);
48 static void handle_pragma_implementation (cpp_reader *);
49 static void handle_pragma_java_exceptions (cpp_reader *);
50
51 static void init_operators (void);
52 static void copy_lang_type (tree);
53
54 /* A constraint that can be tested at compile time.  */
55 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
56
57 /* Functions and data structures for #pragma interface.
58
59    `#pragma implementation' means that the main file being compiled
60    is considered to implement (provide) the classes that appear in
61    its main body.  I.e., if this is file "foo.cc", and class `bar'
62    is defined in "foo.cc", then we say that "foo.cc implements bar".
63
64    All main input files "implement" themselves automagically.
65
66    `#pragma interface' means that unless this file (of the form "foo.h"
67    is not presently being included by file "foo.cc", the
68    CLASSTYPE_INTERFACE_ONLY bit gets set.  The effect is that none
69    of the vtables nor any of the inline functions defined in foo.h
70    will ever be output.
71
72    There are cases when we want to link files such as "defs.h" and
73    "main.cc".  In this case, we give "defs.h" a `#pragma interface',
74    and "main.cc" has `#pragma implementation "defs.h"'.  */
75
76 struct impl_files
77 {
78   const char *filename;
79   struct impl_files *next;
80 };
81
82 static struct impl_files *impl_file_chain;
83
84 \f
85 void
86 cxx_finish (void)
87 {
88   c_common_finish ();
89 }
90
91 /* A mapping from tree codes to operator name information.  */
92 operator_name_info_t operator_name_info[(int) MAX_TREE_CODES];
93 /* Similar, but for assignment operators.  */
94 operator_name_info_t assignment_operator_name_info[(int) MAX_TREE_CODES];
95
96 /* Initialize data structures that keep track of operator names.  */
97
98 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
99  CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
100 #include "operators.def"
101 #undef DEF_OPERATOR
102
103 static void
104 init_operators (void)
105 {
106   tree identifier;
107   char buffer[256];
108   struct operator_name_info_t *oni;
109
110 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)                   \
111   sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
112   identifier = get_identifier (buffer);                                     \
113   IDENTIFIER_OPNAME_P (identifier) = 1;                                     \
114                                                                             \
115   oni = (ASSN_P                                                             \
116          ? &assignment_operator_name_info[(int) CODE]                       \
117          : &operator_name_info[(int) CODE]);                                \
118   oni->identifier = identifier;                                             \
119   oni->name = NAME;                                                         \
120   oni->mangled_name = MANGLING;                                             \
121   oni->arity = ARITY;
122
123 #include "operators.def"
124 #undef DEF_OPERATOR
125
126   operator_name_info[(int) ERROR_MARK].identifier
127     = get_identifier ("<invalid operator>");
128
129   /* Handle some special cases.  These operators are not defined in
130      the language, but can be produced internally.  We may need them
131      for error-reporting.  (Eventually, we should ensure that this
132      does not happen.  Error messages involving these operators will
133      be confusing to users.)  */
134
135   operator_name_info [(int) INIT_EXPR].name
136     = operator_name_info [(int) MODIFY_EXPR].name;
137   operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
138   operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
139   operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
140   operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
141   operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
142   operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
143   operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
144   operator_name_info [(int) ABS_EXPR].name = "abs";
145   operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
146   operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
147   operator_name_info [(int) RANGE_EXPR].name = "...";
148   operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
149
150   assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
151     = "(exact /=)";
152   assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
153     = "(ceiling /=)";
154   assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
155     = "(floor /=)";
156   assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
157     = "(round /=)";
158   assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
159     = "(ceiling %=)";
160   assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
161     = "(floor %=)";
162   assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
163     = "(round %=)";
164 }
165
166 /* Initialize the reserved words.  */
167
168 void
169 init_reswords (void)
170 {
171   unsigned int i;
172   tree id;
173   int mask = 0;
174
175   mask |= D_CONLY;
176   if (cxx_dialect != cxx0x)
177     mask |= D_CXX0X;
178   if (flag_no_asm)
179     mask |= D_ASM | D_EXT;
180   if (flag_no_gnu_keywords)
181     mask |= D_EXT;
182
183   /* The Objective-C keywords are all context-dependent.  */
184   mask |= D_OBJC;
185
186   ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
187   for (i = 0; i < num_c_common_reswords; i++)
188     {
189       id = get_identifier (c_common_reswords[i].word);
190       C_SET_RID_CODE (id, c_common_reswords[i].rid);
191       ridpointers [(int) c_common_reswords[i].rid] = id;
192       if (! (c_common_reswords[i].disable & mask))
193         C_IS_RESERVED_WORD (id) = 1;
194     }
195 }
196
197 static void
198 init_cp_pragma (void)
199 {
200   c_register_pragma (0, "vtable", handle_pragma_vtable);
201   c_register_pragma (0, "unit", handle_pragma_unit);
202   c_register_pragma (0, "interface", handle_pragma_interface);
203   c_register_pragma (0, "implementation", handle_pragma_implementation);
204   c_register_pragma ("GCC", "interface", handle_pragma_interface);
205   c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
206   c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
207 }
208 \f
209 /* TRUE if a code represents a statement.  */
210
211 bool statement_code_p[MAX_TREE_CODES];
212
213 /* Initialize the C++ front end.  This function is very sensitive to
214    the exact order that things are done here.  It would be nice if the
215    initialization done by this routine were moved to its subroutines,
216    and the ordering dependencies clarified and reduced.  */
217 bool
218 cxx_init (void)
219 {
220   location_t saved_loc;
221   unsigned int i;
222   static const enum tree_code stmt_codes[] = {
223    CTOR_INITIALIZER,    TRY_BLOCK,      HANDLER,
224    EH_SPEC_BLOCK,       USING_STMT,     TAG_DEFN,
225    IF_STMT,             CLEANUP_STMT,   FOR_STMT,
226    WHILE_STMT,          DO_STMT,        BREAK_STMT,
227    CONTINUE_STMT,       SWITCH_STMT,    EXPR_STMT
228   };
229
230   memset (&statement_code_p, 0, sizeof (statement_code_p));
231   for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
232     statement_code_p[stmt_codes[i]] = true;
233
234   saved_loc = input_location;
235   input_location = BUILTINS_LOCATION;
236
237   init_reswords ();
238   init_tree ();
239   init_cp_semantics ();
240   init_operators ();
241   init_method ();
242   init_error ();
243
244   current_function_decl = NULL;
245
246   class_type_node = ridpointers[(int) RID_CLASS];
247
248   cxx_init_decl_processing ();
249
250   if (c_common_init () == false)
251     {
252       input_location = saved_loc;
253       return false;
254     }
255
256   init_cp_pragma ();
257
258   init_repo ();
259
260   input_location = saved_loc;
261   return true;
262 }
263 \f
264 /* Return nonzero if S is not considered part of an
265    INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
266
267 static int
268 interface_strcmp (const char* s)
269 {
270   /* Set the interface/implementation bits for this scope.  */
271   struct impl_files *ifiles;
272   const char *s1;
273
274   for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
275     {
276       const char *t1 = ifiles->filename;
277       s1 = s;
278
279       if (*s1 != *t1 || *s1 == 0)
280         continue;
281
282       while (*s1 == *t1 && *s1 != 0)
283         s1++, t1++;
284
285       /* A match.  */
286       if (*s1 == *t1)
287         return 0;
288
289       /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
290       if (strchr (s1, '.') || strchr (t1, '.'))
291         continue;
292
293       if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
294         continue;
295
296       /* A match.  */
297       return 0;
298     }
299
300   /* No matches.  */
301   return 1;
302 }
303
304 \f
305
306 /* Parse a #pragma whose sole argument is a string constant.
307    If OPT is true, the argument is optional.  */
308 static tree
309 parse_strconst_pragma (const char* name, int opt)
310 {
311   tree result, x;
312   enum cpp_ttype t;
313
314   t = pragma_lex (&result);
315   if (t == CPP_STRING)
316     {
317       if (pragma_lex (&x) != CPP_EOF)
318         warning (0, "junk at end of #pragma %s", name);
319       return result;
320     }
321
322   if (t == CPP_EOF && opt)
323     return NULL_TREE;
324
325   error ("invalid #pragma %s", name);
326   return error_mark_node;
327 }
328
329 static void
330 handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
331 {
332   parse_strconst_pragma ("vtable", 0);
333   sorry ("#pragma vtable no longer supported");
334 }
335
336 static void
337 handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
338 {
339   /* Validate syntax, but don't do anything.  */
340   parse_strconst_pragma ("unit", 0);
341 }
342
343 static void
344 handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
345 {
346   tree fname = parse_strconst_pragma ("interface", 1);
347   struct c_fileinfo *finfo;
348   const char *filename;
349
350   if (fname == error_mark_node)
351     return;
352   else if (fname == 0)
353     filename = lbasename (input_filename);
354   else
355     filename = TREE_STRING_POINTER (fname);
356
357   finfo = get_fileinfo (input_filename);
358
359   if (impl_file_chain == 0)
360     {
361       /* If this is zero at this point, then we are
362          auto-implementing.  */
363       if (main_input_filename == 0)
364         main_input_filename = input_filename;
365     }
366
367   finfo->interface_only = interface_strcmp (filename);
368   /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
369      a definition in another file.  */
370   if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
371     finfo->interface_unknown = 0;
372 }
373
374 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
375    We used to only allow this at toplevel, but that restriction was buggy
376    in older compilers and it seems reasonable to allow it in the headers
377    themselves, too.  It only needs to precede the matching #p interface.
378
379    We don't touch finfo->interface_only or finfo->interface_unknown;
380    the user must specify a matching #p interface for this to have
381    any effect.  */
382
383 static void
384 handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
385 {
386   tree fname = parse_strconst_pragma ("implementation", 1);
387   const char *filename;
388   struct impl_files *ifiles = impl_file_chain;
389
390   if (fname == error_mark_node)
391     return;
392
393   if (fname == 0)
394     {
395       if (main_input_filename)
396         filename = main_input_filename;
397       else
398         filename = input_filename;
399       filename = lbasename (filename);
400     }
401   else
402     {
403       filename = TREE_STRING_POINTER (fname);
404       if (cpp_included_before (parse_in, filename, input_location))
405         warning (0, "#pragma implementation for %qs appears after "
406                  "file is included", filename);
407     }
408
409   for (; ifiles; ifiles = ifiles->next)
410     {
411       if (! strcmp (ifiles->filename, filename))
412         break;
413     }
414   if (ifiles == 0)
415     {
416       ifiles = XNEW (struct impl_files);
417       ifiles->filename = xstrdup (filename);
418       ifiles->next = impl_file_chain;
419       impl_file_chain = ifiles;
420     }
421 }
422
423 /* Indicate that this file uses Java-personality exception handling.  */
424 static void
425 handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED)
426 {
427   tree x;
428   if (pragma_lex (&x) != CPP_EOF)
429     warning (0, "junk at end of #pragma GCC java_exceptions");
430
431   choose_personality_routine (lang_java);
432 }
433
434 /* Issue an error message indicating that the lookup of NAME (an
435    IDENTIFIER_NODE) failed.  Returns the ERROR_MARK_NODE.  */
436
437 tree
438 unqualified_name_lookup_error (tree name)
439 {
440   if (IDENTIFIER_OPNAME_P (name))
441     {
442       if (name != ansi_opname (ERROR_MARK))
443         error ("%qD not defined", name);
444     }
445   else
446     {
447       error ("%qD was not declared in this scope", name);
448       /* Prevent repeated error messages by creating a VAR_DECL with
449          this NAME in the innermost block scope.  */
450       if (current_function_decl)
451         {
452           tree decl;
453           decl = build_decl (VAR_DECL, name, error_mark_node);
454           DECL_CONTEXT (decl) = current_function_decl;
455           push_local_binding (name, decl, 0);
456           /* Mark the variable as used so that we do not get warnings
457              about it being unused later.  */
458           TREE_USED (decl) = 1;
459         }
460     }
461
462   return error_mark_node;
463 }
464
465 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
466    used as a function.  Returns an appropriate expression for
467    NAME.  */
468
469 tree
470 unqualified_fn_lookup_error (tree name)
471 {
472   if (processing_template_decl)
473     {
474       /* In a template, it is invalid to write "f()" or "f(3)" if no
475          declaration of "f" is available.  Historically, G++ and most
476          other compilers accepted that usage since they deferred all name
477          lookup until instantiation time rather than doing unqualified
478          name lookup at template definition time; explain to the user what
479          is going wrong.
480
481          Note that we have the exact wording of the following message in
482          the manual (trouble.texi, node "Name lookup"), so they need to
483          be kept in synch.  */
484       permerror (input_location, "there are no arguments to %qD that depend on a template "
485                  "parameter, so a declaration of %qD must be available",
486                  name, name);
487
488       if (!flag_permissive)
489         {
490           static bool hint;
491           if (!hint)
492             {
493               inform (input_location, "(if you use %<-fpermissive%>, G++ will accept your "
494                      "code, but allowing the use of an undeclared name is "
495                      "deprecated)");
496               hint = true;
497             }
498         }
499       return name;
500     }
501
502   return unqualified_name_lookup_error (name);
503 }
504
505 tree
506 build_lang_decl (enum tree_code code, tree name, tree type)
507 {
508   tree t;
509
510   t = build_decl (code, name, type);
511   retrofit_lang_decl (t);
512
513   /* All nesting of C++ functions is lexical; there is never a "static
514      chain" in the sense of GNU C nested functions.  */
515   if (code == FUNCTION_DECL)
516     DECL_NO_STATIC_CHAIN (t) = 1;
517
518   return t;
519 }
520
521 /* Add DECL_LANG_SPECIFIC info to T.  Called from build_lang_decl
522    and pushdecl (for functions generated by the back end).  */
523
524 void
525 retrofit_lang_decl (tree t)
526 {
527   struct lang_decl *ld;
528   size_t size;
529
530   if (CAN_HAVE_FULL_LANG_DECL_P (t))
531     size = sizeof (struct lang_decl);
532   else
533     size = sizeof (struct lang_decl_flags);
534
535   ld = GGC_CNEWVAR (struct lang_decl, size);
536
537   ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
538   ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
539   ld->decl_flags.u2sel = 0;
540   if (ld->decl_flags.can_be_full)
541     ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
542
543   DECL_LANG_SPECIFIC (t) = ld;
544   if (current_lang_name == lang_name_cplusplus
545       || decl_linkage (t) == lk_none)
546     SET_DECL_LANGUAGE (t, lang_cplusplus);
547   else if (current_lang_name == lang_name_c)
548     SET_DECL_LANGUAGE (t, lang_c);
549   else if (current_lang_name == lang_name_java)
550     SET_DECL_LANGUAGE (t, lang_java);
551   else
552     gcc_unreachable ();
553
554 #ifdef GATHER_STATISTICS
555   tree_node_counts[(int)lang_decl] += 1;
556   tree_node_sizes[(int)lang_decl] += size;
557 #endif
558 }
559
560 void
561 cxx_dup_lang_specific_decl (tree node)
562 {
563   int size;
564   struct lang_decl *ld;
565
566   if (! DECL_LANG_SPECIFIC (node))
567     return;
568
569   if (!CAN_HAVE_FULL_LANG_DECL_P (node))
570     size = sizeof (struct lang_decl_flags);
571   else
572     size = sizeof (struct lang_decl);
573   ld = GGC_NEWVAR (struct lang_decl, size);
574   memcpy (ld, DECL_LANG_SPECIFIC (node), size);
575   DECL_LANG_SPECIFIC (node) = ld;
576
577 #ifdef GATHER_STATISTICS
578   tree_node_counts[(int)lang_decl] += 1;
579   tree_node_sizes[(int)lang_decl] += size;
580 #endif
581 }
582
583 /* Copy DECL, including any language-specific parts.  */
584
585 tree
586 copy_decl (tree decl)
587 {
588   tree copy;
589
590   copy = copy_node (decl);
591   cxx_dup_lang_specific_decl (copy);
592   return copy;
593 }
594
595 /* Replace the shared language-specific parts of NODE with a new copy.  */
596
597 static void
598 copy_lang_type (tree node)
599 {
600   int size;
601   struct lang_type *lt;
602
603   if (! TYPE_LANG_SPECIFIC (node))
604     return;
605
606   if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
607     size = sizeof (struct lang_type);
608   else
609     size = sizeof (struct lang_type_ptrmem);
610   lt = GGC_NEWVAR (struct lang_type, size);
611   memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
612   TYPE_LANG_SPECIFIC (node) = lt;
613
614 #ifdef GATHER_STATISTICS
615   tree_node_counts[(int)lang_type] += 1;
616   tree_node_sizes[(int)lang_type] += size;
617 #endif
618 }
619
620 /* Copy TYPE, including any language-specific parts.  */
621
622 tree
623 copy_type (tree type)
624 {
625   tree copy;
626
627   copy = copy_node (type);
628   copy_lang_type (copy);
629   return copy;
630 }
631
632 tree
633 cxx_make_type (enum tree_code code)
634 {
635   tree t = make_node (code);
636
637   /* Create lang_type structure.  */
638   if (RECORD_OR_UNION_CODE_P (code)
639       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
640     {
641       struct lang_type *pi = GGC_CNEW (struct lang_type);
642
643       TYPE_LANG_SPECIFIC (t) = pi;
644       pi->u.c.h.is_lang_type_class = 1;
645
646 #ifdef GATHER_STATISTICS
647       tree_node_counts[(int)lang_type] += 1;
648       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
649 #endif
650     }
651
652   /* Set up some flags that give proper default behavior.  */
653   if (RECORD_OR_UNION_CODE_P (code))
654     {
655       struct c_fileinfo *finfo = get_fileinfo (input_filename);
656       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
657       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
658     }
659
660   return t;
661 }
662
663 tree
664 make_class_type (enum tree_code code)
665 {
666   tree t = cxx_make_type (code);
667   SET_CLASS_TYPE_P (t, 1);
668   return t;
669 }
670
671 /* Returns true if we are currently in the main source file, or in a
672    template instantiation started from the main source file.  */
673
674 bool
675 in_main_input_context (void)
676 {
677   struct tinst_level *tl = outermost_tinst_level();
678
679   if (tl)
680     return strcmp (main_input_filename,
681                   LOCATION_FILE (tl->locus)) == 0;
682   else
683     return strcmp (main_input_filename, input_filename) == 0;
684 }