OSDN Git Service

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