OSDN Git Service

c093fc1f0f7ec69ad366592f5b7d0927b4d59a49
[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 Free Software Foundation, Inc.
4    Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC 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 GNU CC 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 GNU CC; 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 /* Cause the `yydebug' variable to be defined.  */
27 #define YYDEBUG 1
28
29 #include "config.h"
30 #include "system.h"
31 #include "input.h"
32 #include "tree.h"
33 #include "cp-tree.h"
34 #include "cpplib.h"
35 #include "c-lex.h"
36 #include "lex.h"
37 #include "parse.h"
38 #include "flags.h"
39 #include "c-pragma.h"
40 #include "toplev.h"
41 #include "output.h"
42 #include "ggc.h"
43 #include "tm_p.h"
44 #include "timevar.h"
45 #include "diagnostic.h"
46
47 #ifdef MULTIBYTE_CHARS
48 #include "mbchar.h"
49 #include <locale.h>
50 #endif
51
52 extern void yyprint PARAMS ((FILE *, int, YYSTYPE));
53
54 static int interface_strcmp PARAMS ((const char *));
55 static int *init_cpp_parse PARAMS ((void));
56 static void init_cp_pragma PARAMS ((void));
57
58 static tree parse_strconst_pragma PARAMS ((const char *, int));
59 static void handle_pragma_vtable PARAMS ((cpp_reader *));
60 static void handle_pragma_unit PARAMS ((cpp_reader *));
61 static void handle_pragma_interface PARAMS ((cpp_reader *));
62 static void handle_pragma_implementation PARAMS ((cpp_reader *));
63 static void handle_pragma_java_exceptions PARAMS ((cpp_reader *));
64
65 #ifdef GATHER_STATISTICS
66 #ifdef REDUCE_LENGTH
67 static int reduce_cmp PARAMS ((int *, int *));
68 static int token_cmp PARAMS ((int *, int *));
69 #endif
70 #endif
71 static int is_global PARAMS ((tree));
72 static void init_operators PARAMS ((void));
73 static void copy_lang_type PARAMS ((tree));
74
75 /* A constraint that can be tested at compile time.  */
76 #ifdef __STDC__
77 #define CONSTRAINT(name, expr) extern int constraint_##name [(expr) ? 1 : -1]
78 #else
79 #define CONSTRAINT(name, expr) extern int constraint_/**/name [(expr) ? 1 : -1]
80 #endif
81
82 #include "cpplib.h"
83
84 extern int yychar;              /*  the lookahead symbol                */
85 extern YYSTYPE yylval;          /*  the semantic value of the           */
86                                 /*  lookahead symbol                    */
87
88 /* These flags are used by c-lex.c.  In C++, they're always off and on,
89    respectively.  */
90 int warn_traditional = 0;
91 int flag_digraphs = 1;
92
93 /* the declaration found for the last IDENTIFIER token read in.
94    yylex must look this up to detect typedefs, which get token type TYPENAME,
95    so it is left around in case the identifier is not a typedef but is
96    used in a context which makes it a reference to a variable.  */
97 tree lastiddecl;
98
99 /* Array for holding counts of the numbers of tokens seen.  */
100 extern int *token_count;
101
102 /* Functions and data structures for #pragma interface.
103
104    `#pragma implementation' means that the main file being compiled
105    is considered to implement (provide) the classes that appear in
106    its main body.  I.e., if this is file "foo.cc", and class `bar'
107    is defined in "foo.cc", then we say that "foo.cc implements bar".
108
109    All main input files "implement" themselves automagically.
110
111    `#pragma interface' means that unless this file (of the form "foo.h"
112    is not presently being included by file "foo.cc", the
113    CLASSTYPE_INTERFACE_ONLY bit gets set.  The effect is that none
114    of the vtables nor any of the inline functions defined in foo.h
115    will ever be output.
116
117    There are cases when we want to link files such as "defs.h" and
118    "main.cc".  In this case, we give "defs.h" a `#pragma interface',
119    and "main.cc" has `#pragma implementation "defs.h"'.  */
120
121 struct impl_files
122 {
123   const char *filename;
124   struct impl_files *next;
125 };
126
127 static struct impl_files *impl_file_chain;
128
129 \f
130 /* Return something to represent absolute declarators containing a *.
131    TARGET is the absolute declarator that the * contains.
132    CV_QUALIFIERS is a list of modifiers such as const or volatile
133    to apply to the pointer type, represented as identifiers.
134
135    We return an INDIRECT_REF whose "contents" are TARGET
136    and whose type is the modifier list.  */
137
138 tree
139 make_pointer_declarator (cv_qualifiers, target)
140      tree cv_qualifiers, target;
141 {
142   if (target && TREE_CODE (target) == IDENTIFIER_NODE
143       && ANON_AGGRNAME_P (target))
144     error ("type name expected before `*'");
145   target = build_nt (INDIRECT_REF, target);
146   TREE_TYPE (target) = cv_qualifiers;
147   return target;
148 }
149
150 /* Return something to represent absolute declarators containing a &.
151    TARGET is the absolute declarator that the & contains.
152    CV_QUALIFIERS is a list of modifiers such as const or volatile
153    to apply to the reference type, represented as identifiers.
154
155    We return an ADDR_EXPR whose "contents" are TARGET
156    and whose type is the modifier list.  */
157
158 tree
159 make_reference_declarator (cv_qualifiers, target)
160      tree cv_qualifiers, target;
161 {
162   if (target)
163     {
164       if (TREE_CODE (target) == ADDR_EXPR)
165         {
166           error ("cannot declare references to references");
167           return target;
168         }
169       if (TREE_CODE (target) == INDIRECT_REF)
170         {
171           error ("cannot declare pointers to references");
172           return target;
173         }
174       if (TREE_CODE (target) == IDENTIFIER_NODE && ANON_AGGRNAME_P (target))
175           error ("type name expected before `&'");
176     }
177   target = build_nt (ADDR_EXPR, target);
178   TREE_TYPE (target) = cv_qualifiers;
179   return target;
180 }
181
182 tree
183 make_call_declarator (target, parms, cv_qualifiers, exception_specification)
184      tree target, parms, cv_qualifiers, exception_specification;
185 {
186   target = build_nt (CALL_EXPR, target,
187                      tree_cons (parms, cv_qualifiers, NULL_TREE),
188                      /* The third operand is really RTL.  We
189                         shouldn't put anything there.  */
190                      NULL_TREE);
191   CALL_DECLARATOR_EXCEPTION_SPEC (target) = exception_specification;
192   return target;
193 }
194
195 void
196 set_quals_and_spec (call_declarator, cv_qualifiers, exception_specification)
197      tree call_declarator, cv_qualifiers, exception_specification;
198 {
199   CALL_DECLARATOR_QUALS (call_declarator) = cv_qualifiers;
200   CALL_DECLARATOR_EXCEPTION_SPEC (call_declarator) = exception_specification;
201 }
202 \f
203 int interface_only;             /* whether or not current file is only for
204                                    interface definitions.  */
205 int interface_unknown;          /* whether or not we know this class
206                                    to behave according to #pragma interface.  */
207
208 /* Tree code classes. */
209
210 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
211
212 static const char cplus_tree_code_type[] = {
213   'x',
214 #include "cp-tree.def"
215 };
216 #undef DEFTREECODE
217
218 /* Table indexed by tree code giving number of expression
219    operands beyond the fixed part of the node structure.
220    Not used for types or decls.  */
221
222 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
223
224 static const int cplus_tree_code_length[] = {
225   0,
226 #include "cp-tree.def"
227 };
228 #undef DEFTREECODE
229
230 /* Names of tree components.
231    Used for printing out the tree and error messages.  */
232 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
233
234 static const char *const cplus_tree_code_name[] = {
235   "@@dummy",
236 #include "cp-tree.def"
237 };
238 #undef DEFTREECODE
239 \f
240 /* Post-switch processing.  */
241 void
242 cxx_post_options ()
243 {
244   cpp_post_options (parse_in);
245 }
246
247 void
248 cxx_init_options ()
249 {
250   parse_in = cpp_create_reader (CLK_GNUCXX);
251
252   /* Default exceptions on.  */
253   flag_exceptions = 1;
254   /* Mark as "unspecified".  */
255   flag_bounds_check = -1;
256   /* By default wrap lines at 80 characters.  Is getenv ("COLUMNS")
257      preferable?  */
258   diagnostic_line_cutoff (global_dc) = 80;
259   /* By default, emit location information once for every
260      diagnostic message.  */
261   diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
262 }
263
264 void
265 cxx_finish ()
266 {
267   if (flag_gnu_xref)
268     GNU_xref_end (errorcount + sorrycount);
269   c_common_finish ();
270 }
271
272 static int *
273 init_cpp_parse ()
274 {
275 #ifdef GATHER_STATISTICS
276 #ifdef REDUCE_LENGTH
277   reduce_count = (int *) xcalloc (sizeof (int), (REDUCE_LENGTH + 1));
278   reduce_count += 1;
279   token_count = (int *) xcalloc (sizeof (int), (TOKEN_LENGTH + 1));
280   token_count += 1;
281 #endif
282 #endif
283   return token_count;
284 }
285
286 /* A mapping from tree codes to operator name information.  */
287 operator_name_info_t operator_name_info[(int) LAST_CPLUS_TREE_CODE];
288 /* Similar, but for assignment operators.  */
289 operator_name_info_t assignment_operator_name_info[(int) LAST_CPLUS_TREE_CODE];
290
291 /* Initialize data structures that keep track of operator names.  */
292
293 #define DEF_OPERATOR(NAME, C, M, AR, AP) \
294  CONSTRAINT (C, sizeof "operator " + sizeof NAME <= 256);
295 #include "operators.def"
296 #undef DEF_OPERATOR
297
298 static void
299 init_operators ()
300 {
301   tree identifier;
302   char buffer[256];
303   struct operator_name_info_t *oni;
304
305 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)                   \
306   sprintf (buffer, ISALPHA (NAME[0]) ? "operator %s" : "operator%s", NAME); \
307   identifier = get_identifier (buffer);                                     \
308   IDENTIFIER_OPNAME_P (identifier) = 1;                                     \
309                                                                             \
310   oni = (ASSN_P                                                             \
311          ? &assignment_operator_name_info[(int) CODE]                       \
312          : &operator_name_info[(int) CODE]);                                \
313   oni->identifier = identifier;                                             \
314   oni->name = NAME;                                                         \
315   oni->mangled_name = MANGLING;
316
317 #include "operators.def"
318 #undef DEF_OPERATOR
319
320   operator_name_info[(int) ERROR_MARK].identifier
321     = get_identifier ("<invalid operator>");
322
323   /* Handle some special cases.  These operators are not defined in
324      the language, but can be produced internally.  We may need them
325      for error-reporting.  (Eventually, we should ensure that this
326      does not happen.  Error messages involving these operators will
327      be confusing to users.)  */
328
329   operator_name_info [(int) INIT_EXPR].name
330     = operator_name_info [(int) MODIFY_EXPR].name;
331   operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
332   operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
333   operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
334   operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
335   operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
336   operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
337   operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
338   operator_name_info [(int) ABS_EXPR].name = "abs";
339   operator_name_info [(int) FFS_EXPR].name = "ffs";
340   operator_name_info [(int) BIT_ANDTC_EXPR].name = "&~";
341   operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
342   operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
343   operator_name_info [(int) IN_EXPR].name = "in";
344   operator_name_info [(int) RANGE_EXPR].name = "...";
345   operator_name_info [(int) CONVERT_EXPR].name = "+";
346
347   assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
348     = "(exact /=)";
349   assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
350     = "(ceiling /=)";
351   assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
352     = "(floor /=)";
353   assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
354     = "(round /=)";
355   assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
356     = "(ceiling %=)";
357   assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
358     = "(floor %=)";
359   assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
360     = "(round %=)";
361 }
362
363 /* The reserved keyword table.  */
364 struct resword
365 {
366   const char *const word;
367   const ENUM_BITFIELD(rid) rid : 16;
368   const unsigned int disable   : 16;
369 };
370
371 /* Disable mask.  Keywords are disabled if (reswords[i].disable & mask) is
372    _true_.  */
373 #define D_EXT           0x01    /* GCC extension */
374 #define D_ASM           0x02    /* in C99, but has a switch to turn it off */
375 #define D_OPNAME        0x04    /* operator names */
376
377 CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
378
379 static const struct resword reswords[] =
380 {
381   { "_Complex",         RID_COMPLEX,    0 },
382   { "__FUNCTION__",     RID_FUNCTION_NAME, 0 },
383   { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
384   { "__alignof",        RID_ALIGNOF,    0 },
385   { "__alignof__",      RID_ALIGNOF,    0 },
386   { "__asm",            RID_ASM,        0 },
387   { "__asm__",          RID_ASM,        0 },
388   { "__attribute",      RID_ATTRIBUTE,  0 },
389   { "__attribute__",    RID_ATTRIBUTE,  0 },
390   { "__builtin_va_arg", RID_VA_ARG,     0 },
391   { "__complex",        RID_COMPLEX,    0 },
392   { "__complex__",      RID_COMPLEX,    0 },
393   { "__const",          RID_CONST,      0 },
394   { "__const__",        RID_CONST,      0 },
395   { "__extension__",    RID_EXTENSION,  0 },
396   { "__func__",         RID_C99_FUNCTION_NAME,  0 },
397   { "__imag",           RID_IMAGPART,   0 },
398   { "__imag__",         RID_IMAGPART,   0 },
399   { "__inline",         RID_INLINE,     0 },
400   { "__inline__",       RID_INLINE,     0 },
401   { "__label__",        RID_LABEL,      0 },
402   { "__null",           RID_NULL,       0 },
403   { "__real",           RID_REALPART,   0 },
404   { "__real__",         RID_REALPART,   0 },
405   { "__restrict",       RID_RESTRICT,   0 },
406   { "__restrict__",     RID_RESTRICT,   0 },
407   { "__signed",         RID_SIGNED,     0 },
408   { "__signed__",       RID_SIGNED,     0 },
409   { "__typeof",         RID_TYPEOF,     0 },
410   { "__typeof__",       RID_TYPEOF,     0 },
411   { "__volatile",       RID_VOLATILE,   0 },
412   { "__volatile__",     RID_VOLATILE,   0 },
413   { "asm",              RID_ASM,        D_ASM },
414   { "and",              RID_AND,        D_OPNAME },
415   { "and_eq",           RID_AND_EQ,     D_OPNAME },
416   { "auto",             RID_AUTO,       0 },
417   { "bitand",           RID_BITAND,     D_OPNAME },
418   { "bitor",            RID_BITOR,      D_OPNAME },
419   { "bool",             RID_BOOL,       0 },
420   { "break",            RID_BREAK,      0 },
421   { "case",             RID_CASE,       0 },
422   { "catch",            RID_CATCH,      0 },
423   { "char",             RID_CHAR,       0 },
424   { "class",            RID_CLASS,      0 },
425   { "compl",            RID_COMPL,      D_OPNAME },
426   { "const",            RID_CONST,      0 },
427   { "const_cast",       RID_CONSTCAST,  0 },
428   { "continue",         RID_CONTINUE,   0 },
429   { "default",          RID_DEFAULT,    0 },
430   { "delete",           RID_DELETE,     0 },
431   { "do",               RID_DO,         0 },
432   { "double",           RID_DOUBLE,     0 },
433   { "dynamic_cast",     RID_DYNCAST,    0 },
434   { "else",             RID_ELSE,       0 },
435   { "enum",             RID_ENUM,       0 },
436   { "explicit",         RID_EXPLICIT,   0 },
437   { "export",           RID_EXPORT,     0 },
438   { "extern",           RID_EXTERN,     0 },
439   { "false",            RID_FALSE,      0 },
440   { "float",            RID_FLOAT,      0 },
441   { "for",              RID_FOR,        0 },
442   { "friend",           RID_FRIEND,     0 },
443   { "goto",             RID_GOTO,       0 },
444   { "if",               RID_IF,         0 },
445   { "inline",           RID_INLINE,     0 },
446   { "int",              RID_INT,        0 },
447   { "long",             RID_LONG,       0 },
448   { "mutable",          RID_MUTABLE,    0 },
449   { "namespace",        RID_NAMESPACE,  0 },
450   { "new",              RID_NEW,        0 },
451   { "not",              RID_NOT,        D_OPNAME },
452   { "not_eq",           RID_NOT_EQ,     D_OPNAME },
453   { "operator",         RID_OPERATOR,   0 },
454   { "or",               RID_OR,         D_OPNAME },
455   { "or_eq",            RID_OR_EQ,      D_OPNAME },
456   { "private",          RID_PRIVATE,    0 },
457   { "protected",        RID_PROTECTED,  0 },
458   { "public",           RID_PUBLIC,     0 },
459   { "register",         RID_REGISTER,   0 },
460   { "reinterpret_cast", RID_REINTCAST,  0 },
461   { "return",           RID_RETURN,     0 },
462   { "short",            RID_SHORT,      0 },
463   { "signed",           RID_SIGNED,     0 },
464   { "sizeof",           RID_SIZEOF,     0 },
465   { "static",           RID_STATIC,     0 },
466   { "static_cast",      RID_STATCAST,   0 },
467   { "struct",           RID_STRUCT,     0 },
468   { "switch",           RID_SWITCH,     0 },
469   { "template",         RID_TEMPLATE,   0 },
470   { "this",             RID_THIS,       0 },
471   { "throw",            RID_THROW,      0 },
472   { "true",             RID_TRUE,       0 },
473   { "try",              RID_TRY,        0 },
474   { "typedef",          RID_TYPEDEF,    0 },
475   { "typename",         RID_TYPENAME,   0 },
476   { "typeid",           RID_TYPEID,     0 },
477   { "typeof",           RID_TYPEOF,     D_ASM|D_EXT },
478   { "union",            RID_UNION,      0 },
479   { "unsigned",         RID_UNSIGNED,   0 },
480   { "using",            RID_USING,      0 },
481   { "virtual",          RID_VIRTUAL,    0 },
482   { "void",             RID_VOID,       0 },
483   { "volatile",         RID_VOLATILE,   0 },
484   { "wchar_t",          RID_WCHAR,      0 },
485   { "while",            RID_WHILE,      0 },
486   { "xor",              RID_XOR,        D_OPNAME },
487   { "xor_eq",           RID_XOR_EQ,     D_OPNAME },
488
489 };
490 #define N_reswords (sizeof reswords / sizeof (struct resword))
491
492 /* Table mapping from RID_* constants to yacc token numbers.
493    Unfortunately we have to have entries for all the keywords in all
494    three languages.  */
495 const short rid_to_yy[RID_MAX] =
496 {
497   /* RID_STATIC */      SCSPEC,
498   /* RID_UNSIGNED */    TYPESPEC,
499   /* RID_LONG */        TYPESPEC,
500   /* RID_CONST */       CV_QUALIFIER,
501   /* RID_EXTERN */      SCSPEC,
502   /* RID_REGISTER */    SCSPEC,
503   /* RID_TYPEDEF */     SCSPEC,
504   /* RID_SHORT */       TYPESPEC,
505   /* RID_INLINE */      SCSPEC,
506   /* RID_VOLATILE */    CV_QUALIFIER,
507   /* RID_SIGNED */      TYPESPEC,
508   /* RID_AUTO */        SCSPEC,
509   /* RID_RESTRICT */    CV_QUALIFIER,
510
511   /* C extensions.  Bounded pointers are not yet in C++ */
512   /* RID_BOUNDED */     0,
513   /* RID_UNBOUNDED */   0,
514   /* RID_COMPLEX */     TYPESPEC,
515
516   /* C++ */
517   /* RID_FRIEND */      SCSPEC,
518   /* RID_VIRTUAL */     SCSPEC,
519   /* RID_EXPLICIT */    SCSPEC,
520   /* RID_EXPORT */      EXPORT,
521   /* RID_MUTABLE */     SCSPEC,
522
523   /* ObjC */
524   /* RID_IN */          0,
525   /* RID_OUT */         0,
526   /* RID_INOUT */       0,
527   /* RID_BYCOPY */      0,
528   /* RID_BYREF */       0,
529   /* RID_ONEWAY */      0,
530
531   /* C */
532   /* RID_INT */         TYPESPEC,
533   /* RID_CHAR */        TYPESPEC,
534   /* RID_FLOAT */       TYPESPEC,
535   /* RID_DOUBLE */      TYPESPEC,
536   /* RID_VOID */        TYPESPEC,
537   /* RID_ENUM */        ENUM,
538   /* RID_STRUCT */      AGGR,
539   /* RID_UNION */       AGGR,
540   /* RID_IF */          IF,
541   /* RID_ELSE */        ELSE,
542   /* RID_WHILE */       WHILE,
543   /* RID_DO */          DO,
544   /* RID_FOR */         FOR,
545   /* RID_SWITCH */      SWITCH,
546   /* RID_CASE */        CASE,
547   /* RID_DEFAULT */     DEFAULT,
548   /* RID_BREAK */       BREAK,
549   /* RID_CONTINUE */    CONTINUE,
550   /* RID_RETURN */      RETURN_KEYWORD,
551   /* RID_GOTO */        GOTO,
552   /* RID_SIZEOF */      SIZEOF,
553
554   /* C extensions */
555   /* RID_ASM */         ASM_KEYWORD,
556   /* RID_TYPEOF */      TYPEOF,
557   /* RID_ALIGNOF */     ALIGNOF,
558   /* RID_ATTRIBUTE */   ATTRIBUTE,
559   /* RID_VA_ARG */      VA_ARG,
560   /* RID_EXTENSION */   EXTENSION,
561   /* RID_IMAGPART */    IMAGPART,
562   /* RID_REALPART */    REALPART,
563   /* RID_LABEL */       LABEL,
564   /* RID_PTRBASE */     0,
565   /* RID_PTREXTENT */   0,
566   /* RID_PTRVALUE */    0,
567
568   /* RID_FUNCTION_NAME */       VAR_FUNC_NAME,
569   /* RID_PRETTY_FUNCTION_NAME */ VAR_FUNC_NAME,
570   /* RID_c99_FUNCTION_NAME */   VAR_FUNC_NAME,
571
572   /* C++ */
573   /* RID_BOOL */        TYPESPEC,
574   /* RID_WCHAR */       TYPESPEC,
575   /* RID_CLASS */       AGGR,
576   /* RID_PUBLIC */      VISSPEC,
577   /* RID_PRIVATE */     VISSPEC,
578   /* RID_PROTECTED */   VISSPEC,
579   /* RID_TEMPLATE */    TEMPLATE,
580   /* RID_NULL */        CONSTANT,
581   /* RID_CATCH */       CATCH,
582   /* RID_DELETE */      DELETE,
583   /* RID_FALSE */       CXX_FALSE,
584   /* RID_NAMESPACE */   NAMESPACE,
585   /* RID_NEW */         NEW,
586   /* RID_OPERATOR */    OPERATOR,
587   /* RID_THIS */        THIS,
588   /* RID_THROW */       THROW,
589   /* RID_TRUE */        CXX_TRUE,
590   /* RID_TRY */         TRY,
591   /* RID_TYPENAME */    TYPENAME_KEYWORD,
592   /* RID_TYPEID */      TYPEID,
593   /* RID_USING */       USING,
594
595   /* casts */
596   /* RID_CONSTCAST */   CONST_CAST,
597   /* RID_DYNCAST */     DYNAMIC_CAST,
598   /* RID_REINTCAST */   REINTERPRET_CAST,
599   /* RID_STATCAST */    STATIC_CAST,
600
601   /* alternate spellings */
602   /* RID_AND */         ANDAND,
603   /* RID_AND_EQ */      ASSIGN,
604   /* RID_NOT */         '!',
605   /* RID_NOT_EQ */      EQCOMPARE,
606   /* RID_OR */          OROR,
607   /* RID_OR_EQ */       ASSIGN,
608   /* RID_XOR */         '^',
609   /* RID_XOR_EQ */      ASSIGN,
610   /* RID_BITAND */      '&',
611   /* RID_BITOR */       '|',
612   /* RID_COMPL */       '~',
613
614   /* Objective C */
615   /* RID_ID */                  0,
616   /* RID_AT_ENCODE */           0,
617   /* RID_AT_END */              0,
618   /* RID_AT_CLASS */            0,
619   /* RID_AT_ALIAS */            0,
620   /* RID_AT_DEFS */             0,
621   /* RID_AT_PRIVATE */          0,
622   /* RID_AT_PROTECTED */        0,
623   /* RID_AT_PUBLIC */           0,
624   /* RID_AT_PROTOCOL */         0,
625   /* RID_AT_SELECTOR */         0,
626   /* RID_AT_INTERFACE */        0,
627   /* RID_AT_IMPLEMENTATION */   0
628 };
629
630 void
631 init_reswords ()
632 {
633   unsigned int i;
634   tree id;
635   int mask = ((flag_operator_names ? 0 : D_OPNAME)
636               | (flag_no_asm ? D_ASM : 0)
637               | (flag_no_gnu_keywords ? D_EXT : 0));
638
639   /* It is not necessary to register ridpointers as a GC root, because
640      all the trees it points to are permanently interned in the
641      get_identifier hash anyway.  */
642   ridpointers = (tree *) xcalloc ((int) RID_MAX, sizeof (tree));
643   for (i = 0; i < N_reswords; i++)
644     {
645       id = get_identifier (reswords[i].word);
646       C_RID_CODE (id) = reswords[i].rid;
647       ridpointers [(int) reswords[i].rid] = id;
648       if (! (reswords[i].disable & mask))
649         C_IS_RESERVED_WORD (id) = 1;
650     }
651 }
652
653 static void
654 init_cp_pragma ()
655 {
656   cpp_register_pragma (parse_in, 0, "vtable", handle_pragma_vtable);
657   cpp_register_pragma (parse_in, 0, "unit", handle_pragma_unit);
658
659   cpp_register_pragma (parse_in, 0, "interface", handle_pragma_interface);
660   cpp_register_pragma (parse_in, 0, "implementation",
661                        handle_pragma_implementation);
662
663   cpp_register_pragma (parse_in, "GCC", "interface", handle_pragma_interface);
664   cpp_register_pragma (parse_in, "GCC", "implementation",
665                        handle_pragma_implementation);
666   cpp_register_pragma (parse_in, "GCC", "java_exceptions",
667                        handle_pragma_java_exceptions);
668 }
669
670 /* Initialize the C++ front end.  This function is very sensitive to
671    the exact order that things are done here.  It would be nice if the
672    initialization done by this routine were moved to its subroutines,
673    and the ordering dependencies clarified and reduced.  */
674 const char *
675 cxx_init (filename)
676      const char *filename;
677 {
678   decl_printable_name = lang_printable_name;
679   input_filename = "<internal>";
680
681   init_reswords ();
682   init_spew ();
683   init_tree ();
684   init_cplus_expand ();
685   init_cp_semantics ();
686
687   add_c_tree_codes ();
688
689   memcpy (tree_code_type + (int) LAST_C_TREE_CODE,
690           cplus_tree_code_type,
691           (int)LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE);
692   memcpy (tree_code_length + (int) LAST_C_TREE_CODE,
693           cplus_tree_code_length,
694           (LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE) * sizeof (int));
695   memcpy (tree_code_name + (int) LAST_C_TREE_CODE,
696           cplus_tree_code_name,
697           (LAST_CPLUS_TREE_CODE - (int)LAST_C_TREE_CODE) * sizeof (char *));
698
699   init_operators ();
700   init_method ();
701   init_error ();
702
703   current_function_decl = NULL;
704
705   class_type_node = build_int_2 (class_type, 0);
706   TREE_TYPE (class_type_node) = class_type_node;
707   ridpointers[(int) RID_CLASS] = class_type_node;
708
709   record_type_node = build_int_2 (record_type, 0);
710   TREE_TYPE (record_type_node) = record_type_node;
711   ridpointers[(int) RID_STRUCT] = record_type_node;
712
713   union_type_node = build_int_2 (union_type, 0);
714   TREE_TYPE (union_type_node) = union_type_node;
715   ridpointers[(int) RID_UNION] = union_type_node;
716
717   enum_type_node = build_int_2 (enum_type, 0);
718   TREE_TYPE (enum_type_node) = enum_type_node;
719   ridpointers[(int) RID_ENUM] = enum_type_node;
720
721   cxx_init_decl_processing ();
722
723   /* Create the built-in __null node.  Note that we can't yet call for
724      type_for_size here because integer_type_node and so forth are not
725      set up.  Therefore, we don't set the type of these nodes until
726      cxx_init_decl_processing.  */
727   null_node = build_int_2 (0, 0);
728   TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
729   ridpointers[RID_NULL] = null_node;
730
731   token_count = init_cpp_parse ();
732   interface_unknown = 1;
733
734   filename = c_common_lang_init (filename);
735
736   init_cp_pragma ();
737
738   if (flag_gnu_xref)
739     GNU_xref_begin (filename);
740   init_repo (filename);
741
742   return filename;
743 }
744 \f
745 inline void
746 yyprint (file, yychar, yylval)
747      FILE *file;
748      int yychar;
749      YYSTYPE yylval;
750 {
751   tree t;
752   switch (yychar)
753     {
754     case IDENTIFIER:
755     case TYPENAME:
756     case TYPESPEC:
757     case PTYPENAME:
758     case PFUNCNAME:
759     case IDENTIFIER_DEFN:
760     case TYPENAME_DEFN:
761     case PTYPENAME_DEFN:
762     case SCSPEC:
763     case PRE_PARSED_CLASS_DECL:
764       t = yylval.ttype;
765       if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
766         {
767           fprintf (file, " `%s'", IDENTIFIER_POINTER (DECL_NAME (t)));
768           break;
769         }
770       my_friendly_assert (TREE_CODE (t) == IDENTIFIER_NODE, 224);
771       if (IDENTIFIER_POINTER (t))
772           fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
773       break;
774
775     case AGGR:
776       if (yylval.ttype == class_type_node)
777         fprintf (file, " `class'");
778       else if (yylval.ttype == record_type_node)
779         fprintf (file, " `struct'");
780       else if (yylval.ttype == union_type_node)
781         fprintf (file, " `union'");
782       else if (yylval.ttype == enum_type_node)
783         fprintf (file, " `enum'");
784       else
785         my_friendly_abort (80);
786       break;
787
788     case CONSTANT:
789       t = yylval.ttype;
790       if (TREE_CODE (t) == INTEGER_CST)
791         fprintf (file,
792 #if HOST_BITS_PER_WIDE_INT == 64
793 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
794                  " 0x%x%016x",
795 #else
796 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
797                  " 0x%lx%016lx",
798 #else
799                  " 0x%llx%016llx",
800 #endif
801 #endif
802 #else
803 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
804                  " 0x%lx%08lx",
805 #else
806                  " 0x%x%08x",
807 #endif
808 #endif
809                  TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
810       break;
811     }
812 }
813
814 #if defined(GATHER_STATISTICS) && defined(REDUCE_LENGTH)
815 static int *reduce_count;
816 #endif
817
818 int *token_count;
819
820 #if 0
821 #define REDUCE_LENGTH (sizeof (yyr2) / sizeof (yyr2[0]))
822 #define TOKEN_LENGTH (256 + sizeof (yytname) / sizeof (yytname[0]))
823 #endif
824
825 #ifdef GATHER_STATISTICS
826 #ifdef REDUCE_LENGTH
827 void
828 yyhook (yyn)
829      int yyn;
830 {
831   reduce_count[yyn] += 1;
832 }
833
834 static int
835 reduce_cmp (p, q)
836      int *p, *q;
837 {
838   return reduce_count[*q] - reduce_count[*p];
839 }
840
841 static int
842 token_cmp (p, q)
843      int *p, *q;
844 {
845   return token_count[*q] - token_count[*p];
846 }
847 #endif
848 #endif
849
850 void
851 print_parse_statistics ()
852 {
853 #ifdef GATHER_STATISTICS
854 #ifdef REDUCE_LENGTH
855 #if YYDEBUG != 0
856   int i;
857   int maxlen = REDUCE_LENGTH;
858   unsigned *sorted;
859
860   if (reduce_count[-1] == 0)
861     return;
862
863   if (TOKEN_LENGTH > REDUCE_LENGTH)
864     maxlen = TOKEN_LENGTH;
865   sorted = (unsigned *) alloca (sizeof (int) * maxlen);
866
867   for (i = 0; i < TOKEN_LENGTH; i++)
868     sorted[i] = i;
869   qsort (sorted, TOKEN_LENGTH, sizeof (int), token_cmp);
870   for (i = 0; i < TOKEN_LENGTH; i++)
871     {
872       int idx = sorted[i];
873       if (token_count[idx] == 0)
874         break;
875       if (token_count[idx] < token_count[-1])
876         break;
877       fprintf (stderr, "token %d, `%s', count = %d\n",
878                idx, yytname[YYTRANSLATE (idx)], token_count[idx]);
879     }
880   fprintf (stderr, "\n");
881   for (i = 0; i < REDUCE_LENGTH; i++)
882     sorted[i] = i;
883   qsort (sorted, REDUCE_LENGTH, sizeof (int), reduce_cmp);
884   for (i = 0; i < REDUCE_LENGTH; i++)
885     {
886       int idx = sorted[i];
887       if (reduce_count[idx] == 0)
888         break;
889       if (reduce_count[idx] < reduce_count[-1])
890         break;
891       fprintf (stderr, "rule %d, line %d, count = %d\n",
892                idx, yyrline[idx], reduce_count[idx]);
893     }
894   fprintf (stderr, "\n");
895 #endif
896 #endif
897 #endif
898 }
899
900 /* Sets the value of the 'yydebug' variable to VALUE.
901    This is a function so we don't have to have YYDEBUG defined
902    in order to build the compiler.  */
903
904 void
905 cxx_set_yydebug (value)
906      int value;
907 {
908 #if YYDEBUG != 0
909   extern int yydebug;
910   yydebug = value;
911 #else
912   warning ("YYDEBUG not defined.");
913 #endif
914 }
915
916 /* Helper function to load global variables with interface
917    information.  */
918
919 void
920 extract_interface_info ()
921 {
922   struct c_fileinfo *finfo = 0;
923
924   if (flag_alt_external_templates)
925     {
926       tree til = tinst_for_decl ();
927
928       if (til)
929         finfo = get_fileinfo (TINST_FILE (til));
930     }
931   if (!finfo)
932     finfo = get_fileinfo (input_filename);
933
934   interface_only = finfo->interface_only;
935   interface_unknown = finfo->interface_unknown;
936
937   /* This happens to be a convenient place to put this.  */
938   if (flag_gnu_xref) GNU_xref_file (input_filename);
939 }
940
941 /* Return nonzero if S is not considered part of an
942    INTERFACE/IMPLEMENTATION pair.  Otherwise, return 0.  */
943
944 static int
945 interface_strcmp (s)
946      const char *s;
947 {
948   /* Set the interface/implementation bits for this scope.  */
949   struct impl_files *ifiles;
950   const char *s1;
951
952   for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
953     {
954       const char *t1 = ifiles->filename;
955       s1 = s;
956
957       if (*s1 != *t1 || *s1 == 0)
958         continue;
959
960       while (*s1 == *t1 && *s1 != 0)
961         s1++, t1++;
962
963       /* A match.  */
964       if (*s1 == *t1)
965         return 0;
966
967       /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc.  */
968       if (strchr (s1, '.') || strchr (t1, '.'))
969         continue;
970
971       if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
972         continue;
973
974       /* A match.  */
975       return 0;
976     }
977
978   /* No matches.  */
979   return 1;
980 }
981
982 /* Heuristic to tell whether the user is missing a semicolon
983    after a struct or enum declaration.  Emit an error message
984    if we know the user has blown it.  */
985
986 void
987 check_for_missing_semicolon (type)
988      tree type;
989 {
990   if (yychar < 0)
991     yychar = yylex ();
992
993   if ((yychar > 255
994        && yychar != SCSPEC
995        && yychar != IDENTIFIER
996        && yychar != TYPENAME
997        && yychar != CV_QUALIFIER
998        && yychar != SELFNAME)
999       || yychar == 0  /* EOF */)
1000     {
1001       if (TYPE_ANONYMOUS_P (type))
1002         error ("semicolon missing after %s declaration",
1003                TREE_CODE (type) == ENUMERAL_TYPE ? "enum" : "struct");
1004       else
1005         cp_error ("semicolon missing after declaration of `%T'", type);
1006       shadow_tag (build_tree_list (0, type));
1007     }
1008   /* Could probably also hack cases where class { ... } f (); appears.  */
1009   clear_anon_tags ();
1010 }
1011
1012 void
1013 note_got_semicolon (type)
1014      tree type;
1015 {
1016   if (!TYPE_P (type))
1017     my_friendly_abort (60);
1018   if (CLASS_TYPE_P (type))
1019     CLASSTYPE_GOT_SEMICOLON (type) = 1;
1020 }
1021
1022 void
1023 note_list_got_semicolon (declspecs)
1024      tree declspecs;
1025 {
1026   tree link;
1027
1028   for (link = declspecs; link; link = TREE_CHAIN (link))
1029     {
1030       tree type = TREE_VALUE (link);
1031       if (TYPE_P (type))
1032         note_got_semicolon (type);
1033     }
1034   clear_anon_tags ();
1035 }
1036 \f
1037
1038 /* Parse a #pragma whose sole argument is a string constant.
1039    If OPT is true, the argument is optional.  */
1040 static tree
1041 parse_strconst_pragma (name, opt)
1042      const char *name;
1043      int opt;
1044 {
1045   tree result, x;
1046   enum cpp_ttype t;
1047
1048   t = c_lex (&x);
1049   if (t == CPP_STRING)
1050     {
1051       result = x;
1052       if (c_lex (&x) != CPP_EOF)
1053         warning ("junk at end of #pragma %s", name);
1054       return result;
1055     }
1056
1057   if (t == CPP_EOF && opt)
1058     return 0;
1059
1060   error ("invalid #pragma %s", name);
1061   return (tree)-1;
1062 }
1063
1064 static void
1065 handle_pragma_vtable (dfile)
1066      cpp_reader *dfile ATTRIBUTE_UNUSED;
1067 {
1068   parse_strconst_pragma ("vtable", 0);
1069   sorry ("#pragma vtable no longer supported");
1070 }
1071
1072 static void
1073 handle_pragma_unit (dfile)
1074      cpp_reader *dfile ATTRIBUTE_UNUSED;
1075 {
1076   /* Validate syntax, but don't do anything.  */
1077   parse_strconst_pragma ("unit", 0);
1078 }
1079
1080 static void
1081 handle_pragma_interface (dfile)
1082      cpp_reader *dfile ATTRIBUTE_UNUSED;
1083 {
1084   tree fname = parse_strconst_pragma ("interface", 1);
1085   struct c_fileinfo *finfo;
1086   const char *main_filename;
1087
1088   if (fname == (tree)-1)
1089     return;
1090   else if (fname == 0)
1091     main_filename = lbasename (input_filename);
1092   else
1093     main_filename = TREE_STRING_POINTER (fname);
1094
1095   finfo = get_fileinfo (input_filename);
1096
1097   if (impl_file_chain == 0)
1098     {
1099       /* If this is zero at this point, then we are
1100          auto-implementing.  */
1101       if (main_input_filename == 0)
1102         main_input_filename = input_filename;
1103     }
1104
1105   interface_only = interface_strcmp (main_filename);
1106 #ifdef MULTIPLE_SYMBOL_SPACES
1107   if (! interface_only)
1108 #endif
1109     interface_unknown = 0;
1110
1111   finfo->interface_only = interface_only;
1112   finfo->interface_unknown = interface_unknown;
1113 }
1114
1115 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
1116    We used to only allow this at toplevel, but that restriction was buggy
1117    in older compilers and it seems reasonable to allow it in the headers
1118    themselves, too.  It only needs to precede the matching #p interface.
1119
1120    We don't touch interface_only or interface_unknown; the user must specify
1121    a matching #p interface for this to have any effect.  */
1122
1123 static void
1124 handle_pragma_implementation (dfile)
1125      cpp_reader *dfile ATTRIBUTE_UNUSED;
1126 {
1127   tree fname = parse_strconst_pragma ("implementation", 1);
1128   const char *main_filename;
1129   struct impl_files *ifiles = impl_file_chain;
1130
1131   if (fname == (tree)-1)
1132     return;
1133
1134   if (fname == 0)
1135     {
1136       if (main_input_filename)
1137         main_filename = main_input_filename;
1138       else
1139         main_filename = input_filename;
1140       main_filename = lbasename (main_filename);
1141     }
1142   else
1143     {
1144       main_filename = TREE_STRING_POINTER (fname);
1145       if (cpp_included (parse_in, main_filename))
1146         warning ("#pragma implementation for %s appears after file is included",
1147                  main_filename);
1148     }
1149
1150   for (; ifiles; ifiles = ifiles->next)
1151     {
1152       if (! strcmp (ifiles->filename, main_filename))
1153         break;
1154     }
1155   if (ifiles == 0)
1156     {
1157       ifiles = (struct impl_files*) xmalloc (sizeof (struct impl_files));
1158       ifiles->filename = main_filename;
1159       ifiles->next = impl_file_chain;
1160       impl_file_chain = ifiles;
1161     }
1162 }
1163
1164 /* Indicate that this file uses Java-personality exception handling.  */
1165 static void
1166 handle_pragma_java_exceptions (dfile)
1167      cpp_reader *dfile ATTRIBUTE_UNUSED;
1168 {
1169   tree x;
1170   if (c_lex (&x) != CPP_EOF)
1171     warning ("junk at end of #pragma GCC java_exceptions");
1172
1173   choose_personality_routine (lang_java);
1174 }
1175
1176 void
1177 do_pending_lang_change ()
1178 {
1179   for (; pending_lang_change > 0; --pending_lang_change)
1180     push_lang_context (lang_name_c);
1181   for (; pending_lang_change < 0; ++pending_lang_change)
1182     pop_lang_context ();
1183 }
1184
1185 /* Return true if d is in a global scope. */
1186
1187 static int
1188 is_global (d)
1189   tree d;
1190 {
1191   while (1)
1192     switch (TREE_CODE (d))
1193       {
1194       case ERROR_MARK:
1195         return 1;
1196
1197       case OVERLOAD: d = OVL_FUNCTION (d); continue;
1198       case TREE_LIST: d = TREE_VALUE (d); continue;
1199       default:
1200         my_friendly_assert (DECL_P (d), 980629);
1201
1202         return DECL_NAMESPACE_SCOPE_P (d);
1203       }
1204 }
1205
1206 tree
1207 do_identifier (token, parsing, args)
1208      register tree token;
1209      int parsing;
1210      tree args;
1211 {
1212   register tree id;
1213   int lexing = (parsing == 1);
1214
1215   if (! lexing)
1216     id = lookup_name (token, 0);
1217   else
1218     id = lastiddecl;
1219
1220   /* Do Koenig lookup if appropriate (inside templates we build lookup
1221      expressions instead).
1222
1223      [basic.lookup.koenig]: If the ordinary unqualified lookup of the name
1224      finds the declaration of a class member function, the associated
1225      namespaces and classes are not considered.  */
1226
1227   if (args && !current_template_parms && (!id || is_global (id)))
1228     id = lookup_arg_dependent (token, id, args);
1229
1230   /* Remember that this name has been used in the class definition, as per
1231      [class.scope0] */
1232   if (id && parsing)
1233     maybe_note_name_used_in_class (token, id);
1234
1235   if (id == error_mark_node)
1236     {
1237       /* lookup_name quietly returns error_mark_node if we're parsing,
1238          as we don't want to complain about an identifier that ends up
1239          being used as a declarator.  So we call it again to get the error
1240          message.  */
1241       id = lookup_name (token, 0);
1242       return error_mark_node;
1243     }
1244
1245   if (!id || (TREE_CODE (id) == FUNCTION_DECL
1246               && DECL_ANTICIPATED (id)))
1247     {
1248       if (current_template_parms)
1249         return build_min_nt (LOOKUP_EXPR, token);
1250       else if (IDENTIFIER_OPNAME_P (token))
1251         {
1252           if (token != ansi_opname (ERROR_MARK))
1253             cp_error ("`%D' not defined", token);
1254           id = error_mark_node;
1255         }
1256       else if (current_function_decl == 0)
1257         {
1258           cp_error ("`%D' was not declared in this scope", token);
1259           id = error_mark_node;
1260         }
1261       else
1262         {
1263           if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node
1264               || IDENTIFIER_ERROR_LOCUS (token) != current_function_decl)
1265             {
1266               static int undeclared_variable_notice;
1267
1268               cp_error ("`%D' undeclared (first use this function)", token);
1269
1270               if (! undeclared_variable_notice)
1271                 {
1272                   error ("(Each undeclared identifier is reported only once for each function it appears in.)");
1273                   undeclared_variable_notice = 1;
1274                 }
1275             }
1276           id = error_mark_node;
1277           /* Prevent repeated error messages.  */
1278           SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node);
1279           SET_IDENTIFIER_ERROR_LOCUS (token, current_function_decl);
1280         }
1281     }
1282
1283   if (TREE_CODE (id) == VAR_DECL && DECL_DEAD_FOR_LOCAL (id))
1284     {
1285       tree shadowed = DECL_SHADOWED_FOR_VAR (id);
1286       while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
1287              && DECL_DEAD_FOR_LOCAL (shadowed))
1288         shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
1289       if (!shadowed)
1290         shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (id));
1291       if (shadowed)
1292         {
1293           if (!DECL_ERROR_REPORTED (id))
1294             {
1295               warning ("name lookup of `%s' changed",
1296                        IDENTIFIER_POINTER (token));
1297               cp_warning_at ("  matches this `%D' under ISO standard rules",
1298                              shadowed);
1299               cp_warning_at ("  matches this `%D' under old rules", id);
1300               DECL_ERROR_REPORTED (id) = 1;
1301             }
1302           id = shadowed;
1303         }
1304       else if (!DECL_ERROR_REPORTED (id))
1305         {
1306           DECL_ERROR_REPORTED (id) = 1;
1307           if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (id)))
1308             {
1309               error ("name lookup of `%s' changed for new ISO `for' scoping",
1310                      IDENTIFIER_POINTER (token));
1311               cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", id);
1312               id = error_mark_node;
1313             }
1314           else
1315             {
1316               pedwarn ("name lookup of `%s' changed for new ISO `for' scoping",
1317                        IDENTIFIER_POINTER (token));
1318               cp_pedwarn_at ("  using obsolete binding at `%D'", id);
1319             }
1320         }
1321     }
1322   /* TREE_USED is set in `hack_identifier'.  */
1323   if (TREE_CODE (id) == CONST_DECL)
1324     {
1325       /* Check access.  */
1326       if (IDENTIFIER_CLASS_VALUE (token) == id)
1327         enforce_access (CP_DECL_CONTEXT(id), id);
1328       if (!processing_template_decl || DECL_TEMPLATE_PARM_P (id))
1329         id = DECL_INITIAL (id);
1330     }
1331   else
1332     id = hack_identifier (id, token);
1333
1334   /* We must look up dependent names when the template is
1335      instantiated, not while parsing it.  For now, we don't
1336      distinguish between dependent and independent names.  So, for
1337      example, we look up all overloaded functions at
1338      instantiation-time, even though in some cases we should just use
1339      the DECL we have here.  We also use LOOKUP_EXPRs to find things
1340      like local variables, rather than creating TEMPLATE_DECLs for the
1341      local variables and then finding matching instantiations.  */
1342   if (current_template_parms
1343       && (is_overloaded_fn (id)
1344           || (TREE_CODE (id) == VAR_DECL
1345               && CP_DECL_CONTEXT (id)
1346               && TREE_CODE (CP_DECL_CONTEXT (id)) == FUNCTION_DECL)
1347           || TREE_CODE (id) == PARM_DECL
1348           || TREE_CODE (id) == RESULT_DECL
1349           || TREE_CODE (id) == USING_DECL))
1350     id = build_min_nt (LOOKUP_EXPR, token);
1351
1352   return id;
1353 }
1354
1355 tree
1356 do_scoped_id (token, parsing)
1357      tree token;
1358      int parsing;
1359 {
1360   tree id;
1361   /* during parsing, this is ::name. Otherwise, it is black magic. */
1362   if (parsing)
1363     {
1364       id = make_node (CPLUS_BINDING);
1365       if (!qualified_lookup_using_namespace (token, global_namespace, id, 0))
1366         id = NULL_TREE;
1367       else
1368         id = BINDING_VALUE (id);
1369     }
1370   else
1371     id = IDENTIFIER_GLOBAL_VALUE (token);
1372   if (parsing && yychar == YYEMPTY)
1373     yychar = yylex ();
1374   if (! id)
1375     {
1376       if (processing_template_decl)
1377         {
1378           id = build_min_nt (LOOKUP_EXPR, token);
1379           LOOKUP_EXPR_GLOBAL (id) = 1;
1380           return id;
1381         }
1382       if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node)
1383         cp_error ("`::%D' undeclared (first use here)", token);
1384       id = error_mark_node;
1385       /* Prevent repeated error messages.  */
1386       SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node);
1387     }
1388   else
1389     {
1390       if (TREE_CODE (id) == ADDR_EXPR)
1391         mark_used (TREE_OPERAND (id, 0));
1392       else if (TREE_CODE (id) != OVERLOAD)
1393         mark_used (id);
1394     }
1395   if (TREE_CODE (id) == CONST_DECL && ! processing_template_decl)
1396     {
1397       /* XXX CHS - should we set TREE_USED of the constant? */
1398       id = DECL_INITIAL (id);
1399       /* This is to prevent an enum whose value is 0
1400          from being considered a null pointer constant.  */
1401       id = build1 (NOP_EXPR, TREE_TYPE (id), id);
1402       TREE_CONSTANT (id) = 1;
1403     }
1404
1405   if (processing_template_decl)
1406     {
1407       if (is_overloaded_fn (id))
1408         {
1409           id = build_min_nt (LOOKUP_EXPR, token);
1410           LOOKUP_EXPR_GLOBAL (id) = 1;
1411           return id;
1412         }
1413       /* else just use the decl */
1414     }
1415   return convert_from_reference (id);
1416 }
1417
1418 tree
1419 identifier_typedecl_value (node)
1420      tree node;
1421 {
1422   tree t, type;
1423   type = IDENTIFIER_TYPE_VALUE (node);
1424   if (type == NULL_TREE)
1425     return NULL_TREE;
1426
1427   if (IDENTIFIER_BINDING (node))
1428     {
1429       t = IDENTIFIER_VALUE (node);
1430       if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
1431         return t;
1432     }
1433   if (IDENTIFIER_NAMESPACE_VALUE (node))
1434     {
1435       t = IDENTIFIER_NAMESPACE_VALUE (node);
1436       if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
1437         return t;
1438     }
1439
1440   /* Will this one ever happen?  */
1441   if (TYPE_MAIN_DECL (type))
1442     return TYPE_MAIN_DECL (type);
1443
1444   /* We used to do an internal error of 62 here, but instead we will
1445      handle the return of a null appropriately in the callers.  */
1446   return NULL_TREE;
1447 }
1448
1449 #ifdef GATHER_STATISTICS
1450 /* The original for tree_node_kind is in the toplevel tree.c; changes there
1451    need to be brought into here, unless this were actually put into a header
1452    instead.  */
1453 /* Statistics-gathering stuff.  */
1454 typedef enum
1455 {
1456   d_kind,
1457   t_kind,
1458   b_kind,
1459   s_kind,
1460   r_kind,
1461   e_kind,
1462   c_kind,
1463   id_kind,
1464   op_id_kind,
1465   perm_list_kind,
1466   temp_list_kind,
1467   vec_kind,
1468   x_kind,
1469   lang_decl,
1470   lang_type,
1471   all_kinds
1472 } tree_node_kind;
1473
1474 extern int tree_node_counts[];
1475 extern int tree_node_sizes[];
1476 #endif
1477
1478 tree
1479 build_lang_decl (code, name, type)
1480      enum tree_code code;
1481      tree name;
1482      tree type;
1483 {
1484   tree t;
1485
1486   t = build_decl (code, name, type);
1487   retrofit_lang_decl (t);
1488
1489   return t;
1490 }
1491
1492 /* Add DECL_LANG_SPECIFIC info to T.  Called from build_lang_decl
1493    and pushdecl (for functions generated by the backend).  */
1494
1495 void
1496 retrofit_lang_decl (t)
1497      tree t;
1498 {
1499   struct lang_decl *ld;
1500   size_t size;
1501
1502   if (CAN_HAVE_FULL_LANG_DECL_P (t))
1503     size = sizeof (struct lang_decl);
1504   else
1505     size = sizeof (struct lang_decl_flags);
1506
1507   ld = (struct lang_decl *) ggc_alloc_cleared (size);
1508
1509   DECL_LANG_SPECIFIC (t) = ld;
1510   if (current_lang_name == lang_name_cplusplus)
1511     SET_DECL_LANGUAGE (t, lang_cplusplus);
1512   else if (current_lang_name == lang_name_c)
1513     SET_DECL_LANGUAGE (t, lang_c);
1514   else if (current_lang_name == lang_name_java)
1515     SET_DECL_LANGUAGE (t, lang_java);
1516   else my_friendly_abort (64);
1517
1518 #ifdef GATHER_STATISTICS
1519   tree_node_counts[(int)lang_decl] += 1;
1520   tree_node_sizes[(int)lang_decl] += size;
1521 #endif
1522 }
1523
1524 void
1525 copy_lang_decl (node)
1526      tree node;
1527 {
1528   int size;
1529   struct lang_decl *ld;
1530
1531   if (! DECL_LANG_SPECIFIC (node))
1532     return;
1533
1534   if (!CAN_HAVE_FULL_LANG_DECL_P (node))
1535     size = sizeof (struct lang_decl_flags);
1536   else
1537     size = sizeof (struct lang_decl);
1538   ld = (struct lang_decl *) ggc_alloc (size);
1539   memcpy (ld, DECL_LANG_SPECIFIC (node), size);
1540   DECL_LANG_SPECIFIC (node) = ld;
1541
1542 #ifdef GATHER_STATISTICS
1543   tree_node_counts[(int)lang_decl] += 1;
1544   tree_node_sizes[(int)lang_decl] += size;
1545 #endif
1546 }
1547
1548 /* Copy DECL, including any language-specific parts.  */
1549
1550 tree
1551 copy_decl (decl)
1552      tree decl;
1553 {
1554   tree copy;
1555
1556   copy = copy_node (decl);
1557   copy_lang_decl (copy);
1558   return copy;
1559 }
1560
1561 /* Replace the shared language-specific parts of NODE with a new copy.  */
1562
1563 static void
1564 copy_lang_type (node)
1565      tree node;
1566 {
1567   int size;
1568   struct lang_type *lt;
1569
1570   if (! TYPE_LANG_SPECIFIC (node))
1571     return;
1572
1573   size = sizeof (struct lang_type);
1574   lt = (struct lang_type *) ggc_alloc (size);
1575   memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
1576   TYPE_LANG_SPECIFIC (node) = lt;
1577
1578 #ifdef GATHER_STATISTICS
1579   tree_node_counts[(int)lang_type] += 1;
1580   tree_node_sizes[(int)lang_type] += size;
1581 #endif
1582 }
1583
1584 /* Copy TYPE, including any language-specific parts.  */
1585
1586 tree
1587 copy_type (type)
1588      tree type;
1589 {
1590   tree copy;
1591
1592   copy = copy_node (type);
1593   copy_lang_type (copy);
1594   return copy;
1595 }
1596
1597 tree
1598 cp_make_lang_type (code)
1599      enum tree_code code;
1600 {
1601   register tree t = make_node (code);
1602
1603   /* Create lang_type structure.  */
1604   if (IS_AGGR_TYPE_CODE (code)
1605       || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1606     {
1607       struct lang_type *pi;
1608
1609       pi = ((struct lang_type *)
1610             ggc_alloc_cleared (sizeof (struct lang_type)));
1611
1612       TYPE_LANG_SPECIFIC (t) = pi;
1613
1614 #ifdef GATHER_STATISTICS
1615       tree_node_counts[(int)lang_type] += 1;
1616       tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1617 #endif
1618     }
1619
1620   /* Set up some flags that give proper default behavior.  */
1621   if (IS_AGGR_TYPE_CODE (code))
1622     {
1623       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
1624       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1625
1626       /* Make sure this is laid out, for ease of use later.  In the
1627          presence of parse errors, the normal was of assuring this
1628          might not ever get executed, so we lay it out *immediately*.  */
1629       build_pointer_type (t);
1630     }
1631   else
1632     /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits.  But,
1633        TYPE_ALIAS_SET is initialized to -1 by default, so we must
1634        clear it here.  */
1635     TYPE_ALIAS_SET (t) = 0;
1636
1637   /* We need to allocate a TYPE_BINFO even for TEMPLATE_TYPE_PARMs
1638      since they can be virtual base types, and we then need a
1639      canonical binfo for them.  Ideally, this would be done lazily for
1640      all types.  */
1641   if (IS_AGGR_TYPE_CODE (code) || code == TEMPLATE_TYPE_PARM
1642       || code == BOUND_TEMPLATE_TEMPLATE_PARM
1643       || code == TYPENAME_TYPE)
1644     TYPE_BINFO (t) = make_binfo (size_zero_node, t, NULL_TREE, NULL_TREE);
1645
1646   return t;
1647 }
1648
1649 tree
1650 make_aggr_type (code)
1651      enum tree_code code;
1652 {
1653   tree t = cp_make_lang_type (code);
1654
1655   if (IS_AGGR_TYPE_CODE (code))
1656     SET_IS_AGGR_TYPE (t, 1);
1657
1658   return t;
1659 }
1660
1661 void
1662 compiler_error VPARAMS ((const char *msg, ...))
1663 {
1664   char buf[1024];
1665
1666   VA_OPEN (ap, msg);
1667   VA_FIXEDARG (ap, const char *, msg);
1668
1669   vsprintf (buf, msg, ap);
1670   VA_CLOSE (ap);
1671
1672   error_with_file_and_line (input_filename, lineno, "%s (compiler error)", buf);
1673 }
1674
1675 /* Return the type-qualifier corresponding to the identifier given by
1676    RID.  */
1677
1678 int
1679 cp_type_qual_from_rid (rid)
1680      tree rid;
1681 {
1682   if (rid == ridpointers[(int) RID_CONST])
1683     return TYPE_QUAL_CONST;
1684   else if (rid == ridpointers[(int) RID_VOLATILE])
1685     return TYPE_QUAL_VOLATILE;
1686   else if (rid == ridpointers[(int) RID_RESTRICT])
1687     return TYPE_QUAL_RESTRICT;
1688
1689   my_friendly_abort (0);
1690   return TYPE_UNQUALIFIED;
1691 }