OSDN Git Service

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