OSDN Git Service

PR c++/16002
[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 Free Software Foundation, Inc.
4    Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
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) LAST_CPLUS_TREE_CODE];
93 /* Similar, but for assignment operators.  */
94 operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE];
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) CONVERT_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 /* The reserved keyword table.  */
167 struct resword
168 {
169   const char *const word;
170   ENUM_BITFIELD(rid) const rid : 16;
171   const unsigned int disable   : 16;
172 };
173
174 /* Disable mask.  Keywords are disabled if (reswords[i].disable & mask) is
175    _true_.  */
176 #define D_EXT           0x01    /* GCC extension */
177 #define D_ASM           0x02    /* in C99, but has a switch to turn it off */
178
179 CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
180
181 static const struct resword reswords[] =
182 {
183   { "_Complex",         RID_COMPLEX,    0 },
184   { "__FUNCTION__",     RID_FUNCTION_NAME, 0 },
185   { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
186   { "__alignof",        RID_ALIGNOF,    0 },
187   { "__alignof__",      RID_ALIGNOF,    0 },
188   { "__asm",            RID_ASM,        0 },
189   { "__asm__",          RID_ASM,        0 },
190   { "__attribute",      RID_ATTRIBUTE,  0 },
191   { "__attribute__",    RID_ATTRIBUTE,  0 },
192   { "__builtin_offsetof", RID_OFFSETOF, 0 },
193   { "__builtin_va_arg", RID_VA_ARG,     0 },
194   { "__complex",        RID_COMPLEX,    0 },
195   { "__complex__",      RID_COMPLEX,    0 },
196   { "__const",          RID_CONST,      0 },
197   { "__const__",        RID_CONST,      0 },
198   { "__extension__",    RID_EXTENSION,  0 },
199   { "__func__",         RID_C99_FUNCTION_NAME,  0 },
200   { "__imag",           RID_IMAGPART,   0 },
201   { "__imag__",         RID_IMAGPART,   0 },
202   { "__inline",         RID_INLINE,     0 },
203   { "__inline__",       RID_INLINE,     0 },
204   { "__label__",        RID_LABEL,      0 },
205   { "__null",           RID_NULL,       0 },
206   { "__real",           RID_REALPART,   0 },
207   { "__real__",         RID_REALPART,   0 },
208   { "__restrict",       RID_RESTRICT,   0 },
209   { "__restrict__",     RID_RESTRICT,   0 },
210   { "__signed",         RID_SIGNED,     0 },
211   { "__signed__",       RID_SIGNED,     0 },
212   { "__thread",         RID_THREAD,     0 },
213   { "__typeof",         RID_TYPEOF,     0 },
214   { "__typeof__",       RID_TYPEOF,     0 },
215   { "__volatile",       RID_VOLATILE,   0 },
216   { "__volatile__",     RID_VOLATILE,   0 },
217   { "asm",              RID_ASM,        D_ASM },
218   { "auto",             RID_AUTO,       0 },
219   { "bool",             RID_BOOL,       0 },
220   { "break",            RID_BREAK,      0 },
221   { "case",             RID_CASE,       0 },
222   { "catch",            RID_CATCH,      0 },
223   { "char",             RID_CHAR,       0 },
224   { "class",            RID_CLASS,      0 },
225   { "const",            RID_CONST,      0 },
226   { "const_cast",       RID_CONSTCAST,  0 },
227   { "continue",         RID_CONTINUE,   0 },
228   { "default",          RID_DEFAULT,    0 },
229   { "delete",           RID_DELETE,     0 },
230   { "do",               RID_DO,         0 },
231   { "double",           RID_DOUBLE,     0 },
232   { "dynamic_cast",     RID_DYNCAST,    0 },
233   { "else",             RID_ELSE,       0 },
234   { "enum",             RID_ENUM,       0 },
235   { "explicit",         RID_EXPLICIT,   0 },
236   { "export",           RID_EXPORT,     0 },
237   { "extern",           RID_EXTERN,     0 },
238   { "false",            RID_FALSE,      0 },
239   { "float",            RID_FLOAT,      0 },
240   { "for",              RID_FOR,        0 },
241   { "friend",           RID_FRIEND,     0 },
242   { "goto",             RID_GOTO,       0 },
243   { "if",               RID_IF,         0 },
244   { "inline",           RID_INLINE,     0 },
245   { "int",              RID_INT,        0 },
246   { "long",             RID_LONG,       0 },
247   { "mutable",          RID_MUTABLE,    0 },
248   { "namespace",        RID_NAMESPACE,  0 },
249   { "new",              RID_NEW,        0 },
250   { "operator",         RID_OPERATOR,   0 },
251   { "private",          RID_PRIVATE,    0 },
252   { "protected",        RID_PROTECTED,  0 },
253   { "public",           RID_PUBLIC,     0 },
254   { "register",         RID_REGISTER,   0 },
255   { "reinterpret_cast", RID_REINTCAST,  0 },
256   { "return",           RID_RETURN,     0 },
257   { "short",            RID_SHORT,      0 },
258   { "signed",           RID_SIGNED,     0 },
259   { "sizeof",           RID_SIZEOF,     0 },
260   { "static",           RID_STATIC,     0 },
261   { "static_cast",      RID_STATCAST,   0 },
262   { "struct",           RID_STRUCT,     0 },
263   { "switch",           RID_SWITCH,     0 },
264   { "template",         RID_TEMPLATE,   0 },
265   { "this",             RID_THIS,       0 },
266   { "throw",            RID_THROW,      0 },
267   { "true",             RID_TRUE,       0 },
268   { "try",              RID_TRY,        0 },
269   { "typedef",          RID_TYPEDEF,    0 },
270   { "typename",         RID_TYPENAME,   0 },
271   { "typeid",           RID_TYPEID,     0 },
272   { "typeof",           RID_TYPEOF,     D_ASM|D_EXT },
273   { "union",            RID_UNION,      0 },
274   { "unsigned",         RID_UNSIGNED,   0 },
275   { "using",            RID_USING,      0 },
276   { "virtual",          RID_VIRTUAL,    0 },
277   { "void",             RID_VOID,       0 },
278   { "volatile",         RID_VOLATILE,   0 },
279   { "wchar_t",          RID_WCHAR,      0 },
280   { "while",            RID_WHILE,      0 },
281
282 };
283
284 void
285 init_reswords (void)
286 {
287   unsigned int i;
288   tree id;
289   int mask = ((flag_no_asm ? D_ASM : 0)
290               | (flag_no_gnu_keywords ? D_EXT : 0));
291
292   ridpointers = ggc_calloc ((int) RID_MAX, sizeof (tree));
293   for (i = 0; i < ARRAY_SIZE (reswords); i++)
294     {
295       id = get_identifier (reswords[i].word);
296       C_RID_CODE (id) = reswords[i].rid;
297       ridpointers [(int) reswords[i].rid] = id;
298       if (! (reswords[i].disable & mask))
299         C_IS_RESERVED_WORD (id) = 1;
300     }
301 }
302
303 static void
304 init_cp_pragma (void)
305 {
306   c_register_pragma (0, "vtable", handle_pragma_vtable);
307   c_register_pragma (0, "unit", handle_pragma_unit);
308   c_register_pragma (0, "interface", handle_pragma_interface);
309   c_register_pragma (0, "implementation", handle_pragma_implementation);
310   c_register_pragma ("GCC", "interface", handle_pragma_interface);
311   c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
312   c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
313 }
314 \f
315 /* Initialize the C++ front end.  This function is very sensitive to
316    the exact order that things are done here.  It would be nice if the
317    initialization done by this routine were moved to its subroutines,
318    and the ordering dependencies clarified and reduced.  */
319 bool
320 cxx_init (void)
321 {
322   static const enum tree_code stmt_codes[] = {
323     c_common_stmt_codes,
324     cp_stmt_codes
325   };
326
327   INIT_STATEMENT_CODES (stmt_codes);
328
329   /* We cannot just assign to input_filename because it has already
330      been initialized and will be used later as an N_BINCL for stabs+
331      debugging.  */
332 #ifdef USE_MAPPED_LOCATION
333   push_srcloc (BUILTINS_LOCATION);
334 #else
335   push_srcloc ("<built-in>", 0);
336 #endif
337
338   init_reswords ();
339   init_tree ();
340   init_cp_semantics ();
341   init_operators ();
342   init_method ();
343   init_error ();
344
345   current_function_decl = NULL;
346
347   class_type_node = ridpointers[(int) RID_CLASS];
348
349   cxx_init_decl_processing ();
350
351   /* Create the built-in __null node.  It is important that this is
352      not shared. */
353   null_node = make_node (INTEGER_CST);
354   TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
355   ridpointers[RID_NULL] = null_node;
356
357   /* The fact that G++ uses COMDAT for many entities (inline
358      functions, template instantiations, virtual tables, etc.) mean
359      that it is fundamentally unreliable to try to make decisions
360      about whether or not to output a particular entity until the end
361      of the compilation.  However, the inliner requires that functions
362      be provided to the back end if they are to be inlined.
363      Therefore, we always use unit-at-a-time mode; in that mode, we
364      can provide entities to the back end and it will decide what to
365      emit based on what is actually needed.  */
366   flag_unit_at_a_time = 1;
367
368   if (c_common_init () == false)
369     {
370       pop_srcloc();
371       return false;
372     }
373
374   init_cp_pragma ();
375
376   init_repo ();
377
378   pop_srcloc();
379   return true;
380 }
381 \f
382 /* Return nonzero if S is not considered part of an
383    INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
384
385 static int
386 interface_strcmp (const char* s)
387 {
388   /* Set the interface/implementation bits for this scope.  */
389   struct impl_files *ifiles;
390   const char *s1;
391
392   for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
393     {
394       const char *t1 = ifiles->filename;
395       s1 = s;
396
397       if (*s1 != *t1 || *s1 == 0)
398         continue;
399
400       while (*s1 == *t1 && *s1 != 0)
401         s1++, t1++;
402
403       /* A match.  */
404       if (*s1 == *t1)
405         return 0;
406
407       /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
408       if (strchr (s1, '.') || strchr (t1, '.'))
409         continue;
410
411       if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
412         continue;
413
414       /* A match.  */
415       return 0;
416     }
417
418   /* No matches.  */
419   return 1;
420 }
421
422 \f
423
424 /* Parse a #pragma whose sole argument is a string constant.
425    If OPT is true, the argument is optional.  */
426 static tree
427 parse_strconst_pragma (const char* name, int opt)
428 {
429   tree result, x;
430   enum cpp_ttype t;
431
432   t = c_lex (&x);
433   if (t == CPP_STRING)
434     {
435       result = x;
436       if (c_lex (&x) != CPP_EOF)
437         warning ("junk at end of #pragma %s", name);
438       return result;
439     }
440
441   if (t == CPP_EOF && opt)
442     return 0;
443
444   error ("invalid #pragma %s", name);
445   return (tree)-1;
446 }
447
448 static void
449 handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
450 {
451   parse_strconst_pragma ("vtable", 0);
452   sorry ("#pragma vtable no longer supported");
453 }
454
455 static void
456 handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
457 {
458   /* Validate syntax, but don't do anything.  */
459   parse_strconst_pragma ("unit", 0);
460 }
461
462 static void
463 handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
464 {
465   tree fname = parse_strconst_pragma ("interface", 1);
466   struct c_fileinfo *finfo;
467   const char *main_filename;
468
469   if (fname == (tree)-1)
470     return;
471   else if (fname == 0)
472     main_filename = lbasename (input_filename);
473   else
474     main_filename = TREE_STRING_POINTER (fname);
475
476   finfo = get_fileinfo (input_filename);
477
478   if (impl_file_chain == 0)
479     {
480       /* If this is zero at this point, then we are
481          auto-implementing.  */
482       if (main_input_filename == 0)
483         main_input_filename = input_filename;
484     }
485
486   finfo->interface_only = interface_strcmp (main_filename);
487   /* If MULTIPLE_SYMBOL_SPACES is set, we cannot assume that we can see
488      a definition in another file.  */
489   if (!MULTIPLE_SYMBOL_SPACES || !finfo->interface_only)
490     finfo->interface_unknown = 0;
491 }
492
493 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
494    We used to only allow this at toplevel, but that restriction was buggy
495    in older compilers and it seems reasonable to allow it in the headers
496    themselves, too.  It only needs to precede the matching #p interface.
497
498    We don't touch finfo->interface_only or finfo->interface_unknown;
499    the user must specify a matching #p interface for this to have
500    any effect.  */
501
502 static void
503 handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
504 {
505   tree fname = parse_strconst_pragma ("implementation", 1);
506   const char *main_filename;
507   struct impl_files *ifiles = impl_file_chain;
508
509   if (fname == (tree)-1)
510     return;
511
512   if (fname == 0)
513     {
514       if (main_input_filename)
515         main_filename = main_input_filename;
516       else
517         main_filename = input_filename;
518       main_filename = lbasename (main_filename);
519     }
520   else
521     {
522       main_filename = TREE_STRING_POINTER (fname);
523       if (cpp_included (parse_in, main_filename))
524         warning ("#pragma implementation for %s appears after file is included",
525                  main_filename);
526     }
527
528   for (; ifiles; ifiles = ifiles->next)
529     {
530       if (! strcmp (ifiles->filename, main_filename))
531         break;
532     }
533   if (ifiles == 0)
534     {
535       ifiles = xmalloc (sizeof (struct impl_files));
536       ifiles->filename = main_filename;
537       ifiles->next = impl_file_chain;
538       impl_file_chain = ifiles;
539     }
540 }
541
542 /* Indicate that this file uses Java-personality exception handling.  */
543 static void
544 handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED )
545 {
546   tree x;
547   if (c_lex (&x) != CPP_EOF)
548     warning ("junk at end of #pragma GCC java_exceptions");
549
550   choose_personality_routine (lang_java);
551 }
552
553 /* Issue an error message indicating that the lookup of NAME (an
554    IDENTIFIER_NODE) failed.  Returns the ERROR_MARK_NODE.  */
555
556 tree
557 unqualified_name_lookup_error (tree name)
558 {
559   if (IDENTIFIER_OPNAME_P (name))
560     {
561       if (name != ansi_opname (ERROR_MARK))
562         error ("`%D' not defined", name);
563     }
564   else
565     {
566       error ("`%D' was not declared in this scope", name);
567       /* Prevent repeated error messages by creating a VAR_DECL with
568          this NAME in the innermost block scope.  */
569       if (current_function_decl)
570         {
571           tree decl;
572           decl = build_decl (VAR_DECL, name, error_mark_node);
573           DECL_CONTEXT (decl) = current_function_decl;
574           push_local_binding (name, decl, 0);
575           /* Mark the variable as used so that we do not get warnings
576              about it being unused later.  */
577           TREE_USED (decl) = 1;
578         }
579     }
580
581   return error_mark_node;
582 }
583
584 /* Like unqualified_name_lookup_error, but NAME is an unqualified-id
585    used as a function.  Returns an appropriate expression for
586    NAME.  */
587
588 tree
589 unqualified_fn_lookup_error (tree name)
590 {
591   if (processing_template_decl)
592     {
593       /* In a template, it is invalid to write "f()" or "f(3)" if no
594          declaration of "f" is available.  Historically, G++ and most
595          other compilers accepted that usage since they deferred all name
596          lookup until instantiation time rather than doing unqualified
597          name lookup at template definition time; explain to the user what 
598          is going wrong.
599
600          Note that we have the exact wording of the following message in
601          the manual (trouble.texi, node "Name lookup"), so they need to
602          be kept in synch.  */
603       pedwarn ("there are no arguments to `%D' that depend on a template "
604                "parameter, so a declaration of `%D' must be available", 
605                name, name);
606       
607       if (!flag_permissive)
608         {
609           static bool hint;
610           if (!hint)
611             {
612               error ("(if you use `-fpermissive', G++ will accept your code, "
613                      "but allowing the use of an undeclared name is "
614                      "deprecated)");
615               hint = true;
616             }
617         }
618       return name;
619     }
620
621   return unqualified_name_lookup_error (name);
622 }
623
624 tree
625 build_lang_decl (enum tree_code code, tree name, tree type)
626 {
627   tree t;
628
629   t = build_decl (code, name, type);
630   retrofit_lang_decl (t);
631
632   /* All nesting of C++ functions is lexical; there is never a "static
633      chain" in the sense of GNU C nested functions.  */
634   if (code == FUNCTION_DECL) 
635     DECL_NO_STATIC_CHAIN (t) = 1;
636
637   return t;
638 }
639
640 /* Add DECL_LANG_SPECIFIC info to T.  Called from build_lang_decl
641    and pushdecl (for functions generated by the backend).  */
642
643 void
644 retrofit_lang_decl (tree t)
645 {
646   struct lang_decl *ld;
647   size_t size;
648
649   if (CAN_HAVE_FULL_LANG_DECL_P (t))
650     size = sizeof (struct lang_decl);
651   else
652     size = sizeof (struct lang_decl_flags);
653
654   ld = GGC_CNEWVAR (struct lang_decl, size);
655
656   ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
657   ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
658   ld->decl_flags.u2sel = 0;
659   if (ld->decl_flags.can_be_full)
660     ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
661
662   DECL_LANG_SPECIFIC (t) = ld;
663   if (current_lang_name == lang_name_cplusplus
664       || decl_linkage (t) == lk_none)
665     SET_DECL_LANGUAGE (t, lang_cplusplus);
666   else if (current_lang_name == lang_name_c)
667     SET_DECL_LANGUAGE (t, lang_c);
668   else if (current_lang_name == lang_name_java)
669     SET_DECL_LANGUAGE (t, lang_java);
670   else
671     gcc_unreachable ();
672
673 #ifdef GATHER_STATISTICS
674   tree_node_counts[(int)lang_decl] += 1;
675   tree_node_sizes[(int)lang_decl] += size;
676 #endif
677 }
678
679 void
680 cxx_dup_lang_specific_decl (tree node)
681 {
682   int size;
683   struct lang_decl *ld;
684
685   if (! DECL_LANG_SPECIFIC (node))
686     return;
687
688   if (!CAN_HAVE_FULL_LANG_DECL_P (node))
689     size = sizeof (struct lang_decl_flags);
690   else
691     size = sizeof (struct lang_decl);
692   ld = GGC_NEWVAR (struct lang_decl, size);
693   memcpy (ld, DECL_LANG_SPECIFIC (node), size);
694   DECL_LANG_SPECIFIC (node) = ld;
695
696 #ifdef GATHER_STATISTICS
697   tree_node_counts[(int)lang_decl] += 1;
698   tree_node_sizes[(int)lang_decl] += size;
699 #endif
700 }
701
702 /* Copy DECL, including any language-specific parts.  */
703
704 tree
705 copy_decl (tree decl)
706 {
707   tree copy;
708
709   copy = copy_node (decl);
710   cxx_dup_lang_specific_decl (copy);
711   return copy;
712 }
713
714 /* Replace the shared language-specific parts of NODE with a new copy.  */
715
716 static void
717 copy_lang_type (tree node)
718 {
719   int size;
720   struct lang_type *lt;
721
722   if (! TYPE_LANG_SPECIFIC (node))
723     return;
724
725   if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
726     size = sizeof (struct lang_type);
727   else
728     size = sizeof (struct lang_type_ptrmem);
729   lt = GGC_NEWVAR (struct lang_type, size);
730   memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
731   TYPE_LANG_SPECIFIC (node) = lt;
732
733 #ifdef GATHER_STATISTICS
734   tree_node_counts[(int)lang_type] += 1;
735   tree_node_sizes[(int)lang_type] += size;
736 #endif
737 }
738
739 /* Copy TYPE, including any language-specific parts.  */
740
741 tree
742 copy_type (tree type)
743 {
744   tree copy;
745
746   copy = copy_node (type);
747   copy_lang_type (copy);
748   return copy;
749 }
750
751 tree
752 cxx_make_type (enum tree_code code)
753 {
754   tree t = make_node (code);
755
756   /* Create lang_type structure.  */
757   if (IS_AGGR_TYPE_CODE (code)
758       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
759     {
760       struct lang_type *pi = GGC_CNEW (struct lang_type);
761
762       TYPE_LANG_SPECIFIC (t) = pi;
763       pi->u.c.h.is_lang_type_class = 1;
764
765 #ifdef GATHER_STATISTICS
766       tree_node_counts[(int)lang_type] += 1;
767       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
768 #endif
769     }
770
771   /* Set up some flags that give proper default behavior.  */
772   if (IS_AGGR_TYPE_CODE (code))
773     {
774       struct c_fileinfo *finfo = get_fileinfo (input_filename);
775       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, finfo->interface_unknown);
776       CLASSTYPE_INTERFACE_ONLY (t) = finfo->interface_only;
777     }
778   else
779     /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits.  But,
780        TYPE_ALIAS_SET is initialized to -1 by default, so we must
781        clear it here.  */
782     TYPE_ALIAS_SET (t) = 0;
783
784   return t;
785 }
786
787 tree
788 make_aggr_type (enum tree_code code)
789 {
790   tree t = cxx_make_type (code);
791
792   if (IS_AGGR_TYPE_CODE (code))
793     SET_IS_AGGR_TYPE (t, 1);
794
795   return t;
796 }
797
798 /* Return the type-qualifier corresponding to the identifier given by
799    RID.  */
800
801 int
802 cp_type_qual_from_rid (tree rid)
803 {
804   if (rid == ridpointers[(int) RID_CONST])
805     return TYPE_QUAL_CONST;
806   else if (rid == ridpointers[(int) RID_VOLATILE])
807     return TYPE_QUAL_VOLATILE;
808   else if (rid == ridpointers[(int) RID_RESTRICT])
809     return TYPE_QUAL_RESTRICT;
810
811   gcc_unreachable ();
812   return TYPE_UNQUALIFIED;
813 }