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