OSDN Git Service

PR c++/11030
[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 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 "lex.h"
35 #include "flags.h"
36 #include "c-pragma.h"
37 #include "toplev.h"
38 #include "output.h"
39 #include "tm_p.h"
40 #include "timevar.h"
41
42 static int interface_strcmp (const char *);
43 static void init_cp_pragma (void);
44
45 static tree parse_strconst_pragma (const char *, int);
46 static void handle_pragma_vtable (cpp_reader *);
47 static void handle_pragma_unit (cpp_reader *);
48 static void handle_pragma_interface (cpp_reader *);
49 static void handle_pragma_implementation (cpp_reader *);
50 static void handle_pragma_java_exceptions (cpp_reader *);
51
52 static int is_global (tree);
53 static void init_operators (void);
54 static void copy_lang_type (tree);
55
56 /* A constraint that can be tested at compile time.  */
57 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
58
59 /* Functions and data structures for #pragma interface.
60
61    `#pragma implementation' means that the main file being compiled
62    is considered to implement (provide) the classes that appear in
63    its main body.  I.e., if this is file "foo.cc", and class `bar'
64    is defined in "foo.cc", then we say that "foo.cc implements bar".
65
66    All main input files "implement" themselves automagically.
67
68    `#pragma interface' means that unless this file (of the form "foo.h"
69    is not presently being included by file "foo.cc", the
70    CLASSTYPE_INTERFACE_ONLY bit gets set.  The effect is that none
71    of the vtables nor any of the inline functions defined in foo.h
72    will ever be output.
73
74    There are cases when we want to link files such as "defs.h" and
75    "main.cc".  In this case, we give "defs.h" a `#pragma interface',
76    and "main.cc" has `#pragma implementation "defs.h"'.  */
77
78 struct impl_files
79 {
80   const char *filename;
81   struct impl_files *next;
82 };
83
84 static struct impl_files *impl_file_chain;
85
86 \f
87 /* Return something to represent absolute declarators containing a *.
88    TARGET is the absolute declarator that the * contains.
89    CV_QUALIFIERS is a list of modifiers such as const or volatile
90    to apply to the pointer type, represented as identifiers.
91
92    We return an INDIRECT_REF whose "contents" are TARGET
93    and whose type is the modifier list.  */
94
95 tree
96 make_pointer_declarator (tree cv_qualifiers, tree target)
97 {
98   if (target && TREE_CODE (target) == IDENTIFIER_NODE
99       && ANON_AGGRNAME_P (target))
100     error ("type name expected before `*'");
101   target = build_nt (INDIRECT_REF, target);
102   TREE_TYPE (target) = cv_qualifiers;
103   return target;
104 }
105
106 /* Return something to represent absolute declarators containing a &.
107    TARGET is the absolute declarator that the & contains.
108    CV_QUALIFIERS is a list of modifiers such as const or volatile
109    to apply to the reference type, represented as identifiers.
110
111    We return an ADDR_EXPR whose "contents" are TARGET
112    and whose type is the modifier list.  */
113
114 tree
115 make_reference_declarator (tree cv_qualifiers, tree target)
116 {
117   target = build_nt (ADDR_EXPR, target);
118   TREE_TYPE (target) = cv_qualifiers;
119   return target;
120 }
121
122 tree
123 make_call_declarator (tree target, tree parms, tree cv_qualifiers, 
124                       tree exception_specification)
125 {
126   target = build_nt (CALL_EXPR, target,
127                      tree_cons (parms, cv_qualifiers, NULL_TREE),
128                      /* The third operand is really RTL.  We
129                         shouldn't put anything there.  */
130                      NULL_TREE);
131   CALL_DECLARATOR_EXCEPTION_SPEC (target) = exception_specification;
132   return target;
133 }
134
135 void
136 set_quals_and_spec (tree call_declarator, tree cv_qualifiers, 
137                     tree exception_specification)
138 {
139   CALL_DECLARATOR_QUALS (call_declarator) = cv_qualifiers;
140   CALL_DECLARATOR_EXCEPTION_SPEC (call_declarator) = exception_specification;
141 }
142 \f
143 int interface_only;             /* whether or not current file is only for
144                                    interface definitions.  */
145 int interface_unknown;          /* whether or not we know this class
146                                    to behave according to #pragma interface.  */
147
148 \f
149 void
150 cxx_finish (void)
151 {
152   c_common_finish ();
153 }
154
155 /* A mapping from tree codes to operator name information.  */
156 operator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE];
157 /* Similar, but for assignment operators.  */
158 operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE];
159
160 /* Initialize data structures that keep track of operator names.  */
161
162 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
163  CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
164 #include "operators.def"
165 #undef DEF_OPERATOR
166
167 static void
168 init_operators (void)
169 {
170   tree identifier;
171   char buffer[256];
172   struct operator_name_info_t *oni;
173
174 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)                   \
175   sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
176   identifier = get_identifier (buffer);                                     \
177   IDENTIFIER_OPNAME_P (identifier) = 1;                                     \
178                                                                             \
179   oni = (ASSN_P                                                             \
180          ? &assignment_operator_name_info[(int) CODE]                       \
181          : &operator_name_info[(int) CODE]);                                \
182   oni->identifier = identifier;                                             \
183   oni->name = NAME;                                                         \
184   oni->mangled_name = MANGLING;                                             \
185   oni->arity = ARITY;
186
187 #include "operators.def"
188 #undef DEF_OPERATOR
189
190   operator_name_info[(int) ERROR_MARK].identifier
191     = get_identifier ("<invalid operator>");
192
193   /* Handle some special cases.  These operators are not defined in
194      the language, but can be produced internally.  We may need them
195      for error-reporting.  (Eventually, we should ensure that this
196      does not happen.  Error messages involving these operators will
197      be confusing to users.)  */
198
199   operator_name_info [(int) INIT_EXPR].name
200     = operator_name_info [(int) MODIFY_EXPR].name;
201   operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
202   operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
203   operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
204   operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
205   operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
206   operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
207   operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
208   operator_name_info [(int) ABS_EXPR].name = "abs";
209   operator_name_info [(int) FFS_EXPR].name = "ffs";
210   operator_name_info [(int) BIT_ANDTC_EXPR].name = "&~";
211   operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
212   operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
213   operator_name_info [(int) IN_EXPR].name = "in";
214   operator_name_info [(int) RANGE_EXPR].name = "...";
215   operator_name_info [(int) CONVERT_EXPR].name = "+";
216
217   assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
218     = "(exact /=)";
219   assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
220     = "(ceiling /=)";
221   assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
222     = "(floor /=)";
223   assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
224     = "(round /=)";
225   assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
226     = "(ceiling %=)";
227   assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
228     = "(floor %=)";
229   assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
230     = "(round %=)";
231 }
232
233 /* The reserved keyword table.  */
234 struct resword
235 {
236   const char *const word;
237   const ENUM_BITFIELD(rid) rid : 16;
238   const unsigned int disable   : 16;
239 };
240
241 /* Disable mask.  Keywords are disabled if (reswords[i].disable & mask) is
242    _true_.  */
243 #define D_EXT           0x01    /* GCC extension */
244 #define D_ASM           0x02    /* in C99, but has a switch to turn it off */
245
246 CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
247
248 static const struct resword reswords[] =
249 {
250   { "_Complex",         RID_COMPLEX,    0 },
251   { "__FUNCTION__",     RID_FUNCTION_NAME, 0 },
252   { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
253   { "__alignof",        RID_ALIGNOF,    0 },
254   { "__alignof__",      RID_ALIGNOF,    0 },
255   { "__asm",            RID_ASM,        0 },
256   { "__asm__",          RID_ASM,        0 },
257   { "__attribute",      RID_ATTRIBUTE,  0 },
258   { "__attribute__",    RID_ATTRIBUTE,  0 },
259   { "__builtin_va_arg", RID_VA_ARG,     0 },
260   { "__complex",        RID_COMPLEX,    0 },
261   { "__complex__",      RID_COMPLEX,    0 },
262   { "__const",          RID_CONST,      0 },
263   { "__const__",        RID_CONST,      0 },
264   { "__extension__",    RID_EXTENSION,  0 },
265   { "__func__",         RID_C99_FUNCTION_NAME,  0 },
266   { "__imag",           RID_IMAGPART,   0 },
267   { "__imag__",         RID_IMAGPART,   0 },
268   { "__inline",         RID_INLINE,     0 },
269   { "__inline__",       RID_INLINE,     0 },
270   { "__label__",        RID_LABEL,      0 },
271   { "__null",           RID_NULL,       0 },
272   { "__real",           RID_REALPART,   0 },
273   { "__real__",         RID_REALPART,   0 },
274   { "__restrict",       RID_RESTRICT,   0 },
275   { "__restrict__",     RID_RESTRICT,   0 },
276   { "__signed",         RID_SIGNED,     0 },
277   { "__signed__",       RID_SIGNED,     0 },
278   { "__thread",         RID_THREAD,     0 },
279   { "__typeof",         RID_TYPEOF,     0 },
280   { "__typeof__",       RID_TYPEOF,     0 },
281   { "__volatile",       RID_VOLATILE,   0 },
282   { "__volatile__",     RID_VOLATILE,   0 },
283   { "asm",              RID_ASM,        D_ASM },
284   { "auto",             RID_AUTO,       0 },
285   { "bool",             RID_BOOL,       0 },
286   { "break",            RID_BREAK,      0 },
287   { "case",             RID_CASE,       0 },
288   { "catch",            RID_CATCH,      0 },
289   { "char",             RID_CHAR,       0 },
290   { "class",            RID_CLASS,      0 },
291   { "const",            RID_CONST,      0 },
292   { "const_cast",       RID_CONSTCAST,  0 },
293   { "continue",         RID_CONTINUE,   0 },
294   { "default",          RID_DEFAULT,    0 },
295   { "delete",           RID_DELETE,     0 },
296   { "do",               RID_DO,         0 },
297   { "double",           RID_DOUBLE,     0 },
298   { "dynamic_cast",     RID_DYNCAST,    0 },
299   { "else",             RID_ELSE,       0 },
300   { "enum",             RID_ENUM,       0 },
301   { "explicit",         RID_EXPLICIT,   0 },
302   { "export",           RID_EXPORT,     0 },
303   { "extern",           RID_EXTERN,     0 },
304   { "false",            RID_FALSE,      0 },
305   { "float",            RID_FLOAT,      0 },
306   { "for",              RID_FOR,        0 },
307   { "friend",           RID_FRIEND,     0 },
308   { "goto",             RID_GOTO,       0 },
309   { "if",               RID_IF,         0 },
310   { "inline",           RID_INLINE,     0 },
311   { "int",              RID_INT,        0 },
312   { "long",             RID_LONG,       0 },
313   { "mutable",          RID_MUTABLE,    0 },
314   { "namespace",        RID_NAMESPACE,  0 },
315   { "new",              RID_NEW,        0 },
316   { "operator",         RID_OPERATOR,   0 },
317   { "private",          RID_PRIVATE,    0 },
318   { "protected",        RID_PROTECTED,  0 },
319   { "public",           RID_PUBLIC,     0 },
320   { "register",         RID_REGISTER,   0 },
321   { "reinterpret_cast", RID_REINTCAST,  0 },
322   { "return",           RID_RETURN,     0 },
323   { "short",            RID_SHORT,      0 },
324   { "signed",           RID_SIGNED,     0 },
325   { "sizeof",           RID_SIZEOF,     0 },
326   { "static",           RID_STATIC,     0 },
327   { "static_cast",      RID_STATCAST,   0 },
328   { "struct",           RID_STRUCT,     0 },
329   { "switch",           RID_SWITCH,     0 },
330   { "template",         RID_TEMPLATE,   0 },
331   { "this",             RID_THIS,       0 },
332   { "throw",            RID_THROW,      0 },
333   { "true",             RID_TRUE,       0 },
334   { "try",              RID_TRY,        0 },
335   { "typedef",          RID_TYPEDEF,    0 },
336   { "typename",         RID_TYPENAME,   0 },
337   { "typeid",           RID_TYPEID,     0 },
338   { "typeof",           RID_TYPEOF,     D_ASM|D_EXT },
339   { "union",            RID_UNION,      0 },
340   { "unsigned",         RID_UNSIGNED,   0 },
341   { "using",            RID_USING,      0 },
342   { "virtual",          RID_VIRTUAL,    0 },
343   { "void",             RID_VOID,       0 },
344   { "volatile",         RID_VOLATILE,   0 },
345   { "wchar_t",          RID_WCHAR,      0 },
346   { "while",            RID_WHILE,      0 },
347
348 };
349
350 void
351 init_reswords (void)
352 {
353   unsigned int i;
354   tree id;
355   int mask = ((flag_no_asm ? D_ASM : 0)
356               | (flag_no_gnu_keywords ? D_EXT : 0));
357
358   ridpointers = (tree *) ggc_calloc ((int) RID_MAX, sizeof (tree));
359   for (i = 0; i < ARRAY_SIZE (reswords); i++)
360     {
361       id = get_identifier (reswords[i].word);
362       C_RID_CODE (id) = reswords[i].rid;
363       ridpointers [(int) reswords[i].rid] = id;
364       if (! (reswords[i].disable & mask))
365         C_IS_RESERVED_WORD (id) = 1;
366     }
367 }
368
369 static void
370 init_cp_pragma (void)
371 {
372   c_register_pragma (0, "vtable", handle_pragma_vtable);
373   c_register_pragma (0, "unit", handle_pragma_unit);
374   c_register_pragma (0, "interface", handle_pragma_interface);
375   c_register_pragma (0, "implementation", handle_pragma_implementation);
376   c_register_pragma ("GCC", "interface", handle_pragma_interface);
377   c_register_pragma ("GCC", "implementation", handle_pragma_implementation);
378   c_register_pragma ("GCC", "java_exceptions", handle_pragma_java_exceptions);
379 }
380 \f
381 /* Initialize the C++ front end.  This function is very sensitive to
382    the exact order that things are done here.  It would be nice if the
383    initialization done by this routine were moved to its subroutines,
384    and the ordering dependencies clarified and reduced.  */
385 bool
386 cxx_init (void)
387 {
388   static const enum tree_code stmt_codes[] = {
389     c_common_stmt_codes,
390     cp_stmt_codes
391   };
392
393   INIT_STATEMENT_CODES (stmt_codes);
394
395   /* We cannot just assign to input_filename because it has already
396      been initialized and will be used later as an N_BINCL for stabs+
397      debugging.  */
398   push_srcloc ("<internal>", 0);
399
400   init_reswords ();
401   init_tree ();
402   init_cp_semantics ();
403   init_operators ();
404   init_method ();
405   init_error ();
406
407   current_function_decl = NULL;
408
409   class_type_node = build_int_2 (class_type, 0);
410   TREE_TYPE (class_type_node) = class_type_node;
411   ridpointers[(int) RID_CLASS] = class_type_node;
412
413   record_type_node = build_int_2 (record_type, 0);
414   TREE_TYPE (record_type_node) = record_type_node;
415   ridpointers[(int) RID_STRUCT] = record_type_node;
416
417   union_type_node = build_int_2 (union_type, 0);
418   TREE_TYPE (union_type_node) = union_type_node;
419   ridpointers[(int) RID_UNION] = union_type_node;
420
421   enum_type_node = build_int_2 (enum_type, 0);
422   TREE_TYPE (enum_type_node) = enum_type_node;
423   ridpointers[(int) RID_ENUM] = enum_type_node;
424
425   cxx_init_decl_processing ();
426
427   /* Create the built-in __null node.  */
428   null_node = build_int_2 (0, 0);
429   TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
430   ridpointers[RID_NULL] = null_node;
431
432   interface_unknown = 1;
433
434   if (c_common_init () == false)
435     {
436       pop_srcloc();
437       return false;
438     }
439
440   init_cp_pragma ();
441
442   init_repo (main_input_filename);
443
444   pop_srcloc();
445   return true;
446 }
447 \f
448 /* Helper function to load global variables with interface
449    information.  */
450
451 void
452 extract_interface_info (void)
453 {
454   struct c_fileinfo *finfo = 0;
455
456   if (flag_alt_external_templates)
457     {
458       tree til = tinst_for_decl ();
459
460       if (til)
461         finfo = get_fileinfo (TINST_FILE (til));
462     }
463   if (!finfo)
464     finfo = get_fileinfo (input_filename);
465
466   interface_only = finfo->interface_only;
467   interface_unknown = finfo->interface_unknown;
468 }
469
470 /* Return nonzero if S is not considered part of an
471    INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
472
473 static int
474 interface_strcmp (const char* s)
475 {
476   /* Set the interface/implementation bits for this scope.  */
477   struct impl_files *ifiles;
478   const char *s1;
479
480   for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
481     {
482       const char *t1 = ifiles->filename;
483       s1 = s;
484
485       if (*s1 != *t1 || *s1 == 0)
486         continue;
487
488       while (*s1 == *t1 && *s1 != 0)
489         s1++, t1++;
490
491       /* A match.  */
492       if (*s1 == *t1)
493         return 0;
494
495       /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
496       if (strchr (s1, '.') || strchr (t1, '.'))
497         continue;
498
499       if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
500         continue;
501
502       /* A match.  */
503       return 0;
504     }
505
506   /* No matches.  */
507   return 1;
508 }
509
510 void
511 note_got_semicolon (tree type)
512 {
513   if (!TYPE_P (type))
514     abort ();
515   if (CLASS_TYPE_P (type))
516     CLASSTYPE_GOT_SEMICOLON (type) = 1;
517 }
518
519 void
520 note_list_got_semicolon (tree declspecs)
521 {
522   tree link;
523
524   for (link = declspecs; link; link = TREE_CHAIN (link))
525     {
526       tree type = TREE_VALUE (link);
527       if (type && TYPE_P (type))
528         note_got_semicolon (type);
529     }
530   clear_anon_tags ();
531 }
532 \f
533
534 /* Parse a #pragma whose sole argument is a string constant.
535    If OPT is true, the argument is optional.  */
536 static tree
537 parse_strconst_pragma (const char* name, int opt)
538 {
539   tree result, x;
540   enum cpp_ttype t;
541
542   t = c_lex (&x);
543   if (t == CPP_STRING)
544     {
545       result = x;
546       if (c_lex (&x) != CPP_EOF)
547         warning ("junk at end of #pragma %s", name);
548       return result;
549     }
550
551   if (t == CPP_EOF && opt)
552     return 0;
553
554   error ("invalid #pragma %s", name);
555   return (tree)-1;
556 }
557
558 static void
559 handle_pragma_vtable (cpp_reader* dfile ATTRIBUTE_UNUSED )
560 {
561   parse_strconst_pragma ("vtable", 0);
562   sorry ("#pragma vtable no longer supported");
563 }
564
565 static void
566 handle_pragma_unit (cpp_reader* dfile ATTRIBUTE_UNUSED )
567 {
568   /* Validate syntax, but don't do anything.  */
569   parse_strconst_pragma ("unit", 0);
570 }
571
572 static void
573 handle_pragma_interface (cpp_reader* dfile ATTRIBUTE_UNUSED )
574 {
575   tree fname = parse_strconst_pragma ("interface", 1);
576   struct c_fileinfo *finfo;
577   const char *main_filename;
578
579   if (fname == (tree)-1)
580     return;
581   else if (fname == 0)
582     main_filename = lbasename (input_filename);
583   else
584     main_filename = TREE_STRING_POINTER (fname);
585
586   finfo = get_fileinfo (input_filename);
587
588   if (impl_file_chain == 0)
589     {
590       /* If this is zero at this point, then we are
591          auto-implementing.  */
592       if (main_input_filename == 0)
593         main_input_filename = input_filename;
594     }
595
596   interface_only = interface_strcmp (main_filename);
597 #ifdef MULTIPLE_SYMBOL_SPACES
598   if (! interface_only)
599 #endif
600     interface_unknown = 0;
601
602   finfo->interface_only = interface_only;
603   finfo->interface_unknown = interface_unknown;
604 }
605
606 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
607    We used to only allow this at toplevel, but that restriction was buggy
608    in older compilers and it seems reasonable to allow it in the headers
609    themselves, too.  It only needs to precede the matching #p interface.
610
611    We don't touch interface_only or interface_unknown; the user must specify
612    a matching #p interface for this to have any effect.  */
613
614 static void
615 handle_pragma_implementation (cpp_reader* dfile ATTRIBUTE_UNUSED )
616 {
617   tree fname = parse_strconst_pragma ("implementation", 1);
618   const char *main_filename;
619   struct impl_files *ifiles = impl_file_chain;
620
621   if (fname == (tree)-1)
622     return;
623
624   if (fname == 0)
625     {
626       if (main_input_filename)
627         main_filename = main_input_filename;
628       else
629         main_filename = input_filename;
630       main_filename = lbasename (main_filename);
631     }
632   else
633     {
634       main_filename = TREE_STRING_POINTER (fname);
635       if (cpp_included (parse_in, main_filename))
636         warning ("#pragma implementation for %s appears after file is included",
637                  main_filename);
638     }
639
640   for (; ifiles; ifiles = ifiles->next)
641     {
642       if (! strcmp (ifiles->filename, main_filename))
643         break;
644     }
645   if (ifiles == 0)
646     {
647       ifiles = (struct impl_files*) xmalloc (sizeof (struct impl_files));
648       ifiles->filename = main_filename;
649       ifiles->next = impl_file_chain;
650       impl_file_chain = ifiles;
651     }
652 }
653
654 /* Indicate that this file uses Java-personality exception handling.  */
655 static void
656 handle_pragma_java_exceptions (cpp_reader* dfile ATTRIBUTE_UNUSED )
657 {
658   tree x;
659   if (c_lex (&x) != CPP_EOF)
660     warning ("junk at end of #pragma GCC java_exceptions");
661
662   choose_personality_routine (lang_java);
663 }
664
665 /* Return true if d is in a global scope.  */
666
667 static int
668 is_global (tree d)
669 {
670   while (1)
671     switch (TREE_CODE (d))
672       {
673       case ERROR_MARK:
674         return 1;
675
676       case OVERLOAD: d = OVL_FUNCTION (d); continue;
677       case TREE_LIST: d = TREE_VALUE (d); continue;
678       default:
679         my_friendly_assert (DECL_P (d), 980629);
680
681         return DECL_NAMESPACE_SCOPE_P (d);
682       }
683 }
684
685 /* Issue an error message indicating that the lookup of NAME (an
686    IDENTIFIER_NODE) failed.  */
687
688 void
689 unqualified_name_lookup_error (tree name)
690 {
691   if (IDENTIFIER_OPNAME_P (name))
692     {
693       if (name != ansi_opname (ERROR_MARK))
694         error ("`%D' not defined", name);
695     }
696   else if (current_function_decl == 0)
697     error ("`%D' was not declared in this scope", name);
698   else
699     {
700       if (IDENTIFIER_NAMESPACE_VALUE (name) != error_mark_node
701           || IDENTIFIER_ERROR_LOCUS (name) != current_function_decl)
702         {
703           static int undeclared_variable_notice;
704
705           error ("`%D' undeclared (first use this function)", name);
706
707           if (! undeclared_variable_notice)
708             {
709               error ("(Each undeclared identifier is reported only once for each function it appears in.)");
710               undeclared_variable_notice = 1;
711             }
712         }
713       /* Prevent repeated error messages.  */
714       SET_IDENTIFIER_NAMESPACE_VALUE (name, error_mark_node);
715       SET_IDENTIFIER_ERROR_LOCUS (name, current_function_decl);
716     }
717 }
718
719 tree
720 do_identifier (register tree token, tree args)
721 {
722   register tree id;
723
724   timevar_push (TV_NAME_LOOKUP);
725   id = lookup_name (token, 0);
726
727   /* Do Koenig lookup if appropriate (inside templates we build lookup
728      expressions instead).
729
730      [basic.lookup.koenig]: If the ordinary unqualified lookup of the name
731      finds the declaration of a class member function, the associated
732      namespaces and classes are not considered.  */
733
734   if (args && !current_template_parms && (!id || is_global (id)))
735     id = lookup_arg_dependent (token, id, args);
736
737   if (id == error_mark_node)
738     {
739       /* lookup_name quietly returns error_mark_node if we're parsing,
740          as we don't want to complain about an identifier that ends up
741          being used as a declarator.  So we call it again to get the error
742          message.  */
743       id = lookup_name (token, 0);
744       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
745     }
746
747   if (!id || (TREE_CODE (id) == FUNCTION_DECL
748               && DECL_ANTICIPATED (id)))
749     {
750       if (current_template_parms)
751         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
752                                 build_min_nt (LOOKUP_EXPR, token));
753       else if (IDENTIFIER_TYPENAME_P (token))
754         /* A templated conversion operator might exist.  */
755         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, token);
756       else
757         {
758           unqualified_name_lookup_error (token);
759           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
760         }
761     }
762
763   id = check_for_out_of_scope_variable (id);
764
765   /* TREE_USED is set in `hack_identifier'.  */
766   if (TREE_CODE (id) == CONST_DECL)
767     {
768       /* Check access.  */
769       if (IDENTIFIER_CLASS_VALUE (token) == id)
770         perform_or_defer_access_check (TYPE_BINFO (DECL_CONTEXT (id)), id);
771       if (!processing_template_decl || DECL_TEMPLATE_PARM_P (id))
772         id = DECL_INITIAL (id);
773     }
774   else
775     id = hack_identifier (id, token);
776
777   /* We must look up dependent names when the template is
778      instantiated, not while parsing it.  For now, we don't
779      distinguish between dependent and independent names.  So, for
780      example, we look up all overloaded functions at
781      instantiation-time, even though in some cases we should just use
782      the DECL we have here.  We also use LOOKUP_EXPRs to find things
783      like local variables, rather than creating TEMPLATE_DECLs for the
784      local variables and then finding matching instantiations.  */
785   if (current_template_parms
786       && (is_overloaded_fn (id)
787           || (TREE_CODE (id) == VAR_DECL
788               && CP_DECL_CONTEXT (id)
789               && TREE_CODE (CP_DECL_CONTEXT (id)) == FUNCTION_DECL)
790           || TREE_CODE (id) == PARM_DECL
791           || TREE_CODE (id) == RESULT_DECL
792           || TREE_CODE (id) == USING_DECL))
793     id = build_min_nt (LOOKUP_EXPR, token);
794
795   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, id);
796 }
797
798 tree
799 do_scoped_id (tree token, tree id)
800 {
801   timevar_push (TV_NAME_LOOKUP);
802   if (!id || (TREE_CODE (id) == FUNCTION_DECL
803               && DECL_ANTICIPATED (id)))
804     {
805       if (processing_template_decl)
806         {
807           id = build_min_nt (LOOKUP_EXPR, token);
808           LOOKUP_EXPR_GLOBAL (id) = 1;
809           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, id);
810         }
811       if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node)
812         error ("`::%D' undeclared (first use here)", token);
813       id = error_mark_node;
814       /* Prevent repeated error messages.  */
815       SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node);
816     }
817   else
818     {
819       if (TREE_CODE (id) == ADDR_EXPR)
820         mark_used (TREE_OPERAND (id, 0));
821       else if (TREE_CODE (id) != OVERLOAD)
822         mark_used (id);
823     }
824   if (TREE_CODE (id) == CONST_DECL && ! processing_template_decl)
825     {
826       /* XXX CHS - should we set TREE_USED of the constant? */
827       id = DECL_INITIAL (id);
828       /* This is to prevent an enum whose value is 0
829          from being considered a null pointer constant.  */
830       id = build1 (NOP_EXPR, TREE_TYPE (id), id);
831       TREE_CONSTANT (id) = 1;
832     }
833
834   if (processing_template_decl)
835     {
836       if (is_overloaded_fn (id))
837         {
838           id = build_min_nt (LOOKUP_EXPR, token);
839           LOOKUP_EXPR_GLOBAL (id) = 1;
840           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, id);
841         }
842       /* else just use the decl */
843     }
844   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, convert_from_reference (id));
845 }
846
847 tree
848 identifier_typedecl_value (tree node)
849 {
850   tree t, type;
851   type = IDENTIFIER_TYPE_VALUE (node);
852   if (type == NULL_TREE)
853     return NULL_TREE;
854
855   if (IDENTIFIER_BINDING (node))
856     {
857       t = IDENTIFIER_VALUE (node);
858       if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
859         return t;
860     }
861   if (IDENTIFIER_NAMESPACE_VALUE (node))
862     {
863       t = IDENTIFIER_NAMESPACE_VALUE (node);
864       if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
865         return t;
866     }
867
868   /* Will this one ever happen?  */
869   if (TYPE_MAIN_DECL (type))
870     return TYPE_MAIN_DECL (type);
871
872   /* We used to do an internal error of 62 here, but instead we will
873      handle the return of a null appropriately in the callers.  */
874   return NULL_TREE;
875 }
876
877 #ifdef GATHER_STATISTICS
878 /* The original for tree_node_kind is in the toplevel tree.c; changes there
879    need to be brought into here, unless this were actually put into a header
880    instead.  */
881 /* Statistics-gathering stuff.  */
882 typedef enum
883 {
884   d_kind,
885   t_kind,
886   b_kind,
887   s_kind,
888   r_kind,
889   e_kind,
890   c_kind,
891   id_kind,
892   op_id_kind,
893   perm_list_kind,
894   temp_list_kind,
895   vec_kind,
896   x_kind,
897   lang_decl,
898   lang_type,
899   all_kinds
900 } tree_node_kind;
901
902 extern int tree_node_counts[];
903 extern int tree_node_sizes[];
904 #endif
905
906 tree
907 build_lang_decl (enum tree_code code, tree name, tree type)
908 {
909   tree t;
910
911   t = build_decl (code, name, type);
912   retrofit_lang_decl (t);
913
914   return t;
915 }
916
917 /* Add DECL_LANG_SPECIFIC info to T.  Called from build_lang_decl
918    and pushdecl (for functions generated by the backend).  */
919
920 void
921 retrofit_lang_decl (tree t)
922 {
923   struct lang_decl *ld;
924   size_t size;
925
926   if (CAN_HAVE_FULL_LANG_DECL_P (t))
927     size = sizeof (struct lang_decl);
928   else
929     size = sizeof (struct lang_decl_flags);
930
931   ld = (struct lang_decl *) ggc_alloc_cleared (size);
932
933   ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
934   ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
935   ld->decl_flags.u2sel = 0;
936   if (ld->decl_flags.can_be_full)
937     ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
938
939   DECL_LANG_SPECIFIC (t) = ld;
940   if (current_lang_name == lang_name_cplusplus)
941     SET_DECL_LANGUAGE (t, lang_cplusplus);
942   else if (current_lang_name == lang_name_c)
943     SET_DECL_LANGUAGE (t, lang_c);
944   else if (current_lang_name == lang_name_java)
945     SET_DECL_LANGUAGE (t, lang_java);
946   else abort ();
947
948 #ifdef GATHER_STATISTICS
949   tree_node_counts[(int)lang_decl] += 1;
950   tree_node_sizes[(int)lang_decl] += size;
951 #endif
952 }
953
954 void
955 cxx_dup_lang_specific_decl (tree node)
956 {
957   int size;
958   struct lang_decl *ld;
959
960   if (! DECL_LANG_SPECIFIC (node))
961     return;
962
963   if (!CAN_HAVE_FULL_LANG_DECL_P (node))
964     size = sizeof (struct lang_decl_flags);
965   else
966     size = sizeof (struct lang_decl);
967   ld = (struct lang_decl *) ggc_alloc (size);
968   memcpy (ld, DECL_LANG_SPECIFIC (node), size);
969   DECL_LANG_SPECIFIC (node) = ld;
970
971 #ifdef GATHER_STATISTICS
972   tree_node_counts[(int)lang_decl] += 1;
973   tree_node_sizes[(int)lang_decl] += size;
974 #endif
975 }
976
977 /* Copy DECL, including any language-specific parts.  */
978
979 tree
980 copy_decl (tree decl)
981 {
982   tree copy;
983
984   copy = copy_node (decl);
985   cxx_dup_lang_specific_decl (copy);
986   return copy;
987 }
988
989 /* Replace the shared language-specific parts of NODE with a new copy.  */
990
991 static void
992 copy_lang_type (tree node)
993 {
994   int size;
995   struct lang_type *lt;
996
997   if (! TYPE_LANG_SPECIFIC (node))
998     return;
999
1000   if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
1001     size = sizeof (struct lang_type);
1002   else
1003     size = sizeof (struct lang_type_ptrmem);
1004   lt = (struct lang_type *) ggc_alloc (size);
1005   memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
1006   TYPE_LANG_SPECIFIC (node) = lt;
1007
1008 #ifdef GATHER_STATISTICS
1009   tree_node_counts[(int)lang_type] += 1;
1010   tree_node_sizes[(int)lang_type] += size;
1011 #endif
1012 }
1013
1014 /* Copy TYPE, including any language-specific parts.  */
1015
1016 tree
1017 copy_type (tree type)
1018 {
1019   tree copy;
1020
1021   copy = copy_node (type);
1022   copy_lang_type (copy);
1023   return copy;
1024 }
1025
1026 tree
1027 cxx_make_type (enum tree_code code)
1028 {
1029   register tree t = make_node (code);
1030
1031   /* Create lang_type structure.  */
1032   if (IS_AGGR_TYPE_CODE (code)
1033       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1034     {
1035       struct lang_type *pi;
1036
1037       pi = ((struct lang_type *)
1038             ggc_alloc_cleared (sizeof (struct lang_type)));
1039
1040       TYPE_LANG_SPECIFIC (t) = pi;
1041       pi->u.c.h.is_lang_type_class = 1;
1042
1043 #ifdef GATHER_STATISTICS
1044       tree_node_counts[(int)lang_type] += 1;
1045       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1046 #endif
1047     }
1048
1049   /* Set up some flags that give proper default behavior.  */
1050   if (IS_AGGR_TYPE_CODE (code))
1051     {
1052       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
1053       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1054
1055       /* Make sure this is laid out, for ease of use later.  In the
1056          presence of parse errors, the normal was of assuring this
1057          might not ever get executed, so we lay it out *immediately*.  */
1058       build_pointer_type (t);
1059     }
1060   else
1061     /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits.  But,
1062        TYPE_ALIAS_SET is initialized to -1 by default, so we must
1063        clear it here.  */
1064     TYPE_ALIAS_SET (t) = 0;
1065
1066   /* We need to allocate a TYPE_BINFO even for TEMPLATE_TYPE_PARMs
1067      since they can be virtual base types, and we then need a
1068      canonical binfo for them.  Ideally, this would be done lazily for
1069      all types.  */
1070   if (IS_AGGR_TYPE_CODE (code) || code == TEMPLATE_TYPE_PARM
1071       || code == BOUND_TEMPLATE_TEMPLATE_PARM
1072       || code == TYPENAME_TYPE)
1073     TYPE_BINFO (t) = make_binfo (size_zero_node, t, NULL_TREE, NULL_TREE);
1074
1075   return t;
1076 }
1077
1078 tree
1079 make_aggr_type (enum tree_code code)
1080 {
1081   tree t = cxx_make_type (code);
1082
1083   if (IS_AGGR_TYPE_CODE (code))
1084     SET_IS_AGGR_TYPE (t, 1);
1085
1086   return t;
1087 }
1088
1089 /* Return the type-qualifier corresponding to the identifier given by
1090    RID.  */
1091
1092 int
1093 cp_type_qual_from_rid (tree rid)
1094 {
1095   if (rid == ridpointers[(int) RID_CONST])
1096     return TYPE_QUAL_CONST;
1097   else if (rid == ridpointers[(int) RID_VOLATILE])
1098     return TYPE_QUAL_VOLATILE;
1099   else if (rid == ridpointers[(int) RID_RESTRICT])
1100     return TYPE_QUAL_RESTRICT;
1101
1102   abort ();
1103   return TYPE_UNQUALIFIED;
1104 }