OSDN Git Service

2007-07-14 Rafael Avila de Espindola <espindola@google.com>
[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   if (!c_dialect_objc())
183     mask |= D_OBJC;
184
185   ridpointers = GGC_CNEWVEC (tree, (int) RID_MAX);
186   for (i = 0; i < num_c_common_reswords; i++)
187     {
188       id = get_identifier (c_common_reswords[i].word);
189       C_SET_RID_CODE (id, c_common_reswords[i].rid);
190       ridpointers [(int) c_common_reswords[i].rid] = id;
191       if (! (c_common_reswords[i].disable & mask))
192         C_IS_RESERVED_WORD (id) = 1;
193     }
194 }
195
196 static void
197 init_cp_pragma (void)
198 {
199   c_register_pragma (0, "vtable", handle_pragma_vtable);
200   c_register_pragma (0, "unit", handle_pragma_unit);
201   c_register_pragma (0, "interface", handle_pragma_interface);
202   c_register_pragma (0, "implementation", handle_pragma_implementation);
203   c_register_pragma ("GCC", "interface", handle_pragma_interface);
204   c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
205   c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
206 }
207 \f
208 /* TRUE if a code represents a statement.  */
209
210 bool statement_code_p[MAX_TREE_CODES];
211
212 /* Initialize the C++ front end.  This function is very sensitive to
213    the exact order that things are done here.  It would be nice if the
214    initialization done by this routine were moved to its subroutines,
215    and the ordering dependencies clarified and reduced.  */
216 bool
217 cxx_init (void)
218 {
219   location_t saved_loc;
220   unsigned int i;
221   static const enum tree_code stmt_codes[] = {
222    CTOR_INITIALIZER,    TRY_BLOCK,      HANDLER,
223    EH_SPEC_BLOCK,       USING_STMT,     TAG_DEFN,
224    IF_STMT,             CLEANUP_STMT,   FOR_STMT,
225    WHILE_STMT,          DO_STMT,        BREAK_STMT,
226    CONTINUE_STMT,       SWITCH_STMT,    EXPR_STMT
227   };
228
229   memset (&statement_code_p, 0, sizeof (statement_code_p));
230   for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
231     statement_code_p[stmt_codes[i]] = true;
232
233   saved_loc = input_location;
234   input_location = BUILTINS_LOCATION;
235
236   init_reswords ();
237   init_tree ();
238   init_cp_semantics ();
239   init_operators ();
240   init_method ();
241   init_error ();
242
243   current_function_decl = NULL;
244
245   class_type_node = ridpointers[(int) RID_CLASS];
246
247   cxx_init_decl_processing ();
248
249   /* The fact that G++ uses COMDAT for many entities (inline
250      functions, template instantiations, virtual tables, etc.) mean
251      that it is fundamentally unreliable to try to make decisions
252      about whether or not to output a particular entity until the end
253      of the compilation.  However, the inliner requires that functions
254      be provided to the back end if they are to be inlined.
255      Therefore, we always use unit-at-a-time mode; in that mode, we
256      can provide entities to the back end and it will decide what to
257      emit based on what is actually needed.  */
258   flag_unit_at_a_time = 1;
259
260   if (c_common_init () == false)
261     {
262       input_location = saved_loc;
263       return false;
264     }
265
266   init_cp_pragma ();
267
268   init_repo ();
269
270   input_location = saved_loc;
271   return true;
272 }
273 \f
274 /* Return nonzero if S is not considered part of an
275    INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
276
277 static int
278 interface_strcmp (const char* s)
279 {
280   /* Set the interface/implementation bits for this scope.  */
281   struct impl_files *ifiles;
282   const char *s1;
283
284   for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
285     {
286       const char *t1 = ifiles->filename;
287       s1 = s;
288
289       if (*s1 != *t1 || *s1 == 0)
290         continue;
291
292       while (*s1 == *t1 && *s1 != 0)
293         s1++, t1++;
294
295       /* A match.  */
296       if (*s1 == *t1)
297         return 0;
298
299       /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
300       if (strchr (s1, '.') || strchr (t1, '.'))
301         continue;
302
303       if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
304         continue;
305
306       /* A match.  */
307       return 0;
308     }
309
310   /* No matches.  */
311   return 1;
312 }
313
314 \f
315
316 /* Parse a #pragma whose sole argument is a string constant.
317    If OPT is true, the argument is optional.  */
318 static tree
319 parse_strconst_pragma (const char* name, int opt)
320 {
321   tree result, x;
322   enum cpp_ttype t;
323
324   t = pragma_lex (&result);
325   if (t == CPP_STRING)
326     {
327       if (pragma_lex (&x) != CPP_EOF)
328         warning (0, "junk at end of #pragma %s", name);
329       return result;
330     }
331
332   if (t == CPP_EOF && opt)
333     return NULL_TREE;
334
335   error ("invalid #pragma %s", name);
336   return error_mark_node;
337 }
338
339 static void
340 handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
341 {
342   parse_strconst_pragma ("vtable", 0);
343   sorry ("#pragma vtable no longer supported");
344 }
345
346 static void
347 handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
348 {
349   /* Validate syntax, but don't do anything.  */
350   parse_strconst_pragma ("unit", 0);
351 }
352
353 static void
354 handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
355 {
356   tree fname = parse_strconst_pragma ("interface", 1);
357   struct c_fileinfo *finfo;
358   const char *filename;
359
360   if (fname == error_mark_node)
361     return;
362   else if (fname == 0)
363     filename = lbasename (input_filename);
364   else
365     filename = TREE_STRING_POINTER (fname);
366
367   finfo = get_fileinfo (input_filename);
368
369   if (impl_file_chain == 0)
370     {
371       /* If this is zero at this point, then we are
372          auto-implementing.  */
373       if (main_input_filename == 0)
374         main_input_filename = input_filename;
375     }
376
377   finfo->interface_only = interface_strcmp (filename);
378   /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
379      a definition in another file.  */
380   if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
381     finfo->interface_unknown = 0;
382 }
383
384 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
385    We used to only allow this at toplevel, but that restriction was buggy
386    in older compilers and it seems reasonable to allow it in the headers
387    themselves, too.  It only needs to precede the matching #p interface.
388
389    We don't touch finfo->interface_only or finfo->interface_unknown;
390    the user must specify a matching #p interface for this to have
391    any effect.  */
392
393 static void
394 handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
395 {
396   tree fname = parse_strconst_pragma ("implementation", 1);
397   const char *filename;
398   struct impl_files *ifiles = impl_file_chain;
399
400   if (fname == error_mark_node)
401     return;
402
403   if (fname == 0)
404     {
405       if (main_input_filename)
406         filename = main_input_filename;
407       else
408         filename = input_filename;
409       filename = lbasename (filename);
410     }
411   else
412     {
413       filename = TREE_STRING_POINTER (fname);
414       if (cpp_included_before (parse_in, filename, input_location))
415         warning (0, "#pragma implementation for %qs appears after "
416                  "file is included", filename);
417     }
418
419   for (; ifiles; ifiles = ifiles->next)
420     {
421       if (! strcmp (ifiles->filename, filename))
422         break;
423     }
424   if (ifiles == 0)
425     {
426       ifiles = XNEW (struct impl_files);
427       ifiles->filename = xstrdup (filename);
428       ifiles->next = impl_file_chain;
429       impl_file_chain = ifiles;
430     }
431 }
432
433 /* Indicate that this file uses Java-personality exception handling.  */
434 static void
435 handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED)
436 {
437   tree x;
438   if (pragma_lex (&x) != CPP_EOF)
439     warning (0, "junk at end of #pragma GCC java_exceptions");
440
441   choose_personality_routine (lang_java);
442 }
443
444 /* Issue an error message indicating that the lookup of NAME (an
445    IDENTIFIER_NODE) failed.  Returns the ERROR_MARK_NODE.  */
446
447 tree
448 unqualified_name_lookup_error (tree name)
449 {
450   if (IDENTIFIER_OPNAME_P (name))
451     {
452       if (name != ansi_opname (ERROR_MARK))
453         error ("%qD not defined", name);
454     }
455   else
456     {
457       error ("%qD was not declared in this scope", name);
458       /* Prevent repeated error messages by creating a VAR_DECL with
459          this NAME in the innermost block scope.  */
460       if (current_function_decl)
461         {
462           tree decl;
463           decl = build_decl (VAR_DECL, name, error_mark_node);
464           DECL_CONTEXT (decl) = current_function_decl;
465           push_local_binding (name, decl, 0);
466           /* Mark the variable as used so that we do not get warnings
467              about it being unused later.  */
468           TREE_USED (decl) = 1;
469         }
470     }
471
472   return error_mark_node;
473 }
474
475 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
476    used as a function.  Returns an appropriate expression for
477    NAME.  */
478
479 tree
480 unqualified_fn_lookup_error (tree name)
481 {
482   if (processing_template_decl)
483     {
484       /* In a template, it is invalid to write "f()" or "f(3)" if no
485          declaration of "f" is available.  Historically, G++ and most
486          other compilers accepted that usage since they deferred all name
487          lookup until instantiation time rather than doing unqualified
488          name lookup at template definition time; explain to the user what
489          is going wrong.
490
491          Note that we have the exact wording of the following message in
492          the manual (trouble.texi, node "Name lookup"), so they need to
493          be kept in synch.  */
494       permerror ("there are no arguments to %qD that depend on a template "
495                  "parameter, so a declaration of %qD must be available",
496                  name, name);
497
498       if (!flag_permissive)
499         {
500           static bool hint;
501           if (!hint)
502             {
503               inform ("(if you use %<-fpermissive%>, G++ will accept your "
504                      "code, but allowing the use of an undeclared name is "
505                      "deprecated)");
506               hint = true;
507             }
508         }
509       return name;
510     }
511
512   return unqualified_name_lookup_error (name);
513 }
514
515 tree
516 build_lang_decl (enum tree_code code, tree name, tree type)
517 {
518   tree t;
519
520   t = build_decl (code, name, type);
521   retrofit_lang_decl (t);
522
523   /* All nesting of C++ functions is lexical; there is never a "static
524      chain" in the sense of GNU C nested functions.  */
525   if (code == FUNCTION_DECL)
526     DECL_NO_STATIC_CHAIN (t) = 1;
527
528   return t;
529 }
530
531 /* Add DECL_LANG_SPECIFIC info to T.  Called from build_lang_decl
532    and pushdecl (for functions generated by the back end).  */
533
534 void
535 retrofit_lang_decl (tree t)
536 {
537   struct lang_decl *ld;
538   size_t size;
539
540   if (CAN_HAVE_FULL_LANG_DECL_P (t))
541     size = sizeof (struct lang_decl);
542   else
543     size = sizeof (struct lang_decl_flags);
544
545   ld = GGC_CNEWVAR (struct lang_decl, size);
546
547   ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
548   ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
549   ld->decl_flags.u2sel = 0;
550   if (ld->decl_flags.can_be_full)
551     ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
552
553   DECL_LANG_SPECIFIC (t) = ld;
554   if (current_lang_name == lang_name_cplusplus
555       || decl_linkage (t) == lk_none)
556     SET_DECL_LANGUAGE (t, lang_cplusplus);
557   else if (current_lang_name == lang_name_c)
558     SET_DECL_LANGUAGE (t, lang_c);
559   else if (current_lang_name == lang_name_java)
560     SET_DECL_LANGUAGE (t, lang_java);
561   else
562     gcc_unreachable ();
563
564 #ifdef GATHER_STATISTICS
565   tree_node_counts[(int)lang_decl] += 1;
566   tree_node_sizes[(int)lang_decl] += size;
567 #endif
568 }
569
570 void
571 cxx_dup_lang_specific_decl (tree node)
572 {
573   int size;
574   struct lang_decl *ld;
575
576   if (! DECL_LANG_SPECIFIC (node))
577     return;
578
579   if (!CAN_HAVE_FULL_LANG_DECL_P (node))
580     size = sizeof (struct lang_decl_flags);
581   else
582     size = sizeof (struct lang_decl);
583   ld = GGC_NEWVAR (struct lang_decl, size);
584   memcpy (ld, DECL_LANG_SPECIFIC (node), size);
585   DECL_LANG_SPECIFIC (node) = ld;
586
587 #ifdef GATHER_STATISTICS
588   tree_node_counts[(int)lang_decl] += 1;
589   tree_node_sizes[(int)lang_decl] += size;
590 #endif
591 }
592
593 /* Copy DECL, including any language-specific parts.  */
594
595 tree
596 copy_decl (tree decl)
597 {
598   tree copy;
599
600   copy = copy_node (decl);
601   cxx_dup_lang_specific_decl (copy);
602   return copy;
603 }
604
605 /* Replace the shared language-specific parts of NODE with a new copy.  */
606
607 static void
608 copy_lang_type (tree node)
609 {
610   int size;
611   struct lang_type *lt;
612
613   if (! TYPE_LANG_SPECIFIC (node))
614     return;
615
616   if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
617     size = sizeof (struct lang_type);
618   else
619     size = sizeof (struct lang_type_ptrmem);
620   lt = GGC_NEWVAR (struct lang_type, size);
621   memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
622   TYPE_LANG_SPECIFIC (node) = lt;
623
624 #ifdef GATHER_STATISTICS
625   tree_node_counts[(int)lang_type] += 1;
626   tree_node_sizes[(int)lang_type] += size;
627 #endif
628 }
629
630 /* Copy TYPE, including any language-specific parts.  */
631
632 tree
633 copy_type (tree type)
634 {
635   tree copy;
636
637   copy = copy_node (type);
638   copy_lang_type (copy);
639   return copy;
640 }
641
642 tree
643 cxx_make_type (enum tree_code code)
644 {
645   tree t = make_node (code);
646
647   /* Create lang_type structure.  */
648   if (RECORD_OR_UNION_CODE_P (code)
649       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
650     {
651       struct lang_type *pi = GGC_CNEW (struct lang_type);
652
653       TYPE_LANG_SPECIFIC (t) = pi;
654       pi->u.c.h.is_lang_type_class = 1;
655
656 #ifdef GATHER_STATISTICS
657       tree_node_counts[(int)lang_type] += 1;
658       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
659 #endif
660     }
661
662   /* Set up some flags that give proper default behavior.  */
663   if (RECORD_OR_UNION_CODE_P (code))
664     {
665       struct c_fileinfo *finfo = get_fileinfo (input_filename);
666       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
667       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
668     }
669
670   return t;
671 }
672
673 tree
674 make_class_type (enum tree_code code)
675 {
676   tree t = cxx_make_type (code);
677   SET_CLASS_TYPE_P (t, 1);
678   return t;
679 }
680
681 /* Returns true if we are currently in the main source file, or in a
682    template instantiation started from the main source file.  */
683
684 bool
685 in_main_input_context (void)
686 {
687   struct tinst_level *tl = outermost_tinst_level();
688
689   if (tl)
690     return strcmp (main_input_filename,
691                   LOCATION_FILE (tl->locus)) == 0;
692   else
693     return strcmp (main_input_filename, input_filename) == 0;
694 }