OSDN Git Service

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