OSDN Git Service

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