* loop.c: Fix a comment typo.
[official-gcc.git] / gcc / cp / lex.c
blob971d19319e0dfeede1cbcdf785f9bfd98e8d9398
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)
6 This file is part of GNU CC.
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.
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.
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. */
24 /* This file is the lexical analyzer for GNU C++. */
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"
43 #ifdef MULTIBYTE_CHARS
44 #include "mbchar.h"
45 #include <locale.h>
46 #endif
48 extern void yyprint PARAMS ((FILE *, int, YYSTYPE));
50 static int interface_strcmp PARAMS ((const char *));
51 static int *init_cpp_parse PARAMS ((void));
52 static void init_cp_pragma PARAMS ((void));
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 *));
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));
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
78 #include "cpplib.h"
80 extern int yychar; /* the lookahead symbol */
81 extern YYSTYPE yylval; /* the semantic value of the */
82 /* lookahead symbol */
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;
91 /* Array for holding counts of the numbers of tokens seen. */
92 extern int *token_count;
94 /* Functions and data structures for #pragma interface.
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".
101 All main input files "implement" themselves automagically.
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.
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"'. */
113 struct impl_files
115 const char *filename;
116 struct impl_files *next;
119 static struct impl_files *impl_file_chain;
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.
127 We return an INDIRECT_REF whose "contents" are TARGET
128 and whose type is the modifier list. */
130 tree
131 make_pointer_declarator (cv_qualifiers, target)
132 tree cv_qualifiers, target;
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;
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.
147 We return an ADDR_EXPR whose "contents" are TARGET
148 and whose type is the modifier list. */
150 tree
151 make_reference_declarator (cv_qualifiers, target)
152 tree cv_qualifiers, target;
154 if (target)
156 if (TREE_CODE (target) == ADDR_EXPR)
158 error ("cannot declare references to references");
159 return target;
161 if (TREE_CODE (target) == INDIRECT_REF)
163 error ("cannot declare pointers to references");
164 return target;
166 if (TREE_CODE (target) == IDENTIFIER_NODE && ANON_AGGRNAME_P (target))
167 error ("type name expected before `&'");
169 target = build_nt (ADDR_EXPR, target);
170 TREE_TYPE (target) = cv_qualifiers;
171 return target;
174 tree
175 make_call_declarator (target, parms, cv_qualifiers, exception_specification)
176 tree target, parms, cv_qualifiers, exception_specification;
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;
187 void
188 set_quals_and_spec (call_declarator, cv_qualifiers, exception_specification)
189 tree call_declarator, cv_qualifiers, exception_specification;
191 CALL_DECLARATOR_QUALS (call_declarator) = cv_qualifiers;
192 CALL_DECLARATOR_EXCEPTION_SPEC (call_declarator) = exception_specification;
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. */
201 /* Initialization before switch parsing. */
202 void
203 cxx_init_options ()
205 c_common_init_options (clk_cplusplus);
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;
217 void
218 cxx_finish ()
220 c_common_finish ();
223 static int *
224 init_cpp_parse ()
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;
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];
242 /* Initialize data structures that keep track of operator names. */
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
249 static void
250 init_operators ()
252 tree identifier;
253 char buffer[256];
254 struct operator_name_info_t *oni;
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; \
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;
268 #include "operators.def"
269 #undef DEF_OPERATOR
271 operator_name_info[(int) ERROR_MARK].identifier
272 = get_identifier ("<invalid operator>");
274 /* Handle some special cases. These operators are not defined in
275 the language, but can be produced internally. We may need them
276 for error-reporting. (Eventually, we should ensure that this
277 does not happen. Error messages involving these operators will
278 be confusing to users.) */
280 operator_name_info [(int) INIT_EXPR].name
281 = operator_name_info [(int) MODIFY_EXPR].name;
282 operator_name_info [(int) EXACT_DIV_EXPR].name = "(ceiling /)";
283 operator_name_info [(int) CEIL_DIV_EXPR].name = "(ceiling /)";
284 operator_name_info [(int) FLOOR_DIV_EXPR].name = "(floor /)";
285 operator_name_info [(int) ROUND_DIV_EXPR].name = "(round /)";
286 operator_name_info [(int) CEIL_MOD_EXPR].name = "(ceiling %)";
287 operator_name_info [(int) FLOOR_MOD_EXPR].name = "(floor %)";
288 operator_name_info [(int) ROUND_MOD_EXPR].name = "(round %)";
289 operator_name_info [(int) ABS_EXPR].name = "abs";
290 operator_name_info [(int) FFS_EXPR].name = "ffs";
291 operator_name_info [(int) BIT_ANDTC_EXPR].name = "&~";
292 operator_name_info [(int) TRUTH_AND_EXPR].name = "strict &&";
293 operator_name_info [(int) TRUTH_OR_EXPR].name = "strict ||";
294 operator_name_info [(int) IN_EXPR].name = "in";
295 operator_name_info [(int) RANGE_EXPR].name = "...";
296 operator_name_info [(int) CONVERT_EXPR].name = "+";
298 assignment_operator_name_info [(int) EXACT_DIV_EXPR].name
299 = "(exact /=)";
300 assignment_operator_name_info [(int) CEIL_DIV_EXPR].name
301 = "(ceiling /=)";
302 assignment_operator_name_info [(int) FLOOR_DIV_EXPR].name
303 = "(floor /=)";
304 assignment_operator_name_info [(int) ROUND_DIV_EXPR].name
305 = "(round /=)";
306 assignment_operator_name_info [(int) CEIL_MOD_EXPR].name
307 = "(ceiling %=)";
308 assignment_operator_name_info [(int) FLOOR_MOD_EXPR].name
309 = "(floor %=)";
310 assignment_operator_name_info [(int) ROUND_MOD_EXPR].name
311 = "(round %=)";
314 /* The reserved keyword table. */
315 struct resword
317 const char *const word;
318 const ENUM_BITFIELD(rid) rid : 16;
319 const unsigned int disable : 16;
322 /* Disable mask. Keywords are disabled if (reswords[i].disable & mask) is
323 _true_. */
324 #define D_EXT 0x01 /* GCC extension */
325 #define D_ASM 0x02 /* in C99, but has a switch to turn it off */
327 CONSTRAINT(ridbits_fit, RID_LAST_MODIFIER < sizeof(unsigned long) * CHAR_BIT);
329 static const struct resword reswords[] =
331 { "_Complex", RID_COMPLEX, 0 },
332 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
333 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
334 { "__alignof", RID_ALIGNOF, 0 },
335 { "__alignof__", RID_ALIGNOF, 0 },
336 { "__asm", RID_ASM, 0 },
337 { "__asm__", RID_ASM, 0 },
338 { "__attribute", RID_ATTRIBUTE, 0 },
339 { "__attribute__", RID_ATTRIBUTE, 0 },
340 { "__builtin_va_arg", RID_VA_ARG, 0 },
341 { "__complex", RID_COMPLEX, 0 },
342 { "__complex__", RID_COMPLEX, 0 },
343 { "__const", RID_CONST, 0 },
344 { "__const__", RID_CONST, 0 },
345 { "__extension__", RID_EXTENSION, 0 },
346 { "__func__", RID_C99_FUNCTION_NAME, 0 },
347 { "__imag", RID_IMAGPART, 0 },
348 { "__imag__", RID_IMAGPART, 0 },
349 { "__inline", RID_INLINE, 0 },
350 { "__inline__", RID_INLINE, 0 },
351 { "__label__", RID_LABEL, 0 },
352 { "__null", RID_NULL, 0 },
353 { "__real", RID_REALPART, 0 },
354 { "__real__", RID_REALPART, 0 },
355 { "__restrict", RID_RESTRICT, 0 },
356 { "__restrict__", RID_RESTRICT, 0 },
357 { "__signed", RID_SIGNED, 0 },
358 { "__signed__", RID_SIGNED, 0 },
359 { "__thread", RID_THREAD, 0 },
360 { "__typeof", RID_TYPEOF, 0 },
361 { "__typeof__", RID_TYPEOF, 0 },
362 { "__volatile", RID_VOLATILE, 0 },
363 { "__volatile__", RID_VOLATILE, 0 },
364 { "asm", RID_ASM, D_ASM },
365 { "auto", RID_AUTO, 0 },
366 { "bool", RID_BOOL, 0 },
367 { "break", RID_BREAK, 0 },
368 { "case", RID_CASE, 0 },
369 { "catch", RID_CATCH, 0 },
370 { "char", RID_CHAR, 0 },
371 { "class", RID_CLASS, 0 },
372 { "const", RID_CONST, 0 },
373 { "const_cast", RID_CONSTCAST, 0 },
374 { "continue", RID_CONTINUE, 0 },
375 { "default", RID_DEFAULT, 0 },
376 { "delete", RID_DELETE, 0 },
377 { "do", RID_DO, 0 },
378 { "double", RID_DOUBLE, 0 },
379 { "dynamic_cast", RID_DYNCAST, 0 },
380 { "else", RID_ELSE, 0 },
381 { "enum", RID_ENUM, 0 },
382 { "explicit", RID_EXPLICIT, 0 },
383 { "export", RID_EXPORT, 0 },
384 { "extern", RID_EXTERN, 0 },
385 { "false", RID_FALSE, 0 },
386 { "float", RID_FLOAT, 0 },
387 { "for", RID_FOR, 0 },
388 { "friend", RID_FRIEND, 0 },
389 { "goto", RID_GOTO, 0 },
390 { "if", RID_IF, 0 },
391 { "inline", RID_INLINE, 0 },
392 { "int", RID_INT, 0 },
393 { "long", RID_LONG, 0 },
394 { "mutable", RID_MUTABLE, 0 },
395 { "namespace", RID_NAMESPACE, 0 },
396 { "new", RID_NEW, 0 },
397 { "operator", RID_OPERATOR, 0 },
398 { "private", RID_PRIVATE, 0 },
399 { "protected", RID_PROTECTED, 0 },
400 { "public", RID_PUBLIC, 0 },
401 { "register", RID_REGISTER, 0 },
402 { "reinterpret_cast", RID_REINTCAST, 0 },
403 { "return", RID_RETURN, 0 },
404 { "short", RID_SHORT, 0 },
405 { "signed", RID_SIGNED, 0 },
406 { "sizeof", RID_SIZEOF, 0 },
407 { "static", RID_STATIC, 0 },
408 { "static_cast", RID_STATCAST, 0 },
409 { "struct", RID_STRUCT, 0 },
410 { "switch", RID_SWITCH, 0 },
411 { "template", RID_TEMPLATE, 0 },
412 { "this", RID_THIS, 0 },
413 { "throw", RID_THROW, 0 },
414 { "true", RID_TRUE, 0 },
415 { "try", RID_TRY, 0 },
416 { "typedef", RID_TYPEDEF, 0 },
417 { "typename", RID_TYPENAME, 0 },
418 { "typeid", RID_TYPEID, 0 },
419 { "typeof", RID_TYPEOF, D_ASM|D_EXT },
420 { "union", RID_UNION, 0 },
421 { "unsigned", RID_UNSIGNED, 0 },
422 { "using", RID_USING, 0 },
423 { "virtual", RID_VIRTUAL, 0 },
424 { "void", RID_VOID, 0 },
425 { "volatile", RID_VOLATILE, 0 },
426 { "wchar_t", RID_WCHAR, 0 },
427 { "while", RID_WHILE, 0 },
431 /* Table mapping from RID_* constants to yacc token numbers.
432 Unfortunately we have to have entries for all the keywords in all
433 three languages. */
434 const short rid_to_yy[RID_MAX] =
436 /* RID_STATIC */ SCSPEC,
437 /* RID_UNSIGNED */ TYPESPEC,
438 /* RID_LONG */ TYPESPEC,
439 /* RID_CONST */ CV_QUALIFIER,
440 /* RID_EXTERN */ SCSPEC,
441 /* RID_REGISTER */ SCSPEC,
442 /* RID_TYPEDEF */ SCSPEC,
443 /* RID_SHORT */ TYPESPEC,
444 /* RID_INLINE */ SCSPEC,
445 /* RID_VOLATILE */ CV_QUALIFIER,
446 /* RID_SIGNED */ TYPESPEC,
447 /* RID_AUTO */ SCSPEC,
448 /* RID_RESTRICT */ CV_QUALIFIER,
450 /* C extensions. Bounded pointers are not yet in C++ */
451 /* RID_BOUNDED */ 0,
452 /* RID_UNBOUNDED */ 0,
453 /* RID_COMPLEX */ TYPESPEC,
454 /* RID_THREAD */ SCSPEC,
456 /* C++ */
457 /* RID_FRIEND */ SCSPEC,
458 /* RID_VIRTUAL */ SCSPEC,
459 /* RID_EXPLICIT */ SCSPEC,
460 /* RID_EXPORT */ EXPORT,
461 /* RID_MUTABLE */ SCSPEC,
463 /* ObjC */
464 /* RID_IN */ 0,
465 /* RID_OUT */ 0,
466 /* RID_INOUT */ 0,
467 /* RID_BYCOPY */ 0,
468 /* RID_BYREF */ 0,
469 /* RID_ONEWAY */ 0,
471 /* C */
472 /* RID_INT */ TYPESPEC,
473 /* RID_CHAR */ TYPESPEC,
474 /* RID_FLOAT */ TYPESPEC,
475 /* RID_DOUBLE */ TYPESPEC,
476 /* RID_VOID */ TYPESPEC,
477 /* RID_ENUM */ ENUM,
478 /* RID_STRUCT */ AGGR,
479 /* RID_UNION */ AGGR,
480 /* RID_IF */ IF,
481 /* RID_ELSE */ ELSE,
482 /* RID_WHILE */ WHILE,
483 /* RID_DO */ DO,
484 /* RID_FOR */ FOR,
485 /* RID_SWITCH */ SWITCH,
486 /* RID_CASE */ CASE,
487 /* RID_DEFAULT */ DEFAULT,
488 /* RID_BREAK */ BREAK,
489 /* RID_CONTINUE */ CONTINUE,
490 /* RID_RETURN */ RETURN_KEYWORD,
491 /* RID_GOTO */ GOTO,
492 /* RID_SIZEOF */ SIZEOF,
494 /* C extensions */
495 /* RID_ASM */ ASM_KEYWORD,
496 /* RID_TYPEOF */ TYPEOF,
497 /* RID_ALIGNOF */ ALIGNOF,
498 /* RID_ATTRIBUTE */ ATTRIBUTE,
499 /* RID_VA_ARG */ VA_ARG,
500 /* RID_EXTENSION */ EXTENSION,
501 /* RID_IMAGPART */ IMAGPART,
502 /* RID_REALPART */ REALPART,
503 /* RID_LABEL */ LABEL,
504 /* RID_PTRBASE */ 0,
505 /* RID_PTREXTENT */ 0,
506 /* RID_PTRVALUE */ 0,
507 /* RID_CHOOSE_EXPR */ 0,
508 /* RID_TYPES_COMPATIBLE_P */ 0,
510 /* RID_FUNCTION_NAME */ VAR_FUNC_NAME,
511 /* RID_PRETTY_FUNCTION_NAME */ VAR_FUNC_NAME,
512 /* RID_c99_FUNCTION_NAME */ VAR_FUNC_NAME,
514 /* C++ */
515 /* RID_BOOL */ TYPESPEC,
516 /* RID_WCHAR */ TYPESPEC,
517 /* RID_CLASS */ AGGR,
518 /* RID_PUBLIC */ VISSPEC,
519 /* RID_PRIVATE */ VISSPEC,
520 /* RID_PROTECTED */ VISSPEC,
521 /* RID_TEMPLATE */ TEMPLATE,
522 /* RID_NULL */ CONSTANT,
523 /* RID_CATCH */ CATCH,
524 /* RID_DELETE */ DELETE,
525 /* RID_FALSE */ CXX_FALSE,
526 /* RID_NAMESPACE */ NAMESPACE,
527 /* RID_NEW */ NEW,
528 /* RID_OPERATOR */ OPERATOR,
529 /* RID_THIS */ THIS,
530 /* RID_THROW */ THROW,
531 /* RID_TRUE */ CXX_TRUE,
532 /* RID_TRY */ TRY,
533 /* RID_TYPENAME */ TYPENAME_KEYWORD,
534 /* RID_TYPEID */ TYPEID,
535 /* RID_USING */ USING,
537 /* casts */
538 /* RID_CONSTCAST */ CONST_CAST,
539 /* RID_DYNCAST */ DYNAMIC_CAST,
540 /* RID_REINTCAST */ REINTERPRET_CAST,
541 /* RID_STATCAST */ STATIC_CAST,
543 /* Objective C */
544 /* RID_ID */ 0,
545 /* RID_AT_ENCODE */ 0,
546 /* RID_AT_END */ 0,
547 /* RID_AT_CLASS */ 0,
548 /* RID_AT_ALIAS */ 0,
549 /* RID_AT_DEFS */ 0,
550 /* RID_AT_PRIVATE */ 0,
551 /* RID_AT_PROTECTED */ 0,
552 /* RID_AT_PUBLIC */ 0,
553 /* RID_AT_PROTOCOL */ 0,
554 /* RID_AT_SELECTOR */ 0,
555 /* RID_AT_INTERFACE */ 0,
556 /* RID_AT_IMPLEMENTATION */ 0
559 void
560 init_reswords ()
562 unsigned int i;
563 tree id;
564 int mask = ((flag_no_asm ? D_ASM : 0)
565 | (flag_no_gnu_keywords ? D_EXT : 0));
567 /* It is not necessary to register ridpointers as a GC root, because
568 all the trees it points to are permanently interned in the
569 get_identifier hash anyway. */
570 ridpointers = (tree *) xcalloc ((int) RID_MAX, sizeof (tree));
571 for (i = 0; i < ARRAY_SIZE (reswords); i++)
573 id = get_identifier (reswords[i].word);
574 C_RID_CODE (id) = reswords[i].rid;
575 ridpointers [(int) reswords[i].rid] = id;
576 if (! (reswords[i].disable & mask))
577 C_IS_RESERVED_WORD (id) = 1;
581 static void
582 init_cp_pragma ()
584 cpp_register_pragma (parse_in, 0, "vtable", handle_pragma_vtable);
585 cpp_register_pragma (parse_in, 0, "unit", handle_pragma_unit);
587 cpp_register_pragma (parse_in, 0, "interface", handle_pragma_interface);
588 cpp_register_pragma (parse_in, 0, "implementation",
589 handle_pragma_implementation);
591 cpp_register_pragma (parse_in, "GCC", "interface", handle_pragma_interface);
592 cpp_register_pragma (parse_in, "GCC", "implementation",
593 handle_pragma_implementation);
594 cpp_register_pragma (parse_in, "GCC", "java_exceptions",
595 handle_pragma_java_exceptions);
598 /* Initialize the C++ front end. This function is very sensitive to
599 the exact order that things are done here. It would be nice if the
600 initialization done by this routine were moved to its subroutines,
601 and the ordering dependencies clarified and reduced. */
602 const char *
603 cxx_init (filename)
604 const char *filename;
606 input_filename = "<internal>";
608 init_reswords ();
609 init_spew ();
610 init_tree ();
611 init_cp_semantics ();
612 init_operators ();
613 init_method ();
614 init_error ();
616 current_function_decl = NULL;
618 class_type_node = build_int_2 (class_type, 0);
619 TREE_TYPE (class_type_node) = class_type_node;
620 ridpointers[(int) RID_CLASS] = class_type_node;
622 record_type_node = build_int_2 (record_type, 0);
623 TREE_TYPE (record_type_node) = record_type_node;
624 ridpointers[(int) RID_STRUCT] = record_type_node;
626 union_type_node = build_int_2 (union_type, 0);
627 TREE_TYPE (union_type_node) = union_type_node;
628 ridpointers[(int) RID_UNION] = union_type_node;
630 enum_type_node = build_int_2 (enum_type, 0);
631 TREE_TYPE (enum_type_node) = enum_type_node;
632 ridpointers[(int) RID_ENUM] = enum_type_node;
634 cxx_init_decl_processing ();
636 /* Create the built-in __null node. */
637 null_node = build_int_2 (0, 0);
638 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
639 ridpointers[RID_NULL] = null_node;
641 token_count = init_cpp_parse ();
642 interface_unknown = 1;
644 filename = c_common_init (filename);
645 if (filename == NULL)
646 return NULL;
648 init_cp_pragma ();
650 init_repo (filename);
652 return filename;
655 inline void
656 yyprint (file, yychar, yylval)
657 FILE *file;
658 int yychar;
659 YYSTYPE yylval;
661 tree t;
662 switch (yychar)
664 case IDENTIFIER:
665 case tTYPENAME:
666 case TYPESPEC:
667 case PTYPENAME:
668 case PFUNCNAME:
669 case IDENTIFIER_DEFN:
670 case TYPENAME_DEFN:
671 case PTYPENAME_DEFN:
672 case SCSPEC:
673 case PRE_PARSED_CLASS_DECL:
674 t = yylval.ttype;
675 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
677 fprintf (file, " `%s'", IDENTIFIER_POINTER (DECL_NAME (t)));
678 break;
680 my_friendly_assert (TREE_CODE (t) == IDENTIFIER_NODE, 224);
681 if (IDENTIFIER_POINTER (t))
682 fprintf (file, " `%s'", IDENTIFIER_POINTER (t));
683 break;
685 case AGGR:
686 if (yylval.ttype == class_type_node)
687 fprintf (file, " `class'");
688 else if (yylval.ttype == record_type_node)
689 fprintf (file, " `struct'");
690 else if (yylval.ttype == union_type_node)
691 fprintf (file, " `union'");
692 else if (yylval.ttype == enum_type_node)
693 fprintf (file, " `enum'");
694 else
695 abort ();
696 break;
698 case CONSTANT:
699 t = yylval.ttype;
700 if (TREE_CODE (t) == INTEGER_CST)
701 fprintf (file,
702 #if HOST_BITS_PER_WIDE_INT == 64
703 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
704 " 0x%x%016x",
705 #else
706 #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_LONG
707 " 0x%lx%016lx",
708 #else
709 " 0x%llx%016llx",
710 #endif
711 #endif
712 #else
713 #if HOST_BITS_PER_WIDE_INT != HOST_BITS_PER_INT
714 " 0x%lx%08lx",
715 #else
716 " 0x%x%08x",
717 #endif
718 #endif
719 TREE_INT_CST_HIGH (t), TREE_INT_CST_LOW (t));
720 break;
724 #if defined(GATHER_STATISTICS) && defined(REDUCE_LENGTH)
725 static int *reduce_count;
726 #endif
728 int *token_count;
730 #if 0
731 #define REDUCE_LENGTH ARRAY_SIZE (yyr2)
732 #define TOKEN_LENGTH (256 + ARRAY_SIZE (yytname))
733 #endif
735 #ifdef GATHER_STATISTICS
736 #ifdef REDUCE_LENGTH
737 void
738 yyhook (yyn)
739 int yyn;
741 reduce_count[yyn] += 1;
744 static int
745 reduce_cmp (p, q)
746 int *p, *q;
748 return reduce_count[*q] - reduce_count[*p];
751 static int
752 token_cmp (p, q)
753 int *p, *q;
755 return token_count[*q] - token_count[*p];
757 #endif
758 #endif
760 void
761 print_parse_statistics ()
763 #ifdef GATHER_STATISTICS
764 #ifdef REDUCE_LENGTH
765 #if YYDEBUG != 0
766 int i;
767 int maxlen = REDUCE_LENGTH;
768 unsigned *sorted;
770 if (reduce_count[-1] == 0)
771 return;
773 if (TOKEN_LENGTH > REDUCE_LENGTH)
774 maxlen = TOKEN_LENGTH;
775 sorted = (unsigned *) alloca (sizeof (int) * maxlen);
777 for (i = 0; i < TOKEN_LENGTH; i++)
778 sorted[i] = i;
779 qsort (sorted, TOKEN_LENGTH, sizeof (int), token_cmp);
780 for (i = 0; i < TOKEN_LENGTH; i++)
782 int idx = sorted[i];
783 if (token_count[idx] == 0)
784 break;
785 if (token_count[idx] < token_count[-1])
786 break;
787 fprintf (stderr, "token %d, `%s', count = %d\n",
788 idx, yytname[YYTRANSLATE (idx)], token_count[idx]);
790 fprintf (stderr, "\n");
791 for (i = 0; i < REDUCE_LENGTH; i++)
792 sorted[i] = i;
793 qsort (sorted, REDUCE_LENGTH, sizeof (int), reduce_cmp);
794 for (i = 0; i < REDUCE_LENGTH; i++)
796 int idx = sorted[i];
797 if (reduce_count[idx] == 0)
798 break;
799 if (reduce_count[idx] < reduce_count[-1])
800 break;
801 fprintf (stderr, "rule %d, line %d, count = %d\n",
802 idx, yyrline[idx], reduce_count[idx]);
804 fprintf (stderr, "\n");
805 #endif
806 #endif
807 #endif
810 /* Helper function to load global variables with interface
811 information. */
813 void
814 extract_interface_info ()
816 struct c_fileinfo *finfo = 0;
818 if (flag_alt_external_templates)
820 tree til = tinst_for_decl ();
822 if (til)
823 finfo = get_fileinfo (TINST_FILE (til));
825 if (!finfo)
826 finfo = get_fileinfo (input_filename);
828 interface_only = finfo->interface_only;
829 interface_unknown = finfo->interface_unknown;
832 /* Return nonzero if S is not considered part of an
833 INTERFACE/IMPLEMENTATION pair. Otherwise, return 0. */
835 static int
836 interface_strcmp (s)
837 const char *s;
839 /* Set the interface/implementation bits for this scope. */
840 struct impl_files *ifiles;
841 const char *s1;
843 for (ifiles = impl_file_chain; ifiles; ifiles = ifiles->next)
845 const char *t1 = ifiles->filename;
846 s1 = s;
848 if (*s1 != *t1 || *s1 == 0)
849 continue;
851 while (*s1 == *t1 && *s1 != 0)
852 s1++, t1++;
854 /* A match. */
855 if (*s1 == *t1)
856 return 0;
858 /* Don't get faked out by xxx.yyy.cc vs xxx.zzz.cc. */
859 if (strchr (s1, '.') || strchr (t1, '.'))
860 continue;
862 if (*s1 == '\0' || s1[-1] != '.' || t1[-1] != '.')
863 continue;
865 /* A match. */
866 return 0;
869 /* No matches. */
870 return 1;
873 /* Heuristic to tell whether the user is missing a semicolon
874 after a struct or enum declaration. Emit an error message
875 if we know the user has blown it. */
877 void
878 check_for_missing_semicolon (type)
879 tree type;
881 if (yychar < 0)
882 yychar = yylex ();
884 if ((yychar > 255
885 && yychar != SCSPEC
886 && yychar != IDENTIFIER
887 && yychar != tTYPENAME
888 && yychar != CV_QUALIFIER
889 && yychar != SELFNAME)
890 || yychar == 0 /* EOF */)
892 if (TYPE_ANONYMOUS_P (type))
893 error ("semicolon missing after %s declaration",
894 TREE_CODE (type) == ENUMERAL_TYPE ? "enum" : "struct");
895 else
896 error ("semicolon missing after declaration of `%T'", type);
897 shadow_tag (build_tree_list (0, type));
899 /* Could probably also hack cases where class { ... } f (); appears. */
900 clear_anon_tags ();
903 void
904 note_got_semicolon (type)
905 tree type;
907 if (!TYPE_P (type))
908 abort ();
909 if (CLASS_TYPE_P (type))
910 CLASSTYPE_GOT_SEMICOLON (type) = 1;
913 void
914 note_list_got_semicolon (declspecs)
915 tree declspecs;
917 tree link;
919 for (link = declspecs; link; link = TREE_CHAIN (link))
921 tree type = TREE_VALUE (link);
922 if (type && TYPE_P (type))
923 note_got_semicolon (type);
925 clear_anon_tags ();
929 /* Parse a #pragma whose sole argument is a string constant.
930 If OPT is true, the argument is optional. */
931 static tree
932 parse_strconst_pragma (name, opt)
933 const char *name;
934 int opt;
936 tree result, x;
937 enum cpp_ttype t;
939 t = c_lex (&x);
940 if (t == CPP_STRING)
942 result = x;
943 if (c_lex (&x) != CPP_EOF)
944 warning ("junk at end of #pragma %s", name);
945 return result;
948 if (t == CPP_EOF && opt)
949 return 0;
951 error ("invalid #pragma %s", name);
952 return (tree)-1;
955 static void
956 handle_pragma_vtable (dfile)
957 cpp_reader *dfile ATTRIBUTE_UNUSED;
959 parse_strconst_pragma ("vtable", 0);
960 sorry ("#pragma vtable no longer supported");
963 static void
964 handle_pragma_unit (dfile)
965 cpp_reader *dfile ATTRIBUTE_UNUSED;
967 /* Validate syntax, but don't do anything. */
968 parse_strconst_pragma ("unit", 0);
971 static void
972 handle_pragma_interface (dfile)
973 cpp_reader *dfile ATTRIBUTE_UNUSED;
975 tree fname = parse_strconst_pragma ("interface", 1);
976 struct c_fileinfo *finfo;
977 const char *main_filename;
979 if (fname == (tree)-1)
980 return;
981 else if (fname == 0)
982 main_filename = lbasename (input_filename);
983 else
984 main_filename = TREE_STRING_POINTER (fname);
986 finfo = get_fileinfo (input_filename);
988 if (impl_file_chain == 0)
990 /* If this is zero at this point, then we are
991 auto-implementing. */
992 if (main_input_filename == 0)
993 main_input_filename = input_filename;
996 interface_only = interface_strcmp (main_filename);
997 #ifdef MULTIPLE_SYMBOL_SPACES
998 if (! interface_only)
999 #endif
1000 interface_unknown = 0;
1002 finfo->interface_only = interface_only;
1003 finfo->interface_unknown = interface_unknown;
1006 /* Note that we have seen a #pragma implementation for the key MAIN_FILENAME.
1007 We used to only allow this at toplevel, but that restriction was buggy
1008 in older compilers and it seems reasonable to allow it in the headers
1009 themselves, too. It only needs to precede the matching #p interface.
1011 We don't touch interface_only or interface_unknown; the user must specify
1012 a matching #p interface for this to have any effect. */
1014 static void
1015 handle_pragma_implementation (dfile)
1016 cpp_reader *dfile ATTRIBUTE_UNUSED;
1018 tree fname = parse_strconst_pragma ("implementation", 1);
1019 const char *main_filename;
1020 struct impl_files *ifiles = impl_file_chain;
1022 if (fname == (tree)-1)
1023 return;
1025 if (fname == 0)
1027 if (main_input_filename)
1028 main_filename = main_input_filename;
1029 else
1030 main_filename = input_filename;
1031 main_filename = lbasename (main_filename);
1033 else
1035 main_filename = TREE_STRING_POINTER (fname);
1036 if (cpp_included (parse_in, main_filename))
1037 warning ("#pragma implementation for %s appears after file is included",
1038 main_filename);
1041 for (; ifiles; ifiles = ifiles->next)
1043 if (! strcmp (ifiles->filename, main_filename))
1044 break;
1046 if (ifiles == 0)
1048 ifiles = (struct impl_files*) xmalloc (sizeof (struct impl_files));
1049 ifiles->filename = main_filename;
1050 ifiles->next = impl_file_chain;
1051 impl_file_chain = ifiles;
1055 /* Indicate that this file uses Java-personality exception handling. */
1056 static void
1057 handle_pragma_java_exceptions (dfile)
1058 cpp_reader *dfile ATTRIBUTE_UNUSED;
1060 tree x;
1061 if (c_lex (&x) != CPP_EOF)
1062 warning ("junk at end of #pragma GCC java_exceptions");
1064 choose_personality_routine (lang_java);
1067 void
1068 do_pending_lang_change ()
1070 for (; pending_lang_change > 0; --pending_lang_change)
1071 push_lang_context (lang_name_c);
1072 for (; pending_lang_change < 0; ++pending_lang_change)
1073 pop_lang_context ();
1076 /* Return true if d is in a global scope. */
1078 static int
1079 is_global (d)
1080 tree d;
1082 while (1)
1083 switch (TREE_CODE (d))
1085 case ERROR_MARK:
1086 return 1;
1088 case OVERLOAD: d = OVL_FUNCTION (d); continue;
1089 case TREE_LIST: d = TREE_VALUE (d); continue;
1090 default:
1091 my_friendly_assert (DECL_P (d), 980629);
1093 return DECL_NAMESPACE_SCOPE_P (d);
1097 /* Issue an error message indicating that the lookup of NAME (an
1098 IDENTIFIER_NODE) failed. */
1100 void
1101 unqualified_name_lookup_error (tree name)
1103 if (IDENTIFIER_OPNAME_P (name))
1105 if (name != ansi_opname (ERROR_MARK))
1106 error ("`%D' not defined", name);
1108 else if (current_function_decl == 0)
1109 error ("`%D' was not declared in this scope", name);
1110 else
1112 if (IDENTIFIER_NAMESPACE_VALUE (name) != error_mark_node
1113 || IDENTIFIER_ERROR_LOCUS (name) != current_function_decl)
1115 static int undeclared_variable_notice;
1117 error ("`%D' undeclared (first use this function)", name);
1119 if (! undeclared_variable_notice)
1121 error ("(Each undeclared identifier is reported only once for each function it appears in.)");
1122 undeclared_variable_notice = 1;
1125 /* Prevent repeated error messages. */
1126 SET_IDENTIFIER_NAMESPACE_VALUE (name, error_mark_node);
1127 SET_IDENTIFIER_ERROR_LOCUS (name, current_function_decl);
1131 tree
1132 do_identifier (token, parsing, args)
1133 register tree token;
1134 int parsing;
1135 tree args;
1137 register tree id;
1138 int lexing = (parsing == 1);
1140 if (! lexing)
1141 id = lookup_name (token, 0);
1142 else
1143 id = lastiddecl;
1145 if (lexing && id && TREE_DEPRECATED (id))
1146 warn_deprecated_use (id);
1148 /* Do Koenig lookup if appropriate (inside templates we build lookup
1149 expressions instead).
1151 [basic.lookup.koenig]: If the ordinary unqualified lookup of the name
1152 finds the declaration of a class member function, the associated
1153 namespaces and classes are not considered. */
1155 if (args && !current_template_parms && (!id || is_global (id)))
1156 id = lookup_arg_dependent (token, id, args);
1158 /* Remember that this name has been used in the class definition, as per
1159 [class.scope0] */
1160 if (id && parsing)
1161 maybe_note_name_used_in_class (token, id);
1163 if (id == error_mark_node)
1165 /* lookup_name quietly returns error_mark_node if we're parsing,
1166 as we don't want to complain about an identifier that ends up
1167 being used as a declarator. So we call it again to get the error
1168 message. */
1169 id = lookup_name (token, 0);
1170 return error_mark_node;
1173 if (!id || (TREE_CODE (id) == FUNCTION_DECL
1174 && DECL_ANTICIPATED (id)))
1176 if (current_template_parms)
1177 return build_min_nt (LOOKUP_EXPR, token);
1178 else if (IDENTIFIER_TYPENAME_P (token))
1179 /* A templated conversion operator might exist. */
1180 return token;
1181 else
1183 unqualified_name_lookup_error (token);
1184 return error_mark_node;
1188 id = check_for_out_of_scope_variable (id);
1190 /* TREE_USED is set in `hack_identifier'. */
1191 if (TREE_CODE (id) == CONST_DECL)
1193 /* Check access. */
1194 if (IDENTIFIER_CLASS_VALUE (token) == id)
1195 enforce_access (CP_DECL_CONTEXT(id), id);
1196 if (!processing_template_decl || DECL_TEMPLATE_PARM_P (id))
1197 id = DECL_INITIAL (id);
1199 else
1200 id = hack_identifier (id, token);
1202 /* We must look up dependent names when the template is
1203 instantiated, not while parsing it. For now, we don't
1204 distinguish between dependent and independent names. So, for
1205 example, we look up all overloaded functions at
1206 instantiation-time, even though in some cases we should just use
1207 the DECL we have here. We also use LOOKUP_EXPRs to find things
1208 like local variables, rather than creating TEMPLATE_DECLs for the
1209 local variables and then finding matching instantiations. */
1210 if (current_template_parms
1211 && (is_overloaded_fn (id)
1212 || (TREE_CODE (id) == VAR_DECL
1213 && CP_DECL_CONTEXT (id)
1214 && TREE_CODE (CP_DECL_CONTEXT (id)) == FUNCTION_DECL)
1215 || TREE_CODE (id) == PARM_DECL
1216 || TREE_CODE (id) == RESULT_DECL
1217 || TREE_CODE (id) == USING_DECL))
1218 id = build_min_nt (LOOKUP_EXPR, token);
1220 return id;
1223 tree
1224 do_scoped_id (token, id)
1225 tree token;
1226 tree id;
1228 if (!id || (TREE_CODE (id) == FUNCTION_DECL
1229 && DECL_ANTICIPATED (id)))
1231 if (processing_template_decl)
1233 id = build_min_nt (LOOKUP_EXPR, token);
1234 LOOKUP_EXPR_GLOBAL (id) = 1;
1235 return id;
1237 if (IDENTIFIER_NAMESPACE_VALUE (token) != error_mark_node)
1238 error ("`::%D' undeclared (first use here)", token);
1239 id = error_mark_node;
1240 /* Prevent repeated error messages. */
1241 SET_IDENTIFIER_NAMESPACE_VALUE (token, error_mark_node);
1243 else
1245 if (TREE_CODE (id) == ADDR_EXPR)
1246 mark_used (TREE_OPERAND (id, 0));
1247 else if (TREE_CODE (id) != OVERLOAD)
1248 mark_used (id);
1250 if (TREE_CODE (id) == CONST_DECL && ! processing_template_decl)
1252 /* XXX CHS - should we set TREE_USED of the constant? */
1253 id = DECL_INITIAL (id);
1254 /* This is to prevent an enum whose value is 0
1255 from being considered a null pointer constant. */
1256 id = build1 (NOP_EXPR, TREE_TYPE (id), id);
1257 TREE_CONSTANT (id) = 1;
1260 if (processing_template_decl)
1262 if (is_overloaded_fn (id))
1264 id = build_min_nt (LOOKUP_EXPR, token);
1265 LOOKUP_EXPR_GLOBAL (id) = 1;
1266 return id;
1268 /* else just use the decl */
1270 return convert_from_reference (id);
1273 tree
1274 identifier_typedecl_value (node)
1275 tree node;
1277 tree t, type;
1278 type = IDENTIFIER_TYPE_VALUE (node);
1279 if (type == NULL_TREE)
1280 return NULL_TREE;
1282 if (IDENTIFIER_BINDING (node))
1284 t = IDENTIFIER_VALUE (node);
1285 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
1286 return t;
1288 if (IDENTIFIER_NAMESPACE_VALUE (node))
1290 t = IDENTIFIER_NAMESPACE_VALUE (node);
1291 if (t && TREE_CODE (t) == TYPE_DECL && TREE_TYPE (t) == type)
1292 return t;
1295 /* Will this one ever happen? */
1296 if (TYPE_MAIN_DECL (type))
1297 return TYPE_MAIN_DECL (type);
1299 /* We used to do an internal error of 62 here, but instead we will
1300 handle the return of a null appropriately in the callers. */
1301 return NULL_TREE;
1304 #ifdef GATHER_STATISTICS
1305 /* The original for tree_node_kind is in the toplevel tree.c; changes there
1306 need to be brought into here, unless this were actually put into a header
1307 instead. */
1308 /* Statistics-gathering stuff. */
1309 typedef enum
1311 d_kind,
1312 t_kind,
1313 b_kind,
1314 s_kind,
1315 r_kind,
1316 e_kind,
1317 c_kind,
1318 id_kind,
1319 op_id_kind,
1320 perm_list_kind,
1321 temp_list_kind,
1322 vec_kind,
1323 x_kind,
1324 lang_decl,
1325 lang_type,
1326 all_kinds
1327 } tree_node_kind;
1329 extern int tree_node_counts[];
1330 extern int tree_node_sizes[];
1331 #endif
1333 tree
1334 build_lang_decl (code, name, type)
1335 enum tree_code code;
1336 tree name;
1337 tree type;
1339 tree t;
1341 t = build_decl (code, name, type);
1342 retrofit_lang_decl (t);
1344 return t;
1347 /* Add DECL_LANG_SPECIFIC info to T. Called from build_lang_decl
1348 and pushdecl (for functions generated by the backend). */
1350 void
1351 retrofit_lang_decl (t)
1352 tree t;
1354 struct lang_decl *ld;
1355 size_t size;
1357 if (CAN_HAVE_FULL_LANG_DECL_P (t))
1358 size = sizeof (struct lang_decl);
1359 else
1360 size = sizeof (struct lang_decl_flags);
1362 ld = (struct lang_decl *) ggc_alloc_cleared (size);
1364 ld->decl_flags.can_be_full = CAN_HAVE_FULL_LANG_DECL_P (t) ? 1 : 0;
1365 ld->decl_flags.u1sel = TREE_CODE (t) == NAMESPACE_DECL ? 1 : 0;
1366 ld->decl_flags.u2sel = 0;
1367 if (ld->decl_flags.can_be_full)
1368 ld->u.f.u3sel = TREE_CODE (t) == FUNCTION_DECL ? 1 : 0;
1370 DECL_LANG_SPECIFIC (t) = ld;
1371 if (current_lang_name == lang_name_cplusplus)
1372 SET_DECL_LANGUAGE (t, lang_cplusplus);
1373 else if (current_lang_name == lang_name_c)
1374 SET_DECL_LANGUAGE (t, lang_c);
1375 else if (current_lang_name == lang_name_java)
1376 SET_DECL_LANGUAGE (t, lang_java);
1377 else abort ();
1379 #ifdef GATHER_STATISTICS
1380 tree_node_counts[(int)lang_decl] += 1;
1381 tree_node_sizes[(int)lang_decl] += size;
1382 #endif
1385 void
1386 cxx_dup_lang_specific_decl (node)
1387 tree node;
1389 int size;
1390 struct lang_decl *ld;
1392 if (! DECL_LANG_SPECIFIC (node))
1393 return;
1395 if (!CAN_HAVE_FULL_LANG_DECL_P (node))
1396 size = sizeof (struct lang_decl_flags);
1397 else
1398 size = sizeof (struct lang_decl);
1399 ld = (struct lang_decl *) ggc_alloc (size);
1400 memcpy (ld, DECL_LANG_SPECIFIC (node), size);
1401 DECL_LANG_SPECIFIC (node) = ld;
1403 #ifdef GATHER_STATISTICS
1404 tree_node_counts[(int)lang_decl] += 1;
1405 tree_node_sizes[(int)lang_decl] += size;
1406 #endif
1409 /* Copy DECL, including any language-specific parts. */
1411 tree
1412 copy_decl (decl)
1413 tree decl;
1415 tree copy;
1417 copy = copy_node (decl);
1418 cxx_dup_lang_specific_decl (copy);
1419 return copy;
1422 /* Replace the shared language-specific parts of NODE with a new copy. */
1424 static void
1425 copy_lang_type (node)
1426 tree node;
1428 int size;
1429 struct lang_type *lt;
1431 if (! TYPE_LANG_SPECIFIC (node))
1432 return;
1434 if (TYPE_LANG_SPECIFIC (node)->u.h.is_lang_type_class)
1435 size = sizeof (struct lang_type);
1436 else
1437 size = sizeof (struct lang_type_ptrmem);
1438 lt = (struct lang_type *) ggc_alloc (size);
1439 memcpy (lt, TYPE_LANG_SPECIFIC (node), size);
1440 TYPE_LANG_SPECIFIC (node) = lt;
1442 #ifdef GATHER_STATISTICS
1443 tree_node_counts[(int)lang_type] += 1;
1444 tree_node_sizes[(int)lang_type] += size;
1445 #endif
1448 /* Copy TYPE, including any language-specific parts. */
1450 tree
1451 copy_type (type)
1452 tree type;
1454 tree copy;
1456 copy = copy_node (type);
1457 copy_lang_type (copy);
1458 return copy;
1461 tree
1462 cxx_make_type (code)
1463 enum tree_code code;
1465 register tree t = make_node (code);
1467 /* Create lang_type structure. */
1468 if (IS_AGGR_TYPE_CODE (code)
1469 || code == BOUND_TEMPLATE_TEMPLATE_PARM)
1471 struct lang_type *pi;
1473 pi = ((struct lang_type *)
1474 ggc_alloc_cleared (sizeof (struct lang_type)));
1476 TYPE_LANG_SPECIFIC (t) = pi;
1477 pi->u.c.h.is_lang_type_class = 1;
1479 #ifdef GATHER_STATISTICS
1480 tree_node_counts[(int)lang_type] += 1;
1481 tree_node_sizes[(int)lang_type] += sizeof (struct lang_type);
1482 #endif
1485 /* Set up some flags that give proper default behavior. */
1486 if (IS_AGGR_TYPE_CODE (code))
1488 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
1489 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1491 /* Make sure this is laid out, for ease of use later. In the
1492 presence of parse errors, the normal was of assuring this
1493 might not ever get executed, so we lay it out *immediately*. */
1494 build_pointer_type (t);
1496 else
1497 /* We use TYPE_ALIAS_SET for the CLASSTYPE_MARKED bits. But,
1498 TYPE_ALIAS_SET is initialized to -1 by default, so we must
1499 clear it here. */
1500 TYPE_ALIAS_SET (t) = 0;
1502 /* We need to allocate a TYPE_BINFO even for TEMPLATE_TYPE_PARMs
1503 since they can be virtual base types, and we then need a
1504 canonical binfo for them. Ideally, this would be done lazily for
1505 all types. */
1506 if (IS_AGGR_TYPE_CODE (code) || code == TEMPLATE_TYPE_PARM
1507 || code == BOUND_TEMPLATE_TEMPLATE_PARM
1508 || code == TYPENAME_TYPE)
1509 TYPE_BINFO (t) = make_binfo (size_zero_node, t, NULL_TREE, NULL_TREE);
1511 return t;
1514 tree
1515 make_aggr_type (code)
1516 enum tree_code code;
1518 tree t = cxx_make_type (code);
1520 if (IS_AGGR_TYPE_CODE (code))
1521 SET_IS_AGGR_TYPE (t, 1);
1523 return t;
1526 /* Return the type-qualifier corresponding to the identifier given by
1527 RID. */
1530 cp_type_qual_from_rid (rid)
1531 tree rid;
1533 if (rid == ridpointers[(int) RID_CONST])
1534 return TYPE_QUAL_CONST;
1535 else if (rid == ridpointers[(int) RID_VOLATILE])
1536 return TYPE_QUAL_VOLATILE;
1537 else if (rid == ridpointers[(int) RID_RESTRICT])
1538 return TYPE_QUAL_RESTRICT;
1540 abort ();
1541 return TYPE_UNQUALIFIED;