OSDN Git Service

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