OSDN Git Service

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