OSDN Git Service

Restore canonical type comparison for dependent type(def)s
[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-family/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 /* True if we saw "#pragma GCC java_exceptions".  */
85 bool pragma_java_exceptions;
86 \f
87 void
88 cxx_finish (void)
89 {
90   c_common_finish ();
91 }
92
93 /* A mapping from tree codes to operator name information.  */
94 operator_name_info_t operator_name_info[(int) MAX_TREE_CODES];
95 /* Similar, but for assignment operators.  */
96 operator_name_info_t assignment_operator_name_info[(int) MAX_TREE_CODES];
97
98 /* Initialize data structures that keep track of operator names.  */
99
100 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
101  CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
102 #include "operators.def"
103 #undef DEF_OPERATOR
104
105 static void
106 init_operators (void)
107 {
108   tree identifier;
109   char buffer[256];
110   struct operator_name_info_t *oni;
111
112 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)                   \
113   sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
114   identifier = get_identifier (buffer);                                     \
115   IDENTIFIER_OPNAME_P (identifier) = 1;                                     \
116                                                                             \
117   oni = (ASSN_P                                                             \
118          ? &assignment_operator_name_info[(int) CODE]                       \
119          : &operator_name_info[(int) CODE]);                                \
120   oni->identifier = identifier;                                             \
121   oni->name = NAME;                                                         \
122   oni->mangled_name = MANGLING;                                             \
123   oni->arity = ARITY;
124
125 #include "operators.def"
126 #undef DEF_OPERATOR
127
128   operator_name_info[(int) ERROR_MARK].identifier
129     = get_identifier ("<invalid operator>");
130
131   /* Handle some special cases.  These operators are not defined in
132      the language, but can be produced internally.  We may need them
133      for error-reporting.  (Eventually, we should ensure that this
134      does not happen.  Error messages involving these operators will
135      be confusing to users.)  */
136
137   operator_name_info [(int) INIT_EXPR].name
138     = operator_name_info [(int) MODIFY_EXPR].name;
139   operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
140   operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
141   operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
142   operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
143   operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
144   operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
145   operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
146   operator_name_info [(int) ABS_EXPR].name = "abs";
147   operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
148   operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
149   operator_name_info [(int) RANGE_EXPR].name = "...";
150   operator_name_info [(int) UNARY_PLUS_EXPR].name = "+";
151
152   assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
153     = "(exact /=)";
154   assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
155     = "(ceiling /=)";
156   assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
157     = "(floor /=)";
158   assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
159     = "(round /=)";
160   assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
161     = "(ceiling %=)";
162   assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
163     = "(floor %=)";
164   assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
165     = "(round %=)";
166 }
167
168 /* Initialize the reserved words.  */
169
170 void
171 init_reswords (void)
172 {
173   unsigned int i;
174   tree id;
175   int mask = 0;
176
177   if (cxx_dialect != cxx0x)
178     mask |= D_CXX0X;
179   if (flag_no_asm)
180     mask |= D_ASM | D_EXT;
181   if (flag_no_gnu_keywords)
182     mask |= D_EXT;
183
184   /* The Objective-C keywords are all context-dependent.  */
185   mask |= D_OBJC;
186
187   ridpointers = ggc_alloc_cleared_vec_tree ((int) RID_MAX);
188   for (i = 0; i < num_c_common_reswords; i++)
189     {
190       if (c_common_reswords[i].disable & D_CONLY)
191         continue;
192       id = get_identifier (c_common_reswords[i].word);
193       C_SET_RID_CODE (id, c_common_reswords[i].rid);
194       ridpointers [(int) c_common_reswords[i].rid] = id;
195       if (! (c_common_reswords[i].disable & mask))
196         C_IS_RESERVED_WORD (id) = 1;
197     }
198 }
199
200 static void
201 init_cp_pragma (void)
202 {
203   c_register_pragma (0, "vtable", handle_pragma_vtable);
204   c_register_pragma (0, "unit", handle_pragma_unit);
205   c_register_pragma (0, "interface", handle_pragma_interface);
206   c_register_pragma (0, "implementation", handle_pragma_implementation);
207   c_register_pragma ("GCC", "interface", handle_pragma_interface);
208   c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
209   c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
210 }
211 \f
212 /* TRUE if a code represents a statement.  */
213
214 bool statement_code_p[MAX_TREE_CODES];
215
216 /* Initialize the C++ front end.  This function is very sensitive to
217    the exact order that things are done here.  It would be nice if the
218    initialization done by this routine were moved to its subroutines,
219    and the ordering dependencies clarified and reduced.  */
220 bool
221 cxx_init (void)
222 {
223   location_t saved_loc;
224   unsigned int i;
225   static const enum tree_code stmt_codes[] = {
226    CTOR_INITIALIZER,    TRY_BLOCK,      HANDLER,
227    EH_SPEC_BLOCK,       USING_STMT,     TAG_DEFN,
228    IF_STMT,             CLEANUP_STMT,   FOR_STMT,
229    RANGE_FOR_STMT,      WHILE_STMT,     DO_STMT,
230    BREAK_STMT,          CONTINUE_STMT,  SWITCH_STMT,
231    EXPR_STMT
232   };
233
234   memset (&statement_code_p, 0, sizeof (statement_code_p));
235   for (i = 0; i < ARRAY_SIZE (stmt_codes); i++)
236     statement_code_p[stmt_codes[i]] = true;
237
238   saved_loc = input_location;
239   input_location = BUILTINS_LOCATION;
240
241   init_reswords ();
242   init_tree ();
243   init_cp_semantics ();
244   init_operators ();
245   init_method ();
246   init_error ();
247
248   current_function_decl = NULL;
249
250   class_type_node = ridpointers[(int) RID_CLASS];
251
252   cxx_init_decl_processing ();
253
254   if (c_common_init () == false)
255     {
256       input_location = saved_loc;
257       return false;
258     }
259
260   init_cp_pragma ();
261
262   init_repo ();
263
264   input_location = saved_loc;
265   return true;
266 }
267 \f
268 /* Return nonzero if S is not considered part of an
269    INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
270
271 static int
272 interface_strcmp (const char* s)
273 {
274   /* Set the interface/implementation bits for this scope.  */
275   struct impl_files *ifiles;
276   const char *s1;
277
278   for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
279     {
280       const char *t1 = ifiles->filename;
281       s1 = s;
282
283       if (*s1 != *t1 || *s1 == 0)
284         continue;
285
286       while (*s1 == *t1 && *s1 != 0)
287         s1++, t1++;
288
289       /* A match.  */
290       if (*s1 == *t1)
291         return 0;
292
293       /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
294       if (strchr (s1, '.') || strchr (t1, '.'))
295         continue;
296
297       if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
298         continue;
299
300       /* A match.  */
301       return 0;
302     }
303
304   /* No matches.  */
305   return 1;
306 }
307
308 \f
309
310 /* Parse a #pragma whose sole argument is a string constant.
311    If OPT is true, the argument is optional.  */
312 static tree
313 parse_strconst_pragma (const char* name, int opt)
314 {
315   tree result, x;
316   enum cpp_ttype t;
317
318   t = pragma_lex (&result);
319   if (t == CPP_STRING)
320     {
321       if (pragma_lex (&x) != CPP_EOF)
322         warning (0, "junk at end of #pragma %s", name);
323       return result;
324     }
325
326   if (t == CPP_EOF && opt)
327     return NULL_TREE;
328
329   error ("invalid #pragma %s", name);
330   return error_mark_node;
331 }
332
333 static void
334 handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
335 {
336   parse_strconst_pragma ("vtable", 0);
337   sorry ("#pragma vtable no longer supported");
338 }
339
340 static void
341 handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
342 {
343   /* Validate syntax, but don't do anything.  */
344   parse_strconst_pragma ("unit", 0);
345 }
346
347 static void
348 handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
349 {
350   tree fname = parse_strconst_pragma ("interface", 1);
351   struct c_fileinfo *finfo;
352   const char *filename;
353
354   if (fname == error_mark_node)
355     return;
356   else if (fname == 0)
357     filename = lbasename (input_filename);
358   else
359     filename = TREE_STRING_POINTER (fname);
360
361   finfo = get_fileinfo (input_filename);
362
363   if (impl_file_chain == 0)
364     {
365       /* If this is zero at this point, then we are
366          auto-implementing.  */
367       if (main_input_filename == 0)
368         main_input_filename = input_filename;
369     }
370
371   finfo->interface_only = interface_strcmp (filename);
372   /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
373      a definition in another file.  */
374   if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
375     finfo->interface_unknown = 0;
376 }
377
378 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
379    We used to only allow this at toplevel, but that restriction was buggy
380    in older compilers and it seems reasonable to allow it in the headers
381    themselves, too.  It only needs to precede the matching #p interface.
382
383    We don't touch finfo->interface_only or finfo->interface_unknown;
384    the user must specify a matching #p interface for this to have
385    any effect.  */
386
387 static void
388 handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
389 {
390   tree fname = parse_strconst_pragma ("implementation", 1);
391   const char *filename;
392   struct impl_files *ifiles = impl_file_chain;
393
394   if (fname == error_mark_node)
395     return;
396
397   if (fname == 0)
398     {
399       if (main_input_filename)
400         filename = main_input_filename;
401       else
402         filename = input_filename;
403       filename = lbasename (filename);
404     }
405   else
406     {
407       filename = TREE_STRING_POINTER (fname);
408       if (cpp_included_before (parse_in, filename, input_location))
409         warning (0, "#pragma implementation for %qs appears after "
410                  "file is included", filename);
411     }
412
413   for (; ifiles; ifiles = ifiles->next)
414     {
415       if (! strcmp (ifiles->filename, filename))
416         break;
417     }
418   if (ifiles == 0)
419     {
420       ifiles = XNEW (struct impl_files);
421       ifiles->filename = xstrdup (filename);
422       ifiles->next = impl_file_chain;
423       impl_file_chain = ifiles;
424     }
425 }
426
427 /* Indicate that this file uses Java-personality exception handling.  */
428 static void
429 handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED)
430 {
431   tree x;
432   if (pragma_lex (&x) != CPP_EOF)
433     warning (0, "junk at end of #pragma GCC java_exceptions");
434
435   choose_personality_routine (lang_java);
436   pragma_java_exceptions = true;
437 }
438
439 /* Issue an error message indicating that the lookup of NAME (an
440    IDENTIFIER_NODE) failed.  Returns the ERROR_MARK_NODE.  */
441
442 tree
443 unqualified_name_lookup_error (tree name)
444 {
445   if (IDENTIFIER_OPNAME_P (name))
446     {
447       if (name != ansi_opname (ERROR_MARK))
448         error ("%qD not defined", name);
449     }
450   else
451     {
452       if (!objc_diagnose_private_ivar (name))
453         error ("%qD was not declared in this scope", name);
454       /* Prevent repeated error messages by creating a VAR_DECL with
455          this NAME in the innermost block scope.  */
456       if (current_function_decl)
457         {
458           tree decl;
459           decl = build_decl (input_location,
460                              VAR_DECL, name, error_mark_node);
461           DECL_CONTEXT (decl) = current_function_decl;
462           push_local_binding (name, decl, 0);
463           /* Mark the variable as used so that we do not get warnings
464              about it being unused later.  */
465           TREE_USED (decl) = 1;
466         }
467     }
468
469   return error_mark_node;
470 }
471
472 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
473    used as a function.  Returns an appropriate expression for
474    NAME.  */
475
476 tree
477 unqualified_fn_lookup_error (tree name)
478 {
479   if (processing_template_decl)
480     {
481       /* In a template, it is invalid to write "f()" or "f(3)" if no
482          declaration of "f" is available.  Historically, G++ and most
483          other compilers accepted that usage since they deferred all name
484          lookup until instantiation time rather than doing unqualified
485          name lookup at template definition time; explain to the user what
486          is going wrong.
487
488          Note that we have the exact wording of the following message in
489          the manual (trouble.texi, node "Name lookup"), so they need to
490          be kept in synch.  */
491       permerror (input_location, "there are no arguments to %qD that depend on a template "
492                  "parameter, so a declaration of %qD must be available",
493                  name, name);
494
495       if (!flag_permissive)
496         {
497           static bool hint;
498           if (!hint)
499             {
500               inform (input_location, "(if you use %<-fpermissive%>, G++ will accept your "
501                      "code, but allowing the use of an undeclared name is "
502                      "deprecated)");
503               hint = true;
504             }
505         }
506       return name;
507     }
508
509   return unqualified_name_lookup_error (name);
510 }
511
512 /* Wrapper around build_lang_decl_loc(). Should gradually move to
513    build_lang_decl_loc() and then rename build_lang_decl_loc() back to
514    build_lang_decl().  */
515
516 tree
517 build_lang_decl (enum tree_code code, tree name, tree type)
518 {
519   return build_lang_decl_loc (input_location, code, name, type);
520 }
521
522 /* Build a decl from CODE, NAME, TYPE declared at LOC, and then add
523    DECL_LANG_SPECIFIC info to the result.  */
524
525 tree
526 build_lang_decl_loc (location_t loc, enum tree_code code, tree name, tree type)
527 {
528   tree t;
529
530   t = build_decl (loc, code, name, type);
531   retrofit_lang_decl (t);
532
533   return t;
534 }
535
536 /* Add DECL_LANG_SPECIFIC info to T.  Called from build_lang_decl
537    and pushdecl (for functions generated by the back end).  */
538
539 void
540 retrofit_lang_decl (tree t)
541 {
542   struct lang_decl *ld;
543   size_t size;
544   int sel;
545
546   if (TREE_CODE (t) == FUNCTION_DECL)
547     sel = 1, size = sizeof (struct lang_decl_fn);
548   else if (TREE_CODE (t) == NAMESPACE_DECL)
549     sel = 2, size = sizeof (struct lang_decl_ns);
550   else if (TREE_CODE (t) == PARM_DECL)
551     sel = 3, size = sizeof (struct lang_decl_parm);
552   else if (LANG_DECL_HAS_MIN (t))
553     sel = 0, size = sizeof (struct lang_decl_min);
554   else
555     gcc_unreachable ();
556
557   ld = ggc_alloc_cleared_lang_decl (size);
558
559   ld->u.base.selector = sel;
560
561   DECL_LANG_SPECIFIC (t) = ld;
562   if (current_lang_name == lang_name_cplusplus
563       || decl_linkage (t) == lk_none)
564     SET_DECL_LANGUAGE (t, lang_cplusplus);
565   else if (current_lang_name == lang_name_c)
566     SET_DECL_LANGUAGE (t, lang_c);
567   else if (current_lang_name == lang_name_java)
568     SET_DECL_LANGUAGE (t, lang_java);
569   else
570     gcc_unreachable ();
571
572 #ifdef GATHER_STATISTICS
573   tree_node_counts[(int)lang_decl] += 1;
574   tree_node_sizes[(int)lang_decl] += size;
575 #endif
576 }
577
578 void
579 cxx_dup_lang_specific_decl (tree node)
580 {
581   int size;
582   struct lang_decl *ld;
583
584   if (! DECL_LANG_SPECIFIC (node))
585     return;
586
587   if (TREE_CODE (node) == FUNCTION_DECL)
588     size = sizeof (struct lang_decl_fn);
589   else if (TREE_CODE (node) == NAMESPACE_DECL)
590     size = sizeof (struct lang_decl_ns);
591   else if (TREE_CODE (node) == PARM_DECL)
592     size = sizeof (struct lang_decl_parm);
593   else if (LANG_DECL_HAS_MIN (node))
594     size = sizeof (struct lang_decl_min);
595   else
596     gcc_unreachable ();
597
598   ld = ggc_alloc_lang_decl (size);
599   memcpy (ld, DECL_LANG_SPECIFIC (node), size);
600   DECL_LANG_SPECIFIC (node) = ld;
601
602 #ifdef GATHER_STATISTICS
603   tree_node_counts[(int)lang_decl] += 1;
604   tree_node_sizes[(int)lang_decl] += size;
605 #endif
606 }
607
608 /* Copy DECL, including any language-specific parts.  */
609
610 tree
611 copy_decl (tree decl)
612 {
613   tree copy;
614
615   copy = copy_node (decl);
616   cxx_dup_lang_specific_decl (copy);
617   return copy;
618 }
619
620 /* Replace the shared language-specific parts of NODE with a new copy.  */
621
622 static void
623 copy_lang_type (tree node)
624 {
625   int size;
626   struct lang_type *lt;
627
628   if (! TYPE_LANG_SPECIFIC (node))
629     return;
630
631   if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
632     size = sizeof (struct lang_type);
633   else
634     size = sizeof (struct lang_type_ptrmem);
635   lt = ggc_alloc_lang_type (size);
636   memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
637   TYPE_LANG_SPECIFIC (node) = lt;
638
639 #ifdef GATHER_STATISTICS
640   tree_node_counts[(int)lang_type] += 1;
641   tree_node_sizes[(int)lang_type] += size;
642 #endif
643 }
644
645 /* Copy TYPE, including any language-specific parts.  */
646
647 tree
648 copy_type (tree type)
649 {
650   tree copy;
651
652   copy = copy_node (type);
653   copy_lang_type (copy);
654   return copy;
655 }
656
657 tree
658 cxx_make_type (enum tree_code code)
659 {
660   tree t = make_node (code);
661
662   /* Create lang_type structure.  */
663   if (RECORD_OR_UNION_CODE_P (code)
664       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
665     {
666       struct lang_type *pi
667           = ggc_alloc_cleared_lang_type (sizeof (struct lang_type));
668
669       TYPE_LANG_SPECIFIC (t) = pi;
670       pi->u.c.h.is_lang_type_class = 1;
671
672 #ifdef GATHER_STATISTICS
673       tree_node_counts[(int)lang_type] += 1;
674       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
675 #endif
676     }
677
678   /* Set up some flags that give proper default behavior.  */
679   if (RECORD_OR_UNION_CODE_P (code))
680     {
681       struct c_fileinfo *finfo = get_fileinfo (input_filename);
682       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
683       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
684     }
685
686   return t;
687 }
688
689 tree
690 make_class_type (enum tree_code code)
691 {
692   tree t = cxx_make_type (code);
693   SET_CLASS_TYPE_P (t, 1);
694   return t;
695 }
696
697 /* Returns true if we are currently in the main source file, or in a
698    template instantiation started from the main source file.  */
699
700 bool
701 in_main_input_context (void)
702 {
703   struct tinst_level *tl = outermost_tinst_level();
704
705   if (tl)
706     return strcmp (main_input_filename,
707                   LOCATION_FILE (tl->locus)) == 0;
708   else
709     return strcmp (main_input_filename, input_filename) == 0;
710 }