OSDN Git Service

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